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