Skip to content

Commit

Permalink
feature change
Browse files Browse the repository at this point in the history
  • Loading branch information
isabelmsft committed Jul 24, 2024
1 parent a610784 commit 0fe0971
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 84 deletions.
3 changes: 3 additions & 0 deletions common_utils/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const (
DBUS_CONFIG_RELOAD
DBUS_STOP_SERVICE
DBUS_RESTART_SERVICE
DBUS_FILE_STAT
COUNTER_SIZE
)

Expand Down Expand Up @@ -88,6 +89,8 @@ func (c CounterType) String() string {
return "DBUS stop service"
case DBUS_RESTART_SERVICE:
return "DBUS restart service"
case DBUS_FILE_STAT:
return "DBUS file stat"
default:
return ""
}
Expand Down
109 changes: 90 additions & 19 deletions gnmi_server/gnoi.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"context"
"errors"
"os"
"strconv"
"strings"
gnoi_system_pb "github.com/openconfig/gnoi/system"
gnoi_file_pb "github.com/openconfig/gnoi/file"
log "github.com/golang/glog"
"time"
spb "github.com/sonic-net/sonic-gnmi/proto/gnoi"
Expand All @@ -20,6 +23,76 @@ import (
jwt "github.com/dgrijalva/jwt-go"
)

func ReadFileStat(path string) (*gnoi_file_pb.StatInfo, error) {
sc, err := ssc.NewDbusClient()
if err != nil {
return nil, err
}

log.V(2).Infof("Reading file stat at path %s...", path)
data, err := sc.GetFileStat(path)
if err != nil {
log.V(2).Infof("Failed to read file stat at path %s", path)
}
// Parse the data and populate StatInfo
lastModified, err := strconv.ParseUint(data["last_modified"], 10, 64)
if err != nil {
return nil, err
}

permissions, err := strconv.ParseUint(data["permissions"], 8, 32)
if err != nil {
return nil, err
}

size, err := strconv.ParseUint(data["size"], 10, 64)
if err != nil {
return nil, err
}

umaskStr := data["umask"]
if strings.HasPrefix(umaskStr, "o") {
umaskStr = umaskStr[1:] // Remove leading "o"
}
umask, err := strconv.ParseUint(umaskStr, 8, 32)
if err != nil {
return nil, err
}

statInfo := &gnoi_file_pb.StatInfo{
Path: data["path"],
LastModified: lastModified,
Permissions: uint32(permissions),
Size: size,
Umask: uint32(umask),
}
return statInfo, nil
}

func (srv *GNOIServer) Stat(ctx context.Context, req *gnoi_file_pb.StatRequest) (*gnoi_file_pb.StatResponse, error) {
_, err := authenticate(srv.config, ctx)
if err != nil {
return nil, err
}
path := req.GetPath()
log.V(1).Info("gNOI: Read File Stat")
log.V(1).Info("Request: ", req)
statInfo, err := ReadFileStat(path)
if err != nil {
return nil, err
}
resp := &gnoi_file_pb.StatResponse{
Stats: []*gnoi_file_pb.StatInfo{statInfo},
}
return resp, nil
}

// TODO: Support GNOI File Get
func (srv *GNOIServer) Get(req *gnoi_file_pb.GetRequest, stream gnoi_file_pb.File_GetServer) error {
log.V(1).Info("gNOI: File Get")
return status.Errorf(codes.Unimplemented, "")
}

