mirror of
https://github.com/alice-lg/alice-lg.git
synced 2024-05-11 05:55:03 +00:00
used pointers instead
This commit is contained in:
@ -220,7 +220,7 @@ func apiLookupPrefixGlobal(req *http.Request, params httprouter.Params) (api.Res
|
||||
t0 := time.Now()
|
||||
|
||||
// Perform query
|
||||
var routes []api.LookupRoute
|
||||
var routes []*api.LookupRoute
|
||||
if lookupPrefix {
|
||||
routes = AliceRoutesStore.LookupPrefix(q)
|
||||
|
||||
|
@ -81,7 +81,7 @@ type RouteserversResponse struct {
|
||||
}
|
||||
|
||||
// Neighbours
|
||||
type Neighbours []Neighbour
|
||||
type Neighbours []*Neighbour
|
||||
|
||||
type Neighbour struct {
|
||||
Id string `json:"id"`
|
||||
@ -122,7 +122,7 @@ type NeighboursResponse struct {
|
||||
Neighbours Neighbours `json:"neighbours"`
|
||||
}
|
||||
|
||||
type NeighboursLookupResults map[int][]Neighbour
|
||||
type NeighboursLookupResults map[int][]*Neighbour
|
||||
|
||||
// BGP
|
||||
type Community []int
|
||||
@ -155,9 +155,9 @@ type Route struct {
|
||||
|
||||
// Lookup Prefixes
|
||||
type LookupRoute struct {
|
||||
Id string `json:"id"`
|
||||
NeighbourId string `json:"neighbour_id"`
|
||||
Neighbour Neighbour `json:"neighbour"`
|
||||
Id string `json:"id"`
|
||||
NeighbourId string `json:"neighbour_id"`
|
||||
Neighbour *Neighbour `json:"neighbour"`
|
||||
|
||||
State string `json:"state"` // Filtered, Imported, ...
|
||||
|
||||
@ -174,7 +174,7 @@ type LookupRoute struct {
|
||||
Details Details `json:"details"`
|
||||
}
|
||||
|
||||
type Routes []Route
|
||||
type Routes []*Route
|
||||
|
||||
// Implement sorting interface for routes
|
||||
func (routes Routes) Len() int {
|
||||
@ -191,18 +191,18 @@ func (routes Routes) Swap(i, j int) {
|
||||
|
||||
type RoutesResponse struct {
|
||||
Api ApiStatus `json:"api"`
|
||||
Imported []Route `json:"imported"`
|
||||
Filtered []Route `json:"filtered"`
|
||||
NotExported []Route `json:"not_exported"`
|
||||
Imported []*Route `json:"imported"`
|
||||
Filtered []*Route `json:"filtered"`
|
||||
NotExported []*Route `json:"not_exported"`
|
||||
}
|
||||
|
||||
type RoutesLookupResponse struct {
|
||||
Api ApiStatus `json:"api"`
|
||||
Routes []LookupRoute `json:"routes"`
|
||||
Api ApiStatus `json:"api"`
|
||||
Routes []*LookupRoute `json:"routes"`
|
||||
}
|
||||
|
||||
type RoutesLookupResponseGlobal struct {
|
||||
Routes []LookupRoute `json:"routes"`
|
||||
Routes []*LookupRoute `json:"routes"`
|
||||
|
||||
// Pagination
|
||||
TotalRoutes int `json:"total_routes"`
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
"github.com/alice-lg/alice-lg/backend/api"
|
||||
)
|
||||
|
||||
type NeighboursIndex map[string]api.Neighbour
|
||||
type NeighboursIndex map[string]*api.Neighbour
|
||||
|
||||
type NeighboursStore struct {
|
||||
neighboursMap map[int]NeighboursIndex
|
||||
@ -122,7 +122,7 @@ func (self *NeighboursStore) update() {
|
||||
func (self *NeighboursStore) GetNeighbourAt(
|
||||
sourceId int,
|
||||
id string,
|
||||
) api.Neighbour {
|
||||
) *api.Neighbour {
|
||||
// Lookup neighbour on RS
|
||||
self.RLock()
|
||||
neighbours := self.neighboursMap[sourceId]
|
||||
@ -133,8 +133,8 @@ func (self *NeighboursStore) GetNeighbourAt(
|
||||
func (self *NeighboursStore) LookupNeighboursAt(
|
||||
sourceId int,
|
||||
query string,
|
||||
) []api.Neighbour {
|
||||
results := []api.Neighbour{}
|
||||
) []*api.Neighbour {
|
||||
results := []*api.Neighbour{}
|
||||
|
||||
self.RLock()
|
||||
neighbours := self.neighboursMap[sourceId]
|
||||
|
@ -23,26 +23,26 @@ func makeTestNeighboursStore() *NeighboursStore {
|
||||
|
||||
// Populate neighbours
|
||||
rs1 := NeighboursIndex{
|
||||
"ID2233_AS2342": api.Neighbour{
|
||||
"ID2233_AS2342": &api.Neighbour{
|
||||
Id: "ID2233_AS2342",
|
||||
Description: "PEER AS2342 192.9.23.42 Customer Peer 1",
|
||||
},
|
||||
"ID2233_AS2343": api.Neighbour{
|
||||
"ID2233_AS2343": &api.Neighbour{
|
||||
Id: "ID2233_AS2343",
|
||||
Description: "PEER AS2343 192.9.23.43 Different Peer 1",
|
||||
},
|
||||
"ID2233_AS2344": api.Neighbour{
|
||||
"ID2233_AS2344": &api.Neighbour{
|
||||
Id: "ID2233_AS2344",
|
||||
Description: "PEER AS2344 192.9.23.44 3rd Peer from the sun",
|
||||
},
|
||||
}
|
||||
|
||||
rs2 := NeighboursIndex{
|
||||
"ID2233_AS2342": api.Neighbour{
|
||||
"ID2233_AS2342": &api.Neighbour{
|
||||
Id: "ID2233_AS2342",
|
||||
Description: "PEER AS2342 192.9.23.42 Customer Peer 1",
|
||||
},
|
||||
"ID2233_AS4223": api.Neighbour{
|
||||
"ID2233_AS4223": &api.Neighbour{
|
||||
Id: "ID2233_AS4223",
|
||||
Description: "PEER AS4223 192.9.42.23 Cloudfoo Inc.",
|
||||
},
|
||||
|
@ -153,13 +153,13 @@ func (self *RoutesStore) Stats() RoutesStoreStats {
|
||||
}
|
||||
|
||||
// Lookup routes transform
|
||||
func routeToLookupRoute(source SourceConfig, state string, route api.Route) api.LookupRoute {
|
||||
func routeToLookupRoute(source SourceConfig, state string, route *api.Route) *api.LookupRoute {
|
||||
|
||||
// Get neighbour
|
||||
neighbour := AliceNeighboursStore.GetNeighbourAt(source.Id, route.NeighbourId)
|
||||
|
||||
// Make route
|
||||
lookup := api.LookupRoute{
|
||||
lookup := &api.LookupRoute{
|
||||
Id: route.Id,
|
||||
|
||||
NeighbourId: route.NeighbourId,
|
||||
@ -187,12 +187,12 @@ func routeToLookupRoute(source SourceConfig, state string, route api.Route) api.
|
||||
// Routes filter
|
||||
func filterRoutesByPrefix(
|
||||
source SourceConfig,
|
||||
routes []api.Route,
|
||||
routes []*api.Route,
|
||||
prefix string,
|
||||
state string,
|
||||
) []api.LookupRoute {
|
||||
) []*api.LookupRoute {
|
||||
|
||||
results := []api.LookupRoute{}
|
||||
results := []*api.LookupRoute{}
|
||||
for _, route := range routes {
|
||||
// Naiive filtering:
|
||||
if strings.HasPrefix(route.Network, prefix) {
|
||||
@ -205,12 +205,12 @@ func filterRoutesByPrefix(
|
||||
|
||||
func filterRoutesByNeighbourIds(
|
||||
source SourceConfig,
|
||||
routes []api.Route,
|
||||
routes []*api.Route,
|
||||
neighbourIds []string,
|
||||
state string,
|
||||
) []api.LookupRoute {
|
||||
) []*api.LookupRoute {
|
||||
|
||||
results := []api.LookupRoute{}
|
||||
results := []*api.LookupRoute{}
|
||||
for _, route := range routes {
|
||||
// Filtering:
|
||||
if MemberOf(neighbourIds, route.NeighbourId) == true {
|
||||
@ -225,8 +225,8 @@ func filterRoutesByNeighbourIds(
|
||||
func (self *RoutesStore) LookupNeighboursPrefixesAt(
|
||||
sourceId int,
|
||||
neighbourIds []string,
|
||||
) chan []api.LookupRoute {
|
||||
response := make(chan []api.LookupRoute)
|
||||
) chan []*api.LookupRoute {
|
||||
response := make(chan []*api.LookupRoute)
|
||||
|
||||
go func() {
|
||||
self.RLock()
|
||||
@ -245,7 +245,7 @@ func (self *RoutesStore) LookupNeighboursPrefixesAt(
|
||||
neighbourIds,
|
||||
"imported")
|
||||
|
||||
var result []api.LookupRoute
|
||||
var result []*api.LookupRoute
|
||||
result = append(filtered, imported...)
|
||||
|
||||
response <- result
|
||||
@ -258,9 +258,9 @@ func (self *RoutesStore) LookupNeighboursPrefixesAt(
|
||||
func (self *RoutesStore) LookupPrefixAt(
|
||||
sourceId int,
|
||||
prefix string,
|
||||
) chan []api.LookupRoute {
|
||||
) chan []*api.LookupRoute {
|
||||
|
||||
response := make(chan []api.LookupRoute)
|
||||
response := make(chan []*api.LookupRoute)
|
||||
|
||||
go func() {
|
||||
self.RLock()
|
||||
@ -279,7 +279,7 @@ func (self *RoutesStore) LookupPrefixAt(
|
||||
prefix,
|
||||
"imported")
|
||||
|
||||
var result []api.LookupRoute
|
||||
var result []*api.LookupRoute
|
||||
result = append(filtered, imported...)
|
||||
|
||||
response <- result
|
||||
@ -288,9 +288,9 @@ func (self *RoutesStore) LookupPrefixAt(
|
||||
return response
|
||||
}
|
||||
|
||||
func (self *RoutesStore) LookupPrefix(prefix string) []api.LookupRoute {
|
||||
result := []api.LookupRoute{}
|
||||
responses := []chan []api.LookupRoute{}
|
||||
func (self *RoutesStore) LookupPrefix(prefix string) []*api.LookupRoute {
|
||||
result := []*api.LookupRoute{}
|
||||
responses := []chan []*api.LookupRoute{}
|
||||
|
||||
// Dispatch
|
||||
self.RLock()
|
||||
@ -312,10 +312,10 @@ func (self *RoutesStore) LookupPrefix(prefix string) []api.LookupRoute {
|
||||
|
||||
func (self *RoutesStore) LookupPrefixForNeighbours(
|
||||
neighbours api.NeighboursLookupResults,
|
||||
) []api.LookupRoute {
|
||||
) []*api.LookupRoute {
|
||||
|
||||
result := []api.LookupRoute{}
|
||||
responses := []chan []api.LookupRoute{}
|
||||
result := []*api.LookupRoute{}
|
||||
responses := []chan []*api.LookupRoute{}
|
||||
|
||||
// Dispatch
|
||||
for sourceId, locals := range neighbours {
|
||||
|
@ -193,8 +193,8 @@ func TestLookupNeighboursPrefixesAt(t *testing.T) {
|
||||
func TestLookupPrefixForNeighbours(t *testing.T) {
|
||||
// Construct a neighbours lookup result
|
||||
neighbours := api.NeighboursLookupResults{
|
||||
1: []api.Neighbour{
|
||||
api.Neighbour{
|
||||
1: []*api.Neighbour{
|
||||
&api.Neighbour{
|
||||
Id: "ID163_AS31078",
|
||||
},
|
||||
},
|
||||
|
@ -115,7 +115,7 @@ func parseRelativeServerTime(uptime interface{}, config Config) time.Duration {
|
||||
}
|
||||
|
||||
// Parse neighbours response
|
||||
func parseNeighbours(bird ClientResponse, config Config) ([]api.Neighbour, error) {
|
||||
func parseNeighbours(bird ClientResponse, config Config) ([]*api.Neighbour, error) {
|
||||
neighbours := api.Neighbours{}
|
||||
protocols := bird["protocols"].(map[string]interface{})
|
||||
|
||||
@ -127,7 +127,7 @@ func parseNeighbours(bird ClientResponse, config Config) ([]api.Neighbour, error
|
||||
uptime := parseRelativeServerTime(protocol["state_changed"], config)
|
||||
lastError := mustString(protocol["last_error"], "")
|
||||
|
||||
neighbour := api.Neighbour{
|
||||
neighbour := &api.Neighbour{
|
||||
Id: protocolId,
|
||||
|
||||
Address: mustString(protocol["neighbor_address"], "error"),
|
||||
@ -259,7 +259,7 @@ func parseRoutesData(birdRoutes []interface{}, config Config) api.Routes {
|
||||
rtype := mustStringList(rdata["type"])
|
||||
bgpInfo := parseRouteBgpInfo(rdata["bgp"])
|
||||
|
||||
route := api.Route{
|
||||
route := &api.Route{
|
||||
Id: mustString(rdata["network"], "unknown"),
|
||||
NeighbourId: mustString(rdata["from_protocol"], "unknown neighbour"),
|
||||
|
||||
@ -280,10 +280,10 @@ func parseRoutesData(birdRoutes []interface{}, config Config) api.Routes {
|
||||
}
|
||||
|
||||
// Parse routes response
|
||||
func parseRoutes(bird ClientResponse, config Config) ([]api.Route, error) {
|
||||
func parseRoutes(bird ClientResponse, config Config) ([]*api.Route, error) {
|
||||
birdRoutes, ok := bird["routes"].([]interface{})
|
||||
if !ok {
|
||||
return []api.Route{}, fmt.Errorf("Routes response missing")
|
||||
return []*api.Route{}, fmt.Errorf("Routes response missing")
|
||||
}
|
||||
|
||||
routes := parseRoutesData(birdRoutes, config)
|
||||
|
@ -106,7 +106,7 @@ func (self *Birdwatcher) Routes(neighbourId string) (api.RoutesResponse, error)
|
||||
result_filtered := make(api.Routes, 0, len(filtered))
|
||||
result_imported := make(api.Routes, 0, len(imported))
|
||||
|
||||
importedMap := make(map[string]api.Route) // for O(1) access
|
||||
importedMap := make(map[string]*api.Route) // for O(1) access
|
||||
for _, route := range imported {
|
||||
importedMap[route.Id] = route
|
||||
}
|
||||
@ -136,7 +136,7 @@ func (self *Birdwatcher) Routes(neighbourId string) (api.RoutesResponse, error)
|
||||
log.Println("WARNING Could not retrieve routes not exported:", err)
|
||||
log.Println("Is the 'routes_noexport' module active in birdwatcher?")
|
||||
} else {
|
||||
result_noexport := make([]api.Route, 0, len(noexport))
|
||||
result_noexport := make([]*api.Route, 0, len(noexport))
|
||||
// choose routes with next_hop == gateway of this neighbour
|
||||
for _, route := range noexport {
|
||||
if (route.Gateway == gateway) || (route.Gateway == learnt_from) {
|
||||
@ -179,11 +179,11 @@ func (self *Birdwatcher) LookupPrefix(prefix string) (api.RoutesLookupResponse,
|
||||
routes, err := parseRoutes(bird, self.config)
|
||||
|
||||
// Add corresponding neighbour and source rs to result
|
||||
results := []api.LookupRoute{}
|
||||
results := []*api.LookupRoute{}
|
||||
for _, src := range routes {
|
||||
// Okay. This is actually really hacky.
|
||||
// A less bruteforce approach would be highly appreciated
|
||||
route := api.LookupRoute{
|
||||
route := &api.LookupRoute{
|
||||
Id: src.Id,
|
||||
|
||||
Routeserver: rs,
|
||||
|
@ -11,13 +11,13 @@ Helper functions for dealing with birdwatcher API data
|
||||
*/
|
||||
|
||||
// Get neighbour by protocol id
|
||||
func getNeighbourById(neighbours api.Neighbours, id string) (api.Neighbour, error) {
|
||||
func getNeighbourById(neighbours api.Neighbours, id string) (*api.Neighbour, error) {
|
||||
for _, n := range neighbours {
|
||||
if n.Id == id {
|
||||
return n, nil
|
||||
}
|
||||
}
|
||||
unknown := api.Neighbour{
|
||||
unknown := &api.Neighbour{
|
||||
Id: "unknown",
|
||||
Description: "Unknown neighbour",
|
||||
}
|
||||
|
Reference in New Issue
Block a user