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

Add SaveOnSet #108

Merged
merged 29 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
53fca81
Add SaveOnSet
rlucus May 4, 2023
63d1461
Add SaveOnSet
rlucus May 4, 2023
e28fd28
Merge branch 'master' into master
rlucus Oct 30, 2023
add7f88
use trans_util for save on set
rlucus Oct 30, 2023
7f7fb7c
use trans_util for save on set
rlucus Oct 30, 2023
3feafb0
use trans_util for save on set
rlucus Oct 30, 2023
561fccb
use trans_util for save on set
rlucus Oct 30, 2023
39dd1d5
use trans_util for save on set
rlucus Oct 30, 2023
bd0fd0b
use trans_util for save on set
rlucus Oct 30, 2023
d14cc0d
change save-on-set to sonic-services-client
rlucus Mar 27, 2024
0ef3e3b
Merge branch 'master' into master
rlucus Mar 27, 2024
ba53d89
change save-on-set to sonic-services-client
rlucus Mar 27, 2024
4d9c147
Update telemetry.go formating
rlucus Mar 27, 2024
2ac1886
Merge branch 'master' into master
rlucus Apr 12, 2024
9d4d843
Merge branch 'master' into master
rlucus Apr 23, 2024
8daa3cf
Merge branch 'master' into master
rlucus Apr 23, 2024
77edc8e
fix merge
rlucus Apr 24, 2024
c6d234a
add coverage
rlucus Apr 24, 2024
d925cea
Merge branch 'master' into master
rlucus Apr 24, 2024
cb77023
add coverage
rlucus Apr 24, 2024
98040c4
checking test
rlucus Apr 25, 2024
427c6f9
mistake
rlucus Apr 25, 2024
b08ce09
re-enable unit test
rlucus May 7, 2024
be4e02f
Merge branch 'master' into master
rlucus May 7, 2024
4b4a0f6
Merge branch 'master' into master
rlucus May 7, 2024
9e1ac63
Merge branch 'master' into master
rlucus May 7, 2024
e7aae6e
Merge branch 'master' into master
rlucus May 13, 2024
f3e3c35
Merge branch 'master' into master
sneelam20 May 17, 2024
d8aa6b6
Merge branch 'master' into master
sneelam20 May 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Copy link
Contributor

Choose a reason for hiding this comment

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

No locks in both Set() and SaveOnSetEnabled(). Is it safe to allow parallel db writes and config saves?

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
Loading