Fix various pylint violations
[csit.git] / resources / libraries / python / VppCounters.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 """VPP counters utilities library."""
15
16 import time
17
18 from robot.api import logger
19 from resources.libraries.python.topology import NodeType, Topology
20 from resources.libraries.python.VatExecutor import VatExecutor, VatTerminal
21
22
23 class VppCounters(object):
24     """VPP counters utilities."""
25
26     def __init__(self):
27         self._stats_table = None
28
29     @staticmethod
30     def vpp_show_errors(node):
31         """Run "show errors" debug CLI command.
32
33         :param node: Node to run command on.
34         :type node: dict
35         """
36         vat = VatExecutor()
37         vat.execute_script("show_errors.vat", node, json_out=False)
38         vat.script_should_have_passed()
39
40     @staticmethod
41     def vpp_show_errors_verbose(node):
42         """Run "show errors verbose" debug CLI command.
43
44         :param node: Node to run command on.
45         :type node: dict
46         """
47         vat = VatExecutor()
48         vat.execute_script("show_errors_verbose.vat", node, json_out=False)
49         vat.script_should_have_passed()
50
51     @staticmethod
52     def vpp_show_errors_on_all_duts(nodes, verbose=False):
53         """Show errors on all DUTs.
54
55         :param nodes: VPP nodes.
56         :param verbose: If True show verbose output.
57         :type nodes: dict
58         :type verbose: bool
59         """
60
61         for node in nodes.values():
62             if node['type'] == NodeType.DUT:
63                 if verbose:
64                     VppCounters.vpp_show_errors_verbose(node)
65                 else:
66                     VppCounters.vpp_show_errors(node)
67
68     @staticmethod
69     def vpp_show_runtime(node):
70         """Run "show runtime" CLI command.
71
72         :param node: Node to run command on.
73         :type node: dict
74         """
75         vat = VatExecutor()
76         vat.execute_script("show_runtime.vat", node, json_out=False)
77         vat.script_should_have_passed()
78
79     @staticmethod
80     def show_runtime_counters_on_all_duts(nodes):
81         """Clear VPP runtime counters on all DUTs.
82
83         :param nodes: VPP nodes.
84         :type nodes: dict
85         """
86         for node in nodes.values():
87             if node['type'] == NodeType.DUT:
88                 VppCounters.vpp_show_runtime(node)
89
90     @staticmethod
91     def vpp_show_runtime_verbose(node):
92         """Run "show runtime verbose" CLI command.
93
94         :param node: Node to run command on.
95         :type node: dict
96         """
97         vat = VatExecutor()
98         vat.execute_script("show_runtime_verbose.vat", node, json_out=False)
99         vat.script_should_have_passed()
100
101     @staticmethod
102     def vpp_show_hardware_detail(node):
103         """Run "show hardware-interfaces detail" debug CLI command.
104
105         :param node: Node to run command on.
106         :type node: dict
107         """
108         vat = VatExecutor()
109         vat.execute_script("show_hardware_detail.vat", node, json_out=False)
110         vat.script_should_have_passed()
111
112     @staticmethod
113     def vpp_clear_runtime(node):
114         """Run "clear runtime" CLI command.
115
116         :param node: Node to run command on.
117         :type node: dict
118         """
119         vat = VatExecutor()
120         vat.execute_script("clear_runtime.vat", node, json_out=False)
121         vat.script_should_have_passed()
122
123     @staticmethod
124     def clear_runtime_counters_on_all_duts(nodes):
125         """Run "clear runtime" CLI command on all DUTs.
126
127         :param nodes: VPP nodes.
128         :type nodes: dict
129         """
130         for node in nodes.values():
131             if node['type'] == NodeType.DUT:
132                 VppCounters.vpp_clear_runtime(node)
133
134     @staticmethod
135     def vpp_clear_interface_counters(node):
136         """Clear interface counters on VPP node.
137
138         :param node: Node to clear interface counters on.
139         :type node: dict
140         """
141         vat = VatExecutor()
142         vat.execute_script('clear_interface.vat', node)
143         vat.script_should_have_passed()
144
145     @staticmethod
146     def clear_interface_counters_on_all_duts(nodes):
147         """Clear interface counters on all DUTs.
148
149         :param nodes: VPP nodes.
150         :type nodes: dict
151         """
152         for node in nodes.values():
153             if node['type'] == NodeType.DUT:
154                 VppCounters.vpp_clear_interface_counters(node)
155
156     @staticmethod
157     def vpp_clear_hardware_counters(node):
158         """Clear interface hardware counters on VPP node.
159
160         :param node: Node to clear hardware counters on.
161         :type node: dict
162         """
163         vat = VatExecutor()
164         vat.execute_script('clear_hardware.vat', node)
165         vat.script_should_have_passed()
166
167     @staticmethod
168     def clear_hardware_counters_on_all_duts(nodes):
169         """Clear hardware counters on all DUTs.
170
171         :param nodes: VPP nodes.
172         :type nodes: dict
173         """
174         for node in nodes.values():
175             if node['type'] == NodeType.DUT:
176                 VppCounters.vpp_clear_hardware_counters(node)
177
178     @staticmethod
179     def vpp_clear_errors_counters(node):
180         """Clear errors counters on VPP node.
181
182         :param node: Node to clear errors counters on.
183         :type node: dict
184         """
185         vat = VatExecutor()
186         vat.execute_script('clear_errors.vat', node)
187         vat.script_should_have_passed()
188
189     @staticmethod
190     def clear_error_counters_on_all_duts(nodes):
191         """Clear VPP errors counters on all DUTs.
192
193         :param nodes: VPP nodes.
194         :type nodes: dict
195         """
196         for node in nodes.values():
197             if node['type'] == NodeType.DUT:
198                 VppCounters.vpp_clear_errors_counters(node)
199
200     def vpp_dump_stats_table(self, node):
201         """Dump stats table on VPP node.
202
203         :param node: Node to dump stats table on.
204         :type node: dict
205         :returns: Stats table.
206         """
207         with VatTerminal(node) as vat:
208             vat.vat_terminal_exec_cmd('want_stats enable')
209             for _ in range(0, 12):
210                 stats_table = vat.vat_terminal_exec_cmd('dump_stats_table')
211                 if stats_table['interface_counters']:
212                     self._stats_table = stats_table
213                     return stats_table
214                 time.sleep(1)
215             return None
216
217     def vpp_get_ipv4_interface_counter(self, node, interface):
218         """
219
220         :param node: Node to get interface IPv4 counter on.
221         :param interface: Interface name.
222         :type node: dict
223         :type interface: str
224         :returns: Interface IPv4 counter.
225         :rtype: int
226         """
227         return self.vpp_get_ipv46_interface_counter(node, interface, False)
228
229     def vpp_get_ipv6_interface_counter(self, node, interface):
230         """
231
232         :param node: Node to get interface IPv6 counter on.
233         :param interface: Interface name.
234         :type node: dict
235         :type interface: str
236         :returns: Interface IPv6 counter.
237         :rtype: int
238         """
239         return self.vpp_get_ipv46_interface_counter(node, interface, True)
240
241     def vpp_get_ipv46_interface_counter(self, node, interface, is_ipv6=True):
242         """Return interface IPv4/IPv6 counter.
243
244         :param node: Node to get interface IPv4/IPv6 counter on.
245         :param interface: Interface name.
246         :param is_ipv6: Specify IP version.
247         :type node: dict
248         :type interface: str
249         :type is_ipv6: bool
250         :returns: Interface IPv4/IPv6 counter.
251         :rtype: int
252         """
253         version = 'ip6' if is_ipv6 else 'ip4'
254         topo = Topology()
255         if_index = topo.get_interface_sw_index(node, interface)
256         if if_index is None:
257             logger.trace('{i} sw_index not found.'.format(i=interface))
258             return 0
259
260         if_counters = self._stats_table.get('interface_counters')
261         if not if_counters:
262             logger.trace('No interface counters.')
263             return 0
264         for counter in if_counters:
265             if counter['vnet_counter_type'] == version:
266                 data = counter['data']
267                 return data[if_index]
268         logger.trace('{i} {v} counter not found.'.format(
269             i=interface, v=version))
270         return 0
271
272     @staticmethod
273     def show_vpp_statistics(node):
274         """Show [error, hardware, interface] stats.
275
276         :param node: VPP node.
277         :type node: dict
278         """
279         VppCounters.vpp_show_errors(node)
280         VppCounters.vpp_show_hardware_detail(node)
281         VppCounters.vpp_show_runtime(node)
282
283     @staticmethod
284     def show_statistics_on_all_duts(nodes, sleeptime=5):
285         """Show VPP statistics on all DUTs.
286
287         :param nodes: VPP nodes.
288         :type nodes: dict
289         :param sleeptime: Time to wait for traffic to arrive back to TG.
290         :type sleeptime: int
291         """
292         logger.trace('Waiting for statistics to be collected')
293         time.sleep(sleeptime)
294         for node in nodes.values():
295             if node['type'] == NodeType.DUT:
296                 VppCounters.show_vpp_statistics(node)