Implements filebased config, config override through env vars, use of osm
[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['name'])
61 except VcaDeploymentInfoNotFound:
62 continue
63 measures = self.loop.run_until_complete(self.n2vc.GetMetrics(vca_deployment_info['model'],
64 vca_deployment_info['application']))
65 log.debug('Measures: %s', measures)
66 for measure_list in measures.values():
67 for measure in measure_list:
68 log.debug("Measure: %s", measure)
69 metric = VnfMetric(nsr_id, vnf_member_index, vdur['name'], measure['key'],
70 float(measure['value']))
71 metrics.append(metric)
72 if 'vnf-configuration' in vnfd and 'metrics' in vnfd['vnf-configuration']:
73 try:
74 vca_deployment_info = self.get_vca_deployment_info(nsr_id, vnf_member_index, None)
75 except VcaDeploymentInfoNotFound:
76 return metrics
77 measures = self.loop.run_until_complete(self.n2vc.GetMetrics(vca_deployment_info['model'],
78 vca_deployment_info['application']))
79 log.debug('Measures: %s', measures)
80 for measure_list in measures.values():
81 for measure in measure_list:
82 log.debug("Measure: %s", measure)
83 metric = VnfMetric(nsr_id, vnf_member_index, '', measure['key'], float(measure['value']))
84 metrics.append(metric)
85 return metrics
86
87 def get_vca_deployment_info(self, nsr_id, vnf_member_index, vdur_name):
88 nsr = self.common_db.get_nsr(nsr_id)
89 for vca_deployment in nsr["_admin"]["deployed"]["VCA"]:
90 if vca_deployment:
91 if vca_deployment['member-vnf-index'] == vnf_member_index and vca_deployment['vdu_name'] == vdur_name:
92 return vca_deployment
93 raise VcaDeploymentInfoNotFound("VCA deployment info for nsr_id {}, index {} and vdur_name {} not found."
94 .format(nsr_id, vnf_member_index, vdur_name))