Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
SuvarnaMeenakshi committed Jan 30, 2025
2 parents bad1f4a + a023991 commit c4fe5c4
Show file tree
Hide file tree
Showing 13 changed files with 461 additions and 85 deletions.
3 changes: 3 additions & 0 deletions common_utils/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const (
DBUS_HALT_SYSTEM
DBUS_IMAGE_DOWNLOAD
DBUS_IMAGE_INSTALL
DBUS_IMAGE_LIST
COUNTER_SIZE
)

Expand Down Expand Up @@ -100,6 +101,8 @@ func (c CounterType) String() string {
return "DBUS image download"
case DBUS_IMAGE_INSTALL:
return "DBUS image install"
case DBUS_IMAGE_LIST:
return "DBUS image list"
default:
return ""
}
Expand Down
76 changes: 59 additions & 17 deletions gnmi_server/gnoi.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
gnoi_system_pb "github.com/openconfig/gnoi/system"
gnoi_file_pb "github.com/openconfig/gnoi/file"
gnoi_os_pb "github.com/openconfig/gnoi/os"
log "github.com/golang/glog"
"time"
spb "github.com/sonic-net/sonic-gnmi/proto/gnoi"
Expand Down Expand Up @@ -115,6 +116,47 @@ func KillOrRestartProcess(restart bool, serviceName string) error {
return err
}

func (srv *OSServer) Verify(ctx context.Context, req *gnoi_os_pb.VerifyRequest) (*gnoi_os_pb.VerifyResponse, error) {
_, err := authenticate(srv.config, ctx, false)
if err != nil {
log.V(2).Infof("Failed to authenticate: %v", err)
return nil, err
}

log.V(1).Info("gNOI: Verify")
dbus, err := ssc.NewDbusClient()
if err != nil {
log.V(2).Infof("Failed to create dbus client: %v", err)
return nil, err
}

image_json, err := dbus.ListImages()
if err != nil {
log.V(2).Infof("Failed to list images: %v", err)
return nil, err
}

images := make(map[string]interface{})
err = json.Unmarshal([]byte(image_json), &images)
if err != nil {
log.V(2).Infof("Failed to unmarshal images: %v", err)
return nil, err
}

current, exists := images["current"]
if !exists {
return nil, status.Errorf(codes.Internal, "Key 'current' not found in images")
}
current_image, ok := current.(string)
if !ok {
return nil, status.Errorf(codes.Internal, "Failed to assert current image as string")
}
resp := &gnoi_os_pb.VerifyResponse{
Version: current_image,
}
return resp, nil
}

func (srv *SystemServer) KillProcess(ctx context.Context, req *gnoi_system_pb.KillProcessRequest) (*gnoi_system_pb.KillProcessResponse, error) {
_, err := authenticate(srv.config, ctx, true)
if err != nil {
Expand Down Expand Up @@ -286,7 +328,7 @@ func (srv *Server) Authenticate(ctx context.Context, req *spb_jwt.AuthenticateRe
return &spb_jwt.AuthenticateResponse{Token: tokenResp(req.Username, roles)}, nil
}
}

}
return nil, status.Errorf(codes.PermissionDenied, "Invalid Username or Password")

Expand Down Expand Up @@ -314,7 +356,7 @@ func (srv *Server) Refresh(ctx context.Context, req *spb_jwt.RefreshRequest) (*s
if time.Unix(claims.ExpiresAt, 0).Sub(time.Now()) > JwtRefreshInt {
return nil, status.Errorf(codes.InvalidArgument, "Invalid JWT Token")
}

return &spb_jwt.RefreshResponse{Token: tokenResp(claims.Username, claims.Roles)}, nil

}
Expand Down Expand Up @@ -357,13 +399,13 @@ func (srv *Server) CopyConfig(ctx context.Context, req *spb.CopyConfigRequest) (
return nil, err
}
log.V(1).Info("gNOI: Sonic CopyConfig")

resp := &spb.CopyConfigResponse{
Output: &spb.SonicOutput {

},
}

