13
13
import argparse
14
14
import os
15
15
import sys
16
+ from utilities_common import constants
17
+ from sonic_py_common import multi_asic
18
+ from utilities_common import multi_asic as multi_asic_util
16
19
17
20
from tabulate import tabulate
18
21
19
22
# mock the redis for unit test purposes #
20
23
try :
21
- if os .environ [ "UTILITIES_UNIT_TESTING" ] == "1" :
24
+ if os .getenv ( "UTILITIES_UNIT_TESTING" ) == "1" :
22
25
modules_path = os .path .join (os .path .dirname (__file__ ), ".." )
23
26
test_path = os .path .join (modules_path , "tests" )
24
27
sys .path .insert (0 , modules_path )
25
28
sys .path .insert (0 , test_path )
26
29
import mock_tables .dbconnector
30
+ if os .getenv ("UTILITIES_UNIT_TESTING_TOPOLOGY" ) == "multi_asic" :
31
+ import tests .mock_tables .mock_multi_asic
32
+ mock_tables .dbconnector .load_namespace_config ()
33
+
27
34
except KeyError :
28
35
pass
29
36
@@ -51,14 +58,11 @@ class InvalidArgumentError(RuntimeError):
51
58
def __init__ (self , msg ):
52
59
self .message = msg
53
60
54
-
55
61
class DropConfig (object ):
56
- def __init__ (self ):
57
- self .config_db = ConfigDBConnector ()
58
- self .config_db .connect ()
59
-
60
- self .state_db = SonicV2Connector (use_unix_socket_path = False )
61
- self .state_db .connect (self .state_db .STATE_DB )
62
+ def __init__ (self , namespace , db , config_db ):
63
+ self .db = db
64
+ self .config_db = config_db
65
+ self .namespace = namespace
62
66
63
67
# -c show_config
64
68
def print_counter_config (self , group ):
@@ -76,6 +80,9 @@ class DropConfig(object):
76
80
counter .get ('reason' , '' ),
77
81
counter .get ('description' , '' )))
78
82
83
+ if multi_asic .is_multi_asic ():
84
+ print ("For namespace:" , self .namespace )
85
+
79
86
print (tabulate (table ,
80
87
drop_counter_config_header ,
81
88
tablefmt = 'simple' ,
@@ -95,6 +102,10 @@ class DropConfig(object):
95
102
table = []
96
103
for counter , capabilities in device_caps .items ():
97
104
table .append ((counter , capabilities .get ('count' , 'N/A' )))
105
+
106
+ if multi_asic .is_multi_asic ():
107
+ print ("For namespace:" , self .namespace )
108
+
98
109
print (tabulate (table ,
99
110
drop_counter_capability_header ,
100
111
tablefmt = 'simple' ,
@@ -266,15 +277,15 @@ class DropConfig(object):
266
277
Get the device capabilities from STATE_DB
267
278
"""
268
279
269
- capability_query = self .state_db .keys (self .state_db .STATE_DB , '{}|*' .format (DEBUG_COUNTER_CAPABILITY_TABLE ))
280
+ capability_query = self .db .keys (self .db .STATE_DB , '{}|*' .format (DEBUG_COUNTER_CAPABILITY_TABLE ))
270
281
271
282
if not capability_query :
272
283
return None
273
284
274
285
counter_caps = {}
275
286
for counter_type in capability_query :
276
287
# Because keys returns the whole key, we trim off the DEBUG_COUNTER_CAPABILITY prefix here
277
- counter_caps [counter_type [len (DEBUG_COUNTER_CAPABILITY_TABLE ) + 1 :]] = self .state_db .get_all (self .state_db .STATE_DB , counter_type )
288
+ counter_caps [counter_type [len (DEBUG_COUNTER_CAPABILITY_TABLE ) + 1 :]] = self .db .get_all (self .db .STATE_DB , counter_type )
278
289
return counter_caps
279
290
280
291
def counter_name_in_use (self , counter_name ):
@@ -287,7 +298,7 @@ class DropConfig(object):
287
298
if counter_type is None :
288
299
return None
289
300
290
- cap_query = self .state_db .get_all (self .state_db .STATE_DB , '{}|{}' .format (DEBUG_COUNTER_CAPABILITY_TABLE , counter_type ))
301
+ cap_query = self .db .get_all (self .db .STATE_DB , '{}|{}' .format (DEBUG_COUNTER_CAPABILITY_TABLE , counter_type ))
291
302
292
303
if not cap_query :
293
304
return None
@@ -298,7 +309,7 @@ class DropConfig(object):
298
309
if counter_type is None :
299
310
return None
300
311
301
- cap_query = self .state_db .get_all (self .state_db .STATE_DB , '{}|{}' .format (DEBUG_COUNTER_CAPABILITY_TABLE , counter_type ))
312
+ cap_query = self .db .get_all (self .db .STATE_DB , '{}|{}' .format (DEBUG_COUNTER_CAPABILITY_TABLE , counter_type ))
302
313
303
314
if not cap_query :
304
315
return None
@@ -310,6 +321,67 @@ class DropConfig(object):
310
321
# get_keys will normalize the table name to uppercase.
311
322
return [key for key in self .config_db .get_keys (DROP_REASON_CONFIG_TABLE ) if key [0 ] == counter_name ]
312
323
324
+ class DropConfigWrapper (object ):
325
+ """A wrapper to execute dropconfig cmd over the correct namespaces"""
326
+ def __init__ (self , namespace ):
327
+ self .namespace = namespace
328
+ if namespace is not None and namespace not in multi_asic .get_namespace_list ():
329
+ print ('Encountered error, namespace not recognized: {}. Valid namespaces {}' .format (namespace ,
330
+ multi_asic .get_namespace_list ()))
331
+ sys .exit (1 )
332
+
333
+ # Initialize the multi-asic namespace
334
+ self .multi_asic = multi_asic_util .MultiAsic (constants .DISPLAY_ALL , namespace_option = namespace )
335
+ self .db = None
336
+ self .config_db = None
337
+
338
+ @multi_asic_util .run_on_multi_asic
339
+ def run (self ,
340
+ command ,
341
+ name ,
342
+ alias ,
343
+ group ,
344
+ counter_type ,
345
+ description ,
346
+ reasons ):
347
+
348
+ dconfig = DropConfig (self .multi_asic .current_namespace , self .db , self .config_db )
349
+
350
+ if command == 'install' :
351
+ try :
352
+ dconfig .create_counter (name ,
353
+ alias ,
354
+ group ,
355
+ counter_type ,
356
+ description ,
357
+ reasons )
358
+ except InvalidArgumentError as err :
359
+ print ('Encountered error trying to install counter: {}' .format (err .message ))
360
+ sys .exit (1 )
361
+ elif command == 'uninstall' :
362
+ try :
363
+ dconfig .delete_counter (name )
364
+ except InvalidArgumentError as err :
365
+ print ('Encountered error trying to uninstall counter: {}' .format (err .message ))
366
+ sys .exit (1 )
367
+ elif command == 'add' :
368
+ try :
369
+ dconfig .add_reasons (name , reasons )
370
+ except InvalidArgumentError as err :
371
+ print ('Encountered error trying to add reasons: {}' .format (err .message ))
372
+ sys .exit (1 )
373
+ elif command == 'remove' :
374
+ try :
375
+ dconfig .remove_reasons (name , reasons )
376
+ except InvalidArgumentError as err :
377
+ print ('Encountered error trying to remove reasons: {}' .format (err .message ))
378
+ sys .exit (1 )
379
+ elif command == 'show_config' :
380
+ dconfig .print_counter_config (group )
381
+ elif command == 'show_capabilities' :
382
+ dconfig .print_device_capabilities ()
383
+ else :
384
+ print ("Command not recognized" )
313
385
314
386
def deserialize_reason_list (list_str ):
315
387
if list_str is None :
@@ -334,6 +406,7 @@ def main():
334
406
epilog = """
335
407
Examples:
336
408
dropconfig
409
+ dropconfig -ns asic0
337
410
""" )
338
411
339
412
# Version
@@ -349,6 +422,7 @@ Examples:
349
422
parser .add_argument ('-t' , '--type' , type = str , help = 'The type of the target drop counter' , default = None )
350
423
parser .add_argument ('-d' , '--desc' , type = str , help = 'The description for the target drop counter' , default = None )
351
424
parser .add_argument ('-r' , '--reasons' , type = str , help = 'The list of drop reasons for the target drop counter' , default = None )
425
+ parser .add_argument ('-ns' , '--namespace' , type = str , help = 'Perform operation on a specific namespace or skip for all' , default = None )
352
426
353
427
args = parser .parse_args ()
354
428
@@ -360,46 +434,19 @@ Examples:
360
434
counter_type = args .type
361
435
description = args .desc
362
436
drop_reasons = args .reasons
437
+ namespace = args .namespace
363
438
364
439
reasons = deserialize_reason_list (drop_reasons )
365
440
366
- dconfig = DropConfig ()
367
-
368
- if command == 'install' :
369
- try :
370
- dconfig .create_counter (name ,
371
- alias ,
372
- group ,
373
- counter_type ,
374
- description ,
375
- reasons )
376
- except InvalidArgumentError as err :
377
- print ('Encountered error trying to install counter: {}' .format (err .message ))
378
- sys .exit (1 )
379
- elif command == 'uninstall' :
380
- try :
381
- dconfig .delete_counter (name )
382
- except InvalidArgumentError as err :
383
- print ('Encountered error trying to uninstall counter: {}' .format (err .message ))
384
- sys .exit (1 )
385
- elif command == 'add' :
386
- try :
387
- dconfig .add_reasons (name , reasons )
388
- except InvalidArgumentError as err :
389
- print ('Encountered error trying to add reasons: {}' .format (err .message ))
390
- sys .exit (1 )
391
- elif command == 'remove' :
392
- try :
393
- dconfig .remove_reasons (name , reasons )
394
- except InvalidArgumentError as err :
395
- print ('Encountered error trying to remove reasons: {}' .format (err .message ))
396
- sys .exit (1 )
397
- elif command == 'show_config' :
398
- dconfig .print_counter_config (group )
399
- elif command == 'show_capabilities' :
400
- dconfig .print_device_capabilities ()
401
- else :
402
- print ("Command not recognized" )
441
+ dropconfig_wrapper = DropConfigWrapper (namespace )
442
+ dropconfig_wrapper .run (command ,
443
+ name ,
444
+ alias ,
445
+ group ,
446
+ counter_type ,
447
+ description ,
448
+ reasons )
449
+
403
450
404
451
if __name__ == '__main__' :
405
452
main ()
0 commit comments