Fix Bug 2181 92/12592/2
authorGulsum Atici <gulsum.atici@canonical.com>
Sun, 16 Oct 2022 18:13:53 +0000 (21:13 +0300)
committerbeierlm <mark.beierl@canonical.com>
Sun, 16 Oct 2022 18:48:12 +0000 (20:48 +0200)
VDU level charm application name creation problem has been fixed.

Change-Id: Ie9e039ae2a3b8734dc6bf0b6dfc14b3455941b84
Signed-off-by: Gulsum Atici <gulsum.atici@canonical.com>
n2vc/n2vc_juju_conn.py
n2vc/tests/unit/test_n2vc_juju_conn.py

index c6d00a8..da09fdb 100644 (file)
@@ -1294,6 +1294,7 @@ class N2VCJujuConnector(N2VCConnector):
         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
@@ -1301,10 +1302,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:
@@ -1344,26 +1346,26 @@ 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 = next(
+                filter(lambda record: record["vdu_id"] == vdu_id, vca_records), {}
+            )
             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"
@@ -1474,6 +1476,7 @@ class N2VCJujuConnector(N2VCConnector):
                 db_vnfr,
                 vca_records,
                 vnf_count=vnf_count,
+                vdu_id=vdu_id,
                 vdu_count=vdu_count,
             )
         else:
index 8720d96..8324006 100644 (file)
@@ -473,12 +473,14 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
         ]
         vnf_count = ""
         vdu_count = ""
+        vdu_id = None
         expected_result = "simple-ns-charm-abc-000-rrrr-nnnn-4444-h-ns"
         application_name = self.n2vc._generate_application_name(
             charm_level,
             vnfrs,
             vca_records,
             vnf_count=vnf_count,
+            vdu_id=vdu_id,
             vdu_count=vdu_count,
         )
         self.assertEqual(application_name, expected_result)
@@ -490,12 +492,14 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
         vca_records = []
         vnf_count = ""
         vdu_count = ""
+        vdu_id = None
         with self.assertRaises(N2VCException):
             self.n2vc._generate_application_name(
                 charm_level,
                 vnfrs,
                 vca_records,
                 vnf_count=vnf_count,
+                vdu_id=vdu_id,
                 vdu_count=vdu_count,
             )
 
@@ -516,12 +520,14 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
         ]
         vnf_count = "1"
         vdu_count = ""
+        vdu_id = None
         expected_result = "simple-ee-ab-1-vnf111-xxx-y-vnf"
         application_name = self.n2vc._generate_application_name(
             charm_level,
             vnfrs,
             vca_records,
             vnf_count=vnf_count,
+            vdu_id=vdu_id,
             vdu_count=vdu_count,
         )
         self.assertEqual(application_name, expected_result)
@@ -540,7 +546,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
             {
                 "target_element": "vnf/vnf1/mgmtvm",
                 "member-vnf-index": "vnf111-xxx-yyy-zzz",
-                "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
+                "vdu_id": "mgmtVM",
                 "kdu_name": None,
                 "vdu_count_index": None,
                 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
@@ -564,18 +570,20 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
         ]
         vnf_count = "2"
         vdu_count = "0"
+        vdu_id = "mgmtVM"
         expected_result = "simple-ee-ab-2-vnf111-xxx-y-mgmtVM-0-vdu"
         application_name = self.n2vc._generate_application_name(
             charm_level,
             vnfrs,
             vca_records,
             vnf_count=vnf_count,
+            vdu_id=vdu_id,
             vdu_count=vdu_count,
         )
         self.assertEqual(application_name, expected_result)
         self.assertLess(len(application_name), 50)
 
-    def test_generate_application_name_vdu_charm_wrong_vnfrs(self):
+    def test_generate_application_name_vdu_charm_vdu_id_none(self):
         charm_level = "vdu-level"
         vnfrs = {
             "member-vnf-index-ref": "vnf111-xxx-yyy-zzz",
@@ -584,7 +592,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
             {
                 "target_element": "vnf/vnf1/mgmtvm",
                 "member-vnf-index": "vnf111-xxx-yyy-zzz",
-                "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
+                "vdu_id": None,
                 "kdu_name": None,
                 "vdu_count_index": None,
                 "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
@@ -596,12 +604,14 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
         ]
         vnf_count = "2"
         vdu_count = "0"
-        with self.assertRaises(KeyError):
+        vdu_id = None
+        with self.assertRaises(N2VCException):
             self.n2vc._generate_application_name(
                 charm_level,
                 vnfrs,
                 vca_records,
                 vnf_count=vnf_count,
+                vdu_id=vdu_id,
                 vdu_count=vdu_count,
             )
 
@@ -1011,7 +1021,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
                         {
                             "target_element": "vnf/vnf1/mgmtvm",
                             "member-vnf-index": "vnf111-xxx-yyy-zzz",
-                            "vdu_id": "38912ff7-5bdd-4228-911f-c2bee259c44a",
+                            "vdu_id": "mgmtVM",
                             "kdu_name": None,
                             "vdu_count_index": None,
                             "vnfd_id": "r7fbd751-3de4-4e68-bd40-ec5ae0a53898",
@@ -1067,7 +1077,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
                         {
                             "target_element": "vnf/openldap/kdu/ldap",
                             "member-vnf-index": "openldap",
-                            "vdu_id": None,
+                            "vdu_id": "ldap",
                             "kdu_name": "ldap",
                             "vdu_count_index": 0,
                             "operational-status": "init",
@@ -1094,7 +1104,7 @@ class GenerateApplicationNameTest(N2VCJujuConnTestCase):
         }
         vnf_count = "0"
         mock_vnf_count_and_record.return_value = (vnf_count, db_vnfr)
-        expected_result = "openldap-ee-z0-openldap-vdu"
+        expected_result = "openldap-ee-z0-openldap-ldap-vdu"
         with patch.object(self.n2vc, "db", self.db), patch.object(
             self.n2vc, "_get_vnf_count_and_record", mock_vnf_count_and_record
         ):