Fix Bug 2012 use existing volumes as instantiation parameters 83/12083/4
authoraticig <gulsum.atici@canonical.com>
Thu, 19 May 2022 09:29:35 +0000 (12:29 +0300)
committercubag <gcuba@whitestack.com>
Tue, 7 Jun 2022 23:47:51 +0000 (01:47 +0200)
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 <gulsum.atici@canonical.com>
osm_lcm/data_utils/vnfr.py
osm_lcm/ns.py

index fe98102..fc64145 100644 (file)
@@ -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
         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
index 02b9f08..d57b739 100644 (file)
@@ -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
     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
 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(
 
     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)
         )
         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:
                 )
                 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(
                     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]
                             )
                         )
                     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"
                 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
         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:
                 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
 
                 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)
                 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"
         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",
 
             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"
             )
             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")
 
             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()
             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,
         finally:
             self._write_ns_status(
                 nsr_id=nsr_id,
@@ -7260,9 +7281,7 @@ class NsLcm(LcmBase):
                 current_operation_id=None,
             )
             if exc:
                 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"
                 nslcmop_operation_state = "FAILED"
             else:
                 nslcmop_operation_state = "COMPLETED"