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_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_enable_disable.vat',
40 return JsonParser().parse_data(vat.get_script_stdout())
43 def vpp_show_lisp_locator_set(node):
44 """Get lisp locator_set from VPP node.
46 :param node: VPP node.
48 :return: Lisp locator_set data as python list.
53 vat.execute_script_json_out('lisp/show_lisp_locator_set.vat', node)
54 return JsonParser().parse_data(vat.get_script_stdout())
57 def vpp_show_lisp_local_eid_table(node):
58 """Get lisp local eid table from VPP node.
60 :param node: VPP node.
62 :return: Lisp eid table as python list.
67 vat.execute_script_json_out('lisp/show_lisp_local_eid_table.vat', node)
68 return JsonParser().parse_data(vat.get_script_stdout())
71 def vpp_show_lisp_map_resolver(node):
72 """Get lisp map resolver from VPP node.
74 :param node: VPP node.
76 :return: Lisp map resolver as python list.
81 vat.execute_script_json_out('lisp/show_lisp_map_resolver.vat', node)
82 return JsonParser().parse_data(vat.get_script_stdout())
85 def lisp_should_be_equal(lisp_val1, lisp_val2):
86 """Fail if the lisp values are not equal.
88 :param lisp_val1: First lisp value.
89 :param lisp_val2: Second lisp value.
97 raise RuntimeError('Values are not same. '
99 'Value 2 {}.'.format(lisp_val1,
102 for tmp in lisp_val1:
103 if tmp not in lisp_val2:
104 raise RuntimeError('Value {} is not find in vpp:\n'
105 '{}'.format(tmp, lisp_val2))
107 def lisp_locator_s_should_be_equal(self, locator_set1, locator_set2):
108 """Fail if the lisp values are not equal.
110 :param locator_set1: Generate lisp value.
111 :param locator_set2: Lisp value from VPP.
112 :type locator_set1: dict
113 :type locator_set2: list
117 locator_set_list = []
118 for locator_set_type, item in locator_set1.iteritems():
119 if locator_set_type == 'normal':
120 self.lisp_should_be_equal(item, locator_set2)
121 elif locator_set_type == 'reset':
122 for locator_list in reversed(item):
123 name = locator_list.get('locator-set')
124 if name not in locator_set_list:
125 reset_list.insert(0, locator_list)
126 locator_set_list.append(name)
127 self.lisp_should_be_equal(reset_list, locator_set2)
129 raise ValueError('Unknown locator_set_type value: '
130 '{}'.format(locator_set_type))
133 def generate_lisp_locator_set_data(node, locator_set_number):
134 """Generate a list of lisp locator_set we want set to VPP and
135 then check if is set correct.
137 "normal" type of data set locator_set just once.
139 :param node: VPP node.
140 :param locator_set_number: Generate n locator_set.
142 :type locator_set_number: str
143 :return: dict of lisp locator_set.
149 locator_set_list = []
151 for num in range(0, int(locator_set_number)):
152 for interface in node['interfaces'].values():
153 link = interface.get('link')
158 if_name = topo.get_interface_by_link_name(node, link)
159 sw_if_index = topo.get_interface_sw_index(node, if_name)
160 if if_name is not None:
161 l_name = 'ls{0}'.format(num)
162 locator_set = {'locator-set': l_name,
163 'locator': sw_if_index,
166 locator_set_list.append(locator_set)
168 loc_type = {'normal': locator_set_list}
172 def generate_lisp_locator_set_reset_data(node, locator_set_number):
173 """Generate a list of lisp locator_set we want set to VPP and
174 then check if is set correct.
176 "reset" type of data set locator_set multiple times,
177 use to test reset locator_set in vpp.
179 :param node: VPP node.
180 :param locator_set_number: Generate n locator_set.
182 :type locator_set_number: str
183 :return: dict of lisp locator_set.
189 locator_set_list = []
190 for num in range(0, int(locator_set_number)):
191 for interface in node['interfaces'].values():
192 link = interface.get('link')
196 if_name = topo.get_interface_by_link_name(node, link)
197 sw_if_index = topo.get_interface_sw_index(node, if_name)
198 if if_name is not None:
199 l_name = 'ls{0}'.format(num)
200 locator_set = {'locator-set': l_name,
201 'locator': sw_if_index,
204 locator_set_list.append(locator_set)
206 loc_type = {'reset': locator_set_list}
209 def lisp_is_empty(self, lisp_params):
210 """Check if the input param are empty.
212 :param lisp_params: Should be empty list.
213 :type lisp_params: list
216 self.lisp_should_be_equal([], lisp_params)