Changes way of getting VCA application name corresponding to vdu and vnf
[osm/MON.git] / osm_mon / collector / 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.collectors.base import BaseCollector
29 from osm_mon.collector.metric import Metric
30 from osm_mon.core.common_db import CommonDbClient
31 from osm_mon.core.exceptions import VcaDeploymentInfoNotFound
32 from osm_mon.core.settings import Config
33
34 log = logging.getLogger(__name__)
35
36
37 class VCACollector(BaseCollector):
38 def __init__(self):
39 cfg = Config.instance()
40 self.common_db = CommonDbClient()
41 self.loop = asyncio.get_event_loop()
42 self.n2vc = N2VC(server=cfg.OSMMON_VCA_HOST, user=cfg.OSMMON_VCA_USER, secret=cfg.OSMMON_VCA_SECRET)
43
44 def collect(self, vnfr: dict) -> List[Metric]:
45 nsr_id = vnfr['nsr-id-ref']
46 vnf_member_index = vnfr['member-vnf-index-ref']
47 vnfd = self.common_db.get_vnfd(vnfr['vnfd-id'])
48 metrics = []
49 for vdur in vnfr['vdur']:
50 # This avoids errors when vdur records have not been completely filled
51 if 'name' not in vdur:
52 continue
53 vdu = next(
54 filter(lambda vdu: vdu['id'] == vdur['vdu-id-ref'], vnfd['vdu'])
55 )
56 if 'vdu-configuration' in vdu and 'metrics' in vdu['vdu-configuration']:
57 try:
58 vca_deployment_info = self.get_vca_deployment_info(nsr_id, vnf_member_index, vdur['name'])
59 except VcaDeploymentInfoNotFound:
60 continue
61 measures = self.loop.run_until_complete(self.n2vc.GetMetrics(vca_deployment_info['model'],
62 vca_deployment_info['application']))
63 log.debug('Measures: %s', measures)
64 for measure_list in measures.values():
65 for measure in measure_list:
66 log.debug("Measure: %s", measure)
67 metric = Metric(nsr_id, vnf_member_index, vdur['name'], measure['key'], float(measure['value']))
68 metrics.append(metric)
69 if 'vnf-configuration' in vnfd and 'metrics' in vnfd['vnf-configuration']:
70 try:
71 vca_deployment_info = self.get_vca_deployment_info(nsr_id, vnf_member_index, None)
72 except VcaDeploymentInfoNotFound:
73 return metrics
74 measures = self.loop.run_until_complete(self.n2vc.GetMetrics(vca_deployment_info['model'],
75 vca_deployment_info['application']))
76 log.debug('Measures: %s', measures)
77 for measure_list in measures.values():
78 for measure in measure_list:
79 log.debug("Measure: %s", measure)
80 metric = Metric(nsr_id, vnf_member_index, '', measure['key'], float(measure['value']))
81 metrics.append(metric)
82 return metrics
83
84 def get_vca_deployment_info(self, nsr_id, vnf_member_index, vdur_name):
85 nsr = self.common_db.get_nsr(nsr_id)
86 for vca_deployment in nsr["_admin"]["deployed"]["VCA"]:
87 if vca_deployment:
88 if vca_deployment['member-vnf-index'] == vnf_member_index and vca_deployment['vdu_name'] == vdur_name:
89 return vca_deployment
90 raise VcaDeploymentInfoNotFound("VCA deployment info for nsr_id {}, index {} and vdur_name {} not found."
91 .format(nsr_id, vnf_member_index, vdur_name))