diff options
author | IWASE Yusuke <iwase.yusuke0@gmail.com> | 2018-02-27 09:49:35 +0900 |
---|---|---|
committer | IWASE Yusuke <iwase.yusuke0@gmail.com> | 2018-04-03 09:16:41 +0900 |
commit | 264bd0ca126bf721687764332a01315d56e5b671 (patch) | |
tree | 05a0eb911e99fcba2a621fde34f6fc749f130d1d /table/path.go | |
parent | a25522edddf05653bef26c5ff220f34f7354791f (diff) |
packet/bgp: Getter functions for AS segment
This patch adds getter functions for the segment type and AS list of
each segment without using type assertion.
Signed-off-by: IWASE Yusuke <iwase.yusuke0@gmail.com>
Diffstat (limited to 'table/path.go')
-rw-r--r-- | table/path.go | 85 |
1 files changed, 44 insertions, 41 deletions
diff --git a/table/path.go b/table/path.go index a04e0d66..de60e615 100644 --- a/table/path.go +++ b/table/path.go @@ -196,10 +196,10 @@ func (path *Path) IsEOR() bool { func cloneAsPath(asAttr *bgp.PathAttributeAsPath) *bgp.PathAttributeAsPath { newASparams := make([]bgp.AsPathParamInterface, len(asAttr.Value)) for i, param := range asAttr.Value { - asParam := param.(*bgp.As4PathParam) - as := make([]uint32, len(asParam.AS)) - copy(as, asParam.AS) - newASparams[i] = bgp.NewAs4PathParam(asParam.Type, as) + asList := param.GetAS() + as := make([]uint32, len(asList)) + copy(as, asList) + newASparams[i] = bgp.NewAs4PathParam(param.GetType(), as) } return bgp.NewPathAttributeAsPath(newASparams) } @@ -439,11 +439,11 @@ func (path *Path) GetSourceAs() uint32 { if len(asPathParam) == 0 { return 0 } - asPath := asPathParam[len(asPathParam)-1].(*bgp.As4PathParam) - if asPath.Num == 0 { + asList := asPathParam[len(asPathParam)-1].GetAS() + if len(asList) == 0 { return 0 } - return asPath.AS[asPath.Num-1] + return asList[len(asList)-1] } return 0 } @@ -631,14 +631,15 @@ func (path *Path) GetAsPathLen() int { func (path *Path) GetAsString() string { s := bytes.NewBuffer(make([]byte, 0, 64)) if aspath := path.GetAsPath(); aspath != nil { - for i, paramIf := range aspath.Value { - segment := paramIf.(*bgp.As4PathParam) + for i, param := range aspath.Value { + segType := param.GetType() + asList := param.GetAS() if i != 0 { s.WriteString(" ") } sep := " " - switch segment.Type { + switch segType { case bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SEQ: s.WriteString("(") case bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SET: @@ -648,13 +649,13 @@ func (path *Path) GetAsString() string { s.WriteString("{") sep = "," } - for j, as := range segment.AS { + for j, as := range asList { s.WriteString(fmt.Sprintf("%d", as)) - if j != len(segment.AS)-1 { + if j != len(asList)-1 { s.WriteString(sep) } } - switch segment.Type { + switch segType { case bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SEQ: s.WriteString(")") case bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SET: @@ -668,26 +669,26 @@ func (path *Path) GetAsString() string { } func (path *Path) GetAsList() []uint32 { - return path.getAsListofSpecificType(true, true) + return path.getAsListOfSpecificType(true, true) } func (path *Path) GetAsSeqList() []uint32 { - return path.getAsListofSpecificType(true, false) + return path.getAsListOfSpecificType(true, false) } -func (path *Path) getAsListofSpecificType(getAsSeq, getAsSet bool) []uint32 { +func (path *Path) getAsListOfSpecificType(getAsSeq, getAsSet bool) []uint32 { asList := []uint32{} if aspath := path.GetAsPath(); aspath != nil { - for _, paramIf := range aspath.Value { - segment := paramIf.(*bgp.As4PathParam) - if getAsSeq && segment.Type == bgp.BGP_ASPATH_ATTR_TYPE_SEQ { - asList = append(asList, segment.AS...) + for _, param := range aspath.Value { + segType := param.GetType() + if getAsSeq && segType == bgp.BGP_ASPATH_ATTR_TYPE_SEQ { + asList = append(asList, param.GetAS()...) continue } - if getAsSet && segment.Type == bgp.BGP_ASPATH_ATTR_TYPE_SET { - asList = append(asList, segment.AS...) + if getAsSet && segType == bgp.BGP_ASPATH_ATTR_TYPE_SET { + asList = append(asList, param.GetAS()...) } else { asList = append(asList, 0) } @@ -757,7 +758,7 @@ func (path *Path) PrependAsn(asn uint32, repeat uint8, confed bool) { original := path.GetAsPath() asns := make([]uint32, repeat) - for i, _ := range asns { + for i := range asns { asns[i] = asn } @@ -769,13 +770,14 @@ func (path *Path) PrependAsn(asn uint32, repeat uint8, confed bool) { } if len(asPath.Value) > 0 { - fst := asPath.Value[0].(*bgp.As4PathParam) - if fst.Type == segType { - if len(fst.AS)+int(repeat) > 255 { - repeat = uint8(255 - len(fst.AS)) + param := asPath.Value[0] + asList := param.GetAS() + if param.GetType() == segType { + if int(repeat)+len(asList) > 255 { + repeat = uint8(255 - len(asList)) } - fst.AS = append(asns[:int(repeat)], fst.AS...) - fst.Num += repeat + newAsList := append(asns[:int(repeat)], asList...) + asPath.Value[0] = bgp.NewAs4PathParam(segType, newAsList) asns = asns[int(repeat):] } } @@ -800,9 +802,9 @@ func (path *Path) RemovePrivateAS(localAS uint32, option config.RemovePrivateAsO case config.REMOVE_PRIVATE_AS_OPTION_ALL, config.REMOVE_PRIVATE_AS_OPTION_REPLACE: newASParams := make([]bgp.AsPathParamInterface, 0, len(original.Value)) for _, param := range original.Value { - asParam := param.(*bgp.As4PathParam) - newASParam := make([]uint32, 0, len(asParam.AS)) - for _, as := range asParam.AS { + asList := param.GetAS() + newASParam := make([]uint32, 0, len(asList)) + for _, as := range asList { if isPrivateAS(as) { if option == config.REMOVE_PRIVATE_AS_OPTION_REPLACE { newASParam = append(newASParam, localAS) @@ -812,7 +814,7 @@ func (path *Path) RemovePrivateAS(localAS uint32, option config.RemovePrivateAsO } } if len(newASParam) > 0 { - newASParams = append(newASParams, bgp.NewAs4PathParam(asParam.Type, newASParam)) + newASParams = append(newASParams, bgp.NewAs4PathParam(param.GetType(), newASParam)) } } path.setPathAttr(bgp.NewPathAttributeAsPath(newASParams)) @@ -826,10 +828,10 @@ func (path *Path) removeConfedAs() { return } newAsParams := make([]bgp.AsPathParamInterface, 0, len(original.Value)) - for _, v := range original.Value { - p := v.(*bgp.As4PathParam) - if p.Type == bgp.BGP_ASPATH_ATTR_TYPE_SEQ || p.Type == bgp.BGP_ASPATH_ATTR_TYPE_SET { - newAsParams = append(newAsParams, p) + for _, param := range original.Value { + switch param.GetType() { + case bgp.BGP_ASPATH_ATTR_TYPE_SEQ, bgp.BGP_ASPATH_ATTR_TYPE_SET: + newAsParams = append(newAsParams, param) } } path.setPathAttr(bgp.NewPathAttributeAsPath(newAsParams)) @@ -843,16 +845,17 @@ func (path *Path) ReplaceAS(localAS, peerAS uint32) *Path { newASParams := make([]bgp.AsPathParamInterface, 0, len(original.Value)) changed := false for _, param := range original.Value { - asParam := param.(*bgp.As4PathParam) - newASParam := make([]uint32, 0, len(asParam.AS)) - for _, as := range asParam.AS { + segType := param.GetType() + asList := param.GetAS() + newASParam := make([]uint32, 0, len(asList)) + for _, as := range asList { if as == peerAS { as = localAS changed = true } newASParam = append(newASParam, as) } - newASParams = append(newASParams, bgp.NewAs4PathParam(asParam.Type, newASParam)) + newASParams = append(newASParams, bgp.NewAs4PathParam(segType, newASParam)) } if changed { path = path.Clone(path.IsWithdraw) |