diff --git a/gnmi_server/gnoi.go b/gnmi_server/gnoi.go index dd3b39ab1..f8cbb5e28 100644 --- a/gnmi_server/gnoi.go +++ b/gnmi_server/gnoi.go @@ -72,6 +72,28 @@ func ReadFileStat(path string) (*gnoi_file_pb.StatInfo, error) { return statInfo, nil } +func (srv *FileServer) Remove(ctx context.Context, req *gnoi_file_pb.RemoveRequest) (*gnoi_file_pb.RemoveResponse, error) { + ctx, err := authenticate(srv.config, ctx, "gnoi", true) + if err != nil { + return nil, err + } + log.V(1).Info("gNOI: File Remove") + log.V(1).Info("Request: ", req) + + reqstr, err := json.Marshal(req) + if err != nil { + return nil, status.Error(codes.Unknown, err.Error()) + } + + _, err = transutil.TranslProcessAction("/sonic-file-mgmt:remove", []byte(reqstr), ctx) + if err != nil { + return nil, status.Error(codes.Unknown, err.Error()) + } + + resp := &gnoi_file_pb.RemoveResponse{} + return resp, nil +} + func (srv *FileServer) Stat(ctx context.Context, req *gnoi_file_pb.StatRequest) (*gnoi_file_pb.StatResponse, error) { _, err := authenticate(srv.config, ctx, "gnoi", false) if err != nil { diff --git a/gnmi_server/server_test.go b/gnmi_server/server_test.go index e9a0561c0..b02e5bb57 100644 --- a/gnmi_server/server_test.go +++ b/gnmi_server/server_test.go @@ -31,6 +31,7 @@ import ( ssc "github.com/sonic-net/sonic-gnmi/sonic_service_client" "github.com/sonic-net/sonic-gnmi/test_utils" testcert "github.com/sonic-net/sonic-gnmi/testdata/tls" + transutil "github.com/sonic-net/sonic-gnmi/transl_utils" "github.com/go-redis/redis" "github.com/golang/protobuf/proto" @@ -3052,6 +3053,59 @@ func TestGNOI(t *testing.T) { } }) + t.Run("FileRemoveSuccess", func(t *testing.T) { + actualURI := "" + actualPayload := "" + + mock := gomonkey.ApplyFunc(transutil.TranslProcessAction, func(uri string, payload []byte, _ context.Context) ([]byte, error) { + actualURI = uri + actualPayload = string(payload) + return nil, nil + }) + defer mock.Reset() + + ctx := context.Background() + req := &gnoi_file_pb.RemoveRequest{RemoteFile: "/test/path"} + fc := gnoi_file_pb.NewFileClient(conn) + + _, err := fc.Remove(ctx, req) + if err != nil { + t.Fatalf("FileRemove failed: %v", err) + } + + expectedURI := "/sonic-file-mgmt:remove" + if actualURI != expectedURI { + t.Errorf("Expected uri %s but got %s", expectedURI, actualURI) + } + + expectedPayload := `{"remote_file":"/test/path"}` + if actualPayload != expectedPayload { + t.Errorf("Expected payload %s but got %s", expectedPayload, actualPayload) + } + }) + + t.Run("FileRemoveFailure", func(t *testing.T) { + expectedError := fmt.Errorf("failed to remove a file") + + mock := gomonkey.ApplyFunc(transutil.TranslProcessAction, func(_ string, _ []byte, _ context.Context) ([]byte, error) { + return nil, expectedError + }) + defer mock.Reset() + + ctx := context.Background() + req := &gnoi_file_pb.RemoveRequest{RemoteFile: "/test/path"} + fc := gnoi_file_pb.NewFileClient(conn) + + _, err := fc.Remove(ctx, req) + if err == nil { + t.Fatalf("Expected error but got none") + } + + if !strings.Contains(err.Error(), expectedError.Error()) { + t.Errorf("Expected error to contain '%v' but got '%v'", expectedError, err) + } + }) + t.Run("FileStatSuccess", func(t *testing.T) { mockClient := &ssc.DbusClient{} expectedResult := map[string]string{ diff --git a/gnoi_client/file/file.go b/gnoi_client/file/file.go index 464a7a671..f1667916c 100644 --- a/gnoi_client/file/file.go +++ b/gnoi_client/file/file.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + pb "github.com/openconfig/gnoi/file" "github.com/sonic-net/sonic-gnmi/gnoi_client/config" "github.com/sonic-net/sonic-gnmi/gnoi_client/utils" @@ -29,3 +30,23 @@ func Stat(conn *grpc.ClientConn, ctx context.Context) { } fmt.Println(string(respstr)) } + +func Remove(conn *grpc.ClientConn, ctx context.Context) { + fmt.Println("File Remove") + ctx = utils.SetUserCreds(ctx) + fc := pb.NewFileClient(conn) + req := &pb.RemoveRequest{} + err := json.Unmarshal([]byte(*config.Args), req) + if err != nil { + panic(err.Error()) + } + resp, err := fc.Remove(ctx, req) + if err != nil { + panic(err.Error()) + } + respstr, err := json.Marshal(resp) + if err != nil { + panic(err.Error()) + } + fmt.Println(string(respstr)) +} diff --git a/gnoi_client/gnoi_client.go b/gnoi_client/gnoi_client.go index ffa2e1539..99d363950 100644 --- a/gnoi_client/gnoi_client.go +++ b/gnoi_client/gnoi_client.go @@ -50,6 +50,8 @@ func main() { } case "File": switch *config.Rpc { + case "Remove": + file.Remove(conn, ctx) case "Stat": file.Stat(conn, ctx) default: