From 5445305188efb078a6d642690f68cb44ab25a291 Mon Sep 17 00:00:00 2001 From: ISHIDA Wataru Date: Sat, 10 Jan 2015 13:40:37 +0000 Subject: table: sort destinations when marshaling Signed-off-by: FUJITA Tomonori --- table/table.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'table') 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 -- cgit v1.2.3