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
21 class LispUtil(object):
22 """Implements keywords for Lisp tests."""
28 def vpp_show_lisp_locator_set(node):
29 """Get lisp locator_set from VPP node.
31 :param node: VPP node.
33 :return: Lisp locator_set data as python list.
38 vat.execute_script_json_out('lisp/show_lisp_locator_set.vat', node)
39 return JsonParser().parse_data(vat.get_script_stdout())
42 def vpp_show_lisp_local_eid_table(node):
43 """Get lisp local eid table from VPP node.
45 :param node: VPP node.
47 :return: Lisp eid table as python list.
52 vat.execute_script_json_out('lisp/show_lisp_local_eid_table.vat', node)
53 return JsonParser().parse_data(vat.get_script_stdout())
56 def vpp_show_lisp_map_resolver(node):
57 """Get lisp map resolver from VPP node.
59 :param node: VPP node.
61 :return: Lisp map resolver as python list.
66 vat.execute_script_json_out('lisp/show_lisp_map_resolver.vat', node)
67 return JsonParser().parse_data(vat.get_script_stdout())
70 def lisp_should_be_equal(lisp_val1, lisp_val2):
71 """Fail if the lisp values are not equal.
73 :param lisp_val1: First lisp value.
74 :param lisp_val2: Second lisp value.
82 raise RuntimeError('Values are not same. '
84 'Value 2 {}.'.format(lisp_val1,
88 if tmp not in lisp_val2:
89 raise RuntimeError('Value {} is not find in vpp:\n'
90 '{}'.format(tmp, lisp_val2))
92 def lisp_locator_s_should_be_equal(self, locator_set1, locator_set2):
93 """Fail if the lisp values are not equal.
95 :param locator_set1: Generate lisp value.
96 :param locator_set2: Lisp value from VPP.
97 :type locator_set1: dict
98 :type locator_set2: list
102 locator_set_list = []
103 for locator_set_type, item in locator_set1.iteritems():
104 if locator_set_type == 'normal':
105 self.lisp_should_be_equal(item, locator_set2)
106 elif locator_set_type == 'reset':
107 for locator_list in reversed(item):
108 name = locator_list.get('locator-set')
109 if name not in locator_set_list:
110 reset_list.insert(0, locator_list)
111 locator_set_list.append(name)
112 self.lisp_should_be_equal(reset_list, locator_set2)
114 raise ValueError('Unknown locator_set_type value: '
115 '{}'.format(locator_set_type))
118 def generate_lisp_locator_set_data(node, locator_set_number):
119 """Generate a list of lisp locator_set we want set to VPP and
120 then check if is set correct.
122 "normal" type of data set locator_set just once.
124 :param node: VPP node.
125 :param locator_set_number: Generate n locator_set.
127 :type locator_set_number: str
128 :return: dict of lisp locator_set.
134 locator_set_list = []
136 for num in range(0, int(locator_set_number)):
137 for interface in node['interfaces'].values():
138 link = interface.get('link')
143 if_name = topo.get_interface_by_link_name(node, link)
144 sw_if_index = topo.get_interface_sw_index(node, if_name)
145 if if_name is not None:
146 l_name = 'ls{0}'.format(num)
147 locator_set = {'locator-set': l_name,
148 'locator': sw_if_index,
151 locator_set_list.append(locator_set)
153 loc_type = {'normal': locator_set_list}
157 def generate_lisp_locator_set_reset_data(node, locator_set_number):
158 """Generate a list of lisp locator_set we want set to VPP and
159 then check if is set correct.
161 "reset" type of data set locator_set multiple times,
162 use to test reset locator_set in vpp.
164 :param node: VPP node.
165 :param locator_set_number: Generate n locator_set.
167 :type locator_set_number: str
168 :return: dict of lisp locator_set.
174 locator_set_list = []
175 for num in range(0, int(locator_set_number)):
176 for interface in node['interfaces'].values():
177 link = interface.get('link')
181 if_name = topo.get_interface_by_link_name(node, link)
182 sw_if_index = topo.get_interface_sw_index(node, if_name)
183 if if_name is not None:
184 l_name = 'ls{0}'.format(num)
185 locator_set = {'locator-set': l_name,
186 'locator': sw_if_index,
189 locator_set_list.append(locator_set)
191 loc_type = {'reset': locator_set_list}
195 def generate_lisp_local_eid_data(ipv4_num, ipv6_num):
196 """Generate a list of lisp local eid we want set to VPP and
197 then check if is set correct.
199 :param ipv4_num: Generate n ipv4 eid address.
200 :param ipv6_num: Generate n ipv6 eid address.
203 :return: list of lisp local eid.
208 for num in range(0, int(ipv4_num)):
209 addrr = '192.168.{}.1'.format(num)
210 eid = {'eid address': addrr,
211 'eid prefix len': 24,
212 'locator-set': 'ls1'}
213 eid_table.append(eid)
215 for num in range(0, int(ipv6_num)):
216 addrr = '10:{}::1'.format(num + 1)
217 eid = {'eid address': addrr,
218 'eid prefix len': 32,
219 'locator-set': 'ls1'}
220 eid_table.append(eid)
225 def generate_lisp_map_resolver_data(ipv4_num, ipv6_num):
226 """Generate a list of lisp map resolvers we want set to VPP and
227 then check if is set correct.
229 :param ipv4_num: Generate n ipv4 map resolver address.
230 :param ipv6_num: Generate n ipv6 map resolver address.
233 :return: list of lisp map resolver.
238 for i in range(0, int(ipv4_num)):
239 addr = '192.169.{}.1'.format(i)
240 resolver = {'map resolver': addr}
241 map_resolver.append(resolver)
243 for i in range(0, int(ipv6_num)):
244 addr = '12:{}::1'.format(i + 1)
245 resolver = {'map resolver': addr}
246 map_resolver.append(resolver)
250 def lisp_is_empty(self, lisp_params):
251 """Check if the input param are empty.
253 :param lisp_params: Should be empty list.
254 :type lisp_params: list
257 self.lisp_should_be_equal([], lisp_params)