oof changes for the new sdnr-configdb changes
[optf/osdf.git] / osdf / optimizers / pciopt / pci_opt_processor.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 2018 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 import traceback
20 from requests import RequestException
21
22 from osdf.logging.osdf_logging import metrics_log, MH, error_log
23 from osdf.operation.error_handling import build_json_error_body
24 from osdf.utils.interfaces import get_rest_client
25 from .configdb import request as config_request
26 from .solver.optimizer import pci_optimize as optimize
27 from .solver.pci_utils import get_cell_id, get_pci_value
28
29 """
30 This application generates PCI Optimization API calls using the information received from PCI-Handler-MS, SDN-C
31 and Policy.
32 """
33
34
35 def process_pci_optimation(request_json, osdf_config, flat_policies):
36     """
37     Process a PCI request from a Client (build config-db, policy and  API call, make the call, return result)
38     :param req_object: Request parameters from the client
39     :param osdf_config: Configuration specific to OSDF application (core + deployment)
40     :param flat_policies: policies related to pci (fetched based on request)
41     :return: response from PCI Opt
42     """
43     try:
44         rc = get_rest_client(request_json, service="pcih")
45         req_id = request_json["requestInfo"]["requestId"]
46         cell_info_list, network_cell_info = config_request(request_json, osdf_config, flat_policies)
47         pci_response = get_solutions(cell_info_list, network_cell_info, request_json)
48
49         metrics_log.info(MH.inside_worker_thread(req_id))
50     except Exception as err:
51         error_log.error("Error for {} {}".format(req_id, traceback.format_exc()))
52
53         try:
54             body = build_json_error_body(err)
55             metrics_log.info(MH.sending_response(req_id, "ERROR"))
56             rc.request(json=body, noresponse=True)
57         except RequestException:
58             error_log.error("Error sending asynchronous notification for {} {}".format(req_id, traceback.format_exc()))
59         return
60
61     try:
62         metrics_log.info(MH.calling_back_with_body(req_id, rc.url, pci_response))
63         rc.request(json=pci_response, noresponse=True)
64     except RequestException:  # can't do much here but log it and move on
65         error_log.error("Error sending asynchronous notification for {} {}".format(req_id, traceback.format_exc()))
66
67
68 def get_solutions(cell_info_list, network_cell_info, request_json):
69     return {
70         "transactionId": request_json['requestInfo']['transactionId'],
71         "requestId": request_json["requestInfo"]["requestId"],
72         "requestStatus": "completed",
73         "statusMessage": "success",
74         "solutions": [
75             {
76                 'networkId': request_json['cellInfo']['networkId'],
77                 'pciSolutions': build_solution_list(cell_info_list, network_cell_info, request_json)
78             }
79         ]
80     }
81
82
83 def build_solution_list(cell_info_list, network_cell_info, request_json):
84     solution_list = []
85     # for cell in request_json['cellInfo']['cellIdList']:
86     opt_solution = optimize(network_cell_info, cell_info_list)
87     sol = opt_solution[0]['pci']
88     for k, v in sol.items():
89         response = {
90             'cellId': get_cell_id(network_cell_info, k),
91             'pci': get_pci_value(network_cell_info, v)
92         }
93         solution_list.append(response)
94     return solution_list