From b6b2fdcb8a421adee68307de8af7ba9b67e94200 Mon Sep 17 00:00:00 2001
From: Daniel Czerwonk <daniel@dan-nrw.de>
Date: Tue, 15 May 2018 12:05:54 +0200
Subject: [PATCH] added local pref modification to filters

---
 .../filter/actions/set_local_pref_action.go   | 28 +++++++++++
 .../actions/set_local_pref_action_test.go     | 47 +++++++++++++++++++
 2 files changed, 75 insertions(+)
 create mode 100644 routingtable/filter/actions/set_local_pref_action.go
 create mode 100644 routingtable/filter/actions/set_local_pref_action_test.go

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 00000000..1b59be48
--- /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 00000000..a7914877
--- /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
+}
-- 
GitLab