Reformat files according to new black validation
[osm/LCM.git] / osm_lcm / ns.py
index 5d960c5..915581f 100644 (file)
@@ -245,7 +245,6 @@ class NsLcm(LcmBase):
         return None
 
     def _on_update_ro_db(self, nsrs_id, ro_descriptor):
-
         # self.logger.debug('_on_update_ro_db(nsrs_id={}'.format(nsrs_id))
 
         try:
@@ -263,7 +262,6 @@ class NsLcm(LcmBase):
             )
 
     async def _on_update_n2vc_db(self, table, filter, path, updated_data, vca_id=None):
-
         # remove last dot from path (if exists)
         if path.endswith("."):
             path = path[:-1]
@@ -271,7 +269,6 @@ class NsLcm(LcmBase):
         # self.logger.debug('_on_update_n2vc_db(table={}, filter={}, path={}, updated_data={}'
         #                   .format(table, filter, path, updated_data))
         try:
-
             nsr_id = filter.get("_id")
 
             # read ns record from database
@@ -532,7 +529,6 @@ class NsLcm(LcmBase):
             return wim_account
 
     def scale_vnfr(self, db_vnfr, vdu_create=None, vdu_delete=None, mark_delete=False):
-
         db_vdu_push_list = []
         template_vdur = []
         db_update = {"_admin.modified": time()}
@@ -820,7 +816,6 @@ class NsLcm(LcmBase):
         start_deploy,
         timeout_ns_deploy,
     ):
-
         db_vims = {}
 
         def get_vim_account(vim_account_id):
@@ -915,7 +910,6 @@ class NsLcm(LcmBase):
                             lambda v_vld: v_vld["name"] in (a_vld["name"], a_vld["id"]),
                         )
                         if target_vld:
-
                             if vnf_params.get("vimAccountId") not in a_vld.get(
                                 "vim_info", {}
                             ):
@@ -1014,16 +1008,16 @@ class NsLcm(LcmBase):
             # check if this network needs SDN assist
             if vld.get("pci-interfaces"):
                 db_vim = get_vim_account(ns_params["vimAccountId"])
-                sdnc_id = db_vim["config"].get("sdn-controller")
-                if sdnc_id:
-                    sdn_vld = "nsrs:{}:vld.{}".format(nsr_id, vld["id"])
-                    target_sdn = "sdn:{}".format(sdnc_id)
-                    target_vld["vim_info"][target_sdn] = {
-                        "sdn": True,
-                        "target_vim": target_vim,
-                        "vlds": [sdn_vld],
-                        "type": vld.get("type"),
-                    }
+                if vim_config := db_vim.get("config"):
+                    if sdnc_id := vim_config.get("sdn-controller"):
+                        sdn_vld = "nsrs:{}:vld.{}".format(nsr_id, vld["id"])
+                        target_sdn = "sdn:{}".format(sdnc_id)
+                        target_vld["vim_info"][target_sdn] = {
+                            "sdn": True,
+                            "target_vim": target_vim,
+                            "vlds": [sdn_vld],
+                            "type": vld.get("type"),
+                        }
 
             nsd_vnf_profiles = get_vnf_profiles(nsd)
             for nsd_vnf_profile in nsd_vnf_profiles:
@@ -1568,7 +1562,6 @@ class NsLcm(LcmBase):
         ro_retries = 0
 
         while True:
-
             ro_retries += 1
             if ro_retries >= 360:  # 1 hour
                 raise LcmException(
@@ -1753,7 +1746,6 @@ class NsLcm(LcmBase):
         }
         step = ""
         try:
-
             element_type = "NS"
             element_under_configuration = nsr_id
 
@@ -1837,7 +1829,6 @@ class NsLcm(LcmBase):
             vca_id = self.get_vca_id(db_vnfr, db_nsr)
             # create or register execution environment in VCA
             if vca_type in ("lxc_proxy_charm", "k8s_proxy_charm", "helm", "helm-v3"):
-
                 self._write_configuration_status(
                     nsr_id=nsr_id,
                     vca_index=vca_index,
@@ -2157,6 +2148,7 @@ class NsLcm(LcmBase):
                     vnfr_id=vnfr_id,
                     nsr_id=nsr_id,
                     target_ip=rw_mgmt_ip,
+                    element_type=element_type,
                     vnf_member_index=db_vnfr.get("member-vnf-index-ref", ""),
                     vdu_id=vdu_id,
                     vdu_index=vdu_index,
@@ -2297,7 +2289,6 @@ class NsLcm(LcmBase):
         element_type: str = None,
         other_update: dict = None,
     ):
