v13.0 Branch Preparation
[osm/N2VC.git] / n2vc / n2vc_juju_conn.py
index f0569b1..64e338c 100644 (file)
@@ -1061,15 +1061,17 @@ class N2VCJujuConnector(N2VCConnector):
                 if status == "completed":
                     return output
                 else:
-                    raise Exception("status is not completed: {}".format(status))
+                    if "output" in output:
+                        raise Exception(f'{status}: {output["output"]}')
+                    else:
+                        raise Exception(
+                            f"{status}: No further information received from action"
+                        )
+
             except Exception as e:
-                self.log.error(
-                    "Error executing primitive {}: {}".format(primitive_name, e)
-                )
+                self.log.error(f"Error executing primitive {primitive_name}: {e}")
                 raise N2VCExecutionException(
-                    message="Error executing primitive {} into ee={} : {}".format(
-                        primitive_name, ee_id, e
-                    ),
+                    message=f"Error executing primitive {primitive_name} in ee={ee_id}: {e}",
                     primitive_name=primitive_name,
                 )
 
@@ -1286,12 +1288,30 @@ class N2VCJujuConnector(N2VCConnector):
         )
         return application_name
 
+    @staticmethod
+    def _get_vca_record(search_key: str, vca_records: list, vdu_id: str) -> dict:
+        """Get the correct VCA record dict depending on the search key
+
+        Args:
+            search_key  (str):      keyword to find the correct VCA record
+            vca_records (list):     All VCA records as list
+            vdu_id  (str):          VDU ID
+
+        Returns:
+            vca_record  (dict):     Dictionary which includes the correct VCA record
+
+        """
+        return next(
+            filter(lambda record: record[search_key] == vdu_id, vca_records), {}
+        )
+
     @staticmethod
     def _generate_application_name(
         charm_level: str,
         vnfrs: dict,
         vca_records: list,
         vnf_count: str = None,
+        vdu_id: str = None,
         vdu_count: str = None,
     ) -> str:
         """Generate application name to make the relevant charm of VDU/KDU
@@ -1299,10 +1319,11 @@ class N2VCJujuConnector(N2VCConnector):
         Limiting the app name to 50 characters.
 
         Args:
-            charm_level  (str):  VNF ID
-            vnfrs  (dict):  VDU ID
+            charm_level  (str):  level of charm
+            vnfrs  (dict):  vnf record dict
             vca_records   (list):   db_nsr["_admin"]["deployed"]["VCA"] as list
             vnf_count   (str): vnf count index
+            vdu_id   (str):  VDU ID
             vdu_count   (str):  vdu count index
 
         Returns:
@@ -1342,26 +1363,33 @@ class N2VCJujuConnector(N2VCConnector):
             # Charms are also used for deployments with Helm charts.
             # If deployment unit is a Helm chart/KDU,
             # vdu_profile_id and vdu_count will be empty string.
-            vdu_profile_id = ""
-
             if vdu_count is None:
                 vdu_count = ""
 
-            elif vdu_count:
-                vdu_profile_id = vnfrs["vdur"][int(vdu_count)]["vdu-id-ref"]
-
             # If vnf/vdu is scaled, more than one VCA record may be included in vca_records
             # but ee_descriptor_id is same.
             # Shorten the ee_descriptor_id, member-vnf-index-ref and vdu_profile_id
             # to first 12 characters.
+            if not vdu_id:
+                raise N2VCException(message="vdu-id should be provided.")
+
+            vca_record = N2VCJujuConnector._get_vca_record(
+                "vdu_id", vca_records, vdu_id
+            )
+
+            if not vca_record:
+                vca_record = N2VCJujuConnector._get_vca_record(
+                    "kdu_name", vca_records, vdu_id
+                )
+
             application_name = (
-                vca_records[0]["ee_descriptor_id"][:12]
+                vca_record["ee_descriptor_id"][:12]
                 + "-"
                 + vnf_count
                 + "-"
                 + vnfrs["member-vnf-index-ref"][:12]
                 + "-"
-                + vdu_profile_id[:12]
+                + vdu_id[:12]
                 + "-"
                 + vdu_count
                 + "-vdu"
@@ -1472,6 +1500,7 @@ class N2VCJujuConnector(N2VCConnector):
                 db_vnfr,
                 vca_records,
                 vnf_count=vnf_count,
+                vdu_id=vdu_id,
                 vdu_count=vdu_count,
             )
         else: