Add with-statment support to VatTerminal.
[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 from topology import NodeType, Topology
18 from VatExecutor import VatExecutor, VatTerminal
19 from robot.api import logger
20
21
22 class VppCounters(object):
23     """VPP counters utilities."""
24
25     def __init__(self):
26         self._stats_table = None
27
28     def vpp_nodes_clear_interface_counters(self, nodes):
29         """Clear interface counters on all VPP nodes in topology.
30
31            :param nodes: Nodes in topology.
32            :type nodes: dict
33         """
34         for node in nodes.values():
35             if node['type'] == NodeType.DUT:
36                 self.vpp_clear_interface_counters(node)
37
38     @staticmethod
39     def vpp_show_errors_verbose(node):
40         """Run "show errors verbose" debug CLI command
41
42         :param node: Node to run command on
43         :type node: dict
44         """
45         vat = VatExecutor()
46         vat.execute_script("show_errors_verbose.vat", node, json_out=False)
47
48     @staticmethod
49     def vpp_show_runtime_verbose(node):
50         """Run "show runtime" debug CLI command
51
52         :param node: Node to run command on
53         :type node: dict
54         """
55         vat = VatExecutor()
56         vat.execute_script("show_runtime_verbose.vat", node, json_out=False)
57
58     @staticmethod
59     def vpp_show_hardware_detail(node):
60         """Run "show hardware-interfaces detail" debug CLI command
61
62         :param node: Node to run command on
63         :type node: dict
64         """
65         vat = VatExecutor()
66         vat.execute_script("show_hardware_detail.vat", node, json_out=False)
67
68     @staticmethod
69     def vpp_clear_interface_counters(node):
70         """Clear interface counters on VPP node.
71
72            :param node: Node to clear interface counters on.
73            :type node: dict
74         """
75         vat = VatExecutor()
76         vat.execute_script('clear_interface.vat', node)
77         vat.script_should_have_passed()
78
79     def vpp_dump_stats_table(self, node):
80         """Dump stats table on VPP node.
81
82            :param node: Node to dump stats table on.
83            :type node: dict
84            :return: Stats table.
85         """
86         with VatTerminal(node) as vat:
87             vat.vat_terminal_exec_cmd('want_stats enable')
88             for _ in range(0, 12):
89                 stats_table = vat.vat_terminal_exec_cmd('dump_stats_table')
90                 if_counters = stats_table['interface_counters']
91                 if len(if_counters) > 0:
92                     self._stats_table = stats_table
93                     return stats_table
94                 time.sleep(1)
95             return None
96
97     def vpp_get_ipv4_interface_counter(self, node, interface):
98         return self.vpp_get_ipv46_interface_counter(node, interface, False)
99
100     def vpp_get_ipv6_interface_counter(self, node, interface):
101         return self.vpp_get_ipv46_interface_counter(node, interface, True)
102
103     def vpp_get_ipv46_interface_counter(self, node, interface, is_ipv6=True):
104         """Return interface IPv4/IPv6 counter
105
106            :param node: Node to get interface IPv4/IPv6 counter on.
107            :param interface: Interface name.
108            :type node: dict
109            :type interface: str
110            :return: Interface IPv4/IPv6 counter.
111            :param is_ipv6: specify IP version
112            :type is_ipv6: bool
113            :rtype: int
114         """
115         version = 'ip6' if is_ipv6 else 'ip4'
116         topo = Topology()
117         if_index = topo.get_interface_sw_index(node, interface)
118         if if_index is None:
119             logger.trace('{i} sw_index not found.'.format(i=interface))
120             return 0
121
122         if_counters = self._stats_table.get('interface_counters')
123         if if_counters is None or len(if_counters) == 0:
124             logger.trace('No interface counters.')
125             return 0
126         for counter in if_counters:
127             if counter['vnet_counter_type'] == version:
128                 data = counter['data']
129                 return data[if_index]
130         logger.trace('{i} {v} counter not found.'.format(i=interface,
131                                                          v=version))
132         return 0

©2016 FD.io a Linux Foundation Collaborative Project. All Rights Reserved.
Linux Foundation is a registered trademark of The Linux Foundation. Linux is a registered trademark of Linus Torvalds.
Please see our privacy policy and terms of use.