1 # Copyright (c) 2016-2019 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 ipaddress import ip_address
18 from resources.libraries.python.topology import NodeType
19 from resources.libraries.python.PapiExecutor import PapiSocketExecutor
20 from resources.libraries.python.L2Util import L2Util
22 class LispStatus(object):
23 """Class for lisp API."""
29 def vpp_lisp_enable_disable(node, state):
30 """Enable/Disable lisp in the VPP node in topology.
32 :param node: Node of the test topology.
33 :param state: State of the lisp, enable or disable.
38 args = dict(is_en=0 if state == 'disable' else 1)
40 cmd = 'lisp_enable_disable'
41 err_msg = "Failed to set LISP status on host {host}".format(
44 with PapiSocketExecutor(node) as papi_exec:
45 papi_exec.add(cmd, **args).get_reply(err_msg)
47 class LispRemoteMapping(object):
48 """Class for lisp remote mapping API."""
54 def vpp_add_lisp_remote_mapping(node, vni, deid, deid_prefix, seid,
55 seid_prefix, rloc, is_mac=False):
56 """Add lisp remote mapping on the VPP node in topology.
58 :param node: VPP node.
60 :param deid: Destination eid address.
61 :param deid_prefix: Destination eid address prefix_len.
62 :param seid: Source eid address.
63 :param seid_prefix: Source eid address prefix_len.
64 :param rloc: Receiver locator.
65 :param is_mac: Set to True if the deid/seid is MAC address.
69 :type deid_prefix: int
71 :type seid_prefix: int
77 eid_type = 0 if ip_address(unicode(deid)).version == 4 else 1
78 eid_packed = ip_address(unicode(deid)).packed
79 seid_packed = ip_address(unicode(seid)).packed
81 seid_len = seid_prefix
84 eid_packed = L2Util.mac_to_bin(deid)
85 seid_packed = L2Util.mac_to_bin(seid)
89 rlocs = [dict(is_ip4=1 if ip_address(unicode(rloc)).version == 4 else 0,
90 addr=ip_address(unicode(rloc)).packed)]
103 cmd = 'lisp_add_del_remote_mapping'
104 err_msg = "Failed to add remote mapping on host {host}".format(
107 with PapiSocketExecutor(node) as papi_exec:
108 papi_exec.add(cmd, **args).get_reply(err_msg)
111 def vpp_del_lisp_remote_mapping(node, vni, deid, deid_prefix, seid,
113 """Delete lisp remote mapping on the VPP node in topology.
115 :param node: VPP node.
117 :param deid: Destination eid address.
118 :param deid_prefix: Destination eid address prefix_len.
119 :param seid: Source eid address.
120 :param seid_prefix: Source eid address prefix_len.
121 :param rloc: Receiver locator.
125 :type deid_prefix: int
127 :type seid_prefix: int
135 eid_type = 0 if ip_address(unicode(deid)).version == 4 else 1
136 eid_packed = ip_address(unicode(deid)).packed
137 seid_packed = ip_address(unicode(seid)).packed
138 eid_len = deid_prefix
139 seid_len = seid_prefix
142 eid_packed = L2Util.mac_to_bin(deid)
143 seid_packed = L2Util.mac_to_bin(seid)
147 rlocs = [dict(is_ip4=1 if ip_address(unicode(rloc)).version == 4 else 0,
148 addr=ip_address(unicode(rloc)).packed)]
150 args = dict(is_add=0,
161 cmd = 'lisp_add_del_remote_mapping'
162 err_msg = "Failed to delete remote mapping on host {host}".format(
165 with PapiSocketExecutor(node) as papi_exec:
166 papi_exec.add(cmd, **args).get_reply(err_msg)
168 class LispAdjacency(object):
169 """Class for lisp adjacency API."""
175 def vpp_add_lisp_adjacency(node, vni, deid, deid_prefix, seid,
176 seid_prefix, is_mac=False):
177 """Add lisp adjacency on the VPP node in topology.
179 :param node: VPP node.
181 :param deid: Destination eid address.
182 :param deid_prefix: Destination eid address prefix_len.
183 :param seid: Source eid address.
184 :param seid_prefix: Source eid address prefix_len.
185 :param is_mac: Set to True if the deid/seid is MAC address.
189 :type deid_prefix: int
191 :type seid_prefix: int
196 eid_type = 0 if ip_address(unicode(deid)).version == 4 else 1
197 reid = ip_address(unicode(deid)).packed
198 leid = ip_address(unicode(seid)).packed
199 reid_len = deid_prefix
200 leid_len = seid_prefix
203 reid = L2Util.mac_to_bin(deid)
204 leid = L2Util.mac_to_bin(seid)
208 args = dict(is_add=1,
216 cmd = 'lisp_add_del_adjacency'
217 err_msg = "Failed to add lisp adjacency on host {host}".format(
220 with PapiSocketExecutor(node) as papi_exec:
221 papi_exec.add(cmd, **args).get_reply(err_msg)
224 def vpp_del_lisp_adjacency(node, vni, deid, deid_prefix, seid,
226 """Delete lisp adjacency on the VPP node in topology.
228 :param node: VPP node.
230 :param deid: Destination eid address.
231 :param deid_prefix: Destination eid address prefix_len.
232 :param seid: Source eid address.
233 :param seid_prefix: Source eid address prefix_len.
237 :type deid_prefix: int
239 :type seid_prefix: int
246 eid_type = 0 if ip_address(unicode(deid)).version == 4 else 1
247 reid = ip_address(unicode(deid)).packed
248 leid = ip_address(unicode(seid)).packed
249 reid_len = deid_prefix
250 leid_len = seid_prefix
253 reid = L2Util.mac_to_bin(deid)
254 leid = L2Util.mac_to_bin(seid)
258 args = dict(is_add=0,
266 cmd = 'lisp_add_del_adjacency'
267 err_msg = "Failed to delete lisp adjacency on host {host}".format(
270 with PapiSocketExecutor(node) as papi_exec:
271 papi_exec.add(cmd, **args).get_reply(err_msg)
273 class LispGpeStatus(object):
274 """Clas for LISP GPE status manipulation."""
280 def vpp_lisp_gpe_enable_disable(node, state):
281 """Change the state of LISP GPE - enable or disable.
283 :param node: VPP node.
284 :param state: Requested state - enable or disable.
289 args = dict(is_en=0 if state == 'disable' else 1)
291 cmd = 'gpe_enable_disable'
292 err_msg = "Failed to set LISP GPE status on host {host}".format(
295 with PapiSocketExecutor(node) as papi_exec:
296 papi_exec.add(cmd, **args).get_reply(err_msg)
298 class LispGpeForwardEntry(object):
299 """The functionality needed for these methods is not implemented in VPP
300 (VAT). Bug https://jira.fd.io/browse/VPP-334 was open to cover this issue.
302 TODO: Implement when VPP-334 is fixed.
309 def add_lisp_gpe_forward_entry(node, *args):
310 """Not implemented"""
311 # TODO: Implement when VPP-334 is fixed.
315 def del_lisp_gpe_forward_entry(node, *args):
316 """Not implemented"""
317 # TODO: Implement when VPP-334 is fixed.
321 class LispMapResolver(object):
322 """Class for Lisp map resolver API."""
328 def vpp_add_map_resolver(node, map_resolver_ip):
329 """Set lisp map resolver on the VPP node in topology.
331 :param node: VPP node.
332 :param map_resolver_ip: IP address of the map resolver.
334 :type map_resolver_ip: str
337 args = dict(is_add=1,
338 is_ipv6=0 if ip_address(unicode(map_resolver_ip)).version \
340 ip_address=ip_address(unicode(map_resolver_ip)).packed)
342 cmd = 'lisp_add_del_map_resolver'
343 err_msg = "Failed to add map resolver on host {host}".format(
346 with PapiSocketExecutor(node) as papi_exec:
347 papi_exec.add(cmd, **args).get_reply(err_msg)
350 def vpp_del_map_resolver(node, map_resolver_ip):
351 """Unset lisp map resolver on the VPP node in topology.
353 :param node: VPP node.
354 :param map_resolver_ip: IP address of the map resolver.
356 :type map_resolver_ip: str
359 args = dict(is_add=0,
360 is_ipv6=0 if ip_address(unicode(map_resolver_ip)).version \
362 ip_address=ip_address(unicode(map_resolver_ip)).packed)
364 cmd = 'lisp_add_del_map_resolver'
365 err_msg = "Failed to delete map resolver on host {host}".format(
368 with PapiSocketExecutor(node) as papi_exec:
369 papi_exec.add(cmd, **args).get_reply(err_msg)
371 class LispLocalEid(object):
372 """Class for Lisp local eid API."""
378 def vpp_add_lisp_local_eid(node, locator_set_name, vni, eid,
380 """Set lisp eid address on the VPP node in topology.
382 :param node: VPP node.
383 :param locator_set_name: Name of the locator_set.
384 :param vni: vni value.
385 :param eid: Eid value.
386 :param prefix_len: prefix len if the eid is IP address.
388 :type locator_set_name: str
391 :type prefix_len: int
395 eid_type = 0 if ip_address(unicode(eid)).version == 4 else 1
396 eid_packed = ip_address(unicode(eid)).packed
399 eid_packed = L2Util.mac_to_bin(eid)
401 args = dict(is_add=1,
404 prefix_len=prefix_len,
405 locator_set_name=locator_set_name,
408 cmd = 'lisp_add_del_local_eid'
409 err_msg = "Failed to add local eid on host {host}".format(
412 with PapiSocketExecutor(node) as papi_exec:
413 papi_exec.add(cmd, **args).get_reply(err_msg)
416 def vpp_del_lisp_local_eid(node, locator_set_name, vni, eid,
418 """Set lisp eid addres on the VPP node in topology.
420 :param node: VPP node.
421 :param locator_set_name: Name of the locator_set.
422 :param vni: vni value.
423 :param eid: Eid value.
424 :param prefix_len: prefix len if the eid is IP address.
426 :type locator_set_name: str
429 :type prefix_len: int
433 eid_type = 0 if ip_address(unicode(eid)).version == 4 else 1
434 eid_packed = ip_address(unicode(eid)).packed
437 eid_packed = L2Util.mac_to_bin(eid)
439 args = dict(is_add=0,
442 prefix_len=prefix_len,
443 locator_set_name=locator_set_name,
446 cmd = 'lisp_add_del_local_eid'
447 err_msg = "Failed to delete local eid on host {host}".format(
450 with PapiSocketExecutor(node) as papi_exec:
451 papi_exec.add(cmd, **args).get_reply(err_msg)
453 class LispLocator(object):
454 """Class for the Lisp Locator API."""
460 def vpp_add_lisp_locator(node, locator_name, sw_if_index, priority, weight):
461 """Set lisp locator on the VPP node in topology.
463 :param node: VPP node.
464 :param locator_name: Name of the locator_set.
465 :param sw_if_index: sw_if_index if the interface.
466 :param priority: priority of the locator.
467 :param weight: weight of the locator.
469 :type locator_name: str
470 :type sw_if_index: int
475 args = dict(is_add=1,
476 locator_set_name=locator_name,
477 sw_if_index=sw_if_index,
481 cmd = 'lisp_add_del_locator'
482 err_msg = "Failed to add locator on host {host}".format(
485 with PapiSocketExecutor(node) as papi_exec:
486 papi_exec.add(cmd, **args).get_reply(err_msg)
489 def vpp_del_lisp_locator(node, locator_name, sw_if_index, priority, weight):
490 """Unset lisp locator on the VPP node in topology.
492 :param node: VPP node.
493 :param locator_name: Name of the locator_set.
494 :param sw_if_index: sw_if_index if the interface.
495 :param priority: priority of the locator.
496 :param weight: weight of the locator.
498 :type locator_name: str
499 :type sw_if_index: int
504 args = dict(is_add=0,
505 locator_set_name=locator_name,
506 sw_if_index=sw_if_index,
510 cmd = 'lisp_add_del_locator'
511 err_msg = "Failed to delete locator on host {host}".format(
514 with PapiSocketExecutor(node) as papi_exec:
515 papi_exec.add(cmd, **args).get_reply(err_msg)
517 class LispLocatorSet(object):
518 """Class for Lisp Locator Set API."""
524 def vpp_add_lisp_locator_set(node, name):
525 """Add lisp locator_set on VPP.
527 :param node: VPP node.
528 :param name: VPP locator name.
533 args = dict(is_add=1,
534 locator_set_name=name,
538 cmd = 'lisp_add_del_locator_set'
539 err_msg = "Failed to add locator set on host {host}".format(
542 with PapiSocketExecutor(node) as papi_exec:
543 papi_exec.add(cmd, **args).get_reply(err_msg)
546 def vpp_del_lisp_locator_set(node, name):
547 """Del lisp locator_set on VPP.
549 :param node: VPP node.
550 :param name: VPP locator name.
555 args = dict(is_add=0,
556 locator_set_name=name,
560 cmd = 'lisp_add_del_locator_set'
561 err_msg = "Failed to delete locator set on host {host}".format(
564 with PapiSocketExecutor(node) as papi_exec:
565 papi_exec.add(cmd, **args).get_reply(err_msg)
567 class LispSetup(object):
568 """Lisp setup in topology."""
574 def vpp_set_lisp_locator_set(node, locator_set_list):
575 """Set lisp locator_sets on VPP node in topology.
577 :param node: VPP node.
578 :param locator_set_list: List of locator_set.
580 :type locator_set_list: list
583 if node['type'] != NodeType.DUT:
584 raise ValueError('Node is not DUT')
586 lisp_locator = LispLocator()
587 lisp_locator_set = LispLocatorSet()
588 for locator_set in locator_set_list:
589 locator_set_name = locator_set.get('locator-set')
590 locator_list = locator_set.get('locator')
591 lisp_locator_set.vpp_add_lisp_locator_set(node,
593 for locator in locator_list:
594 sw_if_index = locator.get('locator-index')
595 priority = locator.get('priority')
596 weight = locator.get('weight')
597 lisp_locator.vpp_add_lisp_locator(node,
604 def vpp_unset_lisp_locator_set(node, locator_set_list):
605 """Unset lisp locator_sets on VPP node in topology.
607 :param node: VPP node.
608 :param locator_set_list: List of locator_set.
610 :type locator_set_list: list
613 if node['type'] != NodeType.DUT:
614 raise ValueError('Lisp locator set, node is not DUT')
616 lisp_locator = LispLocator()
617 lisp_locator_set = LispLocatorSet()
618 for locator_set in locator_set_list:
619 locator_set_name = locator_set.get('locator-set')
620 locator_list = locator_set.get('locator')
621 for locator in locator_list:
622 sw_if_index = locator.get('locator-index')
623 priority = locator.get('priority')
624 weight = locator.get('weight')
625 lisp_locator.vpp_del_lisp_locator(node,
631 lisp_locator_set.vpp_del_lisp_locator_set(node,
635 def vpp_set_lisp_eid_table(node, eid_table):
636 """Set lisp eid tables on VPP node in topology.
638 :param node: VPP node.
639 :param eid_table: Dictionary containing information of eid_table.
641 :type eid_table: dict
644 if node['type'] != NodeType.DUT:
645 raise ValueError('Node is not DUT')
647 lisp_locator_set = LispLocatorSet()
648 lisp_eid = LispLocalEid()
649 for eid in eid_table:
651 eid_address = eid.get('eid')
652 eid_prefix_len = eid.get('eid-prefix-len')
653 locator_set_name = eid.get('locator-set')
654 lisp_locator_set.vpp_add_lisp_locator_set(node, locator_set_name)
655 lisp_eid.vpp_add_lisp_local_eid(node,
662 def vpp_unset_lisp_eid_table(node, eid_table):
663 """Unset lisp eid tables on VPP node in topology.
665 :param node: VPP node.
666 :param eid_table: Dictionary containing information of eid_table.
668 :type eid_table: dict
671 if node['type'] != NodeType.DUT:
672 raise ValueError('Node is not DUT')
674 locator_set_list = []
675 lisp_locator_set = LispLocatorSet()
676 lisp_eid = LispLocalEid()
677 for eid in eid_table:
679 eid_address = eid.get('eid')
680 eid_prefix_len = eid.get('eid-prefix-len')
681 locator_set_name = eid.get('locator-set')
682 if locator_set_name not in locator_set_list:
683 locator_set_list.append(locator_set_name)
685 lisp_eid.vpp_del_lisp_local_eid(node,
691 for locator_set_name in locator_set_list:
692 lisp_locator_set.vpp_del_lisp_locator_set(node, locator_set_name)
695 def vpp_set_lisp_map_resolver(node, map_resolver):
696 """Set lisp map resolvers on VPP node in topology.
698 :param node: VPP node.
699 :param map_resolver: Dictionary containing information of map resolver.
701 :type map_resolver: dict
704 lisp_map_res = LispMapResolver()
705 for map_ip in map_resolver:
706 lisp_map_res.vpp_add_map_resolver(node, map_ip.get('map resolver'))
709 def vpp_unset_lisp_map_resolver(node, map_resolver):
710 """Unset lisp map resolvers on VPP node in topology.
712 :param node: VPP node.
713 :param map_resolver: Dictionary containing information of map resolver.
715 :type map_resolver: dict
718 lisp_map_res = LispMapResolver()
719 for map_ip in map_resolver:
720 lisp_map_res.vpp_del_map_resolver(node, map_ip.get('map resolver'))
722 class LispEidTableMap(object):
724 Class for EID table map.
728 def vpp_lisp_eid_table_mapping(node, vni, bd_id=None, vrf=None):
730 Map LISP VNI to either bridge domain ID, or VRF ID.
732 :param node: VPP node.
733 :param vni: Lisp VNI.
734 :param bd_id: Bridge domain ID.
742 # adding default mapping vni=0, vrf=0 needs to be skipped
751 # skip adding default mapping
752 if (int(vrf) == 0) and (int(vni) == 0):
755 args = dict(is_add=1,
757 dp_table=int(dp_table),
760 cmd = 'lisp_eid_table_add_del_map'
761 err_msg = "Failed to add eid table map on host {host}".format(
765 with PapiSocketExecutor(node) as papi_exec:
766 papi_exec.add(cmd, **args).get_reply(err_msg)