summaryrefslogtreecommitdiffhomepage
path: root/docs
diff options
context:
space:
mode:
authorFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2018-08-10 13:38:13 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2018-08-10 13:38:13 +0900
commit834fb80a886b0234980fe5368419b81705ed5a23 (patch)
treecc2e05419bf593a44b11325f3973aebd266fe4f4 /docs
parent840807a520732ab09e83709382f7da1dc4a22ed0 (diff)
fix library usage example
also enable the CI for it again. Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
Diffstat (limited to 'docs')
-rw-r--r--docs/sources/lib.md82
1 files changed, 43 insertions, 39 deletions
diff --git a/docs/sources/lib.md b/docs/sources/lib.md
index b7b16698..2b79c092 100644
--- a/docs/sources/lib.md
+++ b/docs/sources/lib.md
@@ -12,13 +12,14 @@ This page explains how to use GoBGP as a Go Native BGP library.
package main
import (
- "fmt"
- log "github.com/sirupsen/logrus"
- "github.com/osrg/gobgp/config"
- "github.com/osrg/gobgp/packet/bgp"
- gobgp "github.com/osrg/gobgp/server"
- "github.com/osrg/gobgp/table"
+ "context"
"time"
+
+ "github.com/golang/protobuf/ptypes"
+ "github.com/golang/protobuf/ptypes/any"
+ api "github.com/osrg/gobgp/api"
+ gobgp "github.com/osrg/gobgp/pkg/server"
+ log "github.com/sirupsen/logrus"
)
func main() {
@@ -32,53 +33,56 @@ func main() {
go g.Serve()
// global configuration
- global := &config.Global{
- Config: config.GlobalConfig{
- As: 65000,
- RouterId: "10.0.255.254",
- Port: -1, // gobgp won't listen on tcp:179
+ if err := s.StartBgp(context.Background(), &api.StartBgpRequest{
+ Global: &api.Global{
+ As: 65003,
+ RouterId: "10.0.255.254",
+ ListenPort: -1, // gobgp won't listen on tcp:179
},
- }
-
- if err := s.Start(global); err != nil {
+ }); err != nil {
log.Fatal(err)
}
// neighbor configuration
- n := &config.Neighbor{
- Config: config.NeighborConfig{
- NeighborAddress: "10.0.255.1",
- PeerAs: 65001,
+ n := &api.Peer{
+ Conf: &api.PeerConf{
+ NeighborAddress: "172.17.0.2",
+ PeerAs: 65002,
},
}
- if err := s.AddNeighbor(n); err != nil {
+ if err := s.AddPeer(context.Background(), &api.AddPeerRequest{
+ Peer: n,
+ }); err != nil {
log.Fatal(err)
}
// add routes
- 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 {
+ nlri, _ := ptypes.MarshalAny(&api.IPAddressPrefix{
+ Prefix: "10.0.0.0",
+ PrefixLen: 24,
+ })
+
+ a1, _ := ptypes.MarshalAny(&api.OriginAttribute{
+ Origin: 0,
+ })
+ a2, _ := ptypes.MarshalAny(&api.NextHopAttribute{
+ NextHop: "10.0.0.1",
+ })
+ attrs := []*any.Any{a1, a2}
+
+ _, err := s.AddPath(context.Background(), &api.AddPathRequest{
+ Path: &api.Path{
+ Family: uint32(api.Family_IPv4),
+ AnyNlri: nlri,
+ AnyPattrs: attrs,
+ },
+ })
+ if err != nil {
log.Fatal(err)
}
- // monitor new routes
- w := s.Watch(gobgp.WatchBestPath(false))
- for {
- select {
- case ev := <-w.Event():
- switch msg := ev.(type) {
- case *gobgp.WatchEventBestPath:
- for _, path := range msg.PathList {
- // do something useful
- fmt.Println(path)
- }
- }
- }
- }
+ // do something useful here instead of exiting
+ time.Sleep(time.Minute * 3)
}
```