@@ -12,6 +12,7 @@ import (
12
12
log "github.com/sirupsen/logrus"
13
13
14
14
bnet "github.com/bio-routing/bio-rd/net"
15
+ "github.com/bio-routing/bio-rd/protocols/bgp/api"
15
16
"github.com/bio-routing/bio-rd/protocols/bgp/packet"
16
17
bmppkt "github.com/bio-routing/bio-rd/protocols/bmp/packet"
17
18
"github.com/bio-routing/bio-rd/routingtable"
@@ -128,6 +129,48 @@ func (r *Router) serve(con net.Conn) {
128
129
}
129
130
}
130
131
132
+ // GetNeighborSessions returns all neighbors as API session objects
133
+ func (r * Router ) GetNeighborSessions () []* api.Session {
134
+ sessions := make ([]* api.Session , 0 )
135
+
136
+ for _ , neigh := range r .neighborManager .list () {
137
+ estSince := neigh .fsm .establishedTime .Unix ()
138
+ if estSince < 0 {
139
+ estSince = 0
140
+ }
141
+
142
+ // for now get this from adjRibIn/adjRibOut, can be replaced when we
143
+ // bmp gets its own bgpSrv or Router gets the bmpMetricsService
144
+ var routesReceived , routesSent uint64
145
+ for _ , afi := range []uint16 {packet .IPv4AFI , packet .IPv6AFI } {
146
+ ribIn , err1 := r .GetNeighborRIBIn (neigh .fsm .peer .addr , afi , packet .UnicastSAFI )
147
+ if err1 == nil {
148
+ routesReceived += uint64 (ribIn .RouteCount ())
149
+ }
150
+
151
+ // adjRIBOut might not work properly with BMP, keeping it here for when it will
152
+ ribOut , err2 := r .GetNeighborRIBOut (neigh .fsm .peer .addr , afi , packet .UnicastSAFI )
153
+ if err2 == nil {
154
+ routesSent += uint64 (ribOut .RouteCount ())
155
+ }
156
+ }
157
+ session := & api.Session {
158
+ LocalAddress : neigh .fsm .peer .localAddr .ToProto (),
159
+ NeighborAddress : neigh .fsm .peer .addr .ToProto (),
160
+ LocalAsn : neigh .localAS ,
161
+ PeerAsn : neigh .peerAS ,
162
+ Status : stateToProto (neigh .fsm .state ),
163
+ Stats : & api.SessionStats {
164
+ RoutesReceived : routesReceived ,
165
+ RoutesExported : routesSent ,
166
+ },
167
+ EstablishedSince : uint64 (estSince ),
168
+ }
169
+ sessions = append (sessions , session )
170
+ }
171
+ return sessions
172
+ }
173
+
131
174
func (r * Router ) processMsg (msg []byte ) {
132
175
bmpMsg , err := bmppkt .Decode (msg )
133
176
if err != nil {
0 commit comments