Merge "Dockerfile: apt-get clean to save layer space"
[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 LOG = logging.getLogger(__name__)
21
22
23 class Collector(object):
24     """Class that handles dictionary of results in yardstick-plot format."""
25
26     def __init__(self, vnfs, nodes, traffic_profile, timeout=3600):
27         super(Collector, self).__init__()
28         self.traffic_profile = traffic_profile
29         self.vnfs = vnfs
30         self.nodes = nodes
31         self.timeout = timeout
32         self.bin_path = get_nsb_option('bin_path', '')
33         self.resource_profiles = {node_name: ResourceProfile.make_from_node(node, self.timeout)
34                                   for node_name, node in self.nodes.items()
35                                   if node.get("collectd")}
36
37     def start(self):
38         """Nothing to do, yet"""
39         for resource in self.resource_profiles.values():
40             resource.initiate_systemagent(self.bin_path)
41             resource.start()
42             resource.amqp_process_for_nfvi_kpi()
43
44     def stop(self):
45         """Nothing to do, yet"""
46         for resource in self.resource_profiles.values():
47             resource.stop()
48
49     def get_kpi(self):
50         """Returns dictionary of results in yardstick-plot format
51
52         :return:
53         """
54         results = {}
55         for vnf in self.vnfs:
56             # Result example:
57             # {"VNF1: { "tput" : [1000, 999] }, "VNF2": { "latency": 100 }}
58             LOG.debug("collect KPI for %s", vnf.name)
59             results[vnf.name] = vnf.collect_kpi()
60
61         for node_name, resource in self.resource_profiles.items():
62             # Result example:
63             # {"VNF1: { "tput" : [1000, 999] }, "VNF2": { "latency": 100 }}
64             LOG.debug("collect KPI for %s", node_name)
65             if resource.check_if_sa_running("collectd")[0] != 0:
66                 continue
67
68             try:
69                 results[node_name] = {"core": resource.amqp_collect_nfvi_kpi()}
70                 LOG.debug("%s collect KPIs %s", node_name, results[node_name]['core'])
71             except Exception:
72                 LOG.exception("")
73         return results