Refactor common_db client code 49/6649/3
authorBenjamin Diaz <bdiaz@whitestack.com>
Mon, 8 Oct 2018 15:20:47 +0000 (12:20 -0300)
committerBenjamin Diaz <bdiaz@whitestack.com>
Sun, 14 Oct 2018 21:28:35 +0000 (18:28 -0300)
Creates CommonDbClient which exposes methods to interact with the common
database, using osm_common module.

Signed-off-by: Benjamin Diaz <bdiaz@whitestack.com>
Change-Id: I98babcdb892e86fdb14d04873b2bbbdabbdad23f

osm_mon/common/__init__.py [new file with mode: 0644]
osm_mon/common/common_db_client.py [new file with mode: 0644]
osm_mon/core/message_bus/common_consumer.py
osm_mon/test/OpenStack/integration/test_alarm_integration.py
osm_mon/test/core/test_common_consumer.py

diff --git a/osm_mon/common/__init__.py b/osm_mon/common/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/osm_mon/common/common_db_client.py b/osm_mon/common/common_db_client.py
new file mode 100644 (file)
index 0000000..920fe36
--- /dev/null
@@ -0,0 +1,67 @@
+# -*- coding: utf-8 -*-
+
+# Copyright 2018 Whitestack, LLC
+# *************************************************************
+
+# This file is part of OSM Monitoring module
+# All Rights Reserved to Whitestack, LLC
+
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+
+#         http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+# For those usages not covered by the Apache License, Version 2.0 please
+# contact: bdiaz@whitestack.com or glavado@whitestack.com
+##
+from osm_common import dbmongo
+
+from osm_mon.core.settings import Config
+
+
+class CommonDbClient:
+    def __init__(self):
+        cfg = Config.instance()
+        self.common_db = dbmongo.DbMongo()
+        self.common_db.db_connect({'host': cfg.MONGO_URI.split(':')[0],
+                                   'port': int(cfg.MONGO_URI.split(':')[1]),
+                                   'name': 'osm'})
+
+    def get_vnfr(self, nsr_id: str, member_index: int):
+        vnfr = self.common_db.get_one("vnfrs",
+                                      {"nsr-id-ref": nsr_id, "member-vnf-index-ref": str(member_index)})
+        return vnfr
+
+    def get_vnfrs(self, nsr_id: str):
+        return [self.get_vnfr(nsr_id, member['member-vnf-index']) for member in
+                self.get_nsr(nsr_id)['nsd']['constituent-vnfd']]
+
+    def get_vnfd(self, vnfd_id: str):
+        vnfr = self.common_db.get_one("vnfds",
+                                      {"_id": vnfd_id})
+        return vnfr
+
+    def get_nsr(self, nsr_id: str):
+        nsr = self.common_db.get_one("nsrs",
+                                     {"id": nsr_id})
+        return nsr
+
+    def get_nslcmop(self, nslcmop_id):
+        nslcmop = self.common_db.get_one("nslcmops",
+                                         {"_id": nslcmop_id})
+        return nslcmop
+
+    def get_vdur(self, nsr_id, member_index, vdu_name):
+        vnfr = self.get_vnfr(nsr_id, member_index)
+        for vdur in vnfr['vdur']:
+            if vdur['name'] == vdu_name:
+                return vdur
+        raise ValueError('vdur not found for nsr-id %s, member_index %s and vdu_name %s', nsr_id, member_index,
+                         vdu_name)
index 3af2d4a..fd97f83 100755 (executable)
@@ -27,8 +27,8 @@ import threading
 import six
 import yaml
 from kafka import KafkaConsumer
-from osm_common import dbmongo
 
+from osm_mon.common.common_db_client import CommonDbClient
 from osm_mon.core.auth import AuthManager
 from osm_mon.core.database import DatabaseManager
 from osm_mon.core.settings import Config
@@ -79,9 +79,7 @@ class CommonConsumer:
         self.vrops_rcvr = plugin_receiver.PluginReceiver()
 
         log.info("Connecting to MongoDB...")
-        self.common_db = dbmongo.DbMongo()
-        common_db_uri = cfg.MONGO_URI.split(':')
-        self.common_db.db_connect({'host': common_db_uri[0], 'port': int(common_db_uri[1]), 'name': 'osm'})
+        self.common_db = CommonDbClient()
         log.info("Connection successful.")
 
     def get_vim_type(self, vim_uuid):
@@ -89,19 +87,6 @@ class CommonConsumer:
         credentials = self.database_manager.get_credentials(vim_uuid)
         return credentials.type
 
