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
30 from peewee
import CharField
, TextField
, FloatField
, Model
, AutoField
, Proxy
31 from peewee_migrate
import Router
32 from playhouse
.db_url
import connect
34 from osm_mon
import migrations
35 from osm_mon
.core
.config
import Config
37 log
= logging
.getLogger(__name__
)
42 class BaseModel(Model
):
43 id = AutoField(primary_key
=True)
49 class VimCredentials(BaseModel
):
50 uuid
= CharField(unique
=True)
55 password
= CharField()
56 tenant_name
= CharField()
60 class Alarm(BaseModel
):
61 uuid
= CharField(unique
=True)
63 severity
= CharField()
64 threshold
= FloatField()
65 operation
= CharField()
66 statistic
= CharField()
67 monitoring_param
= CharField()
68 vdur_name
= CharField()
69 vnf_member_index
= CharField()
73 class DatabaseManager
:
74 def __init__(self
, config
: Config
):
75 db
.initialize(connect(config
.get('sql', 'database_uri')))
77 def create_tables(self
) -> None:
80 router
= Router(db
, os
.path
.dirname(migrations
.__file
__))
84 def get_credentials(self
, vim_uuid
: str = None) -> VimCredentials
:
88 vim_credentials
= VimCredentials
.get_or_none(VimCredentials
.uuid
== vim_uuid
)
89 return vim_credentials
93 def save_credentials(self
, vim_credentials
) -> VimCredentials
:
94 """Saves vim credentials. If a record with same uuid exists, overwrite it."""
98 exists
= VimCredentials
.get_or_none(VimCredentials
.uuid
== vim_credentials
.uuid
)
100 vim_credentials
.id = exists
.id
101 vim_credentials
.save()
102 return vim_credentials
106 def get_alarm(self
, alarm_id
) -> Alarm
:
110 alarm
= (Alarm
.select()
111 .where(Alarm
.alarm_id
== alarm_id
)
117 def save_alarm(self
, name
, threshold
, operation
, severity
, statistic
, metric_name
, vdur_name
,
118 vnf_member_index
, nsr_id
) -> Alarm
:
120 # TODO: Add uuid optional param and check if exists to handle updates (see self.save_credentials)
125 alarm
.uuid
= str(uuid
.uuid4())
127 alarm
.threshold
= threshold
128 alarm
.operation
= operation
129 alarm
.severity
= severity
130 alarm
.statistic
= statistic
131 alarm
.monitoring_param
= metric_name
132 alarm
.vdur_name
= vdur_name
133 alarm
.vnf_member_index
= vnf_member_index
134 alarm
.nsr_id
= nsr_id
140 def delete_alarm(self
, alarm_uuid
) -> None:
143 alarm
= (Alarm
.select()
144 .where(Alarm
.uuid
== alarm_uuid
)
146 alarm
.delete_instance()
149 def get_vim_type(self
, vim_account_id
) -> str:
150 """Get the vim type that is required by the message."""
151 credentials
= self
.get_credentials(vim_account_id
)
152 config
= json
.loads(credentials
.config
)
153 if 'vim_type' in config
:
154 vim_type
= config
['vim_type']
155 return str(vim_type
.lower())
157 return str(credentials
.type)