summaryrefslogtreecommitdiffhomepage
path: root/policy
diff options
context:
space:
mode:
Diffstat (limited to 'policy')
-rw-r--r--policy/policy.go39
-rw-r--r--policy/policy_test.go65
2 files changed, 55 insertions, 49 deletions
diff --git a/policy/policy.go b/policy/policy.go
index 49d210aa..9f8be9e0 100644
--- a/policy/policy.go
+++ b/policy/policy.go
@@ -141,7 +141,7 @@ type Statement struct {
}
// evaluate each condition in the statement according to MatchSetOptions
-func (s *Statement) evaluate(p table.Path) bool {
+func (s *Statement) evaluate(p *table.Path) bool {
optionType := s.MatchSetOptions
@@ -186,14 +186,14 @@ func (s *Statement) evaluate(p table.Path) bool {
}
type Condition interface {
- evaluate(table.Path) bool
+ evaluate(*table.Path) bool
}
type DefaultCondition struct {
CallPolicy string
}
-func (c *DefaultCondition) evaluate(path table.Path) bool {
+func (c *DefaultCondition) evaluate(path *table.Path) bool {
return false
}
@@ -235,7 +235,7 @@ func NewPrefixCondition(prefixSetName string, defPrefixList []config.PrefixSet)
// compare prefixes in this condition and nlri of path and
// subsequent comparison is skipped if that matches the conditions.
// If PrefixList's length is zero, return true.
-func (c *PrefixCondition) evaluate(path table.Path) bool {
+func (c *PrefixCondition) evaluate(path *table.Path) bool {
if len(c.PrefixList) == 0 {
log.Debug("PrefixList doesn't have elements")
@@ -283,7 +283,7 @@ func NewNeighborCondition(neighborSetName string, defNeighborSetList []config.Ne
// compare neighbor ipaddress of this condition and source address of path
// and, subsequent comparisons are skipped if that matches the conditions.
// If NeighborList's length is zero, return true.
-func (c *NeighborCondition) evaluate(path table.Path) bool {
+func (c *NeighborCondition) evaluate(path *table.Path) bool {
if len(c.NeighborList) == 0 {
log.Debug("NeighborList doesn't have elements")
@@ -339,7 +339,7 @@ func NewAsPathLengthCondition(defAsPathLength config.AsPathLength) *AsPathLength
// compare AS_PATH length in the message's AS_PATH attribute with
// the one in condition.
-func (c *AsPathLengthCondition) evaluate(path table.Path) bool {
+func (c *AsPathLengthCondition) evaluate(path *table.Path) bool {
length := uint32(path.GetAsPathLen())
result := false
@@ -448,7 +448,7 @@ func NewAsPathCondition(asPathSetName string, defAsPathSetList []config.AsPathSe
// compare AS_PATH in the message's AS_PATH attribute with
// the one in condition.
-func (c *AsPathCondition) evaluate(path table.Path) bool {
+func (c *AsPathCondition) evaluate(path *table.Path) bool {
aspath := path.GetAsSeqList()
@@ -619,7 +619,7 @@ func getCommunityValue(comStr string) (bool, uint32) {
// compare community in the message's attribute with
// the one in the condition.
-func (c *CommunityCondition) evaluate(path table.Path) bool {
+func (c *CommunityCondition) evaluate(path *table.Path) bool {
communities := path.GetCommunities()
@@ -683,13 +683,13 @@ func (c *CommunityCondition) evaluate(path table.Path) bool {
}
type Action interface {
- apply(table.Path) table.Path
+ apply(*table.Path) *table.Path
}
type DefaultAction struct {
}
-func (a *DefaultAction) apply(path table.Path) table.Path {
+func (a *DefaultAction) apply(path *table.Path) *table.Path {
return path
}
@@ -705,7 +705,7 @@ func NewRoutingAction(action config.Actions) *RoutingAction {
return r
}
-func (r *RoutingAction) apply(path table.Path) table.Path {
+func (r *RoutingAction) apply(path *table.Path) *table.Path {
if r.AcceptRoute {
return path
} else {
@@ -773,7 +773,7 @@ func NewCommunityAction(action config.SetCommunity) *CommunityAction {
return m
}
-func (a *CommunityAction) apply(path table.Path) table.Path {
+func (a *CommunityAction) apply(path *table.Path) *table.Path {
list := a.Values
switch a.action {
@@ -791,7 +791,7 @@ func (a *CommunityAction) apply(path table.Path) table.Path {
type MedAction struct {
DefaultAction
- Value int64
+ Value int64
action ActionType
}
@@ -802,12 +802,11 @@ const (
MED_ACTION_SUB
)
-
// NewMedAction creates MedAction object.
// If it cannot parse med string, then return nil.
func NewMedAction(med config.BgpSetMedType) *MedAction {
- if med == ""{
+ if med == "" {
return nil
}
@@ -850,7 +849,7 @@ func getMedValue(medStr string) (bool, int64, ActionType) {
}
return false, int64(0), MED_ACTION_NONE
}
-func (a *MedAction) apply(path table.Path) table.Path {
+func (a *MedAction) apply(path *table.Path) *table.Path {
var err error
switch a.action {
@@ -927,7 +926,7 @@ func NewPrefix(addr net.IP, maskLen uint8, 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) (bool, RouteType, *table.Path) {
for _, statement := range p.Statements {
result := statement.evaluate(path)
@@ -937,13 +936,13 @@ func (p *Policy) Apply(path table.Path) (bool, RouteType, table.Path) {
"PolicyName": p.Name,
}).Debug("statement.Conditions.evaluate : ", result)
- var p table.Path
+ 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())
+ cloned := path.Clone(p.IsWithdraw)
for _, action := range statement.modificationActions {
cloned = action.apply(cloned)
}
@@ -956,7 +955,7 @@ func (p *Policy) Apply(path table.Path) (bool, RouteType, table.Path) {
return false, ROUTE_TYPE_NONE, nil
}
-func ipPrefixCalculate(path table.Path, cPrefix Prefix) bool {
+func ipPrefixCalculate(path *table.Path, cPrefix Prefix) bool {
rf := path.GetRouteFamily()
log.Debug("path routefamily : ", rf.String())
var pAddr net.IP
diff --git a/policy/policy_test.go b/policy/policy_test.go
index ebabb3c8..ae615935 100644
--- a/policy/policy_test.go
+++ b/policy/policy_test.go
@@ -16,17 +16,17 @@
package policy
import (
+ "fmt"
log "github.com/Sirupsen/logrus"
"github.com/osrg/gobgp/config"
"github.com/osrg/gobgp/packet"
"github.com/osrg/gobgp/table"
"github.com/stretchr/testify/assert"
+ "math"
"net"
"strconv"
"strings"
"testing"
- "fmt"
- "math"
)
func TestPrefixCalcurateNoRange(t *testing.T) {
@@ -277,7 +277,7 @@ func TestPolicyNotMatch(t *testing.T) {
match, pType, newPath := p.Apply(path)
assert.Equal(t, false, match)
assert.Equal(t, ROUTE_TYPE_NONE, pType)
- assert.Equal(t, nil, newPath)
+ assert.Nil(t, newPath)
}
func TestPolicyMatchAndReject(t *testing.T) {
@@ -334,7 +334,7 @@ func TestPolicyMatchAndReject(t *testing.T) {
match, pType, newPath := p.Apply(path)
assert.Equal(t, true, match)
assert.Equal(t, ROUTE_TYPE_REJECT, pType)
- assert.Equal(t, nil, newPath)
+ assert.Nil(t, newPath)
}
func TestPolicyMatchAndAccept(t *testing.T) {
@@ -454,12 +454,12 @@ func TestPolicyRejectOnlyPrefixSet(t *testing.T) {
match, pType, newPath := p.Apply(path1)
assert.Equal(t, true, match)
assert.Equal(t, ROUTE_TYPE_REJECT, pType)
- assert.Equal(t, nil, newPath)
+ assert.Nil(t, newPath)
match2, pType2, newPath2 := p.Apply(path2)
assert.Equal(t, false, match2)
assert.Equal(t, ROUTE_TYPE_NONE, pType2)
- assert.Equal(t, nil, newPath2)
+ assert.Nil(t, newPath2)
}
func TestPolicyRejectOnlyNeighborSet(t *testing.T) {
@@ -520,12 +520,12 @@ func TestPolicyRejectOnlyNeighborSet(t *testing.T) {
match, pType, newPath := p.Apply(path1)
assert.Equal(t, true, match)
assert.Equal(t, ROUTE_TYPE_REJECT, pType)
- assert.Equal(t, nil, newPath)
+ assert.Nil(t, newPath)
match2, pType2, newPath2 := p.Apply(path2)
assert.Equal(t, false, match2)
assert.Equal(t, ROUTE_TYPE_NONE, pType2)
- assert.Equal(t, nil, newPath2)
+ assert.Nil(t, newPath2)
}
func TestPolicyDifferentRoutefamilyOfPathAndPolicy(t *testing.T) {
@@ -623,12 +623,12 @@ func TestPolicyDifferentRoutefamilyOfPathAndPolicy(t *testing.T) {
match1, pType1, newPath1 := p.Apply(pathIPv4)
assert.Equal(t, true, match1)
assert.Equal(t, ROUTE_TYPE_REJECT, pType1)
- assert.Equal(t, nil, newPath1)
+ assert.Nil(t, newPath1)
match2, pType2, newPath2 := p.Apply(pathIPv6)
assert.Equal(t, true, match2)
assert.Equal(t, ROUTE_TYPE_REJECT, pType2)
- assert.Equal(t, nil, newPath2)
+ assert.Nil(t, newPath2)
}
func TestAsPathLengthConditionEvaluate(t *testing.T) {
@@ -754,7 +754,7 @@ func TestAsPathLengthConditionWithOtherCondition(t *testing.T) {
match, pType, newPath := p.Apply(path)
assert.Equal(t, true, match)
assert.Equal(t, ROUTE_TYPE_REJECT, pType)
- assert.Equal(t, nil, newPath)
+ assert.Nil(t, newPath)
}
@@ -920,7 +920,7 @@ func TestAsPathConditionWithOtherCondition(t *testing.T) {
match, pType, newPath := p.Apply(path)
assert.Equal(t, true, match)
assert.Equal(t, ROUTE_TYPE_REJECT, pType)
- assert.Equal(t, nil, newPath)
+ assert.Nil(t, newPath)
}
@@ -1141,18 +1141,17 @@ func TestConditionConditionEvaluateWithOtherCondition(t *testing.T) {
match, pType, newPath := p.Apply(path)
assert.Equal(t, true, match)
assert.Equal(t, ROUTE_TYPE_REJECT, pType)
- assert.Equal(t, nil, newPath)
+ assert.Nil(t, newPath)
df = pl.DefinedSets
p = NewPolicy(pl.PolicyDefinitionList[1], df)
match, pType, newPath = p.Apply(path)
assert.Equal(t, false, match)
assert.Equal(t, ROUTE_TYPE_NONE, pType)
- assert.Equal(t, nil, newPath)
+ assert.Nil(t, newPath)
}
-
func TestPolicyMatchAndAddCommunities(t *testing.T) {
// create path
@@ -1203,7 +1202,7 @@ func TestPolicyMatchAndAddCommunities(t *testing.T) {
BgpActions: config.BgpActions{
SetCommunity: config.SetCommunity{
Communities: []string{community},
- Options: "ADD",
+ Options: "ADD",
},
},
},
@@ -1222,7 +1221,6 @@ func TestPolicyMatchAndAddCommunities(t *testing.T) {
assert.Equal(t, []uint32{stringToCommunityValue(community)}, newPath.GetCommunities())
}
-
func TestPolicyMatchAndReplaceCommunities(t *testing.T) {
// create path
@@ -1276,7 +1274,7 @@ func TestPolicyMatchAndReplaceCommunities(t *testing.T) {
BgpActions: config.BgpActions{
SetCommunity: config.SetCommunity{
Communities: []string{community},
- Options: "REPLACE",
+ Options: "REPLACE",
},
},
},
@@ -1348,7 +1346,7 @@ func TestPolicyMatchAndRemoveCommunities(t *testing.T) {
BgpActions: config.BgpActions{
SetCommunity: config.SetCommunity{
Communities: []string{community1},
- Options: "REMOVE",
+ Options: "REMOVE",
},
},
},
@@ -1420,7 +1418,7 @@ func TestPolicyMatchAndClearCommunities(t *testing.T) {
BgpActions: config.BgpActions{
SetCommunity: config.SetCommunity{
Communities: []string{community1},
- Options: "NULL",
+ Options: "NULL",
},
},
},
@@ -1507,8 +1505,9 @@ func TestPolicyMatchAndReplaceMed(t *testing.T) {
assert.Equal(t, true, match)
assert.Equal(t, ROUTE_TYPE_ACCEPT, pType)
assert.NotEqual(t, nil, newPath)
-
- newMed := fmt.Sprintf("%d", newPath.GetMed())
+ v, err := newPath.GetMed()
+ assert.Nil(t, err)
+ newMed := fmt.Sprintf("%d", v)
assert.Equal(t, m, newMed)
}
@@ -1576,7 +1575,9 @@ func TestPolicyMatchAndAddingMed(t *testing.T) {
assert.Equal(t, ROUTE_TYPE_ACCEPT, pType)
assert.NotEqual(t, nil, newPath)
- newMed := fmt.Sprintf("%d", newPath.GetMed())
+ v, err := newPath.GetMed()
+ assert.Nil(t, err)
+ newMed := fmt.Sprintf("%d", v)
assert.Equal(t, ma, newMed)
}
@@ -1617,7 +1618,7 @@ func TestPolicyMatchAndAddingMedOverFlow(t *testing.T) {
NeighborSetList: []config.NeighborSet{ns},
}
- m := fmt.Sprintf("+%d",math.MaxUint32)
+ m := fmt.Sprintf("+%d", math.MaxUint32)
ma := "1"
s := config.Statement{
Name: "statement1",
@@ -1644,7 +1645,9 @@ func TestPolicyMatchAndAddingMedOverFlow(t *testing.T) {
assert.Equal(t, ROUTE_TYPE_ACCEPT, pType)
assert.NotEqual(t, nil, newPath)
- newMed := fmt.Sprintf("%d", newPath.GetMed())
+ v, err := newPath.GetMed()
+ assert.Nil(t, err)
+ newMed := fmt.Sprintf("%d", v)
assert.Equal(t, ma, newMed)
}
@@ -1712,7 +1715,9 @@ func TestPolicyMatchAndSubtractMed(t *testing.T) {
assert.Equal(t, ROUTE_TYPE_ACCEPT, pType)
assert.NotEqual(t, nil, newPath)
- newMed := fmt.Sprintf("%d", newPath.GetMed())
+ v, err := newPath.GetMed()
+ assert.Nil(t, err)
+ newMed := fmt.Sprintf("%d", v)
assert.Equal(t, ma, newMed)
}
@@ -1780,7 +1785,9 @@ func TestPolicyMatchAndSubtractMedUnderFlow(t *testing.T) {
assert.Equal(t, ROUTE_TYPE_ACCEPT, pType)
assert.NotEqual(t, nil, newPath)
- newMed := fmt.Sprintf("%d", newPath.GetMed())
+ v, err := newPath.GetMed()
+ assert.Nil(t, err)
+ newMed := fmt.Sprintf("%d", v)
assert.Equal(t, ma, newMed)
}
@@ -1846,6 +1853,6 @@ func TestPolicyMatchWhenPathHaveNotMed(t *testing.T) {
assert.Equal(t, ROUTE_TYPE_ACCEPT, pType)
assert.NotEqual(t, nil, newPath)
- newMed := fmt.Sprintf("%d", newPath.GetMed())
- assert.Equal(t, "0", newMed)
+ _, err := newPath.GetMed()
+ assert.NotNil(t, err)
}