a41c0fddcd3c654e80531f931319d346fb6979ef
[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 import uuid
27
28 from peewee import CharField, TextField, FloatField, Model, AutoField
29 from playhouse.db_url import connect
30
31 from osm_mon.core.settings import Config
32
33 log = logging.getLogger(__name__)
34 cfg = Config.instance()
35 cfg.read_environ()
36
37 db = connect(cfg.DATABASE)
38
39
40 class BaseModel(Model):
41 id = AutoField(primary_key=True)
42
43 class Meta:
44 database = db
45
46
47 class VimCredentials(BaseModel):
48 uuid = CharField(unique=True)
49 name = CharField()
50 type = CharField()
51 url = CharField()
52 user = CharField()
53 password = CharField()
54 tenant_name = CharField()
55 config = TextField(default='{}')
56
57
58 class Alarm(BaseModel):
59 uuid = CharField(unique=True)
60 name = CharField()
61 severity = CharField()
62 threshold = FloatField()
63 operation = CharField()
64 statistic = CharField()
65 monitoring_param = CharField()
66 vdur_name = CharField()
67 vnf_member_index = CharField()
68 nsr_id = CharField()
69
70
71 class DatabaseManager:
72 def create_tables(self) -> None:
73 try:
74 db.connect()
75 db.create_tables([VimCredentials, Alarm])
76 db.close()
77 except Exception:
78 log.exception("Error creating tables: ")
79
80 def get_credentials(self, vim_uuid) -> VimCredentials:
81 return VimCredentials.get_or_none(VimCredentials.uuid == vim_uuid)
82
83 def save_credentials(self, vim_credentials) -> VimCredentials:
84 """Saves vim credentials. If a record with same uuid exists, overwrite it."""
85 exists = VimCredentials.get_or_none(VimCredentials.uuid == vim_credentials.uuid)
86 if exists:
87 vim_credentials.id = exists.id
88 vim_credentials.save()
89 return vim_credentials
90
91 def get_alarm(self, alarm_id) -> Alarm:
92 alarm = (Alarm.select()
93 .where(Alarm.alarm_id == alarm_id)
94 .get())
95 return alarm
96
97 def save_alarm(self, name, threshold, operation, severity, statistic, metric_name, vdur_name,
98 vnf_member_index, nsr_id) -> Alarm:
99 """Saves alarm."""
100 # TODO: Add uuid optional param and check if exists to handle updates (see self.save_credentials)
101 alarm = Alarm()
102 alarm.uuid = str(uuid.uuid4())
103 alarm.name = name
104 alarm.threshold = threshold
105 alarm.operation = operation
106 alarm.severity = severity
107 alarm.statistic = statistic
108 alarm.monitoring_param = metric_name
109 alarm.vdur_name = vdur_name
110 alarm.vnf_member_index = vnf_member_index
111 alarm.nsr_id = nsr_id
112 alarm.save()
113 return alarm
114
115 def delete_alarm(self, alarm_uuid) -> None:
116 alarm = (Alarm.select()
117 .where(Alarm.uuid == alarm_uuid)
118 .get())
119 alarm.delete_instance()
120
121 def get_vim_type(self, vim_account_id) -> str:
122 """Get the vim type that is required by the message."""
123 credentials = self.get_credentials(vim_account_id)
124 return str(credentials.type)