78e71a75e40324f88c288dc5661746e1df9b6fff
[csit.git] / resources / libraries / python / honeycomb / proxyARP.py
1 # Copyright (c) 2017 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 """This module implements keywords to configure proxyARP using Honeycomb
15 REST API."""
16
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 HoneycombUtil as HcUtil
21 from resources.libraries.python.honeycomb.HoneycombUtil \
22     import DataRepresentation
23 from resources.libraries.python.topology import Topology
24
25
26 class ProxyARPKeywords(object):
27     """Implementation of keywords which make it possible to:
28     - configure proxyARP behaviour
29     - enable/disable proxyARP on individual interfaces
30     """
31
32     def __init__(self):
33         """Initializer."""
34         pass
35
36     @staticmethod
37     def configure_proxyarp(node, data):
38         """Configure the proxyARP feature and check the return code.
39
40         :param node: Honeycomb node.
41         :param data: Configuration to use.
42         :type node: dict
43         :type data: dict
44         :returns: Content of response.
45         :rtype: bytearray
46         :raises HoneycombError: If the status code in response to PUT is not
47         200 = OK or 201 = ACCEPTED.
48         """
49
50         data = {
51             "proxy-ranges": {
52                 "proxy-range": [
53                     data,
54                 ]
55             }
56         }
57
58         status_code, resp = HcUtil.\
59             put_honeycomb_data(node, "config_proxyarp_ranges", data,
60                                data_representation=DataRepresentation.JSON)
61
62         if status_code not in (HTTPCodes.OK, HTTPCodes.ACCEPTED):
63             raise HoneycombError(
64                 "proxyARP configuration unsuccessful. "
65                 "Status code: {0}.".format(status_code))
66         else:
67             return resp
68
69     @staticmethod
70     def remove_proxyarp_configuration(node):
71         """Delete the proxyARP node, removing all of its configuration.
72
73         :param node: Honeycomb node.
74         :type node: dict
75         :returns: Content of response.
76         :rtype: bytearray
77         :raises HoneycombError: If the status code in response is not 200 = OK.
78         """
79
80         status_code, resp = HcUtil. \
81             delete_honeycomb_data(node, "config_proxyarp_ranges")
82
83         if status_code != HTTPCodes.OK:
84             raise HoneycombError(
85                 "proxyARP removal unsuccessful. "
86                 "Status code: {0}.".format(status_code))
87         else:
88             return resp
89
90     @staticmethod
91     def get_proxyarp_operational_data(node):
92         """Retrieve proxyARP properties from Honeycomb operational data.
93         Note: The proxyARP feature has no operational data available.
94
95         :param node: Honeycomb node.
96         :type node: dict
97         :returns: proxyARP operational data.
98         :rtype: bytearray
99         """
100
101         raise NotImplementedError("Not supported in VPP.")
102
103     @staticmethod
104     def set_proxyarp_interface_config(node, interface, state):
105         """Enable or disable the proxyARP feature on the specified interface.
106
107         :param node: Honeycomb node.
108         :param interface: Name or sw_if_index of an interface on the node.
109         :param state: Desired proxyARP state: enable, disable.
110         :type node: dict
111         :type interface: str
112         :type state: str
113         :raises ValueError: If the state argument is incorrect.
114         :raises HoneycombError: If the status code in response is not
115         200 = OK or 201 = ACCEPTED.
116         """
117
118         interface = Topology.convert_interface_reference(
119             node, interface, "name")
120         interface = interface.replace("/", "%2F")
121
122         path = "/interface/{0}/proxy-arp".format(interface)
123
124         if state == "disable":
125             status_code, resp = HcUtil.delete_honeycomb_data(
126                 node, "config_vpp_interfaces", path)
127         elif state == "enable":
128             data = {"proxy-arp": {}}
129             status_code, resp = HcUtil.put_honeycomb_data(
130                 node, "config_vpp_interfaces", data, path)
131         else:
132             raise ValueError("State argument has to be enable or disable.")
133
134         if status_code not in (HTTPCodes.OK, HTTPCodes.ACCEPTED):
135             raise HoneycombError(
136                 "Interface proxyARP configuration on node {0} was not"
137                 " successful.".format(node))
138
139     @staticmethod
140     def get_proxyarp_interface_assignment(node, interface):
141         """Read the status of proxyARP feature on the specified interface.
142         Note: The proxyARP feature has no operational data available.
143
144         :param node: Honeycomb node.
145         :param interface: Name or sw_if_index of an interface on the node.
146         :type node: dict
147         :type interface: str
148         :returns: Content of response.
149         :rtype: bytearray
150         """
151
152         raise NotImplementedError("Not supported in VPP.")