diff options
-rw-r--r-- | table/destination.go | 20 | ||||
-rw-r--r-- | table/table.go | 11 | ||||
-rw-r--r-- | table/table_manager.go | 60 | ||||
-rw-r--r-- | table/table_manager_test.go | 1236 |
4 files changed, 1068 insertions, 259 deletions
diff --git a/table/destination.go b/table/destination.go index 7857c0fc..6bee6df3 100644 --- a/table/destination.go +++ b/table/destination.go @@ -48,6 +48,8 @@ type Destination interface { setBestPathReason(string) getBestPath() Path setBestPath(path Path) + getOldBestPath() Path + setOldBestPath(path Path) getKnownPathList() []Path String() string addWithdraw(withdraw Path) @@ -67,6 +69,7 @@ type DestinationDefault struct { newPathList []Path bestPath Path bestPathReason string + oldBestPath Path sentRoutes map[*Peer]*SentRoute } @@ -79,6 +82,7 @@ func NewDestinationDefault(nlri bgp.AddrPrefixInterface) *DestinationDefault { destination.newPathList = make([]Path, 0) destination.bestPath = nil destination.bestPathReason = "" + destination.oldBestPath = nil destination.sentRoutes = make(map[*Peer]*SentRoute) return destination } @@ -115,6 +119,14 @@ func (dd *DestinationDefault) setBestPath(path Path) { dd.bestPath = path } +func (dd *DestinationDefault) getOldBestPath() Path { + return dd.oldBestPath +} + +func (dd *DestinationDefault) setOldBestPath(path Path) { + dd.oldBestPath = path +} + func (dd *DestinationDefault) getKnownPathList() []Path { return dd.knownPathList } @@ -245,14 +257,12 @@ func (dest *DestinationDefault) removeWithdrawls() { matches := make(map[string]Path) wMatches := make(map[string]Path) // Match all withdrawals from destination paths. - for _, wItem := range dest.withdrawList { - withdraw := wItem.(*PathDefault) + for _, withdraw := range dest.withdrawList { var isFound bool = false - for _, item := range dest.knownPathList { - path := item.(*PathDefault) + for _, path := range dest.knownPathList { // We have a match if the source are same. // TODO add GetSource to Path interface - if path.source == withdraw.source { + if path.getSource() == withdraw.getSource() { isFound = true matches[path.String()] = path wMatches[withdraw.String()] = withdraw diff --git a/table/table.go b/table/table.go index 99c4de1f..c3339a9f 100644 --- a/table/table.go +++ b/table/table.go @@ -81,18 +81,17 @@ func insert(table Table, path Path) Destination { return dest } func insertSentRoute(table Table, sentRoute *SentRoute) { - pd := sentRoute.path.(*PathDefault) + pd := sentRoute.path table.validatePath(pd) dest := getOrCreateDest(table, pd.getNlri()) - dest.(*DestinationDefault).addSentRoute(sentRoute) + dest.addSentRoute(sentRoute) } //"Remove old paths from whose source is `peer` func (td *TableDefault) cleanupPathsForPeer(peer *Peer) { for _, dest := range td.destinations { - dd := dest.(*DestinationDefault) - pathsDeleted := dd.removeOldPathsFromSource(peer) - hadSent := dd.removeSentRoute(peer) + pathsDeleted := dest.removeOldPathsFromSource(peer) + hadSent := dest.removeSentRoute(peer) if hadSent { logger.Errorf("Cleaning paths from table %s for peer %s.", td, peer) } @@ -119,7 +118,7 @@ func (td *TableDefault) cleanUninterestingPaths(interested_rts) int { } */ -func deleteDestByNlri(table Table, nlri *bgp.NLRInfo) Destination { +func deleteDestByNlri(table Table, nlri bgp.AddrPrefixInterface) Destination { table.validateNlri(nlri) destinations := table.getDestinations() dest := destinations[table.tableKey(nlri).String()] diff --git a/table/table_manager.go b/table/table_manager.go index 448776a2..081b19c4 100644 --- a/table/table_manager.go +++ b/table/table_manager.go @@ -20,7 +20,6 @@ import ( "github.com/osrg/gobgp/packet" "net" "os" - "reflect" "time" ) @@ -135,11 +134,10 @@ func (attr AttributeType) String() string { } type TableManager struct { - Tables map[RouteFamily]Table - adjInLocalRib map[string]*ReceivedRoute - processMessages chan *ProcessMessage - Counter map[PeerCounterName]int - localAsn uint32 + Tables map[RouteFamily]Table + adjInLocalRib map[string]*ReceivedRoute + Counter map[PeerCounterName]int + localAsn uint32 } type ProcessMessage struct { @@ -152,8 +150,6 @@ func NewTableManager() *TableManager { t.Tables = make(map[RouteFamily]Table) t.Tables[RF_IPv4_UC] = NewIPv4Table(0, nil) t.Tables[RF_IPv6_UC] = NewIPv6Table(0, nil) - - t.processMessages = make(chan *ProcessMessage, 10) // initialize prefix counter t.Counter = make(map[PeerCounterName]int) t.Counter[RECV_PREFIXES] = 0 @@ -175,19 +171,21 @@ func (manager *TableManager) incrCounter(name PeerCounterName, step int) { func (manager *TableManager) handleNlri(p *ProcessMessage) ([]Destination, error) { updateMsg := p.innerMessage.Body.(*bgp.BGPUpdate) - nlriList := updateMsg.NLRI + nlriList := updateMsg.NLRI // NLRI is an array of NLRInfo. pathAttributes := updateMsg.PathAttributes destList := make([]Destination, 0) for _, nlri_info := range nlriList { + + // define local variable to pass nlri's address to CreatePath + var nlri bgp.NLRInfo = nlri_info // create Path object - path := CreatePath(p.fromPeer, &nlri_info, pathAttributes, false) + path := CreatePath(p.fromPeer, &nlri, pathAttributes, false) // TODO process filter rf := path.getRouteFamily() // push Path into table destination := insert(manager.Tables[rf], path) - destList = append(destList, destination) manager.incrCounter(RECV_PREFIXES, len(nlriList)) // TODO handle adj-in-loc-rib @@ -196,7 +194,7 @@ func (manager *TableManager) handleNlri(p *ProcessMessage) ([]Destination, error // manager.adjInChanged <- rr } - logger.Debugf("destinationList contains %d destinations from nlri", len(destList)) + logger.Debugf("destinationList contains %d destinations from nlri_info", len(destList)) return destList, nil } @@ -212,8 +210,10 @@ func (manager *TableManager) handleWithdraw(p *ProcessMessage) ([]Destination, e // process withdraw path for _, nlriWithdraw := range withdrawnRoutes { + // define local variable to pass nlri's address to CreatePath + var w bgp.WithdrawnRoute = nlriWithdraw // create withdrawn Path object - path := CreatePath(p.fromPeer, &nlriWithdraw, pathAttributes, true) + path := CreatePath(p.fromPeer, &w, pathAttributes, true) rf := path.getRouteFamily() // push Path into table destination := insert(manager.Tables[rf], path) @@ -231,11 +231,12 @@ func (manager *TableManager) handleMPReachNlri(p *ProcessMessage) ([]Destination pathAttributes := updateMsg.PathAttributes attrList := []*bgp.PathAttributeMpReachNLRI{} +LOOP: for _, attr := range pathAttributes { - logger.Debugf("attr type: %s", reflect.TypeOf(attr)) switch a := attr.(type) { case *bgp.PathAttributeMpReachNLRI: attrList = append(attrList, a) + break LOOP } } @@ -271,10 +272,12 @@ func (manager *TableManager) handleMPUNReachNlri(p *ProcessMessage) ([]Destinati pathAttributes := updateMsg.PathAttributes attrList := []*bgp.PathAttributeMpUnreachNLRI{} +LOOP: for _, attr := range pathAttributes { switch a := attr.(type) { case *bgp.PathAttributeMpUnreachNLRI: attrList = append(attrList, a) + break LOOP } } @@ -300,10 +303,10 @@ func (manager *TableManager) handleMPUNReachNlri(p *ProcessMessage) ([]Destinati // process BGPUpdate message // this function processes only BGPUpdate -func (manager *TableManager) ProcessUpdate(fromPeer *Peer, message *bgp.BGPMessage) ([]Path, []Path, error) { +func (manager *TableManager) ProcessUpdate(fromPeer *Peer, message *bgp.BGPMessage) ([]Path, []Destination, error) { var bestPaths []Path = make([]Path, 0) - var withdrawPaths []Path = make([]Path, 0) + var lostDest []Destination = make([]Destination, 0) // check msg's type if it's BGPUpdate body := message.Body @@ -348,12 +351,10 @@ func (manager *TableManager) ProcessUpdate(fromPeer *Peer, message *bgp.BGPMessa if destinationList != nil { for _, destination := range destinationList { // compute best path - logger.Infof("Processing destination: %s", destination.String()) + logger.Infof("Processing destination: %v", destination.String()) newBestPath, reason, err := destination.Calculate(manager.localAsn) - logger.Debugf("new best path: %s, reason=%s", newBestPath, reason) - logger.Infof("new best path: NLRI: %s, next_hop=%s, reason=%s", newBestPath.getPrefix().String(), - newBestPath.getNexthop().String(), reason) + logger.Debugf("new best path: %v, reason=%v", newBestPath, reason) if err != nil { logger.Error(err) continue @@ -369,20 +370,24 @@ func (manager *TableManager) ProcessUpdate(fromPeer *Peer, message *bgp.BGPMessa } if newBestPath == nil { - - logger.Debug("new best path is nil") - + logger.Debug("best path is nil") if len(destination.getKnownPathList()) == 0 { // create withdraw path if currentBestPath != nil { - withdrawPaths = append(withdrawPaths, currentBestPath) + logger.Debug("best path is lost") + destination.setOldBestPath(destination.getBestPath()) + lostDest = append(lostDest, destination) } destination.setBestPath(nil) } else { - panic("known path list is not empty") + logger.Error("known path list is not empty") } } else { - logger.Debug("best path : ", newBestPath.String()) + logger.Debugf("new best path: NLRI: %v, next_hop=%v, reason=%v", + newBestPath.getPrefix().String(), + newBestPath.getNexthop().String(), + reason) + bestPaths = append(bestPaths, newBestPath) destination.setBestPath(newBestPath) } @@ -391,6 +396,7 @@ func (manager *TableManager) ProcessUpdate(fromPeer *Peer, message *bgp.BGPMessa rf := destination.getRouteFamily() t := manager.Tables[rf] deleteDest(t, destination) + logger.Debugf("destination removed route_family=%v, destination=%v", rf, destination) } } } @@ -398,7 +404,7 @@ func (manager *TableManager) ProcessUpdate(fromPeer *Peer, message *bgp.BGPMessa logger.Warn("message is not BGPUpdate") } - return bestPaths, withdrawPaths, nil + return bestPaths, lostDest, nil } type ReceivedRoute struct { diff --git a/table/table_manager_test.go b/table/table_manager_test.go index 8b1d7e2c..20d056b7 100644 --- a/table/table_manager_test.go +++ b/table/table_manager_test.go @@ -20,18 +20,17 @@ import ( log "github.com/Sirupsen/logrus" "github.com/osrg/gobgp/packet" "github.com/stretchr/testify/assert" - "net" "os" "reflect" "testing" ) -func getLogger() *log.Logger { +func getLogger(lv log.Level) *log.Logger { var l *log.Logger = &log.Logger{ Out: os.Stderr, Formatter: new(log.JSONFormatter), Hooks: make(map[log.Level][]log.Hook), - Level: log.InfoLevel, + Level: lv, } return l } @@ -74,47 +73,46 @@ func peerR3() *Peer { func TestProcessBGPUpdate_0_select_onlypath_ipv4(t *testing.T) { tm := NewTableManager() - setLogger(getLogger()) bgpMessage := update_fromR1() peer := peerR1() pList, wList, err := tm.ProcessUpdate(peer, bgpMessage) - assert.Equal(t, len(pList), 1, "pList length should be 1") - assert.Equal(t, len(wList), 0, "wList length should be 0") - assert.NoError(t, err, "err should be nil") + assert.Equal(t, len(pList), 1) + assert.Equal(t, len(wList), 0) + assert.NoError(t, err) // check type path := pList[0] expectedType := "*table.IPv4Path" - assert.Equal(t, reflect.TypeOf(path).String(), expectedType, "best path should be *table.IPv4Path") + assert.Equal(t, reflect.TypeOf(path).String(), expectedType) // check PathAttribute pathAttributes := bgpMessage.Body.(*bgp.BGPUpdate).PathAttributes expectedOrigin := pathAttributes[0] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin, "PathAttributeOrigin should be ", expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[1] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath, "PathAttributeAsPath should be ", expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedNexthopAttr := pathAttributes[2] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP).(*bgp.PathAttributeNextHop) - assert.Equal(t, pathNexthop, expectedNexthopAttr, "PathAttributeNextHop should be ", expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) - assert.Equal(t, expectedMed, pathMed, "PathAttributeMed should be ", expectedMed) + assert.Equal(t, expectedMed, pathMed) // check PathAttribute length - assert.Equal(t, 4, path.getPathAttributeMap().Len(), "PathAttribute length should be ", 4) + assert.Equal(t, 4, path.getPathAttributeMap().Len()) // check destination expectedPrefix := "10.10.10.0" - assert.Equal(t, expectedPrefix, path.getPrefix().String(), "prefix should be ", expectedPrefix) + assert.Equal(t, expectedPrefix, path.getPrefix().String()) // check nexthop expectedNexthop := "192.168.50.1" - assert.Equal(t, expectedNexthop, path.getNexthop().String(), "nexthop should be ", expectedNexthop) + assert.Equal(t, expectedNexthop, path.getNexthop().String()) } @@ -122,7 +120,6 @@ func TestProcessBGPUpdate_0_select_onlypath_ipv4(t *testing.T) { func TestProcessBGPUpdate_0_select_onlypath_ipv6(t *testing.T) { tm := NewTableManager() - setLogger(getLogger()) bgpMessage := update_fromR1_ipv6() peer := peerR1() @@ -134,36 +131,36 @@ func TestProcessBGPUpdate_0_select_onlypath_ipv6(t *testing.T) { // check type path := pList[0] expectedType := "*table.IPv6Path" - assert.Equal(t, expectedType, reflect.TypeOf(path).String(), "best path should be *table.IPv6Path") + assert.Equal(t, expectedType, reflect.TypeOf(path).String()) // check PathAttribute pathAttributes := bgpMessage.Body.(*bgp.BGPUpdate).PathAttributes expectedNexthopAttr := pathAttributes[0] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) - assert.Equal(t, pathNexthop, expectedNexthopAttr, "PathAttributeNextHop should be ", expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedOrigin := pathAttributes[1] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin, "PathAttributeOrigin should be ", expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[2] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath, "PathAttributeAsPath should be ", expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) - assert.Equal(t, expectedMed, pathMed, "PathAttributeMed should be ", expectedMed) + assert.Equal(t, expectedMed, pathMed) // check PathAttribute length - assert.Equal(t, 4, path.getPathAttributeMap().Len(), "PathAttribute length should be ", 4) + assert.Equal(t, 4, path.getPathAttributeMap().Len()) // check destination expectedPrefix := "2001:123:123:1::" - assert.Equal(t, expectedPrefix, path.getPrefix().String(), "prefix should be ", expectedPrefix) + assert.Equal(t, expectedPrefix, path.getPrefix().String()) // check nexthop expectedNexthop := "2001::192:168:50:1" - assert.Equal(t, expectedNexthop, path.getNexthop().String(), "nexthop should be ", expectedNexthop) + assert.Equal(t, expectedNexthop, path.getNexthop().String()) } @@ -171,7 +168,8 @@ func TestProcessBGPUpdate_0_select_onlypath_ipv6(t *testing.T) { func TestProcessBGPUpdate_1_select_high_localpref_ipv4(t *testing.T) { tm := NewTableManager() - var pList, wList []Path + var pList []Path + var wList []Destination var err error // low localpref message @@ -204,60 +202,62 @@ func TestProcessBGPUpdate_1_select_high_localpref_ipv4(t *testing.T) { peer1 := peerR1() pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) - assert.Equal(t, 1, len(pList), "pList length should be 1") - assert.Equal(t, 0, len(wList), "wList length should be 0") - assert.NoError(t, err, "err should be nil") + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) peer2 := peerR2() pList, wList, err = tm.ProcessUpdate(peer2, bgpMessage2) - assert.Equal(t, 1, len(pList), "pList length should be 1") - assert.Equal(t, 0, len(wList), "wList length should be 0") - assert.NoError(t, err, "err should be nil") + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) // check type path := pList[0] expectedType := "*table.IPv4Path" - assert.Equal(t, reflect.TypeOf(path).String(), expectedType, "best path should be *table.IPv4Path") + assert.Equal(t, reflect.TypeOf(path).String(), expectedType) // check PathAttribute pathAttributes := bgpMessage2.Body.(*bgp.BGPUpdate).PathAttributes expectedOrigin := pathAttributes[0] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin, "PathAttributeOrigin should be ", expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[1] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath, "PathAttributeAsPath should be ", expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedNexthopAttr := pathAttributes[2] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP).(*bgp.PathAttributeNextHop) - assert.Equal(t, pathNexthop, expectedNexthopAttr, "PathAttributeNextHop should be ", expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) - assert.Equal(t, expectedMed, pathMed, "PathAttributeMed should be ", expectedMed) + assert.Equal(t, expectedMed, pathMed) // check PathAttribute length - assert.Equal(t, len(pathAttributes2), path.getPathAttributeMap().Len(), "PathAttribute length should be ", 4) + assert.Equal(t, len(pathAttributes2), path.getPathAttributeMap().Len()) // check destination expectedPrefix := "10.10.10.0" - assert.Equal(t, expectedPrefix, path.getPrefix().String(), "prefix should be ", expectedPrefix) + assert.Equal(t, expectedPrefix, path.getPrefix().String()) // check nexthop expectedNexthop := "192.168.50.1" - assert.Equal(t, expectedNexthop, path.getNexthop().String(), "nexthop should be ", expectedNexthop) + assert.Equal(t, expectedNexthop, path.getNexthop().String()) } func TestProcessBGPUpdate_1_select_high_localpref_ipv6(t *testing.T) { tm := NewTableManager() - var pList, wList []Path + var pList []Path + var wList []Destination var err error origin1 := bgp.NewPathAttributeOrigin(0) aspath1 := createAsPathAttribute([]uint16{65000}) - mp_reach1 := createMpReach("2001::192:168:50:1", "2001:123:123:1::", 64) + mp_reach1 := createMpReach("2001::192:168:50:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) med1 := bgp.NewPathAttributeMultiExitDisc(100) localpref1 := bgp.NewPathAttributeLocalPref(100) @@ -271,7 +271,8 @@ func TestProcessBGPUpdate_1_select_high_localpref_ipv6(t *testing.T) { origin2 := bgp.NewPathAttributeOrigin(0) aspath2 := createAsPathAttribute([]uint16{65100, 65000}) - mp_reach2 := createMpReach("2001::192:168:100:1", "2001:123:123:1::", 64) + mp_reach2 := createMpReach("2001::192:168:100:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) med2 := bgp.NewPathAttributeMultiExitDisc(100) localpref2 := bgp.NewPathAttributeLocalPref(200) @@ -287,13 +288,13 @@ func TestProcessBGPUpdate_1_select_high_localpref_ipv6(t *testing.T) { pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) peer2 := peerR2() pList, wList, err = tm.ProcessUpdate(peer2, bgpMessage2) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) // check type path := pList[0] @@ -305,15 +306,15 @@ func TestProcessBGPUpdate_1_select_high_localpref_ipv6(t *testing.T) { expectedNexthopAttr := pathAttributes[0] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) - assert.Equal(t, pathNexthop, expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedOrigin := pathAttributes[1] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[2] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) @@ -335,7 +336,8 @@ func TestProcessBGPUpdate_1_select_high_localpref_ipv6(t *testing.T) { func TestProcessBGPUpdate_2_select_local_origin_ipv4(t *testing.T) { tm := NewTableManager() - var pList, wList []Path + var pList []Path + var wList []Destination var err error // low localpref message @@ -387,15 +389,15 @@ func TestProcessBGPUpdate_2_select_local_origin_ipv4(t *testing.T) { pathAttributes := bgpMessage2.Body.(*bgp.BGPUpdate).PathAttributes expectedOrigin := pathAttributes[0] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[1] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedNexthopAttr := pathAttributes[2] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP).(*bgp.PathAttributeNextHop) - assert.Equal(t, pathNexthop, expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) @@ -416,12 +418,14 @@ func TestProcessBGPUpdate_2_select_local_origin_ipv4(t *testing.T) { func TestProcessBGPUpdate_2_select_local_origin_ipv6(t *testing.T) { tm := NewTableManager() - var pList, wList []Path + var pList []Path + var wList []Destination var err error origin1 := bgp.NewPathAttributeOrigin(0) aspath1 := createAsPathAttribute([]uint16{65000}) - mp_reach1 := createMpReach("2001::192:168:50:1", "2001:123:123:1::", 64) + mp_reach1 := createMpReach("2001::192:168:50:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) med1 := bgp.NewPathAttributeMultiExitDisc(100) localpref1 := bgp.NewPathAttributeLocalPref(100) @@ -435,7 +439,8 @@ func TestProcessBGPUpdate_2_select_local_origin_ipv6(t *testing.T) { origin2 := bgp.NewPathAttributeOrigin(0) aspath2 := createAsPathAttribute([]uint16{}) - mp_reach2 := createMpReach("::", "2001:123:123:1::", 64) + mp_reach2 := createMpReach("::", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) med2 := bgp.NewPathAttributeMultiExitDisc(100) localpref2 := bgp.NewPathAttributeLocalPref(100) @@ -451,13 +456,13 @@ func TestProcessBGPUpdate_2_select_local_origin_ipv6(t *testing.T) { pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) var peer2 *Peer = nil pList, wList, err = tm.ProcessUpdate(peer2, bgpMessage2) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) // check type path := pList[0] @@ -469,15 +474,15 @@ func TestProcessBGPUpdate_2_select_local_origin_ipv6(t *testing.T) { expectedNexthopAttr := pathAttributes[0] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) - assert.Equal(t, pathNexthop, expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedOrigin := pathAttributes[1] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[2] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) @@ -499,61 +504,63 @@ func TestProcessBGPUpdate_2_select_local_origin_ipv6(t *testing.T) { func TestProcessBGPUpdate_3_select_aspath_ipv4(t *testing.T) { tm := NewTableManager() - var pList, wList []Path + var pList []Path + var wList []Destination var err error bgpMessage1 := update_fromR2viaR1() peer1 := peerR1() pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) - assert.Equal(t, 1, len(pList), "pList length should be 1") - assert.Equal(t, 0, len(wList), "wList length should be 0") - assert.NoError(t, err, "err should be nil") + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) bgpMessage2 := update_fromR2() peer2 := peerR2() pList, wList, err = tm.ProcessUpdate(peer2, bgpMessage2) - assert.Equal(t, 1, len(pList), "pList length should be 1") - assert.Equal(t, 0, len(wList), "wList length should be 0") - assert.NoError(t, err, "err should be nil") + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) // check type path := pList[0] expectedType := "*table.IPv4Path" - assert.Equal(t, reflect.TypeOf(path).String(), expectedType, "best path should be *table.IPv4Path") + assert.Equal(t, reflect.TypeOf(path).String(), expectedType) // check PathAttribute pathAttributes := bgpMessage2.Body.(*bgp.BGPUpdate).PathAttributes expectedOrigin := pathAttributes[0] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin, "PathAttributeOrigin should be ", expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[1] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath, "PathAttributeAsPath should be ", expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedNexthopAttr := pathAttributes[2] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP).(*bgp.PathAttributeNextHop) - assert.Equal(t, pathNexthop, expectedNexthopAttr, "PathAttributeNextHop should be ", expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) - assert.Equal(t, expectedMed, pathMed, "PathAttributeMed should be ", expectedMed) + assert.Equal(t, expectedMed, pathMed) // check PathAttribute length - assert.Equal(t, 4, path.getPathAttributeMap().Len(), "PathAttribute length should be ", 4) + assert.Equal(t, 4, path.getPathAttributeMap().Len()) // check destination expectedPrefix := "20.20.20.0" - assert.Equal(t, expectedPrefix, path.getPrefix().String(), "prefix should be ", expectedPrefix) + assert.Equal(t, expectedPrefix, path.getPrefix().String()) // check nexthop expectedNexthop := "192.168.100.1" - assert.Equal(t, expectedNexthop, path.getNexthop().String(), "nexthop should be ", expectedNexthop) + assert.Equal(t, expectedNexthop, path.getNexthop().String()) } func TestProcessBGPUpdate_3_select_aspath_ipv6(t *testing.T) { tm := NewTableManager() - var pList, wList []Path + var pList []Path + var wList []Destination var err error bgpMessage1 := update_fromR2viaR1_ipv6() @@ -561,47 +568,47 @@ func TestProcessBGPUpdate_3_select_aspath_ipv6(t *testing.T) { pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) bgpMessage2 := update_fromR2_ipv6() peer2 := peerR2() pList, wList, err = tm.ProcessUpdate(peer2, bgpMessage2) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) // check type path := pList[0] expectedType := "*table.IPv6Path" - assert.Equal(t, reflect.TypeOf(path).String(), expectedType, "best path should be *table.IPv6Path") + assert.Equal(t, expectedType, reflect.TypeOf(path).String()) // check PathAttribute pathAttributes := bgpMessage2.Body.(*bgp.BGPUpdate).PathAttributes expectedNexthopAttr := pathAttributes[0] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) - assert.Equal(t, pathNexthop, expectedNexthopAttr, "PathAttributeNextHop should be ", expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedOrigin := pathAttributes[1] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin, "PathAttributeOrigin should be ", expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[2] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath, "PathAttributeAsPath should be ", expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) - assert.Equal(t, expectedMed, pathMed, "PathAttributeMed should be ", expectedMed) + assert.Equal(t, expectedMed, pathMed) // check PathAttribute length - assert.Equal(t, 4, path.getPathAttributeMap().Len(), "PathAttribute length should be ", 4) + assert.Equal(t, 4, path.getPathAttributeMap().Len()) // check destination expectedPrefix := "2002:223:123:1::" - assert.Equal(t, expectedPrefix, path.getPrefix().String(), "prefix should be ", expectedPrefix) + assert.Equal(t, expectedPrefix, path.getPrefix().String()) // check nexthop expectedNexthop := "2001::192:168:100:1" - assert.Equal(t, expectedNexthop, path.getNexthop().String(), "nexthop should be ", expectedNexthop) + assert.Equal(t, expectedNexthop, path.getNexthop().String()) } @@ -609,7 +616,8 @@ func TestProcessBGPUpdate_3_select_aspath_ipv6(t *testing.T) { func TestProcessBGPUpdate_4_select_low_origin_ipv4(t *testing.T) { tm := NewTableManager() - var pList, wList []Path + var pList []Path + var wList []Destination var err error // low origin message @@ -661,15 +669,15 @@ func TestProcessBGPUpdate_4_select_low_origin_ipv4(t *testing.T) { pathAttributes := bgpMessage2.Body.(*bgp.BGPUpdate).PathAttributes expectedOrigin := pathAttributes[0] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[1] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedNexthopAttr := pathAttributes[2] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP).(*bgp.PathAttributeNextHop) - assert.Equal(t, pathNexthop, expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) @@ -690,12 +698,14 @@ func TestProcessBGPUpdate_4_select_low_origin_ipv4(t *testing.T) { func TestProcessBGPUpdate_4_select_low_origin_ipv6(t *testing.T) { tm := NewTableManager() - var pList, wList []Path + var pList []Path + var wList []Destination var err error origin1 := bgp.NewPathAttributeOrigin(1) aspath1 := createAsPathAttribute([]uint16{65200, 65000}) - mp_reach1 := createMpReach("2001::192:168:50:1", "2001:123:123:1::", 64) + mp_reach1 := createMpReach("2001::192:168:50:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) med1 := bgp.NewPathAttributeMultiExitDisc(100) localpref1 := bgp.NewPathAttributeLocalPref(100) @@ -709,7 +719,8 @@ func TestProcessBGPUpdate_4_select_low_origin_ipv6(t *testing.T) { origin2 := bgp.NewPathAttributeOrigin(0) aspath2 := createAsPathAttribute([]uint16{65100, 65000}) - mp_reach2 := createMpReach("2001::192:168:100:1", "2001:123:123:1::", 64) + mp_reach2 := createMpReach("2001::192:168:100:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) med2 := bgp.NewPathAttributeMultiExitDisc(100) localpref2 := bgp.NewPathAttributeLocalPref(200) @@ -725,13 +736,13 @@ func TestProcessBGPUpdate_4_select_low_origin_ipv6(t *testing.T) { pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) peer2 := peerR2() pList, wList, err = tm.ProcessUpdate(peer2, bgpMessage2) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) // check type path := pList[0] @@ -743,15 +754,15 @@ func TestProcessBGPUpdate_4_select_low_origin_ipv6(t *testing.T) { expectedNexthopAttr := pathAttributes[0] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) - assert.Equal(t, pathNexthop, expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedOrigin := pathAttributes[1] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[2] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) @@ -773,7 +784,8 @@ func TestProcessBGPUpdate_4_select_low_origin_ipv6(t *testing.T) { func TestProcessBGPUpdate_5_select_low_med_ipv4(t *testing.T) { tm := NewTableManager() - var pList, wList []Path + var pList []Path + var wList []Destination var err error // low origin message @@ -825,15 +837,15 @@ func TestProcessBGPUpdate_5_select_low_med_ipv4(t *testing.T) { pathAttributes := bgpMessage2.Body.(*bgp.BGPUpdate).PathAttributes expectedOrigin := pathAttributes[0] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[1] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedNexthopAttr := pathAttributes[2] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP).(*bgp.PathAttributeNextHop) - assert.Equal(t, pathNexthop, expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) @@ -854,12 +866,14 @@ func TestProcessBGPUpdate_5_select_low_med_ipv4(t *testing.T) { func TestProcessBGPUpdate_5_select_low_med_ipv6(t *testing.T) { tm := NewTableManager() - var pList, wList []Path + var pList []Path + var wList []Destination var err error origin1 := bgp.NewPathAttributeOrigin(0) aspath1 := createAsPathAttribute([]uint16{65200, 65000}) - mp_reach1 := createMpReach("2001::192:168:50:1", "2001:123:123:1::", 64) + mp_reach1 := createMpReach("2001::192:168:50:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) med1 := bgp.NewPathAttributeMultiExitDisc(500) localpref1 := bgp.NewPathAttributeLocalPref(100) @@ -873,7 +887,8 @@ func TestProcessBGPUpdate_5_select_low_med_ipv6(t *testing.T) { origin2 := bgp.NewPathAttributeOrigin(0) aspath2 := createAsPathAttribute([]uint16{65100, 65000}) - mp_reach2 := createMpReach("2001::192:168:100:1", "2001:123:123:1::", 64) + mp_reach2 := createMpReach("2001::192:168:100:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) med2 := bgp.NewPathAttributeMultiExitDisc(200) localpref2 := bgp.NewPathAttributeLocalPref(100) @@ -889,13 +904,13 @@ func TestProcessBGPUpdate_5_select_low_med_ipv6(t *testing.T) { pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) peer2 := peerR2() pList, wList, err = tm.ProcessUpdate(peer2, bgpMessage2) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) // check type path := pList[0] @@ -907,15 +922,15 @@ func TestProcessBGPUpdate_5_select_low_med_ipv6(t *testing.T) { expectedNexthopAttr := pathAttributes[0] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) - assert.Equal(t, pathNexthop, expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedOrigin := pathAttributes[1] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[2] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) @@ -939,7 +954,8 @@ func TestProcessBGPUpdate_6_select_ebgp_path_ipv4(t *testing.T) { tm := NewTableManager() tm.localAsn = uint32(65000) - var pList, wList []Path + var pList []Path + var wList []Destination var err error // low origin message @@ -991,15 +1007,15 @@ func TestProcessBGPUpdate_6_select_ebgp_path_ipv4(t *testing.T) { pathAttributes := bgpMessage2.Body.(*bgp.BGPUpdate).PathAttributes expectedOrigin := pathAttributes[0] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[1] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedNexthopAttr := pathAttributes[2] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP).(*bgp.PathAttributeNextHop) - assert.Equal(t, pathNexthop, expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) @@ -1021,12 +1037,14 @@ func TestProcessBGPUpdate_6_select_ebgp_path_ipv6(t *testing.T) { tm := NewTableManager() tm.localAsn = uint32(65000) - var pList, wList []Path + var pList []Path + var wList []Destination var err error origin1 := bgp.NewPathAttributeOrigin(0) aspath1 := createAsPathAttribute([]uint16{65000, 65200}) - mp_reach1 := createMpReach("2001::192:168:50:1", "2001:123:123:1::", 64) + mp_reach1 := createMpReach("2001::192:168:50:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) med1 := bgp.NewPathAttributeMultiExitDisc(200) localpref1 := bgp.NewPathAttributeLocalPref(100) @@ -1040,7 +1058,8 @@ func TestProcessBGPUpdate_6_select_ebgp_path_ipv6(t *testing.T) { origin2 := bgp.NewPathAttributeOrigin(0) aspath2 := createAsPathAttribute([]uint16{65100, 65200}) - mp_reach2 := createMpReach("2001::192:168:100:1", "2001:123:123:1::", 64) + mp_reach2 := createMpReach("2001::192:168:100:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) med2 := bgp.NewPathAttributeMultiExitDisc(200) localpref2 := bgp.NewPathAttributeLocalPref(100) @@ -1056,13 +1075,13 @@ func TestProcessBGPUpdate_6_select_ebgp_path_ipv6(t *testing.T) { pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) peer2 := peerR2() pList, wList, err = tm.ProcessUpdate(peer2, bgpMessage2) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) // check type path := pList[0] @@ -1074,15 +1093,15 @@ func TestProcessBGPUpdate_6_select_ebgp_path_ipv6(t *testing.T) { expectedNexthopAttr := pathAttributes[0] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) - assert.Equal(t, pathNexthop, expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedOrigin := pathAttributes[1] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[2] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) @@ -1108,7 +1127,8 @@ func TestProcessBGPUpdate_7_select_low_routerid_path_ipv4(t *testing.T) { tm := NewTableManager() tm.localAsn = uint32(65000) - var pList, wList []Path + var pList []Path + var wList []Destination var err error // low origin message @@ -1160,15 +1180,15 @@ func TestProcessBGPUpdate_7_select_low_routerid_path_ipv4(t *testing.T) { pathAttributes := bgpMessage2.Body.(*bgp.BGPUpdate).PathAttributes expectedOrigin := pathAttributes[0] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[1] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedNexthopAttr := pathAttributes[2] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP).(*bgp.PathAttributeNextHop) - assert.Equal(t, pathNexthop, expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) @@ -1190,12 +1210,14 @@ func TestProcessBGPUpdate_7_select_low_routerid_path_ipv6(t *testing.T) { tm := NewTableManager() tm.localAsn = uint32(65000) - var pList, wList []Path + var pList []Path + var wList []Destination var err error origin1 := bgp.NewPathAttributeOrigin(0) aspath1 := createAsPathAttribute([]uint16{65000, 65200}) - mp_reach1 := createMpReach("2001::192:168:50:1", "2001:123:123:1::", 64) + mp_reach1 := createMpReach("2001::192:168:50:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) med1 := bgp.NewPathAttributeMultiExitDisc(200) localpref1 := bgp.NewPathAttributeLocalPref(100) @@ -1209,7 +1231,8 @@ func TestProcessBGPUpdate_7_select_low_routerid_path_ipv6(t *testing.T) { origin2 := bgp.NewPathAttributeOrigin(0) aspath2 := createAsPathAttribute([]uint16{65100, 65200}) - mp_reach2 := createMpReach("2001::192:168:100:1", "2001:123:123:1::", 64) + mp_reach2 := createMpReach("2001::192:168:100:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) med2 := bgp.NewPathAttributeMultiExitDisc(200) localpref2 := bgp.NewPathAttributeLocalPref(100) @@ -1225,13 +1248,13 @@ func TestProcessBGPUpdate_7_select_low_routerid_path_ipv6(t *testing.T) { pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) peer3 := peerR3() pList, wList, err = tm.ProcessUpdate(peer3, bgpMessage2) assert.Equal(t, 1, len(pList)) assert.Equal(t, 0, len(wList)) - assert.NoError(t, err, "err should be nil") + assert.NoError(t, err) // check type path := pList[0] @@ -1243,15 +1266,15 @@ func TestProcessBGPUpdate_7_select_low_routerid_path_ipv6(t *testing.T) { expectedNexthopAttr := pathAttributes[0] pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) - assert.Equal(t, pathNexthop, expectedNexthopAttr) + assert.Equal(t, expectedNexthopAttr, pathNexthop) expectedOrigin := pathAttributes[1] pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) - assert.Equal(t, pathOrigin, expectedOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) expectedAsPath := pathAttributes[2] pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) - assert.Equal(t, pathAspath, expectedAsPath) + assert.Equal(t, expectedAsPath, pathAspath) expectedMed := pathAttributes[3] pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) @@ -1269,6 +1292,853 @@ func TestProcessBGPUpdate_7_select_low_routerid_path_ipv6(t *testing.T) { } +// test: withdraw and mpunreach path +func TestProcessBGPUpdate_8_withdraw_path_ipv4(t *testing.T) { + + tm := NewTableManager() + //setLogger(getLogger(log.DebugLevel)) + var pList []Path + var wList []Destination + var err error + + // path1 + origin1 := bgp.NewPathAttributeOrigin(0) + aspath1 := createAsPathAttribute([]uint16{65000}) + nexthop1 := bgp.NewPathAttributeNextHop("192.168.50.1") + med1 := bgp.NewPathAttributeMultiExitDisc(200) + localpref1 := bgp.NewPathAttributeLocalPref(100) + + pathAttributes1 := []bgp.PathAttributeInterface{ + origin1, aspath1, nexthop1, med1, localpref1, + } + nlri1 := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.10.0")} + withdrawnRoutes1 := []bgp.WithdrawnRoute{} + bgpMessage1 := bgp.NewBGPUpdateMessage(withdrawnRoutes1, pathAttributes1, nlri1) + + // path 2 + origin2 := bgp.NewPathAttributeOrigin(0) + aspath2 := createAsPathAttribute([]uint16{65100, 65000}) + nexthop2 := bgp.NewPathAttributeNextHop("192.168.100.1") + med2 := bgp.NewPathAttributeMultiExitDisc(200) + localpref2 := bgp.NewPathAttributeLocalPref(200) + + pathAttributes2 := []bgp.PathAttributeInterface{ + origin2, aspath2, nexthop2, med2, localpref2, + } + nlri2 := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.10.0")} + withdrawnRoutes2 := []bgp.WithdrawnRoute{} + bgpMessage2 := bgp.NewBGPUpdateMessage(withdrawnRoutes2, pathAttributes2, nlri2) + + peer1 := peerR1() + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + peer2 := peerR2() + pList, wList, err = tm.ProcessUpdate(peer2, bgpMessage2) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + // check type + path := pList[0] + expectedType := "*table.IPv4Path" + assert.Equal(t, reflect.TypeOf(path).String(), expectedType) + + // check PathAttribute + checkPattr := func(expected *bgp.BGPMessage, actual Path) { + pathAttributes := expected.Body.(*bgp.BGPUpdate).PathAttributes + expectedOrigin := pathAttributes[0] + pathOrigin := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) + + expectedAsPath := pathAttributes[1] + pathAspath := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) + assert.Equal(t, expectedAsPath, pathAspath) + + expectedNexthopAttr := pathAttributes[2] + pathNexthop := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP).(*bgp.PathAttributeNextHop) + assert.Equal(t, expectedNexthopAttr, pathNexthop) + + expectedMed := pathAttributes[3] + pathMed := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) + assert.Equal(t, expectedMed, pathMed) + + // check PathAttribute length + assert.Equal(t, len(pathAttributes), path.getPathAttributeMap().Len()) + } + checkPattr(bgpMessage2, path) + // check destination + expectedPrefix := "10.10.10.0" + assert.Equal(t, expectedPrefix, path.getPrefix().String()) + // check nexthop + expectedNexthop := "192.168.100.1" + assert.Equal(t, expectedNexthop, path.getNexthop().String()) + + //withdraw path + w1 := bgp.WithdrawnRoute{*bgp.NewIPAddrPrefix(24, "10.10.10.0")} + w := []bgp.WithdrawnRoute{w1} + bgpMessage3 := bgp.NewBGPUpdateMessage(w, []bgp.PathAttributeInterface{}, []bgp.NLRInfo{}) + + pList, wList, err = tm.ProcessUpdate(peer2, bgpMessage3) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + path = pList[0] + expectedType = "*table.IPv4Path" + assert.Equal(t, reflect.TypeOf(path).String(), expectedType) + + checkPattr(bgpMessage1, path) + // check destination + expectedPrefix = "10.10.10.0" + assert.Equal(t, expectedPrefix, path.getPrefix().String()) + // check nexthop + expectedNexthop = "192.168.50.1" + assert.Equal(t, expectedNexthop, path.getNexthop().String()) +} + +// TODO MP_UNREACH +func TestProcessBGPUpdate_8_mpunreach_path_ipv6(t *testing.T) { + + tm := NewTableManager() + var pList []Path + var wList []Destination + var err error + + origin1 := bgp.NewPathAttributeOrigin(0) + aspath1 := createAsPathAttribute([]uint16{65000}) + mp_reach1 := createMpReach("2001::192:168:50:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) + med1 := bgp.NewPathAttributeMultiExitDisc(200) + localpref1 := bgp.NewPathAttributeLocalPref(100) + + pathAttributes1 := []bgp.PathAttributeInterface{ + mp_reach1, origin1, aspath1, med1, localpref1, + } + + nlri1 := []bgp.NLRInfo{} + withdrawnRoutes1 := []bgp.WithdrawnRoute{} + bgpMessage1 := bgp.NewBGPUpdateMessage(withdrawnRoutes1, pathAttributes1, nlri1) + + origin2 := bgp.NewPathAttributeOrigin(0) + aspath2 := createAsPathAttribute([]uint16{65100, 65000}) + mp_reach2 := createMpReach("2001::192:168:100:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) + med2 := bgp.NewPathAttributeMultiExitDisc(200) + localpref2 := bgp.NewPathAttributeLocalPref(200) + + pathAttributes2 := []bgp.PathAttributeInterface{ + mp_reach2, origin2, aspath2, med2, localpref2, + } + + nlri2 := []bgp.NLRInfo{} + withdrawnRoutes2 := []bgp.WithdrawnRoute{} + bgpMessage2 := bgp.NewBGPUpdateMessage(withdrawnRoutes2, pathAttributes2, nlri2) + + peer1 := peerR1() + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + peer2 := peerR2() + pList, wList, err = tm.ProcessUpdate(peer2, bgpMessage2) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + // check type + path := pList[0] + expectedType := "*table.IPv6Path" + assert.Equal(t, reflect.TypeOf(path).String(), expectedType) + + // check PathAttribute + pathAttributes := bgpMessage2.Body.(*bgp.BGPUpdate).PathAttributes + + expectedNexthopAttr := pathAttributes[0] + pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) + assert.Equal(t, expectedNexthopAttr, pathNexthop) + + expectedOrigin := pathAttributes[1] + pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) + + expectedAsPath := pathAttributes[2] + pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) + assert.Equal(t, expectedAsPath, pathAspath) + + expectedMed := pathAttributes[3] + pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) + assert.Equal(t, expectedMed, pathMed) + + // check PathAttribute + checkPattr := func(expected *bgp.BGPMessage, actual Path) { + pathAttributes := expected.Body.(*bgp.BGPUpdate).PathAttributes + + expectedNexthopAttr := pathAttributes[0] + pathNexthop := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) + assert.Equal(t, expectedNexthopAttr, pathNexthop) + + expectedOrigin := pathAttributes[1] + pathOrigin := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) + + expectedAsPath := pathAttributes[2] + pathAspath := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) + assert.Equal(t, expectedAsPath, pathAspath) + + expectedMed := pathAttributes[3] + pathMed := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) + assert.Equal(t, expectedMed, pathMed) + // check PathAttribute length + assert.Equal(t, len(pathAttributes), path.getPathAttributeMap().Len()) + } + + checkPattr(bgpMessage2, path) + + // check destination + expectedPrefix := "2001:123:123:1::" + assert.Equal(t, expectedPrefix, path.getPrefix().String()) + // check nexthop + expectedNexthop := "2001::192:168:100:1" + assert.Equal(t, expectedNexthop, path.getNexthop().String()) + + //mpunreach path + mp_unreach := createMpUNReach("2001:123:123:1::", 64) + bgpMessage3 := bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, + []bgp.PathAttributeInterface{mp_unreach}, []bgp.NLRInfo{}) + + pList, wList, err = tm.ProcessUpdate(peer2, bgpMessage3) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + path = pList[0] + expectedType = "*table.IPv6Path" + assert.Equal(t, reflect.TypeOf(path).String(), expectedType) + + checkPattr(bgpMessage1, path) + // check destination + expectedPrefix = "2001:123:123:1::" + assert.Equal(t, expectedPrefix, path.getPrefix().String()) + // check nexthop + expectedNexthop = "2001::192:168:50:1" + assert.Equal(t, expectedNexthop, path.getNexthop().String()) + +} + +// handle bestpath lost +func TestProcessBGPUpdate_bestpath_lost_ipv4(t *testing.T) { + + tm := NewTableManager() + //setLogger(getLogger(log.DebugLevel)) + var pList []Path + var wList []Destination + var err error + + // path1 + origin1 := bgp.NewPathAttributeOrigin(0) + aspath1 := createAsPathAttribute([]uint16{65000}) + nexthop1 := bgp.NewPathAttributeNextHop("192.168.50.1") + med1 := bgp.NewPathAttributeMultiExitDisc(200) + localpref1 := bgp.NewPathAttributeLocalPref(100) + + pathAttributes1 := []bgp.PathAttributeInterface{ + origin1, aspath1, nexthop1, med1, localpref1, + } + nlri1 := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.10.0")} + bgpMessage1 := bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, pathAttributes1, nlri1) + + // path 1 withdraw + w1 := bgp.WithdrawnRoute{*bgp.NewIPAddrPrefix(24, "10.10.10.0")} + w := []bgp.WithdrawnRoute{w1} + bgpMessage1_w := bgp.NewBGPUpdateMessage(w, []bgp.PathAttributeInterface{}, []bgp.NLRInfo{}) + + peer1 := peerR1() + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1_w) + assert.Equal(t, 0, len(pList)) + assert.Equal(t, 1, len(wList)) + assert.NoError(t, err) + + // check old best path + path := wList[0].getOldBestPath() + expectedType := "*table.IPv4Path" + assert.Equal(t, reflect.TypeOf(path).String(), expectedType) + + // check PathAttribute + checkPattr := func(expected *bgp.BGPMessage, actual Path) { + pathAttributes := expected.Body.(*bgp.BGPUpdate).PathAttributes + expectedOrigin := pathAttributes[0] + pathOrigin := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) + + expectedAsPath := pathAttributes[1] + pathAspath := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) + assert.Equal(t, expectedAsPath, pathAspath) + + expectedNexthopAttr := pathAttributes[2] + pathNexthop := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP).(*bgp.PathAttributeNextHop) + assert.Equal(t, expectedNexthopAttr, pathNexthop) + + expectedMed := pathAttributes[3] + pathMed := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) + assert.Equal(t, expectedMed, pathMed) + + // check PathAttribute length + assert.Equal(t, len(pathAttributes), path.getPathAttributeMap().Len()) + } + + checkPattr(bgpMessage1, path) + // check destination + expectedPrefix := "10.10.10.0" + assert.Equal(t, expectedPrefix, path.getPrefix().String()) + // check nexthop + expectedNexthop := "192.168.50.1" + assert.Equal(t, expectedNexthop, path.getNexthop().String()) + +} + +func TestProcessBGPUpdate_bestpath_lost_ipv6(t *testing.T) { + + tm := NewTableManager() + var pList []Path + var wList []Destination + var err error + + origin1 := bgp.NewPathAttributeOrigin(0) + aspath1 := createAsPathAttribute([]uint16{65000}) + mp_reach1 := createMpReach("2001::192:168:50:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) + med1 := bgp.NewPathAttributeMultiExitDisc(200) + localpref1 := bgp.NewPathAttributeLocalPref(100) + + pathAttributes1 := []bgp.PathAttributeInterface{ + mp_reach1, origin1, aspath1, med1, localpref1, + } + + nlri1 := []bgp.NLRInfo{} + withdrawnRoutes1 := []bgp.WithdrawnRoute{} + bgpMessage1 := bgp.NewBGPUpdateMessage(withdrawnRoutes1, pathAttributes1, nlri1) + + peer1 := peerR1() + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + // path1 mpunreach + mp_unreach := createMpUNReach("2001:123:123:1::", 64) + bgpMessage1_w := bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, + []bgp.PathAttributeInterface{mp_unreach}, []bgp.NLRInfo{}) + + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1_w) + assert.Equal(t, 0, len(pList)) + assert.Equal(t, 1, len(wList)) + assert.NoError(t, err) + + // check old best path + path := wList[0].getOldBestPath() + expectedType := "*table.IPv6Path" + assert.Equal(t, reflect.TypeOf(path).String(), expectedType) + + // check PathAttribute + checkPattr := func(expected *bgp.BGPMessage, actual Path) { + pathAttributes := expected.Body.(*bgp.BGPUpdate).PathAttributes + + expectedNexthopAttr := pathAttributes[0] + pathNexthop := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) + assert.Equal(t, expectedNexthopAttr, pathNexthop) + + expectedOrigin := pathAttributes[1] + pathOrigin := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) + + expectedAsPath := pathAttributes[2] + pathAspath := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) + assert.Equal(t, expectedAsPath, pathAspath) + + expectedMed := pathAttributes[3] + pathMed := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) + assert.Equal(t, expectedMed, pathMed) + // check PathAttribute length + assert.Equal(t, len(pathAttributes), path.getPathAttributeMap().Len()) + } + + checkPattr(bgpMessage1, path) + + // check destination + expectedPrefix := "2001:123:123:1::" + assert.Equal(t, expectedPrefix, path.getPrefix().String()) + // check nexthop + expectedNexthop := "2001::192:168:50:1" + assert.Equal(t, expectedNexthop, path.getNexthop().String()) + +} + +// test: implicit withdrawal case +func TestProcessBGPUpdate_implicit_withdrwal_ipv4(t *testing.T) { + + tm := NewTableManager() + //setLogger(getLogger(log.DebugLevel)) + var pList []Path + var wList []Destination + var err error + + // path1 + origin1 := bgp.NewPathAttributeOrigin(0) + aspath1 := createAsPathAttribute([]uint16{65000, 65100, 65200}) + nexthop1 := bgp.NewPathAttributeNextHop("192.168.50.1") + med1 := bgp.NewPathAttributeMultiExitDisc(200) + localpref1 := bgp.NewPathAttributeLocalPref(100) + + pathAttributes1 := []bgp.PathAttributeInterface{ + origin1, aspath1, nexthop1, med1, localpref1, + } + nlri1 := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.10.0")} + withdrawnRoutes1 := []bgp.WithdrawnRoute{} + bgpMessage1 := bgp.NewBGPUpdateMessage(withdrawnRoutes1, pathAttributes1, nlri1) + + // path 1 from same peer but short AS_PATH + origin2 := bgp.NewPathAttributeOrigin(0) + aspath2 := createAsPathAttribute([]uint16{65000, 65100}) + nexthop2 := bgp.NewPathAttributeNextHop("192.168.50.1") + med2 := bgp.NewPathAttributeMultiExitDisc(200) + localpref2 := bgp.NewPathAttributeLocalPref(100) + + pathAttributes2 := []bgp.PathAttributeInterface{ + origin2, aspath2, nexthop2, med2, localpref2, + } + nlri2 := []bgp.NLRInfo{*bgp.NewNLRInfo(24, "10.10.10.0")} + withdrawnRoutes2 := []bgp.WithdrawnRoute{} + bgpMessage2 := bgp.NewBGPUpdateMessage(withdrawnRoutes2, pathAttributes2, nlri2) + + peer1 := peerR1() + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage2) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + // check type + path := pList[0] + expectedType := "*table.IPv4Path" + assert.Equal(t, reflect.TypeOf(path).String(), expectedType) + + // check PathAttribute + checkPattr := func(expected *bgp.BGPMessage, actual Path) { + pathAttributes := expected.Body.(*bgp.BGPUpdate).PathAttributes + expectedOrigin := pathAttributes[0] + pathOrigin := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) + + expectedAsPath := pathAttributes[1] + pathAspath := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) + assert.Equal(t, expectedAsPath, pathAspath) + + expectedNexthopAttr := pathAttributes[2] + pathNexthop := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP).(*bgp.PathAttributeNextHop) + assert.Equal(t, expectedNexthopAttr, pathNexthop) + + expectedMed := pathAttributes[3] + pathMed := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) + assert.Equal(t, expectedMed, pathMed) + + // check PathAttribute length + assert.Equal(t, len(pathAttributes), path.getPathAttributeMap().Len()) + } + checkPattr(bgpMessage2, path) + // check destination + expectedPrefix := "10.10.10.0" + assert.Equal(t, expectedPrefix, path.getPrefix().String()) + // check nexthop + expectedNexthop := "192.168.50.1" + assert.Equal(t, expectedNexthop, path.getNexthop().String()) + +} + +func TestProcessBGPUpdate_implicit_withdrwal_ipv6(t *testing.T) { + + tm := NewTableManager() + var pList []Path + var wList []Destination + var err error + + origin1 := bgp.NewPathAttributeOrigin(0) + aspath1 := createAsPathAttribute([]uint16{65000, 65100, 65200}) + mp_reach1 := createMpReach("2001::192:168:50:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) + med1 := bgp.NewPathAttributeMultiExitDisc(200) + localpref1 := bgp.NewPathAttributeLocalPref(200) + + pathAttributes1 := []bgp.PathAttributeInterface{ + mp_reach1, origin1, aspath1, med1, localpref1, + } + + nlri1 := []bgp.NLRInfo{} + withdrawnRoutes1 := []bgp.WithdrawnRoute{} + bgpMessage1 := bgp.NewBGPUpdateMessage(withdrawnRoutes1, pathAttributes1, nlri1) + + origin2 := bgp.NewPathAttributeOrigin(0) + aspath2 := createAsPathAttribute([]uint16{65000, 65100}) + mp_reach2 := createMpReach("2001::192:168:50:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2001:123:123:1::")}) + med2 := bgp.NewPathAttributeMultiExitDisc(200) + localpref2 := bgp.NewPathAttributeLocalPref(200) + + pathAttributes2 := []bgp.PathAttributeInterface{ + mp_reach2, origin2, aspath2, med2, localpref2, + } + + nlri2 := []bgp.NLRInfo{} + withdrawnRoutes2 := []bgp.WithdrawnRoute{} + bgpMessage2 := bgp.NewBGPUpdateMessage(withdrawnRoutes2, pathAttributes2, nlri2) + + peer1 := peerR1() + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage2) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + // check type + path := pList[0] + expectedType := "*table.IPv6Path" + assert.Equal(t, reflect.TypeOf(path).String(), expectedType) + + // check PathAttribute + pathAttributes := bgpMessage2.Body.(*bgp.BGPUpdate).PathAttributes + + expectedNexthopAttr := pathAttributes[0] + pathNexthop := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) + assert.Equal(t, expectedNexthopAttr, pathNexthop) + + expectedOrigin := pathAttributes[1] + pathOrigin := path.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) + + expectedAsPath := pathAttributes[2] + pathAspath := path.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) + assert.Equal(t, expectedAsPath, pathAspath) + + expectedMed := pathAttributes[3] + pathMed := path.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) + assert.Equal(t, expectedMed, pathMed) + + // check PathAttribute + checkPattr := func(expected *bgp.BGPMessage, actual Path) { + pathAttributes := expected.Body.(*bgp.BGPUpdate).PathAttributes + + expectedNexthopAttr := pathAttributes[0] + pathNexthop := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) + assert.Equal(t, expectedNexthopAttr, pathNexthop) + + expectedOrigin := pathAttributes[1] + pathOrigin := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) + + expectedAsPath := pathAttributes[2] + pathAspath := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) + assert.Equal(t, expectedAsPath, pathAspath) + + expectedMed := pathAttributes[3] + pathMed := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) + assert.Equal(t, expectedMed, pathMed) + // check PathAttribute length + assert.Equal(t, len(pathAttributes), path.getPathAttributeMap().Len()) + } + + checkPattr(bgpMessage2, path) + + // check destination + expectedPrefix := "2001:123:123:1::" + assert.Equal(t, expectedPrefix, path.getPrefix().String()) + // check nexthop + expectedNexthop := "2001::192:168:50:1" + assert.Equal(t, expectedNexthop, path.getNexthop().String()) + +} + +// check multiple paths +func TestProcessBGPUpdate_multiple_nlri_ipv4(t *testing.T) { + + tm := NewTableManager() + //setLogger(getLogger(log.DebugLevel)) + var pList []Path + var wList []Destination + var err error + + createPathAttr := func(aspaths []uint16, nh string) []bgp.PathAttributeInterface { + origin := bgp.NewPathAttributeOrigin(0) + aspath := createAsPathAttribute(aspaths) + nexthop := bgp.NewPathAttributeNextHop(nh) + med := bgp.NewPathAttributeMultiExitDisc(200) + localpref := bgp.NewPathAttributeLocalPref(100) + pathAttr := []bgp.PathAttributeInterface{ + origin, aspath, nexthop, med, localpref, + } + return pathAttr + } + + // check PathAttribute + checkPattr := func(expected *bgp.BGPMessage, actual Path) { + pathAttributes := expected.Body.(*bgp.BGPUpdate).PathAttributes + expectedOrigin := pathAttributes[0] + pathOrigin := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) + + expectedAsPath := pathAttributes[1] + pathAspath := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) + assert.Equal(t, expectedAsPath, pathAspath) + + expectedNexthopAttr := pathAttributes[2] + pathNexthop := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_NEXT_HOP).(*bgp.PathAttributeNextHop) + assert.Equal(t, expectedNexthopAttr, pathNexthop) + + expectedMed := pathAttributes[3] + pathMed := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) + assert.Equal(t, expectedMed, pathMed) + + // check PathAttribute length + assert.Equal(t, len(pathAttributes), actual.getPathAttributeMap().Len()) + } + + checkBestPathResult := func(pType, prefix, nexthop string, p Path, m *bgp.BGPMessage) { + expectedType := pType + assert.Equal(t, reflect.TypeOf(p).String(), expectedType) + checkPattr(m, p) + // check destination + assert.Equal(t, prefix, p.getPrefix().String()) + // check nexthop + assert.Equal(t, nexthop, p.getNexthop().String()) + } + + // path1 + pathAttributes1 := createPathAttr([]uint16{65000, 65100, 65200}, "192.168.50.1") + nlri1 := []bgp.NLRInfo{ + *bgp.NewNLRInfo(24, "10.10.10.0"), + *bgp.NewNLRInfo(24, "20.20.20.0"), + *bgp.NewNLRInfo(24, "30.30.30.0"), + *bgp.NewNLRInfo(24, "40.40.40.0"), + *bgp.NewNLRInfo(24, "50.50.50.0")} + bgpMessage1 := bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, pathAttributes1, nlri1) + + // path2 + pathAttributes2 := createPathAttr([]uint16{65000, 65100, 65300}, "192.168.50.1") + nlri2 := []bgp.NLRInfo{ + *bgp.NewNLRInfo(24, "11.11.11.0"), + *bgp.NewNLRInfo(24, "22.22.22.0"), + *bgp.NewNLRInfo(24, "33.33.33.0"), + *bgp.NewNLRInfo(24, "44.44.44.0"), + *bgp.NewNLRInfo(24, "55.55.55.0")} + bgpMessage2 := bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, pathAttributes2, nlri2) + + // path3 + pathAttributes3 := createPathAttr([]uint16{65000, 65100, 65400}, "192.168.50.1") + nlri3 := []bgp.NLRInfo{ + *bgp.NewNLRInfo(24, "77.77.77.0"), + *bgp.NewNLRInfo(24, "88.88.88.0"), + } + bgpMessage3 := bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, pathAttributes3, nlri3) + + // path4 + pathAttributes4 := createPathAttr([]uint16{65000, 65100, 65500}, "192.168.50.1") + nlri4 := []bgp.NLRInfo{ + *bgp.NewNLRInfo(24, "99.99.99.0"), + } + bgpMessage4 := bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, pathAttributes4, nlri4) + + peer1 := peerR1() + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) + assert.Equal(t, 5, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + checkBestPathResult("*table.IPv4Path", "10.10.10.0", "192.168.50.1", pList[0], bgpMessage1) + checkBestPathResult("*table.IPv4Path", "20.20.20.0", "192.168.50.1", pList[1], bgpMessage1) + checkBestPathResult("*table.IPv4Path", "30.30.30.0", "192.168.50.1", pList[2], bgpMessage1) + checkBestPathResult("*table.IPv4Path", "40.40.40.0", "192.168.50.1", pList[3], bgpMessage1) + checkBestPathResult("*table.IPv4Path", "50.50.50.0", "192.168.50.1", pList[4], bgpMessage1) + + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage2) + assert.Equal(t, 5, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + checkBestPathResult("*table.IPv4Path", "11.11.11.0", "192.168.50.1", pList[0], bgpMessage2) + checkBestPathResult("*table.IPv4Path", "22.22.22.0", "192.168.50.1", pList[1], bgpMessage2) + checkBestPathResult("*table.IPv4Path", "33.33.33.0", "192.168.50.1", pList[2], bgpMessage2) + checkBestPathResult("*table.IPv4Path", "44.44.44.0", "192.168.50.1", pList[3], bgpMessage2) + checkBestPathResult("*table.IPv4Path", "55.55.55.0", "192.168.50.1", pList[4], bgpMessage2) + + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage3) + assert.Equal(t, 2, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage4) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + // check table + table := tm.Tables[RF_IPv4_UC] + assert.Equal(t, 13, len(table.getDestinations())) + +} + +// check multiple paths +func TestProcessBGPUpdate_multiple_nlri_ipv6(t *testing.T) { + + tm := NewTableManager() + //setLogger(getLogger(log.DebugLevel)) + var pList []Path + var wList []Destination + var err error + + createPathAttr := func(aspaths []uint16) []bgp.PathAttributeInterface { + origin := bgp.NewPathAttributeOrigin(0) + aspath := createAsPathAttribute(aspaths) + med := bgp.NewPathAttributeMultiExitDisc(100) + localpref := bgp.NewPathAttributeLocalPref(100) + pathAttr := []bgp.PathAttributeInterface{ + origin, aspath, med, localpref, + } + return pathAttr + } + + // check PathAttribute + checkPattr := func(expected *bgp.BGPMessage, actual Path) { + pathAttributes := expected.Body.(*bgp.BGPUpdate).PathAttributes + pathNexthop := pathAttributes[4] + expectedNexthopAttr := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_MP_REACH_NLRI).(*bgp.PathAttributeMpReachNLRI) + assert.Equal(t, expectedNexthopAttr, pathNexthop) + + expectedOrigin := pathAttributes[0] + pathOrigin := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_ORIGIN).(*bgp.PathAttributeOrigin) + assert.Equal(t, expectedOrigin, pathOrigin) + + expectedAsPath := pathAttributes[1] + pathAspath := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_AS_PATH).(*bgp.PathAttributeAsPath) + assert.Equal(t, expectedAsPath, pathAspath) + + expectedMed := pathAttributes[2] + pathMed := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC).(*bgp.PathAttributeMultiExitDisc) + assert.Equal(t, expectedMed, pathMed) + + expectedLocalpref := pathAttributes[3] + localpref := actual.getPathAttribute(bgp.BGP_ATTR_TYPE_LOCAL_PREF).(*bgp.PathAttributeLocalPref) + assert.Equal(t, expectedLocalpref, localpref) + + // check PathAttribute length + assert.Equal(t, len(pathAttributes), actual.getPathAttributeMap().Len()) + + } + + checkBestPathResult := func(pType, prefix, nexthop string, p Path, m *bgp.BGPMessage) { + expectedType := pType + assert.Equal(t, reflect.TypeOf(p).String(), expectedType) + checkPattr(m, p) + // check destination + assert.Equal(t, prefix, p.getPrefix().String()) + // check nexthop + assert.Equal(t, nexthop, p.getNexthop().String()) + } + + // path1 + pathAttributes1 := createPathAttr([]uint16{65000, 65100, 65200}) + mpreach1 := createMpReach("2001::192:168:50:1", []bgp.AddrPrefixInterface{ + bgp.NewIPv6AddrPrefix(64, "2001:123:1210:11::"), + bgp.NewIPv6AddrPrefix(64, "2001:123:1220:11::"), + bgp.NewIPv6AddrPrefix(64, "2001:123:1230:11::"), + bgp.NewIPv6AddrPrefix(64, "2001:123:1240:11::"), + bgp.NewIPv6AddrPrefix(64, "2001:123:1250:11::"), + }) + pathAttributes1 = append(pathAttributes1, mpreach1) + bgpMessage1 := bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, pathAttributes1, []bgp.NLRInfo{}) + + // path2 + pathAttributes2 := createPathAttr([]uint16{65000, 65100, 65300}) + mpreach2 := createMpReach("2001::192:168:50:1", []bgp.AddrPrefixInterface{ + bgp.NewIPv6AddrPrefix(64, "2001:123:1211:11::"), + bgp.NewIPv6AddrPrefix(64, "2001:123:1222:11::"), + bgp.NewIPv6AddrPrefix(64, "2001:123:1233:11::"), + bgp.NewIPv6AddrPrefix(64, "2001:123:1244:11::"), + bgp.NewIPv6AddrPrefix(64, "2001:123:1255:11::"), + }) + pathAttributes2 = append(pathAttributes2, mpreach2) + bgpMessage2 := bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, pathAttributes2, []bgp.NLRInfo{}) + + // path3 + pathAttributes3 := createPathAttr([]uint16{65000, 65100, 65400}) + mpreach3 := createMpReach("2001::192:168:50:1", []bgp.AddrPrefixInterface{ + bgp.NewIPv6AddrPrefix(64, "2001:123:1277:11::"), + bgp.NewIPv6AddrPrefix(64, "2001:123:1288:11::"), + }) + pathAttributes3 = append(pathAttributes3, mpreach3) + bgpMessage3 := bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, pathAttributes3, []bgp.NLRInfo{}) + + // path4 + pathAttributes4 := createPathAttr([]uint16{65000, 65100, 65500}) + mpreach4 := createMpReach("2001::192:168:50:1", []bgp.AddrPrefixInterface{ + bgp.NewIPv6AddrPrefix(64, "2001:123:1299:11::"), + }) + pathAttributes4 = append(pathAttributes4, mpreach4) + bgpMessage4 := bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, pathAttributes4, []bgp.NLRInfo{}) + + peer1 := peerR1() + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage1) + assert.Equal(t, 5, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + checkBestPathResult("*table.IPv6Path", "2001:123:1210:11::", "2001::192:168:50:1", pList[0], bgpMessage1) + checkBestPathResult("*table.IPv6Path", "2001:123:1220:11::", "2001::192:168:50:1", pList[1], bgpMessage1) + checkBestPathResult("*table.IPv6Path", "2001:123:1230:11::", "2001::192:168:50:1", pList[2], bgpMessage1) + checkBestPathResult("*table.IPv6Path", "2001:123:1240:11::", "2001::192:168:50:1", pList[3], bgpMessage1) + checkBestPathResult("*table.IPv6Path", "2001:123:1250:11::", "2001::192:168:50:1", pList[4], bgpMessage1) + + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage2) + assert.Equal(t, 5, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + checkBestPathResult("*table.IPv6Path", "2001:123:1211:11::", "2001::192:168:50:1", pList[0], bgpMessage2) + checkBestPathResult("*table.IPv6Path", "2001:123:1222:11::", "2001::192:168:50:1", pList[1], bgpMessage2) + checkBestPathResult("*table.IPv6Path", "2001:123:1233:11::", "2001::192:168:50:1", pList[2], bgpMessage2) + checkBestPathResult("*table.IPv6Path", "2001:123:1244:11::", "2001::192:168:50:1", pList[3], bgpMessage2) + checkBestPathResult("*table.IPv6Path", "2001:123:1255:11::", "2001::192:168:50:1", pList[4], bgpMessage2) + + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage3) + assert.Equal(t, 2, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + pList, wList, err = tm.ProcessUpdate(peer1, bgpMessage4) + assert.Equal(t, 1, len(pList)) + assert.Equal(t, 0, len(wList)) + assert.NoError(t, err) + + // check table + table := tm.Tables[RF_IPv6_UC] + assert.Equal(t, 13, len(table.getDestinations())) + +} + func update_fromR1() *bgp.BGPMessage { origin := bgp.NewPathAttributeOrigin(0) @@ -1335,7 +2205,8 @@ func update_fromR2_ipv6() *bgp.BGPMessage { origin := bgp.NewPathAttributeOrigin(0) aspath := createAsPathAttribute([]uint16{65100}) - mp_reach := createMpReach("2001::192:168:100:1", "2002:223:123:1::", 64) + mp_reach := createMpReach("2001::192:168:100:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2002:223:123:1::")}) med := bgp.NewPathAttributeMultiExitDisc(100) pathAttributes := []bgp.PathAttributeInterface{ @@ -1355,12 +2226,17 @@ func createAsPathAttribute(ases []uint16) *bgp.PathAttributeAsPath { return aspath } -func createMpReach(nexthop, nlri string, len uint8) *bgp.PathAttributeMpReachNLRI { - mp_nlri := []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(len, nlri)} - mp_reach := bgp.NewPathAttributeMpReachNLRI(nexthop, mp_nlri) +func createMpReach(nexthop string, prefix []bgp.AddrPrefixInterface) *bgp.PathAttributeMpReachNLRI { + mp_reach := bgp.NewPathAttributeMpReachNLRI(nexthop, prefix) return mp_reach } +func createMpUNReach(nlri string, len uint8) *bgp.PathAttributeMpUnreachNLRI { + mp_nlri := []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(len, nlri)} + mp_unreach := bgp.NewPathAttributeMpUnreachNLRI(mp_nlri) + return mp_unreach +} + func update_fromR2viaR1() *bgp.BGPMessage { origin := bgp.NewPathAttributeOrigin(0) @@ -1383,7 +2259,8 @@ func update_fromR2viaR1_ipv6() *bgp.BGPMessage { origin := bgp.NewPathAttributeOrigin(0) aspath := createAsPathAttribute([]uint16{65000, 65100}) - mp_reach := createMpReach("2001::192:168:50:1", "2002:223:123:1::", 64) + mp_reach := createMpReach("2001::192:168:50:1", + []bgp.AddrPrefixInterface{bgp.NewIPv6AddrPrefix(64, "2002:223:123:1::")}) med := bgp.NewPathAttributeMultiExitDisc(100) pathAttributes := []bgp.PathAttributeInterface{ @@ -1397,86 +2274,3 @@ func update_fromR2viaR1_ipv6() *bgp.BGPMessage { return bgp.NewBGPUpdateMessage(withdrawnRoutes, pathAttributes, nlri) } - -func update() *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")} - return bgp.NewBGPUpdateMessage(w, p, n) -} |