Skip to content
Snippets Groups Projects
Commit 13eaf56b authored by Christoph Petrausch's avatar Christoph Petrausch Committed by Daniel Czerwonk
Browse files

Make it possible to unregister vrf (#197)

* Add Unregister function to VRF to remove VRF from global registry

* Add test for Unregister
parent efa7c15d
No related branches found
No related tags found
No related merge requests found
......@@ -26,7 +26,7 @@ type VRF struct {
ribNames map[string]*locRIB.LocRIB
}
// New creates a new VRF
// New creates a new VRF. The VRF is registered automatically to the global VRF registry.
func New(name string) (*VRF, error) {
v := newUntrackedVRF(name)
v.CreateIPv4UnicastLocRIB("inet.0")
......@@ -89,6 +89,11 @@ func (v *VRF) Name() string {
return v.name
}
// Unregister removes this VRF from the global registry.
func (v *VRF) Unregister() {
globalRegistry.unregisterVRF(v)
}
func (v *VRF) ribForAddressFamily(family addressFamily) *locRIB.LocRIB {
v.mu.Lock()
defer v.mu.Unlock()
......
......@@ -13,11 +13,14 @@ func init() {
}
}
// vrfRegistry holds a reference to all active VRFs. Every VRF have to have a different name.
type vrfRegistry struct {
vrfs map[string]*VRF
mu sync.Mutex
}
// registerVRF adds the given VRF from the global registry.
// An error is returned if there is already a VRF registered with the same name.
func (r *vrfRegistry) registerVRF(v *VRF) error {
r.mu.Lock()
defer r.mu.Unlock()
......@@ -30,3 +33,11 @@ func (r *vrfRegistry) registerVRF(v *VRF) error {
r.vrfs[v.name] = v
return nil
}
// unregisterVRF removes the given VRF from the global registry.
func (r *vrfRegistry) unregisterVRF(v *VRF) {
r.mu.Lock()
defer r.mu.Unlock()
delete(r.vrfs, v.name)
}
......@@ -53,3 +53,21 @@ func TestName(t *testing.T) {
v := newUntrackedVRF("foo")
assert.Equal(t, "foo", v.Name())
}
func TestUnregister(t *testing.T) {
vrfName := "registeredVRF"
v, err := New(vrfName)
assert.Nil(t, err, "error must be nil on first invokation")
_, err = New(vrfName)
assert.NotNil(t, err, "error must not be nil on second invokation")
_, found := globalRegistry.vrfs[vrfName]
assert.True(t, found, "vrf must be in global registry")
v.Unregister()
_, found = globalRegistry.vrfs[vrfName]
assert.False(t, found, "vrf must not be in global registry")
}
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