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()
103 class AlarmAction(BaseModel
):
106 alarm
= ForeignKeyField(VnfAlarm
, related_name
="actions", on_delete
="CASCADE")
109 class DatabaseManager
:
110 def __init__(self
, config
: Config
):
111 db
.initialize(connect(config
.get("sql", "database_uri")))
113 def create_tables(self
) -> None:
116 router
= Router(db
, os
.path
.dirname(migrations
.__file
__))
121 class ScalingAlarmRepository
:
123 def list(*expressions
) -> Iterable
[ScalingAlarm
]:
124 return ScalingAlarm
.select().where(*expressions
)
127 def get(*expressions
, join_classes
: List
= None) -> ScalingAlarm
:
128 query
= ScalingAlarm
.select()
130 for join_class
in join_classes
:
131 query
= query
.join(join_class
)
132 return query
.where(*expressions
).get()
135 def create(**query
) -> ScalingAlarm
:
136 return ScalingAlarm
.create(**query
)
139 class ScalingGroupRepository
:
141 def list(*expressions
) -> Iterable
[ScalingGroup
]:
142 return ScalingGroup
.select().where(*expressions
)
145 def get(*expressions
) -> ScalingGroup
:
146 return ScalingGroup
.select().where(*expressions
).get()
149 def create(**query
) -> ScalingGroup
:
150 return ScalingGroup
.create(**query
)
153 class ScalingPolicyRepository
:
155 def list(*expressions
, join_classes
: List
= None) -> Iterable
[ScalingPolicy
]:
156 query
= ScalingPolicy
.select()
158 for join_class
in join_classes
:
159 query
= query
.join(join_class
)
160 return query
.where(*expressions
)
163 def get(*expressions
, join_classes
: List
= None) -> ScalingPolicy
:
164 query
= ScalingPolicy
.select()
166 for join_class
in join_classes
:
167 query
= query
.join(join_class
)
168 return query
.where(*expressions
).get()
171 def create(**query
) -> ScalingPolicy
:
172 return ScalingPolicy
.create(**query
)
175 class ScalingCriteriaRepository
:
177 def list(*expressions
, join_classes
: List
= None) -> Iterable
[ScalingCriteria
]:
178 query
= ScalingCriteria
.select()
180 for join_class
in join_classes
:
181 query
= query
.join(join_class
)
182 return query
.where(*expressions
)
185 def get(*expressions
, join_classes
: List
= None) -> ScalingCriteria
:
186 query
= ScalingCriteria
.select()
188 for join_class
in join_classes
:
189 query
= query
.join(join_class
)
190 return query
.where(*expressions
).get()
193 def create(**query
) -> ScalingCriteria
:
194 return ScalingCriteria
.create(**query
)
197 class VnfAlarmRepository
:
199 def list(*expressions
) -> Iterable
[VnfAlarm
]:
200 return VnfAlarm
.select().where(*expressions
)
203 def get(*expressions
) -> VnfAlarm
:
204 return VnfAlarm
.select().where(*expressions
).get()
207 def create(**query
) -> VnfAlarm
:
208 return VnfAlarm
.create(**query
)
211 class AlarmActionRepository
:
213 def list(*expressions
) -> Iterable
[AlarmAction
]:
214 return AlarmAction
.select().where(*expressions
)
217 def get(*expressions
) -> AlarmAction
:
218 return AlarmAction
.select().where(*expressions
).get()
221 def create(**query
) -> AlarmAction
:
222 return AlarmAction
.create(**query
)