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