X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_mon%2Fcore%2Fdatabase.py;h=e763a5b73f18dbab830d6422be05549ea088daab;hb=c3b07c71978706a4187602ce1c5d1acdc3341901;hp=2191106fc16edb33a7dc6fb0fe68381c67d4a68a;hpb=b3f86c9f43a0eb4d56487118a403c39f979ec042;p=osm%2FMON.git diff --git a/osm_mon/core/database.py b/osm_mon/core/database.py index 2191106..e763a5b 100644 --- a/osm_mon/core/database.py +++ b/osm_mon/core/database.py @@ -25,14 +25,15 @@ import logging from peewee import * -from playhouse.sqlite_ext import SqliteExtDatabase +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): @@ -41,27 +42,76 @@ class BaseModel(Model): 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): + alarm_id = CharField() + threshold = FloatField() + operation = CharField() + metric_name = CharField() + vdu_name = CharField() + vnf_member_index = CharField() + ns_id = CharField() + credentials = ForeignKeyField(VimCredentials, backref='alarms') class DatabaseManager: def create_tables(self): try: db.connect() - db.create_tables([VimCredentials]) + db.create_tables([VimCredentials, Alarm]) db.close() except Exception as e: log.exception("Error creating tables: ") def get_credentials(self, vim_uuid): - return VimCredentials.get(VimCredentials.uuid == vim_uuid) + return VimCredentials.get_or_none(VimCredentials.uuid == vim_uuid) def save_credentials(self, vim_credentials): + """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 get_alarm(self, alarm_id, vim_type): + alarm = Alarm.select() \ + .where(Alarm.alarm_id == alarm_id) \ + .join(VimCredentials) \ + .where(VimCredentials.type == vim_type).get() + return alarm + + def save_alarm(self, alarm_id, vim_uuid, threshold=None, operation=None, metric_name=None, vdu_name=None, + vnf_member_index=None, ns_id=None): + """Saves alarm. If a record with same id and vim_uuid exists, overwrite it.""" + alarm = Alarm() + alarm.alarm_id = alarm_id + creds = VimCredentials.get(VimCredentials.uuid == vim_uuid) + alarm.credentials = creds + alarm.threshold = threshold + alarm.operation = operation + alarm.metric_name = metric_name + alarm.vdu_name = vdu_name + alarm.vnf_member_index = vnf_member_index + alarm.ns_id = ns_id + 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.save()