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