Add cps client to PCI app 35/122735/1
authorkrishnaa96 <krishna.moorthy6@wipro.com>
Tue, 20 Jul 2021 06:14:26 +0000 (11:44 +0530)
committerkrishnaa96 <krishna.moorthy6@wipro.com>
Tue, 20 Jul 2021 09:08:09 +0000 (14:38 +0530)
- 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:
apps/pci/optimizers/__init__.py
apps/pci/optimizers/config/__init__.py [new file with mode: 0644]
apps/pci/optimizers/config/config_client.py [new file with mode: 0644]
apps/pci/optimizers/config/configdb.py [new file with mode: 0644]
apps/pci/optimizers/config/cps.py [new file with mode: 0644]
apps/pci/optimizers/config_request.py [moved from apps/pci/optimizers/configdb.py with 54% similarity]
apps/pci/optimizers/pci_opt_processor.py
config/osdf_config.yaml
csit/scripts/osdf-properties/osdf_config.yaml
csit/tests/osdf/data/pci-opt-request.json
test/config/osdf_config.yaml
test/configdb/test_configdb_calls.py
test/functest/simulators/configdb/response-payloads/getCellList-netw1000.json [moved from test/functest/simulators/configdb/response-payloads/getCellList-1000.json with 100% similarity]
test/functest/simulators/configdb/response-payloads/getCellList-netw2000.json [moved from test/functest/simulators/configdb/response-payloads/getCellList-2000.json with 100% similarity]
test/functest/simulators/simulated-config/osdf_config.yaml
test/pci-optimization-tests/fixed_pci.json
test/pci-optimization-tests/pci_anr_request.json
test/pci-optimization-tests/request.json
test/test_process_fixed_pci.py
test/test_process_pci_anr_opt.py
test/test_process_pci_opt.py
tox.ini

index e69de29..35bc5a0 100644 (file)
@@ -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 (file)
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 (file)
index 0000000..7e5a737
--- /dev/null
@@ -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 (file)
index 0000000..cfc7ce1
--- /dev/null
@@ -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 (file)
index 0000000..9cf1b1f
--- /dev/null
@@ -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
similarity index 54%
rename from apps/pci/optimizers/configdb.py
rename to apps/pci/optimizers/config_request.py
index 8f003c2..f62641d 100644 (file)
 # -------------------------------------------------------------------------
 #
 
-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
index 01ae0b4..a58d2f4 100644 (file)
@@ -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:
index ee32221..6780d13 100755 (executable)
@@ -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"
index 3ca4cd1..93ea197 100755 (executable)
@@ -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"
index 7ec9ab5..09a2f97 100644 (file)
@@ -12,7 +12,7 @@
     "timeout": 600
   },
   "cellInfo": {
-    "networkId": "1000",
+    "networkId": "netw1000",
     "cellIdList": [
       "cell0"
     ]
index 2b38ddd..05d7c6a 100755 (executable)
@@ -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
index a6f1c97..3393991 100644 (file)
@@ -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
index 2f03d99..eb02b33 100755 (executable)
@@ -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
index 8bd159e..0038d04 100644 (file)
@@ -26,7 +26,7 @@
 
   "cellInfo": {
 
-    "networkId": "2000",
+    "networkId": "netw2000",
 
     "cellIdList": [
 
index e8947b1..34e2409 100644 (file)
@@ -12,7 +12,7 @@
     "timeout": 600
   },
   "cellInfo": {
-    "networkId": "2000",
+    "networkId": "netw2000",
     "cellIdList": [
       "cell20"
     ]
index 517ee10..a8eb4f6 100644 (file)
@@ -12,9 +12,9 @@
     "timeout": 600
   },
   "cellInfo": {
-    "networkId": "1000",
+    "networkId": "netw1000",
     "cellIdList": [
       "cell0"
     ]
   }
-}
\ No newline at end of file
+}
index da68289..3d805c5 100644 (file)
@@ -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)
index b717d32..17c3e6d 100644 (file)
@@ -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)
index ffbc718..d52599d 100644 (file)
@@ -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/tox.ini b/tox.ini
index 6dc85e0..8dc20c9 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -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