diff options
author | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2015-04-03 23:38:23 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2015-04-03 23:38:23 +0900 |
commit | e6a5f817416bfd63e0b7a75be613d9af1cf2b6d6 (patch) | |
tree | c04a5ec4d060244665c07a0213bd756d40effd39 /server/server.go | |
parent | d1a207366e1e72c4737e76623b9ab998057cb751 (diff) |
server: show peers in the sorted order
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
Diffstat (limited to 'server/server.go')
-rw-r--r-- | server/server.go | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/server/server.go b/server/server.go index 22fb48c3..b9d0ddf6 100644 --- a/server/server.go +++ b/server/server.go @@ -24,6 +24,7 @@ import ( "github.com/osrg/gobgp/policy" "net" "os" + "sort" "strconv" "strings" ) @@ -274,14 +275,40 @@ func (server *BgpServer) SetPolicy(pl config.RoutingPolicy) { server.policyMap = pMap } +type peers []*Peer + +func (p peers) Len() int { + return len(p) +} + +func (p peers) Swap(i, j int) { + p[i], p[j] = p[j], p[i] +} + +func (p peers) Less(i, j int) bool { + p1 := p[i].peerConfig.NeighborAddress + p2 := p[j].peerConfig.NeighborAddress + p1Isv4 := p1.To4() != nil + p2Isv4 := p2.To4() != nil + if p1Isv4 != p2Isv4 { + if p1Isv4 == true { + return true + } + return false + } + strings := sort.StringSlice{p1.String(), p2.String()} + return strings.Less(0, 1) +} + func (server *BgpServer) handleRest(restReq *api.RestRequest) { switch restReq.RequestType { case api.REQ_NEIGHBORS: result := &api.RestResponse{} - peerList := make([]*Peer, 0) + peerList := peers{} for _, info := range server.peerMap { peerList = append(peerList, info.peer) } + sort.Sort(peerList) j, _ := json.Marshal(peerList) result.Data = j restReq.ResponseCh <- result |