Fix: use pci/rescan to avoid occasional bind issue on Centos7
[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, VatTerminal
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         :returns: 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, items_filter):
44         """Get lisp locator_set from VPP node.
45
46         :param node: VPP node.
47         :param items_filter: Filter which specifies which items should be
48         retrieved - local, remote, empty string = both.
49         :type node: dict
50         :type items_filter: str
51         :returns: Lisp locator_set data as python list.
52         :rtype: list
53         """
54
55         try:
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)
59             return response[0]
60         except ValueError:
61             return []
62
63     @staticmethod
64     def vpp_show_lisp_eid_table(node):
65         """Get lisp eid table from VPP node.
66
67         :param node: VPP node.
68         :type node: dict
69         :returns: Lisp eid table as python list.
70         :rtype: list
71         """
72
73         vat = VatExecutor()
74         vat.execute_script_json_out('lisp/show_lisp_eid_table.vat', node)
75         return JsonParser().parse_data(vat.get_script_stdout())
76
77     @staticmethod
78     def vpp_show_lisp_map_resolver(node):
79         """Get lisp map resolver from VPP node.
80
81         :param node: VPP node.
82         :type node: dict
83         :returns: Lisp map resolver as python list.
84         :rtype: list
85         """
86
87         vat = VatExecutor()
88         vat.execute_script_json_out('lisp/show_lisp_map_resolver.vat', node)
89         return JsonParser().parse_data(vat.get_script_stdout())
90
91     @staticmethod
92     def vpp_show_lisp_pitr(node):
93         """Get Lisp PITR feature config from VPP node.
94
95         :param node: VPP node.
96         :type node: dict
97         :returns: Lisp PITR config data.
98         :rtype: dict
99         """
100
101         vat = VatExecutor()
102         vat.execute_script_json_out('lisp/show_lisp_pitr.vat', node)
103         return JsonParser().parse_data(vat.get_script_stdout())
104
105     @staticmethod
106     def lisp_should_be_equal(lisp_val1, lisp_val2):
107         """Fail if the lisp values are not equal.
108
109         :param lisp_val1: First lisp value.
110         :param lisp_val2: Second lisp value.
111         :type lisp_val1: list
112         :type lisp_val2: list
113         """
114
115         len1 = len(lisp_val1)
116         len2 = len(lisp_val2)
117         if len1 != len2:
118             raise RuntimeError('Values are not same. '
119                                'Value 1 {} \n'
120                                'Value 2 {}.'.format(lisp_val1,
121                                                     lisp_val2))
122
123         for tmp in lisp_val1:
124             if tmp not in lisp_val2:
125                 raise RuntimeError('Value {} not found in vpp:\n'
126                                    '{}'.format(tmp, lisp_val2))
127
128     def lisp_locator_s_should_be_equal(self, locator_set1, locator_set2):
129         """Fail if the lisp values are not equal.
130
131         :param locator_set1: Generate lisp value.
132         :param locator_set2: Lisp value from VPP.
133         :type locator_set1: list
134         :type locator_set2: list
135         """
136
137         locator_set_list = []
138         for item in locator_set1:
139             if item not in locator_set_list:
140                 locator_set_list.append(item)
141         self.lisp_should_be_equal(locator_set_list, locator_set2)
142
143     @staticmethod
144     def generate_unique_lisp_locator_set_data(node, locator_set_number):
145         """Generate a list of lisp locator_set we want set to VPP and
146         then check if it is set correctly. All locator_sets are unique.
147
148         :param node: VPP node.
149         :param locator_set_number: Generate n locator_set.
150         :type node: dict
151         :type locator_set_number: str
152         :returns: list of lisp locator_set, list of lisp locator_set expected
153         from VAT.
154         :rtype: tuple
155         """
156
157         topo = Topology()
158
159         locator_set_list = []
160         locator_set_list_vat = []
161         i = 0
162         for num in range(0, int(locator_set_number)):
163             locator_list = []
164             for interface in node['interfaces'].values():
165                 link = interface.get('link')
166                 i += 1
167                 if link is None:
168                     continue
169
170                 if_name = topo.get_interface_by_link_name(node, link)
171                 sw_if_index = topo.get_interface_sw_index(node, if_name)
172                 if if_name is not None:
173                     locator = {'locator-index': sw_if_index,
174                                'priority': i,
175                                'weight': i}
176                     locator_list.append(locator)
177
178             l_name = 'ls{0}'.format(num)
179             locator_set = {'locator-set': l_name,
180                            'locator': locator_list}
181             locator_set_list.append(locator_set)
182
183             locator_set_vat = {"ls_name": l_name,
184                                "ls_index": num}
185             locator_set_list_vat.append(locator_set_vat)
186
187         return locator_set_list, locator_set_list_vat
188
189     @staticmethod
190     def generate_duplicate_lisp_locator_set_data(node, locator_set_number):
191         """Generate a list of lisp locator_set we want set to VPP and
192         then check if it is set correctly. Some locator_sets are duplicated.
193
194         :param node: VPP node.
195         :param locator_set_number: Generate n locator_set.
196         :type node: dict
197         :type locator_set_number: str
198         :returns: list of lisp locator_set, list of lisp locator_set expected
199         from VAT.
200         :rtype: tuple
201         """
202
203         topo = Topology()
204         locator_set_list = []
205         locator_set_list_vat = []
206         i = 0
207         for num in range(0, int(locator_set_number)):
208             locator_list = []
209             for interface in node['interfaces'].values():
210                 link = interface.get('link')
211                 i += 1
212                 if link is None:
213                     continue
214
215                 if_name = topo.get_interface_by_link_name(node, link)
216                 sw_if_index = topo.get_interface_sw_index(node, if_name)
217                 if if_name is not None:
218                     l_name = 'ls{0}'.format(num)
219                     locator = {'locator-index': sw_if_index,
220                                'priority': i,
221                                'weight': i}
222                     locator_list.append(locator)
223                     locator_set = {'locator-set': l_name,
224                                    'locator': locator_list}
225                     locator_set_list.append(locator_set)
226
227                     locator_set_vat = {"ls_name": l_name,
228                                        "ls_index": num}
229                     locator_set_list_vat.append(locator_set_vat)
230
231         return locator_set_list, locator_set_list_vat
232
233     def lisp_is_empty(self, lisp_params):
234         """Check if the input param are empty.
235
236         :param lisp_params: Should be empty list.
237         :type lisp_params: list
238         """
239
240         self.lisp_should_be_equal([], lisp_params)