@@ -8,7 +8,18 @@ from tabulate import tabulate
8
8
from natsort import natsorted
9
9
from swsssdk import ConfigDBConnector
10
10
from pprint import pprint
11
-
11
+ import os
12
+
13
+ # mock the redis for unit test purposes #
14
+ try :
15
+ if os .environ ["UTILITIES_UNIT_TESTING" ] == "1" :
16
+ modules_path = os .path .join (os .path .dirname (__file__ ), ".." )
17
+ tests_path = os .path .join (modules_path , "sonic-utilities-tests" )
18
+ sys .path .insert (0 , modules_path )
19
+ sys .path .insert (0 , tests_path )
20
+ import mock_tables .dbconnector
21
+ except KeyError :
22
+ pass
12
23
13
24
# ========================== Common interface-utils logic ==========================
14
25
@@ -25,6 +36,11 @@ PORT_DESCRIPTION = "description"
25
36
PORT_OPTICS_TYPE = "type"
26
37
PORT_PFC_ASYM_STATUS = "pfc_asym"
27
38
39
+ VLAN_SUB_INTERFACE_SEPARATOR = "."
40
+ VLAN_SUB_INTERFACE_TYPE = "802.1q-encapsulation"
41
+
42
+ SUB_PORT = "subport"
43
+
28
44
def db_connect_configdb ():
29
45
"""
30
46
Connect to configdb
@@ -43,6 +59,15 @@ def get_frontpanel_port_list(config_db):
43
59
return front_panel_ports_list
44
60
45
61
62
+ def get_sub_port_intf_list (config_db ):
63
+ sub_intf_dict = config_db .get_table ('VLAN_SUB_INTERFACE' )
64
+ sub_intf_list = []
65
+ for sub_intf in sub_intf_dict .keys ():
66
+ if isinstance (sub_intf , basestring ):
67
+ sub_intf_list .append (sub_intf )
68
+ return sub_intf_list
69
+
70
+
46
71
def get_interface_vlan_dict (config_db ):
47
72
"""
48
73
Get info from REDIS ConfigDB and create interface to vlan mapping
@@ -93,6 +118,24 @@ def appl_db_keys_get(appl_db, front_panel_ports_list, intf_name):
93
118
return appl_db_keys
94
119
95
120
121
+ def appl_db_sub_intf_keys_get (appl_db , sub_intf_list , sub_intf_name ):
122
+ """
123
+ Get APPL_DB sub port interface keys
124
+ """
125
+ if sub_intf_name is None :
126
+ appl_db_sub_intf_keys = []
127
+ appl_db_intf_keys = appl_db .keys (appl_db .APPL_DB , "INTF_TABLE:*" )
128
+ if appl_db_intf_keys is not None :
129
+ for appl_db_intf_key in appl_db_intf_keys :
130
+ if re .split (':' , appl_db_intf_key , maxsplit = 1 )[- 1 ].strip () in sub_intf_list :
131
+ appl_db_sub_intf_keys .append (appl_db_intf_key )
132
+ elif sub_intf_name in sub_intf_list :
133
+ appl_db_sub_intf_keys = appl_db .keys (appl_db .APPL_DB , "INTF_TABLE:%s" % sub_intf_name )
134
+ else :
135
+ return []
136
+ return appl_db_sub_intf_keys
137
+
138
+
96
139
def appl_db_port_status_get (appl_db , intf_name , status_type ):
97
140
"""
98
141
Get the port status
@@ -267,13 +310,42 @@ def appl_db_portchannel_status_get(appl_db, config_db, po_name, status_type, por
267
310
return "N/A"
268
311
return status
269
312
313
+ def appl_db_sub_intf_status_get (appl_db , config_db , front_panel_ports_list , portchannel_speed_dict , sub_intf_name , status_type ):
314
+ sub_intf_sep_idx = sub_intf_name .find (VLAN_SUB_INTERFACE_SEPARATOR )
315
+ if sub_intf_sep_idx != - 1 :
316
+ parent_port_name = sub_intf_name [:sub_intf_sep_idx ]
317
+ vlan_id = sub_intf_name [sub_intf_sep_idx + 1 :]
318
+
319
+ full_intf_table_name = "INTF_TABLE" + ":" + sub_intf_name
320
+
321
+ if status_type == "vlan" :
322
+ return vlan_id
323
+
324
+ if status_type == "admin_status" :
325
+ status = appl_db .get (appl_db .APPL_DB , full_intf_table_name , status_type )
326
+ return status if status is not None else "N/A"
327
+
328
+ if status_type == "type" :
329
+ return VLAN_SUB_INTERFACE_TYPE
330
+
331
+ if status_type == "mtu" or status_type == "speed" :
332
+ if parent_port_name in front_panel_ports_list :
333
+ return appl_db_port_status_get (appl_db , parent_port_name , status_type )
334
+ elif parent_port_name in portchannel_speed_dict .keys ():
335
+ return appl_db_portchannel_status_get (appl_db , config_db , parent_port_name , status_type , portchannel_speed_dict )
336
+ else :
337
+ return "N/A"
338
+
339
+ return "N/A"
340
+
270
341
# ========================== interface-status logic ==========================
271
342
272
343
header_stat = ['Interface' , 'Lanes' , 'Speed' , 'MTU' , 'Alias' , 'Vlan' , 'Oper' , 'Admin' , 'Type' , 'Asym PFC' ]
344
+ header_stat_sub_intf = ['Sub port interface' , 'Speed' , 'MTU' , 'Vlan' , 'Admin' , 'Type' ]
273
345
274
346
class IntfStatus (object ):
275
347
276
- def display_intf_status (self , appl_db_keys , front_panel_ports_list , portchannel_speed_dict ):
348
+ def display_intf_status (self , appl_db_keys , front_panel_ports_list , portchannel_speed_dict , appl_db_sub_intf_keys , sub_intf_list , sub_intf_only ):
277
349
"""
278
350
Generate interface-status output
279
351
"""
@@ -286,35 +358,47 @@ class IntfStatus(object):
286
358
# Iterate through all the keys and append port's associated state to
287
359
# the result table.
288
360
#
289
- for i in appl_db_keys :
290
- key = re .split (':' , i , maxsplit = 1 )[- 1 ].strip ()
291
- if key in front_panel_ports_list :
292
- table .append ((key ,
293
- appl_db_port_status_get (self .appl_db , key , PORT_LANES_STATUS ),
294
- appl_db_port_status_get (self .appl_db , key , PORT_SPEED ),
295
- appl_db_port_status_get (self .appl_db , key , PORT_MTU_STATUS ),
296
- appl_db_port_status_get (self .appl_db , key , PORT_ALIAS ),
297
- config_db_vlan_port_keys_get (self .combined_int_to_vlan_po_dict , self .front_panel_ports_list , key ),
298
- appl_db_port_status_get (self .appl_db , key , PORT_OPER_STATUS ),
299
- appl_db_port_status_get (self .appl_db , key , PORT_ADMIN_STATUS ),
300
- state_db_port_optics_get (self .state_db , key , PORT_OPTICS_TYPE ),
301
- appl_db_port_status_get (self .appl_db , key , PORT_PFC_ASYM_STATUS )))
302
- # Sorting and tabulating the result table.
303
- for po , value in portchannel_speed_dict .iteritems ():
304
- if po :
305
- table .append ((po ,
306
- appl_db_portchannel_status_get (self .appl_db , self .config_db , po , PORT_LANES_STATUS , self .portchannel_speed_dict ),
307
- appl_db_portchannel_status_get (self .appl_db , self .config_db , po , PORT_SPEED , self .portchannel_speed_dict ),
308
- appl_db_portchannel_status_get (self .appl_db , self .config_db , po , PORT_MTU_STATUS , self .portchannel_speed_dict ),
309
- appl_db_portchannel_status_get (self .appl_db , self .config_db , po , PORT_ALIAS , self .portchannel_speed_dict ),
310
- appl_db_portchannel_status_get (self .appl_db , self .config_db , po , "vlan" , self .portchannel_speed_dict ),
311
- appl_db_portchannel_status_get (self .appl_db , self .config_db , po , PORT_OPER_STATUS , self .portchannel_speed_dict ),
312
- appl_db_portchannel_status_get (self .appl_db , self .config_db , po , PORT_ADMIN_STATUS , self .portchannel_speed_dict ),
313
- appl_db_portchannel_status_get (self .appl_db , self .config_db , po , PORT_OPTICS_TYPE , self .portchannel_speed_dict ),
314
- appl_db_portchannel_status_get (self .appl_db , self .config_db , po , PORT_PFC_ASYM_STATUS , self .portchannel_speed_dict )))
361
+ if not sub_intf_only :
362
+ for i in appl_db_keys :
363
+ key = re .split (':' , i , maxsplit = 1 )[- 1 ].strip ()
364
+ if key in front_panel_ports_list :
365
+ table .append ((key ,
366
+ appl_db_port_status_get (self .appl_db , key , PORT_LANES_STATUS ),
367
+ appl_db_port_status_get (self .appl_db , key , PORT_SPEED ),
368
+ appl_db_port_status_get (self .appl_db , key , PORT_MTU_STATUS ),
369
+ appl_db_port_status_get (self .appl_db , key , PORT_ALIAS ),
370
+ config_db_vlan_port_keys_get (self .combined_int_to_vlan_po_dict , self .front_panel_ports_list , key ),
371
+ appl_db_port_status_get (self .appl_db , key , PORT_OPER_STATUS ),
372
+ appl_db_port_status_get (self .appl_db , key , PORT_ADMIN_STATUS ),
373
+ state_db_port_optics_get (self .state_db , key , PORT_OPTICS_TYPE ),
374
+ appl_db_port_status_get (self .appl_db , key , PORT_PFC_ASYM_STATUS )))
375
+
376
+ for po , value in portchannel_speed_dict .iteritems ():
377
+ if po :
378
+ table .append ((po ,
379
+ appl_db_portchannel_status_get (self .appl_db , self .config_db , po , PORT_LANES_STATUS , self .portchannel_speed_dict ),
380
+ appl_db_portchannel_status_get (self .appl_db , self .config_db , po , PORT_SPEED , self .portchannel_speed_dict ),
381
+ appl_db_portchannel_status_get (self .appl_db , self .config_db , po , PORT_MTU_STATUS , self .portchannel_speed_dict ),
382
+ appl_db_portchannel_status_get (self .appl_db , self .config_db , po , PORT_ALIAS , self .portchannel_speed_dict ),
383
+ appl_db_portchannel_status_get (self .appl_db , self .config_db , po , "vlan" , self .portchannel_speed_dict ),
384
+ appl_db_portchannel_status_get (self .appl_db , self .config_db , po , PORT_OPER_STATUS , self .portchannel_speed_dict ),
385
+ appl_db_portchannel_status_get (self .appl_db , self .config_db , po , PORT_ADMIN_STATUS , self .portchannel_speed_dict ),
386
+ appl_db_portchannel_status_get (self .appl_db , self .config_db , po , PORT_OPTICS_TYPE , self .portchannel_speed_dict ),
387
+ appl_db_portchannel_status_get (self .appl_db , self .config_db , po , PORT_PFC_ASYM_STATUS , self .portchannel_speed_dict )))
388
+ else :
389
+ for key in appl_db_sub_intf_keys :
390
+ sub_intf = re .split (':' , key , maxsplit = 1 )[- 1 ].strip ()
391
+ if sub_intf in sub_intf_list :
392
+ table .append ((sub_intf ,
393
+ appl_db_sub_intf_status_get (self .appl_db , self .config_db , self .front_panel_ports_list , self .portchannel_speed_dict , sub_intf , PORT_SPEED ),
394
+ appl_db_sub_intf_status_get (self .appl_db , self .config_db , self .front_panel_ports_list , self .portchannel_speed_dict , sub_intf , PORT_MTU_STATUS ),
395
+ appl_db_sub_intf_status_get (self .appl_db , self .config_db , self .front_panel_ports_list , self .portchannel_speed_dict , sub_intf , "vlan" ),
396
+ appl_db_sub_intf_status_get (self .appl_db , self .config_db , self .front_panel_ports_list , self .portchannel_speed_dict , sub_intf , PORT_ADMIN_STATUS ),
397
+ appl_db_sub_intf_status_get (self .appl_db , self .config_db , self .front_panel_ports_list , self .portchannel_speed_dict , sub_intf , PORT_OPTICS_TYPE )))
315
398
399
+ # Sorting and tabulating the result table.
316
400
sorted_table = natsorted (table )
317
- print tabulate (sorted_table , header_stat , tablefmt = "simple" , stralign = 'right' )
401
+ print tabulate (sorted_table , header_stat if not sub_intf_only else header_stat_sub_intf , tablefmt = "simple" , stralign = 'right' )
318
402
319
403
320
404
def __init__ (self , intf_name ):
@@ -333,6 +417,20 @@ class IntfStatus(object):
333
417
return
334
418
if self .config_db is None :
335
419
return
420
+
421
+ sub_intf_only = False
422
+ sub_intf_name = intf_name
423
+ if intf_name is not None :
424
+ if intf_name == SUB_PORT :
425
+ intf_name = None
426
+ sub_intf_name = None
427
+ sub_intf_only = True
428
+ else :
429
+ sub_intf_sep_idx = intf_name .find (VLAN_SUB_INTERFACE_SEPARATOR )
430
+ if sub_intf_sep_idx != - 1 :
431
+ sub_intf_only = True
432
+ intf_name = intf_name [:sub_intf_sep_idx ]
433
+
336
434
self .front_panel_ports_list = get_frontpanel_port_list (self .config_db )
337
435
appl_db_keys = appl_db_keys_get (self .appl_db , self .front_panel_ports_list , intf_name )
338
436
self .int_to_vlan_dict = get_interface_vlan_dict (self .config_db )
@@ -344,9 +442,12 @@ class IntfStatus(object):
344
442
self .combined_int_to_vlan_po_dict = merge_dicts (self .int_to_vlan_dict , self .int_po_dict )
345
443
self .portchannel_speed_dict = po_speed_dict (self .po_int_dict , self .appl_db )
346
444
self .portchannel_keys = self .portchannel_speed_dict .keys ()
445
+
446
+ self .sub_intf_list = get_sub_port_intf_list (self .config_db )
447
+ appl_db_sub_intf_keys = appl_db_sub_intf_keys_get (self .appl_db , self .sub_intf_list , sub_intf_name )
347
448
if appl_db_keys is None :
348
449
return
349
- self .display_intf_status (appl_db_keys , self .front_panel_ports_list , self .portchannel_speed_dict )
450
+ self .display_intf_status (appl_db_keys , self .front_panel_ports_list , self .portchannel_speed_dict , appl_db_sub_intf_keys , self . sub_intf_list , sub_intf_only )
350
451
351
452
352
453
@@ -392,6 +493,10 @@ class IntfDescription(object):
392
493
return
393
494
if self .config_db is None :
394
495
return
496
+
497
+ if intf_name is not None and intf_name == SUB_PORT :
498
+ intf_name = None
499
+
395
500
self .front_panel_ports_list = get_frontpanel_port_list (self .config_db )
396
501
appl_db_keys = appl_db_keys_get (self .appl_db , self .front_panel_ports_list , intf_name )
397
502
if appl_db_keys is None :
0 commit comments