From 0e965c83f26ba24532ec1c80ec3f66868b7a3c73 Mon Sep 17 00:00:00 2001
From: Malte Bauch <malte.bauch@extern.h-da.de>
Date: Mon, 20 Jun 2022 15:38:41 +0200
Subject: [PATCH] Switch to route replace in case the default route has to be
 adjusted

---
 os_clients/ubuntu/supportedPaths.go | 43 ++++++++++++++++++++++++++---
 1 file changed, 39 insertions(+), 4 deletions(-)

diff --git a/os_clients/ubuntu/supportedPaths.go b/os_clients/ubuntu/supportedPaths.go
index 934a30d..f199c22 100644
--- a/os_clients/ubuntu/supportedPaths.go
+++ b/os_clients/ubuntu/supportedPaths.go
@@ -104,6 +104,7 @@ func walkThroughConfig(config ygot.ValidatedGoStruct) error {
 			}
 		}
 	}
+
 	if networkInstances := conf.NetworkInstances; networkInstances != nil {
 		if networkInstanceMap := networkInstances.NetworkInstance; networkInstanceMap != nil {
 			for _, networkInstance := range networkInstanceMap {
@@ -117,6 +118,7 @@ func walkThroughConfig(config ygot.ValidatedGoStruct) error {
 			}
 		}
 	}
+
 	return nil
 }
 
@@ -153,6 +155,7 @@ func generateSubinterface(subintf *gnmitargetygot.OpenconfigInterfaces_Interface
 	if len(errs) != 0 {
 		return fmt.Errorf("encountered %v errors during address adding \n%v", len(errs), errs)
 	}
+
 	return nil
 }
 
@@ -175,12 +178,43 @@ func generateProtocol(protocol *gnmitargetygot.OpenconfigNetworkInstance_Network
 				return err
 			}
 
-			nextHop := net.ParseIP(hop.GetConfig().GetIndex())
+			_ = ipnet
+
+			nextHop, ok := hop.GetConfig().GetNextHop().(*gnmitargetygot.OpenconfigNetworkInstance_NetworkInstances_NetworkInstance_Protocols_Protocol_StaticRoutes_Static_NextHops_NextHop_Config_NextHop_Union_String)
+			if !ok {
+				return fmt.Errorf("only string union is supported for nextHop")
+			}
+
+			nextHopParsed := net.ParseIP(nextHop.String)
+
+			if nextHop == nil {
+				return fmt.Errorf("failed parsing ip")
+			}
+
+			route := &netlink.Route{
+				LinkIndex: link.Attrs().Index,
+				Dst:       ipnet,
+				Gw:        nextHopParsed,
+			}
+
+			//if route.Dst != nil {
+
+			//	fmt.Println("Route GW: ", route.Gw.String())
+			//	fmt.Println("Route Src: ", route.Src.String())
+			//	fmt.Println("Route Dst: ", route.Dst.String())
+			//	err != netlink.RouteAdd(route)
 
-			route := netlink.Route{LinkIndex: link.Attrs().Index, Dst: ipnet, Src: nextHop}
-			netlink.RouteAdd(&route)
+			//}
+			fmt.Println("Route GW: ", route.Gw.String())
+			fmt.Println("Route Src: ", route.Src.String())
+			fmt.Println("Route Dst: ", route.Dst.String())
+
+			if err := netlink.RouteReplace(route); err != nil {
+				return err
+			}
 		}
 	}
+
 	return nil
 }
 
@@ -190,8 +224,8 @@ func CIDRToIPNet(s string) (*net.IPNet, error) {
 		return nil, err
 	}
 	ipnet.IP = ip
-	return ipnet, nil
 
+	return ipnet, nil
 }
 
 func filterKeys(path *gnmi.Path) []map[string]string {
@@ -201,5 +235,6 @@ func filterKeys(path *gnmi.Path) []map[string]string {
 			keyMap = append(keyMap, key)
 		}
 	}
+
 	return keyMap
 }
-- 
GitLab