2017-05-18 12:52:10 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2017-05-18 15:26:22 +02:00
|
|
|
// General api response
|
|
|
|
type Response interface{}
|
|
|
|
|
2017-05-18 12:52:10 +02:00
|
|
|
// Details, usually the original backend response
|
|
|
|
type Details map[string]interface{}
|
|
|
|
|
2017-05-18 14:44:38 +02:00
|
|
|
// Error Handling
|
|
|
|
type ErrorResponse struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
2018-07-11 14:07:52 +02:00
|
|
|
// Cache aware api response
|
|
|
|
type CacheableResponse interface {
|
|
|
|
CacheTtl() time.Duration
|
|
|
|
}
|
|
|
|
|
2017-05-18 14:44:38 +02:00
|
|
|
// Config
|
2017-05-18 15:23:36 +02:00
|
|
|
type ConfigResponse struct {
|
2017-07-10 14:19:36 +01:00
|
|
|
Rejection Rejection `json:"rejection"`
|
|
|
|
RejectReasons map[string]string `json:"reject_reasons"`
|
2017-05-18 14:44:38 +02:00
|
|
|
|
2017-07-10 14:19:36 +01:00
|
|
|
Noexport Noexport `json:"noexport"`
|
|
|
|
NoexportReasons map[string]string `json:"noexport_reasons"`
|
2017-05-18 14:44:38 +02:00
|
|
|
|
2018-07-02 15:57:29 +02:00
|
|
|
NeighboursColumns map[string]string `json:"neighbours_columns"`
|
|
|
|
NeighboursColumnsOrder []string `json:"neighbours_columns_order"`
|
|
|
|
|
|
|
|
RoutesColumns map[string]string `json:"routes_columns"`
|
|
|
|
RoutesColumnsOrder []string `json:"routes_columns_order"`
|
2017-07-04 12:36:48 +02:00
|
|
|
|
2018-08-03 10:37:05 +02:00
|
|
|
LookupColumns map[string]string `json:"lookup_columns"`
|
|
|
|
LookupColumnsOrder []string `json:"lookup_columns_order"`
|
|
|
|
|
2017-07-04 12:36:48 +02:00
|
|
|
PrefixLookupEnabled bool `json:"prefix_lookup_enabled"`
|
2017-05-18 14:44:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Rejection struct {
|
|
|
|
Asn int `json:"asn"`
|
|
|
|
RejectId int `json:"reject_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Noexport struct {
|
|
|
|
Asn int `json:"asn"`
|
|
|
|
NoexportId int `json:"noexport_id"`
|
|
|
|
}
|
|
|
|
|
2017-05-18 12:52:10 +02:00
|
|
|
// Status
|
|
|
|
type ApiStatus struct {
|
|
|
|
Version string `json:"version"`
|
|
|
|
CacheStatus CacheStatus `json:"cache_status"`
|
|
|
|
ResultFromCache bool `json:"result_from_cache"`
|
2017-05-19 17:56:24 +02:00
|
|
|
Ttl time.Time `json:"ttl"`
|
2017-05-18 12:52:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type CacheStatus struct {
|
|
|
|
CachedAt time.Time `json:"cached_at"`
|
|
|
|
OrigTtl int `json:"orig_ttl"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Status struct {
|
|
|
|
ServerTime time.Time `json:"server_time"`
|
|
|
|
LastReboot time.Time `json:"last_reboot"`
|
|
|
|
LastReconfig time.Time `json:"last_reconfig"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
RouterId string `json:"router_id"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
Backend string `json:"backend"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type StatusResponse struct {
|
|
|
|
Api ApiStatus `json:"api"`
|
|
|
|
Status Status `json:"status"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Routeservers
|
|
|
|
type Routeserver struct {
|
|
|
|
Id int `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type RouteserversResponse struct {
|
|
|
|
Routeservers []Routeserver `json:"routeservers"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Neighbours
|
2018-07-06 17:04:09 +02:00
|
|
|
type Neighbours []*Neighbour
|
2017-05-22 16:21:48 +02:00
|
|
|
|
2017-05-18 12:52:10 +02:00
|
|
|
type Neighbour struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
|
|
|
|
// Mandatory fields
|
2018-02-15 14:38:00 +01:00
|
|
|
Address string `json:"address"`
|
|
|
|
Asn int `json:"asn"`
|
|
|
|
State string `json:"state"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
RoutesReceived int `json:"routes_received"`
|
|
|
|
RoutesFiltered int `json:"routes_filtered"`
|
|
|
|
RoutesExported int `json:"routes_exported"`
|
|
|
|
RoutesPreferred int `json:"routes_preferred"`
|
|
|
|
RoutesAccepted int `json:"routes_accepted"`
|
|
|
|
RoutesPipeFiltered int `json:"routes_pipe_filtered"`
|
|
|
|
Uptime time.Duration `json:"uptime"`
|
|
|
|
LastError string `json:"last_error"`
|
2017-05-18 12:52:10 +02:00
|
|
|
|
|
|
|
// Original response
|
|
|
|
Details map[string]interface{} `json:"details"`
|
|
|
|
}
|
|
|
|
|
2017-05-22 16:21:48 +02:00
|
|
|
// Implement sorting interface for routes
|
|
|
|
func (neighbours Neighbours) Len() int {
|
|
|
|
return len(neighbours)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (neighbours Neighbours) Less(i, j int) bool {
|
|
|
|
return neighbours[i].Asn < neighbours[j].Asn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (neighbours Neighbours) Swap(i, j int) {
|
|
|
|
neighbours[i], neighbours[j] = neighbours[j], neighbours[i]
|
|
|
|
}
|
|
|
|
|
2017-05-18 12:52:10 +02:00
|
|
|
type NeighboursResponse struct {
|
2017-05-22 16:21:48 +02:00
|
|
|
Api ApiStatus `json:"api"`
|
|
|
|
Neighbours Neighbours `json:"neighbours"`
|
2017-05-18 12:52:10 +02:00
|
|
|
}
|
|
|
|
|
2018-07-11 14:07:52 +02:00
|
|
|
// Neighbours response is cacheable
|
|
|
|
func (self *NeighboursResponse) CacheTtl() time.Duration {
|
|
|
|
now := time.Now().UTC()
|
|
|
|
return self.Api.Ttl.Sub(now)
|
|
|
|
}
|
|
|
|
|
2018-07-07 11:30:40 +02:00
|
|
|
type NeighboursLookupResults map[int]Neighbours
|
2017-06-29 14:57:08 +02:00
|
|
|
|
2017-05-18 12:52:10 +02:00
|
|
|
// BGP
|
|
|
|
type Community []int
|
|
|
|
|
|
|
|
type BgpInfo struct {
|
2017-05-22 16:52:09 +02:00
|
|
|
Origin string `json:"origin"`
|
2017-05-22 13:26:37 +02:00
|
|
|
AsPath []int `json:"as_path"`
|
|
|
|
NextHop string `json:"next_hop"`
|
|
|
|
Communities []Community `json:"communities"`
|
|
|
|
LargeCommunities []Community `json:"large_communities"`
|
|
|
|
LocalPref int `json:"local_pref"`
|
|
|
|
Med int `json:"med"`
|
2017-05-18 12:52:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prefixes
|
2017-05-18 18:10:06 +02:00
|
|
|
type Route struct {
|
2017-05-22 12:47:38 +02:00
|
|
|
Id string `json:"id"`
|
|
|
|
NeighbourId string `json:"neighbour_id"`
|
|
|
|
|
|
|
|
Network string `json:"network"`
|
|
|
|
Interface string `json:"interface"`
|
|
|
|
Gateway string `json:"gateway"`
|
|
|
|
Metric int `json:"metric"`
|
|
|
|
Bgp BgpInfo `json:"bgp"`
|
|
|
|
Age time.Duration `json:"age"`
|
|
|
|
Type []string `json:"type"` // [BGP, unicast, univ]
|
2018-08-02 14:26:28 +02:00
|
|
|
Primary bool `json:"primary"`
|
2017-05-22 12:47:38 +02:00
|
|
|
|
2017-05-18 12:52:10 +02:00
|
|
|
Details Details `json:"details"`
|
|
|
|
}
|
2017-05-18 18:10:06 +02:00
|
|
|
|
2018-07-07 11:30:40 +02:00
|
|
|
type Routes []*Route
|
|
|
|
|
|
|
|
// Implement sorting interface for routes
|
|
|
|
func (routes Routes) Len() int {
|
|
|
|
return len(routes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (routes Routes) Less(i, j int) bool {
|
|
|
|
return routes[i].Network < routes[j].Network
|
|
|
|
}
|
|
|
|
|
|
|
|
func (routes Routes) Swap(i, j int) {
|
|
|
|
routes[i], routes[j] = routes[j], routes[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
type RoutesResponse struct {
|
|
|
|
Api ApiStatus `json:"api"`
|
|
|
|
Imported Routes `json:"imported"`
|
|
|
|
Filtered Routes `json:"filtered"`
|
|
|
|
NotExported Routes `json:"not_exported"`
|
|
|
|
}
|
|
|
|
|
2018-07-11 14:07:52 +02:00
|
|
|
func (self *RoutesResponse) CacheTtl() time.Duration {
|
|
|
|
now := time.Now().UTC()
|
|
|
|
return self.Api.Ttl.Sub(now)
|
|
|
|
}
|
|
|
|
|
2018-07-13 16:45:05 +02:00
|
|
|
type PaginatedRoutesResponse struct {
|
|
|
|
*RoutesResponse
|
|
|
|
Pagination Pagination `json:"pagination"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Pagination struct {
|
|
|
|
Page int `json:"page"`
|
2018-07-18 11:45:14 +02:00
|
|
|
PageSize int `json:"page_size"`
|
2018-07-13 16:45:05 +02:00
|
|
|
TotalPages int `json:"total_pages"`
|
|
|
|
TotalResults int `json:"total_results"`
|
|
|
|
}
|
|
|
|
|
2017-06-19 15:27:11 +02:00
|
|
|
// Lookup Prefixes
|
|
|
|
type LookupRoute struct {
|
2018-07-06 17:04:09 +02:00
|
|
|
Id string `json:"id"`
|
|
|
|
NeighbourId string `json:"neighbour_id"`
|
|
|
|
Neighbour *Neighbour `json:"neighbour"`
|
2017-06-19 15:27:11 +02:00
|
|
|
|
2017-06-23 15:47:28 +02:00
|
|
|
State string `json:"state"` // Filtered, Imported, ...
|
2017-06-19 15:27:11 +02:00
|
|
|
|
2017-06-23 15:47:28 +02:00
|
|
|
Routeserver Routeserver `json:"routeserver"`
|
2017-06-19 15:27:11 +02:00
|
|
|
|
|
|
|
Network string `json:"network"`
|
|
|
|
Interface string `json:"interface"`
|
|
|
|
Gateway string `json:"gateway"`
|
|
|
|
Metric int `json:"metric"`
|
|
|
|
Bgp BgpInfo `json:"bgp"`
|
|
|
|
Age time.Duration `json:"age"`
|
|
|
|
Type []string `json:"type"` // [BGP, unicast, univ]
|
2018-08-02 14:30:40 +02:00
|
|
|
Primary bool `json:"primary"`
|
2017-06-19 15:27:11 +02:00
|
|
|
|
|
|
|
Details Details `json:"details"`
|
|
|
|
}
|
|
|
|
|
2018-07-07 11:30:40 +02:00
|
|
|
type LookupRoutes []*LookupRoute
|
2017-05-23 13:58:58 +02:00
|
|
|
|
2017-06-29 14:57:08 +02:00
|
|
|
type RoutesLookupResponse struct {
|
2018-07-07 11:30:40 +02:00
|
|
|
Api ApiStatus `json:"api"`
|
|
|
|
Routes LookupRoutes `json:"routes"`
|
2017-05-23 13:58:58 +02:00
|
|
|
}
|
2017-06-19 16:44:24 +02:00
|
|
|
|
2017-06-29 14:57:08 +02:00
|
|
|
type RoutesLookupResponseGlobal struct {
|
2018-07-07 11:30:40 +02:00
|
|
|
Routes LookupRoutes `json:"routes"`
|
2017-06-28 12:42:28 +02:00
|
|
|
|
|
|
|
// Pagination
|
|
|
|
TotalRoutes int `json:"total_routes"`
|
|
|
|
Limit int `json:"limit"`
|
|
|
|
Offset int `json:"offset"`
|
|
|
|
|
|
|
|
// Meta
|
|
|
|
Time float64 `json:"query_duration_ms"`
|
2017-06-19 16:44:24 +02:00
|
|
|
}
|