updated usage of routes with pools

This commit is contained in:
Annika Hannig 2022-11-16 11:52:59 +01:00
parent 3a5c2e235c
commit 76c4f1fb6f
8 changed files with 51 additions and 39 deletions

View File

@ -49,8 +49,8 @@ func apiQueryFilterNextHopGateway(
results := make(api.Routes, 0, len(routes))
for _, r := range routes {
if strings.HasPrefix(strings.ToLower(r.Network), queryString) ||
strings.HasPrefix(strings.ToLower(r.Gateway), queryString) {
if strings.HasPrefix(strings.ToLower(*r.Network), queryString) ||
strings.HasPrefix(strings.ToLower(*r.Gateway), queryString) {
results = append(results, r)
}
}

View File

@ -6,6 +6,7 @@ import (
"testing"
"github.com/alice-lg/alice-lg/pkg/api"
"github.com/alice-lg/alice-lg/pkg/pools"
)
func makeQueryRequest(q string) *http.Request {
@ -20,21 +21,21 @@ func makeQueryRoutes() api.Routes {
routes := api.Routes{
&api.Route{
ID: "route_01",
NeighborID: "n01",
Network: "123.42.43.0/24",
Gateway: "23.42.42.1",
NeighborID: pools.Neighbors.Acquire("n01"),
Network: pools.Networks4.Acquire("123.42.43.0/24"),
Gateway: pools.Gateways4.Acquire("23.42.42.1"),
},
&api.Route{
ID: "route_02",
NeighborID: "n01",
Network: "142.23.0.0/16",
Gateway: "42.42.42.1",
NeighborID: pools.Neighbors.Acquire("n01"),
Network: pools.Networks4.Acquire("142.23.0.0/16"),
Gateway: pools.Gateways4.Acquire("42.42.42.1"),
},
&api.Route{
ID: "route_03",
NeighborID: "n01",
Network: "123.43.0.0/16",
Gateway: "23.42.43.1",
NeighborID: pools.Neighbors.Acquire("n01"),
Network: pools.Networks4.Acquire("123.43.0.0/16"),
Gateway: pools.Gateways4.Acquire("23.42.43.1"),
},
}

View File

@ -11,6 +11,7 @@ import (
"github.com/osrg/gobgp/pkg/packet/bgp"
"github.com/alice-lg/alice-lg/pkg/api"
"github.com/alice-lg/alice-lg/pkg/pools"
"github.com/alice-lg/alice-lg/pkg/sources/gobgp/apiutil"
)
@ -85,9 +86,10 @@ func (gobgp *GoBGP) parsePathIntoRoute(
route := api.Route{}
route.ID = fmt.Sprintf("%s_%s", path.SourceId, prefix)
route.NeighborID = PeerHashWithASAndAddress(path.SourceAsn, path.NeighborIp)
route.Network = prefix
route.Interface = "Unknown"
route.NeighborID = pools.Neighbors.Acquire(
PeerHashWithASAndAddress(path.SourceAsn, path.NeighborIp))
route.Network = pools.Networks4.Acquire(prefix)
route.Interface = pools.Interfaces.Acquire("unknown")
route.Age = time.Since(time.Unix(path.Age.GetSeconds(), int64(path.Age.GetNanos())))
route.Primary = path.Best
@ -106,18 +108,18 @@ func (gobgp *GoBGP) parsePathIntoRoute(
case *bgp.PathAttributeMultiExitDisc:
route.BGP.Med = int(attr.Value)
case *bgp.PathAttributeNextHop:
route.Gateway = attr.Value.String()
route.BGP.NextHop = attr.Value.String()
route.Gateway = pools.Gateways4.Acquire(attr.Value.String())
route.BGP.NextHop = pools.Gateways4.Acquire(attr.Value.String())
case *bgp.PathAttributeLocalPref:
route.BGP.LocalPref = int(attr.Value)
case *bgp.PathAttributeOrigin:
switch attr.Value {
case bgp.BGP_ORIGIN_ATTR_TYPE_IGP:
route.BGP.Origin = "IGP"
route.BGP.Origin = pools.Origins.Acquire("IGP")
case bgp.BGP_ORIGIN_ATTR_TYPE_EGP:
route.BGP.Origin = "EGP"
route.BGP.Origin = pools.Origins.Acquire("EGP")
case bgp.BGP_ORIGIN_ATTR_TYPE_INCOMPLETE:
route.BGP.Origin = "Incomplete"
route.BGP.Origin = pools.Origins.Acquire("Incomplete")
}
case *bgp.PathAttributeAsPath:
for _, aspth := range attr.Value {
@ -155,6 +157,10 @@ func (gobgp *GoBGP) parsePathIntoRoute(
}
}
route.BGP.AsPath = pools.ASPaths.Acquire(route.BGP.AsPath)
route.BGP.Communities = pools.Communities.Acquire(route.BGP.Communities)
route.BGP.LargeCommunities = pools.LargeCommunities.Acquire(route.BGP.LargeCommunities)
route.Metric = (route.BGP.LocalPref + route.BGP.Med)
return &route, nil

View File

@ -9,6 +9,7 @@ import (
"github.com/alice-lg/alice-lg/pkg/api"
"github.com/alice-lg/alice-lg/pkg/decoders"
"github.com/alice-lg/alice-lg/pkg/pools"
)
// Decode the api status response from the openbgpd
@ -179,12 +180,12 @@ func decodeRoute(details map[string]interface{}) (*api.Route, error) {
// Make bgp info
bgpInfo := &api.BGPInfo{
Origin: origin,
AsPath: asPath,
NextHop: trueNextHop,
Communities: communities,
Origin: pools.Origins.Acquire(origin),
AsPath: pools.ASPaths.Acquire(asPath),
NextHop: pools.Gateways4.Acquire(trueNextHop),
Communities: pools.Communities.Acquire(communities),
ExtCommunities: extendedCommunities,
LargeCommunities: largeCommunities,
LargeCommunities: pools.LargeCommunities.Acquire(largeCommunities),
LocalPref: localPref,
}
@ -196,12 +197,12 @@ func decodeRoute(details map[string]interface{}) (*api.Route, error) {
r := &api.Route{
ID: prefix,
NeighborID: neighborID,
Network: prefix,
Gateway: trueNextHop,
NeighborID: pools.Neighbors.Acquire(neighborID),
Network: pools.Networks4.Acquire(prefix),
Gateway: pools.Gateways4.Acquire(trueNextHop),
BGP: bgpInfo,
Age: lastUpdate,
Type: []string{origin},
Type: pools.Types.Acquire([]string{origin}),
Primary: isPrimary,
Details: &rawDetails,
}

View File

@ -5,6 +5,8 @@ import (
"io/ioutil"
"path/filepath"
"testing"
"github.com/alice-lg/alice-lg/pkg/pools"
)
func readTestData(filename string) map[string]interface{} {
@ -54,7 +56,8 @@ func TestDecodeRoutes(t *testing.T) {
// Check first route
r := routes[0]
if r.Network != "23.42.1.0/24" {
ipPtr := pools.Networks4.Acquire("23.42.1.0/24")
if r.Network != ipPtr {
t.Error("unexpected network:", r.Network)
}
// Community decoding

View File

@ -72,7 +72,7 @@ func (r *RoutesBackend) FindByNeighbors(
r.routes.Range(func(k, rs interface{}) bool {
for _, route := range rs.(api.LookupRoutes) {
if isMemberOf(neighborIDs, route.NeighborID) {
if isMemberOf(neighborIDs, *route.NeighborID) {
result = append(result, route)
}
}
@ -93,7 +93,7 @@ func (r *RoutesBackend) FindByPrefix(
r.routes.Range(func(k, rs interface{}) bool {
for _, route := range rs.(api.LookupRoutes) {
// Naiive string filtering:
if strings.HasPrefix(strings.ToLower(route.Network), prefix) {
if strings.HasPrefix(strings.ToLower(*route.Network), prefix) {
result = append(result, route)
}
}

View File

@ -7,6 +7,7 @@ import (
"github.com/alice-lg/alice-lg/pkg/api"
"github.com/alice-lg/alice-lg/pkg/config"
"github.com/alice-lg/alice-lg/pkg/pools"
)
func TestRoutesTable(t *testing.T) {
@ -35,7 +36,7 @@ func TestCountRoutesAt(t *testing.T) {
},
Route: &api.Route{
ID: "r1.2.3.4",
Network: "1.2.3.0/24",
Network: pools.Networks4.Acquire("1.2.3.0/24"),
},
}
b.initTable(ctx, tx, "rs1")
@ -87,7 +88,7 @@ func TestFindByNeighbors(t *testing.T) {
},
Route: &api.Route{
ID: "r1.2.3.4",
Network: "1.2.3.0/24",
Network: pools.Networks4.Acquire("1.2.3.0/24"),
},
}
b.initTable(ctx, tx, "rs1")
@ -145,7 +146,7 @@ func TestFindByPrefix(t *testing.T) {
},
Route: &api.Route{
ID: "r1.2.3.4",
Network: "1.2.3.0/24",
Network: pools.Networks4.Acquire("1.2.3.0/24"),
},
}
@ -154,16 +155,16 @@ func TestFindByPrefix(t *testing.T) {
b.persist(ctx, tx, "rs1", r, now)
r.Route.ID = "r4242"
r.Route.Network = "1.2.4.0/24"
r.Route.Network = pools.Networks4.Acquire("1.2.4.0/24")
b.persist(ctx, tx, "rs1", r, now)
r.Route.ID = "r4243"
r.Route.Network = "1.2.5.0/24"
r.Route.Network = pools.Networks4.Acquire("1.2.5.0/24")
r.Neighbor.ID = "n24"
b.persist(ctx, tx, "rs2", r, now)
r.Route.ID = "r4244"
r.Route.Network = "5.5.5.0/24"
r.Route.Network = pools.Networks4.Acquire("5.5.5.0/24")
r.Neighbor.ID = "n25"
b.persist(ctx, tx, "rs1", r, now)

View File

@ -133,7 +133,7 @@ func TestLookupPrefix(t *testing.T) {
// Check results
for _, prefix := range results {
if strings.HasPrefix(prefix.Network, query) == false {
if strings.HasPrefix(*prefix.Network, query) == false {
t.Error(
"All network addresses should start with the",
"queried prefix",
@ -171,7 +171,7 @@ func TestLookupPrefixForNeighbors(t *testing.T) {
resultset := []string{}
for _, prefix := range results {
resultset = append(resultset, prefix.Network)
resultset = append(resultset, *prefix.Network)
}
testCheckPrefixesPresence(presence, resultset, t)