diff --git a/routingtable/filter/actions/set_local_pref_action_test.go b/routingtable/filter/actions/set_local_pref_action_test.go index a79148771232f8a28078af6b0739ba4861721ff7..0f21a637379bf7c0018db89a0fab0a46deadd5b0 100644 --- a/routingtable/filter/actions/set_local_pref_action_test.go +++ b/routingtable/filter/actions/set_local_pref_action_test.go @@ -15,8 +15,7 @@ func TestSetLocalPref(t *testing.T) { expectedLocalPref uint32 }{ { - name: "BGPPath is nil", - expectedLocalPref: 0, + name: "BGPPath is nil", }, { name: "modify path", diff --git a/routingtable/filter/actions/set_nexthop_action.go b/routingtable/filter/actions/set_nexthop_action.go new file mode 100644 index 0000000000000000000000000000000000000000..41200a845a6f4ba6c31f9e30e202971eca1be9cb --- /dev/null +++ b/routingtable/filter/actions/set_nexthop_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 setNextHopAction struct { + addr uint32 +} + +func NewSetNextHopAction(addr uint32) filter.FilterAction { + return &setNextHopAction{ + addr: addr, + } +} + +func (a *setNextHopAction) Do(p net.Prefix, pa *route.Path) (modPath *route.Path, reject bool) { + if pa.BGPPath == nil { + return pa, false + } + + modified := *pa + modified.BGPPath.NextHop = a.addr + + return &modified, false +} diff --git a/routingtable/filter/actions/set_nexthop_action_test.go b/routingtable/filter/actions/set_nexthop_action_test.go new file mode 100644 index 0000000000000000000000000000000000000000..7501e6bdcc388c65577cea8ce0a425acf8b7d3e3 --- /dev/null +++ b/routingtable/filter/actions/set_nexthop_action_test.go @@ -0,0 +1,41 @@ +package actions + +import ( + "testing" + + "github.com/bio-routing/bio-rd/net" + "github.com/bio-routing/bio-rd/route" + "github.com/stretchr/testify/assert" +) + +func TestSetNextHopTest(t *testing.T) { + tests := []struct { + name string + bgpPath *route.BGPPath + expected uint32 + }{ + { + name: "BGPPath is nil", + }, + { + name: "modify path", + bgpPath: &route.BGPPath{ + NextHop: strAddr("100.64.2.1"), + }, + expected: strAddr("100.64.2.1"), + }, + } + + for _, test := range tests { + t.Run(test.name, func(te *testing.T) { + a := NewSetNextHopAction(strAddr("100.64.2.1")) + p, _ := a.Do(net.NewPfx(strAddr("10.0.0.0"), 8), &route.Path{ + BGPPath: test.bgpPath, + }) + + if test.expected > 0 { + assert.Equal(te, test.expected, p.BGPPath.NextHop) + } + }) + } +}