Update Honeycomb tests
[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
20 from resources.libraries.python.topology import NodeType, Topology
21 from resources.libraries.python.VatExecutor import VatExecutor, VatTerminal
22
23
24 class VppCounters(object):
25     """VPP counters utilities."""
26
27     def __init__(self):
28         self._stats_table = None
29
30     def vpp_nodes_clear_interface_counters(self, nodes):
31         """Clear interface counters on all VPP nodes in topology.
32
33         :param nodes: Nodes in topology.
34         :type nodes: dict
35         """
36         for node in nodes.values():
37             if node['type'] == NodeType.DUT:
38                 self.vpp_clear_interface_counters(node)
39
40     @staticmethod
41     def vpp_show_errors(node):
42         """Run "show errors" 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.vat", node, json_out=False)
49
50     @staticmethod
51     def vpp_show_errors_verbose(node):
52         """Run "show errors verbose" debug CLI command.
53
54         :param node: Node to run command on.
55         :type node: dict
56         """
57         vat = VatExecutor()
58         vat.execute_script("show_errors_verbose.vat", node, json_out=False)
59
60     @staticmethod
61     def vpp_show_runtime(node):
62         """Run "show runtime" CLI command.
63
64         :param node: Node to run command on.
65         :type node: dict
66         """
67         vat = VatExecutor()
68         vat.execute_script("show_runtime.vat", node, json_out=False)
69
70     @staticmethod
71     def vpp_show_runtime_verbose(node):
72         """Run "show runtime verbose" CLI command.
73
74         :param node: Node to run command on.
75         :type node: dict
76         """
77         vat = VatExecutor()
78         vat.execute_script("show_runtime_verbose.vat", node, json_out=False)
79
80     @staticmethod
81     def vpp_show_hardware_detail(node):
82         """Run "show hardware-interfaces detail" debug CLI command.
83
84         :param node: Node to run command on.
85         :type node: dict
86         """
87         vat = VatExecutor()
88         vat.execute_script("show_hardware_detail.vat", node, json_out=False)
89
90     @staticmethod
91     def vpp_clear_runtime(node):
92         """Run "clear runtime" CLI command.
93
94         :param node: Node to run command on.
95         :type node: dict
96         """
97         vat = VatExecutor()
98         vat.execute_script("clear_runtime.vat", node, json_out=False)
99
100     @staticmethod
101     def vpp_clear_interface_counters(node):
102         """Clear interface counters on VPP node.
103
104         :param node: Node to clear interface counters on.
105         :type node: dict
106         """
107         vat = VatExecutor()
108         vat.execute_script('clear_interface.vat', node)
109         vat.script_should_have_passed()
110
111     @staticmethod
112     def vpp_clear_hardware_counters(node):
113         """Clear interface hardware counters on VPP node.
114
115         :param node: Node to clear hardware counters on.
116         :type node: dict
117         """
118         vat = VatExecutor()
119         vat.execute_script('clear_hardware.vat', node)
120         vat.script_should_have_passed()
121
122     @staticmethod
123     def vpp_clear_errors_counters(node):
124         """Clear errors counters on VPP node.
125
126         :param node: Node to clear errors counters on.
127         :type node: dict
128         """
129         vat = VatExecutor()
130         vat.execute_script('clear_errors.vat', node)
131         vat.script_should_have_passed()
132
133     def vpp_dump_stats_table(self, node):
134         """Dump stats table on VPP node.
135
136         :param node: Node to dump stats table on.
137         :type node: dict
138         :return: Stats table.
139         """
140         with VatTerminal(node) as vat:
141             vat.vat_terminal_exec_cmd('want_stats enable')
142             for _ in range(0, 12):
143                 stats_table = vat.vat_terminal_exec_cmd('dump_stats_table')
144                 if_counters = stats_table['interface_counters']
145                 if len(if_counters) > 0:
146                     self._stats_table = stats_table
147                     return stats_table
148                 time.sleep(1)
149             return None
150
151     def vpp_get_ipv4_interface_counter(self, node, interface):
152         return self.vpp_get_ipv46_interface_counter(node, interface, False)
153
154     def vpp_get_ipv6_interface_counter(self, node, interface):
155         return self.vpp_get_ipv46_interface_counter(node, interface, True)
156
157     def vpp_get_ipv46_interface_counter(self, node, interface, is_ipv6=True):
158         """Return interface IPv4/IPv6 counter.
159
160         :param node: Node to get interface IPv4/IPv6 counter on.
161         :param interface: Interface name.
162         :param is_ipv6: Specify IP version.
163         :type node: dict
164         :type interface: str
165         :type is_ipv6: bool
166         :return: Interface IPv4/IPv6 counter.
167         :rtype: int
168         """
169         version = 'ip6' if is_ipv6 else 'ip4'
170         topo = Topology()
171         if_index = topo.get_interface_sw_index(node, interface)
172         if if_index is None:
173             logger.trace('{i} sw_index not found.'.format(i=interface))
174             return 0
175
176         if_counters = self._stats_table.get('interface_counters')
177         if if_counters is None or len(if_counters) == 0:
178             logger.trace('No interface counters.')
179             return 0
180         for counter in if_counters:
181             if counter['vnet_counter_type'] == version:
182                 data = counter['data']
183                 return data[if_index]
184         logger.trace('{i} {v} counter not found.'.format(i=interface,
185                                                          v=version))
186         return 0