summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>2015-01-10 13:40:37 +0000
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2015-01-11 09:54:45 +0900
commit5445305188efb078a6d642690f68cb44ab25a291 (patch)
treeabcc37f052ab7287ead736539cdba5e991a7aab2
parentd6d4f24454975a61f88fc48531ffbcd9e6e8fdd7 (diff)
table: sort destinations when marshaling
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r--table/table.go25
1 files changed, 23 insertions, 2 deletions
diff --git a/table/table.go b/table/table.go
index b52a1eb1..79b310d2 100644
--- a/table/table.go
+++ b/table/table.go
@@ -16,9 +16,13 @@
package table
import (
+ "bytes"
"encoding/json"
+ "fmt"
log "github.com/Sirupsen/logrus"
"github.com/osrg/gobgp/packet"
+ "github.com/tchap/go-patricia/patricia"
+ "net"
"reflect"
)
@@ -49,11 +53,28 @@ func NewTableDefault(scope_id int) *TableDefault {
}
+func cidr2prefix(cidr string) patricia.Prefix {
+ _, n, _ := net.ParseCIDR(cidr)
+ var buffer bytes.Buffer
+ for i := 0; i < len(n.IP); i++ {
+ buffer.WriteString(fmt.Sprintf("%08b", n.IP[i]))
+ }
+ ones, _ := n.Mask.Size()
+ return patricia.Prefix(buffer.String()[:ones])
+}
+
func (td *TableDefault) MarshalJSON() ([]byte, error) {
+ trie := patricia.NewTrie()
+ for key, dest := range td.destinations {
+ trie.Insert(cidr2prefix(key), dest)
+ }
+
destList := make([]Destination, 0)
- for _, dest := range td.destinations {
+ trie.Visit(func(prefix patricia.Prefix, item patricia.Item) error {
+ dest, _ := item.(Destination)
destList = append(destList, dest)
- }
+ return nil
+ })
return json.Marshal(struct {
Destinations []Destination