@@ -492,16 +492,18 @@ def get_copp_trap_capabilities(duthost):
492492 return trap_ids .split ("," )
493493
494494
495- def parse_show_copp_configuration (duthost ):
495+ def parse_show_copp_configuration (duthost , namespace ):
496496 """
497497 Parses the output of the `show copp configuration` command into a structured dictionary.
498498 Args:
499499 duthost (SonicHost): The target device.
500500 Returns:
501501 dict: A dictionary mapping trap IDs to their configuration details.
502502 """
503-
504- copp_config_output = duthost .shell ("show copp configuration" )["stdout" ]
503+ command = "show copp configuration"
504+ if namespace is not None :
505+ command = namespace .ns_arg + ' ' + command
506+ copp_config_output = duthost .shell (command )["stdout" ]
505507 copp_config_lines = copp_config_output .splitlines ()
506508
507509 # Parse the command output into a structured format
@@ -523,7 +525,7 @@ def parse_show_copp_configuration(duthost):
523525 return copp_config_data
524526
525527
526- def is_trap_installed (duthost , trap_id ):
528+ def is_trap_installed (duthost , trap_id , namespace = None ):
527529 """
528530 Checks if a specific trap is installed by parsing the output of `show copp configuration`.
529531 Args:
@@ -533,7 +535,7 @@ def is_trap_installed(duthost, trap_id):
533535 bool: True if the trap is installed, False otherwise.
534536 """
535537
536- output = parse_show_copp_configuration (duthost )
538+ output = parse_show_copp_configuration (duthost , namespace )
537539 assert trap_id in output , f"Trap { trap_id } not found in the configuration"
538540 assert "hw_status" in output [trap_id ], f"hw_status not found for trap { trap_id } "
539541
@@ -552,22 +554,27 @@ def is_trap_uninstalled(duthost, trap_id):
552554 return not is_trap_installed (duthost , trap_id )
553555
554556
555- def get_trap_hw_status (duthost ):
557+ def get_trap_hw_status (duthost , namespace ):
556558 """
557559 Retrieves the hw_status for traps from the STATE_DB.
558560 Args:
559561 dut (SonicHost): The target device
560562 Returns:
561563 dict: A dictionary mapping trap IDs to their hw_status.
562564 """
563-
564- state_db_data = duthost .shell ("sonic-db-cli STATE_DB KEYS 'COPP_TRAP_TABLE|*'" )["stdout" ]
565+ if namespace is None :
566+ state_db_cmd = "sonic-db-cli STATE_DB KEYS 'COPP_TRAP_TABLE|*'"
567+ trap_data_cmd = "sonic-db-cli STATE_DB HGETALL "
568+ else :
569+ state_db_cmd = "sonic-db-cli -n {} STATE_DB KEYS 'COPP_TRAP_TABLE|*'" .format (namespace .namespace )
570+ trap_data_cmd = "sonic-db-cli -n {} STATE_DB HGETALL " .format (namespace .namespace )
571+ state_db_data = duthost .shell (state_db_cmd )["stdout" ]
565572 state_db_data = state_db_data .splitlines ()
566573 hw_status = {}
567574
568575 for key in state_db_data :
569576 trap_id = key .split ("|" )[- 1 ]
570- trap_data = duthost .shell (f"sonic-db-cli STATE_DB HGETALL '{ key } '" )["stdout" ]
577+ trap_data = duthost .shell (trap_data_cmd + f" '{ key } '" )["stdout" ]
571578 trap_data_dict = ast .literal_eval (trap_data )
572579 hw_status [trap_id ] = trap_data_dict .get ("hw_status" , "not-installed" )
573580
0 commit comments