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 |
1 |
import datetime |
25 |
1 |
import logging |
26 |
1 |
import os |
27 |
1 |
from typing import Iterable, List |
28 |
|
|
29 |
1 |
from peewee import ( |
30 |
|
CharField, |
31 |
|
IntegerField, |
32 |
|
ForeignKeyField, |
33 |
|
Model, |
34 |
|
TextField, |
35 |
|
AutoField, |
36 |
|
DateTimeField, |
37 |
|
Proxy, |
38 |
|
BooleanField, |
39 |
|
) |
40 |
1 |
from peewee_migrate import Router |
41 |
1 |
from playhouse.db_url import connect |
42 |
|
|
43 |
1 |
from osm_policy_module import migrations |
44 |
1 |
from osm_policy_module.core.config import Config |
45 |
|
|
46 |
1 |
log = logging.getLogger(__name__) |
47 |
|
|
48 |
1 |
db = Proxy() |
49 |
|
|
50 |
|
|
51 |
1 |
class BaseModel(Model): |
52 |
1 |
id = AutoField(primary_key=True) |
53 |
|
|
54 |
1 |
class Meta: |
55 |
1 |
database = db |
56 |
|
|
57 |
|
|
58 |
1 |
class ScalingGroup(BaseModel): |
59 |
1 |
nsr_id = CharField() |
60 |
1 |
vnf_member_index = CharField() |
61 |
1 |
name = CharField() |
62 |
1 |
content = TextField() |
63 |
|
|
64 |
|
|
65 |
1 |
class ScalingPolicy(BaseModel): |
66 |
1 |
name = CharField() |
67 |
1 |
cooldown_time = IntegerField() |
68 |
1 |
scale_in_operation = CharField(default="AND") |
69 |
1 |
scale_out_operation = CharField(default="OR") |
70 |
1 |
enabled = BooleanField(default=True) |
71 |
1 |
last_scale = DateTimeField(default=datetime.datetime.now) |
72 |
1 |
scaling_group = ForeignKeyField( |
73 |
|
ScalingGroup, related_name="scaling_policies", on_delete="CASCADE" |
74 |
|
) |
75 |
|
|
76 |
|
|
77 |
1 |
class ScalingCriteria(BaseModel): |
78 |
1 |
name = CharField() |
79 |
1 |
scaling_policy = ForeignKeyField( |
80 |
|
ScalingPolicy, related_name="scaling_criterias", on_delete="CASCADE" |
81 |
|
) |
82 |
|
|
83 |
|
|
84 |
1 |
class ScalingAlarm(BaseModel): |
85 |
1 |
alarm_uuid = CharField(unique=True) |
86 |
1 |
action = CharField() |
87 |
1 |
vnf_member_index = CharField() |
88 |
1 |
vdu_name = CharField() |
89 |
1 |
scaling_criteria = ForeignKeyField( |
90 |
|
ScalingCriteria, related_name="scaling_alarms", on_delete="CASCADE" |
91 |
|
) |
92 |
1 |
last_status = CharField(default="insufficient-data") |
93 |
|
|
94 |
|
|
95 |
1 |
class VnfAlarm(BaseModel): |
96 |
1 |
alarm_id = CharField() |
97 |
1 |
alarm_uuid = CharField(unique=True) |
98 |
1 |
nsr_id = CharField() |
99 |
1 |
vnf_member_index = CharField() |
100 |
1 |
vdu_name = CharField() |
101 |
1 |
last_action = CharField(default="insufficient-data") |
102 |
1 |
id_suffix = IntegerField() |
103 |
1 |
ok_ack = BooleanField(default=False) |
104 |
1 |
alarm_ack = BooleanField(default=False) |
105 |
|
|
106 |
|
|
107 |
1 |
class AlarmAction(BaseModel): |
108 |
1 |
type = CharField() |
109 |
1 |
url = TextField() |
110 |
1 |
alarm = ForeignKeyField(VnfAlarm, related_name="actions", on_delete="CASCADE") |
111 |
|
|
112 |
|
|
113 |
1 |
class HealingAction(BaseModel): |
114 |
1 |
alarm_id = CharField() |
115 |
1 |
recovery_action = CharField() |
116 |
1 |
alarm_uuid = CharField(unique=True) |
117 |
1 |
nsr_id = CharField() |
118 |
1 |
vnfinstance_id = CharField() |
119 |
1 |
vnf_member_index = CharField() |
120 |
1 |
vdur_name = CharField() |
121 |
1 |
vdu_id = CharField() |
122 |
1 |
cooldown_time = IntegerField() |
123 |
1 |
count_index = IntegerField() |
124 |
1 |
last_heal = DateTimeField(default=datetime.datetime.now) |
125 |
1 |
last_status = CharField(default="insufficient-data") |
126 |
1 |
day1 = BooleanField(default=False) |
127 |
|
|
128 |
|
|
129 |
1 |
class DatabaseManager: |
130 |
1 |
def __init__(self, config: Config): |
131 |
0 |
db.initialize(connect(config.get("sql", "database_uri"))) |
132 |
|
|
133 |
1 |
def create_tables(self) -> None: |
134 |
0 |
db.connect() |
135 |
0 |
with db.atomic(): |
136 |
0 |
router = Router(db, os.path.dirname(migrations.__file__)) |
137 |
0 |
router.run() |
138 |
0 |
db.close() |
139 |
|
|
140 |
|
|
141 |
1 |
class ScalingAlarmRepository: |
142 |
1 |
@staticmethod |
143 |
1 |
def list(*expressions) -> Iterable[ScalingAlarm]: |
144 |
0 |
return ScalingAlarm.select().where(*expressions) |
145 |
|
|
146 |
1 |
@staticmethod |
147 |
1 |
def get(*expressions, join_classes: List = None) -> ScalingAlarm: |
148 |
1 |
query = ScalingAlarm.select() |
149 |
1 |
if join_classes: |
150 |
1 |
for join_class in join_classes: |
151 |
1 |
query = query.join(join_class) |
152 |
1 |
return query.where(*expressions).get() |
153 |
|
|
154 |
1 |
@staticmethod |
155 |
1 |
def create(**query) -> ScalingAlarm: |
156 |
1 |
return ScalingAlarm.create(**query) |
157 |
|
|
158 |
|
|
159 |
1 |
class ScalingGroupRepository: |
160 |
1 |
@staticmethod |
161 |
1 |
def list(*expressions) -> Iterable[ScalingGroup]: |
162 |
0 |
return ScalingGroup.select().where(*expressions) |
163 |
|
|
164 |
1 |
@staticmethod |
165 |
1 |
def get(*expressions) -> ScalingGroup: |
166 |
1 |
return ScalingGroup.select().where(*expressions).get() |
167 |
|
|
168 |
1 |
@staticmethod |
169 |
1 |
def create(**query) -> ScalingGroup: |
170 |
1 |
return ScalingGroup.create(**query) |
171 |
|
|
172 |
|
|
173 |
1 |
class ScalingPolicyRepository: |
174 |
1 |
@staticmethod |
175 |
1 |
def list(*expressions, join_classes: List = None) -> Iterable[ScalingPolicy]: |
176 |
0 |
query = ScalingPolicy.select() |
177 |
0 |
if join_classes: |
178 |
0 |
for join_class in join_classes: |
179 |
0 |
query = query.join(join_class) |
180 |
0 |
return query.where(*expressions) |
181 |
|
|
182 |
1 |
@staticmethod |
183 |
1 |
def get(*expressions, join_classes: List = None) -> ScalingPolicy: |
184 |
1 |
query = ScalingPolicy.select() |
185 |
1 |
if join_classes: |
186 |
1 |
for join_class in join_classes: |
187 |
1 |
query = query.join(join_class) |
188 |
1 |
return query.where(*expressions).get() |
189 |
|
|
190 |
1 |
@staticmethod |
191 |
1 |
def create(**query) -> ScalingPolicy: |
192 |
1 |
return ScalingPolicy.create(**query) |
193 |
|
|
194 |
|
|
195 |
1 |
class ScalingCriteriaRepository: |
196 |
1 |
@staticmethod |
197 |
1 |
def list(*expressions, join_classes: List = None) -> Iterable[ScalingCriteria]: |
198 |
0 |
query = ScalingCriteria.select() |
199 |
0 |
if join_classes: |
200 |
0 |
for join_class in join_classes: |
201 |
0 |
query = query.join(join_class) |
202 |
0 |
return query.where(*expressions) |
203 |
|
|
204 |
1 |
@staticmethod |
205 |
1 |
def get(*expressions, join_classes: List = None) -> ScalingCriteria: |
206 |
1 |
query = ScalingCriteria.select() |
207 |
1 |
if join_classes: |
208 |
1 |
for join_class in join_classes: |
209 |
1 |
query = query.join(join_class) |
210 |
1 |
return query.where(*expressions).get() |
211 |
|
|
212 |
1 |
@staticmethod |
213 |
1 |
def create(**query) -> ScalingCriteria: |
214 |
1 |
return ScalingCriteria.create(**query) |
215 |
|
|
216 |
|
|
217 |
1 |
class VnfAlarmRepository: |
218 |
1 |
@staticmethod |
219 |
1 |
def list(*expressions) -> Iterable[VnfAlarm]: |
220 |
0 |
return VnfAlarm.select().where(*expressions) |
221 |
|
|
222 |
1 |
@staticmethod |
223 |
1 |
def get(*expressions) -> VnfAlarm: |
224 |
1 |
return VnfAlarm.select().where(*expressions).get() |
225 |
|
|
226 |
1 |
@staticmethod |
227 |
1 |
def create(**query) -> VnfAlarm: |
228 |
1 |
return VnfAlarm.create(**query) |
229 |
|
|
230 |
|
|
231 |
1 |
class AlarmActionRepository: |
232 |
1 |
@staticmethod |
233 |
1 |
def list(*expressions) -> Iterable[AlarmAction]: |
234 |
0 |
return AlarmAction.select().where(*expressions) |
235 |
|
|
236 |
1 |
@staticmethod |
237 |
1 |
def get(*expressions) -> AlarmAction: |
238 |
0 |
return AlarmAction.select().where(*expressions).get() |
239 |
|
|
240 |
1 |
@staticmethod |
241 |
1 |
def create(**query) -> AlarmAction: |
242 |
1 |
return AlarmAction.create(**query) |
243 |
|
|
244 |
|
|
245 |
1 |
class HealingActionRepository: |
246 |
1 |
@staticmethod |
247 |
1 |
def list(*expressions) -> Iterable[HealingAction]: |
248 |
0 |
log.info( |
249 |
|
"### Printing healing action db alarm {}".format( |
250 |
|
HealingAction.select().where(*expressions) |
251 |
|
) |
252 |
|
) |
253 |
0 |
return HealingAction.select().where(*expressions) |
254 |
|
|
255 |
1 |
@staticmethod |
256 |
1 |
def get(*expressions) -> HealingAction: |
257 |
1 |
return HealingAction.select().where(*expressions).get() |
258 |
|
|
259 |
1 |
@staticmethod |
260 |
1 |
def create(**query) -> HealingAction: |
261 |
1 |
return HealingAction.create(**query) |