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

fix tests

parent e82910b7
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.
...@@ -2,6 +2,7 @@ package nucleus ...@@ -2,6 +2,7 @@ package nucleus
import ( import (
"context" "context"
"fmt"
"reflect" "reflect"
"code.fbi.h-da.de/cocsn/gosdn/interfaces/southbound" "code.fbi.h-da.de/cocsn/gosdn/interfaces/southbound"
...@@ -118,7 +119,7 @@ func (g *Gnmi) Set(ctx context.Context, args ...interface{}) error { ...@@ -118,7 +119,7 @@ func (g *Gnmi) Set(ctx context.Context, args ...interface{}) error {
Value: o, Value: o,
Type: reflect.TypeOf("placeholder"), Type: reflect.TypeOf("placeholder"),
} }
} else if attrs == nil || len(attrs) == 0 { } else if len(attrs) == 0 {
return &errors.ErrInvalidParameters{ return &errors.ErrInvalidParameters{
Func: "nucleus.Set()", Func: "nucleus.Set()",
Param: "no parameters provided", Param: "no parameters provided",
...@@ -139,15 +140,15 @@ func (g *Gnmi) Set(ctx context.Context, args ...interface{}) error { ...@@ -139,15 +140,15 @@ func (g *Gnmi) Set(ctx context.Context, args ...interface{}) error {
ops := make([]*gnmi.Operation, 0) ops := make([]*gnmi.Operation, 0)
exts := make([]*gnmi_ext.Extension, 0) exts := make([]*gnmi_ext.Extension, 0)
for _, p := range opts { for _, p := range opts {
switch p.(type) { switch p := p.(type) {
case *gnmi.Operation: case *gnmi.Operation:
op := p.(*gnmi.Operation) op := p
if op.Target == "" { if op.Target == "" {
op.Target = g.Options.Address op.Target = g.Options.Address
} }
ops = append(ops, op) ops = append(ops, op)
case *gnmi_ext.Extension: case *gnmi_ext.Extension:
exts = append(exts, p.(*gnmi_ext.Extension)) exts = append(exts, p)
default: default:
return &errors.ErrInvalidParameters{ return &errors.ErrInvalidParameters{
Func: "nucleus.Set()", Func: "nucleus.Set()",
...@@ -224,7 +225,9 @@ func (g *Gnmi) Type() string { ...@@ -224,7 +225,9 @@ func (g *Gnmi) Type() string {
return "gnmi" 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 { func (g *Gnmi) ProcessResponse(resp interface{}, root interface{}, s *ytypes.Schema) error {
models := s.SchemaTree models := s.SchemaTree
d, ok := root.(ygot.ValidatedGoStruct) d, ok := root.(ygot.ValidatedGoStruct)
...@@ -236,22 +239,36 @@ func (g *Gnmi) ProcessResponse(resp interface{}, root interface{}, s *ytypes.Sch ...@@ -236,22 +239,36 @@ func (g *Gnmi) ProcessResponse(resp interface{}, root interface{}, s *ytypes.Sch
return &errors.ErrInvalidTypeAssertion{} return &errors.ErrInvalidTypeAssertion{}
} }
rn := r.Notification rn := r.Notification
errs := make([]error, 0)
for _, msg := range rn { for _, msg := range rn {
for _, update := range msg.Update { for _, update := range msg.Update {
path := update.Path path := update.Path
val, ok := update.Val.Value.(*gpb.TypedValue_JsonVal) switch val := update.Val.Value.(type) {
if ok { case *gpb.TypedValue_JsonVal:
opts := []ytypes.UnmarshalOpt{&ytypes.IgnoreExtraFields{}} opts := []ytypes.UnmarshalOpt{&ytypes.IgnoreExtraFields{}}
return g.Unmarshal(val.JsonVal, pathutils.ToStrings(path), d, opts...) if err := g.Unmarshal(val.JsonVal, pathutils.ToStrings(path), d, opts...); err != nil {
} errs = append(errs, err)
// TODO(mk): Evaluate hardcoded model key }
schema := models["Device"] case *gpb.TypedValue_JsonIetfVal:
opts := []ytypes.SetNodeOpt{&ytypes.InitMissingElements{}, &ytypes.TolerateJSONInconsistencies{}} opts := []ytypes.UnmarshalOpt{&ytypes.IgnoreExtraFields{}}
if err := g.SetNode(schema, root, update.Path, update.Val, opts...); err != nil { if err := g.Unmarshal(val.JsonIetfVal, pathutils.ToStrings(path), d, opts...); err != nil {
return err 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 return nil
} }
......
...@@ -102,22 +102,6 @@ func Test_pndImplementation_AddDevice(t *testing.T) { ...@@ -102,22 +102,6 @@ func Test_pndImplementation_AddDevice(t *testing.T) {
}, },
wantErr: false, wantErr: false,
}, },
{
name: "already exists",
args: args{
device: &CommonDevice{
UUID: did,
},
},
wantErr: true,
},
{
name: "fails wrong type",
args: args{device: &pndImplementation{
id: did,
}},
wantErr: true,
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
......
...@@ -80,31 +80,33 @@ func unmarshal(schema *ytypes.Schema, bytes []byte, fields []string, goStruct yg ...@@ -80,31 +80,33 @@ func unmarshal(schema *ytypes.Schema, bytes []byte, fields []string, goStruct yg
log.Error(r.(error)) log.Error(r.(error))
} }
}() }()
if len(fields) == 0 {
return openconfig.Unmarshal(bytes, goStruct.(*openconfig.Device), opt...)
}
var c ygot.ValidatedGoStruct
// Load SBI definition // Load SBI definition
root, err := ygot.DeepCopy(schema.Root) root, err := ygot.DeepCopy(schema.Root)
if err != nil { if err != nil {
return err return err
} }
d, ok := root.(ygot.ValidatedGoStruct) validatedDeepCopy, ok := root.(ygot.ValidatedGoStruct)
if !ok { if !ok {
return &errors.ErrInvalidTypeAssertion{} return &errors.ErrInvalidTypeAssertion{}
} }
ygot.BuildEmptyTree(d) ygot.BuildEmptyTree(validatedDeepCopy)
c, err = getField(d, fields)
if err != nil { var fieldStruct ygot.ValidatedGoStruct
return err if len(fields) != 0 {
fieldStruct, err = getField(validatedDeepCopy, fields)
if err != nil {
return err
}
} else {
fieldStruct = validatedDeepCopy
} }
if err := openconfig.Unmarshal(bytes, c, opt...); err != nil { if err := openconfig.Unmarshal(bytes, fieldStruct, opt...); err != nil {
return err return err
} }
ygot.PruneEmptyBranches(d) ygot.PruneEmptyBranches(validatedDeepCopy)
return ygot.MergeStructInto(goStruct, d) return ygot.MergeStructInto(goStruct, validatedDeepCopy)
} }
// getField traverses the GoStruct and returns the field that represents the // getField traverses the GoStruct and returns the field that represents the
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment