Skip to content
Snippets Groups Projects
adj_rib_in.go 1.97 KiB
Newer Older
package adjRIBIn

import (
Oliver Herms's avatar
Oliver Herms committed
	"sync"

	"github.com/bio-routing/bio-rd/net"
	"github.com/bio-routing/bio-rd/route"
	"github.com/bio-routing/bio-rd/routingtable"
	log "github.com/sirupsen/logrus"
)

// AdjRIBIn represents an Adjacency RIB In as described in RFC4271
type AdjRIBIn struct {
	routingtable.ClientManager
Oliver Herms's avatar
Oliver Herms committed
	rt *routingtable.RoutingTable
Oliver Herms's avatar
Oliver Herms committed
	mu sync.RWMutex
Oliver Herms's avatar
Oliver Herms committed
// New creates a new Adjacency RIB In
func New() *AdjRIBIn {
		rt: routingtable.NewRoutingTable(),
	}
	a.ClientManager = routingtable.NewClientManager(a)
	return a
}

// UpdateNewClient sends current state to a new client
func (a *AdjRIBIn) UpdateNewClient(client routingtable.RouteTableClient) error {
	a.mu.RLock()
	defer a.mu.RUnlock()

	routes := a.rt.Dump()
	for _, route := range routes {
		paths := route.Paths()
		for _, path := range paths {

			err := client.AddPath(route.Prefix(), path)
			if err != nil {
				log.WithField("Sender", "AdjRIBOutAddPath").WithError(err).Error("Could not send update to client")
			}
		}
	}
	return nil
}

// AddPath replaces the path for prefix `pfx`. If the prefix doesn't exist it is added.
func (a *AdjRIBIn) AddPath(pfx net.Prefix, p *route.Path) error {
Oliver Herms's avatar
Oliver Herms committed
	a.mu.Lock()
	defer a.mu.Unlock()

	oldPaths := a.rt.ReplacePath(pfx, p)
Oliver Herms's avatar
Oliver Herms committed
	a.removePathsFromClients(pfx, oldPaths)

	for _, client := range a.ClientManager.Clients() {
		client.AddPath(pfx, p)
	}
	return nil
}

// RemovePath removes the path for prefix `pfx`
func (a *AdjRIBIn) RemovePath(pfx net.Prefix, p *route.Path) bool {
Oliver Herms's avatar
Oliver Herms committed
	a.mu.Lock()
	defer a.mu.Unlock()

Oliver Herms's avatar
Oliver Herms committed
	r := a.rt.Get(pfx)
	if r == nil {
Oliver Herms's avatar
Oliver Herms committed
	oldPaths := r.Paths()
	for _, path := range oldPaths {
		a.rt.RemovePath(pfx, path)
Oliver Herms's avatar
Oliver Herms committed
	a.removePathsFromClients(pfx, oldPaths)
Oliver Herms's avatar
Oliver Herms committed

func (a *AdjRIBIn) removePathsFromClients(pfx net.Prefix, paths []*route.Path) {
	for _, path := range paths {
		for _, client := range a.ClientManager.Clients() {
			client.RemovePath(pfx, path)
		}
	}
}
Oliver Herms's avatar
Oliver Herms committed

func (a *AdjRIBIn) RT() *routingtable.RoutingTable {
	return a.rt
}