-
         # self.logger.debug('_write_configuration_status(): vca_index={}, status={}'
         #                   .format(vca_index, status))
 
@@ -2999,6 +2990,9 @@ class NsLcm(LcmBase):
         cached_vnfds: Dict[str, Any],
     ) -> List[Relation]:
         relations = []
+        if vca.target_element == "ns":
+            self.logger.debug("VCA is a NS charm, not a VNF.")
+            return relations
         vnf_profile = get_vnf_profile(nsd, vca.vnf_profile_id)
         vnf_profile_id = vnf_profile["id"]
         vnfd_id = vnf_profile["vnfd-id"]
@@ -3184,7 +3178,6 @@ class NsLcm(LcmBase):
         vca_index: int,
         timeout: int = 3600,
     ) -> bool:
-
         # steps:
         # 1. find all relations for this VCA
         # 2. wait for other peers related
@@ -3262,7 +3255,6 @@ class NsLcm(LcmBase):
         timeout: int = 600,
         vca_id: str = None,
     ):
-
         try:
             k8sclustertype = k8s_instance_info["k8scluster-type"]
             # Instantiate kdu
@@ -5517,7 +5509,6 @@ class NsLcm(LcmBase):
             raise
 
         except Exception as e:
-
             self.logger.debug("Error upgrading charm {}".format(path))
 
             return "FAILED", "Error upgrading charm {}: {}".format(path, e)
@@ -5581,7 +5572,6 @@ class NsLcm(LcmBase):
             nsr_deployed = db_nsr["_admin"].get("deployed")
 
             if update_type == "CHANGE_VNFPKG":
-
                 # Get the input parameters given through update request
                 vnf_instance_id = db_nslcmop["operationParams"][
                     "changeVnfPackageData"
@@ -5621,7 +5611,6 @@ class NsLcm(LcmBase):
 
                 step = "Checking if revision has changed in VNFD"
                 if current_vnf_revision != latest_vnfd_revision:
-
                     change_type = "policy_updated"
 
                     # There is new revision of VNFD, update operation is required
@@ -5658,7 +5647,6 @@ class NsLcm(LcmBase):
 
                             step = "Getting descriptor config"
                             if current_vnfd.get("kdu"):
-
                                 search_key = "kdu_name"
                             else:
                                 search_key = "vnfd_id"
@@ -5679,7 +5667,6 @@ class NsLcm(LcmBase):
                             # There could be several charm used in the same VNF
                             for ee_item in ee_list:
                                 if ee_item.get("juju"):
-
                                     step = "Getting charm name"
                                     charm_name = ee_item["juju"].get("charm")
 
@@ -5741,7 +5728,6 @@ class NsLcm(LcmBase):
                     if find_software_version(current_vnfd) != find_software_version(
                         latest_vnfd
                     ):
-
                         step = "Checking if existing VNF has charm"
                         for current_charm_path, target_charm_path in list(
                             charm_artifact_paths
@@ -5783,10 +5769,8 @@ class NsLcm(LcmBase):
                                     current_charm_path, target_charm_path
                                 )
                             ):
-
                                 step = "Checking whether VNF uses juju bundle"
                                 if check_juju_bundle_existence(current_vnfd):
