From be40351b8aa925a99798998514244a9d700c5691 Mon Sep 17 00:00:00 2001 From: IWASE Yusuke Date: Sun, 11 Feb 2018 21:46:02 +0900 Subject: packet/bgp: Missing String()/MarshalJSON() of PathAttribute subs First this patch removes String() and MarshalJSON() functions of PathAttribute struct. This helps to find the struct types which embed PathAttribute struct without having own String() and MarshalJSON() functions. If those functions of PathAttribute struct are unexpectedly called, those functions are not enough to display its contents. Then this patch implements missing String() and MarshalJSON() functions for some struct types which embed PathAttribute struct. Signed-off-by: IWASE Yusuke --- packet/bgp/bgp.go | 58 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 14 deletions(-) (limited to 'packet') diff --git a/packet/bgp/bgp.go b/packet/bgp/bgp.go index d585133e..1d2e5917 100644 --- a/packet/bgp/bgp.go +++ b/packet/bgp/bgp.go @@ -4943,20 +4943,6 @@ func (p *PathAttribute) Serialize(value []byte, options ...*MarshallingOption) ( return buf, nil } -func (p *PathAttribute) String() string { - return fmt.Sprintf("{Type: %s, Flags: %s}", p.Type, p.Flags) -} - -func (p *PathAttribute) MarshalJSON() ([]byte, error) { - return json.Marshal(struct { - Type BGPAttrType `json:"type"` - Flags BGPAttrFlag `json:"flags"` - }{ - Type: p.GetType(), - Flags: p.Flags, - }) -} - type PathAttributeOrigin struct { PathAttribute Value uint8 @@ -7333,6 +7319,16 @@ func (p *PathAttributeAs4Path) String() string { return strings.Join(params, " ") } +func (p *PathAttributeAs4Path) MarshalJSON() ([]byte, error) { + return json.Marshal(struct { + Type BGPAttrType `json:"type"` + Value []*As4PathParam `json:"as_paths"` + }{ + Type: p.GetType(), + Value: p.Value, + }) +} + func NewPathAttributeAs4Path(value []*As4PathParam) *PathAttributeAs4Path { t := BGP_ATTR_TYPE_AS4_PATH return &PathAttributeAs4Path{ @@ -7371,6 +7367,22 @@ func (p *PathAttributeAs4Aggregator) Serialize(options ...*MarshallingOption) ([ return p.PathAttribute.Serialize(buf, options...) } +func (p *PathAttributeAs4Aggregator) String() string { + return fmt.Sprintf("{As4Aggregator: {AS: %d, Address: %s}}", p.Value.AS, p.Value.Address) +} + +func (p *PathAttributeAs4Aggregator) MarshalJSON() ([]byte, error) { + return json.Marshal(struct { + Type BGPAttrType `json:"type"` + AS uint32 `json:"as"` + Address string `json:"address"` + }{ + Type: p.GetType(), + AS: p.Value.AS, + Address: p.Value.Address.String(), + }) +} + func NewPathAttributeAs4Aggregator(as uint32, address string) *PathAttributeAs4Aggregator { t := BGP_ATTR_TYPE_AS4_AGGREGATOR return &PathAttributeAs4Aggregator{ @@ -7572,6 +7584,24 @@ func (p *PathAttributeTunnelEncap) Serialize(options ...*MarshallingOption) ([]b return p.PathAttribute.Serialize(buf, options...) } +func (p *PathAttributeTunnelEncap) String() string { + tlvList := make([]string, len(p.Value), len(p.Value)) + for i, v := range p.Value { + tlvList[i] = v.String() + } + return fmt.Sprintf("{TunnelEncap: %s}", strings.Join(tlvList, ", ")) +} + +func (p *PathAttributeTunnelEncap) MarshalJSON() ([]byte, error) { + return json.Marshal(struct { + Type BGPAttrType `json:"type"` + Value []*TunnelEncapTLV `json:"value"` + }{ + Type: p.Type, + Value: p.Value, + }) +} + func NewPathAttributeTunnelEncap(value []*TunnelEncapTLV) *PathAttributeTunnelEncap { t := BGP_ATTR_TYPE_TUNNEL_ENCAP return &PathAttributeTunnelEncap{ -- cgit v1.2.3