From 395ed59e90c2e536d556efd6dbff0e4f77f1dcd8 Mon Sep 17 00:00:00 2001 From: Malte Bauch <malte.bauch@extern.h-da.de> Date: Mon, 23 May 2022 17:29:59 +0200 Subject: [PATCH] Adding a callback function for `Set` --- .gitignore | 2 ++ gnmitarget/target.go | 4 +-- os_clients/ubuntu/ubuntu.go | 59 ++++++++++++++++++++++++++----------- 3 files changed, 46 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 36d0846..127ccd6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,9 @@ # tooling build-tools/ +artifacts/ # non vimmers +.devcontainer/ .vscode/ .idea/ diff --git a/gnmitarget/target.go b/gnmitarget/target.go index 710a637..62bc59b 100644 --- a/gnmitarget/target.go +++ b/gnmitarget/target.go @@ -27,7 +27,7 @@ var GnmiTargetYgot ygot.ValidatedGoStruct func scheduler(server *gnmiserver.Server, osclient os_clients.Osclient) { for { - <-time.After(5 * time.Second) + <-time.After(30 * time.Second) log.Info("Update config...") go server.InternalUpdate(currentosclient.UpdateConfig) } @@ -66,7 +66,7 @@ func StartGNMITarget(bindAddress string, configFile string, osclient string) err } } - gnmiServer, err := gnmiserver.NewServer(model, GnmiTargetYgot, nil) + gnmiServer, err := gnmiserver.NewServer(model, GnmiTargetYgot, currentosclient.GetCallbackFunc()) if err != nil { log.Fatalf("error in creating GNMI target: %v", err) } diff --git a/os_clients/ubuntu/ubuntu.go b/os_clients/ubuntu/ubuntu.go index 1227139..c287dbc 100644 --- a/os_clients/ubuntu/ubuntu.go +++ b/os_clients/ubuntu/ubuntu.go @@ -1,15 +1,16 @@ package ubuntu import ( - "fmt" "net" "os" "os/exec" "strings" + "syscall" "time" "code.fbi.h-da.de/danet/gnmi-target/modeldata/gnmitargetygot" + gnmiv "github.com/openconfig/gnmi/value" "github.com/openconfig/ygot/ygot" log "github.com/sirupsen/logrus" "github.com/vishvananda/netlink" @@ -32,19 +33,19 @@ func (ou OsclientUbuntu) UpdateConfig(config *ygot.ValidatedGoStruct) error { func (ou *OsclientUbuntu) GetConfig() *gnmitargetygot.Gnmitarget { ou.gt = &gnmitargetygot.Gnmitarget{} - // Create Interfaces - h, err := netlink.NewHandle() - if err != nil { - log.WithFields(log.Fields{}).Error(err) - } - // defer h.Delete() - localIfaces, err := h.LinkList() - if err != nil { - log.WithFields(log.Fields{}).Error(err) - } - for _, localIface := range localIfaces { - ou.createInterfaces(localIface, ou.gt) - } + // // Create Interfaces + // h, err := netlink.NewHandle() + // if err != nil { + // log.WithFields(log.Fields{}).Error(err) + // } + // // defer h.Delete() + // localIfaces, err := h.LinkList() + // if err != nil { + // log.WithFields(log.Fields{}).Error(err) + // } + // for _, localIface := range localIfaces { + // ou.createInterfaces(localIface, ou.gt) + // } ou.getSystem(ou.gt) @@ -52,11 +53,35 @@ func (ou *OsclientUbuntu) GetConfig() *gnmitargetygot.Gnmitarget { } func (ou *OsclientUbuntu) GetCallbackFunc() func(ygot.ValidatedGoStruct) error { - return callbackFunc + return ou.callbackFunc } -func callbackFunc(config ygot.ValidatedGoStruct) error { - fmt.Printf("config: %v\n", config) +func (ou *OsclientUbuntu) callbackFunc(config ygot.ValidatedGoStruct) error { + diffs, err := ygot.Diff(ou.GetConfig(), config) + if err != nil { + return err + } + for _, diff := range diffs.GetUpdate() { + path, err := ygot.PathToString(diff.GetPath()) + if err != nil { + return err + } + log.Info(path) + switch path { + case "/system/config/hostname": + value, err := gnmiv.ToScalar(diff.GetVal()) + if err != nil { + return err + } + err = syscall.Sethostname([]byte(value.(string))) + if err != nil { + return err + } + default: + //INFO: currently this case is ignored + } + } + return nil } -- GitLab