X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_policy_module%2Fcore%2Fdatabase.py;h=e326ca277e9cba197ffc94f87754867c5718a8c3;hb=f7451f8879d188c9fef9c976d82aa39141601938;hp=3ca2e33e21aa87ae7b6e1dd501f32ac3eacede0b;hpb=10be7c984475707be716708dfdd2d91a655158dc;p=osm%2FPOL.git diff --git a/osm_policy_module/core/database.py b/osm_policy_module/core/database.py index 3ca2e33..e326ca2 100644 --- a/osm_policy_module/core/database.py +++ b/osm_policy_module/core/database.py @@ -24,8 +24,10 @@ import datetime import logging import os +from typing import Iterable, List -from peewee import CharField, IntegerField, ForeignKeyField, Model, TextField, AutoField, DateTimeField, Proxy +from peewee import CharField, IntegerField, ForeignKeyField, Model, TextField, AutoField, DateTimeField, Proxy, \ + BooleanField from peewee_migrate import Router from playhouse.db_url import connect @@ -54,6 +56,9 @@ class ScalingGroup(BaseModel): class ScalingPolicy(BaseModel): name = CharField() cooldown_time = IntegerField() + scale_in_operation = CharField(default='AND') + scale_out_operation = CharField(default='OR') + enabled = BooleanField(default=True) last_scale = DateTimeField(default=datetime.datetime.now) scaling_group = ForeignKeyField(ScalingGroup, related_name='scaling_policies', on_delete='CASCADE') @@ -69,6 +74,21 @@ class ScalingAlarm(BaseModel): vnf_member_index = IntegerField() vdu_name = CharField() scaling_criteria = ForeignKeyField(ScalingCriteria, related_name='scaling_alarms', on_delete='CASCADE') + last_status = CharField(default='insufficient-data') + + +class VnfAlarm(BaseModel): + alarm_id = CharField() + alarm_uuid = CharField(unique=True) + nsr_id = CharField() + vnf_member_index = IntegerField() + vdu_name = CharField() + + +class AlarmAction(BaseModel): + type = CharField() + url = TextField() + alarm = ForeignKeyField(VnfAlarm, related_name='actions', on_delete='CASCADE') class DatabaseManager: @@ -76,10 +96,118 @@ class DatabaseManager: db.initialize(connect(config.get('sql', 'database_uri'))) def create_tables(self) -> None: + db.connect() with db.atomic(): router = Router(db, os.path.dirname(migrations.__file__)) router.run() + db.close() - def get_alarm(self, alarm_uuid: str): - with db.atomic(): - return ScalingAlarm.select().where(ScalingAlarm.alarm_uuid == alarm_uuid).get() + +class ScalingAlarmRepository: + + @staticmethod + def list(*expressions) -> Iterable[ScalingAlarm]: + return ScalingAlarm.select().where(*expressions) + + @staticmethod + def get(*expressions, join_classes: List = None) -> ScalingAlarm: + query = ScalingAlarm.select() + if join_classes: + for join_class in join_classes: + query = query.join(join_class) + return query.where(*expressions).get() + + @staticmethod + def create(**query) -> ScalingAlarm: + return ScalingAlarm.create(**query) + + +class ScalingGroupRepository: + + @staticmethod + def list(*expressions) -> Iterable[ScalingGroup]: + return ScalingGroup.select().where(*expressions) + + @staticmethod + def get(*expressions) -> ScalingGroup: + return ScalingGroup.select().where(*expressions).get() + + @staticmethod + def create(**query) -> ScalingGroup: + return ScalingGroup.create(**query) + + +class ScalingPolicyRepository: + + @staticmethod + def list(*expressions, join_classes: List = None) -> Iterable[ScalingPolicy]: + query = ScalingPolicy.select() + if join_classes: + for join_class in join_classes: + query = query.join(join_class) + return query.where(*expressions) + + @staticmethod + def get(*expressions, join_classes: List = None) -> ScalingPolicy: + query = ScalingPolicy.select() + if join_classes: + for join_class in join_classes: + query = query.join(join_class) + return query.where(*expressions).get() + + @staticmethod + def create(**query) -> ScalingPolicy: + return ScalingPolicy.create(**query) + + +class ScalingCriteriaRepository: + + @staticmethod + def list(*expressions, join_classes: List = None) -> Iterable[ScalingCriteria]: + query = ScalingCriteria.select() + if join_classes: + for join_class in join_classes: + query = query.join(join_class) + return query.where(*expressions) + + @staticmethod + def get(*expressions, join_classes: List = None) -> ScalingCriteria: + query = ScalingCriteria.select() + if join_classes: + for join_class in join_classes: + query = query.join(join_class) + return query.where(*expressions).get() + + @staticmethod + def create(**query) -> ScalingCriteria: + return ScalingCriteria.create(**query) + + +class VnfAlarmRepository: + + @staticmethod + def list(*expressions) -> Iterable[VnfAlarm]: + return VnfAlarm.select().where(*expressions) + + @staticmethod + def get(*expressions) -> VnfAlarm: + return VnfAlarm.select().where(*expressions).get() + + @staticmethod + def create(**query) -> VnfAlarm: + return VnfAlarm.create(**query) + + +class AlarmActionRepository: + + @staticmethod + def list(*expressions) -> Iterable[AlarmAction]: + return AlarmAction.select().where(*expressions) + + @staticmethod + def get(*expressions) -> AlarmAction: + return AlarmAction.select().where(*expressions).get() + + @staticmethod + def create(**query) -> AlarmAction: + return AlarmAction.create(**query)