Skip to content

Commit

Permalink
fix(evpn): issue259 consider breaking too long files
Browse files Browse the repository at this point in the history
Signed-off-by: atulpatel261194 <atul.patel@intel.com>
  • Loading branch information
atulpatel261194 authored and glimchb committed Aug 10, 2023
1 parent d7d7f54 commit a94c5e2
Show file tree
Hide file tree
Showing 16 changed files with 1,535 additions and 1,384 deletions.
215 changes: 215 additions & 0 deletions cmd/evpn-bridge-port.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2022-2023 Intel Corporation, or its subsidiaries.
// Copyright (c) 2022-2023 Dell Inc, or its subsidiaries.

// Package cmd implements the CLI commands
package cmd

import (
"context"
"log"
"time"

"github.com/opiproject/godpu/network"
"github.com/spf13/cobra"
)

// CreateBridgePort creates an Bridge Port an OPI server
func CreateBridgePort() *cobra.Command {
var addr string
var name string
var mac string
var bridgePortType string
var logicalBridges []string

cmd := &cobra.Command{
Use: "create-bp",
Short: "Create a bridge port",
Long: "Create a BridgePort with the specified name, MAC address, type, and VLAN IDs",
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
evpnClient, err := network.NewBridgePort(addr)
if err != nil {
log.Fatalf("could not create gRPC client: %v", err)
}
defer cancel()
// grpc call to create the bridge port
bridgePort, err := evpnClient.CreateBridgePort(ctx, name, mac, bridgePortType, logicalBridges)
if err != nil {
log.Fatalf("could not create Bridge Port: %v", err)
}
log.Printf("Created Bridge Port:\n status: %s\n type: %s\n name: %s\n bridges: %s\n mac: %s\n", bridgePort.GetStatus().GetOperStatus(), bridgePort.GetSpec().GetPtype(),
bridgePort.GetName(), bridgePort.GetSpec().GetLogicalBridges(), bridgePort.GetSpec().GetMacAddress())
},
}

cmd.Flags().StringVarP(&name, "name", "n", "", "Specify the name of the BridgePort")
cmd.Flags().StringVar(&mac, "mac", "", "Specify the MAC address")
cmd.Flags().StringVarP(&bridgePortType, "type", "t", "", "Specify the type (access or trunk)")
cmd.Flags().StringSliceVar(&logicalBridges, "logicalBridges", []string{}, "Specify VLAN IDs (multiple values supported)")
cmd.Flags().StringVar(&addr, "addr", "localhost:50151", "address of OPI gRPC server")

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

// Define allowed choices for the "type" Flag
err := cmd.RegisterFlagCompletionFunc("type", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"access", "trunk"}, cobra.ShellCompDirectiveNoFileComp
})
if err != nil {
log.Fatalf("Error registering flag completion function: %v", err)
}
return cmd
}

// DeleteBridgePort delete an Bridge Port an OPI server
func DeleteBridgePort() *cobra.Command {
var addr string
var name string
var allowMissing bool

cmd := &cobra.Command{
Use: "delete-bp",
Short: "Delete a bridge port",
Long: "Delete a BridgePort with the specified name",
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
evpnClient, err := network.NewBridgePort(addr)
if err != nil {
log.Fatalf("could not create gRPC client: %v", err)
}
defer cancel()

// grpc call to create the bridge port
_, err = evpnClient.DeleteBridgePort(ctx, name, allowMissing)
if err != nil {
log.Fatalf("DeleteBridgePort: Error occurred while deleting Bridge Port: %q", err)
}
log.Printf("Deleted BridgePort ")
},
}

cmd.Flags().StringVarP(&name, "name", "n", "", "Specify the name of the BridgePort")
cmd.Flags().BoolVarP(&allowMissing, "allowMissing", "a", false, "Specify if missing allowed")
cmd.Flags().StringVar(&addr, "addr", "localhost:50151", "address of OPI gRPC server")

return cmd
}

