X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_mon%2Fcore%2Fdatabase.py;h=61bd18060122f759a22c625064e7a1b98b1db84e;hb=628df021896fa8775f9743af62a4267b617cc35c;hp=d1c2e6b774773f3b6f32af7d097cd95444d54bab;hpb=2bdf4023aa0e0c4d61af6af969fb8c90522e2fe0;p=osm%2FMON.git diff --git a/osm_mon/core/database.py b/osm_mon/core/database.py index d1c2e6b..61bd180 100644 --- a/osm_mon/core/database.py +++ b/osm_mon/core/database.py @@ -22,12 +22,11 @@ # contact: bdiaz@whitestack.com or glavado@whitestack.com ## -import json import logging import os -import uuid +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,77 +72,25 @@ class DatabaseManager: router.run() db.close() - def get_credentials(self, vim_uuid: str = None) -> VimCredentials: - db.connect() - try: - with db.atomic(): - vim_credentials = VimCredentials.get_or_none(VimCredentials.uuid == vim_uuid) - return vim_credentials - finally: - db.close() - - def save_credentials(self, vim_credentials) -> VimCredentials: - """Saves vim credentials. If a record with same uuid exists, overwrite it.""" - db.connect() - try: - with db.atomic(): - exists = VimCredentials.get_or_none(VimCredentials.uuid == vim_credentials.uuid) - if exists: - vim_credentials.id = exists.id - vim_credentials.save() - return vim_credentials - finally: - db.close() - - def get_alarm(self, alarm_id) -> Alarm: - db.connect() - try: - with db.atomic(): - alarm = (Alarm.select() - .where(Alarm.alarm_id == alarm_id) - .get()) - return alarm - finally: - db.close() - - 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() - try: - 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() - return alarm - finally: - db.close() - - 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() - 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) - config = json.loads(credentials.config) - if 'vim_type' in config: - vim_type = config['vim_type'] - return str(vim_type.lower()) +class AlarmTagRepository: + @staticmethod + def create(**query) -> Alarm: + return AlarmTag.create(**query) + + +class AlarmRepository: + @staticmethod + def create(**query) -> Alarm: + return Alarm.create(**query) + + @staticmethod + def get(*expressions) -> Alarm: + return Alarm.select().where(*expressions).get() + + @staticmethod + def list(*expressions) -> Iterable[Alarm]: + if expressions == (): + return Alarm.select() else: - return str(credentials.type) + return Alarm.select().where(*expressions)