From 35145891c55f7468ff9ae464d3edfe475cdda985 Mon Sep 17 00:00:00 2001 From: FUJITA Tomonori Date: Thu, 25 Dec 2014 02:00:20 -0800 Subject: table: add MarshalJSON to Destination and Table Signed-off-by: FUJITA Tomonori --- table/destination.go | 14 ++++++++++++++ table/table.go | 16 ++++++++++++++++ 2 files changed, 30 insertions(+) (limited to 'table') diff --git a/table/destination.go b/table/destination.go index 0d16a0db..f90a53fa 100644 --- a/table/destination.go +++ b/table/destination.go @@ -17,6 +17,7 @@ package table import ( "encoding/binary" + "encoding/json" "fmt" log "github.com/Sirupsen/logrus" "github.com/osrg/gobgp/packet" @@ -65,6 +66,7 @@ type Destination interface { addNewPath(newPath Path) constructWithdrawPath() Path removeOldPathsFromSource(source *PeerInfo) []Path + MarshalJSON() ([]byte, error) } type DestinationDefault struct { @@ -91,6 +93,18 @@ func NewDestinationDefault(nlri bgp.AddrPrefixInterface) *DestinationDefault { return destination } +func (dd *DestinationDefault) MarshalJSON() ([]byte, error) { + prefix := dd.getNlri().(*bgp.NLRInfo).Prefix + paths, _ := json.Marshal(dd.knownPathList) + return json.Marshal(struct { + Prefix string + Paths string + }{ + Prefix: prefix.String(), + Paths: string(paths), + }) +} + func (dd *DestinationDefault) getRouteFamily() RouteFamily { return dd.ROUTE_FAMILY } diff --git a/table/table.go b/table/table.go index 1215d51d..df55947b 100644 --- a/table/table.go +++ b/table/table.go @@ -16,6 +16,7 @@ package table import ( + "encoding/json" log "github.com/Sirupsen/logrus" "github.com/osrg/gobgp/packet" "net" @@ -32,6 +33,7 @@ type Table interface { validatePath(path Path) validateNlri(nlri bgp.AddrPrefixInterface) DeleteDestByPeer(*PeerInfo) []Destination + MarshalJSON() ([]byte, error) } type TableDefault struct { @@ -48,6 +50,20 @@ func NewTableDefault(scope_id int) *TableDefault { } +func (td *TableDefault) MarshalJSON() ([]byte, error) { + destList := make([]Destination, 0) + for _, dest := range td.destinations { + destList = append(destList, dest) + } + j, _ := json.Marshal(destList) + + return json.Marshal(struct { + Destinations string + }{ + Destinations: string(j), + }) +} + func (td *TableDefault) getRoutefamily() RouteFamily { return td.ROUTE_FAMILY } -- cgit v1.2.3