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 DatabaseManager
:
114 def __init__(self
, config
: Config
):
115 db
.initialize(connect(config
.get("sql", "database_uri")))
117 def create_tables(self
) -> None:
120 router
= Router(db
, os
.path
.dirname(migrations
.__file
__))
125 class ScalingAlarmRepository
:
127 def list(*expressions
) -> Iterable
[ScalingAlarm
]:
128 return ScalingAlarm
.select().where(*expressions
)
131 def get(*expressions
, join_classes
: List
= None) -> ScalingAlarm
:
132 query
= ScalingAlarm
.select()
134 for join_class
in join_classes
:
135 query
= query
.join(join_class
)
136 return query
.where(*expressions
).get()
139 def create(**query
) -> ScalingAlarm
:
140 return ScalingAlarm
.create(**query
)
143 class ScalingGroupRepository
:
145 def list(*expressions
) -> Iterable
[ScalingGroup
]:
146 return ScalingGroup
.select().where(*expressions
)
149 def get(*expressions
) -> ScalingGroup
:
150 return ScalingGroup
.select().where(*expressions
).get()
153 def create(**query
) -> ScalingGroup
:
154 return ScalingGroup
.create(**query
)
157 class ScalingPolicyRepository
:
159 def list(*expressions
, join_classes
: List
= None) -> Iterable
[ScalingPolicy
]:
160 query
= ScalingPolicy
.select()
162 for join_class
in join_classes
:
163 query
= query
.join(join_class
)
164 return query
.where(*expressions
)
167 def get(*expressions
, join_classes
: List
= None) -> ScalingPolicy
:
168 query
= ScalingPolicy
.select()
170 for join_class
in join_classes
:
171 query
= query
.join(join_class
)
172 return query
.where(*expressions
).get()
175 def create(**query
) -> ScalingPolicy
:
176 return ScalingPolicy
.create(**query
)
179 class ScalingCriteriaRepository
:
181 def list(*expressions
, join_classes
: List
= None) -> Iterable
[ScalingCriteria
]:
182 query
= ScalingCriteria
.select()
184 for join_class
in join_classes
:
185 query
= query
.join(join_class
)
186 return query
.where(*expressions
)
189 def get(*expressions
, join_classes
: List
= None) -> ScalingCriteria
:
190 query
= ScalingCriteria
.select()
192 for join_class
in join_classes
:
193 query
= query
.join(join_class
)
194 return query
.where(*expressions
).get()
197 def create(**query
) -> ScalingCriteria
:
198 return ScalingCriteria
.create(**query
)
201 class VnfAlarmRepository
:
203 def list(*expressions
) -> Iterable
[VnfAlarm
]:
204 return VnfAlarm
.select().where(*expressions
)
207 def get(*expressions
) -> VnfAlarm
:
208 return VnfAlarm
.select().where(*expressions
).get()
211 def create(**query
) -> VnfAlarm
:
212 return VnfAlarm
.create(**query
)
215 class AlarmActionRepository
:
217 def list(*expressions
) -> Iterable
[AlarmAction
]:
218 return AlarmAction
.select().where(*expressions
)
221 def get(*expressions
) -> AlarmAction
:
222 return AlarmAction
.select().where(*expressions
).get()
225 def create(**query
) -> AlarmAction
:
226 return AlarmAction
.create(**query
)