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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
14 """Lisp utilities library."""
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
21 class LispUtil(object):
22 """Implements keywords for Lisp tests."""
28 def vpp_show_lisp_state(node):
29 """Get lisp state from VPP node.
31 :param node: VPP node.
33 :returns: Lisp gpe state.
38 vat.execute_script_json_out('lisp/show_lisp_status.vat',
40 return JsonParser().parse_data(vat.get_script_stdout())
43 def vpp_show_lisp_locator_set(node, items_filter):
44 """Get lisp locator_set from VPP node.
46 :param node: VPP node.
47 :param items_filter: Filter which specifies which items should be
48 retrieved - local, remote, empty string = both.
50 :type items_filter: str
51 :returns: Lisp locator_set data as python list.
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)
64 def vpp_show_lisp_eid_table(node):
65 """Get lisp eid table from VPP node.
67 :param node: VPP node.
69 :returns: Lisp eid table as python list.
74 vat.execute_script_json_out('lisp/show_lisp_eid_table.vat', node)
75 return JsonParser().parse_data(vat.get_script_stdout())
78 def vpp_show_lisp_map_resolver(node):
79 """Get lisp map resolver from VPP node.
81 :param node: VPP node.
83 :returns: Lisp map resolver as python list.
88 vat.execute_script_json_out('lisp/show_lisp_map_resolver.vat', node)
89 return JsonParser().parse_data(vat.get_script_stdout())
92 def vpp_show_lisp_map_register(node):
93 """Get LISP Map Register from VPP node.
95 :param node: VPP node.
97 :returns: LISP Map Register as python list.
102 vat.execute_script_json_out('lisp/show_lisp_map_register.vat', node)
103 return JsonParser().parse_data(vat.get_script_stdout())
106 def vpp_show_lisp_map_request_mode(node):
107 """Get LISP Map Request mode from VPP node.
109 :param node: VPP node.
111 :returns: LISP Map Request mode as python list.
116 vat.execute_script_json_out('lisp/show_lisp_map_request_mode.vat', node)
117 return JsonParser().parse_data(vat.get_script_stdout())
120 def vpp_show_lisp_map_server(node):
121 """Get LISP Map Server from VPP node.
123 :param node: VPP node.
125 :returns: LISP Map Server as python list.
130 vat.execute_script_json_out('lisp/show_lisp_map_server.vat', node)
131 return JsonParser().parse_data(vat.get_script_stdout())
134 def vpp_show_lisp_petr_config(node):
135 """Get LISP PETR configuration from VPP node.
137 :param node: VPP node.
139 :returns: LISP PETR configuration as python list.
144 vat.execute_script_json_out('lisp/show_lisp_petr_config.vat', node)
145 return JsonParser().parse_data(vat.get_script_stdout())
148 def vpp_show_lisp_rloc_config(node):
149 """Get LISP RLOC configuration from VPP node.
151 :param node: VPP node.
153 :returns: LISP RLOC configuration as python list.
158 vat.execute_script_json_out('lisp/show_lisp_rloc_config.vat', node)
159 return JsonParser().parse_data(vat.get_script_stdout())
162 def vpp_show_lisp_pitr(node):
163 """Get Lisp PITR feature config from VPP node.
165 :param node: VPP node.
167 :returns: Lisp PITR config data.
172 vat.execute_script_json_out('lisp/show_lisp_pitr.vat', node)
173 return JsonParser().parse_data(vat.get_script_stdout())
176 def lisp_should_be_equal(lisp_val1, lisp_val2):
177 """Fail if the lisp values are not equal.
179 :param lisp_val1: First lisp value.
180 :param lisp_val2: Second lisp value.
181 :type lisp_val1: list
182 :type lisp_val2: list
185 len1 = len(lisp_val1)
186 len2 = len(lisp_val2)
188 raise RuntimeError('Values are not same. '
190 'Value 2 {}.'.format(lisp_val1,
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))
198 def lisp_locator_s_should_be_equal(self, locator_set1, locator_set2):
199 """Fail if the lisp values are not equal.
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
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)
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.
218 :param node: VPP node.
219 :param locator_set_number: Generate n locator_set.
221 :type locator_set_number: str
222 :returns: list of lisp locator_set, list of lisp locator_set expected
229 locator_set_list = []
230 locator_set_list_vat = []
232 for num in range(0, int(locator_set_number)):
234 for interface in node['interfaces'].values():
235 link = interface.get('link')
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,
246 locator_list.append(locator)
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)
253 locator_set_vat = {"ls_name": l_name,
255 locator_set_list_vat.append(locator_set_vat)
257 return locator_set_list, locator_set_list_vat
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.
264 :param node: VPP node.
265 :param locator_set_number: Generate n locator_set.
267 :type locator_set_number: str
268 :returns: list of lisp locator_set, list of lisp locator_set expected
274 locator_set_list = []
275 locator_set_list_vat = []
277 for num in range(0, int(locator_set_number)):
279 for interface in node['interfaces'].values():
280 link = interface.get('link')
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,
292 locator_list.append(locator)
293 locator_set = {'locator-set': l_name,
294 'locator': locator_list}
295 locator_set_list.append(locator_set)
297 locator_set_vat = {"ls_name": l_name,
299 locator_set_list_vat.append(locator_set_vat)
301 return locator_set_list, locator_set_list_vat
303 def lisp_is_empty(self, lisp_params):
304 """Check if the input param are empty.
306 :param lisp_params: Should be empty list.
307 :type lisp_params: list
310 self.lisp_should_be_equal([], lisp_params)