Fix various pylint violations
[csit.git] / resources / libraries / python / NATUtil.py
1 # Copyright (c) 2018 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 """NAT utilities library."""
15
16 from resources.libraries.python.VatExecutor import VatTerminal, VatExecutor
17
18
19 class NATUtil(object):
20     """This class defines the methods to set NAT."""
21
22     def __init__(self):
23         pass
24
25     @staticmethod
26     def set_nat44_interfaces(node, int_in, int_out):
27         """Set inside and outside interfaces for NAT44.
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             NAT44 fails.
39         """
40
41         try:
42             with VatTerminal(node, json_param=False) as vat:
43                 response = vat.vat_terminal_exec_cmd_from_template(
44                     'nat/nat44_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                                "NAT failed!")
50
51     @staticmethod
52     def set_nat44_deterministic(node, ip_in, subnet_in, ip_out, subnet_out):
53         """Set deterministic behaviour of NAT44.
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 NAT44
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                     'nat/nat44_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 NAT "
80                                "failed!")
81
82     @staticmethod
83     def set_nat_workers(node, lcores):
84         """Set NAT 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 NAT 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                     'nat/nat_set_workers.vat', lcores=lcores)
100                 return response
101         except:
102             raise RuntimeError("Setting of NAT workers failed!")
103
104     @staticmethod
105     def show_nat(node):
106         """Show the NAT 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 NAT 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                     'nat/nat_show_nat.vat')
119                 return response
120         except:
121             raise RuntimeError("Getting of NAT settings failed!")
122
123     @staticmethod
124     def show_nat44_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                     'nat/nat44_deterministic_forward.vat', ip=ip_addr)
141                 return response
142         except:
143             raise RuntimeError("Command 'exec nat44 deterministic forward {ip}'"
144                                " failed!".format(ip=ip_addr))
145
146     @staticmethod
147     def show_nat44_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                     'nat/nat44_deterministic_reverse.vat',
166                     ip=ip_addr, port=port)
167                 return response
168         except:
169             raise RuntimeError(
170                 "Command 'exec nat44 deterministic reverse {ip}:{port}'"
171                 " failed!".format(ip=ip_addr, port=port))
172
173     @staticmethod
174     def get_nat_static_mappings(node):
175         """Get NAT static mappings from VPP node.
176
177         :param node: VPP node.
178         :type node: dict
179         :returns: List of static mappings.
180         :rtype: list
181         :raises RuntimeError: If the output is not as expected.
182         """
183
184         vat = VatExecutor()
185         # JSON output not supported for this command
186         vat.execute_script('nat/snat_mapping_dump.vat', node, json_out=False)
187
188         stdout = vat.get_script_stdout()
189         lines = stdout.split("\n")
190
191         data = []
192         # lines[0,1] are table and column headers
193         for line in lines[2::]:
194             # Ignore extra data after NAT table
195             if "snat_static_mapping_dump error: Misc" in line or "vat#" in line:
196                 continue
197             items = line.split(" ")
198             while "" in items:
199                 items.remove("")
200             if not items:
201                 continue
202             if len(items) == 4:
203                 # no ports were returned
204                 data.append({
205                     "local_address": items[0],
206                     "remote_address": items[1],
207                     "vrf": items[2],
208                     "protocol": items[3]
209                 })
210             elif len(items) == 6:
211                 data.append({
212                     "local_address": items[0],
213                     "local_port": items[1],
214                     "remote_address": items[2],
215                     "remote_port": items[3],
216                     "vrf": items[4],
217                     "protocol": items[5]
218                 })
219             else:
220                 raise RuntimeError("Unexpected output from snat_mapping_dump.")
221
222         return data
223
224     @staticmethod
225     def get_nat_interfaces(node):
226         """Get list of interfaces configured with NAT from VPP node.
227
228         :param node: VPP node.
229         :type node: dict
230         :returns: List of interfaces on the node that are configured with NAT.
231         :rtype: list
232         :raises RuntimeError: If the output is not as expected.
233         """
234
235         vat = VatExecutor()
236         # JSON output not supported for this command
237         vat.execute_script('nat/snat_interface_dump.vat', node,
238                            json_out=False)
239
240         stdout = vat.get_script_stdout()
241         lines = stdout.split("\n")
242
243         data = []
244         for line in lines:
245             items = line.split(" ")
246             for trash in ("", "vat#"):
247                 while trash in items:
248                     items.remove(trash)
249             if not items:
250                 continue
251             if len(items) == 3:
252                 data.append({
253                     # items[0] is the table header - "sw_if_index"
254                     "sw_if_index": items[1],
255                     "direction": items[2]
256                 })
257             else:
258                 raise RuntimeError(
259                     "Unexpected output from snat_interface_dump.")
260
261         return data