From f2328d53716d98211d151a8b334aed703f855b1d Mon Sep 17 00:00:00 2001 From: Teja Tamboli Date: Sat, 14 Feb 2026 00:33:34 +0000 Subject: [PATCH] Added support to remove UDF --- dataplane/saiserver/udf.go | 35 +++++++++++++++++++ dataplane/saiserver/udf_test.go | 62 +++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/dataplane/saiserver/udf.go b/dataplane/saiserver/udf.go index b4a91e152..e7eedb6a0 100644 --- a/dataplane/saiserver/udf.go +++ b/dataplane/saiserver/udf.go @@ -65,3 +65,38 @@ func (u *udf) CreateUdfMatch(context.Context, *saipb.CreateUdfMatchRequest) (*sa Oid: u.mgr.NextID(), }, nil } + +func (u *udf) RemoveUdf(ctx context.Context, req *saipb.RemoveUdfRequest) (*saipb.RemoveUdfResponse, error) { + groupIDAttr := u.mgr.GetAttribute(fmt.Sprint(req.GetOid()), int32(saipb.UdfAttr_UDF_ATTR_GROUP_ID)) + if groupIDAttr == nil { + return &saipb.RemoveUdfResponse{}, nil + } + groupID := groupIDAttr.(uint64) + udfGroup := &saipb.UdfGroupAttribute{} + if err := u.mgr.PopulateAllAttributes(fmt.Sprint(groupID), udfGroup); err != nil { + return nil, err + } + newList := []uint64{} + found := false + for _, id := range udfGroup.UdfList { + if id != req.GetOid() { + newList = append(newList, id) + } else { + found = true + } + } + if found { + udfGroup.UdfList = newList + u.mgr.StoreAttributes(groupID, udfGroup) + } + + return &saipb.RemoveUdfResponse{}, nil +} + +func (u *udf) RemoveUdfGroup(context.Context, *saipb.RemoveUdfGroupRequest) (*saipb.RemoveUdfGroupResponse, error) { + return &saipb.RemoveUdfGroupResponse{}, nil +} + +func (u *udf) RemoveUdfMatch(context.Context, *saipb.RemoveUdfMatchRequest) (*saipb.RemoveUdfMatchResponse, error) { + return &saipb.RemoveUdfMatchResponse{}, nil +} diff --git a/dataplane/saiserver/udf_test.go b/dataplane/saiserver/udf_test.go index 851ab0833..9b8486b05 100644 --- a/dataplane/saiserver/udf_test.go +++ b/dataplane/saiserver/udf_test.go @@ -73,6 +73,68 @@ func TestCreateUdf(t *testing.T) { } } +func TestRemoveUdf(t *testing.T) { + tests := []struct { + desc string + wantErr string + req *saipb.RemoveUdfRequest + setup func(*attrmgr.AttrMgr) + wantGroup []uint64 + }{{ + desc: "remove from list", + req: &saipb.RemoveUdfRequest{ + Oid: 1, + }, + setup: func(mgr *attrmgr.AttrMgr) { + mgr.StoreAttributes(10, &saipb.UdfGroupAttribute{ + UdfList: []uint64{1, 2, 3}, + }) + mgr.StoreAttributes(1, &saipb.CreateUdfRequest{ + GroupId: proto.Uint64(10), + }) + }, + wantGroup: []uint64{2, 3}, + }, { + desc: "remove not found", + req: &saipb.RemoveUdfRequest{ + Oid: 4, + }, + setup: func(mgr *attrmgr.AttrMgr) { + mgr.StoreAttributes(10, &saipb.UdfGroupAttribute{ + UdfList: []uint64{1, 2, 3}, + }) + mgr.StoreAttributes(4, &saipb.CreateUdfRequest{ + GroupId: proto.Uint64(10), + }) + }, + wantGroup: []uint64{1, 2, 3}, + }} + for _, tt := range tests { + t.Run(tt.desc, func(t *testing.T) { + dplane := &fakeSwitchDataplane{} + c, mgr, stopFn := newTestUdf(t, dplane) + defer stopFn() + if tt.setup != nil { + tt.setup(mgr) + } + _, gotErr := c.RemoveUdf(context.Background(), tt.req) + if diff := errdiff.Check(gotErr, tt.wantErr); diff != "" { + t.Fatalf("RemoveUdf() unexpected err: %s", diff) + } + if gotErr != nil { + return + } + gr := &saipb.UdfGroupAttribute{} + if err := mgr.PopulateAllAttributes("10", gr); err != nil { + t.Fatal(err) + } + if slices.Compare(gr.UdfList, tt.wantGroup) != 0 { + t.Errorf("invalid group got %v, want: %v", gr.UdfList, tt.wantGroup) + } + }) + } +} + func newTestUdf(t testing.TB, api switchDataplaneAPI) (saipb.UdfClient, *attrmgr.AttrMgr, func()) { conn, mgr, stopFn := newTestServer(t, func(mgr *attrmgr.AttrMgr, srv *grpc.Server) { newUdf(mgr, api, srv)