8c124d194c45aab7bba4f77e93f152509035a710
[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 verify_map_server_data_from_honeycomb(data, ip_addresses):
98         """Verify whether MAP server data from Honeycomb is correct.
99
100         :param data: Lisp operational data containing map server IP addresses.
101         :param ip_addresses: IP addresses to verify map server data against.
102         :type data: dict
103         :type ip_addresses: list
104         :returns: Boolean Value indicating equality of IP Lists.
105         :rtype: bool
106         """
107
108         data =\
109             data['lisp-state']['lisp-feature-data']['map-servers']['map-server']
110
111         data = sorted([entry['ip-address'] for entry in data])
112         ip_addresses.sort()
113
114         return data == ip_addresses
115
116     @staticmethod
117     def verify_map_server_data_from_vat(data, ip_addresses):
118         """Verify whether MAP server data from VAT is correct.
119
120         :param data: Lisp operational data containing map server IP addresses.
121         :param ip_addresses: IP addresses to verify map server data against.
122         :type data: dict
123         :type ip_addresses: list
124         :returns: Boolean Value indicating equality of IP Lists.
125         :rtype: bool
126         """
127
128         data = sorted([entry['map-server'] for entry in data])
129         ip_addresses.sort()
130
131         return data == ip_addresses
132
133     @staticmethod
134     def set_lisp_state(node, state=True):
135         """Enable or disable the Lisp feature.
136
137         :param node: Honeycomb node.
138         :param state: Enable or disable Lisp.
139         :type node: dict
140         :type state: bool
141         :returns: Content of response.
142         :rtype: bytearray
143         :raises HoneycombError: If the return code is not 200:OK
144         or 404:NOT FOUND.
145         """
146
147         ret_code, data = HcUtil.get_honeycomb_data(node, "config_lisp")
148         if ret_code == HTTPCodes.OK:
149             data["lisp"]["enable"] = bool(state)
150         elif ret_code == HTTPCodes.NOT_FOUND:
151                 data = {"lisp": {"enable": bool(state)}}
152         else:
153             raise HoneycombError("Unexpected return code when getting existing"
154                                  " Lisp configuration.")
155
156         return LispKeywords._set_lisp_properties(node, '', data)
157
158     @staticmethod
159     def set_rloc_probe_state(node, state=False):
160         """Enable or disable the Routing Locator probe.
161
162         :param node: Honeycomb node.
163         :param state: Enable or Disable the Rloc probe.
164         :type node: dict
165         :type state: bool
166         :returns: Content of response.
167         :rtype: bytearray
168         """
169
170         path = "/lisp-feature-data/rloc-probe"
171
172         data = {
173             "rloc-probe": {
174                 "enabled": bool(state)
175             }
176         }
177
178         return LispKeywords._set_lisp_properties(node, path, data)
179
180     @staticmethod
181     def add_locator(node, interface, locator_set, priority=1, weight=1):
182         """Configure a new Lisp locator set.
183
184         :param node: Honeycomb node.
185         :param interface: An interface on the node.
186         :param locator_set: Name for the new locator set.
187         :param priority: Priority parameter for the locator.
188         :param weight. Weight parameter for the locator.
189         :type node: dict
190         :type interface: str
191         :type locator_set: str
192         :type priority: int
193         :type weight: int
194         :returns: Content of response.
195         :rtype: bytearray
196         """
197
198         path = "/lisp-feature-data/locator-sets/locator-set" \
199                "/{0}".format(locator_set)
200
201         data = {
202             "locator-set": {
203                 "name": locator_set,
204                 "interface": {
205                     "interface-ref": interface,
206                     "priority": priority,
207                     "weight": weight
208                 }
209             }
210         }
211
212         return LispKeywords._set_lisp_properties(node, path, data)
213
214     @staticmethod
215     def configure_lisp_mapping(node, data):
216         """Modify eid-table configuration to the data provided.
217
218         :param node: Honeycomb node.
219         :param data: Settings for the Lisp mappings.
220         :type node: dict
221         :type data: dict
222         :returns: Content of response.
223         :rtype: bytearray
224         """
225
226         path = "/lisp-feature-data/eid-table"
227         return LispKeywords._set_lisp_properties(node, path, data)
228
229     @staticmethod
230     def configure_lisp_map_request_mode(node, option):
231         """Modify Lisp Map Request Mode configuration to the data provided.
232
233         :param node: Honeycomb node.
234         :param option: Settings for the Lisp map request mode.
235         :type node: dict
236         :type option: str
237         :returns: Content of response.
238         :rtype: bytearray
239         """
240
241         data = {
242             "map-request-mode": {
243                 "mode": option
244             }
245         }
246
247         path = "/lisp-feature-data/map-request-mode"
248         return LispKeywords._set_lisp_properties(node, path, data)
249
250     @staticmethod
251     def add_lisp_adjacency(node, vni_id, map_name, adjacency_name, data):
252         """Add an adjacency to an existing Lisp mapping.
253
254         :param node: Honeycomb node.
255         :param vni_id: vni_id of the mapping.
256         :param map_name: Name of the mapping.
257         :param adjacency_name: Name for the new adjacency.
258         :param data: Adjacency settings.
259         :type node: dict
260         :type vni_id: int
261         :type map_name: str
262         :type adjacency_name: str
263         :type data: dict
264         :returns: Content of response.
265         :rtype: bytearray
266         """
267
268         path = (
269             "/lisp-feature-data/eid-table/vni-table/{vni_id}/"
270             "vrf-subtable/remote-mappings/remote-mapping/{map_name}/"
271             "adjacencies/adjacency/{adjacency_name}"
272         )
273         path = path.format(
274             vni_id=vni_id,
275             map_name=map_name,
276             adjacency_name=adjacency_name
277         )
278
279         return LispKeywords._set_lisp_properties(node, path, data)
280
281     @staticmethod
282     def add_map_resolver(node, ip_address):
283         """Configure map resolver with the specified IP address.
284
285         :param node: Honeycomb node.
286         :param ip_address: IP address to configure map resolver with.
287         :type node: dict
288         :type ip_address: str
289         :returns: Content of response.
290         :rtype: bytearray
291         """
292
293         path = "/lisp-feature-data/map-resolvers/map-resolver/{0}".format(
294             ip_address)
295
296         data = {
297             "map-resolver": {
298                 "ip-address": ip_address
299             }
300         }
301
302         return LispKeywords._set_lisp_properties(node, path, data)
303
304     @staticmethod
305     def set_map_register(node, map_register=False):
306         """Configure Map Register.
307
308         :param node: Honeycomb node.
309         :param map_register: Enable or disable Map Register.
310         :type node: dict
311         :type map_register: bool
312         :returns: Content of response.
313         :rtype: bytearray
314         """
315
316         path = "/lisp-feature-data/map-register"
317
318         data = {
319             "map-register": {
320                 "enabled": bool(map_register)
321             }
322         }
323
324         return LispKeywords._set_lisp_properties(node, path, data)
325
326     @staticmethod
327     def set_map_request_mode(node, src_dst=False):
328         """Configure Map Request Mode.
329
330         :param node: Honeycomb node.
331         :param src_dst: Configure Map Request Mode with source destination.
332         :type node: dict
333         :type src_dst: bool
334         :returns: Content of response.
335         :rtype: bytearray
336         """
337
338         path = "/lisp-feature-data/map-request-mode"
339
340         data = {
341             "map-request-mode": {
342                 "mode": "source-destination" if src_dst
343                 else "target-destination"
344             }
345         }
346
347         return LispKeywords._set_lisp_properties(node, path, data)
348
349     @staticmethod
350     def delete_map_resolver(node):
351         """Delete an existing map resolver.
352
353         :param node: Honeycomb node
354         :type node: dict
355         :returns: Content of response
356         :rtype: bytearray
357         """
358
359         path = "/lisp-feature-data/map-resolvers"
360
361         return LispKeywords._set_lisp_properties(node, path)
362
363     @staticmethod
364     def add_map_server(node, *ip_addresses):
365         """Configure map server with the specified IP addresses.
366
367         :param node: Honeycomb node.
368         :param ip_addresses: IP addresses to configure map server with.
369         :type node: dict
370         :type ip_addresses: list
371         :returns: Content of response.
372         :rtype: bytearray
373         """
374
375         path = "/lisp-feature-data/map-servers"
376
377         data = {
378             "map-servers": {
379                 "map-server": [
380                     {"ip-address": ip_address} for ip_address in ip_addresses
381                 ]
382             }
383         }
384
385         return LispKeywords._set_lisp_properties(node, path, data)
386
387     @staticmethod
388     def delete_map_server(node):
389         """Delete all map servers.
390
391         :param node: Honeycomb node
392         :type node: dict
393         :returns: Content of response
394         :rtype: bytearray
395         """
396
397         path = "/lisp-feature-data/map-servers"
398
399         return LispKeywords._set_lisp_properties(node, path)
400
401     @staticmethod
402     def configure_pitr(node, locator_set=None):
403         """Configure PITR feature with the specified locator set. If not locator
404         set is specified, disable PITR instead.
405
406         :param node: Honeycomb node.
407         :param locator_set: Name of a locator set. Optional.
408         :type node: dict
409         :type locator_set: str
410         :returns: Content of response.
411         :rtype: bytearray
412         """
413
414         path = "/lisp-feature-data/pitr-cfg"
415
416         if locator_set:
417             data = {
418                 "pitr-cfg": {
419                     "locator-set": locator_set
420                 }
421             }
422         else:
423             data = None
424
425         return LispKeywords._set_lisp_properties(node, path, data)
426
427     @staticmethod
428     def configure_petr(node, ip_address):
429         """Configure PETR feature with the specified IP. If no IP
430         specified, disable PETR instead.
431
432         :param node: Honeycomb node.
433         :param ip_address: IPv6 address.
434         :type node: dict
435         :type ip_address: str
436         :returns: Content of response.
437         :rtype: bytearray
438         """
439
440         path = "/lisp-feature-data/petr-cfg"
441
442         if ip_address:
443             data = {
444                 "petr-cfg": {
445                     "petr-address": ip_address
446                 }
447             }
448         else:
449             data = None
450
451         return LispKeywords._set_lisp_properties(node, path, data)
452
453     @staticmethod
454     def disable_lisp(node):
455         """Remove all Lisp settings on the node.
456
457         :param node: Honeycomb node.
458         :type node: dict
459         :returns: Content of response.
460         :rtype: bytearray
461         """
462
463         return LispKeywords._set_lisp_properties(node, "")