Adds deletion of scaling alarms in POL and MON after ns termination 44/6944/4
authorBenjamin Diaz <bdiaz@whitestack.com>
Wed, 21 Nov 2018 21:53:18 +0000 (18:53 -0300)
committerBenjamin Diaz <bdiaz@whitestack.com>
Thu, 22 Nov 2018 20:28:13 +0000 (17:28 -0300)
Signed-off-by: Benjamin Diaz <bdiaz@whitestack.com>
Change-Id: Iefe8de25061cfffa0220671a8072c2bc155332eb

osm_policy_module/common/mon_client.py
osm_policy_module/core/agent.py
osm_policy_module/core/database.py

index 911e655..2f2dc12 100644 (file)
@@ -74,7 +74,7 @@ class MonClient:
                 if message.key == 'create_alarm_response':
                     content = json.loads(message.value)
                     log.info("Received create_alarm_response %s", content)
-                    if self._is_alarm_response_correlation_id_eq(cor_id, content):
+                    if content['alarm_create_response']['correlation_id'] == cor_id:
                         if not content['alarm_create_response']['status']:
                             raise ValueError("Error creating alarm in MON")
                         alarm_uuid = content['alarm_create_response']['alarm_uuid']
@@ -112,7 +112,7 @@ class MonClient:
                 if message.key == 'delete_alarm_response':
                     content = json.loads(message.value)
                     log.info("Received delete_alarm_response %s", content)
-                    if self._is_alarm_response_correlation_id_eq(cor_id, content):
+                    if content['alarm_delete_response']['correlation_id'] == cor_id:
                         if not content['alarm_delete_response']['status']:
                             raise ValueError("Error deleting alarm in MON")
                         alarm_uuid = content['alarm_delete_response']['alarm_uuid']
@@ -155,6 +155,3 @@ class MonClient:
             'alarm_delete_request': alarm_delete_request,
         }
         return msg
-
-    def _is_alarm_response_correlation_id_eq(self, cor_id, message_content):
-        return message_content['alarm_create_response']['correlation_id'] == cor_id
index 8a1a31c..ab8f857 100644 (file)
@@ -39,7 +39,7 @@ from osm_policy_module.core.database import ScalingGroup, ScalingAlarm, ScalingP
 
 log = logging.getLogger(__name__)
 
-ALLOWED_KAFKA_KEYS = ['instantiated', 'scaled', 'notify_alarm']
+ALLOWED_KAFKA_KEYS = ['instantiated', 'scaled', 'terminated', 'notify_alarm']
 
 
 class PolicyModuleAgent:
@@ -71,6 +71,7 @@ class PolicyModuleAgent:
         await consumer.start()
         try:
             async for msg in consumer:
+                log.info("Message arrived: %s", msg)
                 await self._process_msg(msg.topic, msg.key, msg.value)
         finally:
             await consumer.stop()
@@ -87,6 +88,9 @@ class PolicyModuleAgent:
                 if key == 'instantiated' or key == 'scaled':
                     await self._handle_instantiated_or_scaled(content)
 
+                if key == 'terminated':
+                    await self._handle_terminated(content)
+
                 if key == 'notify_alarm':
                     await self._handle_alarm_notification(content)
             else:
@@ -96,13 +100,13 @@ class PolicyModuleAgent:
 
     async def _handle_alarm_notification(self, content):
         log.debug("_handle_alarm_notification: %s", content)
-        alarm_id = content['notify_details']['alarm_uuid']
+        alarm_uuid = content['notify_details']['alarm_uuid']
         metric_name = content['notify_details']['metric_name']
         operation = content['notify_details']['operation']
         threshold = content['notify_details']['threshold_value']
         vdu_name = content['notify_details']['vdu_name']
         vnf_member_index = content['notify_details']['vnf_member_index']
