summaryrefslogtreecommitdiffhomepage
path: root/policy
diff options
context:
space:
mode:
authorISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>2015-09-30 14:56:42 +0900
committerISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>2015-09-30 15:29:08 +0900
commiteaa7810893c10d2768e61306e38859e80da01111 (patch)
tree0bddb983624da1f10711c6529b0fd01773e02a53 /policy
parentc81fad9103e7bda297590040befbbce25f351d53 (diff)
server: kill LocalRib
Signed-off-by: ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>
Diffstat (limited to 'policy')
-rw-r--r--policy/policy.go33
-rw-r--r--policy/policy_test.go109
2 files changed, 57 insertions, 85 deletions
diff --git a/policy/policy.go b/policy/policy.go
index 988b70ca..62a667af 100644
--- a/policy/policy.go
+++ b/policy/policy.go
@@ -996,18 +996,16 @@ type RoutingAction struct {
}
func NewRoutingAction(action config.Actions) *RoutingAction {
- r := &RoutingAction{
+ return &RoutingAction{
AcceptRoute: action.RouteDisposition.AcceptRoute,
}
- return r
}
func (r *RoutingAction) apply(path *table.Path) *table.Path {
if r.AcceptRoute {
return path
- } else {
- return nil
}
+ return nil
}
type CommunityAction struct {
@@ -1346,7 +1344,7 @@ func NewPrefix(prefixStr string, maskRange string) (Prefix, error) {
// Compare path with a policy's condition in stored order in the policy.
// If a condition match, then this function stops evaluation and
// subsequent conditions are skipped.
-func (p *Policy) Apply(path *table.Path) (bool, RouteType, *table.Path) {
+func (p *Policy) Apply(path *table.Path) (RouteType, *table.Path) {
for _, statement := range p.Statements {
result := statement.evaluate(path)
@@ -1356,23 +1354,24 @@ func (p *Policy) Apply(path *table.Path) (bool, RouteType, *table.Path) {
"PolicyName": p.Name,
}).Debug("statement evaluate : ", result)
- var p *table.Path
if result {
//Routing action
- p = statement.routingAction.apply(path)
- if p != nil {
- // apply all modification actions
- cloned := path.Clone(p.IsWithdraw)
- for _, action := range statement.modificationActions {
- cloned = action.apply(cloned)
- }
- return true, ROUTE_TYPE_ACCEPT, cloned
- } else {
- return true, ROUTE_TYPE_REJECT, nil
+ p := statement.routingAction.apply(path)
+ if p == nil {
+ return ROUTE_TYPE_REJECT, path
+ }
+ if len(statement.modificationActions) == 0 {
+ return ROUTE_TYPE_ACCEPT, path
+ }
+ // apply all modification actions
+ cloned := path.Clone(p.IsWithdraw)
+ for _, action := range statement.modificationActions {
+ cloned = action.apply(cloned)
}
+ return ROUTE_TYPE_ACCEPT, cloned
}
}
- return false, ROUTE_TYPE_NONE, nil
+ return ROUTE_TYPE_NONE, path
}
func ipPrefixCalculate(path *table.Path, cPrefix Prefix) bool {
diff --git a/policy/policy_test.go b/policy/policy_test.go
index 981d1723..b36dae6c 100644
--- a/policy/policy_test.go
+++ b/policy/policy_test.go
@@ -243,10 +243,9 @@ func TestPolicyNotMatch(t *testing.T) {
//test
df := pl.DefinedSets
p := NewPolicy(pl.PolicyDefinitions.PolicyDefinitionList[0], df)
- match, pType, newPath := p.Apply(path)
- assert.Equal(t, false, match)
+ pType, newPath := p.Apply(path)
assert.Equal(t, ROUTE_TYPE_NONE, pType)
- assert.Nil(t, newPath)
+ assert.Equal(t, newPath, path)
}
func TestPolicyMatchAndReject(t *testing.T) {
@@ -275,10 +274,9 @@ func TestPolicyMatchAndReject(t *testing.T) {
//test
df := pl.DefinedSets
p := NewPolicy(pl.PolicyDefinitions.PolicyDefinitionList[0], df)
- match, pType, newPath := p.Apply(path)
- assert.Equal(t, true, match)
+ pType, newPath := p.Apply(path)
assert.Equal(t, ROUTE_TYPE_REJECT, pType)
- assert.Nil(t, newPath)
+ assert.Equal(t, newPath, path)
}
func TestPolicyMatchAndAccept(t *testing.T) {
@@ -307,8 +305,7 @@ func TestPolicyMatchAndAccept(t *testing.T) {
//test
df := pl.DefinedSets
p := NewPolicy(pl.PolicyDefinitions.PolicyDefinitionList[0], df)
- match, pType, newPath := p.Apply(path)
- assert.Equal(t, true, match)
+ pType, newPath := p.Apply(path)
assert.Equal(t, ROUTE_TYPE_ACCEPT, pType)
assert.Equal(t, path, newPath)
}
@@ -349,15 +346,13 @@ func TestPolicyRejectOnlyPrefixSet(t *testing.T) {
//test
df := pl.DefinedSets
p := NewPolicy(pl.PolicyDefinitions.PolicyDefinitionList[0], df)
- match, pType, newPath := p.Apply(path1)
- assert.Equal(t, true, match)
+ pType, newPath := p.Apply(path1)
assert.Equal(t, ROUTE_TYPE_REJECT, pType)
- assert.Nil(t, newPath)
+ assert.Equal(t, newPath, path1)
- match2, pType2, newPath2 := p.Apply(path2)
- assert.Equal(t, false, match2)
+ pType2, newPath2 := p.Apply(path2)
assert.Equal(t, ROUTE_TYPE_NONE, pType2)
- assert.Nil(t, newPath2)
+ assert.Equal(t, newPath2, path2)
}
func TestPolicyRejectOnlyNeighborSet(t *testing.T) {
@@ -396,15 +391,13 @@ func TestPolicyRejectOnlyNeighborSet(t *testing.T) {
//test
df := pl.DefinedSets
p := NewPolicy(pl.PolicyDefinitions.PolicyDefinitionList[0], df)
- match, pType, newPath := p.Apply(path1)
- assert.Equal(t, true, match)
+ pType, newPath := p.Apply(path1)
assert.Equal(t, ROUTE_TYPE_REJECT, pType)
- assert.Nil(t, newPath)
+ assert.Equal(t, newPath, path1)
- match2, pType2, newPath2 := p.Apply(path2)
- assert.Equal(t, false, match2)
+ pType2, newPath2 := p.Apply(path2)
assert.Equal(t, ROUTE_TYPE_NONE, pType2)
- assert.Nil(t, newPath2)
+ assert.Equal(t, newPath2, path2)
}
func TestPolicyDifferentRoutefamilyOfPathAndPolicy(t *testing.T) {
@@ -450,15 +443,13 @@ func TestPolicyDifferentRoutefamilyOfPathAndPolicy(t *testing.T) {
//test
df := pl.DefinedSets
p := NewPolicy(pl.PolicyDefinitions.PolicyDefinitionList[0], df)
- match1, pType1, newPath1 := p.Apply(pathIPv4)
- assert.Equal(t, true, match1)
+ pType1, newPath1 := p.Apply(pathIPv4)
assert.Equal(t, ROUTE_TYPE_REJECT, pType1)
- assert.Nil(t, newPath1)
+ assert.Equal(t, newPath1, pathIPv4)
- match2, pType2, newPath2 := p.Apply(pathIPv6)
- assert.Equal(t, true, match2)
+ pType2, newPath2 := p.Apply(pathIPv6)
assert.Equal(t, ROUTE_TYPE_REJECT, pType2)
- assert.Nil(t, newPath2)
+ assert.Equal(t, newPath2, pathIPv6)
}
func TestAsPathLengthConditionEvaluate(t *testing.T) {
@@ -550,10 +541,9 @@ func TestAsPathLengthConditionWithOtherCondition(t *testing.T) {
//test
df := pl.DefinedSets
p := NewPolicy(pl.PolicyDefinitions.PolicyDefinitionList[0], df)
- match, pType, newPath := p.Apply(path)
- assert.Equal(t, true, match)
+ pType, newPath := p.Apply(path)
assert.Equal(t, ROUTE_TYPE_REJECT, pType)
- assert.Nil(t, newPath)
+ assert.Equal(t, newPath, path)
}
@@ -889,10 +879,9 @@ func TestAsPathConditionWithOtherCondition(t *testing.T) {
//test
df := pl.DefinedSets
p := NewPolicy(pl.PolicyDefinitions.PolicyDefinitionList[0], df)
- match, pType, newPath := p.Apply(path)
- assert.Equal(t, true, match)
+ pType, newPath := p.Apply(path)
assert.Equal(t, ROUTE_TYPE_REJECT, pType)
- assert.Nil(t, newPath)
+ assert.Equal(t, newPath, path)
}
@@ -1121,17 +1110,15 @@ func TestCommunityConditionEvaluateWithOtherCondition(t *testing.T) {
//test
df := pl.DefinedSets
p := NewPolicy(pl.PolicyDefinitions.PolicyDefinitionList[0], df)
- match, pType, newPath := p.Apply(path)
- assert.Equal(t, true, match)
+ pType, newPath := p.Apply(path)
assert.Equal(t, ROUTE_TYPE_REJECT, pType)
- assert.Nil(t, newPath)
+ assert.Equal(t, newPath, path)
df = pl.DefinedSets
p = NewPolicy(pl.PolicyDefinitions.PolicyDefinitionList[1], df)
- match, pType, newPath = p.Apply(path)
- assert.Equal(t, false, match)
+ pType, newPath = p.Apply(path)
assert.Equal(t, ROUTE_TYPE_NONE, pType)
- assert.Nil(t, newPath)
+ assert.Equal(t, newPath, path)
}
@@ -1168,8 +1155,7 @@ func TestPolicyMatchAndAddCommunities(t *testing.T) {
df := pl.DefinedSets
p := NewPolicy(pl.PolicyDefinitions.PolicyDefinitionList[0], df)
- match, pType, newPath := p.Apply(path)
- assert.Equal(t, true, match)
+ pType, newPath := p.Apply(path)
assert.Equal(t, ROUTE_TYPE_ACCEPT, pType)
assert.NotEqual(t, nil, newPath)
log.Debug(newPath)
@@ -1212,8 +1198,7 @@ func TestPolicyMatchAndReplaceCommunities(t *testing.T) {
df := pl.DefinedSets
p := NewPolicy(pl.PolicyDefinitions.PolicyDefinitionList[0], df)
- match, pType, newPath := p.Apply(path)
- assert.Equal(t, true, match)
+ pType, newPath := p.Apply(path)
assert.Equal(t, ROUTE_TYPE_ACCEPT, pType)
assert.NotEqual(t, nil, newPath)
assert.Equal(t, []uint32{stringToCommunityValue(community)}, newPath.GetCommunities())
@@ -1255,8 +1240,7 @@ func TestPolicyMatchAndRemoveCommunities(t *testing.T) {
//test
df := pl.DefinedSets
p := NewPolicy(pl.PolicyDefinitions.PolicyDefinitionList[0], df)
- match, pType, newPath := p.Apply(path)
- assert.Equal(t, true, match)
+ pType, newPath := p.Apply(path)
assert.Equal(t, ROUTE_TYPE_ACCEPT, pType)
assert.NotEqual(t, nil, newPath)
assert.Equal(t, []uint32{stringToCommunityValue(community2)}, newPath.GetCommunities())
@@ -1301,8 +1285,7 @@ func TestPolicyMatchAndClearCommunities(t *testing.T) {
df := pl.DefinedSets
p := NewPolicy(pl.PolicyDefinitions.PolicyDefinitionList[0], df)
- match, pType, newPath := p.Apply(path)
- assert.Equal(t, true, match)
+ pType, newPath := p.Apply(path)
assert.Equal(t, ROUTE_TYPE_ACCEPT, pType)
assert.NotEqual(t, nil, newPath)
//assert.Equal(t, []uint32{}, newPath.GetCommunities())
@@ -1632,16 +1615,14 @@ func TestExtCommunityConditionEvaluateWithOtherCondition(t *testing.T) {
//test
df := pl.DefinedSets
p := NewPolicy(pl.PolicyDefinitions.PolicyDefinitionList[0], df)
- match, pType, newPath := p.Apply(path)
- assert.Equal(t, false, match)
+ pType, newPath := p.Apply(path)
assert.Equal(t, ROUTE_TYPE_NONE, pType)
- assert.Nil(t, newPath)
+ assert.Equal(t, newPath, path)
p = NewPolicy(pl.PolicyDefinitions.PolicyDefinitionList[1], df)
- match, pType, newPath = p.Apply(path)
- assert.Equal(t, true, match)
+ pType, newPath = p.Apply(path)
assert.Equal(t, ROUTE_TYPE_REJECT, pType)
- assert.Nil(t, newPath)
+ assert.Equal(t, newPath, path)
}
@@ -1678,8 +1659,7 @@ func TestPolicyMatchAndReplaceMed(t *testing.T) {
df := pl.DefinedSets
p := NewPolicy(pl.PolicyDefinitions.PolicyDefinitionList[0], df)
- match, pType, newPath := p.Apply(path)
- assert.Equal(t, true, match)
+ pType, newPath := p.Apply(path)
assert.Equal(t, ROUTE_TYPE_ACCEPT, pType)
assert.NotEqual(t, nil, newPath)
v, err := newPath.GetMed()
@@ -1720,8 +1700,7 @@ func TestPolicyMatchAndAddingMed(t *testing.T) {
//test
df := pl.DefinedSets
p := NewPolicy(pl.PolicyDefinitions.PolicyDefinitionList[0], df)
- match, pType, newPath := p.Apply(path)
- assert.Equal(t, true, match)
+ pType, newPath := p.Apply(path)
assert.Equal(t, ROUTE_TYPE_ACCEPT, pType)
assert.NotEqual(t, nil, newPath)
@@ -1765,8 +1744,7 @@ func TestPolicyMatchAndAddingMedOverFlow(t *testing.T) {
df := pl.DefinedSets
p := NewPolicy(pl.PolicyDefinitions.PolicyDefinitionList[0], df)
- match, pType, newPath := p.Apply(path)
- assert.Equal(t, true, match)
+ pType, newPath := p.Apply(path)
assert.Equal(t, ROUTE_TYPE_ACCEPT, pType)
assert.NotEqual(t, nil, newPath)
@@ -1810,8 +1788,7 @@ func TestPolicyMatchAndSubtractMed(t *testing.T) {
df := pl.DefinedSets
p := NewPolicy(pl.PolicyDefinitions.PolicyDefinitionList[0], df)
- match, pType, newPath := p.Apply(path)
- assert.Equal(t, true, match)
+ pType, newPath := p.Apply(path)
assert.Equal(t, ROUTE_TYPE_ACCEPT, pType)
assert.NotEqual(t, nil, newPath)
@@ -1855,8 +1832,7 @@ func TestPolicyMatchAndSubtractMedUnderFlow(t *testing.T) {
df := pl.DefinedSets
p := NewPolicy(pl.PolicyDefinitions.PolicyDefinitionList[0], df)
- match, pType, newPath := p.Apply(path)
- assert.Equal(t, true, match)
+ pType, newPath := p.Apply(path)
assert.Equal(t, ROUTE_TYPE_ACCEPT, pType)
assert.NotEqual(t, nil, newPath)
@@ -1897,8 +1873,7 @@ func TestPolicyMatchWhenPathHaveNotMed(t *testing.T) {
df := pl.DefinedSets
p := NewPolicy(pl.PolicyDefinitions.PolicyDefinitionList[0], df)
- match, pType, newPath := p.Apply(path)
- assert.Equal(t, true, match)
+ pType, newPath := p.Apply(path)
assert.Equal(t, ROUTE_TYPE_ACCEPT, pType)
assert.NotEqual(t, nil, newPath)
@@ -1944,8 +1919,7 @@ func TestPolicyAsPathPrepend(t *testing.T) {
df := pl.DefinedSets
p := NewPolicy(pl.PolicyDefinitions.PolicyDefinitionList[0], df)
- match, pType, newPath := p.Apply(path)
- assert.Equal(true, match)
+ pType, newPath := p.Apply(path)
assert.Equal(ROUTE_TYPE_ACCEPT, pType)
assert.NotEqual(nil, newPath)
assert.Equal([]uint32{65002, 65002, 65002, 65002, 65002, 65002, 65002, 65002, 65002, 65002, 65001, 65000}, newPath.GetAsSeqList())
@@ -1988,8 +1962,7 @@ func TestPolicyAsPathPrependLastAs(t *testing.T) {
df := pl.DefinedSets
p := NewPolicy(pl.PolicyDefinitions.PolicyDefinitionList[0], df)
- match, pType, newPath := p.Apply(path)
- assert.Equal(true, match)
+ pType, newPath := p.Apply(path)
assert.Equal(ROUTE_TYPE_ACCEPT, pType)
assert.NotEqual(nil, newPath)
assert.Equal([]uint32{65002, 65002, 65002, 65002, 65002, 65002, 65001, 65000}, newPath.GetAsSeqList())