Feature 10339 - Enhanced Alarm Mgmt. (SOL005 FM Interface) 10/10510/6
authorAtul Agarwal <atul.agarwal@altran.com>
Thu, 18 Mar 2021 07:54:40 +0000 (07:54 +0000)
committerpalsus <subhankar.pal@aricent.com>
Mon, 31 May 2021 16:49:06 +0000 (18:49 +0200)
Change-Id: Iea141198dbde8ec2485f868511d803e65f3276f6
Signed-off-by: Atul Agarwal <atul.agarwal@altran.com>
osm_mon/core/common_db.py
osm_mon/core/models.py
osm_mon/evaluator/evaluator.py
osm_mon/evaluator/service.py
osm_mon/server/server.py
osm_mon/server/service.py
osm_mon/tests/unit/core/test_common_db_client.py

index df8db60..cf52d85 100644 (file)
@@ -157,9 +157,12 @@ class CommonDbClient:
         return self.common_db.get_one("projects", {"_id": project_id})
 
     def create_alarm(self, alarm: Alarm):
+        action_data = {"uuid": alarm.uuid, "action": alarm.action}
+        self.common_db.create("alarms_action", action_data)
         return self.common_db.create("alarms", alarm.to_dict())
 
     def delete_alarm(self, alarm_uuid: str):
+        self.common_db.del_one("alarms_action", {"uuid": alarm_uuid})
         return self.common_db.del_one("alarms", {"uuid": alarm_uuid})
 
     def get_alarms(self) -> List[Alarm]:
@@ -169,6 +172,13 @@ class CommonDbClient:
             alarms.append(Alarm.from_dict(alarm_dict))
         return alarms
 
+    def update_alarm_status(self, alarm_state: str, uuid):
+        modified_count = self.common_db.set_one("alarms", {"uuid": uuid}, {"alarm_status": alarm_state})
+        return modified_count
+
+    def get_alarm_by_uuid(self, uuid: str):
+        return self.common_db.get_one("alarms", {"uuid": uuid})
+
     def get_user(self, username: str):
         return self.common_db.get_one("users", {"username": username})
 
index 8fe3b6b..48fe8c4 100644 (file)
@@ -32,7 +32,9 @@ class Alarm:
         operation: str = None,
         statistic: str = None,
         metric: str = None,
+        action: str = None,
         tags: dict = {},
+        alarm_status: str = "ok",
     ):
         self.uuid = str(uuid.uuid4())
         self.name = name
@@ -41,7 +43,9 @@ class Alarm:
         self.operation = operation
         self.statistic = statistic
         self.metric = metric
+        self.action = action
         self.tags = tags
+        self.alarm_status = alarm_status
 
     def to_dict(self) -> dict:
         alarm = {
@@ -53,6 +57,7 @@ class Alarm:
             "metric": self.metric,
             "tags": self.tags,
             "operation": self.operation,
+            "alarm_status": self.alarm_status,
         }
         return alarm
 
@@ -67,4 +72,5 @@ class Alarm:
         alarm.metric = data.get("metric")
         alarm.tags = data.get("tags")
         alarm.operation = data.get("operation")
+        alarm.alarm_status = data.get("alarm_status")
         return alarm
index 2d319c9..732f8ac 100644 (file)
@@ -72,6 +72,9 @@ class Evaluator:
         self.loop.run_until_complete(
             self.msg_bus.aiowrite("alarm_response", "notify_alarm", resp_message)
         )
+        evaluator_service = EvaluatorService(self.conf)
+        evaluator_service.update_alarm_status(status.value, alarm.uuid)
+        return
 
     def _build_alarm_response(self, alarm: Alarm, status: AlarmStatus):
         log.debug("_build_alarm_response")
index 2ba0625..9049d95 100644 (file)
@@ -39,6 +39,7 @@ class AlarmStatus(Enum):
     ALARM = "alarm"
     OK = "ok"
     INSUFFICIENT = "insufficient-data"
