Skip to content

Commit

Permalink
fix(evpn): fix evpn and unit test
Browse files Browse the repository at this point in the history
Signed-off-by: atulpatel261194 <Atul.Patel@intel.com>
Co-authored-by: Dimitrios Markou <dimitrios.markou@ericsson.com>
Co-authored-by: Venkatesh Vemula <venkatesh.vemula@intel.com>
Co-authored-by: Banoth Saikumar <banoth.saikumar@intel.com>
Co-authored-by: Jambekar Vishakha <vishakha.jambekar@intel.com>
  • Loading branch information
5 people committed Apr 3, 2024
1 parent acb3a85 commit 9aae8b4
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 31 deletions.
12 changes: 8 additions & 4 deletions cmd/network/evpn-logical-brige.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ func CreateLogicalBridge() *cobra.Command {
Short: "Create a logical bridge",
Long: "Create a logical bridge with the specified name, VLAN ID, and VNI",
Run: func(_ *cobra.Command, _ []string) {
var vniparam *uint32

Check warning on line 31 in cmd/network/evpn-logical-brige.go

View check run for this annotation

Codecov / codecov/patch

cmd/network/evpn-logical-brige.go#L31

Added line #L31 was not covered by tests
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
evpnClient, err := network.NewLogicalBridge(addr)
if err != nil {
log.Fatalf("could create gRPC client: %v", err)
}
defer cancel()

lb, err := evpnClient.CreateLogicalBridge(ctx, name, vlanID, vni, vtep)
if vni != 0 {
vniparam = &vni
}

Check warning on line 41 in cmd/network/evpn-logical-brige.go

View check run for this annotation

Codecov / codecov/patch

cmd/network/evpn-logical-brige.go#L39-L41

Added lines #L39 - L41 were not covered by tests

lb, err := evpnClient.CreateLogicalBridge(ctx, name, vlanID, vniparam, vtep)

Check warning on line 43 in cmd/network/evpn-logical-brige.go

View check run for this annotation

Codecov / codecov/patch

cmd/network/evpn-logical-brige.go#L43

Added line #L43 was not covered by tests
if err != nil {
log.Fatalf("failed to create logical bridge: %v", err)
}
Expand All @@ -59,9 +64,8 @@ func CreateLogicalBridge() *cobra.Command {
if err := cmd.MarkFlagRequired("vlan-id"); err != nil {
log.Fatalf("Error marking flag as required: %v", err)
}
if err := cmd.MarkFlagRequired("vni"); err != nil {
log.Fatalf("Error marking flag as required: %v", err)
}

cmd.MarkFlagsRequiredTogether("vni", "vtep")

Check warning on line 68 in cmd/network/evpn-logical-brige.go

View check run for this annotation

Codecov / codecov/patch

cmd/network/evpn-logical-brige.go#L68

Added line #L68 was not covered by tests

return cmd
}
Expand Down
11 changes: 5 additions & 6 deletions cmd/network/evpn-vrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ func CreateVRF() *cobra.Command {
Use: "create-vrf",
Short: "Create a VRF",
Run: func(_ *cobra.Command, _ []string) {
var vniparam *uint32

Check warning on line 30 in cmd/network/evpn-vrf.go

View check run for this annotation

Codecov / codecov/patch

cmd/network/evpn-vrf.go#L30

Added line #L30 was not covered by tests
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
evpnClient, err := network.NewVRF(addr)
if err != nil {
log.Fatalf("could not create gRPC client: %v", err)
}
defer cancel()

vrf, err := evpnClient.CreateVrf(ctx, name, vni, loopback, vtep)
if vni != 0 {
vniparam = &vni
}
vrf, err := evpnClient.CreateVrf(ctx, name, vniparam, loopback, vtep)

Check warning on line 40 in cmd/network/evpn-vrf.go

View check run for this annotation

Codecov / codecov/patch

cmd/network/evpn-vrf.go#L37-L40

Added lines #L37 - L40 were not covered by tests
if err != nil {
log.Fatalf("failed to create vrf: %v", err)
}
Expand All @@ -49,10 +52,6 @@ func CreateVRF() *cobra.Command {
cmd.Flags().StringVar(&vtep, "vtep", "", "VTEP IP address")
cmd.Flags().StringVar(&addr, "addr", "localhost:50151", "address of OPI gRPC server")

if err := cmd.MarkFlagRequired("vni"); err != nil {
log.Fatalf("Error marking flag as required: %v", err)
}

if err := cmd.MarkFlagRequired("loopback"); err != nil {
log.Fatalf("Error marking flag as required: %v", err)
}
Expand Down
9 changes: 8 additions & 1 deletion network/bridge_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ package network

import (
"context"
"fmt"
"log"
"net"

pb "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go"
"google.golang.org/protobuf/types/known/emptypb"
Expand All @@ -25,6 +27,11 @@ func (c evpnClientImpl) CreateBridgePort(ctx context.Context, name string, mac s
defer closer()

client := c.getEvpnBridgePortClient(conn)
macBytes, err := net.ParseMAC(mac)
if err != nil {
fmt.Println("Error parsing MAC address:", err)
return nil, err
}

Check warning on line 34 in network/bridge_port.go

View check run for this annotation

Codecov / codecov/patch

network/bridge_port.go#L32-L34

Added lines #L32 - L34 were not covered by tests
switch bridgePortType {
case "access":
typeOfPort = pb.BridgePortType_BRIDGE_PORT_TYPE_ACCESS
Expand All @@ -37,7 +44,7 @@ func (c evpnClientImpl) CreateBridgePort(ctx context.Context, name string, mac s
BridgePortId: name,
BridgePort: &pb.BridgePort{
Spec: &pb.BridgePortSpec{
MacAddress: []byte(mac),
MacAddress: macBytes,
Ptype: typeOfPort,
LogicalBridges: logicalBridges,
},
Expand Down
4 changes: 3 additions & 1 deletion network/bridge_port_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package network
import (
"context"
"errors"
"net"
"testing"

"github.com/opiproject/godpu/mocks"
Expand All @@ -22,9 +23,10 @@ import (
)

func TestCreateBridgePort(t *testing.T) {
macBytes, _ := net.ParseMAC("00:11:22:aa:bb:cc")
testBridgePort := &pb.BridgePort{
Spec: &pb.BridgePortSpec{
MacAddress: []byte("00:11:22:aa:bb:cc"),
MacAddress: macBytes,
Ptype: pb.BridgePortType_BRIDGE_PORT_TYPE_ACCESS,
LogicalBridges: []string{"lb1", "lb2"},
},
Expand Down
4 changes: 2 additions & 2 deletions network/evpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type evpnClientImpl struct {
type EvpnClient interface {

// Logical Bridge interfaces
CreateLogicalBridge(ctx context.Context, name string, vlanID uint32, vni uint32, vtepIP string) (*pb.LogicalBridge, error)
CreateLogicalBridge(ctx context.Context, name string, vlanID uint32, vni *uint32, vtepIP string) (*pb.LogicalBridge, error)
DeleteLogicalBridge(ctx context.Context, name string, allowMissing bool) (*emptypb.Empty, error)
GetLogicalBridge(ctx context.Context, name string) (*pb.LogicalBridge, error)
ListLogicalBridges(ctx context.Context, pageSize int32, pageToken string) (*pb.ListLogicalBridgesResponse, error)
Expand All @@ -55,7 +55,7 @@ type EvpnClient interface {
UpdateBridgePort(ctx context.Context, name string, updateMask []string, allowMissing bool) (*pb.BridgePort, error)

// VRF Interfaces
CreateVrf(ctx context.Context, name string, vni uint32, loopback string, vtep string) (*pb.Vrf, error)
CreateVrf(ctx context.Context, name string, vni *uint32, loopback string, vtep string) (*pb.Vrf, error)
DeleteVrf(ctx context.Context, name string, allowMissing bool) (*emptypb.Empty, error)
GetVrf(ctx context.Context, name string) (*pb.Vrf, error)
ListVrfs(ctx context.Context, pageSize int32, pageToken string) (*pb.ListVrfsResponse, error)
Expand Down
23 changes: 17 additions & 6 deletions network/logical_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ package network

import (
"context"
"errors"
"log"

pb "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go"
pc "github.com/opiproject/opi-api/network/opinetcommon/v1alpha1/gen/go"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/fieldmaskpb"
)

// CreateLogicalBridge creates an Logical Bridge an OPI server
func (c evpnClientImpl) CreateLogicalBridge(ctx context.Context, name string, vlanID uint32, vni uint32, vtepIP string) (*pb.LogicalBridge, error) {
func (c evpnClientImpl) CreateLogicalBridge(ctx context.Context, name string, vlanID uint32, vni *uint32, vtepIP string) (*pb.LogicalBridge, error) {
var ipVtep *pc.IPPrefix

conn, closer, err := c.NewConn()
if err != nil {
log.Printf("error creating connection: %s\n", err)
Expand All @@ -24,17 +28,24 @@ func (c evpnClientImpl) CreateLogicalBridge(ctx context.Context, name string, vl
defer closer()

client := c.getEvpnLogicalBridgeClient(conn)
ipVtep, err := parseIPAndPrefix(vtepIP)
if err != nil {
log.Printf("parseIPAndPrefix: error creating vrf: %s\n", err)
return nil, err

if (vni == nil && vtepIP != "") || (vtepIP == "" && vni != nil) {
return nil, errors.New("one of the required together parameter [vni, vtep] wasn't passed ")
}

Check warning on line 34 in network/logical_bridge.go

View check run for this annotation

Codecov / codecov/patch

network/logical_bridge.go#L33-L34

Added lines #L33 - L34 were not covered by tests

if vni != nil && vtepIP != "" {
ipVtep, err = parseIPAndPrefix(vtepIP)
if err != nil {
log.Printf("parseIPAndPrefix: error creating Logical Bridge: %s\n", err)
return nil, err
}

Check warning on line 41 in network/logical_bridge.go

View check run for this annotation

Codecov / codecov/patch

network/logical_bridge.go#L39-L41

Added lines #L39 - L41 were not covered by tests
}
data, err := client.CreateLogicalBridge(ctx, &pb.CreateLogicalBridgeRequest{
LogicalBridgeId: name,
LogicalBridge: &pb.LogicalBridge{
Spec: &pb.LogicalBridgeSpec{
VlanId: vlanID,
Vni: &vni,
Vni: vni,
VtepIpPrefix: ipVtep,
},
},
Expand Down
3 changes: 2 additions & 1 deletion network/logical_bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
)

func TestCreateLogicalBridge(t *testing.T) {
testVni := uint32(500)
wantIPPefix := &pc.IPPrefix{
Addr: &pc.IPAddress{
Af: pc.IpAf_IP_AF_INET,
Expand Down Expand Up @@ -106,7 +107,7 @@ func TestCreateLogicalBridge(t *testing.T) {

response, err := c.CreateLogicalBridge(
context.Background(),
"lb1", 100, 500, "192.168.1.0/24",
"lb1", 100, &testVni, "192.168.1.0/24",
)

assert.Equal(t, tt.wantErr, err)
Expand Down
9 changes: 8 additions & 1 deletion network/svi.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ package network

import (
"context"
"fmt"
"log"
"net"

pb "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go"
"google.golang.org/protobuf/types/known/emptypb"
Expand All @@ -30,13 +32,18 @@ func (c evpnClientImpl) CreateSvi(ctx context.Context, name string, vrf string,
log.Printf("error parsing GwIPs: %s\n", err)
return nil, err
}
macBytes, err := net.ParseMAC(mac)
if err != nil {
fmt.Println("Error parsing MAC address:", err)
return nil, err
}

Check warning on line 39 in network/svi.go

View check run for this annotation

Codecov / codecov/patch

network/svi.go#L37-L39

Added lines #L37 - L39 were not covered by tests
data, err := client.CreateSvi(ctx, &pb.CreateSviRequest{
SviId: name,
Svi: &pb.Svi{
Spec: &pb.SviSpec{
Vrf: vrf,
LogicalBridge: logicalBridge,
MacAddress: []byte(mac),
MacAddress: macBytes,
GwIpPrefix: gwPrefixes,
EnableBgp: ebgp,
RemoteAs: remoteAS,
Expand Down
5 changes: 4 additions & 1 deletion network/svi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package network
import (
"context"
"errors"
"net"
"testing"

"github.com/opiproject/godpu/mocks"
Expand All @@ -33,11 +34,13 @@ func TestCreateSvi(t *testing.T) {
Len: 32,
},
}
macBytes, _ := net.ParseMAC("01:23:45:67:89:ab")

testSvi := &pb.Svi{
Spec: &pb.SviSpec{
Vrf: "vrf1",
LogicalBridge: "logical1",
MacAddress: []byte("01:23:45:67:89:ab"),
MacAddress: macBytes,
GwIpPrefix: wantGWPrefixes,
EnableBgp: true,
RemoteAs: 65000,
Expand Down
23 changes: 16 additions & 7 deletions network/vrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,47 @@ package network

import (
"context"
"errors"
"log"

pb "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go"
pc "github.com/opiproject/opi-api/network/opinetcommon/v1alpha1/gen/go"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/fieldmaskpb"
)

// CreateVrf Create vrf on OPI Server
func (c evpnClientImpl) CreateVrf(ctx context.Context, name string, vni uint32, loopback string, vtep string) (*pb.Vrf, error) {
func (c evpnClientImpl) CreateVrf(ctx context.Context, name string, vni *uint32, loopbackIP string, vtepIP string) (*pb.Vrf, error) {
conn, closer, err := c.NewConn()
var ipVtep *pc.IPPrefix
if err != nil {
log.Printf("error creating connection: %s\n", err)
return nil, err
}
defer closer()

client := c.getEvpnVRFClient(conn)
ipLoopback, err := parseIPAndPrefix(loopback)
ipLoopback, err := parseIPAndPrefix(loopbackIP)
if err != nil {
log.Printf("parseIPAndPrefix: error creating vrf: %s\n", err)
return nil, err
}
ipVtep, err := parseIPAndPrefix(vtep)
if err != nil {
log.Printf("parseIPAndPrefix: error creating vrf: %s\n", err)
return nil, err
if (vni == nil && vtepIP != "") || (vtepIP == "" && vni != nil) {
return nil, errors.New("one of the required together parameter [vni, vtep] wasn't passed ")
}

Check warning on line 37 in network/vrf.go

View check run for this annotation

Codecov / codecov/patch

network/vrf.go#L36-L37

Added lines #L36 - L37 were not covered by tests

if vni != nil && vtepIP != "" {
ipVtep, err = parseIPAndPrefix(vtepIP)
if err != nil {
log.Printf("parseIPAndPrefix: error creating vrf: %s\n", err)
return nil, err
}

Check warning on line 44 in network/vrf.go

View check run for this annotation

Codecov / codecov/patch

network/vrf.go#L42-L44

Added lines #L42 - L44 were not covered by tests
}
data, err := client.CreateVrf(ctx, &pb.CreateVrfRequest{
VrfId: name,
Vrf: &pb.Vrf{
Spec: &pb.VrfSpec{
Vni: &vni,
Vni: vni,
LoopbackIpPrefix: ipLoopback,
VtepIpPrefix: ipVtep,
},
Expand Down
2 changes: 1 addition & 1 deletion network/vrf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestCreateVrf(t *testing.T) {
},
)

response, err := c.CreateVrf(context.Background(), "Vrf1", 100, "192.168.1.1/24", "10.0.0.1/32")
response, err := c.CreateVrf(context.Background(), "Vrf1", &vni, "192.168.1.1/24", "10.0.0.1/32")

assert.Equal(t, tt.wantErr, err)
assert.True(t, proto.Equal(response, tt.wantResponse))
Expand Down

0 comments on commit 9aae8b4

Please sign in to comment.