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.HTTPRequest import HTTPCodes
17 from resources.libraries.python.honeycomb.HoneycombSetup import HoneycombError
18 from resources.libraries.python.honeycomb.HoneycombUtil \
19 import DataRepresentation
20 from resources.libraries.python.honeycomb.HoneycombUtil \
21 import HoneycombUtil as HcUtil
24 class DHCPRelayKeywords(object):
25 """Keywords for NAT configuration."""
31 def _set_dhcp_relay_properties(node, path, data=None):
32 """Set DHCP relay properties and check the return code.
34 :param node: Honeycomb node.
35 :param path: Path which is added to the base path to identify the data.
36 :param data: The new data to be set. If None, the item will be removed.
40 :returns: Content of response.
42 :raises HoneycombError: If the status code in response is not
43 200 = OK or 201 = ACCEPTED.
47 status_code, resp = HcUtil. \
48 put_honeycomb_data(node, "config_dhcp_relay", data, path,
49 data_representation=DataRepresentation.JSON)
51 status_code, resp = HcUtil. \
52 delete_honeycomb_data(node, "config_dhcp_relay", path)
54 if status_code not in (HTTPCodes.OK, HTTPCodes.ACCEPTED):
56 "The configuration of DHCP relay was not successful. "
57 "Status code: {0}.".format(status_code))
61 def add_dhcp_relay(node, data, ip_version, entry_id):
62 """Add a DHCP relay entry to the list on entries.
64 :param node: Honeycomb node.
65 :param data: Configuration for the relay entry.
66 :param ip_version: IP protocol version, ipv4 or ipv6.
67 :param entry_id: Numeric ID.
72 :returns: Content of response.
76 path = "/relay/vpp-fib-table-management:{0}/{1}".format(ip_version,
79 return DHCPRelayKeywords._set_dhcp_relay_properties(node, path, data)
82 def clear_dhcp_relay_configuration(node):
83 """Remove all DHCP relay configuration from the node.
85 :param node: Honeycomb node.
87 :returns: Content of response.
90 return DHCPRelayKeywords._set_dhcp_relay_properties(node, "")
93 def get_dhcp_relay_oper_data(node):
94 """Get operational data about the DHCP relay feature.
96 :param node: Honeycomb node.
98 :returns: Content of response.
100 :raises HoneycombError: If the status code in response is not 200 = OK.
103 status_code, resp = HcUtil. \
104 get_honeycomb_data(node, "config_dhcp_relay")
106 if status_code != HTTPCodes.OK:
107 raise HoneycombError(
108 "Could not retrieve DHCP relay configuration. "
109 "Status code: {0}.".format(status_code))