Fix: use pci/rescan to avoid occasional bind issue on Centos7
[csit.git] / resources / libraries / python / honeycomb / Lisp.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 """This module implements keywords to manipulate Lisp data structures using
15 Honeycomb 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
24
25 class LispKeywords(object):
26     """Implementation of keywords which make it possible to:
27     - enable/disable Lisp feature
28     - configure Lisp mappings
29     - configure locator sets
30     - configure map resolver
31     - configure Lisp PITR feature
32     - read operational data for all of the above
33     """
34
35     def __init__(self):
36         """Initializer."""
37         pass
38
39     @staticmethod
40     def _set_lisp_properties(node, path, data=None):
41         """Set Lisp properties and check the return code.
42
43         :param node: Honeycomb node.
44         :param path: Path which is added to the base path to identify the data.
45         :param data: The new data to be set. If None, the item will be removed.
46         :type node: dict
47         :type path: str
48         :type data: dict
49         :returns: Content of response.
50         :rtype: bytearray
51         :raises HoneycombError: If the status code in response to PUT is not
52         200 = OK or 201 = ACCEPTED.
53         """
54
55         if data:
56             status_code, resp = HcUtil.\
57                 put_honeycomb_data(node, "config_lisp", data, path,
58                                    data_representation=DataRepresentation.JSON)
59         else:
60             status_code, resp = HcUtil.\
61                 delete_honeycomb_data(node, "config_lisp", path)
62
63         if status_code not in (HTTPCodes.OK, HTTPCodes.ACCEPTED):
64             raise HoneycombError(
65                 "Lisp configuration unsuccessful. "
66                 "Status code: {0}.".format(status_code))
67         else:
68             return resp
69
70     @staticmethod
71     def get_lisp_operational_data(node):
72         """Retrieve Lisp properties from Honeycomb operational data.
73
74         :param node: Honeycomb node.
75         :type node: dict
76         :returns: List operational data.
77         :rtype: bytearray
78         """
79
80         status_code, resp = HcUtil.get_honeycomb_data(node, "oper_lisp")
81
82         if status_code != HTTPCodes.OK:
83             raise HoneycombError("Could not retrieve Lisp operational data."
84                                  "Status code: {0}.".format(status_code))
85         else:
86             # get rid of empty vni-table entry
87             resp["lisp-state"]["lisp-feature-data"]["eid-table"][
88                 "vni-table"].remove(
89                     {
90                         "virtual-network-identifier": 0,
91                         "vrf-subtable": {"table-id": 0}
92                     }
93                 )
94             return resp
95
96     @staticmethod
97     def set_lisp_state(node, state):
98         """Enable or disable the Lisp feature.
99
100         :param node: Honeycomb node.
101         :param state: Desired Lisp state, enable or disable.
102         :type node: dict
103         :type state: str
104         :returns: Content of response.
105         :rtype: bytearray
106         """
107
108         data = {
109             "lisp": {
110                 "enable": True if state.lower() == "enable" else False
111             }
112         }
113
114         return LispKeywords._set_lisp_properties(node, '', data)
115
116     @staticmethod
117     def add_locator(node, interface, locator_set, priority=1, weight=1):
118         """Configure a new Lisp locator set.
119
120         :param node: Honeycomb node.
121         :param interface: An interface on the node.
122         :param locator_set: Name for the new locator set.
123         :param priority: Priority parameter for the locator.
124         :param weight. Weight parameter for the locator.
125         :type node: dict
126         :type interface: str
127         :type locator_set: str
128         :type priority: int
129         :type weight: int
130         :returns: Content of response.
131         :rtype: bytearray
132         """
133
134         path = "/lisp-feature-data/locator-sets/locator-set" \
135                "/{0}".format(locator_set)
136
137         data = {
138             "locator-set": {
139                 "name": locator_set,
140                 "interface": {
141                     "interface-ref": interface,
142                     "priority": priority,
143                     "weight": weight
144                 }
145             }
146         }
147
148         return LispKeywords._set_lisp_properties(node, path, data)
149
150     @staticmethod
151     def configure_lisp_mapping(node, data):
152         """Modify eid-table configuration to the data provided.
153
154         :param node: Honeycomb node.
155         :param data: Settings for the Lisp mappings.
156         :type node: dict
157         :type data: dict
158         :returns: Content of response.
159         :rtype: bytearray
160         """
161
162         path = "/lisp-feature-data/eid-table"
163         return LispKeywords._set_lisp_properties(node, path, data)
164
165     @staticmethod
166     def add_lisp_adjacency(node, vni_id, map_name, adjacency_name, data):
167         """Add an adjacency to an existing Lisp mapping.
168
169         :param node: Honeycomb node.
170         :param vni_id: vni_id of the mapping.
171         :param map_name: Name of the mapping.
172         :param adjacency_name: Name for the new adjacency.
173         :param data: Adjacency settings.
174         :type node: dict
175         :type vni_id: int
176         :type map_name: str
177         :type adjacency_name: str
178         :type data: dict
179         :returns: Content of response.
180         :rtype: bytearray
181         """
182
183         path = (
184             "/lisp-feature-data/eid-table/vni-table/{vni_id}/"
185             "vrf-subtable/remote-mappings/remote-mapping/{map_name}/"
186             "adjacencies/adjacency/{adjacency_name}"
187         )
188         path = path.format(
189             vni_id=vni_id,
190             map_name=map_name,
191             adjacency_name=adjacency_name
192         )
193
194         return LispKeywords._set_lisp_properties(node, path, data)
195
196     @staticmethod
197     def add_map_resolver(node, ip_address):
198         """Configure map resolver with the specified IP address.
199
200         :param node: Honeycomb node.
201         :param ip_address: IP address to configure map resolver with.
202         :type node: dict
203         :type ip_address: str
204         :returns: Content of response.
205         :rtype: bytearray
206         """
207
208         path = "/lisp-feature-data/map-resolvers/map-resolver/{0}".format(
209             ip_address)
210
211         data = {
212             "map-resolver": {
213                 "ip-address": ip_address
214             }
215         }
216
217         return LispKeywords._set_lisp_properties(node, path, data)
218
219     @staticmethod
220     def delete_map_resolver(node):
221         """Delete an existing map resolver.
222
223         :param node: Honeycomb node
224         :type node: dict
225         :returns: Content of response
226         :rtype: bytearray
227         """
228
229         path = "/lisp-feature-data/map-resolvers"
230
231         return LispKeywords._set_lisp_properties(node, path)
232
233     @staticmethod
234     def configure_pitr(node, locator_set=None):
235         """Configure PITR feature with the specified locator set. If not locator
236         set is specified, disable PITR instead.
237
238         :param node: Honeycomb node.
239         :param locator_set: Name of a locator set. Optional.
240         :type node: dict
241         :type locator_set: str
242         :returns: Content of response.
243         :rtype: bytearray
244         """
245
246         path = "/lisp-feature-data/pitr-cfg"
247
248         if locator_set:
249             data = {
250                 "pitr-cfg": {
251                     "locator-set": locator_set
252                 }
253             }
254         else:
255             data = None
256
257         return LispKeywords._set_lisp_properties(node, path, data)
258
259     @staticmethod
260     def disable_lisp(node):
261         """Remove all Lisp settings on the node.
262
263         :param node: Honeycomb node.
264         :type node: dict
265         :returns: Content of response.
266         :rtype: bytearray
267         """
268
269         return LispKeywords._set_lisp_properties(node, "")