Skip to content
Snippets Groups Projects
term.go 1.13 KiB
Newer Older
package filter

import (
	"github.com/bio-routing/bio-rd/net"
	"github.com/bio-routing/bio-rd/route"
)

type FilterAction interface {
	Do(p net.Prefix, pa *route.Path) (modPath *route.Path, reject bool)
}

// Term matches a path against a list of conditions and performs actions if it matches
type Term struct {
Daniel Czerwonk's avatar
Daniel Czerwonk committed
	from []*TermCondition
	then []FilterAction
}

// NewTerm creates a new term
Daniel Czerwonk's avatar
Daniel Czerwonk committed
func NewTerm(from []*TermCondition, then []FilterAction) *Term {
	t := &Term{
		from: from,
		then: then,
	}

	return t
}

// Process processes a path returning if the path should be rejected and returns a possible modified version of the path
func (t *Term) Process(p net.Prefix, pa *route.Path) (modPath *route.Path, reject bool) {
	orig := pa

	if len(t.from) == 0 {
		return t.processActions(p, pa)
	}

	for _, f := range t.from {
		if f.Matches(p, pa) {
			return t.processActions(p, pa)
		}
	}

Oliver Herms's avatar
Oliver Herms committed
	return orig, false
}

func (t *Term) processActions(p net.Prefix, pa *route.Path) (modPath *route.Path, reject bool) {
	modPath = pa

	for _, action := range t.then {
		modPath, reject = action.Do(p, modPath)
		if reject {
			continue
		}
	}

	return modPath, reject
}