Collect null project_ids as empty strings
[osm/MON.git] / osm_mon / collector / vnf_collectors / openstack.py
index 0215b51..60b387c 100644 (file)
@@ -24,10 +24,11 @@ from enum import Enum
 from typing import List
 
 import gnocchiclient.exceptions
-from ceilometerclient.v2 import client as ceilometer_client
+from ceilometerclient import client as ceilometer_client
+from ceilometerclient.exc import HTTPException
 from gnocchiclient.v1 import client as gnocchi_client
-from keystoneauth1.exceptions.catalog import EndpointNotFound
 from keystoneclient.v3 import client as keystone_client
+from keystoneauth1.exceptions.catalog import EndpointNotFound
 from neutronclient.v2_0 import client as neutron_client
 
 from osm_mon.collector.metric import Metric
@@ -64,12 +65,12 @@ class MetricType(Enum):
 class OpenstackCollector(BaseVimCollector):
     def __init__(self, config: Config, vim_account_id: str):
         super().__init__(config, vim_account_id)
-        self.conf = config
         self.common_db = CommonDbClient(config)
-        self.backend = self._get_backend(vim_account_id)
+        vim_account = self.common_db.get_vim_account(vim_account_id)
+        self.backend = self._get_backend(vim_account)
 
-    def _build_keystone_client(self, vim_account_id: str) -> keystone_client.Client:
-        sess = OpenstackUtils.get_session(vim_account_id)
+    def _build_keystone_client(self, vim_account: dict) -> keystone_client.Client:
+        sess = OpenstackUtils.get_session(vim_account)
         return keystone_client.Client(session=sess)
 
     def _get_resource_uuid(self, nsr_id: str, vnf_member_index: str, vdur_name: str) -> str:
@@ -80,6 +81,15 @@ class OpenstackCollector(BaseVimCollector):
         nsr_id = vnfr['nsr-id-ref']
         vnf_member_index = vnfr['member-vnf-index-ref']
         vnfd = self.common_db.get_vnfd(vnfr['vnfd-id'])
+
+        # Populate extra tags for metrics
+        tags = {}
+        tags['ns_name'] = self.common_db.get_nsr(nsr_id)['name']
+        if vnfr['_admin']['projects_read']:
+            tags['project_id'] = vnfr['_admin']['projects_read'][0]
+        else:
+            tags['project_id'] = ''
+
         metrics = []
         for vdur in vnfr['vdur']:
             # This avoids errors when vdur records have not been completely filled
@@ -106,7 +116,6 @@ class OpenstackCollector(BaseVimCollector):
                         value = self.backend.collect_metric(metric_type, openstack_metric_name, resource_id,
                                                             interface_name)
                         if value is not None:
-                            tags = {}
                             if interface_name:
                                 tags['interface'] = interface_name
                             metric = VnfMetric(nsr_id, vnf_member_index, vdur['name'], metric_name, value, tags)
@@ -115,14 +124,14 @@ class OpenstackCollector(BaseVimCollector):
                         log.exception("Error collecting metric %s for vdu %s" % (metric_name, vdur['name']))
         return metrics
 
-    def _get_backend(self, vim_account_id: str):
+    def _get_backend(self, vim_account: dict):
         try:
-            ceilometer = CeilometerBackend(vim_account_id)
+            ceilometer = CeilometerBackend(vim_account)
             ceilometer.client.capabilities.get()
             return ceilometer
-        except EndpointNotFound:
-            gnocchi = GnocchiBackend(vim_account_id)
-            gnocchi.client.status.get()
+        except (HTTPException, EndpointNotFound):
+            gnocchi = GnocchiBackend(vim_account)
+            gnocchi.client.metric.list(limit=1)
             return gnocchi
 
     def _get_metric_type(self, metric_name: str, interface_name: str) -> MetricType:
@@ -141,16 +150,16 @@ class OpenstackBackend:
 
 class GnocchiBackend(OpenstackBackend):
 
-    def __init__(self, vim_account_id: str):
-        self.client = self._build_gnocchi_client(vim_account_id)
-        self.neutron = self._build_neutron_client(vim_account_id)
+    def __init__(self, vim_account: dict):
+        self.client = self._build_gnocchi_client(vim_account)
+        self.neutron = self._build_neutron_client(vim_account)
 
-    def _build_gnocchi_client(self, vim_account_id: str) -> gnocchi_client.Client:
-        sess = OpenstackUtils.get_session(vim_account_id)
+    def _build_gnocchi_client(self, vim_account: dict) -> gnocchi_client.Client:
+        sess = OpenstackUtils.get_session(vim_account)
         return gnocchi_client.Client(session=sess)
 
-    def _build_neutron_client(self, vim_account_id: str) -> neutron_client.Client:
-        sess = OpenstackUtils.get_session(vim_account_id)
+    def _build_neutron_client(self, vim_account: dict) -> neutron_client.Client:
+        sess = OpenstackUtils.get_session(vim_account)
         return neutron_client.Client(session=sess)
 
     def collect_metric(self, metric_type: MetricType, metric_name: str, resource_id: str, interface_name: str):
@@ -215,12 +224,12 @@ class GnocchiBackend(OpenstackBackend):
 
 
 class CeilometerBackend(OpenstackBackend):
-    def __init__(self, vim_account_id: str):
-        self.client = self._build_ceilometer_client(vim_account_id)
+    def __init__(self, vim_account: dict):
+        self.client = self._build_ceilometer_client(vim_account)
 
-    def _build_ceilometer_client(self, vim_account_id: str) -> ceilometer_client.Client:
-        sess = OpenstackUtils.get_session(vim_account_id)
-        return ceilometer_client.Client(session=sess)
+    def _build_ceilometer_client(self, vim_account: dict) -> ceilometer_client.Client:
+        sess = OpenstackUtils.get_session(vim_account)
+        return ceilometer_client.Client("2", session=sess)
 
     def collect_metric(self, metric_type: MetricType, metric_name: str, resource_id: str, interface_name: str):
         if metric_type != MetricType.INSTANCE: