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.L2Util import L2Util
19 from resources.libraries.python.PapiExecutor import PapiSocketExecutor
20 from resources.libraries.python.topology import NodeType
24 """Class for lisp API."""
27 def vpp_lisp_enable_disable(node, state):
28 """Enable/Disable lisp in the VPP node in topology.
30 :param node: Node of the test topology.
31 :param state: State of the lisp, enable or disable.
35 args = dict(is_en=0 if state == u"disable" else 1)
37 cmd = u"lisp_enable_disable"
38 err_msg = f"Failed to set LISP status on host {node[u'host']}"
40 with PapiSocketExecutor(node) as papi_exec:
41 papi_exec.add(cmd, **args).get_reply(err_msg)
43 class LispRemoteMapping:
44 """Class for lisp remote mapping API."""
47 def vpp_add_lisp_remote_mapping(
48 node, vni, deid, deid_prefix, seid, seid_prefix, rloc,
50 """Add lisp remote mapping on the VPP node in topology.
52 :param node: VPP node.
54 :param deid: Destination eid address.
55 :param deid_prefix: Destination eid address prefix_len.
56 :param seid: Source eid address.
57 :param seid_prefix: Source eid address prefix_len.
58 :param rloc: Receiver locator.
59 :param is_mac: Set to True if the deid/seid is MAC address.
63 :type deid_prefix: int
65 :type seid_prefix: int
70 eid_type = 0 if ip_address(deid).version == 4 else 1
71 eid_packed = ip_address(deid).packed
72 seid_packed = ip_address(seid).packed
74 seid_len = seid_prefix
77 eid_packed = L2Util.mac_to_bin(deid)
78 seid_packed = L2Util.mac_to_bin(seid)
84 is_ip4=1 if ip_address(rloc).version == 4 else 0,
85 addr=ip_address(rloc).packed
102 cmd = u"lisp_add_del_remote_mapping"
103 err_msg = f"Failed to add remote mapping on host {node[u'host']}"
105 with PapiSocketExecutor(node) as papi_exec:
106 papi_exec.add(cmd, **args).get_reply(err_msg)
109 def vpp_del_lisp_remote_mapping(
110 node, vni, deid, deid_prefix, seid, seid_prefix, rloc):
111 """Delete lisp remote mapping on the VPP node in topology.
113 :param node: VPP node.
115 :param deid: Destination eid address.
116 :param deid_prefix: Destination eid address prefix_len.
117 :param seid: Source eid address.
118 :param seid_prefix: Source eid address prefix_len.
119 :param rloc: Receiver locator.
123 :type deid_prefix: int
125 :type seid_prefix: int
132 eid_type = 0 if ip_address(deid).version == 4 else 1
133 eid_packed = ip_address(deid).packed
134 seid_packed = ip_address(seid).packed
135 eid_len = deid_prefix
136 seid_len = seid_prefix
139 eid_packed = L2Util.mac_to_bin(deid)
140 seid_packed = L2Util.mac_to_bin(seid)
146 is_ip4=1 if ip_address(str(rloc)).version == 4 else 0,
147 addr=ip_address(str(rloc)).packed
164 cmd = u"lisp_add_del_remote_mapping"
165 err_msg = f"Failed to delete remote mapping on host {node[u'host']}"
167 with PapiSocketExecutor(node) as papi_exec:
168 papi_exec.add(cmd, **args).get_reply(err_msg)
171 """Class for lisp adjacency API."""
174 def vpp_add_lisp_adjacency(
175 node, vni, deid, deid_prefix, seid, seid_prefix, is_mac=False):
176 """Add lisp adjacency on the VPP node in topology.
178 :param node: VPP node.
180 :param deid: Destination eid address.
181 :param deid_prefix: Destination eid address prefix_len.
182 :param seid: Source eid address.
183 :param seid_prefix: Source eid address prefix_len.
184 :param is_mac: Set to True if the deid/seid is MAC address.
188 :type deid_prefix: int
190 :type seid_prefix: int
194 eid_type = 0 if ip_address(deid).version == 4 else 1
195 reid = ip_address(deid).packed
196 leid = ip_address(seid).packed
197 reid_len = deid_prefix
198 leid_len = seid_prefix
201 reid = L2Util.mac_to_bin(deid)
202 leid = L2Util.mac_to_bin(seid)
216 cmd = u"lisp_add_del_adjacency"
217 err_msg = f"Failed to add lisp adjacency on host {node[u'host']}"
219 with PapiSocketExecutor(node) as papi_exec:
220 papi_exec.add(cmd, **args).get_reply(err_msg)
223 def vpp_del_lisp_adjacency(
224 node, vni, deid, deid_prefix, seid, seid_prefix):
225 """Delete lisp adjacency on the VPP node in topology.
227 :param node: VPP node.
229 :param deid: Destination eid address.
230 :param deid_prefix: Destination eid address prefix_len.
231 :param seid: Source eid address.
232 :param seid_prefix: Source eid address prefix_len.
236 :type deid_prefix: int
238 :type seid_prefix: int
244 eid_type = 0 if ip_address(deid).version == 4 else 1
245 reid = ip_address(deid).packed
246 leid = ip_address(seid).packed
247 reid_len = deid_prefix
248 leid_len = seid_prefix
251 reid = L2Util.mac_to_bin(deid)
252 leid = L2Util.mac_to_bin(seid)
266 cmd = u"lisp_add_del_adjacency"
267 err_msg = f"Failed to delete lisp adjacency on host {node[u'host']}"
269 with PapiSocketExecutor(node) as papi_exec:
270 papi_exec.add(cmd, **args).get_reply(err_msg)
273 """Clas for LISP GPE status manipulation."""
276 def vpp_lisp_gpe_enable_disable(node, state):
277 """Change the state of LISP GPE - enable or disable.
279 :param node: VPP node.
280 :param state: Requested state - enable or disable.
284 args = dict(is_en=0 if state == u"disable" else 1)
286 cmd = u"gpe_enable_disable"
287 err_msg = f"Failed to set LISP GPE status on host {node[u'host']}"
289 with PapiSocketExecutor(node) as papi_exec:
290 papi_exec.add(cmd, **args).get_reply(err_msg)
292 class LispGpeForwardEntry:
293 """The functionality needed for these methods is not implemented in VPP
294 (VAT). Bug https://jira.fd.io/browse/VPP-334 was open to cover this issue.
296 TODO: Implement when VPP-334 is fixed.
300 def add_lisp_gpe_forward_entry(node, *args):
301 """Not implemented"""
302 # TODO: Implement when VPP-334 is fixed.
305 def del_lisp_gpe_forward_entry(node, *args):
306 """Not implemented"""
307 # TODO: Implement when VPP-334 is fixed.
310 class LispMapResolver:
311 """Class for Lisp map resolver API."""
314 def vpp_add_map_resolver(node, map_resolver_ip):
315 """Set lisp map resolver on the VPP node in topology.
317 :param node: VPP node.
318 :param map_resolver_ip: IP address of the map resolver.
320 :type map_resolver_ip: str
324 is_ipv6=0 if ip_address(map_resolver_ip).version == 4 else 1,
325 ip_address=ip_address(map_resolver_ip).packed
328 cmd = u"lisp_add_del_map_resolver"
329 err_msg = f"Failed to add map resolver on host {node[u'host']}"
331 with PapiSocketExecutor(node) as papi_exec:
332 papi_exec.add(cmd, **args).get_reply(err_msg)
335 def vpp_del_map_resolver(node, map_resolver_ip):
336 """Unset lisp map resolver on the VPP node in topology.
338 :param node: VPP node.
339 :param map_resolver_ip: IP address of the map resolver.
341 :type map_resolver_ip: str
345 is_ipv6=0 if ip_address(map_resolver_ip).version == 4 else 1,
346 ip_address=ip_address(map_resolver_ip).packed
349 cmd = u"lisp_add_del_map_resolver"
350 err_msg = f"Failed to delete map resolver on host {node[u'host']}"
352 with PapiSocketExecutor(node) as papi_exec:
353 papi_exec.add(cmd, **args).get_reply(err_msg)
356 """Class for Lisp local eid API."""
359 def vpp_add_lisp_local_eid(
360 node, locator_set_name, vni, eid, prefix_len=None):
361 """Set lisp eid address on the VPP node in topology.
363 :param node: VPP node.
364 :param locator_set_name: Name of the locator_set.
365 :param vni: vni value.
366 :param eid: Eid value.
367 :param prefix_len: prefix len if the eid is IP address.
369 :type locator_set_name: str
372 :type prefix_len: int
375 eid_type = 0 if ip_address(eid).version == 4 else 1
376 eid_packed = ip_address(eid).packed
379 eid_packed = L2Util.mac_to_bin(eid)
385 prefix_len=prefix_len,
386 locator_set_name=locator_set_name,
390 cmd = u"lisp_add_del_local_eid"
391 err_msg = f"Failed to add local eid on host {node[u'host']}"
393 with PapiSocketExecutor(node) as papi_exec:
394 papi_exec.add(cmd, **args).get_reply(err_msg)
397 def vpp_del_lisp_local_eid(
398 node, locator_set_name, vni, eid, prefix_len=None):
399 """Set lisp eid addres on the VPP node in topology.
401 :param node: VPP node.
402 :param locator_set_name: Name of the locator_set.
403 :param vni: vni value.
404 :param eid: Eid value.
405 :param prefix_len: prefix len if the eid is IP address.
407 :type locator_set_name: str
410 :type prefix_len: int
413 eid_type = 0 if ip_address(eid).version == 4 else 1
414 eid_packed = ip_address(eid).packed
417 eid_packed = L2Util.mac_to_bin(eid)
423 prefix_len=prefix_len,
424 locator_set_name=locator_set_name,
428 cmd = u"lisp_add_del_local_eid"
429 err_msg = f"Failed to delete local eid on host {node[u'host']}"
431 with PapiSocketExecutor(node) as papi_exec:
432 papi_exec.add(cmd, **args).get_reply(err_msg)
435 """Class for the Lisp Locator API."""
438 def vpp_add_lisp_locator(node, locator_name, sw_if_index, priority, weight):
439 """Set lisp locator on the VPP node in topology.
441 :param node: VPP node.
442 :param locator_name: Name of the locator_set.
443 :param sw_if_index: sw_if_index if the interface.
444 :param priority: priority of the locator.
445 :param weight: weight of the locator.
447 :type locator_name: str
448 :type sw_if_index: int
455 locator_set_name=locator_name,
456 sw_if_index=sw_if_index,
461 cmd = u"lisp_add_del_locator"
462 err_msg = f"Failed to add locator on host {node[u'host']}"
464 with PapiSocketExecutor(node) as papi_exec:
465 papi_exec.add(cmd, **args).get_reply(err_msg)
468 def vpp_del_lisp_locator(node, locator_name, sw_if_index, priority, weight):
469 """Unset lisp locator on the VPP node in topology.
471 :param node: VPP node.
472 :param locator_name: Name of the locator_set.
473 :param sw_if_index: sw_if_index if the interface.
474 :param priority: priority of the locator.
475 :param weight: weight of the locator.
477 :type locator_name: str
478 :type sw_if_index: int
484 locator_set_name=locator_name,
485 sw_if_index=sw_if_index,
490 cmd = u"lisp_add_del_locator"
491 err_msg = f"Failed to delete locator on host {node[u'host']}"
493 with PapiSocketExecutor(node) as papi_exec:
494 papi_exec.add(cmd, **args).get_reply(err_msg)
496 class LispLocatorSet:
497 """Class for Lisp Locator Set API."""
500 def vpp_add_lisp_locator_set(node, name):
501 """Add lisp locator_set on VPP.
503 :param node: VPP node.
504 :param name: VPP locator name.
510 locator_set_name=name,
515 cmd = u"lisp_add_del_locator_set"
516 err_msg = f"Failed to add locator set on host {node[u'host']}"
518 with PapiSocketExecutor(node) as papi_exec:
519 papi_exec.add(cmd, **args).get_reply(err_msg)
522 def vpp_del_lisp_locator_set(node, name):
523 """Del lisp locator_set on VPP.
525 :param node: VPP node.
526 :param name: VPP locator name.
532 locator_set_name=name,
537 cmd = u"lisp_add_del_locator_set"
538 err_msg = f"Failed to delete locator set on host {node[u'host']}"
540 with PapiSocketExecutor(node) as papi_exec:
541 papi_exec.add(cmd, **args).get_reply(err_msg)
544 """Lisp setup in topology."""
547 def vpp_set_lisp_locator_set(node, locator_set_list):
548 """Set lisp locator_sets on VPP node in topology.
550 :param node: VPP node.
551 :param locator_set_list: List of locator_set.
553 :type locator_set_list: list
556 if node[u"type"] != NodeType.DUT:
557 raise ValueError(u"Node is not DUT")
559 lisp_locator = LispLocator()
560 lisp_locator_set = LispLocatorSet()
561 for locator_set in locator_set_list:
562 locator_set_name = locator_set.get(u"locator-set")
563 locator_list = locator_set.get(u"locator")
564 lisp_locator_set.vpp_add_lisp_locator_set(node, locator_set_name)
565 for locator in locator_list:
566 sw_if_index = locator.get(u"locator-index")
567 priority = locator.get(u"priority")
568 weight = locator.get(u"weight")
569 lisp_locator.vpp_add_lisp_locator(
570 node, locator_set_name, sw_if_index, priority, weight
574 def vpp_unset_lisp_locator_set(node, locator_set_list):
575 """Unset 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
582 if node[u"type"] != NodeType.DUT:
583 raise ValueError(u"Lisp locator set, node is not DUT")
585 lisp_locator = LispLocator()
586 lisp_locator_set = LispLocatorSet()
587 for locator_set in locator_set_list:
588 locator_set_name = locator_set.get(u"locator-set")
589 locator_list = locator_set.get(u"locator")
590 for locator in locator_list:
591 sw_if_index = locator.get(u"locator-index")
592 priority = locator.get(u"priority")
593 weight = locator.get(u"weight")
594 lisp_locator.vpp_del_lisp_locator(
595 node, locator_set_name, sw_if_index, priority, weight
598 lisp_locator_set.vpp_del_lisp_locator_set(node, locator_set_name)
601 def vpp_set_lisp_eid_table(node, eid_table):
602 """Set lisp eid tables on VPP node in topology.
604 :param node: VPP node.
605 :param eid_table: Dictionary containing information of eid_table.
607 :type eid_table: dict
609 if node[u"type"] != NodeType.DUT:
610 raise ValueError(u"Node is not DUT")
612 lisp_locator_set = LispLocatorSet()
613 lisp_eid = LispLocalEid()
614 for eid in eid_table:
615 vni = eid.get(u"vni")
616 eid_address = eid.get(u"eid")
617 eid_prefix_len = eid.get(u"eid-prefix-len")
618 locator_set_name = eid.get(u"locator-set")
619 lisp_locator_set.vpp_add_lisp_locator_set(node, locator_set_name)
620 lisp_eid.vpp_add_lisp_local_eid(
621 node, locator_set_name, vni, eid_address, eid_prefix_len
625 def vpp_unset_lisp_eid_table(node, eid_table):
626 """Unset lisp eid tables on VPP node in topology.
628 :param node: VPP node.
629 :param eid_table: Dictionary containing information of eid_table.
631 :type eid_table: dict
633 if node[u"type"] != NodeType.DUT:
634 raise ValueError(u"Node is not DUT")
636 locator_set_list = []
637 lisp_locator_set = LispLocatorSet()
638 lisp_eid = LispLocalEid()
639 for eid in eid_table:
640 vni = eid.get(u"vni")
641 eid_address = eid.get(u"eid")
642 eid_prefix_len = eid.get(u"eid-prefix-len")
643 locator_set_name = eid.get(u"locator-set")
644 if locator_set_name not in locator_set_list:
645 locator_set_list.append(locator_set_name)
647 lisp_eid.vpp_del_lisp_local_eid(
648 node, locator_set_name, vni, eid_address, eid_prefix_len
651 for locator_set_name in locator_set_list:
652 lisp_locator_set.vpp_del_lisp_locator_set(node, locator_set_name)
655 def vpp_set_lisp_map_resolver(node, map_resolver):
656 """Set lisp map resolvers on VPP node in topology.
658 :param node: VPP node.
659 :param map_resolver: Dictionary containing information of map resolver.
661 :type map_resolver: dict
663 lisp_map_res = LispMapResolver()
664 for map_ip in map_resolver:
665 lisp_map_res.vpp_add_map_resolver(node, map_ip.get(u"map resolver"))
668 def vpp_unset_lisp_map_resolver(node, map_resolver):
669 """Unset lisp map resolvers on VPP node in topology.
671 :param node: VPP node.
672 :param map_resolver: Dictionary containing information of map resolver.
674 :type map_resolver: dict
676 lisp_map_res = LispMapResolver()
677 for map_ip in map_resolver:
678 lisp_map_res.vpp_del_map_resolver(node, map_ip.get(u"map resolver"))
680 class LispEidTableMap:
682 Class for EID table map.
686 def vpp_lisp_eid_table_mapping(node, vni, bd_id=None, vrf=None):
688 Map LISP VNI to either bridge domain ID, or VRF ID.
690 :param node: VPP node.
691 :param vni: Lisp VNI.
692 :param bd_id: Bridge domain ID.
699 # adding default mapping vni=0, vrf=0 needs to be skipped
708 # skip adding default mapping
709 if (int(vrf) == 0) and (int(vni) == 0):
715 dp_table=int(dp_table),
719 cmd = u"lisp_eid_table_add_del_map"
720 err_msg = f"Failed to add eid table map on host {node[u'host']}"
723 with PapiSocketExecutor(node) as papi_exec:
724 papi_exec.add(cmd, **args).get_reply(err_msg)