CSIT-229: ip4-lispgpe-ip4
[csit.git] / resources / libraries / python / LispSetup.py
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:
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 resources.libraries.python.topology import NodeType
17 from resources.libraries.python.VatExecutor import VatExecutor
18
19
20 class LispStatus(object):
21     """Class for lisp API."""
22
23     def __init__(self):
24         pass
25
26     @staticmethod
27     def vpp_lisp_enable_disable(node, state):
28         """Enable/Disable lisp in the VPP node in topology.
29
30         :param node: Node of the test topology.
31         :param state: State of the lisp, enable or disable.
32         :type node: dict
33         :type state: str
34         """
35
36         VatExecutor.cmd_from_template(node,
37                                       'lisp/lisp_status.vat',
38                                       state=state)
39
40
41 class LispRemoteMapping(object):
42     """Class for lisp remote mapping API."""
43
44     def __init__(self):
45         pass
46
47     @staticmethod
48     def vpp_add_lisp_remote_mapping(node, vni, deid, deid_prefix, seid,
49                                     seid_prefix, rloc):
50         """Add lisp remote mapping on the VPP node in topology.
51
52         :param node: VPP node.
53         :param vni: Vni.
54         :param deid: Destination eid address.
55         :param deid_predix: 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         :type node: dict
60         :type vni: int
61         :type deid: str
62         :type deid_prefix: int
63         :type seid: str
64         :type seid_prefix: int
65         :type rloc: str
66         """
67
68         VatExecutor.cmd_from_template(node,
69                                       'lisp/add_lisp_remote_mapping.vat',
70                                       vni=vni,
71                                       deid=deid,
72                                       deid_prefix=deid_prefix,
73                                       seid=seid,
74                                       seid_prefix=seid_prefix,
75                                       rloc=rloc)
76
77     @staticmethod
78     def vpp_del_lisp_remote_mapping(node, vni, deid, deid_prefix, seid,
79                                     seid_prefix, rloc):
80         """Delete lisp remote mapping on the VPP node in topology.
81
82         :param node: VPP node.
83         :param vni: Vni.
84         :param deid: Destination eid address.
85         :param deid_predix: Destination eid address prefix_len.
86         :param seid: Source eid address.
87         :param seid_prefix: Source eid address prefix_len.
88         :param rloc: Receiver locator.
89         :type node: dict
90         :type vni: int
91         :type deid: str
92         :type deid_prefix: int
93         :type seid: str
94         :type seid_prefix: int
95         :type rloc: str
96         """
97
98         VatExecutor.cmd_from_template(node,
99                                       'lisp/del_lisp_remote_mapping.vat',
100                                       vni=vni,
101                                       deid=deid,
102                                       deid_prefix=deid_prefix,
103                                       seid=seid,
104                                       seid_prefix=seid_prefix,
105                                       rloc=rloc)
106 class LispAdjacency(object):
107     """Class for lisp adjacency API."""
108
109     def __init__(self):
110         pass
111
112     @staticmethod
113     def vpp_add_lisp_adjacency(node, vni, deid, deid_prefix, seid,
114                                seid_prefix):
115         """Add lisp adjacency on the VPP node in topology.
116
117         :param node: VPP node.
118         :param vni: Vni.
119         :param deid: Destination eid address.
120         :param deid_predix: Destination eid address prefix_len.
121         :param seid: Source eid address.
122         :param seid_prefix: Source eid address prefix_len.
123         :type node: dict
124         :type vni: int
125         :type deid: str
126         :type deid_prefix: int
127         :type seid: str
128         :type seid_prefix: int
129         """
130
131         VatExecutor.cmd_from_template(node,
132                                       'lisp/add_lisp_adjacency.vat',
133                                       vni=vni,
134                                       deid=deid,
135                                       deid_prefix=deid_prefix,
136                                       seid=seid,
137                                       seid_prefix=seid_prefix)
138
139     @staticmethod
140     def vpp_del_lisp_adjacency(node, vni, deid, deid_prefix, seid,
141                                seid_prefix):
142         """Delete lisp adjacency 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_predix: Destination eid address prefix_len.
148         :param seid: Source eid address.
149         :param seid_prefix: Source eid address prefix_len.
150         :type node: dict
151         :type vni: int
152         :type deid: str
153         :type deid_prefix: int
154         :type seid: str
155         :type seid_prefix: int
156         """
157
158         VatExecutor.cmd_from_template(node,
159                                       'lisp/del_lisp_adjacency.vat',
160                                       vni=vni,
161                                       deid=deid,
162                                       deid_prefix=deid_prefix,
163                                       seid=seid,
164                                       seid_prefix=seid_prefix)
165
166
167 class LispGpeStatus(object):
168     """Clas for LISP GPE status manipulation."""
169
170     def __init__(self):
171         pass
172
173     @staticmethod
174     def vpp_lisp_gpe_enable_disable(node, state):
175         """Change the state of LISP GPE - enable or disable.
176
177         :param node: VPP node.
178         :param state: Requested state - enable or disable.
179         :type node: dict
180         :type state: str
181         """
182
183         VatExecutor.cmd_from_template(node, 'lisp/lisp_gpe_status.vat',
184                                       state=state)
185
186
187 class LispGpeIface(object):
188     """Class for Lisp gpe interface API."""
189
190     def __init__(self):
191         pass
192
193     @staticmethod
194     def vpp_lisp_gpe_iface(node, state):
195         """Set lisp gpe interface up or down on the VPP node in topology.
196
197         :param node: VPP node.
198         :param state: State of the gpe iface, up or down.
199         :type node: dict
200         :type state: str
201         """
202
203         VatExecutor.cmd_from_template(node, 'lisp/lisp_gpe_iface.vat',
204                                       state=state)
205
206
207 class LispGpeForwardEntry(object):
208     """The functionality needed for these methods is not implemented in VPP
209     (VAT). Bug https://jira.fd.io/browse/VPP-334 was open to cover this issue.
210
211     TODO: Implement when VPP-334 is fixed.
212     """
213
214     def __init__(self):
215         pass
216
217     @staticmethod
218     def add_lisp_gpe_forward_entry(node, *args):
219         # TODO: Implement when VPP-334 is fixed.
220         pass
221
222     @staticmethod
223     def del_lisp_gpe_forward_entry(node, *args):
224         # TODO: Implement when VPP-334 is fixed.
225         pass
226
227
228 class LispMapResolver(object):
229     """Class for Lisp map resolver API."""
230
231     def __init__(self):
232         pass
233
234     @staticmethod
235     def vpp_add_map_resolver(node, map_resolver_ip):
236         """Set lisp map resolver on the VPP node in topology.
237
238         :param node: VPP node.
239         :param map_resolver_ip: IP address of the map resolver.
240         :type node: dict
241         :type map_resolver_ip: str
242         """
243
244         VatExecutor.cmd_from_template(node,
245                                       'lisp/add_lisp_map_resolver.vat',
246                                       address=map_resolver_ip)
247
248     @staticmethod
249     def vpp_del_map_resolver(node, map_resolver_ip):
250         """Unset lisp map resolver on the VPP node in topology.
251
252         :param node: VPP node.
253         :param map_resolver_ip: IP address of the map resolver.
254         :type node: dict
255         :type map_resolver_ip: str
256         """
257
258         VatExecutor.cmd_from_template(node,
259                                       'lisp/del_lisp_map_resolver.vat',
260                                       address=map_resolver_ip)
261
262
263 class LispLocalEid(object):
264     """Class for Lisp local eid API."""
265
266     def __init__(self):
267         pass
268
269     @staticmethod
270     def vpp_add_lisp_local_eid(node, locator_set_name, vni, eid,
271                                prefix_len=None):
272         """Set lisp eid address on the VPP node in topology.
273
274         :param node: VPP node.
275         :param locator_set_name: Name of the locator_set.
276         :param vni: vni value.
277         :param eid: Eid value.
278         :param prefix_len: prefix len if the eid is IP address.
279         :type node: dict
280         :type locator_set_name: str
281         :type vni: int
282         :type eid: str
283         :type prefix_len: int
284         """
285
286         if prefix_len is not None:
287             VatExecutor.cmd_from_template(node,
288                                           'lisp/add_lisp_local_eid.vat',
289                                           vni=vni,
290                                           eid=eid,
291                                           eid_prefix=prefix_len,
292                                           locator_name=locator_set_name)
293         else:
294             VatExecutor.cmd_from_template(node,
295                                           'lisp/add_lisp_local_eid_mac.vat',
296                                           vni=vni,
297                                           eid=eid,
298                                           locator_name=locator_set_name)
299
300     @staticmethod
301     def vpp_del_lisp_local_eid(node, locator_set_name, vni, eid,
302                                prefix_len=None):
303         """Set lisp eid addres on the VPP node in topology.
304
305         :param node: VPP node.
306         :param locator_set_name: Name of the locator_set.
307         :param vni: vni value.
308         :param eid: Eid value.
309         :param prefix_len: prefix len if the eid is IP address.
310         :type node: dict
311         :type locator_set_name: str
312         :type vni: int
313         :type eid: str
314         :type prefix_len: int
315         """
316
317         if prefix_len is not None:
318             VatExecutor.cmd_from_template(node,
319                                           'lisp/del_lisp_local_eid.vat',
320                                           vni=vni,
321                                           eid=eid,
322                                           eid_prefix=prefix_len,
323                                           locator_name=locator_set_name)
324         else:
325             VatExecutor.cmd_from_template(node,
326                                           'lisp/del_lisp_local_eid_mac.vat',
327                                           vni=vni,
328                                           eid=eid,
329                                           locator_name=locator_set_name)
330
331
332 class LispLocator(object):
333     """Class for the Lisp Locator API."""
334
335     def __init__(self):
336         pass
337
338     @staticmethod
339     def vpp_add_lisp_locator(node, locator_name, sw_if_index, priority, weight):
340         """Set lisp locator on the VPP node in topology.
341
342         :param node: VPP node.
343         :param locator_name: Name of the locator_set.
344         :param sw_if_index: sw_if_index if the interface.
345         :param priority: priority of the locator.
346         :param weight: weight of the locator.
347         :type node: dict
348         :type locator_name: str
349         :type sw_if_index: int
350         :type priority: int
351         :type weight: int
352         """
353
354         VatExecutor.cmd_from_template(node,
355                                       'lisp/add_lisp_locator.vat',
356                                       lisp_name=locator_name,
357                                       sw_if_index=sw_if_index,
358                                       priority=priority,
359                                       weight=weight)
360
361     @staticmethod
362     def vpp_del_lisp_locator(node, locator_name, sw_if_index, priority, weight):
363         """Unset lisp locator on the VPP node in topology.
364
365         :param node: VPP node.
366         :param locator_name: Name of the locator_set.
367         :param sw_if_index: sw_if_index if the interface.
368         :param priority: priority of the locator.
369         :param weight: weight of the locator.
370         :type node: dict
371         :type locator_name: str
372         :type sw_if_index: int
373         :type priority: int
374         :type weight: int
375         """
376
377         VatExecutor.cmd_from_template(node,
378                                       'lisp/del_lisp_locator.vat',
379                                       lisp_name=locator_name,
380                                       sw_if_index=sw_if_index,
381                                       priority=priority,
382                                       weight=weight)
383
384
385 class LispLocatorSet(object):
386     """Class for Lisp Locator Set API."""
387
388     def __init__(self):
389         pass
390
391     @staticmethod
392     def vpp_add_lisp_locator_set(node, name):
393         """Add lisp locator_set on VPP.
394
395         :param node: VPP node.
396         :param name: VPP locator name.
397         :type node: dict
398         :type name: str
399         """
400
401         VatExecutor.cmd_from_template(node,
402                                       'lisp/add_lisp_locator_set.vat',
403                                       lisp_name=name)
404
405     @staticmethod
406     def vpp_del_lisp_locator_set(node, name):
407         """Del lisp locator_set on VPP.
408
409         :param node: VPP node.
410         :param name: VPP locator name.
411         :type node: dict
412         :type name: str
413         """
414
415         VatExecutor.cmd_from_template(node,
416                                       'lisp/del_lisp_locator_set.vat',
417                                       lisp_name=name)
418
419
420 class LispSetup(object):
421     """Lisp setup in topology."""
422
423     def __init__(self):
424         pass
425
426     @staticmethod
427     def vpp_set_lisp_locator_set(node, locator_set_list):
428         """Set lisp locator_sets on VPP node in topology.
429
430         :param node: VPP node.
431         :param locator_set_list: List of locator_set.
432         :type node: dict
433         :type locator_set_list: list
434         """
435
436         if node['type'] != NodeType.DUT:
437             raise ValueError('Node is not DUT')
438
439         lisp_locator = LispLocator()
440         lisp_locator_set = LispLocatorSet()
441         for locator_set in locator_set_list:
442             locator_set_name = locator_set.get('locator-set')
443             locator_list = locator_set.get('locator')
444             lisp_locator_set.vpp_add_lisp_locator_set(node,
445                                                       locator_set_name)
446             for locator in locator_list:
447                 sw_if_index = locator.get('locator-index')
448                 priority = locator.get('priority')
449                 weight = locator.get('weight')
450                 lisp_locator.vpp_add_lisp_locator(node,
451                                                   locator_set_name,
452                                                   sw_if_index,
453                                                   priority,
454                                                   weight)
455
456     @staticmethod
457     def vpp_unset_lisp_locator_set(node, locator_set_list):
458         """Unset lisp locator_sets on VPP node in topology.
459
460         :param node: VPP node.
461         :param locator_set_list: List of locator_set.
462         :type node: dict
463         :type locator_set_list: list
464         """
465
466         if node['type'] != NodeType.DUT:
467             raise ValueError('Lisp locator set, node is not DUT')
468
469         lisp_locator = LispLocator()
470         lisp_locator_set = LispLocatorSet()
471         for locator_set in locator_set_list:
472             locator_set_name = locator_set.get('locator-set')
473             locator_list = locator_set.get('locator')
474             for locator in locator_list:
475                 sw_if_index = locator.get('locator-index')
476                 priority = locator.get('priority')
477                 weight = locator.get('weight')
478                 lisp_locator.vpp_del_lisp_locator(node,
479                                                   locator_set_name,
480                                                   sw_if_index,
481                                                   priority,
482                                                   weight)
483
484             lisp_locator_set.vpp_del_lisp_locator_set(node,
485                                                       locator_set_name)
486
487     @staticmethod
488     def vpp_set_lisp_eid_table(node, eid_table):
489         """Set lisp eid tables on VPP node in topology.
490
491         :param node: VPP node.
492         :param eid_table: Dictionary containing information of eid_table.
493         :type node: dict
494         :type eid_table: dict
495         """
496
497         if node['type'] != NodeType.DUT:
498             raise ValueError('Node is not DUT')
499
500         lisp_locator_set = LispLocatorSet()
501         lisp_eid = LispLocalEid()
502         for eid in eid_table:
503             vni = eid.get('vni')
504             eid_address = eid.get('eid')
505             eid_prefix_len = eid.get('eid-prefix-len')
506             locator_set_name = eid.get('locator-set')
507             lisp_locator_set.vpp_add_lisp_locator_set(node, locator_set_name)
508             lisp_eid.vpp_add_lisp_local_eid(node,
509                                             locator_set_name,
510                                             vni,
511                                             eid_address,
512                                             eid_prefix_len)
513
514     @staticmethod
515     def vpp_unset_lisp_eid_table(node, eid_table):
516         """Unset lisp eid tables on VPP node in topology.
517
518         :param node: VPP node.
519         :param eid_table: Dictionary containing information of eid_table.
520         :type node: dict
521         :type eid_table: dict
522         """
523
524         if node['type'] != NodeType.DUT:
525             raise ValueError('Node is not DUT')
526
527         locator_set_list = []
528         lisp_locator_set = LispLocatorSet()
529         lisp_eid = LispLocalEid()
530         for eid in eid_table:
531             vni = eid.get('vni')
532             eid_address = eid.get('eid')
533             eid_prefix_len = eid.get('eid-prefix-len')
534             locator_set_name = eid.get('locator-set')
535             if locator_set_name not in locator_set_list:
536                 locator_set_list.append(locator_set_name)
537
538             lisp_eid.vpp_del_lisp_local_eid(node,
539                                             locator_set_name,
540                                             vni,
541                                             eid_address,
542                                             eid_prefix_len)
543
544         for locator_set_name in locator_set_list:
545             lisp_locator_set.vpp_del_lisp_locator_set(node, locator_set_name)
546
547     @staticmethod
548     def vpp_set_lisp_map_resolver(node, map_resolver):
549         """Set lisp map resolvers on VPP node in topology.
550
551         :param node: VPP node.
552         :param map_resolver: Dictionary containing information of map resolver.
553         :type node: dict
554         :type map_resolver: dict
555         """
556
557         lisp_map_res = LispMapResolver()
558         for map_ip in map_resolver:
559             lisp_map_res.vpp_add_map_resolver(node, map_ip.get('map resolver'))
560
561     @staticmethod
562     def vpp_unset_lisp_map_resolver(node, map_resolver):
563         """Unset lisp map resolvers on VPP node in topology.
564
565         :param node: VPP node.
566         :param map_resolver: Dictionary containing information of map resolver.
567         :type node: dict
568         :type map_resolver: dict
569         """
570
571         lisp_map_res = LispMapResolver()
572         for map_ip in map_resolver:
573             lisp_map_res.vpp_del_map_resolver(node, map_ip.get('map resolver'))
574
575     @staticmethod
576     def vpp_lisp_gpe_interface_status(node, state):
577         """Set lisp gpe interface status on VPP node in topology.
578
579         :param node: VPP node.
580         :param state: State of the gpe iface, up or down
581         :type node: dict
582         :type state: str
583         """
584
585         lgi = LispGpeIface()
586         lgi.vpp_lisp_gpe_iface(node, state)