summaryrefslogtreecommitdiffhomepage
path: root/table
diff options
context:
space:
mode:
authorHiroshi Yokoi <yokoi.hiroshi@po.ntts.co.jp>2014-12-05 14:11:18 +0900
committerHiroshi Yokoi <yokoi.hiroshi@po.ntts.co.jp>2014-12-05 14:11:18 +0900
commitea42263b49d7dee42153d346172c1b275fc47b61 (patch)
tree20458bb789bcd30c86ac20428ca797a9c6ee1b60 /table
parent39686e18dbbbcfb32d891b86c3b1a978525bf370 (diff)
[bestpath_selection] fix build error
Diffstat (limited to 'table')
-rw-r--r--table/destination_test.go276
-rw-r--r--table/path.go54
-rw-r--r--table/path_test.go464
-rw-r--r--table/table.go6
4 files changed, 422 insertions, 378 deletions
diff --git a/table/destination_test.go b/table/destination_test.go
index 03b839a8..33cc379a 100644
--- a/table/destination_test.go
+++ b/table/destination_test.go
@@ -23,6 +23,158 @@ import (
"testing"
)
+func TestDestinationNewIPv4(t *testing.T) {
+ peerD := DestCreatePeer()
+ msgD := DestCreateMSG(peerD)
+ pathD := DestCreatePath(msgD)
+ ipv4d := NewIPv4Destination(pathD[0].getNlri())
+ assert.NotNil(t, ipv4d)
+}
+func TestDestinationNewIPv6(t *testing.T) {
+ peerD := DestCreatePeer()
+ msgD := DestCreateMSG(peerD)
+ pathD := DestCreatePath(msgD)
+ ipv6d := NewIPv6Destination(pathD[0].getNlri())
+ assert.NotNil(t, ipv6d)
+}
+
+func TestDestinationSetRouteFamily(t *testing.T) {
+ dd := &DestinationDefault{}
+ dd.setRouteFamily(RF_IPv4_UC)
+ rf := dd.getRouteFamily()
+ assert.Equal(t, rf, RF_IPv4_UC)
+}
+func TestDestinationGetRouteFamily(t *testing.T) {
+ dd := &DestinationDefault{}
+ dd.setRouteFamily(RF_IPv6_UC)
+ rf := dd.getRouteFamily()
+ assert.Equal(t, rf, RF_IPv6_UC)
+}
+func TestDestinationSetNlri(t *testing.T) {
+ dd := &DestinationDefault{}
+ nlri := bgp.NewNLRInfo(24, "13.2.3.1")
+ dd.setNlri(nlri)
+ r_nlri := dd.getNlri()
+ assert.Equal(t, r_nlri, nlri)
+}
+func TestDestinationGetNlri(t *testing.T) {
+ dd := &DestinationDefault{}
+ nlri := bgp.NewNLRInfo(24, "10.110.123.1")
+ dd.setNlri(nlri)
+ r_nlri := dd.getNlri()
+ assert.Equal(t, r_nlri, nlri)
+}
+func TestDestinationSetBestPathReason(t *testing.T) {
+ dd := &DestinationDefault{}
+ reason := "reason1"
+ dd.setBestPathReason(reason)
+ r_reason := dd.getBestPathReason()
+ assert.Equal(t, r_reason, reason)
+}
+func TestDestinationGetBestPathReason(t *testing.T) {
+ dd := &DestinationDefault{}
+ reason := "reason2"
+ dd.setBestPathReason(reason)
+ r_reason := dd.getBestPathReason()
+ assert.Equal(t, r_reason, reason)
+}
+func TestDestinationSetBestPath(t *testing.T) {
+ peerD := DestCreatePeer()
+ msgD := DestCreateMSG(peerD)
+ pathD := DestCreatePath(msgD)
+ ipv4d := NewIPv4Destination(pathD[0].getNlri())
+ ipv4d.setBestPath(pathD[0])
+ r_pathD := ipv4d.getBestPath()
+ assert.Equal(t, r_pathD, pathD[0])
+}
+func TestDestinationGetBestPath(t *testing.T) {
+ peerD := DestCreatePeer()
+ msgD := DestCreateMSG(peerD)
+ pathD := DestCreatePath(msgD)
+ ipv4d := NewIPv4Destination(pathD[0].getNlri())
+ ipv4d.setBestPath(pathD[0])
+ r_pathD := ipv4d.getBestPath()
+ assert.Equal(t, r_pathD, pathD[0])
+}
+func TestDestinationCalculate(t *testing.T) {
+ peerD := DestCreatePeer()
+ msgD := DestCreateMSG(peerD)
+ pathD := DestCreatePath(msgD)
+ ipv4d := NewIPv4Destination(pathD[0].getNlri())
+ //best path selection
+ ipv4d.addNewPath(pathD[0])
+ ipv4d.addNewPath(pathD[1])
+ ipv4d.addNewPath(pathD[2])
+ ipv4d.addWithdraw(pathD[2])
+ _, _, e := ipv4d.Calculate(uint32(100))
+ assert.Nil(t, e)
+}
+
+func TestDestinationRemoveSentRoute(t *testing.T) {
+ peerD := DestCreatePeer()
+ msgD := DestCreateMSG(peerD)
+ pathD := DestCreatePath(msgD)
+ ipv4d := NewIPv4Destination(pathD[0].getNlri())
+ //sent route and remove sent route
+ sroute1 := &SentRoute{path: pathD[0], peer: peerD[0]}
+ sroute2 := &SentRoute{path: pathD[1], peer: peerD[0]}
+ sroute3 := &SentRoute{path: pathD[2], peer: peerD[1]}
+ ipv4d.addSentRoute(sroute1)
+ ipv4d.addSentRoute(sroute2)
+ ipv4d.addSentRoute(sroute3)
+ result := ipv4d.removeSentRoute(peerD[2])
+ assert.Equal(t, result, false)
+ result = ipv4d.removeSentRoute(peerD[1])
+ assert.Equal(t, result, true)
+}
+
+func TestDestinationRemoveOldPathsFromSource(t *testing.T) {
+ peerD := DestCreatePeer()
+ msgD := DestCreateMSG(peerD)
+ pathD := DestCreatePath(msgD)
+ ipv4d := NewIPv4Destination(pathD[0].getNlri())
+ sroute1 := &SentRoute{path: pathD[0], peer: peerD[0]}
+ sroute2 := &SentRoute{path: pathD[1], peer: peerD[0]}
+ sroute3 := &SentRoute{path: pathD[2], peer: peerD[1]}
+ ipv4d.addSentRoute(sroute1)
+ ipv4d.addSentRoute(sroute2)
+ ipv4d.addSentRoute(sroute3)
+ compPath := make([]Path, 0)
+ for _, peer := range peerD {
+ r_path := ipv4d.removeOldPathsFromSource(peer)
+ assert.Equal(t, r_path, compPath)
+ }
+}
+
+func DestCreatePeer() []*Peer {
+ peerD1 := &Peer{VersionNum: 4, RemoteAs: 65000}
+ peerD2 := &Peer{VersionNum: 4, RemoteAs: 65001}
+ peerD3 := &Peer{VersionNum: 4, RemoteAs: 65002}
+ peerD := []*Peer{peerD1, peerD2, peerD3}
+ return peerD
+}
+func DestCreateMSG(peerD []*Peer) []*ProcessMessage {
+ bgpMsgD1 := updateMsgD1()
+ bgpMsgD2 := updateMsgD2()
+ bgpMsgD3 := updateMsgD3()
+ msgD1 := &ProcessMessage{innerMessage: bgpMsgD1, fromPeer: peerD[0]}
+ msgD2 := &ProcessMessage{innerMessage: bgpMsgD2, fromPeer: peerD[1]}
+ msgD3 := &ProcessMessage{innerMessage: bgpMsgD3, fromPeer: peerD[2]}
+ msgD := []*ProcessMessage{msgD1, msgD2, msgD3}
+ return msgD
+}
+func DestCreatePath(msgs []*ProcessMessage) []Path {
+ pathD := make([]Path, 3)
+ for i, msg := range msgs {
+ updateMsgD := msg.innerMessage.Body.(*bgp.BGPUpdate)
+ nlriList := updateMsgD.NLRI
+ pathAttributes := updateMsgD.PathAttributes
+ nlri_info := nlriList[0]
+ pathD[i] = CreatePath(msg.fromPeer, &nlri_info, pathAttributes, false)
+ }
+ return pathD
+}
+
func updateMsgD1() *bgp.BGPMessage {
origin := bgp.NewPathAttributeOrigin(0)
@@ -81,127 +233,3 @@ func updateMsgD3() *bgp.BGPMessage {
withdrawnRoutes := []bgp.WithdrawnRoute{w1}
return bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri)
}
-func createDestinationCheck(t *testing.T, path Path) (Destination, string) {
- dest := NewIPv4Destination(path.getNlri())
- ar := assert.NotNil(t, dest)
- if !ar {
- return nil, "NG"
- }
- return dest, "OK"
-}
-
-func routeCheck(t *testing.T, dest Destination) string {
- bgpMsg1 := updateMsgD1()
- bgpMsg2 := updateMsgD2()
- bgpMsg3 := updateMsgD3()
- pr1 := &Peer{VersionNum: 2, RemoteAs: 65000}
- pr2 := &Peer{VersionNum: 4, RemoteAs: 65001}
- pr3 := &Peer{VersionNum: 4, RemoteAs: 65002}
- msg1 := &ProcessMessage{innerMessage: bgpMsg1, fromPeer: pr1}
- msg2 := &ProcessMessage{innerMessage: bgpMsg2, fromPeer: pr1}
- msg3 := &ProcessMessage{innerMessage: bgpMsg3, fromPeer: pr2}
- path1, _ := createPathCheck(t, msg1)
- path2, _ := createPathCheck(t, msg2)
- path3, _ := createPathCheck(t, msg3)
-
- //best path selection
- dest.addNewPath(path1)
- dest.addNewPath(path2)
- dest.addNewPath(path3)
- dest.addWithdraw(path3)
-
- _, _, e := dest.Calculate(uint32(100))
- //bpath, str, e := dest.Calculate()
- //t.Log(bpath)
- //t.Log(str)
- ar := assert.Nil(t, e)
- if !ar {
- return "NG"
- }
-
- //sent route and remove sent route
- sroute1 := &SentRoute{path: path1, peer: pr1}
- sroute2 := &SentRoute{path: path2, peer: pr1}
- sroute3 := &SentRoute{path: path3, peer: pr2}
- dest.addSentRoute(sroute1)
- dest.addSentRoute(sroute2)
- dest.addSentRoute(sroute3)
- result := dest.removeSentRoute(pr3)
- ar = assert.Equal(t, result, false)
- if !ar {
- return "NG"
- }
- result = dest.removeSentRoute(pr2)
- ar = assert.Equal(t, result, true)
- if !ar {
- return "NG"
- }
-
- //remote old path
- rpath := dest.removeOldPathsFromSource(pr1)
- t.Log(rpath)
- //t.Log(dest.getKnownPathList())
- return "OK"
-}
-
-/*
-func getKnownPathListCheck(t *testing.T, dest Destination) string {
- paths := dest.getKnownPathList()
- t.Log(paths)
- return "OK"
-}
-*/
-
-//getter&setter test
-func dgsTerCheck(t *testing.T, path Path) string {
- dd := &DestinationDefault{}
- //check Route Family
- dd.setRouteFamily(RF_IPv4_UC)
- rf := dd.getRouteFamily()
- ar := assert.Equal(t, rf, RF_IPv4_UC)
- if !ar {
- return "NG"
- }
- //check nlri
- nlri := bgp.NewNLRInfo(24, "13.2.3.1")
- dd.setNlri(nlri)
- r_nlri := dd.getNlri()
- ar = assert.Equal(t, r_nlri, nlri)
- if !ar {
- return "NG"
- }
- // check best path reason
- reason := "reason"
- dd.setBestPathReason(reason)
- r_reason := dd.getBestPathReason()
- ar = assert.Equal(t, r_reason, reason)
- if !ar {
- return "NG"
- }
- //check best path
- dd.setBestPath(path)
- r_path := dd.getBestPath()
- ar = assert.Equal(t, r_path, path)
- if !ar {
- return "NG"
- }
- return "OK"
-}
-func TestDestination(t *testing.T) {
- bgpMsg1 := updateMsgD1()
- pr1 := &Peer{VersionNum: 2, RemoteAs: 65000}
- msg := &ProcessMessage{innerMessage: bgpMsg1, fromPeer: pr1}
- path, _ := createPathCheck(t, msg)
- t.Log("# CREATE PATH CHECK")
- dest, result := createDestinationCheck(t, path)
- t.Log("# CHECK END -> [ ", result, " ]")
- t.Log("")
- t.Log("# ROUTE CHECK")
- result = routeCheck(t, dest)
- t.Log("# CHECK END -> [ ", result, " ]")
- t.Log("")
- t.Log("# GETTER SETTER CHECK")
- result = dgsTerCheck(t, path)
- t.Log("# CHECK END -> [ ", result, " ]")
- t.Log("")
-}
diff --git a/table/path.go b/table/path.go
index 93d09d8f..0d5fe519 100644
--- a/table/path.go
+++ b/table/path.go
@@ -57,10 +57,6 @@ type PathDefault struct {
func NewPathDefault(source *Peer, nlri bgp.AddrPrefixInterface, sourceVerNum int, nexthop net.IP,
isWithdraw bool, pattr *utils.OrderedMap, medSetByTargetNeighbor bool) *PathDefault {
- if source == nil {
- logger.Error("Need to provide source")
- return nil
- }
if !isWithdraw && (pattr == nil || nexthop == nil) {
logger.Error("Need to provide nexthop and patattrs for path that is not a withdraw.")
@@ -174,9 +170,16 @@ func (pi *PathDefault) clone(forWithdrawal bool) Path {
if !forWithdrawal {
pathAttrs = pi.getPathAttributeMap()
}
- clone := NewPathDefault(pi.getSource(), pi.getNlri(), pi.getSourceVerNum(),
+ def := NewPathDefault(pi.getSource(), pi.getNlri(), pi.getSourceVerNum(),
pi.getNexthop(), forWithdrawal, pathAttrs, pi.getMedSetByTargetNeighbor())
- return clone
+ switch pi.ROUTE_FAMILY {
+ case RF_IPv4_UC:
+ return &IPv4Path{PathDefault: def}
+ case RF_IPv6_UC:
+ return &IPv6Path{PathDefault: def}
+ default:
+ return def
+ }
}
// return Path's string representation
@@ -190,20 +193,14 @@ func (pi *PathDefault) String() string {
}
func (pi *PathDefault) getPrefix() net.IP {
- addrPrefix := pi.nlri.(*bgp.NLRInfo)
- return addrPrefix.Prefix
-}
-
-func getNextHop(pathAttributes []bgp.PathAttributeInterface) net.IP {
- for _, pathAttr := range pathAttributes {
- switch p := pathAttr.(type) {
- case *bgp.PathAttributeNextHop:
- return p.Value
- }
+ switch nlri := pi.nlri.(type) {
+ case *bgp.NLRInfo:
+ return nlri.Prefix
+ case *bgp.WithdrawnRoute:
+ return nlri.Prefix
}
return nil
-
}
func createPathAttributeMap(pathAttributes []bgp.PathAttributeInterface) *utils.OrderedMap {
@@ -258,25 +255,36 @@ func CreatePath(source *Peer, nlri bgp.AddrPrefixInterface,
logger.Debugf("afi: %d, safi: %d ", int(nlri.AFI()), nlri.SAFI())
pathAttrMap := createPathAttributeMap(pathAttributes)
var path Path
+ var sourceVerNum int = 1
+
+ if source != nil {
+ sourceVerNum = source.VersionNum
+ }
switch rf {
case RF_IPv4_UC:
logger.Debugf("RouteFamily : %s", RF_IPv4_UC.String())
- nexthop := getNextHop(pathAttributes)
- path = NewIPv4Path(source, nlri, source.VersionNum, nexthop, isWithdraw, pathAttrMap, false)
+ var nexthop net.IP
+
+ if !isWithdraw {
+ nexthop_attr := pathAttrMap.Get(bgp.BGP_ATTR_TYPE_NEXT_HOP).(*bgp.PathAttributeNextHop)
+ nexthop = nexthop_attr.Value
+ } else {
+ nexthop = nil
+ }
+
+ path = NewIPv4Path(source, nlri, sourceVerNum, nexthop, isWithdraw, pathAttrMap, false)
case RF_IPv6_UC:
logger.Debugf("RouteFamily : %s", RF_IPv6_UC.String())
- var mpattr *bgp.PathAttributeMpReachNLRI
var nexthop net.IP
if !isWithdraw {
- mpattr = pathAttrMap.Get(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI)
+ mpattr := pathAttrMap.Get(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI)
nexthop = mpattr.Nexthop
} else {
nexthop = nil
}
-
- path = NewIPv6Path(source, nlri, source.VersionNum, nexthop, isWithdraw, pathAttrMap, false)
+ path = NewIPv6Path(source, nlri, sourceVerNum, nexthop, isWithdraw, pathAttrMap, false)
}
return path
}
diff --git a/table/path_test.go b/table/path_test.go
index d15a20f2..b8ac3b9d 100644
--- a/table/path_test.go
+++ b/table/path_test.go
@@ -9,262 +9,270 @@ import (
"testing"
)
-func updateMsg() *bgp.BGPMessage {
- w1 := bgp.WithdrawnRoute{*bgp.NewIPAddrPrefix(23, "121.1.3.2")}
- w2 := bgp.WithdrawnRoute{*bgp.NewIPAddrPrefix(17, "100.33.3.0")}
- w := []bgp.WithdrawnRoute{w1, w2}
- //w := []WithdrawnRoute{}
-
- aspath1 := []bgp.AsPathParamInterface{
- bgp.NewAsPathParam(2, []uint16{1000}),
- bgp.NewAsPathParam(1, []uint16{1001, 1002}),
- bgp.NewAsPathParam(2, []uint16{1003, 1004}),
- }
-
- aspath2 := []bgp.AsPathParamInterface{
- bgp.NewAs4PathParam(2, []uint32{1000000}),
- bgp.NewAs4PathParam(1, []uint32{1000001, 1002}),
- bgp.NewAs4PathParam(2, []uint32{1003, 100004}),
- }
-
- aspath3 := []*bgp.As4PathParam{
- bgp.NewAs4PathParam(2, []uint32{1000000}),
- bgp.NewAs4PathParam(1, []uint32{1000001, 1002}),
- bgp.NewAs4PathParam(2, []uint32{1003, 100004}),
- }
-
- ecommunities := []bgp.ExtendedCommunityInterface{
- &bgp.TwoOctetAsSpecificExtended{SubType: 1, AS: 10003, LocalAdmin: 3 << 20},
- &bgp.FourOctetAsSpecificExtended{SubType: 2, AS: 1 << 20, LocalAdmin: 300},
- &bgp.IPv4AddressSpecificExtended{SubType: 3, IPv4: net.ParseIP("192.2.1.2").To4(), LocalAdmin: 3000},
- &bgp.OpaqueExtended{Value: []byte{0, 1, 2, 3, 4, 5, 6, 7}},
- &bgp.UnknownExtended{Type: 99, Value: []byte{0, 1, 2, 3, 4, 5, 6, 7}},
- }
-
- mp_nlri := []bgp.AddrPrefixInterface{
- bgp.NewLabelledVPNIPAddrPrefix(20, "192.0.9.0", *bgp.NewLabel(1, 2, 3),
- bgp.NewRouteDistinguisherTwoOctetAS(256, 10000)),
- bgp.NewLabelledVPNIPAddrPrefix(26, "192.10.8.192", *bgp.NewLabel(5, 6, 7, 8),
- bgp.NewRouteDistinguisherIPAddressAS("10.0.1.1", 10001)),
- }
-
- mp_nlri2 := []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(100,
- "fe80:1234:1234:5667:8967:af12:8912:1023")}
-
- mp_nlri3 := []bgp.AddrPrefixInterface{bgp.NewLabelledVPNIPv6AddrPrefix(100,
- "fe80:1234:1234:5667:8967:af12:1203:33a1", *bgp.NewLabel(5, 6),
- bgp.NewRouteDistinguisherFourOctetAS(5, 6))}
-
- mp_nlri4 := []bgp.AddrPrefixInterface{bgp.NewLabelledIPAddrPrefix(25, "192.168.0.0",
- *bgp.NewLabel(5, 6, 7))}
-
- p := []bgp.PathAttributeInterface{
- bgp.NewPathAttributeOrigin(3),
- bgp.NewPathAttributeAsPath(aspath1),
- bgp.NewPathAttributeAsPath(aspath2),
- bgp.NewPathAttributeNextHop("129.1.1.2"),
- bgp.NewPathAttributeMultiExitDisc(1 << 20),
- bgp.NewPathAttributeLocalPref(1 << 22),
- bgp.NewPathAttributeAtomicAggregate(),
- bgp.NewPathAttributeAggregator(uint16(30002), "129.0.2.99"),
- bgp.NewPathAttributeAggregator(uint32(30002), "129.0.2.99"),
- bgp.NewPathAttributeAggregator(uint32(300020), "129.0.2.99"),
- bgp.NewPathAttributeCommunities([]uint32{1, 3}),
- bgp.NewPathAttributeOriginatorId("10.10.0.1"),
- bgp.NewPathAttributeClusterList([]string{"10.10.0.2", "10.10.0.3"}),
- bgp.NewPathAttributeExtendedCommunities(ecommunities),
- bgp.NewPathAttributeAs4Path(aspath3),
- bgp.NewPathAttributeAs4Aggregator(10000, "112.22.2.1"),
- bgp.NewPathAttributeMpReachNLRI("112.22.2.0", mp_nlri),
- bgp.NewPathAttributeMpReachNLRI("1023::", mp_nlri2),
- bgp.NewPathAttributeMpReachNLRI("fe80::", mp_nlri3),
- bgp.NewPathAttributeMpReachNLRI("129.1.1.1", mp_nlri4),
- bgp.NewPathAttributeMpUnreachNLRI(mp_nlri),
- &bgp.PathAttributeUnknown{
- PathAttribute: bgp.PathAttribute{
- Flags: 1,
- Type: 100,
- Value: []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
- },
- },
- }
- n := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "13.2.3.1"), *bgp.NewNLRInfo(24, "13.2.3.2")}
- //n := []bgp.NLRInfo{*bgp.NewNLRInfo(100, "fe80:1234:1234:5667:8967:af12:1203:33a1")}
- return bgp.NewBGPUpdateMessage(w, p, n)
+func TestPathNewIPv4(t *testing.T) {
+ peerP := PathCreatePeer()
+ msgP := PathCreateMSG(peerP)
+ pathP := PathCreatePath(msgP)
+ ipv4p := NewIPv4Path(pathP[0].getSource(), pathP[0].getNlri(), pathP[0].getSourceVerNum(), pathP[0].getNexthop(),
+ true, pathP[0].getPathAttributeMap(), pathP[0].getMedSetByTargetNeighbor())
+ assert.NotNil(t, ipv4p)
+}
+func TestPathNewIPv6(t *testing.T) {
+ peerP := PathCreatePeer()
+ msgP := PathCreateMSG(peerP)
+ pathP := PathCreatePath(msgP)
+ ipv6p := NewIPv6Path(pathP[0].getSource(), pathP[0].getNlri(), pathP[0].getSourceVerNum(), pathP[0].getNexthop(),
+ true, pathP[0].getPathAttributeMap(), pathP[0].getMedSetByTargetNeighbor())
+ assert.NotNil(t, ipv6p)
}
-func initMsg() *ProcessMessage {
- bgpMessage := updateMsg()
- peer := &Peer{}
- peer.VersionNum = 4
- peer.RemoteAs = 65000
-
- msg := &ProcessMessage{
- innerMessage: bgpMessage,
- fromPeer: peer,
- }
- return msg
+func TestPathIPv4SetDefault(t *testing.T) {
+ pd := &PathDefault{withdraw: true}
+ ipv4p := &IPv4Path{}
+ ipv4p.setPathDefault(pd)
+ r_pd := ipv4p.getPathDefault()
+ assert.Equal(t, r_pd, pd)
}
-func createPathCheck(t *testing.T, msg *ProcessMessage) (Path, string) {
- updateMsg := msg.innerMessage.Body.(*bgp.BGPUpdate)
- nlriList := updateMsg.NLRI
- pathAttributes := updateMsg.PathAttributes
- nlri_info := nlriList[0]
- path := CreatePath(msg.fromPeer, &nlri_info, pathAttributes, false)
- ar := assert.NotNil(t, path, "Path is Nil")
- if !ar {
- return nil, "NG"
- }
- return path, "OK"
+func TestPathIPv4GetDefault(t *testing.T) {
+ pd := &PathDefault{withdraw: false}
+ ipv4p := &IPv4Path{}
+ ipv4p.setPathDefault(pd)
+ r_pd := ipv4p.getPathDefault()
+ assert.Equal(t, r_pd, pd)
}
-func getNextHopCheck(t *testing.T, msg *ProcessMessage) string {
- nexthop := "129.1.1.2"
- pAttr := msg.innerMessage.Body.(*bgp.BGPUpdate).PathAttributes
- r_nexthop := getNextHop(pAttr)
- ar := assert.Equal(t, r_nexthop.String(), nexthop, "unmatch nexthop")
- if !ar {
- return "NG"
- }
- return "OK"
+func TestPathIPv6SetDefault(t *testing.T) {
+ pd := &PathDefault{sourceVerNum: 4}
+ ipv6p := &IPv6Path{}
+ ipv6p.setPathDefault(pd)
+ r_pd := ipv6p.getPathDefault()
+ assert.Equal(t, r_pd, pd)
}
-func getPrefixCheck(t *testing.T, path Path) string {
- prefix := "13.2.3.1"
- //prefix := "fe80:1234:1234:5667:8967:af12:1203:33a1"
- r_prefix := path.getPrefix()
- ar := assert.Equal(t, r_prefix.String(), prefix, "unmatch prefix")
- if !ar {
- return "NG"
- }
- return "OK"
-}
-
-func getPathAttributeCheck(t *testing.T, path Path) string {
- nh := "129.1.1.2"
- pa := path.getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP)
- r_nh := pa.(*bgp.PathAttributeNextHop).Value.String()
- ar := assert.Equal(t, r_nh, nh, "unmatch nexthop")
- if !ar {
- return "NG"
- }
- return "OK"
+func TestPathIPv6GetDefault(t *testing.T) {
+ pd := &PathDefault{sourceVerNum: 5}
+ ipv6p := &IPv6Path{}
+ ipv6p.setPathDefault(pd)
+ r_pd := ipv6p.getPathDefault()
+ assert.Equal(t, r_pd, pd)
}
-
-func cloneCheck(t *testing.T, path Path) string {
-
- prefix := path.getPrefix()
- cl := path.clone(false)
- r_prefix := cl.getPrefix()
- ar := assert.Equal(t, r_prefix, prefix, "unmatch prefix in clone element")
- if !ar {
- return "NG"
- }
- return "OK"
-}
-
-//getter&setter test
-func pgsTerCheck(t *testing.T) string {
+func TestPathSetRouteFamily(t *testing.T) {
pd := &PathDefault{}
- //check Route Family
pd.setRouteFamily(RF_IPv4_UC)
rf := pd.getRouteFamily()
- ar := assert.Equal(t, rf, RF_IPv4_UC, "unmatch route family")
- if !ar {
- return "NG"
- }
-
- //check source
- pr := &Peer{
- RemoteAs: 65000,
- VersionNum: 4,
- }
+ assert.Equal(t, rf, RF_IPv4_UC)
+}
+func TestPathGetRouteFamily(t *testing.T) {
+ pd := &PathDefault{}
+ pd.setRouteFamily(RF_IPv6_UC)
+ rf := pd.getRouteFamily()
+ assert.Equal(t, rf, RF_IPv6_UC)
+}
+func TestPathSetSource(t *testing.T) {
+ pd := &PathDefault{}
+ pr := &Peer{RemoteAs: 65000, VersionNum: 4}
pd.setSource(pr)
r_pr := pd.getSource()
- ar = assert.Equal(t, r_pr, pr, "unmatch source")
- if !ar {
- return "NG"
- }
- //check nexthop
+ assert.Equal(t, r_pr, pr)
+}
+func TestPathGetSource(t *testing.T) {
+ pd := &PathDefault{}
+ pr := &Peer{RemoteAs: 65001, VersionNum: 4}
+ pd.setSource(pr)
+ r_pr := pd.getSource()
+ assert.Equal(t, r_pr, pr)
+}
+func TestPathSetNexthop(t *testing.T) {
+ pd := &PathDefault{}
ip := net.ParseIP("192.168.0.1")
pd.setNexthop(ip)
nh := pd.getNexthop()
- ar = assert.Equal(t, nh, ip, "unmatch nexthop")
- if !ar {
- return "NG"
- }
- //check source version num
+ assert.Equal(t, nh, ip)
+}
+func TestPathgetNexthop(t *testing.T) {
+ pd := &PathDefault{}
+ ip := net.ParseIP("192.168.0.2")
+ pd.setNexthop(ip)
+ nh := pd.getNexthop()
+ assert.Equal(t, nh, ip)
+}
+func TestPathSetSourceVerNum(t *testing.T) {
+ pd := &PathDefault{}
svn := 4
pd.setSourceVerNum(svn)
r_svn := pd.getSourceVerNum()
- ar = assert.Equal(t, r_svn, svn, "unmatch source ver num")
- if !ar {
- return "NG"
- }
- //check wighdrow
+ assert.Equal(t, r_svn, svn)
+}
+func TestPathGetSourceVerNum(t *testing.T) {
+ pd := &PathDefault{}
+ svn := 5
+ pd.setSourceVerNum(svn)
+ r_svn := pd.getSourceVerNum()
+ assert.Equal(t, r_svn, svn)
+}
+func TestPathSetWithdraw(t *testing.T) {
+ pd := &PathDefault{}
wd := true
pd.setWithdraw(wd)
r_wd := pd.isWithdraw()
- ar = assert.Equal(t, r_wd, wd, "unmatch withdrow flg")
- if !ar {
- return "NG"
- }
- //check nlri
+ assert.Equal(t, r_wd, wd)
+}
+func TestPathGetWithdaw(t *testing.T) {
+ pd := &PathDefault{}
+ wd := false
+ pd.setWithdraw(wd)
+ r_wd := pd.isWithdraw()
+ assert.Equal(t, r_wd, wd)
+}
+func TestPathSetNlri(t *testing.T) {
+ pd := &PathDefault{}
nlri := bgp.NewNLRInfo(24, "13.2.3.1")
pd.setNlri(nlri)
r_nlri := pd.getNlri()
- ar = assert.Equal(t, r_nlri, nlri, "unmatch nlri")
- if !ar {
- return "NG"
- }
- //check med set by targetNeighbor
+ assert.Equal(t, r_nlri, nlri)
+}
+func TestPathGetNlri(t *testing.T) {
+ pd := &PathDefault{}
+ nlri := bgp.NewNLRInfo(24, "13.2.3.2")
+ pd.setNlri(nlri)
+ r_nlri := pd.getNlri()
+ assert.Equal(t, r_nlri, nlri)
+}
+func TestPathSetMedSetByTargetNeighbor(t *testing.T) {
+ pd := &PathDefault{}
msbt := true
pd.setMedSetByTargetNeighbor(msbt)
r_msbt := pd.getMedSetByTargetNeighbor()
- ar = assert.Equal(t, r_msbt, msbt, "unmatch med flg")
- if !ar {
- return "NG"
+ assert.Equal(t, r_msbt, msbt)
+}
+func TestPathGetMedSetByTargetNeighbor(t *testing.T) {
+ pd := &PathDefault{}
+ msbt := true
+ pd.setMedSetByTargetNeighbor(msbt)
+ r_msbt := pd.getMedSetByTargetNeighbor()
+ assert.Equal(t, r_msbt, msbt)
+}
+
+func TestPathCreatePath(t *testing.T) {
+ peerP := PathCreatePeer()
+ msgP := PathCreateMSG(peerP)
+ updateMsgP := msgP[0].innerMessage.Body.(*bgp.BGPUpdate)
+ nlriList := updateMsgP.NLRI
+ pathAttributes := updateMsgP.PathAttributes
+ nlri_info := nlriList[0]
+ path := CreatePath(msgP[0].fromPeer, &nlri_info, pathAttributes, false)
+ assert.NotNil(t, path)
+
+}
+
+func TestPathGetPrefix(t *testing.T) {
+ peerP := PathCreatePeer()
+ msgP := PathCreateMSG(peerP)
+ pathP := PathCreatePath(msgP)
+ prefix := "10.10.10.0"
+ r_prefix := pathP[0].getPrefix()
+ assert.Equal(t, r_prefix.String(), prefix)
+}
+func TestPathGetAttribute(t *testing.T) {
+ peerP := PathCreatePeer()
+ msgP := PathCreateMSG(peerP)
+ pathP := PathCreatePath(msgP)
+ nh := "192.168.50.1"
+ pa := pathP[0].getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP)
+ r_nh := pa.(*bgp.PathAttributeNextHop).Value.String()
+ assert.Equal(t, r_nh, nh)
+}
+
+func TestPathClone(t *testing.T) {
+ peerP := PathCreatePeer()
+ msgP := PathCreateMSG(peerP)
+ pathP := PathCreatePath(msgP)
+ clPath := pathP[0].clone(false)
+ assert.Equal(t, clPath, pathP[0])
+}
+
+func PathCreatePeer() []*Peer {
+ peerP1 := &Peer{VersionNum: 4, RemoteAs: 65000}
+ peerP2 := &Peer{VersionNum: 4, RemoteAs: 65001}
+ peerP3 := &Peer{VersionNum: 4, RemoteAs: 65002}
+ peerP := []*Peer{peerP1, peerP2, peerP3}
+ return peerP
+}
+func PathCreateMSG(peerP []*Peer) []*ProcessMessage {
+ bgpMsgP1 := updateMsgP1()
+ bgpMsgP2 := updateMsgP2()
+ bgpMsgP3 := updateMsgP3()
+ msgP1 := &ProcessMessage{innerMessage: bgpMsgP1, fromPeer: peerP[0]}
+ msgP2 := &ProcessMessage{innerMessage: bgpMsgP2, fromPeer: peerP[1]}
+ msgP3 := &ProcessMessage{innerMessage: bgpMsgP3, fromPeer: peerP[2]}
+ msgP := []*ProcessMessage{msgP1, msgP2, msgP3}
+ return msgP
+}
+func PathCreatePath(msgs []*ProcessMessage) []Path {
+ pathP := make([]Path, 3)
+ for i, msg := range msgs {
+ updateMsgP := msg.innerMessage.Body.(*bgp.BGPUpdate)
+ nlriList := updateMsgP.NLRI
+ pathAttributes := updateMsgP.PathAttributes
+ nlri_info := nlriList[0]
+ pathP[i] = CreatePath(msg.fromPeer, &nlri_info, pathAttributes, false)
}
- //ipv4 pathDefault
- ipv4 := &IPv4Path{}
- ipv4.setPathDefault(pd)
- r_pd4 := ipv4.getPathDefault()
- ar = assert.Equal(t, r_pd4, pd, "unmatch path default")
- if !ar {
- return "NG"
+ return pathP
+}
+
+func updateMsgP1() *bgp.BGPMessage {
+
+ origin := bgp.NewPathAttributeOrigin(0)
+ aspathParam := []bgp.AsPathParamInterface{bgp.NewAsPathParam(2, []uint16{65000})}
+ aspath := bgp.NewPathAttributeAsPath(aspathParam)
+ nexthop := bgp.NewPathAttributeNextHop("192.168.50.1")
+ med := bgp.NewPathAttributeMultiExitDisc(0)
+
+ pathAttributes := []bgp.PathAttributeInterface{
+ origin,
+ aspath,
+ nexthop,
+ med,
}
- //ipv6 pathDefault
- ipv6 := &IPv6Path{}
- ipv6.setPathDefault(pd)
- r_pd6 := ipv6.getPathDefault()
- ar = assert.Equal(t, r_pd6, pd, "unmatch path default")
- if !ar {
- return "NG"
+
+ nlri := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.10.0")}
+ withdrawnRoutes := []bgp.WithdrawnRoute{}
+ return bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri)
+}
+
+func updateMsgP2() *bgp.BGPMessage {
+
+ origin := bgp.NewPathAttributeOrigin(0)
+ aspathParam := []bgp.AsPathParamInterface{bgp.NewAsPathParam(2, []uint16{65100})}
+ aspath := bgp.NewPathAttributeAsPath(aspathParam)
+ nexthop := bgp.NewPathAttributeNextHop("192.168.100.1")
+ med := bgp.NewPathAttributeMultiExitDisc(100)
+
+ pathAttributes := []bgp.PathAttributeInterface{
+ origin,
+ aspath,
+ nexthop,
+ med,
}
- return "OK"
+
+ nlri := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "20.20.20.0")}
+ withdrawnRoutes := []bgp.WithdrawnRoute{}
+ return bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri)
}
-func TestPath(t *testing.T) {
- msg := initMsg()
- t.Log("# CREATE PATH CHECK")
- path, result := createPathCheck(t, msg)
- t.Log("# CHECK END -> [ ", result, " ]")
- t.Log("")
- t.Log("# GET NEXTHOP CHECK")
- result = getNextHopCheck(t, msg)
- t.Log("# CHECK END -> [ ", result, " ]")
- t.Log("")
- t.Log("# GET PREFIX CHECK")
- result = getPrefixCheck(t, path)
- t.Log("# CHECK END -> [ ", result, " ]")
- t.Log("")
- t.Log("# GET PATH ATTRIBUTE CHECK")
- result = getPathAttributeCheck(t, path)
- t.Log("# CHECK END -> [ ", result, " ]")
- t.Log("")
- t.Log("# CLONE CHECK")
- result = cloneCheck(t, path)
- t.Log("# CHECK END -> [ ", result, " ]")
- t.Log("")
- t.Log("# GETTER SETTER CHECK")
- result = pgsTerCheck(t)
- t.Log("# CHECK END -> [ ", result, " ]")
- t.Log("")
+func updateMsgP3() *bgp.BGPMessage {
+ origin := bgp.NewPathAttributeOrigin(0)
+ aspathParam := []bgp.AsPathParamInterface{bgp.NewAsPathParam(2, []uint16{65100})}
+ aspath := bgp.NewPathAttributeAsPath(aspathParam)
+ nexthop := bgp.NewPathAttributeNextHop("192.168.150.1")
+ med := bgp.NewPathAttributeMultiExitDisc(100)
+
+ pathAttributes := []bgp.PathAttributeInterface{
+ origin,
+ aspath,
+ nexthop,
+ med,
+ }
+
+ 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)
}
diff --git a/table/table.go b/table/table.go
index 22554eba..99c4de1f 100644
--- a/table/table.go
+++ b/table/table.go
@@ -39,7 +39,7 @@ type TableDefault struct {
//need SignalBus
}
-func NewTableDefault(scope_id, coreService *CoreService) *TableDefault {
+func NewTableDefault(scope_id int, coreService *CoreService) *TableDefault {
table := &TableDefault{}
table.ROUTE_FAMILY = RF_IPv4_UC
table.destinations = make(map[string]Destination)
@@ -194,7 +194,7 @@ type IPv4Table struct {
//need structure
}
-func NewIPv4Table(scope_id, coreService *CoreService) *IPv4Table {
+func NewIPv4Table(scope_id int, coreService *CoreService) *IPv4Table {
ipv4Table := &IPv4Table{}
ipv4Table.TableDefault = NewTableDefault(scope_id, coreService)
ipv4Table.TableDefault.ROUTE_FAMILY = RF_IPv4_UC
@@ -228,7 +228,7 @@ type IPv6Table struct {
//need structure
}
-func NewIPv6Table(scope_id, coreService *CoreService) *IPv6Table {
+func NewIPv6Table(scope_id int, coreService *CoreService) *IPv6Table {
ipv6Table := &IPv6Table{}
ipv6Table.TableDefault = NewTableDefault(scope_id, coreService)
ipv6Table.TableDefault.ROUTE_FAMILY = RF_IPv6_UC