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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
14 """SNAT utilities library."""
16 from resources.libraries.python.VatExecutor import VatTerminal
19 class SNATUtil(object):
20 """This class defines the methods to set SNAT."""
26 def set_snat_interfaces(node, int_in, int_out):
27 """Set inside and outside interfaces for SNAT.
29 :param node: DUT node.
30 :param int_in: Inside interface.
31 :param int_out: Outside interface.
35 :returns: Response of the command.
37 :raises RuntimeError: If setting of inside and outside interfaces for
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)
48 raise RuntimeError("Setting of inside and outside interfaces for "
52 def set_snat_deterministic(node, ip_in, subnet_in, ip_out, subnet_out):
53 """Set deterministic behaviour of SNAT.
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.
62 :type subnet_in: str or int
64 :type subnet_out: str or int
65 :returns: Response of the command.
67 :raises RuntimeError: If setting of deterministic behaviour of SNAT
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)
79 raise RuntimeError("Setting of deterministic behaviour of SNAT "
83 def set_snat_workers(node, lcores):
86 :param node: DUT node.
87 :param lcores: list of cores, format: range e.g. 1-5 or list of ranges
91 :returns: Response of the command.
93 :raises RuntimeError: If setting of SNAT workers fails.
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)
102 raise RuntimeError("Setting of SNAT workers failed!")
106 """Show the SNAT settings.
108 :param node: DUT node.
110 :returns: Response of the command.
112 :raises RuntimeError: If getting of SNAT settings fails.
116 with VatTerminal(node, json_param=False) as vat:
117 response = vat.vat_terminal_exec_cmd_from_template(
118 'snat/snat_show_snat.vat')
121 raise RuntimeError("Getting of SNAT settings failed!")
124 def show_snat_deterministic_forward(node, ip_addr):
125 """Show forward IP address and port(s).
127 :param node: DUT node.
128 :param ip_addr: IP address.
131 :returns: Response of the command.
133 :raises RuntimeError: If command 'exec snat deterministic forward'
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)
143 raise RuntimeError("Command 'exec snat deterministic forward {ip}'"
144 " failed!".format(ip=ip_addr))
147 def show_snat_deterministic_reverse(node, ip_addr, port):
148 """Show reverse IP address.
150 :param node: DUT node.
151 :param ip_addr: IP address.
155 :type port: str or int
156 :returns: Response of the command.
158 :raises RuntimeError: If command 'exec snat deterministic reverse'
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)
170 "Command 'exec snat deterministic reverse {ip}:{port}'"
171 " failed!".format(ip=ip_addr, port=port))