Skip to content

Commit

Permalink
feat(mgmt): adding Mgmt api for opi-evpn-bridge netlink DB dump
Browse files Browse the repository at this point in the history
Signed-off-by: atulpatel261194 <[email protected]>
  • Loading branch information
atulpatel261194 committed Nov 19, 2024
1 parent 4918e77 commit 7ccabc8
Show file tree
Hide file tree
Showing 7 changed files with 549 additions and 0 deletions.
57 changes: 57 additions & 0 deletions cmd/network/evpn-mgmt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2022-2023 Intel Corporation, or its subsidiaries.
// Copyright (c) 2022-2023 Dell Inc, or its subsidiaries.
// Copyright (c) 2024 Ericsson AB.

// Package network implements the network related CLI commands
package network

import (
"context"
"log"
"time"

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

// DumpNetlinkDatabase Get netlink database details
func DumpNetlinkDatabase() *cobra.Command {
var details bool

cmd := &cobra.Command{
Use: "dump-netlink-DB",
Short: "Show details of a netlink database",
Long: "Show details of netlink database with current running config",
Run: func(c *cobra.Command, _ []string) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)

tlsFiles, err := c.Flags().GetString(common.TLSFiles)
cobra.CheckErr(err)

addr, err := c.Flags().GetString(common.AddrCmdLineArg)
cobra.CheckErr(err)

evpnClient, err := network.NewManagement(addr, tlsFiles)
if err != nil {
log.Fatalf("could not create gRPC client: %v", err)
}
defer cancel()

// grpc call to create the bridge port
result, err := evpnClient.DumpNetlinkDB(ctx, details)
if err != nil {
log.Fatalf("DumpNetlinkDatabase: Error occurred while dumping the netlink database: %q", err)
}
log.Printf("DumpNetlinkDatabase: %s", result.Details)
},
}

cmd.Flags().BoolVar(&details, "details", false, "get the dump with details")

if err := cmd.MarkFlagRequired("details"); err != nil {
log.Fatalf("Error marking flag as required: %v", err)
}
return cmd
}
2 changes: 2 additions & 0 deletions cmd/network/evpn-test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func NewEvpnCommand() *cobra.Command {
cmd.AddCommand(GetSVI())
cmd.AddCommand(ListSVIs())
cmd.AddCommand(UpdateSVI())
// Mgmt cli's
cmd.AddCommand(DumpNetlinkDatabase())

return cmd
}
38 changes: 38 additions & 0 deletions network/evpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"errors"

grpcOpi "github.com/opiproject/godpu/grpc"

pm "github.com/opiproject/godpu/network/proto/gen"
pb "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go"
"go.einride.tech/aip/resourcename"
"google.golang.org/grpc"
Expand All @@ -28,12 +30,16 @@ type PbEvpnVRFClientGetter func(c grpc.ClientConnInterface) pb.VrfServiceClient
// PbEvpnSVIClientGetter defines the function type used to retrieve an evpn protobuf SviServiceClient
type PbEvpnSVIClientGetter func(c grpc.ClientConnInterface) pb.SviServiceClient

// PbEvpnMgmt defines the function type used to retrieve an evpn protobuf ManagementServiceClient
type PbEvpnMgmt func(c grpc.ClientConnInterface) pm.ManagementServiceClient

type evpnClientImpl struct {
// getEvpnClient PbEvpnClientGetter
getEvpnLogicalBridgeClient PbEvpnLogicalBridgeClientGetter
getEvpnBridgePortClient PbEvpnBridgePortClientGetter
getEvpnVRFClient PbEvpnVRFClientGetter
getEvpnSVIClient PbEvpnSVIClientGetter
getEvpnMgmtClient PbEvpnMgmt
grpcOpi.Connector
}

Expand Down Expand Up @@ -67,6 +73,9 @@ type EvpnClient interface {
GetSvi(ctx context.Context, name string) (*pb.Svi, error)
ListSvis(ctx context.Context, pageSize int32, pageToken string) (*pb.ListSvisResponse, error)
UpdateSvi(ctx context.Context, name string, updateMask []string, allowMissing bool) (*pb.Svi, error)

// Management Interface
DumpNetlinkDB(ctx context.Context, details bool) (*pm.DumpNetlinkDbResult, error)
}

func resourceIDToFullName(container string, resourceID string) string {
Expand Down Expand Up @@ -105,6 +114,35 @@ func NewLogicalBridgeWithArgs(c grpcOpi.Connector, getter PbEvpnLogicalBridgeCli
}, nil
}

// NewManagement creates an evpn Logical Bridge client for use with OPI server at the given address
func NewManagement(addr string, tls string) (EvpnClient, error) {
c, err := grpcOpi.New(addr, tls)
if err != nil {
return nil, err
}

// Default is to use the OPI grpc client and the pb client generated from the protobuf spec

return NewManagementWithArgs(c, pm.NewManagementServiceClient)
}

// NewManagementWithArgs creates an evpn Mgmt client for use with OPI server using the given gRPC client and the given function for
// retrieving an evpn protobuf client
func NewManagementWithArgs(c grpcOpi.Connector, getter PbEvpnMgmt) (EvpnClient, error) {
if c == nil {
return nil, errors.New("grpc connector is nil")
}

if getter == nil {
return nil, errors.New("protobuf client getter is nil")
}

return evpnClientImpl{
getEvpnMgmtClient: getter,
Connector: c,
}, nil
}

// NewBridgePort creates an evpn Bridge Port client for use with OPI server at the given address
func NewBridgePort(addr string, tls string) (EvpnClient, error) {
c, err := grpcOpi.New(addr, tls)
Expand Down
34 changes: 34 additions & 0 deletions network/mgmt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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 network implements the go library for OPI to be used to establish networking
package network

import (
"context"
"log"

pm "github.com/opiproject/godpu/network/proto/gen"
)

// DumpNetlinkDb get netlink DB details from OPI server
func (c evpnClientImpl) DumpNetlinkDB(ctx context.Context, details bool) (*pm.DumpNetlinkDbResult, error) {
conn, closer, err := c.NewConn()
if err != nil {
log.Printf("error creating connection: %s\n", err)
return nil, err
}
defer closer()

client := c.getEvpnMgmtClient(conn)
data, err := client.DumpNetlinkDb(ctx, &pm.DumpNetlinkDbRequest{
Details: details,
})
if err != nil {
log.Printf("error getting vrf: %s\n", err)
return nil, err
}

return data, nil
}
Loading

0 comments on commit 7ccabc8

Please sign in to comment.