Skip to content

Commit

Permalink
gNOI System APIs (Reboot, RebootStatus, CancelReboot) changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ndas7 committed Dec 11, 2024
1 parent bfef03a commit d2bcd6c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 49 deletions.
43 changes: 6 additions & 37 deletions gnmi_server/gnoi_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ const (
notificationTimeout = 10 * time.Second
)

// Vaild reboot method map.
var validRebootMap = map[syspb.RebootMethod]bool{
// Supported reboot method map.
var supportedRebootMap = map[syspb.RebootMethod]bool{
syspb.RebootMethod_COLD: true,
syspb.RebootMethod_WARM: true,
syspb.RebootMethod_POWERDOWN: true,
syspb.RebootMethod_NSF: true,
}

// Validates reboot request.
func validRebootReq(req *syspb.RebootRequest) error {
if _, ok := validRebootMap[req.GetMethod()]; !ok {
func ValidRebootReq(req *syspb.RebootRequest) error {
if _, ok := supportedRebootMap[req.GetMethod()]; !ok {
log.Error("Invalid request: reboot method is not supported.")
return fmt.Errorf("Invalid request: reboot method is not supported.")
}
Expand Down Expand Up @@ -125,37 +125,6 @@ func processMsgPayload(pload string) (string, string, map[string]string, error)
return op, data, fvs, nil
}

// Converts a SWSS error code string into a gRPC code.
func swssToErrorCode(statusStr string) codes.Code {
switch statusStr {
case "SWSS_RC_SUCCESS":
return codes.OK
case "SWSS_RC_UNKNOWN":
return codes.Unknown
case "SWSS_RC_IN_USE", "SWSS_RC_INVALID_PARAM":
return codes.InvalidArgument
case "SWSS_RC_DEADLINE_EXCEEDED":
return codes.DeadlineExceeded
case "SWSS_RC_NOT_FOUND":
return codes.NotFound
case "SWSS_RC_EXISTS":
return codes.AlreadyExists
case "SWSS_RC_PERMISSION_DENIED":
return codes.PermissionDenied
case "SWSS_RC_FULL", "SWSS_RC_NO_MEMORY":
return codes.ResourceExhausted
case "SWSS_RC_UNIMPLEMENTED":
return codes.Unimplemented
case "SWSS_RC_INTERNAL":
return codes.Internal
case "SWSS_RC_NOT_EXECUTED", "SWSS_RC_FAILED_PRECONDITION":
return codes.FailedPrecondition
case "SWSS_RC_UNAVAIL":
return codes.Unavailable
}
return codes.Internal
}

func sendRebootReqOnNotifCh(ctx context.Context, req proto.Message, sc *redis.Client, rebootNotifKey string) (resp proto.Message, err error, msgDataStr string) {
np, err := common_utils.NewNotificationProducer(rebootReqCh)
if err != nil {
Expand Down Expand Up @@ -219,7 +188,7 @@ func sendRebootReqOnNotifCh(ctx context.Context, req proto.Message, sc *redis.Cl
msgDataStr = fvs[dataMsgFld]
}
}
if swssCode := swssToErrorCode(data); swssCode != codes.OK {
if swssCode := SwssToErrorCode(data); swssCode != codes.OK {
errStr := fmt.Sprintf("Response Notification returned SWSS Error code: %v, error = %v", swssCode, msgDataStr)
log.V(1).Infof(errStr)
return nil, status.Errorf(swssCode, errStr), msgDataStr
Expand All @@ -244,7 +213,7 @@ func (srv *Server) Reboot(ctx context.Context, req *syspb.RebootRequest) (*syspb
log.V(1).Info("Error while authenticating: ", err.Error())
}
log.V(2).Info("gNOI: Reboot")
if err := validRebootReq(req); err != nil {
if err := ValidRebootReq(req); err != nil {
return nil, status.Errorf(codes.InvalidArgument, err.Error())
}
// Initialize State DB.
Expand Down
24 changes: 12 additions & 12 deletions gnmi_server/gnoi_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,35 +63,35 @@ func errorCodeToSwss(errCode codes.Code) string {
return ""
}

func rebootBackendResponse(t *testing.T, sc *redis.Client, expectedResponse codes.Code, fvs map[string]string, done chan bool, key string) {
func RebootBackendResponse(t *testing.T, sc *redis.Client, expectedResponse codes.Code, fvs map[string]string, done chan bool, key string) {
sub := sc.Subscribe("Reboot_Request_Channel")
if _, err := sub.Receive(); err != nil {
t.Errorf("rebootBackendResponse failed to subscribe to request channel: %v", err)
t.Errorf("RebootBackendResponse failed to subscribe to request channel: %v", err)
return
}
defer sub.Close()
channel := sub.Channel()

np, err := common_utils.NewNotificationProducer("Reboot_Response_Channel")
if err != nil {
t.Errorf("rebootBackendResponse failed to create notification producer: %v", err)
t.Errorf("RebootBackendResponse failed to create notification producer: %v", err)
return
}
defer np.Close()

tc := time.After(5 * time.Second)
select {
case msg := <-channel:
t.Logf("rebootBackendResponse received request: %v", msg)
t.Logf("RebootBackendResponse received request: %v", msg)
// Respond to the request
if err := np.Send(key, errorCodeToSwss(expectedResponse), fvs); err != nil {
t.Errorf("rebootBackendResponse failed to send response: %v", err)
t.Errorf("RebootBackendResponse failed to send response: %v", err)
return
}
case <-done:
return
case <-tc:
t.Error("rebootBackendResponse timed out waiting for request")
t.Error("RebootBackendResponse timed out waiting for request")
return
}
}
Expand Down Expand Up @@ -200,7 +200,7 @@ func TestSystem(t *testing.T) {
Delay: 0,
Message: "Cold reboot starting ...",
}
go rebootBackendResponse(t, rclient, codes.OK, fvs, done, "testKey")
go RebootBackendResponse(t, rclient, codes.OK, fvs, done, "testKey")
defer func() { done <- true }()
_, err := sc.Reboot(ctx, req)
testErr(err, codes.Internal, "Op: testKey doesn't match for Reboot!", t)
Expand All @@ -211,7 +211,7 @@ func TestSystem(t *testing.T) {
fvs["MESSAGE"] = "{}"
// Start goroutine for mock Reboot Backend to respond to Reboot requests
done := make(chan bool, 1)
go rebootBackendResponse(t, rclient, code, fvs, done, rebootKey)
go RebootBackendResponse(t, rclient, code, fvs, done, rebootKey)
defer func() { done <- true }()
req := &syspb.RebootRequest{
Method: syspb.RebootMethod_COLD,
Expand All @@ -227,7 +227,7 @@ func TestSystem(t *testing.T) {
fvs["MESSAGE"] = "{}"
// Start goroutine for mock Reboot Backend to respond to Reboot requests
done := make(chan bool, 1)
go rebootBackendResponse(t, rclient, codes.Unauthenticated, fvs, done, rebootKey)
go RebootBackendResponse(t, rclient, codes.Unauthenticated, fvs, done, rebootKey)
defer func() { done <- true }()
req := &syspb.RebootRequest{
Method: syspb.RebootMethod_COLD,
Expand All @@ -242,7 +242,7 @@ func TestSystem(t *testing.T) {
done := make(chan bool, 1)
fvs := make(map[string]string)
fvs["MESSAGE"] = "{}"
go rebootBackendResponse(t, rclient, codes.OK, fvs, done, rebootKey)
go RebootBackendResponse(t, rclient, codes.OK, fvs, done, rebootKey)
defer func() { done <- true }()

req := &syspb.RebootRequest{
Expand All @@ -264,7 +264,7 @@ func TestSystem(t *testing.T) {
done := make(chan bool, 1)
fvs := make(map[string]string)
fvs["MESSAGE"] = "{\"active\": true, \"method\":\"NSF\",\"status\":{\"status\":\"STATUS_SUCCESS\"}}"
go rebootBackendResponse(t, rclient, codes.OK, fvs, done, rebootStatusKey)
go RebootBackendResponse(t, rclient, codes.OK, fvs, done, rebootStatusKey)
defer func() { done <- true }()

_, err := sc.RebootStatus(ctx, &syspb.RebootStatusRequest{})
Expand All @@ -288,7 +288,7 @@ func TestSystem(t *testing.T) {
done := make(chan bool, 1)
fvs := make(map[string]string)
fvs["MESSAGE"] = "{}"
go rebootBackendResponse(t, rclient, codes.OK, fvs, done, rebootCancelKey)
go RebootBackendResponse(t, rclient, codes.OK, fvs, done, rebootCancelKey)
defer func() { done <- true }()

req := &syspb.CancelRebootRequest{
Expand Down
36 changes: 36 additions & 0 deletions gnmi_server/swss_util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package gnmi

import (
"google.golang.org/grpc/codes"
)

// Converts a SWSS error code string into a gRPC code.
func SwssToErrorCode(statusStr string) codes.Code {
switch statusStr {
case "SWSS_RC_SUCCESS":
return codes.OK
case "SWSS_RC_UNKNOWN":
return codes.Unknown
case "SWSS_RC_IN_USE", "SWSS_RC_INVALID_PARAM":
return codes.InvalidArgument
case "SWSS_RC_DEADLINE_EXCEEDED":
return codes.DeadlineExceeded
case "SWSS_RC_NOT_FOUND":
return codes.NotFound
case "SWSS_RC_EXISTS":
return codes.AlreadyExists
case "SWSS_RC_PERMISSION_DENIED":
return codes.PermissionDenied
case "SWSS_RC_FULL", "SWSS_RC_NO_MEMORY":
return codes.ResourceExhausted
case "SWSS_RC_UNIMPLEMENTED":
return codes.Unimplemented
case "SWSS_RC_INTERNAL":
return codes.Internal
case "SWSS_RC_NOT_EXECUTED", "SWSS_RC_FAILED_PRECONDITION":
return codes.FailedPrecondition
case "SWSS_RC_UNAVAIL":
return codes.Unavailable
}
return codes.Internal
}

0 comments on commit d2bcd6c

Please sign in to comment.