X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_policy_module%2Fcore%2Fdatabase.py;h=3ca2e33e21aa87ae7b6e1dd501f32ac3eacede0b;hb=10be7c984475707be716708dfdd2d91a655158dc;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..3ca2e33 100644 --- a/osm_policy_module/core/database.py +++ b/osm_policy_module/core/database.py @@ -21,43 +21,65 @@ # 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 +import os -from peewee import * -from playhouse.sqlite_ext import SqliteExtDatabase +from peewee import CharField, IntegerField, ForeignKeyField, Model, TextField, AutoField, DateTimeField, Proxy +from peewee_migrate import Router +from playhouse.db_url import connect +from osm_policy_module import migrations 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 create_tables(self): - try: - db.connect() - db.create_tables([ScalingRecord, ScalingAlarm]) - db.close() - except Exception as e: - log.exception("Error creating tables: ") + def __init__(self, config: Config): + db.initialize(connect(config.get('sql', 'database_uri'))) + + def create_tables(self) -> None: + with db.atomic(): + router = Router(db, os.path.dirname(migrations.__file__)) + router.run() + + def get_alarm(self, alarm_uuid: str): + with db.atomic(): + return ScalingAlarm.select().where(ScalingAlarm.alarm_uuid == alarm_uuid).get()