-    def get_vdur(self, nsr_id, member_index, vdu_name):
-        vnfr = self.get_vnfr(nsr_id, member_index)
-        for vdur in vnfr['vdur']:
-            if vdur['name'] == vdu_name:
-                return vdur
-        raise ValueError('vdur not found for nsr-id %s, member_index %s and vdu_name %s', nsr_id, member_index,
-                         vdu_name)
-
-    def get_vnfr(self, nsr_id, member_index):
-        vnfr = self.common_db.get_one("vnfrs",
-                                      {"nsr-id-ref": nsr_id, "member-vnf-index-ref": str(member_index)})
-        return vnfr
-
     def run(self):
         common_consumer = KafkaConsumer(bootstrap_servers=cfg.BROKER_URI,
                                         key_deserializer=bytes.decode,
@@ -151,12 +136,12 @@ class CommonConsumer:
                 vnf_index = values[list_index]['vnf_member_index'] if contains_list else values['vnf_member_index']
 
                 # Check the vim desired by the message
-                vnfr = self.get_vnfr(ns_id, vnf_index)
+                vnfr = self.common_db.get_vnfr(ns_id, vnf_index)
                 vim_uuid = vnfr['vim-account-id']
 
                 if (contains_list and 'vdu_name' in values[list_index]) or 'vdu_name' in values:
                     vdu_name = values[list_index]['vdu_name'] if contains_list else values['vdu_name']
-                    vdur = self.get_vdur(ns_id, vnf_index, vdu_name)
+                    vdur = self.common_db.get_vdur(ns_id, vnf_index, vdu_name)
                     if contains_list:
                         values[list_index]['resource_uuid'] = vdur['vim-id']
                     else:
@@ -184,7 +169,7 @@ class CommonConsumer:
 
                 elif vim_type == "vmware":
                     log.info("This metric_request message is for the vROPs plugin.")
-                    self.vrops_rcvr.consume(message,vim_uuid)
+                    self.vrops_rcvr.consume(message, vim_uuid)
 
                 else:
                     log.debug("vim_type is misconfigured or unsupported; %s",
index bdd2033..278c8c2 100644 (file)
@@ -217,7 +217,7 @@ class AlarmIntegrationTest(unittest.TestCase):
         for message in self.req_consumer:
             if message.key == "acknowledge_alarm":
                 self.alarms.alarming(message, 'test_id')
-                ack_alarm.assert_called_with(mock.ANY, mock.ANY, 'alarm_id')
+                ack_alarm.assert_called_with(mock.ANY, mock.ANY, 'alarm_id', True)
                 return
 
         self.fail("No message received in consumer")
index ddbdf8b..0306893 100644 (file)
@@ -3,6 +3,7 @@ import unittest
 import mock
 from kafka import KafkaProducer
 from kafka.errors import KafkaError
+from osm_common import dbmongo
 
 from osm_mon.core.database import VimCredentials
 from osm_mon.core.message_bus.common_consumer import *
@@ -51,7 +52,8 @@ class CommonConsumerTest(unittest.TestCase):
                                         'internal-connection-point': [],
                                         'vdu-id-ref': 'ubuntuvnf_vnfd-VM',
                                         'id': 'ffd73f33-c8bb-4541-a977-44dcc3cbe28d',
-                                        'vim-id': '27042672-5190-4209-b844-95bbaeea7ea7'
+                                        'vim-id': '27042672-5190-4209-b844-95bbaeea7ea7',
+                                        'name': 'ubuntuvnf_vnfd-VM'
                                     }
                                 ],
                                 'vnfd-ref': 'ubuntuvnf_vnfd',
@@ -60,12 +62,13 @@ class CommonConsumerTest(unittest.TestCase):
                                 'vnfd-id': 'a314c865-aee7-4d9b-9c9d-079d7f857f01',
                                 'id': 'a314c865-aee7-4d9b-9c9d-079d7f857f01'}
         common_consumer = CommonConsumer()
-        vdur = common_consumer.get_vdur('5ec3f571-d540-4cb0-9992-971d1b08312e', '1', 'ubuntuvnf_vnfd-VM')
+        vdur = common_consumer.common_db.get_vdur('5ec3f571-d540-4cb0-9992-971d1b08312e', '1', 'ubuntuvnf_vnfd-VM')
         expected_vdur = {
             'internal-connection-point': [],
             'vdu-id-ref': 'ubuntuvnf_vnfd-VM',
             'id': 'ffd73f33-c8bb-4541-a977-44dcc3cbe28d',
-            'vim-id': '27042672-5190-4209-b844-95bbaeea7ea7'
+            'vim-id': '27042672-5190-4209-b844-95bbaeea7ea7',
+            'name': 'ubuntuvnf_vnfd-VM'
         }
 
         self.assertDictEqual(vdur, expected_vdur)