Merge "Adding 2 node ixia generic scale-out test case generation"
[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     @staticmethod
27     def make_resource_profile(node, timeout):
28         # node dict works as mgmt dict
29         # don't need port names, there is no way we can
30         # tell what port is used on the compute node
31         collectd_options = node["collectd"]
32         plugins = collectd_options.get("plugins", {})
33         interval = collectd_options.get("interval")
34
35         # use default cores = None to MatchAllCores
36         return ResourceProfile(node, plugins=plugins, interval=interval, timeout=timeout)
37
38     def __init__(self, vnfs, nodes, traffic_profile, timeout=3600):
39         super(Collector, self).__init__()
40         self.traffic_profile = traffic_profile
41         self.vnfs = vnfs
42         self.nodes = nodes
43         self.timeout = timeout
44         self.bin_path = get_nsb_option('bin_path', '')
45         self.resource_profiles = {node_name: self.make_resource_profile(node, self.timeout)
46                                   for node_name, node in self.nodes.items()
47                                   if node.get("collectd")}
48
49     def start(self):
50         """Nothing to do, yet"""
51         for resource in self.resource_profiles.values():
52             resource.initiate_systemagent(self.bin_path)
53             resource.start()
54             resource.amqp_process_for_nfvi_kpi()
55
56     def stop(self):
57         """Nothing to do, yet"""
58         for resource in self.resource_profiles.values():
59             resource.stop()
60
61     def get_kpi(self):
62         """Returns dictionary of results in yardstick-plot format
63
64         :return:
65         """
66         results = {}
67         for vnf in self.vnfs:
68             # Result example:
69             # {"VNF1: { "tput" : [1000, 999] }, "VNF2": { "latency": 100 }}
70             LOG.debug("collect KPI for %s", vnf.name)
71             results[vnf.name] = vnf.collect_kpi()
72
73         for node_name, resource in self.resource_profiles.items():
74             # Result example:
75             # {"VNF1: { "tput" : [1000, 999] }, "VNF2": { "latency": 100 }}
76             LOG.debug("collect KPI for %s", node_name)
77             if resource.check_if_sa_running("collectd")[0] != 0:
78                 continue
79
80             try:
81                 results[node_name] = {"core": resource.amqp_collect_nfvi_kpi()}
82                 LOG.debug("%s collect KPIs %s", node_name, results[node_name]['core'])
83             except Exception:
84                 LOG.exception("")
85         return results