|
Packit Service |
8a4b7a |
################################################################################
|
|
Packit Service |
8a4b7a |
# BSD LICENSE
|
|
Packit Service |
8a4b7a |
#
|
|
Packit Service |
8a4b7a |
# Copyright(c) 2019-2020 Intel Corporation. All rights reserved.
|
|
Packit Service |
8a4b7a |
# All rights reserved.
|
|
Packit Service |
8a4b7a |
#
|
|
Packit Service |
8a4b7a |
# Redistribution and use in source and binary forms, with or without
|
|
Packit Service |
8a4b7a |
# modification, are permitted provided that the following conditions
|
|
Packit Service |
8a4b7a |
# are met:
|
|
Packit Service |
8a4b7a |
#
|
|
Packit Service |
8a4b7a |
# * Redistributions of source code must retain the above copyright
|
|
Packit Service |
8a4b7a |
# notice, this list of conditions and the following disclaimer.
|
|
Packit Service |
8a4b7a |
# * Redistributions in binary form must reproduce the above copyright
|
|
Packit Service |
8a4b7a |
# notice, this list of conditions and the following disclaimer in
|
|
Packit Service |
8a4b7a |
# the documentation and/or other materials provided with the
|
|
Packit Service |
8a4b7a |
# distribution.
|
|
Packit Service |
8a4b7a |
# * Neither the name of Intel Corporation nor the names of its
|
|
Packit Service |
8a4b7a |
# contributors may be used to endorse or promote products derived
|
|
Packit Service |
8a4b7a |
# from this software without specific prior written permission.
|
|
Packit Service |
8a4b7a |
#
|
|
Packit Service |
8a4b7a |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
Packit Service |
8a4b7a |
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
Packit Service |
8a4b7a |
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
Packit Service |
8a4b7a |
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
Packit Service |
8a4b7a |
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
Packit Service |
8a4b7a |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
Packit Service |
8a4b7a |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
Packit Service |
8a4b7a |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
Packit Service |
8a4b7a |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
Packit Service |
8a4b7a |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
Packit Service |
8a4b7a |
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
Packit Service |
8a4b7a |
################################################################################
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
"""
|
|
Packit Service |
8a4b7a |
REST API module
|
|
Packit Service |
8a4b7a |
Stats, Caps, etc.
|
|
Packit Service |
8a4b7a |
"""
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
from flask_restful import Resource, request
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
import jsonschema
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
import caps
|
|
Packit Service |
8a4b7a |
import common
|
|
Packit Service |
8a4b7a |
import power
|
|
Packit Service |
8a4b7a |
import sstbf
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
from rest.rest_exceptions import BadRequest, InternalError
|
|
Packit Service |
8a4b7a |
from rest.rest_auth import auth
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
from config import ConfigStore
|
|
Packit Service |
8a4b7a |
from stats import StatsStore
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
class Stats(Resource):
|
|
Packit Service |
8a4b7a |
"""
|
|
Packit Service |
8a4b7a |
Handles /stats HTTP requests
|
|
Packit Service |
8a4b7a |
"""
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
@staticmethod
|
|
Packit Service |
8a4b7a |
@auth.login_required
|
|
Packit Service |
8a4b7a |
def get():
|
|
Packit Service |
8a4b7a |
"""
|
|
Packit Service |
8a4b7a |
Handles HTTP GET /stats request.
|
|
Packit Service |
8a4b7a |
Retrieve general stats
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
Returns:
|
|
Packit Service |
8a4b7a |
response, status code
|
|
Packit Service |
8a4b7a |
"""
|
|
Packit Service |
8a4b7a |
res = {
|
|
Packit Service |
8a4b7a |
'num_apps_moves': \
|
|
Packit Service |
8a4b7a |
common.STATS_STORE.general_stats_get(StatsStore.General.NUM_APPS_MOVES),
|
|
Packit Service |
8a4b7a |
'num_err': common.STATS_STORE.general_stats_get(StatsStore.General.NUM_ERR)
|
|
Packit Service |
8a4b7a |
}
|
|
Packit Service |
8a4b7a |
return res, 200
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
class Caps(Resource):
|
|
Packit Service |
8a4b7a |
"""
|
|
Packit Service |
8a4b7a |
Handles /caps HTTP requests
|
|
Packit Service |
8a4b7a |
"""
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
@staticmethod
|
|
Packit Service |
8a4b7a |
@auth.login_required
|
|
Packit Service |
8a4b7a |
def get():
|
|
Packit Service |
8a4b7a |
"""
|
|
Packit Service |
8a4b7a |
Handles HTTP GET /caps request.
|
|
Packit Service |
8a4b7a |
Retrieve capabilities
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
Returns:
|
|
Packit Service |
8a4b7a |
response, status code
|
|
Packit Service |
8a4b7a |
"""
|
|
Packit Service |
8a4b7a |
res = {'capabilities': caps.SYSTEM_CAPS}
|
|
Packit Service |
8a4b7a |
return res, 200
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
class Sstbf(Resource):
|
|
Packit Service |
8a4b7a |
"""
|
|
Packit Service |
8a4b7a |
Handles /caps/sstbf HTTP requests
|
|
Packit Service |
8a4b7a |
"""
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
@staticmethod
|
|
Packit Service |
8a4b7a |
@auth.login_required
|
|
Packit Service |
8a4b7a |
def get():
|
|
Packit Service |
8a4b7a |
"""
|
|
Packit Service |
8a4b7a |
Handles HTTP GET /caps/sstbf request.
|
|
Packit Service |
8a4b7a |
Retrieve SST-BF capabilities details
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
Returns:
|
|
Packit Service |
8a4b7a |
response, status code
|
|
Packit Service |
8a4b7a |
"""
|
|
Packit Service |
8a4b7a |
res = {
|
|
Packit Service |
8a4b7a |
'configured': sstbf.is_sstbf_configured(),
|
|
Packit Service |
8a4b7a |
'hp_cores': sstbf.get_hp_cores(),
|
|
Packit Service |
8a4b7a |
'std_cores': sstbf.get_std_cores()
|
|
Packit Service |
8a4b7a |
}
|
|
Packit Service |
8a4b7a |
return res, 200
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
@staticmethod
|
|
Packit Service |
8a4b7a |
@auth.login_required
|
|
Packit Service |
8a4b7a |
def put():
|
|
Packit Service |
8a4b7a |
"""
|
|
Packit Service |
8a4b7a |
Handles HTTP PUT /caps/sstbf request.
|
|
Packit Service |
8a4b7a |
Raises BadRequest, InternalError
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
Returns:
|
|
Packit Service |
8a4b7a |
response, status code
|
|
Packit Service |
8a4b7a |
"""
|
|
Packit Service |
8a4b7a |
json_data = request.get_json()
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
# validate app schema
|
|
Packit Service |
8a4b7a |
try:
|
|
Packit Service |
8a4b7a |
schema, resolver = ConfigStore.load_json_schema('modify_sstbf.json')
|
|
Packit Service |
8a4b7a |
jsonschema.validate(json_data, schema, resolver=resolver)
|
|
Packit Service |
8a4b7a |
except jsonschema.ValidationError as error:
|
|
Packit Service |
8a4b7a |
raise BadRequest("Request validation failed - %s" % (str(error)))
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
if not sstbf.configure_sstbf(json_data['configured']) == 0:
|
|
Packit Service |
8a4b7a |
raise InternalError("Failed to change SST-BF configured state.")
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
# update power profile configuration
|
|
Packit Service |
8a4b7a |
power.configure_power()
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
res = {'message': "SST-BF caps modified"}
|
|
Packit Service |
8a4b7a |
return res, 200
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
class Reset(Resource):
|
|
Packit Service |
8a4b7a |
"""
|
|
Packit Service |
8a4b7a |
Handles /reset HTTP requests
|
|
Packit Service |
8a4b7a |
"""
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
@staticmethod
|
|
Packit Service |
8a4b7a |
@auth.login_required
|
|
Packit Service |
8a4b7a |
def post():
|
|
Packit Service |
8a4b7a |
"""
|
|
Packit Service |
8a4b7a |
Handles HTTP POST /reset request.
|
|
Packit Service |
8a4b7a |
Resets configuration, reloads config file
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
Returns:
|
|
Packit Service |
8a4b7a |
response, status code
|
|
Packit Service |
8a4b7a |
"""
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
common.CONFIG_STORE.reset()
|
|
Packit Service |
8a4b7a |
|
|
Packit Service |
8a4b7a |
res = {'message': "Reset performed. Configuration reloaded."}
|
|
Packit Service |
8a4b7a |
return res, 200
|