diff options
author | ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp> | 2015-11-13 23:54:24 +0900 |
---|---|---|
committer | ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp> | 2015-11-13 23:54:24 +0900 |
commit | f78e1a91dd6f28e00e395bd44873942b7fc11132 (patch) | |
tree | ccc14064bbda7566797840373e15438663b56f3f | |
parent | fe61508edb29c3344c8e33eaa84a7d97d954b434 (diff) |
packet: add String() to PathAttributeAsPath and PathAttributeAs4Path
Signed-off-by: ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>
-rw-r--r-- | gobgp/cmd/neighbor.go | 41 | ||||
-rw-r--r-- | packet/bgp.go | 62 |
2 files changed, 63 insertions, 40 deletions
diff --git a/gobgp/cmd/neighbor.go b/gobgp/cmd/neighbor.go index 170e6726..a3181ba0 100644 --- a/gobgp/cmd/neighbor.go +++ b/gobgp/cmd/neighbor.go @@ -16,7 +16,6 @@ package cmd import ( - "bytes" "encoding/json" "fmt" api "github.com/osrg/gobgp/api" @@ -246,44 +245,6 @@ func showRoute(pathList []*Path, showAge, showBest, showLabel, isMonitor, printH maxNexthopLen := 20 maxAsPathLen := 20 maxLabelLen := 10 - aspath := func(a bgp.PathAttributeInterface) string { - - delimiter := make(map[uint8]*AsPathFormat) - delimiter[bgp.BGP_ASPATH_ATTR_TYPE_SET] = &AsPathFormat{"{", "}", ","} - delimiter[bgp.BGP_ASPATH_ATTR_TYPE_SEQ] = &AsPathFormat{"", "", " "} - delimiter[bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SEQ] = &AsPathFormat{"(", ")", " "} - delimiter[bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SET] = &AsPathFormat{"[", "]", ","} - - var segments []string = make([]string, 0) - aspaths := a.(*bgp.PathAttributeAsPath).Value - for _, aspath := range aspaths { - var t uint8 - var asnsStr []string - switch aspath.(type) { - case *bgp.AsPathParam: - a := aspath.(*bgp.AsPathParam) - t = a.Type - for _, asn := range a.AS { - asnsStr = append(asnsStr, fmt.Sprintf("%d", asn)) - } - case *bgp.As4PathParam: - a := aspath.(*bgp.As4PathParam) - t = a.Type - for _, asn := range a.AS { - asnsStr = append(asnsStr, fmt.Sprintf("%d", asn)) - } - } - s := bytes.NewBuffer(make([]byte, 0, 64)) - start := delimiter[t].start - end := delimiter[t].end - separator := delimiter[t].separator - s.WriteString(start) - s.WriteString(strings.Join(asnsStr, separator)) - s.WriteString(end) - segments = append(segments, s.String()) - } - return strings.Join(segments, " ") - } for _, p := range pathList { var nexthop string @@ -302,7 +263,7 @@ func showRoute(pathList []*Path, showAge, showBest, showLabel, isMonitor, printH nexthop = "fictitious" } case bgp.BGP_ATTR_TYPE_AS_PATH: - aspathstr = aspath(a) + aspathstr = a.String() case bgp.BGP_ATTR_TYPE_AS4_PATH: continue default: 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{ |