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
26 changes: 21 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@ BUILD_DIR := build/bin
export CVL_SCHEMA_PATH := $(MGMT_COMMON_DIR)/build/cvl/schema
export GOBIN := $(abspath $(BUILD_DIR))
export PATH := $(PATH):$(GOBIN):$(shell dirname $(GO))
export CGO_LDFLAGS := -lswsscommon -lhiredis
export CGO_CXXFLAGS := -I/usr/include/swss -w -Wall -fpermissive

SRC_FILES=$(shell find . -name '*.go' | grep -v '_test.go' | grep -v '/tests/')
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 @@ -30,7 +39,7 @@ all: sonic-gnmi $(TELEMETRY_TEST_BIN)
go.mod:
$(GO) mod init github.com/sonic-net/sonic-gnmi

$(GO_DEPS): go.mod $(PATCHES)
$(GO_DEPS): go.mod $(PATCHES) swsscommon_wrap
$(GO) mod vendor
$(GO) mod download golang.org/x/crypto@v0.0.0-20191206172530-e9b2fee46413
$(GO) mod download github.com/jipanyang/gnxi@v0.0.0-20181221084354-f0a90cca6fd0
Expand All @@ -41,6 +50,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 Down Expand Up @@ -68,14 +79,19 @@ else
$(GO) install -mod=vendor github.com/sonic-net/sonic-gnmi/gnmi_dump
endif

swsscommon_wrap:
make -C swsscommon

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-dialcout.txt -covermode=atomic -mod=vendor $(BLD_FLAGS) -v github.com/sonic-net/sonic-gnmi/dialout/dialout_client
sudo CGO_LDFLAGS="$(CGO_LDFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" $(GO) test -coverprofile=coverage-config.txt -covermode=atomic -v github.com/sonic-net/sonic-gnmi/sonic_db_config
sudo CGO_LDFLAGS="$(CGO_LDFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" $(GO) test -coverprofile=coverage-gnmi.txt -covermode=atomic -mod=vendor $(BLD_FLAGS) -v github.com/sonic-net/sonic-gnmi/gnmi_server -coverpkg ../...
sudo CGO_LDFLAGS="$(CGO_LDFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" $(GO) test -coverprofile=coverage-dialcout.txt -covermode=atomic -mod=vendor $(BLD_FLAGS) -v github.com/sonic-net/sonic-gnmi/dialout/dialout_client
sudo CGO_LDFLAGS="$(CGO_LDFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" $(GO) test -coverprofile=coverage-data.txt -covermode=atomic -mod=vendor -v github.com/sonic-net/sonic-gnmi/sonic_data_client
sudo CGO_LDFLAGS="$(CGO_LDFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" $(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://github.com/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://github.com/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
Loading