1 # Copyright (c) 2016 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 """NAT utilities library."""
16 from resources.libraries.python.VatExecutor import VatExecutor
19 class NATUtil(object):
26 def vpp_get_nat_static_mappings(node):
27 """Get NAT static mappings from VPP node.
29 :param node: VPP node.
31 :returns: List of static mappings.
33 :raises RuntimeError: If the output is not as expected.
37 # JSON output not supported for this command
38 vat.execute_script('snat/snat_mapping_dump.vat', node, json_out=False)
40 stdout = vat.get_script_stdout()
41 lines = stdout.split("\n")
44 # lines[0,1] are table and column headers
45 for line in lines[2::]:
46 # Ignore extra data after NAT table
47 if "snat_static_mapping_dump error: Misc" in line or "vat#" in line:
49 items = line.split(" ")
55 # no ports were returned
57 "local_address": items[0],
58 "remote_address": items[1],
64 "local_address": items[0],
65 "local_port": items[1],
66 "remote_address": items[2],
67 "remote_port": items[3],
72 raise RuntimeError("Unexpected output from snat_mapping_dump.")
77 def vpp_get_nat_interfaces(node):
78 """Get list of interfaces configured with NAT from VPP node.
80 :param node: VPP node.
82 :returns: List of interfaces on the node that are configured with NAT.
84 :raises RuntimeError: If the output is not as expected.
88 # JSON output not supported for this command
89 vat.execute_script('snat/snat_interface_dump.vat', node,
92 stdout = vat.get_script_stdout()
93 lines = stdout.split("\n")
97 items = line.split(" ")
98 for trash in ("", "vat#"):
103 elif len(items) == 3:
105 # items[0] is the table header - "sw_if_index"
106 "sw_if_index": items[1],
107 "direction": items[2]
111 "Unexpected output from snat_interface_dump.")