1fb066d10cd8a6647920833165768836ecac5b85
[csit.git] / resources / libraries / python / honeycomb / 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 """Keywords to manipulate NAT configuration using Honeycomb REST API."""
15
16 from resources.libraries.python.topology import Topology
17 from resources.libraries.python.HTTPRequest import HTTPCodes
18 from resources.libraries.python.honeycomb.HoneycombSetup import HoneycombError
19 from resources.libraries.python.honeycomb.HoneycombUtil \
20     import DataRepresentation
21 from resources.libraries.python.honeycomb.HoneycombUtil \
22     import HoneycombUtil as HcUtil
23
24
25 class NATKeywords(object):
26     """Keywords for NAT configuration."""
27
28     def __init__(self):
29         pass
30
31     @staticmethod
32     def _set_nat_properties(node, path, data=None):
33         """Set NAT properties and check the return code.
34
35         :param node: Honeycomb node.
36         :param path: Path which is added to the base path to identify the data.
37         :param data: The new data to be set. If None, the item will be removed.
38         :type node: dict
39         :type path: str
40         :type data: dict
41         :returns: Content of response.
42         :rtype: bytearray
43         :raises HoneycombError: If the status code in response to PUT is not
44         OK or ACCEPTED.
45         """
46
47         if data:
48             status_code, resp = HcUtil. \
49                 put_honeycomb_data(node, "config_nat", data, path,
50                                    data_representation=DataRepresentation.JSON)
51         else:
52             status_code, resp = HcUtil. \
53                 delete_honeycomb_data(node, "config_nat", path)
54
55         if status_code not in (HTTPCodes.OK, HTTPCodes.ACCEPTED):
56             raise HoneycombError(
57                 "The configuration of NAT was not successful. "
58                 "Status code: {0}.".format(status_code))
59         return resp
60
61     @staticmethod
62     def get_nat_oper_data(node):
63         """Read NAT operational data.
64
65         :param node: Honeycomb node.
66         :type node: dict
67         :returns: Content of response.
68         :rtype: bytearray
69         :raises HoneycombError: If the operation fails or the response
70         is not as expected.
71         """
72
73         status_code, resp = HcUtil.get_honeycomb_data(node, "oper_nat")
74
75         if status_code != HTTPCodes.OK:
76             raise HoneycombError("Could not retrieve NAT operational data.")
77
78         if "nat-state" not in resp.keys():
79             raise HoneycombError(
80                 "Unexpected format, response does not contain nat-state.")
81         return resp['nat-state']
82
83     @staticmethod
84     def configure_nat_entries(node, data, instance=0, entry=1):
85         """Configure NAT entries on node.
86
87         :param node: Honeycomb node.
88         :param data: Data to be configured on node.
89         :param instance: NAT instance ID.
90         :param entry: NAT entry index.
91         :type node: dict
92         :type data: dict
93         :type instance: int
94         :type entry: int
95         :returns: Content of response.
96         :rtype: bytearray
97         """
98
99         return NATKeywords._set_nat_properties(
100             node,
101             '/nat-instances/nat-instance/{0}/'
102             'mapping-table/mapping-entry/{1}/'.format(instance, entry),
103             data)
104
105     @staticmethod
106     def configure_nat_on_interface(node, interface, direction, delete=False):
107         """Configure NAT on the specified interface.
108
109         :param node: Honeycomb node.
110         :param interface: Name of an interface on the node.
111         :param direction: NAT direction, outbound or inbound.
112         :param delete: Delete an existing interface NAT configuration.
113         :type node: dict
114         :type interface: str
115         :type direction: str
116         :type delete: bool
117         :returns: Content of response.
118         :rtype: bytearray
119         :raises HoneycombError: If the operation fails.
120         """
121
122         interface = Topology.convert_interface_reference(
123             node, interface, "name")
124
125         interface = interface.replace("/", "%2F")
126         path = "/interface/{0}/interface-nat:nat/{1}".format(
127             interface, direction)
128
129         data = {direction: {}}
130
131         if delete:
132             status_code, resp = HcUtil.delete_honeycomb_data(
133                 node, "config_vpp_interfaces", path)
134         else:
135             status_code, resp = HcUtil.put_honeycomb_data(
136                 node, "config_vpp_interfaces", data, path)
137
138         if status_code not in (HTTPCodes.OK, HTTPCodes.ACCEPTED):
139             raise HoneycombError(
140                 "Could not configure NAT on interface. "
141                 "Status code: {0}.".format(status_code))
142
143         return resp