diff options
author | ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp> | 2015-04-27 11:02:42 +0000 |
---|---|---|
committer | ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp> | 2015-04-27 15:20:51 +0000 |
commit | 87a951546c43cb70ad3a03ec42de7cead8d25d72 (patch) | |
tree | 62e79e2dea8ae5a6a8f13c7f88f9cd3d0eaf3a29 | |
parent | 91cb8ea23f454592be9fb4565f0f477afdbcb0ec (diff) |
table: make table.ProcessMessage a function
Signed-off-by: ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>
-rw-r--r-- | policy/policy_test.go | 51 | ||||
-rw-r--r-- | server/peer.go | 3 | ||||
-rw-r--r-- | server/peer_test.go | 3 | ||||
-rw-r--r-- | table/destination_test.go | 31 | ||||
-rw-r--r-- | table/path_test.go | 34 | ||||
-rw-r--r-- | table/table_manager.go | 53 | ||||
-rw-r--r-- | table/table_manager_test.go | 9 | ||||
-rw-r--r-- | table/table_test.go | 28 |
8 files changed, 71 insertions, 141 deletions
diff --git a/policy/policy_test.go b/policy/policy_test.go index d77a7b7c..ac2c930b 100644 --- a/policy/policy_test.go +++ b/policy/policy_test.go @@ -38,8 +38,7 @@ func TestPrefixCalcurateNoRange(t *testing.T) { nlri := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.0.101")} withdrawnRoutes := []bgp.WithdrawnRoute{} updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri) - msg := table.NewProcessMessage(updateMsg, peer) - path := msg.ToPathList()[0] + path := table.ProcessMessage(updateMsg, peer)[0] // test pl1, _ := NewPrefix(net.ParseIP("10.10.0.0"), 24, "") match1 := IpPrefixCalculate(path, pl1) @@ -64,8 +63,7 @@ func TestPrefixCalcurateAddress(t *testing.T) { nlri := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.0.101")} withdrawnRoutes := []bgp.WithdrawnRoute{} updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri) - msg := table.NewProcessMessage(updateMsg, peer) - path := msg.ToPathList()[0] + path := table.ProcessMessage(updateMsg, peer)[0] // test pl1, _ := NewPrefix(net.ParseIP("10.11.0.0"), 16, "21..24") match1 := IpPrefixCalculate(path, pl1) @@ -87,8 +85,7 @@ func TestPrefixCalcurateLength(t *testing.T) { nlri := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.0.101")} withdrawnRoutes := []bgp.WithdrawnRoute{} updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri) - msg := table.NewProcessMessage(updateMsg, peer) - path := msg.ToPathList()[0] + path := table.ProcessMessage(updateMsg, peer)[0] // test pl1, _ := NewPrefix(net.ParseIP("10.10.64.0"), 24, "21..24") match1 := IpPrefixCalculate(path, pl1) @@ -110,8 +107,7 @@ func TestPrefixCalcurateLengthRange(t *testing.T) { nlri := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.0.101")} withdrawnRoutes := []bgp.WithdrawnRoute{} updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri) - msg := table.NewProcessMessage(updateMsg, peer) - path := msg.ToPathList()[0] + path := table.ProcessMessage(updateMsg, peer)[0] // test pl1, _ := NewPrefix(net.ParseIP("10.10.0.0"), 16, "21..23") match1 := IpPrefixCalculate(path, pl1) @@ -138,8 +134,7 @@ func TestPrefixCalcurateNoRangeIPv6(t *testing.T) { nlri := []bgp.NLRInfo{} withdrawnRoutes := []bgp.WithdrawnRoute{} updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri) - msg := table.NewProcessMessage(updateMsg, peer) - path := msg.ToPathList()[0] + path := table.ProcessMessage(updateMsg, peer)[0] // test pl1, _ := NewPrefix(net.ParseIP("2001:123:123::"), 48, "") match1 := IpPrefixCalculate(path, pl1) @@ -165,8 +160,7 @@ func TestPrefixCalcurateAddressIPv6(t *testing.T) { nlri := []bgp.NLRInfo{} withdrawnRoutes := []bgp.WithdrawnRoute{} updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri) - msg := table.NewProcessMessage(updateMsg, peer) - path := msg.ToPathList()[0] + path := table.ProcessMessage(updateMsg, peer)[0] // test pl1, _ := NewPrefix(net.ParseIP("2001:123:128::"), 48, "64..80") match1 := IpPrefixCalculate(path, pl1) @@ -189,8 +183,7 @@ func TestPrefixCalcurateLengthIPv6(t *testing.T) { nlri := []bgp.NLRInfo{} withdrawnRoutes := []bgp.WithdrawnRoute{} updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri) - msg := table.NewProcessMessage(updateMsg, peer) - path := msg.ToPathList()[0] + path := table.ProcessMessage(updateMsg, peer)[0] // test pl1, _ := NewPrefix(net.ParseIP("2001:123:123:64::"), 64, "64..80") match1 := IpPrefixCalculate(path, pl1) @@ -213,8 +206,7 @@ func TestPrefixCalcurateLengthRangeIPv6(t *testing.T) { nlri := []bgp.NLRInfo{} withdrawnRoutes := []bgp.WithdrawnRoute{} updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri) - msg := table.NewProcessMessage(updateMsg, peer) - path := msg.ToPathList()[0] + path := table.ProcessMessage(updateMsg, peer)[0] // test pl1, _ := NewPrefix(net.ParseIP("2001:123:123::"), 48, "62..63") match1 := IpPrefixCalculate(path, pl1) @@ -239,8 +231,7 @@ func TestPolicyNotMatch(t *testing.T) { nlri := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.0.101")} withdrawnRoutes := []bgp.WithdrawnRoute{} updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri) - msg := table.NewProcessMessage(updateMsg, peer) - path := msg.ToPathList()[0] + path := table.ProcessMessage(updateMsg, peer)[0] // create policy ps := config.PrefixSet{ PrefixSetName: "ps1", @@ -298,8 +289,7 @@ func TestPolicyMatchAndReject(t *testing.T) { nlri := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.0.101")} withdrawnRoutes := []bgp.WithdrawnRoute{} updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri) - msg := table.NewProcessMessage(updateMsg, peer) - path := msg.ToPathList()[0] + path := table.ProcessMessage(updateMsg, peer)[0] // create policy ps := config.PrefixSet{ PrefixSetName: "ps1", @@ -357,8 +347,7 @@ func TestPolicyMatchAndAccept(t *testing.T) { nlri := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.0.101")} withdrawnRoutes := []bgp.WithdrawnRoute{} updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri) - msg := table.NewProcessMessage(updateMsg, peer) - path := msg.ToPathList()[0] + path := table.ProcessMessage(updateMsg, peer)[0] // create policy ps := config.PrefixSet{ PrefixSetName: "ps1", @@ -416,8 +405,7 @@ func TestPolicyRejectOnlyPrefixSet(t *testing.T) { nlri := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.1.101")} withdrawnRoutes := []bgp.WithdrawnRoute{} updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri) - msg := table.NewProcessMessage(updateMsg, peer) - path1 := msg.ToPathList()[0] + path1 := table.ProcessMessage(updateMsg, peer)[0] peer = &table.PeerInfo{AS: 65002, Address: net.ParseIP("10.0.2.2")} origin = bgp.NewPathAttributeOrigin(0) @@ -429,8 +417,7 @@ func TestPolicyRejectOnlyPrefixSet(t *testing.T) { nlri = []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.9.2.102")} withdrawnRoutes = []bgp.WithdrawnRoute{} updateMsg = bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri) - msg = table.NewProcessMessage(updateMsg, peer) - path2 := msg.ToPathList()[0] + path2 := table.ProcessMessage(updateMsg, peer)[0] // create policy ps := config.PrefixSet{ @@ -487,8 +474,7 @@ func TestPolicyRejectOnlyNeighborSet(t *testing.T) { nlri := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.1.101")} withdrawnRoutes := []bgp.WithdrawnRoute{} updateMsg := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri) - msg := table.NewProcessMessage(updateMsg, peer) - path1 := msg.ToPathList()[0] + path1 := table.ProcessMessage(updateMsg, peer)[0] peer = &table.PeerInfo{AS: 65002, Address: net.ParseIP("10.0.2.2")} origin = bgp.NewPathAttributeOrigin(0) @@ -500,8 +486,7 @@ func TestPolicyRejectOnlyNeighborSet(t *testing.T) { nlri = []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.2.102")} withdrawnRoutes = []bgp.WithdrawnRoute{} updateMsg = bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri) - msg = table.NewProcessMessage(updateMsg, peer) - path2 := msg.ToPathList()[0] + path2 := table.ProcessMessage(updateMsg, peer)[0] // create policy ns := config.NeighborSet{ @@ -556,8 +541,7 @@ func TestPolicyDifferentRoutefamilyOfPathAndPolicy(t *testing.T) { nlriIPv4 := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.0.101")} withdrawnRoutesIPv4 := []bgp.WithdrawnRoute{} updateMsgIPv4 := bgp.NewBGPUpdateMessage(withdrawnRoutesIPv4, pathAttributesIPv4, nlriIPv4) - msgIPv4 := table.NewProcessMessage(updateMsgIPv4, peerIPv4) - pathIPv4 := msgIPv4.ToPathList()[0] + pathIPv4 := table.ProcessMessage(updateMsgIPv4, peerIPv4)[0] // creatae path ipv6 peerIPv6 := &table.PeerInfo{AS: 65001, Address: net.ParseIP("2001::192:168:50:1")} originIPv6 := bgp.NewPathAttributeOrigin(0) @@ -570,8 +554,7 @@ func TestPolicyDifferentRoutefamilyOfPathAndPolicy(t *testing.T) { nlriIPv6 := []bgp.NLRInfo{} withdrawnRoutesIPv6 := []bgp.WithdrawnRoute{} updateMsgIPv6 := bgp.NewBGPUpdateMessage(withdrawnRoutesIPv6, pathAttributesIPv6, nlriIPv6) - msgIPv6 := table.NewProcessMessage(updateMsgIPv6, peerIPv6) - pathIPv6 := msgIPv6.ToPathList()[0] + pathIPv6 := table.ProcessMessage(updateMsgIPv6, peerIPv6)[0] // create policy psIPv4 := config.PrefixSet{ PrefixSetName: "psIPv4", diff --git a/server/peer.go b/server/peer.go index c99f5fd2..d56e3e34 100644 --- a/server/peer.go +++ b/server/peer.go @@ -257,8 +257,7 @@ func (peer *Peer) handleBGPmessage(m *bgp.BGPMessage) { return } table.UpdatePathAttrs4ByteAs(body) - msg := table.NewProcessMessage(m, peer.peerInfo) - pathList := msg.ToPathList() + pathList := table.ProcessMessage(m, peer.peerInfo) peer.adjRib.UpdateIn(pathList) peer.sendPathsToSiblings(pathList) } diff --git a/server/peer_test.go b/server/peer_test.go index ab3bfa54..756607e2 100644 --- a/server/peer_test.go +++ b/server/peer_test.go @@ -64,8 +64,7 @@ func TestProcessBGPUpdate_fourbyteAS(t *testing.T) { m := update_fromRC3() peerInfo := peerRC3() - msg := table.NewProcessMessage(m, peerInfo) - pathList := msg.ToPathList() + pathList := table.ProcessMessage(m, peerInfo) pList, _ := rib1.ProcessPaths(pathList) assert.Equal(t, len(pList), 1) diff --git a/table/destination_test.go b/table/destination_test.go index db4e49c1..751c94db 100644 --- a/table/destination_test.go +++ b/table/destination_test.go @@ -26,15 +26,13 @@ import ( func TestDestinationNewIPv4(t *testing.T) { peerD := DestCreatePeer() - msgD := DestCreateMSG(peerD) - pathD := DestCreatePath(msgD) + pathD := DestCreatePath(peerD) ipv4d := NewIPv4Destination(pathD[0].GetNlri()) assert.NotNil(t, ipv4d) } func TestDestinationNewIPv6(t *testing.T) { peerD := DestCreatePeer() - msgD := DestCreateMSG(peerD) - pathD := DestCreatePath(msgD) + pathD := DestCreatePath(peerD) ipv6d := NewIPv6Destination(pathD[0].GetNlri()) assert.NotNil(t, ipv6d) } @@ -81,8 +79,7 @@ func TestDestinationGetBestPathReason(t *testing.T) { } func TestDestinationSetBestPath(t *testing.T) { peerD := DestCreatePeer() - msgD := DestCreateMSG(peerD) - pathD := DestCreatePath(msgD) + pathD := DestCreatePath(peerD) ipv4d := NewIPv4Destination(pathD[0].GetNlri()) ipv4d.setBestPath(pathD[0]) r_pathD := ipv4d.getBestPath() @@ -90,8 +87,7 @@ func TestDestinationSetBestPath(t *testing.T) { } func TestDestinationGetBestPath(t *testing.T) { peerD := DestCreatePeer() - msgD := DestCreateMSG(peerD) - pathD := DestCreatePath(msgD) + pathD := DestCreatePath(peerD) ipv4d := NewIPv4Destination(pathD[0].GetNlri()) ipv4d.setBestPath(pathD[0]) r_pathD := ipv4d.getBestPath() @@ -99,8 +95,7 @@ func TestDestinationGetBestPath(t *testing.T) { } func TestDestinationCalculate(t *testing.T) { peerD := DestCreatePeer() - msgD := DestCreateMSG(peerD) - pathD := DestCreatePath(msgD) + pathD := DestCreatePath(peerD) ipv4d := NewIPv4Destination(pathD[0].GetNlri()) //best path selection ipv4d.addNewPath(pathD[0]) @@ -118,24 +113,18 @@ func DestCreatePeer() []*PeerInfo { peerD := []*PeerInfo{peerD1, peerD2, peerD3} return peerD } -func DestCreateMSG(peerD []*PeerInfo) []*ProcessMessage { + +func DestCreatePath(peerD []*PeerInfo) []Path { 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) + for i, msg := range []*bgp.BGPMessage{bgpMsgD1, bgpMsgD2, bgpMsgD3} { + updateMsgD := msg.Body.(*bgp.BGPUpdate) nlriList := updateMsgD.NLRI pathAttributes := updateMsgD.PathAttributes nlri_info := nlriList[0] - pathD[i], _ = CreatePath(msg.fromPeer, &nlri_info, pathAttributes, false, time.Now()) + pathD[i], _ = CreatePath(peerD[i], &nlri_info, pathAttributes, false, time.Now()) } return pathD } diff --git a/table/path_test.go b/table/path_test.go index 378e37c7..aa8c4aa2 100644 --- a/table/path_test.go +++ b/table/path_test.go @@ -11,15 +11,13 @@ import ( func TestPathNewIPv4(t *testing.T) { peerP := PathCreatePeer() - msgP := PathCreateMSG(peerP) - pathP := PathCreatePath(msgP) + pathP := PathCreatePath(peerP) ipv4p := NewIPv4Path(pathP[0].GetSource(), pathP[0].GetNlri(), true, pathP[0].getPathAttrs(), pathP[0].getMedSetByTargetNeighbor(), time.Now()) assert.NotNil(t, ipv4p) } func TestPathNewIPv6(t *testing.T) { peerP := PathCreatePeer() - msgP := PathCreateMSG(peerP) - pathP := PathCreatePath(msgP) + pathP := PathCreatePath(peerP) ipv6p := NewIPv6Path(pathP[0].GetSource(), pathP[0].GetNlri(), true, pathP[0].getPathAttrs(), pathP[0].getMedSetByTargetNeighbor(), time.Now()) assert.NotNil(t, ipv6p) } @@ -121,28 +119,26 @@ func TestPathGetMedSetByTargetNeighbor(t *testing.T) { func TestPathCreatePath(t *testing.T) { peerP := PathCreatePeer() - msgP := PathCreateMSG(peerP) - updateMsgP := msgP[0].innerMessage.Body.(*bgp.BGPUpdate) + msg := updateMsgP1() + updateMsgP := msg.Body.(*bgp.BGPUpdate) nlriList := updateMsgP.NLRI pathAttributes := updateMsgP.PathAttributes nlri_info := nlriList[0] - path, _ := CreatePath(msgP[0].fromPeer, &nlri_info, pathAttributes, false, time.Now()) + path, _ := CreatePath(peerP[0], &nlri_info, pathAttributes, false, time.Now()) assert.NotNil(t, path) } func TestPathGetPrefix(t *testing.T) { peerP := PathCreatePeer() - msgP := PathCreateMSG(peerP) - pathP := PathCreatePath(msgP) + pathP := PathCreatePath(peerP) prefix := "10.10.10.0/24" r_prefix := pathP[0].getPrefix() assert.Equal(t, r_prefix, prefix) } func TestPathGetAttribute(t *testing.T) { peerP := PathCreatePeer() - msgP := PathCreateMSG(peerP) - pathP := PathCreatePath(msgP) + pathP := PathCreatePath(peerP) nh := "192.168.50.1" _, pa := pathP[0].getPathAttr(bgp.BGP_ATTR_TYPE_NEXT_HOP) r_nh := pa.(*bgp.PathAttributeNextHop).Value.String() @@ -156,24 +152,18 @@ func PathCreatePeer() []*PeerInfo { peerP := []*PeerInfo{peerP1, peerP2, peerP3} return peerP } -func PathCreateMSG(peerP []*PeerInfo) []*ProcessMessage { + +func PathCreatePath(peerP []*PeerInfo) []Path { 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) + for i, msg := range []*bgp.BGPMessage{bgpMsgP1, bgpMsgP2, bgpMsgP3} { + updateMsgP := msg.Body.(*bgp.BGPUpdate) nlriList := updateMsgP.NLRI pathAttributes := updateMsgP.PathAttributes nlri_info := nlriList[0] - pathP[i], _ = CreatePath(msg.fromPeer, &nlri_info, pathAttributes, false, time.Now()) + pathP[i], _ = CreatePath(peerP[i], &nlri_info, pathAttributes, false, time.Now()) } return pathP } diff --git a/table/table_manager.go b/table/table_manager.go index d56c2e65..a027e4a4 100644 --- a/table/table_manager.go +++ b/table/table_manager.go @@ -22,48 +22,36 @@ import ( "time" ) -type ProcessMessage struct { - innerMessage *bgp.BGPMessage - fromPeer *PeerInfo -} - -func NewProcessMessage(m *bgp.BGPMessage, peerInfo *PeerInfo) *ProcessMessage { - return &ProcessMessage{ - innerMessage: m, - fromPeer: peerInfo, - } -} - -func (p *ProcessMessage) nlri2Path(now time.Time) []Path { - updateMsg := p.innerMessage.Body.(*bgp.BGPUpdate) +func nlri2Path(m *bgp.BGPMessage, p *PeerInfo, now time.Time) []Path { + updateMsg := m.Body.(*bgp.BGPUpdate) pathAttributes := updateMsg.PathAttributes pathList := make([]Path, 0) for _, nlri_info := range updateMsg.NLRI { // define local variable to pass nlri's address to CreatePath var nlri bgp.NLRInfo = nlri_info // create Path object - path, _ := CreatePath(p.fromPeer, &nlri, pathAttributes, false, now) + path, _ := CreatePath(p, &nlri, pathAttributes, false, now) pathList = append(pathList, path) } return pathList } -func (p *ProcessMessage) withdraw2Path(now time.Time) []Path { - updateMsg := p.innerMessage.Body.(*bgp.BGPUpdate) +func withdraw2Path(m *bgp.BGPMessage, p *PeerInfo, now time.Time) []Path { + updateMsg := m.Body.(*bgp.BGPUpdate) pathAttributes := updateMsg.PathAttributes pathList := make([]Path, 0) for _, nlriWithdraw := range updateMsg.WithdrawnRoutes { // define local variable to pass nlri's address to CreatePath var w bgp.WithdrawnRoute = nlriWithdraw // create withdrawn Path object - path, _ := CreatePath(p.fromPeer, &w, pathAttributes, true, now) + path, _ := CreatePath(p, &w, pathAttributes, true, now) pathList = append(pathList, path) } return pathList } -func (p *ProcessMessage) mpreachNlri2Path(now time.Time) []Path { - updateMsg := p.innerMessage.Body.(*bgp.BGPUpdate) +func mpreachNlri2Path(m *bgp.BGPMessage, p *PeerInfo, now time.Time) []Path { + updateMsg := m.Body.(*bgp.BGPUpdate) pathAttributes := updateMsg.PathAttributes attrList := []*bgp.PathAttributeMpReachNLRI{} @@ -79,15 +67,15 @@ func (p *ProcessMessage) mpreachNlri2Path(now time.Time) []Path { for _, mp := range attrList { nlri_info := mp.Value for _, nlri := range nlri_info { - path, _ := CreatePath(p.fromPeer, nlri, pathAttributes, false, now) + path, _ := CreatePath(p, nlri, pathAttributes, false, now) pathList = append(pathList, path) } } return pathList } -func (p *ProcessMessage) mpunreachNlri2Path(now time.Time) []Path { - updateMsg := p.innerMessage.Body.(*bgp.BGPUpdate) +func mpunreachNlri2Path(m *bgp.BGPMessage, p *PeerInfo, now time.Time) []Path { + updateMsg := m.Body.(*bgp.BGPUpdate) pathAttributes := updateMsg.PathAttributes attrList := []*bgp.PathAttributeMpUnreachNLRI{} @@ -104,20 +92,20 @@ func (p *ProcessMessage) mpunreachNlri2Path(now time.Time) []Path { nlri_info := mp.Value for _, nlri := range nlri_info { - path, _ := CreatePath(p.fromPeer, nlri, pathAttributes, true, now) + path, _ := CreatePath(p, nlri, pathAttributes, true, now) pathList = append(pathList, path) } } return pathList } -func (p *ProcessMessage) ToPathList() []Path { +func ProcessMessage(m *bgp.BGPMessage, peerInfo *PeerInfo) []Path { pathList := make([]Path, 0) now := time.Now() - pathList = append(pathList, p.nlri2Path(now)...) - pathList = append(pathList, p.withdraw2Path(now)...) - pathList = append(pathList, p.mpreachNlri2Path(now)...) - pathList = append(pathList, p.mpunreachNlri2Path(now)...) + pathList = append(pathList, nlri2Path(m, peerInfo, now)...) + pathList = append(pathList, withdraw2Path(m, peerInfo, now)...) + pathList = append(pathList, mpreachNlri2Path(m, peerInfo, now)...) + pathList = append(pathList, mpunreachNlri2Path(m, peerInfo, now)...) return pathList } @@ -289,12 +277,7 @@ func (manager *TableManager) ProcessUpdate(fromPeer *PeerInfo, message *bgp.BGPM return []Path{}, nil } - msg := &ProcessMessage{ - innerMessage: message, - fromPeer: fromPeer, - } - - return manager.ProcessPaths(msg.ToPathList()) + return manager.ProcessPaths(ProcessMessage(message, fromPeer)) } type AdjRib struct { diff --git a/table/table_manager_test.go b/table/table_manager_test.go index 91887cfb..d65db21f 100644 --- a/table/table_manager_test.go +++ b/table/table_manager_test.go @@ -2224,15 +2224,13 @@ func TestProcessBGPUpdate_Timestamp(t *testing.T) { adjRib := NewAdjRib([]bgp.RouteFamily{bgp.RF_IPv4_UC, bgp.RF_IPv6_UC}) m1 := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri) peer := peerR1() - msg1 := NewProcessMessage(m1, peer) - pList1 := msg1.ToPathList() + pList1 := ProcessMessage(m1, peer) path1 := pList1[0].(*IPv4Path) t1 := path1.timestamp adjRib.UpdateIn(pList1) m2 := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri) - msg2 := NewProcessMessage(m2, peer) - pList2 := msg2.ToPathList() + pList2 := ProcessMessage(m2, peer) //path2 := pList2[0].(*IPv4Path) //t2 = path2.timestamp adjRib.UpdateIn(pList2) @@ -2250,8 +2248,7 @@ func TestProcessBGPUpdate_Timestamp(t *testing.T) { } m3 := bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes2, nlri) - msg3 := NewProcessMessage(m3, peer) - pList3 := msg3.ToPathList() + pList3 := ProcessMessage(m3, peer) t3 := pList3[0].getTimestamp() adjRib.UpdateIn(pList3) diff --git a/table/table_test.go b/table/table_test.go index 70e9c86b..fe15204c 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -38,8 +38,7 @@ func TestTableTableKeyDefault(t *testing.T) { func TestTableDeleteDestByNlri(t *testing.T) { peerT := TableCreatePeer() - msgT := TableCreateMSG(peerT) - pathT := TableCreatePath(msgT) + pathT := TableCreatePath(peerT) ipv4t := NewIPv4Table(0) for _, path := range pathT { tableKey := ipv4t.tableKey(path.GetNlri()) @@ -54,8 +53,7 @@ func TestTableDeleteDestByNlri(t *testing.T) { func TestTableDeleteDest(t *testing.T) { peerT := TableCreatePeer() - msgT := TableCreateMSG(peerT) - pathT := TableCreatePath(msgT) + pathT := TableCreatePath(peerT) ipv4t := NewIPv4Table(0) for _, path := range pathT { tableKey := ipv4t.tableKey(path.GetNlri()) @@ -78,8 +76,7 @@ func TestTableGetRouteFamily(t *testing.T) { func TestTableSetDestinations(t *testing.T) { peerT := TableCreatePeer() - msgT := TableCreateMSG(peerT) - pathT := TableCreatePath(msgT) + pathT := TableCreatePath(peerT) ipv4t := NewIPv4Table(0) destinations := make(map[string]Destination) for _, path := range pathT { @@ -93,8 +90,7 @@ func TestTableSetDestinations(t *testing.T) { } func TestTableGetDestinations(t *testing.T) { peerT := DestCreatePeer() - msgT := DestCreateMSG(peerT) - pathT := DestCreatePath(msgT) + pathT := DestCreatePath(peerT) ipv4t := NewIPv4Table(0) destinations := make(map[string]Destination) for _, path := range pathT { @@ -114,24 +110,18 @@ func TableCreatePeer() []*PeerInfo { peerT := []*PeerInfo{peerT1, peerT2, peerT3} return peerT } -func TableCreateMSG(peerT []*PeerInfo) []*ProcessMessage { + +func TableCreatePath(peerT []*PeerInfo) []Path { bgpMsgT1 := updateMsgT1() bgpMsgT2 := updateMsgT2() bgpMsgT3 := updateMsgT3() - msgT1 := &ProcessMessage{innerMessage: bgpMsgT1, fromPeer: peerT[0]} - msgT2 := &ProcessMessage{innerMessage: bgpMsgT2, fromPeer: peerT[1]} - msgT3 := &ProcessMessage{innerMessage: bgpMsgT3, fromPeer: peerT[2]} - msgT := []*ProcessMessage{msgT1, msgT2, msgT3} - return msgT -} -func TableCreatePath(msgs []*ProcessMessage) []Path { pathT := make([]Path, 3) - for i, msg := range msgs { - updateMsgT := msg.innerMessage.Body.(*bgp.BGPUpdate) + for i, msg := range []*bgp.BGPMessage{bgpMsgT1, bgpMsgT2, bgpMsgT3} { + updateMsgT := msg.Body.(*bgp.BGPUpdate) nlriList := updateMsgT.NLRI pathAttributes := updateMsgT.PathAttributes nlri_info := nlriList[0] - pathT[i], _ = CreatePath(msg.fromPeer, &nlri_info, pathAttributes, false, time.Now()) + pathT[i], _ = CreatePath(peerT[i], &nlri_info, pathAttributes, false, time.Now()) } return pathT } |