CSIT-203: Expand LISP test
[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 LispGpeIface(object):
168     """Class for Lisp gpe interface API."""
169
170     def __init__(self):
171         pass
172
173     @staticmethod
174     def vpp_lisp_gpe_iface(node, state):
175         """Set lisp gpe interface up or down on the VPP node in topology.
176
177         :param node: VPP node.
178         :param state: State of the gpe iface, up or down.
179         :type node: dict
180         :type state: str
181         """
182
183         VatExecutor.cmd_from_template(node,
184                                       'lisp/lisp_gpe_iface.vat',
185                                       state=state)
186
187
188 class LispMapResolver(object):
189     """Class for Lisp map resolver API."""
190
191     def __init__(self):
192         pass
193
194     @staticmethod
195     def vpp_add_map_resolver(node, map_resolver_ip):
196         """Set lisp map resolver on the VPP node in topology.
197
198         :param node: VPP node.
199         :param map_resolver_ip: IP address of the map resolver.
200         :type node: dict
201         :type map_resolver_ip: str
202         """
203
204         VatExecutor.cmd_from_template(node,
205                                       'lisp/add_lisp_map_resolver.vat',
206                                       address=map_resolver_ip)
207
208     @staticmethod
209     def vpp_del_map_resolver(node, map_resolver_ip):
210         """Unset lisp map resolver on the VPP node in topology.
211
212         :param node: VPP node.
213         :param map_resolver_ip: IP address of the map resolver.
214         :type node: dict
215         :type map_resolver_ip: str
216         """
217
218         VatExecutor.cmd_from_template(node,
219                                       'lisp/del_lisp_map_resolver.vat',
220                                       address=map_resolver_ip)
221
222
223 class LispLocalEid(object):
224     """Class for Lisp local eid API."""
225
226     def __init__(self):
227         pass
228
229     @staticmethod
230     def vpp_add_lisp_local_eid(node, locator_set_name, vni, eid,
231                                prefix_len=None):
232         """Set lisp eid address on the VPP node in topology.
233
234         :param node: VPP node.
235         :param locator_set_name: Name of the locator_set.
236         :param vni: vni value.
237         :param eid: Eid value.
238         :param prefix_len: prefix len if the eid is IP address.
239         :type node: dict
240         :type locator_set_name: str
241         :type vni: int
242         :type eid: str
243         :type prefix_len: int
244         """
245
246         if prefix_len is not None:
247             VatExecutor.cmd_from_template(node,
248                                           'lisp/add_lisp_local_eid.vat',
249                                           vni=vni,
250                                           eid=eid,
251                                           eid_prefix=prefix_len,
252                                           locator_name=locator_set_name)
253         else:
254             VatExecutor.cmd_from_template(node,
255                                           'lisp/add_lisp_local_eid_mac.vat',
256                                           vni=vni,
257                                           eid=eid,
258                                           locator_name=locator_set_name)
259
260     @staticmethod
261     def vpp_del_lisp_local_eid(node, locator_set_name, vni, eid,
262                                prefix_len=None):
263         """Set lisp eid addres on the VPP node in topology.
264
265         :param node: VPP node.
266         :param locator_set_name: Name of the locator_set.
267         :param vni: vni value.
268         :param eid: Eid value.
269         :param prefix_len: prefix len if the eid is IP address.
270         :type node: dict
271         :type locator_set_name: str
272         :type vni: int
273         :type eid: str
274         :type prefix_len: int
275         """
276
277         if prefix_len is not None:
278             VatExecutor.cmd_from_template(node,
279                                           'lisp/del_lisp_local_eid.vat',
280                                           vni=vni,
281                                           eid=eid,
282                                           eid_prefix=prefix_len,
283                                           locator_name=locator_set_name)
284         else:
285             VatExecutor.cmd_from_template(node,
286                                           'lisp/del_lisp_local_eid_mac.vat',
287                                           vni=vni,
288                                           eid=eid,
289                                           locator_name=locator_set_name)
290
291
292 class LispLocator(object):
293     """Class for the Lisp Locator API."""
294
295     def __init__(self):
296         pass
297
298     @staticmethod
299     def vpp_add_lisp_locator(node, locator_name, sw_if_index, priority, weight):
300         """Set lisp locator on the VPP node in topology.
301
302         :param node: VPP node.
303         :param locator_name: Name of the locator_set.
304         :param sw_if_index: sw_if_index if the interface.
305         :param priority: priority of the locator.
306         :param weight: weight of the locator.
307         :type node: dict
308         :type locator_name: str
309         :type sw_if_index: int
310         :type priority: int
311         :type weight: int
312         """
313
314         VatExecutor.cmd_from_template(node,
315                                       'lisp/add_lisp_locator.vat',
316                                       lisp_name=locator_name,
317                                       sw_if_index=sw_if_index,
318                                       priority=priority,
319                                       weight=weight)
320
321     @staticmethod
322     def vpp_del_lisp_locator(node, locator_name, sw_if_index, priority, weight):
323         """Unset lisp locator on the VPP node in topology.
324
325         :param node: VPP node.
326         :param locator_name: Name of the locator_set.
327         :param sw_if_index: sw_if_index if the interface.
328         :param priority: priority of the locator.
329         :param weight: weight of the locator.
330         :type node: dict
331         :type locator_name: str
332         :type sw_if_index: int
333         :type priority: int
334         :type weight: int
335         """
336
337         VatExecutor.cmd_from_template(node,
338                                       'lisp/del_lisp_locator.vat',
339                                       lisp_name=locator_name,
340                                       sw_if_index=sw_if_index,
341                                       priority=priority,
342                                       weight=weight)
343
344
345 class LispLocatorSet(object):
346     """Class for Lisp Locator Set API."""
347
348     def __init__(self):
349         pass
350
351     @staticmethod
352     def vpp_add_lisp_locator_set(node, name):
353         """Add lisp locator_set on VPP.
354
355         :param node: VPP node.
356         :param name: VPP locator name.
357         :type node: dict
358         :type name: str
359         """
360
361         VatExecutor.cmd_from_template(node,
362                                       'lisp/add_lisp_locator_set.vat',
363                                       lisp_name=name)
364
365     @staticmethod
366     def vpp_del_lisp_locator_set(node, name):
367         """Del lisp locator_set on VPP.
368
369         :param node: VPP node.
370         :param name: VPP locator name.
371         :type node: dict
372         :type name: str
373         """
374
375         VatExecutor.cmd_from_template(node,
376                                       'lisp/del_lisp_locator_set.vat',
377                                       lisp_name=name)
378
379
380 class LispSetup(object):
381     """Lisp setup in topology."""
382
383     def __init__(self):
384         pass
385
386     @staticmethod
387     def vpp_set_lisp_locator_set(node, locator_set_list):
388         """Set lisp locator_sets on VPP node in topology.
389
390         :param node: VPP node.
391         :param locator_set_list: List of locator_set.
392         :type node: dict
393         :type locator_set_list: list
394         """
395
396         if node['type'] != NodeType.DUT:
397             raise ValueError('Node is not DUT')
398
399         lisp_locator = LispLocator()
400         lisp_locator_set = LispLocatorSet()
401         for locator_set in locator_set_list:
402             locator_set_name = locator_set.get('locator-set')
403             locator_list = locator_set.get('locator')
404             lisp_locator_set.vpp_add_lisp_locator_set(node,
405                                                       locator_set_name)
406             for locator in locator_list:
407                 sw_if_index = locator.get('locator-index')
408                 priority = locator.get('priority')
409                 weight = locator.get('weight')
410                 lisp_locator.vpp_add_lisp_locator(node,
411                                                   locator_set_name,
412                                                   sw_if_index,
413                                                   priority,
414                                                   weight)
415
416     @staticmethod
417     def vpp_unset_lisp_locator_set(node, locator_set_list):
418         """Unset lisp locator_sets on VPP node in topology.
419
420         :param node: VPP node.
421         :param locator_set_list: List of locator_set.
422         :type node: dict
423         :type locator_set_list: list
424         """
425
426         if node['type'] != NodeType.DUT:
427             raise ValueError('Lisp locator set, node is not DUT')
428
429         lisp_locator = LispLocator()
430         lisp_locator_set = LispLocatorSet()
431         for locator_set in locator_set_list:
432             locator_set_name = locator_set.get('locator-set')
433             locator_list = locator_set.get('locator')
434             for locator in locator_list:
435                 sw_if_index = locator.get('locator-index')
436                 priority = locator.get('priority')
437                 weight = locator.get('weight')
438                 lisp_locator.vpp_del_lisp_locator(node,
439                                                   locator_set_name,
440                                                   sw_if_index,
441                                                   priority,
442                                                   weight)
443
444             lisp_locator_set.vpp_del_lisp_locator_set(node,
445                                                       locator_set_name)
446
447     @staticmethod
448     def vpp_set_lisp_eid_table(node, eid_table):
449         """Set lisp eid tables on VPP node in topology.
450
451         :param node: VPP node.
452         :param eid_table: Dictionary containing information of eid_table.
453         :type node: dict
454         :type eid_table: dict
455         """
456
457         if node['type'] != NodeType.DUT:
458             raise ValueError('Node is not DUT')
459
460         lisp_locator_set = LispLocatorSet()
461         lisp_eid = LispLocalEid()
462         for eid in eid_table:
463             vni = eid.get('vni')
464             eid_address = eid.get('eid')
465             eid_prefix_len = eid.get('eid-prefix-len')
466             locator_set_name = eid.get('locator-set')
467             lisp_locator_set.vpp_add_lisp_locator_set(node, locator_set_name)
468             lisp_eid.vpp_add_lisp_local_eid(node,
469                                             locator_set_name,
470                                             vni,
471                                             eid_address,
472                                             eid_prefix_len)
473
474     @staticmethod
475     def vpp_unset_lisp_eid_table(node, eid_table):
476         """Unset lisp eid tables on VPP node in topology.
477
478         :param node: VPP node.
479         :param eid_table: Dictionary containing information of eid_table.
480         :type node: dict
481         :type eid_table: dict
482         """
483
484         if node['type'] != NodeType.DUT:
485             raise ValueError('Node is not DUT')
486
487         locator_set_list = []
488         lisp_locator_set = LispLocatorSet()
489         lisp_eid = LispLocalEid()
490         for eid in eid_table:
491             vni = eid.get('vni')
492             eid_address = eid.get('eid')
493             eid_prefix_len = eid.get('eid-prefix-len')
494             locator_set_name = eid.get('locator-set')
495             if locator_set_name not in locator_set_list:
496                 locator_set_list.append(locator_set_name)
497
498             lisp_eid.vpp_del_lisp_local_eid(node,
499                                             locator_set_name,
500                                             vni,
501                                             eid_address,
502                                             eid_prefix_len)
503
504         for locator_set_name in locator_set_list:
505             lisp_locator_set.vpp_del_lisp_locator_set(node, locator_set_name)
506
507     @staticmethod
508     def vpp_set_lisp_map_resolver(node, map_resolver):
509         """Set lisp map resolvers on VPP node in topology.
510
511         :param node: VPP node.
512         :param map_resolver: Dictionary containing information of map resolver.
513         :type node: dict
514         :type map_resolver: dict
515         """
516
517         lisp_map_res = LispMapResolver()
518         for map_ip in map_resolver:
519             lisp_map_res.vpp_add_map_resolver(node, map_ip.get('map resolver'))
520
521     @staticmethod
522     def vpp_unset_lisp_map_resolver(node, map_resolver):
523         """Unset lisp map resolvers on VPP node in topology.
524
525         :param node: VPP node.
526         :param map_resolver: Dictionary containing information of map resolver.
527         :type node: dict
528         :type map_resolver: dict
529         """
530
531         lisp_map_res = LispMapResolver()
532         for map_ip in map_resolver:
533             lisp_map_res.vpp_del_map_resolver(node, map_ip.get('map resolver'))
534
535     @staticmethod
536     def vpp_lisp_gpe_interface_status(node, state):
537         """Set lisp gpe interface status on VPP node in topology.
538
539         :param node: VPP node.
540         :param state: State of the gpe iface, up or down
541         :type node: dict
542         :type state: str
543         """
544
545         lgi = LispGpeIface()
546         lgi.vpp_lisp_gpe_iface(node, state)