Adds alarm engine
[osm/MON.git] / osm_mon / core / database.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
5
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
8
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
12
13 # http://www.apache.org/licenses/LICENSE-2.0
14
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
19 # under the License.
20
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact: bdiaz@whitestack.com or glavado@whitestack.com
23 ##
24
25 import logging
26
27 from peewee import CharField, TextField, FloatField, Model
28 from playhouse.db_url import connect
29
30 from osm_mon.core.settings import Config
31
32 log = logging.getLogger(__name__)
33 cfg = Config.instance()
34 cfg.read_environ()
35
36 db = connect(cfg.DATABASE)
37
38
39 class BaseModel(Model):
40
41 class Meta:
42 database = db
43
44
45 class VimCredentials(BaseModel):
46 uuid = CharField(unique=True)
47 name = CharField()
48 type = CharField()
49 url = CharField()
50 user = CharField()
51 password = CharField()
52 tenant_name = CharField()
53 config = TextField(default='{}')
54
55
56 class Alarm(BaseModel):
57 name = CharField()
58 severity = CharField()
59 threshold = FloatField()
60 operation = CharField()
61 statistic = CharField()
62 monitoring_param = CharField()
63 vdur_name = CharField()
64 vnf_member_index = CharField()
65 nsr_id = CharField()
66
67
68 class DatabaseManager:
69 def create_tables(self) -> None:
70 try:
71 db.connect()
72 db.create_tables([VimCredentials, Alarm])
73 db.close()
74 except Exception:
75 log.exception("Error creating tables: ")
76
77 def get_credentials(self, vim_uuid) -> VimCredentials:
78 return VimCredentials.get_or_none(VimCredentials.uuid == vim_uuid)
79
80 def save_credentials(self, vim_credentials) -> VimCredentials:
81 """Saves vim credentials. If a record with same uuid exists, overwrite it."""
82 exists = VimCredentials.get_or_none(VimCredentials.uuid == vim_credentials.uuid)
83 if exists:
84 vim_credentials.uuid = exists.uuid
85 vim_credentials.save()
86 return vim_credentials
87
88 def get_alarm(self, alarm_id) -> Alarm:
89 alarm = (Alarm.select()
90 .where(Alarm.alarm_id == alarm_id)
91 .get())
92 return alarm
93
94 def save_alarm(self, name, threshold, operation, severity, statistic, metric_name, vdur_name,
95 vnf_member_index, nsr_id) -> Alarm:
96 """Saves alarm."""
97 alarm = Alarm()
98 alarm.name = name
99 alarm.threshold = threshold
100 alarm.operation = operation
101 alarm.severity = severity
102 alarm.statistic = statistic
103 alarm.monitoring_param = metric_name
104 alarm.vdur_name = vdur_name
105 alarm.vnf_member_index = vnf_member_index
106 alarm.nsr_id = nsr_id
107 alarm.save()
108 return alarm
109
110 def delete_alarm(self, alarm_id) -> None:
111 alarm = (Alarm.select()
112 .where(Alarm.alarm_id == alarm_id)
113 .get())
114 alarm.delete()
115
116 def get_vim_type(self, vim_account_id) -> str:
117 """Get the vim type that is required by the message."""
118 credentials = self.get_credentials(vim_account_id)
119 return str(credentials.type)