1 # Copyright (c) 2016-2020 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 """Library to set up Lisp in topology."""
16 from enum import IntEnum
18 from ipaddress import ip_address
20 from resources.libraries.python.IPUtil import IPUtil
21 from resources.libraries.python.PapiExecutor import PapiSocketExecutor
24 class EidType(IntEnum):
32 """Class for lisp eid."""
35 def create_eid(eid, prefix_len):
36 """Create lisp eid object.
38 :param eid: Eid value.
39 :param prefix_len: Prefix len if the eid is IP address.
44 prefix=IPUtil.create_prefix_object(ip_address(eid), prefix_len)
45 ) if prefix_len else dict(mac=str(eid))
49 EidType, u"PREFIX" if prefix_len else u"MAC"
55 class LispRemoteLocator:
56 """Class for lisp remote locator."""
59 def create_rloc(ip_addr, priority=0, weight=0):
60 """Create lisp remote locator object.
62 :param ip_addr: IP/IPv6 address.
63 :param priority: Priority of the remote locator.
64 :param weight: Weight of the remote locator.
73 ip_address=ip_address(ip_addr)
79 """Class for lisp API."""
82 def vpp_lisp_enable_disable(node, state):
83 """Enable/Disable lisp in the VPP node in topology.
85 :param node: Node of the test topology.
86 :param state: State of the lisp, enable or disable.
90 args = dict(is_enable=bool(state == u"enable"))
91 cmd = u"lisp_enable_disable"
92 err_msg = f"Failed to set LISP status on host {node[u'host']}"
94 with PapiSocketExecutor(node) as papi_exec:
95 papi_exec.add(cmd, **args).get_reply(err_msg)
98 class LispRemoteMapping:
99 """Class for lisp remote mapping API."""
102 def vpp_add_lisp_remote_mapping(
103 node, vni, deid, deid_prefix, seid, seid_prefix, rloc,
105 """Add lisp remote mapping on the VPP node in topology.
107 :param node: VPP node.
109 :param deid: Destination eid address.
110 :param deid_prefix: Destination eid address prefix_len.
111 :param seid: Source eid address.
112 :param seid_prefix: Source eid address prefix_len.
113 :param rloc: Receiver locator.
114 :param is_mac: Set to True if the deid/seid is MAC address.
118 :type deid_prefix: int
120 :type seid_prefix: int
128 deid=LispEid.create_eid(deid, deid_prefix if not is_mac else None),
129 seid=LispEid.create_eid(seid, seid_prefix if not is_mac else None),
131 rlocs=LispRemoteLocator.create_rloc(rloc)
133 cmd = u"lisp_add_del_remote_mapping"
134 err_msg = f"Failed to add remote mapping on host {node[u'host']}"
136 with PapiSocketExecutor(node) as papi_exec:
137 papi_exec.add(cmd, **args).get_reply(err_msg)
140 def vpp_del_lisp_remote_mapping(
141 node, vni, deid, deid_prefix, seid, seid_prefix, rloc):
142 """Delete lisp remote mapping on the VPP node in topology.
144 :param node: VPP node.
146 :param deid: Destination eid address.
147 :param deid_prefix: Destination eid address prefix_len.
148 :param seid: Source eid address.
149 :param seid_prefix: Source eid address prefix_len.
150 :param rloc: Receiver locator.
154 :type deid_prefix: int
156 :type seid_prefix: int
164 deid=LispEid.create_eid(deid, deid_prefix),
165 seid=LispEid.create_eid(seid, seid_prefix),
167 rlocs=LispRemoteLocator.create_rloc(rloc)
169 cmd = u"lisp_add_del_remote_mapping"
170 err_msg = f"Failed to delete remote mapping on host {node[u'host']}"
172 with PapiSocketExecutor(node) as papi_exec:
173 papi_exec.add(cmd, **args).get_reply(err_msg)
177 """Class for lisp adjacency API."""
180 def vpp_add_lisp_adjacency(
181 node, vni, deid, deid_prefix, seid, seid_prefix, is_mac=False):
182 """Add lisp adjacency on the VPP node in topology.
184 :param node: VPP node.
186 :param deid: Destination eid address.
187 :param deid_prefix: Destination eid address prefix_len.
188 :param seid: Source eid address.
189 :param seid_prefix: Source eid address prefix_len.
190 :param is_mac: Set to True if the deid/seid is MAC address.
194 :type deid_prefix: int
196 :type seid_prefix: int
202 reid=LispEid.create_eid(deid, deid_prefix if not is_mac else None),
203 leid=LispEid.create_eid(seid, seid_prefix if not is_mac else None)
205 cmd = u"lisp_add_del_adjacency"
206 err_msg = f"Failed to add lisp adjacency on host {node[u'host']}"
208 with PapiSocketExecutor(node) as papi_exec:
209 papi_exec.add(cmd, **args).get_reply(err_msg)
212 def vpp_del_lisp_adjacency(
213 node, vni, deid, deid_prefix, seid, seid_prefix):
214 """Delete lisp adjacency on the VPP node in topology.
216 :param node: VPP node.
218 :param deid: Destination eid address.
219 :param deid_prefix: Destination eid address prefix_len.
220 :param seid: Source eid address.
221 :param seid_prefix: Source eid address prefix_len.
225 :type deid_prefix: int
227 :type seid_prefix: int
233 eid=LispEid.create_eid(deid, deid_prefix),
234 leid=LispEid.create_eid(seid, seid_prefix)
236 cmd = u"lisp_add_del_adjacency"
237 err_msg = f"Failed to delete lisp adjacency on host {node[u'host']}"
239 with PapiSocketExecutor(node) as papi_exec:
240 papi_exec.add(cmd, **args).get_reply(err_msg)
244 """Class for LISP GPE status manipulation."""
247 def vpp_lisp_gpe_enable_disable(node, state):
248 """Change the state of LISP GPE - enable or disable.
250 :param node: VPP node.
251 :param state: Requested state - enable or disable.
255 args = dict(is_enable=bool(state == u"enable"))
256 cmd = u"gpe_enable_disable"
257 err_msg = f"Failed to set LISP GPE status on host {node[u'host']}"
259 with PapiSocketExecutor(node) as papi_exec:
260 papi_exec.add(cmd, **args).get_reply(err_msg)
264 """Class for Lisp local eid API."""
267 def vpp_add_lisp_local_eid(
268 node, locator_set_name, vni, eid, prefix_len=None):
269 """Set lisp eid address on the VPP node in topology.
271 :param node: VPP node.
272 :param locator_set_name: Name of the locator_set.
273 :param vni: Vni value.
274 :param eid: Eid value.
275 :param prefix_len: Prefix len if the eid is IP address.
277 :type locator_set_name: str
280 :type prefix_len: int
284 eid=LispEid.create_eid(eid, prefix_len),
285 locator_set_name=locator_set_name,
288 cmd = u"lisp_add_del_local_eid"
289 err_msg = f"Failed to add local eid on host {node[u'host']}"
291 with PapiSocketExecutor(node) as papi_exec:
292 papi_exec.add(cmd, **args).get_reply(err_msg)
295 def vpp_del_lisp_local_eid(
296 node, locator_set_name, vni, eid, prefix_len=None):
297 """Set lisp eid address on the VPP node in topology.
299 :param node: VPP node.
300 :param locator_set_name: Name of the locator_set.
301 :param vni: Vni value.
302 :param eid: Eid value.
303 :param prefix_len: Prefix len if the eid is IP address.
305 :type locator_set_name: str
308 :type prefix_len: int
312 eid=LispEid.create_eid(eid, prefix_len),
313 locator_set_name=locator_set_name,
316 cmd = u"lisp_add_del_local_eid"
317 err_msg = f"Failed to delete local eid on host {node[u'host']}"
319 with PapiSocketExecutor(node) as papi_exec:
320 papi_exec.add(cmd, **args).get_reply(err_msg)
324 """Class for the Lisp Locator API."""
327 def vpp_add_lisp_locator(node, locator_name, sw_if_index, priority, weight):
328 """Set lisp locator on the VPP node in topology.
330 :param node: VPP node.
331 :param locator_name: Name of the locator_set.
332 :param sw_if_index: Sw_if_index if the interface.
333 :param priority: Priority of the locator.
334 :param weight: Weight of the locator.
336 :type locator_name: str
337 :type sw_if_index: int
344 locator_set_name=locator_name,
345 sw_if_index=sw_if_index,
349 cmd = u"lisp_add_del_locator"
350 err_msg = f"Failed to add locator on host {node[u'host']}"
352 with PapiSocketExecutor(node) as papi_exec:
353 papi_exec.add(cmd, **args).get_reply(err_msg)
356 def vpp_del_lisp_locator(node, locator_name, sw_if_index, priority, weight):
357 """Unset lisp locator on the VPP node in topology.
359 :param node: VPP node.
360 :param locator_name: Name of the locator_set.
361 :param sw_if_index: Sw_if_index if the interface.
362 :param priority: Priority of the locator.
363 :param weight: Weight of the locator.
365 :type locator_name: str
366 :type sw_if_index: int
372 locator_set_name=locator_name,
373 sw_if_index=sw_if_index,
377 cmd = u"lisp_add_del_locator"
378 err_msg = f"Failed to delete locator on host {node[u'host']}"
380 with PapiSocketExecutor(node) as papi_exec:
381 papi_exec.add(cmd, **args).get_reply(err_msg)
384 class LispLocatorSet:
385 """Class for Lisp Locator Set API."""
388 def vpp_add_lisp_locator_set(node, name):
389 """Add lisp locator_set on VPP.
391 :param node: VPP node.
392 :param name: VPP locator name.
398 locator_set_name=name,
402 cmd = u"lisp_add_del_locator_set"
403 err_msg = f"Failed to add locator set on host {node[u'host']}"
405 with PapiSocketExecutor(node) as papi_exec:
406 papi_exec.add(cmd, **args).get_reply(err_msg)
409 def vpp_del_lisp_locator_set(node, name):
410 """Del lisp locator_set on VPP.
412 :param node: VPP node.
413 :param name: VPP locator name.
419 locator_set_name=name,
423 cmd = u"lisp_add_del_locator_set"
424 err_msg = f"Failed to delete locator set on host {node[u'host']}"
426 with PapiSocketExecutor(node) as papi_exec:
427 papi_exec.add(cmd, **args).get_reply(err_msg)
430 class LispEidTableMap:
432 Class for EID table map.
436 def vpp_lisp_eid_table_mapping(node, vni, bd_id=None, vrf=None):
438 Map LISP VNI to either bridge domain ID, or VRF ID.
440 :param node: VPP node.
441 :param vni: Lisp VNI.
442 :param bd_id: Bridge domain ID.
449 # adding default mapping vni=0, vrf=0 needs to be skipped
450 if bd_id is not None or int(vrf if vrf else 0) or int(vni):
454 dp_table=int(bd_id) if bd_id is not None else int(vrf),
455 is_l2=bool(bd_id is not None)
457 cmd = u"lisp_eid_table_add_del_map"
458 err_msg = f"Failed to add eid table map on host {node[u'host']}"
460 with PapiSocketExecutor(node) as papi_exec:
461 papi_exec.add(cmd, **args).get_reply(err_msg)