diff --git a/bird/parser.go b/bird/parser.go index 70a3896..b49583e 100644 --- a/bird/parser.go +++ b/bird/parser.go @@ -196,25 +196,6 @@ func parseSymbols(input []byte) Parsed { return Parsed{"symbols": res} } -func parseMainRouteDetail(groups []string, route Parsed) Parsed { - route["network"] = groups[1] - route["gateway"] = groups[2] - route["interface"] = groups[3] - route["from_protocol"] = groups[4] - route["age"] = groups[5] - route["learnt_from"] = groups[6] - route["primary"] = groups[7] == "*" - route["metric"] = parseInt(groups[8]) - - for k := range route { - if dirtyContains(ParserConf.FilterFields, k) { - route[k] = nil - } - } - - return route -} - func parseRoutes(input []byte) Parsed { res := Parsed{} lines := getLines(input) @@ -261,6 +242,25 @@ func parseRoutes(input []byte) Parsed { return res } +func parseMainRouteDetail(groups []string, route Parsed) Parsed { + route["network"] = groups[1] + route["gateway"] = groups[2] + route["interface"] = groups[3] + route["from_protocol"] = groups[4] + route["age"] = groups[5] + route["learnt_from"] = groups[6] + route["primary"] = groups[7] == "*" + route["metric"] = parseInt(groups[8]) + + for k := range route { + if dirtyContains(ParserConf.FilterFields, k) { + route[k] = nil + } + } + + return route +} + func parseRoutesSecond(line string, route Parsed) Parsed { tmp, ok := route["network"] if !ok { diff --git a/bird/parser_test.go b/bird/parser_test.go index b616413..51ab94e 100644 --- a/bird/parser_test.go +++ b/bird/parser_test.go @@ -68,7 +68,9 @@ func TestParseRoutesAll(t *testing.T) { {9033, 65666, 12}, {9033, 65666, 9}, }, - metric: 100, + metric: 100, + protocol: "ID8503_AS1340", + primary: true, }, routes[0], "Route 1", t) assertRouteIsEqual(expectedRoute{ network: "200.0.0.0/24", @@ -82,7 +84,9 @@ func TestParseRoutesAll(t *testing.T) { {9033, 65666, 12}, {9033, 65666, 9}, }, - metric: 100, + metric: 100, + protocol: "ID8503_AS1340", + primary: true, }, routes[1], "Route 2", t) assertRouteIsEqual(expectedRoute{ network: "200.0.0.0/24", @@ -96,7 +100,9 @@ func TestParseRoutesAll(t *testing.T) { {9033, 65666, 12}, {9033, 65666, 9}, }, - metric: 100, + metric: 100, + protocol: "ID8503_AS1340", + primary: false, }, routes[2], "Route 3", t) assertRouteIsEqual(expectedRoute{ network: "16.0.0.0/24", @@ -110,7 +116,9 @@ func TestParseRoutesAll(t *testing.T) { {9033, 65666, 12}, {9033, 65666, 9}, }, - metric: 100, + metric: 100, + protocol: "ID8503_AS1340", + primary: true, }, routes[3], "Route 4", t) } @@ -165,7 +173,9 @@ func assertIpv6RoutesAsExpected(routes []Parsed, t *testing.T) { {48821, 0, 2000}, {48821, 0, 2100}, }, - metric: 500, + metric: 500, + primary: true, + protocol: "upstream1", }, routes[0], "Route 1", t) assertRouteIsEqual(expectedRoute{ network: "2001:4860::/32", @@ -179,7 +189,9 @@ func assertIpv6RoutesAsExpected(routes []Parsed, t *testing.T) { {48821, 0, 3000}, {48821, 0, 3100}, }, - metric: 100, + metric: 100, + primary: false, + protocol: "upstream2", }, routes[1], "Route 2", t) assertRouteIsEqual(expectedRoute{ network: "2001:678:1e0::/48", @@ -193,7 +205,9 @@ func assertIpv6RoutesAsExpected(routes []Parsed, t *testing.T) { {48821, 0, 2000}, {48821, 0, 2100}, }, - metric: 5000, + metric: 5000, + primary: true, + protocol: "upstream2", }, routes[2], "Route 3", t) } @@ -210,6 +224,10 @@ func assertRouteIsEqual(expected expectedRoute, actual Parsed, name string, t *t t.Fatal(name, ": Expected metric to be:", expected.metric, "not", metric) } + if protocol := actual["from_protocol"].(string); protocol != expected.protocol { + t.Fatal(name, ": Expected protocol to be:", expected.protocol, "not", protocol) + } + bgp := actual["bgp"].(Parsed) if asPath := bgp["as_path"].([]string); !reflect.DeepEqual(asPath, expected.asPath) { t.Fatal(name, ": Expected as_path to be:", expected.asPath, "not", asPath) @@ -231,4 +249,6 @@ type expectedRoute struct { community [][]int64 largeCommunities [][]int64 metric int64 + protocol string + primary bool }