CSIT-1338 FIX: Failed to get VPP version on host
[csit.git] / resources / libraries / python / VPPUtil.py
1 # Copyright (c) 2018 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 time
17
18 from resources.libraries.python.constants import Constants
19 from resources.libraries.python.DUTSetup import DUTSetup
20 from resources.libraries.python.ssh import exec_cmd, exec_cmd_no_error
21 from resources.libraries.python.topology import NodeType
22 from resources.libraries.python.VatExecutor import VatExecutor
23
24
25 class VPPUtil(object):
26     """General class for any VPP related methods/functions."""
27
28     @staticmethod
29     def show_vpp_settings(node, *additional_cmds):
30         """Print default VPP settings. In case others are needed, can be
31         accepted as next parameters (each setting one parameter), preferably
32         in form of a string.
33
34         :param node: VPP node.
35         :param additional_cmds: Additional commands that the vpp should print
36             settings for.
37         :type node: dict
38         :type additional_cmds: tuple
39         """
40         def_setting_tb_displayed = {
41             'IPv6 FIB': 'ip6 fib',
42             'IPv4 FIB': 'ip fib',
43             'Interface IP': 'int addr',
44             'Interfaces': 'int',
45             'ARP': 'ip arp',
46             'Errors': 'err'
47         }
48
49         if additional_cmds:
50             for cmd in additional_cmds:
51                 def_setting_tb_displayed['Custom Setting: {}'.format(cmd)] = cmd
52
53         for _, cmd in def_setting_tb_displayed.items():
54             command = 'vppctl sh {cmd}'.format(cmd=cmd)
55             exec_cmd_no_error(node, command, timeout=30, sudo=True)
56
57     @staticmethod
58     def start_vpp_service(node, retries=60):
59         """Start VPP service on the specified node.
60
61         :param node: VPP node.
62         :param retries: Number of times (default 60) to re-try waiting.
63         :type node: dict
64         :type retries: int
65         :raises RuntimeError: If VPP service fails to start.
66         """
67         DUTSetup.start_service(node, Constants.VPP_UNIT)
68         # Sleep 1 second, up to <retry> times,
69         # and verify if VPP is running.
70         for _ in range(retries):
71             time.sleep(1)
72             command = 'vppctl show pci'
73             ret, stdout, _ = exec_cmd(node, command, timeout=30, sudo=True)
74             if not ret and 'Connection refused' not in stdout:
75                 break
76         else:
77             raise RuntimeError('VPP failed to start on host {name}'.
78                                    format(name=node['host']))
79         DUTSetup.get_service_logs(node, Constants.VPP_UNIT)
80
81     @staticmethod
82     def start_vpp_service_on_all_duts(nodes):
83         """Start up the VPP service on all nodes.
84
85         :param nodes: Nodes in the topology.
86         :type nodes: dict
87         """
88         for node in nodes.values():
89             if node['type'] == NodeType.DUT:
90                 VPPUtil.start_vpp_service(node)
91
92     @staticmethod
93     def stop_vpp_service(node):
94         """Stop VPP service on the specified node.
95
96         :param node: VPP node.
97         :type node: dict
98         :raises RuntimeError: If VPP service fails to stop.
99         """
100         DUTSetup.stop_service(node, Constants.VPP_UNIT)
101
102     @staticmethod
103     def stop_vpp_service_on_all_duts(nodes):
104         """Stop VPP service on all nodes.
105
106         :param nodes: Nodes in the topology.
107         :type nodes: dict
108         """
109         for node in nodes.values():
110             if node['type'] == NodeType.DUT:
111                 VPPUtil.stop_vpp_service(node)
112
113     @staticmethod
114     def verify_vpp_on_dut(node):
115         """Verify that VPP is installed on DUT node.
116
117         :param node: DUT node.
118         :type node: dict
119         :raises RuntimeError: If failed to restart VPP, get VPP version
120             or get VPP interfaces.
121         """
122         VPPUtil.vpp_show_version_verbose(node)
123         VPPUtil.vpp_show_interfaces(node)
124
125     @staticmethod
126     def verify_vpp_on_all_duts(nodes):
127         """Verify that VPP is installed on all DUT nodes.
128
129         :param nodes: Nodes in the topology.
130         :type nodes: dict
131         """
132         for node in nodes.values():
133             if node['type'] == NodeType.DUT:
134                 VPPUtil.start_vpp_service(node)
135                 VPPUtil.verify_vpp_on_dut(node)
136
137     @staticmethod
138     def vpp_show_version_verbose(node):
139         """Run "show version verbose" CLI command.
140
141         :param node: Node to run command on.
142         :type node: dict
143         """
144         vat = VatExecutor()
145         vat.execute_script("show_version_verbose.vat", node, json_out=False)
146
147         try:
148             vat.script_should_have_passed()
149         except AssertionError:
150             raise RuntimeError('Failed to get VPP version on host: {name}'.
151                                format(name=node['host']))
152
153     @staticmethod
154     def show_vpp_version_on_all_duts(nodes):
155         """Show VPP version verbose on all DUTs.
156
157         :param nodes: VPP nodes.
158         :type nodes: dict
159         """
160         for node in nodes.values():
161             if node['type'] == NodeType.DUT:
162                 VPPUtil.vpp_show_version_verbose(node)
163
164     @staticmethod
165     def vpp_show_interfaces(node):
166         """Run "show interface" CLI command.
167
168         :param node: Node to run command on.
169         :type node: dict
170         """
171         vat = VatExecutor()
172         vat.execute_script("show_interface.vat", node, json_out=False)
173
174         try:
175             vat.script_should_have_passed()
176         except AssertionError:
177             raise RuntimeError('Failed to get VPP interfaces on host: {name}'.
178                                format(name=node['host']))
179
180     @staticmethod
181     def vpp_show_crypto_device_mapping(node):
182         """Run "show crypto device mapping" CLI command.
183
184         :param node: Node to run command on.
185         :type node: dict
186         """
187         vat = VatExecutor()
188         vat.execute_script("show_crypto_device_mapping.vat", node,
189                            json_out=False)
190
191     @staticmethod
192     def vpp_api_trace_dump(node):
193         """Run "api trace custom-dump" CLI command.
194
195         :param node: Node to run command on.
196         :type node: dict
197         """
198         vat = VatExecutor()
199         vat.execute_script("api_trace_dump.vat", node, json_out=False)
200
201     @staticmethod
202     def vpp_api_trace_save(node):
203         """Run "api trace save" CLI command.
204
205         :param node: Node to run command on.
206         :type node: dict
207         """
208         vat = VatExecutor()
209         vat.execute_script("api_trace_save.vat", node, json_out=False)
210
211     @staticmethod
212     def vpp_enable_traces_on_dut(node):
213         """Enable vpp packet traces on the DUT node.
214
215         :param node: DUT node to set up.
216         :type node: dict
217         """
218         vat = VatExecutor()
219         vat.execute_script("enable_dpdk_traces.vat", node, json_out=False)
220         vat.execute_script("enable_vhost_user_traces.vat", node, json_out=False)
221         vat.execute_script("enable_memif_traces.vat", node, json_out=False)
222
223     @staticmethod
224     def vpp_enable_traces_on_all_duts(nodes):
225         """Enable vpp packet traces on all DUTs in the given topology.
226
227         :param nodes: Nodes in the topology.
228         :type nodes: dict
229         """
230         for node in nodes.values():
231             if node['type'] == NodeType.DUT:
232                 VPPUtil.vpp_enable_traces_on_dut(node)
233
234     @staticmethod
235     def vpp_enable_elog_traces_on_dut(node):
236         """Enable API/CLI/Barrier traces on the DUT node.
237
238         :param node: DUT node to set up.
239         :type node: dict
240         """
241         vat = VatExecutor()
242         vat.execute_script("elog_trace_api_cli_barrier.vat", node,
243                            json_out=False)
244
245     @staticmethod
246     def vpp_enable_elog_traces_on_all_duts(nodes):
247         """Enable API/CLI/Barrier traces on all DUTs in the given topology.
248
249         :param nodes: Nodes in the topology.
250         :type nodes: dict
251         """
252         for node in nodes.values():
253             if node['type'] == NodeType.DUT:
254                 VPPUtil.vpp_enable_elog_traces_on_dut(node)
255
256     @staticmethod
257     def show_event_logger_on_dut(node):
258         """Show event logger on the DUT node.
259
260         :param node: DUT node to show traces on.
261         :type node: dict
262         """
263         vat = VatExecutor()
264         vat.execute_script("show_event_logger.vat", node, json_out=False)
265
266     @staticmethod
267     def show_event_logger_on_all_duts(nodes):
268         """Show event logger on all DUTs in the given topology.
269
270         :param nodes: Nodes in the topology.
271         :type nodes: dict
272         """
273         for node in nodes.values():
274             if node['type'] == NodeType.DUT:
275                 VPPUtil.show_event_logger_on_dut(node)