diff --git a/os_clients/ubuntu/supportedPaths.go b/os_clients/ubuntu/supportedPaths.go index 94b414771c9f980f89e1f752411b559c2de2943f..ddfafd10111cd40deeab5a10468c3d6aec8906be 100644 --- a/os_clients/ubuntu/supportedPaths.go +++ b/os_clients/ubuntu/supportedPaths.go @@ -1,26 +1,77 @@ package ubuntu import ( - "syscall" + "fmt" + "net" osc "code.fbi.h-da.de/danet/gnmi-target/os_clients" "github.com/openconfig/gnmi/proto/gnmi" - "github.com/openconfig/gnmi/value" + "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) - if err != nil { - return err + //value, err := value.ToScalar(val) + //if err != nil { + // return err + //} + //err = syscall.Sethostname([]byte(value.(string))) + //if err != nil { + // return err + //} + return nil + }, + "/interfaces/interface/subinterfaces/subinterface/ipv4/addresses/address/ip": func(path *gnmi.Path, val *gnmi.TypedValue) error { + //NOTE: this is just for testing purpose. Normally the information from + // different paths should be combined + keys := filterKeys(path) + + linkName, ok := keys[0]["name"] + if !ok { + return fmt.Errorf("internal error: no interface key has been provided within path:") } - err = syscall.Sethostname([]byte(value.(string))) + + link, err := netlink.LinkByName(linkName) if err != nil { + _, ok := err.(netlink.LinkNotFoundError) + if !ok { + return err + } + link = &netlink.Dummy{ + LinkAttrs: netlink.LinkAttrs{ + Flags: net.FlagUp, + Name: linkName, + }, + } + } + + addr := &netlink.Addr{ + IPNet: &net.IPNet{ + IP: net.ParseIP(val.GetStringVal()), + // just for testing should come from other path + Mask: net.CIDRMask(24, 32), + }, + } + + if err := netlink.AddrAdd(link, addr); err != nil { + fmt.Println("adding the address has failed: ", err) return err } + return nil }, "/interfaces/interface/name": func(path *gnmi.Path, val *gnmi.TypedValue) error { + fmt.Printf("we were in: %s", "/interfaces/interface/name") return nil }, } + +func filterKeys(path *gnmi.Path) []map[string]string { + keyMap := make([]map[string]string, 0) + for _, p := range path.GetElem() { + if key := p.GetKey(); key != nil { + keyMap = append(keyMap, key) + } + } + return keyMap +} diff --git a/os_clients/ubuntu/ubuntu.go b/os_clients/ubuntu/ubuntu.go index b5b91a614e62472a229735f99517a446a0403c14..9bda5bc7068a8bfab3e2f9a9ca27de79b6636637 100644 --- a/os_clients/ubuntu/ubuntu.go +++ b/os_clients/ubuntu/ubuntu.go @@ -1,6 +1,7 @@ package ubuntu import ( + "fmt" "net" "os" "os/exec" @@ -59,10 +60,21 @@ func (ou *OsclientUbuntu) GetCallbackFunc() func(ygot.ValidatedGoStruct) error { } 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 } + + 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 {