1 # -*- coding: utf-8 -*-
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact: bdiaz@whitestack.com or glavado@whitestack.com
27 from typing
import Iterable
, List
40 from peewee_migrate
import Router
41 from playhouse
.db_url
import connect
43 from osm_policy_module
import migrations
44 from osm_policy_module
.core
.config
import Config
46 log
= logging
.getLogger(__name__
)
51 class BaseModel(Model
):
52 id = AutoField(primary_key
=True)
58 class ScalingGroup(BaseModel
):
60 vnf_member_index
= CharField()
65 class ScalingPolicy(BaseModel
):
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"
77 class ScalingCriteria(BaseModel
):
79 scaling_policy
= ForeignKeyField(
80 ScalingPolicy
, related_name
="scaling_criterias", on_delete
="CASCADE"
84 class ScalingAlarm(BaseModel
):
85 alarm_uuid
= CharField(unique
=True)
87 vnf_member_index
= CharField()
88 vdu_name
= CharField()
89 scaling_criteria
= ForeignKeyField(
90 ScalingCriteria
, related_name
="scaling_alarms", on_delete
="CASCADE"
92 last_status
= CharField(default
="insufficient-data")
95 class VnfAlarm(BaseModel
):
96 alarm_id
= CharField()
97 alarm_uuid
= CharField(unique
=True)
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)
107 class AlarmAction(BaseModel
):
110 alarm
= ForeignKeyField(VnfAlarm
, related_name
="actions", on_delete
="CASCADE")
113 class HealingAction(BaseModel
):
114 alarm_id
= CharField()
115 recovery_action
= CharField()
116 alarm_uuid
= CharField(unique
=True)
118 vnfinstance_id
= CharField()
119 vnf_member_index
= CharField()
120 vdur_name
= CharField()
122 cooldown_time
= IntegerField()
123 count_index
= IntegerField()
124 last_heal
= DateTimeField(default
=datetime
.datetime
.now
)
125 last_status
= CharField(default
="insufficient-data")
126 day1
= BooleanField(default
=False)
129 class DatabaseManager
:
130 def __init__(self
, config
: Config
):
131 db
.initialize(connect(config
.get("sql", "database_uri")))
133 def create_tables(self
) -> None:
136 router
= Router(db
, os
.path
.dirname(migrations
.__file
__))
141 class ScalingAlarmRepository
:
143 def list(*expressions
) -> Iterable
[ScalingAlarm
]:
144 return ScalingAlarm
.select().where(*expressions
).__iter
__()
147 def get(*expressions
, join_classes
: List
= None) -> ScalingAlarm
:
148 query
= ScalingAlarm
.select()
150 for join_class
in join_classes
:
151 query
= query
.join(join_class
)
152 return query
.where(*expressions
).get()
155 def create(**query
) -> ScalingAlarm
:
156 return ScalingAlarm
.create(**query
)
159 class ScalingGroupRepository
:
161 def list(*expressions
) -> Iterable
[ScalingGroup
]:
162 return ScalingGroup
.select().where(*expressions
).__iter
__()
165 def get(*expressions
) -> ScalingGroup
:
166 return ScalingGroup
.select().where(*expressions
).get()
169 def create(**query
) -> ScalingGroup
:
170 return ScalingGroup
.create(**query
)
173 class ScalingPolicyRepository
:
175 def list(*expressions
, join_classes
: List
= None) -> Iterable
[ScalingPolicy
]:
176 query
= ScalingPolicy
.select()
178 for join_class
in join_classes
:
179 query
= query
.join(join_class
)
180 return query
.where(*expressions
).__iter
__()
183 def get(*expressions
, join_classes
: List
= None) -> ScalingPolicy
:
184 query
= ScalingPolicy
.select()
186 for join_class
in join_classes
:
187 query
= query
.join(join_class
)
188 return query
.where(*expressions
).get()
191 def create(**query
) -> ScalingPolicy
:
192 return ScalingPolicy
.create(**query
)
195 class ScalingCriteriaRepository
:
197 def list(*expressions
, join_classes
: List
= None) -> Iterable
[ScalingCriteria
]:
198 query
= ScalingCriteria
.select()
200 for join_class
in join_classes
:
201 query
= query
.join(join_class
)
202 return query
.where(*expressions
).__iter
__()
205 def get(*expressions
, join_classes
: List
= None) -> ScalingCriteria
:
206 query
= ScalingCriteria
.select()
208 for join_class
in join_classes
:
209 query
= query
.join(join_class
)
210 return query
.where(*expressions
).get()
213 def create(**query
) -> ScalingCriteria
:
214 return ScalingCriteria
.create(**query
)
217 class VnfAlarmRepository
:
219 def list(*expressions
) -> Iterable
[VnfAlarm
]:
220 return VnfAlarm
.select().where(*expressions
).__iter
__()
223 def get(*expressions
) -> VnfAlarm
:
224 return VnfAlarm
.select().where(*expressions
).get()
227 def create(**query
) -> VnfAlarm
:
228 return VnfAlarm
.create(**query
)
231 class AlarmActionRepository
:
233 def list(*expressions
) -> Iterable
[AlarmAction
]:
234 return AlarmAction
.select().where(*expressions
).__iter
__()
237 def get(*expressions
) -> AlarmAction
:
238 return AlarmAction
.select().where(*expressions
).get()
241 def create(**query
) -> AlarmAction
:
242 return AlarmAction
.create(**query
)
245 class HealingActionRepository
:
247 def list(*expressions
) -> Iterable
[HealingAction
]:
249 "### Printing healing action db alarm {}".format(
250 HealingAction
.select().where(*expressions
)
253 return HealingAction
.select().where(*expressions
).__iter
__()
256 def get(*expressions
) -> HealingAction
:
257 return HealingAction
.select().where(*expressions
).get()
260 def create(**query
) -> HealingAction
:
261 return HealingAction
.create(**query
)