Fix bug 1432: Support for alternative images for sol006 65/10265/5
authorlloretgalleg <illoret@indra.es>
Mon, 8 Feb 2021 11:48:48 +0000 (11:48 +0000)
committerlloretgalleg <illoret@indra.es>
Mon, 22 Feb 2021 10:43:04 +0000 (10:43 +0000)
Change-Id: I9e8488c02a00916e7b6f1c4f9beb7dbdcda49677
Signed-off-by: lloretgalleg <illoret@indra.es>
osm_nbi/instance_topics.py

index 05ca347..cabfc1c 100644 (file)
@@ -419,18 +419,13 @@ class NsrTopic(BaseTopic):
 
                     sw_image_id = vdu.get("sw-image-desc")
                     if sw_image_id:
-                        sw_image_desc = utils.find_in_list(vnfd.get("sw-image-desc", ()),
-                                                           lambda sw: sw["id"] == sw_image_id)
-                        image_data = {}
-                        if sw_image_desc.get("image"):
-                            image_data["image"] = sw_image_desc["image"]
-                        if sw_image_desc.get("checksum"):
-                            image_data["image_checksum"] = sw_image_desc["checksum"]["hash"]
-                    img = next((f for f in nsr_descriptor["image"] if
-                                all(f.get(k) == image_data[k] for k in image_data)), None)
-                    if not img:
-                        image_data["id"] = str(len(nsr_descriptor["image"]))
-                        nsr_descriptor["image"].append(image_data)
+                        image_data = self._get_image_data_from_vnfd(vnfd, sw_image_id)
+                        self._add_image_to_nsr(nsr_descriptor, image_data)
+
+                    # also add alternative images to the list of images
+                    for alt_image in vdu.get("alternative-sw-image-desc", ()):
+                        image_data = self._get_image_data_from_vnfd(vnfd, alt_image)
+                        self._add_image_to_nsr(nsr_descriptor, image_data)
 
             for vld in nsr_vld:
                 vld["vnfd-connection-point-ref"] = all_vld_connection_point_data.get(vld.get("id"), [])
@@ -439,6 +434,28 @@ class NsrTopic(BaseTopic):
 
         return nsr_descriptor
 
+    def _get_image_data_from_vnfd(self, vnfd, sw_image_id):
+        sw_image_desc = utils.find_in_list(vnfd.get("sw-image-desc", ()),
+                                           lambda sw: sw["id"] == sw_image_id)
+        image_data = {}
+        if sw_image_desc.get("image"):
+            image_data["image"] = sw_image_desc["image"]
+        if sw_image_desc.get("checksum"):
+            image_data["image_checksum"] = sw_image_desc["checksum"]["hash"]
+        if sw_image_desc.get("vim-type"):
+            image_data["vim-type"] = sw_image_desc["vim-type"]
+        return image_data
+
+    def _add_image_to_nsr(self, nsr_descriptor, image_data):
+        """
+        Adds image to nsr checking first it is not already added
+        """
+        img = next((f for f in nsr_descriptor["image"] if
+                    all(f.get(k) == image_data[k] for k in image_data)), None)
+        if not img:
+            image_data["id"] = str(len(nsr_descriptor["image"]))
+            nsr_descriptor["image"].append(image_data)
+
     def _create_vnfr_descriptor_from_vnfd(self, nsd, vnfd, vnfd_id, vnf_index, nsr_descriptor,
                                           ns_request, ns_k8s_namespace):
         vnfr_id = str(uuid4())
@@ -624,6 +641,19 @@ class NsrTopic(BaseTopic):
                 )
                 vdur["ns-image-id"] = nsr_sw_image_data["id"]
 
+            if vdu.get("alternative-sw-image-desc"):
+                alt_image_ids = []
+                for alt_image_id in vdu.get("alternative-sw-image-desc", ()):
+                    sw_image = utils.find_in_list(
+                        vnfd.get("sw-image-desc", ()),
+                        lambda image: image["id"] == alt_image_id)
+                    nsr_sw_image_data = utils.find_in_list(
+                        nsr_descriptor["image"],
+                        lambda nsr_image: (nsr_image.get("image") == sw_image.get("image"))
+                    )
+                    alt_image_ids.append(nsr_sw_image_data["id"])
+                vdur["alt-image-ids"] = alt_image_ids
+
             flavor_data_name = vdu["id"][:56] + "-flv"
             nsr_flavor_desc = utils.find_in_list(
                 nsr_descriptor["flavor"],