Skip to content
Snippets Groups Projects

Process response overhaul

Merged Ghost User requested to merge process-response-overhaul into develop
Files
3
+ 31
14
@@ -2,6 +2,7 @@ package nucleus
import (
"context"
"fmt"
"reflect"
"code.fbi.h-da.de/cocsn/gosdn/interfaces/southbound"
@@ -118,7 +119,7 @@ func (g *Gnmi) Set(ctx context.Context, args ...interface{}) error {
Value: o,
Type: reflect.TypeOf("placeholder"),
}
} else if attrs == nil || len(attrs) == 0 {
} else if len(attrs) == 0 {
return &errors.ErrInvalidParameters{
Func: "nucleus.Set()",
Param: "no parameters provided",
@@ -139,15 +140,15 @@ func (g *Gnmi) Set(ctx context.Context, args ...interface{}) error {
ops := make([]*gnmi.Operation, 0)
exts := make([]*gnmi_ext.Extension, 0)
for _, p := range opts {
switch p.(type) {
switch p := p.(type) {
case *gnmi.Operation:
op := p.(*gnmi.Operation)
op := p
if op.Target == "" {
op.Target = g.Options.Address
}
ops = append(ops, op)
case *gnmi_ext.Extension:
exts = append(exts, p.(*gnmi_ext.Extension))
exts = append(exts, p)
default:
return &errors.ErrInvalidParameters{
Func: "nucleus.Set()",
@@ -224,7 +225,9 @@ func (g *Gnmi) Type() string {
return "gnmi"
}
// ProcessResponse takes a gNMI response and serializes the contents to the root struct.
// ProcessResponse takes a gNMI response and serializes the contents to the
// root struct. It logs all errors and returns an error containing the number
// off errors encountered during the process.
func (g *Gnmi) ProcessResponse(resp interface{}, root interface{}, s *ytypes.Schema) error {
models := s.SchemaTree
d, ok := root.(ygot.ValidatedGoStruct)
@@ -236,22 +239,36 @@ func (g *Gnmi) ProcessResponse(resp interface{}, root interface{}, s *ytypes.Sch
return &errors.ErrInvalidTypeAssertion{}
}
rn := r.Notification
errs := make([]error, 0)
for _, msg := range rn {
for _, update := range msg.Update {
path := update.Path
val, ok := update.Val.Value.(*gpb.TypedValue_JsonVal)
if ok {
switch val := update.Val.Value.(type) {
case *gpb.TypedValue_JsonVal:
opts := []ytypes.UnmarshalOpt{&ytypes.IgnoreExtraFields{}}
return g.Unmarshal(val.JsonVal, pathutils.ToStrings(path), d, opts...)
}
// TODO(mk): Evaluate hardcoded model key
schema := models["Device"]
opts := []ytypes.SetNodeOpt{&ytypes.InitMissingElements{}, &ytypes.TolerateJSONInconsistencies{}}
if err := g.SetNode(schema, root, update.Path, update.Val, opts...); err != nil {
return err
if err := g.Unmarshal(val.JsonVal, pathutils.ToStrings(path), d, opts...); err != nil {
errs = append(errs, err)
}
case *gpb.TypedValue_JsonIetfVal:
opts := []ytypes.UnmarshalOpt{&ytypes.IgnoreExtraFields{}}
if err := g.Unmarshal(val.JsonIetfVal, pathutils.ToStrings(path), d, opts...); err != nil {
errs = append(errs, err)
}
default:
schema := models["Device"]
opts := []ytypes.SetNodeOpt{&ytypes.InitMissingElements{}, &ytypes.TolerateJSONInconsistencies{}}
if err := g.SetNode(schema, root, update.Path, update.Val, opts...); err != nil {
errs = append(errs, err)
}
}
}
}
for _, e := range errs {
log.Error(e)
}
if len(errs) != 0 {
return fmt.Errorf("encountered %v errors during response processing", len(errs))
}
return nil
}
Loading