Skip to content
Snippets Groups Projects
Commit 9281c78b authored by Manuel Kieweg's avatar Manuel Kieweg
Browse files

attach unmarshal() to sbi struct

parent c53b5391
No related branches found
No related tags found
1 merge request!173Process response overhaul
This commit is part of merge request !173. Comments created here will be created in the context of that merge request.
......@@ -3,7 +3,6 @@ package nucleus
import (
"context"
"reflect"
"strings"
"code.fbi.h-da.de/cocsn/gosdn/interfaces/southbound"
......@@ -242,21 +241,6 @@ func (g *Gnmi) ProcessResponse(resp interface{}, root interface{}, s *ytypes.Sch
path := update.Path
val, ok := update.Val.Value.(*gpb.TypedValue_JsonVal)
if ok {
buf := strings.Builder{}
for _,e := range path.Elem {
key := strings.Title(e.Name)
buf.WriteString(key)
buf.WriteString("_")
}
filter := buf.String()
filter = strings.TrimSuffix(filter,"_")
keys := make([]string, 0)
for k := range models {
if strings.Contains(k, filter) {
keys = append(keys, k)
}
}
//TODO: Fetch field by name
opts := []ytypes.UnmarshalOpt{&ytypes.IgnoreExtraFields{}}
return g.Unmarshal(val.JsonVal, pathutils.ToStrings(path), d, opts...)
}
......
package nucleus
import (
"code.fbi.h-da.de/cocsn/gosdn/nucleus/errors"
"reflect"
spb "code.fbi.h-da.de/cocsn/api/go/gosdn/southbound"
......@@ -67,11 +68,12 @@ 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, ygot.ValidatedGoStruct, ...ytypes.UnmarshalOpt) error {
return unmarshal
return oc.unmarshal
}
// unmarshal parses a root or 1st level gNMI response to a go struct
func unmarshal(bytes []byte, fields []string, goStruct ygot.ValidatedGoStruct, opt ...ytypes.UnmarshalOpt) error {
// unmarshal parses gNMI response to a go struct. If it's a root level response
// it uses
func (oc *OpenConfig)unmarshal(bytes []byte, fields []string, goStruct ygot.ValidatedGoStruct, opt ...ytypes.UnmarshalOpt) error {
defer func() {
if r := recover(); r != nil {
log.Error(r.(error))
......@@ -85,22 +87,29 @@ func unmarshal(bytes []byte, fields []string, goStruct ygot.ValidatedGoStruct, o
var c ygot.ValidatedGoStruct
// Load SBI definition
d := openconfig.Device{}
ygot.BuildEmptyTree(&d)
c = getField(&d, fields)
root, err := ygot.DeepCopy(oc.schema.Root)
if err != nil {
return err
}
d, ok := root.(ygot.ValidatedGoStruct)
if !ok {
return &errors.ErrInvalidTypeAssertion{}
}
ygot.BuildEmptyTree(d)
c = getField(d, fields)
if err := openconfig.Unmarshal(bytes, c, opt...); err != nil {
return err
}
if err := ygot.MergeStructInto(goStruct, &d); err != nil {
ygot.PruneEmptyBranches(d)
if err := ygot.MergeStructInto(goStruct, d); err != nil {
return err
}
return nil
}
// iter walks down the provided paths and initializes the ygot.GoStruct. It only works for
// the root level.
// TODO(mk): Fix deeper layers
// getField traverses the GoStruct and returns the field that represents the
// tail of the path
func getField(inStruct ygot.ValidatedGoStruct, fields []string) ygot.ValidatedGoStruct {
defer func() {
if r := recover(); r != nil {
......@@ -145,7 +154,8 @@ 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, ygot.ValidatedGoStruct, ...ytypes.UnmarshalOpt) error {
return unmarshal
oc := OpenConfig{}
return oc.unmarshal
}
// Schema is holding the default OpenConfig schema for minimal compatibility
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment