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 :return: 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 :return: 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 :return: 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 :return: 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 lisp_should_be_equal(lisp_val1, lisp_val2):
93 """Fail if the lisp values are not equal.
95 :param lisp_val1: First lisp value.
96 :param lisp_val2: Second lisp value.
101 len1 = len(lisp_val1)
102 len2 = len(lisp_val2)
104 raise RuntimeError('Values are not same. '
106 'Value 2 {}.'.format(lisp_val1,
109 for tmp in lisp_val1:
110 if tmp not in lisp_val2:
111 raise RuntimeError('Value {} not found in vpp:\n'
112 '{}'.format(tmp, lisp_val2))
114 def lisp_locator_s_should_be_equal(self, locator_set1, locator_set2):
115 """Fail if the lisp values are not equal.
117 :param locator_set1: Generate lisp value.
118 :param locator_set2: Lisp value from VPP.
119 :type locator_set1: list
120 :type locator_set2: list
123 # Remove duplicate value which is not set in vpp node.
124 locator_set_list = []
125 tmp_list = list(locator_set1)
127 locator_set = tmp_list.pop(0)
128 locator_set_name = locator_set.get('locator-set')
129 for tmp_loc_set in tmp_list:
130 tmp_loc_set_name = tmp_loc_set.get('locator-set')
131 if locator_set_name == tmp_loc_set_name:
132 locator_set = tmp_loc_set
133 tmp_list.remove(tmp_loc_set)
134 locator_set_list.append(locator_set)
136 for locator_set in locator_set2:
137 if 'locator-set-index' in locator_set:
138 del locator_set['locator-set-index']
140 self.lisp_should_be_equal(locator_set_list, locator_set2)
143 def generate_unique_lisp_locator_set_data(node, locator_set_number):
144 """Generate a list of lisp locator_set we want set to VPP and
145 then check if it is set correctly. All locator_sets are unique.
147 :param node: VPP node.
148 :param locator_set_number: Generate n locator_set.
150 :type locator_set_number: str
151 :return: list of lisp locator_set.
157 locator_set_list = []
159 for num in range(0, int(locator_set_number)):
161 for interface in node['interfaces'].values():
162 link = interface.get('link')
167 if_name = topo.get_interface_by_link_name(node, link)
168 sw_if_index = topo.get_interface_sw_index(node, if_name)
169 if if_name is not None:
170 locator = {'locator-index': sw_if_index,
173 locator_list.append(locator)
175 l_name = 'ls{0}'.format(num)
176 locator_set = {'locator-set': l_name,
177 'locator': locator_list}
178 locator_set_list.append(locator_set)
180 return locator_set_list
183 def generate_duplicate_lisp_locator_set_data(node, locator_set_number):
184 """Generate a list of lisp locator_set we want set to VPP and
185 then check if it is set correctly. Some locator_sets are duplicated.
187 :param node: VPP node.
188 :param locator_set_number: Generate n locator_set.
190 :type locator_set_number: str
191 :return: list of lisp locator_set.
196 locator_set_list = []
198 for num in range(0, int(locator_set_number)):
200 for interface in node['interfaces'].values():
201 link = interface.get('link')
206 if_name = topo.get_interface_by_link_name(node, link)
207 sw_if_index = topo.get_interface_sw_index(node, if_name)
208 if if_name is not None:
209 l_name = 'ls{0}'.format(num)
210 locator = {'locator-index': sw_if_index,
213 locator_list.append(locator)
214 locator_set = {'locator-set': l_name,
215 'locator': locator_list}
216 locator_set_list.append(locator_set)
218 return locator_set_list
220 def lisp_is_empty(self, lisp_params):
221 """Check if the input param are empty.
223 :param lisp_params: Should be empty list.
224 :type lisp_params: list
227 self.lisp_should_be_equal([], lisp_params)