summaryrefslogtreecommitdiffhomepage
path: root/table
diff options
context:
space:
mode:
Diffstat (limited to 'table')
-rw-r--r--table/destination.go21
-rw-r--r--table/destination_test.go12
-rw-r--r--table/path.go13
-rw-r--r--table/path_test.go29
4 files changed, 46 insertions, 29 deletions
diff --git a/table/destination.go b/table/destination.go
index a01c6e04..16795a89 100644
--- a/table/destination.go
+++ b/table/destination.go
@@ -622,30 +622,19 @@ func compareByASPath(path1, path2 Path) Path {
_, attribute1 := path1.getPathAttr(bgp.BGP_ATTR_TYPE_AS_PATH)
_, attribute2 := path2.getPathAttr(bgp.BGP_ATTR_TYPE_AS_PATH)
- asPath1 := attribute1.(*bgp.PathAttributeAsPath)
- asPath2 := attribute2.(*bgp.PathAttributeAsPath)
-
- if asPath1 == nil || asPath2 == nil {
+ if attribute1 == nil || attribute2 == nil {
log.WithFields(log.Fields{
"Topic": "Table",
"Key": "compareByASPath",
- "ASPath1": asPath1,
- "ASPath2": asPath2,
+ "ASPath1": attribute1,
+ "ASPath2": attribute2,
}).Error("can't compare ASPath because it's not present")
}
- var l1, l2 int
- for _, pathParam := range asPath1.Value {
- l1 += pathParam.ASLen()
- }
-
- for _, pathParam := range asPath2.Value {
- l2 += pathParam.ASLen()
- }
+ l1 := path1.GetAsPathLen()
+ l2 := path2.GetAsPathLen()
log.Debugf("compareByASPath -- l1: %d, l2: %d", l1, l2)
- log.Debug(reflect.TypeOf(asPath1.Value))
- log.Debug(asPath1.Value)
if l1 > l2 {
return path2
} else if l1 < l2 {
diff --git a/table/destination_test.go b/table/destination_test.go
index 751c94db..f6005588 100644
--- a/table/destination_test.go
+++ b/table/destination_test.go
@@ -146,7 +146,9 @@ func updateMsgD1() *bgp.BGPMessage {
nlri := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.10.0")}
withdrawnRoutes := []bgp.WithdrawnRoute{}
- return bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri)
+ updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri)
+ UpdatePathAttrs4ByteAs(updateMsg.Body.(*bgp.BGPUpdate))
+ return updateMsg
}
func updateMsgD2() *bgp.BGPMessage {
@@ -166,7 +168,9 @@ func updateMsgD2() *bgp.BGPMessage {
nlri := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "20.20.20.0")}
withdrawnRoutes := []bgp.WithdrawnRoute{}
- return bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri)
+ updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri)
+ UpdatePathAttrs4ByteAs(updateMsg.Body.(*bgp.BGPUpdate))
+ return updateMsg
}
func updateMsgD3() *bgp.BGPMessage {
origin := bgp.NewPathAttributeOrigin(0)
@@ -185,5 +189,7 @@ func updateMsgD3() *bgp.BGPMessage {
nlri := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "30.30.30.0")}
w1 := bgp.WithdrawnRoute{*bgp.NewIPAddrPrefix(23, "40.40.40.0")}
withdrawnRoutes := []bgp.WithdrawnRoute{w1}
- return bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri)
+ updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri)
+ UpdatePathAttrs4ByteAs(updateMsg.Body.(*bgp.BGPUpdate))
+ return updateMsg
}
diff --git a/table/path.go b/table/path.go
index 522534d9..b72a894e 100644
--- a/table/path.go
+++ b/table/path.go
@@ -339,29 +339,22 @@ func (pi *PathDefault) getPrefix() string {
return pi.nlri.String()
}
-// return total length of AS_PATH whose type is AS_SEQ or AS_SET
+// GetAsPathLen returns the number of AS_PATH
func (pd *PathDefault) GetAsPathLen() int {
- aslen := func(p *bgp.As4PathParam) int {
- if p.Type == bgp.BGP_ASPATH_ATTR_TYPE_SEQ || p.Type == bgp.BGP_ASPATH_ATTR_TYPE_SET {
- return p.ASLen()
- }
- return 0
- }
-
var length int = 0
if _, attr := pd.getPathAttr(bgp.BGP_ATTR_TYPE_AS_PATH); attr != nil {
aspath := attr.(*bgp.PathAttributeAsPath)
for _, paramIf := range aspath.Value {
segment := paramIf.(*bgp.As4PathParam)
- length += aslen(segment)
+ length += segment.ASLen()
}
} else {
_, attr := pd.getPathAttr(bgp.BGP_ATTR_TYPE_AS4_PATH)
aspath := attr.(*bgp.PathAttributeAs4Path)
for _, segment := range aspath.Value {
- length += aslen(segment)
+ length += segment.ASLen()
}
}
return length
diff --git a/table/path_test.go b/table/path_test.go
index aa8c4aa2..05aa423d 100644
--- a/table/path_test.go
+++ b/table/path_test.go
@@ -145,6 +145,35 @@ func TestPathGetAttribute(t *testing.T) {
assert.Equal(t, r_nh, nh)
}
+func TestASPathLen(t *testing.T) {
+ assert := assert.New(t)
+ origin := bgp.NewPathAttributeOrigin(0)
+ aspathParam := []bgp.AsPathParamInterface{
+ bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, []uint16{65001, 65002, 65003, 65004, 65004, 65004, 65004, 65004, 65005}),
+ bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_SET, []uint16{65001, 65002, 65003, 65004, 65005}),
+ bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SEQ, []uint16{65100, 65101, 65102}),
+ bgp.NewAsPathParam(bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SET, []uint16{65100, 65101}),}
+ aspath := bgp.NewPathAttributeAsPath(aspathParam)
+ nexthop := bgp.NewPathAttributeNextHop("192.168.50.1")
+ med := bgp.NewPathAttributeMultiExitDisc(0)
+
+ pathAttributes := []bgp.PathAttributeInterface{
+ origin,
+ aspath,
+ nexthop,
+ med,
+ }
+
+ nlri := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.10.0")}
+ withdrawnRoutes := []bgp.WithdrawnRoute{}
+ bgpmsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri)
+ update := bgpmsg.Body.(*bgp.BGPUpdate)
+ UpdatePathAttrs4ByteAs(update)
+ peer := PathCreatePeer()
+ p, _:= CreatePath(peer[0], &update.NLRI[0], update.PathAttributes, false, time.Now())
+ assert.Equal(10, p.GetAsPathLen())
+}
+
func PathCreatePeer() []*PeerInfo {
peerP1 := &PeerInfo{AS: 65000}
peerP2 := &PeerInfo{AS: 65001}