4cbd75f8fe60e6c0e7434158befb72f51b539016
[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, Proxy
29 from playhouse.db_url import connect
30
31 from osm_mon.core.settings import Config
32
33 log = logging.getLogger(__name__)
34
35 db = Proxy()
36
37
38 class BaseModel(Model):
39 id = AutoField(primary_key=True)
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 uuid = CharField(unique=True)
58 name = CharField()
59 severity = CharField()
60 threshold = FloatField()
61 operation = CharField()
62 statistic = CharField()
63 monitoring_param = CharField()
64 vdur_name = CharField()
65 vnf_member_index = CharField()
66 nsr_id = CharField()
67
68
69 class DatabaseManager:
70 def __init__(self):
71 cfg = Config.instance()
72 cfg.read_environ()
73 db.initialize(connect(cfg.DATABASE))
74
75 def create_tables(self) -> None:
76 with db.atomic():
77 db.create_tables([VimCredentials, Alarm])
78
79 def get_credentials(self, vim_uuid: str = None) -> VimCredentials:
80 with db.atomic():
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 with db.atomic():
86 exists = VimCredentials.get_or_none(VimCredentials.uuid == vim_credentials.uuid)
87 if exists:
88 vim_credentials.id = exists.id
89 vim_credentials.save()
90 return vim_credentials
91
92 def get_alarm(self, alarm_id) -> Alarm:
93 with db.atomic():
94 alarm = (Alarm.select()
95 .where(Alarm.alarm_id == alarm_id)
96 .get())
97 return alarm
98
99 def save_alarm(self, name, threshold, operation, severity, statistic, metric_name, vdur_name,
100 vnf_member_index, nsr_id) -> Alarm:
101 """Saves alarm."""
102 # TODO: Add uuid optional param and check if exists to handle updates (see self.save_credentials)
103 with db.atomic():
104 alarm = Alarm()
105 alarm.uuid = str(uuid.uuid4())
106 alarm.name = name
107 alarm.threshold = threshold
108 alarm.operation = operation
109 alarm.severity = severity
110 alarm.statistic = statistic
111 alarm.monitoring_param = metric_name
112 alarm.vdur_name = vdur_name
113 alarm.vnf_member_index = vnf_member_index
114 alarm.nsr_id = nsr_id
115 alarm.save()
116 return alarm
117
118 def delete_alarm(self, alarm_uuid) -> None:
119 with db.atomic():
120 alarm = (Alarm.select()
121 .where(Alarm.uuid == alarm_uuid)
122 .get())
123 alarm.delete_instance()
124
125 def get_vim_type(self, vim_account_id) -> str:
126 """Get the vim type that is required by the message."""
127 credentials = self.get_credentials(vim_account_id)
128 return str(credentials.type)