summaryrefslogtreecommitdiffhomepage
path: root/policy
diff options
context:
space:
mode:
Diffstat (limited to 'policy')
-rw-r--r--policy/policy.go32
-rw-r--r--policy/policy_test.go43
2 files changed, 33 insertions, 42 deletions
diff --git a/policy/policy.go b/policy/policy.go
index afe2f1a8..0ea3d373 100644
--- a/policy/policy.go
+++ b/policy/policy.go
@@ -193,9 +193,7 @@ func NewPrefixCondition(matchPref config.MatchPrefixSet, defPrefixList []config.
for _, ps := range defPrefixList {
if ps.PrefixSetName == prefixSetName {
for _, prefix := range ps.PrefixList {
- addr := prefix.IpPrefix.IP
- maskLength, _ := prefix.IpPrefix.Mask.Size()
- prefix, e := NewPrefix(addr, uint8(maskLength), prefix.MasklengthRange)
+ prefix, e := NewPrefix(prefix.IpPrefix, prefix.MasklengthRange)
if e != nil {
log.WithFields(log.Fields{
"Topic": "Policy",
@@ -1316,13 +1314,17 @@ type Prefix struct {
MasklengthRange map[MaskLengthRangeType]uint8
}
-func NewPrefix(addr net.IP, maskLen uint8, maskRange string) (Prefix, error) {
+func NewPrefix(prefixStr string, maskRange string) (Prefix, error) {
+ p := Prefix{}
mlr := make(map[MaskLengthRangeType]uint8)
- p := Prefix{
- Address: addr,
- Masklength: maskLen,
- MasklengthRange: make(map[MaskLengthRangeType]uint8),
+ addr, ipPref, e := net.ParseCIDR(prefixStr)
+
+ if e != nil {
+ return p, e
}
+ maskLength, _ := ipPref.Mask.Size()
+ p.Address = addr
+ p.Masklength = uint8(maskLength)
if ipv4Family := addr.To4(); ipv4Family != nil {
p.AddressFamily, _ = bgp.GetRouteFamily("ipv4-unicast")
@@ -1632,13 +1634,8 @@ func IndexOfPolicyDefinition(conPolicyList []config.PolicyDefinition, reqPolicy
func PrefixSetToApiStruct(ps config.PrefixSet) *api.PrefixSet {
resPrefixList := make([]*api.Prefix, 0)
for _, p := range ps.PrefixList {
-
- addr := p.IpPrefix.IP
- length, _ := p.IpPrefix.Mask.Size()
-
resPrefix := &api.Prefix{
- Address: addr.String(),
- MaskLength: uint32(length),
+ IpPrefix: p.IpPrefix,
MaskLengthRange: p.MasklengthRange,
}
resPrefixList = append(resPrefixList, resPrefix)
@@ -1656,13 +1653,8 @@ func PrefixSetToConfigStruct(reqPrefixSet *api.PrefixSet) (bool, config.PrefixSe
var prefixSet config.PrefixSet
isReqPrefixSet := true
if reqPrefixSet.PrefixList != nil {
-
- prefItem := reqPrefixSet.PrefixList[0]
- prefStr := prefItem.Address + "/" + strconv.Itoa(int(prefItem.MaskLength))
- _, ipprefix, _ := net.ParseCIDR(prefStr)
-
prefix = config.Prefix{
- IpPrefix: *ipprefix,
+ IpPrefix: reqPrefixSet.PrefixList[0].IpPrefix,
MasklengthRange: reqPrefixSet.PrefixList[0].MaskLengthRange,
}
prefixList := []config.Prefix{prefix}
diff --git a/policy/policy_test.go b/policy/policy_test.go
index e184f42d..a6d4a4e4 100644
--- a/policy/policy_test.go
+++ b/policy/policy_test.go
@@ -48,13 +48,13 @@ func TestPrefixCalcurateNoRange(t *testing.T) {
updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri)
path := table.ProcessMessage(updateMsg, peer)[0]
// test
- pl1, _ := NewPrefix(net.ParseIP("10.10.0.0"), 24, "")
+ pl1, _ := NewPrefix("10.10.0.0/24", "")
match1 := ipPrefixCalculate(path, pl1)
assert.Equal(t, false, match1)
- pl2, _ := NewPrefix(net.ParseIP("10.10.0.101"), 24, "")
+ pl2, _ := NewPrefix("10.10.0.101/24", "")
match2 := ipPrefixCalculate(path, pl2)
assert.Equal(t, true, match2)
- pl3, _ := NewPrefix(net.ParseIP("10.10.0.0"), 16, "21..24")
+ pl3, _ := NewPrefix("10.10.0.0/16", "21..24")
match3 := ipPrefixCalculate(path, pl3)
assert.Equal(t, true, match3)
}
@@ -73,10 +73,10 @@ func TestPrefixCalcurateAddress(t *testing.T) {
updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri)
path := table.ProcessMessage(updateMsg, peer)[0]
// test
- pl1, _ := NewPrefix(net.ParseIP("10.11.0.0"), 16, "21..24")
+ pl1, _ := NewPrefix("10.11.0.0/16", "21..24")
match1 := ipPrefixCalculate(path, pl1)
assert.Equal(t, false, match1)
- pl2, _ := NewPrefix(net.ParseIP("10.10.0.0"), 16, "21..24")
+ pl2, _ := NewPrefix("10.10.0.0/16", "21..24")
match2 := ipPrefixCalculate(path, pl2)
assert.Equal(t, true, match2)
}
@@ -95,10 +95,10 @@ func TestPrefixCalcurateLength(t *testing.T) {
updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri)
path := table.ProcessMessage(updateMsg, peer)[0]
// test
- pl1, _ := NewPrefix(net.ParseIP("10.10.64.0"), 24, "21..24")
+ pl1, _ := NewPrefix("10.10.64.0/24", "21..24")
match1 := ipPrefixCalculate(path, pl1)
assert.Equal(t, false, match1)
- pl2, _ := NewPrefix(net.ParseIP("10.10.64.0"), 16, "21..24")
+ pl2, _ := NewPrefix("10.10.64.0/16", "21..24")
match2 := ipPrefixCalculate(path, pl2)
assert.Equal(t, true, match2)
}
@@ -117,13 +117,13 @@ func TestPrefixCalcurateLengthRange(t *testing.T) {
updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri)
path := table.ProcessMessage(updateMsg, peer)[0]
// test
- pl1, _ := NewPrefix(net.ParseIP("10.10.0.0"), 16, "21..23")
+ pl1, _ := NewPrefix("10.10.0.0/16", "21..23")
match1 := ipPrefixCalculate(path, pl1)
assert.Equal(t, false, match1)
- pl2, _ := NewPrefix(net.ParseIP("10.10.0.0"), 16, "25..26")
+ pl2, _ := NewPrefix("10.10.0.0/16", "25..26")
match2 := ipPrefixCalculate(path, pl2)
assert.Equal(t, false, match2)
- pl3, _ := NewPrefix(net.ParseIP("10.10.0.0"), 16, "21..24")
+ pl3, _ := NewPrefix("10.10.0.0/16", "21..24")
match3 := ipPrefixCalculate(path, pl3)
assert.Equal(t, true, match3)
}
@@ -144,13 +144,13 @@ func TestPrefixCalcurateNoRangeIPv6(t *testing.T) {
updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri)
path := table.ProcessMessage(updateMsg, peer)[0]
// test
- pl1, _ := NewPrefix(net.ParseIP("2001:123:123::"), 48, "")
+ pl1, _ := NewPrefix("2001:123:123::/48", "")
match1 := ipPrefixCalculate(path, pl1)
assert.Equal(t, false, match1)
- pl2, _ := NewPrefix(net.ParseIP("2001:123:123:1::"), 64, "")
+ pl2, _ := NewPrefix("2001:123:123:1::/64", "")
match2 := ipPrefixCalculate(path, pl2)
assert.Equal(t, true, match2)
- pl3, _ := NewPrefix(net.ParseIP("2001:123:123::"), 48, "64..80")
+ pl3, _ := NewPrefix("2001:123:123::/48", "64..80")
match3 := ipPrefixCalculate(path, pl3)
assert.Equal(t, true, match3)
}
@@ -170,10 +170,10 @@ func TestPrefixCalcurateAddressIPv6(t *testing.T) {
updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri)
path := table.ProcessMessage(updateMsg, peer)[0]
// test
- pl1, _ := NewPrefix(net.ParseIP("2001:123:128::"), 48, "64..80")
+ pl1, _ := NewPrefix("2001:123:128::/48", "64..80")
match1 := ipPrefixCalculate(path, pl1)
assert.Equal(t, false, match1)
- pl2, _ := NewPrefix(net.ParseIP("2001:123:123::"), 48, "64..80")
+ pl2, _ := NewPrefix("2001:123:123::/48", "64..80")
match2 := ipPrefixCalculate(path, pl2)
assert.Equal(t, true, match2)
}
@@ -193,10 +193,10 @@ func TestPrefixCalcurateLengthIPv6(t *testing.T) {
updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri)
path := table.ProcessMessage(updateMsg, peer)[0]
// test
- pl1, _ := NewPrefix(net.ParseIP("2001:123:123:64::"), 64, "64..80")
+ pl1, _ := NewPrefix("2001:123:123:64::/64", "64..80")
match1 := ipPrefixCalculate(path, pl1)
assert.Equal(t, false, match1)
- pl2, _ := NewPrefix(net.ParseIP("2001:123:123:64::"), 48, "64..80")
+ pl2, _ := NewPrefix("2001:123:123:64::/48", "64..80")
match2 := ipPrefixCalculate(path, pl2)
assert.Equal(t, true, match2)
}
@@ -216,13 +216,13 @@ func TestPrefixCalcurateLengthRangeIPv6(t *testing.T) {
updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri)
path := table.ProcessMessage(updateMsg, peer)[0]
// test
- pl1, _ := NewPrefix(net.ParseIP("2001:123:123::"), 48, "62..63")
+ pl1, _ := NewPrefix("2001:123:123::/48", "62..63")
match1 := ipPrefixCalculate(path, pl1)
assert.Equal(t, false, match1)
- pl2, _ := NewPrefix(net.ParseIP("2001:123:123::"), 48, "65..66")
+ pl2, _ := NewPrefix("2001:123:123::/48", "65..66")
match2 := ipPrefixCalculate(path, pl2)
assert.Equal(t, false, match2)
- pl3, _ := NewPrefix(net.ParseIP("2001:123:123::"), 48, "63..65")
+ pl3, _ := NewPrefix("2001:123:123::/48", "63..65")
match3 := ipPrefixCalculate(path, pl3)
assert.Equal(t, true, match3)
}
@@ -1917,12 +1917,11 @@ func createRoutingPolicy(ds config.DefinedSets, pd ...config.PolicyDefinition) c
}
func createPrefixSet(name string, prefix string, maskLength string) config.PrefixSet {
- _, ippref, _ := net.ParseCIDR(prefix)
ps := config.PrefixSet{
PrefixSetName: name,
PrefixList: []config.Prefix{
config.Prefix{
- IpPrefix: *ippref,
+ IpPrefix: prefix,
MasklengthRange: maskLength,
}},
}