From 3db5d9e989348541fcd75c5aa09055fb7f7c7b68 Mon Sep 17 00:00:00 2001 From: Annika Hannig Date: Fri, 15 Oct 2021 21:45:28 +0200 Subject: [PATCH] moved config to separate package --- pkg/backend/bgp_communities.go | 176 -------------------- pkg/backend/bgp_communities_test.go | 92 ---------- pkg/{backend => config}/config.go | 0 pkg/{backend => config}/config_test.go | 0 pkg/{backend => config}/testdata/alice.conf | 0 5 files changed, 268 deletions(-) delete mode 100644 pkg/backend/bgp_communities.go delete mode 100644 pkg/backend/bgp_communities_test.go rename pkg/{backend => config}/config.go (100%) rename pkg/{backend => config}/config_test.go (100%) rename pkg/{backend => config}/testdata/alice.conf (100%) diff --git a/pkg/backend/bgp_communities.go b/pkg/backend/bgp_communities.go deleted file mode 100644 index 79533e1..0000000 --- a/pkg/backend/bgp_communities.go +++ /dev/null @@ -1,176 +0,0 @@ -package backend - -import ( - "fmt" - "strconv" - "strings" - - "github.com/alice-lg/alice-lg/pkg/api" -) - -/* -Implement BGP Communities Lookup Base - -We initialize the dictionary with well known communities and -store the representation as a string with : as delimiter. - -From: https://www.iana.org/assignments/bgp-well-known-communities/bgp-well-known-communities.xhtml - - 0x00000000-0x0000FFFF Reserved [RFC1997] - 0x00010000-0xFFFEFFFF Reserved for Private Use [RFC1997] - - 0xFFFF0000 GRACEFUL_SHUTDOWN [RFC8326] - 0xFFFF0001 ACCEPT_OWN [RFC7611] - 0xFFFF0002 ROUTE_FILTER_TRANSLATED_v4 [draft-l3vpn-legacy-rtc] - 0xFFFF0003 ROUTE_FILTER_v4 [draft-l3vpn-legacy-rtc] - 0xFFFF0004 ROUTE_FILTER_TRANSLATED_v6 [draft-l3vpn-legacy-rtc] - 0xFFFF0005 ROUTE_FILTER_v6 [draft-l3vpn-legacy-rtc] - 0xFFFF0006 LLGR_STALE [draft-uttaro-idr-bgp-persistence] - 0xFFFF0007 NO_LLGR [draft-uttaro-idr-bgp-persistence] - 0xFFFF0008 accept-own-nexthop [draft-agrewal-idr-accept-own-nexthop] - - 0xFFFF0009-0xFFFF0299 Unassigned - - 0xFFFF029A BLACKHOLE [RFC7999] - - 0xFFFF029B-0xFFFFFF00 Unassigned - - 0xFFFFFF01 NO_EXPORT [RFC1997] - 0xFFFFFF02 NO_ADVERTISE [RFC1997] - 0xFFFFFF03 NO_EXPORT_SUBCONFED [RFC1997] - 0xFFFFFF04 NOPEER [RFC3765] - 0xFFFFFF05-0xFFFFFFFF Unassigned -*/ - -// BgpCommunities is a tree representation of BGP communities -type BgpCommunities map[string]interface{} - -// MakeWellKnownBgpCommunities returns a BgpCommunities -// map with well known communities. -func MakeWellKnownBgpCommunities() BgpCommunities { - c := BgpCommunities{ - "65535": BgpCommunities{ - "0": "graceful shutdown", - "1": "accept own", - "2": "route filter translated v4", - "3": "route filter v4", - "4": "route filter translated v6", - "5": "route filter v6", - "6": "llgr stale", - "7": "no llgr", - "8": "accept-own-nexthop", - - "666": "blackhole", - - "1048321": "no export", - "1048322": "no advertise", - "1048323": "no export subconfed", - "1048324": "nopeer", - }, - } - - return c -} - -// Lookup searches for a label in the communities map -func (c BgpCommunities) Lookup(community string) (string, error) { - path := strings.Split(community, ":") - var lookup interface{} // This is all much too dynamic... - lookup = c - - for _, key := range path { - key = strings.TrimSpace(key) - - clookup, ok := lookup.(BgpCommunities) - if !ok { - // This happens if path.len > depth - return "", fmt.Errorf("community not found @ %v", key) - } - - res, ok := clookup[key] - if !ok { - // Try to fall back to wildcard key - res, ok = clookup["*"] - if !ok { - break // we did everything we could. - } - } - - lookup = res - } - - label, ok := lookup.(string) - if !ok { - return "", fmt.Errorf("community not found: %v", community) - } - - return label, nil -} - -// Set assignes a label to a community -func (c BgpCommunities) Set(community string, label string) { - path := strings.Split(community, ":") - var lookup interface{} // Again, this is all much too dynamic... - lookup = c - - for _, key := range path[:len(path)-1] { - key = strings.TrimSpace(key) - clookup, ok := lookup.(BgpCommunities) - if !ok { - break - } - - res, ok := clookup[key] - if !ok { - // The key does not exist, create it! - clookup[key] = BgpCommunities{} - res = clookup[key] - } - - lookup = res - } - - slookup := lookup.(BgpCommunities) - slookup[path[len(path)-1]] = label -} - -// APICommunities enumerates all bgp communities into -// a set of api.Communities. -// CAVEAT: Wildcards are substituted by 0 and ** ARE NOT ** expanded. -func (c BgpCommunities) APICommunities() api.Communities { - communities := api.Communities{} - // We could do this recursive, or assume that - // the max depth is 3. - for uVal, c1 := range c { - u, err := strconv.Atoi(uVal) - if err != nil { - u = 0 - } - for vVal, c2 := range c1.(BgpCommunities) { - v, err := strconv.Atoi(vVal) - if err != nil { - v = 0 - } - - com2, ok := c2.(BgpCommunities) - if !ok { - // we only have labels here - communities = append( - communities, api.Community{u, v}) - continue - } - - for wVal := range com2 { - w, err := strconv.Atoi(wVal) - if err != nil { - w = 0 - } - - communities = append( - communities, api.Community{u, v, w}) - continue - } - } - } - return communities -} diff --git a/pkg/backend/bgp_communities_test.go b/pkg/backend/bgp_communities_test.go deleted file mode 100644 index 9534d6c..0000000 --- a/pkg/backend/bgp_communities_test.go +++ /dev/null @@ -1,92 +0,0 @@ -package backend - -import ( - "testing" -) - -func TestCommunityLookup(t *testing.T) { - - c := MakeWellKnownBgpCommunities() - - label, err := c.Lookup("65535:666") - if err != nil { - t.Error(err) - } - if label != "blackhole" { - t.Error("Label should have been: blackhole, got:", label) - } - - // Okay now try some fails - label, err = c.Lookup("65535") - if err == nil { - t.Error("Expected error!") - } - - label, err = c.Lookup("65535:23:42") - if err == nil { - t.Error("Expected not found error!") - } -} - -func TestSetCommunity(t *testing.T) { - c := MakeWellKnownBgpCommunities() - - c.Set("2342:10", "foo") - c.Set("2342:42:23", "bar") - - // Simple lookup - label, err := c.Lookup("2342:10") - if err != nil { - t.Error(err) - } - if label != "foo" { - t.Error("Expected foo for 2342:10, got:", label) - } - - label, err = c.Lookup("2342:42:23") - if err != nil { - t.Error(err) - } - if label != "bar" { - t.Error("Expected bar for 2342:42:23, got:", label) - } -} - -func TestWildcardLookup(t *testing.T) { - c := MakeWellKnownBgpCommunities() - - c.Set("2342:*", "foobar $0") - c.Set("42:*:1", "baz") - - // This should work - label, err := c.Lookup("2342:23") - if err != nil { - t.Error(err) - } - if label != "foobar $0" { - t.Error("Did not get expected label.") - } - - // This however not - label, err = c.Lookup("2342:23:666") - if err == nil { - t.Error("Lookup should have failed, got label:", label) - } - - // This should again work - label, err = c.Lookup("42:123:1") - if err != nil { - t.Error(err) - } - if label != "baz" { - t.Error("Unexpected label for key") - } -} - -func TestAPICommunities(t *testing.T) { - c := MakeWellKnownBgpCommunities() - comm := c.APICommunities() - if len(comm) != 14 { - t.Error("unexpected len(communities) = ", len(comm)) - } -} diff --git a/pkg/backend/config.go b/pkg/config/config.go similarity index 100% rename from pkg/backend/config.go rename to pkg/config/config.go diff --git a/pkg/backend/config_test.go b/pkg/config/config_test.go similarity index 100% rename from pkg/backend/config_test.go rename to pkg/config/config_test.go diff --git a/pkg/backend/testdata/alice.conf b/pkg/config/testdata/alice.conf similarity index 100% rename from pkg/backend/testdata/alice.conf rename to pkg/config/testdata/alice.conf