1 # Copyright (c) 2018 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 """Keywords to manipulate NAT configuration using Honeycomb REST API."""
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
25 class NATKeywords(object):
26 """Keywords for NAT configuration."""
32 def _set_nat_properties(node, path, data=None):
33 """Set NAT properties and check the return code.
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.
41 :returns: Content of response.
43 :raises HoneycombError: If the status code in response to PUT is not
48 status_code, resp = HcUtil. \
49 put_honeycomb_data(node, "config_nat", data, path,
50 data_representation=DataRepresentation.JSON)
52 status_code, resp = HcUtil. \
53 delete_honeycomb_data(node, "config_nat", path)
55 if status_code not in (HTTPCodes.OK, HTTPCodes.ACCEPTED):
57 "The configuration of NAT was not successful. "
58 "Status code: {0}.".format(status_code))
62 def get_nat_oper_data(node):
63 """Read NAT operational data.
65 :param node: Honeycomb node.
67 :returns: Content of response.
69 :raises HoneycombError: If the operation fails or the response
73 status_code, resp = HcUtil.get_honeycomb_data(node, "oper_nat")
75 if status_code != HTTPCodes.OK:
76 raise HoneycombError("Could not retrieve NAT operational data.")
78 if "nat-state" not in resp.keys():
80 "Unexpected format, response does not contain nat-state.")
81 return resp['nat-state']
84 def configure_nat_entries(node, data, instance=0, entry=1):
85 """Configure NAT entries on node.
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.
95 :returns: Content of response.
99 return NATKeywords._set_nat_properties(
101 '/nat-instances/nat-instance/{0}/'
102 'mapping-table/mapping-entry/{1}/'.format(instance, entry),
106 def configure_nat_on_interface(node, interface, direction, delete=False):
107 """Configure NAT on the specified interface.
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.
117 :returns: Content of response.
119 :raises HoneycombError: If the operation fails.
122 interface = Topology.convert_interface_reference(
123 node, interface, "name")
125 interface = interface.replace("/", "%2F")
126 path = "/interface/{0}/interface-nat:nat/{1}".format(
127 interface, direction)
129 data = {direction: {}}
132 status_code, resp = HcUtil.delete_honeycomb_data(
133 node, "config_vpp_interfaces", path)
135 status_code, resp = HcUtil.put_honeycomb_data(
136 node, "config_vpp_interfaces", data, path)
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))