Create Dockerfile to create a yardstick-image of docker
[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 """This module implements stub for publishing results in yardstick format."""
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 contexts_nodes.items():
35             for node in (node for node in nodes if node.get('collectd')):
36                 name = ".".join([node['name'], ctx_name])
37                 self.resource_profiles.update(
38                     {name: ResourceProfile.make_from_node(node, timeout)}
39                     )
40
41     def start(self):
42         for resource in self.resource_profiles.values():
43             resource.initiate_systemagent(self.bin_path)
44             resource.start()
45             resource.amqp_process_for_nfvi_kpi()
46
47         for vnf in self.vnfs:
48             vnf.start_collect()
49
50     def stop(self):
51         for vnf in self.vnfs:
52             vnf.stop_collect()
53
54         for resource in self.resource_profiles.values():
55             resource.stop()
56
57     def get_kpi(self):
58         """Returns dictionary of results in yardstick-plot format
59
60         :return: (dict) dictionary of kpis collected from the VNFs;
61                  the keys are the names of the VNFs.
62         """
63         results = {}
64         for vnf in self.vnfs:
65             # Result example:
66             # {"VNF1: { "tput" : [1000, 999] }, "VNF2": { "latency": 100 }}
67             LOG.debug("collect KPI for vnf %s", vnf.name)
68             results[vnf.name] = vnf.collect_kpi()
69
70         for node_name, resource in self.resource_profiles.items():
71             LOG.debug("collect KPI for nfvi_node %s", node_name)
72             results[node_name] = {"core": resource.amqp_collect_nfvi_kpi()}
73             LOG.debug("%s collect KPIs %s", node_name, results[node_name]['core'])
74
75         return results