From c5c4e165e28efe94cb6bea407f37d1e89fe21c3f Mon Sep 17 00:00:00 2001 From: Feng Pan Date: Tue, 3 Mar 2026 03:26:10 +0000 Subject: [PATCH] Fix port wildcard-prefix --- sonic_data_client/virtual_db.go | 8 ++++ sonic_data_client/virtual_db_test.go | 70 ++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/sonic_data_client/virtual_db.go b/sonic_data_client/virtual_db.go index d5b826709..66ca897ea 100644 --- a/sonic_data_client/virtual_db.go +++ b/sonic_data_client/virtual_db.go @@ -959,7 +959,11 @@ func v2rAclRuleStats(paths []string) ([]tablePath, error) { func v2rPortPhyAttrStats(paths []string) ([]tablePath, error) { var tblPaths []tablePath if strings.HasSuffix(paths[KeyIdx], "*") { // All Ethernet ports + keyPrefix := strings.TrimSuffix(paths[KeyIdx], "*") for port, oid := range countersPortNameMap { + if !strings.HasPrefix(port, keyPrefix) { + continue + } namespace, ok := port2namespaceMap[port] if !ok { return nil, fmt.Errorf("%v does not have namespace associated", port) @@ -1005,7 +1009,11 @@ func v2rPortPhyAttrStats(paths []string) ([]tablePath, error) { func v2rPortPhyAttrFieldStats(paths []string) ([]tablePath, error) { var tblPaths []tablePath if strings.HasSuffix(paths[KeyIdx], "*") { + keyPrefix := strings.TrimSuffix(paths[KeyIdx], "*") for port, oid := range countersPortNameMap { + if !strings.HasPrefix(port, keyPrefix) { + continue + } namespace, ok := port2namespaceMap[port] if !ok { return nil, fmt.Errorf("%v does not have namespace associated", port) diff --git a/sonic_data_client/virtual_db_test.go b/sonic_data_client/virtual_db_test.go index dc1da8325..6b5c3b2c4 100644 --- a/sonic_data_client/virtual_db_test.go +++ b/sonic_data_client/virtual_db_test.go @@ -106,6 +106,41 @@ func TestV2rPortPhyAttrStats_Wildcard(t *testing.T) { } } +func TestV2rPortPhyAttrStats_WildcardPrefixFilter(t *testing.T) { + sdcfg.Init() + restore := setupPortMaps(t) + defer restore() + + countersPortNameMap = map[string]string{ + "etp0": "oid:0x1000000000001", + "etp1": "oid:0x1000000000002", + } + port2namespaceMap = map[string]string{ + "etp0": "", + "etp1": "", + } + + // Query Ethernet* should not match etp* keys. + paths := []string{"COUNTERS_DB", "PORT_PHY_ATTR", "Ethernet*"} + tblPaths, err := v2rPortPhyAttrStats(paths) + if err != nil { + t.Fatalf("v2rPortPhyAttrStats returned error: %v", err) + } + if len(tblPaths) != 0 { + t.Fatalf("expected 0 table paths for Ethernet* against etp* map, got %d", len(tblPaths)) + } + + // Query etp* should match etp keys. + paths = []string{"COUNTERS_DB", "PORT_PHY_ATTR", "etp*"} + tblPaths, err = v2rPortPhyAttrStats(paths) + if err != nil { + t.Fatalf("v2rPortPhyAttrStats returned error: %v", err) + } + if len(tblPaths) != 2 { + t.Fatalf("expected 2 table paths for etp* wildcard, got %d", len(tblPaths)) + } +} + func TestV2rPortPhyAttrStats_SinglePort(t *testing.T) { sdcfg.Init() restore := setupPortMaps(t) @@ -254,6 +289,41 @@ func TestV2rPortPhyAttrFieldStats_Wildcard(t *testing.T) { } } +func TestV2rPortPhyAttrFieldStats_WildcardPrefixFilter(t *testing.T) { + sdcfg.Init() + restore := setupPortMaps(t) + defer restore() + + countersPortNameMap = map[string]string{ + "etp0": "oid:0x1000000000001", + "etp1": "oid:0x1000000000002", + } + port2namespaceMap = map[string]string{ + "etp0": "", + "etp1": "", + } + + // Query Ethernet* should not match etp* keys. + paths := []string{"COUNTERS_DB", "PORT_PHY_ATTR", "Ethernet*", "phy_rx_signal_detect"} + tblPaths, err := v2rPortPhyAttrFieldStats(paths) + if err != nil { + t.Fatalf("v2rPortPhyAttrFieldStats returned error: %v", err) + } + if len(tblPaths) != 0 { + t.Fatalf("expected 0 table paths for Ethernet* against etp* map, got %d", len(tblPaths)) + } + + // Query etp* should match etp keys. + paths = []string{"COUNTERS_DB", "PORT_PHY_ATTR", "etp*", "phy_rx_signal_detect"} + tblPaths, err = v2rPortPhyAttrFieldStats(paths) + if err != nil { + t.Fatalf("v2rPortPhyAttrFieldStats returned error: %v", err) + } + if len(tblPaths) != 2 { + t.Fatalf("expected 2 table paths for etp* wildcard, got %d", len(tblPaths)) + } +} + func TestV2rPortPhyAttrFieldStats_SinglePort(t *testing.T) { sdcfg.Init() restore := setupPortMaps(t)