Skip to content

Commit

Permalink
Add BGP-LS usage example
Browse files Browse the repository at this point in the history
  • Loading branch information
amakutunowicz committed Jan 31, 2019
1 parent 887e8a4 commit 48508c7
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -34,6 +34,7 @@ Try [a binary release](https://github.com/osrg/gobgp/releases/latest).
- [RPKI](docs/sources/rpki.md)
- [Managing GoBGP with your favorite language with gRPC](docs/sources/grpc-client.md)
- [Using GoBGP as a Go Native BGP library](docs/sources/lib.md)
- [Using BGP-LS in GoBGP library mode](docs/sources/lib-ls.md)
- [Graceful Restart](docs/sources/graceful-restart.md)
- [Additional Paths](docs/sources/add-paths.md)
- [Peer Group](docs/sources/peer-group.md)
Expand Down
98 changes: 98 additions & 0 deletions docs/sources/lib-ls.md
@@ -0,0 +1,98 @@
# Using BGP-LS in GoBGP library mode

This page explains how to use GoBGP for getting BGP-LS prefixes.

## Contents

- [Basic BGP-LS Example](#basic-bgp-ls-example)

## Basic BGP-LS Example

```go
package main

import (
"context"
"os"

"github.com/golang/protobuf/jsonpb"
api "github.com/osrg/gobgp/api"
gobgp "github.com/osrg/gobgp/pkg/server"
log "github.com/sirupsen/logrus"
)

func main() {
log.SetLevel(log.DebugLevel)
s := gobgp.NewBgpServer()
go s.Serve()

if err := s.StartBgp(context.Background(), &api.StartBgpRequest{
Global: &api.Global{
As: 64512,
RouterId: "10.0.255.254",
ListenPort: -1, // gobgp won't listen on tcp:179
},
}); err != nil {
log.Fatal(err)
}

if err := s.MonitorPeer(context.Background(), &api.MonitorPeerRequest{}, func(p *api.Peer) { log.Info(p) }); err != nil {
log.Fatal(err)
}

// neighbor configuration
n := &api.Peer{
Conf: &api.PeerConf{
NeighborAddress: "172.17.0.2",
PeerAs: 65002,
},
ApplyPolicy: &api.ApplyPolicy{
ImportPolicy: &api.PolicyAssignment{
DefaultAction: api.RouteAction_ACCEPT,
},
ExportPolicy: &api.PolicyAssignment{
DefaultAction: api.RouteAction_REJECT,
},
},
AfiSafis: []*api.AfiSafi{
{
Config: &api.AfiSafiConfig{
Family: &api.Family{
Afi: api.Family_AFI_LS,
Safi: api.Family_SAFI_LS,
},
Enabled: true,
},
},
},
}

if err := s.AddPeer(context.Background(), &api.AddPeerRequest{
Peer: n,
}); err != nil {
log.Fatal(err)
}

marshaller := jsonpb.Marshaler{
Indent: " ",
OrigName: true,
}

// Display incoming Prefixes in JSON format.
if err := s.MonitorTable(context.Background(), &api.MonitorTableRequest{
TableType: api.TableType_GLOBAL,
Family: &api.Family{
Afi: api.Family_AFI_LS,
Safi: api.Family_SAFI_LS,
},
}, func(p *api.Path) {
// Your application should do something useful with the BGP-LS path here.
marshaller.Marshal(os.Stdout, p)
}); err != nil {
log.Fatal(err)
}

select {}
}

```

0 comments on commit 48508c7

Please sign in to comment.