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:
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_pitr(node):
93 """Get Lisp PITR feature config from VPP node.
95 :param node: VPP node.
97 :returns: Lisp PITR config data.
102 vat.execute_script_json_out('lisp/show_lisp_pitr.vat', node)
103 return JsonParser().parse_data(vat.get_script_stdout())
106 def lisp_should_be_equal(lisp_val1, lisp_val2):
107 """Fail if the lisp values are not equal.
109 :param lisp_val1: First lisp value.
110 :param lisp_val2: Second lisp value.
111 :type lisp_val1: list
112 :type lisp_val2: list
115 len1 = len(lisp_val1)
116 len2 = len(lisp_val2)
118 raise RuntimeError('Values are not same. '
120 'Value 2 {}.'.format(lisp_val1,
123 for tmp in lisp_val1:
124 if tmp not in lisp_val2:
125 raise RuntimeError('Value {} not found in vpp:\n'
126 '{}'.format(tmp, lisp_val2))
128 def lisp_locator_s_should_be_equal(self, locator_set1, locator_set2):
129 """Fail if the lisp values are not equal.
131 :param locator_set1: Generate lisp value.
132 :param locator_set2: Lisp value from VPP.
133 :type locator_set1: list
134 :type locator_set2: list
137 locator_set_list = []
138 for item in locator_set1:
139 if item not in locator_set_list:
140 locator_set_list.append(item)
141 self.lisp_should_be_equal(locator_set_list, locator_set2)
144 def generate_unique_lisp_locator_set_data(node, locator_set_number):
145 """Generate a list of lisp locator_set we want set to VPP and
146 then check if it is set correctly. All locator_sets are unique.
148 :param node: VPP node.
149 :param locator_set_number: Generate n locator_set.
151 :type locator_set_number: str
152 :returns: list of lisp locator_set, list of lisp locator_set expected
159 locator_set_list = []
160 locator_set_list_vat = []
162 for num in range(0, int(locator_set_number)):
164 for interface in node['interfaces'].values():
165 link = interface.get('link')
170 if_name = topo.get_interface_by_link_name(node, link)
171 sw_if_index = topo.get_interface_sw_index(node, if_name)
172 if if_name is not None:
173 locator = {'locator-index': sw_if_index,
176 locator_list.append(locator)
178 l_name = 'ls{0}'.format(num)
179 locator_set = {'locator-set': l_name,
180 'locator': locator_list}
181 locator_set_list.append(locator_set)
183 locator_set_vat = {"ls_name": l_name,
185 locator_set_list_vat.append(locator_set_vat)
187 return locator_set_list, locator_set_list_vat
190 def generate_duplicate_lisp_locator_set_data(node, locator_set_number):
191 """Generate a list of lisp locator_set we want set to VPP and
192 then check if it is set correctly. Some locator_sets are duplicated.
194 :param node: VPP node.
195 :param locator_set_number: Generate n locator_set.
197 :type locator_set_number: str
198 :returns: list of lisp locator_set, list of lisp locator_set expected
204 locator_set_list = []
205 locator_set_list_vat = []
207 for num in range(0, int(locator_set_number)):
209 for interface in node['interfaces'].values():
210 link = interface.get('link')
215 if_name = topo.get_interface_by_link_name(node, link)
216 sw_if_index = topo.get_interface_sw_index(node, if_name)
217 if if_name is not None:
218 l_name = 'ls{0}'.format(num)
219 locator = {'locator-index': sw_if_index,
222 locator_list.append(locator)
223 locator_set = {'locator-set': l_name,
224 'locator': locator_list}
225 locator_set_list.append(locator_set)
227 locator_set_vat = {"ls_name": l_name,
229 locator_set_list_vat.append(locator_set_vat)
231 return locator_set_list, locator_set_list_vat
233 def lisp_is_empty(self, lisp_params):
234 """Check if the input param are empty.
236 :param lisp_params: Should be empty list.
237 :type lisp_params: list
240 self.lisp_should_be_equal([], lisp_params)