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:
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 """This module implements keywords to manipulate Lisp data structures using
15 Honeycomb REST API."""
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
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
40 def _set_lisp_properties(node, path, data=None):
41 """Set Lisp properties and check the return code.
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.
49 :returns: Content of response.
51 :raises HoneycombError: If the status code in response to PUT is not
52 200 = OK or 201 = ACCEPTED.
56 status_code, resp = HcUtil.\
57 put_honeycomb_data(node, "config_lisp", data, path,
58 data_representation=DataRepresentation.JSON)
60 status_code, resp = HcUtil.\
61 delete_honeycomb_data(node, "config_lisp", path)
63 if status_code not in (HTTPCodes.OK, HTTPCodes.ACCEPTED):
65 "Lisp configuration unsuccessful. "
66 "Status code: {0}.".format(status_code))
71 def get_lisp_operational_data(node):
72 """Retrieve Lisp properties from Honeycomb operational data.
74 :param node: Honeycomb node.
76 :returns: List operational data.
80 status_code, resp = HcUtil.get_honeycomb_data(node, "oper_lisp")
82 if status_code != HTTPCodes.OK:
83 raise HoneycombError("Could not retrieve Lisp operational data."
84 "Status code: {0}.".format(status_code))
86 # get rid of empty vni-table entry
87 resp["lisp-state"]["lisp-feature-data"]["eid-table"][
90 "virtual-network-identifier": 0,
91 "vrf-subtable": {"table-id": 0}
97 def verify_map_server_data_from_honeycomb(data, ip_addresses):
98 """Verify whether MAP server data from Honeycomb is correct.
100 :param data: Lisp operational data containing map server IP addresses.
101 :param ip_addresses: IP addresses to verify map server data against.
103 :type ip_addresses: list
104 :returns: Boolean Value indicating equality of IP Lists.
109 data['lisp-state']['lisp-feature-data']['map-servers']['map-server']
111 data = sorted([entry['ip-address'] for entry in data])
114 return data == ip_addresses
117 def verify_map_server_data_from_vat(data, ip_addresses):
118 """Verify whether MAP server data from VAT is correct.
120 :param data: Lisp operational data containing map server IP addresses.
121 :param ip_addresses: IP addresses to verify map server data against.
123 :type ip_addresses: list
124 :returns: Boolean Value indicating equality of IP Lists.
128 data = sorted([entry['map-server'] for entry in data])
131 return data == ip_addresses
134 def set_lisp_state(node, state=True):
135 """Enable or disable the Lisp feature.
137 :param node: Honeycomb node.
138 :param state: Enable or disable Lisp.
141 :returns: Content of response.
143 :raises HoneycombError: If the return code is not 200:OK
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)}}
153 raise HoneycombError("Unexpected return code when getting existing"
154 " Lisp configuration.")
156 return LispKeywords._set_lisp_properties(node, '', data)
159 def set_rloc_probe_state(node, state=False):
160 """Enable or disable the Routing Locator probe.
162 :param node: Honeycomb node.
163 :param state: Enable or Disable the Rloc probe.
166 :returns: Content of response.
170 path = "/lisp-feature-data/rloc-probe"
174 "enabled": bool(state)
178 return LispKeywords._set_lisp_properties(node, path, data)
181 def add_locator(node, interface, locator_set, priority=1, weight=1):
182 """Configure a new Lisp locator set.
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.
191 :type locator_set: str
194 :returns: Content of response.
198 path = "/lisp-feature-data/locator-sets/locator-set" \
199 "/{0}".format(locator_set)
205 "interface-ref": interface,
206 "priority": priority,
212 return LispKeywords._set_lisp_properties(node, path, data)
215 def configure_lisp_mapping(node, data):
216 """Modify eid-table configuration to the data provided.
218 :param node: Honeycomb node.
219 :param data: Settings for the Lisp mappings.
222 :returns: Content of response.
226 path = "/lisp-feature-data/eid-table"
227 return LispKeywords._set_lisp_properties(node, path, data)
230 def configure_lisp_map_request_mode(node, option):
231 """Modify Lisp Map Request Mode configuration to the data provided.
233 :param node: Honeycomb node.
234 :param option: Settings for the Lisp map request mode.
237 :returns: Content of response.
242 "map-request-mode": {
247 path = "/lisp-feature-data/map-request-mode"
248 return LispKeywords._set_lisp_properties(node, path, data)
251 def add_lisp_adjacency(node, vni_id, map_name, adjacency_name, data):
252 """Add an adjacency to an existing Lisp mapping.
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.
262 :type adjacency_name: str
264 :returns: Content of response.
269 "/lisp-feature-data/eid-table/vni-table/{vni_id}/"
270 "vrf-subtable/remote-mappings/remote-mapping/{map_name}/"
271 "adjacencies/adjacency/{adjacency_name}"
276 adjacency_name=adjacency_name
279 return LispKeywords._set_lisp_properties(node, path, data)
282 def add_map_resolver(node, ip_address):
283 """Configure map resolver with the specified IP address.
285 :param node: Honeycomb node.
286 :param ip_address: IP address to configure map resolver with.
288 :type ip_address: str
289 :returns: Content of response.
293 path = "/lisp-feature-data/map-resolvers/map-resolver/{0}".format(
298 "ip-address": ip_address
302 return LispKeywords._set_lisp_properties(node, path, data)
305 def set_map_register(node, map_register=False):
306 """Configure Map Register.
308 :param node: Honeycomb node.
309 :param map_register: Enable or disable Map Register.
311 :type map_register: bool
312 :returns: Content of response.
316 path = "/lisp-feature-data/map-register"
320 "enabled": bool(map_register)
324 return LispKeywords._set_lisp_properties(node, path, data)
327 def set_map_request_mode(node, src_dst=False):
328 """Configure Map Request Mode.
330 :param node: Honeycomb node.
331 :param src_dst: Configure Map Request Mode with source destination.
334 :returns: Content of response.
338 path = "/lisp-feature-data/map-request-mode"
341 "map-request-mode": {
342 "mode": "source-destination" if src_dst
343 else "target-destination"
347 return LispKeywords._set_lisp_properties(node, path, data)
350 def delete_map_resolver(node):
351 """Delete an existing map resolver.
353 :param node: Honeycomb node
355 :returns: Content of response
359 path = "/lisp-feature-data/map-resolvers"
361 return LispKeywords._set_lisp_properties(node, path)
364 def add_map_server(node, *ip_addresses):
365 """Configure map server with the specified IP addresses.
367 :param node: Honeycomb node.
368 :param ip_addresses: IP addresses to configure map server with.
370 :type ip_addresses: list
371 :returns: Content of response.
375 path = "/lisp-feature-data/map-servers"
380 {"ip-address": ip_address} for ip_address in ip_addresses
385 return LispKeywords._set_lisp_properties(node, path, data)
388 def delete_map_server(node):
389 """Delete all map servers.
391 :param node: Honeycomb node
393 :returns: Content of response
397 path = "/lisp-feature-data/map-servers"
399 return LispKeywords._set_lisp_properties(node, path)
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.
406 :param node: Honeycomb node.
407 :param locator_set: Name of a locator set. Optional.
409 :type locator_set: str
410 :returns: Content of response.
414 path = "/lisp-feature-data/pitr-cfg"
419 "locator-set": locator_set
425 return LispKeywords._set_lisp_properties(node, path, data)
428 def configure_petr(node, ip_address):
429 """Configure PETR feature with the specified IP. If no IP
430 specified, disable PETR instead.
432 :param node: Honeycomb node.
433 :param ip_address: IPv6 address.
435 :type ip_address: str
436 :returns: Content of response.
440 path = "/lisp-feature-data/petr-cfg"
445 "petr-address": ip_address
451 return LispKeywords._set_lisp_properties(node, path, data)
454 def disable_lisp(node):
455 """Remove all Lisp settings on the node.
457 :param node: Honeycomb node.
459 :returns: Content of response.
463 return LispKeywords._set_lisp_properties(node, "")