CSIT-545: Performance tests for SNAT
[csit.git] / resources / libraries / python / SNATUtil.py
1 # Copyright (c) 2017 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 """SNAT utilities library."""
15
16 from resources.libraries.python.VatExecutor import VatTerminal
17
18
19 class SNATUtil(object):
20     """This class defines the methods to set SNAT."""
21
22     def __init__(self):
23         pass
24
25     @staticmethod
26     def set_snat_interfaces(node, int_in, int_out):
27         """Set inside and outside interfaces for SNAT.
28
29         :param node: DUT node.
30         :param int_in: Inside interface.
31         :param int_out: Outside interface.
32         :type node: dict
33         :type int_in: str
34         :type int_out: str
35         :returns: Response of the command.
36         :rtype: str
37         :raises RuntimeError: If setting of inside and outside interfaces for
38         SNAT fails.
39         """
40
41         try:
42             with VatTerminal(node, json_param=False) as vat:
43                 response = vat.vat_terminal_exec_cmd_from_template(
44                     'snat/snat_set_interfaces.vat',
45                     int_in=int_in, int_out=int_out)
46                 return response
47         except:
48             raise RuntimeError("Setting of inside and outside interfaces for "
49                                "SNAT failed!")
50
51     @staticmethod
52     def set_snat_deterministic(node, ip_in, subnet_in, ip_out, subnet_out):
53         """Set deterministic behaviour of SNAT.
54
55         :param node: DUT node.
56         :param ip_in: Inside IP.
57         :param subnet_in: Inside IP subnet.
58         :param ip_out: Outside IP.
59         :param subnet_out: Outside IP subnet.
60         :type node: dict
61         :type ip_in: str
62         :type subnet_in: str or int
63         :type ip_out: str
64         :type subnet_out: str or int
65         :returns: Response of the command.
66         :rtype: str
67         :raises RuntimeError: If setting of deterministic behaviour of SNAT
68         fails.
69         """
70
71         try:
72             with VatTerminal(node, json_param=False) as vat:
73                 response = vat.vat_terminal_exec_cmd_from_template(
74                     'snat/snat_set_deterministic.vat',
75                     ip_in=ip_in, subnet_in=subnet_in,
76                     ip_out=ip_out, subnet_out=subnet_out)
77                 return response
78         except:
79             raise RuntimeError("Setting of deterministic behaviour of SNAT "
80                                "failed!")
81
82     @staticmethod
83     def set_snat_workers(node, lcores):
84         """Set SNAT workers.
85
86         :param node: DUT node.
87         :param lcores: list of cores, format: range e.g. 1-5 or list of ranges
88         e.g.: 1-5,18-22.
89         :type node: dict
90         :type lcores: str
91         :returns: Response of the command.
92         :rtype: str
93         :raises RuntimeError: If setting of SNAT workers fails.
94         """
95
96         try:
97             with VatTerminal(node, json_param=False) as vat:
98                 response = vat.vat_terminal_exec_cmd_from_template(
99                     'snat/snat_set_workers.vat', lcores=lcores)
100                 return response
101         except:
102             raise RuntimeError("Setting of SNAT workers failed!")
103
104     @staticmethod
105     def show_snat(node):
106         """Show the SNAT settings.
107
108         :param node: DUT node.
109         :type node: dict
110         :returns: Response of the command.
111         :rtype: str
112         :raises RuntimeError: If getting of SNAT settings fails.
113         """
114
115         try:
116             with VatTerminal(node, json_param=False) as vat:
117                 response = vat.vat_terminal_exec_cmd_from_template(
118                     'snat/snat_show_snat.vat')
119                 return response
120         except:
121             raise RuntimeError("Getting of SNAT settings failed!")
122
123     @staticmethod
124     def show_snat_deterministic_forward(node, ip_addr):
125         """Show forward IP address and port(s).
126
127         :param node: DUT node.
128         :param ip_addr: IP address.
129         :type node: dict
130         :type ip_addr: str
131         :returns: Response of the command.
132         :rtype: str
133         :raises RuntimeError: If command 'exec snat deterministic forward'
134         fails.
135         """
136
137         try:
138             with VatTerminal(node, json_param=False) as vat:
139                 response = vat.vat_terminal_exec_cmd_from_template(
140                     'snat/snat_deterministic_forward.vat', ip=ip_addr)
141                 return response
142         except:
143             raise RuntimeError("Command 'exec snat deterministic forward {ip}'"
144                                " failed!".format(ip=ip_addr))
145
146     @staticmethod
147     def show_snat_deterministic_reverse(node, ip_addr, port):
148         """Show reverse IP address.
149
150         :param node: DUT node.
151         :param ip_addr: IP address.
152         :param port: Port.
153         :type node: dict
154         :type ip_addr: str
155         :type port: str or int
156         :returns: Response of the command.
157         :rtype: str
158         :raises RuntimeError: If command 'exec snat deterministic reverse'
159         fails.
160         """
161
162         try:
163             with VatTerminal(node, json_param=False) as vat:
164                 response = vat.vat_terminal_exec_cmd_from_template(
165                     'snat/snat_deterministic_reverse.vat',
166                     ip=ip_addr, port=port)
167                 return response
168         except:
169             raise RuntimeError(
170                 "Command 'exec snat deterministic reverse {ip}:{port}'"
171                 " failed!".format(ip=ip_addr, port=port))