Skip to content
Snippets Groups Projects
Commit 2bfe6090 authored by Vincentius Raynaldi's avatar Vincentius Raynaldi
Browse files

update the SubscribeToRoute function

parent 155445ff
No related branches found
No related tags found
No related merge requests found
Pipeline #268110 failed
...@@ -76,7 +76,6 @@ func (yh *NetworkInstanceHandler) Init(config *handler.Config, publishToSubsFunc ...@@ -76,7 +76,6 @@ func (yh *NetworkInstanceHandler) Init(config *handler.Config, publishToSubsFunc
// } // }
// } // }
// } // }
_, err := yh.updateOrCreateNetworkInstance(update) _, err := yh.updateOrCreateNetworkInstance(update)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
...@@ -135,7 +134,6 @@ func (yh *NetworkInstanceHandler) Update(c ygot.ValidatedGoStruct, jobs []*gnmi. ...@@ -135,7 +134,6 @@ func (yh *NetworkInstanceHandler) Update(c ygot.ValidatedGoStruct, jobs []*gnmi.
func (yh *NetworkInstanceHandler) updateOrCreateNetworkInstance(localStaticRoute *additions.StaticRoute) ([]*gnmi.Notification, error) { func (yh *NetworkInstanceHandler) updateOrCreateNetworkInstance(localStaticRoute *additions.StaticRoute) ([]*gnmi.Notification, error) {
yh.Config.Lock() yh.Config.Lock()
fmt.Println("received static route: ", localStaticRoute)
defer yh.Config.Unlock() defer yh.Config.Unlock()
copyCurrentConfig, err := ygot.DeepCopy(yh.Config.Data) copyCurrentConfig, err := ygot.DeepCopy(yh.Config.Data)
...@@ -150,9 +148,9 @@ func (yh *NetworkInstanceHandler) updateOrCreateNetworkInstance(localStaticRoute ...@@ -150,9 +148,9 @@ func (yh *NetworkInstanceHandler) updateOrCreateNetworkInstance(localStaticRoute
confNetworkInstances := newConfig.GetOrCreateNetworkInstances() 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 { if config := networkInstances.GetOrCreateConfig(); config != nil {
config.Name = ygot.String("DEFAULT") config.Name = ygot.String("default")
} }
if protocols := networkInstances.GetOrCreateProtocols(); protocols != nil { if protocols := networkInstances.GetOrCreateProtocols(); protocols != nil {
staticProtocol := protocols.GetOrCreateProtocol(gnmitargetygot.OpenconfigPolicyTypes_INSTALL_PROTOCOL_TYPE_STATIC, "STATIC") staticProtocol := protocols.GetOrCreateProtocol(gnmitargetygot.OpenconfigPolicyTypes_INSTALL_PROTOCOL_TYPE_STATIC, "STATIC")
...@@ -164,24 +162,33 @@ func (yh *NetworkInstanceHandler) updateOrCreateNetworkInstance(localStaticRoute ...@@ -164,24 +162,33 @@ func (yh *NetworkInstanceHandler) updateOrCreateNetworkInstance(localStaticRoute
staticRoutes := staticProtocol.GetOrCreateStaticRoutes() staticRoutes := staticProtocol.GetOrCreateStaticRoutes()
staticRoute := staticRoutes.GetOrCreateStatic(*localStaticRoute.Prefix) staticRoute := staticRoutes.GetOrCreateStatic(*localStaticRoute.Prefix)
staticRouteConfig := staticRoute.GetOrCreateConfig() if localStaticRoute.RouteType == "delete"{
staticRouteConfig.Prefix = localStaticRoute.Prefix if staticRoutes.Static != nil {
delete(staticRoutes.Static, *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
} }
} 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() interfaceRef := staticRouteHop.GetOrCreateInterfaceRef()
interfaceRefConfig := interfaceRef.GetOrCreateConfig() interfaceRefConfig := interfaceRef.GetOrCreateConfig()
if hop.InterfaceRef != nil { if hop.InterfaceRef != nil {
interfaceRefConfig.Interface = hop.InterfaceRef interfaceRefConfig.Interface = hop.InterfaceRef
}
} }
} }
} }
......
...@@ -45,6 +45,7 @@ type StaticRoute struct { ...@@ -45,6 +45,7 @@ type StaticRoute struct {
//TODO: add more //TODO: add more
Prefix *string Prefix *string
Hops []Hop Hops []Hop
RouteType string
} }
type Hop struct { type Hop struct {
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
gnmitargetygot "code.fbi.h-da.de/danet/gnmi-target/examples/example01/model" gnmitargetygot "code.fbi.h-da.de/danet/gnmi-target/examples/example01/model"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
"github.com/vishvananda/netlink" "github.com/vishvananda/netlink"
"github.com/vishvananda/netlink/nl" "github.com/vishvananda/netlink/nl"
...@@ -90,46 +91,50 @@ func (oc *networkInstances) GetStaticRoutes(linkName string) ([]*StaticRoute, er ...@@ -90,46 +91,50 @@ func (oc *networkInstances) GetStaticRoutes(linkName string) ([]*StaticRoute, er
} }
func (os *networkInstances) SubscribeToRoute() (chan *StaticRoute, error) { func (os *networkInstances) SubscribeToRoute() (chan *StaticRoute, error) {
// till now, when there is a new notif, it will get all of the network routes table staticRouteChannel := make(chan *StaticRoute)
// todo: when get notif, search what has happened (add or update or delete) routeChannel := make(chan netlink.RouteUpdate)
// 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 routeDone:= make(chan struct{})
// and if we update the all of routing table, it will hurt the performance if err := netlink.RouteSubscribe(routeChannel, routeDone); err != nil {
// todo: if possible change only the specific route either add, update or delete on that specific interface return nil, err
staticRouteChannel := make(chan *StaticRoute) }
routeChannel := make(chan netlink.RouteUpdate)
routeDone:= make(chan struct{}) go func() {
if err := netlink.RouteSubscribe(routeChannel, routeDone); err != nil { for {
return nil, err 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() { if update.Route.Gw != nil && update.Route.Src == nil {
for { family := nl.FAMILY_V4
select { if update.Route.Gw.To4() == nil {
case update := <- routeChannel: family = nl.FAMILY_V6
log.Printf("received a route update for route: %s", update.Gw.String()) }
// staticRouteChannel <- &StaticRoute{} staticRoute := staticRouteBasedOnFamily(link, update.Route, family)
if update.Route.LinkIndex > 0 { // fmt.Println("staticroute: ", staticRoute)
link, err := netlink.LinkByIndex(update.Route.LinkIndex) // rtm new route also include update route
if err != nil { if update.Type == unix.RTM_NEWROUTE {
fmt.Println("error getting link: ", err) staticRoute.RouteType = "add"
continue } else if update.Type == unix.RTM_DELROUTE {
} staticRoute.RouteType = "delete"
if update.Route.Gw != nil && update.Route.Src == nil { } else {
family := nl.FAMILY_V4 staticRoute.RouteType = ""
if update.Route.Gw.To4() == nil { }
family = nl.FAMILY_V6
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 { func staticRouteBasedOnFamily(link netlink.Link, route netlink.Route, family int) *StaticRoute {
prefix := ipNetIsNilConverter(route.Dst, family) prefix := ipNetIsNilConverter(route.Dst, family)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment