CSIT-1291 Improve service handling with container detection
[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 from resources.libraries.python.constants import Constants
16 from resources.libraries.python.DUTSetup import DUTSetup
17 from resources.libraries.python.ssh import exec_cmd_no_error
18 from resources.libraries.python.topology import NodeType
19 from resources.libraries.python.VatExecutor import VatExecutor
20
21
22 class VPPUtil(object):
23     """General class for any VPP related methods/functions."""
24
25     @staticmethod
26     def show_vpp_settings(node, *additional_cmds):
27         """Print default VPP settings. In case others are needed, can be
28         accepted as next parameters (each setting one parameter), preferably
29         in form of a string.
30
31         :param node: VPP node.
32         :param additional_cmds: Additional commands that the vpp should print
33             settings for.
34         :type node: dict
35         :type additional_cmds: tuple
36         """
37         def_setting_tb_displayed = {
38             'IPv6 FIB': 'ip6 fib',
39             'IPv4 FIB': 'ip fib',
40             'Interface IP': 'int addr',
41             'Interfaces': 'int',
42             'ARP': 'ip arp',
43             'Errors': 'err'
44         }
45
46         if additional_cmds:
47             for cmd in additional_cmds:
48                 def_setting_tb_displayed['Custom Setting: {}'.format(cmd)] = cmd
49
50         for _, cmd in def_setting_tb_displayed.items():
51             command = 'vppctl sh {cmd}'.format(cmd=cmd)
52             exec_cmd_no_error(node, command, timeout=30, sudo=True)
53
54     @staticmethod
55     def start_vpp_service(node):
56         """Start VPP service on the specified node.
57
58         :param node: VPP node.
59         :type node: dict
60         :raises RuntimeError: If VPP service fails to start.
61         """
62         DUTSetup.start_service(node, Constants.VPP_UNIT)
63         DUTSetup.get_service_logs(node, Constants.VPP_UNIT)
64
65     @staticmethod
66     def start_vpp_service_on_all_duts(nodes):
67         """Start up the VPP service on all nodes.
68
69         :param nodes: Nodes in the topology.
70         :type nodes: dict
71         """
72         DUTSetup.start_service_on_all_duts(nodes, Constants.VPP_UNIT)
73
74     @staticmethod
75     def stop_vpp_service(node):
76         """Stop VPP service on the specified node.
77
78         :param node: VPP node.
79         :type node: dict
80         :raises RuntimeError: If VPP service fails to stop.
81         """
82         DUTSetup.stop_service(node, Constants.VPP_UNIT)
83
84     @staticmethod
85     def verify_vpp_on_dut(node):
86         """Verify that VPP is installed on DUT node.
87
88         :param node: DUT node.
89         :type node: dict
90         :raises RuntimeError: If failed to restart VPP, get VPP version
91             or get VPP interfaces.
92         """
93         VPPUtil.vpp_show_version_verbose(node)
94         VPPUtil.vpp_show_interfaces(node)
95
96     @staticmethod
97     def verify_vpp_on_all_duts(nodes):
98         """Verify that VPP is installed on all DUT nodes.
99
100         :param nodes: Nodes in the topology.
101         :type nodes: dict
102         """
103         for node in nodes.values():
104             if node['type'] == NodeType.DUT:
105                 DUTSetup.start_service(node, Constants.VPP_UNIT)
106                 VPPUtil.vpp_show_version_verbose(node)
107                 VPPUtil.vpp_show_interfaces(node)
108
109     @staticmethod
110     def vpp_show_version_verbose(node):
111         """Run "show version verbose" CLI command.
112
113         :param node: Node to run command on.
114         :type node: dict
115         """
116         vat = VatExecutor()
117         vat.execute_script("show_version_verbose.vat", node, json_out=False)
118
119         try:
120             vat.script_should_have_passed()
121         except AssertionError:
122             raise RuntimeError('Failed to get VPP version on host: {name}'.
123                                format(name=node['host']))
124
125     @staticmethod
126     def show_vpp_version_on_all_duts(nodes):
127         """Show VPP version verbose on all DUTs.
128
129         :param nodes: VPP nodes.
130         :type nodes: dict
131         """
132         for node in nodes.values():
133             if node['type'] == NodeType.DUT:
134                 VPPUtil.vpp_show_version_verbose(node)
135
136     @staticmethod
137     def vpp_show_interfaces(node):
138         """Run "show interface" CLI command.
139
140         :param node: Node to run command on.
141         :type node: dict
142         """
143         vat = VatExecutor()
144         vat.execute_script("show_interface.vat", node, json_out=False)
145
146         try:
147             vat.script_should_have_passed()
148         except AssertionError:
149             raise RuntimeError('Failed to get VPP interfaces on host: {name}'.
150                                format(name=node['host']))
151
152     @staticmethod
153     def vpp_show_crypto_device_mapping(node):
154         """Run "show crypto device mapping" CLI command.
155
156         :param node: Node to run command on.
157         :type node: dict
158         """
159         vat = VatExecutor()
160         vat.execute_script("show_crypto_device_mapping.vat", node,
161                            json_out=False)
162
163     @staticmethod
164     def vpp_api_trace_dump(node):
165         """Run "api trace custom-dump" CLI command.
166
167         :param node: Node to run command on.
168         :type node: dict
169         """
170         vat = VatExecutor()
171         vat.execute_script("api_trace_dump.vat", node, json_out=False)
172
173     @staticmethod
174     def vpp_api_trace_save(node):
175         """Run "api trace save" CLI command.
176
177         :param node: Node to run command on.
178         :type node: dict
179         """
180         vat = VatExecutor()
181         vat.execute_script("api_trace_save.vat", node, json_out=False)
182
183     @staticmethod
184     def vpp_enable_traces_on_dut(node):
185         """Enable vpp packet traces on the DUT node.
186
187         :param node: DUT node to set up.
188         :type node: dict
189         """
190         vat = VatExecutor()
191         vat.execute_script("enable_dpdk_traces.vat", node, json_out=False)
192         vat.execute_script("enable_vhost_user_traces.vat", node, json_out=False)
193         vat.execute_script("enable_memif_traces.vat", node, json_out=False)
194
195     @staticmethod
196     def vpp_enable_traces_on_all_duts(nodes):
197         """Enable vpp packet traces on all DUTs in the given topology.
198
199         :param nodes: Nodes in the topology.
200         :type nodes: dict
201         """
202         for node in nodes.values():
203             if node['type'] == NodeType.DUT:
204                 VPPUtil.vpp_enable_traces_on_dut(node)