diff --git a/routingtable/filter/actions/set_local_pref_action.go b/routingtable/filter/actions/set_local_pref_action.go new file mode 100644 index 0000000000000000000000000000000000000000..1b59be48e16b844aae069c5795d7acc691e87612 --- /dev/null +++ b/routingtable/filter/actions/set_local_pref_action.go @@ -0,0 +1,28 @@ +package actions + +import ( + "github.com/bio-routing/bio-rd/net" + "github.com/bio-routing/bio-rd/route" + "github.com/bio-routing/bio-rd/routingtable/filter" +) + +type setLocalPrefAction struct { + pref uint32 +} + +func NewSetLocalPrefAction(pref uint32) filter.FilterAction { + return &setLocalPrefAction{ + pref: pref, + } +} + +func (a *setLocalPrefAction) Do(p net.Prefix, pa *route.Path) (modPath *route.Path, reject bool) { + if pa.BGPPath == nil { + return pa, false + } + + modified := *pa + modified.BGPPath.LocalPref = a.pref + + return &modified, false +} diff --git a/routingtable/filter/actions/set_local_pref_action_test.go b/routingtable/filter/actions/set_local_pref_action_test.go new file mode 100644 index 0000000000000000000000000000000000000000..a79148771232f8a28078af6b0739ba4861721ff7 --- /dev/null +++ b/routingtable/filter/actions/set_local_pref_action_test.go @@ -0,0 +1,47 @@ +package actions + +import ( + "testing" + + "github.com/bio-routing/bio-rd/net" + "github.com/bio-routing/bio-rd/route" + "github.com/stretchr/testify/assert" +) + +func TestSetLocalPref(t *testing.T) { + tests := []struct { + name string + bgpPath *route.BGPPath + expectedLocalPref uint32 + }{ + { + name: "BGPPath is nil", + expectedLocalPref: 0, + }, + { + name: "modify path", + bgpPath: &route.BGPPath{ + LocalPref: 100, + }, + expectedLocalPref: 150, + }, + } + + for _, test := range tests { + t.Run(test.name, func(te *testing.T) { + a := NewSetLocalPrefAction(150) + p, _ := a.Do(net.NewPfx(strAddr("10.0.0.0"), 8), &route.Path{ + BGPPath: test.bgpPath, + }) + + if test.expectedLocalPref > 0 { + assert.Equal(te, test.expectedLocalPref, p.BGPPath.LocalPref) + } + }) + } +} + +func strAddr(s string) uint32 { + ret, _ := net.StrToAddr(s) + return ret +}