summaryrefslogtreecommitdiffhomepage
path: root/packet/bgp.go
diff options
context:
space:
mode:
authorISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>2015-09-02 03:03:53 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2015-09-02 09:57:52 +0900
commitada1d611be510e950d42997bbfd6896d65f5c234 (patch)
treed1668119845bbea9beaad9b615a517ead3fa9474 /packet/bgp.go
parent78e2f93e2fddf86596880c1785bf67a861f1328d (diff)
packet: add MarshalJSON() to flowspec related structs
make it more pretty when marshaling flowspec nlri Signed-off-by: ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>
Diffstat (limited to 'packet/bgp.go')
-rw-r--r--packet/bgp.go43
1 files changed, 39 insertions, 4 deletions
diff --git a/packet/bgp.go b/packet/bgp.go
index a546db88..0e46ae5e 100644
--- a/packet/bgp.go
+++ b/packet/bgp.go
@@ -2064,6 +2064,16 @@ func (p *flowSpecPrefix) String() string {
return fmt.Sprintf("[%s:%s]", p.Type(), p.Prefix.String())
}
+func (p *flowSpecPrefix) MarshalJSON() ([]byte, error) {
+ return json.Marshal(struct {
+ Type BGPFlowSpecType `json:"type"`
+ Value AddrPrefixInterface `json:"value"`
+ }{
+ Type: p.Type(),
+ Value: p.Prefix,
+ })
+}
+
type FlowSpecDestinationPrefix struct {
flowSpecPrefix
}
@@ -2081,8 +2091,8 @@ func NewFlowSpecSourcePrefix(prefix AddrPrefixInterface) *FlowSpecSourcePrefix {
}
type FlowSpecComponentItem struct {
- Op int
- Value int
+ Op int `json:"op"`
+ Value int `json:"value"`
}
func (v *FlowSpecComponentItem) Serialize() ([]byte, error) {
@@ -2275,6 +2285,16 @@ func (p *FlowSpecComponent) String() string {
return fmt.Sprintf("[%s:%s]", p.type_, buf.String())
}
+func (p *FlowSpecComponent) MarshalJSON() ([]byte, error) {
+ return json.Marshal(struct {
+ Type BGPFlowSpecType `json:"type"`
+ Value []*FlowSpecComponentItem `json:"value"`
+ }{
+ Type: p.Type(),
+ Value: p.Items,
+ })
+}
+
func NewFlowSpecComponent(type_ BGPFlowSpecType, items []*FlowSpecComponentItem) *FlowSpecComponent {
return &FlowSpecComponent{
Items: items,
@@ -3159,12 +3179,16 @@ func (p *PathAttributeNextHop) String() string {
}
func (p *PathAttributeNextHop) MarshalJSON() ([]byte, error) {
+ value := "0.0.0.0"
+ if p.Value != nil {
+ value = p.Value.String()
+ }
return json.Marshal(struct {
Type BGPAttrType `json:"type"`
Value string `json:"nexthop"`
}{
Type: p.GetType(),
- Value: p.Value.String(),
+ Value: value,
})
}
@@ -3710,6 +3734,17 @@ func (p *PathAttributeMpReachNLRI) Serialize() ([]byte, error) {
}
func (p *PathAttributeMpReachNLRI) MarshalJSON() ([]byte, error) {
+ nexthop := p.Nexthop.String()
+ if p.Nexthop == nil {
+ switch p.AFI {
+ case AFI_IP:
+ nexthop = "0.0.0.0"
+ case AFI_IP6:
+ nexthop = "::"
+ default:
+ nexthop = "fictitious"
+ }
+ }
return json.Marshal(struct {
Type BGPAttrType `json:"type"`
Nexthop string `json:"nexthop"`
@@ -3718,7 +3753,7 @@ func (p *PathAttributeMpReachNLRI) MarshalJSON() ([]byte, error) {
Value []AddrPrefixInterface `json:"value"`
}{
Type: p.GetType(),
- Nexthop: p.Nexthop.String(),
+ Nexthop: nexthop,
AFI: p.AFI,
SAFI: p.SAFI,
Value: p.Value,