take-1 for the NST selection function
[optf/osdf.git] / osdfapp.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 2015-2017 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 """
20 OSDF Manager Main Flask Application
21 """
22
23 import json
24 from threading import Thread  # for scaling up, may need celery with RabbitMQ or redis
25
26 from flask import request, g
27
28 from osdf.apps.baseapp import app, run_app
29 from apps.nst.models.api.nstSelectionRequest import NSTSelectionAPI
30 from apps.pci.models.api.pciOptimizationRequest import PCIOptimizationAPI
31 from apps.nst.optimizers.nst_select_processor import process_nst_selection
32 from apps.pci.optimizers.pci_opt_processor import process_pci_optimation
33 from apps.placement.models.api.placementRequest import PlacementAPI
34 from apps.placement.optimizers.conductor.remote_opt_processor import process_placement_opt
35 from apps.route.optimizers.simple_route_opt import RouteOpt
36 from osdf.adapters.policy.interface import get_policies
37 from osdf.adapters.policy.interface import upload_policy_models
38 from osdf.config.base import osdf_config
39 from osdf.logging.osdf_logging import MH, audit_log
40 from osdf.operation.responses import osdf_response_for_request_accept as req_accept
41 from osdf.utils import api_data_utils
42 from osdf.webapp.appcontroller import auth_basic
43
44
45 @app.route("/api/oof/v1/healthcheck", methods=["GET"])
46 def do_osdf_health_check():
47     """Simple health check"""
48     audit_log.info("A health check request is processed!")
49     return "OK"
50
51
52 @app.route("/api/oof/loadmodels/v1", methods=["GET"])
53 def do_osdf_load_policies():
54     audit_log.info("Uploading policy models")
55     """Upload policy models"""
56     response = upload_policy_models()
57     audit_log.info(response)
58     return "OK"
59
60
61 @app.route("/api/oof/v1/placement", methods=["POST"])
62 @auth_basic.login_required
63 def do_placement_opt():
64     return placement_rest_api()
65
66
67 @app.route("/api/oof/placement/v1", methods=["POST"])
68 @auth_basic.login_required
69 def do_placement_opt_common_versioning():
70     return placement_rest_api()
71
72
73 def placement_rest_api():
74     """Perform placement optimization after validating the request and fetching policies
75     Make a call to the call-back URL with the output of the placement request.
76     Note: Call to Conductor for placement optimization may have redirects, so account for them
77     """
78     request_json = request.get_json()
79     req_id = request_json['requestInfo']['requestId']
80     g.request_id = req_id
81     audit_log.info(MH.received_request(request.url, request.remote_addr, json.dumps(request_json)))
82     api_version_info = api_data_utils.retrieve_version_info(request, req_id)
83     PlacementAPI(request_json).validate()
84     policies = get_policies(request_json, "placement")
85     audit_log.info(MH.new_worker_thread(req_id, "[for placement]"))
86     t = Thread(target=process_placement_opt, args=(request_json, policies, osdf_config))
87     t.start()
88     audit_log.info(MH.accepted_valid_request(req_id, request))
89     return req_accept(request_id=req_id,
90                       transaction_id=request_json['requestInfo']['transactionId'],
91                       version_info=api_version_info, request_status="accepted", status_message="")
92
93
94 @app.route("/api/oof/v1/route", methods=["POST"])
95 def do_route_calc():
96     """
97     Perform the basic route calculations and returnn the vpn-bindings
98     """
99     request_json = request.get_json()
100     audit_log.info("Calculate Route request received!")
101     return RouteOpt().getRoute(request_json)
102
103 @app.route("/api/oof/v1/selection/nst", methods=["POST"])
104 def do_nst_selection():
105     request_json = request.get_json()
106     req_id = request_json['requestInfo']['requestId']
107     NSTSelectionAPI(request_json).validate()
108     response = process_nst_selection(request_json, osdf_config)
109     return response
110
111 @app.route("/api/oof/v1/pci", methods=["POST"])
112 @app.route("/api/oof/pci/v1", methods=["POST"])
113 @auth_basic.login_required
114 def do_pci_optimization():
115     request_json = request.get_json()
116     req_id = request_json['requestInfo']['requestId']
117     g.request_id = req_id
118     audit_log.info(MH.received_request(request.url, request.remote_addr, json.dumps(request_json)))
119     PCIOptimizationAPI(request_json).validate()
120     # disable policy retrieval
121     # policies = get_policies(request_json, "pciopt")
122     audit_log.info(MH.new_worker_thread(req_id, "[for pciopt]"))
123     t = Thread(target=process_pci_optimation, args=(request_json, osdf_config, None))
124     t.start()
125     audit_log.info(MH.accepted_valid_request(req_id, request))
126     return req_accept(request_id=req_id,
127                       transaction_id=request_json['requestInfo']['transactionId'],
128                       request_status="accepted", status_message="")
129
130
131 if __name__ == "__main__":
132     run_app()