+    DISABLED = "disabled"
 
 
 class EvaluatorService:
@@ -55,20 +56,27 @@ class EvaluatorService:
     def _evaluate_metric(self, alarm: Alarm):
         log.debug("_evaluate_metric")
         metric_value = self._get_metric_value(alarm.metric, alarm.tags)
-        if metric_value is None:
-            log.warning("No metric result for alarm %s", alarm.uuid)
-            self.queue.put((alarm, AlarmStatus.INSUFFICIENT))
-        else:
-            if alarm.operation.upper() == "GT":
-                if metric_value > alarm.threshold:
-                    self.queue.put((alarm, AlarmStatus.ALARM))
-                else:
-                    self.queue.put((alarm, AlarmStatus.OK))
-            elif alarm.operation.upper() == "LT":
-                if metric_value < alarm.threshold:
-                    self.queue.put((alarm, AlarmStatus.ALARM))
-                else:
-                    self.queue.put((alarm, AlarmStatus.OK))
+        if alarm.alarm_status.upper() != AlarmStatus.DISABLED.value.upper():
+            if metric_value is None:
+                log.warning("No metric result for alarm %s", alarm.uuid)
+                self.queue.put((alarm, AlarmStatus.INSUFFICIENT))
+            else:
+                if alarm.operation.upper() == "GT":
+                    if metric_value > alarm.threshold:
+                        self.queue.put((alarm, AlarmStatus.ALARM))
+                    else:
+                        self.queue.put((alarm, AlarmStatus.OK))
+                elif alarm.operation.upper() == "LT":
+                    if metric_value < alarm.threshold:
+                        self.queue.put((alarm, AlarmStatus.ALARM))
+                    else:
+                        self.queue.put((alarm, AlarmStatus.OK))
+
+    def update_alarm_status(self, alarm_state, uuid):
+        alarm_data = self.common_db.get_alarm_by_uuid(uuid)
+        if alarm_data.get("alarm_status").upper() != AlarmStatus.DISABLED.value.upper():
+            self.common_db.update_alarm_status(alarm_state, uuid)
+        return
 
     def evaluate_alarms(self) -> List[Tuple[Alarm, AlarmStatus]]:
         log.debug("evaluate_alarms")
index 07fdd22..62721ff 100755 (executable)
@@ -74,6 +74,7 @@ class Server:
                             alarm_details["severity"].lower(),
                             alarm_details["statistic"].lower(),
                             alarm_details["metric_name"],
+                            alarm_details["action"],
                             alarm_details["tags"],
                         )
                         response = response_builder.generate_response(
index 94bdf4e..584a42f 100755 (executable)
@@ -41,11 +41,12 @@ class ServerService:
         severity: str,
         statistic: str,
         metric_name: str,
+        action: str,
         tags: dict,
     ) -> Alarm:
         log.debug("create_alarm")
         alarm = Alarm(
-            name, severity, threshold, operation, statistic, metric_name, tags
+            name, severity, threshold, operation, statistic, metric_name, action, tags
         )
         self.common_db.create_alarm(alarm)
         log.info("Alarm %s created", alarm.name)
index b639b7a..8ffc3d8 100644 (file)
@@ -185,7 +185,7 @@ class CommonDbClientTest(unittest.TestCase):
     @mock.patch.object(dbmongo.DbMongo, "db_connect", mock.Mock())
     @mock.patch.object(dbmongo.DbMongo, "create")
     def test_create_alarm(self, create):
-        alarm = Alarm("name", "severity", 50.0, "operation", "statistic", "metric", {})
+        alarm = Alarm("name", "severity", 50.0, "operation", "statistic", "metric", "scale_out", {}, "ok")
         alarm.uuid = "1"
         common_db_client = CommonDbClient(self.config)
         common_db_client.create_alarm(alarm)
@@ -200,6 +200,7 @@ class CommonDbClientTest(unittest.TestCase):
                 "name": "name",
                 "operation": "operation",
                 "uuid": "1",
+                "alarm_status": "ok",
             },
         )