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 |
|
|
102 |
|
|
103 |
1 |
class AlarmAction(BaseModel): |
104 |
1 |
type = CharField() |
105 |
1 |
url = TextField() |
106 |
1 |
alarm = ForeignKeyField(VnfAlarm, related_name="actions", on_delete="CASCADE") |
107 |
|
|
108 |
|
|
109 |
1 |
class DatabaseManager: |
110 |
1 |
def __init__(self, config: Config): |
111 |
0 |
db.initialize(connect(config.get("sql", "database_uri"))) |
112 |
|
|
113 |
1 |
def create_tables(self) -> None: |
114 |
0 |
db.connect() |
115 |
0 |
with db.atomic(): |
116 |
0 |
router = Router(db, os.path.dirname(migrations.__file__)) |
117 |
0 |
router.run() |
118 |
0 |
db.close() |
119 |
|
|
120 |
|
|
121 |
1 |
class ScalingAlarmRepository: |
122 |
1 |
@staticmethod |
123 |
1 |
def list(*expressions) -> Iterable[ScalingAlarm]: |
124 |
0 |
return ScalingAlarm.select().where(*expressions) |
125 |
|
|
126 |
1 |
@staticmethod |
127 |
1 |
def get(*expressions, join_classes: List = None) -> ScalingAlarm: |
128 |
1 |
query = ScalingAlarm.select() |
129 |
1 |
if join_classes: |
130 |
1 |
for join_class in join_classes: |
131 |
1 |
query = query.join(join_class) |
132 |
1 |
return query.where(*expressions).get() |
133 |
|
|
134 |
1 |
@staticmethod |
135 |
1 |
def create(**query) -> ScalingAlarm: |
136 |
1 |
return ScalingAlarm.create(**query) |
137 |
|
|
138 |
|
|
139 |
1 |
class ScalingGroupRepository: |
140 |
1 |
@staticmethod |
141 |
1 |
def list(*expressions) -> Iterable[ScalingGroup]: |
142 |
0 |
return ScalingGroup.select().where(*expressions) |
143 |
|
|
144 |
1 |
@staticmethod |
145 |
1 |
def get(*expressions) -> ScalingGroup: |
146 |
1 |
return ScalingGroup.select().where(*expressions).get() |
147 |
|
|
148 |
1 |
@staticmethod |
149 |
1 |
def create(**query) -> ScalingGroup: |
150 |
1 |
return ScalingGroup.create(**query) |
151 |
|
|
152 |
|
|
153 |
1 |
class ScalingPolicyRepository: |
154 |
1 |
@staticmethod |
155 |
1 |
def list(*expressions, join_classes: List = None) -> Iterable[ScalingPolicy]: |
156 |
0 |
query = ScalingPolicy.select() |
157 |
0 |
if join_classes: |
158 |
0 |
for join_class in join_classes: |
159 |
0 |
query = query.join(join_class) |
160 |
0 |
return query.where(*expressions) |
161 |
|
|
162 |
1 |
@staticmethod |
163 |
1 |
def get(*expressions, join_classes: List = None) -> ScalingPolicy: |
164 |
1 |
query = ScalingPolicy.select() |
165 |
1 |
if join_classes: |
166 |
1 |
for join_class in join_classes: |
167 |
1 |
query = query.join(join_class) |
168 |
1 |
return query.where(*expressions).get() |
169 |
|
|
170 |
1 |
@staticmethod |
171 |
1 |
def create(**query) -> ScalingPolicy: |
172 |
1 |
return ScalingPolicy.create(**query) |
173 |
|
|
174 |
|
|
175 |
1 |
class ScalingCriteriaRepository: |
176 |
1 |
@staticmethod |
177 |
1 |
def list(*expressions, join_classes: List = None) -> Iterable[ScalingCriteria]: |
178 |
0 |
query = ScalingCriteria.select() |
179 |
0 |
if join_classes: |
180 |
0 |
for join_class in join_classes: |
181 |
0 |
query = query.join(join_class) |
182 |
0 |
return query.where(*expressions) |
183 |
|
|
184 |
1 |
@staticmethod |
185 |
1 |
def get(*expressions, join_classes: List = None) -> ScalingCriteria: |
186 |
1 |
query = ScalingCriteria.select() |
187 |
1 |
if join_classes: |
188 |
1 |
for join_class in join_classes: |
189 |
1 |
query = query.join(join_class) |
190 |
1 |
return query.where(*expressions).get() |
191 |
|
|
192 |
1 |
@staticmethod |
193 |
1 |
def create(**query) -> ScalingCriteria: |
194 |
1 |
return ScalingCriteria.create(**query) |
195 |
|
|
196 |
|
|
197 |
1 |
class VnfAlarmRepository: |
198 |
1 |
@staticmethod |
199 |
1 |
def list(*expressions) -> Iterable[VnfAlarm]: |
200 |
0 |
return VnfAlarm.select().where(*expressions) |
201 |
|
|
202 |
1 |
@staticmethod |
203 |
1 |
def get(*expressions) -> VnfAlarm: |
204 |
1 |
return VnfAlarm.select().where(*expressions).get() |
205 |
|
|
206 |
1 |
@staticmethod |
207 |
1 |
def create(**query) -> VnfAlarm: |
208 |
1 |
return VnfAlarm.create(**query) |
209 |
|
|
210 |
|
|
211 |
1 |
class AlarmActionRepository: |
212 |
1 |
@staticmethod |
213 |
1 |
def list(*expressions) -> Iterable[AlarmAction]: |
214 |
0 |
return AlarmAction.select().where(*expressions) |
215 |
|
|
216 |
1 |
@staticmethod |
217 |
1 |
def get(*expressions) -> AlarmAction: |
218 |
0 |
return AlarmAction.select().where(*expressions).get() |
219 |
|
|
220 |
1 |
@staticmethod |
221 |
1 |
def create(**query) -> AlarmAction: |
222 |
1 |
return AlarmAction.create(**query) |