diff --git a/examples/example01/handlers/network-instances/networkInstanceHandler.go b/examples/example01/handlers/network-instances/networkInstanceHandler.go index e5571d7f5955e51c001d406735175d70c1af4ce5..c4924e56587a28318a24097b2c63be39b74634b2 100644 --- a/examples/example01/handlers/network-instances/networkInstanceHandler.go +++ b/examples/example01/handlers/network-instances/networkInstanceHandler.go @@ -76,7 +76,6 @@ func (yh *NetworkInstanceHandler) Init(config *handler.Config, publishToSubsFunc // } // } // } - _, err := yh.updateOrCreateNetworkInstance(update) if err != nil { fmt.Println(err) @@ -135,7 +134,6 @@ func (yh *NetworkInstanceHandler) Update(c ygot.ValidatedGoStruct, jobs []*gnmi. func (yh *NetworkInstanceHandler) updateOrCreateNetworkInstance(localStaticRoute *additions.StaticRoute) ([]*gnmi.Notification, error) { yh.Config.Lock() - fmt.Println("received static route: ", localStaticRoute) defer yh.Config.Unlock() copyCurrentConfig, err := ygot.DeepCopy(yh.Config.Data) @@ -150,9 +148,9 @@ func (yh *NetworkInstanceHandler) updateOrCreateNetworkInstance(localStaticRoute confNetworkInstances := newConfig.GetOrCreateNetworkInstances() - if networkInstances := confNetworkInstances.GetOrCreateNetworkInstance("DEFAULT"); networkInstances != nil && localStaticRoute != nil { + if networkInstances := confNetworkInstances.GetOrCreateNetworkInstance("default"); networkInstances != nil && localStaticRoute != nil { if config := networkInstances.GetOrCreateConfig(); config != nil { - config.Name = ygot.String("DEFAULT") + config.Name = ygot.String("default") } if protocols := networkInstances.GetOrCreateProtocols(); protocols != nil { staticProtocol := protocols.GetOrCreateProtocol(gnmitargetygot.OpenconfigPolicyTypes_INSTALL_PROTOCOL_TYPE_STATIC, "STATIC") @@ -164,24 +162,33 @@ func (yh *NetworkInstanceHandler) updateOrCreateNetworkInstance(localStaticRoute staticRoutes := staticProtocol.GetOrCreateStaticRoutes() staticRoute := staticRoutes.GetOrCreateStatic(*localStaticRoute.Prefix) - staticRouteConfig := staticRoute.GetOrCreateConfig() - staticRouteConfig.Prefix = localStaticRoute.Prefix - - staticRouteHops := staticRoute.GetOrCreateNextHops() - for _, hop := range localStaticRoute.Hops { - staticRouteHop := staticRouteHops.GetOrCreateNextHop(*hop.Index) - staticRouteHopConfig := staticRouteHop.GetOrCreateConfig() - if hop.Index != nil { - staticRouteHopConfig.Index = hop.Index - } - if hop.NextHop != nil { - staticRouteHopConfig.NextHop = hop.NextHop + if localStaticRoute.RouteType == "delete"{ + if staticRoutes.Static != nil { + delete(staticRoutes.Static, *localStaticRoute.Prefix) + } + } else { + staticRouteConfig := staticRoute.GetOrCreateConfig() + staticRouteConfig.Prefix = localStaticRoute.Prefix + + staticRouteHops := staticRoute.GetOrCreateNextHops() + // make a new map so previous unused hop gets deleted + staticRouteHops.NextHop = make(map[string]*gnmitargetygot.OpenconfigNetworkInstance_NetworkInstances_NetworkInstance_Protocols_Protocol_StaticRoutes_Static_NextHops_NextHop) + for _, hop := range localStaticRoute.Hops { + staticRouteHop := staticRouteHops.GetOrCreateNextHop(*hop.Index) + staticRouteHopConfig := staticRouteHop.GetOrCreateConfig() + if hop.Index != nil { + staticRouteHopConfig.Index = hop.Index + } + if hop.NextHop != nil { + staticRouteHopConfig.NextHop = hop.NextHop + } - interfaceRef := staticRouteHop.GetOrCreateInterfaceRef() - interfaceRefConfig := interfaceRef.GetOrCreateConfig() - if hop.InterfaceRef != nil { - interfaceRefConfig.Interface = hop.InterfaceRef + interfaceRef := staticRouteHop.GetOrCreateInterfaceRef() + interfaceRefConfig := interfaceRef.GetOrCreateConfig() + if hop.InterfaceRef != nil { + interfaceRefConfig.Interface = hop.InterfaceRef + } } } } diff --git a/examples/example01/osclient/additions/additions.go b/examples/example01/osclient/additions/additions.go index a5eba4db320c567267a7e6fb0c05f9ff4fa3c07b..8456ee1c5a19357fc874ed2f57417c42777eda0d 100644 --- a/examples/example01/osclient/additions/additions.go +++ b/examples/example01/osclient/additions/additions.go @@ -45,6 +45,7 @@ type StaticRoute struct { //TODO: add more Prefix *string Hops []Hop + RouteType string } type Hop struct { diff --git a/examples/example01/osclient/additions/networkInstances_linux.go b/examples/example01/osclient/additions/networkInstances_linux.go index b563d9a4e1a8d42d107647c7d264e86d03bd0f45..83f6e1c1a288b802cb5c05b43fca21ba28f34089 100644 --- a/examples/example01/osclient/additions/networkInstances_linux.go +++ b/examples/example01/osclient/additions/networkInstances_linux.go @@ -6,6 +6,7 @@ import ( gnmitargetygot "code.fbi.h-da.de/danet/gnmi-target/examples/example01/model" log "github.com/sirupsen/logrus" + "golang.org/x/sys/unix" "github.com/vishvananda/netlink" "github.com/vishvananda/netlink/nl" @@ -90,46 +91,50 @@ func (oc *networkInstances) GetStaticRoutes(linkName string) ([]*StaticRoute, er } func (os *networkInstances) SubscribeToRoute() (chan *StaticRoute, error) { - // till now, when there is a new notif, it will get all of the network routes table - // todo: when get notif, search what has happened (add or update or delete) - // todo: only get the network routes table on that specific interfaces(link variable in netlink or code), because there will be more than 1 interfaces - // and if we update the all of routing table, it will hurt the performance - // todo: if possible change only the specific route either add, update or delete on that specific interface - staticRouteChannel := make(chan *StaticRoute) - routeChannel := make(chan netlink.RouteUpdate) - routeDone:= make(chan struct{}) - if err := netlink.RouteSubscribe(routeChannel, routeDone); err != nil { - return nil, err - } + staticRouteChannel := make(chan *StaticRoute) + routeChannel := make(chan netlink.RouteUpdate) + routeDone:= make(chan struct{}) + if err := netlink.RouteSubscribe(routeChannel, routeDone); err != nil { + return nil, err + } + + go func() { + for { + select { + case update := <- routeChannel: + // staticRouteChannel <- &StaticRoute{} + if update.Route.LinkIndex > 0 { + link, err := netlink.LinkByIndex(update.Route.LinkIndex) + if err != nil { + fmt.Println("error getting link: ", err) + continue + } - go func() { - for { - select { - case update := <- routeChannel: - log.Printf("received a route update for route: %s", update.Gw.String()) - // staticRouteChannel <- &StaticRoute{} - if update.Route.LinkIndex > 0 { - link, err := netlink.LinkByIndex(update.Route.LinkIndex) - if err != nil { - fmt.Println("error getting link: ", err) - continue - } - if update.Route.Gw != nil && update.Route.Src == nil { - family := nl.FAMILY_V4 - if update.Route.Gw.To4() == nil { - family = nl.FAMILY_V6 + if update.Route.Gw != nil && update.Route.Src == nil { + family := nl.FAMILY_V4 + if update.Route.Gw.To4() == nil { + family = nl.FAMILY_V6 + } + staticRoute := staticRouteBasedOnFamily(link, update.Route, family) + // fmt.Println("staticroute: ", staticRoute) + // rtm new route also include update route + if update.Type == unix.RTM_NEWROUTE { + staticRoute.RouteType = "add" + } else if update.Type == unix.RTM_DELROUTE { + staticRoute.RouteType = "delete" + } else { + staticRoute.RouteType = "" + } + + staticRouteChannel <- staticRoute } - staticRoute := staticRouteBasedOnFamily(link, update.Route, family) - - staticRouteChannel <- staticRoute } - } - } - } - }() - - return staticRouteChannel, nil -} + } + } + }() + + return staticRouteChannel, nil + } func staticRouteBasedOnFamily(link netlink.Link, route netlink.Route, family int) *StaticRoute { prefix := ipNetIsNilConverter(route.Dst, family)