Skip to content
Snippets Groups Projects
Commit 16a5c7fa authored by Daniel Czerwonk's avatar Daniel Czerwonk
Browse files

added action to add large communities

parent 98e8b40b
No related branches found
No related tags found
No related merge requests found
package actions
import (
"strings"
"github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/protocols/bgp/packet"
"github.com/bio-routing/bio-rd/route"
)
type AddLargeCommunityAction struct {
communities []*packet.LargeCommunity
}
func NewAddLargeCommunityAction(coms []*packet.LargeCommunity) *AddLargeCommunityAction {
return &AddLargeCommunityAction{
communities: coms,
}
}
func (a *AddLargeCommunityAction) Do(p net.Prefix, pa *route.Path) (modPath *route.Path, reject bool) {
if pa.BGPPath == nil || len(a.communities) == 0 {
return pa, false
}
modified := pa.Copy()
for _, com := range a.communities {
modified.BGPPath.LargeCommunities = modified.BGPPath.LargeCommunities + " " + com.String()
}
modified.BGPPath.LargeCommunities = strings.TrimLeft(modified.BGPPath.LargeCommunities, " ")
return modified, false
}
package actions
import (
"testing"
"github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/protocols/bgp/packet"
"github.com/bio-routing/bio-rd/route"
"github.com/stretchr/testify/assert"
)
func TestAddingLargeCommunities(t *testing.T) {
tests := []struct {
name string
current string
communities []*packet.LargeCommunity
expected string
}{
{
name: "add one to empty",
communities: []*packet.LargeCommunity{
&packet.LargeCommunity{
GlobalAdministrator: 1,
DataPart1: 2,
DataPart2: 3,
},
},
expected: "(1,2,3)",
},
{
name: "add one to existing",
current: "(5,6,7)",
communities: []*packet.LargeCommunity{
&packet.LargeCommunity{
GlobalAdministrator: 1,
DataPart1: 2,
DataPart2: 3,
},
},
expected: "(5,6,7) (1,2,3)",
},
{
name: "add two to existing",
current: "(5,6,7)",
communities: []*packet.LargeCommunity{
&packet.LargeCommunity{
GlobalAdministrator: 1,
DataPart1: 2,
DataPart2: 3,
},
&packet.LargeCommunity{
GlobalAdministrator: 7,
DataPart1: 8,
DataPart2: 9,
},
},
expected: "(5,6,7) (1,2,3) (7,8,9)",
},
}
for _, test := range tests {
t.Run(test.name, func(te *testing.T) {
p := &route.Path{
BGPPath: &route.BGPPath{
LargeCommunities: test.current,
},
}
a := NewAddLargeCommunityAction(test.communities)
modPath, _ := a.Do(net.Prefix{}, p)
assert.Equal(te, test.expected, modPath.BGPPath.LargeCommunities)
})
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment