OSMENG-1062: Fix states in VnfInstantiateWorkflow 93/13393/2
authorDario Faccin <dario.faccin@canonical.com>
Mon, 15 May 2023 09:42:06 +0000 (11:42 +0200)
committerDario Faccin <dario.faccin@canonical.com>
Tue, 16 May 2023 09:35:44 +0000 (11:35 +0200)
At the beginning of the workflow, update only VnfInstantiationState
(NOT_INSTANTIATED).
At the end of the workflow, update VnfInstantationState and VnfState:
* if succeeded: INSTANTIATED, STARTED
* if failed: INSTANTIATED, STOPPED

Change-Id: Ifb6df225d4e09f48941ce649ad3195d2b4e8b3b4
Signed-off-by: Dario Faccin <dario.faccin@canonical.com>
osm_lcm/temporal/vnf_workflows.py
osm_lcm/tests/test_vnf_workflows.py

index d3f0a98..c97f908 100644 (file)
@@ -73,16 +73,18 @@ class VnfInstantiateWorkflow:
     @workflow.run
     async def run(self, input: VnfInstantiateInput) -> None:
         self.logger.info(f"Deploying VNF {input.vnfr_uuid}")
-        vnf_instantiation_state = ChangeVnfInstantiationStateInput(
-            vnfr_uuid=input.vnfr_uuid, state=VnfInstantiationState.NOT_INSTANTIATED
-        )
-        vnf_state = ChangeVnfStateInput(
-            vnfr_uuid=input.vnfr_uuid, state=VnfState.STOPPED
-        )
         try:
