82e1a9787a6c4bac765b26612515cf87834239a0
[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_predix=deid_prefix,
103                                       seid=seid,
104                                       seid_prefix=seid_prefix,
105                                       rloc=rloc)
106
107
108 class LispGpeIface(object):
109     """Class for Lisp gpe interface API."""
110
111     def __init__(self):
112         pass
113
114     @staticmethod
115     def vpp_lisp_gpe_iface(node, state):
116         """Set lisp gpe interface up or down on the VPP node in topology.
117
118         :param node: VPP node.
119         :param state: State of the gpe iface, up or down.
120         :type node: dict
121         :type state: str
122         """
123
124         VatExecutor.cmd_from_template(node,
125                                       'lisp/lisp_gpe_iface.vat',
126                                       state=state)
127
128
129 class LispMapResolver(object):
130     """Class for Lisp map resolver API."""
131
132     def __init__(self):
133         pass
134
135     @staticmethod
136     def vpp_add_map_resolver(node, map_resolver_ip):
137         """Set lisp map resolver on the VPP node in topology.
138
139         :param node: VPP node.
140         :param map_resolver_ip: IP address of the map resolver.
141         :type node: dict
142         :type map_resolver_ip: str
143         """
144
145         VatExecutor.cmd_from_template(node,
146                                       'lisp/add_lisp_map_resolver.vat',
147                                       address=map_resolver_ip)
148
149     @staticmethod
150     def vpp_del_map_resolver(node, map_resolver_ip):
151         """Unset lisp map resolver on the VPP node in topology.
152
153         :param node: VPP node.
154         :param map_resolver_ip: IP address of the map resolver.
155         :type node: dict
156         :type map_resolver_ip: str
157         """
158
159         VatExecutor.cmd_from_template(node,
160                                       'lisp/del_lisp_map_resolver.vat',
161                                       address=map_resolver_ip)
162
163
164 class LispLocalEid(object):
165     """Class for Lisp local eid API."""
166
167     def __init__(self):
168         pass
169
170     @staticmethod
171     def vpp_add_lisp_local_eid(node, locator_set_name, vni, eid,
172                                prefix_len=None):
173         """Set lisp eid address on the VPP node in topology.
174
175         :param node: VPP node.
176         :param locator_set_name: Name of the locator_set.
177         :param vni: vni value.
178         :param eid: Eid value.
179         :param prefix_len: prefix len if the eid is IP address.
180         :type node: dict
181         :type locator_set_name: str
182         :type vni: int
183         :type eid: str
184         :type prefix_len: int
185         """
186
187         if prefix_len is not None:
188             VatExecutor.cmd_from_template(node,
189                                           'lisp/add_lisp_local_eid.vat',
190                                           vni=vni,
191                                           eid=eid,
192                                           eid_prefix=prefix_len,
193                                           locator_name=locator_set_name)
194         else:
195             VatExecutor.cmd_from_template(node,
196                                           'lisp/add_lisp_local_eid_mac.vat',
197                                           vni=vni,
198                                           eid=eid,
199                                           locator_name=locator_set_name)
200
201     @staticmethod
202     def vpp_del_lisp_local_eid(node, locator_set_name, vni, eid,
203                                prefix_len=None):
204         """Set lisp eid addres on the VPP node in topology.
205
206         :param node: VPP node.
207         :param locator_set_name: Name of the locator_set.
208         :param vni: vni value.
209         :param eid: Eid value.
210         :param prefix_len: prefix len if the eid is IP address.
211         :type node: dict
212         :type locator_set_name: str
213         :type vni: int
214         :type eid: str
215         :type prefix_len: int
216         """
217
218         if prefix_len is not None:
219             VatExecutor.cmd_from_template(node,
220                                           'lisp/del_lisp_local_eid.vat',
221                                           vni=vni,
222                                           eid=eid,
223                                           eid_prefix=prefix_len,
224                                           locator_name=locator_set_name)
225         else:
226             VatExecutor.cmd_from_template(node,
227                                           'lisp/del_lisp_local_eid_mac.vat',
228                                           vni=vni,
229                                           eid=eid,
230                                           locator_name=locator_set_name)
231
232
233 class LispLocator(object):
234     """Class for the Lisp Locator API."""
235
236     def __init__(self):
237         pass
238
239     @staticmethod
240     def vpp_add_lisp_locator(node, locator_name, sw_if_index, priority, weight):
241         """Set lisp locator on the VPP node in topology.
242
243         :param node: VPP node.
244         :param locator_name: Name of the locator_set.
245         :param sw_if_index: sw_if_index if the interface.
246         :param priority: priority of the locator.
247         :param weight: weight of the locator.
248         :type node: dict
249         :type locator_name: str
250         :type sw_if_index: int
251         :type priority: int
252         :type weight: int
253         """
254
255         VatExecutor.cmd_from_template(node,
256                                       'lisp/add_lisp_locator.vat',
257                                       lisp_name=locator_name,
258                                       sw_if_index=sw_if_index,
259                                       priority=priority,
260                                       weight=weight)
261
262     @staticmethod
263     def vpp_del_lisp_locator(node, locator_name, sw_if_index, priority, weight):
264         """Unset lisp locator on the VPP node in topology.
265
266         :param node: VPP node.
267         :param locator_name: Name of the locator_set.
268         :param sw_if_index: sw_if_index if the interface.
269         :param priority: priority of the locator.
270         :param weight: weight of the locator.
271         :type node: dict
272         :type locator_name: str
273         :type sw_if_index: int
274         :type priority: int
275         :type weight: int
276         """
277
278         VatExecutor.cmd_from_template(node,
279                                       'lisp/del_lisp_locator.vat',
280                                       lisp_name=locator_name,
281                                       sw_if_index=sw_if_index,
282                                       priority=priority,
283                                       weight=weight)
284
285
286 class LispLocatorSet(object):
287     """Class for Lisp Locator Set API."""
288
289     def __init__(self):
290         pass
291
292     @staticmethod
293     def vpp_add_lisp_locator_set(node, name):
294         """Add lisp locator_set on VPP.
295
296         :param node: VPP node.
297         :param name: VPP locator name.
298         :type node: dict
299         :type name: str
300         """
301
302         VatExecutor.cmd_from_template(node,
303                                       'lisp/add_lisp_locator_set.vat',
304                                       lisp_name=name)
305
306     @staticmethod
307     def vpp_del_lisp_locator_set(node, name):
308         """Del lisp locator_set on VPP.
309
310         :param node: VPP node.
311         :param name: VPP locator name.
312         :type node: dict
313         :type name: str
314         """
315
316         VatExecutor.cmd_from_template(node,
317                                       'lisp/del_lisp_locator_set.vat',
318                                       lisp_name=name)
319
320
321 class LispSetup(object):
322     """Lisp setup in topology."""
323
324     def __init__(self):
325         pass
326
327     @staticmethod
328     def vpp_set_lisp_locator_set(node, locator_set_list):
329         """Set lisp locator_sets on VPP node in topology.
330
331         :param node: VPP node.
332         :param locator_set_list: List of locator_set.
333         :type node: dict
334         :type locator_set_list: list
335         """
336
337         if node['type'] != NodeType.DUT:
338             raise ValueError('Node is not DUT')
339
340         lisp_locator = LispLocator()
341         lisp_locator_set = LispLocatorSet()
342         for locator_set in locator_set_list:
343             locator_set_name = locator_set.get('locator-set')
344             locator_list = locator_set.get('locator')
345             lisp_locator_set.vpp_add_lisp_locator_set(node,
346                                                       locator_set_name)
347             for locator in locator_list:
348                 sw_if_index = locator.get('locator-index')
349                 priority = locator.get('priority')
350                 weight = locator.get('weight')
351                 lisp_locator.vpp_add_lisp_locator(node,
352                                                   locator_set_name,
353                                                   sw_if_index,
354                                                   priority,
355                                                   weight)
356
357     @staticmethod
358     def vpp_unset_lisp_locator_set(node, locator_set_list):
359         """Unset lisp locator_sets on VPP node in topology.
360
361         :param node: VPP node.
362         :param locator_set_list: List of locator_set.
363         :type node: dict
364         :type locator_set_list: list
365         """
366
367         if node['type'] != NodeType.DUT:
368             raise ValueError('Lisp locator set, node is not DUT')
369
370         lisp_locator = LispLocator()
371         lisp_locator_set = LispLocatorSet()
372         for locator_set in locator_set_list:
373             locator_set_name = locator_set.get('locator-set')
374             locator_list = locator_set.get('locator')
375             for locator in locator_list:
376                 sw_if_index = locator.get('locator-index')
377                 priority = locator.get('priority')
378                 weight = locator.get('weight')
379                 lisp_locator.vpp_del_lisp_locator(node,
380                                                   locator_set_name,
381                                                   sw_if_index,
382                                                   priority,
383                                                   weight)
384
385             lisp_locator_set.vpp_del_lisp_locator_set(node,
386                                                       locator_set_name)
387
388     @staticmethod
389     def vpp_set_lisp_eid_table(node, eid_table):
390         """Set lisp eid tables on VPP node in topology.
391
392         :param node: VPP node.
393         :param eid_table: Dictionary containing information of eid_table.
394         :type node: dict
395         :type eid_table: dict
396         """
397
398         if node['type'] != NodeType.DUT:
399             raise ValueError('Node is not DUT')
400
401         lisp_locator_set = LispLocatorSet()
402         lisp_eid = LispLocalEid()
403         for eid in eid_table:
404             vni = eid.get('vni')
405             eid_address = eid.get('eid')
406             eid_prefix_len = eid.get('eid-prefix-len')
407             locator_set_name = eid.get('locator-set')
408             lisp_locator_set.vpp_add_lisp_locator_set(node, locator_set_name)
409             lisp_eid.vpp_add_lisp_local_eid(node,
410                                             locator_set_name,
411                                             vni,
412                                             eid_address,
413                                             eid_prefix_len)
414
415     @staticmethod
416     def vpp_unset_lisp_eid_table(node, eid_table):
417         """Unset lisp eid tables on VPP node in topology.
418
419         :param node: VPP node.
420         :param eid_table: Dictionary containing information of eid_table.
421         :type node: dict
422         :type eid_table: dict
423         """
424
425         if node['type'] != NodeType.DUT:
426             raise ValueError('Node is not DUT')
427
428         locator_set_list = []
429         lisp_locator_set = LispLocatorSet()
430         lisp_eid = LispLocalEid()
431         for eid in eid_table:
432             vni = eid.get('vni')
433             eid_address = eid.get('eid')
434             eid_prefix_len = eid.get('eid-prefix-len')
435             locator_set_name = eid.get('locator-set')
436             if locator_set_name not in locator_set_list:
437                 locator_set_list.append(locator_set_name)
438
439             lisp_eid.vpp_del_lisp_local_eid(node,
440                                             locator_set_name,
441                                             vni,
442                                             eid_address,
443                                             eid_prefix_len)
444
445         for locator_set_name in locator_set_list:
446             lisp_locator_set.vpp_del_lisp_locator_set(node, locator_set_name)
447
448     @staticmethod
449     def vpp_set_lisp_map_resolver(node, map_resolver):
450         """Set lisp map resolvers on VPP node in topology.
451
452         :param node: VPP node.
453         :param map_resolver: Dictionary containing information of map resolver.
454         :type node: dict
455         :type map_resolver: dict
456         """
457
458         lisp_map_res = LispMapResolver()
459         for map_ip in map_resolver:
460             lisp_map_res.vpp_add_map_resolver(node, map_ip.get('map resolver'))
461
462     @staticmethod
463     def vpp_unset_lisp_map_resolver(node, map_resolver):
464         """Unset lisp map resolvers on VPP node in topology.
465
466         :param node: VPP node.
467         :param map_resolver: Dictionary containing information of map resolver.
468         :type node: dict
469         :type map_resolver: dict
470         """
471
472         lisp_map_res = LispMapResolver()
473         for map_ip in map_resolver:
474             lisp_map_res.vpp_del_map_resolver(node, map_ip.get('map resolver'))
475
476     @staticmethod
477     def vpp_lisp_gpe_interface_status(node, state):
478         """Set lisp gpe interface status on VPP node in topology.
479
480         :param node: VPP node.
481         :param state: State of the gpe iface, up or down
482         :type node: dict
483         :type state: str
484         """
485
486         lgi = LispGpeIface()
487         lgi.vpp_lisp_gpe_iface(node, state)