@@ -12,6 +12,7 @@ import (
1212 log "github.com/sirupsen/logrus"
1313
1414 bnet "github.com/bio-routing/bio-rd/net"
15+ "github.com/bio-routing/bio-rd/protocols/bgp/api"
1516 "github.com/bio-routing/bio-rd/protocols/bgp/packet"
1617 bmppkt "github.com/bio-routing/bio-rd/protocols/bmp/packet"
1718 "github.com/bio-routing/bio-rd/routingtable"
@@ -128,6 +129,48 @@ func (r *Router) serve(con net.Conn) {
128129 }
129130}
130131
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+
131174func (r * Router ) processMsg (msg []byte ) {
132175 bmpMsg , err := bmppkt .Decode (msg )
133176 if err != nil {
0 commit comments