1
0
mirror of https://github.com/alice-lg/alice-lg.git synced 2024-05-11 05:55:03 +00:00

306 lines
7.8 KiB
Go
Raw Normal View History

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 {
2018-08-05 18:25:59 +02:00
Message string `json:"message"`
Code int `json:"code"`
Tag string `json:"tag"`
RouteserverId int `json:"routeserver_id"`
2017-05-18 14:44:38 +02:00
}
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 {
2018-10-03 18:37:20 +02:00
Asn int `json:"asn"`
RejectReasons map[string]interface{} `json:"reject_reasons"`
2017-05-18 14:44:38 +02:00
Noexport Noexport `json:"noexport"`
NoexportReasons map[string]interface{} `json:"noexport_reasons"`
2017-05-18 14:44:38 +02:00
2018-10-02 15:52:46 +02:00
RejectCandidates RejectCandidates `json:"reject_candidates"`
2018-09-22 17:56:30 +02:00
Rpki Rpki `json:"rpki"`
BgpCommunities map[string]interface{} `json:"bgp_communities"`
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 Noexport struct {
LoadOnDemand bool `json:"load_on_demand"`
2017-05-18 14:44:38 +02:00
}
2018-10-02 15:52:46 +02:00
type RejectCandidates struct {
Communities map[string]interface{} `json:"communities"`
}
2018-09-22 17:56:30 +02:00
type Rpki struct {
2018-09-23 17:22:39 +02:00
Enabled bool `json:"enabled"`
2018-09-22 17:56:30 +02:00
Valid []string `json:"valid"`
Unknown []string `json:"unknown"`
NotChecked []string `json:"not_checked"`
Invalid []string `json:"invalid"`
}
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"`
Blackholes []string `json:"blackholes"`
2017-05-18 12:52:10 +02:00
}
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
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-05-18 12:52:10 +02:00
// BGP
type Community []int
type ExtCommunity []interface{}
2017-05-18 12:52:10 +02:00
type BgpInfo struct {
Origin string `json:"origin"`
AsPath []int `json:"as_path"`
NextHop string `json:"next_hop"`
Communities []Community `json:"communities"`
LargeCommunities []Community `json:"large_communities"`
ExtCommunities []ExtCommunity `json:"ext_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 {
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-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-09-24 22:29:13 +02:00
type TimedResponse struct {
RequestDuration float64 `json:"request_duration_ms"`
}
type Pagination struct {
Page int `json:"page"`
2018-07-18 11:45:14 +02:00
PageSize int `json:"page_size"`
TotalPages int `json:"total_pages"`
TotalResults int `json:"total_results"`
}
2018-09-24 22:29:13 +02:00
type PaginatedResponse struct {
Pagination Pagination `json:"pagination"`
}
type PaginatedRoutesResponse struct {
*RoutesResponse
Pagination Pagination `json:"pagination"`
}
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-09-24 22:36:11 +02:00
// Implement sorting interface for lookup routes
func (routes LookupRoutes) Len() int {
return len(routes)
}
func (routes LookupRoutes) Less(i, j int) bool {
return routes[i].Network < routes[j].Network
}
func (routes LookupRoutes) Swap(i, j int) {
routes[i], routes[j] = routes[j], routes[i]
}
2018-07-07 11:30:40 +02:00
type LookupRoutes []*LookupRoute
2017-05-23 13:58:58 +02:00
2018-09-24 22:29:13 +02:00
// TODO: Naming is a bit yuck
type LookupRoutesResponse struct {
*PaginatedResponse
Routes LookupRoutes `json:"routes"`
}
// TODO: Refactor this (might be legacy)
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
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
}
2018-09-24 22:29:13 +02:00
type PaginatedRoutesLookupResponse struct {
2018-09-25 21:26:11 +02:00
TimedResponse
Api ApiStatus `json:"api"` // Add to provide cache status information
2018-09-24 22:29:13 +02:00
Imported *LookupRoutesResponse `json:"imported"`
Filtered *LookupRoutesResponse `json:"filtered"`
}