Skip to content

Commit

Permalink
Support proto encoding (#140)
Browse files Browse the repository at this point in the history
Why I did it
Need to support GNMI request with proto encoding.

How I did it
Update gnmi client and gnmi server to support both json ietf and proto encoding.

How to verify it
Run GNMI unit test.
  • Loading branch information
ganglyu authored Jul 28, 2023
1 parent fb338d5 commit 2c8e4ab
Show file tree
Hide file tree
Showing 7 changed files with 291 additions and 59 deletions.
10 changes: 6 additions & 4 deletions gnmi_server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

var (
supportedEncodings = []gnmipb.Encoding{gnmipb.Encoding_JSON, gnmipb.Encoding_JSON_IETF}
supportedEncodings = []gnmipb.Encoding{gnmipb.Encoding_JSON, gnmipb.Encoding_JSON_IETF, gnmipb.Encoding_PROTO}
)

// Server manages a single gNMI Server implementation. Each client that connects
Expand Down Expand Up @@ -329,6 +329,7 @@ func (s *Server) Get(ctx context.Context, req *gnmipb.GetRequest) (*gnmipb.GetRe

paths := req.GetPath()
extensions := req.GetExtension()
encoding := req.GetEncoding()
log.V(2).Infof("GetRequest paths: %v", paths)

var dc sdc.Client
Expand All @@ -344,7 +345,7 @@ func (s *Server) Get(ctx context.Context, req *gnmipb.GetRequest) (*gnmipb.GetRe
return nil, err
}
if check := IsNativeOrigin(origin); check {
dc, err = sdc.NewMixedDbClient(paths, prefix, origin, s.config.ZmqAddress)
dc, err = sdc.NewMixedDbClient(paths, prefix, origin, encoding, s.config.ZmqAddress)
} else {
dc, err = sdc.NewTranslClient(prefix, paths, ctx, extensions)
}
Expand Down Expand Up @@ -393,6 +394,7 @@ func (s *Server) Set(ctx context.Context, req *gnmipb.SetRequest) (*gnmipb.SetRe
/* Fetch the prefix. */
prefix := req.GetPrefix()
extensions := req.GetExtension()
encoding := gnmipb.Encoding_JSON_IETF

var dc sdc.Client
paths := req.GetDelete()
Expand All @@ -411,7 +413,7 @@ func (s *Server) Set(ctx context.Context, req *gnmipb.SetRequest) (*gnmipb.SetRe
common_utils.IncCounter(common_utils.GNMI_SET_FAIL)
return nil, grpc.Errorf(codes.Unimplemented, "GNMI native write is disabled")
}
dc, err = sdc.NewMixedDbClient(paths, prefix, origin, s.config.ZmqAddress)
dc, err = sdc.NewMixedDbClient(paths, prefix, origin, encoding, s.config.ZmqAddress)
} else {
if s.config.EnableTranslibWrite == false {
common_utils.IncCounter(common_utils.GNMI_SET_FAIL)
Expand Down Expand Up @@ -486,7 +488,7 @@ func (s *Server) Capabilities(ctx context.Context, req *gnmipb.CapabilityRequest
var supportedModels []gnmipb.ModelData
dc, _ := sdc.NewTranslClient(nil, nil, ctx, extensions)
supportedModels = append(supportedModels, dc.Capabilities()...)
dc, _ = sdc.NewMixedDbClient(nil, nil, "", s.config.ZmqAddress)
dc, _ = sdc.NewMixedDbClient(nil, nil, "", gnmipb.Encoding_JSON_IETF, s.config.ZmqAddress)
supportedModels = append(supportedModels, dc.Capabilities()...)

suppModels := make([]*gnmipb.ModelData, len(supportedModels))
Expand Down
43 changes: 40 additions & 3 deletions patches/gnmi_get.patch
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
--- ./github.com/jipanyang/gnxi/gnmi_get/gnmi_get.go 2019-11-26 15:44:07.303598063 -0800
+++ ./github.com/jipanyang/gnxi/gnmi_get/gnmi_get.go 2019-12-19 19:56:11.364223008 -0800
@@ -30,7 +30,7 @@
@@ -21,6 +21,7 @@
"fmt"
"strings"
"time"
+ "io/ioutil"

log "github.com/golang/glog"
"github.com/golang/protobuf/proto"
@@ -30,7 +31,7 @@
"github.com/google/gnxi/utils"
"github.com/google/gnxi/utils/credentials"
"github.com/jipanyang/gnxi/utils/xpath"
Expand All @@ -9,11 +17,12 @@
pb "github.com/openconfig/gnmi/proto/gnmi"
)

@@ -63,11 +63,12 @@
@@ -63,17 +64,20 @@
xPathFlags arrayFlags
pbPathFlags arrayFlags
pbModelDataFlags arrayFlags
- pathTarget = flag.String("xpath_target", "CONFIG_DB", "name of the target for which the path is a member")
+ protoFileFlags arrayFlags
+ pathTarget = flag.String("xpath_target", "", "name of the target for which the path is a member")
targetAddr = flag.String("target_addr", "localhost:10161", "The target address in the format of host:port")
targetName = flag.String("target_name", "hostname.com", "The target name use to verify the hostname returned by TLS handshake")
Expand All @@ -23,7 +32,14 @@
)

func main() {
@@ -88,6 +89,10 @@
flag.Var(&xPathFlags, "xpath", "xpath of the config node to be fetched")
flag.Var(&pbPathFlags, "pbpath", "protobuf format path of the config node to be fetched")
flag.Var(&pbModelDataFlags, "model_data", "Data models to be used by the target in the format of 'name,organization,version'")
+ flag.Var(&protoFileFlags, "proto_file", "Files for proto bytes in get response")
flag.Parse()

opts := credentials.ClientCredentials(*targetName)
@@ -88,6 +92,10 @@
ctx, cancel := context.WithTimeout(context.Background(), *timeOut)
defer cancel()

Expand All @@ -34,3 +50,24 @@
encoding, ok := pb.Encoding_value[*encodingName]
if !ok {
var gnmiEncodingList []string
@@ -139,4 +147,20 @@

fmt.Println("== getResponse:")
utils.PrintProto(getResponse)
+
+ if *encodingName == "PROTO" {
+ cnt := 0
+ for _, notification := range getResponse.GetNotification() {
+ for _, update := range notification.GetUpdate() {
+ val := update.GetVal()
+ protoBytes := val.GetProtoBytes()
+ fileName := protoFileFlags[cnt]
+ err := ioutil.WriteFile(fileName, protoBytes, 0666)
+ if err != nil {
+ log.Exitf("Write %v failed", "PROTO" + string(cnt))
+ }
+ cnt = cnt + 1
+ }
+ }
+ }
}
22 changes: 20 additions & 2 deletions patches/gnmi_set.patch
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,25 @@
jsonFile := pathValuePair[1][1:]
jsonConfig, err := ioutil.ReadFile(jsonFile)
if err != nil {
@@ -144,8 +168,10 @@
@@ -81,6 +105,17 @@
JsonIetfVal: jsonConfig,
},
}
+ } else if pathValuePair[1][0] == '$' {
+ protoFile := pathValuePair[1][1:]
+ protoConfig, err := ioutil.ReadFile(protoFile)
+ if err != nil {
+ log.Exitf("cannot read data from file %v", protoFile)
+ }
+ pbVal = &pb.TypedValue{
+ Value: &pb.TypedValue_ProtoBytes{
+ ProtoBytes: protoConfig,
+ },
+ }
} else {
if strVal, err := strconv.Unquote(pathValuePair[1]); err == nil {
pbVal = &pb.TypedValue{
@@ -144,8 +179,10 @@
}
replaceList := buildPbUpdateList(replaceOpt)
updateList := buildPbUpdateList(updateOpt)
Expand All @@ -73,7 +91,7 @@
Delete: deleteList,
Replace: replaceList,
Update: updateList,
@@ -155,11 +181,17 @@
@@ -155,11 +192,17 @@
utils.PrintProto(setRequest)

cli := pb.NewGNMIClient(conn)
Expand Down
3 changes: 2 additions & 1 deletion sonic_data_client/db_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ type tablePath struct {
tableKey string
delimitor string
field string
value string
jsonValue string
protoValue string
index int
operation int
// path name to be used in json data which may be different
Expand Down
Loading

0 comments on commit 2c8e4ab

Please sign in to comment.