From e9316e4c2a92a1a1fb0ae2ad79ff4d1ea523fd1b Mon Sep 17 00:00:00 2001 From: Manuel Kieweg <manuel.kieweg@h-da.de> Date: Mon, 21 Jun 2021 17:13:19 +0100 Subject: [PATCH] change southbound interface --- interfaces/southbound/sbi.go | 3 ++- nucleus/southbound.go | 35 +++++------------------------------ 2 files changed, 7 insertions(+), 31 deletions(-) diff --git a/interfaces/southbound/sbi.go b/interfaces/southbound/sbi.go index 11fdb71b1..92e17fcbd 100644 --- a/interfaces/southbound/sbi.go +++ b/interfaces/southbound/sbi.go @@ -2,6 +2,7 @@ package southbound import ( spb "code.fbi.h-da.de/cocsn/api/go/gosdn/southbound" + "github.com/openconfig/ygot/ygot" "github.com/google/uuid" gpb "github.com/openconfig/gnmi/proto/gnmi" @@ -22,5 +23,5 @@ type SouthboundInterface interface { // nolint Schema() *ytypes.Schema ID() uuid.UUID Type() spb.Type - Unmarshal() func([]byte, []string, interface{}, ...ytypes.UnmarshalOpt) error + Unmarshal() func([]byte, []string, ygot.GoStruct, ...ytypes.UnmarshalOpt) error } diff --git a/nucleus/southbound.go b/nucleus/southbound.go index 49e1ea8ec..ae8333612 100644 --- a/nucleus/southbound.go +++ b/nucleus/southbound.go @@ -5,7 +5,6 @@ import ( spb "code.fbi.h-da.de/cocsn/api/go/gosdn/southbound" "code.fbi.h-da.de/cocsn/gosdn/interfaces/southbound" - "code.fbi.h-da.de/cocsn/gosdn/nucleus/errors" "code.fbi.h-da.de/cocsn/yang-models/generated/openconfig" "github.com/google/uuid" @@ -68,39 +67,15 @@ func (oc *OpenConfig) SetNode() func(schema *yang.Entry, root interface{}, path // Unmarshal injects OpenConfig specific model representation to the transport. // Needed for type assertion. -func (oc *OpenConfig) Unmarshal() func([]byte, []string, interface{}, ...ytypes.UnmarshalOpt) error { +func (oc *OpenConfig) Unmarshal() func([]byte, []string, ygot.GoStruct, ...ytypes.UnmarshalOpt) error { return unmarshal } // unmarshal parses a root or 1st level gNMI response to a go struct // Named return to return appropriate recover error -func unmarshal(bytes []byte, fields []string, goStruct interface{}, opt ...ytypes.UnmarshalOpt) (err error) { - defer func() { - if r := recover(); r != nil { - err = r.(error) - } - }() - switch l := len(fields); l { - case 0: - return openconfig.Unmarshal(bytes, goStruct.(*openconfig.Device), opt...) - case 1: - default: - return &errors.ErrUnsupportedPath{Path: fields} - } - var c ygot.GoStruct - var field string - - // Load SBI definition - d := openconfig.Device{} - c, field, err = iter(&d, fields) - if err != nil { - return - } - if err = openconfig.Unmarshal(bytes, c, opt...); err != nil { - return - } - reflect.ValueOf(goStruct.(*openconfig.Device)).Elem().FieldByName(field).Set(reflect.ValueOf(c)) - return nil +func unmarshal(bytes []byte, fields []string, goStruct ygot.GoStruct, opt ...ytypes.UnmarshalOpt) (err error) { + ygot.BuildEmptyTree(goStruct) + return openconfig.Unmarshal(bytes, goStruct.(*openconfig.Device), opt...) } // iter walks down the provided paths and initializes the ygot.GoStruct. It only works for @@ -170,7 +145,7 @@ func (csbi *Csbi) SetNode() func(schema *yang.Entry, root interface{}, path *gpb // Unmarshal injects OpenConfig specific model representation to the transport. // Needed for type assertion. -func (csbi *Csbi) Unmarshal() func([]byte, []string, interface{}, ...ytypes.UnmarshalOpt) error { +func (csbi *Csbi) Unmarshal() func([]byte, []string, ygot.GoStruct, ...ytypes.UnmarshalOpt) error { return unmarshal } -- GitLab