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 items = line.split(" ")
52 # no ports were returned
54 "local_address": items[0],
55 "remote_address": items[1],
60 "local_address": items[0],
61 "local_port": items[1],
62 "remote_address": items[2],
63 "remote_port": items[3],
67 raise RuntimeError("Unexpected output from span_mapping_dump.")
72 def vpp_get_nat_interfaces(node):
73 """Get list of interfaces configured with NAT from VPP node.
75 :param node: VPP node.
77 :returns: List of interfaces on the node that are configured with NAT.
79 :raises RuntimeError: If the output is not as expected.
83 # JSON output not supported for this command
84 vat.execute_script('snat/snat_interface_dump.vat', node,
87 stdout = vat.get_script_stdout()
88 lines = stdout.split("\n")
92 items = line.split(" ")
99 # items[0] is the table header - "sw_if_index"
100 "sw_if_index": items[1],
101 "direction": items[2]
105 "Unexpected output from span_interface_dump.")