summaryrefslogtreecommitdiffhomepage
path: root/table/path.go
diff options
context:
space:
mode:
authorFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2014-12-25 01:02:28 -0800
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2014-12-25 01:02:28 -0800
commit037bc0169ca6e90cda1c52347c66d74198109a5c (patch)
tree7a1bd751e6e01bcef1109da9ce4ba3584a40bafa /table/path.go
parentc0b90c40ae42fb0dc3088376a147ddfd5835519f (diff)
table: add MarshalJSON() to Path struct
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
Diffstat (limited to 'table/path.go')
-rw-r--r--table/path.go51
1 files changed, 50 insertions, 1 deletions
diff --git a/table/path.go b/table/path.go
index 777e17c6..9b38bc44 100644
--- a/table/path.go
+++ b/table/path.go
@@ -16,6 +16,7 @@
package table
import (
+ "encoding/json"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/osrg/gobgp/packet"
@@ -41,6 +42,8 @@ type Path interface {
setMedSetByTargetNeighbor(medSetByTargetNeighbor bool)
getMedSetByTargetNeighbor() bool
Clone(IsWithdraw bool) Path
+ setBest(isBest bool)
+ MarshalJSON() ([]byte, error)
}
type PathDefault struct {
@@ -52,6 +55,7 @@ type PathDefault struct {
nlri bgp.AddrPrefixInterface
pathAttrs []bgp.PathAttributeInterface
medSetByTargetNeighbor bool
+ isBest bool
}
func NewPathDefault(rf RouteFamily, source *PeerInfo, nlri bgp.AddrPrefixInterface, sourceVerNum int, nexthop net.IP, isWithdraw bool, pattrs []bgp.PathAttributeInterface, medSetByTargetNeighbor bool) *PathDefault {
@@ -70,10 +74,55 @@ func NewPathDefault(rf RouteFamily, source *PeerInfo, nlri bgp.AddrPrefixInterfa
path.sourceVerNum = sourceVerNum
path.withdraw = isWithdraw
path.medSetByTargetNeighbor = medSetByTargetNeighbor
-
+ path.isBest = false
return path
}
+func (pd *PathDefault) setBest(isBest bool) {
+ pd.isBest = isBest
+}
+
+func (pd *PathDefault) MarshalJSON() ([]byte, error) {
+ med := uint32(0)
+ _, attr := pd.GetPathAttr(bgp.BGP_ATTR_TYPE_MULTI_EXIT_DISC)
+ if attr != nil {
+ med = attr.(*bgp.PathAttributeMultiExitDisc).Value
+ }
+
+ aslist := make([]uint32, 0)
+ _, attr = pd.GetPathAttr(bgp.BGP_ATTR_TYPE_AS_PATH)
+ if attr != nil {
+ for _, p := range attr.(*bgp.PathAttributeAsPath).Value {
+ path := p.(*bgp.As4PathParam)
+ aslist = append(aslist, path.AS...)
+ }
+ }
+ asPath, _ := json.Marshal(aslist)
+
+ var prefix net.IP
+ var prefixLen uint8
+ switch nlri := pd.nlri.(type) {
+ case *bgp.NLRInfo:
+ prefix = nlri.Prefix
+ prefixLen = nlri.Length
+ }
+
+ return json.Marshal(struct {
+ Network string
+ Nexthop string
+ AsPath string
+ Metric string
+ //origin string
+ Best string
+ }{
+ Network: prefix.String() + "/" + fmt.Sprint(prefixLen),
+ Nexthop: pd.nexthop.String(),
+ Metric: fmt.Sprint(med),
+ AsPath: string(asPath),
+ Best: fmt.Sprint(pd.isBest),
+ })
+}
+
// create new PathAttributes
func (pd *PathDefault) Clone(isWithdraw bool) Path {
copiedAttrs := []bgp.PathAttributeInterface(nil)