diff --git a/interfaces/southbound/sbi.go b/interfaces/southbound/sbi.go index 11fdb71b1c62877d16e3439ee69c5c4b08c669f3..92e17fcbde646a0f4778d5be6039d30165269b79 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 49e1ea8ec864a142c126cfcc9aa9d99e869797c8..ae8333612602333d3677c096d5d45ad9ed17cc20 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 }