diff options
-rw-r--r-- | packet/bgp.go | 4 | ||||
-rw-r--r-- | packet/bgp_test.go | 10 | ||||
-rw-r--r-- | table/table.go | 35 | ||||
-rw-r--r-- | table/table_test.go | 16 |
4 files changed, 38 insertions, 27 deletions
diff --git a/packet/bgp.go b/packet/bgp.go index 505212b4..ae222a34 100644 --- a/packet/bgp.go +++ b/packet/bgp.go @@ -469,6 +469,10 @@ func (r *IPAddrPrefixDefault) Len() int { return int(1 + ((r.Length + 7) / 8)) } +func (r *IPAddrPrefixDefault) String() string { + return fmt.Sprintf("%s/%d", r.Prefix.String(), r.Length) +} + type IPAddrPrefix struct { IPAddrPrefixDefault addrlen uint8 diff --git a/packet/bgp_test.go b/packet/bgp_test.go index 46ff293d..2cc36a85 100644 --- a/packet/bgp_test.go +++ b/packet/bgp_test.go @@ -2,6 +2,7 @@ package bgp import ( "bytes" + "github.com/stretchr/testify/assert" "net" "testing" ) @@ -131,3 +132,12 @@ func Test_Message(t *testing.T) { } } } + +func Test_IPAddrPrefixString(t *testing.T) { + ipv4 := NewIPAddrPrefix(24, "129.6.10.0") + assert.Equal(t, "129.6.10.0/24", ipv4.String()) + ipv6 := NewIPv6AddrPrefix(18, "3343:faba:3903::1") + assert.Equal(t, "3343:faba:3903::1/18", ipv6.String()) + ipv6 = NewIPv6AddrPrefix(18, "3343:faba:3903::0") + assert.Equal(t, "3343:faba:3903::/18", ipv6.String()) +} diff --git a/table/table.go b/table/table.go index de36510d..b52a1eb1 100644 --- a/table/table.go +++ b/table/table.go @@ -19,7 +19,6 @@ import ( "encoding/json" log "github.com/Sirupsen/logrus" "github.com/osrg/gobgp/packet" - "net" "reflect" ) @@ -29,7 +28,7 @@ type Table interface { setDestinations(destinations map[string]Destination) getDestination(key string) Destination setDestination(key string, dest Destination) - tableKey(nlri bgp.AddrPrefixInterface) net.IP + tableKey(nlri bgp.AddrPrefixInterface) string validatePath(path Path) validateNlri(nlri bgp.AddrPrefixInterface) DeleteDestByPeer(*PeerInfo) []Destination @@ -129,16 +128,16 @@ func (td *TableDefault) DeleteDestByPeer(peerInfo *PeerInfo) []Destination { func deleteDestByNlri(table Table, nlri bgp.AddrPrefixInterface) Destination { table.validateNlri(nlri) destinations := table.getDestinations() - dest := destinations[table.tableKey(nlri).String()] + dest := destinations[table.tableKey(nlri)] if dest != nil { - delete(destinations, table.tableKey(nlri).String()) + delete(destinations, table.tableKey(nlri)) } return dest } func deleteDest(table Table, dest Destination) { destinations := table.getDestinations() - delete(destinations, table.tableKey(dest.getNlri()).String()) + delete(destinations, table.tableKey(dest.getNlri())) } func (td *TableDefault) validatePath(path Path) { @@ -171,12 +170,12 @@ func (td *TableDefault) validateNlri(nlri bgp.AddrPrefixInterface) { func getOrCreateDest(table Table, nlri bgp.AddrPrefixInterface) Destination { log.Debugf("Table type : %s", reflect.TypeOf(table)) tableKey := table.tableKey(nlri) - dest := table.getDestination(tableKey.String()) + dest := table.getDestination(tableKey) // If destination for given prefix does not exist we create it. if dest == nil { - log.Debugf("dest with key %s is not found", tableKey.String()) + log.Debugf("dest with key %s is not found", tableKey) dest = table.createDest(nlri) - table.setDestination(tableKey.String(), dest) + table.setDestination(tableKey, dest) } return dest } @@ -201,11 +200,11 @@ func (td *TableDefault) setDestination(key string, dest Destination) { } //Implements interface -func (td *TableDefault) tableKey(nlri bgp.AddrPrefixInterface) net.IP { +func (td *TableDefault) tableKey(nlri bgp.AddrPrefixInterface) string { //need Inheritance over ride //return &nlri.IPAddrPrefix.IPAddrPrefixDefault.Prefix log.Error("CreateDest NotImplementedError") - return nil + return "" } /* @@ -233,17 +232,15 @@ func (ipv4t *IPv4Table) createDest(nlri bgp.AddrPrefixInterface) Destination { //make tablekey //Implements interface -func (ipv4t *IPv4Table) tableKey(nlri bgp.AddrPrefixInterface) net.IP { - //addrPrefix := nlri.(*bgp.IPAddrPrefix) - - var ip net.IP +func (ipv4t *IPv4Table) tableKey(nlri bgp.AddrPrefixInterface) string { switch p := nlri.(type) { case *bgp.NLRInfo: - ip = p.IPAddrPrefix.IPAddrPrefixDefault.Prefix + return p.IPAddrPrefix.IPAddrPrefixDefault.String() case *bgp.WithdrawnRoute: - ip = p.IPAddrPrefix.IPAddrPrefixDefault.Prefix + return p.IPAddrPrefix.IPAddrPrefixDefault.String() } - return ip + log.Fatal() + return "" } type IPv6Table struct { @@ -267,9 +264,9 @@ func (ipv6t *IPv6Table) createDest(nlri bgp.AddrPrefixInterface) Destination { //make tablekey //Implements interface -func (ipv6t *IPv6Table) tableKey(nlri bgp.AddrPrefixInterface) net.IP { +func (ipv6t *IPv6Table) tableKey(nlri bgp.AddrPrefixInterface) string { addrPrefix := nlri.(*bgp.IPv6AddrPrefix) - return addrPrefix.IPAddrPrefixDefault.Prefix + return addrPrefix.IPAddrPrefixDefault.String() } diff --git a/table/table_test.go b/table/table_test.go index 74381e8b..4af1d221 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -32,7 +32,7 @@ func TestTableTableKeyDefault(t *testing.T) { td := NewTableDefault(0) nlri := bgp.NewNLRInfo(24, "13.2.3.1") tk := td.tableKey(nlri) - assert.Nil(t, tk) + assert.Equal(t, tk, "") } func TestTableDeleteDestByNlri(t *testing.T) { @@ -43,10 +43,10 @@ func TestTableDeleteDestByNlri(t *testing.T) { for _, path := range pathT { tableKey := ipv4t.tableKey(path.getNlri()) dest := ipv4t.createDest(path.getNlri()) - ipv4t.setDestination(tableKey.String(), dest) + ipv4t.setDestination(tableKey, dest) } tableKey := ipv4t.tableKey(pathT[0].getNlri()) - gdest := ipv4t.getDestination(tableKey.String()) + gdest := ipv4t.getDestination(tableKey) rdest := deleteDestByNlri(ipv4t, pathT[0].getNlri()) assert.Equal(t, rdest, gdest) } @@ -59,13 +59,13 @@ func TestTableDeleteDest(t *testing.T) { for _, path := range pathT { tableKey := ipv4t.tableKey(path.getNlri()) dest := ipv4t.createDest(path.getNlri()) - ipv4t.setDestination(tableKey.String(), dest) + ipv4t.setDestination(tableKey, dest) } tableKey := ipv4t.tableKey(pathT[0].getNlri()) dest := ipv4t.createDest(pathT[0].getNlri()) - ipv4t.setDestination(tableKey.String(), dest) + ipv4t.setDestination(tableKey, dest) deleteDest(ipv4t, dest) - gdest := ipv4t.getDestination(tableKey.String()) + gdest := ipv4t.getDestination(tableKey) assert.Nil(t, gdest) } @@ -84,7 +84,7 @@ func TestTableSetDestinations(t *testing.T) { for _, path := range pathT { tableKey := ipv4t.tableKey(path.getNlri()) dest := ipv4t.createDest(path.getNlri()) - destinations[tableKey.String()] = dest + destinations[tableKey] = dest } ipv4t.setDestinations(destinations) ds := ipv4t.getDestinations() @@ -99,7 +99,7 @@ func TestTableGetDestinations(t *testing.T) { for _, path := range pathT { tableKey := ipv4t.tableKey(path.getNlri()) dest := ipv4t.createDest(path.getNlri()) - destinations[tableKey.String()] = dest + destinations[tableKey] = dest } ipv4t.setDestinations(destinations) ds := ipv4t.getDestinations() |