3b601b25cf00f59bc7a2e34b5134dc0a890d6611
[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 os
27 import uuid
28 import json
29
30 from peewee import CharField, TextField, FloatField, Model, AutoField, Proxy
31 from peewee_migrate import Router
32 from playhouse.db_url import connect
33
34 from osm_mon import migrations
35 from osm_mon.core.config import Config
36
37 log = logging.getLogger(__name__)
38
39 db = Proxy()
40
41
42 class BaseModel(Model):
43 id = AutoField(primary_key=True)
44
45 class Meta:
46 database = db
47
48
49 class VimCredentials(BaseModel):
50 uuid = CharField(unique=True)
51 name = CharField()
52 type = CharField()
53 url = CharField()
54 user = CharField()
55 password = CharField()
56 tenant_name = CharField()
57 config = TextField()
58
59
60 class Alarm(BaseModel):
61 uuid = CharField(unique=True)
62 name = CharField()
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()
70 nsr_id = CharField()
71
72
73 class DatabaseManager:
74 def __init__(self, config: Config):
75 db.initialize(connect(config.get('sql', 'database_uri')))
76
77 def create_tables(self) -> None:
78 db.connect()
79 with db.atomic():
80 router = Router(db, os.path.dirname(migrations.__file__))
81 router.run()
82 db.close()
83
84 def get_credentials(self, vim_uuid: str = None) -> VimCredentials:
85 db.connect()
86 with db.atomic():
87 vim_credentials = VimCredentials.get_or_none(VimCredentials.uuid == vim_uuid)
88 db.close()
89 return vim_credentials
90
91 def save_credentials(self, vim_credentials) -> VimCredentials:
92 """Saves vim credentials. If a record with same uuid exists, overwrite it."""
93 db.connect()
94 with db.atomic():
95 exists = VimCredentials.get_or_none(VimCredentials.uuid == vim_credentials.uuid)
96 if exists:
97 vim_credentials.id = exists.id
98 vim_credentials.save()
99 db.close()
100 return vim_credentials
101
102 def get_alarm(self, alarm_id) -> Alarm:
103 db.connect()
104 with db.atomic():
105 alarm = (Alarm.select()
106 .where(Alarm.alarm_id == alarm_id)
107 .get())
108 db.close()
109 return alarm
110
111 def save_alarm(self, name, threshold, operation, severity, statistic, metric_name, vdur_name,
112 vnf_member_index, nsr_id) -> Alarm:
113 """Saves alarm."""
114 # TODO: Add uuid optional param and check if exists to handle updates (see self.save_credentials)
115 db.connect()
116 with db.atomic():
117 alarm = Alarm()
118 alarm.uuid = str(uuid.uuid4())
119 alarm.name = name
120 alarm.threshold = threshold
121 alarm.operation = operation
122 alarm.severity = severity
123 alarm.statistic = statistic
124 alarm.monitoring_param = metric_name
125 alarm.vdur_name = vdur_name
126 alarm.vnf_member_index = vnf_member_index
127 alarm.nsr_id = nsr_id
128 alarm.save()
129 db.close()
130 return alarm
131
132 def delete_alarm(self, alarm_uuid) -> None:
133 db.connect()
134 with db.atomic():
135 alarm = (Alarm.select()
136 .where(Alarm.uuid == alarm_uuid)
137 .get())
138 alarm.delete_instance()
139 db.close()
140
141 def get_vim_type(self, vim_account_id) -> str:
142 """Get the vim type that is required by the message."""
143 vim_type = None
144 credentials = self.get_credentials(vim_account_id)
145 config = json.loads(credentials.config)
146 if 'vim_type' in config:
147 vim_type = config['vim_type']
148 return str(vim_type.lower())
149 else:
150 return str(credentials.type)