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 ...@@ -3,7 +3,6 @@ package nucleus
import ( import (
"context" "context"
"reflect" "reflect"
"strings"
"code.fbi.h-da.de/cocsn/gosdn/interfaces/southbound" "code.fbi.h-da.de/cocsn/gosdn/interfaces/southbound"
...@@ -242,21 +241,6 @@ func (g *Gnmi) ProcessResponse(resp interface{}, root interface{}, s *ytypes.Sch ...@@ -242,21 +241,6 @@ func (g *Gnmi) ProcessResponse(resp interface{}, root interface{}, s *ytypes.Sch
path := update.Path path := update.Path
val, ok := update.Val.Value.(*gpb.TypedValue_JsonVal) val, ok := update.Val.Value.(*gpb.TypedValue_JsonVal)
if ok { 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{}} opts := []ytypes.UnmarshalOpt{&ytypes.IgnoreExtraFields{}}
return g.Unmarshal(val.JsonVal, pathutils.ToStrings(path), d, opts...) return g.Unmarshal(val.JsonVal, pathutils.ToStrings(path), d, opts...)
} }
......
package nucleus package nucleus
import ( import (
"code.fbi.h-da.de/cocsn/gosdn/nucleus/errors"
"reflect" "reflect"
spb "code.fbi.h-da.de/cocsn/api/go/gosdn/southbound" 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 ...@@ -67,11 +68,12 @@ func (oc *OpenConfig) SetNode() func(schema *yang.Entry, root interface{}, path
// Unmarshal injects OpenConfig specific model representation to the transport. // Unmarshal injects OpenConfig specific model representation to the transport.
// Needed for type assertion. // Needed for type assertion.
func (oc *OpenConfig) Unmarshal() func([]byte, []string, ygot.ValidatedGoStruct, ...ytypes.UnmarshalOpt) error { 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 // unmarshal parses gNMI response to a go struct. If it's a root level response
func unmarshal(bytes []byte, fields []string, goStruct ygot.ValidatedGoStruct, opt ...ytypes.UnmarshalOpt) error { // it uses
func (oc *OpenConfig)unmarshal(bytes []byte, fields []string, goStruct ygot.ValidatedGoStruct, opt ...ytypes.UnmarshalOpt) error {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
log.Error(r.(error)) log.Error(r.(error))
...@@ -85,22 +87,29 @@ func unmarshal(bytes []byte, fields []string, goStruct ygot.ValidatedGoStruct, o ...@@ -85,22 +87,29 @@ func unmarshal(bytes []byte, fields []string, goStruct ygot.ValidatedGoStruct, o
var c ygot.ValidatedGoStruct var c ygot.ValidatedGoStruct
// Load SBI definition // Load SBI definition
d := openconfig.Device{} root, err := ygot.DeepCopy(oc.schema.Root)
ygot.BuildEmptyTree(&d) if err != nil {
c = getField(&d, fields) 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 { if err := openconfig.Unmarshal(bytes, c, opt...); err != nil {
return err return err
} }
if err := ygot.MergeStructInto(goStruct, &d); err != nil { ygot.PruneEmptyBranches(d)
if err := ygot.MergeStructInto(goStruct, d); err != nil {
return err return err
} }
return nil return nil
} }
// iter walks down the provided paths and initializes the ygot.GoStruct. It only works for // getField traverses the GoStruct and returns the field that represents the
// the root level. // tail of the path
// TODO(mk): Fix deeper layers
func getField(inStruct ygot.ValidatedGoStruct, fields []string) ygot.ValidatedGoStruct { func getField(inStruct ygot.ValidatedGoStruct, fields []string) ygot.ValidatedGoStruct {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
...@@ -145,7 +154,8 @@ func (csbi *Csbi) SetNode() func(schema *yang.Entry, root interface{}, path *gpb ...@@ -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. // Unmarshal injects OpenConfig specific model representation to the transport.
// Needed for type assertion. // Needed for type assertion.
func (csbi *Csbi) Unmarshal() func([]byte, []string, ygot.ValidatedGoStruct, ...ytypes.UnmarshalOpt) error { 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 // 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