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
29 from peewee
import CharField
, IntegerField
, ForeignKeyField
, Model
, TextField
, AutoField
, DateTimeField
, Proxy
, \
31 from peewee_migrate
import Router
32 from playhouse
.db_url
import connect
34 from osm_policy_module
import migrations
35 from osm_policy_module
.core
.config
import Config
37 log
= logging
.getLogger(__name__
)
42 class BaseModel(Model
):
43 id = AutoField(primary_key
=True)
49 class ScalingGroup(BaseModel
):
51 vnf_member_index
= CharField()
56 class ScalingPolicy(BaseModel
):
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')
66 class ScalingCriteria(BaseModel
):
68 scaling_policy
= ForeignKeyField(ScalingPolicy
, related_name
='scaling_criterias', on_delete
='CASCADE')
71 class ScalingAlarm(BaseModel
):
72 alarm_uuid
= CharField(unique
=True)
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')
80 class VnfAlarm(BaseModel
):
81 alarm_id
= CharField()
82 alarm_uuid
= CharField(unique
=True)
84 vnf_member_index
= CharField()
85 vdu_name
= CharField()
88 class AlarmAction(BaseModel
):
91 alarm
= ForeignKeyField(VnfAlarm
, related_name
='actions', on_delete
='CASCADE')
94 class DatabaseManager
:
95 def __init__(self
, config
: Config
):
96 db
.initialize(connect(config
.get('sql', 'database_uri')))
98 def create_tables(self
) -> None:
101 router
= Router(db
, os
.path
.dirname(migrations
.__file
__))
106 class ScalingAlarmRepository
:
109 def list(*expressions
) -> Iterable
[ScalingAlarm
]:
110 return ScalingAlarm
.select().where(*expressions
)
113 def get(*expressions
, join_classes
: List
= None) -> ScalingAlarm
:
114 query
= ScalingAlarm
.select()
116 for join_class
in join_classes
:
117 query
= query
.join(join_class
)
118 return query
.where(*expressions
).get()
121 def create(**query
) -> ScalingAlarm
:
122 return ScalingAlarm
.create(**query
)
125 class ScalingGroupRepository
:
128 def list(*expressions
) -> Iterable
[ScalingGroup
]:
129 return ScalingGroup
.select().where(*expressions
)
132 def get(*expressions
) -> ScalingGroup
:
133 return ScalingGroup
.select().where(*expressions
).get()
136 def create(**query
) -> ScalingGroup
:
137 return ScalingGroup
.create(**query
)
140 class ScalingPolicyRepository
:
143 def list(*expressions
, join_classes
: List
= None) -> Iterable
[ScalingPolicy
]:
144 query
= ScalingPolicy
.select()
146 for join_class
in join_classes
:
147 query
= query
.join(join_class
)
148 return query
.where(*expressions
)
151 def get(*expressions
, join_classes
: List
= None) -> ScalingPolicy
:
152 query
= ScalingPolicy
.select()
154 for join_class
in join_classes
:
155 query
= query
.join(join_class
)
156 return query
.where(*expressions
).get()
159 def create(**query
) -> ScalingPolicy
:
160 return ScalingPolicy
.create(**query
)
163 class ScalingCriteriaRepository
:
166 def list(*expressions
, join_classes
: List
= None) -> Iterable
[ScalingCriteria
]:
167 query
= ScalingCriteria
.select()
169 for join_class
in join_classes
:
170 query
= query
.join(join_class
)
171 return query
.where(*expressions
)
174 def get(*expressions
, join_classes
: List
= None) -> ScalingCriteria
:
175 query
= ScalingCriteria
.select()
177 for join_class
in join_classes
:
178 query
= query
.join(join_class
)
179 return query
.where(*expressions
).get()
182 def create(**query
) -> ScalingCriteria
:
183 return ScalingCriteria
.create(**query
)
186 class VnfAlarmRepository
:
189 def list(*expressions
) -> Iterable
[VnfAlarm
]:
190 return VnfAlarm
.select().where(*expressions
)
193 def get(*expressions
) -> VnfAlarm
:
194 return VnfAlarm
.select().where(*expressions
).get()
197 def create(**query
) -> VnfAlarm
:
198 return VnfAlarm
.create(**query
)
201 class AlarmActionRepository
:
204 def list(*expressions
) -> Iterable
[AlarmAction
]:
205 return AlarmAction
.select().where(*expressions
)
208 def get(*expressions
) -> AlarmAction
:
209 return AlarmAction
.select().where(*expressions
).get()
212 def create(**query
) -> AlarmAction
:
213 return AlarmAction
.create(**query
)