Bug 2315: Fix for Unable to update healing policy using NS Update 18/14018/4 v14.0
authorelumalai <deepika.e@tataelxsi.co.in>
Tue, 14 Nov 2023 15:33:34 +0000 (21:03 +0530)
committerelumalai <deepika.e@tataelxsi.co.in>
Fri, 31 May 2024 06:18:58 +0000 (08:18 +0200)
Change-Id: I63d71756392eba12eec9feb688cc1ccdda57f7bf
Signed-off-by: elumalai <deepika.e@tataelxsi.co.in>
osm_policy_module/core/agent.py
osm_policy_module/healing/service.py
osm_policy_module/tests/unit/core/test_policy_agent.py

index 696f69c..4a20527 100644 (file)
@@ -184,10 +184,14 @@ class PolicyModuleAgent:
                 nsr_id, vnf_member_index
             )
             await self.alarming_service.delete_vnf_alarms(nsr_id, vnf_member_index)
+            await self.healing_service.delete_healing_alarms(nsr_id, vnf_member_index)
             await self.autoscaling_service.configure_scaling_groups(
                 nsr_id, vnf_member_index
             )
             await self.alarming_service.configure_vnf_alarms(nsr_id, vnf_member_index)
+            await self.healing_service.configure_healing_alarms(
+                nsr_id, vnf_member_index
+            )
         else:
             log.info(
                 "Network service is not in COMPLETED or PARTIALLY_COMPLETED state. "
index 4376852..b36f867 100644 (file)
@@ -24,6 +24,8 @@
 ##
 import logging
 import datetime
+import functools
+import operator
 
 from osm_policy_module.common.common_db_client import CommonDbClient
 from osm_policy_module.common.lcm_client import LcmClient
@@ -51,17 +53,23 @@ class HealingService:
         self.lcm_client = LcmClient(config)
         log.info("Constructor created for HealingService")
 
-    async def configure_healing_alarms(self, nsr_id: str):
+    async def configure_healing_alarms(self, nsr_id: str, vnf_member_index=None):
         """
         Configuring the Healing alarms
         :param nsr_id: Network service record id
+        :param vnf_member_index: Member VNF Index of VNF
         """
         log.info("Configuring Healing alarm for NS %s", nsr_id)
         alarms_created = []
         database.db.connect()
         try:
             with database.db.atomic():
-                vnfrs = self.db_client.get_vnfrs(nsr_id)
+                if vnf_member_index is None:
+                    vnfrs = self.db_client.get_vnfrs(nsr_id)
+                else:
+                    vnfrs = []
+                    vnfr = self.db_client.get_vnfr(nsr_id, vnf_member_index)
+                    vnfrs.append(vnfr)
                 for vnfr in vnfrs:
                     vnfd = self.db_client.get_vnfd(vnfr["vnfd-id"])
                     df = vnfd.get("df", [{}])[0]
@@ -178,18 +186,25 @@ class HealingService:
         finally:
             database.db.close()
 
-    async def delete_healing_alarms(self, nsr_id):
+    async def delete_healing_alarms(self, nsr_id, vnf_member_index=None):
         """
         Deleting the healing alarms
         :param nsr_id: Network service record id
+        :param vnf_member_index: Member VNF Index of VNF
         """
         log.info("Deleting healing vnf alarms for network service %s", nsr_id)
         database.db.connect()
         try:
             with database.db.atomic():
-                for alarm in HealingActionRepository.list(
-                    HealingAction.nsr_id == nsr_id
-                ):
+                if vnf_member_index is None:
+                    heal_conditions = HealingAction.nsr_id == nsr_id
+                else:
+                    query_list = [
+                        HealingAction.nsr_id == nsr_id,
+                        HealingAction.vnf_member_index == vnf_member_index,
+                    ]
+                    heal_conditions = functools.reduce(operator.and_, query_list)
+                for alarm in HealingActionRepository.list(heal_conditions):
                     try:
                         await self.mon_client.delete_alarm(
                             alarm.nsr_id,
index 32d88a2..7aab928 100644 (file)
@@ -110,16 +110,24 @@ class PolicyAgentTest(unittest.TestCase):
     @mock.patch("osm_policy_module.alarming.service.LcmClient")
     @mock.patch("osm_policy_module.autoscaling.service.MonClient")
     @mock.patch("osm_policy_module.autoscaling.service.LcmClient")
+    @mock.patch("osm_policy_module.healing.service.MonClient")
+    @mock.patch("osm_policy_module.healing.service.LcmClient")
+    @mock.patch.object(HealingService, "configure_healing_alarms")
     @mock.patch.object(AutoscalingService, "configure_scaling_groups")
     @mock.patch.object(AlarmingService, "configure_vnf_alarms")
+    @mock.patch.object(HealingService, "delete_healing_alarms")
     @mock.patch.object(AutoscalingService, "delete_scaling_groups")
     @mock.patch.object(AlarmingService, "delete_vnf_alarms")
     def test_handle_policy_update(
         self,
         delete_vnf_alarms,
         delete_scaling_groups,
+        delete_healing_alarms,
         configure_vnf_alarms,
         configure_scaling_groups,
+        configure_healing_alarms,
+        healing_lcm_client,
+        healing_mon_client,
         autoscaling_lcm_client,
         autoscaling_mon_client,
         alarming_lcm_client,
@@ -131,18 +139,26 @@ class PolicyAgentTest(unittest.TestCase):
         async def mock_delete_vnf_alarms(nsr_id, vnf_member_index):
             pass
 
+        async def mock_delete_healing_alarms(nsr_id, vnf_member_index):
+            pass
+
         async def mock_configure_scaling_groups(nsr_id, vnf_member_index):
             pass
 
         async def mock_configure_vnf_alarms(nsr_id, vnf_member_index):
             pass
 
+        async def mock_configure_healing_alarms(nsr_id, vnf_member_index):
+            pass
+
         config = Config()
         agent = PolicyModuleAgent(config)
         assert autoscaling_lcm_client.called
         assert autoscaling_mon_client.called
         assert alarming_lcm_client.called
         assert alarming_mon_client.called
+        assert healing_lcm_client.called
+        assert healing_mon_client.called
         content = {
             "nsr_id": "test_nsr_id",
             "vnf_member_index": "1",
@@ -155,8 +171,10 @@ class PolicyAgentTest(unittest.TestCase):
         }
         configure_scaling_groups.side_effect = mock_configure_scaling_groups
         configure_vnf_alarms.side_effect = mock_configure_vnf_alarms
+        configure_healing_alarms.side_effect = mock_configure_healing_alarms
         delete_scaling_groups.side_effect = mock_delete_scaling_groups
         delete_vnf_alarms.side_effect = mock_delete_vnf_alarms
+        delete_healing_alarms.side_effect = mock_delete_healing_alarms
 
         self.loop.run_until_complete(agent._handle_policy_update(content))
         configure_scaling_groups.assert_called_with("test_nsr_id", "1")