Pylint fixes
[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 lisp_should_be_equal(lisp_val1, lisp_val2):
93         """Fail if the lisp values are not equal.
94
95         :param lisp_val1: First lisp value.
96         :param lisp_val2: Second lisp value.
97         :type lisp_val1: list
98         :type lisp_val2: list
99         """
100
101         len1 = len(lisp_val1)
102         len2 = len(lisp_val2)
103         if len1 != len2:
104             raise RuntimeError('Values are not same. '
105                                'Value 1 {} \n'
106                                'Value 2 {}.'.format(lisp_val1,
107                                                     lisp_val2))
108
109         for tmp in lisp_val1:
110             if tmp not in lisp_val2:
111                 raise RuntimeError('Value {} not found in vpp:\n'
112                                    '{}'.format(tmp, lisp_val2))
113
114     def lisp_locator_s_should_be_equal(self, locator_set1, locator_set2):
115         """Fail if the lisp values are not equal.
116
117         :param locator_set1: Generate lisp value.
118         :param locator_set2: Lisp value from VPP.
119         :type locator_set1: list
120         :type locator_set2: list
121         """
122
123         locator_set_list = []
124         for item in locator_set1:
125             if item not in locator_set_list:
126                 locator_set_list.append(item)
127         self.lisp_should_be_equal(locator_set_list, locator_set2)
128
129     @staticmethod
130     def generate_unique_lisp_locator_set_data(node, locator_set_number):
131         """Generate a list of lisp locator_set we want set to VPP and
132         then check if it is set correctly. All locator_sets are unique.
133
134         :param node: VPP node.
135         :param locator_set_number: Generate n locator_set.
136         :type node: dict
137         :type locator_set_number: str
138         :returns: list of lisp locator_set, list of lisp locator_set expected
139         from VAT.
140         :rtype: tuple
141         """
142
143         topo = Topology()
144
145         locator_set_list = []
146         locator_set_list_vat = []
147         i = 0
148         for num in range(0, int(locator_set_number)):
149             locator_list = []
150             for interface in node['interfaces'].values():
151                 link = interface.get('link')
152                 i += 1
153                 if link is None:
154                     continue
155
156                 if_name = topo.get_interface_by_link_name(node, link)
157                 sw_if_index = topo.get_interface_sw_index(node, if_name)
158                 if if_name is not None:
159                     locator = {'locator-index': sw_if_index,
160                                'priority': i,
161                                'weight': i}
162                     locator_list.append(locator)
163
164             l_name = 'ls{0}'.format(num)
165             locator_set = {'locator-set': l_name,
166                            'locator': locator_list}
167             locator_set_list.append(locator_set)
168
169             locator_set_vat = {"ls_name": l_name,
170                                "ls_index": num}
171             locator_set_list_vat.append(locator_set_vat)
172
173         return locator_set_list, locator_set_list_vat
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         :returns: list of lisp locator_set, list of lisp locator_set expected
185         from VAT.
186         :rtype: tuple
187         """
188
189         topo = Topology()
190         locator_set_list = []
191         locator_set_list_vat = []
192         i = 0
193         for num in range(0, int(locator_set_number)):
194             locator_list = []
195             for interface in node['interfaces'].values():
196                 link = interface.get('link')
197                 i += 1
198                 if link is None:
199                     continue
200
201                 if_name = topo.get_interface_by_link_name(node, link)
202                 sw_if_index = topo.get_interface_sw_index(node, if_name)
203                 if if_name is not None:
204                     l_name = 'ls{0}'.format(num)
205                     locator = {'locator-index': sw_if_index,
206                                'priority': i,
207                                'weight': i}
208                     locator_list.append(locator)
209                     locator_set = {'locator-set': l_name,
210                                    'locator': locator_list}
211                     locator_set_list.append(locator_set)
212
213                     locator_set_vat = {"ls_name": l_name,
214                                        "ls_index": num}
215                     locator_set_list_vat.append(locator_set_vat)
216
217         return locator_set_list, locator_set_list_vat
218
219     def lisp_is_empty(self, lisp_params):
220         """Check if the input param are empty.
221
222         :param lisp_params: Should be empty list.
223         :type lisp_params: list
224         """
225
226         self.lisp_should_be_equal([], lisp_params)