Merge "Make injection time configurable for IXIA RFC2544 test cases"
[yardstick.git] / yardstick / network_services / libs / ixia_libs / ixnet / ixnet_api.py
index 393f60f..ba27d4d 100644 (file)
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import ipaddress
 import logging
 
 import IxNetwork
 
 from yardstick.common import exceptions
 from yardstick.common import utils
+from yardstick.network_services.traffic_profile import base as tp_base
 
 
 log = logging.getLogger(__name__)
@@ -32,8 +34,8 @@ PROTO_UDP = 'udp'
 PROTO_TCP = 'tcp'
 PROTO_VLAN = 'vlan'
 
-IP_VERSION_4_MASK = '0.0.0.255'
-IP_VERSION_6_MASK = '0:0:0:0:0:0:0:ff'
+IP_VERSION_4_MASK = 24
+IP_VERSION_6_MASK = 64
 
 TRAFFIC_STATUS_STARTED = 'started'
 TRAFFIC_STATUS_STOPPED = 'stopped'
@@ -166,9 +168,10 @@ class IxNextgen(object):  # pragma: no cover
         :return: list of paired frame sizes and weights
         """
         weighted_range_pairs = []
-        for size, weight in framesize.items():
-            weighted_range_pairs.append(int(size.upper().replace('B', '')))
-            weighted_range_pairs.append(int(weight))
+        for size, weight in ((s, w) for (s, w) in framesize.items()
+                             if int(w) != 0):
+            size = int(size.upper().replace('B', ''))
+            weighted_range_pairs.append([size, size, int(weight)])
         return weighted_range_pairs
 
     def iter_over_get_lists(self, x1, x2, y2, offset=0):
@@ -327,7 +330,7 @@ class IxNextgen(object):  # pragma: no cover
                                      '-valueType', 'singleValue')
         self.ixnet.commit()
 
-    def update_frame(self, traffic):
+    def update_frame(self, traffic, duration):
         """Update the L2 frame
 
         This function updates the L2 frame options:
@@ -335,16 +338,17 @@ class IxNextgen(object):  # pragma: no cover
         - Duration: in case of traffic_type="fixedDuration", amount of seconds
                     to inject traffic.
         - Rate: in frames per seconds or percentage.
-        - Type of rate: "framesPerSecond" ("bitsPerSecond" and
-                        "percentLineRate" no used)
+        - Type of rate: "framesPerSecond" or "percentLineRate" ("bitsPerSecond"
+                         no used)
         - Frame size: custom IMIX [1] definition; a list of packet size in
                       bytes and the weight. E.g.:
-                      [64, 10, 128, 15, 512, 5]
+                      [[64, 64, 10], [128, 128, 15], [512, 512, 5]]
 
         [1] https://en.wikipedia.org/wiki/Internet_Mix
 
         :param traffic: list of traffic elements; each traffic element contains
                         the injection parameter for each flow group.
+        :param duration: (int) injection time in seconds.
         """
         for traffic_param in traffic.values():
             fg_id = str(traffic_param['id'])
@@ -354,7 +358,10 @@ class IxNextgen(object):  # pragma: no cover
 
             type = traffic_param.get('traffic_type', 'fixedDuration')
             duration = traffic_param.get('duration', 30)
-            rate = traffic_param['iload']
+            rate = traffic_param['rate']
+            rate_unit = (
+                'framesPerSecond' if traffic_param['rate_unit'] ==
+                tp_base.TrafficProfileConfig.RATE_FPS else 'percentLineRate')
             weighted_range_pairs = self._parse_framesize(
                 traffic_param['outer_l2']['framesize'])
             srcmac = str(traffic_param.get('srcmac', '00:00:00:00:00:01'))
@@ -369,7 +376,7 @@ class IxNextgen(object):  # pragma: no cover
                 '-type', type, '-duration', duration)
             self.ixnet.setMultiAttribute(
                 config_element + '/frameRate',
-                '-rate', rate, '-type', 'framesPerSecond')
+                '-rate', rate, '-type', rate_unit)
             self.ixnet.setMultiAttribute(
                 config_element + '/frameSize',
                 '-type', 'weightedPairs',
@@ -392,15 +399,17 @@ class IxNextgen(object):  # pragma: no cover
         :param field: (str) field name, e.g.: scrIp, dstIp
         :param ip_address: (str) IP address
         :param seed: (int) seed length
-        :param mask: (str) IP address mask
+        :param mask: (int) IP address mask length
         :param count: (int) number of random IPs to generate
         """
         field_descriptor = self._get_field_in_stack_item(ip_descriptor,
                                                          field)
+        random_mask = str(ipaddress.IPv4Address(
+            2**(ipaddress.IPV4LENGTH - mask) - 1).compressed)
         self.ixnet.setMultiAttribute(field_descriptor,
                                      '-seed', seed,
                                      '-fixedBits', ip_address,
-                                     '-randomMask', mask,
+                                     '-randomMask', random_mask,
                                      '-valueType', 'random',
                                      '-countValue', count)
         self.ixnet.commit()
@@ -419,15 +428,17 @@ class IxNextgen(object):  # pragma: no cover
                 raise exceptions.IxNetworkFlowNotPresent(flow_group=fg_id)
 
             count = traffic_param['outer_l3']['count']
-            srcip4 = str(traffic_param['outer_l3']['srcip4'])
-            dstip4 = str(traffic_param['outer_l3']['dstip4'])
+            srcip = str(traffic_param['outer_l3']['srcip'])
+            dstip = str(traffic_param['outer_l3']['dstip'])
+            srcmask = traffic_param['outer_l3']['srcmask'] or IP_VERSION_4_MASK
+            dstmask = traffic_param['outer_l3']['dstmask'] or IP_VERSION_4_MASK
 
             self._update_ipv4_address(
                 self._get_stack_item(fg_id, PROTO_IPV4)[0],
-                'srcIp', srcip4, 1, IP_VERSION_4_MASK, count)
+                'srcIp', srcip, 1, srcmask, count)
             self._update_ipv4_address(
                 self._get_stack_item(fg_id, PROTO_IPV4)[0],
-                'dstIp', dstip4, 1, IP_VERSION_4_MASK, count)
+                'dstIp', dstip, 1, dstmask, count)
 
     def _build_stats_map(self, view_obj, name_map):
         return {data_yardstick: self.ixnet.execute(