03dc503cf25d9ea897f9e6919c1b2829df2d40e0
[csit.git] / GPL / traffic_profiles / trex / profile_trex_astf_base_class.py
1 # Copyright (c) 2020 Cisco and/or its affiliates.
2 #
3 # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
4 #
5 # Licensed under the Apache License 2.0 or
6 # GNU General Public License v2.0 or later;  you may not use this file
7 # except in compliance with one of these Licenses. You
8 # may obtain a copy of the Licenses at:
9 #
10 #     http://www.apache.org/licenses/LICENSE-2.0
11 #     https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 #
13 # Note: If this file is linked with Scapy, which is GPLv2+, your use of it
14 # must be under GPLv2+.  If at any point in the future it is no longer linked
15 # with Scapy (or other GPLv2+ licensed software), you are free to choose Apache 2.
16 #
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
22
23 """Base class for profiles for T-rex advanced stateful (astf) traffic generator.
24 """
25
26 from random import choices
27 from string import ascii_letters
28
29 from trex.astf.api import *
30
31
32 class TrafficProfileBaseClass:
33     """Base class for profiles for T-rex astf traffic generator."""
34
35     STREAM_TABLE = {
36         u"IMIX_v4": [
37             {u"size": 60, u"pps": 28, u"isg": 0},
38             {u"size": 590, u"pps": 20, u"isg": 0.1},
39             {u"size": 1514, u"pps": 4, u"isg": 0.2}
40         ],
41         'IMIX_v4_1': [
42             {u"size": 64, u"pps": 28, u"isg": 0},
43             {u"size": 570, u"pps": 16, u"isg": 0.1},
44             {u"size": 1518, u"pps": 4, u"isg": 0.2}
45         ]
46     }
47
48     def __init__(self):
49         # Default values of required parameters; can be overwritten in
50         # "get_profile" method.
51         self.framesize = 64
52         self._pcap_dir = u""
53
54         # If needed, add your own parameters.
55
56     @property
57     def pcap_dir(self):
58         """Pcap file directory.
59
60         If needed, implement your own algorithm.
61
62         :returns: Pcap file directory.
63         :rtype: str
64         """
65         return self._pcap_dir
66
67     def _gen_padding(self, current_length, required_length=0):
68         """Generate padding.
69
70         If needed, implement your own algorithm.
71
72         :param current_length: Current length of the packet.
73         :param required_length: Required length of the packet. If set to 0 then
74         self.framesize value is used.
75         :type current_length: int
76         :type required_length: int
77         :returns: The generated padding.
78         :rtype: str
79         """
80         # TODO: Add support for IMIX frame size;
81         #  use random.randrange(0, len(self.STREAM_TABLE[self.framesize])) ?
82         if not required_length:
83             required_length = self.framesize
84
85         return str(choices(ascii_letters, k=required_length - current_length))
86
87     def define_profile(self):
88         """Define profile to be used by T-Rex astf traffic generator.
89
90         This method MUST return:
91
92             return ip_gen, templates, cap_list
93
94             templates or cap_list CAN be None.
95
96         :returns: IP generator and profile templates or list of pcap files for
97         traffic generator.
98         :rtype: tuple
99         """
100         raise NotImplementedError
101
102     def create_profile(self):
103         """Create traffic profile.
104
105         Implement your own traffic profiles.
106
107         :returns: Traffic profile.
108         :rtype: trex.astf.trex_astf_profile.ASTFProfile
109         """
110         ip_gen, templates, cap_list = self.define_profile()
111
112         # In most cases you will not have to change the code below:
113
114         # profile
115         profile = ASTFProfile(
116             default_ip_gen=ip_gen,
117             templates=templates,
118             cap_list=cap_list
119         )
120
121         return profile
122
123     def get_profile(self, **kwargs):
124         """Get traffic profile created by "create_profile" method.
125
126         If needed, add your own parameters.
127
128         :param kwargs: Key-value pairs used by "create_profile" method while
129         creating the profile.
130         :returns: Traffic profile.
131         :rtype: trex.astf.trex_astf_profile.ASTFProfile
132         """
133         self.framesize = kwargs[u"framesize"]
134         self._pcap_dir = kwargs.get(
135             u"pcap_dir",u"/opt/trex-core-2.82/scripts/avl"
136         )
137
138         return self.create_profile()