VIRL test: Replace IP probe for VXLAN test
[csit.git] / resources / libraries / python / NAT.py
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:
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 VatExecutor
17
18
19 class NATUtil(object):
20     """NAT utilities."""
21
22     def __init__(self):
23         pass
24
25     @staticmethod
26     def vpp_get_nat_static_mappings(node):
27         """Get NAT static mappings from VPP node.
28
29         :param node: VPP node.
30         :type node: dict
31         :returns: List of static mappings.
32         :rtype: list
33         :raises RuntimeError: If the output is not as expected.
34         """
35
36         vat = VatExecutor()
37         # JSON output not supported for this command
38         vat.execute_script('snat/snat_mapping_dump.vat', node, json_out=False)
39
40         stdout = vat.get_script_stdout()
41         lines = stdout.split("\n")
42
43         data = []
44         # lines[0,1] are table and column headers
45         for line in lines[2::]:
46             items = line.split(" ")
47             while "" in items:
48                 items.remove("")
49             if len(items) == 0:
50                 continue
51             elif len(items) == 3:
52                 # no ports were returned
53                 data.append({
54                     "local_address": items[0],
55                     "remote_address": items[1],
56                     "vrf": items[2]
57                 })
58             elif len(items) == 5:
59                 data.append({
60                     "local_address": items[0],
61                     "local_port": items[1],
62                     "remote_address": items[2],
63                     "remote_port": items[3],
64                     "vrf": items[4]
65                 })
66             else:
67                 raise RuntimeError("Unexpected output from span_mapping_dump.")
68
69         return data
70
71     @staticmethod
72     def vpp_get_nat_interfaces(node):
73         """Get list of interfaces configured with NAT from VPP node.
74
75         :param node: VPP node.
76         :type node: dict
77         :returns: List of interfaces on the node that are configured with NAT.
78         :rtype: list
79         :raises RuntimeError: If the output is not as expected.
80         """
81
82         vat = VatExecutor()
83         # JSON output not supported for this command
84         vat.execute_script('snat/snat_interface_dump.vat', node,
85                            json_out=False)
86
87         stdout = vat.get_script_stdout()
88         lines = stdout.split("\n")
89
90         data = []
91         for line in lines:
92             items = line.split(" ")
93             while "" in items:
94                 items.remove("")
95             if len(items) == 0:
96                 continue
97             elif len(items) == 3:
98                 data.append({
99                     # items[0] is the table header - "sw_if_index"
100                     "sw_if_index": items[1],
101                     "direction": items[2]
102                 })
103             else:
104                 raise RuntimeError(
105                     "Unexpected output from span_interface_dump.")
106
107         return data