Move from old N2VC to N2VCJujuConnector 28/9728/6
authorDavid Garcia <david.garcia@canonical.com>
Tue, 22 Sep 2020 09:29:01 +0000 (11:29 +0200)
committerbeierlm <mark.beierl@canonical.com>
Tue, 22 Sep 2020 16:52:53 +0000 (12:52 -0400)
Change-Id: Ib8b545823eb803c7e31c1d5c8fd8afc6a39e07a8
Signed-off-by: David Garcia <david.garcia@canonical.com>
devops-stages/stage-test.sh
osm_mon/collector/vnf_collectors/juju.py
requirements.txt
tox.ini

index d588666..4ad4f08 100755 (executable)
@@ -23,4 +23,4 @@
 #__date__   = "14/Sep/2017"
 
 #!/bin/bash
-tox
+tox --recreate
index 1ca8bf5..ba17d65 100644 (file)
@@ -23,7 +23,7 @@ import asyncio
 import logging
 from typing import List
 
-from n2vc.vnf import N2VC
+from n2vc.n2vc_juju_conn import N2VCJujuConnector
 
 from osm_mon.collector.metric import Metric
 from osm_mon.collector.vnf_collectors.base import BaseCollector
@@ -40,86 +40,137 @@ class VCACollector(BaseCollector):
         super().__init__(config)
         self.common_db = CommonDbClient(config)
         self.loop = asyncio.get_event_loop()
-        self.n2vc = N2VC(server=config.get('vca', 'host'),
-                         user=config.get('vca', 'user'),
-                         secret=config.get('vca', 'secret'),
-                         ca_cert=config.get('vca', 'cacert'))
+        host = config.get("vca", "host")
+        port = config.get("vca", "port") if "port" in config.conf["vca"] else 17070
+
+        # Backwards compatibility
+        if "cacert" in config.conf["vca"]:
+            ca_cert = config.conf["vca"].pop("cacert")
+            config.set("vca", "ca_cert", ca_cert)
+
+        if "pubkey" in config.conf["vca"]:
+            public_key = config.conf["vca"].pop("pubkey")
+            config.set("vca", "public_key", public_key)
+
+        if "apiproxy" in config.conf["vca"]:
+            api_proxy = config.conf["vca"].pop("apiproxy")
+            config.set("vca", "api_proxy", api_proxy)
+
+        self.n2vc = N2VCJujuConnector(
+            db=self.common_db.common_db,
+            fs=object(),
+            log=log,
+            loop=self.loop,
+            url="{}:{}".format(host, port),
+            username=config.get("vca", "user"),
+            vca_config=config.conf["vca"],
+            on_update_db=None,
+        )
 
     def collect(self, vnfr: dict) -> List[Metric]:
-        nsr_id = vnfr['nsr-id-ref']
-        vnf_member_index = vnfr['member-vnf-index-ref']
-        vnfd = self.common_db.get_vnfd(vnfr['vnfd-id'])
+        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]
+        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'] = ''
+            tags["project_id"] = ""
 
         metrics = []
-        for vdur in vnfr['vdur']:
+        for vdur in vnfr["vdur"]:
             # This avoids errors when vdur records have not been completely filled
-            if 'name' not in vdur:
+            if "name" not in vdur:
                 continue
-            vdu = next(
-                filter(lambda vdu: vdu['id'] == vdur['vdu-id-ref'], vnfd['vdu'])
-            )
-            if 'vdu-configuration' in vdu and 'metrics' in vdu['vdu-configuration']:
+            vdu = next(filter(lambda vdu: vdu["id"] == vdur["vdu-id-ref"], vnfd["vdu"]))
+            if "vdu-configuration" in vdu and "metrics" in vdu["vdu-configuration"]:
                 try:
-                    vca_deployment_info = self.get_vca_deployment_info(nsr_id, vnf_member_index, vdur['vdu-id-ref'],
-                                                                       vdur['count-index'])
+                    vca_deployment_info = self.get_vca_deployment_info(
+                        nsr_id,
+                        vnf_member_index,
+                        vdur["vdu-id-ref"],
+                        vdur["count-index"],
+                    )
                 except VcaDeploymentInfoNotFound as e:
                     log.warning(repr(e))
                     continue
-                measures = self.loop.run_until_complete(self.n2vc.GetMetrics(vca_deployment_info['model'],
-                                                                             vca_deployment_info['application']))
-                log.debug('Measures: %s', measures)
+                measures = self.loop.run_until_complete(
+                    self.n2vc.get_metrics(
+                        vca_deployment_info["model"], vca_deployment_info["application"]
+                    )
+                )
+                log.debug("Measures: %s", measures)
                 for measure_list in measures.values():
                     for measure in measure_list:
                         log.debug("Measure: %s", measure)
