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