Merge branch 'bugfix/community-deduplication' into develop

This commit is contained in:
Matthias Hannig 2018-11-11 17:54:42 +01:00
commit 5600534d0e
3 changed files with 69 additions and 9 deletions

View File

@ -118,6 +118,27 @@ func (com Community) String() string {
return res[1:]
}
type Communities []Community
/*
Deduplicate communities
*/
func (communities Communities) Unique() Communities {
seen := map[string]bool{}
result := make(Communities, 0, len(communities))
for _, com := range communities {
key := com.String()
if _, ok := seen[key]; !ok {
// We have not seen this community yet
result = append(result, com)
seen[key] = true
}
}
return result
}
type ExtCommunity []interface{}
func (com ExtCommunity) String() string {
@ -128,13 +149,31 @@ func (com ExtCommunity) String() string {
return res[1:]
}
type ExtCommunities []ExtCommunity
func (communities ExtCommunities) Unique() ExtCommunities {
seen := map[string]bool{}
result := make(ExtCommunities, 0, len(communities))
for _, com := range communities {
key := com.String()
if _, ok := seen[key]; !ok {
// We have not seen this community yet
result = append(result, com)
seen[key] = true
}
}
return result
}
type BgpInfo struct {
Origin string `json:"origin"`
AsPath []int `json:"as_path"`
NextHop string `json:"next_hop"`
Communities []Community `json:"communities"`
LargeCommunities []Community `json:"large_communities"`
ExtCommunities []ExtCommunity `json:"ext_communities"`
Communities Communities `json:"communities"`
LargeCommunities Communities `json:"large_communities"`
ExtCommunities ExtCommunities `json:"ext_communities"`
LocalPref int `json:"local_pref"`
Med int `json:"med"`
}

View File

@ -121,3 +121,24 @@ func TestHasCommunity(t *testing.T) {
t.Error("23:42 should not be present in large commnuties")
}
}
func TestUniqueCommunities(t *testing.T) {
all := Communities{Community{23, 42}, Community{42, 123}, Community{23, 42}}
unique := all.Unique()
if len(unique) != 2 {
t.Error("len(unique) should be < len(all)")
}
t.Log("All:", all, "Unique:", unique)
}
func TestUniqueExtCommunities(t *testing.T) {
all := ExtCommunities{
ExtCommunity{"rt", 23, 42},
ExtCommunity{"ro", 42, 123},
ExtCommunity{"rt", 23, 42}}
unique := all.Unique()
if len(unique) != 2 {
t.Error("len(unique) should be < len(all)")
}
t.Log("All:", all, "Unique:", unique)
}

View File

@ -348,21 +348,21 @@ func (self *SearchFilters) UpdateFromLookupRoute(route *LookupRoute) {
// Add communities
communities := self.GetGroupByKey(SEARCH_KEY_COMMUNITIES)
for _, c := range route.Bgp.Communities {
for _, c := range route.Bgp.Communities.Unique() {
communities.AddFilter(&SearchFilter{
Name: c.String(),
Value: c,
})
}
extCommunities := self.GetGroupByKey(SEARCH_KEY_EXT_COMMUNITIES)
for _, c := range route.Bgp.ExtCommunities {
for _, c := range route.Bgp.ExtCommunities.Unique() {
extCommunities.AddFilter(&SearchFilter{
Name: c.String(),
Value: c,
})
}
largeCommunities := self.GetGroupByKey(SEARCH_KEY_LARGE_COMMUNITIES)
for _, c := range route.Bgp.LargeCommunities {
for _, c := range route.Bgp.LargeCommunities.Unique() {
largeCommunities.AddFilter(&SearchFilter{
Name: c.String(),
Value: c,
@ -376,21 +376,21 @@ func (self *SearchFilters) UpdateFromRoute(route *Route) {
// Add communities
communities := self.GetGroupByKey(SEARCH_KEY_COMMUNITIES)
for _, c := range route.Bgp.Communities {
for _, c := range route.Bgp.Communities.Unique() {
communities.AddFilter(&SearchFilter{
Name: c.String(),
Value: c,
})
}
extCommunities := self.GetGroupByKey(SEARCH_KEY_EXT_COMMUNITIES)
for _, c := range route.Bgp.ExtCommunities {
for _, c := range route.Bgp.ExtCommunities.Unique() {
extCommunities.AddFilter(&SearchFilter{
Name: c.String(),
Value: c,
})
}
largeCommunities := self.GetGroupByKey(SEARCH_KEY_LARGE_COMMUNITIES)
for _, c := range route.Bgp.LargeCommunities {
for _, c := range route.Bgp.LargeCommunities.Unique() {
largeCommunities.AddFilter(&SearchFilter{
Name: c.String(),
Value: c,