From 3f6876df0b16043412972e7fd92377226bc9c89d Mon Sep 17 00:00:00 2001 From: ISHIDA Wataru Date: Wed, 15 Apr 2015 05:24:58 +0000 Subject: api: add a method to convert internal structs to protobuf structs - add ToApiStruct() for convertion of internal structs to protobuf structs to avoid ugly convertion by json.Marshal() && json.Unmarshal() - move grpc server code under /server instead of /api - update proto file to include more detailed path information Signed-off-by: ISHIDA Wataru --- table/destination.go | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'table/destination.go') diff --git a/table/destination.go b/table/destination.go index aae02822..cfd411ce 100644 --- a/table/destination.go +++ b/table/destination.go @@ -20,6 +20,7 @@ import ( "encoding/json" "fmt" log "github.com/Sirupsen/logrus" + "github.com/osrg/gobgp/api" "github.com/osrg/gobgp/packet" "net" "reflect" @@ -66,6 +67,7 @@ type Destination interface { addNewPath(newPath Path) constructWithdrawPath() Path removeOldPathsFromSource(source *PeerInfo) []Path + ToApiStruct() *api.Destination MarshalJSON() ([]byte, error) } @@ -94,7 +96,11 @@ func NewDestinationDefault(nlri bgp.AddrPrefixInterface) *DestinationDefault { } func (dd *DestinationDefault) MarshalJSON() ([]byte, error) { - prefix := dd.getNlri().(*bgp.NLRInfo).Prefix + return json.Marshal(dd.ToApiStruct()) +} + +func (dd *DestinationDefault) ToApiStruct() *api.Destination { + prefix := dd.getNlri().String() idx := func() int { for i, p := range dd.knownPathList { @@ -104,19 +110,24 @@ func (dd *DestinationDefault) MarshalJSON() ([]byte, error) { } log.WithFields(log.Fields{ "Topic": "Table", - "Key": prefix.String(), + "Key": prefix, }).Panic("no best path") return 0 }() - return json.Marshal(struct { - Prefix string - Paths []Path - BestPathIdx int `json:"best_path_idx"` - }{ - Prefix: prefix.String(), - Paths: dd.knownPathList, - BestPathIdx: idx, - }) + + paths := func(arg []Path) []*api.Path { + ret := make([]*api.Path, 0, len(arg)) + for _, p := range arg { + ret = append(ret, p.ToApiStruct()) + } + return ret + }(dd.knownPathList) + + return &api.Destination{ + Prefix: prefix, + Paths: paths, + BestPathIdx: uint32(idx), + } } func (dd *DestinationDefault) getRouteFamily() bgp.RouteFamily { -- cgit v1.2.3