Skip to content

Commit 53fca81

Browse files
committed
Add SaveOnSet
1 parent f576fe0 commit 53fca81

File tree

3 files changed

+47
-23
lines changed

3 files changed

+47
-23
lines changed

gnmi_server/server.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
4348
type 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+
379397
func (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,

gnmi_server/server_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ func createServer(t *testing.T, port int64) *Server {
116116
if err != nil {
117117
t.Errorf("Failed to create gNMI server: %v", err)
118118
}
119+
s.SaveStartupConfig = SaveOnSetEnabled
119120
return s
120121
}
121122

@@ -3095,7 +3096,7 @@ func TestConnectionsKeepAlive(t *testing.T) {
30953096
}
30963097

30973098
func TestClient(t *testing.T) {
3098-
// sonic-host:device-test-event is a test event.
3099+
// sonic-host:device-test-event is a test event.
30993100
// Events client will drop it on floor.
31003101
events := [] sdc.Evt_rcvd {
31013102
{ "test0", 7, 777 },
@@ -3229,7 +3230,7 @@ func TestClient(t *testing.T) {
32293230
}
32303231

32313232
func TestGnmiSetBatch(t *testing.T) {
3232-
mockCode :=
3233+
mockCode :=
32333234
`
32343235
print('No Yang validation for test mode...')
32353236
print('%s')
@@ -3314,7 +3315,7 @@ func TestGNMINative(t *testing.T) {
33143315
return &dbus.Call{}
33153316
})
33163317
defer mock2.Reset()
3317-
mockCode :=
3318+
mockCode :=
33183319
`
33193320
print('No Yang validation for test mode...')
33203321
print('%s')

telemetry/telemetry.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,21 @@ import (
2020

2121
var (
2222
userAuth = gnmi.AuthTypes{"password": false, "cert": false, "jwt": false}
23-
port = flag.Int("port", -1, "port to listen on")
23+
port = flag.Int("port", -1, "port to listen on")
2424
// Certificate files.
25-
caCert = flag.String("ca_crt", "", "CA certificate for client certificate validation. Optional.")
26-
serverCert = flag.String("server_crt", "", "TLS server certificate")
27-
serverKey = flag.String("server_key", "", "TLS server private key")
28-
insecure = flag.Bool("insecure", false, "Skip providing TLS cert and key, for testing only!")
29-
noTLS = flag.Bool("noTLS", false, "disable TLS, for testing only!")
30-
allowNoClientCert = flag.Bool("allow_no_client_auth", false, "When set, telemetry server will request but not require a client certificate.")
31-
jwtRefInt = flag.Uint64("jwt_refresh_int", 900, "Seconds before JWT expiry the token can be refreshed.")
32-
jwtValInt = flag.Uint64("jwt_valid_int", 3600, "Seconds that JWT token is valid for.")
25+
caCert = flag.String("ca_crt", "", "CA certificate for client certificate validation. Optional.")
26+
serverCert = flag.String("server_crt", "", "TLS server certificate")
27+
serverKey = flag.String("server_key", "", "TLS server private key")
28+
insecure = flag.Bool("insecure", false, "Skip providing TLS cert and key, for testing only!")
29+
noTLS = flag.Bool("noTLS", false, "disable TLS, for testing only!")
30+
allowNoClientCert = flag.Bool("allow_no_client_auth", false, "When set, telemetry server will request but not require a client certificate.")
31+
jwtRefInt = flag.Uint64("jwt_refresh_int", 900, "Seconds before JWT expiry the token can be refreshed.")
32+
jwtValInt = flag.Uint64("jwt_valid_int", 3600, "Seconds that JWT token is valid for.")
3333
gnmi_translib_write = flag.Bool("gnmi_translib_write", gnmi.ENABLE_TRANSLIB_WRITE, "Enable gNMI translib write for management framework")
3434
gnmi_native_write = flag.Bool("gnmi_native_write", gnmi.ENABLE_NATIVE_WRITE, "Enable gNMI native write")
35-
threshold = flag.Int("threshold", 100, "max number of client connections")
36-
idle_conn_duration = flag.Int("idle_conn_duration", 5, "Seconds before server closes idle connections")
35+
threshold = flag.Int("threshold", 100, "max number of client connections")
36+
idle_conn_duration = flag.Int("idle_conn_duration", 5, "Seconds before server closes idle connections")
37+
withSaveOnSet = flag.Bool("with-save-on-set", false, "Enables save-on-set.")
3738
)
3839

3940
func main() {
@@ -176,6 +177,9 @@ func main() {
176177
log.Errorf("Failed to create gNMI server: %v", err)
177178
return
178179
}
180+
if *withSaveOnSet {
181+
s.SaveStartupConfig = gnmi.SaveOnSetEnabled
182+
}
179183

180184
log.V(1).Infof("Auth Modes: ", userAuth)
181185
log.V(1).Infof("Starting RPC server on address: %s", s.Address())

0 commit comments

Comments
 (0)