From f90e03c6a749ee25df3ac74355981861d59160d0 Mon Sep 17 00:00:00 2001 From: krishnaa96 Date: Tue, 20 Jul 2021 11:44:26 +0530 Subject: Add cps client to PCI app - Add CPS client - Add configuration to switch between CPS and config DB Issue-ID: OPTFRA-978 Signed-off-by: krishnaa96 Change-Id: Ibce259123864a16e5f9e3b8e53563151d8036a88 --- apps/pci/optimizers/__init__.py | 2 + apps/pci/optimizers/config/__init__.py | 0 apps/pci/optimizers/config/config_client.py | 37 +++++++++++++++ apps/pci/optimizers/config/configdb.py | 51 ++++++++++++++++++++ apps/pci/optimizers/config/cps.py | 72 +++++++++++++++++++++++++++++ apps/pci/optimizers/config_request.py | 55 ++++++++++++++++++++++ apps/pci/optimizers/configdb.py | 69 --------------------------- apps/pci/optimizers/pci_opt_processor.py | 27 ++++++----- 8 files changed, 232 insertions(+), 81 deletions(-) create mode 100644 apps/pci/optimizers/config/__init__.py create mode 100644 apps/pci/optimizers/config/config_client.py create mode 100644 apps/pci/optimizers/config/configdb.py create mode 100644 apps/pci/optimizers/config/cps.py create mode 100644 apps/pci/optimizers/config_request.py delete mode 100644 apps/pci/optimizers/configdb.py (limited to 'apps') diff --git a/apps/pci/optimizers/__init__.py b/apps/pci/optimizers/__init__.py index e69de29..35bc5a0 100644 --- a/apps/pci/optimizers/__init__.py +++ b/apps/pci/optimizers/__init__.py @@ -0,0 +1,2 @@ +from apps.pci.optimizers.config import configdb +from apps.pci.optimizers.config import cps diff --git a/apps/pci/optimizers/config/__init__.py b/apps/pci/optimizers/config/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/pci/optimizers/config/config_client.py b/apps/pci/optimizers/config/config_client.py new file mode 100644 index 0000000..7e5a737 --- /dev/null +++ b/apps/pci/optimizers/config/config_client.py @@ -0,0 +1,37 @@ +# ------------------------------------------------------------------------- +# Copyright (C) 2021 Wipro Limited. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# ------------------------------------------------------------------------- +# + + +class ConfigClient(object): + + subclasses = {} + + @classmethod + def register_subclass(cls, type): + def decorator(subclass): + cls.subclasses[type] = subclass + return subclass + + return decorator + + @classmethod + def create(cls, type): + if type not in cls.subclasses: + raise ValueError('Bad config client type {}'.format(type)) + + return cls.subclasses[type]() diff --git a/apps/pci/optimizers/config/configdb.py b/apps/pci/optimizers/config/configdb.py new file mode 100644 index 0000000..cfc7ce1 --- /dev/null +++ b/apps/pci/optimizers/config/configdb.py @@ -0,0 +1,51 @@ +# ------------------------------------------------------------------------- +# Copyright (C) 2021 Wipro Limited. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# ------------------------------------------------------------------------- +# + +from datetime import datetime as dt +import uuid + +from apps.pci.optimizers.config.config_client import ConfigClient +from osdf.config.base import osdf_config +from osdf.logging.osdf_logging import debug_log +from osdf.utils.interfaces import RestClient + + +@ConfigClient.register_subclass('configdb') +class ConfigDb(ConfigClient): + + def __init__(self): + self.config = osdf_config.deployment + uid, passwd = self.config['configDbUserName'], self.config['configDbPassword'] + headers = dict(transaction_id=str(uuid.uuid4())) + self.rc = RestClient(userid=uid, passwd=passwd, method="GET", log_func=debug_log.debug, headers=headers) + + def get_cell_list(self, network_id): + ts = dt.strftime(dt.now(), '%Y-%m-%d %H:%M:%S%z') + cell_list_url = '{}/{}/{}/{}'.format(self.config['configDbUrl'], + self.config['configDbGetCellListUrl'], network_id, ts) + return self.rc.request(raw_response=True, url=cell_list_url).json() + + def get_nbr_list(self, network_id, cell_id): + ts = dt.strftime(dt.now(), '%Y-%m-%d %H:%M:%S%z') + nbr_list_url = '{}/{}/{}/{}'.format(self.config['configDbUrl'], + self.config['configDbGetNbrListUrl'], cell_id, ts) + response = self.rc.request(url=nbr_list_url, raw_response=True).json() + + debug_log.debug("cell_id {} nbr_list {}".format(cell_id, response.get('nbrList'))) + + return response.get('nbrList', []) diff --git a/apps/pci/optimizers/config/cps.py b/apps/pci/optimizers/config/cps.py new file mode 100644 index 0000000..9cf1b1f --- /dev/null +++ b/apps/pci/optimizers/config/cps.py @@ -0,0 +1,72 @@ +# ------------------------------------------------------------------------- +# Copyright (C) 2021 Wipro Limited. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# ------------------------------------------------------------------------- +# + +import json + +from apps.pci.optimizers.config.config_client import ConfigClient +from osdf.config.base import osdf_config +from osdf.logging.osdf_logging import debug_log +from osdf.utils.interfaces import RestClient + + +@ConfigClient.register_subclass('cps') +class Cps(ConfigClient): + + def __init__(self): + self.config = osdf_config.deployment + username, password = self.config['cpsUsername'], self.config['cpsPassword'] + headers = { + "Content-Type": "application/json", + "Accept": "application/json" + } + self.rc = RestClient(userid=username, passwd=password, method="POST", + log_func=debug_log.debug, headers=headers) + + def get_cell_list(self, network_id): + cell_list_url = '{}/{}'.format(self.config['cpsUrl'], self.config['cpsCellListUrl']) + data = { + 'inputParameters': { + 'regionId': network_id + } + } + response = self.rc.request(url=cell_list_url, data=json.dumps(data)) + debug_log.debug("cell list response {}".format(response)) + return sorted([x['idNRCellCU'] for x in response.get('NRCellCU')]) + + def get_nbr_list(self, network_id, cell_id): + nbr_list_url = '{}/{}'.format(self.config['cpsUrl'], self.config['cpsNbrListUrl']) + data = { + 'inputParameters': { + 'regionId': network_id, + 'idNRCellCU': cell_id + } + } + response = self.rc.request(url=nbr_list_url, data=json.dumps(data)) + debug_log.debug("nbr list response {}".format(response)) + nbr_list = [] + for cell_relation in response.get('NRCellRelation'): + nbr = { + 'targetCellId': cell_relation['attributes']['nRTCI'], + 'pciValue': int(cell_relation['attributes']['nRPCI']), + 'ho': cell_relation['attributes']['isHOAllowed'] + } + nbr_list.append(nbr) + + debug_log.debug("cell_id {} nbr_list {}".format(cell_id, nbr_list)) + + return nbr_list diff --git a/apps/pci/optimizers/config_request.py b/apps/pci/optimizers/config_request.py new file mode 100644 index 0000000..f62641d --- /dev/null +++ b/apps/pci/optimizers/config_request.py @@ -0,0 +1,55 @@ +# ------------------------------------------------------------------------- +# Copyright (c) 2018 AT&T Intellectual Property +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# ------------------------------------------------------------------------- +# + +from apps.pci.optimizers.config.config_client import ConfigClient + + +def request(req_object, osdf_config, flat_policies): + """Process a configdb request from a Client (build Conductor API call, make the call, return result) + + :param req_object: Request parameters from the client + :param osdf_config: Configuration specific to OSDF application (core + deployment) + :param flat_policies: policies related to PCI Opt (fetched based on request) + :return: response from ConfigDB (accounting for redirects from Conductor service + """ + cell_list_response = {} + + network_id = req_object['cellInfo']['networkId'] + + cell_list_response['network_id'] = network_id + + config = osdf_config.deployment + + config_client = ConfigClient.create(config['configClientType']) + + cell_resp = config_client.get_cell_list(network_id) + + cell_list = [] + count = 0 + for cell_id in cell_resp: + cell_info = { + 'cell_id': cell_id, + 'id': count, + 'nbr_list': config_client.get_nbr_list(network_id, cell_id) + } + cell_list.append(cell_info) + count += 1 + + cell_list_response['cell_list'] = cell_list + + return cell_resp, cell_list_response diff --git a/apps/pci/optimizers/configdb.py b/apps/pci/optimizers/configdb.py deleted file mode 100644 index 8f003c2..0000000 --- a/apps/pci/optimizers/configdb.py +++ /dev/null @@ -1,69 +0,0 @@ -# ------------------------------------------------------------------------- -# Copyright (c) 2018 AT&T Intellectual Property -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# ------------------------------------------------------------------------- -# - -from datetime import datetime as dt - -from osdf.logging.osdf_logging import debug_log -from osdf.utils.interfaces import RestClient - - -def request(req_object, osdf_config, flat_policies): - """ - Process a configdb request from a Client (build Conductor API call, make the call, return result) - :param req_object: Request parameters from the client - :param osdf_config: Configuration specific to OSDF application (core + deployment) - :param flat_policies: policies related to PCI Opt (fetched based on request) - :return: response from ConfigDB (accounting for redirects from Conductor service - """ - cell_list_response = {} - config = osdf_config.deployment - local_config = osdf_config.core - uid, passwd = config['configDbUserName'], config['configDbPassword'] - req_id = req_object['requestInfo']['requestId'] - transaction_id = req_object['requestInfo']['transactionId'] - headers = dict(transaction_id=transaction_id) - - network_id = req_object['cellInfo']['networkId'] - - cell_list_response['network_id'] = network_id - - ts = dt.strftime(dt.now(), '%Y-%m-%d %H:%M:%S%z') - - rc = RestClient(userid=uid, passwd=passwd, method="GET", log_func=debug_log.debug, headers=headers) - - cell_list_url = '{}/{}/{}/{}'.format(config['configDbUrl'], config['configDbGetCellListUrl'], network_id, ts) - - cell_list_resp = rc.request(raw_response=True, url=cell_list_url) - cell_resp = cell_list_resp.json() - - cell_list = [] - count = 0 - for cell_id in cell_resp: - cell_info = {'cell_id': cell_id, 'id': count} - nbr_list_url = '{}/{}/{}/{}'.format(config['configDbUrl'], config['configDbGetNbrListUrl'], cell_id, ts) - nbr_list_raw = rc.request(url=nbr_list_url, raw_response=True) - cell_info['nbr_list'] = get_neighbor_list(nbr_list_raw.json()) - cell_list.append(cell_info) - count += 1 - - cell_list_response['cell_list'] = cell_list - return cell_resp, cell_list_response - - -def get_neighbor_list(nbr_list_response): - return nbr_list_response.get('nbrList', []) diff --git a/apps/pci/optimizers/pci_opt_processor.py b/apps/pci/optimizers/pci_opt_processor.py index 01ae0b4..a58d2f4 100644 --- a/apps/pci/optimizers/pci_opt_processor.py +++ b/apps/pci/optimizers/pci_opt_processor.py @@ -21,12 +21,15 @@ import traceback from onaplogging.mdcContext import MDC from requests import RequestException -from osdf.logging.osdf_logging import metrics_log, MH, error_log +from apps.pci.optimizers.config_request import request as config_request +from apps.pci.optimizers.solver.optimizer import pci_optimize as optimize +from apps.pci.optimizers.solver.pci_utils import get_cell_id +from apps.pci.optimizers.solver.pci_utils import get_pci_value +from osdf.logging.osdf_logging import error_log +from osdf.logging.osdf_logging import metrics_log +from osdf.logging.osdf_logging import MH from osdf.operation.error_handling import build_json_error_body from osdf.utils.interfaces import get_rest_client -from .configdb import request as config_request -from .solver.optimizer import pci_optimize as optimize -from .solver.pci_utils import get_cell_id, get_pci_value from osdf.utils.mdc_utils import mdc_from_json """ @@ -36,8 +39,8 @@ and Policy. def process_pci_optimation(request_json, osdf_config, flat_policies): - """ - Process a PCI request from a Client (build config-db, policy and API call, make the call, return result) + """Process a PCI request from a Client (build config-db, policy and API call, make the call, return result) + :param req_object: Request parameters from the client :param osdf_config: Configuration specific to OSDF application (core + deployment) :param flat_policies: policies related to pci (fetched based on request) @@ -58,14 +61,14 @@ def process_pci_optimation(request_json, osdf_config, flat_policies): body = build_json_error_body(err) metrics_log.info(MH.sending_response(req_id, "ERROR")) rc.request(json=body, noresponse=True) - except RequestException as err: - MDC.put('requestID',req_id) + except RequestException: + MDC.put('requestID', req_id) error_log.error("Error sending asynchronous notification for {} {}".format(req_id, traceback.format_exc())) raise err - try: metrics_log.info(MH.calling_back_with_body(req_id, rc.url, pci_response)) + error_log.error("pci response: {}".format(str(pci_response))) rc.request(json=pci_response, noresponse=True) except RequestException: # can't do much here but log it and move on error_log.error("Error sending asynchronous notification for {} {}".format(req_id, traceback.format_exc())) @@ -89,11 +92,11 @@ def get_solutions(cell_info_list, network_cell_info, request_json): def build_solution_list(cell_info_list, network_cell_info, request_json): status = "success" req_id = request_json["requestInfo"]["requestId"] - pci_solutions =[] - anr_solutions=[] + pci_solutions = [] + anr_solutions = [] try: opt_solution = optimize(network_cell_info, cell_info_list, request_json) - if opt_solution == 'UNSATISFIABLE': + if opt_solution == 'UNSATISFIABLE': status = 'inconsistent input' return status, pci_solutions, anr_solutions else: -- cgit 1.2.3-korg