1 # -------------------------------------------------------------------------
2 # Copyright (c) 2018 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 # -------------------------------------------------------------------------
21 from requests import RequestException
23 from osdf.logging.osdf_logging import metrics_log, MH, error_log
24 from osdf.operation.error_handling import build_json_error_body
25 from osdf.utils.interfaces import get_rest_client
26 from .configdb import request as config_request
27 from .solver.optimizer import pci_optimize as optimize
28 from .solver.pci_utils import get_cell_id, get_pci_value
29 from osdf.utils.mdc_utils import mdc_from_json
32 This application generates PCI Optimization API calls using the information received from PCI-Handler-MS, SDN-C
37 def process_pci_optimation(request_json, osdf_config, flat_policies):
39 Process a PCI request from a Client (build config-db, policy and API call, make the call, return result)
40 :param req_object: Request parameters from the client
41 :param osdf_config: Configuration specific to OSDF application (core + deployment)
42 :param flat_policies: policies related to pci (fetched based on request)
43 :return: response from PCI Opt
46 mdc_from_json(request_json)
47 rc = get_rest_client(request_json, service="pcih")
48 req_id = request_json["requestInfo"]["requestId"]
49 cell_info_list, network_cell_info = config_request(request_json, osdf_config, flat_policies)
50 pci_response = get_solutions(cell_info_list, network_cell_info, request_json)
52 metrics_log.info(MH.inside_worker_thread(req_id))
53 except Exception as err:
54 error_log.error("Error for {} {}".format(req_id, traceback.format_exc()))
57 body = build_json_error_body(err)
58 metrics_log.info(MH.sending_response(req_id, "ERROR"))
59 rc.request(json=body, noresponse=True)
60 except RequestException as err:
61 MDC.put('requestID',req_id)
62 error_log.error("Error sending asynchronous notification for {} {}".format(req_id, traceback.format_exc()))
67 metrics_log.info(MH.calling_back_with_body(req_id, rc.url, pci_response))
68 rc.request(json=pci_response, noresponse=True)
69 except RequestException: # can't do much here but log it and move on
70 error_log.error("Error sending asynchronous notification for {} {}".format(req_id, traceback.format_exc()))
73 def get_solutions(cell_info_list, network_cell_info, request_json):
74 status, pci_solutions, anr_solutions = build_solution_list(cell_info_list, network_cell_info, request_json)
76 "transactionId": request_json['requestInfo']['transactionId'],
77 "requestId": request_json["requestInfo"]["requestId"],
78 "requestStatus": "completed",
79 "statusMessage": status,
81 'networkId': request_json['cellInfo']['networkId'],
82 'pciSolutions': pci_solutions,
83 'anrSolutions': anr_solutions
88 def build_solution_list(cell_info_list, network_cell_info, request_json):
90 req_id = request_json["requestInfo"]["requestId"]
92 opt_solution = optimize(network_cell_info, cell_info_list, request_json)
93 pci_solutions = build_pci_solution(network_cell_info, opt_solution['pci'])
94 anr_solutions = build_anr_solution(network_cell_info, opt_solution.get('removables', {}))
96 error_log.error("Failed finding solution for {} {}".format(req_id, traceback.format_exc()))
98 return status, pci_solutions, anr_solutions
101 def build_pci_solution(network_cell_info, pci_solution):
103 for k, v in pci_solution.items():
104 old_pci = get_pci_value(network_cell_info, k)
107 'cellId': get_cell_id(network_cell_info, k),
110 pci_solutions.append(response)
114 def build_anr_solution(network_cell_info, removables):
116 for k, v in removables.items():
118 'cellId': get_cell_id(network_cell_info, k),
119 'removeableNeighbors': list(map(lambda x: get_cell_id(network_cell_info, x), v))
121 anr_solutions.append(response)