diff --git a/pkg/evpn/bridge.go b/pkg/evpn/bridge.go index 96986377..17d76a81 100644 --- a/pkg/evpn/bridge.go +++ b/pkg/evpn/bridge.go @@ -22,7 +22,6 @@ import ( "go.einride.tech/aip/resourcename" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/known/emptypb" ) @@ -94,7 +93,7 @@ func (s *Server) CreateLogicalBridge(_ context.Context, in *pb.CreateLogicalBrid } // TODO: bridge link set dev vxlan- neigh_suppress on } - response := proto.Clone(in.LogicalBridge).(*pb.LogicalBridge) + response := protoClone(in.LogicalBridge) response.Status = &pb.LogicalBridgeStatus{OperStatus: pb.LBOperStatus_LB_OPER_STATUS_UP} s.Bridges[in.LogicalBridge.Name] = response log.Printf("CreateLogicalBridge: Sending to client: %v", response) @@ -198,7 +197,7 @@ func (s *Server) UpdateLogicalBridge(_ context.Context, in *pb.UpdateLogicalBrid return nil, err } } - response := proto.Clone(in.LogicalBridge).(*pb.LogicalBridge) + response := protoClone(in.LogicalBridge) response.Status = &pb.LogicalBridgeStatus{OperStatus: pb.LBOperStatus_LB_OPER_STATUS_UP} s.Bridges[in.LogicalBridge.Name] = response log.Printf("UpdateLogicalBridge: Sending to client: %v", response) diff --git a/pkg/evpn/bridge_test.go b/pkg/evpn/bridge_test.go index 4c3dd35f..55e7e4e9 100644 --- a/pkg/evpn/bridge_test.go +++ b/pkg/evpn/bridge_test.go @@ -192,11 +192,11 @@ func Test_CreateLogicalBridge(t *testing.T) { client := pb.NewLogicalBridgeServiceClient(conn) if tt.exist { - opi.Bridges[testLogicalBridgeName] = proto.Clone(&testLogicalBridge).(*pb.LogicalBridge) + opi.Bridges[testLogicalBridgeName] = protoClone(&testLogicalBridge) opi.Bridges[testLogicalBridgeName].Name = testLogicalBridgeName } if tt.out != nil { - tt.out = proto.Clone(tt.out).(*pb.LogicalBridge) + tt.out = protoClone(tt.out) tt.out.Name = testLogicalBridgeName } @@ -325,7 +325,7 @@ func Test_DeleteLogicalBridge(t *testing.T) { client := pb.NewLogicalBridgeServiceClient(conn) fname1 := resourceIDToFullName("bridges", tt.in) - opi.Bridges[testLogicalBridgeName] = proto.Clone(&testLogicalBridge).(*pb.LogicalBridge) + opi.Bridges[testLogicalBridgeName] = protoClone(&testLogicalBridge) request := &pb.DeleteLogicalBridgeRequest{Name: fname1, AllowMissing: tt.missing} response, err := client.DeleteLogicalBridge(ctx, request) @@ -414,11 +414,11 @@ func Test_UpdateLogicalBridge(t *testing.T) { client := pb.NewLogicalBridgeServiceClient(conn) if tt.exist { - opi.Bridges[testLogicalBridgeName] = proto.Clone(&testLogicalBridge).(*pb.LogicalBridge) + opi.Bridges[testLogicalBridgeName] = protoClone(&testLogicalBridge) opi.Bridges[testLogicalBridgeName].Name = testLogicalBridgeName } if tt.out != nil { - tt.out = proto.Clone(tt.out).(*pb.LogicalBridge) + tt.out = protoClone(tt.out) tt.out.Name = testLogicalBridgeName } @@ -494,7 +494,7 @@ func Test_GetLogicalBridge(t *testing.T) { }(conn) client := pb.NewLogicalBridgeServiceClient(conn) - opi.Bridges[testLogicalBridgeID] = proto.Clone(&testLogicalBridge).(*pb.LogicalBridge) + opi.Bridges[testLogicalBridgeID] = protoClone(&testLogicalBridge) request := &pb.GetLogicalBridgeRequest{Name: tt.in} response, err := client.GetLogicalBridge(ctx, request) diff --git a/pkg/evpn/evpn.go b/pkg/evpn/evpn.go index ff09a702..b8e38b81 100644 --- a/pkg/evpn/evpn.go +++ b/pkg/evpn/evpn.go @@ -10,6 +10,8 @@ import ( "fmt" "log" + "google.golang.org/protobuf/proto" + pe "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go" "github.com/opiproject/opi-evpn-bridge/pkg/utils" @@ -57,6 +59,10 @@ func resourceIDToFullName(container string, resourceID string) string { return fmt.Sprintf("//network.opiproject.org/%s/%s", container, resourceID) } +func protoClone[T proto.Message](protoStruct T) T { + return proto.Clone(protoStruct).(T) +} + func generateRandMAC() ([]byte, error) { buf := make([]byte, 6) if _, err := rand.Read(buf); err != nil { diff --git a/pkg/evpn/port.go b/pkg/evpn/port.go index d01e21f0..d0b264b6 100644 --- a/pkg/evpn/port.go +++ b/pkg/evpn/port.go @@ -21,7 +21,6 @@ import ( "go.einride.tech/aip/resourcename" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/known/emptypb" ) @@ -119,7 +118,7 @@ func (s *Server) CreateBridgePort(_ context.Context, in *pb.CreateBridgePortRequ fmt.Printf("Failed to up iface link: %v", err) return nil, err } - response := proto.Clone(in.BridgePort).(*pb.BridgePort) + response := protoClone(in.BridgePort) response.Status = &pb.BridgePortStatus{OperStatus: pb.BPOperStatus_BP_OPER_STATUS_UP} s.Ports[in.BridgePort.Name] = response log.Printf("CreateBridgePort: Sending to client: %v", response) @@ -226,7 +225,7 @@ func (s *Server) UpdateBridgePort(_ context.Context, in *pb.UpdateBridgePortRequ fmt.Printf("Failed to update link: %v", err) return nil, err } - response := proto.Clone(in.BridgePort).(*pb.BridgePort) + response := protoClone(in.BridgePort) response.Status = &pb.BridgePortStatus{OperStatus: pb.BPOperStatus_BP_OPER_STATUS_UP} s.Ports[in.BridgePort.Name] = response log.Printf("UpdateBridgePort: Sending to client: %v", response) diff --git a/pkg/evpn/port_test.go b/pkg/evpn/port_test.go index ae340843..43685f7c 100644 --- a/pkg/evpn/port_test.go +++ b/pkg/evpn/port_test.go @@ -141,11 +141,11 @@ func Test_CreateBridgePort(t *testing.T) { client := pb.NewBridgePortServiceClient(conn) if tt.exist { - opi.Ports[testBridgePortName] = proto.Clone(&testBridgePort).(*pb.BridgePort) + opi.Ports[testBridgePortName] = protoClone(&testBridgePort) opi.Ports[testBridgePortName].Name = testBridgePortName } if tt.out != nil { - tt.out = proto.Clone(tt.out).(*pb.BridgePort) + tt.out = protoClone(tt.out) tt.out.Name = testBridgePortName } @@ -235,7 +235,7 @@ func Test_DeleteBridgePort(t *testing.T) { client := pb.NewBridgePortServiceClient(conn) fname1 := resourceIDToFullName("ports", tt.in) - opi.Ports[testBridgePortName] = proto.Clone(&testBridgePort).(*pb.BridgePort) + opi.Ports[testBridgePortName] = protoClone(&testBridgePort) request := &pb.DeleteBridgePortRequest{Name: fname1, AllowMissing: tt.missing} response, err := client.DeleteBridgePort(ctx, request) @@ -325,11 +325,11 @@ func Test_UpdateBridgePort(t *testing.T) { client := pb.NewBridgePortServiceClient(conn) if tt.exist { - opi.Ports[testBridgePortName] = proto.Clone(&testBridgePort).(*pb.BridgePort) + opi.Ports[testBridgePortName] = protoClone(&testBridgePort) opi.Ports[testBridgePortName].Name = testBridgePortName } if tt.out != nil { - tt.out = proto.Clone(tt.out).(*pb.BridgePort) + tt.out = protoClone(tt.out) tt.out.Name = testBridgePortName } @@ -405,7 +405,7 @@ func Test_GetBridgePort(t *testing.T) { }(conn) client := pb.NewBridgePortServiceClient(conn) - opi.Ports[testBridgePortID] = proto.Clone(&testBridgePort).(*pb.BridgePort) + opi.Ports[testBridgePortID] = protoClone(&testBridgePort) request := &pb.GetBridgePortRequest{Name: tt.in} response, err := client.GetBridgePort(ctx, request) diff --git a/pkg/evpn/svi.go b/pkg/evpn/svi.go index 8b97c335..e27ea610 100644 --- a/pkg/evpn/svi.go +++ b/pkg/evpn/svi.go @@ -23,7 +23,6 @@ import ( "go.einride.tech/aip/resourcename" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/known/emptypb" ) @@ -133,7 +132,7 @@ func (s *Server) CreateSvi(_ context.Context, in *pb.CreateSviRequest) (*pb.Svi, fmt.Printf("Failed to up link: %v", err) return nil, err } - response := proto.Clone(in.Svi).(*pb.Svi) + response := protoClone(in.Svi) response.Status = &pb.SviStatus{OperStatus: pb.SVIOperStatus_SVI_OPER_STATUS_UP} s.Svis[in.Svi.Name] = response log.Printf("CreateSvi: Sending to client: %v", response) @@ -252,7 +251,7 @@ func (s *Server) UpdateSvi(_ context.Context, in *pb.UpdateSviRequest) (*pb.Svi, fmt.Printf("Failed to update link: %v", err) return nil, err } - response := proto.Clone(in.Svi).(*pb.Svi) + response := protoClone(in.Svi) response.Status = &pb.SviStatus{OperStatus: pb.SVIOperStatus_SVI_OPER_STATUS_UP} s.Svis[in.Svi.Name] = response log.Printf("UpdateSvi: Sending to client: %v", response) diff --git a/pkg/evpn/svi_test.go b/pkg/evpn/svi_test.go index 8dac0208..32ef38b0 100644 --- a/pkg/evpn/svi_test.go +++ b/pkg/evpn/svi_test.go @@ -243,15 +243,15 @@ func Test_CreateSvi(t *testing.T) { client := pb.NewSviServiceClient(conn) if tt.exist { - opi.Svis[testSviName] = proto.Clone(&testSvi).(*pb.Svi) + opi.Svis[testSviName] = protoClone(&testSvi) opi.Svis[testSviName].Name = testSviName } if tt.out != nil { - tt.out = proto.Clone(tt.out).(*pb.Svi) + tt.out = protoClone(tt.out) tt.out.Name = testSviName } - opi.Vrfs[testVrfName] = proto.Clone(&testVrf).(*pb.Vrf) - opi.Bridges[testLogicalBridgeName] = proto.Clone(&testLogicalBridge).(*pb.LogicalBridge) + opi.Vrfs[testVrfName] = protoClone(&testVrf) + opi.Bridges[testLogicalBridgeName] = protoClone(&testLogicalBridge) // TODO: refactor this mocking if strings.Contains(name, "failed LinkByName") { @@ -367,7 +367,7 @@ func Test_DeleteSvi(t *testing.T) { client := pb.NewSviServiceClient(conn) fname1 := resourceIDToFullName("svis", tt.in) - opi.Svis[testSviName] = proto.Clone(&testSvi).(*pb.Svi) + opi.Svis[testSviName] = protoClone(&testSvi) request := &pb.DeleteSviRequest{Name: fname1, AllowMissing: tt.missing} response, err := client.DeleteSvi(ctx, request) @@ -458,11 +458,11 @@ func Test_UpdateSvi(t *testing.T) { client := pb.NewSviServiceClient(conn) if tt.exist { - opi.Svis[testSviName] = proto.Clone(&testSvi).(*pb.Svi) + opi.Svis[testSviName] = protoClone(&testSvi) opi.Svis[testSviName].Name = testSviName } if tt.out != nil { - tt.out = proto.Clone(tt.out).(*pb.Svi) + tt.out = protoClone(tt.out) tt.out.Name = testSviName } @@ -538,7 +538,7 @@ func Test_GetSvi(t *testing.T) { }(conn) client := pb.NewSviServiceClient(conn) - opi.Svis[testSviName] = proto.Clone(&testSvi).(*pb.Svi) + opi.Svis[testSviName] = protoClone(&testSvi) request := &pb.GetSviRequest{Name: tt.in} response, err := client.GetSvi(ctx, request) diff --git a/pkg/evpn/vrf.go b/pkg/evpn/vrf.go index 47fbfc5b..b2fb388d 100644 --- a/pkg/evpn/vrf.go +++ b/pkg/evpn/vrf.go @@ -24,7 +24,6 @@ import ( "go.einride.tech/aip/resourcename" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/known/emptypb" ) @@ -140,7 +139,7 @@ func (s *Server) CreateVrf(_ context.Context, in *pb.CreateVrfRequest) (*pb.Vrf, return nil, err } } - response := proto.Clone(in.Vrf).(*pb.Vrf) + response := protoClone(in.Vrf) response.Status = &pb.VrfStatus{LocalAs: 4, RoutingTable: tableID, Rmac: mac} s.Vrfs[in.Vrf.Name] = response log.Printf("CreateVrf: Sending to client: %v", response) @@ -274,7 +273,7 @@ func (s *Server) UpdateVrf(_ context.Context, in *pb.UpdateVrfRequest) (*pb.Vrf, fmt.Printf("Failed to update link: %v", err) return nil, err } - response := proto.Clone(in.Vrf).(*pb.Vrf) + response := protoClone(in.Vrf) response.Status = &pb.VrfStatus{LocalAs: 4} s.Vrfs[in.Vrf.Name] = response log.Printf("UpdateVrf: Sending to client: %v", response) diff --git a/pkg/evpn/vrf_test.go b/pkg/evpn/vrf_test.go index 868d0bc1..6d679e6d 100644 --- a/pkg/evpn/vrf_test.go +++ b/pkg/evpn/vrf_test.go @@ -174,11 +174,11 @@ func Test_CreateVrf(t *testing.T) { client := pb.NewVrfServiceClient(conn) if tt.exist { - opi.Vrfs[testVrfName] = proto.Clone(&testVrf).(*pb.Vrf) + opi.Vrfs[testVrfName] = protoClone(&testVrf) opi.Vrfs[testVrfName].Name = testVrfName } if tt.out != nil { - tt.out = proto.Clone(tt.out).(*pb.Vrf) + tt.out = protoClone(tt.out) tt.out.Name = testVrfName } @@ -298,7 +298,7 @@ func Test_DeleteVrf(t *testing.T) { client := pb.NewVrfServiceClient(conn) fname1 := resourceIDToFullName("vrfs", tt.in) - opi.Vrfs[testVrfName] = proto.Clone(&testVrf).(*pb.Vrf) + opi.Vrfs[testVrfName] = protoClone(&testVrf) request := &pb.DeleteVrfRequest{Name: fname1, AllowMissing: tt.missing} response, err := client.DeleteVrf(ctx, request) @@ -389,11 +389,11 @@ func Test_UpdateVrf(t *testing.T) { client := pb.NewVrfServiceClient(conn) if tt.exist { - opi.Vrfs[testVrfName] = proto.Clone(&testVrf).(*pb.Vrf) + opi.Vrfs[testVrfName] = protoClone(&testVrf) opi.Vrfs[testVrfName].Name = testVrfName } if tt.out != nil { - tt.out = proto.Clone(tt.out).(*pb.Vrf) + tt.out = protoClone(tt.out) tt.out.Name = testVrfName } @@ -469,7 +469,7 @@ func Test_GetVrf(t *testing.T) { }(conn) client := pb.NewVrfServiceClient(conn) - opi.Vrfs[testVrfName] = proto.Clone(&testVrf).(*pb.Vrf) + opi.Vrfs[testVrfName] = protoClone(&testVrf) request := &pb.GetVrfRequest{Name: tt.in} response, err := client.GetVrf(ctx, request)