-            await self.update_states(
-                vnf_instantiation_state=vnf_instantiation_state,
-                vnf_state=vnf_state,
+            await self.update_vnf_instantiation_state(
+                ChangeVnfInstantiationStateInput(
+                    vnfr_uuid=input.vnfr_uuid,
+                    state=VnfInstantiationState.NOT_INSTANTIATED,
+                ),
+            )
+            await self.send_notification_for_vnf(
+                ChangeVnfInstantiationStateInput(
+                    vnfr_uuid=input.vnfr_uuid,
+                    state=VnfInstantiationState.NOT_INSTANTIATED,
+                ),
             )
             vnf_task_queue = value_to_type(
                 GetTaskQueueOutput,
@@ -135,38 +137,70 @@ class VnfInstantiateWorkflow:
                 task_queue=vnf_task_queue.task_queue,
                 cloud=get_cloud.cloud,
             )
-            await self.update_states(
-                vnf_instantiation_state=ChangeVnfInstantiationStateInput(
+            await self.update_vnf_instantiation_state(
+                ChangeVnfInstantiationStateInput(
                     vnfr_uuid=input.vnfr_uuid, state=VnfInstantiationState.INSTANTIATED
                 ),
-                vnf_state=ChangeVnfStateInput(
-                    vnfr_uuid=input.vnfr_uuid, state=VnfState.STARTED
+            )
+            await self.update_vnf_state(
+                ChangeVnfStateInput(vnfr_uuid=input.vnfr_uuid, state=VnfState.STARTED),
+            )
+            await self.send_notification_for_vnf(
+                ChangeVnfInstantiationStateInput(
+                    vnfr_uuid=input.vnfr_uuid, state=VnfInstantiationState.INSTANTIATED
                 ),
             )
 
         except ActivityError as e:
             err_details = str(e.cause.with_traceback(e.__traceback__))
-            await self.update_states(
-                vnf_instantiation_state=vnf_instantiation_state,
-                vnf_state=vnf_state,
+            await self.update_vnf_instantiation_state(
+                ChangeVnfInstantiationStateInput(
+                    vnfr_uuid=input.vnfr_uuid, state=VnfInstantiationState.INSTANTIATED
+                ),
+            )
+            await self.update_vnf_state(
+                ChangeVnfStateInput(vnfr_uuid=input.vnfr_uuid, state=VnfState.STOPPED),
+            )
+            await self.send_notification_for_vnf(
+                ChangeVnfInstantiationStateInput(
+                    vnfr_uuid=input.vnfr_uuid, state=VnfInstantiationState.INSTANTIATED
+                ),
             )
             self.logger.error(f"{WORKFLOW_VNF_INSTANTIATE} failed with {err_details}")
             raise e
 
         except ChildWorkflowError as e:
             err_details = str(e.cause.cause.with_traceback(e.cause.__traceback__))
-            await self.update_states(
-                vnf_instantiation_state=vnf_instantiation_state,
-                vnf_state=vnf_state,
+            await self.update_vnf_instantiation_state(
+                ChangeVnfInstantiationStateInput(
+                    vnfr_uuid=input.vnfr_uuid, state=VnfInstantiationState.INSTANTIATED
+                ),
+            )
+            await self.update_vnf_state(
+                ChangeVnfStateInput(vnfr_uuid=input.vnfr_uuid, state=VnfState.STOPPED),
+            )
+            await self.send_notification_for_vnf(
+                ChangeVnfInstantiationStateInput(
+                    vnfr_uuid=input.vnfr_uuid, state=VnfInstantiationState.INSTANTIATED
+                ),
             )
             self.logger.error(f"{WORKFLOW_VNF_INSTANTIATE} failed with {err_details}")
             raise e
 
         except Exception as e:
             err_details = str(traceback.format_exc())
-            await self.update_states(
-                vnf_instantiation_state=vnf_instantiation_state,
-                vnf_state=vnf_state,
+            await self.update_vnf_instantiation_state(
+                ChangeVnfInstantiationStateInput(
+                    vnfr_uuid=input.vnfr_uuid, state=VnfInstantiationState.INSTANTIATED
+                ),
+            )
+            await self.update_vnf_state(
+                ChangeVnfStateInput(vnfr_uuid=input.vnfr_uuid, state=VnfState.STOPPED),
+            )
+            await self.send_notification_for_vnf(
+                ChangeVnfInstantiationStateInput(
+                    vnfr_uuid=input.vnfr_uuid, state=VnfInstantiationState.INSTANTIATED
+                ),
             )
             self.logger.error(f"{WORKFLOW_VNF_INSTANTIATE} failed with {err_details}")
             raise e
@@ -278,15 +312,6 @@ class VnfInstantiateWorkflow:
         )
         return vdu_instantiate_input, vdu_instantiate_workflow_id
 
-    async def update_states(
-        self,
-        vnf_instantiation_state: ChangeVnfInstantiationStateInput,
-        vnf_state: ChangeVnfStateInput,
-    ):
-        await self.update_vnf_instantiation_state(vnf_instantiation_state)
-        await self.update_vnf_state(vnf_state)
-        await self.send_notification_for_vnf(vnf_instantiation_state)
-
 
 @workflow.defn(name=WORKFLOW_VNF_PREPARE, sandboxed=_SANDBOXED)
 class VnfPrepareWorkflow:
index 22c012a..073b767 100644 (file)
@@ -285,7 +285,7 @@ class TestVnfInstantiateWorkflow(asynctest.TestCase):
 
     def check_state_change_call_counts(self):
         self.assertEqual(self.mock_change_vnf_instantiation_state_tracker.call_count, 2)
-        self.assertEqual(self.mock_change_vnf_state_tracker.call_count, 2)
+        self.assertEqual(self.mock_change_vnf_state_tracker.call_count, 1)
         self.assertEqual(self.mock_send_notification_for_vnf_tracker.call_count, 2)
 
     def workflow_is_succeeded(self):
@@ -302,7 +302,7 @@ class TestVnfInstantiateWorkflow(asynctest.TestCase):
             ),
         )
         self.assertEqual(
-            call_mock_change_vnf_state_tracker[1].args[0],
+            call_mock_change_vnf_state_tracker[0].args[0],
             ChangeVnfStateInput(vnfr_uuid=vnfr_uuid, state=VnfState.STARTED),
         )
 
@@ -316,11 +316,11 @@ class TestVnfInstantiateWorkflow(asynctest.TestCase):
         self.assertEqual(
             call_mock_change_vnf_instantiation_state_tracker[1].args[0],
             ChangeVnfInstantiationStateInput(
-                vnfr_uuid=vnfr_uuid, state=VnfInstantiationState.NOT_INSTANTIATED
+                vnfr_uuid=vnfr_uuid, state=VnfInstantiationState.INSTANTIATED
             ),
         )
         self.assertEqual(
-            call_mock_change_vnf_state_tracker[1].args[0],
+            call_mock_change_vnf_state_tracker[0].args[0],
             ChangeVnfStateInput(vnfr_uuid=vnfr_uuid, state=VnfState.STOPPED),
         )
         self.assertEqual(str(error.exception), "Workflow execution failed")
@@ -437,11 +437,12 @@ class TestVnfInstantiateWorkflow(asynctest.TestCase):
         ):
             with self.assertRaises(WorkflowFailureError) as err:
                 await self.execute_workflow()
-        self.assertEqual(self.mock_change_vnf_instantiation_state_tracker.call_count, 2)
+        self.assertEqual(self.mock_change_vnf_instantiation_state_tracker.call_count, 3)
         self.assertEqual(self.mock_change_vnf_state_tracker.call_count, 6)
-        self.assertEqual(self.mock_send_notification_for_vnf_tracker.call_count, 0)
-        self.workflow_is_failed(err)
-        mock_instantiate_vdus.assert_not_called()
+        self.assertEqual(self.mock_send_notification_for_vnf_tracker.call_count, 1)
+        # using workflow_is_succeeded method since vnfInstantationState is being changed to INSTANTIATED and
+        # vnfState to STARTED (not STOPPED)
+        self.workflow_is_succeeded()
         self.assertEqual(
             str(err.exception.cause.cause.message),
             "change-vnf-state failed.",