Revert "vpp-device: GENEVE tunnel test, l3 mode"
[csit.git] / resources / libraries / python / LispSetup.py
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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """Library to set up Lisp in topology."""
15
16 from enum import IntEnum
17
18 from ipaddress import ip_address
19
20 from resources.libraries.python.IPUtil import IPUtil
21 from resources.libraries.python.PapiExecutor import PapiSocketExecutor
22
23
24 class EidType(IntEnum):
25     """EID types."""
26     PREFIX = 0
27     MAC = 1
28     NSH = 2
29
30
31 class LispEid:
32     """Class for lisp eid."""
33
34     @staticmethod
35     def create_eid(eid, prefix_len):
36         """Create lisp eid object.
37
38         :param eid: Eid value.
39         :param prefix_len: Prefix len if the eid is IP address.
40         :type eid: str
41         :type prefix_len: int
42         """
43         eid_addr = dict(prefix=IPUtil.create_prefix_object(
44             ip_address(eid), prefix_len)
45         ) if prefix_len else dict(mac=str(eid))
46
47         return dict(
48             type=getattr(
49                 EidType, u"PREFIX" if prefix_len else u"MAC"
50             ).value,
51             address=eid_addr
52         )
53
54
55 class LispRemoteLocator:
56     """Class for lisp remote locator."""
57
58     @staticmethod
59     def create_rloc(ip_addr, priority=0, weight=0):
60         """Create lisp remote locator object.
61
62         :param ip_addr: IP/IPv6 address.
63         :param priority: Priority of the remote locator.
64         :param weight: Weight of the remote locator.
65         :type ip_addr: str
66         :type priority: int
67         :type weight: int
68         """
69         return [
70             dict(
71                 priority=priority,
72                 weight=weight,
73                 ip_address=ip_address(ip_addr)
74             )
75         ]
76
77
78 class LispStatus:
79     """Class for lisp API."""
80
81     @staticmethod
82     def vpp_lisp_enable_disable(node, state):
83         """Enable/Disable lisp in the VPP node in topology.
84
85         :param node: Node of the test topology.
86         :param state: State of the lisp, enable or disable.
87         :type node: dict
88         :type state: str
89         """
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']}"
93
94         with PapiSocketExecutor(node) as papi_exec:
95             papi_exec.add(cmd, **args).get_reply(err_msg)
96
97
98 class LispRemoteMapping:
99     """Class for lisp remote mapping API."""
100
101     @staticmethod
102     def vpp_add_lisp_remote_mapping(
103             node, vni, deid, deid_prefix, seid, seid_prefix, rloc,
104             is_mac=False):
105         """Add lisp remote mapping on the VPP node in topology.
106
107         :param node: VPP node.
108         :param vni: Vni.
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.
115         :type node: dict
116         :type vni: int
117         :type deid: str
118         :type deid_prefix: int
119         :type seid: str
120         :type seid_prefix: int
121         :type rloc: str
122         :type is_mac: bool
123         """
124         args = dict(
125             is_add=True,
126             is_src_dst=True,
127             vni=int(vni),
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),
130             rloc_num=1,
131             rlocs=LispRemoteLocator.create_rloc(rloc)
132         )
133         cmd = u"lisp_add_del_remote_mapping"
134         err_msg = f"Failed to add remote mapping on host {node[u'host']}"
135
136         with PapiSocketExecutor(node) as papi_exec:
137             papi_exec.add(cmd, **args).get_reply(err_msg)
138
139     @staticmethod
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.
143
144         :param node: VPP node.
145         :param vni: Vni.
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.
151         :type node: dict
152         :type vni: int
153         :type deid: str
154         :type deid_prefix: int
155         :type seid: str
156         :type seid_prefix: int
157         :type rloc: str
158         """
159         # used only with IPs
160         args = dict(
161             is_add=False,
162             is_src_dst=True,
163             vni=int(vni),
164             deid=LispEid.create_eid(deid, deid_prefix),
165             seid=LispEid.create_eid(seid, seid_prefix),
166             rloc_num=1,
167             rlocs=LispRemoteLocator.create_rloc(rloc)
168         )
169         cmd = u"lisp_add_del_remote_mapping"
170         err_msg = f"Failed to delete remote mapping on host {node[u'host']}"
171
172         with PapiSocketExecutor(node) as papi_exec:
173             papi_exec.add(cmd, **args).get_reply(err_msg)
174
175
176 class LispAdjacency:
177     """Class for lisp adjacency API."""
178
179     @staticmethod
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.
183
184         :param node: VPP node.
185         :param vni: Vni.
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.
191         :type node: dict
192         :type vni: int
193         :type deid: str
194         :type deid_prefix: int
195         :type seid: str
196         :type seid_prefix: int
197         :type is_mac: bool
198         """
199         args = dict(
200             is_add=True,
201             vni=int(vni),
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)
204         )
205         cmd = u"lisp_add_del_adjacency"
206         err_msg = f"Failed to add lisp adjacency on host {node[u'host']}"
207
208         with PapiSocketExecutor(node) as papi_exec:
209             papi_exec.add(cmd, **args).get_reply(err_msg)
210
211     @staticmethod
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.
215
216         :param node: VPP node.
217         :param vni: Vni.
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.
222         :type node: dict
223         :type vni: int
224         :type deid: str
225         :type deid_prefix: int
226         :type seid: str
227         :type seid_prefix: int
228         """
229         # used only with IPs
230         args = dict(
231             is_add=False,
232             vni=int(vni),
233             eid=LispEid.create_eid(deid, deid_prefix),
234             leid=LispEid.create_eid(seid, seid_prefix)
235         )
236         cmd = u"lisp_add_del_adjacency"
237         err_msg = f"Failed to delete lisp adjacency on host {node[u'host']}"
238
239         with PapiSocketExecutor(node) as papi_exec:
240             papi_exec.add(cmd, **args).get_reply(err_msg)
241
242
243 class LispGpeStatus:
244     """Class for LISP GPE status manipulation."""
245
246     @staticmethod
247     def vpp_lisp_gpe_enable_disable(node, state):
248         """Change the state of LISP GPE - enable or disable.
249
250         :param node: VPP node.
251         :param state: Requested state - enable or disable.
252         :type node: dict
253         :type state: str
254         """
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']}"
258
259         with PapiSocketExecutor(node) as papi_exec:
260             papi_exec.add(cmd, **args).get_reply(err_msg)
261
262
263 class LispLocalEid:
264     """Class for Lisp local eid API."""
265
266     @staticmethod
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.
270
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.
276         :type node: dict
277         :type locator_set_name: str
278         :type vni: int
279         :type eid: str
280         :type prefix_len: int
281         """
282         args = dict(
283             is_add=True,
284             eid=LispEid.create_eid(eid, prefix_len),
285             locator_set_name=locator_set_name,
286             vni=int(vni)
287         )
288         cmd = u"lisp_add_del_local_eid"
289         err_msg = f"Failed to add local eid on host {node[u'host']}"
290
291         with PapiSocketExecutor(node) as papi_exec:
292             papi_exec.add(cmd, **args).get_reply(err_msg)
293
294     @staticmethod
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.
298
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.
304         :type node: dict
305         :type locator_set_name: str
306         :type vni: int
307         :type eid: str
308         :type prefix_len: int
309         """
310         args = dict(
311             is_add=False,
312             eid=LispEid.create_eid(eid, prefix_len),
313             locator_set_name=locator_set_name,
314             vni=int(vni)
315         )
316         cmd = u"lisp_add_del_local_eid"
317         err_msg = f"Failed to delete local eid on host {node[u'host']}"
318
319         with PapiSocketExecutor(node) as papi_exec:
320             papi_exec.add(cmd, **args).get_reply(err_msg)
321
322
323 class LispLocator:
324     """Class for the Lisp Locator API."""
325
326     @staticmethod
327     def vpp_add_lisp_locator(node, locator_name, sw_if_index, priority, weight):
328         """Set lisp locator on the VPP node in topology.
329
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.
335         :type node: dict
336         :type locator_name: str
337         :type sw_if_index: int
338         :type priority: int
339         :type weight: int
340         """
341
342         args = dict(
343             is_add=True,
344             locator_set_name=locator_name,
345             sw_if_index=sw_if_index,
346             priority=priority,
347             weight=weight
348         )
349         cmd = u"lisp_add_del_locator"
350         err_msg = f"Failed to add locator on host {node[u'host']}"
351
352         with PapiSocketExecutor(node) as papi_exec:
353             papi_exec.add(cmd, **args).get_reply(err_msg)
354
355     @staticmethod
356     def vpp_del_lisp_locator(node, locator_name, sw_if_index, priority, weight):
357         """Unset lisp locator on the VPP node in topology.
358
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.
364         :type node: dict
365         :type locator_name: str
366         :type sw_if_index: int
367         :type priority: int
368         :type weight: int
369         """
370         args = dict(
371             is_add=False,
372             locator_set_name=locator_name,
373             sw_if_index=sw_if_index,
374             priority=priority,
375             weight=weight
376         )
377         cmd = u"lisp_add_del_locator"
378         err_msg = f"Failed to delete locator on host {node[u'host']}"
379
380         with PapiSocketExecutor(node) as papi_exec:
381             papi_exec.add(cmd, **args).get_reply(err_msg)
382
383
384 class LispLocatorSet:
385     """Class for Lisp Locator Set API."""
386
387     @staticmethod
388     def vpp_add_lisp_locator_set(node, name):
389         """Add lisp locator_set on VPP.
390
391         :param node: VPP node.
392         :param name: VPP locator name.
393         :type node: dict
394         :type name: str
395         """
396         args = dict(
397             is_add=True,
398             locator_set_name=name,
399             locator_num=0,
400             locators=[]
401         )
402         cmd = u"lisp_add_del_locator_set"
403         err_msg = f"Failed to add locator set on host {node[u'host']}"
404
405         with PapiSocketExecutor(node) as papi_exec:
406             papi_exec.add(cmd, **args).get_reply(err_msg)
407
408     @staticmethod
409     def vpp_del_lisp_locator_set(node, name):
410         """Del lisp locator_set on VPP.
411
412         :param node: VPP node.
413         :param name: VPP locator name.
414         :type node: dict
415         :type name: str
416         """
417         args = dict(
418             is_add=False,
419             locator_set_name=name,
420             locator_num=0,
421             locators=[]
422         )
423         cmd = u"lisp_add_del_locator_set"
424         err_msg = f"Failed to delete locator set on host {node[u'host']}"
425
426         with PapiSocketExecutor(node) as papi_exec:
427             papi_exec.add(cmd, **args).get_reply(err_msg)
428
429
430 class LispEidTableMap:
431     """
432     Class for EID table map.
433     """
434
435     @staticmethod
436     def vpp_lisp_eid_table_mapping(node, vni, bd_id=None, vrf=None):
437         """
438         Map LISP VNI to either bridge domain ID, or VRF ID.
439
440         :param node: VPP node.
441         :param vni: Lisp VNI.
442         :param bd_id: Bridge domain ID.
443         :param vrf: VRF id.
444         :type node: dict
445         :type vni: int
446         :type bd_id: int
447         :type vrf: int
448         """
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):
451             args = dict(
452                 is_add=True,
453                 vni=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)
456             )
457             cmd = u"lisp_eid_table_add_del_map"
458             err_msg = f"Failed to add eid table map on host {node[u'host']}"
459
460             with PapiSocketExecutor(node) as papi_exec:
461                 papi_exec.add(cmd, **args).get_reply(err_msg)