diff options
Diffstat (limited to 'table')
-rw-r--r-- | table/path.go | 26 | ||||
-rw-r--r-- | table/path_test.go | 14 |
2 files changed, 40 insertions, 0 deletions
diff --git a/table/path.go b/table/path.go index 3b0fc40d..0453139a 100644 --- a/table/path.go +++ b/table/path.go @@ -713,6 +713,32 @@ func (path *Path) RemovePrivateAS(localAS uint32, option config.RemovePrivateAsO return } +func (path *Path) ReplaceAS(localAS, peerAS uint32) *Path { + original := path.GetAsPath() + if original == nil { + return 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 { + if as == peerAS { + as = localAS + changed = true + } + newASParam = append(newASParam, as) + } + newASParams = append(newASParams, bgp.NewAs4PathParam(asParam.Type, newASParam)) + } + if changed { + path = path.Clone(path.IsWithdraw) + path.setPathAttr(bgp.NewPathAttributeAsPath(newASParams)) + } + return path +} + func (path *Path) GetCommunities() []uint32 { communityList := []uint32{} if attr := path.getPathAttr(bgp.BGP_ATTR_TYPE_COMMUNITIES); attr != nil { diff --git a/table/path_test.go b/table/path_test.go index 29a296ac..ea4e23d5 100644 --- a/table/path_test.go +++ b/table/path_test.go @@ -353,3 +353,17 @@ func TestRemovePrivateAS(t *testing.T) { assert.Equal(t, list[2], uint32(1)) assert.Equal(t, list[3], uint32(2)) } + +func TestReplaceAS(t *testing.T) { + aspathParam := []bgp.AsPathParamInterface{bgp.NewAs4PathParam(2, []uint32{64512, 64513, 1, 2})} + aspath := bgp.NewPathAttributeAsPath(aspathParam) + nlri := bgp.NewIPAddrPrefix(24, "30.30.30.0") + path := NewPath(nil, nlri, false, []bgp.PathAttributeInterface{aspath}, time.Now(), false) + path = path.ReplaceAS(10, 1) + list := path.GetAsList() + assert.Equal(t, len(list), 4) + assert.Equal(t, list[0], uint32(64512)) + assert.Equal(t, list[1], uint32(64513)) + assert.Equal(t, list[2], uint32(10)) + assert.Equal(t, list[3], uint32(2)) +} |