Fix 1472 - Error executing upgrade action over K8S NS
[osm/NBI.git] / osm_nbi / instance_topics.py
index 4a4f4c3..bf0e3a9 100644 (file)
@@ -177,33 +177,29 @@ class NsrTopic(BaseTopic):
                     additional_params[k] = "!!yaml " + safe_dump(v)
 
         if descriptor:
                     additional_params[k] = "!!yaml " + safe_dump(v)
 
         if descriptor:
-            # check that enough parameters are supplied for the initial-config-primitive
-            # TODO: check for cloud-init
-            if member_vnf_index:
-                if kdu_name:
-                    initial_primitives = None
-                elif vdu_id:
-                    vdud = next(x for x in descriptor["vdu"] if x["id"] == vdu_id)
-                    initial_primitives = deep_get(vdud, ("vdu-configuration", "initial-config-primitive"))
-                else:
-                    vnf_configurations = get_iterable(descriptor.get("vnf-configuration"))
+            for df in descriptor.get("df", []):
+                # check that enough parameters are supplied for the initial-config-primitive
+                # TODO: check for cloud-init
+                if member_vnf_index:
                     initial_primitives = []
                     initial_primitives = []
-                    for vnfc in vnf_configurations:
-                        for primitive in get_iterable(vnfc.get("initial-config-primitive")):
-                            initial_primitives.append(primitive)
-            else:
-                initial_primitives = deep_get(descriptor, ("ns-configuration", "initial-config-primitive"))
-
-            for initial_primitive in get_iterable(initial_primitives):
-                for param in get_iterable(initial_primitive.get("parameter")):
-                    if param["value"].startswith("<") and param["value"].endswith(">"):
-                        if param["value"] in ("<rw_mgmt_ip>", "<VDU_SCALE_INFO>", "<ns_config_info>"):
-                            continue
-                        if not additional_params or param["value"][1:-1] not in additional_params:
-                            raise EngineException("Parameter '{}' needed for vnfd[id={}]:vnf-configuration:"
-                                                  "initial-config-primitive[name={}] not supplied".
-                                                  format(param["value"], descriptor["id"],
-                                                         initial_primitive["name"]))
+                    if "lcm-operations-configuration" in df \
+                       and "operate-vnf-op-config" in df["lcm-operations-configuration"]:
+                        for config in df["lcm-operations-configuration"]["operate-vnf-op-config"].get("day1-2", []):
+                            for primitive in get_iterable(config.get("initial-config-primitive")):
+                                initial_primitives.append(primitive)
+                else:
+                    initial_primitives = deep_get(descriptor, ("ns-configuration", "initial-config-primitive"))
+
+                for initial_primitive in get_iterable(initial_primitives):
+                    for param in get_iterable(initial_primitive.get("parameter")):
+                        if param["value"].startswith("<") and param["value"].endswith(">"):
+                            if param["value"] in ("<rw_mgmt_ip>", "<VDU_SCALE_INFO>", "<ns_config_info>"):
+                                continue
+                            if not additional_params or param["value"][1:-1] not in additional_params:
+                                raise EngineException("Parameter '{}' needed for vnfd[id={}]:day1-2 configuration:"
+                                                      "initial-config-primitive[name={}] not supplied".
+                                                      format(param["value"], descriptor["id"],
+                                                             initial_primitive["name"]))
 
         return additional_params or None, other_params or None
 
 
         return additional_params or None, other_params or None
 
@@ -237,7 +233,7 @@ class NsrTopic(BaseTopic):
 
             step = "filling nsr from input data"
             nsr_id = str(uuid4())
 
             step = "filling nsr from input data"
             nsr_id = str(uuid4())
-            nsr_descriptor = self._create_nsr_descriptor_from_nsd(nsd, ns_request, nsr_id)
+            nsr_descriptor = self._create_nsr_descriptor_from_nsd(nsd, ns_request, nsr_id, session)
 
             # Create VNFRs
             needed_vnfds = {}
 
             # Create VNFRs
             needed_vnfds = {}
@@ -310,7 +306,7 @@ class NsrTopic(BaseTopic):
 
         return ns_k8s_namespace
 
 
         return ns_k8s_namespace
 
