1
0
mirror of https://github.com/alice-lg/alice-lg.git synced 2024-05-11 05:55:03 +00:00

created tree based structure

This commit is contained in:
Matthias Hannig
2018-09-06 17:47:43 +02:00
parent f1920b5c26
commit efe2dfcb45
2 changed files with 92 additions and 0 deletions

View File

@ -1,5 +1,10 @@
package main
import (
"fmt"
"strings"
)
/*
Implement BGP Communities Lookup Base
@ -73,3 +78,65 @@ func (self BgpCommunities) Merge(communities BgpCommunities) BgpCommunities {
return merged
}
type NgBgpCommunities map[string]interface{}
func NgMakeWellKnownBgpCommunities() NgBgpCommunities {
c := NgBgpCommunities{
"65535": NgBgpCommunities{
"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
}
func (self NgBgpCommunities) Lookup(community string) (string, error) {
path := strings.Split(community, ":")
var lookup interface{} // This is all much too dynamic...
lookup = self
for _, key := range path {
clookup, ok := lookup.(NgBgpCommunities)
if !ok {
break
}
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")
}
return label, nil
}
func (self NgBgpCommunities) Set(community string, label string) {
}

View File

@ -21,3 +21,28 @@ func TestMergeCommunities(t *testing.T) {
t.Error("new values should be present")
}
}
func TestCommunityLookup(t *testing.T) {
c := NgMakeWellKnownBgpCommunities()
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!")
}
}