Revert "Migrates alarms to MongoDB"
[osm/MON.git] / osm_mon / core / database.py
index 3b601b2..61bd180 100644 (file)
 
 import logging
 import os
-import uuid
-import json
+from typing import Iterable
 
-from peewee import CharField, TextField, FloatField, Model, AutoField, Proxy
+from peewee import CharField, FloatField, Model, AutoField, Proxy, ForeignKeyField
 from peewee_migrate import Router
 from playhouse.db_url import connect
 
@@ -46,17 +45,6 @@ class BaseModel(Model):
         database = db
 
 
-class VimCredentials(BaseModel):
-    uuid = CharField(unique=True)
-    name = CharField()
-    type = CharField()
-    url = CharField()
-    user = CharField()
-    password = CharField()
-    tenant_name = CharField()
-    config = TextField()
-
-
 class Alarm(BaseModel):
     uuid = CharField(unique=True)
     name = CharField()
@@ -64,10 +52,13 @@ class Alarm(BaseModel):
     threshold = FloatField()
     operation = CharField()
     statistic = CharField()
-    monitoring_param = CharField()
-    vdur_name = CharField()
-    vnf_member_index = CharField()
-    nsr_id = CharField()
+    metric = CharField()
+
+
+class AlarmTag(BaseModel):
+    name = CharField()
+    value = CharField()
+    alarm = ForeignKeyField(Alarm, related_name='tags', on_delete='CASCADE')
 
 
 class DatabaseManager:
@@ -81,70 +72,25 @@ class DatabaseManager:
             router.run()
         db.close()
 
-    def get_credentials(self, vim_uuid: str = None) -> VimCredentials:
-        db.connect()
-        with db.atomic():
-            vim_credentials = VimCredentials.get_or_none(VimCredentials.uuid == vim_uuid)
-        db.close()
-        return vim_credentials
 
-    def save_credentials(self, vim_credentials) -> VimCredentials:
-        """Saves vim credentials. If a record with same uuid exists, overwrite it."""
-        db.connect()
-        with db.atomic():
-            exists = VimCredentials.get_or_none(VimCredentials.uuid == vim_credentials.uuid)
-            if exists:
-                vim_credentials.id = exists.id
-            vim_credentials.save()
-        db.close()
-        return vim_credentials
+class AlarmTagRepository:
+    @staticmethod
+    def create(**query) -> Alarm:
+        return AlarmTag.create(**query)
 
-    def get_alarm(self, alarm_id) -> Alarm:
-        db.connect()
-        with db.atomic():
-            alarm = (Alarm.select()
-                     .where(Alarm.alarm_id == alarm_id)
-                     .get())
-        db.close()
-        return alarm
 
-    def save_alarm(self, name, threshold, operation, severity, statistic, metric_name, vdur_name,
-                   vnf_member_index, nsr_id) -> Alarm:
-        """Saves alarm."""
-        # TODO: Add uuid optional param and check if exists to handle updates (see self.save_credentials)
-        db.connect()
-        with db.atomic():
-            alarm = Alarm()
-            alarm.uuid = str(uuid.uuid4())
-            alarm.name = name
-            alarm.threshold = threshold
-            alarm.operation = operation
-            alarm.severity = severity
-            alarm.statistic = statistic
-            alarm.monitoring_param = metric_name
-            alarm.vdur_name = vdur_name
-            alarm.vnf_member_index = vnf_member_index
-            alarm.nsr_id = nsr_id
-            alarm.save()
-        db.close()
-        return alarm
+class AlarmRepository:
+    @staticmethod
+    def create(**query) -> Alarm:
+        return Alarm.create(**query)
 
-    def delete_alarm(self, alarm_uuid) -> None:
-        db.connect()
-        with db.atomic():
-            alarm = (Alarm.select()
-                     .where(Alarm.uuid == alarm_uuid)
-                     .get())
-            alarm.delete_instance()
-        db.close()
+    @staticmethod
+    def get(*expressions) -> Alarm:
+        return Alarm.select().where(*expressions).get()
 
-    def get_vim_type(self, vim_account_id) -> str:
-        """Get the vim type that is required by the message."""
-        vim_type = None
-        credentials = self.get_credentials(vim_account_id)
-        config = json.loads(credentials.config)
-        if 'vim_type' in config:
-            vim_type = config['vim_type']
-            return str(vim_type.lower())
+    @staticmethod
+    def list(*expressions) -> Iterable[Alarm]:
+        if expressions == ():
+            return Alarm.select()
         else:
-            return str(credentials.type)
+            return Alarm.select().where(*expressions)