mirror of
https://github.com/alice-lg/alice-lg.git
synced 2024-05-11 05:55:03 +00:00
updated config parsing for bgp communities
This commit is contained in:
@@ -31,7 +31,7 @@ type ConfigResponse struct {
|
|||||||
Noexport Noexport `json:"noexport"`
|
Noexport Noexport `json:"noexport"`
|
||||||
NoexportReasons map[string]string `json:"noexport_reasons"`
|
NoexportReasons map[string]string `json:"noexport_reasons"`
|
||||||
|
|
||||||
BgpCommunities map[string]string `json:"bgp_communities"`
|
BgpCommunities map[string]interface{} `json:"bgp_communities"`
|
||||||
|
|
||||||
NeighboursColumns map[string]string `json:"neighbours_columns"`
|
NeighboursColumns map[string]string `json:"neighbours_columns"`
|
||||||
NeighboursColumnsOrder []string `json:"neighbours_columns_order"`
|
NeighboursColumnsOrder []string `json:"neighbours_columns_order"`
|
||||||
|
@@ -39,51 +39,11 @@ From: https://www.iana.org/assignments/bgp-well-known-communities/bgp-well-known
|
|||||||
0xFFFFFF05-0xFFFFFFFF Unassigned
|
0xFFFFFF05-0xFFFFFFFF Unassigned
|
||||||
*/
|
*/
|
||||||
|
|
||||||
type BgpCommunities map[string]string
|
type BgpCommunities map[string]interface{}
|
||||||
|
|
||||||
func MakeWellKnownBgpCommunities() BgpCommunities {
|
func MakeWellKnownBgpCommunities() BgpCommunities {
|
||||||
c := BgpCommunities{
|
c := BgpCommunities{
|
||||||
"65535:0": "graceful shutdown",
|
"65535": BgpCommunities{
|
||||||
"65535:1": "accept own",
|
|
||||||
"65535:2": "route filter translated v4",
|
|
||||||
"65535:3": "route filter v4",
|
|
||||||
"65535:4": "route filter translated v6",
|
|
||||||
"65535:5": "route filter v6",
|
|
||||||
"65535:6": "llgr stale",
|
|
||||||
"65535:7": "no llgr",
|
|
||||||
"65535:8": "accept-own-nexthop",
|
|
||||||
|
|
||||||
"65535:666": "blackhole",
|
|
||||||
|
|
||||||
"65535:1048321": "no export",
|
|
||||||
"65535:1048322": "no advertise",
|
|
||||||
"65535:1048323": "no export subconfed",
|
|
||||||
"65535:1048324": "nopeer",
|
|
||||||
}
|
|
||||||
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self BgpCommunities) Merge(communities BgpCommunities) BgpCommunities {
|
|
||||||
merged := BgpCommunities{}
|
|
||||||
|
|
||||||
// Make copy, don't mutate
|
|
||||||
for k, v := range self {
|
|
||||||
merged[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
for k, v := range communities {
|
|
||||||
merged[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
return merged
|
|
||||||
}
|
|
||||||
|
|
||||||
type NgBgpCommunities map[string]interface{}
|
|
||||||
|
|
||||||
func NgMakeWellKnownBgpCommunities() NgBgpCommunities {
|
|
||||||
c := NgBgpCommunities{
|
|
||||||
"65535": NgBgpCommunities{
|
|
||||||
"0": "graceful shutdown",
|
"0": "graceful shutdown",
|
||||||
"1": "accept own",
|
"1": "accept own",
|
||||||
"2": "route filter translated v4",
|
"2": "route filter translated v4",
|
||||||
@@ -106,13 +66,13 @@ func NgMakeWellKnownBgpCommunities() NgBgpCommunities {
|
|||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self NgBgpCommunities) Lookup(community string) (string, error) {
|
func (self BgpCommunities) Lookup(community string) (string, error) {
|
||||||
path := strings.Split(community, ":")
|
path := strings.Split(community, ":")
|
||||||
var lookup interface{} // This is all much too dynamic...
|
var lookup interface{} // This is all much too dynamic...
|
||||||
lookup = self
|
lookup = self
|
||||||
|
|
||||||
for _, key := range path {
|
for _, key := range path {
|
||||||
clookup, ok := lookup.(NgBgpCommunities)
|
clookup, ok := lookup.(BgpCommunities)
|
||||||
if !ok {
|
if !ok {
|
||||||
// This happens if path.len > depth
|
// This happens if path.len > depth
|
||||||
return "", fmt.Errorf("community not found")
|
return "", fmt.Errorf("community not found")
|
||||||
@@ -138,13 +98,13 @@ func (self NgBgpCommunities) Lookup(community string) (string, error) {
|
|||||||
return label, nil
|
return label, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self NgBgpCommunities) Set(community string, label string) {
|
func (self BgpCommunities) Set(community string, label string) {
|
||||||
path := strings.Split(community, ":")
|
path := strings.Split(community, ":")
|
||||||
var lookup interface{} // Again, this is all much too dynamic...
|
var lookup interface{} // Again, this is all much too dynamic...
|
||||||
lookup = self
|
lookup = self
|
||||||
|
|
||||||
for _, key := range path[:len(path)-1] {
|
for _, key := range path[:len(path)-1] {
|
||||||
clookup, ok := lookup.(NgBgpCommunities)
|
clookup, ok := lookup.(BgpCommunities)
|
||||||
if !ok {
|
if !ok {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -152,13 +112,13 @@ func (self NgBgpCommunities) Set(community string, label string) {
|
|||||||
res, ok := clookup[key]
|
res, ok := clookup[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
// The key does not exist, create it!
|
// The key does not exist, create it!
|
||||||
clookup[key] = NgBgpCommunities{}
|
clookup[key] = BgpCommunities{}
|
||||||
res = clookup[key]
|
res = clookup[key]
|
||||||
}
|
}
|
||||||
|
|
||||||
lookup = res
|
lookup = res
|
||||||
}
|
}
|
||||||
|
|
||||||
slookup := lookup.(NgBgpCommunities)
|
slookup := lookup.(BgpCommunities)
|
||||||
slookup[path[len(path)-1]] = label
|
slookup[path[len(path)-1]] = label
|
||||||
}
|
}
|
||||||
|
@@ -4,27 +4,9 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMergeCommunities(t *testing.T) {
|
|
||||||
|
|
||||||
c := MakeWellKnownBgpCommunities()
|
|
||||||
|
|
||||||
merged := c.Merge(BgpCommunities{
|
|
||||||
"2342:0": "foo",
|
|
||||||
"2342:123": "bar",
|
|
||||||
})
|
|
||||||
|
|
||||||
if merged["65535:666"] != "blackhole" {
|
|
||||||
t.Error("old values should be present")
|
|
||||||
}
|
|
||||||
|
|
||||||
if merged["2342:123"] != "bar" {
|
|
||||||
t.Error("new values should be present")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCommunityLookup(t *testing.T) {
|
func TestCommunityLookup(t *testing.T) {
|
||||||
|
|
||||||
c := NgMakeWellKnownBgpCommunities()
|
c := MakeWellKnownBgpCommunities()
|
||||||
|
|
||||||
label, err := c.Lookup("65535:666")
|
label, err := c.Lookup("65535:666")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -47,7 +29,7 @@ func TestCommunityLookup(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSetCommunity(t *testing.T) {
|
func TestSetCommunity(t *testing.T) {
|
||||||
c := NgMakeWellKnownBgpCommunities()
|
c := MakeWellKnownBgpCommunities()
|
||||||
|
|
||||||
c.Set("2342:10", "foo")
|
c.Set("2342:10", "foo")
|
||||||
c.Set("2342:42:23", "bar")
|
c.Set("2342:42:23", "bar")
|
||||||
@@ -71,7 +53,7 @@ func TestSetCommunity(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestWildcardLookup(t *testing.T) {
|
func TestWildcardLookup(t *testing.T) {
|
||||||
c := NgMakeWellKnownBgpCommunities()
|
c := MakeWellKnownBgpCommunities()
|
||||||
|
|
||||||
c.Set("2342:*", "foobar $0")
|
c.Set("2342:*", "foobar $0")
|
||||||
c.Set("42:*:1", "baz")
|
c.Set("42:*:1", "baz")
|
||||||
|
@@ -319,8 +319,7 @@ func getBgpCommunities(config *ini.File) BgpCommunities {
|
|||||||
|
|
||||||
community := strings.TrimSpace(kv[0])
|
community := strings.TrimSpace(kv[0])
|
||||||
label := strings.TrimSpace(kv[1])
|
label := strings.TrimSpace(kv[1])
|
||||||
|
communities.Set(community, label)
|
||||||
communities[community] = label
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return communities
|
return communities
|
||||||
|
@@ -27,9 +27,13 @@ func TestLoadConfigs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check communities
|
// Check communities
|
||||||
if config.Ui.BgpCommunities["1:23"] != "some tag" {
|
label, err := config.Ui.BgpCommunities.Lookup("1:23")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if label != "some tag" {
|
||||||
t.Error("expcted to find example community 1:23 with 'some tag'",
|
t.Error("expcted to find example community 1:23 with 'some tag'",
|
||||||
"but got:", config.Ui.BgpCommunities["1:23"])
|
"but got:", label)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user