diff options
Diffstat (limited to 'policy')
-rw-r--r-- | policy/policy.go | 72 | ||||
-rw-r--r-- | policy/policy_test.go | 165 |
2 files changed, 165 insertions, 72 deletions
diff --git a/policy/policy.go b/policy/policy.go index b0eb3809..2a90f5c6 100644 --- a/policy/policy.go +++ b/policy/policy.go @@ -418,10 +418,11 @@ func NewAsPathCondition(matchSet config.MatchAsPathSet, defAsPathSetList []confi asPathList := make([]*AsPathElement, 0) for _, asPathSet := range defAsPathSetList { if asPathSet.AsPathSetName == asPathSetName { - for _, as := range asPathSet.AsPathSetMember { - if regAsn.MatchString(as) { + for _, aspath := range asPathSet.AsPathList { + a := aspath.AsPath + if regAsn.MatchString(a) { - group := regAsn.FindStringSubmatch(as) + group := regAsn.FindStringSubmatch(a) asn, err := strconv.Atoi(group[2]) if err != nil { log.WithFields(log.Fields{ @@ -583,8 +584,8 @@ func NewCommunityCondition(matchSet config.MatchCommunitySet, defCommunitySetLis communityList := make([]*CommunityElement, 0) for _, communitySet := range defCommunitySetList { if communitySet.CommunitySetName == communitySetName { - for _, c := range communitySet.CommunityMember { - + for _, community := range communitySet.CommunityList { + c := community.Community e := &CommunityElement{ isRegExp: false, communityStr: c, @@ -807,18 +808,19 @@ func NewExtCommunityCondition(matchSet config.MatchExtCommunitySet, defExtComSet extCommunityElemList := make([]*ExtCommunityElement, 0) for _, extComSet := range defExtComSetList { if extComSet.ExtCommunitySetName == extComSetName { - for _, c := range extComSet.ExtCommunityMember { + for _, ecommunity := range extComSet.ExtCommunityList { matchAll := false + ec := ecommunity.ExtCommunity e := &ExtCommunityElement{ isRegExp: false, - comStr: c, + comStr: ec, } - matchType, val := getECommunitySubType(c) + matchType, val := getECommunitySubType(ec) if !matchType { log.WithFields(log.Fields{ "Topic": "Policy", "Type": "Extended Community Condition", - }).Error("failed to parse the sub type %s.", c) + }).Error("failed to parse the sub type %s.", ec) return nil } switch val[1] { @@ -848,7 +850,7 @@ func NewExtCommunityCondition(matchSet config.MatchExtCommunitySet, defExtComSet } if !matchAll { e.isRegExp = true - reg, err := regexp.Compile(c) + reg, err := regexp.Compile(ec) if err != nil { log.WithFields(log.Fields{ "Topic": "Policy", @@ -1545,11 +1547,11 @@ func IndexOfAsPathSet(conAsPathSetList []config.AsPathSet, reqAsPathSet config.A for i, conAsPathSet := range conAsPathSetList { if conAsPathSet.AsPathSetName == reqAsPathSet.AsPathSetName { idxAsPathSet = i - if len(reqAsPathSet.AsPathSetMember) == 0 { + if len(reqAsPathSet.AsPathList) == 0 { return idxAsPathSet, idxAsPath } - for j, conAsPath := range conAsPathSet.AsPathSetMember { - if conAsPath == reqAsPathSet.AsPathSetMember[0] { + for j, conAsPath := range conAsPathSet.AsPathList { + if conAsPath == reqAsPathSet.AsPathList[0] { idxAsPath = j return idxAsPathSet, idxAsPath } @@ -1568,11 +1570,11 @@ func IndexOfCommunitySet(conCommunitySetList []config.CommunitySet, reqCommunity for i, conCommunitySet := range conCommunitySetList { if conCommunitySet.CommunitySetName == reqCommunitySet.CommunitySetName { idxCommunitySet = i - if len(reqCommunitySet.CommunityMember) == 0 { + if len(reqCommunitySet.CommunityList) == 0 { return idxCommunitySet, idxCommunity } - for j, conCommunity := range conCommunitySet.CommunityMember { - if conCommunity == reqCommunitySet.CommunityMember[0] { + for j, conCommunity := range conCommunitySet.CommunityList { + if conCommunity == reqCommunitySet.CommunityList[0] { idxCommunity = j return idxCommunitySet, idxCommunity } @@ -1591,11 +1593,11 @@ func IndexOfExtCommunitySet(conExtCommunitySetList []config.ExtCommunitySet, req for i, conExtCommunitySet := range conExtCommunitySetList { if conExtCommunitySet.ExtCommunitySetName == reqExtCommunitySet.ExtCommunitySetName { idxExtCommunitySet = i - if len(reqExtCommunitySet.ExtCommunityMember) == 0 { + if len(reqExtCommunitySet.ExtCommunityList) == 0 { return idxExtCommunitySet, idxExtCommunity } - for j, conExtCommunity := range conExtCommunitySet.ExtCommunityMember { - if conExtCommunity == reqExtCommunitySet.ExtCommunityMember[0] { + for j, conExtCommunity := range conExtCommunitySet.ExtCommunityList { + if conExtCommunity == reqExtCommunitySet.ExtCommunityList[0] { idxExtCommunity = j return idxExtCommunitySet, idxExtCommunity } @@ -1721,8 +1723,8 @@ func NeighborSetToConfigStruct(reqNeighborSet *api.NeighborSet) (bool, config.Ne func AsPathSetToApiStruct(as config.AsPathSet) *api.AsPathSet { resAsPathMembers := make([]string, 0) - for _, m := range as.AsPathSetMember { - resAsPathMembers = append(resAsPathMembers, m) + for _, a := range as.AsPathList { + resAsPathMembers = append(resAsPathMembers, a.AsPath) } resAsPathSet := &api.AsPathSet{ AsPathSetName: as.AsPathSetName, @@ -1736,17 +1738,21 @@ func AsPathSetToConfigStruct(reqAsPathSet *api.AsPathSet) (bool, config.AsPathSe if len(reqAsPathSet.AsPathMembers) == 0 { isAsPathSetSet = false } + asPathList := make([]config.AsPath, 0) + for _, a := range reqAsPathSet.AsPathMembers { + asPathList = append(asPathList, config.AsPath{AsPath: a}) + } asPathSet := config.AsPathSet{ - AsPathSetName: reqAsPathSet.AsPathSetName, - AsPathSetMember: reqAsPathSet.AsPathMembers, + AsPathSetName: reqAsPathSet.AsPathSetName, + AsPathList: asPathList, } return isAsPathSetSet, asPathSet } func CommunitySetToApiStruct(cs config.CommunitySet) *api.CommunitySet { resCommunityMembers := make([]string, 0) - for _, m := range cs.CommunityMember { - resCommunityMembers = append(resCommunityMembers, m) + for _, c := range cs.CommunityList { + resCommunityMembers = append(resCommunityMembers, c.Community) } resCommunitySet := &api.CommunitySet{ CommunitySetName: cs.CommunitySetName, @@ -1760,17 +1766,21 @@ func CommunitySetToConfigStruct(reqCommunitySet *api.CommunitySet) (bool, config if len(reqCommunitySet.CommunityMembers) == 0 { isCommunitySet = false } + communityList := make([]config.Community, 0) + for _, c := range reqCommunitySet.CommunityMembers { + communityList = append(communityList, config.Community{Community: c}) + } communitySet := config.CommunitySet{ CommunitySetName: reqCommunitySet.CommunitySetName, - CommunityMember: reqCommunitySet.CommunityMembers, + CommunityList: communityList, } return isCommunitySet, communitySet } func ExtCommunitySetToApiStruct(es config.ExtCommunitySet) *api.ExtCommunitySet { resExtCommunityMembers := make([]string, 0) - for _, m := range es.ExtCommunityMember { - resExtCommunityMembers = append(resExtCommunityMembers, m) + for _, ec := range es.ExtCommunityList { + resExtCommunityMembers = append(resExtCommunityMembers, ec.ExtCommunity) } resExtCommunitySet := &api.ExtCommunitySet{ ExtCommunitySetName: es.ExtCommunitySetName, @@ -1784,9 +1794,13 @@ func ExtCommunitySetToConfigStruct(reqExtCommunitySet *api.ExtCommunitySet) (boo if len(reqExtCommunitySet.ExtCommunityMembers) == 0 { isExtCommunitySet = false } + extCommunityList := make([]config.ExtCommunity, 0) + for _, ec := range reqExtCommunitySet.ExtCommunityMembers { + extCommunityList = append(extCommunityList, config.ExtCommunity{ExtCommunity: ec}) + } ExtCommunitySet := config.ExtCommunitySet{ ExtCommunitySetName: reqExtCommunitySet.ExtCommunitySetName, - ExtCommunityMember: reqExtCommunitySet.ExtCommunityMembers, + ExtCommunityList: extCommunityList, } return isExtCommunitySet, ExtCommunitySet } diff --git a/policy/policy_test.go b/policy/policy_test.go index 7859f747..e184f42d 100644 --- a/policy/policy_test.go +++ b/policy/policy_test.go @@ -611,33 +611,46 @@ func TestAsPathConditionEvaluate(t *testing.T) { // create match condition asPathSet1 := config.AsPathSet{ - AsPathSetName: "asset1", - AsPathSetMember: []string{"^65001"}, + AsPathSetName: "asset1", + AsPathList: []config.AsPath{ + config.AsPath{AsPath: "^65001"}, + }, } asPathSet2 := config.AsPathSet{ - AsPathSetName: "asset2", - AsPathSetMember: []string{"65005$"}, + AsPathSetName: "asset2", + AsPathList: []config.AsPath{ + config.AsPath{AsPath: "65005$"}, + }, } asPathSet3 := config.AsPathSet{ - AsPathSetName: "asset3", - AsPathSetMember: []string{"65004", "65005$"}, + AsPathSetName: "asset3", + AsPathList: []config.AsPath{ + config.AsPath{AsPath: "65004"}, + config.AsPath{AsPath: "65005$"}, + }, } asPathSet4 := config.AsPathSet{ - AsPathSetName: "asset4", - AsPathSetMember: []string{"65000$"}, + AsPathSetName: "asset4", + AsPathList: []config.AsPath{ + config.AsPath{AsPath: "65000$"}, + }, } asPathSet5 := config.AsPathSet{ - AsPathSetName: "asset5", - AsPathSetMember: []string{"65010"}, + AsPathSetName: "asset5", + AsPathList: []config.AsPath{ + config.AsPath{AsPath: "65010"}, + }, } asPathSet6 := config.AsPathSet{ - AsPathSetName: "asset6", - AsPathSetMember: []string{"^65010$"}, + AsPathSetName: "asset6", + AsPathList: []config.AsPath{ + config.AsPath{AsPath: "^65010$"}, + }, } asPathSetList := []config.AsPathSet{asPathSet1, asPathSet2, asPathSet3, @@ -698,8 +711,8 @@ func TestAsPathConditionWithOtherCondition(t *testing.T) { // create policy asPathSet := config.AsPathSet{ - AsPathSetName: "asset1", - AsPathSetMember: []string{"65005$"}, + AsPathSetName: "asset1", + AsPathList: []config.AsPath{config.AsPath{"65005$"}}, } ps := createPrefixSet("ps1", "10.10.1.0/16", "21..24") @@ -761,52 +774,78 @@ func TestCommunityConditionEvaluate(t *testing.T) { // create match condition comSet1 := config.CommunitySet{ CommunitySetName: "comset1", - CommunityMember: []string{"65001:10", "65001:50", "65001:100"}, + CommunityList: []config.Community{ + config.Community{"65001:10"}, + config.Community{"65001:50"}, + config.Community{"65001:100"}, + }, } comSet2 := config.CommunitySet{ CommunitySetName: "comset2", - CommunityMember: []string{"65001:200"}, + CommunityList: []config.Community{ + config.Community{"65001:200"}, + }, } comSet3 := config.CommunitySet{ CommunitySetName: "comset3", - CommunityMember: []string{"4259905936"}, + CommunityList: []config.Community{ + config.Community{"4259905936"}, + }, } comSet4 := config.CommunitySet{ CommunitySetName: "comset4", - CommunityMember: []string{"^[0-9]*:300$"}, + CommunityList: []config.Community{ + config.Community{"^[0-9]*:300$"}, + }, } comSet5 := config.CommunitySet{ CommunitySetName: "comset5", - CommunityMember: []string{"INTERNET"}, + CommunityList: []config.Community{ + config.Community{"INTERNET"}, + }, } comSet6 := config.CommunitySet{ CommunitySetName: "comset6", - CommunityMember: []string{"NO_EXPORT"}, + CommunityList: []config.Community{ + config.Community{"NO_EXPORT"}, + }, } comSet7 := config.CommunitySet{ CommunitySetName: "comset7", - CommunityMember: []string{"NO_ADVERTISE"}, + CommunityList: []config.Community{ + config.Community{"NO_ADVERTISE"}, + }, } comSet8 := config.CommunitySet{ CommunitySetName: "comset8", - CommunityMember: []string{"NO_EXPORT_SUBCONFED"}, + CommunityList: []config.Community{ + config.Community{"NO_EXPORT_SUBCONFED"}, + }, } comSet9 := config.CommunitySet{ CommunitySetName: "comset9", - CommunityMember: []string{"65001:100", "65001:200", "65001:300"}, + CommunityList: []config.Community{ + config.Community{"65001:100"}, + config.Community{"65001:200"}, + config.Community{"65001:300"}, + }, } comSet10 := config.CommunitySet{ CommunitySetName: "comset10", - CommunityMember: []string{"65001:1", "65001:2", "65001:3"}, + CommunityList: []config.Community{ + config.Community{"65001:1"}, + config.Community{"65001:2"}, + config.Community{"65001:3"}, + }, } comSetList := []config.CommunitySet{comSet1, comSet2, comSet3, @@ -881,18 +920,26 @@ func TestCommunityConditionEvaluateWithOtherCondition(t *testing.T) { // create policy asPathSet := config.AsPathSet{ - AsPathSetName: "asset1", - AsPathSetMember: []string{"65005$"}, + AsPathSetName: "asset1", + AsPathList: []config.AsPath{ + config.AsPath{"65005$"}, + }, } comSet1 := config.CommunitySet{ CommunitySetName: "comset1", - CommunityMember: []string{"65001:100", "65001:200", "65001:300"}, + CommunityList: []config.Community{ + config.Community{"65001:100"}, + config.Community{"65001:200"}, + config.Community{"65001:300"}, + }, } comSet2 := config.CommunitySet{ CommunitySetName: "comset2", - CommunityMember: []string{"65050:\\d+"}, + CommunityList: []config.Community{ + config.Community{"65050:\\d+"}, + }, } ps := createPrefixSet("ps1", "10.10.0.0/16", "21..24") @@ -1200,48 +1247,74 @@ func TestExtCommunityConditionEvaluate(t *testing.T) { // create match condition ecomSet1 := config.ExtCommunitySet{ ExtCommunitySetName: "ecomSet1", - ExtCommunityMember: []string{"RT:65001:200"}, + ExtCommunityList: []config.ExtCommunity{ + config.ExtCommunity{"RT:65001:200"}, + }, } ecomSet2 := config.ExtCommunitySet{ ExtCommunitySetName: "ecomSet2", - ExtCommunityMember: []string{"RT:10.0.0.1:300"}, + ExtCommunityList: []config.ExtCommunity{ + config.ExtCommunity{"RT:10.0.0.1:300"}, + }, } ecomSet3 := config.ExtCommunitySet{ ExtCommunitySetName: "ecomSet3", - ExtCommunityMember: []string{fmt.Sprintf("RT:%s:200", convUintStr(65030000))}, + ExtCommunityList: []config.ExtCommunity{ + config.ExtCommunity{fmt.Sprintf("RT:%s:200", convUintStr(65030000))}, + }, } ecomSet4 := config.ExtCommunitySet{ ExtCommunitySetName: "ecomSet4", - ExtCommunityMember: []string{"RT:65002:200"}, + ExtCommunityList: []config.ExtCommunity{ + config.ExtCommunity{"RT:65002:200"}, + }, } ecomSet5 := config.ExtCommunitySet{ ExtCommunitySetName: "ecomSet5", - ExtCommunityMember: []string{"RT:10.0.0.2:300"}, + ExtCommunityList: []config.ExtCommunity{ + config.ExtCommunity{"RT:10.0.0.2:300"}, + }, } ecomSet6 := config.ExtCommunitySet{ ExtCommunitySetName: "ecomSet6", - ExtCommunityMember: []string{fmt.Sprintf("RT:%s:200", convUintStr(65030001))}, + ExtCommunityList: []config.ExtCommunity{ + config.ExtCommunity{fmt.Sprintf("RT:%s:200", convUintStr(65030001))}, + }, } ecomSet7 := config.ExtCommunitySet{ ExtCommunitySetName: "ecomSet7", - ExtCommunityMember: []string{"SoO:65010:300"}, + ExtCommunityList: []config.ExtCommunity{ + config.ExtCommunity{"SoO:65010:300"}, + }, } ecomSet8 := config.ExtCommunitySet{ ExtCommunitySetName: "ecomSet8", - ExtCommunityMember: []string{"SoO:10.0.10.10:[0-9]+"}, + ExtCommunityList: []config.ExtCommunity{ + config.ExtCommunity{"SoO:10.0.10.10:[0-9]+"}, + }, } ecomSet9 := config.ExtCommunitySet{ ExtCommunitySetName: "ecomSet9", - ExtCommunityMember: []string{"RT:[0-9]+:[0-9]+"}, + ExtCommunityList: []config.ExtCommunity{ + config.ExtCommunity{"RT:[0-9]+:[0-9]+"}, + }, } ecomSet10 := config.ExtCommunitySet{ ExtCommunitySetName: "ecomSet10", - ExtCommunityMember: []string{"RT:65001:200", "RT:10.0.0.1:300", "SoO:10.0.10.10:[0-9]+"}, + ExtCommunityList: []config.ExtCommunity{ + config.ExtCommunity{"RT:65001:200"}, + config.ExtCommunity{"RT:10.0.0.1:300"}, + config.ExtCommunity{"SoO:10.0.10.10:[0-9]+"}, + }, } ecomSet11 := config.ExtCommunitySet{ ExtCommunitySetName: "ecomSet11", - ExtCommunityMember: []string{"RT:65001:2", "RT:10.0.0.1:3", "SoO:11.0.10.10:[0-9]+"}, + ExtCommunityList: []config.ExtCommunity{ + config.ExtCommunity{"RT:65001:2"}, + config.ExtCommunity{"RT:10.0.0.1:3"}, + config.ExtCommunity{"SoO:11.0.10.10:[0-9]+"}, + }, } comSetList := []config.ExtCommunitySet{ecomSet1, ecomSet2, ecomSet3, ecomSet4, ecomSet5, ecomSet6, ecomSet7, @@ -1368,17 +1441,23 @@ func TestExtCommunityConditionEvaluateWithOtherCondition(t *testing.T) { // create policy asPathSet := config.AsPathSet{ - AsPathSetName: "asset1", - AsPathSetMember: []string{"65005$"}, + AsPathSetName: "asset1", + AsPathList: []config.AsPath{ + config.AsPath{"65005$"}, + }, } ecomSet1 := config.ExtCommunitySet{ ExtCommunitySetName: "ecomSet1", - ExtCommunityMember: []string{"RT:65001:201"}, + ExtCommunityList: []config.ExtCommunity{ + config.ExtCommunity{"RT:65001:201"}, + }, } ecomSet2 := config.ExtCommunitySet{ ExtCommunitySetName: "ecomSet2", - ExtCommunityMember: []string{"RT:[0-9]+:[0-9]+"}, + ExtCommunityList: []config.ExtCommunity{ + config.ExtCommunity{"RT:[0-9]+:[0-9]+"}, + }, } ps := createPrefixSet("ps1", "10.10.1.0/16", "21..24") |