Skip to content

Commit

Permalink
refactor: move proto to utils
Browse files Browse the repository at this point in the history
Signed-off-by: Boris Glimcher <Boris.Glimcher@emc.com>
  • Loading branch information
glimchb committed Oct 19, 2023
1 parent 37a83fb commit 7d9158d
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 94 deletions.
6 changes: 3 additions & 3 deletions pkg/bridge/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func Test_CreateLogicalBridge(t *testing.T) {
_ = opi.store.Set(testLogicalBridgeName, &testLogicalBridgeWithStatus)
}
if tt.out != nil {
tt.out = protoClone(tt.out)
tt.out = utils.ProtoClone(tt.out)
tt.out.Name = testLogicalBridgeName
}
if tt.on != nil {
Expand Down Expand Up @@ -541,7 +541,7 @@ func Test_UpdateLogicalBridge(t *testing.T) {
_ = opi.store.Set(testLogicalBridgeName, &testLogicalBridgeWithStatus)
}
if tt.out != nil {
tt.out = protoClone(tt.out)
tt.out = utils.ProtoClone(tt.out)
tt.out.Name = testLogicalBridgeName
}

Expand Down Expand Up @@ -730,7 +730,7 @@ func Test_ListLogicalBridges(t *testing.T) {

request := &pb.ListLogicalBridgesRequest{PageSize: tt.size, PageToken: tt.token}
response, err := client.ListLogicalBridges(ctx, request)
if !equalProtoSlices(response.GetLogicalBridges(), tt.out) {
if !utils.EqualProtoSlices(response.GetLogicalBridges(), tt.out) {
t.Error("response: expected", tt.out, "received", response.GetLogicalBridges())
}

Expand Down
19 changes: 0 additions & 19 deletions pkg/bridge/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/grpc/test/bufconn"
"google.golang.org/protobuf/proto"

pb "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go"
)
Expand All @@ -35,10 +34,6 @@ func resourceIDToFullName(_ string, resourceID string) string {
return fmt.Sprintf("//network.opiproject.org/bridges/%s", resourceID)
}

func protoClone[T proto.Message](protoStruct T) T {
return proto.Clone(protoStruct).(T)
}

func extractPagination(pageSize int32, pageToken string, pagination map[string]int) (size int, offset int, err error) {
const (
maxPageSize = 250
Expand Down Expand Up @@ -94,17 +89,3 @@ func dialer(opi *Server) func(context.Context, string) (net.Conn, error) {
return listener.Dial()
}
}

func equalProtoSlices[T proto.Message](x, y []T) bool {
if len(x) != len(y) {
return false
}

for i := 0; i < len(x); i++ {
if !proto.Equal(x[i], y[i]) {
return false
}
}

return true
}
5 changes: 3 additions & 2 deletions pkg/bridge/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/google/uuid"
"github.com/opiproject/opi-evpn-bridge/pkg/models"
"github.com/opiproject/opi-evpn-bridge/pkg/utils"

pb "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go"

Expand Down Expand Up @@ -52,7 +53,7 @@ func (s *Server) CreateLogicalBridge(ctx context.Context, in *pb.CreateLogicalBr
return nil, err
}
// translate object
response := protoClone(in.LogicalBridge)
response := utils.ProtoClone(in.LogicalBridge)
response.Status = &pb.LogicalBridgeStatus{OperStatus: pb.LBOperStatus_LB_OPER_STATUS_UP}
log.Printf("new object %v", models.NewBridge(response))
// save object to the database
Expand Down Expand Up @@ -130,7 +131,7 @@ func (s *Server) UpdateLogicalBridge(ctx context.Context, in *pb.UpdateLogicalBr
return nil, err
}
}
response := protoClone(in.LogicalBridge)
response := utils.ProtoClone(in.LogicalBridge)
response.Status = &pb.LogicalBridgeStatus{OperStatus: pb.LBOperStatus_LB_OPER_STATUS_UP}
err = s.store.Set(in.LogicalBridge.Name, response)
if err != nil {
Expand Down
18 changes: 0 additions & 18 deletions pkg/port/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ func resourceIDToFullName(_ string, resourceID string) string {
return fmt.Sprintf("//network.opiproject.org/ports/%s", resourceID)
}

func protoClone[T proto.Message](protoStruct T) T {
return proto.Clone(protoStruct).(T)
}

func extractPagination(pageSize int32, pageToken string, pagination map[string]int) (size int, offset int, err error) {
const (
maxPageSize = 250
Expand Down Expand Up @@ -122,17 +118,3 @@ func dialer(opi *Server) func(context.Context, string) (net.Conn, error) {
return listener.Dial()
}
}

func equalProtoSlices[T proto.Message](x, y []T) bool {
if len(x) != len(y) {
return false
}

for i := 0; i < len(x); i++ {
if !proto.Equal(x[i], y[i]) {
return false
}
}

return true
}
5 changes: 3 additions & 2 deletions pkg/port/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/google/uuid"
"github.com/opiproject/opi-evpn-bridge/pkg/models"
"github.com/opiproject/opi-evpn-bridge/pkg/utils"

pb "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go"

Expand Down Expand Up @@ -53,7 +54,7 @@ func (s *Server) CreateBridgePort(ctx context.Context, in *pb.CreateBridgePortRe
return nil, err
}
// translate object
response := protoClone(in.BridgePort)
response := utils.ProtoClone(in.BridgePort)
response.Status = &pb.BridgePortStatus{OperStatus: pb.BPOperStatus_BP_OPER_STATUS_UP}
log.Printf("new object %v", models.NewPort(response))
// save object to the database
Expand Down Expand Up @@ -128,7 +129,7 @@ func (s *Server) UpdateBridgePort(ctx context.Context, in *pb.UpdateBridgePortRe
fmt.Printf("Failed to update link: %v", err)
return nil, err
}
response := protoClone(in.BridgePort)
response := utils.ProtoClone(in.BridgePort)
response.Status = &pb.BridgePortStatus{OperStatus: pb.BPOperStatus_BP_OPER_STATUS_UP}
err = s.store.Set(in.BridgePort.Name, response)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/port/port_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func Test_CreateBridgePort(t *testing.T) {
_ = opi.store.Set(testBridgePortName, &testBridgePortWithStatus)
}
if tt.out != nil {
tt.out = protoClone(tt.out)
tt.out = utils.ProtoClone(tt.out)
tt.out.Name = testBridgePortName
}
if tt.on != nil {
Expand Down Expand Up @@ -571,7 +571,7 @@ func Test_UpdateBridgePort(t *testing.T) {
_ = opi.store.Set(testBridgePortName, &testBridgePortWithStatus)
}
if tt.out != nil {
tt.out = protoClone(tt.out)
tt.out = utils.ProtoClone(tt.out)
tt.out.Name = testBridgePortName
}

Expand Down Expand Up @@ -760,7 +760,7 @@ func Test_ListBridgePorts(t *testing.T) {

request := &pb.ListBridgePortsRequest{PageSize: tt.size, PageToken: tt.token}
response, err := client.ListBridgePorts(ctx, request)
if !equalProtoSlices(response.GetBridgePorts(), tt.out) {
if !utils.EqualProtoSlices(response.GetBridgePorts(), tt.out) {
t.Error("response: expected", tt.out, "received", response.GetBridgePorts())
}

Expand Down
18 changes: 0 additions & 18 deletions pkg/svi/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ func resourceIDToFullName(_ string, resourceID string) string {
return fmt.Sprintf("//network.opiproject.org/svis/%s", resourceID)
}

func protoClone[T proto.Message](protoStruct T) T {
return proto.Clone(protoStruct).(T)
}

func extractPagination(pageSize int32, pageToken string, pagination map[string]int) (size int, offset int, err error) {
const (
maxPageSize = 250
Expand Down Expand Up @@ -155,17 +151,3 @@ func dialer(opi *Server) func(context.Context, string) (net.Conn, error) {
return listener.Dial()
}
}

func equalProtoSlices[T proto.Message](x, y []T) bool {
if len(x) != len(y) {
return false
}

for i := 0; i < len(x); i++ {
if !proto.Equal(x[i], y[i]) {
return false
}
}

return true
}
5 changes: 3 additions & 2 deletions pkg/svi/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/google/uuid"
"github.com/opiproject/opi-evpn-bridge/pkg/models"
"github.com/opiproject/opi-evpn-bridge/pkg/utils"

pb "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go"

Expand Down Expand Up @@ -82,7 +83,7 @@ func (s *Server) CreateSvi(ctx context.Context, in *pb.CreateSviRequest) (*pb.Sv
return nil, err
}
// translate object
response := protoClone(in.Svi)
response := utils.ProtoClone(in.Svi)
response.Status = &pb.SviStatus{OperStatus: pb.SVIOperStatus_SVI_OPER_STATUS_UP}
log.Printf("new object %v", models.NewSvi(response))
// save object to the database
Expand Down Expand Up @@ -197,7 +198,7 @@ func (s *Server) UpdateSvi(ctx context.Context, in *pb.UpdateSviRequest) (*pb.Sv
fmt.Printf("Failed to update link: %v", err)
return nil, err
}
response := protoClone(in.Svi)
response := utils.ProtoClone(in.Svi)
response.Status = &pb.SviStatus{OperStatus: pb.SVIOperStatus_SVI_OPER_STATUS_UP}
err = s.store.Set(in.Svi.Name, response)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/svi/svi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ func Test_CreateSvi(t *testing.T) {
_ = opi.store.Set(testSviName, &testSviWithStatus)
}
if tt.out != nil {
tt.out = protoClone(tt.out)
tt.out = utils.ProtoClone(tt.out)
tt.out.Name = testSviName
}
if tt.on != nil {
Expand Down Expand Up @@ -700,7 +700,7 @@ func Test_UpdateSvi(t *testing.T) {
_ = opi.store.Set(testSviName, &testSviWithStatus)
}
if tt.out != nil {
tt.out = protoClone(tt.out)
tt.out = utils.ProtoClone(tt.out)
tt.out.Name = testSviName
}

Expand Down Expand Up @@ -889,7 +889,7 @@ func Test_ListSvis(t *testing.T) {

request := &pb.ListSvisRequest{PageSize: tt.size, PageToken: tt.token}
response, err := client.ListSvis(ctx, request)
if !equalProtoSlices(response.GetSvis(), tt.out) {
if !utils.EqualProtoSlices(response.GetSvis(), tt.out) {
t.Error("response: expected", tt.out, "received", response.GetSvis())
}

Expand Down
27 changes: 27 additions & 0 deletions pkg/utils/proto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2022-2023 Dell Inc, or its subsidiaries.

// Package utils has some utility functions and interfaces
package utils

import "google.golang.org/protobuf/proto"

// ProtoClone is a helper function to clone and cast protobufs
func ProtoClone[T proto.Message](protoStruct T) T {
return proto.Clone(protoStruct).(T)
}

// EqualProtoSlices is a helper function to compare protobuf slices
func EqualProtoSlices[T proto.Message](x, y []T) bool {
if len(x) != len(y) {
return false
}

for i := 0; i < len(x); i++ {
if !proto.Equal(x[i], y[i]) {
return false
}
}

return true
}
19 changes: 0 additions & 19 deletions pkg/vrf/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/grpc/test/bufconn"
"google.golang.org/protobuf/proto"

pb "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go"
)
Expand All @@ -32,10 +31,6 @@ func resourceIDToFullName(_ string, resourceID string) string {
return fmt.Sprintf("//network.opiproject.org/vrfs/%s", 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 {
Expand Down Expand Up @@ -103,17 +98,3 @@ func dialer(opi *Server) func(context.Context, string) (net.Conn, error) {
return listener.Dial()
}
}

func equalProtoSlices[T proto.Message](x, y []T) bool {
if len(x) != len(y) {
return false
}

for i := 0; i < len(x); i++ {
if !proto.Equal(x[i], y[i]) {
return false
}
}

return true
}
5 changes: 3 additions & 2 deletions pkg/vrf/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/google/uuid"
"github.com/opiproject/opi-evpn-bridge/pkg/models"
"github.com/opiproject/opi-evpn-bridge/pkg/utils"

pb "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go"

Expand Down Expand Up @@ -69,7 +70,7 @@ func (s *Server) CreateVrf(ctx context.Context, in *pb.CreateVrfRequest) (*pb.Vr
return nil, err
}
// translate object
response := protoClone(in.Vrf)
response := utils.ProtoClone(in.Vrf)
response.Status = &pb.VrfStatus{LocalAs: 4, RoutingTable: tableID, Rmac: mac}
log.Printf("new object %v", models.NewVrf(response))
// save object to the database
Expand Down Expand Up @@ -148,7 +149,7 @@ func (s *Server) UpdateVrf(ctx context.Context, in *pb.UpdateVrfRequest) (*pb.Vr
fmt.Printf("Failed to update link: %v", err)
return nil, err
}
response := protoClone(in.Vrf)
response := utils.ProtoClone(in.Vrf)
response.Status = &pb.VrfStatus{LocalAs: 4}
err = s.store.Set(in.Vrf.Name, response)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/vrf/vrf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func Test_CreateVrf(t *testing.T) {
_ = opi.store.Set(testVrfName, &testVrfWithStatus)
}
if tt.out != nil {
tt.out = protoClone(tt.out)
tt.out = utils.ProtoClone(tt.out)
tt.out.Name = testVrfName
}
if tt.on != nil {
Expand Down Expand Up @@ -736,7 +736,7 @@ func Test_UpdateVrf(t *testing.T) {
_ = opi.store.Set(testVrfName, &testVrfWithStatus)
}
if tt.out != nil {
tt.out = protoClone(tt.out)
tt.out = utils.ProtoClone(tt.out)
tt.out.Name = testVrfName
}

Expand Down Expand Up @@ -925,7 +925,7 @@ func Test_ListVrfs(t *testing.T) {

request := &pb.ListVrfsRequest{PageSize: tt.size, PageToken: tt.token}
response, err := client.ListVrfs(ctx, request)
if !equalProtoSlices(response.GetVrfs(), tt.out) {
if !utils.EqualProtoSlices(response.GetVrfs(), tt.out) {
t.Error("response: expected", tt.out, "received", response.GetVrfs())
}

Expand Down

0 comments on commit 7d9158d

Please sign in to comment.