1
+ // https://github.com/osrg/gobgp/blob/master/docs/sources/lib.md
1
2
package main
2
3
3
4
import (
4
5
"context"
5
6
"fmt"
6
7
7
- "github.com/golang/protobuf/ptypes"
8
- "github.com/golang/protobuf/ptypes/any"
9
- api "github.com/osrg/gobgp/api"
10
- gobgp "github.com/osrg/gobgp/pkg/server"
8
+ api "github.com/osrg/gobgp/v3/api"
9
+ gobgp "github.com/osrg/gobgp/v3/pkg/server"
11
10
log "github.com/sirupsen/logrus"
11
+ apb "google.golang.org/protobuf/types/known/anypb"
12
12
)
13
13
14
14
var (
@@ -19,14 +19,14 @@ type BgpServer struct {
19
19
server * gobgp.BgpServer
20
20
}
21
21
22
- func initBgpServer (routerId string , as uint32 , listenPort int32 ) (* BgpServer , error ) {
22
+ func initBgpServer (routerId string , asn uint32 , listenPort int32 ) (* BgpServer , error ) {
23
23
s := gobgp .NewBgpServer ()
24
24
go s .Serve ()
25
25
26
26
// global configuration
27
27
if err := s .StartBgp (context .Background (), & api.StartBgpRequest {
28
28
Global : & api.Global {
29
- As : as ,
29
+ Asn : asn ,
30
30
RouterId : routerId ,
31
31
ListenPort : listenPort ,
32
32
},
@@ -35,36 +35,40 @@ func initBgpServer(routerId string, as uint32, listenPort int32) (*BgpServer, er
35
35
}
36
36
37
37
// monitor the change of the peer state
38
- if err := s .MonitorPeer (context .Background (), & api.MonitorPeerRequest {}, func (p * api.Peer ) { log .Info (p ) }); err != nil {
38
+ if err := s .WatchEvent (context .Background (), & api.WatchEventRequest {Peer : & api.WatchEventRequest_Peer {}}, func (r * api.WatchEventResponse ) {
39
+ if p := r .GetPeer (); p != nil && p .Type == api .WatchEventResponse_PeerEvent_STATE {
40
+ log .Info (p )
41
+ }
42
+ }); err != nil {
39
43
log .Fatal (err )
40
44
}
41
45
42
46
return & BgpServer {server : s }, nil
43
47
}
44
48
45
- func (bs * BgpServer ) AddPeer (address string , as uint32 ) error {
49
+ func (bs * BgpServer ) AddPeer (address string , asn uint32 ) error {
46
50
n := & api.Peer {
47
51
Conf : & api.PeerConf {
48
52
NeighborAddress : address ,
49
- PeerAs : as ,
53
+ PeerAsn : asn ,
50
54
},
51
55
}
52
56
return bs .server .AddPeer (context .Background (), & api.AddPeerRequest {Peer : n })
53
57
}
54
58
55
59
func (bs * BgpServer ) AddV4Path (prefix string , prefixLen uint32 , nextHop string ) error {
56
- nlri , _ := ptypes . MarshalAny (& api.IPAddressPrefix {
60
+ nlri , _ := apb . New (& api.IPAddressPrefix {
57
61
Prefix : prefix ,
58
62
PrefixLen : prefixLen ,
59
63
})
60
64
61
- a1 , _ := ptypes . MarshalAny (& api.OriginAttribute {
65
+ a1 , _ := apb . New (& api.OriginAttribute {
62
66
Origin : 0 , // the prefix originates from an interior routing protocol (IGP)
63
67
})
64
- a2 , _ := ptypes . MarshalAny (& api.NextHopAttribute {
68
+ a2 , _ := apb . New (& api.NextHopAttribute {
65
69
NextHop : nextHop ,
66
70
})
67
- attrs := []* any .Any {a1 , a2 }
71
+ attrs := []* apb .Any {a1 , a2 }
68
72
69
73
_ , err := bs .server .AddPath (context .Background (), & api.AddPathRequest {
70
74
Path : & api.Path {
@@ -81,18 +85,18 @@ func (bs *BgpServer) AddV4Path(prefix string, prefixLen uint32, nextHop string)
81
85
}
82
86
83
87
func (bs * BgpServer ) DeleteV4Path (prefix string , prefixLen uint32 , nextHop string ) error {
84
- nlri , _ := ptypes . MarshalAny (& api.IPAddressPrefix {
88
+ nlri , _ := apb . New (& api.IPAddressPrefix {
85
89
Prefix : prefix ,
86
90
PrefixLen : prefixLen ,
87
91
})
88
92
89
- a1 , _ := ptypes . MarshalAny (& api.OriginAttribute {
93
+ a1 , _ := apb . New (& api.OriginAttribute {
90
94
Origin : 0 , // the prefix originates from an interior routing protocol (IGP)
91
95
})
92
- a2 , _ := ptypes . MarshalAny (& api.NextHopAttribute {
96
+ a2 , _ := apb . New (& api.NextHopAttribute {
93
97
NextHop : nextHop ,
94
98
})
95
- attrs := []* any .Any {a1 , a2 }
99
+ attrs := []* apb .Any {a1 , a2 }
96
100
97
101
err := bs .server .DeletePath (context .Background (), & api.DeletePathRequest {
98
102
Path : & api.Path {
0 commit comments