From 5aea2d93fd6fa41cef42cfb311e4b185725672ac Mon Sep 17 00:00:00 2001 From: FUJITA Tomonori Date: Wed, 27 Jul 2016 10:28:01 +0900 Subject: doc: update lib.md with the new native APIs Signed-off-by: FUJITA Tomonori --- docs/sources/lib.md | 89 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 37 deletions(-) (limited to 'docs/sources') diff --git a/docs/sources/lib.md b/docs/sources/lib.md index b1f80ca1..34b0225a 100644 --- a/docs/sources/lib.md +++ b/docs/sources/lib.md @@ -11,11 +11,13 @@ This page explains how to use GoBGP as a Go Native BGP library. package main import ( + "fmt" log "github.com/Sirupsen/logrus" - api "github.com/osrg/gobgp/api" - "github.com/osrg/gobgp/gobgp/cmd" + "github.com/osrg/gobgp/config" "github.com/osrg/gobgp/packet/bgp" gobgp "github.com/osrg/gobgp/server" + "github.com/osrg/gobgp/table" + "time" ) func main() { @@ -29,57 +31,70 @@ func main() { go g.Serve() // global configuration - req := gobgp.NewGrpcRequest(gobgp.REQ_START_SERVER, "", bgp.RouteFamily(0), &api.StartServerRequest{ - Global: &api.Global{ - As: 65003, - RouterId: "192.168.0.4", - ListenPort: -1, // gobgp won't listen on tcp:179 + b := &config.BgpConfigSet{ + Global: config.Global{ + Config: config.GlobalConfig{ + As: 65000, + RouterId: "10.0.255.254", + Port: -1, // gobgp won't listen on tcp:179 + }, }, - }) - s.GrpcReqCh <- req - res := <-req.ResponseCh - if err := res.Err(); err != nil { + } + + if err := config.SetDefaultConfigValues(nil, b); err != nil { + log.Fatal(err) + } + + if err := s.Start(&b.Global); err != nil { log.Fatal(err) } // neighbor configuration - req = gobgp.NewGrpcRequest(gobgp.REQ_ADD_NEIGHBOR, "", bgp.RouteFamily(0), &api.AddNeighborRequest{ - Peer: &api.Peer{ - Conf: &api.PeerConf{ - NeighborAddress: "192.168.0.3", - PeerAs: 65000, + if err := s.AddNeighbor(&config.Neighbor{ + Config: config.NeighborConfig{ + NeighborAddress: "10.0.255.1", + PeerAs: 65001, + }, + Timers: config.Timers{ + Config: config.TimersConfig{ + HoldTime: 90, + KeepaliveInterval: 30, }, - Transport: &api.Transport{ - LocalAddress: "192.168.0.4", + }, + AfiSafis: []config.AfiSafi{ + config.AfiSafi{ + Config: config.AfiSafiConfig{ + AfiSafiName: config.AFI_SAFI_TYPE_IPV4_UNICAST, + }, }, }, - }) - s.GrpcReqCh <- req - res = <-req.ResponseCh - if err := res.Err(); err != nil { + }); err != nil { log.Fatal(err) } // add routes - path, _ := cmd.ParsePath(bgp.RF_IPv4_UC, []string{"10.0.0.0/24", "nexthop", "10.10.10.10"}) - req = gobgp.NewGrpcRequest(gobgp.REQ_ADD_PATH, "", bgp.RouteFamily(0), &api.AddPathRequest{ - Resource: api.Resource_GLOBAL, - Path: path, - }) - s.GrpcReqCh <- req - res = <-req.ResponseCh - if err := res.Err(); err != nil { + attrs := []bgp.PathAttributeInterface{ + bgp.NewPathAttributeOrigin(0), + bgp.NewPathAttributeNextHop("10.0.255.254"), + bgp.NewPathAttributeAsPath([]bgp.AsPathParamInterface{bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, []uint32{4000, 400000, 300000, 40001})}), + } + if _, err := s.AddPath("", []*table.Path{table.NewPath(nil, bgp.NewIPAddrPrefix(24, "10.0.0.0"), false, attrs, time.Now(), false)}); err != nil { log.Fatal(err) } // monitor new routes - req = gobgp.NewGrpcRequest(gobgp.REQ_MONITOR_RIB, "", bgp.RF_IPv4_UC, &api.Table{ - Type: api.Resource_GLOBAL, - }) - s.GrpcReqCh <- req - for res := range req.ResponseCh { - p, _ := cmd.ApiStruct2Path(res.Data.(*api.Destination).Paths[0]) - cmd.ShowRoute(p, false, false, false, true, false) + w := s.Watch(gobgp.WatchBestPath()) + for { + select { + case ev := <-w.Event(): + switch msg := ev.(type) { + case *gobgp.WatchEventBestPath: + for _, path := range msg.PathList { + // do something useful + fmt.Println(path) + } + } + } } } ``` -- cgit v1.2.3