FIX: Remove old restart sequence - Honeycomb
[csit.git] / resources / libraries / python / LispUtil.py
1 # Copyright (c) 2018 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 """Lisp utilities library."""
15
16 from resources.libraries.python.parsers.JsonParser import JsonParser
17 from resources.libraries.python.topology import Topology
18 from resources.libraries.python.VatExecutor import VatExecutor, VatTerminal
19
20
21 class LispUtil(object):
22     """Implements keywords for Lisp tests."""
23
24     def __init__(self):
25         pass
26
27     @staticmethod
28     def vpp_show_lisp_state(node):
29         """Get lisp state from VPP node.
30
31         :param node: VPP node.
32         :type node: dict
33         :returns: Lisp gpe state.
34         :rtype: list
35         """
36
37         vat = VatExecutor()
38         vat.execute_script_json_out('lisp/show_lisp_status.vat',
39                                     node)
40         return JsonParser().parse_data(vat.get_script_stdout())
41
42     @staticmethod
43     def vpp_show_lisp_locator_set(node, items_filter):
44         """Get lisp locator_set from VPP node.
45
46         :param node: VPP node.
47         :param items_filter: Filter which specifies which items should be
48             retrieved - local, remote, empty string = both.
49         :type node: dict
50         :type items_filter: str
51         :returns: Lisp locator_set data as python list.
52         :rtype: list
53         """
54
55         try:
56             with VatTerminal(node) as vat:
57                 response = vat.vat_terminal_exec_cmd_from_template(
58                     'lisp/show_lisp_locator_set.vat', filter=items_filter)
59             return response[0]
60         except ValueError:
61             return []
62
63     @staticmethod
64     def vpp_show_lisp_eid_table(node):
65         """Get lisp eid table from VPP node.
66
67         :param node: VPP node.
68         :type node: dict
69         :returns: Lisp eid table as python list.
70         :rtype: list
71         """
72
73         vat = VatExecutor()
74         vat.execute_script_json_out('lisp/show_lisp_eid_table.vat', node)
75         return JsonParser().parse_data(vat.get_script_stdout())
76
77     @staticmethod
78     def vpp_show_lisp_map_resolver(node):
79         """Get lisp map resolver from VPP node.
80
81         :param node: VPP node.
82         :type node: dict
83         :returns: Lisp map resolver as python list.
84         :rtype: list
85         """
86
87         vat = VatExecutor()
88         vat.execute_script_json_out('lisp/show_lisp_map_resolver.vat', node)
89         return JsonParser().parse_data(vat.get_script_stdout())
90
91     @staticmethod
92     def vpp_show_lisp_map_register(node):
93         """Get LISP Map Register from VPP node.
94
95         :param node: VPP node.
96         :type node: dict
97         :returns: LISP Map Register as python list.
98         :rtype: list
99         """
100
101         vat = VatExecutor()
102         vat.execute_script_json_out('lisp/show_lisp_map_register.vat', node)
103         return JsonParser().parse_data(vat.get_script_stdout())
104
105     @staticmethod
106     def vpp_show_lisp_map_request_mode(node):
107         """Get LISP Map Request mode from VPP node.
108
109         :param node: VPP node.
110         :type node: dict
111         :returns: LISP Map Request mode as python list.
112         :rtype: list
113         """
114
115         vat = VatExecutor()
116         vat.execute_script_json_out('lisp/show_lisp_map_request_mode.vat', node)
117         return JsonParser().parse_data(vat.get_script_stdout())
118
119     @staticmethod
120     def vpp_show_lisp_map_server(node):
121         """Get LISP Map Server from VPP node.
122
123         :param node: VPP node.
124         :type node: dict
125         :returns: LISP Map Server as python list.
126         :rtype: list
127         """
128
129         vat = VatExecutor()
130         vat.execute_script_json_out('lisp/show_lisp_map_server.vat', node)
131         return JsonParser().parse_data(vat.get_script_stdout())
132
133     @staticmethod
134     def vpp_show_lisp_petr_config(node):
135         """Get LISP PETR configuration from VPP node.
136
137         :param node: VPP node.
138         :type node: dict
139         :returns: LISP PETR configuration as python list.
140         :rtype: list
141         """
142
143         vat = VatExecutor()
144         vat.execute_script_json_out('lisp/show_lisp_petr_config.vat', node)
145         return JsonParser().parse_data(vat.get_script_stdout())
146
147     @staticmethod
148     def vpp_show_lisp_rloc_config(node):
149         """Get LISP RLOC configuration from VPP node.
150
151         :param node: VPP node.
152         :type node: dict
153         :returns: LISP RLOC configuration as python list.
154         :rtype: list
155         """
156
157         vat = VatExecutor()
158         vat.execute_script_json_out('lisp/show_lisp_rloc_config.vat', node)
159         return JsonParser().parse_data(vat.get_script_stdout())
160
161     @staticmethod
162     def vpp_show_lisp_pitr(node):
163         """Get Lisp PITR feature config from VPP node.
164
165         :param node: VPP node.
166         :type node: dict
167         :returns: Lisp PITR config data.
168         :rtype: dict
169         """
170
171         vat = VatExecutor()
172         vat.execute_script_json_out('lisp/show_lisp_pitr.vat', node)
173         return JsonParser().parse_data(vat.get_script_stdout())
174
175     @staticmethod
176     def lisp_should_be_equal(lisp_val1, lisp_val2):
177         """Fail if the lisp values are not equal.
178
179         :param lisp_val1: First lisp value.
180         :param lisp_val2: Second lisp value.
181         :type lisp_val1: list
182         :type lisp_val2: list
183         """
184
185         len1 = len(lisp_val1)
186         len2 = len(lisp_val2)
187         if len1 != len2:
188             raise RuntimeError('Values are not same. '
189                                'Value 1 {} \n'
190                                'Value 2 {}.'.format(lisp_val1,
191                                                     lisp_val2))
192
193         for tmp in lisp_val1:
194             if tmp not in lisp_val2:
195                 raise RuntimeError('Value {} not found in vpp:\n'
196                                    '{}'.format(tmp, lisp_val2))
197
198     def lisp_locator_s_should_be_equal(self, locator_set1, locator_set2):
199         """Fail if the lisp values are not equal.
200
201         :param locator_set1: Generate lisp value.
202         :param locator_set2: Lisp value from VPP.
203         :type locator_set1: list
204         :type locator_set2: list
205         """
206
207         locator_set_list = []
208         for item in locator_set1:
209             if item not in locator_set_list:
210                 locator_set_list.append(item)
211         self.lisp_should_be_equal(locator_set_list, locator_set2)
212
213     @staticmethod
214     def generate_unique_lisp_locator_set_data(node, locator_set_number):
215         """Generate a list of lisp locator_set we want set to VPP and
216         then check if it is set correctly. All locator_sets are unique.
217
218         :param node: VPP node.
219         :param locator_set_number: Generate n locator_set.
220         :type node: dict
221         :type locator_set_number: str
222         :returns: list of lisp locator_set, list of lisp locator_set expected
223             from VAT.
224         :rtype: tuple
225         """
226
227         topo = Topology()
228
229         locator_set_list = []
230         locator_set_list_vat = []
231         i = 0
232         for num in range(0, int(locator_set_number)):
233             locator_list = []
234             for interface in node['interfaces'].values():
235                 link = interface.get('link')
236                 i += 1
237                 if link is None:
238                     continue
239
240                 if_name = topo.get_interface_by_link_name(node, link)
241                 sw_if_index = topo.get_interface_sw_index(node, if_name)
242                 if if_name is not None:
243                     locator = {'locator-index': sw_if_index,
244                                'priority': i,
245                                'weight': i}
246                     locator_list.append(locator)
247
248             l_name = 'ls{0}'.format(num)
249             locator_set = {'locator-set': l_name,
250                            'locator': locator_list}
251             locator_set_list.append(locator_set)
252
253             locator_set_vat = {"ls_name": l_name,
254                                "ls_index": num}
255             locator_set_list_vat.append(locator_set_vat)
256
257         return locator_set_list, locator_set_list_vat
258
259     @staticmethod
260     def generate_duplicate_lisp_locator_set_data(node, locator_set_number):
261         """Generate a list of lisp locator_set we want set to VPP and
262         then check if it is set correctly. Some locator_sets are duplicated.
263
264         :param node: VPP node.
265         :param locator_set_number: Generate n locator_set.
266         :type node: dict
267         :type locator_set_number: str
268         :returns: list of lisp locator_set, list of lisp locator_set expected
269             from VAT.
270         :rtype: tuple
271         """
272
273         topo = Topology()
274         locator_set_list = []
275         locator_set_list_vat = []
276         i = 0
277         for num in range(0, int(locator_set_number)):
278             locator_list = []
279             for interface in node['interfaces'].values():
280                 link = interface.get('link')
281                 i += 1
282                 if link is None:
283                     continue
284
285                 if_name = topo.get_interface_by_link_name(node, link)
286                 sw_if_index = topo.get_interface_sw_index(node, if_name)
287                 if if_name is not None:
288                     l_name = 'ls{0}'.format(num)
289                     locator = {'locator-index': sw_if_index,
290                                'priority': i,
291                                'weight': i}
292                     locator_list.append(locator)
293                     locator_set = {'locator-set': l_name,
294                                    'locator': locator_list}
295                     locator_set_list.append(locator_set)
296
297                     locator_set_vat = {"ls_name": l_name,
298                                        "ls_index": num}
299                     locator_set_list_vat.append(locator_set_vat)
300
301         return locator_set_list, locator_set_list_vat
302
303     def lisp_is_empty(self, lisp_params):
304         """Check if the input param are empty.
305
306         :param lisp_params: Should be empty list.
307         :type lisp_params: list
308         """
309
310         self.lisp_should_be_equal([], lisp_params)