From 44c35b825dab51770769fafade8923d1487277e8 Mon Sep 17 00:00:00 2001
From: Oliver Herms <oliver.herms@exaring.de>
Date: Sat, 5 May 2018 02:53:03 +0200
Subject: [PATCH] Deduplicate core

---
 routingtable/adjRIBIn/adj_rib_in.go      | 25 ++++++++++++++----------
 routingtable/adjRIBIn/adj_rib_in_test.go |  7 ++-----
 2 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/routingtable/adjRIBIn/adj_rib_in.go b/routingtable/adjRIBIn/adj_rib_in.go
index 446d71e0..01137279 100644
--- a/routingtable/adjRIBIn/adj_rib_in.go
+++ b/routingtable/adjRIBIn/adj_rib_in.go
@@ -22,25 +22,30 @@ func NewAdjRIBIn() *AdjRIBIn {
 // AddPath replaces the path for prefix `pfx`. If the prefix doesn't exist it is added.
 func (a *AdjRIBIn) AddPath(pfx net.Prefix, p *route.Path) error {
 	oldPaths := a.rt.ReplacePath(pfx, p)
-
-	for _, oldPath := range oldPaths {
-		for _, client := range a.ClientManager.Clients() {
-			client.RemovePath(pfx, oldPath)
-		}
-	}
-
+	a.removePathsFromClients(pfx, oldPaths)
 	return nil
 }
 
 // RemovePath removes the path for prefix `pfx`
 func (a *AdjRIBIn) RemovePath(pfx net.Prefix, p *route.Path) error {
-	if !a.rt.RemovePath(pfx, p) {
+	r := a.rt.Get(pfx)
+	if r == nil {
 		return nil
 	}
 
-	for _, client := range a.ClientManager.Clients() {
-		client.RemovePath(pfx, p)
+	oldPaths := r.Paths()
+	for _, path := range oldPaths {
+		a.rt.RemovePath(pfx, path)
 	}
 
+	a.removePathsFromClients(pfx, oldPaths)
 	return nil
 }
+
+func (a *AdjRIBIn) removePathsFromClients(pfx net.Prefix, paths []*route.Path) {
+	for _, path := range paths {
+		for _, client := range a.ClientManager.Clients() {
+			client.RemovePath(pfx, path)
+		}
+	}
+}
diff --git a/routingtable/adjRIBIn/adj_rib_in_test.go b/routingtable/adjRIBIn/adj_rib_in_test.go
index a6c8989e..4ae96263 100644
--- a/routingtable/adjRIBIn/adj_rib_in_test.go
+++ b/routingtable/adjRIBIn/adj_rib_in_test.go
@@ -200,12 +200,9 @@ func TestRemovePath(t *testing.T) {
 			if mc.removePathParams.pfx != test.removePfx {
 				t.Errorf("Test %q failed: Call to RemovePath did not propagate prefix properly: Got: %s Want: %s", test.name, mc.removePathParams.pfx.String(), test.removePfx.String())
 			}
-
-			if mc.removePathParams.path != test.removePath {
-				t.Errorf("Test %q failed: Call to RemovePath did not propagate path properly: Got: %v Want: %v", test.name, mc.removePathParams.path, test.removePath)
-			}
+			assert.Equal(t, test.removePath, mc.removePathParams.path)
 		} else {
-			if mc.removePathParams.pfx != net.NewPfx(0, 0) || mc.removePathParams.path != nil {
+			if mc.removePathParams.pfx != net.NewPfx(0, 0) {
 				t.Errorf("Test %q failed: Call to RemovePath propagated unexpectedly", test.name)
 			}
 		}
-- 
GitLab