summaryrefslogtreecommitdiffhomepage
path: root/table
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
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')
-rw-r--r--table/destination.go33
-rw-r--r--table/path.go31
-rw-r--r--table/table.go8
-rw-r--r--table/table_manager.go2
-rw-r--r--table/table_manager_test.go4
-rw-r--r--table/table_test.go4
6 files changed, 51 insertions, 31 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 {
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
diff --git a/table/table.go b/table/table.go
index 28799a24..d9c14144 100644
--- a/table/table.go
+++ b/table/table.go
@@ -30,7 +30,7 @@ import (
type Table interface {
createDest(nlri bgp.AddrPrefixInterface) Destination
- getDestinations() map[string]Destination
+ GetDestinations() map[string]Destination
setDestinations(destinations map[string]Destination)
getDestination(key string) Destination
setDestination(key string, dest Destination)
@@ -153,7 +153,7 @@ func (td *TableDefault) DeleteDestByPeer(peerInfo *PeerInfo) []Destination {
func deleteDestByNlri(table Table, nlri bgp.AddrPrefixInterface) Destination {
table.validateNlri(nlri)
- destinations := table.getDestinations()
+ destinations := table.GetDestinations()
dest := destinations[table.tableKey(nlri)]
if dest != nil {
delete(destinations, table.tableKey(nlri))
@@ -162,7 +162,7 @@ func deleteDestByNlri(table Table, nlri bgp.AddrPrefixInterface) Destination {
}
func deleteDest(table Table, dest Destination) {
- destinations := table.getDestinations()
+ destinations := table.GetDestinations()
delete(destinations, table.tableKey(dest.getNlri()))
}
@@ -230,7 +230,7 @@ func getOrCreateDest(table Table, nlri bgp.AddrPrefixInterface) Destination {
return dest
}
-func (td *TableDefault) getDestinations() map[string]Destination {
+func (td *TableDefault) GetDestinations() map[string]Destination {
return td.destinations
}
func (td *TableDefault) setDestinations(destinations map[string]Destination) {
diff --git a/table/table_manager.go b/table/table_manager.go
index abb00cf9..679b684c 100644
--- a/table/table_manager.go
+++ b/table/table_manager.go
@@ -269,7 +269,7 @@ func (manager *TableManager) GetPathList(rf bgp.RouteFamily) []Path {
return []Path{}
}
var paths []Path
- for _, dest := range manager.Tables[rf].getDestinations() {
+ for _, dest := range manager.Tables[rf].GetDestinations() {
paths = append(paths, dest.getBestPath())
}
return paths
diff --git a/table/table_manager_test.go b/table/table_manager_test.go
index 8e800c8c..91887cfb 100644
--- a/table/table_manager_test.go
+++ b/table/table_manager_test.go
@@ -2053,7 +2053,7 @@ func TestProcessBGPUpdate_multiple_nlri_ipv4(t *testing.T) {
// check table
table := tm.Tables[bgp.RF_IPv4_UC]
- assert.Equal(t, 13, len(table.getDestinations()))
+ assert.Equal(t, 13, len(table.GetDestinations()))
}
@@ -2200,7 +2200,7 @@ func TestProcessBGPUpdate_multiple_nlri_ipv6(t *testing.T) {
// check table
table := tm.Tables[bgp.RF_IPv6_UC]
- assert.Equal(t, 13, len(table.getDestinations()))
+ assert.Equal(t, 13, len(table.GetDestinations()))
}
diff --git a/table/table_test.go b/table/table_test.go
index 81e377f6..407fbe0a 100644
--- a/table/table_test.go
+++ b/table/table_test.go
@@ -88,7 +88,7 @@ func TestTableSetDestinations(t *testing.T) {
destinations[tableKey] = dest
}
ipv4t.setDestinations(destinations)
- ds := ipv4t.getDestinations()
+ ds := ipv4t.GetDestinations()
assert.Equal(t, ds, destinations)
}
func TestTableGetDestinations(t *testing.T) {
@@ -103,7 +103,7 @@ func TestTableGetDestinations(t *testing.T) {
destinations[tableKey] = dest
}
ipv4t.setDestinations(destinations)
- ds := ipv4t.getDestinations()
+ ds := ipv4t.GetDestinations()
assert.Equal(t, ds, destinations)
}