Readds logic to obtain vim_type when it is available in vim config
[osm/MON.git] / osm_mon / collector / service.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
5
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
8
9 # Licensed under the Apache License, Version 2.0 (the "License"); you may
10 # not use this file except in compliance with the License. You may obtain
11 # a copy of the License at
12
13 # http://www.apache.org/licenses/LICENSE-2.0
14
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 # License for the specific language governing permissions and limitations
19 # under the License.
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: bdiaz@whitestack.com or glavado@whitestack.com
22 ##
23 import logging
24 import multiprocessing
25 from typing import List
26
27 from osm_mon.collector.infra_collectors.onos import OnosInfraCollector
28 from osm_mon.collector.infra_collectors.openstack import OpenstackInfraCollector
29 from osm_mon.collector.infra_collectors.vio import VIOInfraCollector
30 from osm_mon.collector.infra_collectors.vmware import VMwareInfraCollector
31 from osm_mon.collector.metric import Metric
32 from osm_mon.collector.vnf_collectors.juju import VCACollector
33 from osm_mon.collector.vnf_collectors.openstack import OpenstackCollector
34 from osm_mon.collector.vnf_collectors.vio import VIOCollector
35 from osm_mon.collector.vnf_collectors.vmware import VMwareCollector
36 from osm_mon.core.common_db import CommonDbClient
37 from osm_mon.core.config import Config
38
39 log = logging.getLogger(__name__)
40
41 VIM_COLLECTORS = {
42 "openstack": OpenstackCollector,
43 "vmware": VMwareCollector,
44 "vio": VIOCollector
45 }
46 VIM_INFRA_COLLECTORS = {
47 "openstack": OpenstackInfraCollector,
48 "vmware": VMwareInfraCollector,
49 "vio": VIOInfraCollector
50 }
51 SDN_INFRA_COLLECTORS = {
52 "onos": OnosInfraCollector
53 }
54
55
56 class CollectorService:
57 def __init__(self, config: Config):
58 self.conf = config
59 self.common_db = CommonDbClient(self.conf)
60 self.queue = multiprocessing.Queue()
61
62 def _collect_vim_metrics(self, vnfr: dict, vim_account_id: str):
63 # TODO(diazb) Add support for aws
64 vim_type = self._get_vim_type(vim_account_id)
65 if vim_type in VIM_COLLECTORS:
66 collector = VIM_COLLECTORS[vim_type](self.conf, vim_account_id)
67 metrics = collector.collect(vnfr)
68 for metric in metrics:
69 self.queue.put(metric)
70 else:
71 log.debug("vimtype %s is not supported.", vim_type)
72
73 def _collect_vim_infra_metrics(self, vim_account_id: str):
74 vim_type = self._get_vim_type(vim_account_id)
75 if vim_type in VIM_INFRA_COLLECTORS:
76 collector = VIM_INFRA_COLLECTORS[vim_type](self.conf, vim_account_id)
77 metrics = collector.collect()
78 for metric in metrics:
79 self.queue.put(metric)
80 else:
81 log.debug("vimtype %s is not supported.", vim_type)
82
83 def _collect_sdnc_infra_metrics(self, sdnc_id: str):
84 common_db = CommonDbClient(self.conf)
85 sdn_type = common_db.get_sdnc(sdnc_id)['type']
86 if sdn_type in SDN_INFRA_COLLECTORS:
87 collector = SDN_INFRA_COLLECTORS[sdn_type](self.conf, sdnc_id)
88 metrics = collector.collect()
89 for metric in metrics:
90 self.queue.put(metric)
91 else:
92 log.debug("sdn_type %s is not supported.", sdn_type)
93
94 def _collect_vca_metrics(self, vnfr: dict):
95 log.debug('_collect_vca_metrics')
96 log.debug('vnfr: %s', vnfr)
97 vca_collector = VCACollector(self.conf)
98 metrics = vca_collector.collect(vnfr)
99 for metric in metrics:
100 self.queue.put(metric)
101
102 def collect_metrics(self) -> List[Metric]:
103 vnfrs = self.common_db.get_vnfrs()
104 processes = []
105 for vnfr in vnfrs:
106 nsr_id = vnfr['nsr-id-ref']
107 vnf_member_index = vnfr['member-vnf-index-ref']
108 vim_account_id = self.common_db.get_vim_account_id(nsr_id, vnf_member_index)
109 p = multiprocessing.Process(target=self._collect_vim_metrics,
110 args=(vnfr, vim_account_id))
111 processes.append(p)
112 p.start()
113 p = multiprocessing.Process(target=self._collect_vca_metrics,
114 args=(vnfr,))
115 processes.append(p)
116 p.start()
117 vims = self.common_db.get_vim_accounts()
118 for vim in vims:
119 p = multiprocessing.Process(target=self._collect_vim_infra_metrics,
120 args=(vim['_id'],))
121 processes.append(p)
122 p.start()
123 sdncs = self.common_db.get_sdncs()
124 for sdnc in sdncs:
125 p = multiprocessing.Process(target=self._collect_sdnc_infra_metrics,
126 args=(sdnc['_id'],))
127 processes.append(p)
128 p.start()
129 for process in processes:
130 process.join(timeout=10)
131 metrics = []
132 while not self.queue.empty():
133 metrics.append(self.queue.get())
134 return metrics
135
136 def _get_vim_type(self, vim_account_id: str) -> str:
137 common_db = CommonDbClient(self.conf)
138 vim_account = common_db.get_vim_account(vim_account_id)
139 vim_type = vim_account['vim_type']
140 if 'config' in vim_account and 'vim_type' in vim_account['config']:
141 vim_type = vim_account['config']['vim_type'].lower()
142 return vim_type