Replaces use of vdu_name for vdu_id and vdu_count_index in VCACollector
[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.config import Config
33 from osm_mon.core.exceptions import VcaDeploymentInfoNotFound
34
35 log = logging.getLogger(__name__)
36
37
38 class VCACollector(BaseCollector):
39 def __init__(self, config: Config):
40 super().__init__(config)
41 self.common_db = CommonDbClient(config)
42 self.loop = asyncio.get_event_loop()
43 self.n2vc = N2VC(server=config.get('vca', 'host'), user=config.get('vca', 'user'),
44 secret=config.get('vca', 'secret'))
45
46 def collect(self, vnfr: dict) -> List[Metric]:
47 nsr_id = vnfr['nsr-id-ref']
48 vnf_member_index = vnfr['member-vnf-index-ref']
49 vnfd = self.common_db.get_vnfd(vnfr['vnfd-id'])
50 metrics = []
51 for vdur in vnfr['vdur']:
52 # This avoids errors when vdur records have not been completely filled
53 if 'name' not in vdur:
54 continue
55 vdu = next(
56 filter(lambda vdu: vdu['id'] == vdur['vdu-id-ref'], vnfd['vdu'])
57 )
58 if 'vdu-configuration' in vdu and 'metrics' in vdu['vdu-configuration']:
59 try:
60 vca_deployment_info = self.get_vca_deployment_info(nsr_id, vnf_member_index, vdur['vdu-id-ref'],
61 vdur['count-index'])
62 except VcaDeploymentInfoNotFound as e:
63 log.warning(repr(e))
64 continue
65 measures = self.loop.run_until_complete(self.n2vc.GetMetrics(vca_deployment_info['model'],
66 vca_deployment_info['application']))
67 log.debug('Measures: %s', measures)
68 for measure_list in measures.values():
69 for measure in measure_list:
70 log.debug("Measure: %s", measure)
71 metric = VnfMetric(nsr_id, vnf_member_index, vdur['name'], measure['key'],
72 float(measure['value']))
73 metrics.append(metric)
74 if 'vnf-configuration' in vnfd and 'metrics' in vnfd['vnf-configuration']:
75 try:
76 vca_deployment_info = self.get_vca_deployment_info(nsr_id, vnf_member_index)
77 except VcaDeploymentInfoNotFound as e:
78 log.warning(repr(e))
79 return metrics
80 measures = self.loop.run_until_complete(self.n2vc.GetMetrics(vca_deployment_info['model'],
81 vca_deployment_info['application']))
82 log.debug('Measures: %s', measures)
83 for measure_list in measures.values():
84 for measure in measure_list:
85 log.debug("Measure: %s", measure)
86 metric = VnfMetric(nsr_id, vnf_member_index, '', measure['key'], float(measure['value']))
87 metrics.append(metric)
88 return metrics
89
90 def get_vca_deployment_info(self, nsr_id, vnf_member_index, vdu_id=None, vdu_count=0):
91 nsr = self.common_db.get_nsr(nsr_id)
92 for vca_deployment in nsr["_admin"]["deployed"]["VCA"]:
93 if vca_deployment:
94 if vdu_id is None:
95 if vca_deployment['member-vnf-index'] == vnf_member_index and vca_deployment['vdu_id'] is None:
96 return vca_deployment
97 else:
98 if vca_deployment['member-vnf-index'] == vnf_member_index and \
99 vca_deployment['vdu_id'] == vdu_id and vca_deployment['vdu_count_index'] == vdu_count:
100 return vca_deployment
101 raise VcaDeploymentInfoNotFound(
102 "VCA deployment info for nsr_id {}, index {}, vdu_id {} and vdu_count_index {} not found.".format(
103 nsr_id,
104 vnf_member_index,
105 vdu_id,
106 vdu_count))