add yardstick iruya 9.0.0 release notes
[yardstick.git] / yardstick / network_services / collector / subscriber.py
1 # Copyright (c) 2016-2017 Intel Corporation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import logging
16
17 from yardstick.network_services.nfvi.resource import ResourceProfile
18 from yardstick.network_services.utils import get_nsb_option
19
20
21 LOG = logging.getLogger(__name__)
22
23
24 class Collector(object):
25     """Class that handles dictionary of results in yardstick-plot format."""
26
27     def __init__(self, vnfs, contexts_nodes, timeout=3600):
28         super(Collector, self).__init__()
29         self.vnfs = vnfs
30         self.nodes = contexts_nodes
31         self.bin_path = get_nsb_option('bin_path', '')
32         self.resource_profiles = {}
33
34         for ctx_name, nodes in ((ctx_name, nodes) for (ctx_name, nodes)
35                                 in contexts_nodes.items() if nodes):
36             for node in (node for node in nodes
37                          if node and node.get('collectd')):
38                 name = ".".join([node['name'], ctx_name])
39                 self.resource_profiles.update(
40                     {name: ResourceProfile.make_from_node(node, timeout)})
41
42     def start(self):
43         for resource in self.resource_profiles.values():
44             resource.initiate_systemagent(self.bin_path)
45             resource.start()
46             resource.amqp_process_for_nfvi_kpi()
47
48         for vnf in self.vnfs:
49             vnf.start_collect()
50
51     def stop(self):
52         for vnf in self.vnfs:
53             vnf.stop_collect()
54
55         for resource in self.resource_profiles.values():
56             resource.stop()
57
58     def get_kpi(self):
59         """Returns dictionary of results in yardstick-plot format
60
61         :return: (dict) dictionary of kpis collected from the VNFs;
62                  the keys are the names of the VNFs.
63         """
64         results = {}
65         for vnf in self.vnfs:
66             # Result example:
67             # {"VNF1: { "tput" : [1000, 999] }, "VNF2": { "latency": 100 }}
68             LOG.debug("collect KPI for vnf %s", vnf.name)
69             results[vnf.name] = vnf.collect_kpi()
70
71         for node_name, resource in self.resource_profiles.items():
72             LOG.debug("collect KPI for nfvi_node %s", node_name)
73             results[node_name] = {"core": resource.amqp_collect_nfvi_kpi()}
74             LOG.debug("%s collect KPIs %s", node_name, results[node_name]['core'])
75
76         return results