X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_mon%2Fcore%2Fdatabase.py;h=26e3700930aadc7c3b3cbb6a5d3e796b94646597;hb=6eda48bf7489c00fee4e1f276404ab82db23fd1d;hp=2191106fc16edb33a7dc6fb0fe68381c67d4a68a;hpb=b3f86c9f43a0eb4d56487118a403c39f979ec042;p=osm%2FMON.git diff --git a/osm_mon/core/database.py b/osm_mon/core/database.py index 2191106..26e3700 100644 --- a/osm_mon/core/database.py +++ b/osm_mon/core/database.py @@ -24,44 +24,96 @@ import logging -from peewee import * -from playhouse.sqlite_ext import SqliteExtDatabase +from peewee import CharField, TextField, FloatField, Model +from playhouse.db_url import connect -from osm_mon.plugins.OpenStack.settings import Config +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): + class Meta: database = db class VimCredentials(BaseModel): - uuid = CharField() + uuid = CharField(unique=True) name = CharField() type = CharField() url = CharField() user = CharField() password = CharField() tenant_name = CharField() - config = TextField() + config = TextField(default='{}') + + +class Alarm(BaseModel): + 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]) + db.create_tables([VimCredentials, Alarm]) db.close() - except Exception as e: + except Exception: log.exception("Error creating tables: ") - def get_credentials(self, vim_uuid): - return VimCredentials.get(VimCredentials.uuid == 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() + 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.""" + alarm = Alarm() + 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_id) -> None: + alarm = (Alarm.select() + .where(Alarm.alarm_id == alarm_id) + .get()) + alarm.delete() + + 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)