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_neighbors.go

121 lines
3.0 KiB
Go
Raw Normal View History

2018-10-23 18:02:56 +02:00
package api
import (
2021-04-15 17:46:52 +02:00
"encoding/json"
2019-10-07 17:32:56 +02:00
"strings"
2018-10-23 18:02:56 +02:00
"time"
)
// Neighbours
type Neighbours []*Neighbour
type Neighbour struct {
Id string `json:"id"`
// Mandatory fields
2019-09-25 01:53:21 +02: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"`
Uptime time.Duration `json:"uptime"`
LastError string `json:"last_error"`
2019-10-05 12:09:25 +02:00
RouteServerId string `json:"routeserver_id"`
2018-10-23 18:02:56 +02:00
// Original response
Details map[string]interface{} `json:"details"`
}
2021-04-15 17:46:52 +02:00
// String encodes a neighbor as json. This is
// more readable than the golang default represenation.
func (n *Neighbour) String() string {
repr, _ := json.Marshal(n)
return string(repr)
}
2018-10-23 18:02:56 +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]
}
type NeighboursResponse struct {
Api ApiStatus `json:"api"`
Neighbours Neighbours `json:"neighbours"`
}
2019-09-27 11:17:48 +02:00
// Implement Filterable interface
func (self *Neighbour) MatchSourceId(id string) bool {
2019-10-05 12:16:57 +02:00
return self.RouteServerId == id
2019-09-27 11:17:48 +02:00
}
func (self *Neighbour) MatchAsn(asn int) bool {
return self.Asn == asn
}
func (self *Neighbour) MatchCommunity(_community Community) bool {
return true // Ignore
}
func (self *Neighbour) MatchExtCommunity(_community Community) bool {
return true // Ignore
}
func (self *Neighbour) MatchLargeCommunity(_community Community) bool {
return true // Ignore
}
2019-10-07 17:32:56 +02:00
func (self *Neighbour) MatchName(name string) bool {
name = strings.ToLower(name)
neighName := strings.ToLower(self.Description)
return strings.Contains(neighName, name)
}
2018-10-23 18:02:56 +02:00
// Neighbours response is cacheable
2021-03-22 17:35:20 +01:00
func (self *NeighboursResponse) CacheTTL() time.Duration {
2018-10-23 18:02:56 +02:00
now := time.Now().UTC()
return self.Api.Ttl.Sub(now)
}
2018-12-09 17:31:02 +01:00
type NeighboursLookupResults map[string]Neighbours
type NeighboursStatus []*NeighbourStatus
2021-04-26 11:40:12 +02:00
// NeighbourStatus contains only the neighbor state and
// uptime.
type NeighbourStatus struct {
Id string `json:"id"`
State string `json:"state"`
Since time.Duration `json:"uptime"`
}
// Implement sorting interface for status
func (neighbours NeighboursStatus) Len() int {
return len(neighbours)
}
func (neighbours NeighboursStatus) Less(i, j int) bool {
return neighbours[i].Id < neighbours[j].Id
}
func (neighbours NeighboursStatus) Swap(i, j int) {
neighbours[i], neighbours[j] = neighbours[j], neighbours[i]
}
type NeighboursStatusResponse struct {
Api ApiStatus `json:"api"`
Neighbours NeighboursStatus `json:"neighbours"`
}