-    def _create_nsr_descriptor_from_nsd(self, nsd, ns_request, nsr_id):
+    def _create_nsr_descriptor_from_nsd(self, nsd, ns_request, nsr_id, session):
         now = time()
         additional_params, _ = self._format_additional_params(ns_request, descriptor=nsd)
 
         now = time()
         additional_params, _ = self._format_additional_params(ns_request, descriptor=nsd)
 
@@ -371,10 +367,7 @@ class NsrTopic(BaseTopic):
                             "vnfd-id-ref": vnf_profile.get("vnfd-id")
                         })
 
                             "vnfd-id-ref": vnf_profile.get("vnfd-id")
                         })
 
-                vnfd = self.db.get_one("vnfds",
-                                       {"id": vnf_profile.get("vnfd-id")},
-                                       fail_on_empty=True,
-                                       fail_on_more=True)
+                vnfd = self._get_vnfd_from_db(vnf_profile.get("vnfd-id"), session)
 
                 for vdu in vnfd.get("vdu", ()):
                     flavor_data = {}
 
                 for vdu in vnfd.get("vdu", ()):
                     flavor_data = {}
@@ -423,18 +416,13 @@ class NsrTopic(BaseTopic):
 
                     sw_image_id = vdu.get("sw-image-desc")
                     if sw_image_id:
 
                     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"), [])
 
             for vld in nsr_vld:
                 vld["vnfd-connection-point-ref"] = all_vld_connection_point_data.get(vld.get("id"), [])
@@ -443,6 +431,28 @@ class NsrTopic(BaseTopic):
 
         return nsr_descriptor
 
 
         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())
     def _create_vnfr_descriptor_from_vnfd(self, nsd, vnfd, vnfd_id, vnf_index, nsr_descriptor,
                                           ns_request, ns_k8s_namespace):
         vnfr_id = str(uuid4())
@@ -532,7 +542,23 @@ class NsrTopic(BaseTopic):
             vnfr_descriptor["kdur"].append(kdur)
 
         vnfd_mgmt_cp = vnfd.get("mgmt-cp")
             vnfr_descriptor["kdur"].append(kdur)
 
         vnfd_mgmt_cp = vnfd.get("mgmt-cp")
+
         for vdu in vnfd.get("vdu", ()):
         for vdu in vnfd.get("vdu", ()):
+            vdu_mgmt_cp = []
+            try:
+                configs = vnfd.get("df")[0]["lcm-operations-configuration"]["operate-vnf-op-config"]["day1-2"]
+                vdu_config = utils.find_in_list(configs, lambda config: config["id"] == vdu["id"])
+            except Exception:
+                vdu_config = None
+            
+            if vdu_config:
+                external_connection_ee = utils.filter_in_list(
+                    vdu_config.get("execution-environment-list", []),
+                    lambda ee: "external-connection-point-ref" in ee
+                )
+                for ee in external_connection_ee:
+                    vdu_mgmt_cp.append(ee["external-connection-point-ref"])
+
             additional_params, vdu_params = self._format_additional_params(
                 ns_request, vnf_index, vdu_id=vdu["id"], descriptor=vnfd)
             vdur = {
             additional_params, vdu_params = self._format_additional_params(
                 ns_request, vnf_index, vdu_id=vdu["id"], descriptor=vnfd)
             vdur = {
@@ -560,12 +586,6 @@ class NsrTopic(BaseTopic):
                     "name": icp.get("id"),
                 }
 
                     "name": icp.get("id"),
                 }
 
-                if "port-security-enabled" in icp:
-                    vdu_icp["port-security-enabled"] = icp["port-security-enabled"]
-
-                if "port-security-disable-strategy" in icp:
-                    vdu_icp["port-security-disable-strategy"] = icp["port-security-disable-strategy"]
-
                 vdur["internal-connection-point"].append(vdu_icp)
 
                 for iface in icp.get("virtual-network-interface-requirement", ()):
                 vdur["internal-connection-point"].append(vdu_icp)
 
                 for iface in icp.get("virtual-network-interface-requirement", ()):
