From 411af2e5804302a45c911556a60fe702227fa799 Mon Sep 17 00:00:00 2001 From: Gabriel Cuba Date: Fri, 6 Jan 2023 17:23:22 -0500 Subject: [PATCH] Enables pylint in LCM and deletes unused methods Change-Id: I09f92fbbcf6270dfda58d2016e2a9804b4e032f5 Signed-off-by: Gabriel Cuba --- osm_lcm/ROclient.py | 8 +- osm_lcm/data_utils/database/vim_account.py | 14 +- osm_lcm/data_utils/database/wim_account.py | 35 +-- osm_lcm/lcm.py | 4 +- osm_lcm/ns.py | 314 ++------------------- osm_lcm/osm_config.py | 1 + osm_lcm/vim_sdn.py | 12 +- tox.ini | 2 +- 8 files changed, 73 insertions(+), 317 deletions(-) diff --git a/osm_lcm/ROclient.py b/osm_lcm/ROclient.py index e3cb7f7..8d6f510 100644 --- a/osm_lcm/ROclient.py +++ b/osm_lcm/ROclient.py @@ -1268,7 +1268,7 @@ class ROClient: """ if isinstance(descriptor, str): - descriptor = self.parse(descriptor, descriptor_format) + descriptor = self._parse(descriptor, descriptor_format) elif descriptor: pass elif kwargs: @@ -1311,7 +1311,7 @@ class ROClient: """ if isinstance(descriptor, str): - descriptor = self.parse(descriptor, descriptor_format) + descriptor = self._parse(descriptor, descriptor_format) elif descriptor: pass elif kwargs: @@ -1363,7 +1363,7 @@ class ROClient: if all_tenants: tenant_text = "/any" else: - tenant_text = "/" + self._get_tenant() + tenant_text = "/" + self._get_tenant(session) if "datacenter_id" in kwargs or "datacenter_name" in kwargs: datacenter = self._get_item_uuid( @@ -1373,7 +1373,7 @@ class ROClient: all_tenants=all_tenants, ) else: - datacenter = self.get_datacenter(session) + datacenter = self._get_datacenter(session) if action == "list": url = "{}{}/vim/{}/{}".format(self.uri, tenant_text, datacenter, item) diff --git a/osm_lcm/data_utils/database/vim_account.py b/osm_lcm/data_utils/database/vim_account.py index 5c61073..215dee4 100644 --- a/osm_lcm/data_utils/database/vim_account.py +++ b/osm_lcm/data_utils/database/vim_account.py @@ -29,10 +29,12 @@ from osm_lcm.data_utils.database.database import Database class VimAccountDB: db = None - def get_vim_account_with_id(vim_account_id): - if not VimAccountDB.db: - VimAccountDB.initialize_db() - return VimAccountDB.db.get_one("vim_accounts", {"_id": vim_account_id}) or {} + @classmethod + def get_vim_account_with_id(cls, vim_account_id): + if not cls.db: + cls.initialize_db() + return cls.db.get_one("vim_accounts", {"_id": vim_account_id}) or {} - def initialize_db(): - VimAccountDB.db = Database().instance.db + @classmethod + def initialize_db(cls): + cls.db = Database().instance.db diff --git a/osm_lcm/data_utils/database/wim_account.py b/osm_lcm/data_utils/database/wim_account.py index 8b0b5f6..a1f1799 100644 --- a/osm_lcm/data_utils/database/wim_account.py +++ b/osm_lcm/data_utils/database/wim_account.py @@ -28,21 +28,24 @@ class WimAccountDB: db = None db_wims = {} - def initialize_db(): - WimAccountDB.db = Database().instance.db - - def get_wim_account_with_id(wim_account_id): - if not WimAccountDB.db: - WimAccountDB.initialize_db() - if wim_account_id in WimAccountDB.db_wims: - return WimAccountDB.db_wims[wim_account_id] - db_wim = WimAccountDB.db.get_one("wim_accounts", {"_id": wim_account_id}) or {} - WimAccountDB.db_wims[wim_account_id] = db_wim + @classmethod + def initialize_db(cls): + cls.db = Database().instance.db + + @classmethod + def get_wim_account_with_id(cls, wim_account_id): + if not cls.db: + cls.initialize_db() + if wim_account_id in cls.db_wims: + return cls.db_wims[wim_account_id] + db_wim = cls.db.get_one("wim_accounts", {"_id": wim_account_id}) or {} + cls.db_wims[wim_account_id] = db_wim return db_wim - def get_all_wim_accounts(): - if not WimAccountDB.db: - WimAccountDB.initialize_db() - db_wims_list = WimAccountDB.db.get_list("wim_accounts") - WimAccountDB.db_wims.update({db_wim["_id"]: db_wim for db_wim in db_wims_list}) - return WimAccountDB.db_wims + @classmethod + def get_all_wim_accounts(cls): + if not cls.db: + cls.initialize_db() + db_wims_list = cls.db.get_list("wim_accounts") + cls.db_wims.update({db_wim["_id"]: db_wim for db_wim in db_wims_list}) + return cls.db_wims diff --git a/osm_lcm/lcm.py b/osm_lcm/lcm.py index 5638943..e6c4e19 100644 --- a/osm_lcm/lcm.py +++ b/osm_lcm/lcm.py @@ -480,7 +480,7 @@ class Lcm: db_nsr["config-status"], db_nsr["detailed-status"], db_nsr["_admin"]["deployed"], - self.lcm_ns_tasks.get(nsr_id), + self.lcm_tasks.task_registry["ns"].get(nsr_id, ""), ) ) except Exception as e: @@ -542,7 +542,7 @@ class Lcm: db_nsir["config-status"], db_nsir["detailed-status"], db_nsir["_admin"]["deployed"], - self.lcm_netslice_tasks.get(nsir_id), + self.lcm_tasks.task_registry["nsi"].get(nsir_id, ""), ) ) except Exception as e: diff --git a/osm_lcm/ns.py b/osm_lcm/ns.py index b7df0b6..5d960c5 100644 --- a/osm_lcm/ns.py +++ b/osm_lcm/ns.py @@ -1378,7 +1378,6 @@ class NsLcm(LcmBase): } desc = await self.RO.deploy(nsr_id, target) action_id = desc["action_id"] - db_nsr_update["_admin.deployed.RO.nsr_delete_action_id"] = action_id db_nsr_update["_admin.deployed.RO.nsr_status"] = "DELETING" self.logger.debug( logging_text @@ -1396,20 +1395,17 @@ class NsLcm(LcmBase): stage, operation="termination", ) - - db_nsr_update["_admin.deployed.RO.nsr_delete_action_id"] = None db_nsr_update["_admin.deployed.RO.nsr_status"] = "DELETED" # delete all nsr await self.RO.delete(nsr_id) - except Exception as e: - if isinstance(e, NgRoException) and e.http_code == 404: # not found + except NgRoException as e: + if e.http_code == 404: # not found db_nsr_update["_admin.deployed.RO.nsr_id"] = None db_nsr_update["_admin.deployed.RO.nsr_status"] = "DELETED" - db_nsr_update["_admin.deployed.RO.nsr_delete_action_id"] = None self.logger.debug( logging_text + "RO_action_id={} already deleted".format(action_id) ) - elif isinstance(e, NgRoException) and e.http_code == 409: # conflict + elif e.http_code == 409: # conflict failed_detail.append("delete conflict: {}".format(e)) self.logger.debug( logging_text @@ -1421,6 +1417,11 @@ class NsLcm(LcmBase): logging_text + "RO_action_id={} delete error: {}".format(action_id, e) ) + except Exception as e: + failed_detail.append("delete error: {}".format(e)) + self.logger.error( + logging_text + "RO_action_id={} delete error: {}".format(action_id, e) + ) if failed_detail: stage[2] = "Error deleting from VIM" @@ -1562,9 +1563,7 @@ class NsLcm(LcmBase): """ self.logger.debug(logging_text + "Starting wait_vm_up_insert_key_ro") - ro_nsr_id = None ip_address = None - nb_tries = 0 target_vdu_id = None ro_retries = 0 @@ -1652,74 +1651,24 @@ class NsLcm(LcmBase): self.logger.error(logging_text + "Cannot inject ssh-ky to a PDU") return ip_address try: - ro_vm_id = "{}-{}".format( - db_vnfr["member-vnf-index-ref"], target_vdu_id - ) # TODO add vdu_index - if self.ro_config.ng: - target = { - "action": { - "action": "inject_ssh_key", - "key": pub_key, - "user": user, - }, - "vnf": [{"_id": vnfr_id, "vdur": [{"id": vdur["id"]}]}], - } - desc = await self.RO.deploy(nsr_id, target) - action_id = desc["action_id"] - await self._wait_ng_ro( - nsr_id, action_id, timeout=600, operation="instantiation" - ) - break - else: - # wait until NS is deployed at RO - if not ro_nsr_id: - db_nsrs = self.db.get_one("nsrs", {"_id": nsr_id}) - ro_nsr_id = deep_get( - db_nsrs, ("_admin", "deployed", "RO", "nsr_id") - ) - if not ro_nsr_id: - continue - result_dict = await self.RO.create_action( - item="ns", - item_id_name=ro_nsr_id, - descriptor={ - "add_public_key": pub_key, - "vms": [ro_vm_id], - "user": user, - }, - ) - # result_dict contains the format {VM-id: {vim_result: 200, description: text}} - if not result_dict or not isinstance(result_dict, dict): - raise LcmException( - "Unknown response from RO when injecting key" - ) - for result in result_dict.values(): - if result.get("vim_result") == 200: - break - else: - raise ROclient.ROClientException( - "error injecting key: {}".format( - result.get("description") - ) - ) - break + target = { + "action": { + "action": "inject_ssh_key", + "key": pub_key, + "user": user, + }, + "vnf": [{"_id": vnfr_id, "vdur": [{"id": vdur["id"]}]}], + } + desc = await self.RO.deploy(nsr_id, target) + action_id = desc["action_id"] + await self._wait_ng_ro( + nsr_id, action_id, timeout=600, operation="instantiation" + ) + break except NgRoException as e: raise LcmException( "Reaching max tries injecting key. Error: {}".format(e) ) - except ROclient.ROClientException as e: - if not nb_tries: - self.logger.debug( - logging_text - + "error injecting key: {}. Retrying until {} seconds".format( - e, 20 * 10 - ) - ) - nb_tries += 1 - if nb_tries >= 20: - raise LcmException( - "Reaching max tries injecting key. Error: {}".format(e) - ) else: break @@ -2461,6 +2410,8 @@ class NsLcm(LcmBase): # update operation on nslcmops db_nslcmop_update = {} + timeout_ns_deploy = self.timeout.ns_deploy + nslcmop_operation_state = None db_vnfrs = {} # vnf's info indexed by member-index # n2vc_info = {} @@ -2501,8 +2452,6 @@ class NsLcm(LcmBase): ns_params = db_nslcmop.get("operationParams") if ns_params and ns_params.get("timeout_ns_deploy"): timeout_ns_deploy = ns_params["timeout_ns_deploy"] - else: - timeout_ns_deploy = self.timeout.ns_deploy # read from db: ns stage[1] = "Getting nsr={} from db.".format(nsr_id) @@ -4289,206 +4238,6 @@ class NsLcm(LcmBase): pass self._write_all_config_status(db_nsr=db_nsr, status="DELETED") - async def _terminate_RO( - self, logging_text, nsr_deployed, nsr_id, nslcmop_id, stage - ): - """ - Terminates a deployment from RO - :param logging_text: - :param nsr_deployed: db_nsr._admin.deployed - :param nsr_id: - :param nslcmop_id: - :param stage: list of string with the content to write on db_nslcmop.detailed-status. - this method will update only the index 2, but it will write on database the concatenated content of the list - :return: - """ - db_nsr_update = {} - failed_detail = [] - ro_nsr_id = ro_delete_action = None - if nsr_deployed and nsr_deployed.get("RO"): - ro_nsr_id = nsr_deployed["RO"].get("nsr_id") - ro_delete_action = nsr_deployed["RO"].get("nsr_delete_action_id") - try: - if ro_nsr_id: - stage[2] = "Deleting ns from VIM." - db_nsr_update["detailed-status"] = " ".join(stage) - self._write_op_status(nslcmop_id, stage) - self.logger.debug(logging_text + stage[2]) - self.update_db_2("nsrs", nsr_id, db_nsr_update) - self._write_op_status(nslcmop_id, stage) - desc = await self.RO.delete("ns", ro_nsr_id) - ro_delete_action = desc["action_id"] - db_nsr_update[ - "_admin.deployed.RO.nsr_delete_action_id" - ] = ro_delete_action - db_nsr_update["_admin.deployed.RO.nsr_id"] = None - db_nsr_update["_admin.deployed.RO.nsr_status"] = "DELETED" - if ro_delete_action: - # wait until NS is deleted from VIM - stage[2] = "Waiting ns deleted from VIM." - detailed_status_old = None - self.logger.debug( - logging_text - + stage[2] - + " RO_id={} ro_delete_action={}".format( - ro_nsr_id, ro_delete_action - ) - ) - self.update_db_2("nsrs", nsr_id, db_nsr_update) - self._write_op_status(nslcmop_id, stage) - - delete_timeout = 20 * 60 # 20 minutes - while delete_timeout > 0: - desc = await self.RO.show( - "ns", - item_id_name=ro_nsr_id, - extra_item="action", - extra_item_id=ro_delete_action, - ) - - # deploymentStatus - self._on_update_ro_db(nsrs_id=nsr_id, ro_descriptor=desc) - - ns_status, ns_status_info = self.RO.check_action_status(desc) - if ns_status == "ERROR": - raise ROclient.ROClientException(ns_status_info) - elif ns_status == "BUILD": - stage[2] = "Deleting from VIM {}".format(ns_status_info) - elif ns_status == "ACTIVE": - db_nsr_update["_admin.deployed.RO.nsr_delete_action_id"] = None - db_nsr_update["_admin.deployed.RO.nsr_status"] = "DELETED" - break - else: - assert ( - False - ), "ROclient.check_action_status returns unknown {}".format( - ns_status - ) - if stage[2] != detailed_status_old: - detailed_status_old = stage[2] - db_nsr_update["detailed-status"] = " ".join(stage) - self._write_op_status(nslcmop_id, stage) - self.update_db_2("nsrs", nsr_id, db_nsr_update) - await asyncio.sleep(5, loop=self.loop) - delete_timeout -= 5 - else: # delete_timeout <= 0: - raise ROclient.ROClientException( - "Timeout waiting ns deleted from VIM" - ) - - except Exception as e: - self.update_db_2("nsrs", nsr_id, db_nsr_update) - if ( - isinstance(e, ROclient.ROClientException) and e.http_code == 404 - ): # not found - db_nsr_update["_admin.deployed.RO.nsr_id"] = None - db_nsr_update["_admin.deployed.RO.nsr_status"] = "DELETED" - db_nsr_update["_admin.deployed.RO.nsr_delete_action_id"] = None - self.logger.debug( - logging_text + "RO_ns_id={} already deleted".format(ro_nsr_id) - ) - elif ( - isinstance(e, ROclient.ROClientException) and e.http_code == 409 - ): # conflict - failed_detail.append("delete conflict: {}".format(e)) - self.logger.debug( - logging_text - + "RO_ns_id={} delete conflict: {}".format(ro_nsr_id, e) - ) - else: - failed_detail.append("delete error: {}".format(e)) - self.logger.error( - logging_text + "RO_ns_id={} delete error: {}".format(ro_nsr_id, e) - ) - - # Delete nsd - if not failed_detail and deep_get(nsr_deployed, ("RO", "nsd_id")): - ro_nsd_id = nsr_deployed["RO"]["nsd_id"] - try: - stage[2] = "Deleting nsd from RO." - db_nsr_update["detailed-status"] = " ".join(stage) - self.update_db_2("nsrs", nsr_id, db_nsr_update) - self._write_op_status(nslcmop_id, stage) - await self.RO.delete("nsd", ro_nsd_id) - self.logger.debug( - logging_text + "ro_nsd_id={} deleted".format(ro_nsd_id) - ) - db_nsr_update["_admin.deployed.RO.nsd_id"] = None - except Exception as e: - if ( - isinstance(e, ROclient.ROClientException) and e.http_code == 404 - ): # not found - db_nsr_update["_admin.deployed.RO.nsd_id"] = None - self.logger.debug( - logging_text + "ro_nsd_id={} already deleted".format(ro_nsd_id) - ) - elif ( - isinstance(e, ROclient.ROClientException) and e.http_code == 409 - ): # conflict - failed_detail.append( - "ro_nsd_id={} delete conflict: {}".format(ro_nsd_id, e) - ) - self.logger.debug(logging_text + failed_detail[-1]) - else: - failed_detail.append( - "ro_nsd_id={} delete error: {}".format(ro_nsd_id, e) - ) - self.logger.error(logging_text + failed_detail[-1]) - - if not failed_detail and deep_get(nsr_deployed, ("RO", "vnfd")): - for index, vnf_deployed in enumerate(nsr_deployed["RO"]["vnfd"]): - if not vnf_deployed or not vnf_deployed["id"]: - continue - try: - ro_vnfd_id = vnf_deployed["id"] - stage[ - 2 - ] = "Deleting member_vnf_index={} ro_vnfd_id={} from RO.".format( - vnf_deployed["member-vnf-index"], ro_vnfd_id - ) - db_nsr_update["detailed-status"] = " ".join(stage) - self.update_db_2("nsrs", nsr_id, db_nsr_update) - self._write_op_status(nslcmop_id, stage) - await self.RO.delete("vnfd", ro_vnfd_id) - self.logger.debug( - logging_text + "ro_vnfd_id={} deleted".format(ro_vnfd_id) - ) - db_nsr_update["_admin.deployed.RO.vnfd.{}.id".format(index)] = None - except Exception as e: - if ( - isinstance(e, ROclient.ROClientException) and e.http_code == 404 - ): # not found - db_nsr_update[ - "_admin.deployed.RO.vnfd.{}.id".format(index) - ] = None - self.logger.debug( - logging_text - + "ro_vnfd_id={} already deleted ".format(ro_vnfd_id) - ) - elif ( - isinstance(e, ROclient.ROClientException) and e.http_code == 409 - ): # conflict - failed_detail.append( - "ro_vnfd_id={} delete conflict: {}".format(ro_vnfd_id, e) - ) - self.logger.debug(logging_text + failed_detail[-1]) - else: - failed_detail.append( - "ro_vnfd_id={} delete error: {}".format(ro_vnfd_id, e) - ) - self.logger.error(logging_text + failed_detail[-1]) - - if failed_detail: - stage[2] = "Error deleting from VIM" - else: - stage[2] = "Deleted from VIM" - db_nsr_update["detailed-status"] = " ".join(stage) - self.update_db_2("nsrs", nsr_id, db_nsr_update) - self._write_op_status(nslcmop_id, stage) - - if failed_detail: - raise LcmException("; ".join(failed_detail)) - async def terminate(self, nsr_id, nslcmop_id): # Try to lock HA task here task_is_locked_by_me = self.lcm_tasks.lock_HA("ns", "nslcmops", nslcmop_id) @@ -4693,13 +4442,7 @@ class NsLcm(LcmBase): logging_text, nsr_deployed, nsr_id, nslcmop_id, stage ) ) - else: - task_delete_ro = asyncio.ensure_future( - self._terminate_RO( - logging_text, nsr_deployed, nsr_id, nslcmop_id, stage - ) - ) - tasks_dict_info[task_delete_ro] = "Removing deployment from VIM" + tasks_dict_info[task_delete_ro] = "Removing deployment from VIM" # rest of staff will be done at finally @@ -5136,6 +4879,7 @@ class NsLcm(LcmBase): nslcmop_operation_state = None error_description_nslcmop = None exc = None + step = "" try: # wait for any previous tasks in process step = "Waiting for previous operations to terminate" @@ -5810,6 +5554,7 @@ class NsLcm(LcmBase): exc = None change_type = "updated" detailed_status = "" + member_vnf_index = None try: # wait for any previous tasks in process @@ -6314,7 +6059,10 @@ class NsLcm(LcmBase): "nslcmop_id": nslcmop_id, "operationState": nslcmop_operation_state, } - if change_type in ("vnf_terminated", "policy_updated"): + if ( + change_type in ("vnf_terminated", "policy_updated") + and member_vnf_index + ): msg.update({"vnf_member_index": member_vnf_index}) await self.msg.aiowrite("ns", change_type, msg, loop=self.loop) except Exception as e: diff --git a/osm_lcm/osm_config.py b/osm_lcm/osm_config.py index 7dd8f63..21b584a 100644 --- a/osm_lcm/osm_config.py +++ b/osm_lcm/osm_config.py @@ -28,6 +28,7 @@ class K8sConfigV0(BaseModel): services: List[Dict] @validator("services") + @classmethod def parse_services(cls, services: Dict[str, Any]): return { service["name"]: { diff --git a/osm_lcm/vim_sdn.py b/osm_lcm/vim_sdn.py index 0c22305..d95df94 100644 --- a/osm_lcm/vim_sdn.py +++ b/osm_lcm/vim_sdn.py @@ -1006,7 +1006,7 @@ class SdnLcm(LcmBase): logging_text = "Task sdn_delete={} ".format(sdn_id) self.logger.debug(logging_text + "Enter") - db_sdn = None + db_sdn = {} db_sdn_update = {} exc = None step = "Getting sdn from db" @@ -1039,7 +1039,7 @@ class SdnLcm(LcmBase): logging_text + "Skipping. There is not RO information at database" ) self.db.del_one("sdns", {"_id": sdn_id}) - db_sdn = None + db_sdn = {} self.logger.debug("sdn_delete task sdn_id={} Exit Ok".format(sdn_id)) return @@ -1474,9 +1474,10 @@ class VcaLcm(LcmBase): vca_id = vca_content["_id"] self.logger.debug("Task vca_create={} {}".format(vca_id, "Enter")) - db_vca = None db_vca_update = {} + operation_state = "FAILED" + operation_details = "" try: self.logger.debug( "Task vca_create={} {}".format(vca_id, "Getting vca from db") @@ -1514,7 +1515,6 @@ class VcaLcm(LcmBase): self.logger.error("Task vca_create={} {}".format(vca_id, error_msg)) db_vca_update["_admin.operationalState"] = "ERROR" db_vca_update["_admin.detailed-status"] = error_msg - operation_state = "FAILED" operation_details = error_msg finally: try: @@ -1549,6 +1549,9 @@ class VcaLcm(LcmBase): db_vca_update = {} vca_id = vca_content["_id"] + operation_state = "FAILED" + operation_details = "" + try: self.logger.debug( "Task vca_delete={} {}".format(vca_id, "Deleting vca from db") @@ -1568,7 +1571,6 @@ class VcaLcm(LcmBase): self.logger.error("Task vca_delete={} {}".format(vca_id, error_msg)) db_vca_update["_admin.operationalState"] = "ERROR" db_vca_update["_admin.detailed-status"] = error_msg - operation_state = "FAILED" operation_details = error_msg finally: try: diff --git a/tox.ini b/tox.ini index b014de1..1cb2253 100644 --- a/tox.ini +++ b/tox.ini @@ -67,7 +67,7 @@ deps = {[testenv]deps} -r{toxinidir}/requirements-test.txt pylint commands = - - pylint -E osm_lcm + pylint -E osm_lcm --extension-pkg-whitelist=pydantic # issue with pydantic (https://github.com/pydantic/pydantic/issues/1961) ####################################################################################### -- 2.25.1