summaryrefslogtreecommitdiffhomepage
path: root/table/destination.go
diff options
context:
space:
mode:
authorISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>2015-04-15 05:24:58 +0000
committerISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>2015-04-15 05:24:58 +0000
commit3f6876df0b16043412972e7fd92377226bc9c89d (patch)
treed8b1e2a41a0dca1720929807cc8161568e7c3565 /table/destination.go
parenta1114dc7bb6dcb165c6494799be94b5d10fe5b17 (diff)
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 <ishida.wataru@lab.ntt.co.jp>
Diffstat (limited to 'table/destination.go')
-rw-r--r--table/destination.go33
1 files changed, 22 insertions, 11 deletions
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 {