Update Honeycomb tests
[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 LispGpeIface(object):
21     """Class for Lisp gpe interface API."""
22
23     def __init__(self):
24         pass
25
26     @staticmethod
27     def vpp_lisp_gpe_iface(node, state):
28         """Set lisp gpe interface up or down on the VPP node in topology.
29
30         :param node: VPP node.
31         :param state: State of the gpe iface, up or down
32         :type node: dict
33         :type state: str
34         """
35
36         VatExecutor.cmd_from_template(node,
37                                       'lisp/lisp_gpe_iface.vat',
38                                       state=state)
39
40
41 class LispMapResolver(object):
42     """Class for Lisp map resolver API."""
43
44     def __init__(self):
45         pass
46
47     @staticmethod
48     def vpp_add_map_resolver(node, map_resolver_ip):
49         """Set lisp map resolver on the VPP node in topology.
50
51         :param node: VPP node.
52         :param map_resolver_ip: IP address of the map resolver.
53         :type node: dict
54         :type map_resolver_ip: str
55         """
56
57         VatExecutor.cmd_from_template(node,
58                                       'lisp/add_lisp_map_resolver.vat',
59                                       address=map_resolver_ip)
60
61     @staticmethod
62     def vpp_del_map_resolver(node, map_resolver_ip):
63         """Unset lisp map resolver on the VPP node in topology.
64
65         :param node: VPP node.
66         :param map_resolver_ip: IP address of the map resolver.
67         :type node: dict
68         :type map_resolver_ip: str
69         """
70
71         VatExecutor.cmd_from_template(node,
72                                       'lisp/del_lisp_map_resolver.vat',
73                                       address=map_resolver_ip)
74
75
76 class LispLocalEid(object):
77     """Class for Lisp local eid API."""
78
79     def __init__(self):
80         pass
81
82     @staticmethod
83     def vpp_add_lisp_local_eid(node, locator_set_name, address, prefix_len):
84         """Set lisp eid addres on the VPP node in topology.
85
86         :param node: VPP node.
87         :param locator_set_name: Name of the locator_set.
88         :param address: Eid IP address.
89         :param prefix_len: prefix len of the eid IP address.
90         :type node: dict
91         :type locator_set_name: str
92         :type address: str
93         :type prefix_len: int
94         """
95
96         VatExecutor.cmd_from_template(node,
97                                       'lisp/add_lisp_local_eid.vat',
98                                       eid_address=address,
99                                       eid_prefix=prefix_len,
100                                       locator_name=locator_set_name)
101
102     @staticmethod
103     def vpp_del_lisp_local_eid(node, locator_set_name, address, prefix_len):
104         """Set lisp eid addres on the VPP node in topology.
105
106         :param node: VPP node.
107         :param locator_set_name: Name of the locator_set.
108         :param address: Eid IP address.
109         :param prefix_len: prefix len of the eid IP address.
110         :type node: dict
111         :type locator_set_name: str
112         :type address: str
113         :type prefix_len: int
114         """
115
116         VatExecutor.cmd_from_template(node,
117                                       'lisp/del_lisp_local_eid.vat',
118                                       eid_address=address,
119                                       eid_prefix=prefix_len,
120                                       locator_name=locator_set_name)
121
122
123 class LispLocator(object):
124     """Class for the Lisp Locator API."""
125
126     def __init__(self):
127         pass
128
129     @staticmethod
130     def vpp_add_lisp_locator(node, locator_name, sw_if_index, priority, weight):
131         """Set lisp locator on the VPP node in topology.
132
133         :param node: VPP node.
134         :param locator_name: Name of the locator_set.
135         :param sw_if_index: sw_if_index if the interface.
136         :param priority: priority of the locator.
137         :param weight: weight of the locator.
138         :type node: dict
139         :type locator_name: str
140         :type sw_if_index: int
141         :type priority: int
142         :type weight: int
143         """
144
145         VatExecutor.cmd_from_template(node,
146                                       'lisp/add_lisp_locator.vat',
147                                       lisp_name=locator_name,
148                                       sw_if_index=sw_if_index,
149                                       priority=priority,
150                                       weight=weight)
151
152     @staticmethod
153     def vpp_del_lisp_locator(node, locator_name, sw_if_index, priority, weight):
154         """Unset lisp locator on the VPP node in topology.
155
156         :param node: VPP node.
157         :param locator_name: Name of the locator_set.
158         :param sw_if_index: sw_if_index if the interface.
159         :param priority: priority of the locator.
160         :param weight: weight of the locator.
161         :type node: dict
162         :type locator_name: str
163         :type sw_if_index: int
164         :type priority: int
165         :type weight: int
166         """
167
168         VatExecutor.cmd_from_template(node,
169                                       'lisp/del_lisp_locator.vat',
170                                       lisp_name=locator_name,
171                                       sw_if_index=sw_if_index,
172                                       priority=priority,
173                                       weight=weight)
174
175
176 class LispLocatorSet(object):
177     """Class for Lisp Locator Set API."""
178
179     def __init__(self):
180         pass
181
182     @staticmethod
183     def vpp_add_lisp_locator_set(node, name):
184         """Add lisp locator_set on VPP.
185
186         :param node: VPP node.
187         :param name: VPP locator name.
188         :type node: dict
189         :type name: str
190         """
191
192         VatExecutor.cmd_from_template(node,
193                                       'lisp/add_lisp_locator_set.vat',
194                                       lisp_name=name)
195
196     @staticmethod
197     def vpp_del_lisp_locator_set(node, name):
198         """Del lisp locator_set on VPP.
199
200         :param node: VPP node.
201         :param name: VPP locator name.
202         :type node: dict
203         :type name: str
204         """
205
206         VatExecutor.cmd_from_template(node,
207                                       'lisp/del_lisp_locator_set.vat',
208                                       lisp_name=name)
209
210
211 class LispSetup(object):
212     """Lisp setup in topology."""
213
214     def __init__(self):
215         pass
216
217     @staticmethod
218     def vpp_set_lisp_locator_set(node, locator_sets):
219         """Set lisp locator_sets on VPP node in topology.
220
221         There are two ways how set lisp locator_set.
222         One locator_set can contain multiple locators.
223         Add new locator_set with same name as it already save in vpp
224         will reset locator_set and his locator.
225         To not reset locator_set, use "normal" type of generate data,
226         where the locator_set is set just once.
227         To test reset locator_set use "reset" type of generate data,
228         where the locator_set will be set multiple times.
229
230         :param node: VPP node.
231         :param locator_sets: Dictionary containing information of locator_set.
232         :type node: dict
233         :type locator_sets: dict
234         """
235
236         if node['type'] != NodeType.DUT:
237             raise ValueError('Lisp locator set, node is not DUT')
238
239         lisp_locator_set = LispLocatorSet()
240         lisp_locator = LispLocator()
241         for locator_set_type, item in locator_sets.iteritems():
242             if locator_set_type == 'normal':
243                 lsm_set = 1
244             elif locator_set_type == 'reset':
245                 lsm_set = 0
246             else:
247                 raise ValueError('Unknown locator_set_type value: '
248                                  '{}'.format(locator_set_type))
249
250             locator_set_list = []
251             for locator_set in item:
252                 locator_name = locator_set.get('locator-set')
253                 sw_if_index = locator_set.get('locator')
254                 priority = locator_set.get('priority')
255                 weight = locator_set.get('weight')
256                 if lsm_set == 1:
257                     if locator_name not in locator_set_list:
258                         locator_set_list.append(locator_name)
259                         lisp_locator_set.vpp_add_lisp_locator_set(node,
260                                                                   locator_name)
261                 else:
262                     lisp_locator_set.vpp_add_lisp_locator_set(node,
263                                                               locator_name)
264                 lisp_locator.vpp_add_lisp_locator(node,
265                                                   locator_name,
266                                                   sw_if_index,
267                                                   priority,
268                                                   weight)
269
270     @staticmethod
271     def vpp_unset_lisp_locator_set(node, locator_sets):
272         """Unset lisp locator_sets on VPP node in topology.
273
274         :param node: VPP node.
275         :param locator_sets: Dictionary containing information of locator_set.
276         :type node: dict
277         :type locator_sets: dict
278         """
279
280         if node['type'] != NodeType.DUT:
281             raise ValueError('Lisp locator set, node is not DUT')
282
283         lisp_locator = LispLocator()
284         lisp_locator_set = LispLocatorSet()
285         for locator_set_type, item in locator_sets.iteritems():
286             if locator_set_type == 'normal':
287                 lsm_set = 1
288             elif locator_set_type == 'reset':
289                 lsm_set = 0
290             else:
291                 raise ValueError('Unknown locator_set_type value: '
292                                  '{}'.format(locator_set_type))
293
294             locator_set_list = []
295             for locator_set in item:
296                 locator_set_name = locator_set.get('locator-set')
297                 sw_if_index = locator_set.get('locator')
298                 priority = locator_set.get('priority')
299                 weight = locator_set.get('weight')
300                 if lsm_set == 1:
301                     if locator_set_name not in locator_set_list:
302                         locator_set_list.append(locator_set_name)
303                 else:
304                     lisp_locator.vpp_del_lisp_locator(node,
305                                                       locator_set_name,
306                                                       sw_if_index,
307                                                       priority,
308                                                       weight)
309
310         for locator_set_name in locator_set_list:
311             lisp_locator_set.vpp_del_lisp_locator_set(node, locator_set_name)
312
313     @staticmethod
314     def vpp_set_lisp_eid_table(node, eid_table):
315         """Set lisp eid tables on VPP node in topology.
316
317         :param node: VPP node.
318         :param eid_table: Dictionary containing information of eid_table.
319         :type node: dict
320         :type eid_table: dict
321         """
322
323         if node['type'] != NodeType.DUT:
324             raise ValueError('Lisp locator set, node is not DUT')
325
326         lisp_locator_set = LispLocatorSet()
327         lisp_eid = LispLocalEid()
328         for eid in eid_table:
329             eid_address = eid.get('eid address')
330             eid_prefix_len = eid.get('eid prefix len')
331             locator_set_name = eid.get('locator-set')
332             lisp_locator_set.vpp_add_lisp_locator_set(node, locator_set_name)
333             lisp_eid.vpp_add_lisp_local_eid(node,
334                                             locator_set_name,
335                                             eid_address,
336                                             eid_prefix_len)
337
338     @staticmethod
339     def vpp_unset_lisp_eid_table(node, eid_table):
340         """Unset lisp eid tables on VPP node in topology.
341
342         :param node: VPP node.
343         :param eid_table: Dictionary containing information of eid_table.
344         :type node: dict
345         :type eid_table: dict
346         """
347
348         if node['type'] != NodeType.DUT:
349             raise ValueError('Lisp locator set, node is not DUT')
350
351         locator_set_list = []
352         lisp_locator_set = LispLocatorSet()
353         lisp_eid = LispLocalEid()
354         for eid in eid_table:
355             eid_address = eid.get('eid address')
356             eid_prefix_len = eid.get('eid prefix len')
357             locator_set_name = eid.get('locator-set')
358             if locator_set_name not in locator_set_list:
359                 locator_set_list.append(locator_set_name)
360
361             lisp_eid.vpp_del_lisp_local_eid(node,
362                                             locator_set_name,
363                                             eid_address,
364                                             eid_prefix_len)
365
366         for locator_set_name in locator_set_list:
367             lisp_locator_set.vpp_del_lisp_locator_set(node, locator_set_name)
368
369     @staticmethod
370     def vpp_set_lisp_map_resolver(node, map_resolver):
371         """Set lisp map resolvers on VPP node in topology.
372
373         :param node: VPP node.
374         :param map_resolver: Dictionary containing information of map resolver.
375         :type node: dict
376         :type map_resolver: dict
377         """
378
379         lisp_map_res = LispMapResolver()
380         for map_ip in map_resolver:
381             lisp_map_res.vpp_add_map_resolver(node, map_ip.get('map resolver'))
382
383     @staticmethod
384     def vpp_unset_lisp_map_resolver(node, map_resolver):
385         """Unset lisp map resolvers on VPP node in topology.
386
387         :param node: VPP node.
388         :param map_resolver: Dictionary containing information of map resolver.
389         :type node: dict
390         :type map_resolver: dict
391         """
392
393         lisp_map_res = LispMapResolver()
394         for map_ip in map_resolver:
395             lisp_map_res.vpp_del_map_resolver(node, map_ip.get('map resolver'))
396
397     @staticmethod
398     def vpp_lisp_gpe_interface_status(node, state):
399         """Set lisp gpe interface status on VPP node in topology.
400
401         :param node: VPP node.
402         :param state: State of the gpe iface, up or down
403         :type node: dict
404         :type state: str
405         """
406
407         lgi = LispGpeIface()
408         lgi.vpp_lisp_gpe_iface(node, state)