Skip to content

Commit

Permalink
Add SaveOnSet (#108)
Browse files Browse the repository at this point in the history
* Add SaveOnSet

* Add SaveOnSet

* use trans_util for save on set

* use trans_util for save on set

* use trans_util for save on set

* use trans_util for save on set

* use trans_util for save on set

* use trans_util for save on set

* change save-on-set to sonic-services-client

* change save-on-set to sonic-services-client

* Update telemetry.go formating

* fix merge

* add coverage

* add coverage

* checking test

* mistake

* re-enable unit test

---------

Co-authored-by: Shashank Neelam <[email protected]>
  • Loading branch information
rlucus and sneelam20 authored May 21, 2024
1 parent 81174c0 commit 585f441
Show file tree
Hide file tree
Showing 3 changed files with 774 additions and 718 deletions.
58 changes: 43 additions & 15 deletions gnmi_server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ import (
"bytes"
"errors"
"fmt"
"net"
"strings"
"sync"

"github.com/Azure/sonic-mgmt-common/translib"
"github.com/sonic-net/sonic-gnmi/common_utils"
spb "github.com/sonic-net/sonic-gnmi/proto"
spb_gnoi "github.com/sonic-net/sonic-gnmi/proto/gnoi"
spb_jwt_gnoi "github.com/sonic-net/sonic-gnmi/proto/gnoi/jwt"
sdc "github.com/sonic-net/sonic-gnmi/sonic_data_client"
ssc "github.com/sonic-net/sonic-gnmi/sonic_service_client"

log "github.com/golang/glog"
"github.com/golang/protobuf/proto"
gnmipb "github.com/openconfig/gnmi/proto/gnmi"
Expand All @@ -21,9 +27,6 @@ import (
"google.golang.org/grpc/peer"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"
"net"
"strings"
"sync"
)

var (
Expand All @@ -39,6 +42,10 @@ type Server struct {
config *Config
cMu sync.Mutex
clients map[string]*Client
// SaveStartupConfig points to a function that is called to save changes of
// configuration to a file. By default it points to an empty function -
// the configuration is not saved to a file.
SaveStartupConfig func() error
// ReqFromMaster point to a function that is called to verify if the request
// comes from a master controller.
ReqFromMaster func(req *gnmipb.SetRequest, masterEID *uint128) error
Expand All @@ -50,14 +57,14 @@ type AuthTypes map[string]bool
type Config struct {
// Port for the Server to listen on. If 0 or unset the Server will pick a port
// for this Server.
Port int64
LogLevel int
Threshold int
UserAuth AuthTypes
Port int64
LogLevel int
Threshold int
UserAuth AuthTypes
EnableTranslibWrite bool
EnableNativeWrite bool
ZmqPort string
IdleConnDuration int
EnableNativeWrite bool
ZmqPort string
IdleConnDuration int
}

var AuthLock sync.Mutex
Expand Down Expand Up @@ -139,9 +146,10 @@ func NewServer(config *Config, opts []grpc.ServerOption) (*Server, error) {
reflection.Register(s)

srv := &Server{
s: s,
config: config,
clients: map[string]*Client{},
s: s,
config: config,
clients: map[string]*Client{},
SaveStartupConfig: saveOnSetDisabled,
// ReqFromMaster point to a function that is called to verify if
// the request comes from a master controller.
ReqFromMaster: ReqFromMasterDisabledMA,
Expand All @@ -160,7 +168,7 @@ func NewServer(config *Config, opts []grpc.ServerOption) (*Server, error) {
if srv.config.EnableTranslibWrite || srv.config.EnableNativeWrite {
gnoi_system_pb.RegisterSystemServer(srv.s, srv)
}
if srv.config.EnableTranslibWrite {
if srv.config.EnableTranslibWrite {
spb_gnoi.RegisterSonicServiceServer(srv.s, srv)
}
spb_gnoi.RegisterDebugServer(srv.s, srv)
Expand Down Expand Up @@ -400,6 +408,25 @@ func (s *Server) Get(ctx context.Context, req *gnmipb.GetRequest) (*gnmipb.GetRe
return &gnmipb.GetResponse{Notification: notifications}, nil
}

// saveOnSetEnabled saves configuration to a file
func SaveOnSetEnabled() error {
sc, err := ssc.NewDbusClient()
if err != nil {
log.V(0).Infof("Saving startup config failed to create dbus client: %v", err)
return err
}
if err := sc.ConfigSave("/etc/sonic/config_db.json"); err != nil {
log.V(0).Infof("Saving startup config failed: %v", err)
return err
} else {
log.V(1).Infof("Success! Startup config has been saved!")
}
return nil
}

// SaveOnSetDisabeld does nothing.
func saveOnSetDisabled() error { return nil }

func (s *Server) Set(ctx context.Context, req *gnmipb.SetRequest) (*gnmipb.SetResponse, error) {
e := s.ReqFromMaster(req, &s.masterEID)
if e != nil {
Expand Down Expand Up @@ -503,6 +530,7 @@ func (s *Server) Set(ctx context.Context, req *gnmipb.SetRequest) (*gnmipb.SetRe
common_utils.IncCounter(common_utils.GNMI_SET_FAIL)
}

s.SaveStartupConfig()
return &gnmipb.SetResponse{
Prefix: req.GetPrefix(),
Response: results,
Expand Down Expand Up @@ -600,7 +628,7 @@ func ReqFromMasterEnabledMA(req *gnmipb.SetRequest, masterEID *uint128) error {
// Role will be implemented later.
return status.Errorf(codes.Unimplemented, "MA: Role is not implemented")
}

reqEID = uint128{High: ma.ElectionId.High, Low: ma.ElectionId.Low}
// Use the election ID that is in the last extension, so, no 'break' here.
}
Expand Down
Loading

0 comments on commit 585f441

Please sign in to comment.