Skip to content
Snippets Groups Projects
Commit 2b6ebfa5 authored by Daniel Czerwonk's avatar Daniel Czerwonk
Browse files

added large community filter

parent 16a5c7fa
No related branches found
No related tags found
No related merge requests found
package filter
import (
"strings"
"github.com/bio-routing/bio-rd/protocols/bgp/packet"
)
type LargeCommunityFilter struct {
community *packet.LargeCommunity
}
func (f *LargeCommunityFilter) Matches(communityString string) bool {
return strings.Contains(communityString, f.community.String())
}
...@@ -6,12 +6,13 @@ import ( ...@@ -6,12 +6,13 @@ import (
) )
type TermCondition struct { type TermCondition struct {
prefixLists []*PrefixList prefixLists []*PrefixList
routeFilters []*RouteFilter routeFilters []*RouteFilter
largeCommunityFilters []*LargeCommunityFilter
} }
func (f *TermCondition) Matches(p net.Prefix, pa *route.Path) bool { func (f *TermCondition) Matches(p net.Prefix, pa *route.Path) bool {
return f.matchesAnyPrefixList(p) || f.machtchesAnyRouteFilter(p) return f.matchesAnyPrefixList(p) || f.machtchesAnyRouteFilter(p) || f.machtchesAnyLageCommunityFilter(pa)
} }
func (t *TermCondition) matchesAnyPrefixList(p net.Prefix) bool { func (t *TermCondition) matchesAnyPrefixList(p net.Prefix) bool {
...@@ -33,3 +34,17 @@ func (t *TermCondition) machtchesAnyRouteFilter(p net.Prefix) bool { ...@@ -33,3 +34,17 @@ func (t *TermCondition) machtchesAnyRouteFilter(p net.Prefix) bool {
return false return false
} }
func (t *TermCondition) machtchesAnyLageCommunityFilter(pa *route.Path) bool {
if pa.BGPPath == nil {
return false
}
for _, l := range t.largeCommunityFilters {
if l.Matches(pa.BGPPath.LargeCommunities) {
return true
}
}
return false
}
...@@ -4,17 +4,20 @@ import ( ...@@ -4,17 +4,20 @@ import (
"testing" "testing"
"github.com/bio-routing/bio-rd/net" "github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/protocols/bgp/packet"
"github.com/bio-routing/bio-rd/route" "github.com/bio-routing/bio-rd/route"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestMatches(t *testing.T) { func TestMatches(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
prefix net.Prefix prefix net.Prefix
prefixLists []*PrefixList bgpPath *route.BGPPath
routeFilters []*RouteFilter prefixLists []*PrefixList
expected bool routeFilters []*RouteFilter
largeCommunityFilters []*LargeCommunityFilter
expected bool
}{ }{
{ {
name: "one prefix matches in prefix list, no route filters set", name: "one prefix matches in prefix list, no route filters set",
...@@ -105,16 +108,53 @@ func TestMatches(t *testing.T) { ...@@ -105,16 +108,53 @@ func TestMatches(t *testing.T) {
}, },
expected: true, expected: true,
}, },
{
name: "large community matches",
prefix: net.NewPfx(strAddr("10.0.0.0"), 24),
bgpPath: &route.BGPPath{
LargeCommunities: "(1,2,0) (1,2,3)",
},
largeCommunityFilters: []*LargeCommunityFilter{
{
&packet.LargeCommunity{
GlobalAdministrator: 1,
DataPart1: 2,
DataPart2: 3,
},
},
},
expected: true,
},
{
name: "large community does not match",
prefix: net.NewPfx(strAddr("10.0.0.0"), 24),
bgpPath: &route.BGPPath{},
largeCommunityFilters: []*LargeCommunityFilter{
{
&packet.LargeCommunity{
GlobalAdministrator: 1,
DataPart1: 2,
DataPart2: 3,
},
},
},
expected: false,
},
} }
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(te *testing.T) { t.Run(test.name, func(te *testing.T) {
f := &TermCondition{ f := &TermCondition{
prefixLists: test.prefixLists, prefixLists: test.prefixLists,
routeFilters: test.routeFilters, routeFilters: test.routeFilters,
largeCommunityFilters: test.largeCommunityFilters,
}
pa := &route.Path{
BGPPath: test.bgpPath,
} }
assert.Equal(te, test.expected, f.Matches(test.prefix, &route.Path{})) assert.Equal(te, test.expected, f.Matches(test.prefix, pa))
}) })
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment