-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdropcounters.py
More file actions
286 lines (235 loc) · 9.3 KB
/
Copy pathdropcounters.py
File metadata and controls
286 lines (235 loc) · 9.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
"""
Drop counters formatters.
This module contains formatters for dropcounters commands:
- DropcountersCapabilitiesFormatter: show dropcounters capabilities
- DropcountersCountsFormatter: show dropcounters counts
- DropcountersConfigurationFormatter: show dropcounters configuration
"""
from typing import Dict, List, Optional, Union
from tabulate import tabulate
from gnmi_cli_lib.formatters.base import OutputFormatter
from gnmi_cli_lib.common.types import CommandConfig
from gnmi_cli_lib.common.constants import (
STANDARD_PORT_COLUMNS,
DROPCOUNTERS_FIELD_MAPPING,
DROPCOUNTERS_HEADERS,
)
from gnmi_cli_lib.utils.sorting import natural_sort_key
from gnmi_cli_lib.utils.parsing import parse_reasons_string
class DropcountersCapabilitiesFormatter(OutputFormatter):
"""
Formats dropcounters capabilities output.
Example input:
{"PORT_INGRESS_DROPS": {"count": "10", "reasons": "[REASON1,REASON2]"}}
Example output:
Counter Type Total
------------------- -------
PORT_INGRESS_DROPS 10
PORT_INGRESS_DROPS
REASON1
REASON2
"""
def format(
self,
data: Dict,
config: CommandConfig,
table_format: str = "simple",
argument: str = "",
options: Optional[Dict] = None,
) -> str:
"""Format capabilities with summary table and reason lists."""
# Build summary table
table_data = []
reasons_sections = []
for counter_type in sorted(data.keys()):
counter_info = data[counter_type]
if isinstance(counter_info, dict):
count = counter_info.get("count", "N/A")
table_data.append([counter_type, count])
# Parse reasons string: "[REASON1,REASON2,...]" -> list
reasons_str = counter_info.get("reasons", "")
if reasons_str:
reasons = parse_reasons_string(reasons_str)
if reasons:
reasons_sections.append((counter_type, reasons))
# Generate output
lines = []
# Summary table
if table_data:
table_str = tabulate(
table_data,
headers=["Counter Type", "Total"],
tablefmt=table_format,
numalign="right",
)
lines.append(table_str)
# Reasons sections
for counter_type, reasons in reasons_sections:
lines.append("")
lines.append(counter_type)
for reason in reasons:
lines.append(f" {reason}")
return "\n".join(lines) + "\n"
class DropcountersCountsFormatter(OutputFormatter):
"""
Formats dropcounters counts output as two separate tables:
one for port-level counters (IFACE) and one for switch-level counters (DEVICE).
This matches the sonic-utilities ``dropstat`` behaviour which calls
``show_port_drop_counts`` and ``show_switch_drop_counts`` separately.
Example input:
{
"Ethernet0": {"State": "D", "RX_ERR": "10", "RX_DROPS": "100", ...},
"sonic_drops_test": {"SWITCH_DROPS": "1000", "lowercase_counter": "0"}
}
Example output:
IFACE STATE RX_ERR RX_DROPS TX_ERR TX_DROPS
--------- ------- -------- ---------- -------- ----------
Ethernet0 D 10 100 0 0
DEVICE SWITCH_DROPS lowercase_counter
---------------- -------------- -------------------
sonic_drops_test 1000 0
"""
def format(
self,
data: Dict,
config: CommandConfig,
table_format: str = "simple",
argument: str = "",
options: Optional[Dict] = None,
) -> str:
"""Format drop counters as two tables (port + switch)."""
if not data:
return ""
# Separate port-level entries (have "State") from switch-level entries
port_entries: Dict[str, Dict] = {}
switch_entries: Dict[str, Dict] = {}
for key, value in data.items():
if isinstance(value, dict):
if "State" in value:
port_entries[key] = value
else:
switch_entries[key] = value
sections: List[str] = []
# ── Port-level table (IFACE) ──
if port_entries:
port_table = self._build_port_table(port_entries, table_format)
if port_table:
sections.append(port_table)
# ── Switch-level table (DEVICE) ──
if switch_entries:
switch_table = self._build_switch_table(switch_entries, table_format)
if switch_table:
sections.append(switch_table)
if not sections:
return ""
# Join with blank line between tables (matches sonic-utilities dropstat)
return "\n\n".join(sections) + "\n"
# ------------------------------------------------------------------ #
def _build_port_table(self, entries: Dict[str, Dict],
table_format: str) -> str:
"""Build the IFACE table for port-level drop counters."""
# Collect columns present across all port entries
all_columns: set = set()
for port_data in entries.values():
all_columns.update(port_data.keys())
# Order: standard columns first, then debug counters sorted
debug_columns = sorted(c for c in all_columns
if c not in STANDARD_PORT_COLUMNS)
ordered_columns: List[str] = [
c for c in STANDARD_PORT_COLUMNS if c in all_columns
]
ordered_columns.extend(debug_columns)
headers = ["IFACE"] + [
col.upper() if col == "State" else col
for col in ordered_columns
]
table_data = []
for port in sorted(entries.keys(), key=natural_sort_key):
row = [port]
for col in ordered_columns:
row.append(entries[port].get(col, "N/A"))
table_data.append(row)
if not table_data:
return ""
return tabulate(
table_data,
headers=headers,
tablefmt=table_format,
numalign="right",
stralign="right",
)
def _build_switch_table(self, entries: Dict[str, Dict],
table_format: str) -> str:
"""Build the DEVICE table for switch-level drop counters."""
# Collect columns across all switch entries, preserve order
all_columns: set = set()
for sw_data in entries.values():
all_columns.update(sw_data.keys())
ordered_columns = sorted(all_columns)
headers = ["DEVICE"] + ordered_columns
table_data = []
for device in sorted(entries.keys(), key=natural_sort_key):
row = [device]
for col in ordered_columns:
row.append(entries[device].get(col, "N/A"))
table_data.append(row)
if not table_data:
return ""
return tabulate(
table_data,
headers=headers,
tablefmt=table_format,
numalign="right",
stralign="right",
)
class DropcountersConfigurationFormatter(OutputFormatter):
"""
Formats dropcounters configuration output.
Example input (array):
[{"name": "DEBUG_0", "alias": "DEBUG_0", "group": "N/A",
"type": "PORT_INGRESS_DROPS", "reason": "None", "description": "N/A"}]
Example output:
Counter Alias Group Type Reasons Description
--------- ------- ------- ------------------- --------- -----------
DEBUG_0 DEBUG_0 N/A PORT_INGRESS_DROPS None N/A
"""
def format(
self,
data: Union[Dict, List],
config: CommandConfig,
table_format: str = "simple",
argument: str = "",
options: Optional[Dict] = None,
) -> str:
"""Format configuration data as table."""
# Handle both list and dict formats
if isinstance(data, list):
items = data
elif isinstance(data, dict):
# Convert dict to list if needed
items = list(data.values()) if data else []
else:
return ""
if not items:
return ""
# Build table rows
table_data = []
for item in items:
if isinstance(item, dict):
row = []
for header in DROPCOUNTERS_HEADERS:
field = DROPCOUNTERS_FIELD_MAPPING.get(header, header.lower())
value = item.get(field, "N/A")
# Truncate long descriptions for display
if header == "Description" and len(str(value)) > 50:
value = str(value)[:47] + "..."
row.append(value)
table_data.append(row)
if not table_data:
return ""
table_str = tabulate(
table_data,
headers=DROPCOUNTERS_HEADERS,
tablefmt=table_format,
)
return table_str + "\n"