Merge "Added VIO metrics collector for vROPs"
[osm/MON.git] / osm_mon / core / database.py
index 4cbd75f..2a17f9b 100644 (file)
 ##
 
 import logging
+import os
 import uuid
+import json
 
 from peewee import CharField, TextField, FloatField, Model, AutoField, Proxy
+from peewee_migrate import Router
 from playhouse.db_url import connect
 
-from osm_mon.core.settings import Config
+from osm_mon import migrations
+from osm_mon.core.config import Config
 
 log = logging.getLogger(__name__)
 
@@ -50,7 +54,7 @@ class VimCredentials(BaseModel):
     user = CharField()
     password = CharField()
     tenant_name = CharField()
-    config = TextField(default='{}')
+    config = TextField()
 
 
 class Alarm(BaseModel):
@@ -67,14 +71,13 @@ class Alarm(BaseModel):
 
 
 class DatabaseManager:
-    def __init__(self):
-        cfg = Config.instance()
-        cfg.read_environ()
-        db.initialize(connect(cfg.DATABASE))
+    def __init__(self, config: Config):
+        db.initialize(connect(config.get('sql', 'database_uri')))
 
     def create_tables(self) -> None:
         with db.atomic():
-            db.create_tables([VimCredentials, Alarm])
+            router = Router(db, os.path.dirname(migrations.__file__))
+            router.run()
 
     def get_credentials(self, vim_uuid: str = None) -> VimCredentials:
         with db.atomic():
@@ -124,5 +127,11 @@ class DatabaseManager:
 
     def get_vim_type(self, vim_account_id) -> str:
         """Get the vim type that is required by the message."""
+        vim_type = None
         credentials = self.get_credentials(vim_account_id)
-        return str(credentials.type)
+        config = json.loads(credentials.config)
+        if 'vim_type' in config:
+            vim_type = config['vim_type']
+            return str(vim_type.lower())
+        else:
+            return str(credentials.type)