3a896529831d8db116665b59b8b3c700b19d8458
[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 CharField, IntegerField, ForeignKeyField, Model, TextField, AutoField, DateTimeField, Proxy, \
30 BooleanField
31 from peewee_migrate import Router
32 from playhouse.db_url import connect
33
34 from osm_policy_module import migrations
35 from osm_policy_module.core.config import Config
36
37 log = logging.getLogger(__name__)
38
39 db = Proxy()
40
41
42 class BaseModel(Model):
43 id = AutoField(primary_key=True)
44
45 class Meta:
46 database = db
47
48
49 class ScalingGroup(BaseModel):
50 nsr_id = CharField()
51 vnf_member_index = CharField()
52 name = CharField()
53 content = TextField()
54
55
56 class ScalingPolicy(BaseModel):
57 name = CharField()
58 cooldown_time = IntegerField()
59 scale_in_operation = CharField(default='AND')
60 scale_out_operation = CharField(default='OR')
61 enabled = BooleanField(default=True)
62 last_scale = DateTimeField(default=datetime.datetime.now)
63 scaling_group = ForeignKeyField(ScalingGroup, related_name='scaling_policies', on_delete='CASCADE')
64
65
66 class ScalingCriteria(BaseModel):
67 name = CharField()
68 scaling_policy = ForeignKeyField(ScalingPolicy, related_name='scaling_criterias', on_delete='CASCADE')
69
70
71 class ScalingAlarm(BaseModel):
72 alarm_uuid = CharField(unique=True)
73 action = CharField()
74 vnf_member_index = CharField()
75 vdu_name = CharField()
76 scaling_criteria = ForeignKeyField(ScalingCriteria, related_name='scaling_alarms', on_delete='CASCADE')
77 last_status = CharField(default='insufficient-data')
78
79
80 class VnfAlarm(BaseModel):
81 alarm_id = CharField()
82 alarm_uuid = CharField(unique=True)
83 nsr_id = CharField()
84 vnf_member_index = CharField()
85 vdu_name = CharField()
86
87
88 class AlarmAction(BaseModel):
89 type = CharField()
90 url = TextField()
91 alarm = ForeignKeyField(VnfAlarm, related_name='actions', on_delete='CASCADE')
92
93
94 class DatabaseManager:
95 def __init__(self, config: Config):
96 db.initialize(connect(config.get('sql', 'database_uri')))
97
98 def create_tables(self) -> None:
99 db.connect()
100 with db.atomic():
101 router = Router(db, os.path.dirname(migrations.__file__))
102 router.run()
103 db.close()
104
105
106 class ScalingAlarmRepository:
107
108 @staticmethod
109 def list(*expressions) -> Iterable[ScalingAlarm]:
110 return ScalingAlarm.select().where(*expressions)
111
112 @staticmethod
113 def get(*expressions, join_classes: List = None) -> ScalingAlarm:
114 query = ScalingAlarm.select()
115 if join_classes:
116 for join_class in join_classes:
117 query = query.join(join_class)
118 return query.where(*expressions).get()
119
120 @staticmethod
121 def create(**query) -> ScalingAlarm:
122 return ScalingAlarm.create(**query)
123
124
125 class ScalingGroupRepository:
126
127 @staticmethod
128 def list(*expressions) -> Iterable[ScalingGroup]:
129 return ScalingGroup.select().where(*expressions)
130
131 @staticmethod
132 def get(*expressions) -> ScalingGroup:
133 return ScalingGroup.select().where(*expressions).get()
134
135 @staticmethod
136 def create(**query) -> ScalingGroup:
137 return ScalingGroup.create(**query)
138
139
140 class ScalingPolicyRepository:
141
142 @staticmethod
143 def list(*expressions, join_classes: List = None) -> Iterable[ScalingPolicy]:
144 query = ScalingPolicy.select()
145 if join_classes:
146 for join_class in join_classes:
147 query = query.join(join_class)
148 return query.where(*expressions)
149
150 @staticmethod
151 def get(*expressions, join_classes: List = None) -> ScalingPolicy:
152 query = ScalingPolicy.select()
153 if join_classes:
154 for join_class in join_classes:
155 query = query.join(join_class)
156 return query.where(*expressions).get()
157
158 @staticmethod
159 def create(**query) -> ScalingPolicy:
160 return ScalingPolicy.create(**query)
161
162
163 class ScalingCriteriaRepository:
164
165 @staticmethod
166 def list(*expressions, join_classes: List = None) -> Iterable[ScalingCriteria]:
167 query = ScalingCriteria.select()
168 if join_classes:
169 for join_class in join_classes:
170 query = query.join(join_class)
171 return query.where(*expressions)
172
173 @staticmethod
174 def get(*expressions, join_classes: List = None) -> ScalingCriteria:
175 query = ScalingCriteria.select()
176 if join_classes:
177 for join_class in join_classes:
178 query = query.join(join_class)
179 return query.where(*expressions).get()
180
181 @staticmethod
182 def create(**query) -> ScalingCriteria:
183 return ScalingCriteria.create(**query)
184
185
186 class VnfAlarmRepository:
187
188 @staticmethod
189 def list(*expressions) -> Iterable[VnfAlarm]:
190 return VnfAlarm.select().where(*expressions)
191
192 @staticmethod
193 def get(*expressions) -> VnfAlarm:
194 return VnfAlarm.select().where(*expressions).get()
195
196 @staticmethod
197 def create(**query) -> VnfAlarm:
198 return VnfAlarm.create(**query)
199
200
201 class AlarmActionRepository:
202
203 @staticmethod
204 def list(*expressions) -> Iterable[AlarmAction]:
205 return AlarmAction.select().where(*expressions)
206
207 @staticmethod
208 def get(*expressions) -> AlarmAction:
209 return AlarmAction.select().where(*expressions).get()
210
211 @staticmethod
212 def create(**query) -> AlarmAction:
213 return AlarmAction.create(**query)