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

257 lines
5.6 KiB
Go
Raw Normal View History

2017-05-18 12:52:10 +02:00
package api
import (
2018-10-16 18:21:20 +02:00
"fmt"
2017-05-18 12:52:10 +02:00
"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"`
2018-12-09 17:31:02 +01:00
RouteserverId string `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 {
2018-12-09 17:31:02 +01:00
Id string `json:"id"`
Name string `json:"name"`
2018-10-21 11:29:43 +02:00
Group string `json:"group"`
Blackholes []string `json:"blackholes"`
2018-12-09 18:47:40 +01:00
Order int `json:"-"`
}
type Routeservers []Routeserver
// Implement sorting interface for routeservers
func (rs Routeservers) Len() int {
return len(rs)
}
func (rs Routeservers) Less(i, j int) bool {
return rs[i].Order < rs[j].Order
}
func (rs Routeservers) Swap(i, j int) {
rs[i], rs[j] = rs[j], rs[i]
2017-05-18 12:52:10 +02:00
}
type RouteserversResponse struct {
Routeservers []Routeserver `json:"routeservers"`
}
// BGP
type Community []int
2018-10-16 18:21:20 +02:00
func (com Community) String() string {
res := ""
for _, v := range com {
res += fmt.Sprintf(":%d", v)
}
return res[1:]
}
2018-11-11 17:48:34 +01:00
type Communities []Community
/*
Deduplicate communities
*/
func (communities Communities) Unique() Communities {
seen := map[string]bool{}
result := make(Communities, 0, len(communities))
for _, com := range communities {
key := com.String()
if _, ok := seen[key]; !ok {
// We have not seen this community yet
result = append(result, com)
seen[key] = true
}
}
return result
}
type ExtCommunity []interface{}
2017-05-18 12:52:10 +02:00
2018-10-16 18:21:20 +02:00
func (com ExtCommunity) String() string {
res := ""
for _, v := range com {
res += fmt.Sprintf(":%v", v)
}
return res[1:]
}
2018-11-11 17:48:34 +01:00
type ExtCommunities []ExtCommunity
func (communities ExtCommunities) Unique() ExtCommunities {
seen := map[string]bool{}
result := make(ExtCommunities, 0, len(communities))
for _, com := range communities {
key := com.String()
if _, ok := seen[key]; !ok {
// We have not seen this community yet
result = append(result, com)
seen[key] = true
}
}
return result
}
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"`
2018-11-11 17:48:34 +01:00
Communities Communities `json:"communities"`
LargeCommunities Communities `json:"large_communities"`
ExtCommunities ExtCommunities `json:"ext_communities"`
LocalPref int `json:"local_pref"`
Med int `json:"med"`
2017-05-18 12:52:10 +02:00
}
2018-10-17 11:05:48 +02:00
func (bgp BgpInfo) HasCommunity(community Community) bool {
if len(community) != 2 {
return false // This can never match.
}
for _, com := range bgp.Communities {
if len(com) != len(community) {
continue // This can't match.
}
if com[0] == community[0] &&
com[1] == community[1] {
return true
}
}
return false
}
func (bgp BgpInfo) HasExtCommunity(community ExtCommunity) bool {
if len(community) != 3 {
return false // This can never match.
}
for _, com := range bgp.ExtCommunities {
if len(com) != len(community) {
continue // This can't match.
}
if com[0] == community[0] &&
com[1] == community[1] &&
com[2] == community[2] {
return true
}
}
return false
}
func (bgp BgpInfo) HasLargeCommunity(community Community) bool {
// TODO: This is an almost 1:1 match to the function above.
if len(community) != 3 {
return false // This can never match.
}
for _, com := range bgp.LargeCommunities {
if len(com) != len(community) {
continue // This can't match.
}
if com[0] == community[0] &&
com[1] == community[1] &&
com[2] == community[2] {
return true
}
}
return false
}