PAPI: Fix PyLint errors
[csit.git] / resources / libraries / python / VPPUtil.py
1 # Copyright (c) 2019 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 """VPP util library."""
15
16 import binascii
17
18 from robot.api import logger
19
20 from resources.libraries.python.Constants import Constants
21 from resources.libraries.python.DUTSetup import DUTSetup
22 from resources.libraries.python.PapiExecutor import PapiExecutor
23 from resources.libraries.python.ssh import exec_cmd_no_error
24 from resources.libraries.python.topology import NodeType
25 from resources.libraries.python.VatExecutor import VatExecutor
26
27
28 class VPPUtil(object):
29     """General class for any VPP related methods/functions."""
30
31     @staticmethod
32     def show_vpp_settings(node, *additional_cmds):
33         """Print default VPP settings. In case others are needed, can be
34         accepted as next parameters (each setting one parameter), preferably
35         in form of a string.
36
37         :param node: VPP node.
38         :param additional_cmds: Additional commands that the vpp should print
39             settings for.
40         :type node: dict
41         :type additional_cmds: tuple
42         """
43         def_setting_tb_displayed = {
44             'IPv6 FIB': 'ip6 fib',
45             'IPv4 FIB': 'ip fib',
46             'Interface IP': 'int addr',
47             'Interfaces': 'int',
48             'ARP': 'ip arp',
49             'Errors': 'err'
50         }
51
52         if additional_cmds:
53             for cmd in additional_cmds:
54                 def_setting_tb_displayed['Custom Setting: {}'.format(cmd)] = cmd
55
56         for _, cmd in def_setting_tb_displayed.items():
57             command = 'vppctl sh {cmd}'.format(cmd=cmd)
58             exec_cmd_no_error(node, command, timeout=30, sudo=True)
59
60     @staticmethod
61     def restart_vpp_service(node):
62         """Restart VPP service on the specified topology node.
63
64         :param node: Topology node.
65         :type node: dict
66         """
67         DUTSetup.restart_service(node, Constants.VPP_UNIT)
68
69     @staticmethod
70     def restart_vpp_service_on_all_duts(nodes):
71         """Restart VPP service on all DUT nodes.
72
73         :param nodes: Topology nodes.
74         :type nodes: dict
75         """
76         for node in nodes.values():
77             if node['type'] == NodeType.DUT:
78                 VPPUtil.restart_vpp_service(node)
79
80     @staticmethod
81     def stop_vpp_service(node):
82         """Stop VPP service on the specified topology node.
83
84         :param node: Topology node.
85         :type node: dict
86         """
87         DUTSetup.stop_service(node, Constants.VPP_UNIT)
88
89     @staticmethod
90     def stop_vpp_service_on_all_duts(nodes):
91         """Stop VPP service on all DUT nodes.
92
93         :param nodes: Topology nodes.
94         :type nodes: dict
95         """
96         for node in nodes.values():
97             if node['type'] == NodeType.DUT:
98                 VPPUtil.stop_vpp_service(node)
99
100     @staticmethod
101     def verify_vpp_installed(node):
102         """Verify that VPP is installed on the specified topology node.
103
104         :param node: Topology node.
105         :type node: dict
106         """
107         cmd = 'command -v vpp'
108         exec_cmd_no_error(
109             node, cmd, message='VPP is not installed!')
110
111     @staticmethod
112     def verify_vpp_started(node):
113         """Verify that VPP is started on the specified topology node.
114
115         :param node: Topology node.
116         :type node: dict
117         """
118         cmd = ('vppctl show pci 2>&1 | '
119                'fgrep -v "Connection refused" | '
120                'fgrep -v "No such file or directory"')
121         exec_cmd_no_error(
122             node, cmd, sudo=True, message='VPP failed to start!', retries=120)
123
124     @staticmethod
125     def verify_vpp(node):
126         """Verify that VPP is installed and started on the specified topology
127         node.
128
129         :param node: Topology node.
130         :type node: dict
131         :raises RuntimeError: If VPP service fails to start.
132         """
133         VPPUtil.verify_vpp_installed(node)
134         try:
135             # Verify responsivness of vppctl.
136             VPPUtil.verify_vpp_started(node)
137             # Verify responsivness of PAPI.
138             VPPUtil.show_log(node)
139         finally:
140             DUTSetup.get_service_logs(node, Constants.VPP_UNIT)
141
142     @staticmethod
143     def verify_vpp_on_all_duts(nodes):
144         """Verify that VPP is installed and started on all DUT nodes.
145
146         :param nodes: Nodes in the topology.
147         :type nodes: dict
148         """
149         for node in nodes.values():
150             if node['type'] == NodeType.DUT:
151                 VPPUtil.verify_vpp(node)
152
153     @staticmethod
154     def vpp_show_version(node, verbose=True):
155         """Run "show_version" PAPI command.
156
157         :param node: Node to run command on.
158         :param verbose: Show version, compile date and compile location if True
159             otherwise show only version.
160         :type node: dict
161         :type verbose: bool
162         :returns: VPP version.
163         :rtype: str
164         """
165         with PapiExecutor(node) as papi_exec:
166             data = papi_exec.add('show_version').execute_should_pass().\
167                 verify_reply()
168         version = ('VPP version:      {ver}\n'.
169                    format(ver=data['version'].rstrip('\0x00')))
170         if verbose:
171             version += ('Compile date:     {date}\n'
172                         'Compile location: {cl}\n '.
173                         format(date=data['build_date'].rstrip('\0x00'),
174                                cl=data['build_directory'].rstrip('\0x00')))
175         logger.info(version)
176         return data['version'].rstrip('\0x00')
177
178     @staticmethod
179     def show_vpp_version_on_all_duts(nodes):
180         """Show VPP version verbose on all DUTs.
181
182         :param nodes: Nodes in the topology.
183         :type nodes: dict
184         """
185         for node in nodes.values():
186             if node['type'] == NodeType.DUT:
187                 VPPUtil.vpp_show_version(node)
188
189     @staticmethod
190     def vpp_show_interfaces(node):
191         """Run "show interface" CLI command.
192
193         :param node: Node to run command on.
194         :type node: dict
195         """
196
197         cmd = 'sw_interface_dump'
198         cmd_reply = 'sw_interface_details'
199         args = dict(name_filter_valid=0, name_filter='')
200         err_msg = 'Failed to get interface dump on host {host}'.format(
201             host=node['host'])
202         with PapiExecutor(node) as papi_exec:
203             papi_resp = papi_exec.add(cmd, **args).execute_should_pass(err_msg)
204
205         papi_if_dump = papi_resp.reply[0]['api_reply']
206
207         if_data = list()
208         for item in papi_if_dump:
209             data = item[cmd_reply]
210             data['interface_name'] = data['interface_name'].rstrip('\x00')
211             data['tag'] = data['tag'].rstrip('\x00')
212             data['l2_address'] = str(':'.join(binascii.hexlify(
213                 data['l2_address'])[i:i + 2] for i in range(0, 12, 2)).
214                                      decode('ascii'))
215             if_data.append(data)
216         # TODO: return only base data
217         logger.trace('Interface data of host {host}:\n{if_data}'.format(
218             host=node['host'], if_data=if_data))
219
220     @staticmethod
221     def vpp_show_crypto_device_mapping(node):
222         """Run "show crypto device mapping" CLI command.
223
224         :param node: Node to run command on.
225         :type node: dict
226         """
227         vat = VatExecutor()
228         vat.execute_script("show_crypto_device_mapping.vat", node,
229                            json_out=False)
230
231     @staticmethod
232     def vpp_enable_traces_on_dut(node):
233         """Enable vpp packet traces on the DUT node.
234
235         :param node: DUT node to set up.
236         :type node: dict
237         """
238         vat = VatExecutor()
239         vat.execute_script("enable_dpdk_traces.vat", node, json_out=False)
240         vat.execute_script("enable_vhost_user_traces.vat", node, json_out=False)
241         vat.execute_script("enable_memif_traces.vat", node, json_out=False)
242
243     @staticmethod
244     def vpp_enable_traces_on_all_duts(nodes):
245         """Enable vpp packet traces on all DUTs in the given topology.
246
247         :param nodes: Nodes in the topology.
248         :type nodes: dict
249         """
250         for node in nodes.values():
251             if node['type'] == NodeType.DUT:
252                 VPPUtil.vpp_enable_traces_on_dut(node)
253
254     @staticmethod
255     def vpp_enable_elog_traces_on_dut(node):
256         """Enable API/CLI/Barrier traces on the DUT node.
257
258         :param node: DUT node to set up.
259         :type node: dict
260         """
261         vat = VatExecutor()
262         vat.execute_script("elog_trace_api_cli_barrier.vat", node,
263                            json_out=False)
264
265     @staticmethod
266     def vpp_enable_elog_traces_on_all_duts(nodes):
267         """Enable API/CLI/Barrier traces on all DUTs in the given topology.
268
269         :param nodes: Nodes in the topology.
270         :type nodes: dict
271         """
272         for node in nodes.values():
273             if node['type'] == NodeType.DUT:
274                 VPPUtil.vpp_enable_elog_traces_on_dut(node)
275
276     @staticmethod
277     def show_event_logger_on_dut(node):
278         """Show event logger on the DUT node.
279
280         :param node: DUT node to show traces on.
281         :type node: dict
282         """
283         vat = VatExecutor()
284         vat.execute_script("show_event_logger.vat", node, json_out=False)
285
286     @staticmethod
287     def show_event_logger_on_all_duts(nodes):
288         """Show event logger on all DUTs in the given topology.
289
290         :param nodes: Nodes in the topology.
291         :type nodes: dict
292         """
293         for node in nodes.values():
294             if node['type'] == NodeType.DUT:
295                 VPPUtil.show_event_logger_on_dut(node)
296
297     @staticmethod
298     def show_log(node):
299         """Show log on the specified topology node.
300
301         :param node: Topology node.
302         :type node: dict
303         :returns: VPP log data.
304         :rtype: list
305         """
306         with PapiExecutor(node) as papi_exec:
307             return papi_exec.add('cli_inband', cmd='show log').get_replies().\
308                 verify_reply()["reply"]
309
310     @staticmethod
311     def vpp_show_threads(node):
312         """Show VPP threads on node.
313
314         :param node: Node to run command on.
315         :type node: dict
316         :returns: VPP thread data.
317         :rtype: list
318         """
319         with PapiExecutor(node) as papi_exec:
320             return papi_exec.add('show_threads').execute_should_pass().\
321                 verify_reply()["thread_data"]