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