reqstr, err := json.Marshal(req)
if err != nil {
return nil, status.Error(codes.Unknown, err.Error())
Expand All @@ -373,12 +415,12 @@ func (srv *Server) CopyConfig(ctx context.Context, req *spb.CopyConfigRequest) (
if err != nil {
return nil, status.Error(codes.Unknown, err.Error())
}

err = json.Unmarshal(jsresp, resp)
if err != nil {
return nil, status.Error(codes.Unknown, err.Error())
}

return resp, nil
}

Expand All @@ -388,7 +430,7 @@ func (srv *Server) ShowTechsupport(ctx context.Context, req *spb.TechsupportRequ
return nil, err
}
log.V(1).Info("gNOI: Sonic ShowTechsupport")

resp := &spb.TechsupportResponse{
Output: &spb.TechsupportResponse_Output {

Expand All @@ -404,13 +446,13 @@ func (srv *Server) ShowTechsupport(ctx context.Context, req *spb.TechsupportRequ
if err != nil {
return nil, status.Error(codes.Unknown, err.Error())
}

err = json.Unmarshal(jsresp, resp)
if err != nil {
return nil, status.Error(codes.Unknown, err.Error())
}


return resp, nil
}

Expand All @@ -420,7 +462,7 @@ func (srv *Server) ImageInstall(ctx context.Context, req *spb.ImageInstallReques
return nil, err
}
log.V(1).Info("gNOI: Sonic ImageInstall")

resp := &spb.ImageInstallResponse{
Output: &spb.SonicOutput {

Expand All @@ -436,13 +478,13 @@ func (srv *Server) ImageInstall(ctx context.Context, req *spb.ImageInstallReques
if err != nil {
return nil, status.Error(codes.Unknown, err.Error())
}

err = json.Unmarshal(jsresp, resp)
if err != nil {
return nil, status.Error(codes.Unknown, err.Error())
}


return resp, nil
}

Expand All @@ -452,7 +494,7 @@ func (srv *Server) ImageRemove(ctx context.Context, req *spb.ImageRemoveRequest)
return nil, err
}
log.V(1).Info("gNOI: Sonic ImageRemove")

resp := &spb.ImageRemoveResponse{
Output: &spb.SonicOutput {

Expand All @@ -468,7 +510,7 @@ func (srv *Server) ImageRemove(ctx context.Context, req *spb.ImageRemoveRequest)
if err != nil {
return nil, status.Error(codes.Unknown, err.Error())
}

err = json.Unmarshal(jsresp, resp)
if err != nil {
return nil, status.Error(codes.Unknown, err.Error())
Expand All @@ -482,7 +524,7 @@ func (srv *Server) ImageDefault(ctx context.Context, req *spb.ImageDefaultReques
return nil, err
}
log.V(1).Info("gNOI: Sonic ImageDefault")

resp := &spb.ImageDefaultResponse{
Output: &spb.SonicOutput {

Expand All @@ -504,6 +546,6 @@ func (srv *Server) ImageDefault(ctx context.Context, req *spb.ImageDefaultReques
return nil, status.Error(codes.Unknown, err.Error())
}


return resp, nil
}
11 changes: 11 additions & 0 deletions gnmi_server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
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"
gnoi_os_pb "github.com/openconfig/gnoi/os"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -70,6 +71,14 @@ type SystemServer struct {
gnoi_system_pb.UnimplementedSystemServer
}

// OSServer is the server API for System service.
// All implementations must embed UnimplementedSystemServer
// for forward compatibility
type OSServer struct {
*Server
gnoi_os_pb.UnimplementedOSServer
}

type AuthTypes map[string]bool

// Config is a collection of values for Server
Expand Down Expand Up @@ -180,6 +189,7 @@ func NewServer(config *Config, opts []grpc.ServerOption) (*Server, error) {

fileSrv := &FileServer{Server: srv}
systemSrv := &SystemServer{Server: srv}
osSrv := &OSServer{Server: srv}

var err error
if srv.config.Port < 0 {
Expand All @@ -194,6 +204,7 @@ func NewServer(config *Config, opts []grpc.ServerOption) (*Server, error) {
if srv.config.EnableTranslibWrite || srv.config.EnableNativeWrite {
gnoi_system_pb.RegisterSystemServer(srv.s, systemSrv)
gnoi_file_pb.RegisterFileServer(srv.s, fileSrv)
gnoi_os_pb.RegisterOSServer(srv.s, osSrv)
}
if srv.config.EnableTranslibWrite {
spb_gnoi.RegisterSonicServiceServer(srv.s, srv)
Expand Down
Loading

0 comments on commit c4fe5c4

Please sign in to comment.