-        ns_id = content['notify_details']['ns_id']
+        nsr_id = content['notify_details']['ns_id']
         log.info(
             "Received alarm notification for alarm %s, \
             metric %s, \
@@ -111,9 +115,9 @@ class PolicyModuleAgent:
             vdu_name %s, \
             vnf_member_index %s, \
             ns_id %s ",
-            alarm_id, metric_name, operation, threshold, vdu_name, vnf_member_index, ns_id)
+            alarm_uuid, metric_name, operation, threshold, vdu_name, vnf_member_index, nsr_id)
         try:
-            alarm = self.database_manager.get_alarm(alarm_id)
+            alarm = self.database_manager.get_alarm(alarm_uuid)
             delta = datetime.datetime.now() - alarm.scaling_criteria.scaling_policy.last_scale
             log.debug("last_scale: %s", alarm.scaling_criteria.scaling_policy.last_scale)
             log.debug("now: %s", datetime.datetime.now())
@@ -121,15 +125,15 @@ class PolicyModuleAgent:
             if delta.total_seconds() < alarm.scaling_criteria.scaling_policy.cooldown_time:
                 log.info("Time between last scale and now is less than cooldown time. Skipping.")
                 return
-            log.info("Sending scaling action message for ns: %s", alarm_id)
-            await self.lcm_client.scale(ns_id,
+            log.info("Sending scaling action message for ns: %s", nsr_id)
+            await self.lcm_client.scale(nsr_id,
                                         alarm.scaling_criteria.scaling_policy.scaling_group.name,
                                         alarm.vnf_member_index,
                                         alarm.action)
             alarm.scaling_criteria.scaling_policy.last_scale = datetime.datetime.now()
             alarm.scaling_criteria.scaling_policy.save()
         except ScalingAlarm.DoesNotExist:
-            log.info("There is no action configured for alarm %s.", alarm_id)
+            log.info("There is no action configured for alarm %s.", alarm_uuid)
 
     async def _handle_instantiated_or_scaled(self, content):
         log.debug("_handle_instantiated_or_scaled: %s", content)
@@ -145,9 +149,20 @@ class PolicyModuleAgent:
                 "Current state is %s. Skipping...",
                 nslcmop['operationState'])
 
+    async def _handle_terminated(self, content):
+        log.debug("_handle_deleted: %s", content)
+        nsr_id = content['nsr_id']
+        if content['operationState'] == 'COMPLETED' or content['operationState'] == 'PARTIALLY_COMPLETED':
+            log.info("Deleting scaling groups and alarms for network service with nsr_id: %s", nsr_id)
+            await self._delete_scaling_groups(nsr_id)
+        else:
+            log.info(
+                "Network service is not in COMPLETED or PARTIALLY_COMPLETED state. "
+                "Current state is %s. Skipping...",
+                content['operationState'])
+
     async def _configure_scaling_groups(self, nsr_id: str):
         log.debug("_configure_scaling_groups: %s", nsr_id)
-        # TODO: Add support for non-nfvi metrics
         alarms_created = []
         with database.db.atomic() as tx:
             try:
@@ -280,13 +295,14 @@ class PolicyModuleAgent:
                                         operation=scaling_criteria['scale-in-relational-operation'],
                                         statistic=vnf_monitoring_param['aggregation-type']
                                     )
