Skip to content

Commit

Permalink
fix(evpn): allow passing name instead of full name
Browse files Browse the repository at this point in the history
Signed-off-by: atulpatel261194 <[email protected]>
  • Loading branch information
atulpatel261194 committed Jun 7, 2024
1 parent 9cf56d1 commit d0c47b6
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 14 deletions.
4 changes: 3 additions & 1 deletion cmd/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ const TLSFiles = "tlsfiles"
// PrintResponse prints only response string into stdout without any
// additional information
func PrintResponse(response string) {
fmt.Fprintln(os.Stdout, response)
if _, err := fmt.Fprintln(os.Stdout, response); err != nil {
fmt.Fprintf(os.Stderr, "Failed to write to stdout: %v\n", err)
}
}
42 changes: 35 additions & 7 deletions cmd/network/evpn-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"log"
"net"
"strings"

"github.com/PraserX/ipconv"
pb "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go"
Expand All @@ -36,9 +37,17 @@ func ComposeGwIps(comp []*pc.IPPrefix) string {
return status
}

// ExtractShortName takes a full name and returns the short name.
func ExtractShortName(fullName string) string {
parts := strings.Split(fullName, "/")
return parts[len(parts)-1] // Return the last part of the split name.
}

// PrintLB prints the logical bridge fields in human readable format
func PrintLB(lb *pb.LogicalBridge) {
log.Println("name:", lb.GetName())
shortName := ExtractShortName(lb.GetName())
log.Println("name:", shortName)

log.Println("status:", lb.GetStatus().GetOperStatus().String())
log.Println("vlan:", lb.GetSpec().GetVlanId())
if lb.GetSpec().GetVni() != 0 {
Expand All @@ -55,21 +64,37 @@ func PrintLB(lb *pb.LogicalBridge) {

// PrintBP prints the bridge Port fields in human readable format
func PrintBP(bp *pb.BridgePort) {
log.Println("name:", bp.GetName())
shortName := ExtractShortName(bp.GetName())
log.Println("name:", shortName)

log.Println("status:", bp.GetStatus().GetOperStatus().String())
log.Println("ptype:", bp.GetSpec().GetPtype())
log.Println("MacAddress:", net.HardwareAddr(bp.GetSpec().GetMacAddress()).String())
log.Println("bridges:", bp.GetSpec().GetLogicalBridges())

// Extract short names for the logical bridges
bridges := bp.GetSpec().GetLogicalBridges()
shortBridgeNames := make([]string, len(bridges))
for i, bridge := range bridges {
shortBridgeNames[i] = ExtractShortName(bridge)
}
log.Println("bridges:", shortBridgeNames)

log.Println("Component Status:")
log.Println(ComposeComponentsInfo(bp.GetStatus().GetComponents()))
}

// PrintSvi prints the svi fields in human readable format
func PrintSvi(svi *pb.Svi) {
log.Println("name:", svi.GetName())
shortName := ExtractShortName(svi.GetName())
log.Println("name:", shortName)

log.Println("status:", svi.GetStatus().GetOperStatus().String())
log.Println("Vrf:", svi.GetSpec().GetVrf())
log.Println("LogicalBridge:", svi.GetSpec().GetLogicalBridge())

shortName = ExtractShortName(svi.GetSpec().GetVrf())
log.Println("Vrf:", shortName)

shortName = ExtractShortName(svi.GetSpec().GetLogicalBridge())
log.Println("LogicalBridge:", shortName)
log.Println("MacAddress:", net.HardwareAddr(svi.GetSpec().GetMacAddress()).String())
log.Println("EnableBgp:", svi.GetSpec().GetEnableBgp())
log.Println("GwIPs:", ComposeGwIps(svi.GetSpec().GetGwIpPrefix()))
Expand All @@ -81,7 +106,10 @@ func PrintSvi(svi *pb.Svi) {
// PrintVrf prints the vrf fields in human readable format
func PrintVrf(vrf *pb.Vrf) {
Loopback := fmt.Sprintf("%+v/%+v", ipconv.IntToIPv4(vrf.GetSpec().GetLoopbackIpPrefix().GetAddr().GetV4Addr()), vrf.GetSpec().GetLoopbackIpPrefix().GetLen())
log.Println("name:", vrf.GetName())

shortName := ExtractShortName(vrf.GetName())
log.Println("name:", shortName)

log.Println("operation status:", vrf.GetStatus().GetOperStatus().String())

if vrf.GetSpec().GetVni() != 0 {
Expand Down
13 changes: 12 additions & 1 deletion network/bridge_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,32 @@ import (
// CreateBridgePort creates an Bridge Port an OPI server
func (c evpnClientImpl) CreateBridgePort(ctx context.Context, name string, mac string, bridgePortType string, logicalBridges []string) (*pb.BridgePort, error) {
var typeOfPort pb.BridgePortType

conn, closer, err := c.NewConn()
if err != nil {
log.Printf("error creating connection: %s\n", err)
return nil, err
}
defer closer()

if mac == "" || bridgePortType == "" {
return nil, errors.New("required parameter [mac, bridgePortType] wasn't passed ")
}

var lBridges = make([]string, 0)
for _, lb := range logicalBridges {
str := resourceIDToFullName("bridges", lb)
lBridges = append(lBridges, str)
}

client := c.getEvpnBridgePortClient(conn)

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

switch bridgePortType {
case "access":
typeOfPort = pb.BridgePortType_BRIDGE_PORT_TYPE_ACCESS
Expand All @@ -49,7 +60,7 @@ func (c evpnClientImpl) CreateBridgePort(ctx context.Context, name string, mac s
Spec: &pb.BridgePortSpec{
MacAddress: macBytes,
Ptype: typeOfPort,
LogicalBridges: logicalBridges,
LogicalBridges: lBridges,
},
},
})
Expand Down
2 changes: 1 addition & 1 deletion network/bridge_port_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestCreateBridgePort(t *testing.T) {
Spec: &pb.BridgePortSpec{
MacAddress: macBytes,
Ptype: pb.BridgePortType_BRIDGE_PORT_TYPE_ACCESS,
LogicalBridges: []string{"lb1", "lb2"},
LogicalBridges: []string{"//network.opiproject.org/bridges/lb1", "//network.opiproject.org/bridges/lb2"},
},
}

Expand Down
7 changes: 5 additions & 2 deletions network/svi.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func (c evpnClientImpl) CreateSvi(ctx context.Context, name string, vrf string,
if vrf == "" || mac == "" || len(gwIPs) == 0 {
return nil, errors.New("one of the required together parameter [vrf, mac, gwIPs] wasn't passed ")
}
vrfName := resourceIDToFullName("vrfs", vrf)

lBridge := resourceIDToFullName("bridges", logicalBridge)

gwPrefixes, err := parseIPPrefixes(gwIPs)
if err != nil {
Expand All @@ -46,8 +49,8 @@ func (c evpnClientImpl) CreateSvi(ctx context.Context, name string, vrf string,
SviId: name,
Svi: &pb.Svi{
Spec: &pb.SviSpec{
Vrf: vrf,
LogicalBridge: logicalBridge,
Vrf: vrfName,
LogicalBridge: lBridge,
MacAddress: macBytes,
GwIpPrefix: gwPrefixes,
EnableBgp: ebgp,
Expand Down
4 changes: 2 additions & 2 deletions network/svi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func TestCreateSvi(t *testing.T) {

testSvi := &pb.Svi{
Spec: &pb.SviSpec{
Vrf: "vrf1",
LogicalBridge: "logical1",
Vrf: "//network.opiproject.org/vrfs/vrf1",
LogicalBridge: "//network.opiproject.org/bridges/logical1",
MacAddress: macBytes,
GwIpPrefix: wantGWPrefixes,
EnableBgp: true,
Expand Down

0 comments on commit d0c47b6

Please sign in to comment.