X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_mon%2Fcore%2Fdatabase.py;h=a41c0fddcd3c654e80531f931319d346fb6979ef;hb=de3d570360a0a31d3054a6ff72b09ae9f559b59c;hp=eab4bed2d9172f0aad699226ce9eb9f39171d70c;hpb=75512477988ae5e287433c6c859c61de1bc82318;p=osm%2FMON.git diff --git a/osm_mon/core/database.py b/osm_mon/core/database.py index eab4bed..a41c0fd 100644 --- a/osm_mon/core/database.py +++ b/osm_mon/core/database.py @@ -23,19 +23,23 @@ ## import logging +import uuid -from peewee import * -from playhouse.sqlite_ext import SqliteExtDatabase +from peewee import CharField, TextField, FloatField, Model, AutoField +from playhouse.db_url import connect from osm_mon.core.settings import Config log = logging.getLogger(__name__) cfg = Config.instance() +cfg.read_environ() -db = SqliteExtDatabase('mon.db') +db = connect(cfg.DATABASE) class BaseModel(Model): + id = AutoField(primary_key=True) + class Meta: database = db @@ -52,45 +56,69 @@ class VimCredentials(BaseModel): class Alarm(BaseModel): - alarm_id = CharField() - credentials = ForeignKeyField(VimCredentials, backref='alarms') + uuid = CharField(unique=True) + name = CharField() + severity = CharField() + threshold = FloatField() + operation = CharField() + statistic = CharField() + monitoring_param = CharField() + vdur_name = CharField() + vnf_member_index = CharField() + nsr_id = CharField() class DatabaseManager: - def create_tables(self): + def create_tables(self) -> None: try: db.connect() db.create_tables([VimCredentials, Alarm]) db.close() - except Exception as e: + except Exception: log.exception("Error creating tables: ") - def get_credentials(self, vim_uuid): + def get_credentials(self, vim_uuid) -> VimCredentials: return VimCredentials.get_or_none(VimCredentials.uuid == vim_uuid) - def save_credentials(self, vim_credentials): + def save_credentials(self, vim_credentials) -> VimCredentials: """Saves vim credentials. If a record with same uuid exists, overwrite it.""" exists = VimCredentials.get_or_none(VimCredentials.uuid == vim_credentials.uuid) if exists: vim_credentials.id = exists.id vim_credentials.save() - - def get_credentials_for_alarm_id(self, alarm_id, vim_type): - alarm = Alarm.select() \ - .where(Alarm.alarm_id == alarm_id) \ - .join(VimCredentials) \ - .where(VimCredentials.type == vim_type).get() - return alarm.credentials - - def save_alarm(self, alarm_id, vim_uuid): - """Saves alarm. If a record with same id and vim_uuid exists, overwrite it.""" + return vim_credentials + + def get_alarm(self, alarm_id) -> Alarm: + alarm = (Alarm.select() + .where(Alarm.alarm_id == alarm_id) + .get()) + 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) alarm = Alarm() - alarm.alarm_id = alarm_id - creds = VimCredentials.get(VimCredentials.uuid == vim_uuid) - alarm.credentials = creds - exists = Alarm.select(Alarm.alarm_id == alarm.alarm_id) \ - .join(VimCredentials) \ - .where(VimCredentials.uuid == vim_uuid) - if len(exists): - alarm.id = exists[0].id + 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() + return alarm + + def delete_alarm(self, alarm_uuid) -> None: + alarm = (Alarm.select() + .where(Alarm.uuid == alarm_uuid) + .get()) + alarm.delete_instance() + + def get_vim_type(self, vim_account_id) -> str: + """Get the vim type that is required by the message.""" + credentials = self.get_credentials(vim_account_id) + return str(credentials.type)