-                        metric = VnfMetric(nsr_id, vnf_member_index, vdur['name'], measure['key'],
-                                           float(measure['value']), tags)
+                        metric = VnfMetric(
+                            nsr_id,
+                            vnf_member_index,
+                            vdur["name"],
+                            measure["key"],
+                            float(measure["value"]),
+                            tags,
+                        )
                         metrics.append(metric)
-        if 'vnf-configuration' in vnfd and 'metrics' in vnfd['vnf-configuration']:
+        if "vnf-configuration" in vnfd and "metrics" in vnfd["vnf-configuration"]:
             try:
-                vca_deployment_info = self.get_vca_deployment_info(nsr_id, vnf_member_index)
+                vca_deployment_info = self.get_vca_deployment_info(
+                    nsr_id, vnf_member_index
+                )
             except VcaDeploymentInfoNotFound as e:
                 log.warning(repr(e))
                 return metrics
-            measures = self.loop.run_until_complete(self.n2vc.GetMetrics(vca_deployment_info['model'],
-                                                                         vca_deployment_info['application']))
+            measures = self.loop.run_until_complete(
+                self.n2vc.get_metrics(
+                    vca_deployment_info["model"], vca_deployment_info["application"]
+                )
+            )
             # Search for Mgmt VDU name, needed to query Prometheus based on alarm tags
             # TODO: check a better way to look for Mgmt VDU
-            for vdur in vnfr['vdur']:
-                for interface in vdur['interfaces']:
-                    if 'mgmt-vnf' in interface:
-                        vdu_name = vdur['name']
+            for vdur in vnfr["vdur"]:
+                for interface in vdur["interfaces"]:
+                    if "mgmt-vnf" in interface:
+                        vdu_name = vdur["name"]
                         break
-            log.debug('Measures: %s', measures)
+            log.debug("Measures: %s", measures)
             for measure_list in measures.values():
                 for measure in measure_list:
                     log.debug("Measure: %s", measure)
-                    metric = VnfMetric(nsr_id, vnf_member_index, vdu_name,
-                                       measure['key'], float(measure['value']), tags)
+                    metric = VnfMetric(
+                        nsr_id,
+                        vnf_member_index,
+                        vdu_name,
+                        measure["key"],
+                        float(measure["value"]),
+                        tags,
+                    )
                     metrics.append(metric)
         return metrics
 
-    def get_vca_deployment_info(self, nsr_id, vnf_member_index, vdu_id=None, vdu_count=0):
+    def get_vca_deployment_info(
+        self, nsr_id, vnf_member_index, vdu_id=None, vdu_count=0
+    ):
         nsr = self.common_db.get_nsr(nsr_id)
         for vca_deployment in nsr["_admin"]["deployed"]["VCA"]:
             if vca_deployment:
                 if vdu_id is None:
-                    if vca_deployment['member-vnf-index'] == vnf_member_index and vca_deployment['vdu_id'] is None:
+                    if (
+                        vca_deployment["member-vnf-index"] == vnf_member_index
+                        and vca_deployment["vdu_id"] is None
+                    ):
                         return vca_deployment
                 else:
-                    if vca_deployment['member-vnf-index'] == vnf_member_index and \
-                            vca_deployment['vdu_id'] == vdu_id and vca_deployment['vdu_count_index'] == vdu_count:
+                    if (
+                        vca_deployment["member-vnf-index"] == vnf_member_index
+                        and vca_deployment["vdu_id"] == vdu_id
+                        and vca_deployment["vdu_count_index"] == vdu_count
+                    ):
                         return vca_deployment
         raise VcaDeploymentInfoNotFound(
             "VCA deployment info for nsr_id {}, index {}, vdu_id {} and vdu_count_index {} not found.".format(
-                nsr_id,
-                vnf_member_index,
-                vdu_id,
-                vdu_count))
+                nsr_id, vnf_member_index, vdu_id, vdu_count
+            )
+        )
index 3f1c5bd..d4eb273 100644 (file)
@@ -33,4 +33,4 @@ peewee-migrate==1.1.*
 python-novaclient==12.0.*
 python-neutronclient==5.1.*
 git+https://osm.etsi.org/gerrit/osm/common.git#egg=osm-common
-git+https://osm.etsi.org/gerrit/osm/N2VC.git@v8.0.1#egg=n2vc
+git+https://osm.etsi.org/gerrit/osm/N2VC.git#egg=n2vc
diff --git a/tox.ini b/tox.ini
index f0619f7..efea4e6 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -76,6 +76,6 @@ commands = python3 setup.py --command-packages=stdeb.command bdist_deb
 # E123, E125 skipped as they are invalid PEP-8.
 max-line-length = 120
 show-source = True
-ignore = E123,E125,E241
+ignore = E123,E125,E241,W503
 builtins = _
 exclude=.venv,.git,.tox,dist,doc,*lib/python*,*egg,build,devops_stages/*,.rst