added pool for ext. communities

This commit is contained in:
Annika Hannig 2022-11-16 13:30:44 +01:00
parent 76c4f1fb6f
commit afc402f3cc
3 changed files with 77 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package pools
import (
"log"
"reflect"
"sync"
@ -42,3 +43,40 @@ func (p *CommunitiesPool) Acquire(communities []api.Community) []api.Community {
}
return p.root.traverse(communities, ids).([]api.Community)
}
// NewExtCommunitiesPool creates a new pool for extended communities
func NewExtCommunitiesPool() *CommunitiesPool {
return &CommunitiesPool{
communitiesRoot: NewNode([]int{}),
root: NewNode([]api.ExtCommunity{}),
}
}
// AcquireExt a list of ext bgp communities
func (p *CommunitiesPool) AcquireExt(communities []api.ExtCommunity) []api.ExtCommunity {
p.Lock()
defer p.Unlock()
// Make identification list
ids := make([]int, len(communities))
for i, comm := range communities {
if len(comm) != 3 {
log.Println("ERROR: malformed ext. bgp community:", comm)
continue
}
r := 0 // RO
if comm[0].(string) == "rt" {
r = 1
}
icomm := []int{r, comm[1].(int), comm[2].(int)}
// get community identifier
commPtr := p.communitiesRoot.traverse(icomm, icomm)
addr := reflect.ValueOf(commPtr).UnsafePointer()
ids[i] = int(uintptr(addr))
}
if len(ids) == 0 {
return p.root.ptr.([]api.ExtCommunity)
}
return p.root.traverse(communities, ids).([]api.ExtCommunity)
}

View File

@ -42,3 +42,38 @@ func TestAcquireCommunities(t *testing.T) {
fmt.Printf("pc1: %p, pc2: %p, pc3: %p\n", pc1, pc2, pc3)
}
func TestAcquireExtCommunities(t *testing.T) {
c1 := []api.ExtCommunity{
{"ro", 5, 1},
{"ro", 5, 2},
{"rt", 51, 1},
}
c2 := []api.ExtCommunity{
{"ro", 5, 1},
{"ro", 5, 2},
{"rt", 51, 1},
}
c3 := []api.ExtCommunity{
{"ro", 6, 1},
{"rt", 6, 2},
{"rt", 1, 1},
}
p := NewExtCommunitiesPool()
pc1 := p.AcquireExt(c1)
pc2 := p.AcquireExt(c2)
pc3 := p.AcquireExt(c3)
if fmt.Sprintf("%p", c1) == fmt.Sprintf("%p", c2) {
t.Error("expected c1 !== c2")
}
if fmt.Sprintf("%p", pc1) != fmt.Sprintf("%p", pc2) {
t.Error("expected pc1 == pc2")
}
fmt.Printf("c1: %p, c2: %p, c3: %p\n", c1, c2, c3)
fmt.Printf("pc1: %p, pc2: %p, pc3: %p\n", pc1, pc2, pc3)
}

View File

@ -37,6 +37,9 @@ var Types *StringListPool
// Communities store a list of BGP communities
var Communities *CommunitiesPool
// ExtCommunities stores a list of extended communities
var ExtCommunities *CommunitiesPool
// LargeCommunities store a list of large BGP communities
var LargeCommunities *CommunitiesPool
@ -54,5 +57,6 @@ func init() {
ASPaths = NewIntListPool()
Types = NewStringListPool()
Communities = NewCommunitiesPool()
ExtCommunities = NewExtCommunitiesPool()
LargeCommunities = NewCommunitiesPool()
}