-
                                     raise LcmException(
                                         "Charm upgrade is not supported for the instance which"
                                         " uses juju-bundle: {}".format(
@@ -7270,6 +7254,7 @@ class NsLcm(LcmBase):
         vnfr_id: str,
         nsr_id: str,
         target_ip: str,
+        element_type: str,
         vnf_member_index: str = "",
         vdu_id: str = "",
         vdu_index: int = None,
@@ -7286,6 +7271,7 @@ class NsLcm(LcmBase):
             vnfr_id (str): VNFR ID where this EE applies
             nsr_id (str): NSR ID where this EE applies
             target_ip (str): VDU/KDU instance IP address
+            element_type (str): NS or VNF or VDU or KDU
             vnf_member_index (str, optional): VNF index where this EE applies. Defaults to "".
             vdu_id (str, optional): VDU ID where this EE applies. Defaults to "".
             vdu_index (int, optional): VDU index where this EE applies. Defaults to None.
@@ -7298,7 +7284,11 @@ class NsLcm(LcmBase):
         Returns:
             _type_: Prometheus jobs
         """
-        self.logger.debug(f"KDU: {kdu_name}; KDU INDEX: {kdu_index}")
+        # default the vdur and kdur names to an empty string, to avoid any later
+        # problem with Prometheus when the element type is not VDU or KDU
+        vdur_name = ""
+        kdur_name = ""
+
         # look if exist a file called 'prometheus*.j2' and
         artifact_content = self.fs.dir_ls(artifact_path)
         job_file = next(
@@ -7314,51 +7304,51 @@ class NsLcm(LcmBase):
         with self.fs.file_open((artifact_path, job_file), "r") as f:
             job_data = f.read()
 
-        vdur_name = ""
-        kdur_name = ""
-        for r in range(360):
-            db_vnfr = self.db.get_one("vnfrs", {"_id": vnfr_id})
-            if vdu_id and vdu_index is not None:
-                vdur = next(
-                    (
-                        x
-                        for x in get_iterable(db_vnfr, "vdur")
-                        if (
-                            x.get("vdu-id-ref") == vdu_id
-                            and x.get("count-index") == vdu_index
-                        )
-                    ),
-                    {},
-                )
-                if vdur.get("name"):
-                    vdur_name = vdur.get("name")
-                    break
-            if kdu_name and kdu_index is not None:
-                kdur = next(
-                    (
-                        x
-                        for x in get_iterable(db_vnfr, "kdur")
-                        if (
-                            x.get("kdu-name") == kdu_name
-                            and x.get("count-index") == kdu_index
-                        )
-                    ),
-                    {},
-                )
-                if kdur.get("name"):
-                    kdur_name = kdur.get("name")
-                    break
+        # obtain the VDUR or KDUR, if the element type is VDU or KDU
+        if element_type in ("VDU", "KDU"):
+            for _ in range(360):
+                db_vnfr = self.db.get_one("vnfrs", {"_id": vnfr_id})
+                if vdu_id and vdu_index is not None:
+                    vdur = next(
+                        (
+                            x
+                            for x in get_iterable(db_vnfr, "vdur")
+                            if (
+                                x.get("vdu-id-ref") == vdu_id
+                                and x.get("count-index") == vdu_index
+                            )
+                        ),
+                        {},
+                    )
+                    if vdur.get("name"):
+                        vdur_name = vdur.get("name")
+                        break
+                if kdu_name and kdu_index is not None:
+                    kdur = next(
+                        (
+                            x
+                            for x in get_iterable(db_vnfr, "kdur")
+                            if (
+                                x.get("kdu-name") == kdu_name
+                                and x.get("count-index") == kdu_index
+                            )
+                        ),
+                        {},
+                    )
+                    if kdur.get("name"):
+                        kdur_name = kdur.get("name")
+                        break
 
-            await asyncio.sleep(10, loop=self.loop)
-        else:
-            if vdu_id and vdu_index is not None:
-                raise LcmException(
-                    f"Timeout waiting VDU with name={vdu_id} and index={vdu_index} to be intantiated"
-                )
-            if kdu_name and kdu_index is not None:
-                raise LcmException(
-                    f"Timeout waiting KDU with name={kdu_name} and index={kdu_index} to be intantiated"
-                )
+                await asyncio.sleep(10, loop=self.loop)
+            else:
+                if vdu_id and vdu_index is not None:
+                    raise LcmException(
+                        f"Timeout waiting VDU with name={vdu_id} and index={vdu_index} to be intantiated"
+                    )
+                if kdu_name and kdu_index is not None:
+                    raise LcmException(
+                        f"Timeout waiting KDU with name={kdu_name} and index={kdu_index} to be intantiated"
+                    )
 
         # TODO get_service
         _, _, service = ee_id.partition(".")  # remove prefix   "namespace."
@@ -7374,6 +7364,7 @@ class NsLcm(LcmBase):
             "VNF_MEMBER_INDEX": vnf_member_index,
             "VDUR_NAME": vdur_name,
             "KDUR_NAME": kdur_name,
+            "ELEMENT_TYPE": element_type,
         }
         job_list = parse_job(job_data, variables)
         # ensure job_name is using the vnfr_id. Adding the metadata nsr_id
@@ -8166,7 +8157,6 @@ class NsLcm(LcmBase):
         }
         step = ""
         try:
-
             element_type = "NS"
             element_under_configuration = nsr_id