// GetBridgePort Get Bridge Port details
func GetBridgePort() *cobra.Command {
var addr string
var name string

cmd := &cobra.Command{
Use: "get-bp",
Short: "Show details of a bridge port",
Long: "Show details of a BridgePort with the specified name",
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
evpnClient, err := network.NewBridgePort(addr)
if err != nil {
log.Fatalf("could not create gRPC client: %v", err)
}
defer cancel()

// grpc call to create the bridge port
bridgePort, err := evpnClient.GetBridgePort(ctx, name)
if err != nil {
log.Fatalf("GetBridgePort: Error occurred while creating Bridge Port: %q", err)
}
log.Printf("Bridge Port:\n status: %s\n type: %s\n name: %s\n bridges: %s\n mac: %s\n", bridgePort.GetStatus().GetOperStatus(), bridgePort.GetSpec().GetPtype(),
bridgePort.GetName(), bridgePort.GetSpec().GetLogicalBridges(), bridgePort.GetSpec().GetMacAddress())
},
}

cmd.Flags().StringVarP(&name, "name", "n", "", "Specify the name of the BridgePort")
cmd.Flags().StringVar(&addr, "addr", "localhost:50151", "address of OPI gRPC server")

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

// ListBridgePorts list all the Bridge Port an OPI server
func ListBridgePorts() *cobra.Command {
var addr string
var pageSize int32
var pageToken string

cmd := &cobra.Command{
Use: "list-bps",
Short: "Show details of all bridge ports",
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
evpnClient, err := network.NewBridgePort(addr)
if err != nil {
log.Fatalf("could not create gRPC client: %v", err)
}
defer cancel()
for {
resp, err := evpnClient.ListBridgePorts(ctx, pageSize, pageToken)
if err != nil {
log.Fatalf("Failed to get items: %v", err)
}
// Process the server response
for _, bridgePort := range resp.BridgePorts {
log.Printf("Bridge Port:\n status: %s\n type: %s\n name: %s\n bridges: %s\n mac: %s\n", bridgePort.GetStatus().GetOperStatus(), bridgePort.GetSpec().GetPtype(),
bridgePort.GetName(), bridgePort.GetSpec().GetLogicalBridges(), bridgePort.GetSpec().GetMacAddress())
}

// Check if there are more pages to retrieve
if resp.NextPageToken == "" {
// No more pages, break the loop
break
}
// Update the page token for the next request
pageToken = resp.NextPageToken
}
},
}
cmd.Flags().Int32VarP(&pageSize, "pagesize", "s", 0, "Specify page size")
cmd.Flags().StringVarP(&pageToken, "pagetoken", "t", "", "Specify the token")
cmd.Flags().StringVar(&addr, "addr", "localhost:50151", "address of OPI gRPC server")
return cmd
}

// UpdateBridgePort update the Bridge Port on OPI server
func UpdateBridgePort() *cobra.Command {
var addr string
var name string
var updateMask []string
var allowMissing bool

cmd := &cobra.Command{
Use: "update-bp",
Short: "Update the bridge port",
Long: "updates the Bridge Port with updated mask",
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
evpnClient, err := network.NewBridgePort(addr)
if err != nil {
log.Fatalf("could not create gRPC client: %v", err)
}
defer cancel()

// grpc call to create the bridge port
bridgePort, err := evpnClient.UpdateBridgePort(ctx, name, updateMask, allowMissing)
if err != nil {
log.Fatalf("UpdateBridgePort: Error occurred while creating Bridge Port: %q", err)
}
log.Printf("Bridge Port:\n status: %s\n type: %s\n name: %s\n bridges: %s\n mac: %s\n", bridgePort.GetStatus().GetOperStatus(), bridgePort.GetSpec().GetPtype(),
bridgePort.GetName(), bridgePort.GetSpec().GetLogicalBridges(), bridgePort.GetSpec().GetMacAddress())
},
}
cmd.Flags().StringVar(&name, "name", "", "name of the Bridge Port")
cmd.Flags().StringSliceVar(&updateMask, "update-mask", nil, "update mask")
cmd.Flags().BoolVarP(&allowMissing, "allowMissing", "a", false, "allow the missing")
cmd.Flags().StringVar(&addr, "addr", "localhost:50151", "address of OPI gRPC server")
return cmd
}
Loading

0 comments on commit a94c5e2

Please sign in to comment.