summaryrefslogtreecommitdiffhomepage
path: root/table/path.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/path.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/path.go')
-rw-r--r--table/path.go31
1 files changed, 20 insertions, 11 deletions
diff --git a/table/path.go b/table/path.go
index fddde7d3..5abd1f10 100644
--- a/table/path.go
+++ b/table/path.go
@@ -19,6 +19,7 @@ import (
"encoding/json"
"fmt"
log "github.com/Sirupsen/logrus"
+ "github.com/osrg/gobgp/api"
"github.com/osrg/gobgp/config"
"github.com/osrg/gobgp/packet"
"net"
@@ -45,6 +46,7 @@ type Path interface {
Clone(IsWithdraw bool) Path
getTimestamp() time.Time
setTimestamp(t time.Time)
+ ToApiStruct() *api.Path
MarshalJSON() ([]byte, error)
}
@@ -170,18 +172,25 @@ func (pd *PathDefault) setTimestamp(t time.Time) {
pd.timestamp = t
}
+func (pd *PathDefault) ToApiStruct() *api.Path {
+ pathAttrs := func(arg []bgp.PathAttributeInterface) []*api.PathAttr {
+ ret := make([]*api.PathAttr, 0, len(arg))
+ for _, a := range arg {
+ ret = append(ret, a.ToApiStruct())
+ }
+ return ret
+ }(pd.getPathAttrs())
+ return &api.Path{
+ Nlri: pd.GetNlri().ToApiStruct(),
+ Nexthop: pd.GetNexthop().String(),
+ Attrs: pathAttrs,
+ Age: int64(time.Now().Sub(pd.timestamp).Seconds()),
+ IsWithdraw: pd.IsWithdraw(),
+ }
+}
+
func (pd *PathDefault) MarshalJSON() ([]byte, error) {
- return json.Marshal(struct {
- Network string
- Nexthop string
- Attrs []bgp.PathAttributeInterface
- Age int64
- }{
- Network: pd.getPrefix(),
- Nexthop: pd.GetNexthop().String(),
- Attrs: pd.getPathAttrs(),
- Age: int64(time.Now().Sub(pd.timestamp).Seconds()),
- })
+ return json.Marshal(pd.ToApiStruct())
}
// create new PathAttributes