forked from sonic-net/sonic-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintfutil
More file actions
executable file
·525 lines (453 loc) · 21.2 KB
/
intfutil
File metadata and controls
executable file
·525 lines (453 loc) · 21.2 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#! /usr/bin/python
import argparse
import os
import re
import sys
import types
from natsort import natsorted
from tabulate import tabulate
from utilities_common import constants
from utilities_common import multi_asic as multi_asic_util
from utilities_common.intf_filter import parse_interface_in_filter
# mock the redis for unit test purposes #
try:
if os.environ["UTILITIES_UNIT_TESTING"] == "2":
modules_path = os.path.join(os.path.dirname(__file__), "..")
tests_path = os.path.join(modules_path, "tests")
sys.path.insert(0, modules_path)
sys.path.insert(0, tests_path)
import mock_tables.dbconnector
if os.environ["UTILITIES_UNIT_TESTING_TOPOLOGY"] == "multi_asic":
import mock_tables.mock_multi_asic
mock_tables.dbconnector.load_namespace_config()
except KeyError:
pass
# ========================== Common interface-utils logic ==========================
PORT_STATUS_TABLE_PREFIX = "PORT_TABLE:"
PORT_TRANSCEIVER_TABLE_PREFIX = "TRANSCEIVER_INFO|"
PORT_LANES_STATUS = "lanes"
PORT_ALIAS = "alias"
PORT_OPER_STATUS = "oper_status"
PORT_ADMIN_STATUS = "admin_status"
PORT_SPEED = "speed"
PORT_MTU_STATUS = "mtu"
PORT_FEC = "fec"
PORT_DESCRIPTION = "description"
PORT_OPTICS_TYPE = "type"
PORT_PFC_ASYM_STATUS = "pfc_asym"
VLAN_SUB_INTERFACE_SEPARATOR = "."
VLAN_SUB_INTERFACE_TYPE = "802.1q-encapsulation"
SUB_PORT = "subport"
def get_frontpanel_port_list(config_db):
ports_dict = config_db.get_table('PORT')
front_panel_ports_list = []
for port in ports_dict.iterkeys():
front_panel_ports_list.append(port)
return front_panel_ports_list
def get_sub_port_intf_list(config_db):
sub_intf_dict = config_db.get_table('VLAN_SUB_INTERFACE')
sub_intf_list = []
for sub_intf in sub_intf_dict.keys():
if isinstance(sub_intf, basestring):
sub_intf_list.append(sub_intf)
return sub_intf_list
def get_interface_vlan_dict(config_db):
"""
Get info from REDIS ConfigDB and create interface to vlan mapping
"""
get_int_vlan_configdb_info = config_db.get_table('VLAN_MEMBER')
int_list = []
vlan_list = []
for line in get_int_vlan_configdb_info:
vlan_number = line[0]
interface = line[1]
int_list.append(interface)
vlan_list.append(vlan_number)
int_to_vlan_dict = dict(zip(int_list, vlan_list))
return int_to_vlan_dict
def config_db_vlan_port_keys_get(int_to_vlan_dict, front_panel_ports_list, intf_name):
"""
Get interface vlan value and return it.
"""
vlan = "routed"
if intf_name in front_panel_ports_list:
if intf_name in int_to_vlan_dict.keys():
vlan = int_to_vlan_dict[intf_name]
if "Vlan" in vlan:
vlan = "trunk"
return vlan
def appl_db_keys_get(appl_db, front_panel_ports_list, intf_name):
"""
Get APPL_DB Keys
"""
if intf_name is None:
appl_db_keys = appl_db.keys(appl_db.APPL_DB, "PORT_TABLE:*")
elif intf_name in front_panel_ports_list:
appl_db_keys = appl_db.keys(appl_db.APPL_DB, "PORT_TABLE:%s" % intf_name)
else:
return None
return appl_db_keys
def appl_db_sub_intf_keys_get(appl_db, sub_intf_list, sub_intf_name):
"""
Get APPL_DB sub port interface keys
"""
if sub_intf_name is None:
appl_db_sub_intf_keys = []
appl_db_intf_keys = appl_db.keys(appl_db.APPL_DB, "INTF_TABLE:*")
if appl_db_intf_keys is not None:
for appl_db_intf_key in appl_db_intf_keys:
if re.split(':', appl_db_intf_key, maxsplit=1)[-1].strip() in sub_intf_list:
appl_db_sub_intf_keys.append(appl_db_intf_key)
elif sub_intf_name in sub_intf_list:
appl_db_sub_intf_keys = appl_db.keys(appl_db.APPL_DB, "INTF_TABLE:%s" % sub_intf_name)
else:
return []
return appl_db_sub_intf_keys
def appl_db_port_status_get(appl_db, intf_name, status_type):
"""
Get the port status
"""
full_table_id = PORT_STATUS_TABLE_PREFIX + intf_name
status = appl_db.get(appl_db.APPL_DB, full_table_id, status_type)
if status is None:
return "N/A"
if status_type == PORT_SPEED and status != "N/A":
status = '{}G'.format(status[:-3])
return status
def state_db_port_optics_get(state_db, intf_name, type):
"""
Get optic type info for port
"""
full_table_id = PORT_TRANSCEIVER_TABLE_PREFIX + intf_name
optics_type = state_db.get(state_db.STATE_DB, full_table_id, type)
if optics_type is None:
return "N/A"
return optics_type
def merge_dicts(x,y):
# store a copy of x, but overwrite with y's values where applicable
merged = dict(x,**y)
xkeys = x.keys()
# if the value of merged[key] was overwritten with y[key]'s value
# then we need to put back any missing x[key] values
for key in xkeys:
# if this key is a dictionary, recurse
if type(x[key]) is types.DictType and y.has_key(key):
merged[key] = merge(x[key],y[key])
return merged
def tuple_to_dict(tup, new_dict):
"""
From a tuple create a dictionary that uses the first item in the tuple as a key
and the 2nd item in the tuple as a value.
"""
for a, b in tup:
new_dict.setdefault(a, []).append(b)
return new_dict
def get_raw_portchannel_info(config_db):
"""
This function uses the redis config_db as input and gets the "PORTCHANNEL_MEMBER" table
create
>>> get_po_int_configdb_info = get_portchannel_info(config_db)
>>> pprint(get_po_int_configdb_info)
{('PortChannel0001', 'Ethernet108'): {},
('PortChannel0001', 'Ethernet112'): {},
('PortChannel0002', 'Ethernet116'): {},
('PortChannel0003', 'Ethernet120'): {},
('PortChannel0004', 'Ethernet124'): {}}
This function returns a dictionary with the key being portchannels and interface tuple.
"""
get_raw_po_int_configdb_info = config_db.get_table('PORTCHANNEL_MEMBER')
return get_raw_po_int_configdb_info # Return a dictionary with the key being the portchannel and interface
def get_portchannel_list(get_raw_po_int_configdb_info):
"""
>>> portchannel_list = get_portchannel_list(get_raw_po_int_configdb_info)
>>> pprint(portchannel_list)
['PortChannel0001', 'PortChannel0002', 'PortChannel0003', 'PortChannel0004']
>>>
"""
portchannel_list = []
for po in get_raw_po_int_configdb_info:
portchannel = po[0]
if portchannel not in portchannel_list:
portchannel_list.append(portchannel)
return natsorted(portchannel_list)
def create_po_int_tuple_list(get_raw_po_int_configdb_info):
"""
>>> po_int_tuple = get_raw_po_int_configdb_info.keys()
>>> pprint(po_int_tuple_list)
[('PortChannel0001', 'Ethernet108'),
('PortChannel0002', 'Ethernet116'),
('PortChannel0004', 'Ethernet124'),
('PortChannel0003', 'Ethernet120'),
('PortChannel0001', 'Ethernet112')]
>>>
"""
po_int_tuple_list = get_raw_po_int_configdb_info.keys()
return po_int_tuple_list
def create_po_int_dict(po_int_tuple_list):
"""
This function takes the portchannel to interface tuple
and converts that into a portchannel to interface dictionary
with the portchannels as the key and the interfaces as the values.
"""
temp_dict = {}
po_int_dict = tuple_to_dict(po_int_tuple_list, temp_dict)
return po_int_dict
def create_int_to_portchannel_dict(po_int_tuple_list):
"""
This function takes the portchannel to interface tuple
and converts that into an interface to portchannel dictionary
with the interfaces as the key and the portchannels as the values.
"""
int_po_dict = {}
for po, intf in po_int_tuple_list:
int_po_dict.setdefault(intf, po)
return int_po_dict
def po_speed_dict(po_int_dict, appl_db):
"""
This function takes the portchannel to interface dictionary
and the appl_db and then creates a portchannel to speed
dictionary.
"""
if po_int_dict:
po_list = []
for key, value in po_int_dict.iteritems():
agg_speed_list = []
po_list.append(key)
if len(value) == 1:
interface_speed = appl_db.get(appl_db.APPL_DB, "PORT_TABLE:" + value[0], "speed")
if interface_speed == None:
po_list.append("N/A")
else:
interface_speed = '{}G'.format(interface_speed[:-3])
po_list.append(interface_speed)
elif len(value) > 1:
for intf in value:
temp_speed = appl_db.get(appl_db.APPL_DB, "PORT_TABLE:" + intf, "speed")
temp_speed = int(temp_speed) if temp_speed else 0
agg_speed_list.append(temp_speed)
interface_speed = sum(agg_speed_list)
interface_speed = str(interface_speed)
interface_speed = '{}G'.format(interface_speed[:-3])
po_list.append(interface_speed)
po_speed_dict = dict(po_list[i:i+2] for i in range(0, len(po_list), 2))
return po_speed_dict
else:
po_speed_dict = {}
return po_speed_dict
def appl_db_portchannel_status_get(appl_db, config_db, po_name, status_type, portchannel_speed_dict, combined_int_to_vlan_po_dict=None):
"""
Get the port status
"""
full_table_id = "LAG_TABLE:" + po_name
po_table_id = "PORTCHANNEL|" + po_name
#print(full_table_id)
if status_type == "speed":
status = portchannel_speed_dict[po_name]
return status
if status_type == "vlan":
if combined_int_to_vlan_po_dict and po_name in combined_int_to_vlan_po_dict.keys():
status = "trunk"
else:
status = "routed"
return status
if status_type == "mtu":
status = config_db.get(config_db.CONFIG_DB, po_table_id, status_type)
return status
status = appl_db.get(appl_db.APPL_DB, full_table_id, status_type)
#print(status)
if status is None:
return "N/A"
return status
def appl_db_sub_intf_status_get(appl_db, config_db, front_panel_ports_list, portchannel_speed_dict, sub_intf_name, status_type):
sub_intf_sep_idx = sub_intf_name.find(VLAN_SUB_INTERFACE_SEPARATOR)
if sub_intf_sep_idx != -1:
parent_port_name = sub_intf_name[:sub_intf_sep_idx]
vlan_id = sub_intf_name[sub_intf_sep_idx + 1:]
full_intf_table_name = "INTF_TABLE" + ":" + sub_intf_name
if status_type == "vlan":
return vlan_id
if status_type == "admin_status":
status = appl_db.get(appl_db.APPL_DB, full_intf_table_name, status_type)
return status if status is not None else "N/A"
if status_type == "type":
return VLAN_SUB_INTERFACE_TYPE
if status_type == "mtu" or status_type == "speed":
if parent_port_name in front_panel_ports_list:
return appl_db_port_status_get(appl_db, parent_port_name, status_type)
elif parent_port_name in portchannel_speed_dict.keys():
return appl_db_portchannel_status_get(appl_db, config_db, parent_port_name, status_type, portchannel_speed_dict)
else:
return "N/A"
return "N/A"
# ========================== interface-status logic ==========================
header_stat = ['Interface', 'Lanes', 'Speed', 'MTU', 'FEC', 'Alias', 'Vlan', 'Oper', 'Admin', 'Type', 'Asym PFC']
header_stat_sub_intf = ['Sub port interface', 'Speed', 'MTU', 'Vlan', 'Admin', 'Type']
class IntfStatus(object):
def __init__(self, intf_name, namespace_option, display_option):
"""
Class constructor method
:param self:
:param intf_name: string of interface
:return:
"""
self.db = None
self.config_db = None
self.sub_intf_only = False
self.intf_name = intf_name
self.sub_intf_name = intf_name
self.table = []
self.multi_asic = multi_asic_util.MultiAsic(
display_option, namespace_option)
if intf_name is not None:
if intf_name == SUB_PORT:
self.intf_name = None
self.sub_intf_name = None
self.sub_intf_only = True
else:
sub_intf_sep_idx = intf_name.find(VLAN_SUB_INTERFACE_SEPARATOR)
if sub_intf_sep_idx != -1:
self.sub_intf_only = True
self.intf_name = intf_name[:sub_intf_sep_idx]
def display_intf_status(self):
self.get_intf_status()
sorted_table = natsorted(self.table)
print tabulate(sorted_table,
header_stat if not self.sub_intf_only else header_stat_sub_intf,
tablefmt="simple",
stralign='right')
def generate_intf_status(self):
"""
Generate interface-status output
"""
i = {}
table = []
key = []
intf_fs = parse_interface_in_filter(self.intf_name)
#
# Iterate through all the keys and append port's associated state to
# the result table.
#
if not self.sub_intf_only:
for i in self.appl_db_keys:
key = re.split(':', i, maxsplit=1)[-1].strip()
if key in self.front_panel_ports_list:
if self.multi_asic.skip_display(constants.PORT_OBJ, key):
continue
if self.intf_name is None or key in intf_fs:
table.append((key,
appl_db_port_status_get(self.db, key, PORT_LANES_STATUS),
appl_db_port_status_get(self.db, key, PORT_SPEED),
appl_db_port_status_get(self.db, key, PORT_MTU_STATUS),
appl_db_port_status_get(self.db, key, PORT_FEC),
appl_db_port_status_get(self.db, key, PORT_ALIAS),
config_db_vlan_port_keys_get(self.combined_int_to_vlan_po_dict, self.front_panel_ports_list, key),
appl_db_port_status_get(self.db, key, PORT_OPER_STATUS),
appl_db_port_status_get(self.db, key, PORT_ADMIN_STATUS),
state_db_port_optics_get(self.db, key, PORT_OPTICS_TYPE),
appl_db_port_status_get(self.db, key, PORT_PFC_ASYM_STATUS)))
for po, value in self.portchannel_speed_dict.iteritems():
if po:
if self.multi_asic.skip_display(constants.PORT_CHANNEL_OBJ, po):
continue
if self.intf_name is None or po in intf_fs:
table.append((po,
appl_db_portchannel_status_get(self.db, self.config_db, po, PORT_LANES_STATUS, self.portchannel_speed_dict),
appl_db_portchannel_status_get(self.db, self.config_db, po, PORT_SPEED, self.portchannel_speed_dict),
appl_db_portchannel_status_get(self.db, self.config_db, po, PORT_MTU_STATUS, self.portchannel_speed_dict),
appl_db_portchannel_status_get(self.db, self.config_db, po, PORT_FEC, self.portchannel_speed_dict),
appl_db_portchannel_status_get(self.db, self.config_db, po, PORT_ALIAS, self.portchannel_speed_dict),
appl_db_portchannel_status_get(self.db, self.config_db, po, "vlan", self.portchannel_speed_dict),
appl_db_portchannel_status_get(self.db, self.config_db, po, PORT_OPER_STATUS, self.portchannel_speed_dict),
appl_db_portchannel_status_get(self.db, self.config_db, po, PORT_ADMIN_STATUS, self.portchannel_speed_dict),
appl_db_portchannel_status_get(self.db, self.config_db, po, PORT_OPTICS_TYPE, self.portchannel_speed_dict),
appl_db_portchannel_status_get(self.db, self.config_db, po, PORT_PFC_ASYM_STATUS, self.portchannel_speed_dict)))
else:
for key in self.appl_db_sub_intf_keys:
sub_intf = re.split(':', key, maxsplit=1)[-1].strip()
if sub_intf in self.sub_intf_list:
table.append((sub_intf,
appl_db_sub_intf_status_get(self.db, self.config_db, self.front_panel_ports_list, self.portchannel_speed_dict, sub_intf, PORT_SPEED),
appl_db_sub_intf_status_get(self.db, self.config_db, self.front_panel_ports_list, self.portchannel_speed_dict, sub_intf, PORT_MTU_STATUS),
appl_db_sub_intf_status_get(self.db, self.config_db, self.front_panel_ports_list, self.portchannel_speed_dict, sub_intf, "vlan"),
appl_db_sub_intf_status_get(self.db, self.config_db, self.front_panel_ports_list, self.portchannel_speed_dict, sub_intf, PORT_ADMIN_STATUS),
appl_db_sub_intf_status_get(self.db, self.config_db, self.front_panel_ports_list, self.portchannel_speed_dict, sub_intf, PORT_OPTICS_TYPE)))
return table
@multi_asic_util.run_on_multi_asic
def get_intf_status(self):
self.front_panel_ports_list = get_frontpanel_port_list(self.config_db)
self.appl_db_keys = appl_db_keys_get(self.db, self.front_panel_ports_list, None)
self.int_to_vlan_dict = get_interface_vlan_dict(self.config_db)
self.get_raw_po_int_configdb_info = get_raw_portchannel_info(self.config_db)
self.portchannel_list = get_portchannel_list(self.get_raw_po_int_configdb_info)
self.po_int_tuple_list = create_po_int_tuple_list(self.get_raw_po_int_configdb_info)
self.po_int_dict = create_po_int_dict(self.po_int_tuple_list)
self.int_po_dict = create_int_to_portchannel_dict(self.po_int_tuple_list)
self.combined_int_to_vlan_po_dict = merge_dicts(self.int_to_vlan_dict, self.int_po_dict)
self.portchannel_speed_dict = po_speed_dict(self.po_int_dict, self.db)
self.portchannel_keys = self.portchannel_speed_dict.keys()
self.sub_intf_list = get_sub_port_intf_list(self.config_db)
self.appl_db_sub_intf_keys = appl_db_sub_intf_keys_get(self.db, self.sub_intf_list, self.sub_intf_name)
if self.appl_db_keys:
self.table += self.generate_intf_status()
# ========================== interface-description logic ==========================
header_desc = ['Interface', 'Oper', 'Admin', 'Alias', 'Description']
class IntfDescription(object):
def __init__(self, intf_name, namespace_option, display_option):
self.db = None
self.config_db = None
self.table = []
self.multi_asic = multi_asic_util.MultiAsic(
display_option, namespace_option)
if intf_name is not None and intf_name == SUB_PORT:
self.intf_name = None
else:
self.intf_name = intf_name
def display_intf_description(self):
self.get_intf_description()
# Sorting and tabulating the result table.
sorted_table = natsorted(self.table)
print tabulate(sorted_table, header_desc, tablefmt="simple", stralign='right')
def generate_intf_description(self):
"""
Generate interface-description output
"""
i = {}
table = []
key = []
#
# Iterate through all the keys and append port's associated state to
# the result table.
#
for i in self.appl_db_keys:
key = re.split(':', i, maxsplit=1)[-1].strip()
if key in self.front_panel_ports_list:
if self.multi_asic.skip_display(constants.PORT_OBJ, key):
continue
table.append((key,
appl_db_port_status_get(self.db, key, PORT_OPER_STATUS),
appl_db_port_status_get(self.db, key, PORT_ADMIN_STATUS),
appl_db_port_status_get(self.db, key, PORT_ALIAS),
appl_db_port_status_get(self.db, key, PORT_DESCRIPTION)))
return table
@multi_asic_util.run_on_multi_asic
def get_intf_description(self):
self.front_panel_ports_list = get_frontpanel_port_list(self.config_db)
self.appl_db_keys = appl_db_keys_get(self.db, self.front_panel_ports_list, self.intf_name)
if self.appl_db_keys:
self.table += self.generate_intf_description()
def main():
parser = argparse.ArgumentParser(description='Display Interface information',
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-c', '--command', type=str, help='get interface status or description', default=None)
parser.add_argument('-i', '--interface', type=str, help='interface information for specific port: Ethernet0', default=None)
parser = multi_asic_util.multi_asic_args(parser)
args = parser.parse_args()
if args.command == "status":
interface_stat = IntfStatus(args.interface, args.namespace, args.display)
interface_stat.display_intf_status()
elif args.command == "description":
interface_desc = IntfDescription(args.interface, args.namespace, args.display)
interface_desc.display_intf_description()
sys.exit(0)
if __name__ == "__main__":
main()