Merge "update yardstick ha test cases dashboard"
[yardstick.git] / yardstick / network_services / vnf_generic / vnf / tg_rfc2544_ixia.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 from __future__ import absolute_import
16 import time
17 import os
18 import logging
19 import sys
20
21 from yardstick.common.utils import ErrorClass
22 from yardstick.network_services.vnf_generic.vnf.sample_vnf import SampleVNFTrafficGen
23 from yardstick.network_services.vnf_generic.vnf.sample_vnf import ClientResourceHelper
24 from yardstick.network_services.vnf_generic.vnf.sample_vnf import Rfc2544ResourceHelper
25 from yardstick.benchmark.scenarios.networking.vnf_generic import find_relative_file
26
27 LOG = logging.getLogger(__name__)
28
29 WAIT_AFTER_CFG_LOAD = 10
30 WAIT_FOR_TRAFFIC = 30
31 IXIA_LIB = os.path.dirname(os.path.realpath(__file__))
32 IXNET_LIB = os.path.join(IXIA_LIB, "../../libs/ixia_libs/IxNet")
33 sys.path.append(IXNET_LIB)
34
35 try:
36     from IxNet import IxNextgen
37 except ImportError:
38     IxNextgen = ErrorClass
39
40
41 class IxiaRfc2544Helper(Rfc2544ResourceHelper):
42
43     pass
44
45
46 class IxiaResourceHelper(ClientResourceHelper):
47
48     def __init__(self, setup_helper, rfc_helper_type=None):
49         super(IxiaResourceHelper, self).__init__(setup_helper)
50         self.scenario_helper = setup_helper.scenario_helper
51
52         self.client = IxNextgen()
53
54         if rfc_helper_type is None:
55             rfc_helper_type = IxiaRfc2544Helper
56
57         self.rfc_helper = rfc_helper_type(self.scenario_helper)
58         self.tg_port_pairs = []
59         self.priv_ports = None
60         self.pub_ports = None
61
62     def _connect(self, client=None):
63         self.client._connect(self.vnfd_helper)
64
65     def _build_ports(self):
66         # self.generate_port_pairs(self.topology)
67         self.priv_ports = [int(x[0][2:]) for x in self.tg_port_pairs]
68         self.pub_ports = [int(x[1][2:]) for x in self.tg_port_pairs]
69         self.my_ports = list(set(self.priv_ports).union(set(self.pub_ports)))
70
71     def get_stats(self, *args, **kwargs):
72         return self.client.ix_get_statistics()[1]
73
74     def stop_collect(self):
75         self._terminated.value = 0
76         if self.client and self.client.ixnet:
77             self.client.ix_stop_traffic()
78
79     def generate_samples(self, key=None, default=None):
80         last_result = self.get_stats()
81
82         samples = {}
83         for vpci_idx, interface in enumerate(self.vnfd_helper.interfaces):
84             name = "xe{0}".format(vpci_idx)
85             samples[name] = {
86                 "rx_throughput_kps": float(last_result["Rx_Rate_Kbps"][vpci_idx]),
87                 "tx_throughput_kps": float(last_result["Tx_Rate_Kbps"][vpci_idx]),
88                 "rx_throughput_mbps": float(last_result["Rx_Rate_Mbps"][vpci_idx]),
89                 "tx_throughput_mbps": float(last_result["Tx_Rate_Mbps"][vpci_idx]),
90                 "in_packets": int(last_result["Valid_Frames_Rx"][vpci_idx]),
91                 "out_packets": int(last_result["Frames_Tx"][vpci_idx]),
92                 "RxThroughput": int(last_result["Valid_Frames_Rx"][vpci_idx]) / 30,
93                 "TxThroughput": int(last_result["Frames_Tx"][vpci_idx]) / 30,
94             }
95
96         return samples
97
98     def run_traffic(self, traffic_profile):
99         min_tol = self.rfc_helper.tolerance_low
100         max_tol = self.rfc_helper.tolerance_high
101
102         self._build_ports()
103         self._connect()
104
105         # we don't know client_file_name until runtime as instantiate
106         client_file_name = \
107             find_relative_file(self.scenario_helper.scenario_cfg['ixia_profile'],
108                                self.scenario_helper.scenario_cfg["task_path"])
109         self.client.ix_load_config(client_file_name)
110         time.sleep(WAIT_AFTER_CFG_LOAD)
111
112         self.client.ix_assign_ports()
113
114         mac = {}
115         for index, interface in enumerate(self.vnfd_helper.interfaces, 1):
116             virt_intf = interface["virtual-interface"]
117             mac.update({
118                 "src_mac_{}".format(index): virt_intf["local_mac"],
119                 "dst_mac_{}".format(index): virt_intf["dst_mac"],
120             })
121
122         samples = {}
123
124         ixia_file = find_relative_file("ixia_traffic.cfg",
125                                        self.scenario_helper.scenario_cfg["task_path"])
126         # Generate ixia traffic config...
127         while not self._terminated.value:
128             traffic_profile.execute(self, self.client, mac, ixia_file)
129             self.client_started.value = 1
130             time.sleep(WAIT_FOR_TRAFFIC)
131             self.client.ix_stop_traffic()
132             samples = self.generate_samples()
133             self._queue.put(samples)
134             status, samples = traffic_profile.get_drop_percentage(self, samples, min_tol,
135                                                                   max_tol, self.client, mac,
136                                                                   ixia_file)
137
138             current = samples['CurrentDropPercentage']
139             if min_tol <= current <= max_tol or status == 'Completed':
140                 self._terminated.value = 1
141
142         self.client.ix_stop_traffic()
143         self._queue.put(samples)
144
145
146 class IxiaTrafficGen(SampleVNFTrafficGen):
147
148     APP_NAME = 'Ixia'
149
150     def __init__(self, name, vnfd, setup_env_helper_type=None, resource_helper_type=None):
151         if resource_helper_type is None:
152             resource_helper_type = IxiaResourceHelper
153
154         super(IxiaTrafficGen, self).__init__(name, vnfd, setup_env_helper_type,
155                                              resource_helper_type)
156         self._ixia_traffic_gen = None
157         self.ixia_file_name = ''
158         self.tg_port_pairs = []
159         self.vnf_port_pairs = []
160
161     def _check_status(self):
162         pass
163
164     def scale(self, flavor=""):
165         pass
166
167     def listen_traffic(self, traffic_profile):
168         pass
169
170     def terminate(self):
171         self.resource_helper.stop_collect()
172         super(IxiaTrafficGen, self).terminate()
173
174     def wait_for_instantiate(self):
175         # not needed for IxNet
176         pass