17a46cd49cf61f693c970ca09d5b4bcd7e648e0b
[csit.git] / resources / libraries / python / LispUtil.py
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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """Lisp utilities library."""
15
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
19
20
21 class LispUtil(object):
22     """Implements keywords for Lisp tests."""
23
24     def __init__(self):
25         pass
26
27     @staticmethod
28     def vpp_show_lisp_locator_set(node):
29         """Get lisp locator_set from VPP node.
30
31         :param node: VPP node.
32         :type node: dict
33         :return: Lisp locator_set data as python list.
34         :rtype: list
35         """
36
37         vat = VatExecutor()
38         vat.execute_script_json_out('lisp/show_lisp_locator_set.vat', node)
39         return JsonParser().parse_data(vat.get_script_stdout())
40
41     @staticmethod
42     def vpp_show_lisp_local_eid_table(node):
43         """Get lisp local eid table from VPP node.
44
45         :param node: VPP node.
46         :type node: dict
47         :return: Lisp eid table as python list.
48         :rtype: list
49         """
50
51         vat = VatExecutor()
52         vat.execute_script_json_out('lisp/show_lisp_local_eid_table.vat', node)
53         return JsonParser().parse_data(vat.get_script_stdout())
54
55     @staticmethod
56     def vpp_show_lisp_map_resolver(node):
57         """Get lisp map resolver from VPP node.
58
59         :param node: VPP node.
60         :type node: dict
61         :return: Lisp map resolver as python list.
62         :rtype: list
63         """
64
65         vat = VatExecutor()
66         vat.execute_script_json_out('lisp/show_lisp_map_resolver.vat', node)
67         return JsonParser().parse_data(vat.get_script_stdout())
68
69     @staticmethod
70     def lisp_should_be_equal(lisp_val1, lisp_val2):
71         """Fail if the lisp values are not equal.
72
73         :param lisp_val1: First lisp value.
74         :param lisp_val2: Second lisp value.
75         :type lisp_val1: list
76         :type lisp_val2: list
77         """
78
79         len1 = len(lisp_val1)
80         len2 = len(lisp_val2)
81         if len1 != len2:
82             raise RuntimeError('Values are not same. '
83                                'Value 1 {} \n'
84                                'Value 2 {}.'.format(lisp_val1,
85                                                     lisp_val2))
86
87         for tmp in lisp_val1:
88             if tmp not in lisp_val2:
89                 raise RuntimeError('Value {} is not find in vpp:\n'
90                                    '{}'.format(tmp, lisp_val2))
91
92     def lisp_locator_s_should_be_equal(self, locator_set1, locator_set2):
93         """Fail if the lisp values are not equal.
94
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
99         """
100
101         reset_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)
113             else:
114                 raise ValueError('Unknown locator_set_type value: '
115                                  '{}'.format(locator_set_type))
116
117     @staticmethod
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.
121
122         "normal" type of data set locator_set just once.
123
124         :param node: VPP node.
125         :param locator_set_number: Generate n locator_set.
126         :type node: dict
127         :type locator_set_number: str
128         :return: dict of lisp locator_set.
129         :rtype: dict
130         """
131
132         topo = Topology()
133
134         locator_set_list = []
135         i = 0
136         for num in range(0, int(locator_set_number)):
137             for interface in node['interfaces'].values():
138                 link = interface.get('link')
139                 i += 1
140                 if link is None:
141                     continue
142
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,
149                                    'priority': i,
150                                    'weight': i}
151                     locator_set_list.append(locator_set)
152
153         loc_type = {'normal': locator_set_list}
154         return loc_type
155
156     @staticmethod
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.
160
161         "reset" type of data set locator_set multiple times,
162         use to test reset locator_set in vpp.
163
164         :param node: VPP node.
165         :param locator_set_number: Generate n locator_set.
166         :type node: dict
167         :type locator_set_number: str
168         :return: dict of lisp locator_set.
169         :rtype: dict
170         """
171
172         topo = Topology()
173
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')
178                 if link is None:
179                     continue
180
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,
187                                    'priority': 1,
188                                    'weight': 1}
189                     locator_set_list.append(locator_set)
190
191         loc_type = {'reset': locator_set_list}
192         return loc_type
193
194     @staticmethod
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.
198
199         :param ipv4_num: Generate n ipv4 eid address.
200         :param ipv6_num: Generate n ipv6 eid address.
201         :type ipv4_num: str
202         :type ipv6_num: str
203         :return: list of lisp local eid.
204         :rtype: list
205         """
206
207         eid_table = []
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)
214
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)
221
222         return eid_table
223
224     @staticmethod
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.
228
229         :param ipv4_num: Generate n ipv4 map resolver address.
230         :param ipv6_num: Generate n ipv6 map resolver address.
231         :type ipv4_num: str
232         :type ipv6_num: str
233         :return: list of lisp map resolver.
234         :rtype: list
235         """
236
237         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)
242
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)
247
248         return map_resolver
249
250     def lisp_is_empty(self, lisp_params):
251         """Check if the input param are empty.
252
253         :param lisp_params: Should be empty list.
254         :type lisp_params: list
255         """
256
257         self.lisp_should_be_equal([], lisp_params)