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