Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support new gnmi config interface in telemetry container. #7

Merged
merged 15 commits into from
Nov 28, 2022
Merged
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ debian/sonic-telemetry.debhelper.log
debian/sonic-telemetry.substvars
debian/sonic-telemetry/
vendor
src
cvl
translib
22 changes: 19 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ TEST_FILES=$(wildcard *_test.go)
TELEMETRY_TEST_DIR = build/tests/gnmi_server
TELEMETRY_TEST_BIN = $(TELEMETRY_TEST_DIR)/server.test
ifeq ($(ENABLE_TRANSLIB_WRITE),y)
BLD_FLAGS := -tags gnmi_translib_write
BLD_TAGS := gnmi_translib_write
endif
ifeq ($(ENABLE_NATIVE_WRITE),y)
BLD_TAGS := $(BLD_TAGS) gnmi_native_write
endif

ifneq ($(BLD_TAGS),)
BLD_FLAGS := -tags "$(strip $(BLD_TAGS))"
endif

GO_DEPS := vendor/.done
Expand All @@ -41,6 +48,8 @@ $(GO_DEPS): go.mod $(PATCHES)
patch -d vendor -p0 < patches/gnmi_cli.all.patch
patch -d vendor -p0 < patches/gnmi_set.patch
patch -d vendor -p0 < patches/gnmi_get.patch
patch -d vendor -p0 < patches/gnmi_path.patch
patch -d vendor -p0 < patches/gnmi_xpath.patch
git apply patches/0001-Updated-to-filter-and-write-to-file.patch
touch $@

Expand All @@ -49,7 +58,7 @@ go-deps: $(GO_DEPS)
go-deps-clean:
$(RM) -r vendor

sonic-gnmi: $(GO_DEPS)
sonic-gnmi: $(GO_DEPS) libswss
ifeq ($(CROSS_BUILD_ENVIRON),y)
$(GO) build -o ${GOBIN}/telemetry -mod=vendor $(BLD_FLAGS) github.com/sonic-net/sonic-gnmi/telemetry
$(GO) build -o ${GOBIN}/dialout_client_cli -mod=vendor $(BLD_FLAGS) github.com/sonic-net/sonic-gnmi/dialout/dialout_client_cli
Expand All @@ -68,14 +77,21 @@ else
$(GO) install -mod=vendor github.com/sonic-net/sonic-gnmi/gnmi_dump
endif

# TODO: Create a new repo for this lib, sonic-restapi and sonic-gnmi can share this lib
Copy link
Collaborator

@qiluo-msft qiluo-msft Sep 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO

# TODO: Create a new repo for this lib, sonic-restapi and sonic-gnmi can share this lib


Instead of todo, let's do it now? #Closed

libswss:
make -C libcswsscommon
sudo make -C libcswsscommon install

check_gotest:
sudo mkdir -p ${DBDIR}
sudo cp ./testdata/database_config.json ${DBDIR}
sudo mkdir -p /usr/models/yang || true
sudo find $(MGMT_COMMON_DIR)/models -name '*.yang' -exec cp {} /usr/models/yang/ \;
sudo $(GO) test -coverprofile=coverage-config.txt -covermode=atomic -v github.com/sonic-net/sonic-gnmi/sonic_db_config
sudo $(GO) test -coverprofile=coverage-gnmi.txt -covermode=atomic -mod=vendor $(BLD_FLAGS) -v github.com/sonic-net/sonic-gnmi/gnmi_server
sudo $(GO) test -coverprofile=coverage-gnmi.txt -covermode=atomic -mod=vendor $(BLD_FLAGS) -v github.com/sonic-net/sonic-gnmi/gnmi_server -coverpkg ../...
sudo $(GO) test -coverprofile=coverage-dialcout.txt -covermode=atomic -mod=vendor $(BLD_FLAGS) -v github.com/sonic-net/sonic-gnmi/dialout/dialout_client
sudo $(GO) test -coverprofile=coverage-data.txt -covermode=atomic -mod=vendor -v github.com/sonic-net/sonic-gnmi/sonic_data_client
sudo $(GO) test -coverprofile=coverage-dbus.txt -covermode=atomic -mod=vendor -v github.com/sonic-net/sonic-gnmi/sonic_service_client
$(GO) get github.com/axw/gocov/...
$(GO) get github.com/AlekSi/gocov-xml
gocov convert coverage-*.txt | gocov-xml -source $(shell pwd) > coverage.xml
Expand Down
3 changes: 3 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ stages:
displayName: "Download sonic-mgmt-common"

- script: |
# PYTEST
sudo pip3 install -U pytest

# REDIS
sudo apt-get install -y redis-server
sudo sed -ri 's/^# unixsocket/unixsocket/' /etc/redis/redis.conf
Expand Down
4 changes: 4 additions & 0 deletions common_utils/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

