HC Test: update routing test keyword due to namespace changes
[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             # Ignore extra data after NAT table
47             if "snat_static_mapping_dump error: Misc" in line or "vat#" in line:
48                 continue
49             items = line.split(" ")
50             while "" in items:
51                 items.remove("")
52             if len(items) == 0:
53                 continue
54             elif len(items) == 4:
55                 # no ports were returned
56                 data.append({
57                     "local_address": items[0],
58                     "remote_address": items[1],
59                     "vrf": items[2],
60                     "protocol": items[3]
61                 })
62             elif len(items) == 6:
63                 data.append({
64                     "local_address": items[0],
65                     "local_port": items[1],
66                     "remote_address": items[2],
67                     "remote_port": items[3],
68                     "vrf": items[4],
69                     "protocol": items[5]
70                 })
71             else:
72                 raise RuntimeError("Unexpected output from snat_mapping_dump.")
73
74         return data
75
76     @staticmethod
77     def vpp_get_nat_interfaces(node):
78         """Get list of interfaces configured with NAT from VPP node.
79
80         :param node: VPP node.
81         :type node: dict
82         :returns: List of interfaces on the node that are configured with NAT.
83         :rtype: list
84         :raises RuntimeError: If the output is not as expected.
85         """
86
87         vat = VatExecutor()
88         # JSON output not supported for this command
89         vat.execute_script('snat/snat_interface_dump.vat', node,
90                            json_out=False)
91
92         stdout = vat.get_script_stdout()
93         lines = stdout.split("\n")
94
95         data = []
96         for line in lines:
97             items = line.split(" ")
98             for trash in ("", "vat#"):
99                 while trash in items:
100                     items.remove(trash)
101             if len(items) == 0:
102                 continue
103             elif len(items) == 3:
104                 data.append({
105                     # items[0] is the table header - "sw_if_index"
106                     "sw_if_index": items[1],
107                     "direction": items[2]
108                 })
109             else:
110                 raise RuntimeError(
111                     "Unexpected output from snat_interface_dump.")
112
113         return data