Feature 10912: NS Update Change VNF Package 34/11934/5
authorelumalai <deepika.e@tataelxsi.co.in>
Mon, 25 Apr 2022 16:57:59 +0000 (22:27 +0530)
committerelumalai <deepika.e@tataelxsi.co.in>
Wed, 25 May 2022 12:22:04 +0000 (14:22 +0200)
Added support for NS Update Change VNF Package feature

Change-Id: I1a20b170f33e64f20814245602c9412133064ca6
Signed-off-by: elumalai <deepika.e@tataelxsi.co.in>
osm_nbi/instance_topics.py

index 2df6a7d..7bfff0b 100644 (file)
@@ -439,6 +439,80 @@ class NsrTopic(BaseTopic):
 
         return ns_k8s_namespace
 
+    def _add_flavor_to_nsr(self, vdu, vnfd, nsr_descriptor):
+        flavor_data = {}
+        guest_epa = {}
+        # Find this vdu compute and storage descriptors
+        vdu_virtual_compute = {}
+        vdu_virtual_storage = {}
+        for vcd in vnfd.get("virtual-compute-desc", ()):
+            if vcd.get("id") == vdu.get("virtual-compute-desc"):
+                vdu_virtual_compute = vcd
+        for vsd in vnfd.get("virtual-storage-desc", ()):
+            if vsd.get("id") == vdu.get("virtual-storage-desc", [[]])[0]:
+                vdu_virtual_storage = vsd
+        # Get this vdu vcpus, memory and storage info for flavor_data
+        if vdu_virtual_compute.get("virtual-cpu", {}).get(
+            "num-virtual-cpu"
+        ):
+            flavor_data["vcpu-count"] = vdu_virtual_compute["virtual-cpu"][
+                "num-virtual-cpu"
+            ]
+        if vdu_virtual_compute.get("virtual-memory", {}).get("size"):
+            flavor_data["memory-mb"] = (
+                float(vdu_virtual_compute["virtual-memory"]["size"])
+                * 1024.0
+            )
+        if vdu_virtual_storage.get("size-of-storage"):
+            flavor_data["storage-gb"] = vdu_virtual_storage[
+                "size-of-storage"
+            ]
+        # Get this vdu EPA info for guest_epa
+        if vdu_virtual_compute.get("virtual-cpu", {}).get("cpu-quota"):
+            guest_epa["cpu-quota"] = vdu_virtual_compute["virtual-cpu"][
+                "cpu-quota"
+            ]
+        if vdu_virtual_compute.get("virtual-cpu", {}).get("pinning"):
+            vcpu_pinning = vdu_virtual_compute["virtual-cpu"]["pinning"]
+            if vcpu_pinning.get("thread-policy"):
+                guest_epa["cpu-thread-pinning-policy"] = vcpu_pinning[
+                    "thread-policy"
+                ]
+            if vcpu_pinning.get("policy"):
+                cpu_policy = (
+                    "SHARED"
+                    if vcpu_pinning["policy"] == "dynamic"
+                    else "DEDICATED"
+                )
+                guest_epa["cpu-pinning-policy"] = cpu_policy
+        if vdu_virtual_compute.get("virtual-memory", {}).get("mem-quota"):
+            guest_epa["mem-quota"] = vdu_virtual_compute["virtual-memory"][
+                "mem-quota"
+            ]
+        if vdu_virtual_compute.get("virtual-memory", {}).get(
+            "mempage-size"
+        ):
+            guest_epa["mempage-size"] = vdu_virtual_compute[
+                "virtual-memory"
+            ]["mempage-size"]
+        if vdu_virtual_compute.get("virtual-memory", {}).get(
+            "numa-node-policy"
+        ):
+            guest_epa["numa-node-policy"] = vdu_virtual_compute[
+                "virtual-memory"
+            ]["numa-node-policy"]
+        if vdu_virtual_storage.get("disk-io-quota"):
+            guest_epa["disk-io-quota"] = vdu_virtual_storage[
+                "disk-io-quota"
+            ]
+
+        if guest_epa:
+            flavor_data["guest-epa"] = guest_epa
+
+        flavor_data["name"] = vdu["id"][:56] + "-flv"
+        flavor_data["id"] = str(len(nsr_descriptor["flavor"]))
+        nsr_descriptor["flavor"].append(flavor_data)
+
     def _create_nsr_descriptor_from_nsd(self, nsd, ns_request, nsr_id, session):
         now = time()
         additional_params, _ = self._format_additional_params(
@@ -514,79 +588,7 @@ class NsrTopic(BaseTopic):
                 vnfd.pop("_admin")
 
                 for vdu in vnfd.get("vdu", ()):
-                    flavor_data = {}
-                    guest_epa = {}
-                    # Find this vdu compute and storage descriptors
-                    vdu_virtual_compute = {}
-                    vdu_virtual_storage = {}
-                    for vcd in vnfd.get("virtual-compute-desc", ()):
-                        if vcd.get("id") == vdu.get("virtual-compute-desc"):
-                            vdu_virtual_compute = vcd
-                    for vsd in vnfd.get("virtual-storage-desc", ()):
-                        if vsd.get("id") == vdu.get("virtual-storage-desc", [[]])[0]:
-                            vdu_virtual_storage = vsd
-                    # Get this vdu vcpus, memory and storage info for flavor_data
-                    if vdu_virtual_compute.get("virtual-cpu", {}).get(
-                        "num-virtual-cpu"
-                    ):
-                        flavor_data["vcpu-count"] = vdu_virtual_compute["virtual-cpu"][
-                            "num-virtual-cpu"
-                        ]
-                    if vdu_virtual_compute.get("virtual-memory", {}).get("size"):
-                        flavor_data["memory-mb"] = (
-                            float(vdu_virtual_compute["virtual-memory"]["size"])
-                            * 1024.0
-                        )
-                    if vdu_virtual_storage.get("size-of-storage"):
-                        flavor_data["storage-gb"] = vdu_virtual_storage[
-                            "size-of-storage"
-                        ]
-                    # Get this vdu EPA info for guest_epa
-                    if vdu_virtual_compute.get("virtual-cpu", {}).get("cpu-quota"):
-                        guest_epa["cpu-quota"] = vdu_virtual_compute["virtual-cpu"][
-                            "cpu-quota"
-                        ]
-                    if vdu_virtual_compute.get("virtual-cpu", {}).get("pinning"):
-                        vcpu_pinning = vdu_virtual_compute["virtual-cpu"]["pinning"]
-                        if vcpu_pinning.get("thread-policy"):
-                            guest_epa["cpu-thread-pinning-policy"] = vcpu_pinning[
-                                "thread-policy"
-                            ]
-                        if vcpu_pinning.get("policy"):
-                            cpu_policy = (
-                                "SHARED"
-                                if vcpu_pinning["policy"] == "dynamic"
-                                else "DEDICATED"
-                            )
-                            guest_epa["cpu-pinning-policy"] = cpu_policy
-                    if vdu_virtual_compute.get("virtual-memory", {}).get("mem-quota"):
-                        guest_epa["mem-quota"] = vdu_virtual_compute["virtual-memory"][
-                            "mem-quota"
-                        ]
-                    if vdu_virtual_compute.get("virtual-memory", {}).get(
-                        "mempage-size"
-                    ):
-                        guest_epa["mempage-size"] = vdu_virtual_compute[
-                            "virtual-memory"
-                        ]["mempage-size"]
-                    if vdu_virtual_compute.get("virtual-memory", {}).get(
-                        "numa-node-policy"
-                    ):
-                        guest_epa["numa-node-policy"] = vdu_virtual_compute[
-                            "virtual-memory"
-                        ]["numa-node-policy"]
-                    if vdu_virtual_storage.get("disk-io-quota"):
-                        guest_epa["disk-io-quota"] = vdu_virtual_storage[
-                            "disk-io-quota"
-                        ]
-
-                    if guest_epa:
-                        flavor_data["guest-epa"] = guest_epa
-
-                    flavor_data["name"] = vdu["id"][:56] + "-flv"
-                    flavor_data["id"] = str(len(nsr_descriptor["flavor"]))
-                    nsr_descriptor["flavor"].append(flavor_data)
-
+                    self._add_flavor_to_nsr(vdu, vnfd, nsr_descriptor)
                     sw_image_id = vdu.get("sw-image-desc")
                     if sw_image_id:
                         image_data = self._get_image_data_from_vnfd(vnfd, sw_image_id)
@@ -1164,6 +1166,7 @@ class NsLcmOpTopic(BaseTopic):
 
     def __init__(self, db, fs, msg, auth):
         BaseTopic.__init__(self, db, fs, msg, auth)
+        self.nsrtopic = NsrTopic(db, fs, msg, auth)
 
     def _check_ns_operation(self, session, nsr, operation, indata):
         """
@@ -2223,7 +2226,46 @@ class NsLcmOpTopic(BaseTopic):
             if operation == "instantiate":
                 self._update_vnfrs_from_nsd(nsr)
                 self._update_vnfrs(session, rollback, nsr, indata)
-
+            if (operation == "update") and (indata["updateType"] == "CHANGE_VNFPKG"):
+                nsr_update = {}
+                vnfd_id = indata["changeVnfPackageData"]["vnfdId"]
+                vnfd = self.db.get_one("vnfds", {"_id": vnfd_id})
+                nsd = self.db.get_one("nsds", {"_id": nsr["nsd-id"]})
+                ns_request = nsr["instantiate_params"]
+                vnfr = self.db.get_one("vnfrs", {"_id": indata["changeVnfPackageData"]["vnfInstanceId"]})
+                latest_vnfd_revision = vnfd["_admin"].get("revision")
+                vnfr_vnfd_revision = vnfr.get("revision")
+                old_vnfd_id = vnfd_id + ":" + str(vnfr_vnfd_revision)
+                old_db_vnfd = self.db.get_one("vnfds_revisions", {"_id": old_vnfd_id})
+                old_sw_version = old_db_vnfd["software-version"]
+                new_sw_version = vnfd["software-version"]
+                if new_sw_version != old_sw_version:
+                    vnf_index = vnfr["member-vnf-index-ref"]
+                    self.logger.info("nsr {}".format(nsr))
+                    for vdu in vnfd["vdu"]:
+                        self.nsrtopic._add_flavor_to_nsr(vdu, vnfd, nsr)
+                        sw_image_id = vdu.get("sw-image-desc")
+                        if sw_image_id:
+                            image_data = self.nsrtopic._get_image_data_from_vnfd(vnfd, sw_image_id)
+                            self.nsrtopic._add_image_to_nsr(nsr, image_data)
+                        for alt_image in vdu.get("alternative-sw-image-desc", ()):
+                            image_data = self.nsrtopic._get_image_data_from_vnfd(vnfd, alt_image)
+                            self.nsrtopic._add_image_to_nsr(nsr, image_data)
+                    nsr_update["image"] = nsr["image"]
+                    nsr_update["flavor"] = nsr["flavor"]
+                    self.db.set_one("nsrs", {"_id": nsr["_id"]}, nsr_update)
+                    ns_k8s_namespace = self.nsrtopic._get_ns_k8s_namespace(nsd, ns_request, session)
+                    vnfr_descriptor = self.nsrtopic._create_vnfr_descriptor_from_vnfd(
+                        nsd,
+                        vnfd,
+                        vnfd_id,
+                        vnf_index,
+                        nsr,
+                        ns_request,
+                        ns_k8s_namespace,
+                    )
+                    indata["newVdur"] = vnfr_descriptor["vdur"]
+                    # self.logger.info("vnfr_descriptor {}".format(vnfr_descriptor))
             nslcmop_desc = self._create_nslcmop(nsInstanceId, operation, indata)
             _id = nslcmop_desc["_id"]
             self.format_on_new(