Revert "CSIT-986: Use MLRsearch from pip"
[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_counters = stats_table['interface_counters']
212                 if len(if_counters) > 0:
213                     self._stats_table = stats_table
214                     return stats_table
215                 time.sleep(1)
216             return None
217
218     def vpp_get_ipv4_interface_counter(self, node, interface):
219         """
220
221         :param node: Node to get interface IPv4 counter on.
222         :param interface: Interface name.
223         :type node: dict
224         :type interface: str
225         :returns: Interface IPv4 counter.
226         :rtype: int
227         """
228         return self.vpp_get_ipv46_interface_counter(node, interface, False)
229
230     def vpp_get_ipv6_interface_counter(self, node, interface):
231         """
232
233         :param node: Node to get interface IPv6 counter on.
234         :param interface: Interface name.
235         :type node: dict
236         :type interface: str
237         :returns: Interface IPv6 counter.
238         :rtype: int
239         """
240         return self.vpp_get_ipv46_interface_counter(node, interface, True)
241
242     def vpp_get_ipv46_interface_counter(self, node, interface, is_ipv6=True):
243         """Return interface IPv4/IPv6 counter.
244
245         :param node: Node to get interface IPv4/IPv6 counter on.
246         :param interface: Interface name.
247         :param is_ipv6: Specify IP version.
248         :type node: dict
249         :type interface: str
250         :type is_ipv6: bool
251         :returns: Interface IPv4/IPv6 counter.
252         :rtype: int
253         """
254         version = 'ip6' if is_ipv6 else 'ip4'
255         topo = Topology()
256         if_index = topo.get_interface_sw_index(node, interface)
257         if if_index is None:
258             logger.trace('{i} sw_index not found.'.format(i=interface))
259             return 0
260
261         if_counters = self._stats_table.get('interface_counters')
262         if if_counters is None or len(if_counters) == 0:
263             logger.trace('No interface counters.')
264             return 0
265         for counter in if_counters:
266             if counter['vnet_counter_type'] == version:
267                 data = counter['data']
268                 return data[if_index]
269         logger.trace('{i} {v} counter not found.'.format(i=interface,
270                                                          v=version))
271         return 0
272
273     @staticmethod
274     def show_vpp_statistics(node):
275         """Show [error, hardware, interface] stats.
276
277         :param node: VPP node.
278         :type node: dict
279         """
280         VppCounters.vpp_show_errors(node)
281         VppCounters.vpp_show_hardware_detail(node)
282         VppCounters.vpp_show_runtime(node)
283
284     @staticmethod
285     def show_statistics_on_all_duts(nodes, sleeptime=5):
286         """Show VPP statistics on all DUTs.
287
288         :param nodes: VPP nodes.
289         :type nodes: dict
290         :param sleeptime: Time to wait for traffic to arrive back to TG.
291         :type sleeptime: int
292         """
293         logger.trace('Waiting for statistics to be collected')
294         time.sleep(sleeptime)
295         for node in nodes.values():
296             if node['type'] == NodeType.DUT:
297                 VppCounters.show_vpp_statistics(node)