Skip to content
Snippets Groups Projects

Process response overhaul

Merged Ghost User requested to merge process-response-overhaul into develop
2 files
+ 21
27
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 46
22
@@ -2,6 +2,7 @@ package nucleus
import (
"context"
"fmt"
"reflect"
"code.fbi.h-da.de/cocsn/gosdn/interfaces/southbound"
@@ -35,7 +36,7 @@ var opmap = map[ppb.ApiOperation]string{
type Gnmi struct {
SetNode func(schema *yang.Entry, root interface{}, path *gpb.Path, val interface{}, opts ...ytypes.SetNodeOpt) error
RespChan chan *gpb.SubscribeResponse
Unmarshal func([]byte, []string, interface{}, ...ytypes.UnmarshalOpt) error
Unmarshal func([]byte, []string, ygot.ValidatedGoStruct, ...ytypes.UnmarshalOpt) error
Options *tpb.TransportOption
client gpb.GNMIClient
config *gnmi.Config
@@ -66,11 +67,12 @@ func newGnmiTransport(opts *tpb.TransportOption, sbi southbound.SouthboundInterf
"tls": opts.Tls,
}).Info("building new gNMI transport")
return &Gnmi{
SetNode: sbi.SetNode(),
RespChan: make(chan *gpb.SubscribeResponse),
Options: opts,
client: c,
config: gnmiConfig,
SetNode: sbi.SetNode,
RespChan: make(chan *gpb.SubscribeResponse),
Unmarshal: sbi.Unmarshal,
Options: opts,
client: c,
config: gnmiConfig,
}, nil
}
@@ -117,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",
@@ -138,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()",
@@ -223,28 +225,50 @@ 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
r := resp.(*gpb.GetResponse)
d, ok := root.(ygot.ValidatedGoStruct)
if !ok {
return &errors.ErrInvalidTypeAssertion{}
}
r, ok := resp.(*gpb.GetResponse)
if !ok {
return &errors.ErrInvalidTypeAssertion{}
}
rn := r.Notification
errs := make([]error, 0)
for _, msg := range rn {
for _, update := range msg.Update {
path := update.Path
fullPath := path
val, ok := update.Val.Value.(*gpb.TypedValue_JsonIetfVal)
if ok {
switch val := update.Val.Value.(type) {
case *gpb.TypedValue_JsonVal:
opts := []ytypes.UnmarshalOpt{&ytypes.IgnoreExtraFields{}}
return g.Unmarshal(val.JsonIetfVal, pathutils.ToStrings(fullPath), root, 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