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

added community filter matching, fixed term condition with more than one group of conditions

parent ec368617
No related branches found
No related tags found
No related merge requests found
...@@ -20,13 +20,17 @@ func NewTermCondition(prefixLists []*PrefixList, routeFilters []*RouteFilter) *T ...@@ -20,13 +20,17 @@ 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) || return f.matchesPrefixListFilters(p) &&
f.machtchesAnyRouteFilter(p) || f.machtchesRouteFilters(p) &&
f.machtchesAnyLageCommunityFilter(pa) || f.machtchesCommunityFilters(pa) &&
f.machtchesAnyCommunityFilter(pa) f.machtchesLageCommunityFilters(pa)
} }
func (t *TermCondition) matchesAnyPrefixList(p net.Prefix) bool { func (t *TermCondition) matchesPrefixListFilters(p net.Prefix) bool {
if len(t.prefixLists) == 0 {
return true
}
for _, l := range t.prefixLists { for _, l := range t.prefixLists {
if l.Matches(p) { if l.Matches(p) {
return true return true
...@@ -36,7 +40,11 @@ func (t *TermCondition) matchesAnyPrefixList(p net.Prefix) bool { ...@@ -36,7 +40,11 @@ func (t *TermCondition) matchesAnyPrefixList(p net.Prefix) bool {
return false return false
} }
func (t *TermCondition) machtchesAnyRouteFilter(p net.Prefix) bool { func (t *TermCondition) machtchesRouteFilters(p net.Prefix) bool {
if len(t.routeFilters) == 0 {
return true
}
for _, l := range t.routeFilters { for _, l := range t.routeFilters {
if l.Matches(p) { if l.Matches(p) {
return true return true
...@@ -46,7 +54,11 @@ func (t *TermCondition) machtchesAnyRouteFilter(p net.Prefix) bool { ...@@ -46,7 +54,11 @@ func (t *TermCondition) machtchesAnyRouteFilter(p net.Prefix) bool {
return false return false
} }
func (t *TermCondition) machtchesAnyCommunityFilter(pa *route.Path) bool { func (t *TermCondition) machtchesCommunityFilters(pa *route.Path) bool {
if len(t.communityFilters) == 0 {
return true
}
if pa.BGPPath == nil { if pa.BGPPath == nil {
return false return false
} }
...@@ -60,7 +72,11 @@ func (t *TermCondition) machtchesAnyCommunityFilter(pa *route.Path) bool { ...@@ -60,7 +72,11 @@ func (t *TermCondition) machtchesAnyCommunityFilter(pa *route.Path) bool {
return false return false
} }
func (t *TermCondition) machtchesAnyLageCommunityFilter(pa *route.Path) bool { func (t *TermCondition) machtchesLageCommunityFilters(pa *route.Path) bool {
if len(t.largeCommunityFilters) == 0 {
return true
}
if pa.BGPPath == nil { if pa.BGPPath == nil {
return false return false
} }
......
...@@ -26,8 +26,7 @@ func TestMatches(t *testing.T) { ...@@ -26,8 +26,7 @@ func TestMatches(t *testing.T) {
prefixLists: []*PrefixList{ prefixLists: []*PrefixList{
NewPrefixList(net.NewPfx(strAddr("127.0.0.1"), 8)), NewPrefixList(net.NewPfx(strAddr("127.0.0.1"), 8)),
}, },
routeFilters: []*RouteFilter{}, expected: true,
expected: true,
}, },
{ {
name: "one prefix in prefix list and no match, no route filters set", name: "one prefix in prefix list and no match, no route filters set",
...@@ -35,8 +34,7 @@ func TestMatches(t *testing.T) { ...@@ -35,8 +34,7 @@ func TestMatches(t *testing.T) {
prefixLists: []*PrefixList{ prefixLists: []*PrefixList{
NewPrefixList(net.NewPfx(0, 32)), NewPrefixList(net.NewPfx(0, 32)),
}, },
routeFilters: []*RouteFilter{}, expected: false,
expected: false,
}, },
{ {
name: "one prefix of 2 matches in prefix list, no route filters set", name: "one prefix of 2 matches in prefix list, no route filters set",
...@@ -45,22 +43,19 @@ func TestMatches(t *testing.T) { ...@@ -45,22 +43,19 @@ func TestMatches(t *testing.T) {
NewPrefixList(net.NewPfx(strAddr("10.0.0.0"), 8)), NewPrefixList(net.NewPfx(strAddr("10.0.0.0"), 8)),
NewPrefixList(net.NewPfx(strAddr("127.0.0.1"), 8)), NewPrefixList(net.NewPfx(strAddr("127.0.0.1"), 8)),
}, },
routeFilters: []*RouteFilter{}, expected: true,
expected: true,
}, },
{ {
name: "no prefixes in prefix list, only route filter matches", name: "no prefixes in prefix list, only route filter matches",
prefix: net.NewPfx(strAddr("10.0.0.0"), 24), prefix: net.NewPfx(strAddr("10.0.0.0"), 24),
prefixLists: []*PrefixList{},
routeFilters: []*RouteFilter{ routeFilters: []*RouteFilter{
NewRouteFilter(net.NewPfx(strAddr("10.0.0.0"), 8), Longer()), NewRouteFilter(net.NewPfx(strAddr("10.0.0.0"), 8), Longer()),
}, },
expected: true, expected: true,
}, },
{ {
name: "no prefixes in prefix list, one route filter matches", name: "no prefixes in prefix list, one route filter matches",
prefix: net.NewPfx(strAddr("10.0.0.0"), 24), prefix: net.NewPfx(strAddr("10.0.0.0"), 24),
prefixLists: []*PrefixList{},
routeFilters: []*RouteFilter{ routeFilters: []*RouteFilter{
NewRouteFilter(net.NewPfx(strAddr("8.0.0.0"), 8), Longer()), NewRouteFilter(net.NewPfx(strAddr("8.0.0.0"), 8), Longer()),
NewRouteFilter(net.NewPfx(strAddr("10.0.0.0"), 8), Longer()), NewRouteFilter(net.NewPfx(strAddr("10.0.0.0"), 8), Longer()),
...@@ -68,9 +63,8 @@ func TestMatches(t *testing.T) { ...@@ -68,9 +63,8 @@ func TestMatches(t *testing.T) {
expected: true, expected: true,
}, },
{ {
name: "no prefixes in prefix list, one of many route filters matches", name: "no prefixes in prefix list, one of many route filters matches",
prefix: net.NewPfx(strAddr("127.0.0.1"), 8), prefix: net.NewPfx(strAddr("127.0.0.1"), 8),
prefixLists: []*PrefixList{},
routeFilters: []*RouteFilter{ routeFilters: []*RouteFilter{
NewRouteFilter(net.NewPfx(strAddr("10.0.0.0"), 8), Longer()), NewRouteFilter(net.NewPfx(strAddr("10.0.0.0"), 8), Longer()),
}, },
...@@ -88,7 +82,7 @@ func TestMatches(t *testing.T) { ...@@ -88,7 +82,7 @@ func TestMatches(t *testing.T) {
expected: false, expected: false,
}, },
{ {
name: "one prefix in prefixlist, one route fitler, only prefix list matches", name: "one prefix in prefixlist, one route filter, only prefix list matches",
prefix: net.NewPfx(strAddr("8.8.8.0"), 24), prefix: net.NewPfx(strAddr("8.8.8.0"), 24),
prefixLists: []*PrefixList{ prefixLists: []*PrefixList{
NewPrefixList(net.NewPfx(strAddr("8.0.0.0"), 8)), NewPrefixList(net.NewPfx(strAddr("8.0.0.0"), 8)),
...@@ -96,10 +90,10 @@ func TestMatches(t *testing.T) { ...@@ -96,10 +90,10 @@ func TestMatches(t *testing.T) {
routeFilters: []*RouteFilter{ routeFilters: []*RouteFilter{
NewRouteFilter(net.NewPfx(strAddr("10.0.0.0"), 8), Longer()), NewRouteFilter(net.NewPfx(strAddr("10.0.0.0"), 8), Longer()),
}, },
expected: true, expected: false,
}, },
{ {
name: "one prefix in prefixlist, one route fitler, only route filter matches", name: "one prefix in prefixlist, one route filter, only route filter matches",
prefix: net.NewPfx(strAddr("10.0.0.0"), 24), prefix: net.NewPfx(strAddr("10.0.0.0"), 24),
prefixLists: []*PrefixList{ prefixLists: []*PrefixList{
NewPrefixList(net.NewPfx(strAddr("8.0.0.0"), 8)), NewPrefixList(net.NewPfx(strAddr("8.0.0.0"), 8)),
...@@ -107,7 +101,7 @@ func TestMatches(t *testing.T) { ...@@ -107,7 +101,7 @@ func TestMatches(t *testing.T) {
routeFilters: []*RouteFilter{ routeFilters: []*RouteFilter{
NewRouteFilter(net.NewPfx(strAddr("10.0.0.0"), 8), Longer()), NewRouteFilter(net.NewPfx(strAddr("10.0.0.0"), 8), Longer()),
}, },
expected: true, expected: false,
}, },
{ {
name: "community matches", name: "community matches",
...@@ -123,6 +117,17 @@ func TestMatches(t *testing.T) { ...@@ -123,6 +117,17 @@ func TestMatches(t *testing.T) {
{ {
name: "community does not match", name: "community does not match",
prefix: net.NewPfx(strAddr("10.0.0.0"), 24), prefix: net.NewPfx(strAddr("10.0.0.0"), 24),
bgpPath: &route.BGPPath{
Communities: "(1,2) (3,4) (5,6)",
},
communityFilters: []*CommunityFilter{
&CommunityFilter{196608}, // (3,0)
},
expected: false,
},
{
name: "community filter, bgp path is nil",
prefix: net.NewPfx(strAddr("10.0.0.0"), 24),
communityFilters: []*CommunityFilter{ communityFilters: []*CommunityFilter{
&CommunityFilter{196608}, // (3,0) &CommunityFilter{196608}, // (3,0)
}, },
...@@ -160,6 +165,20 @@ func TestMatches(t *testing.T) { ...@@ -160,6 +165,20 @@ func TestMatches(t *testing.T) {
}, },
expected: false, expected: false,
}, },
{
name: "large community filter, bgp path is nil",
prefix: net.NewPfx(strAddr("10.0.0.0"), 24),
largeCommunityFilters: []*LargeCommunityFilter{
{
&packet.LargeCommunity{
GlobalAdministrator: 1,
DataPart1: 2,
DataPart2: 3,
},
},
},
expected: false,
},
} }
for _, test := range tests { for _, test := range tests {
......
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