From c5982f5c43826fbad2af8a7e6d30199c3b22d3e2 Mon Sep 17 00:00:00 2001 From: Daniel Arndt Date: Mon, 10 Jul 2023 09:51:58 -0300 Subject: [PATCH] Get model names activity implementation Change-Id: I4f80ad212b61ccc3f5d01e646cf74ecb7f31a5f2 Signed-off-by: Daniel Arndt --- osm_lcm/temporal/vnf_activities.py | 18 +++++++++++++ osm_lcm/tests/test_vnf_activities.py | 38 ++++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/osm_lcm/temporal/vnf_activities.py b/osm_lcm/temporal/vnf_activities.py index d47318f..3ea8c64 100644 --- a/osm_lcm/temporal/vnf_activities.py +++ b/osm_lcm/temporal/vnf_activities.py @@ -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} + ) diff --git a/osm_lcm/tests/test_vnf_activities.py b/osm_lcm/tests/test_vnf_activities.py index 1f34563..374a648 100644 --- a/osm_lcm/tests/test_vnf_activities.py +++ b/osm_lcm/tests/test_vnf_activities.py @@ -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() -- 2.25.1