Merge "Cleanup ScenarioGeneral unit tests"
[yardstick.git] / yardstick / network_services / traffic_profile / ixia_rfc2544.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 logging
16
17 from yardstick.network_services.traffic_profile.trex_traffic_profile import \
18     TrexProfile
19
20 LOG = logging.getLogger(__name__)
21
22
23 class IXIARFC2544Profile(TrexProfile):
24
25     UPLINK = 'uplink'
26     DOWNLINK = 'downlink'
27
28     def __init__(self, yaml_data):
29         super(IXIARFC2544Profile, self).__init__(yaml_data)
30         self.rate = self.config.frame_rate
31
32     def _get_ixia_traffic_profile(self, profile_data, mac=None):
33         if mac is None:
34             mac = {}
35
36         result = {}
37         for traffickey, values in profile_data.items():
38             if not traffickey.startswith((self.UPLINK, self.DOWNLINK)):
39                 continue
40
41             try:
42                 # values should be single-item dict, so just grab the first item
43                 try:
44                     key, value = next(iter(values.items()))
45                 except StopIteration:
46                     result[traffickey] = {}
47                     continue
48
49                 port_id = value.get('id', 1)
50                 port_index = port_id - 1
51                 try:
52                     ip = value['outer_l3v6']
53                 except KeyError:
54                     ip = value['outer_l3v4']
55                     src_key, dst_key = 'srcip4', 'dstip4'
56                 else:
57                     src_key, dst_key = 'srcip6', 'dstip6'
58
59                 result[traffickey] = {
60                     'bidir': False,
61                     'iload': '100',
62                     'id': port_id,
63                     'outer_l2': {
64                         'framesize': value['outer_l2']['framesize'],
65                         'framesPerSecond': True,
66                         'srcmac': mac['src_mac_{}'.format(port_index)],
67                         'dstmac': mac['dst_mac_{}'.format(port_index)],
68                     },
69                     'outer_l3': {
70                         'count': ip['count'],
71                         'dscp': ip['dscp'],
72                         'ttl': ip['ttl'],
73                         src_key: ip[src_key].split("-")[0],
74                         dst_key: ip[dst_key].split("-")[0],
75                         'type': key,
76                         'proto': ip['proto'],
77                     },
78                     'outer_l4': value['outer_l4'],
79                 }
80             except KeyError:
81                 continue
82
83         return result
84
85     def _ixia_traffic_generate(self, traffic, ixia_obj):
86         for key, value in traffic.items():
87             if key.startswith((self.UPLINK, self.DOWNLINK)):
88                 value['iload'] = str(self.rate)
89         ixia_obj.update_frame(traffic)
90         ixia_obj.update_ip_packet(traffic)
91         ixia_obj.start_traffic()
92
93     def update_traffic_profile(self, traffic_generator):
94         def port_generator():
95             for vld_id, intfs in sorted(traffic_generator.networks.items()):
96                 if not vld_id.startswith((self.UPLINK, self.DOWNLINK)):
97                     continue
98                 profile_data = self.params.get(vld_id)
99                 if not profile_data:
100                     continue
101                 self.profile_data = profile_data
102                 self.full_profile.update({vld_id: self.profile_data})
103                 for intf in intfs:
104                     yield traffic_generator.vnfd_helper.port_num(intf)
105
106         self.ports = [port for port in port_generator()]
107
108     def execute_traffic(self, traffic_generator, ixia_obj=None, mac=None):
109         mac = {} if mac is None else mac
110         first_run = self.first_run
111         if self.first_run:
112             self.first_run = False
113             self.full_profile = {}
114             self.pg_id = 0
115             self.update_traffic_profile(traffic_generator)
116             self.max_rate = self.rate
117             self.min_rate = 0
118         else:
119             self.rate = round(float(self.max_rate + self.min_rate) / 2.0, 2)
120
121         traffic = self._get_ixia_traffic_profile(self.full_profile, mac)
122         self._ixia_traffic_generate(traffic, ixia_obj)
123         return first_run
124
125     def get_drop_percentage(self, samples, tol_min, tolerance, duration=30.0,
126                             first_run=False):
127         completed = False
128         drop_percent = 100
129         num_ifaces = len(samples)
130         in_packets_sum = sum(
131             [samples[iface]['in_packets'] for iface in samples])
132         out_packets_sum = sum(
133             [samples[iface]['out_packets'] for iface in samples])
134         rx_throughput = sum(
135             [samples[iface]['RxThroughput'] for iface in samples])
136         rx_throughput = round(float(rx_throughput), 2)
137         tx_throughput = sum(
138             [samples[iface]['TxThroughput'] for iface in samples])
139         tx_throughput = round(float(tx_throughput), 2)
140         packet_drop = abs(out_packets_sum - in_packets_sum)
141
142         try:
143             drop_percent = round(
144                 (packet_drop / float(out_packets_sum)) * 100, 2)
145         except ZeroDivisionError:
146             LOG.info('No traffic is flowing')
147
148         samples['TxThroughput'] = tx_throughput
149         samples['RxThroughput'] = rx_throughput
150         samples['DropPercentage'] = drop_percent
151
152         if first_run:
153             self.rate = out_packets_sum / duration / num_ifaces
154             completed = True if drop_percent <= tolerance else False
155
156         if drop_percent > tolerance:
157             self.max_rate = self.rate
158         elif drop_percent < tol_min:
159             self.min_rate = self.rate
160         else:
161             completed = True
162
163         return completed, samples