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
29 from peewee
import CharField
, TextField
, FloatField
, Model
, AutoField
, Proxy
30 from peewee_migrate
import Router
31 from playhouse
.db_url
import connect
33 from osm_mon
import migrations
34 from osm_mon
.core
.config
import Config
36 log
= logging
.getLogger(__name__
)
41 class BaseModel(Model
):
42 id = AutoField(primary_key
=True)
48 class VimCredentials(BaseModel
):
49 uuid
= CharField(unique
=True)
54 password
= CharField()
55 tenant_name
= CharField()
56 config
= TextField(default
='{}')
59 class Alarm(BaseModel
):
60 uuid
= CharField(unique
=True)
62 severity
= CharField()
63 threshold
= FloatField()
64 operation
= CharField()
65 statistic
= CharField()
66 monitoring_param
= CharField()
67 vdur_name
= CharField()
68 vnf_member_index
= CharField()
72 class DatabaseManager
:
73 def __init__(self
, config
: Config
):
74 db
.initialize(connect(config
.get('sql', 'database_uri')))
76 def create_tables(self
) -> None:
78 router
= Router(db
, os
.path
.dirname(migrations
.__file
__))
81 def get_credentials(self
, vim_uuid
: str = None) -> VimCredentials
:
83 return VimCredentials
.get_or_none(VimCredentials
.uuid
== vim_uuid
)
85 def save_credentials(self
, vim_credentials
) -> VimCredentials
:
86 """Saves vim credentials. If a record with same uuid exists, overwrite it."""
88 exists
= VimCredentials
.get_or_none(VimCredentials
.uuid
== vim_credentials
.uuid
)
90 vim_credentials
.id = exists
.id
91 vim_credentials
.save()
92 return vim_credentials
94 def get_alarm(self
, alarm_id
) -> Alarm
:
96 alarm
= (Alarm
.select()
97 .where(Alarm
.alarm_id
== alarm_id
)
101 def save_alarm(self
, name
, threshold
, operation
, severity
, statistic
, metric_name
, vdur_name
,
102 vnf_member_index
, nsr_id
) -> Alarm
:
104 # TODO: Add uuid optional param and check if exists to handle updates (see self.save_credentials)
107 alarm
.uuid
= str(uuid
.uuid4())
109 alarm
.threshold
= threshold
110 alarm
.operation
= operation
111 alarm
.severity
= severity
112 alarm
.statistic
= statistic
113 alarm
.monitoring_param
= metric_name
114 alarm
.vdur_name
= vdur_name
115 alarm
.vnf_member_index
= vnf_member_index
116 alarm
.nsr_id
= nsr_id
120 def delete_alarm(self
, alarm_uuid
) -> None:
122 alarm
= (Alarm
.select()
123 .where(Alarm
.uuid
== alarm_uuid
)
125 alarm
.delete_instance()
127 def get_vim_type(self
, vim_account_id
) -> str:
128 """Get the vim type that is required by the message."""
129 credentials
= self
.get_credentials(vim_account_id
)
130 return str(credentials
.type)