diff options
author | krishnaa96 <krishna.moorthy6@wipro.com> | 2021-07-20 11:44:26 +0530 |
---|---|---|
committer | krishnaa96 <krishna.moorthy6@wipro.com> | 2021-07-20 14:38:09 +0530 |
commit | f90e03c6a749ee25df3ac74355981861d59160d0 (patch) | |
tree | 400b9f6b2ef6a07b1dbb4ebeeb58e8c026e00a45 | |
parent | 555e5727d1c818108ff255a1b9fe4542b768ad5c (diff) |
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 <krishna.moorthy6@wipro.com>
Change-Id: Ibce259123864a16e5f9e3b8e53563151d8036a88
22 files changed, 230 insertions, 48 deletions
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 --- /dev/null +++ b/apps/pci/optimizers/config/__init__.py 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/configdb.py b/apps/pci/optimizers/config_request.py index 8f003c2..f62641d 100644 --- a/apps/pci/optimizers/configdb.py +++ b/apps/pci/optimizers/config_request.py @@ -16,54 +16,40 @@ # ------------------------------------------------------------------------- # -from datetime import datetime as dt - -from osdf.logging.osdf_logging import debug_log -from osdf.utils.interfaces import RestClient +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) + """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) + config = osdf_config.deployment - cell_list_url = '{}/{}/{}/{}'.format(config['configDbUrl'], config['configDbGetCellListUrl'], network_id, ts) + config_client = ConfigClient.create(config['configClientType']) - cell_list_resp = rc.request(raw_response=True, url=cell_list_url) - cell_resp = cell_list_resp.json() + 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_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_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 - -def get_neighbor_list(nbr_list_response): - return nbr_list_response.get('nbrList', []) + return cell_resp, cell_list_response 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: diff --git a/config/osdf_config.yaml b/config/osdf_config.yaml index ee32221..6780d13 100755 --- a/config/osdf_config.yaml +++ b/config/osdf_config.yaml @@ -45,10 +45,21 @@ aaf_sms_timeout: 30 secret_domain: osdf aaf_ca_certs: ssl_certs/aaf_root_ca.cer +configClientType: configdb + # config db api configDbUrl: http://config.db.url:8080 configDbGetCellListUrl: 'SDNCConfigDBAPI/getCellList' configDbGetNbrListUrl: 'SDNCConfigDBAPI/getNbrList' +configDbUserName: '' +configDbPassword: '' + +# cps api +cpsUrl: http://cps-tbdmt:8000/execute +cpsCellListUrl: 'ran-network/getCellList' +cpsNbrListUrl: 'ran-network/getNbrList' +cpsUsername: '' +cpsPassword: '' #aai api aaiUrl: "https://aai.url:30233" diff --git a/csit/scripts/osdf-properties/osdf_config.yaml b/csit/scripts/osdf-properties/osdf_config.yaml index 3ca4cd1..93ea197 100755 --- a/csit/scripts/osdf-properties/osdf_config.yaml +++ b/csit/scripts/osdf-properties/osdf_config.yaml @@ -43,11 +43,20 @@ aaf_sms_timeout: 30 secret_domain: osdf aaf_ca_certs: /opt/aaf_root_ca.cer +configClientType: configdb + # config db api configDbUrl: http://127.0.0.1:5000/simulated/configdb configDbGetCellListUrl: 'getCellList' configDbGetNbrListUrl: 'getNbrList' +# cps api +cpsUrl: http://localhost:8080/execute +cpsCellListUrl: 'e2e-cavsta-schemaset/get-cell-list' +cpsNbrListUrl: 'e2e-cavsta-schemaset/get-nbr-list' +cpsUsername: '' +cpsPassword: '' + #aai api aaiUrl: "http://127.0.0.1:5000" aaiGetLinksUrl: "/aai/v16/network/logical-links" diff --git a/csit/tests/osdf/data/pci-opt-request.json b/csit/tests/osdf/data/pci-opt-request.json index 7ec9ab5..09a2f97 100644 --- a/csit/tests/osdf/data/pci-opt-request.json +++ b/csit/tests/osdf/data/pci-opt-request.json @@ -12,7 +12,7 @@ "timeout": 600 }, "cellInfo": { - "networkId": "1000", + "networkId": "netw1000", "cellIdList": [ "cell0" ] diff --git a/test/config/osdf_config.yaml b/test/config/osdf_config.yaml index 2b38ddd..05d7c6a 100755 --- a/test/config/osdf_config.yaml +++ b/test/config/osdf_config.yaml @@ -64,6 +64,15 @@ aaf_ca_certs: ssl_certs/aaf_root_ca.cer pciHMSUsername: "" # pcihandler username for call back. pciHMSPassword: "" # pcihandler password for call back. +configClientType: configdb + +# config db api +configDbUrl: http://127.0.0.1:5000/simulated/configdb +configDbUserName: osdf +configDbPassword: passwd +configDbGetCellListUrl: 'getCellList' +configDbGetNbrListUrl: 'getNbrList' + # Credentials for the OOF PCI Opt service osdfPCIOptUsername: PCI-OSDF-USER osdfPCIOptPassword: PCI-OSDF-PASSWD diff --git a/test/configdb/test_configdb_calls.py b/test/configdb/test_configdb_calls.py index a6f1c97..3393991 100644 --- a/test/configdb/test_configdb_calls.py +++ b/test/configdb/test_configdb_calls.py @@ -16,7 +16,7 @@ # ------------------------------------------------------------------------- # -from apps.pci.optimizers.configdb import request +from apps.pci.optimizers.config_request import request import osdf.config.loader as config_loader from osdf.utils.interfaces import json_from_file from osdf.utils.programming_utils import DotDict diff --git a/test/functest/simulators/configdb/response-payloads/getCellList-1000.json b/test/functest/simulators/configdb/response-payloads/getCellList-netw1000.json index df23f6e..df23f6e 100644 --- a/test/functest/simulators/configdb/response-payloads/getCellList-1000.json +++ b/test/functest/simulators/configdb/response-payloads/getCellList-netw1000.json diff --git a/test/functest/simulators/configdb/response-payloads/getCellList-2000.json b/test/functest/simulators/configdb/response-payloads/getCellList-netw2000.json index 77cf7db..77cf7db 100644 --- a/test/functest/simulators/configdb/response-payloads/getCellList-2000.json +++ b/test/functest/simulators/configdb/response-payloads/getCellList-netw2000.json diff --git a/test/functest/simulators/simulated-config/osdf_config.yaml b/test/functest/simulators/simulated-config/osdf_config.yaml index 2f03d99..eb02b33 100755 --- a/test/functest/simulators/simulated-config/osdf_config.yaml +++ b/test/functest/simulators/simulated-config/osdf_config.yaml @@ -60,6 +60,8 @@ aaf_sms_timeout: 30 secret_domain: osdf aaf_ca_certs: ssl_certs/aaf_root_ca.cer +configClientType: configdb + # config db api configDbUrl: http://127.0.0.1:5000/simulated/configdb configDbUserName: osdf diff --git a/test/pci-optimization-tests/fixed_pci.json b/test/pci-optimization-tests/fixed_pci.json index 8bd159e..0038d04 100644 --- a/test/pci-optimization-tests/fixed_pci.json +++ b/test/pci-optimization-tests/fixed_pci.json @@ -26,7 +26,7 @@ "cellInfo": { - "networkId": "2000", + "networkId": "netw2000", "cellIdList": [ diff --git a/test/pci-optimization-tests/pci_anr_request.json b/test/pci-optimization-tests/pci_anr_request.json index e8947b1..34e2409 100644 --- a/test/pci-optimization-tests/pci_anr_request.json +++ b/test/pci-optimization-tests/pci_anr_request.json @@ -12,7 +12,7 @@ "timeout": 600 }, "cellInfo": { - "networkId": "2000", + "networkId": "netw2000", "cellIdList": [ "cell20" ] diff --git a/test/pci-optimization-tests/request.json b/test/pci-optimization-tests/request.json index 517ee10..a8eb4f6 100644 --- a/test/pci-optimization-tests/request.json +++ b/test/pci-optimization-tests/request.json @@ -12,9 +12,9 @@ "timeout": 600 }, "cellInfo": { - "networkId": "1000", + "networkId": "netw1000", "cellIdList": [ "cell0" ] } -}
\ No newline at end of file +} diff --git a/test/test_process_fixed_pci.py b/test/test_process_fixed_pci.py index da68289..3d805c5 100644 --- a/test/test_process_fixed_pci.py +++ b/test/test_process_fixed_pci.py @@ -31,7 +31,7 @@ class TestProcessPlacementOpt(unittest.TestCase): def setUp(self): mock_req_accept_message = Response("Accepted Request", content_type='application/json; charset=utf-8') - self.patcher_req = patch('apps.pci.optimizers.configdb.request', + self.patcher_req = patch('apps.pci.optimizers.config_request.request', return_value={"solutionInfo": {"placementInfo": "dummy"}}) self.patcher_req_accept = patch('osdf.operation.responses.osdf_response_for_request_accept', return_value=mock_req_accept_message) diff --git a/test/test_process_pci_anr_opt.py b/test/test_process_pci_anr_opt.py index b717d32..17c3e6d 100644 --- a/test/test_process_pci_anr_opt.py +++ b/test/test_process_pci_anr_opt.py @@ -32,7 +32,7 @@ class TestProcessPlacementOpt(unittest.TestCase): def setUp(self): mock_req_accept_message = Response("Accepted Request", content_type='application/json; charset=utf-8') - self.patcher_req = patch('apps.pci.optimizers.configdb.request', + self.patcher_req = patch('apps.pci.optimizers.config_request.request', return_value={"solutionInfo": {"placementInfo": "dummy"}}) self.patcher_req_accept = patch('osdf.operation.responses.osdf_response_for_request_accept', return_value=mock_req_accept_message) diff --git a/test/test_process_pci_opt.py b/test/test_process_pci_opt.py index ffbc718..d52599d 100644 --- a/test/test_process_pci_opt.py +++ b/test/test_process_pci_opt.py @@ -31,7 +31,7 @@ class TestProcessPlacementOpt(unittest.TestCase): def setUp(self): mock_req_accept_message = Response("Accepted Request", content_type='application/json; charset=utf-8') - self.patcher_req = patch('apps.pci.optimizers.configdb.request', + self.patcher_req = patch('apps.pci.optimizers.config_request.request', return_value={"solutionInfo": {"placementInfo": "dummy"}}) self.patcher_req_accept = patch('osdf.operation.responses.osdf_response_for_request_accept', return_value=mock_req_accept_message) @@ -43,4 +43,4 @@ commands = select = E,H,W,F max-line-length = 119 ignore = W503 #conflict with W504 -per-file-ignores= +per-file-ignores= apps/pci/optimizers/__init__.py:F401 |