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 set_lisp_state(node, state):
98 """Enable or disable the Lisp feature.
100 :param node: Honeycomb node.
101 :param state: Desired Lisp state, enable or disable.
104 :returns: Content of response.
110 "enable": True if state.lower() == "enable" else False
114 return LispKeywords._set_lisp_properties(node, '', data)
117 def add_locator(node, interface, locator_set, priority=1, weight=1):
118 """Configure a new Lisp locator set.
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.
127 :type locator_set: str
130 :returns: Content of response.
134 path = "/lisp-feature-data/locator-sets/locator-set" \
135 "/{0}".format(locator_set)
141 "interface-ref": interface,
142 "priority": priority,
148 return LispKeywords._set_lisp_properties(node, path, data)
151 def configure_lisp_mapping(node, data):
152 """Modify eid-table configuration to the data provided.
154 :param node: Honeycomb node.
155 :param data: Settings for the Lisp mappings.
158 :returns: Content of response.
162 path = "/lisp-feature-data/eid-table"
163 return LispKeywords._set_lisp_properties(node, path, data)
166 def add_lisp_adjacency(node, vni_id, map_name, adjacency_name, data):
167 """Add an adjacency to an existing Lisp mapping.
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.
177 :type adjacency_name: str
179 :returns: Content of response.
184 "/lisp-feature-data/eid-table/vni-table/{vni_id}/"
185 "vrf-subtable/remote-mappings/remote-mapping/{map_name}/"
186 "adjacencies/adjacency/{adjacency_name}"
191 adjacency_name=adjacency_name
194 return LispKeywords._set_lisp_properties(node, path, data)
197 def add_map_resolver(node, ip_address):
198 """Configure map resolver with the specified IP address.
200 :param node: Honeycomb node.
201 :param ip_address: IP address to configure map resolver with.
203 :type ip_address: str
204 :returns: Content of response.
208 path = "/lisp-feature-data/map-resolvers/map-resolver/{0}".format(
213 "ip-address": ip_address
217 return LispKeywords._set_lisp_properties(node, path, data)
220 def delete_map_resolver(node):
221 """Delete an existing map resolver.
223 :param node: Honeycomb node
225 :returns: Content of response
229 path = "/lisp-feature-data/map-resolvers"
231 return LispKeywords._set_lisp_properties(node, path)
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.
238 :param node: Honeycomb node.
239 :param locator_set: Name of a locator set. Optional.
241 :type locator_set: str
242 :returns: Content of response.
246 path = "/lisp-feature-data/pitr-cfg"
251 "locator-set": locator_set
257 return LispKeywords._set_lisp_properties(node, path, data)
260 def disable_lisp(node):
261 """Remove all Lisp settings on the node.
263 :param node: Honeycomb node.
265 :returns: Content of response.
269 return LispKeywords._set_lisp_properties(node, "")