Adds support for vdu_name and vnf_member_index in configure_scaling 44/6144/5
authordiazb <bdiaz@whitestack.com>
Mon, 14 May 2018 22:10:31 +0000 (19:10 -0300)
committerdiazb <bdiaz@whitestack.com>
Tue, 15 May 2018 15:30:36 +0000 (12:30 -0300)
message

Handles case when alarm is not registered in database
Adds typing check for methods

Signed-off-by: diazb <bdiaz@whitestack.com>
Change-Id: I6a36e4abf936f0634064e3e69d95e8b5e94c7a88

policy_module/osm_policy_module/common/alarm_config.py
policy_module/osm_policy_module/common/lcm_client.py
policy_module/osm_policy_module/common/mon_client.py
policy_module/osm_policy_module/core/agent.py
policy_module/osm_policy_module/models/configure_scaling.json
policy_module/osm_policy_module/tests/examples/configure_scaling_full_example.json

index f22a0e6..b6586ae 100644 (file)
 
 
 class AlarmConfig:
-    def __init__(self, metric_name, resource_uuid, vim_uuid, threshold, operation, statistic, action):
+    def __init__(self, metric_name, vdu_name, vnf_member_index, threshold, operation, statistic, action):
         self.metric_name = metric_name
-        self.resource_uuid = resource_uuid
-        self.vim_uuid = vim_uuid
+        self.vdu_name = vdu_name
+        self.vnf_member_index = vnf_member_index
         self.threshold = threshold
         self.operation = operation
         self.statistic = statistic
index 75e40c3..be44efe 100644 (file)
@@ -40,13 +40,13 @@ class LcmClient:
                                       key_serializer=str.encode,
                                       value_serializer=str.encode)
 
-    def scale(self, nsr_id, name, action):
+    def scale(self, nsr_id: str, name: str, action: str):
         msg = self._create_scale_action_payload(nsr_id, name, action)
         log.info("Sending scale action message: %s", json.dumps(msg))
         self.producer.send(topic='lcm_pm', key='trigger_scaling', value=json.dumps(msg))
         self.producer.flush()
 
