Skip to content

Commit

Permalink
Support Community Count BGP Policy Condition
Browse files Browse the repository at this point in the history
  • Loading branch information
wenovus committed Apr 3, 2024
1 parent c946d8c commit 0209d34
Show file tree
Hide file tree
Showing 7 changed files with 426 additions and 16 deletions.
32 changes: 20 additions & 12 deletions bgp/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func TestIntendedToGoBGPPolicies(t *testing.T) {
}
v4Stmt.GetOrCreateConditions().GetOrCreateMatchPrefixSet().SetPrefixSet(prefixSet1Name)
v4Stmt.GetOrCreateConditions().GetOrCreateMatchPrefixSet().SetMatchSetOptions(oc.PolicyTypes_MatchSetOptionsRestrictedType_ANY)
v4Stmt.GetOrCreateConditions().GetOrCreateBgpConditions().GetOrCreateCommunityCount().SetOperator(oc.PolicyTypes_ATTRIBUTE_COMPARISON_ATTRIBUTE_EQ)
v4Stmt.GetOrCreateConditions().GetOrCreateBgpConditions().GetOrCreateCommunityCount().SetValue(42)
v4Stmt.GetOrCreateActions().GetOrCreateBgpActions().GetOrCreateSetCommunity().GetOrCreateReference().SetCommunitySetRef(commsetName)
v4Stmt.GetOrCreateActions().GetOrCreateBgpActions().GetOrCreateSetCommunity().SetMethod(oc.SetCommunity_Method_REFERENCE)
v4Stmt.GetOrCreateActions().GetOrCreateBgpActions().GetOrCreateSetCommunity().SetOptions(oc.BgpPolicy_BgpSetCommunityOptionType_ADD)
Expand Down Expand Up @@ -156,12 +158,15 @@ func TestIntendedToGoBGPPolicies(t *testing.T) {
MatchAsPathSet: gobgpoc.MatchAsPathSet{
AsPathSet: "", MatchSetOptions: "any",
},
MedEq: 0x0,
OriginEq: "",
NextHopInList: []string(nil),
AfiSafiInList: []gobgpoc.AfiSafiType(nil),
LocalPrefEq: 0x0,
CommunityCount: gobgpoc.CommunityCount{Operator: "", Value: 0x0},
MedEq: 0x0,
OriginEq: "",
NextHopInList: []string(nil),
AfiSafiInList: []gobgpoc.AfiSafiType(nil),
LocalPrefEq: 0x0,
CommunityCount: gobgpoc.CommunityCount{
Operator: gobgpoc.ATTRIBUTE_COMPARISON_EQ,
Value: 42,
},
AsPathLength: gobgpoc.AsPathLength{Operator: "", Value: 0x0},
RouteType: "",
RpkiValidationResult: "",
Expand Down Expand Up @@ -273,12 +278,15 @@ func TestIntendedToGoBGPPolicies(t *testing.T) {
MatchAsPathSet: gobgpoc.MatchAsPathSet{
AsPathSet: "", MatchSetOptions: "any",
},
MedEq: 0x0,
OriginEq: "",
NextHopInList: []string(nil),
AfiSafiInList: []gobgpoc.AfiSafiType(nil),
LocalPrefEq: 0x0,
CommunityCount: gobgpoc.CommunityCount{Operator: "", Value: 0x0},
MedEq: 0x0,
OriginEq: "",
NextHopInList: []string(nil),
AfiSafiInList: []gobgpoc.AfiSafiType(nil),
LocalPrefEq: 0x0,
CommunityCount: gobgpoc.CommunityCount{
Operator: gobgpoc.ATTRIBUTE_COMPARISON_EQ,
Value: 42,
},
AsPathLength: gobgpoc.AsPathLength{Operator: "", Value: 0x0},
RouteType: "",
RpkiValidationResult: "",
Expand Down
17 changes: 17 additions & 0 deletions bgp/ocgobgp.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ func convertPolicyDefinition(policy *oc.RoutingPolicy_PolicyDefinition, neighAdd
CommunitySet: statement.Conditions.GetBgpConditions().GetCommunitySet(),
MatchSetOptions: convertMatchSetOptionsType(occommset[statement.GetConditions().GetBgpConditions().GetCommunitySet()].GetMatchSetOptions()),
},
CommunityCount: gobgpoc.CommunityCount{
Operator: convertAttributeComparison(statement.Conditions.GetBgpConditions().GetCommunityCount().GetOperator()),
Value: statement.Conditions.GetBgpConditions().GetCommunityCount().GetValue(),
},
MatchAsPathSet: gobgpoc.MatchAsPathSet{
AsPathSet: statement.Conditions.GetBgpConditions().GetMatchAsPathSet().GetAsPathSet(),
MatchSetOptions: convertMatchSetOptionsType(statement.GetConditions().GetBgpConditions().GetMatchAsPathSet().GetMatchSetOptions()),
Expand Down Expand Up @@ -203,6 +207,19 @@ func defaultPolicyToRouteDisp(gobgpdefaultpolicy gobgpoc.DefaultPolicyType) gobg
}
}

func convertAttributeComparison(ocpolicy oc.E_PolicyTypes_ATTRIBUTE_COMPARISON) gobgpoc.AttributeComparison {
switch ocpolicy {
case oc.PolicyTypes_ATTRIBUTE_COMPARISON_ATTRIBUTE_EQ:
return gobgpoc.ATTRIBUTE_COMPARISON_EQ
case oc.PolicyTypes_ATTRIBUTE_COMPARISON_ATTRIBUTE_GE:
return gobgpoc.ATTRIBUTE_COMPARISON_GE
case oc.PolicyTypes_ATTRIBUTE_COMPARISON_ATTRIBUTE_LE:
return gobgpoc.ATTRIBUTE_COMPARISON_LE
default:
return ""
}
}

// ConvertCommunity converts any community union type to its string representation to be used in GoBGP.
func ConvertCommunity(community any) string {
switch c := community.(type) {
Expand Down
1 change: 1 addition & 0 deletions bgp/tests/local_tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go_test(
name = "local_tests_test",
size = "large",
srcs = [
"community_count_test.go",
"community_set_test.go",
"policy_test.go",
"prefix_set_test.go",
Expand Down
Loading

0 comments on commit 0209d34

Please sign in to comment.