Readds logic to obtain vim_type when it is available in vim config 63/7863/2
authorBenjamin Diaz <bdiaz@whitestack.com>
Tue, 27 Aug 2019 20:49:59 +0000 (17:49 -0300)
committerBenjamin Diaz <bdiaz@whitestack.com>
Tue, 27 Aug 2019 20:58:23 +0000 (17:58 -0300)
bug 835

Change-Id: Ia8a870ede8b6ceac71329a09a25c7d9ba4d6219e
Signed-off-by: Benjamin Diaz <bdiaz@whitestack.com>
osm_mon/collector/service.py
osm_mon/tests/unit/collector/test_collector_service.py

index 29ca132..f329918 100644 (file)
@@ -61,8 +61,7 @@ class CollectorService:
 
     def _collect_vim_metrics(self, vnfr: dict, vim_account_id: str):
         # TODO(diazb) Add support for aws
-        common_db = CommonDbClient(self.conf)
-        vim_type = common_db.get_vim_account(vim_account_id)['vim_type']
+        vim_type = self._get_vim_type(vim_account_id)
         if vim_type in VIM_COLLECTORS:
             collector = VIM_COLLECTORS[vim_type](self.conf, vim_account_id)
             metrics = collector.collect(vnfr)
@@ -72,8 +71,7 @@ class CollectorService:
             log.debug("vimtype %s is not supported.", vim_type)
 
     def _collect_vim_infra_metrics(self, vim_account_id: str):
-        common_db = CommonDbClient(self.conf)
-        vim_type = common_db.get_vim_account(vim_account_id)['vim_type']
+        vim_type = self._get_vim_type(vim_account_id)
         if vim_type in VIM_INFRA_COLLECTORS:
             collector = VIM_INFRA_COLLECTORS[vim_type](self.conf, vim_account_id)
             metrics = collector.collect()
@@ -134,3 +132,11 @@ class CollectorService:
         while not self.queue.empty():
             metrics.append(self.queue.get())
         return metrics
+
+    def _get_vim_type(self, vim_account_id: str) -> str:
+        common_db = CommonDbClient(self.conf)
+        vim_account = common_db.get_vim_account(vim_account_id)
+        vim_type = vim_account['vim_type']
+        if 'config' in vim_account and 'vim_type' in vim_account['config']:
+            vim_type = vim_account['config']['vim_type'].lower()
+        return vim_type
index 5539d3c..61a288a 100644 (file)
@@ -24,6 +24,7 @@ from unittest import TestCase, mock
 
 from osm_mon.collector.service import CollectorService
 from osm_mon.collector.vnf_collectors.openstack import OpenstackCollector
+from osm_mon.collector.vnf_collectors.vio import VIOCollector
 from osm_mon.core.common_db import CommonDbClient
 from osm_mon.core.config import Config
 
@@ -51,6 +52,15 @@ class CollectorServiceTest(TestCase):
         collector._collect_vim_metrics({}, 'test_vim_account_id')
         openstack_collect.assert_not_called()
 
+    @mock.patch.object(VIOCollector, "__init__", lambda *args, **kwargs: None)
+    @mock.patch.object(VIOCollector, "collect")
+    @mock.patch.object(CommonDbClient, "get_vim_account")
+    def test_init_vim_collector_and_collect_vio(self, _get_vim_account, vio_collect):
+        _get_vim_account.return_value = {'vim_type': 'openstack', 'config': {'vim_type': 'VIO'}}
+        collector = CollectorService(self.config)
+        collector._collect_vim_metrics({}, 'test_vim_account_id')
+        vio_collect.assert_called_once_with({})
+
     @mock.patch("osm_mon.collector.service.VCACollector", autospec=True)
     def test_collect_vca_metrics(self, vca_collector):
         collector = CollectorService(self.config)