Skip to content
Snippets Groups Projects
Commit 6d34d130 authored by Malte Bauch's avatar Malte Bauch
Browse files

first steps for proof of concept to add route

parent b1ea6e34
No related branches found
No related tags found
4 merge requests!17Build basic ci,!10Add simple modem support on linux through modemmanager,!8General refactoring,!5Draft: Add basic set support for ip/routing via gnmi
......@@ -65,9 +65,9 @@ func (m *Model) NewConfigStruct(jsonConfig []byte) (ygot.ValidatedGoStruct, erro
if err := m.jsonUnmarshaler(jsonConfig, rootStruct); err != nil {
return nil, err
}
if err := rootStruct.ΛValidate(); err != nil {
return nil, err
}
//if err := rootStruct.ΛValidate(); err != nil {
// return nil, err
//}
}
return rootStruct, nil
}
......
......@@ -3,21 +3,23 @@ package ubuntu
import (
"fmt"
"net"
"syscall"
"code.fbi.h-da.de/danet/gnmi-target/modeldata/gnmitargetygot"
osc "code.fbi.h-da.de/danet/gnmi-target/os_clients"
"github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/ygot/util"
"github.com/openconfig/ygot/ygot"
"github.com/vishvananda/netlink"
)
var supportedPaths = map[string]osc.PathFunc{
"/system/config/hostname": func(path *gnmi.Path, val *gnmi.TypedValue) error {
//value, err := value.ToScalar(val)
//value, err := value.toscalar(val)
//if err != nil {
// return err
//}
//err = syscall.Sethostname([]byte(value.(string)))
//err = syscall.sethostname([]byte(value.(string)))
//if err != nil {
// return err
//}
......@@ -71,7 +73,8 @@ var supportedPaths = map[string]osc.PathFunc{
},
}
func walkThroughConfig(config any) error {
// NOTE: playing around -> don't mind me
func walkThroughConfig(config ygot.ValidatedGoStruct) error {
conf, ok := config.(*gnmitargetygot.Gnmitarget)
if !ok {
return fmt.Errorf("failed type assertion for %T", (*gnmitargetygot.Gnmitarget)(nil))
......@@ -90,6 +93,17 @@ func walkThroughConfig(config any) error {
}
}
}
if system := conf.System; system != nil {
if config := system.Config; config != nil {
if hostname := config.Hostname; hostname != nil {
err := syscall.Sethostname([]byte(*hostname))
if err != nil {
return err
}
}
}
}
if networkInstances := conf.NetworkInstances; networkInstances != nil {
if networkInstanceMap := networkInstances.NetworkInstance; networkInstanceMap != nil {
for _, networkInstance := range networkInstanceMap {
......@@ -126,8 +140,7 @@ func generateSubinterface(subintf *gnmitargetygot.OpenconfigInterfaces_Interface
if !util.IsValueNil(addr.Config.Ip) && !util.IsValueNil(addr.Config.PrefixLength) {
netlinkAddr := &netlink.Addr{
IPNet: &net.IPNet{
IP: net.ParseIP(*addr.Config.Ip),
// just for testing should come from other path
IP: net.ParseIP(*addr.Config.Ip),
Mask: net.CIDRMask(int(*addr.Config.PrefixLength), 32),
},
}
......@@ -144,8 +157,10 @@ func generateSubinterface(subintf *gnmitargetygot.OpenconfigInterfaces_Interface
}
func generateProtocol(protocol *gnmitargetygot.OpenconfigNetworkInstance_NetworkInstances_NetworkInstance_Protocols_Protocol) error {
for _, staticRoute := range protocol.StaticRoutes.Static {
staticRoutes := protocol.StaticRoutes
for _, staticRoute := range staticRoutes.Static {
prefix := staticRoute.Prefix
for _, hop := range staticRoute.NextHops.NextHop {
link, err := netlink.LinkByName(*hop.InterfaceRef.Config.Interface)
if err != nil {
......@@ -154,26 +169,31 @@ func generateProtocol(protocol *gnmitargetygot.OpenconfigNetworkInstance_Network
return err
}
}
source := &net.IPNet{
IP: net.IPv4(0, 0, 0, 0),
Mask: net.CIDRMask(32, 32),
}
source := net.ParseIP()
dst := &net.IPNet{
IP: net.ParseIP(),
Mask: net.CIDRMask(32, 32),
ipnet, err := CIDRToIPNet(*prefix)
if err != nil {
return err
}
route = netlink.Route{LinkIndex: link.Attrs().Index, Dst: dst, Src: source}
nextHop := net.ParseIP(hop.GetConfig().GetIndex())
route := netlink.Route{LinkIndex: link.Attrs().Index, Dst: ipnet, Src: nextHop}
netlink.RouteAdd(&route)
}
}
return nil
}
func CIDRToIPNet(s string) (*net.IPNet, error) {
ip, ipnet, err := net.ParseCIDR(s)
if err != nil {
return nil, err
}
ipnet.IP = ip
return ipnet, nil
}
func filterKeys(path *gnmi.Path) []map[string]string {
keyMap := make([]map[string]string, 0)
for _, p := range path.GetElem() {
......
package ubuntu
import (
"fmt"
"net"
"os"
"os/exec"
......@@ -52,6 +51,8 @@ func (ou *OsclientUbuntu) GetConfig() *gnmitargetygot.Gnmitarget {
ou.getSystem(ou.gt)
ou.getStaticRoutes(ou.gt)
return ou.gt
}
......@@ -63,32 +64,33 @@ func (ou *OsclientUbuntu) callbackFunc(config ygot.ValidatedGoStruct) error {
//opts := []ygot.DiffOpt{
// &ygot.DiffPathOpt{MapToSinglePath: true},
//}
diffs, err := ygot.Diff(ou.GetConfig(), config)
if err != nil {
return err
}
//diffs, err := ygot.Diff(ou.GetConfig(), config)
//if err != nil {
// return err
//}
for _, path := range diffs.GetDelete() {
stringPath, err := ygot.PathToString(path)
if err != nil {
return err
}
fmt.Println("PathToDelete: ", stringPath)
}
for _, diff := range diffs.GetUpdate() {
schemaPath, err := ygot.PathToSchemaPath(diff.GetPath())
if err != nil {
return err
}
if fn, found := ou.pm[schemaPath]; found {
if err := fn(diff.GetPath(), diff.GetVal()); err != nil {
return err
}
} else {
//ignore all other paths for now
return fmt.Errorf("the path: %s is not supported", schemaPath)
}
}
//for _, path := range diffs.GetDelete() {
// stringPath, err := ygot.PathToString(path)
// if err != nil {
// return err
// }
// fmt.Println("PathToDelete: ", stringPath)
//}
//for _, diff := range diffs.GetUpdate() {
// schemaPath, err := ygot.PathToSchemaPath(diff.GetPath())
// if err != nil {
// return err
// }
// if fn, found := ou.pm[schemaPath]; found {
// if err := fn(diff.GetPath(), diff.GetVal()); err != nil {
// return err
// }
// } else {
// //ignore all other paths for now
// return fmt.Errorf("the path: %s is not supported", schemaPath)
// }
//}
walkThroughConfig(config)
return nil
}
......@@ -226,6 +228,23 @@ func (ou *OsclientUbuntu) getSystem(gt *gnmitargetygot.Gnmitarget) {
}
}
func (ou *OsclientUbuntu) getStaticRoutes(gt *gnmitargetygot.Gnmitarget) {
networkInstances := gt.GetOrCreateNetworkInstances()
defaultNetworkInstance := networkInstances.GetOrCreateNetworkInstance("default")
defaultProtocols := defaultNetworkInstance.GetOrCreateProtocols()
defaultProtocol := defaultProtocols.GetOrCreateProtocol(gnmitargetygot.OpenconfigPolicyTypes_INSTALL_PROTOCOL_TYPE_DIRECTLY_CONNECTED, "DIRECTLY_CONNECTED")
defaultProtocolStaticRoutes := defaultProtocol.GetOrCreateStaticRoutes()
defaultProtocolStaticRoute := defaultProtocolStaticRoutes.GetOrCreateStatic("10.10.10.1/24")
defaultNextHops := defaultProtocolStaticRoute.GetOrCreateNextHops()
defaultNextHop := defaultNextHops.GetOrCreateNextHop("AUTO_1_10-0-0-1")
defaultInterfaceRef := defaultNextHop.GetOrCreateInterfaceRef()
defaultInterfaceRefConf := defaultInterfaceRef.GetOrCreateConfig()
defaultInterfaceRefConf.Interface = ygot.String("eth0")
}
//TODO: this will be removed in the near future
//func (ou OsclientUbuntu) TestUbuntu() {
// ou.gt = &gnmitargetygot.Gnmitarget{}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment