Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Add read-only mode #76

Open
wants to merge 1 commit into
base: dell_sonic
Choose a base branch
from
Open
Changes from all commits
Commits
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
23 changes: 20 additions & 3 deletions src/gnmi_server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
gnmipb "github.com/openconfig/gnmi/proto/gnmi"
spb "proto/gnoi"
"bytes"
"os"
)

var (
Expand All @@ -43,6 +44,7 @@ type Config struct {
// for this Server.
Port int64
UserAuth AuthTypes
ReadOnly bool
}

func (i AuthTypes) String() string {
Expand Down Expand Up @@ -108,6 +110,12 @@ func (i AuthTypes) Unset(mode string) error {
}
return nil
}
func CheckReadOnlyFile() bool {
if _, err := os.Stat("/etc/sonic/telemetry_readonly"); err == nil {
return true
}
return false
}
// New returns an initialized Server.
func NewServer(config *Config, opts []grpc.ServerOption) (*Server, error) {
if config == nil {
Expand All @@ -118,6 +126,9 @@ func NewServer(config *Config, opts []grpc.ServerOption) (*Server, error) {

reflection.Register(s)

if CheckReadOnlyFile() {
config.ReadOnly = true
}
srv := &Server{
s: s,
config: config,
Expand All @@ -132,8 +143,12 @@ func NewServer(config *Config, opts []grpc.ServerOption) (*Server, error) {
return nil, fmt.Errorf("failed to open listener port %d: %v", srv.config.Port, err)
}
gnmipb.RegisterGNMIServer(srv.s, srv)
gnoi_system_pb.RegisterSystemServer(srv.s, srv)
spb.RegisterSonicServiceServer(srv.s, srv)
if !srv.config.ReadOnly {
//gNOI Services
gnoi_system_pb.RegisterSystemServer(srv.s, srv)
spb.RegisterSonicServiceServer(srv.s, srv)
}

log.V(1).Infof("Created Server on %s", srv.Address())

return srv, nil
Expand Down Expand Up @@ -313,8 +328,10 @@ func (srv *Server) Get(ctx context.Context, req *gnmipb.GetRequest) (*gnmipb.Get
return &gnmipb.GetResponse{Notification: notifications}, nil
}

// Set method is not implemented. Refer to gnxi for examples with openconfig integration
func (srv *Server) Set(ctx context.Context,req *gnmipb.SetRequest) (*gnmipb.SetResponse, error) {
if srv.config.ReadOnly {
return nil, grpc.Errorf(codes.Unimplemented, "Telemetry is in read-only mode")
}
ctx, err := authenticate(srv.config.UserAuth, ctx)
if err != nil {
return nil, err
Expand Down