package common_utils

const GNMI_WORK_PATH = "/tmp"
27 changes: 27 additions & 0 deletions common_utils/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ const (
GNMI_GET_FAIL
GNMI_SET
GNMI_SET_FAIL
GNOI_REBOOT
DBUS
DBUS_FAIL
DBUS_APPLY_PATCH_DB
DBUS_APPLY_PATCH_YANG
DBUS_CREATE_CHECKPOINT
DBUS_DELETE_CHECKPOINT
DBUS_CONFIG_SAVE
DBUS_CONFIG_RELOAD
COUNTER_SIZE
)

Expand All @@ -55,6 +64,24 @@ func (c CounterType) String() string {
return "GNMI set"
case GNMI_SET_FAIL:
return "GNMI set fail"
case GNOI_REBOOT:
return "GNOI reboot"
case DBUS:
return "DBUS"
case DBUS_FAIL:
return "DBUS fail"
case DBUS_APPLY_PATCH_DB:
return "DBUS apply patch db"
case DBUS_APPLY_PATCH_YANG:
return "DBUS apply patch yang"
case DBUS_CREATE_CHECKPOINT:
return "DBUS create checkpoint"
case DBUS_DELETE_CHECKPOINT:
return "DBUS delete checkpoint"
case DBUS_CONFIG_SAVE:
return "DBUS config save"
case DBUS_CONFIG_RELOAD:
return "DBUS config reload"
default:
return ""
}
Expand Down
5 changes: 5 additions & 0 deletions gnmi_server/constants_native.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// +build !gnmi_native_write

package gnmi

const ENABLE_NATIVE_WRITE = false
5 changes: 5 additions & 0 deletions gnmi_server/constants_native_write.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// +build gnmi_native_write

package gnmi

const ENABLE_NATIVE_WRITE = true
10 changes: 5 additions & 5 deletions gnmi_server/constants.go → gnmi_server/constants_translib.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// +build !gnmi_translib_write

package gnmi

const ENABLE_TRANSLIB_WRITE = false
// +build !gnmi_translib_write
package gnmi
const ENABLE_TRANSLIB_WRITE = false
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// +build gnmi_translib_write

package gnmi

const ENABLE_TRANSLIB_WRITE = true
// +build gnmi_translib_write
package gnmi
const ENABLE_TRANSLIB_WRITE = true
37 changes: 36 additions & 1 deletion gnmi_server/gnoi.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,60 @@ package gnmi

import (
"context"
"errors"
"os"
gnoi_system_pb "github.com/openconfig/gnoi/system"
log "github.com/golang/glog"
"time"
spb "github.com/sonic-net/sonic-gnmi/proto/gnoi"
transutil "github.com/sonic-net/sonic-gnmi/transl_utils"
io "io/ioutil"
ssc "github.com/sonic-net/sonic-gnmi/sonic_service_client"
spb_jwt "github.com/sonic-net/sonic-gnmi/proto/gnoi/jwt"
"github.com/sonic-net/sonic-gnmi/common_utils"
"google.golang.org/grpc/status"
"google.golang.org/grpc/codes"
"os/user"
"encoding/json"
jwt "github.com/dgrijalva/jwt-go"
)

func RebootSystem(fileName string) error {
Copy link
Collaborator

@qiluo-msft qiluo-msft Sep 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RebootSystem

Is it "reboot" or "config reload"? #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use reboot to implement "config reload", and please refer to: https:/openconfig/gnoi/blob/main/system/system.proto#L131

log.V(2).Infof("Rebooting with %s...", fileName)
sc, err := ssc.NewDbusClient()
if err != nil {
return err
}
err = sc.ConfigReload(fileName)
return err
}

func (srv *Server) Reboot(ctx context.Context, req *gnoi_system_pb.RebootRequest) (*gnoi_system_pb.RebootResponse, error) {
fileName := common_utils.GNMI_WORK_PATH + "/config_db.json.tmp"

_, err := authenticate(srv.config.UserAuth, ctx)
if err != nil {
return nil, err
}
log.V(1).Info("gNOI: Reboot")
return nil, status.Errorf(codes.Unimplemented, "")
log.V(1).Info("Request:", req)
log.V(1).Info("Reboot system now, delay is ignored...")
// TODO: Support GNOI reboot delay
// Delay in nanoseconds before issuing reboot.
// https:/openconfig/gnoi/blob/master/system/system.proto#L102-L115
config_db_json, err := io.ReadFile(fileName)
if errors.Is(err, os.ErrNotExist) {
fileName = ""
}
err = RebootSystem(string(config_db_json))
if err != nil {
return nil, err
}
var resp gnoi_system_pb.RebootResponse
return &resp, nil
}

