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 1 commit
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
37 changes: 28 additions & 9 deletions gnmi_server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import (
"errors"
"fmt"
"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"
"github.com/Azure/sonic-mgmt-common/translib/transformer"
rlucus marked this conversation as resolved.
Show resolved Hide resolved
log "github.com/golang/glog"
"github.com/golang/protobuf/proto"
gnmipb "github.com/openconfig/gnmi/proto/gnmi"
gnmi_extpb "github.com/openconfig/gnmi/proto/gnmi_ext"
gnoi_system_pb "github.com/openconfig/gnoi/system"
"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"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
Expand All @@ -39,6 +40,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()
}
type AuthTypes map[string]bool

Expand Down Expand Up @@ -133,9 +138,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,
}
var err error
if srv.config.Port < 0 {
Expand All @@ -150,7 +156,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)
}
log.V(1).Infof("Created Server on %s, read-only: %t", srv.Address(), !srv.config.EnableTranslibWrite)
Expand Down Expand Up @@ -376,6 +382,18 @@ 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() {
rlucus marked this conversation as resolved.
Show resolved Hide resolved
if err := transformer.SaveStartupConfig(); err != nil {
rlucus marked this conversation as resolved.
Show resolved Hide resolved
rlucus marked this conversation as resolved.
Show resolved Hide resolved
log.Errorf("Saving startup config failed: %v", err)
} else {
log.Errorf("Success! Startup config has been saved!")
rlucus marked this conversation as resolved.
Show resolved Hide resolved
}
}

// SaveOnSetDisabeld does nothing.
func SaveOnSetDisabled() {}

func (s *Server) Set(ctx context.Context, req *gnmipb.SetRequest) (*gnmipb.SetResponse, error) {
common_utils.IncCounter(common_utils.GNMI_SET)
if s.config.EnableTranslibWrite == false && s.config.EnableNativeWrite == false {
Expand Down Expand Up @@ -467,6 +485,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
7 changes: 4 additions & 3 deletions gnmi_server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func createServer(t *testing.T, port int64) *Server {
if err != nil {
t.Errorf("Failed to create gNMI server: %v", err)
}
s.SaveStartupConfig = SaveOnSetEnabled
return s
}

Expand Down Expand Up @@ -3095,7 +3096,7 @@ func TestConnectionsKeepAlive(t *testing.T) {
}

func TestClient(t *testing.T) {
// sonic-host:device-test-event is a test event.
// sonic-host:device-test-event is a test event.
// Events client will drop it on floor.
events := [] sdc.Evt_rcvd {
{ "test0", 7, 777 },
Expand Down Expand Up @@ -3229,7 +3230,7 @@ func TestClient(t *testing.T) {
}

func TestGnmiSetBatch(t *testing.T) {
mockCode :=
mockCode :=
`
print('No Yang validation for test mode...')
print('%s')
Expand Down Expand Up @@ -3314,7 +3315,7 @@ func TestGNMINative(t *testing.T) {
return &dbus.Call{}
})
defer mock2.Reset()
mockCode :=
mockCode :=
`
print('No Yang validation for test mode...')
print('%s')
Expand Down
26 changes: 15 additions & 11 deletions telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@ import (

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

func main() {
Expand Down Expand Up @@ -176,6 +177,9 @@ func main() {
log.Errorf("Failed to create gNMI server: %v", err)
return
}
if *withSaveOnSet {
s.SaveStartupConfig = gnmi.SaveOnSetEnabled
}

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