diff --git a/controller/nucleus/southbound.go b/controller/nucleus/southbound.go index de92fad6e964f392450d762a7df18a214bfb5068..65b38433556139db1ac9454440cf2b05bcd5019b 100644 --- a/controller/nucleus/southbound.go +++ b/controller/nucleus/southbound.go @@ -122,42 +122,56 @@ func (oc *OpenConfig) Unmarshal(bytes []byte, path *gpb.Path, goStruct ygot.GoSt return unmarshal(oc.Schema(), bytes, path, goStruct, opt...) } -// unmarshal parses a gNMI response to a go struct. -func unmarshal(schema *ytypes.Schema, bytes []byte, path *gpb.Path, goStruct ygot.GoStruct, opt ...ytypes.UnmarshalOpt) error { - defer func() { - if r := recover(); r != nil { - log.Error(r.(error)) - } - }() - +// Generates an empty validated struct from a given schema and path +func generateEmptyValidatedStructs(schema *ytypes.Schema, path *gpb.Path) (ygot.ValidatedGoStruct, ygot.ValidatedGoStruct, error) { // Load SBI definition root, err := ygot.DeepCopy(schema.Root) if err != nil { - return err + return nil, nil, err } validatedDeepCopy, ok := root.(ygot.ValidatedGoStruct) if !ok { - return &customerrs.InvalidTypeAssertionError{ + return nil, nil, &customerrs.InvalidTypeAssertionError{ Value: root, Type: (*ygot.ValidatedGoStruct)(nil), } } - // returns the node we want to fill with the data contained in 'bytes', - // using the specified 'path'. + // returns the node we want to fill with the data contained in 'bytes', using the specified 'path'. createdNode, _, err := ytypes.GetOrCreateNode(schema.RootSchema(), validatedDeepCopy, path) if err != nil { - return err + return nil, nil, err } validatedCreatedNode, ok := createdNode.(ygot.ValidatedGoStruct) if !ok { - return &customerrs.InvalidTypeAssertionError{ + return nil, nil, &customerrs.InvalidTypeAssertionError{ Value: createdNode, Type: (*ygot.ValidatedGoStruct)(nil), } } - if err := openconfig.Unmarshal(bytes, validatedCreatedNode, opt...); err != nil { + return validatedDeepCopy, validatedCreatedNode, nil +} + +// unmarshal parses a gNMI response to a go struct. +func unmarshal(schema *ytypes.Schema, bytes []byte, path *gpb.Path, goStruct ygot.GoStruct, opt ...ytypes.UnmarshalOpt) error { + defer func() { + if r := recover(); r != nil { + log.Error(r.(error)) + } + }() + + validatedDeepCopy, emptyValidatedCreatedNode, err := generateEmptyValidatedStructs(schema, path) + if err != nil { + return err + } + + _, emptyValidatedCreatedNodeForDiff, err := generateEmptyValidatedStructs(schema, path) + if err != nil { + return err + } + + if err := openconfig.Unmarshal(bytes, emptyValidatedCreatedNode, opt...); err != nil { return err }