Create a SampleVNF MQ consumer class
[yardstick.git] / yardstick / network_services / vnf_generic / vnf / tg_ixload.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 collections
16 import csv
17 import glob
18 import logging
19 import os
20 import shutil
21 import subprocess
22
23 from yardstick.common import utils
24 from yardstick.network_services.vnf_generic.vnf.sample_vnf import SampleVNFTrafficGen
25 from yardstick.network_services.vnf_generic.vnf.sample_vnf import ClientResourceHelper
26
27
28 LOG = logging.getLogger(__name__)
29
30 VNF_PATH = os.path.dirname(os.path.realpath(__file__))
31
32 MOUNT_CMD = """\
33 mount.cifs //{0[ip]}/Results {1.RESULTS_MOUNT} \
34 -o username={0[user]},password={0[password]}\
35 """
36
37 IXLOAD_CONFIG_TEMPLATE = '''\
38 {
39     "ixia_chassis": "%s",
40     "IXIA": {
41         "ports": %s,
42         "card": %s
43     },
44     "remote_server": "%s",
45     "result_dir": "%s",
46     "ixload_cfg": "C:/Results/%s"
47 }'''
48
49 IXLOAD_CMD = "{ixloadpy} {http_ixload} {args}"
50
51
52 class ResourceDataHelper(list):
53
54     def get_aggregates(self):
55         return {
56             "min": min(self),
57             "max": max(self),
58             "avg": sum(self) / len(self),
59         }
60
61
62 class IxLoadResourceHelper(ClientResourceHelper):
63
64     RESULTS_MOUNT = "/mnt/Results"
65
66     KPI_LIST = collections.OrderedDict((
67         ('http_throughput', 'HTTP Total Throughput (Kbps)'),
68         ('simulated_users', 'HTTP Simulated Users'),
69         ('concurrent_connections', 'HTTP Concurrent Connections'),
70         ('connection_rate', 'HTTP Connection Rate'),
71         ('transaction_rate', 'HTTP Transaction Rate'),
72     ))
73
74     def __init__(self, setup_helper):
75         super(IxLoadResourceHelper, self).__init__(setup_helper)
76         self.result = collections.OrderedDict((key, ResourceDataHelper())
77                                               for key in self.KPI_LIST)
78         self.resource_file_name = ''
79         self.data = None
80
81     def parse_csv_read(self, reader):
82         for row in reader:
83             try:
84                 new_data = {key_left: int(row[key_right])
85                             for key_left, key_right in self.KPI_LIST.items()}
86             except (TypeError, ValueError):
87                 continue
88             else:
89                 for key, value in new_data.items():
90                     self.result[key].append(value)
91
92     def setup(self):
93         # NOTE: fixup scenario_helper to hanlde ixia
94         self.resource_file_name = \
95             utils.find_relative_file(
96                 self.scenario_helper.scenario_cfg['ixia_profile'],
97                 self.scenario_helper.scenario_cfg["task_path"])
98         utils.makedirs(self.RESULTS_MOUNT)
99         cmd = MOUNT_CMD.format(self.vnfd_helper.mgmt_interface, self)
100         LOG.debug(cmd)
101
102         if not os.path.ismount(self.RESULTS_MOUNT):
103             subprocess.call(cmd, shell=True)
104
105         shutil.rmtree(self.RESULTS_MOUNT, ignore_errors=True)
106         utils.makedirs(self.RESULTS_MOUNT)
107         shutil.copy(self.resource_file_name, self.RESULTS_MOUNT)
108
109     def make_aggregates(self):
110         return {key_right: self.result[key_left].get_aggregates()
111                 for key_left, key_right in self.KPI_LIST.items()}
112
113     def collect_kpi(self):
114         if self.data:
115             self._result.update(self.data)
116         LOG.info("Collect %s KPIs %s", self.RESOURCE_WORD, self._result)
117         return self._result
118
119     def log(self):
120         for key in self.KPI_LIST:
121             LOG.debug(self.result[key])
122
123
124 class IxLoadTrafficGen(SampleVNFTrafficGen):
125
126     def __init__(self, name, vnfd, task_id, setup_env_helper_type=None,
127                  resource_helper_type=None):
128         if resource_helper_type is None:
129             resource_helper_type = IxLoadResourceHelper
130
131         super(IxLoadTrafficGen, self).__init__(
132             name, vnfd, task_id, setup_env_helper_type, resource_helper_type)
133         self._result = {}
134
135     def run_traffic(self, traffic_profile):
136         ports = []
137         card = None
138         for interface in self.vnfd_helper.interfaces:
139             vpci_list = interface['virtual-interface']["vpci"].split(":")
140             card = vpci_list[0]
141             ports.append(str(vpci_list[1]))
142
143         for csv_file in glob.iglob(self.ssh_helper.join_bin_path('*.csv')):
144             os.unlink(csv_file)
145
146         ixia_config = self.vnfd_helper.mgmt_interface["tg-config"]
147         ixload_config = IXLOAD_CONFIG_TEMPLATE % (
148             ixia_config["ixchassis"], ports, card,
149             self.vnfd_helper.mgmt_interface["ip"], self.ssh_helper.bin_path,
150             os.path.basename(self.resource_helper.resource_file_name))
151
152         http_ixload_path = os.path.join(VNF_PATH, "../../traffic_profile")
153
154         cmd = IXLOAD_CMD.format(
155             ixloadpy=os.path.join(ixia_config["py_bin_path"], "ixloadpython"),
156             http_ixload=os.path.join(http_ixload_path, "http_ixload.py"),
157             args="'%s'" % ixload_config)
158
159         LOG.debug(cmd)
160         subprocess.call(cmd, shell=True)
161
162         with open(self.ssh_helper.join_bin_path("ixLoad_HTTP_Client.csv")) as csv_file:
163             lines = csv_file.readlines()[10:]
164         with open(self.ssh_helper.join_bin_path("http_result.csv"), 'wb+') as result_file:
165             result_file.writelines(lines[:-1])
166             result_file.flush()
167             result_file.seek(0)
168             reader = csv.DictReader(result_file)
169             self.resource_helper.parse_csv_read(reader)
170
171         self.resource_helper.log()
172         self.resource_helper.data = self.resource_helper.make_aggregates()
173
174     def terminate(self):
175         subprocess.call(["pkill", "-9", "http_ixload.py"])
176         super(IxLoadTrafficGen, self).terminate()