diff --git a/pkg/decoders/maps.go b/pkg/decoders/maps.go index ebee320..4b82066 100644 --- a/pkg/decoders/maps.go +++ b/pkg/decoders/maps.go @@ -18,7 +18,14 @@ func MapGet(m interface{}, key string, fallback interface{}) interface{} { // MapGetString retrievs a key from a map and // asserts its type is a string. Otherwise fallback // will be returned. -func MapGetString(m interface{}, key, fallback string) string { +func MapGetString(m interface{}, key string, fallback string) string { val := MapGet(m, key, fallback) return val.(string) } + +// MapGetBool will retrieve a boolean value +// for a given key. +func MapGetBool(m interface{}, key string, fallback bool) bool { + val := MapGet(m, key, fallback) + return val.(bool) +} diff --git a/pkg/sources/openbgpd/api.go b/pkg/sources/openbgpd/api.go index f2cf340..fd06a24 100644 --- a/pkg/sources/openbgpd/api.go +++ b/pkg/sources/openbgpd/api.go @@ -35,6 +35,16 @@ func (src *Source) ShowNeighborRIBInRequest( return http.NewRequestWithContext(ctx, http.MethodGet, url, nil) } +// ShowNeighborRIBRequest retrives the routes accepted from the neighbor +// identified by bgp-id. +func (src *Source) ShowNeighborRIBRequest( + ctx context.Context, + neighborID string, +) (*http.Request, error) { + url := src.cfg.APIURL("/v1/bgpd/show/rib/neighbor/%s/detail", neighborID) + return http.NewRequestWithContext(ctx, http.MethodGet, url, nil) +} + // ShowRIBRequest makes a request for retrieving all routes imported // from all peers func (src *Source) ShowRIBRequest(ctx context.Context) (*http.Request, error) { diff --git a/pkg/sources/openbgpd/decoders.go b/pkg/sources/openbgpd/decoders.go index 9d7551e..4113578 100644 --- a/pkg/sources/openbgpd/decoders.go +++ b/pkg/sources/openbgpd/decoders.go @@ -167,6 +167,9 @@ func decodeRoute(details map[string]interface{}) (*api.Route, error) { extendedCommunities := decodeExtendedCommunities( decoders.MapGet(details, "extended_communities", nil)) + // Is preferred route + isPrimary := decoders.MapGetBool(details, "best", false) + // Make bgp info bgpInfo := api.BgpInfo{ Origin: origin, @@ -186,7 +189,7 @@ func decodeRoute(details map[string]interface{}) (*api.Route, error) { Bgp: bgpInfo, Age: lastUpdate, Type: []string{origin}, - Primary: false, // TODO + Primary: isPrimary, Details: api.Details(details), } return r, nil diff --git a/pkg/sources/openbgpd/source.go b/pkg/sources/openbgpd/source.go index 1d660be..957f66d 100644 --- a/pkg/sources/openbgpd/source.go +++ b/pkg/sources/openbgpd/source.go @@ -156,7 +156,7 @@ func (src *Source) Routes(neighborID string) (*api.RoutesResponse, error) { } // Query RIB for routes received - req, err := src.ShowNeighborRIBInRequest(context.Background(), neighborID) + req, err := src.ShowNeighborRIBRequest(context.Background(), neighborID) if err != nil { return nil, err } @@ -193,7 +193,7 @@ func (src *Source) RoutesReceived(neighborID string) (*api.RoutesResponse, error } // Query RIB for routes received - req, err := src.ShowNeighborRIBInRequest(context.Background(), neighborID) + req, err := src.ShowNeighborRIBRequest(context.Background(), neighborID) if err != nil { return nil, err }