Skip to content

Commit

Permalink
Add key to on change updates (#138)
Browse files Browse the repository at this point in the history
### Why I did it

Key is missing in on change updates

Current:

root@efb973f6ce02:~# python /root/gnxi/gnmi_cli_py/py_gnmicli.py -g -t X -p 50051 -m subscribe -x NEIGH_STATE_TABLE -xt STATE_DB -o ndastreamingservertest --subscribe_mode 0 --submode 1 --interval 0 --update_count 2 --create_connections 1

```
2023-07-20 22:05:25.146156 response received: 
sync_response: true

2023-07-20 22:05:31.271959 response received: 
update {
  timestamp: 1689890731162239986
  prefix {
    target: "STATE_DB"
  }
  update {
    path {
      elem {
        name: "NEIGH_STATE_TABLE"
      }
    }
    val {
      json_ietf_val: "{\"peerType\":\"e-BGP\",\"state\":\"Active\"}"
    }
  }
}
```

Changed:

```
2023-07-20 22:01:56.251096 response received: 
sync_response: true

2023-07-20 22:02:15.558270 response received: 
update {
  timestamp: 1689890542962488225
  prefix {
    target: "STATE_DB"
  }
  update {
    path {
      elem {
        name: "NEIGH_STATE_TABLE"
      }
    }
    val {
      json_ietf_val: "{\"10.0.0.57\":{\"peerType\":\"e-BGP\",\"state\":\"Active\"}}"
    }
  }
}
```
#### How I did it

Set use key value to true

#### How to verify it

E2E test (sonic-mgmt test case)
  • Loading branch information
zbud-msft authored and StormLiangMS committed Oct 17, 2023
1 parent de55902 commit f1ea212
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
20 changes: 20 additions & 0 deletions gnmi_server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3438,6 +3438,26 @@ func TestClient(t *testing.T) {
s.s.Stop()
}

func TestTableData2MsiUseKey(t *testing.T) {
tblPath := sdc.CreateTablePath("STATE_DB", "NEIGH_STATE_TABLE", "|", "10.0.0.57")
newMsi := make(map[string]interface{})
sdc.TableData2Msi(&tblPath, true, nil, &newMsi)
newMsiData, _ := json.MarshalIndent(newMsi, "", " ")
t.Logf(string(newMsiData))
expectedMsi := map[string]interface{} {
"10.0.0.57": map[string]interface{} {
"peerType": "e-BGP",
"state": "Established",
},
}
expectedMsiData, _ := json.MarshalIndent(expectedMsi, "", " ")
t.Logf(string(expectedMsiData))

if !reflect.DeepEqual(newMsi, expectedMsi) {
t.Errorf("Msi data does not match for use key = true")
}
}

func TestGnmiSetBatch(t *testing.T) {
mockCode :=
`
Expand Down
21 changes: 15 additions & 6 deletions sonic_data_client/db_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ var IntervalTicker = func(interval time.Duration) <-chan time.Time {
var NeedMock bool = false
var intervalTickerMutex sync.Mutex

func CreateTablePath(dbName string, tableName string, delimitor string, tableKey string) tablePath {
var tblPath tablePath
tblPath.dbName = dbName
tblPath.tableName = tableName
tblPath.delimitor = delimitor
tblPath.tableKey = tableKey
return tblPath
}

// Define a new function to set the IntervalTicker variable
func SetIntervalTicker(f func(interval time.Duration) <-chan time.Time) {
if NeedMock == true {
Expand Down Expand Up @@ -687,11 +696,11 @@ func emitJSON(v *map[string]interface{}) ([]byte, error) {
return j, nil
}

// tableData2Msi renders the redis DB data to map[string]interface{}
// TableData2Msi renders the redis DB data to map[string]interface{}
// which may be marshaled to JSON format
// If only table name provided in the tablePath, find all keys in the table, otherwise
// Use tableName + tableKey as key to get all field value paires
func tableData2Msi(tblPath *tablePath, useKey bool, op *string, msi *map[string]interface{}) error {
func TableData2Msi(tblPath *tablePath, useKey bool, op *string, msi *map[string]interface{}) error {
redisDb := Target2RedisDb[tblPath.dbNamespace][tblPath.dbName]

var pattern string
Expand Down Expand Up @@ -805,7 +814,7 @@ func tableData2TypedValue(tblPaths []tablePath, op *string) (*gnmipb.TypedValue,
}
}

err := tableData2Msi(&tblPath, useKey, nil, &msi)
err := TableData2Msi(&tblPath, useKey, nil, &msi)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1064,7 +1073,7 @@ func dbSingleTableKeySubscribe(c *DbClient, rsd redisSubData, updateChannel chan
} else if subscr.Payload == "hset" {
//op := "SET"
if tblPath.tableKey != "" {
err = tableData2Msi(&tblPath, false, nil, &newMsi)
err = TableData2Msi(&tblPath, false, nil, &newMsi)
if err != nil {
enqueueFatalMsg(c, err.Error())
return
Expand All @@ -1076,7 +1085,7 @@ func dbSingleTableKeySubscribe(c *DbClient, rsd redisSubData, updateChannel chan
continue
}
tblPath.tableKey = subscr.Channel[prefixLen:]
err = tableData2Msi(&tblPath, false, nil, &newMsi)
err = TableData2Msi(&tblPath, true, nil, &newMsi)
if err != nil {
enqueueFatalMsg(c, err.Error())
return
Expand Down Expand Up @@ -1185,7 +1194,7 @@ func dbTableKeySubscribe(c *DbClient, gnmiPath *gnmipb.Path, interval time.Durat
}
log.V(2).Infof("Psubscribe succeeded for %v: %v", tblPath, subscr)

err = tableData2Msi(&tblPath, false, nil, &msiAll)
err = TableData2Msi(&tblPath, false, nil, &msiAll)
if err != nil {
handleFatalMsg(err.Error())
return
Expand Down

0 comments on commit f1ea212

Please sign in to comment.