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