Adds collection of sdnc status metric 81/7281/1
authorBenjamin Diaz <bdiaz@whitestack.com>
Mon, 4 Mar 2019 14:03:43 +0000 (11:03 -0300)
committerBenjamin Diaz <bdiaz@whitestack.com>
Tue, 5 Mar 2019 17:31:04 +0000 (14:31 -0300)
Adds plugin for ONOS controller

Change-Id: I0a885a4061a1cf9f99276d3befece078ac991d32
Signed-off-by: Benjamin Diaz <bdiaz@whitestack.com>
osm_mon/collector/collector.py
osm_mon/collector/infra_collectors/base_sdnc.py [new file with mode: 0644]
osm_mon/collector/infra_collectors/onos.py [new file with mode: 0644]
osm_mon/core/common_db.py

index 5074308..f4ffba4 100644 (file)
@@ -27,6 +27,7 @@ import time
 import peewee
 
 from osm_mon.collector.backends.prometheus import PrometheusBackend
+from osm_mon.collector.infra_collectors.onos import OnosInfraCollector
 from osm_mon.collector.infra_collectors.openstack import OpenstackInfraCollector
 from osm_mon.collector.vnf_collectors.juju import VCACollector
 from osm_mon.collector.vnf_collectors.openstack import OpenstackCollector
@@ -46,6 +47,9 @@ VIM_COLLECTORS = {
 VIM_INFRA_COLLECTORS = {
     "openstack": OpenstackInfraCollector
 }
+SDN_INFRA_COLLECTORS = {
+    "onos": OnosInfraCollector
+}
 METRIC_BACKENDS = [
     PrometheusBackend
 ]
@@ -96,6 +100,17 @@ class Collector:
         else:
             log.debug("vimtype %s is not supported.", vim_type)
 
+    def _collect_sdnc_infra_metrics(self, sdnc_id: str):
+        common_db = CommonDbClient(self.conf)
+        sdn_type = common_db.get_sdnc(sdnc_id)['type']
+        if sdn_type in SDN_INFRA_COLLECTORS:
+            collector = SDN_INFRA_COLLECTORS[sdn_type](self.conf, sdnc_id)
+            metrics = collector.collect()
+            for metric in metrics:
+                self.queue.put(metric)
+        else:
+            log.debug("sdn_type %s is not supported.", sdn_type)
+
     def _collect_vca_metrics(self, vnfr: dict):
         log.debug('_collect_vca_metrics')
         log.debug('vnfr: %s', vnfr)
@@ -125,6 +140,12 @@ class Collector:
                                         args=(vim['_id'],))
             processes.append(p)
             p.start()
+        sdncs = self.common_db.get_sdncs()
+        for sdnc in sdncs:
+            p = multiprocessing.Process(target=self._collect_sdnc_infra_metrics,
+                                        args=(sdnc['_id'],))
+            processes.append(p)
+            p.start()
         for process in processes:
             process.join(timeout=10)
         metrics = []
diff --git a/osm_mon/collector/infra_collectors/base_sdnc.py b/osm_mon/collector/infra_collectors/base_sdnc.py
new file mode 100644 (file)
index 0000000..e46f984
--- /dev/null
@@ -0,0 +1,29 @@
+# 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_mon.collector.infra_collectors.base import BaseInfraCollector
+
+from osm_mon.core.config import Config
+
+
+class BaseSdncInfraCollector(BaseInfraCollector):
+    def __init__(self, config: Config, sdn_id: str):
+        pass
diff --git a/osm_mon/collector/infra_collectors/onos.py b/osm_mon/collector/infra_collectors/onos.py
new file mode 100644 (file)
index 0000000..ea2e505
--- /dev/null
@@ -0,0 +1,64 @@
+# 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
+##
+import logging
+from typing import List
+
+import requests
+from requests.auth import HTTPBasicAuth
+
+from osm_mon.collector.infra_collectors.base_sdnc import BaseSdncInfraCollector
+from osm_mon.collector.metric import Metric
+from osm_mon.core.common_db import CommonDbClient
+from osm_mon.core.config import Config
+
+log = logging.getLogger(__name__)
+
+
+class OnosInfraCollector(BaseSdncInfraCollector):
+    def __init__(self, config: Config, sdnc_id: str):
+        super().__init__(config, sdnc_id)
+        self.common_db = CommonDbClient(config)
+        self.sdnc = self.common_db.get_sdnc(sdnc_id)
+
+    def collect(self) -> List[Metric]:
+        metrics = []
+        sdnc_status = self.is_sdnc_ok()
+        sdnc_status_metric = Metric({'sdnc_id': self.sdnc['_id']}, 'sdnc_status', sdnc_status)
+        metrics.append(sdnc_status_metric)
+
+        return metrics
+
+    def is_sdnc_ok(self) -> bool:
+        try:
+            ip = self.sdnc['ip']
+            port = self.sdnc['port']
+            user = self.sdnc['user']
+            password = self.common_db.decrypt_sdnc_password(self.sdnc['password'],
+                                                            self.sdnc['schema_version'],
+                                                            self.sdnc['_id'])
+            # TODO: Add support for https
+            url = 'http://{}:{}/onos/v1/devices'.format(ip, port)
+            requests.get(url, auth=HTTPBasicAuth(user, password))
+            return True
+        except Exception:
+            log.exception("SDNC status is not OK!")
+            return False
index 33eff02..2d31f6c 100644 (file)
@@ -80,9 +80,18 @@ class CommonDbClient:
     def decrypt_vim_password(self, vim_password: str, schema_version: str, vim_id: str):
         return self.common_db.decrypt(vim_password, schema_version, vim_id)
 
+    def decrypt_sdnc_password(self, sdnc_password: str, schema_version: str, sdnc_id: str):
+        return self.common_db.decrypt(sdnc_password, schema_version, sdnc_id)
+
     def get_vim_account_id(self, nsr_id: str, vnf_member_index: int) -> str:
         vnfr = self.get_vnfr(nsr_id, vnf_member_index)
         return vnfr['vim-account-id']
 
     def get_vim_accounts(self):
         return self.common_db.get_list('vim_accounts')
+
+    def get_sdncs(self):
+        return self.common_db.get_list('sdns')
+
+    def get_sdnc(self, sdnc_id: str):
+        return self.common_db.get_one('sdns', {'_id': sdnc_id})