// TODO: Support GNOI RebootStatus
func (srv *Server) RebootStatus(ctx context.Context, req *gnoi_system_pb.RebootStatusRequest) (*gnoi_system_pb.RebootStatusResponse, error) {
_, err := authenticate(srv.config.UserAuth, ctx)
if err != nil {
Expand All @@ -31,6 +64,8 @@ func (srv *Server) RebootStatus(ctx context.Context, req *gnoi_system_pb.RebootS
log.V(1).Info("gNOI: RebootStatus")
return nil, status.Errorf(codes.Unimplemented, "")
}

// TODO: Support GNOI CancelReboot
func (srv *Server) CancelReboot(ctx context.Context, req *gnoi_system_pb.CancelRebootRequest) (*gnoi_system_pb.CancelRebootResponse, error) {
_, err := authenticate(srv.config.UserAuth, ctx)
if err != nil {
Expand Down
52 changes: 47 additions & 5 deletions gnmi_server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Config struct {
LogLevel int
UserAuth AuthTypes
EnableTranslibWrite bool
EnableNativeWrite bool
}

var AuthLock sync.Mutex
Expand Down Expand Up @@ -144,8 +145,10 @@ func NewServer(config *Config, opts []grpc.ServerOption) (*Server, error) {
}
gnmipb.RegisterGNMIServer(srv.s, srv)
spb_jwt_gnoi.RegisterSonicJwtServiceServer(srv.s, srv)
if srv.config.EnableTranslibWrite {
if srv.config.EnableTranslibWrite || srv.config.EnableNativeWrite {
gnoi_system_pb.RegisterSystemServer(srv.s, srv)
}
if srv.config.EnableTranslibWrite {
spb_gnoi.RegisterSonicServiceServer(srv.s, srv)
}
log.V(1).Infof("Created Server on %s, read-only: %t", srv.Address(), !srv.config.EnableTranslibWrite)
Expand Down Expand Up @@ -315,6 +318,8 @@ func (s *Server) Get(ctx context.Context, req *gnmipb.GetRequest) (*gnmipb.GetRe

if target == "OTHERS" {
dc, err = sdc.NewNonDbClient(paths, prefix)
} else if target == "MIXED" {
dc, err = sdc.NewMixedDbClient(paths, prefix)
} else if _, ok, _, _ := sdc.IsTargetDb(target); ok {
dc, err = sdc.NewDbClient(paths, prefix)
} else {
Expand Down Expand Up @@ -363,9 +368,42 @@ func (s *Server) Set(ctx context.Context, req *gnmipb.SetRequest) (*gnmipb.SetRe

/* Fetch the prefix. */
prefix := req.GetPrefix()
var target string
if prefix == nil {
common_utils.IncCounter(common_utils.GNMI_SET_FAIL)
return nil, status.Error(codes.Unimplemented, "No target specified in prefix")
} else {
target = prefix.GetTarget()
}
extensions := req.GetExtension()
/* Create Transl client. */
dc, _ := sdc.NewTranslClient(prefix, nil, ctx, extensions)

var dc sdc.Client
if target == "MIXED" {
if s.config.EnableNativeWrite == false {
common_utils.IncCounter(common_utils.GNMI_SET_FAIL)
return nil, grpc.Errorf(codes.Unimplemented, "Mixed schema is disabled")
}
paths := req.GetDelete()
for _, path := range req.GetReplace() {
paths = append(paths, path.GetPath())
}
for _, path := range req.GetUpdate() {
paths = append(paths, path.GetPath())
}
dc, err = sdc.NewMixedDbClient(paths, prefix)
} else {
if s.config.EnableTranslibWrite == false {
common_utils.IncCounter(common_utils.GNMI_SET_FAIL)
return nil, grpc.Errorf(codes.Unimplemented, "Telemetry is in read-only mode")
}
/* Create Transl client. */
dc, err = sdc.NewTranslClient(prefix, nil, ctx, extensions)
}

if err != nil {
common_utils.IncCounter(common_utils.GNMI_SET_FAIL)
return nil, status.Error(codes.NotFound, err.Error())
}

/* DELETE */
for _, path := range req.GetDelete() {
Expand Down Expand Up @@ -422,10 +460,14 @@ func (s *Server) Capabilities(ctx context.Context, req *gnmipb.CapabilityRequest
return nil, err
}
extensions := req.GetExtension()
dc, _ := sdc.NewTranslClient(nil, nil, ctx, extensions)

/* Fetch the client capabitlities. */
supportedModels := dc.Capabilities()
var supportedModels []gnmipb.ModelData
dc, _ := sdc.NewTranslClient(nil, nil, ctx, extensions)
supportedModels = append(supportedModels, dc.Capabilities()...)
dc, _ = sdc.NewMixedDbClient(nil, nil)
supportedModels = append(supportedModels, dc.Capabilities()...)

suppModels := make([]*gnmipb.ModelData, len(supportedModels))

for index, model := range supportedModels {
Expand Down
Loading