@@ -573,6 +593,12 @@ class NsrTopic(BaseTopic):
                     vdu_iface = {x: iface[x] for x in iface_fields if iface.get(x) is not None}
 
                     vdu_iface["internal-connection-point-ref"] = vdu_icp["id"]
                     vdu_iface = {x: iface[x] for x in iface_fields if iface.get(x) is not None}
 
                     vdu_iface["internal-connection-point-ref"] = vdu_icp["id"]
+                    if "port-security-enabled" in icp:
+                        vdu_iface["port-security-enabled"] = icp["port-security-enabled"]
+
+                    if "port-security-disable-strategy" in icp:
+                        vdu_iface["port-security-disable-strategy"] = icp["port-security-disable-strategy"]
+
                     for ext_cp in vnfd.get("ext-cpd", ()):
                         if not ext_cp.get("int-cpd"):
                             continue
                     for ext_cp in vnfd.get("ext-cpd", ()):
                         if not ext_cp.get("int-cpd"):
                             continue
@@ -580,11 +606,26 @@ class NsrTopic(BaseTopic):
                             continue
                         if icp["id"] == ext_cp["int-cpd"].get("cpd"):
                             vdu_iface["external-connection-point-ref"] = ext_cp.get("id")
                             continue
                         if icp["id"] == ext_cp["int-cpd"].get("cpd"):
                             vdu_iface["external-connection-point-ref"] = ext_cp.get("id")
+
+                            if "port-security-enabled" in ext_cp:
+                                vdu_iface["port-security-enabled"] = (
+                                    ext_cp["port-security-enabled"]
+                                )
+
+                            if "port-security-disable-strategy" in ext_cp:
+                                vdu_iface["port-security-disable-strategy"] = (
+                                    ext_cp["port-security-disable-strategy"]
+                                )
+
                             break
 
                     if vnfd_mgmt_cp and vdu_iface.get("external-connection-point-ref") == vnfd_mgmt_cp:
                         vdu_iface["mgmt-vnf"] = True
                             break
 
                     if vnfd_mgmt_cp and vdu_iface.get("external-connection-point-ref") == vnfd_mgmt_cp:
                         vdu_iface["mgmt-vnf"] = True
-                        vdu_iface["mgmt-interface"] = True  # TODO change to mgmt-vdu
+                        vdu_iface["mgmt-interface"] = True
+
+                    for ecp in vdu_mgmt_cp:
+                        if vdu_iface.get("external-connection-point-ref") == ecp:
+                            vdu_iface["mgmt-interface"] = True
 
                     if iface.get("virtual-interface"):
                         vdu_iface.update(deepcopy(iface["virtual-interface"]))
 
                     if iface.get("virtual-interface"):
                         vdu_iface.update(deepcopy(iface["virtual-interface"]))
@@ -595,13 +636,25 @@ class NsrTopic(BaseTopic):
                         # TODO: Change for multiple df support
                         for df in get_iterable(nsd.get("df")):
                             for vnf_profile in get_iterable(df.get("vnf-profile")):
                         # TODO: Change for multiple df support
                         for df in get_iterable(nsd.get("df")):
                             for vnf_profile in get_iterable(df.get("vnf-profile")):
-                                for vlc in get_iterable(vnf_profile.get("virtual-link-connectivity")):
+                                for vlc_index, vlc in \
+                                        enumerate(get_iterable(vnf_profile.get("virtual-link-connectivity"))):
                                     for cpd in get_iterable(vlc.get("constituent-cpd-id")):
                                         if cpd.get("constituent-cpd-id") == iface_ext_cp:
                                             vdu_iface["ns-vld-id"] = vlc.get("virtual-link-profile-id")
                                     for cpd in get_iterable(vlc.get("constituent-cpd-id")):
                                         if cpd.get("constituent-cpd-id") == iface_ext_cp:
                                             vdu_iface["ns-vld-id"] = vlc.get("virtual-link-profile-id")
