X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_policy_module%2Fcore%2Fdatabase.py;h=a39f982c85ba9afac09a82a4a4af31d070659181;hb=f3d077b407c594cdeabe267f73d61c355cb70066;hp=f9a37c20b8f8d9c4efc0c9c315db27fcc35d9ed2;hpb=bd499e01b67f2eb3cd9263f5d5d819fb118c5756;p=osm%2FPOL.git diff --git a/osm_policy_module/core/database.py b/osm_policy_module/core/database.py index f9a37c2..a39f982 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 CharField, IntegerField, ForeignKeyField, Model, TextField -from playhouse.sqlite_ext import SqliteExtDatabase +from peewee import CharField, IntegerField, ForeignKeyField, Model, TextField, AutoField, DateTimeField +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 = connect(cfg.OSMPOL_SQL_DATABASE_URI) 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.create_tables([ScalingGroup, ScalingPolicy, ScalingCriteria, ScalingAlarm]) db.close() - except Exception as e: + except Exception: log.exception("Error creating tables: ") + + def get_alarm(self, alarm_uuid: str): + return ScalingAlarm.select().where(ScalingAlarm.alarm_uuid == alarm_uuid).get()