X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_policy_module%2Fcore%2Fdatabase.py;h=330d8c5be8df290f8d41331515e73f651aedbc13;hb=a14cf16181c8b39f12c872c486e0b292c0068944;hp=a89baedfeac7121559d2a7b842a931af77e50012;hpb=7f11ecff803667fb5cd0e79389eece83ddc96c86;p=osm%2FPOL.git diff --git a/osm_policy_module/core/database.py b/osm_policy_module/core/database.py index a89baed..330d8c5 100644 --- a/osm_policy_module/core/database.py +++ b/osm_policy_module/core/database.py @@ -21,43 +21,62 @@ # For those usages not covered by the Apache License, Version 2.0 please # contact: bdiaz@whitestack.com or glavado@whitestack.com ## +import datetime import logging -from peewee import * -from playhouse.sqlite_ext import SqliteExtDatabase +from peewee import CharField, IntegerField, ForeignKeyField, Model, TextField, AutoField, DateTimeField, Proxy +from playhouse.db_url import connect from osm_policy_module.core.config import Config log = logging.getLogger(__name__) -cfg = Config.instance() -db = SqliteExtDatabase('policy_module.db') +db = Proxy() class BaseModel(Model): + id = AutoField(primary_key=True) + class Meta: database = db -class ScalingRecord(BaseModel): +class ScalingGroup(BaseModel): nsr_id = CharField() + vnf_member_index = IntegerField() name = CharField() content = TextField() +class ScalingPolicy(BaseModel): + name = CharField() + cooldown_time = IntegerField() + last_scale = DateTimeField(default=datetime.datetime.now) + scaling_group = ForeignKeyField(ScalingGroup, related_name='scaling_policies', on_delete='CASCADE') + + +class ScalingCriteria(BaseModel): + name = CharField() + scaling_policy = ForeignKeyField(ScalingPolicy, related_name='scaling_criterias', on_delete='CASCADE') + + class ScalingAlarm(BaseModel): - alarm_id = CharField() + alarm_uuid = CharField(unique=True) action = CharField() vnf_member_index = IntegerField() vdu_name = CharField() - scaling_record = ForeignKeyField(ScalingRecord, related_name='scaling_alarms') + scaling_criteria = ForeignKeyField(ScalingCriteria, related_name='scaling_alarms', on_delete='CASCADE') class DatabaseManager: + def init_db(self, config: Config): + db.initialize(connect(config.get('sql', 'database_uri'))) + self.create_tables() + def create_tables(self): - try: - db.connect() - db.create_tables([ScalingRecord, ScalingAlarm]) - db.close() - except Exception as e: - log.exception("Error creating tables: ") + with db.atomic(): + db.create_tables([ScalingGroup, ScalingPolicy, ScalingCriteria, ScalingAlarm]) + + def get_alarm(self, alarm_uuid: str): + with db.atomic(): + return ScalingAlarm.select().where(ScalingAlarm.alarm_uuid == alarm_uuid).get()