+                                            # if iface type is SRIOV or PASSTHROUGH, set pci-interfaces flag to True
+                                            if vdu_iface.get("type") in ("SR-IOV", "PCI-PASSTHROUGH"):
+                                                nsr_descriptor["vld"][vlc_index]["pci-interfaces"] = True
                                             break
                     elif vdu_iface.get("internal-connection-point-ref"):
                         vdu_iface["vnf-vld-id"] = icp.get("int-virtual-link-desc")
                                             break
                     elif vdu_iface.get("internal-connection-point-ref"):
                         vdu_iface["vnf-vld-id"] = icp.get("int-virtual-link-desc")
+                        # TODO: store fixed IP address in the record (if it exists in the ICP)
+                        # if iface type is SRIOV or PASSTHROUGH, set pci-interfaces flag to True
+                        if vdu_iface.get("type") in ("SR-IOV", "PCI-PASSTHROUGH"):
+                            ivld_index = utils.find_index_in_list(vnfd.get("int-virtual-link-desc", ()),
+                                                                  lambda ivld:
+                                                                  ivld["id"] == icp.get("int-virtual-link-desc")
+                                                                  )
+                            vnfr_descriptor["vld"][ivld_index]["pci-interfaces"] = True
 
                     vdur["interfaces"].append(vdu_iface)
 
 
                     vdur["interfaces"].append(vdu_iface)
 
@@ -615,6 +668,19 @@ class NsrTopic(BaseTopic):
                 )
                 vdur["ns-image-id"] = nsr_sw_image_data["id"]
 
                 )
                 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"],
             flavor_data_name = vdu["id"][:56] + "-flv"
             nsr_flavor_desc = utils.find_in_list(
                 nsr_descriptor["flavor"],
@@ -696,18 +762,30 @@ class NsLcmOpTopic(BaseTopic):
             indata["member_vnf_index"] = indata.pop("vnf_member_index")  # for backward compatibility
         if indata.get("member_vnf_index"):
             vnfd = self._get_vnfd_from_vnf_member_index(indata["member_vnf_index"], nsr["_id"])
             indata["member_vnf_index"] = indata.pop("vnf_member_index")  # for backward compatibility
         if indata.get("member_vnf_index"):
             vnfd = self._get_vnfd_from_vnf_member_index(indata["member_vnf_index"], nsr["_id"])
+            try:
+                configs = vnfd.get("df")[0]["lcm-operations-configuration"]["operate-vnf-op-config"]["day1-2"]
+            except Exception:
+                configs = []
+
             if indata.get("vdu_id"):
                 self._check_valid_vdu(vnfd, indata["vdu_id"])
             if indata.get("vdu_id"):
                 self._check_valid_vdu(vnfd, indata["vdu_id"])
-                # TODO: Change the [0] as vdu-configuration is now a list
-                descriptor_configuration = vnfd.get("vdu-configuration", [{}])[0].get("config-primitive")
+                descriptor_configuration = utils.find_in_list(
+                    configs,
+                    lambda config: config["id"] == indata["vdu_id"]
+                )
             elif indata.get("kdu_name"):
                 self._check_valid_kdu(vnfd, indata["kdu_name"])
             elif indata.get("kdu_name"):
                 self._check_valid_kdu(vnfd, indata["kdu_name"])
-                # TODO: Change the [0] as kdu-configuration is now a list
-                kdud = next((k for k in vnfd["kdu"] if k["name"] == indata["kdu_name"]), None)
-                descriptor_configuration = deep_get(kdud, ("kdu-configuration", "config-primitive"))
+                descriptor_configuration = utils.find_in_list(
+                    configs,
+                    lambda config: config["id"] == indata.get("kdu_name")
+                )
             else:
             else:
-                # TODO: Change the [0] as vnf-configuration is now a list
-                descriptor_configuration = vnfd.get("vnf-configuration", [{}])[0].get("config-primitive")
+                descriptor_configuration = utils.find_in_list(
+                    configs,
+                    lambda config: config["id"] == vnfd["id"]
+                )
+            if descriptor_configuration is not None:
+                descriptor_configuration = descriptor_configuration.get("config-primitive")
         else:  # use a NSD
             descriptor_configuration = nsd.get("ns-configuration", {}).get("config-primitive")
 
         else:  # use a NSD
             descriptor_configuration = nsd.get("ns-configuration", {}).get("config-primitive")