@@ -5,16 +5,17 @@ import (
55 "errors"
66 "fmt"
77 "github.com/Azure/sonic-mgmt-common/translib"
8- "github.com/sonic-net/sonic-gnmi/common_utils"
9- spb "github.com/sonic-net/sonic-gnmi/proto"
10- spb_gnoi "github.com/sonic-net/sonic-gnmi/proto/gnoi"
11- spb_jwt_gnoi "github.com/sonic-net/sonic-gnmi/proto/gnoi/jwt"
12- sdc "github.com/sonic-net/sonic-gnmi/sonic_data_client"
8+ "github.com/Azure/sonic-mgmt-common/translib/transformer"
139 log "github.com/golang/glog"
1410 "github.com/golang/protobuf/proto"
1511 gnmipb "github.com/openconfig/gnmi/proto/gnmi"
1612 gnmi_extpb "github.com/openconfig/gnmi/proto/gnmi_ext"
1713 gnoi_system_pb "github.com/openconfig/gnoi/system"
14+ "github.com/sonic-net/sonic-gnmi/common_utils"
15+ spb "github.com/sonic-net/sonic-gnmi/proto"
16+ spb_gnoi "github.com/sonic-net/sonic-gnmi/proto/gnoi"
17+ spb_jwt_gnoi "github.com/sonic-net/sonic-gnmi/proto/gnoi/jwt"
18+ sdc "github.com/sonic-net/sonic-gnmi/sonic_data_client"
1819 "golang.org/x/net/context"
1920 "google.golang.org/grpc"
2021 "google.golang.org/grpc/codes"
@@ -39,6 +40,10 @@ type Server struct {
3940 config * Config
4041 cMu sync.Mutex
4142 clients map [string ]* Client
43+ // SaveStartupConfig points to a function that is called to save changes of
44+ // configuration to a file. By default it points to an empty function -
45+ // the configuration is not saved to a file.
46+ SaveStartupConfig func ()
4247}
4348type AuthTypes map [string ]bool
4449
@@ -133,9 +138,10 @@ func NewServer(config *Config, opts []grpc.ServerOption) (*Server, error) {
133138 reflection .Register (s )
134139
135140 srv := & Server {
136- s : s ,
137- config : config ,
138- clients : map [string ]* Client {},
141+ s : s ,
142+ config : config ,
143+ clients : map [string ]* Client {},
144+ SaveStartupConfig : SaveOnSetDisabled ,
139145 }
140146 var err error
141147 if srv .config .Port < 0 {
@@ -150,7 +156,7 @@ func NewServer(config *Config, opts []grpc.ServerOption) (*Server, error) {
150156 if srv .config .EnableTranslibWrite || srv .config .EnableNativeWrite {
151157 gnoi_system_pb .RegisterSystemServer (srv .s , srv )
152158 }
153- if srv .config .EnableTranslibWrite {
159+ if srv .config .EnableTranslibWrite {
154160 spb_gnoi .RegisterSonicServiceServer (srv .s , srv )
155161 }
156162 log .V (1 ).Infof ("Created Server on %s, read-only: %t" , srv .Address (), ! srv .config .EnableTranslibWrite )
@@ -376,6 +382,18 @@ func (s *Server) Get(ctx context.Context, req *gnmipb.GetRequest) (*gnmipb.GetRe
376382 return & gnmipb.GetResponse {Notification : notifications }, nil
377383}
378384
385+ // SaveOnSetEnabled saves configuration to a file
386+ func SaveOnSetEnabled () {
387+ if err := transformer .SaveStartupConfig (); err != nil {
388+ log .Errorf ("Saving startup config failed: %v" , err )
389+ } else {
390+ log .Errorf ("Success! Startup config has been saved!" )
391+ }
392+ }
393+
394+ // SaveOnSetDisabeld does nothing.
395+ func SaveOnSetDisabled () {}
396+
379397func (s * Server ) Set (ctx context.Context , req * gnmipb.SetRequest ) (* gnmipb.SetResponse , error ) {
380398 common_utils .IncCounter (common_utils .GNMI_SET )
381399 if s .config .EnableTranslibWrite == false && s .config .EnableNativeWrite == false {
@@ -467,6 +485,7 @@ func (s *Server) Set(ctx context.Context, req *gnmipb.SetRequest) (*gnmipb.SetRe
467485 common_utils .IncCounter (common_utils .GNMI_SET_FAIL )
468486 }
469487
488+ s .SaveStartupConfig ()
470489 return & gnmipb.SetResponse {
471490 Prefix : req .GetPrefix (),
472491 Response : results ,
0 commit comments