Skip to content

Instantly share code, notes, and snippets.

@nnao45
Last active October 31, 2017 15:56
Show Gist options
  • Select an option

  • Save nnao45/a94c986684780cbdab8caff5f9bf538f to your computer and use it in GitHub Desktop.

Select an option

Save nnao45/a94c986684780cbdab8caff5f9bf538f to your computer and use it in GitHub Desktop.
AddPath() in multi path args.
package main
import (
"fmt"
api "github.com/osrg/gobgp/api"
"github.com/osrg/gobgp/config"
"github.com/osrg/gobgp/gobgp/cmd"
"github.com/osrg/gobgp/packet/bgp"
gobgp "github.com/osrg/gobgp/server"
"github.com/osrg/gobgp/table"
"github.com/satori/go.uuid"
"golang.org/x/net/context"
"google.golang.org/grpc"
"os"
"time"
)
func main() {
s := gobgp.NewBgpServer()
go s.Serve()
g := api.NewGrpcServer(s, ":50051")
go g.Serve()
global := &config.Global{
Config: config.GlobalConfig{
As: 65056,
RouterId: "10.0.4.44",
Port: -1, // gobgp won't listen on tcp:179
},
}
if err := s.Start(global); err != nil {
fmt.Println(err)
}
timeout := grpc.WithTimeout(time.Second)
conn, err := grpc.Dial("localhost:50051", timeout, grpc.WithBlock(), grpc.WithInsecure())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
args1 := []string{"10.10.0.0/24"}
path1, err := cmd.ParsePath(bgp.RF_IPv4_UC, args1)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
args2 := []string{"10.20.0.0/24"}
path2, err := cmd.ParsePath(bgp.RF_IPv4_UC, args2)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
args3 := []string{"10.30.0.0/24"}
path3, err := cmd.ParsePath(bgp.RF_IPv4_UC, args3)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
b1, _ := s.AddPath("", []*table.Path{path1, path2})
u1, _ := uuid.FromBytes(b1)
fmt.Println("Added a route's UUID is, ", u1.String())
b2, _ := s.AddPath("", []*table.Path{path3})
u2, _ := uuid.FromBytes(b2)
fmt.Println("Added a route's UUID is, ", u2.String())
client := api.NewGobgpApiClient(conn)
r, err := client.GetRib(context.Background(), &api.GetRibRequest{
Table: &api.Table{
Type: api.Resource_GLOBAL,
Family: uint32(bgp.RF_IPv4_UC),
Name: "",
},
})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
t, err := r.Table.ToNativeTable()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for _, d := range t.GetSortedDestinations() {
var ps []*table.Path
ps = d.GetAllKnownPathList()
for _, s := range ps {
fmt.Printf("Show a route's Prefix is %s, UUID is %s\n", fmt.Sprint(s.GetNlri()), s.UUID().String())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment