Adds migration engine for peewee ORM
[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
29 from peewee import CharField, TextField, FloatField, Model, AutoField, Proxy
30 from peewee_migrate import Router
31 from playhouse.db_url import connect
32
33 from osm_mon import migrations
34 from osm_mon.core.config import Config
35
36 log = logging.getLogger(__name__)
37
38 db = Proxy()
39
40
41 class BaseModel(Model):
42 id = AutoField(primary_key=True)
43
44 class Meta:
45 database = db
46
47
48 class VimCredentials(BaseModel):
49 uuid = CharField(unique=True)
50 name = CharField()
51 type = CharField()
52 url = CharField()
53 user = CharField()
54 password = CharField()
55 tenant_name = CharField()
56 config = TextField(default='{}')
57
58
59 class Alarm(BaseModel):
60 uuid = CharField(unique=True)
61 name = CharField()
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()
69 nsr_id = CharField()
70
71
72 class DatabaseManager:
73 def __init__(self, config: Config):
74 db.initialize(connect(config.get('sql', 'database_uri')))
75
76 def create_tables(self) -> None:
77 with db.atomic():
78 router = Router(db, os.path.dirname(migrations.__file__))
79 router.run()
80
81 def get_credentials(self, vim_uuid: str = None) -> VimCredentials:
82 with db.atomic():
83 return VimCredentials.get_or_none(VimCredentials.uuid == vim_uuid)
84
85 def save_credentials(self, vim_credentials) -> VimCredentials:
86 """Saves vim credentials. If a record with same uuid exists, overwrite it."""
87 with db.atomic():
88 exists = VimCredentials.get_or_none(VimCredentials.uuid == vim_credentials.uuid)
89 if exists:
90 vim_credentials.id = exists.id
91 vim_credentials.save()
92 return vim_credentials
93
94 def get_alarm(self, alarm_id) -> Alarm:
95 with db.atomic():
96 alarm = (Alarm.select()
97 .where(Alarm.alarm_id == alarm_id)
98 .get())
99 return alarm
100
101 def save_alarm(self, name, threshold, operation, severity, statistic, metric_name, vdur_name,
102 vnf_member_index, nsr_id) -> Alarm:
103 """Saves alarm."""
104 # TODO: Add uuid optional param and check if exists to handle updates (see self.save_credentials)
105 with db.atomic():
106 alarm = Alarm()
107 alarm.uuid = str(uuid.uuid4())
108 alarm.name = name
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
117 alarm.save()
118 return alarm
119
120 def delete_alarm(self, alarm_uuid) -> None:
121 with db.atomic():
122 alarm = (Alarm.select()
123 .where(Alarm.uuid == alarm_uuid)
124 .get())
125 alarm.delete_instance()
126
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)