From aa2bc7a86089b3fd30cf1ef956214bcb870ef9d4 Mon Sep 17 00:00:00 2001
From: Daniel Czerwonk <daniel@dan-nrw.de>
Date: Tue, 15 May 2018 16:47:35 +0200
Subject: [PATCH] added actions to manipulate next-hop and local-pref

---
 .../actions/set_local_pref_action_test.go     |  3 +-
 .../filter/actions/set_nexthop_action.go      | 28 +++++++++++++
 .../filter/actions/set_nexthop_action_test.go | 41 +++++++++++++++++++
 3 files changed, 70 insertions(+), 2 deletions(-)
 create mode 100644 routingtable/filter/actions/set_nexthop_action.go
 create mode 100644 routingtable/filter/actions/set_nexthop_action_test.go

diff --git a/routingtable/filter/actions/set_local_pref_action_test.go b/routingtable/filter/actions/set_local_pref_action_test.go
index a7914877..0f21a637 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 00000000..41200a84
--- /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 00000000..7501e6bd
--- /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)
+			}
+		})
+	}
+}
-- 
GitLab