CSIT-195: Update LISP test
[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_state(node):
29         """Get lisp state from VPP node.
30
31         :param node: VPP node.
32         :type node: dict
33         :return: Lisp gpe state.
34         :rtype: list
35         """
36
37         vat = VatExecutor()
38         vat.execute_script_json_out('lisp/show_lisp_status.vat',
39                                     node)
40         return JsonParser().parse_data(vat.get_script_stdout())
41
42     @staticmethod
43     def vpp_show_lisp_locator_set(node):
44         """Get lisp locator_set from VPP node.
45
46         :param node: VPP node.
47         :type node: dict
48         :return: Lisp locator_set data as python list.
49         :rtype: list
50         """
51
52         vat = VatExecutor()
53         vat.execute_script_json_out('lisp/show_lisp_locator_set.vat', node)
54         return JsonParser().parse_data(vat.get_script_stdout())
55
56     @staticmethod
57     def vpp_show_lisp_local_eid_table(node):
58         """Get lisp local eid table from VPP node.
59
60         :param node: VPP node.
61         :type node: dict
62         :return: Lisp eid table as python list.
63         :rtype: list
64         """
65
66         vat = VatExecutor()
67         vat.execute_script_json_out('lisp/show_lisp_local_eid_table.vat', node)
68         return JsonParser().parse_data(vat.get_script_stdout())
69
70     @staticmethod
71     def vpp_show_lisp_map_resolver(node):
72         """Get lisp map resolver from VPP node.
73
74         :param node: VPP node.
75         :type node: dict
76         :return: Lisp map resolver as python list.
77         :rtype: list
78         """
79
80         vat = VatExecutor()
81         vat.execute_script_json_out('lisp/show_lisp_map_resolver.vat', node)
82         return JsonParser().parse_data(vat.get_script_stdout())
83
84     @staticmethod
85     def lisp_should_be_equal(lisp_val1, lisp_val2):
86         """Fail if the lisp values are not equal.
87
88         :param lisp_val1: First lisp value.
89         :param lisp_val2: Second lisp value.
90         :type lisp_val1: list
91         :type lisp_val2: list
92         """
93
94         len1 = len(lisp_val1)
95         len2 = len(lisp_val2)
96         if len1 != len2:
97             raise RuntimeError('Values are not same. '
98                                'Value 1 {} \n'
99                                'Value 2 {}.'.format(lisp_val1,
100                                                     lisp_val2))
101
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))
106
107     def lisp_locator_s_should_be_equal(self, locator_set1, locator_set2):
108         """Fail if the lisp values are not equal.
109
110         :param locator_set1: Generate lisp value.
111         :param locator_set2: Lisp value from VPP.
112         :type locator_set1: list
113         :type locator_set2: list
114         """
115
116         # Remove duplicate value which is not set in vpp node.
117         locator_set_list = []
118         tmp_list = list(locator_set1)
119         while len(tmp_list):
120             locator_set = tmp_list.pop(0)
121             locator_set_name = locator_set.get('locator-set')
122             for tmp_loc_set in tmp_list:
123                 tmp_loc_set_name = tmp_loc_set.get('locator-set')
124                 if locator_set_name == tmp_loc_set_name:
125                     locator_set = tmp_loc_set
126                     tmp_list.remove(tmp_loc_set)
127             locator_set_list.append(locator_set)
128
129         for locator_set in locator_set2:
130             if 'locator-set-index' in locator_set:
131                 del locator_set['locator-set-index']
132
133         self.lisp_should_be_equal(locator_set_list, locator_set2)
134
135     @staticmethod
136     def generate_unique_lisp_locator_set_data(node, locator_set_number):
137         """Generate a list of lisp locator_set we want set to VPP and
138         then check if it is set correctly. All locator_sets are unique.
139
140         :param node: VPP node.
141         :param locator_set_number: Generate n locator_set.
142         :type node: dict
143         :type locator_set_number: str
144         :return: list of lisp locator_set.
145         :rtype: list
146         """
147
148         topo = Topology()
149
150         locator_set_list = []
151         i = 0
152         for num in range(0, int(locator_set_number)):
153             locator_list = []
154             for interface in node['interfaces'].values():
155                 link = interface.get('link')
156                 i += 1
157                 if link is None:
158                     continue
159
160                 if_name = topo.get_interface_by_link_name(node, link)
161                 sw_if_index = topo.get_interface_sw_index(node, if_name)
162                 if if_name is not None:
163                     locator = {'locator-index': sw_if_index,
164                                'priority': i,
165                                'weight': i}
166                     locator_list.append(locator)
167
168             l_name = 'ls{0}'.format(num)
169             locator_set = {'locator-set': l_name,
170                            'locator': locator_list}
171             locator_set_list.append(locator_set)
172
173         return locator_set_list
174
175     @staticmethod
176     def generate_duplicate_lisp_locator_set_data(node, locator_set_number):
177         """Generate a list of lisp locator_set we want set to VPP and
178         then check if it is set correctly. Some locator_sets are duplicated.
179
180         :param node: VPP node.
181         :param locator_set_number: Generate n locator_set.
182         :type node: dict
183         :type locator_set_number: str
184         :return: list of lisp locator_set.
185         :rtype: list
186         """
187
188         topo = Topology()
189         locator_set_list = []
190         i = 0
191         for num in range(0, int(locator_set_number)):
192             locator_list = []
193             for interface in node['interfaces'].values():
194                 link = interface.get('link')
195                 i += 1
196                 if link is None:
197                     continue
198
199                 if_name = topo.get_interface_by_link_name(node, link)
200                 sw_if_index = topo.get_interface_sw_index(node, if_name)
201                 if if_name is not None:
202                     l_name = 'ls{0}'.format(num)
203                     locator = {'locator-index': sw_if_index,
204                                'priority': i,
205                                'weight': i}
206                     locator_list.append(locator)
207                     locator_set = {'locator-set': l_name,
208                                    'locator': locator_list}
209                     locator_set_list.append(locator_set)
210
211         return locator_set_list
212
213     def lisp_is_empty(self, lisp_params):
214         """Check if the input param are empty.
215
216         :param lisp_params: Should be empty list.
217         :type lisp_params: list
218         """
219
220         self.lisp_should_be_equal([], lisp_params)