update link to upper-constraints.txt
[optf/osdf.git] / solverapp.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 2020 AT&T Intellectual Property
3 #
4 #   Licensed under the Apache License, Version 2.0 (the "License");
5 #   you may not use this file except in compliance with the License.
6 #   You may obtain a copy of the License at
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #   Unless required by applicable law or agreed to in writing, software
11 #   distributed under the License is distributed on an "AS IS" BASIS,
12 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #   See the License for the specific language governing permissions and
14 #   limitations under the License.
15 #
16 # -------------------------------------------------------------------------
17 #
18
19 from flask import request
20 from markupsafe import Markup
21
22 from osdf.apps.baseapp import app, run_app
23 from osdf.logging.osdf_logging import audit_log
24 from osdf.webapp.appcontroller import auth_basic
25 from runtime.model_api import create_model_data, retrieve_model_data, retrieve_all_models, delete_model_data
26 from runtime.models.api.model_request import OptimModelRequestAPI
27 from runtime.optim_engine import process_request
28
29
30 @app.route("/api/oof/optengine/v1", methods=["POST"])
31 @auth_basic.login_required
32 def opt_engine_rest_api():
33     """Perform OptimEngine optimization after validating the request
34     """
35     request_json = request.get_json()
36     return process_request(request_json)
37
38
39 @app.route("/api/oof/optmodel/v1", methods=["PUT", "POST"])
40 @auth_basic.login_required
41 def opt_model_create_rest_api():
42     """Perform OptimEngine optimization after validating the request
43     """
44     request_json = request.get_json()
45     OptimModelRequestAPI(request_json).validate()
46     return create_model_data(request_json)
47
48
49 @app.route("/api/oof/optmodel/v1/<model_id>", methods=["GET"])
50 @auth_basic.login_required
51 def opt_get_model_rest_api(model_id):
52     """Retrieve model data
53     """
54     model_id = Markup.escape(model_id)
55     return retrieve_model_data(model_id)
56
57
58 @app.route("/api/oof/optmodel/v1", methods=["GET"])
59 @auth_basic.login_required
60 def opt_get_all_models_rest_api():
61     """Retrieve all models data
62     """
63     return retrieve_all_models()
64
65
66 @app.route("/api/oof/optmodel/v1/<model_id>", methods=["DELETE"])
67 @auth_basic.login_required
68 def opt_delete_model_rest_api(model_id):
69     """Perform OptimEngine optimization after validating the request
70     """
71     return delete_model_data(model_id)
72
73
74 @app.route("/api/oof/optengine/healthcheck/v1", methods=["GET"])
75 def do_health_check():
76     """Simple health check"""
77     audit_log.info("A OptimEngine health check v1 request is processed!")
78     return "OK"
79
80
81 if __name__ == "__main__":
82     run_app()