summaryrefslogtreecommitdiffhomepage
path: root/packet
diff options
context:
space:
mode:
authorISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>2015-11-13 23:54:24 +0900
committerISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>2015-11-13 23:54:24 +0900
commitf78e1a91dd6f28e00e395bd44873942b7fc11132 (patch)
treeccc14064bbda7566797840373e15438663b56f3f /packet
parentfe61508edb29c3344c8e33eaa84a7d97d954b434 (diff)
packet: add String() to PathAttributeAsPath and PathAttributeAs4Path
Signed-off-by: ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>
Diffstat (limited to 'packet')
-rw-r--r--packet/bgp.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/packet/bgp.go b/packet/bgp.go
index db3c2093..977ca167 100644
--- a/packet/bgp.go
+++ b/packet/bgp.go
@@ -3285,12 +3285,26 @@ func NewPathAttributeOrigin(value uint8) *PathAttributeOrigin {
}
}
+type AsPathParamFormat struct {
+ start string
+ end string
+ separator string
+}
+
+var asPathParamFormatMap = map[uint8]*AsPathParamFormat{
+ BGP_ASPATH_ATTR_TYPE_SET: &AsPathParamFormat{"{", "}", ","},
+ BGP_ASPATH_ATTR_TYPE_SEQ: &AsPathParamFormat{"", "", " "},
+ BGP_ASPATH_ATTR_TYPE_CONFED_SET: &AsPathParamFormat{"(", ")", " "},
+ BGP_ASPATH_ATTR_TYPE_CONFED_SEQ: &AsPathParamFormat{"[", "]", ","},
+}
+
type AsPathParamInterface interface {
Serialize() ([]byte, error)
DecodeFromBytes([]byte) error
Len() int
ASLen() int
MarshalJSON() ([]byte, error)
+ String() string
}
type AsPathParam struct {
@@ -3344,6 +3358,22 @@ func (a *AsPathParam) ASLen() int {
return 0
}
+func (a *AsPathParam) String() string {
+ format, ok := asPathParamFormatMap[a.Type]
+ if !ok {
+ return fmt.Sprintf("%v", a.AS)
+ }
+ aspath := make([]string, 0, len(a.AS))
+ for _, asn := range a.AS {
+ aspath = append(aspath, fmt.Sprintf("%d", asn))
+ }
+ s := bytes.NewBuffer(make([]byte, 0, 32))
+ s.WriteString(format.start)
+ s.WriteString(strings.Join(aspath, format.separator))
+ s.WriteString(format.end)
+ return s.String()
+}
+
func (a *AsPathParam) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Type uint8 `json:"segment_type"`
@@ -3415,6 +3445,22 @@ func (a *As4PathParam) ASLen() int {
return 0
}
+func (a *As4PathParam) String() string {
+ format, ok := asPathParamFormatMap[a.Type]
+ if !ok {
+ return fmt.Sprintf("%v", a.AS)
+ }
+ aspath := make([]string, 0, len(a.AS))
+ for _, asn := range a.AS {
+ aspath = append(aspath, fmt.Sprintf("%d", asn))
+ }
+ s := bytes.NewBuffer(make([]byte, 0, 32))
+ s.WriteString(format.start)
+ s.WriteString(strings.Join(aspath, format.separator))
+ s.WriteString(format.end)
+ return s.String()
+}
+
func (a *As4PathParam) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Type uint8 `json:"segment_type"`
@@ -3538,6 +3584,14 @@ func (p *PathAttributeAsPath) Serialize() ([]byte, error) {
return p.PathAttribute.Serialize()
}
+func (p *PathAttributeAsPath) String() string {
+ params := make([]string, 0, len(p.Value))
+ for _, param := range p.Value {
+ params = append(params, param.String())
+ }
+ return strings.Join(params, " ")
+}
+
func (p *PathAttributeAsPath) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Type BGPAttrType `json:"type"`
@@ -5301,6 +5355,14 @@ func (p *PathAttributeAs4Path) Serialize() ([]byte, error) {
return p.PathAttribute.Serialize()
}
+func (p *PathAttributeAs4Path) String() string {
+ params := make([]string, 0, len(p.Value))
+ for _, param := range p.Value {
+ params = append(params, param.String())
+ }
+ return strings.Join(params, " ")
+}
+
func NewPathAttributeAs4Path(value []*As4PathParam) *PathAttributeAs4Path {
t := BGP_ATTR_TYPE_AS4_PATH
return &PathAttributeAs4Path{