-                                    ScalingAlarm.create(
-                                        alarm_id=alarm_uuid,
+                                    alarm = ScalingAlarm.create(
+                                        alarm_uuid=alarm_uuid,
                                         action='scale_in',
                                         vnf_member_index=int(vnfr['member-vnf-index-ref']),
                                         vdu_name=vdur['name'],
                                         scaling_criteria=scaling_criteria_record
                                     )
+                                    alarms_created.append(alarm)
                                     alarm_uuid = await self.mon_client.create_alarm(
                                         metric_name=vnf_monitoring_param['id'],
                                         ns_id=nsr_id,
@@ -296,13 +312,14 @@ class PolicyModuleAgent:
                                         operation=scaling_criteria['scale-out-relational-operation'],
                                         statistic=vnf_monitoring_param['aggregation-type']
                                     )
-                                    ScalingAlarm.create(
-                                        alarm_id=alarm_uuid,
+                                    alarm = ScalingAlarm.create(
+                                        alarm_uuid=alarm_uuid,
                                         action='scale_out',
                                         vnf_member_index=int(vnfr['member-vnf-index-ref']),
                                         vdu_name=vdur['name'],
                                         scaling_criteria=scaling_criteria_record
                                     )
+                                    alarms_created.append(alarm)
 
             except Exception as e:
                 log.exception("Error configuring scaling groups:")
@@ -310,5 +327,33 @@ class PolicyModuleAgent:
                 if len(alarms_created) > 0:
                     log.info("Cleaning alarm resources in MON")
                     for alarm in alarms_created:
-                        await self.mon_client.delete_alarm(*alarm)
+                        await self.mon_client.delete_alarm(alarm.scaling_criteria.scaling_policy.scaling_group.nsr_id,
+                                                           alarm.vnf_member_index,
+                                                           alarm.vdu_name,
+                                                           alarm.alarm_uuid)
+                raise e
+
+    async def _delete_scaling_groups(self, nsr_id: str):
+        with database.db.atomic() as tx:
+            try:
+                for scaling_group in ScalingGroup.select().where(ScalingGroup.nsr_id == nsr_id):
+                    for scaling_policy in scaling_group.scaling_policies:
+                        for scaling_criteria in scaling_policy.scaling_criterias:
+                            for alarm in scaling_criteria.scaling_alarms:
+                                try:
+                                    await self.mon_client.delete_alarm(
+                                        alarm.scaling_criteria.scaling_policy.scaling_group.nsr_id,
+                                        alarm.vnf_member_index,
+                                        alarm.vdu_name,
+                                        alarm.alarm_uuid)
+                                except ValueError:
+                                    log.exception("Error deleting alarm in MON %s", alarm.alarm_uuid)
+                                alarm.delete_instance()
+                            scaling_criteria.delete_instance()
+                        scaling_policy.delete_instance()
+                    scaling_group.delete_instance()
+
+            except Exception as e:
+                log.exception("Error deleting scaling groups and alarms:")
+                tx.rollback()
                 raise e
index 9bd8ac8..a39f982 100644 (file)
@@ -53,20 +53,20 @@ class ScalingPolicy(BaseModel):
     name = CharField()
     cooldown_time = IntegerField()
     last_scale = DateTimeField(default=datetime.datetime.now)
-    scaling_group = ForeignKeyField(ScalingGroup, related_name='scaling_policies')
+    scaling_group = ForeignKeyField(ScalingGroup, related_name='scaling_policies', on_delete='CASCADE')
 
 
 class ScalingCriteria(BaseModel):
     name = CharField()
-    scaling_policy = ForeignKeyField(ScalingPolicy, related_name='scaling_criterias')
+    scaling_policy = ForeignKeyField(ScalingPolicy, related_name='scaling_criterias', on_delete='CASCADE')
 
 
 class ScalingAlarm(BaseModel):
-    alarm_id = CharField()
+    alarm_uuid = CharField(unique=True)
     action = CharField()
     vnf_member_index = IntegerField()
     vdu_name = CharField()
-    scaling_criteria = ForeignKeyField(ScalingCriteria, related_name='scaling_alarms')
+    scaling_criteria = ForeignKeyField(ScalingCriteria, related_name='scaling_alarms', on_delete='CASCADE')
 
 
 class DatabaseManager:
@@ -78,5 +78,5 @@ class DatabaseManager:
         except Exception:
             log.exception("Error creating tables: ")
 
-    def get_alarm(self, alarm_id: str):
-        return ScalingAlarm.select().where(ScalingAlarm.alarm_id == alarm_id).get()
+    def get_alarm(self, alarm_uuid: str):
+        return ScalingAlarm.select().where(ScalingAlarm.alarm_uuid == alarm_uuid).get()