Get model names activity implementation 59/13659/5
authorDaniel Arndt <daniel.arndt@canonical.com>
Mon, 10 Jul 2023 12:51:58 +0000 (09:51 -0300)
committeraticig <gulsum.atici@canonical.com>
Tue, 18 Jul 2023 09:34:43 +0000 (11:34 +0200)
Change-Id: I4f80ad212b61ccc3f5d01e646cf74ecb7f31a5f2
Signed-off-by: Daniel Arndt <daniel.arndt@canonical.com>
osm_lcm/temporal/vnf_activities.py
osm_lcm/tests/test_vnf_activities.py

index d47318f..3ea8c64 100644 (file)
@@ -21,6 +21,7 @@ from osm_common.temporal.activities.vnf import (
     GetVimCloud,
     GetVnfDescriptor,
     GetVnfRecord,
+    GetModelNames,
     ChangeVnfState,
     ChangeVnfInstantiationState,
     SetVnfModel,
@@ -171,3 +172,20 @@ class SetVnfModelImpl(SetVnfModel):
 class SendNotificationForVnfImpl(SendNotificationForVnf):
     async def __call__(self, activity_input: SendNotificationForVnf.Input) -> None:
         self.logger.debug("Send notification for VNF not implemented.")
+
+
+@activity.defn(name=GetModelNames.__name__)
+class GetModelNamesImpl(GetModelNames):
+    async def __call__(
+        self, activity_input: GetModelNames.Input
+    ) -> GetModelNames.Output:
+        ns_id = activity_input.ns_uuid
+
+        vnfrs = self.db.get_list("vnfrs", {"nsr-id-ref": ns_id})
+        self.logger.debug(
+            f"Retrieved {len(vnfrs)} vnf records matching {ns_id} from database."
+        )
+
+        return GetModelNames.Output(
+            model_names={vnfr["namespace"] for vnfr in vnfrs if "namespace" in vnfr}
+        )
index 1f34563..374a648 100644 (file)
@@ -24,6 +24,7 @@ from osm_common.temporal_task_queues.task_queues_mappings import LCM_TASK_QUEUE
 from osm_lcm.temporal.vnf_activities import (
     ChangeVnfInstantiationStateImpl,
     ChangeVnfStateImpl,
+    GetModelNamesImpl,
     GetTaskQueueImpl,
     GetVimCloudImpl,
     GetVnfDescriptorImpl,
@@ -42,7 +43,7 @@ vim_uuid = "a64f7c6c-bc27-4ec8-b664-5500a3324eca"
 model_name = "my-model-name"
 set_vnf_model_input = SetVnfModelImpl.Input(vnfr_uuid=vnfr_uuid, model_name=model_name)
 cloud = "microk8s"
-nsr_uuid = "583726d4-957d-47f5-8df5-199456f7afd0"
+nsr_uuid = "dcf4c922-5a73-41bf-a6ca-870c22d6336c"
 sample_vim_record = {
     "_id": vim_uuid,
     "name": "juju",
@@ -64,7 +65,7 @@ vnf_index = "vnf-profile-id"
 sample_vnfr = {
     "_id": vnfr_uuid,
     "id": vnfr_uuid,
-    "nsr-id-ref": "dcf4c922-5a73-41bf-a6ca-870c22d6336c",
+    "nsr-id-ref": nsr_uuid,
     "vnfd-ref": "jar_vnfd_scalable",
     "vnfd-id": "f1b38eac-190c-485d-9a74-c6e169c929d8",
     "vim-account-id": vim_account_id,
@@ -495,5 +496,38 @@ class TestGetVduInstantiateInfoMethods(TestCase):
         self.assertEqual(result, {})
 
 
+class TestGetModelNames(asynctest.TestCase):
+    async def setUp(self):
+        self.db = Mock()
+        self.get_model_names = GetModelNamesImpl(self.db)
+        self.env = ActivityEnvironment()
+
+    async def test_activity__success(self):
+        # Two namespaces, one of them repeated
+        self.db.get_list.return_value = [
+            {
+                "nsr-id-ref": nsr_uuid,
+                "namespace": namespace,
+            }
+            for namespace in ["namespace1", "namespace1", "namespace2"]
+        ]
+
+        activity_result = await self.env.run(
+            self.get_model_names,
+            GetModelNamesImpl.Input(ns_uuid=nsr_uuid),
+        )
+
+        self.assertEqual(activity_result.model_names, {"namespace1", "namespace2"})
+
+    async def test_activity__raise_db_exception(self):
+        self.db.get_list.side_effect = DbException("Can not connect to Database.")
+
+        with self.assertRaises(DbException):
+            await self.env.run(
+                self.get_model_names,
+                GetModelNamesImpl.Input(ns_uuid=nsr_uuid),
+            )
+
+
 if __name__ == "__main__":
     asynctest.main()