Feature 10918: Alarm Notification Enhancement
[osm/POL.git] / osm_policy_module / core / database.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
5
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
8
9 # Licensed under the Apache License, Version 2.0 (the "License"); you may
10 # not use this file except in compliance with the License. You may obtain
11 # a copy of the License at
12
13 # http://www.apache.org/licenses/LICENSE-2.0
14
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 # License for the specific language governing permissions and limitations
19 # under the License.
20
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact: bdiaz@whitestack.com or glavado@whitestack.com
23 ##
24 import datetime
25 import logging
26 import os
27 from typing import Iterable, List
28
29 from peewee import (
30 CharField,
31 IntegerField,
32 ForeignKeyField,
33 Model,
34 TextField,
35 AutoField,
36 DateTimeField,
37 Proxy,
38 BooleanField,
39 )
40 from peewee_migrate import Router
41 from playhouse.db_url import connect
42
43 from osm_policy_module import migrations
44 from osm_policy_module.core.config import Config
45
46 log = logging.getLogger(__name__)
47
48 db = Proxy()
49
50
51 class BaseModel(Model):
52 id = AutoField(primary_key=True)
53
54 class Meta:
55 database = db
56
57
58 class ScalingGroup(BaseModel):
59 nsr_id = CharField()
60 vnf_member_index = CharField()
61 name = CharField()
62 content = TextField()
63
64
65 class ScalingPolicy(BaseModel):
66 name = CharField()
67 cooldown_time = IntegerField()
68 scale_in_operation = CharField(default="AND")
69 scale_out_operation = CharField(default="OR")
70 enabled = BooleanField(default=True)
71 last_scale = DateTimeField(default=datetime.datetime.now)
72 scaling_group = ForeignKeyField(
73 ScalingGroup, related_name="scaling_policies", on_delete="CASCADE"
74 )
75
76
77 class ScalingCriteria(BaseModel):
78 name = CharField()
79 scaling_policy = ForeignKeyField(
80 ScalingPolicy, related_name="scaling_criterias", on_delete="CASCADE"
81 )
82
83
84 class ScalingAlarm(BaseModel):
85 alarm_uuid = CharField(unique=True)
86 action = CharField()
87 vnf_member_index = CharField()
88 vdu_name = CharField()
89 scaling_criteria = ForeignKeyField(
90 ScalingCriteria, related_name="scaling_alarms", on_delete="CASCADE"
91 )
92 last_status = CharField(default="insufficient-data")
93
94
95 class VnfAlarm(BaseModel):
96 alarm_id = CharField()
97 alarm_uuid = CharField(unique=True)
98 nsr_id = CharField()
99 vnf_member_index = CharField()
100 vdu_name = CharField()
101 last_action = CharField(default='insufficient-data')
102 id_suffix = IntegerField()
103 ok_ack = BooleanField(default=False)
104 alarm_ack = BooleanField(default=False)
105
106
107 class AlarmAction(BaseModel):
108 type = CharField()
109 url = TextField()
110 alarm = ForeignKeyField(VnfAlarm, related_name="actions", on_delete="CASCADE")
111
112
113 class DatabaseManager:
114 def __init__(self, config: Config):
115 db.initialize(connect(config.get("sql", "database_uri")))
116
117 def create_tables(self) -> None:
118 db.connect()
119 with db.atomic():
120 router = Router(db, os.path.dirname(migrations.__file__))
121 router.run()
122 db.close()
123
124
125 class ScalingAlarmRepository:
126 @staticmethod
127 def list(*expressions) -> Iterable[ScalingAlarm]:
128 return ScalingAlarm.select().where(*expressions)
129
130 @staticmethod
131 def get(*expressions, join_classes: List = None) -> ScalingAlarm:
132 query = ScalingAlarm.select()
133 if join_classes:
134 for join_class in join_classes:
135 query = query.join(join_class)
136 return query.where(*expressions).get()
137
138 @staticmethod
139 def create(**query) -> ScalingAlarm:
140 return ScalingAlarm.create(**query)
141
142
143 class ScalingGroupRepository:
144 @staticmethod
145 def list(*expressions) -> Iterable[ScalingGroup]:
146 return ScalingGroup.select().where(*expressions)
147
148 @staticmethod
149 def get(*expressions) -> ScalingGroup:
150 return ScalingGroup.select().where(*expressions).get()
151
152 @staticmethod
153 def create(**query) -> ScalingGroup:
154 return ScalingGroup.create(**query)
155
156
157 class ScalingPolicyRepository:
158 @staticmethod
159 def list(*expressions, join_classes: List = None) -> Iterable[ScalingPolicy]:
160 query = ScalingPolicy.select()
161 if join_classes:
162 for join_class in join_classes:
163 query = query.join(join_class)
164 return query.where(*expressions)
165
166 @staticmethod
167 def get(*expressions, join_classes: List = None) -> ScalingPolicy:
168 query = ScalingPolicy.select()
169 if join_classes:
170 for join_class in join_classes:
171 query = query.join(join_class)
172 return query.where(*expressions).get()
173
174 @staticmethod
175 def create(**query) -> ScalingPolicy:
176 return ScalingPolicy.create(**query)
177
178
179 class ScalingCriteriaRepository:
180 @staticmethod
181 def list(*expressions, join_classes: List = None) -> Iterable[ScalingCriteria]:
182 query = ScalingCriteria.select()
183 if join_classes:
184 for join_class in join_classes:
185 query = query.join(join_class)
186 return query.where(*expressions)
187
188 @staticmethod
189 def get(*expressions, join_classes: List = None) -> ScalingCriteria:
190 query = ScalingCriteria.select()
191 if join_classes:
192 for join_class in join_classes:
193 query = query.join(join_class)
194 return query.where(*expressions).get()
195
196 @staticmethod
197 def create(**query) -> ScalingCriteria:
198 return ScalingCriteria.create(**query)
199
200
201 class VnfAlarmRepository:
202 @staticmethod
203 def list(*expressions) -> Iterable[VnfAlarm]:
204 return VnfAlarm.select().where(*expressions)
205
206 @staticmethod
207 def get(*expressions) -> VnfAlarm:
208 return VnfAlarm.select().where(*expressions).get()
209
210 @staticmethod
211 def create(**query) -> VnfAlarm:
212 return VnfAlarm.create(**query)
213
214
215 class AlarmActionRepository:
216 @staticmethod
217 def list(*expressions) -> Iterable[AlarmAction]:
218 return AlarmAction.select().where(*expressions)
219
220 @staticmethod
221 def get(*expressions) -> AlarmAction:
222 return AlarmAction.select().where(*expressions).get()
223
224 @staticmethod
225 def create(**query) -> AlarmAction:
226 return AlarmAction.create(**query)