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)) results := make(api.Routes, 0, len(routes))
for _, r := range routes { for _, r := range routes {
if strings.HasPrefix(strings.ToLower(r.Network), queryString) || if strings.HasPrefix(strings.ToLower(*r.Network), queryString) ||
strings.HasPrefix(strings.ToLower(r.Gateway), queryString) { strings.HasPrefix(strings.ToLower(*r.Gateway), queryString) {
results = append(results, r) results = append(results, r)
} }
} }

View File

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

View File

@ -11,6 +11,7 @@ import (
"github.com/osrg/gobgp/pkg/packet/bgp" "github.com/osrg/gobgp/pkg/packet/bgp"
"github.com/alice-lg/alice-lg/pkg/api" "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" "github.com/alice-lg/alice-lg/pkg/sources/gobgp/apiutil"
) )
@ -85,9 +86,10 @@ func (gobgp *GoBGP) parsePathIntoRoute(
route := api.Route{} route := api.Route{}
route.ID = fmt.Sprintf("%s_%s", path.SourceId, prefix) route.ID = fmt.Sprintf("%s_%s", path.SourceId, prefix)
route.NeighborID = PeerHashWithASAndAddress(path.SourceAsn, path.NeighborIp) route.NeighborID = pools.Neighbors.Acquire(
route.Network = prefix PeerHashWithASAndAddress(path.SourceAsn, path.NeighborIp))
route.Interface = "Unknown" 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.Age = time.Since(time.Unix(path.Age.GetSeconds(), int64(path.Age.GetNanos())))
route.Primary = path.Best route.Primary = path.Best
@ -106,18 +108,18 @@ func (gobgp *GoBGP) parsePathIntoRoute(
case *bgp.PathAttributeMultiExitDisc: case *bgp.PathAttributeMultiExitDisc:
route.BGP.Med = int(attr.Value) route.BGP.Med = int(attr.Value)
case *bgp.PathAttributeNextHop: case *bgp.PathAttributeNextHop:
route.Gateway = attr.Value.String() route.Gateway = pools.Gateways4.Acquire(attr.Value.String())
route.BGP.NextHop = attr.Value.String() route.BGP.NextHop = pools.Gateways4.Acquire(attr.Value.String())
case *bgp.PathAttributeLocalPref: case *bgp.PathAttributeLocalPref:
route.BGP.LocalPref = int(attr.Value) route.BGP.LocalPref = int(attr.Value)
case *bgp.PathAttributeOrigin: case *bgp.PathAttributeOrigin:
switch attr.Value { switch attr.Value {
case bgp.BGP_ORIGIN_ATTR_TYPE_IGP: 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: 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: case bgp.BGP_ORIGIN_ATTR_TYPE_INCOMPLETE:
route.BGP.Origin = "Incomplete" route.BGP.Origin = pools.Origins.Acquire("Incomplete")
} }
case *bgp.PathAttributeAsPath: case *bgp.PathAttributeAsPath:
for _, aspth := range attr.Value { 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) route.Metric = (route.BGP.LocalPref + route.BGP.Med)
return &route, nil 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/api"
"github.com/alice-lg/alice-lg/pkg/decoders" "github.com/alice-lg/alice-lg/pkg/decoders"
"github.com/alice-lg/alice-lg/pkg/pools"
) )
// Decode the api status response from the openbgpd // Decode the api status response from the openbgpd
@ -179,12 +180,12 @@ func decodeRoute(details map[string]interface{}) (*api.Route, error) {
// Make bgp info // Make bgp info
bgpInfo := &api.BGPInfo{ bgpInfo := &api.BGPInfo{
Origin: origin, Origin: pools.Origins.Acquire(origin),
AsPath: asPath, AsPath: pools.ASPaths.Acquire(asPath),
NextHop: trueNextHop, NextHop: pools.Gateways4.Acquire(trueNextHop),
Communities: communities, Communities: pools.Communities.Acquire(communities),
ExtCommunities: extendedCommunities, ExtCommunities: extendedCommunities,
LargeCommunities: largeCommunities, LargeCommunities: pools.LargeCommunities.Acquire(largeCommunities),
LocalPref: localPref, LocalPref: localPref,
} }
@ -196,12 +197,12 @@ func decodeRoute(details map[string]interface{}) (*api.Route, error) {
r := &api.Route{ r := &api.Route{
ID: prefix, ID: prefix,
NeighborID: neighborID, NeighborID: pools.Neighbors.Acquire(neighborID),
Network: prefix, Network: pools.Networks4.Acquire(prefix),
Gateway: trueNextHop, Gateway: pools.Gateways4.Acquire(trueNextHop),
BGP: bgpInfo, BGP: bgpInfo,
Age: lastUpdate, Age: lastUpdate,
Type: []string{origin}, Type: pools.Types.Acquire([]string{origin}),
Primary: isPrimary, Primary: isPrimary,
Details: &rawDetails, Details: &rawDetails,
} }

View File

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

View File

@ -72,7 +72,7 @@ func (r *RoutesBackend) FindByNeighbors(
r.routes.Range(func(k, rs interface{}) bool { r.routes.Range(func(k, rs interface{}) bool {
for _, route := range rs.(api.LookupRoutes) { for _, route := range rs.(api.LookupRoutes) {
if isMemberOf(neighborIDs, route.NeighborID) { if isMemberOf(neighborIDs, *route.NeighborID) {
result = append(result, route) result = append(result, route)
} }
} }
@ -93,7 +93,7 @@ func (r *RoutesBackend) FindByPrefix(
r.routes.Range(func(k, rs interface{}) bool { r.routes.Range(func(k, rs interface{}) bool {
for _, route := range rs.(api.LookupRoutes) { for _, route := range rs.(api.LookupRoutes) {
// Naiive string filtering: // Naiive string filtering:
if strings.HasPrefix(strings.ToLower(route.Network), prefix) { if strings.HasPrefix(strings.ToLower(*route.Network), prefix) {
result = append(result, route) 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/api"
"github.com/alice-lg/alice-lg/pkg/config" "github.com/alice-lg/alice-lg/pkg/config"
"github.com/alice-lg/alice-lg/pkg/pools"
) )
func TestRoutesTable(t *testing.T) { func TestRoutesTable(t *testing.T) {
@ -35,7 +36,7 @@ func TestCountRoutesAt(t *testing.T) {
}, },
Route: &api.Route{ Route: &api.Route{
ID: "r1.2.3.4", 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") b.initTable(ctx, tx, "rs1")
@ -87,7 +88,7 @@ func TestFindByNeighbors(t *testing.T) {
}, },
Route: &api.Route{ Route: &api.Route{
ID: "r1.2.3.4", 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") b.initTable(ctx, tx, "rs1")
@ -145,7 +146,7 @@ func TestFindByPrefix(t *testing.T) {
}, },
Route: &api.Route{ Route: &api.Route{
ID: "r1.2.3.4", 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) b.persist(ctx, tx, "rs1", r, now)
r.Route.ID = "r4242" 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) b.persist(ctx, tx, "rs1", r, now)
r.Route.ID = "r4243" 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" r.Neighbor.ID = "n24"
b.persist(ctx, tx, "rs2", r, now) b.persist(ctx, tx, "rs2", r, now)
r.Route.ID = "r4244" 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" r.Neighbor.ID = "n25"
b.persist(ctx, tx, "rs1", r, now) b.persist(ctx, tx, "rs1", r, now)

View File

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