feat(astf): Support framesizes for ASTF
[csit.git] / GPL / traffic_profiles / trex / profile_trex_astf_base_class.py
index ed28ccb..cb8fd76 100644 (file)
@@ -1,9 +1,19 @@
-# Copyright (c) 2020 Cisco and/or its affiliates.
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at:
+# Copyright (c) 2022 Cisco and/or its affiliates.
+#
+# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
+#
+# Licensed under the Apache License 2.0 or
+# GNU General Public License v2.0 or later;  you may not use this file
+# except in compliance with one of these Licenses. You
+# may obtain a copy of the Licenses at:
 #
 #     http://www.apache.org/licenses/LICENSE-2.0
+#     https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
+#
+# Note: If this file is linked with Scapy, which is GPLv2+, your use of it
+# must be under GPLv2+.  If at any point in the future it is no longer linked
+# with Scapy (or other GPLv2+ licensed software), you are free to choose
+# Apache 2.
 #
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
@@ -36,13 +46,9 @@ class TrafficProfileBaseClass:
         ]
     }
 
-    def __init__(self):
-        # Default values of required parameters; can be overwritten in
-        # "get_profile" method.
-        self.framesize = 64
-        self._pcap_dir = u""
-
-        # If needed, add your own parameters.
+    # TODO: Declare and document fields in a contructor to make pylint happier.
+    # TODO: Consider passing the values to define_profile(),
+    #       instead of keeping (and documenting) them as instance fields here.
 
     @property
     def pcap_dir(self):
@@ -62,7 +68,7 @@ class TrafficProfileBaseClass:
 
         :param current_length: Current length of the packet.
         :param required_length: Required length of the packet. If set to 0 then
-        self.framesize value is used.
+            self.framesize value is used.
         :type current_length: int
         :type required_length: int
         :returns: The generated padding.
@@ -71,21 +77,26 @@ class TrafficProfileBaseClass:
         # TODO: Add support for IMIX frame size;
         #  use random.randrange(0, len(self.STREAM_TABLE[self.framesize])) ?
         if not required_length:
-           required_length = self.framesize
-
-        return str(choices(ascii_letters, k=required_length - current_length))
+            required_length = self.framesize
+        missing = required_length - current_length
+        if missing < 0:
+            msg = f"Cannot to pad from {current_length} to {required_length}."
+            raise RuntimeError(msg)
+        padding = u"".join(choices(ascii_letters, k=missing))
+        return padding
 
     def define_profile(self):
         """Define profile to be used by T-Rex astf traffic generator.
 
         This method MUST return:
 
-            return ip_gen, templates, cap_list
+            return ip_gen, templates, kwargs
 
-            templates or cap_list CAN be None.
+            templates or kwargs CAN be None.
+            Kwargs can be used to define PCAP file, set MSS, ...
 
         :returns: IP generator and profile templates or list of pcap files for
-        traffic generator.
+            traffic generator.
         :rtype: tuple
         """
         raise NotImplementedError
@@ -98,15 +109,14 @@ class TrafficProfileBaseClass:
         :returns: Traffic profile.
         :rtype: trex.astf.trex_astf_profile.ASTFProfile
         """
-        ip_gen, templates, cap_list = self.define_profile()
-
-        # In most cases you will not have to change the code below:
+        ip_gen, templates, kwargs = self.define_profile()
+        if kwargs is None:
+            kwargs = dict()
 
-        # profile
         profile = ASTFProfile(
             default_ip_gen=ip_gen,
             templates=templates,
-            cap_list=cap_list
+            **kwargs
         )
 
         return profile
@@ -117,13 +127,15 @@ class TrafficProfileBaseClass:
         If needed, add your own parameters.
 
         :param kwargs: Key-value pairs used by "create_profile" method while
-        creating the profile.
+            creating the profile.
+        :type kwargs: dict
         :returns: Traffic profile.
         :rtype: trex.astf.trex_astf_profile.ASTFProfile
         """
         self.framesize = kwargs[u"framesize"]
+        self.n_data_frames = kwargs[u"n_data_frames"]
         self._pcap_dir = kwargs.get(
-            u"pcap_dir",u"/opt/trex-core-2.73/scripts/avl"
+            u"pcap_dir", u"/opt/trex-core-2.88/scripts/avl"
         )
 
         return self.create_profile()