Adds collection of vim status metric
[osm/MON.git] / osm_mon / collector / vnf_collectors / juju.py
1 # Copyright 2018 Whitestack, LLC
2 # *************************************************************
3
4 # This file is part of OSM Monitoring module
5 # All Rights Reserved to Whitestack, LLC
6
7 # Licensed under the Apache License, Version 2.0 (the "License"); you may
8 # not use this file except in compliance with the License. You may obtain
9 # a copy of the License at
10
11 # http://www.apache.org/licenses/LICENSE-2.0
12
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 # License for the specific language governing permissions and limitations
17 # under the License.
18
19 # For those usages not covered by the Apache License, Version 2.0 please
20 # contact: bdiaz@whitestack.com or glavado@whitestack.com
21 ##
22 import asyncio
23 import logging
24 from typing import List
25
26 from n2vc.vnf import N2VC
27
28 from osm_mon.collector.metric import Metric
29 from osm_mon.collector.vnf_collectors.base import BaseCollector
30 from osm_mon.collector.vnf_metric import VnfMetric
31 from osm_mon.core.common_db import CommonDbClient
32 from osm_mon.core.exceptions import VcaDeploymentInfoNotFound
33 from osm_mon.core.settings import Config
34
35 log = logging.getLogger(__name__)
36
37
38 class VCACollector(BaseCollector):
39 def __init__(self):
40 cfg = Config.instance()
41 self.common_db = CommonDbClient()
42 self.loop = asyncio.get_event_loop()
43 self.n2vc = N2VC(server=cfg.OSMMON_VCA_HOST, user=cfg.OSMMON_VCA_USER, secret=cfg.OSMMON_VCA_SECRET)
44
45 def collect(self, vnfr: dict) -> List[Metric]:
46 nsr_id = vnfr['nsr-id-ref']
47 vnf_member_index = vnfr['member-vnf-index-ref']
48 vnfd = self.common_db.get_vnfd(vnfr['vnfd-id'])
49 metrics = []
50 for vdur in vnfr['vdur']:
51 # This avoids errors when vdur records have not been completely filled
52 if 'name' not in vdur:
53 continue
54 vdu = next(
55 filter(lambda vdu: vdu['id'] == vdur['vdu-id-ref'], vnfd['vdu'])
56 )
57 if 'vdu-configuration' in vdu and 'metrics' in vdu['vdu-configuration']:
58 try:
59 vca_deployment_info = self.get_vca_deployment_info(nsr_id, vnf_member_index, vdur['name'])
60 except VcaDeploymentInfoNotFound:
61 continue
62 measures = self.loop.run_until_complete(self.n2vc.GetMetrics(vca_deployment_info['model'],
63 vca_deployment_info['application']))
64 log.debug('Measures: %s', measures)
65 for measure_list in measures.values():
66 for measure in measure_list:
67 log.debug("Measure: %s", measure)
68 metric = VnfMetric(nsr_id, vnf_member_index, vdur['name'], measure['key'],
69 float(measure['value']))
70 metrics.append(metric)
71 if 'vnf-configuration' in vnfd and 'metrics' in vnfd['vnf-configuration']:
72 try:
73 vca_deployment_info = self.get_vca_deployment_info(nsr_id, vnf_member_index, None)
74 except VcaDeploymentInfoNotFound:
75 return metrics
76 measures = self.loop.run_until_complete(self.n2vc.GetMetrics(vca_deployment_info['model'],
77 vca_deployment_info['application']))
78 log.debug('Measures: %s', measures)
79 for measure_list in measures.values():
80 for measure in measure_list:
81 log.debug("Measure: %s", measure)
82 metric = VnfMetric(nsr_id, vnf_member_index, '', measure['key'], float(measure['value']))
83 metrics.append(metric)
84 return metrics
85
86 def get_vca_deployment_info(self, nsr_id, vnf_member_index, vdur_name):
87 nsr = self.common_db.get_nsr(nsr_id)
88 for vca_deployment in nsr["_admin"]["deployed"]["VCA"]:
89 if vca_deployment:
90 if vca_deployment['member-vnf-index'] == vnf_member_index and vca_deployment['vdu_name'] == vdur_name:
91 return vca_deployment
92 raise VcaDeploymentInfoNotFound("VCA deployment info for nsr_id {}, index {} and vdur_name {} not found."
93 .format(nsr_id, vnf_member_index, vdur_name))