func KillOrRestartProcess(restart bool, serviceName string) error {
sc, err := ssc.NewDbusClient()
if err != nil {
Expand All @@ -41,7 +114,7 @@ func KillOrRestartProcess(restart bool, serviceName string) error {
return err
}

func (srv *Server) KillProcess(ctx context.Context, req *gnoi_system_pb.KillProcessRequest) (*gnoi_system_pb.KillProcessResponse, error) {
func (srv *GNOIServer) KillProcess(ctx context.Context, req *gnoi_system_pb.KillProcessRequest) (*gnoi_system_pb.KillProcessResponse, error) {
_, err := authenticate(srv.config, ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -75,7 +148,7 @@ func RebootSystem(fileName string) error {
return err
}

func (srv *Server) Reboot(ctx context.Context, req *gnoi_system_pb.RebootRequest) (*gnoi_system_pb.RebootResponse, error) {
func (srv *GNOIServer) Reboot(ctx context.Context, req *gnoi_system_pb.RebootRequest) (*gnoi_system_pb.RebootResponse, error) {
fileName := common_utils.GNMI_WORK_PATH + "/config_db.json.tmp"

_, err := authenticate(srv.config, ctx)
Expand All @@ -101,7 +174,7 @@ func (srv *Server) Reboot(ctx context.Context, req *gnoi_system_pb.RebootRequest
}

// TODO: Support GNOI RebootStatus
func (srv *Server) RebootStatus(ctx context.Context, req *gnoi_system_pb.RebootStatusRequest) (*gnoi_system_pb.RebootStatusResponse, error) {
func (srv *GNOIServer) RebootStatus(ctx context.Context, req *gnoi_system_pb.RebootStatusRequest) (*gnoi_system_pb.RebootStatusResponse, error) {
_, err := authenticate(srv.config, ctx)
if err != nil {
return nil, err
Expand All @@ -111,15 +184,15 @@ func (srv *Server) RebootStatus(ctx context.Context, req *gnoi_system_pb.RebootS
}

// TODO: Support GNOI CancelReboot
func (srv *Server) CancelReboot(ctx context.Context, req *gnoi_system_pb.CancelRebootRequest) (*gnoi_system_pb.CancelRebootResponse, error) {
func (srv *GNOIServer) CancelReboot(ctx context.Context, req *gnoi_system_pb.CancelRebootRequest) (*gnoi_system_pb.CancelRebootResponse, error) {
_, err := authenticate(srv.config, ctx)
if err != nil {
return nil, err
}
log.V(1).Info("gNOI: CancelReboot")
return nil, status.Errorf(codes.Unimplemented, "")
}
func (srv *Server) Ping(req *gnoi_system_pb.PingRequest, rs gnoi_system_pb.System_PingServer) error {
func (srv *GNOIServer) Ping(req *gnoi_system_pb.PingRequest, rs gnoi_system_pb.System_PingServer) error {
ctx := rs.Context()
_, err := authenticate(srv.config, ctx)
if err != nil {
Expand All @@ -128,7 +201,7 @@ func (srv *Server) Ping(req *gnoi_system_pb.PingRequest, rs gnoi_system_pb.Syste
log.V(1).Info("gNOI: Ping")
return status.Errorf(codes.Unimplemented, "")
}
func (srv *Server) Traceroute(req *gnoi_system_pb.TracerouteRequest, rs gnoi_system_pb.System_TracerouteServer) error {
func (srv *GNOIServer) Traceroute(req *gnoi_system_pb.TracerouteRequest, rs gnoi_system_pb.System_TracerouteServer) error {
ctx := rs.Context()
_, err := authenticate(srv.config, ctx)
if err != nil {
Expand All @@ -137,7 +210,7 @@ func (srv *Server) Traceroute(req *gnoi_system_pb.TracerouteRequest, rs gnoi_sys
log.V(1).Info("gNOI: Traceroute")
return status.Errorf(codes.Unimplemented, "")
}
func (srv *Server) SetPackage(rs gnoi_system_pb.System_SetPackageServer) error {
func (srv *GNOIServer) SetPackage(rs gnoi_system_pb.System_SetPackageServer) error {
ctx := rs.Context()
_, err := authenticate(srv.config, ctx)
if err != nil {
Expand All @@ -146,15 +219,15 @@ func (srv *Server) SetPackage(rs gnoi_system_pb.System_SetPackageServer) error {
log.V(1).Info("gNOI: SetPackage")
return status.Errorf(codes.Unimplemented, "")
}
func (srv *Server) SwitchControlProcessor(ctx context.Context, req *gnoi_system_pb.SwitchControlProcessorRequest) (*gnoi_system_pb.SwitchControlProcessorResponse, error) {
func (srv *GNOIServer) SwitchControlProcessor(ctx context.Context, req *gnoi_system_pb.SwitchControlProcessorRequest) (*gnoi_system_pb.SwitchControlProcessorResponse, error) {
_, err := authenticate(srv.config, ctx)
if err != nil {
return nil, err
}
log.V(1).Info("gNOI: SwitchControlProcessor")
return nil, status.Errorf(codes.Unimplemented, "")
}
func (srv *Server) Time(ctx context.Context, req *gnoi_system_pb.TimeRequest) (*gnoi_system_pb.TimeResponse, error) {
func (srv *GNOIServer) Time(ctx context.Context, req *gnoi_system_pb.TimeRequest) (*gnoi_system_pb.TimeResponse, error) {
_, err := authenticate(srv.config, ctx)
if err != nil {
return nil, err
Expand All @@ -165,7 +238,7 @@ func (srv *Server) Time(ctx context.Context, req *gnoi_system_pb.TimeRequest) (*
return &tm, nil
}

func (srv *Server) Authenticate(ctx context.Context, req *spb_jwt.AuthenticateRequest) (*spb_jwt.AuthenticateResponse, error) {
func (srv *GNOIServer) Authenticate(ctx context.Context, req *spb_jwt.AuthenticateRequest) (*spb_jwt.AuthenticateResponse, error) {
// Can't enforce normal authentication here.. maybe only enforce client cert auth if enabled?
// ctx,err := authenticate(srv.config, ctx)
// if err != nil {
Expand All @@ -191,7 +264,7 @@ func (srv *Server) Authenticate(ctx context.Context, req *spb_jwt.AuthenticateRe
return nil, status.Errorf(codes.PermissionDenied, "Invalid Username or Password")

}
func (srv *Server) Refresh(ctx context.Context, req *spb_jwt.RefreshRequest) (*spb_jwt.RefreshResponse, error) {
func (srv *GNOIServer) Refresh(ctx context.Context, req *spb_jwt.RefreshRequest) (*spb_jwt.RefreshResponse, error) {
ctx, err := authenticate(srv.config, ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -219,7 +292,7 @@ func (srv *Server) Refresh(ctx context.Context, req *spb_jwt.RefreshRequest) (*s

}

func (srv *Server) ClearNeighbors(ctx context.Context, req *spb.ClearNeighborsRequest) (*spb.ClearNeighborsResponse, error) {
func (srv *GNOIServer) ClearNeighbors(ctx context.Context, req *spb.ClearNeighborsRequest) (*spb.ClearNeighborsResponse, error) {
ctx, err := authenticate(srv.config, ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -251,7 +324,7 @@ func (srv *Server) ClearNeighbors(ctx context.Context, req *spb.ClearNeighborsRe
return resp, nil
}

func (srv *Server) CopyConfig(ctx context.Context, req *spb.CopyConfigRequest) (*spb.CopyConfigResponse, error) {
func (srv *GNOIServer) CopyConfig(ctx context.Context, req *spb.CopyConfigRequest) (*spb.CopyConfigResponse, error) {
ctx, err := authenticate(srv.config, ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -282,7 +355,7 @@ func (srv *Server) CopyConfig(ctx context.Context, req *spb.CopyConfigRequest) (
return resp, nil
}

func (srv *Server) ShowTechsupport(ctx context.Context, req *spb.TechsupportRequest) (*spb.TechsupportResponse, error) {
func (srv *GNOIServer) ShowTechsupport(ctx context.Context, req *spb.TechsupportRequest) (*spb.TechsupportResponse, error) {
ctx, err := authenticate(srv.config, ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -314,7 +387,7 @@ func (srv *Server) ShowTechsupport(ctx context.Context, req *spb.TechsupportRequ
return resp, nil
}

func (srv *Server) ImageInstall(ctx context.Context, req *spb.ImageInstallRequest) (*spb.ImageInstallResponse, error) {
func (srv *GNOIServer) ImageInstall(ctx context.Context, req *spb.ImageInstallRequest) (*spb.ImageInstallResponse, error) {
ctx, err := authenticate(srv.config, ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -346,7 +419,7 @@ func (srv *Server) ImageInstall(ctx context.Context, req *spb.ImageInstallReques
return resp, nil
}

func (srv *Server) ImageRemove(ctx context.Context, req *spb.ImageRemoveRequest) (*spb.ImageRemoveResponse, error) {
func (srv *GNOIServer) ImageRemove(ctx context.Context, req *spb.ImageRemoveRequest) (*spb.ImageRemoveResponse, error) {
ctx, err := authenticate(srv.config, ctx)
if err != nil {
return nil, err
Expand All @@ -373,12 +446,10 @@ func (srv *Server) ImageRemove(ctx context.Context, req *spb.ImageRemoveRequest)
if err != nil {
return nil, status.Error(codes.Unknown, err.Error())
}


return resp, nil
}

func (srv *Server) ImageDefault(ctx context.Context, req *spb.ImageDefaultRequest) (*spb.ImageDefaultResponse, error) {
func (srv *GNOIServer) ImageDefault(ctx context.Context, req *spb.ImageDefaultRequest) (*spb.ImageDefaultResponse, error) {
ctx, err := authenticate(srv.config, ctx)
if err != nil {
return nil, err
Expand Down
33 changes: 25 additions & 8 deletions gnmi_server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
gnmipb "github.com/openconfig/gnmi/proto/gnmi"
gnmi_extpb "github.com/openconfig/gnmi/proto/gnmi_ext"
gnoi_system_pb "github.com/openconfig/gnoi/system"
gnoi_file_pb "github.com/openconfig/gnoi/file"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -50,8 +51,18 @@ type Server struct {
// comes from a master controller.
ReqFromMaster func(req *gnmipb.SetRequest, masterEID *uint128) error
masterEID uint128
// UnimplementedSystemServer is embedded to satisfy SystemServer interface requirements
gnoi_system_pb.UnimplementedSystemServer
}

// GNMIServer struct for gNMI specific implementation
type GNMIServer struct {
*Server
}

// GNOIServer struct for gNOI specific implementation
type GNOIServer struct {
*Server
gnoi_system_pb.UnimplementedSystemServer
gnoi_file_pb.UnimplementedFileServer
}

type AuthTypes map[string]bool
Expand Down Expand Up @@ -158,6 +169,11 @@ func NewServer(config *Config, opts []grpc.ServerOption) (*Server, error) {
ReqFromMaster: ReqFromMasterDisabledMA,
masterEID: uint128{High: 0, Low: 0},
}

// Create instances of GNMIServer and GNOIServer
gnmiserver := &GNMIServer{Server: srv}
gnoiserver := &GNOIServer{Server: srv}

var err error
if srv.config.Port < 0 {
srv.config.Port = 0
Expand All @@ -166,15 +182,16 @@ func NewServer(config *Config, opts []grpc.ServerOption) (*Server, error) {
if err != nil {
return nil, fmt.Errorf("failed to open listener port %d: %v", srv.config.Port, err)
}
gnmipb.RegisterGNMIServer(srv.s, srv)
spb_jwt_gnoi.RegisterSonicJwtServiceServer(srv.s, srv)
gnmipb.RegisterGNMIServer(srv.s, gnmiserver)
spb_jwt_gnoi.RegisterSonicJwtServiceServer(srv.s, gnoiserver)
if srv.config.EnableTranslibWrite || srv.config.EnableNativeWrite {
gnoi_system_pb.RegisterSystemServer(srv.s, srv)
gnoi_system_pb.RegisterSystemServer(srv.s, gnoiserver)
gnoi_file_pb.RegisterFileServer(srv.s, gnoiserver)
}
if srv.config.EnableTranslibWrite {
spb_gnoi.RegisterSonicServiceServer(srv.s, srv)
spb_gnoi.RegisterSonicServiceServer(srv.s, gnoiserver)
}
spb_gnoi.RegisterDebugServer(srv.s, srv)
spb_gnoi.RegisterDebugServer(srv.s, gnoiserver)
log.V(1).Infof("Created Server on %s, read-only: %t", srv.Address(), !srv.config.EnableTranslibWrite)
return srv, nil
}
Expand Down Expand Up @@ -334,7 +351,7 @@ func IsNativeOrigin(origin string) bool {
}

// Get implements the Get RPC in gNMI spec.
func (s *Server) Get(ctx context.Context, req *gnmipb.GetRequest) (*gnmipb.GetResponse, error) {
func (s *GNMIServer) Get(ctx context.Context, req *gnmipb.GetRequest) (*gnmipb.GetResponse, error) {
common_utils.IncCounter(common_utils.GNMI_GET)
ctx, err := authenticate(s.config, ctx)
if err != nil {
Expand Down
30 changes: 29 additions & 1 deletion gnoi_client/gnoi_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"google.golang.org/grpc"
gnoi_system_pb "github.com/openconfig/gnoi/system"
gnoi_file_pb "github.com/openconfig/gnoi/file"
spb "github.com/sonic-net/sonic-gnmi/proto/gnoi"
spb_jwt "github.com/sonic-net/sonic-gnmi/proto/gnoi/jwt"
"context"
Expand Down Expand Up @@ -62,6 +63,14 @@ func main() {
default:
panic("Invalid RPC Name")
}
case "File":
fc := gnoi_file_pb.NewFileClient(conn)
switch *rpc {
case "Stat":
fileStat(fc, ctx)
default:
panic("Invalid RPC Name")
}
case "Sonic":
switch *rpc {
case "showtechsupport":
Expand Down Expand Up @@ -125,6 +134,25 @@ func killProcess(sc gnoi_system_pb.SystemClient, ctx context.Context) {
}
}

func fileStat(fc gnoi_file_pb.FileClient, ctx context.Context) {
fmt.Println("File Stat")
ctx = setUserCreds(ctx)
req := &gnoi_file_pb.StatRequest {}
err := json.Unmarshal([]byte(*args), req)
if err != nil {
panic(err.Error())
}
resp,err := fc.Stat(ctx, req)
if err != nil {
panic(err.Error())
}
respstr, err := json.Marshal(resp)
if err != nil {
panic(err.Error())
}
fmt.Println(string(respstr))
}

func systemReboot(sc gnoi_system_pb.SystemClient, ctx context.Context) {
fmt.Println("System Reboot")
ctx = setUserCreds(ctx)
Expand Down Expand Up @@ -321,4 +349,4 @@ func clearNeighbors(sc spb.SonicServiceClient, ctx context.Context) {
panic(err.Error())
}
fmt.Println(string(respstr))
}
}
Loading

0 comments on commit 0fe0971

Please sign in to comment.