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

added community filter

parent b6676257
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 CommunityFilter struct {
community uint32
}
func (f *CommunityFilter) Matches(communityString string) bool {
return strings.Contains(communityString, packet.CommunityStringForUint32(f.community))
}
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
type TermCondition struct { type TermCondition struct {
prefixLists []*PrefixList prefixLists []*PrefixList
routeFilters []*RouteFilter routeFilters []*RouteFilter
communityFilters []*CommunityFilter
largeCommunityFilters []*LargeCommunityFilter largeCommunityFilters []*LargeCommunityFilter
} }
...@@ -19,7 +20,10 @@ func NewTermCondition(prefixLists []*PrefixList, routeFilters []*RouteFilter) *T ...@@ -19,7 +20,10 @@ func NewTermCondition(prefixLists []*PrefixList, routeFilters []*RouteFilter) *T
} }
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) || f.machtchesAnyLageCommunityFilter(pa) return f.matchesAnyPrefixList(p) ||
f.machtchesAnyRouteFilter(p) ||
f.machtchesAnyLageCommunityFilter(pa) ||
f.machtchesAnyCommunityFilter(pa)
} }
func (t *TermCondition) matchesAnyPrefixList(p net.Prefix) bool { func (t *TermCondition) matchesAnyPrefixList(p net.Prefix) bool {
...@@ -42,6 +46,20 @@ func (t *TermCondition) machtchesAnyRouteFilter(p net.Prefix) bool { ...@@ -42,6 +46,20 @@ func (t *TermCondition) machtchesAnyRouteFilter(p net.Prefix) bool {
return false return false
} }
func (t *TermCondition) machtchesAnyCommunityFilter(pa *route.Path) bool {
if pa.BGPPath == nil {
return false
}
for _, l := range t.communityFilters {
if l.Matches(pa.BGPPath.Communities) {
return true
}
}
return false
}
func (t *TermCondition) machtchesAnyLageCommunityFilter(pa *route.Path) bool { func (t *TermCondition) machtchesAnyLageCommunityFilter(pa *route.Path) bool {
if pa.BGPPath == nil { if pa.BGPPath == nil {
return false return false
......
...@@ -16,6 +16,7 @@ func TestMatches(t *testing.T) { ...@@ -16,6 +16,7 @@ func TestMatches(t *testing.T) {
bgpPath *route.BGPPath bgpPath *route.BGPPath
prefixLists []*PrefixList prefixLists []*PrefixList
routeFilters []*RouteFilter routeFilters []*RouteFilter
communityFilters []*CommunityFilter
largeCommunityFilters []*LargeCommunityFilter largeCommunityFilters []*LargeCommunityFilter
expected bool expected bool
}{ }{
...@@ -108,6 +109,25 @@ func TestMatches(t *testing.T) { ...@@ -108,6 +109,25 @@ func TestMatches(t *testing.T) {
}, },
expected: true, expected: true,
}, },
{
name: "community matches",
prefix: net.NewPfx(strAddr("10.0.0.0"), 24),
bgpPath: &route.BGPPath{
Communities: "(1,2) (3,4) (5,6)",
},
communityFilters: []*CommunityFilter{
&CommunityFilter{196612}, // (3,4)
},
expected: true,
},
{
name: "community does not match",
prefix: net.NewPfx(strAddr("10.0.0.0"), 24),
communityFilters: []*CommunityFilter{
&CommunityFilter{196608}, // (3,0)
},
expected: false,
},
{ {
name: "large community matches", name: "large community matches",
prefix: net.NewPfx(strAddr("10.0.0.0"), 24), prefix: net.NewPfx(strAddr("10.0.0.0"), 24),
...@@ -145,6 +165,7 @@ func TestMatches(t *testing.T) { ...@@ -145,6 +165,7 @@ func TestMatches(t *testing.T) {
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 := NewTermCondition(test.prefixLists, test.routeFilters) f := NewTermCondition(test.prefixLists, test.routeFilters)
f.communityFilters = test.communityFilters
f.largeCommunityFilters = test.largeCommunityFilters f.largeCommunityFilters = test.largeCommunityFilters
pa := &route.Path{ pa := &route.Path{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment