Improve test tag string parsing
[csit.git] / resources / libraries / python / honeycomb / DHCP.py
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:
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.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
22
23
24 class DHCPRelayKeywords(object):
25     """Keywords for NAT configuration."""
26
27     def __init__(self):
28         pass
29
30     @staticmethod
31     def _set_dhcp_relay_properties(node, path, data=None):
32         """Set DHCP relay properties and check the return code.
33
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.
37         :type node: dict
38         :type path: str
39         :type data: dict
40         :returns: Content of response.
41         :rtype: bytearray
42         :raises HoneycombError: If the status code in response is not
43         200 = OK or 201 = ACCEPTED.
44         """
45
46         if data:
47             status_code, resp = HcUtil. \
48                 put_honeycomb_data(node, "config_dhcp_relay", data, path,
49                                    data_representation=DataRepresentation.JSON)
50         else:
51             status_code, resp = HcUtil. \
52                 delete_honeycomb_data(node, "config_dhcp_relay", path)
53
54         if status_code not in (HTTPCodes.OK, HTTPCodes.ACCEPTED):
55             raise HoneycombError(
56                 "The configuration of DHCP relay was not successful. "
57                 "Status code: {0}.".format(status_code))
58         return resp
59
60     @staticmethod
61     def add_dhcp_relay(node, data, ip_version, entry_id):
62         """Add a DHCP relay entry to the list on entries.
63
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.
68         :type node: dict
69         :type data: dict
70         :type ip_version: str
71         :type entry_id: int
72         :returns: Content of response.
73         :rtype: bytearray
74         """
75
76         path = "/relay/vpp-fib-table-management:{0}/{1}".format(ip_version,
77                                                                 entry_id)
78
79         return DHCPRelayKeywords._set_dhcp_relay_properties(node, path, data)
80
81     @staticmethod
82     def clear_dhcp_relay_configuration(node):
83         """Remove all DHCP relay configuration from the node.
84
85         :param node: Honeycomb node.
86         :type node: dict
87         :returns: Content of response.
88         :rtype: bytearray
89         """
90         return DHCPRelayKeywords._set_dhcp_relay_properties(node, "")
91
92     @staticmethod
93     def get_dhcp_relay_oper_data(node):
94         """Get operational data about the DHCP relay feature.
95
96         :param node: Honeycomb node.
97         :type node: dict
98         :returns: Content of response.
99         :rtype: bytearray
100         :raises HoneycombError: If the status code in response is not 200 = OK.
101         """
102
103         status_code, resp = HcUtil. \
104             get_honeycomb_data(node, "config_dhcp_relay")
105
106         if status_code != HTTPCodes.OK:
107             raise HoneycombError(
108                 "Could not retrieve DHCP relay configuration. "
109                 "Status code: {0}.".format(status_code))
110         return resp