From 349aa468cc1fce58d85f50a8bf650a94f73f938e Mon Sep 17 00:00:00 2001 From: aticig Date: Thu, 19 May 2022 12:29:35 +0300 Subject: [PATCH] Fix Bug 2012 use existing volumes as instantiation parameters This fix parses the vdu instantiation parameters regarding with the persistent volumes and than RO can process it. Additionally corrected some Black and Flake8 errors. Change-Id: If6fb41684555364914af52af334d70f794f133a2 Signed-off-by: aticig --- osm_lcm/data_utils/vnfr.py | 27 ++++++++++++++++ osm_lcm/ns.py | 65 ++++++++++++++++++++++++-------------- 2 files changed, 69 insertions(+), 23 deletions(-) diff --git a/osm_lcm/data_utils/vnfr.py b/osm_lcm/data_utils/vnfr.py index fe98102..fc64145 100644 --- a/osm_lcm/data_utils/vnfr.py +++ b/osm_lcm/data_utils/vnfr.py @@ -77,3 +77,30 @@ def get_kdur(db_vnfr, kdu_name): return next(x for x in kdur_list if x.get("kdu-name") == kdu_name) else: return None + + +def get_volumes_from_instantiation_params( + vdu_instantiation_params: dict, vdud: dict +) -> list: + """Get the VDU volumes from instantiation parameters + + Args: + vdu_instantiation_params: VDU instantiation parameters + vdud: VDU description as a dictionary extracted from VNFD + Returns: + vdu_volume_list:(list) + + """ + vdu_volume_list = [] + if vdu_instantiation_params.get("volume"): + for volume in vdu_instantiation_params["volume"]: + if volume.get("vim-volume-id") and volume.get("name") in vdud.get( + "virtual-storage-desc" + ): + vdu_volume = { + "name": volume["name"], + "vim-volume-id": volume["vim-volume-id"], + } + vdu_volume_list.append(vdu_volume) + + return vdu_volume_list diff --git a/osm_lcm/ns.py b/osm_lcm/ns.py index 02b9f08..d57b739 100644 --- a/osm_lcm/ns.py +++ b/osm_lcm/ns.py @@ -84,7 +84,12 @@ from osm_lcm.data_utils.vnfd import ( find_software_version, ) from osm_lcm.data_utils.list_utils import find_in_list -from osm_lcm.data_utils.vnfr import get_osm_params, get_vdur_index, get_kdur +from osm_lcm.data_utils.vnfr import ( + get_osm_params, + get_vdur_index, + get_kdur, + get_volumes_from_instantiation_params, +) from osm_lcm.data_utils.dict_utils import parse_yaml_strings from osm_lcm.data_utils.database.vim_account import VimAccountDB from n2vc.definitions import RelationEndpoint @@ -455,8 +460,7 @@ class NsLcm(LcmBase): def _get_vdu_additional_params(self, db_vnfr, vdu_id): vdur = next( - (vdur for vdur in db_vnfr.get("vdur") if vdu_id == vdur["vdu-id-ref"]), - {} + (vdur for vdur in db_vnfr.get("vdur") if vdu_id == vdur["vdu-id-ref"]), {} ) additional_params = vdur.get("additionalParams") return parse_yaml_strings(additional_params) @@ -547,21 +551,24 @@ class NsLcm(LcmBase): ) if not vdur: # Read the template saved in the db: - self.logger.debug(f"No vdur in the database. Using the vdur-template to scale") + self.logger.debug( + "No vdur in the database. Using the vdur-template to scale" + ) vdur_template = db_vnfr.get("vdur-template") if not vdur_template: raise LcmException( - "Error scaling OUT VNFR for {}. No vnfr or template exists".format( - vdu_id + "Error scaling OUT VNFR for {}. No vnfr or template exists".format( + vdu_id ) ) vdur = vdur_template[0] - #Delete a template from the database after using it - self.db.set_one("vnfrs", - {"_id": db_vnfr["_id"]}, - None, - pull={"vdur-template": {"_id": vdur['_id']}} - ) + # Delete a template from the database after using it + self.db.set_one( + "vnfrs", + {"_id": db_vnfr["_id"]}, + None, + pull={"vdur-template": {"_id": vdur["_id"]}}, + ) for count in range(vdu_count): vdur_copy = deepcopy(vdur) vdur_copy["status"] = "BUILD" @@ -595,7 +602,9 @@ class NsLcm(LcmBase): if vdu_delete: if len(db_vnfr["vdur"]) == 1: # The scale will move to 0 instances - self.logger.debug(f"Scaling to 0 !, creating the template with the last vdur") + self.logger.debug( + "Scaling to 0 !, creating the template with the last vdur" + ) template_vdur = [db_vnfr["vdur"][0]] for vdu_id, vdu_count in vdu_delete.items(): if mark_delete: @@ -1215,9 +1224,17 @@ class NsLcm(LcmBase): vdur["vim_info"] = {target_vim: {}} # instantiation parameters - # if vnf_params: - # vdu_instantiation_params = next((v for v in get_iterable(vnf_params, "vdu") if v["id"] == - # vdud["id"]), None) + if vnf_params: + vdu_instantiation_params = find_in_list( + get_iterable(vnf_params, "vdu"), + lambda i_vdu: i_vdu["id"] == vdud["id"], + ) + if vdu_instantiation_params: + # Parse the vdu_volumes from the instantiation params + vdu_volumes = get_volumes_from_instantiation_params( + vdu_instantiation_params, vdud + ) + vdur["additionalParams"]["OSM"]["vdu_volumes"] = vdu_volumes vdur_list.append(vdur) target_vnf["vdur"] = vdur_list target["vnf"].append(target_vnf) @@ -7222,16 +7239,18 @@ class NsLcm(LcmBase): try: # wait for any previous tasks in process step = "Waiting for previous operations to terminate" - await self.lcm_tasks.waitfor_related_HA('ns', 'nslcmops', nslcmop_id) + await self.lcm_tasks.waitfor_related_HA("ns", "nslcmops", nslcmop_id) self._write_ns_status( nsr_id=nsr_id, ns_state=None, current_operation="MIGRATING", - current_operation_id=nslcmop_id + current_operation_id=nslcmop_id, ) step = "Getting nslcmop from database" - self.logger.debug(step + " after having waited for previous tasks to be completed") + self.logger.debug( + step + " after having waited for previous tasks to be completed" + ) db_nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id}) migrate_params = db_nslcmop.get("operationParams") @@ -7251,7 +7270,9 @@ class NsLcm(LcmBase): exc = "Operation was cancelled" except Exception as e: exc = traceback.format_exc() - self.logger.critical("Exit Exception {} {}".format(type(e).__name__, e), exc_info=True) + self.logger.critical( + "Exit Exception {} {}".format(type(e).__name__, e), exc_info=True + ) finally: self._write_ns_status( nsr_id=nsr_id, @@ -7260,9 +7281,7 @@ class NsLcm(LcmBase): current_operation_id=None, ) if exc: - db_nslcmop_update[ - "detailed-status" - ] = "FAILED {}: {}".format(step, exc) + db_nslcmop_update["detailed-status"] = "FAILED {}: {}".format(step, exc) nslcmop_operation_state = "FAILED" else: nslcmop_operation_state = "COMPLETED" -- 2.25.1