@@ -10,9 +10,12 @@ import operator
10
10
import os
11
11
12
12
from natsort import natsorted
13
- from swsssdk import SonicV2Connector
14
13
from tabulate import tabulate
15
14
15
+ from utilities_common import multi_asic as multi_asic_util
16
+ from sonic_py_common .interface import front_panel_prefix , backplane_prefix
17
+ from sonic_py_common import multi_asic
18
+
16
19
# Mock the redis for unit test purposes #
17
20
try :
18
21
if os .environ ["UTILITIES_UNIT_TESTING" ] == "2" :
21
24
sys .path .insert (0 , modules_path )
22
25
sys .path .insert (0 , test_path )
23
26
import mock_tables .dbconnector
27
+ if os .environ ["UTILITIES_UNIT_TESTING_TOPOLOGY" ] == "multi_asic" :
28
+ import mock_tables .mock_multi_asic
29
+ mock_tables .dbconnector .load_namespace_config ()
24
30
except KeyError :
25
31
pass
26
32
@@ -126,18 +132,27 @@ dom_value_unit_map = {'rx1power': 'dBm', 'rx2power': 'dBm',
126
132
'tx3power' : 'dBm' , 'tx4power' : 'dBm' ,
127
133
'temperature' : 'C' , 'voltage' : 'Volts' }
128
134
135
+ def display_invalid_intf_eeprom (intf_name ):
136
+ output = intf_name + ': ' + 'SFP EEPROM Not detected' + '\n '
137
+ click .echo (output )
129
138
139
+ def display_invalid_intf_presence (intf_name ):
140
+ header = ['Port' , 'Presence' ]
141
+ port_table = []
142
+ port_table .append ((intf_name , 'Not present' ))
143
+ click .echo (tabulate (port_table , header ))
130
144
131
145
class SFPShow (object ):
132
146
133
- def __init__ (self ):
147
+ def __init__ (self , intf_name , namespace_option , dump_dom = False ):
134
148
super (SFPShow ,self ).__init__ ()
135
- self .adb = SonicV2Connector (host = "127.0.0.1" )
136
- self .adb .connect (self .adb .APPL_DB )
137
-
138
- self .sdb = SonicV2Connector (host = "127.0.0.1" )
139
- self .sdb .connect (self .sdb .STATE_DB )
140
- return
149
+ self .db = None
150
+ self .config_db = None
151
+ self .intf_name = intf_name
152
+ self .dump_dom = dump_dom
153
+ self .table = []
154
+ self .output = ''
155
+ self .multi_asic = multi_asic_util .MultiAsic (namespace_option = namespace_option )
141
156
142
157
# Convert dict values to cli output string
143
158
def format_dict_value_to_string (self , sorted_key_table ,
@@ -291,53 +306,61 @@ class SFPShow(object):
291
306
292
307
return out_put
293
308
294
- def display_eeprom (self , interfacename , dump_dom ):
309
+ @multi_asic_util .run_on_multi_asic
310
+ def get_eeprom (self ):
295
311
out_put = ''
296
312
297
- if interfacename is not None :
298
- presence = self .sdb .exists (self .sdb .STATE_DB , 'TRANSCEIVER_INFO|{}' .format (interfacename ))
313
+ if self . intf_name is not None :
314
+ presence = self .db .exists (self .db .STATE_DB , 'TRANSCEIVER_INFO|{}' .format (self . intf_name ))
299
315
if presence :
300
- out_put = self .convert_interface_sfp_info_to_cli_output_string (self .sdb , interfacename , dump_dom )
316
+ out_put = self .convert_interface_sfp_info_to_cli_output_string (self .db , self . intf_name , self . dump_dom )
301
317
else :
302
- out_put = out_put + interfacename + ': ' + 'SFP EEPROM Not detected' + '\n '
318
+ out_put = out_put + self . intf_name + ': ' + 'SFP EEPROM Not detected' + '\n '
303
319
else :
304
- port_table_keys = self .adb .keys (self .adb .APPL_DB , "PORT_TABLE:*" )
320
+ port_table_keys = self .db .keys (self .db .APPL_DB , "PORT_TABLE:*" )
305
321
sorted_table_keys = natsorted (port_table_keys )
306
322
for i in sorted_table_keys :
307
323
interface = re .split (':' , i , maxsplit = 1 )[- 1 ].strip ()
308
- if interface and interface .startswith ('Ethernet' ):
309
- presence = self .sdb .exists (self .sdb .STATE_DB , 'TRANSCEIVER_INFO|{}' .format (interface ))
324
+ if interface and interface .startswith (front_panel_prefix ()) and not interface . startswith ( backplane_prefix () ):
325
+ presence = self .db .exists (self .db .STATE_DB , 'TRANSCEIVER_INFO|{}' .format (interface ))
310
326
if presence :
311
- out_put = out_put + self .convert_interface_sfp_info_to_cli_output_string (self .sdb , interface , dump_dom )
327
+ out_put = out_put + self .convert_interface_sfp_info_to_cli_output_string (self .db , interface , self . dump_dom )
312
328
else :
313
329
out_put = out_put + interface + ': ' + 'SFP EEPROM Not detected' + '\n '
314
330
315
- out_put = out_put + '\n '
331
+ out_put = out_put + '\n '
316
332
317
- click . echo ( out_put )
333
+ self . output += out_put
318
334
319
- def display_presence (self , interfacename ):
335
+ @multi_asic_util .run_on_multi_asic
336
+ def get_presence (self ):
320
337
port_table = []
321
- header = ['Port' , 'Presence' ]
322
338
323
- if interfacename is not None :
324
- presence = self .sdb .exists (self .sdb .STATE_DB , 'TRANSCEIVER_INFO|{}' .format (interfacename ))
339
+ if self . intf_name is not None :
340
+ presence = self .db .exists (self .db .STATE_DB , 'TRANSCEIVER_INFO|{}' .format (self . intf_name ))
325
341
if presence :
326
- port_table .append ((interfacename , 'Present' ))
342
+ port_table .append ((self . intf_name , 'Present' ))
327
343
else :
328
- port_table .append ((interfacename , 'Not present' ))
344
+ port_table .append ((self . intf_name , 'Not present' ))
329
345
else :
330
- port_table_keys = self .adb .keys (self .adb .APPL_DB , "PORT_TABLE:*" )
346
+ port_table_keys = self .db .keys (self .db .APPL_DB , "PORT_TABLE:*" )
331
347
for i in port_table_keys :
332
348
key = re .split (':' , i , maxsplit = 1 )[- 1 ].strip ()
333
- if key and key .startswith ('Ethernet' ):
334
- presence = self .sdb .exists (self .sdb .STATE_DB , 'TRANSCEIVER_INFO|{}' .format (key ))
349
+ if key and key .startswith (front_panel_prefix ()) and not key . startswith ( backplane_prefix () ):
350
+ presence = self .db .exists (self .db .STATE_DB , 'TRANSCEIVER_INFO|{}' .format (key ))
335
351
if presence :
336
352
port_table .append ((key ,'Present' ))
337
353
else :
338
354
port_table .append ((key ,'Not present' ))
339
355
340
- sorted_port_table = natsorted (port_table )
356
+ self .table += port_table
357
+
358
+ def display_eeprom (self ):
359
+ click .echo (self .output )
360
+
361
+ def display_presence (self ):
362
+ header = ['Port' , 'Presence' ]
363
+ sorted_port_table = natsorted (self .table )
341
364
click .echo (tabulate (sorted_port_table , header ))
342
365
343
366
# This is our main entrypoint - the main 'sfpshow' command
@@ -350,16 +373,36 @@ def cli():
350
373
@cli .command ()
351
374
@click .option ('-p' , '--port' , metavar = '<port_name>' , help = "Display SFP EEPROM data for port <port_name> only" )
352
375
@click .option ('-d' , '--dom' , 'dump_dom' , is_flag = True , help = "Also display Digital Optical Monitoring (DOM) data" )
353
- def eeprom (port , dump_dom ):
354
- sfp = SFPShow ()
355
- sfp .display_eeprom (port , dump_dom )
376
+ @click .option ('-n' , '--namespace' , default = None , help = "Display interfaces for specific namespace" )
377
+ def eeprom (port , dump_dom , namespace ):
378
+ if port and multi_asic .is_multi_asic () and namespace is None :
379
+ try :
380
+ ns = multi_asic .get_namespace_for_port (port )
381
+ namespace = ns
382
+ except Exception :
383
+ display_invalid_intf_eeprom (port )
384
+ sys .exit (1 )
385
+
386
+ sfp = SFPShow (port , namespace , dump_dom )
387
+ sfp .get_eeprom ()
388
+ sfp .display_eeprom ()
356
389
357
390
# 'presence' subcommand
358
391
@cli .command ()
359
392
@click .option ('-p' , '--port' , metavar = '<port_name>' , help = "Display SFP presence for port <port_name> only" )
360
- def presence (port ):
361
- sfp = SFPShow ()
362
- sfp .display_presence (port )
393
+ @click .option ('-n' , '--namespace' , default = None , help = "Display interfaces for specific namespace" )
394
+ def presence (port , namespace ):
395
+ if port and multi_asic .is_multi_asic () and namespace is None :
396
+ try :
397
+ ns = multi_asic .get_namespace_for_port (port )
398
+ namespace = ns
399
+ except Exception :
400
+ display_invalid_intf_presence (port )
401
+ sys .exit (1 )
402
+
403
+ sfp = SFPShow (port , namespace )
404
+ sfp .get_presence ()
405
+ sfp .display_presence ()
363
406
364
407
if __name__ == "__main__" :
365
408
cli ()
0 commit comments