import json
import logging
import random
-import re
import uuid
-from string import ascii_lowercase
from n2vc.vnf import N2VC
from prometheus_client.core import GaugeMetricFamily
self.kafka_server = cfg.BROKER_URI
self.common_db_client = CommonDbClient()
self.n2vc = N2VC(server=cfg.OSMMON_VCA_HOST, user=cfg.OSMMON_VCA_USER, secret=cfg.OSMMON_VCA_SECRET)
+ self.producer_timeout = 5
async def collect_metrics(self):
"""
vca_model_name = 'default'
for vnfr in vnfrs:
nsr_id = vnfr['nsr-id-ref']
+ nsr = self.common_db_client.get_nsr(nsr_id)
vnfd = self.common_db_client.get_vnfd(vnfr['vnfd-id'])
for vdur in vnfr['vdur']:
# This avoids errors when vdur records have not been completely filled
if 'monitoring-param' in vdu:
for param in vdu['monitoring-param']:
metric_name = param['nfvi-metric']
- payload = await self._generate_read_metric_payload(metric_name, nsr_id, vdu_name,
- vnf_member_index)
+ payload = self._generate_read_metric_payload(metric_name,
+ nsr_id,
+ vdu_name,
+ vnf_member_index)
producer.send(topic='metric_request', key='read_metric_data_request',
value=json.dumps(payload))
- producer.flush(5)
+ producer.flush(self.producer_timeout)
for message in consumer:
if message.key == 'read_metric_data_response':
content = json.loads(message.value)
metric_reading)
break
if 'vdu-configuration' in vdu and 'metrics' in vdu['vdu-configuration']:
- vnf_name_vca = await self._generate_vca_vdu_name(vdu_name)
+ vnf_name_vca = self.n2vc.FormatApplicationName(nsr['name'], vnf_member_index, vdur['vdu-id-ref'])
vnf_metrics = await self.n2vc.GetMetrics(vca_model_name, vnf_name_vca)
log.debug('VNF Metrics: %s', vnf_metrics)
for vnf_metric_list in vnf_metrics.values():
metrics[vnf_metric['key']].add_metric([nsr_id, vnf_member_index, vdu_name],
float(vnf_metric['value']))
consumer.close()
- producer.close(5)
+ producer.close(self.producer_timeout)
log.debug("metric.values = %s", metrics.values())
return metrics.values()
@staticmethod
- async def _generate_vca_vdu_name(vdu_name) -> str:
- """
- Replaces all digits in vdu name for corresponding ascii characters. This is the format required by N2VC.
- :param vdu_name: Vdu name according to the vdur
- :return: Name with digits replaced with characters
- """
- vnf_name_vca = ''.join(
- ascii_lowercase[int(char)] if char.isdigit() else char for char in vdu_name)
- vnf_name_vca = re.sub(r'-[a-z]+$', '', vnf_name_vca)
- return vnf_name_vca
-
- @staticmethod
- async def _generate_read_metric_payload(metric_name, nsr_id, vdu_name, vnf_member_index) -> dict:
+ def _generate_read_metric_payload(metric_name, nsr_id, vdu_name, vnf_member_index) -> dict:
"""
Builds JSON payload for asking for a metric measurement in MON. It follows the model defined in core.models.
:param metric_name: OSM metric name (e.g.: cpu_utilization)
import asyncio
import random
import unittest
-
-from mock import mock
+from unittest import mock
from osm_mon.collector.collector import MonCollector
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(None)
- def test_generate_vca_vdu_name(self):
- vdur_name = 'test-juju-metrics01-1-ubuntuvdu1-1'
- expected = 'test-juju-metricsab-b-ubuntuvdub'
- result = self.loop.run_until_complete(MonCollector._generate_vca_vdu_name(vdur_name))
- self.assertEqual(result, expected)
-
@mock.patch.object(random, 'randint')
def test_generate_read_metric_payload(self, randint):
randint.return_value = 1
'collection_period': 1,
'collection_unit': 'DAY',
}
- result = self.loop.run_until_complete(
- MonCollector._generate_read_metric_payload(metric_name, nsr_id, vdu_name, vnf_member_index))
+ result = MonCollector._generate_read_metric_payload(metric_name, nsr_id, vdu_name, vnf_member_index)
self.assertEqual(result, expected_payload)