84cebe0b6cadbe3aadb93c6f00d150f89cfdaae0
[osm/MON.git] / osm_mon / collector / vnf_collectors / vio.py
1 # -*- coding: utf-8 -*-
2
3 ##
4 # Copyright 2016-2017 VMware Inc.
5 # This file is part of ETSI OSM
6 # All Rights Reserved.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
18 # under the License.
19 #
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: osslegalrouting@vmware.com
22 ##
23
24 import json
25 import logging
26
27 from osm_mon.collector.utils.collector import CollectorUtils
28 from osm_mon.collector.vnf_collectors.base_vim import BaseVimCollector
29 from osm_mon.core.common_db import CommonDbClient
30 from osm_mon.core.config import Config
31 from osm_mon.collector.vnf_collectors.vrops.vrops_helper import vROPS_Helper
32
33 log = logging.getLogger(__name__)
34
35
36 class VIOCollector(BaseVimCollector):
37 def __init__(self, config: Config, vim_account_id: str):
38 super().__init__(config, vim_account_id)
39 self.common_db = CommonDbClient(config)
40 cfg = self.get_vim_account(vim_account_id)
41 self.vrops = vROPS_Helper(vrops_site=cfg['vrops_site'],
42 vrops_user=cfg['vrops_user'],
43 vrops_password=cfg['vrops_password'])
44
45 def get_vim_account(self, vim_account_id: str):
46 vim_account_info = CollectorUtils.get_credentials(vim_account_id)
47 return json.loads(vim_account_info.config)
48
49 def collect(self, vnfr: dict):
50 vnfd = self.common_db.get_vnfd(vnfr['vnfd-id'])
51 vdu_mappings = {}
52
53 # Fetch the list of all known resources from vROPS.
54 resource_list = self.vrops.get_vm_resource_list_from_vrops()
55
56 for vdur in vnfr['vdur']:
57 # This avoids errors when vdur records have not been completely filled
58 if 'name' not in vdur:
59 continue
60
61 vdu = next(
62 filter(lambda vdu: vdu['id'] == vdur['vdu-id-ref'], vnfd['vdu'])
63 )
64 if 'monitoring-param' not in vdu:
65 continue
66
67 vim_id = vdur['vim-id']
68 vdu_mappings[vim_id] = {'name': vdur['name']}
69
70 # Map the vROPS instance id to the vim-id so we can look it up.
71 for resource in resource_list:
72 for resourceIdentifier in resource['resourceKey']['resourceIdentifiers']:
73 if resourceIdentifier['identifierType']['name'] == 'VMEntityInstanceUUID':
74 if resourceIdentifier['value'] != vim_id:
75 continue
76 vdu_mappings[vim_id]['vrops_id'] = resource['identifier']
77
78 if len(vdu_mappings) != 0:
79 return self.vrops.get_metrics(vdu_mappings=vdu_mappings,
80 monitoring_params=vdu['monitoring-param'],
81 vnfr=vnfr)
82 else:
83 return []