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

added community serialization

parent f48a4b5a
No related branches found
No related tags found
No related merge requests found
......@@ -446,6 +446,8 @@ func (pa *PathAttribute) serialize(buf *bytes.Buffer) uint8 {
pathAttrLen = pa.serializeAtomicAggregate(buf)
case AggregatorAttr:
pathAttrLen = pa.serializeAggregator(buf)
case CommunitiesAttr:
pathAttrLen = pa.serializeCommunities(buf)
case LargeCommunityAttr:
pathAttrLen = pa.serializeLargeCommunities(buf)
}
......@@ -543,6 +545,30 @@ func (pa *PathAttribute) serializeAggregator(buf *bytes.Buffer) uint8 {
return 5
}
func (pa *PathAttribute) serializeCommunities(buf *bytes.Buffer) uint8 {
coms := pa.Value.([]uint32)
if len(coms) == 0 {
return 0
}
attrFlags := uint8(0)
attrFlags = setOptional(attrFlags)
attrFlags = setTransitive(attrFlags)
attrFlags = setPartial(attrFlags)
buf.WriteByte(attrFlags)
buf.WriteByte(CommunitiesAttr)
length := uint8(CommunityLen * len(coms))
buf.WriteByte(length)
for _, com := range coms {
buf.Write(convert.Uint32Byte(com))
}
return length
}
func (pa *PathAttribute) serializeLargeCommunities(buf *bytes.Buffer) uint8 {
coms := pa.Value.([]LargeCommunity)
if len(coms) == 0 {
......
......@@ -1335,6 +1335,53 @@ func TestSerializeLargeCommunities(t *testing.T) {
}
}
func TestSerializeCommunities(t *testing.T) {
tests := []struct {
name string
input *PathAttribute
expected []byte
expectedLen uint8
}{
{
name: "2 communities",
input: &PathAttribute{
TypeCode: LargeCommunityAttr,
Value: []uint32{
131080, 16778241,
},
},
expected: []byte{
0xe0, // Attribute flags
8, // Type
8, // Length
0, 2, 0, 8, 1, 0, 4, 1, // Communities (2,8), (256,1025)
},
expectedLen: 8,
},
{
name: "empty list of communities",
input: &PathAttribute{
TypeCode: CommunitiesAttr,
Value: []uint32{},
},
expected: []byte{},
expectedLen: 0,
},
}
for _, test := range tests {
t.Run(test.name, func(te *testing.T) {
buf := bytes.NewBuffer([]byte{})
n := test.input.serializeCommunities(buf)
if n != test.expectedLen {
t.Fatalf("Unexpected length for test %q: %d", test.name, n)
}
assert.Equal(t, test.expected, buf.Bytes())
})
}
}
func TestSerialize(t *testing.T) {
tests := []struct {
name string
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment