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