X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_mon%2Fcore%2Fdatabase.py;h=2f51b1e063fb4911ef803ef3892ec67fef350399;hb=a97bdb3eafa4f3d07d61d32635f7f36f5cc36c58;hp=a41c0fddcd3c654e80531f931319d346fb6979ef;hpb=de3d570360a0a31d3054a6ff72b09ae9f559b59c;p=osm%2FMON.git diff --git a/osm_mon/core/database.py b/osm_mon/core/database.py index a41c0fd..2f51b1e 100644 --- a/osm_mon/core/database.py +++ b/osm_mon/core/database.py @@ -23,18 +23,19 @@ ## import logging -import uuid +import os +from typing import Iterable -from peewee import CharField, TextField, FloatField, Model, AutoField +from peewee import CharField, TextField, FloatField, Model, AutoField, Proxy +from peewee_migrate import Router from playhouse.db_url import connect -from osm_mon.core.settings import Config +from osm_mon import migrations +from osm_mon.core.config import Config log = logging.getLogger(__name__) -cfg = Config.instance() -cfg.read_environ() -db = connect(cfg.DATABASE) +db = Proxy() class BaseModel(Model): @@ -52,7 +53,7 @@ class VimCredentials(BaseModel): user = CharField() password = CharField() tenant_name = CharField() - config = TextField(default='{}') + config = TextField() class Alarm(BaseModel): @@ -69,56 +70,43 @@ class Alarm(BaseModel): class DatabaseManager: + def __init__(self, config: Config): + db.initialize(connect(config.get('sql', 'database_uri'))) + def create_tables(self) -> None: - try: - db.connect() - db.create_tables([VimCredentials, Alarm]) - db.close() - except Exception: - log.exception("Error creating tables: ") - - def get_credentials(self, vim_uuid) -> VimCredentials: - return VimCredentials.get_or_none(VimCredentials.uuid == vim_uuid) - - 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.""" - # TODO: Add uuid optional param and check if exists to handle updates (see self.save_credentials) - 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 - - 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) + db.connect() + with db.atomic(): + router = Router(db, os.path.dirname(migrations.__file__)) + router.run() + db.close() + + +class VimCredentialsRepository: + @staticmethod + def upsert(**query) -> VimCredentials: + vim_credentials = VimCredentials.get_or_none(**query) + if vim_credentials: + query.update({'id': vim_credentials.id}) + vim_id = VimCredentials.insert(**query).on_conflict_replace().execute() + return VimCredentials.get(id=vim_id) + + @staticmethod + def get(*expressions) -> VimCredentials: + return VimCredentials.select().where(*expressions).get() + + +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 Alarm.select().where(*expressions)