-    def _create_scale_action_payload(self, nsr_id, name, action):
+    def _create_scale_action_payload(self, nsr_id: str, name: str, action: str):
         msg = {
             "ns_id": nsr_id,
             "scaling_group_descriptor": {
index 3e81757..d1336db 100644 (file)
@@ -42,9 +42,11 @@ class MonClient:
                                       key_serializer=str.encode,
                                       value_serializer=str.encode)
 
-    def create_alarm(self, metric_name, resource_uuid, vim_uuid, threshold, statistic, operation):
+    def create_alarm(self, metric_name: str, ns_id: str, vdu_name: str, vnf_member_index: str, threshold: int,
+                     statistic: str, operation: str):
         cor_id = random.randint(1, 1000000)
-        msg = self._create_alarm_payload(cor_id, metric_name, resource_uuid, vim_uuid, threshold, statistic, operation)
+        msg = self._create_alarm_payload(cor_id, metric_name, ns_id, vdu_name, vnf_member_index, threshold, statistic,
+                                         operation)
         log.info("Sending create_alarm_request %s", msg)
         future = self.producer.send(topic='alarm_request', key='create_alarm_request', value=json.dumps(msg))
         future.get(timeout=60)
@@ -64,12 +66,15 @@ class MonClient:
 
         raise ValueError('Timeout: No alarm creation response from MON. Is MON up?')
 
-    def _create_alarm_payload(self, cor_id, metric_name, resource_uuid, vim_uuid, threshold, statistic, operation):
+    def _create_alarm_payload(self, cor_id: int, metric_name: str, ns_id: str, vdu_name: str, vnf_member_index: str,
+                              threshold: int, statistic: str, operation: str):
         alarm_create_request = {
             'correlation_id': cor_id,
             'alarm_name': str(uuid.uuid4()),
             'metric_name': metric_name,
-            'resource_uuid': resource_uuid,
+            'ns_id': ns_id,
+            'vdu_name': vdu_name,
+            'vnf_member_index': vnf_member_index,
             'operation': operation,
             'severity': 'critical',
             'threshold_value': threshold,
@@ -77,7 +82,6 @@ class MonClient:
         }
         msg = {
             'alarm_create_request': alarm_create_request,
-            'vim_uuid': vim_uuid
         }
         return msg
 
index f8f82b5..60da337 100644 (file)
@@ -23,6 +23,9 @@
 ##
 import json
 import logging
+from typing import Dict, List
+
+import peewee
 import yaml
 
 from kafka import KafkaConsumer
@@ -77,8 +80,9 @@ class PolicyModuleAgent:
                         log.info("Creating alarm record in DB")
                         alarm_uuid = mon_client.create_alarm(
                             metric_name=config.metric_name,
-                            resource_uuid=config.resource_uuid,
-                            vim_uuid=config.vim_uuid,
+                            ns_id=scaling_record.nsr_id,
+                            vdu_name=config.vdu_name,
+                            vnf_member_index=config.vnf_member_index,
                             threshold=config.threshold,
                             operation=config.operation,
                             statistic=config.statistic
@@ -91,15 +95,18 @@ class PolicyModuleAgent:
                 if message.key == 'notify_alarm':
                     content = json.loads(message.value)
                     alarm_id = content['notify_details']['alarm_uuid']
-                    alarm = ScalingAlarm.select().where(ScalingAlarm.alarm_id == alarm_id).get()
-                    if alarm:
+                    log.info("Received alarm notification for alarm %s", alarm_id)
+                    try:
+                        alarm = ScalingAlarm.select().where(ScalingAlarm.alarm_id == alarm_id).get()
                         lcm_client = LcmClient()
                         log.info("Sending scaling action message for ns: %s", alarm_id)
                         lcm_client.scale(alarm.scaling_record.nsr_id, alarm.scaling_record.name, alarm.action)
+                    except ScalingAlarm.DoesNotExist:
+                        log.info("There is no action configured for alarm %.", alarm_id)
             except Exception:
                 log.exception("Error consuming message: ")
 
-    def _get_alarm_configs(self, message_content):
+    def _get_alarm_configs(self, message_content: Dict) -> List[AlarmConfig]:
         scaling_criterias = message_content['scaling_group_descriptor']['scaling_policy']['scaling_criteria']
         alarm_configs = []
         for criteria in scaling_criterias:
@@ -109,11 +116,11 @@ class PolicyModuleAgent:
             scale_out_operation = criteria['scale_out_relational_operation']
             scale_in_operation = criteria['scale_in_relational_operation']
             statistic = criteria['monitoring_param']['aggregation_type']
-            vim_uuid = ''
-            resource_uuid = ''
+            vdu_name = ''
+            vnf_member_index = ''
             if 'vdu_monitoring_param' in criteria['monitoring_param']:
-                vim_uuid = criteria['monitoring_param']['vdu_monitoring_param']['vim_uuid']
-                resource_uuid = criteria['monitoring_param']['vdu_monitoring_param']['resource_id']
+                vdu_name = criteria['monitoring_param']['vdu_monitoring_param']['vdu_name']
+                vnf_member_index = criteria['monitoring_param']['vdu_monitoring_param']['vnf_member_index']
                 metric_name = criteria['monitoring_param']['vdu_monitoring_param']['name']
             if 'vnf_metric' in criteria['monitoring_param']:
                 # TODO vnf_metric
@@ -122,15 +129,15 @@ class PolicyModuleAgent:
                 # TODO vdu_metric
                 continue
             scale_out_alarm_config = AlarmConfig(metric_name,
-                                                 resource_uuid,
-                                                 vim_uuid,
+                                                 vdu_name,
+                                                 vnf_member_index,
                                                  scale_out_threshold,
                                                  scale_out_operation,
                                                  statistic,
                                                  'scale_out')
             scale_in_alarm_config = AlarmConfig(metric_name,
-                                                resource_uuid,
-                                                vim_uuid,
+                                                vdu_name,
+                                                vnf_member_index,
                                                 scale_in_threshold,
                                                 scale_in_operation,
                                                 statistic,
index 72e7963..0a87491 100644 (file)
                       "vdu_monitoring_param": {
                         "type": "object",
                         "properties": {
-                          "vim_uuid": {
+                          "vnf_member_index": {
                             "type": "string"
                           },
-                          "resource_id": {
+                          "vdu_name": {
                             "type": "string"
                           },
                           "name": {
index be14fb4..eab1cc7 100644 (file)
@@ -18,8 +18,8 @@
             "name": "test_param",
             "aggregation_type": "average",
             "vdu_monitoring_param": {
-              "vim_uuid": "1",
-              "resource_id": "2d8d5355-acf7-42be-9f34-a10d02f9df39",
+              "vnf_member_index": "1",
+              "vdu_name": "2d8d5355-acf7-42be-9f34-a10d02f9df39",
               "name": "cpu_utilization"
             }
           }