Skip to content

Commit cb57cb1

Browse files
authored
Add BridgePort API stubs (#628)
* Add stub implementation for bridge port APIs * Fix AdminState type mismatch in CreateBridgePort * Update BUILD file * Update copyright year
1 parent 5dbbbeb commit cb57cb1

3 files changed

Lines changed: 170 additions & 3 deletions

File tree

dataplane/saiserver/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ go_test(
4242
name = "saiserver_test",
4343
srcs = [
4444
"acl_test.go",
45+
"bridge_test.go",
4546
"hostif_test.go",
4647
"l2mc_test.go",
4748
"mirror_test.go",

dataplane/saiserver/bridge_test.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package saiserver
16+
17+
import (
18+
"context"
19+
"testing"
20+
21+
"google.golang.org/grpc"
22+
"google.golang.org/protobuf/proto"
23+
"google.golang.org/protobuf/testing/protocmp"
24+
25+
"github.com/google/go-cmp/cmp"
26+
"github.com/openconfig/gnmi/errdiff"
27+
28+
"github.com/openconfig/lemming/dataplane/saiserver/attrmgr"
29+
30+
saipb "github.com/openconfig/lemming/dataplane/proto/sai"
31+
)
32+
33+
func TestBridgePort(t *testing.T) {
34+
tests := []struct {
35+
desc string
36+
req *saipb.CreateBridgePortRequest
37+
want *saipb.CreateBridgePortResponse
38+
wantAttr *saipb.BridgePortAttribute
39+
wantErr string
40+
}{{
41+
desc: "success",
42+
req: &saipb.CreateBridgePortRequest{
43+
Switch: 1,
44+
Type: saipb.BridgePortType_BRIDGE_PORT_TYPE_PORT.Enum(),
45+
PortId: proto.Uint64(10),
46+
FdbLearningMode: saipb.BridgePortFdbLearningMode_BRIDGE_PORT_FDB_LEARNING_MODE_DISABLE.Enum(),
47+
AdminState: proto.Bool(true),
48+
},
49+
want: &saipb.CreateBridgePortResponse{
50+
Oid: 1,
51+
},
52+
wantAttr: &saipb.BridgePortAttribute{
53+
Type: saipb.BridgePortType_BRIDGE_PORT_TYPE_PORT.Enum(),
54+
PortId: proto.Uint64(10),
55+
FdbLearningMode: saipb.BridgePortFdbLearningMode_BRIDGE_PORT_FDB_LEARNING_MODE_DISABLE.Enum(),
56+
AdminState: proto.Bool(true),
57+
},
58+
}}
59+
for _, tt := range tests {
60+
t.Run(tt.desc, func(t *testing.T) {
61+
conn, mgr, stopFn := newTestBridge(t)
62+
defer stopFn()
63+
c := saipb.NewBridgeClient(conn)
64+
65+
got, gotErr := c.CreateBridgePort(context.TODO(), tt.req)
66+
if diff := errdiff.Check(gotErr, tt.wantErr); diff != "" {
67+
t.Fatalf("CreateBridgePort() unexpected err: %s w/ error %v", diff, gotErr)
68+
}
69+
if gotErr != nil {
70+
return
71+
}
72+
if d := cmp.Diff(got, tt.want, protocmp.Transform()); d != "" {
73+
t.Errorf("CreateBridgePort() failed: diff(-got,+want)\n:%s", d)
74+
}
75+
attr := &saipb.BridgePortAttribute{}
76+
if err := mgr.PopulateAllAttributes("1", attr); err != nil {
77+
t.Fatal(err)
78+
}
79+
if d := cmp.Diff(attr, tt.wantAttr, protocmp.Transform()); d != "" {
80+
t.Errorf("CreateBridgePort() failed: diff(-got,+want)\n:%s", d)
81+
}
82+
83+
// Test Set Attribute
84+
_, err := c.SetBridgePortAttribute(context.TODO(), &saipb.SetBridgePortAttributeRequest{
85+
Oid: got.Oid,
86+
AdminState: proto.Bool(false),
87+
})
88+
if err != nil {
89+
t.Fatalf("SetBridgePortAttribute() failed: %v", err)
90+
}
91+
if err := mgr.PopulateAllAttributes("1", attr); err != nil {
92+
t.Fatal(err)
93+
}
94+
if attr.GetAdminState() != false {
95+
t.Errorf("SetBridgePortAttribute() failed: got %v, want false", attr.GetAdminState())
96+
}
97+
98+
// Test Get Attribute
99+
resp, err := c.GetBridgePortAttribute(context.TODO(), &saipb.GetBridgePortAttributeRequest{
100+
Oid: got.Oid,
101+
AttrType: []saipb.BridgePortAttr{saipb.BridgePortAttr_BRIDGE_PORT_ATTR_ADMIN_STATE},
102+
})
103+
if err != nil {
104+
t.Fatalf("GetBridgePortAttribute() failed: %v", err)
105+
}
106+
if resp.GetAttr().GetAdminState() != false {
107+
t.Errorf("GetBridgePortAttribute() failed: got %v, want false", resp.GetAttr().GetAdminState())
108+
}
109+
// Test Remove
110+
if _, err := c.RemoveBridgePort(context.TODO(), &saipb.RemoveBridgePortRequest{Oid: got.Oid}); err != nil {
111+
t.Fatalf("RemoveBridgePort() failed: %v", err)
112+
}
113+
})
114+
}
115+
}
116+
117+
func newTestBridge(t testing.TB) (grpc.ClientConnInterface, *attrmgr.AttrMgr, func()) {
118+
conn, mgr, stopFn := newTestServer(t, func(mgr *attrmgr.AttrMgr, srv *grpc.Server) {
119+
newBridge(mgr, nil, srv)
120+
})
121+
return conn, mgr, stopFn
122+
}

dataplane/saiserver/routing.go

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,20 +1147,64 @@ func newBridge(mgr *attrmgr.AttrMgr, dataplane switchDataplaneAPI, s *grpc.Serve
11471147
return b
11481148
}
11491149

1150-
func (br *bridge) CreateBridge(context.Context, *saipb.CreateBridgeRequest) (*saipb.CreateBridgeResponse, error) {
1151-
id := br.mgr.NextID()
1150+
func (b *bridge) CreateBridge(ctx context.Context, req *saipb.CreateBridgeRequest) (*saipb.CreateBridgeResponse, error) {
1151+
id := b.mgr.NextID()
11521152
attrs := &saipb.BridgeAttribute{
11531153
PortList: []uint64{},
11541154
UnknownUnicastFloodGroup: proto.Uint64(0),
11551155
UnknownMulticastFloodGroup: proto.Uint64(0),
11561156
BroadcastFloodGroup: proto.Uint64(0),
11571157
}
1158-
br.mgr.StoreAttributes(id, attrs)
1158+
b.mgr.StoreAttributes(id, attrs)
11591159
return &saipb.CreateBridgeResponse{
11601160
Oid: id,
11611161
}, nil
11621162
}
11631163

1164+
func (b *bridge) RemoveBridge(ctx context.Context, req *saipb.RemoveBridgeRequest) (*saipb.RemoveBridgeResponse, error) {
1165+
return &saipb.RemoveBridgeResponse{}, nil
1166+
}
1167+
1168+
func (b *bridge) SetBridgeAttribute(ctx context.Context, req *saipb.SetBridgeAttributeRequest) (*saipb.SetBridgeAttributeResponse, error) {
1169+
return &saipb.SetBridgeAttributeResponse{}, nil
1170+
}
1171+
1172+
func (b *bridge) GetBridgeAttribute(ctx context.Context, req *saipb.GetBridgeAttributeRequest) (*saipb.GetBridgeAttributeResponse, error) {
1173+
return &saipb.GetBridgeAttributeResponse{}, nil
1174+
}
1175+
1176+
func (b *bridge) GetBridgeStats(ctx context.Context, req *saipb.GetBridgeStatsRequest) (*saipb.GetBridgeStatsResponse, error) {
1177+
return &saipb.GetBridgeStatsResponse{}, nil
1178+
}
1179+
1180+
func (b *bridge) CreateBridgePort(ctx context.Context, req *saipb.CreateBridgePortRequest) (*saipb.CreateBridgePortResponse, error) {
1181+
oid := b.mgr.NextID()
1182+
adminState := req.GetAdminState()
1183+
attrs := &saipb.BridgePortAttribute{
1184+
AdminState: proto.Bool(adminState),
1185+
}
1186+
b.mgr.StoreAttributes(oid, attrs)
1187+
return &saipb.CreateBridgePortResponse{
1188+
Oid: oid,
1189+
}, nil
1190+
}
1191+
1192+
func (b *bridge) RemoveBridgePort(ctx context.Context, req *saipb.RemoveBridgePortRequest) (*saipb.RemoveBridgePortResponse, error) {
1193+
return &saipb.RemoveBridgePortResponse{}, nil
1194+
}
1195+
1196+
func (b *bridge) SetBridgePortAttribute(ctx context.Context, req *saipb.SetBridgePortAttributeRequest) (*saipb.SetBridgePortAttributeResponse, error) {
1197+
return &saipb.SetBridgePortAttributeResponse{}, nil
1198+
}
1199+
1200+
func (b *bridge) GetBridgePortAttribute(ctx context.Context, req *saipb.GetBridgePortAttributeRequest) (*saipb.GetBridgePortAttributeResponse, error) {
1201+
return &saipb.GetBridgePortAttributeResponse{}, nil
1202+
}
1203+
1204+
func (b *bridge) GetBridgePortStats(ctx context.Context, req *saipb.GetBridgePortStatsRequest) (*saipb.GetBridgePortStatsResponse, error) {
1205+
return &saipb.GetBridgePortStatsResponse{}, nil
1206+
}
1207+
11641208
type hash struct {
11651209
saipb.UnimplementedHashServer
11661210
mgr *attrmgr.AttrMgr

0 commit comments

Comments
 (0)