Skip to content
Snippets Groups Projects

Enable export and import of SDN configuration

Merged Ghost User requested to merge export-import-sdn-config into develop
1 file
+ 7
43
Compare changes
  • Side-by-side
  • Inline
@@ -16,7 +16,6 @@ import (
"code.fbi.h-da.de/danet/gosdn/forks/goarista/gnmi"
gpb "github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/goyang/pkg/yang"
"github.com/openconfig/ygot/util"
"github.com/openconfig/ygot/ygot"
"github.com/openconfig/ygot/ytypes"
log "github.com/sirupsen/logrus"
@@ -123,14 +122,9 @@ func (g *Gnmi) applyDiff(ctx context.Context, payload change.Payload, path *gpb.
return customerrs.NoNewChangesError{Original: payload.Original, Modified: payload.Modified}
}
var json []byte
if op := ctx.Value(types.CtxKeyOperation); op == ppb.ApiOperation_API_OPERATION_UPDATE || op == ppb.ApiOperation_API_OPERATION_REPLACE {
rootCopy, err := ygot.DeepCopy(schema.Root)
if err != nil {
return err
}
setNodeOpts := []ytypes.SetNodeOpt{&ytypes.InitMissingElements{}, &ytypes.TolerateJSONInconsistencies{}}
filteredUpdates := make([]*gpb.Update, 0)
for _, u := range diff.Update {
rootSchema := schema.RootSchema()
pathString, err := ygot.PathToString(u.GetPath())
@@ -139,34 +133,14 @@ func (g *Gnmi) applyDiff(ctx context.Context, payload change.Payload, path *gpb.
}
entry := rootSchema.Find(pathString)
if !entry.ReadOnly() {
if err := g.SetNode(rootSchema, rootCopy, u.GetPath(), u.GetVal(), setNodeOpts...); err != nil {
return err
}
filteredUpdates = append(filteredUpdates, u)
}
}
ygot.PruneEmptyBranches(rootCopy)
getNodeOpts := []ytypes.GetNodeOpt{
&ytypes.GetHandleWildcards{},
}
nodes, err := ytypes.GetNode(schema.RootSchema(), rootCopy, path, getNodeOpts...)
if err != nil {
return err
}
// TODO: error handling if get nodes[0] returns a nil value
if len(nodes) == 0 || err != nil || util.IsValueNil(nodes[0].Data) {
return customerrs.PathNotFoundError{Path: path, Err: err}
}
json, err = ygot.Marshal7951(nodes[0].Data, &ygot.RFC7951JSONConfig{AppendModuleName: true})
if err != nil {
return err
}
diff.Update = filteredUpdates
}
req, err := createSetRequest(ctx, diff, json, path)
req, err := createSetRequest(ctx, diff)
if err != nil {
return err
}
@@ -176,25 +150,15 @@ func (g *Gnmi) applyDiff(ctx context.Context, payload change.Payload, path *gpb.
return err
}
func createSetRequest(ctx context.Context, diff *gpb.Notification, json []byte, path *gpb.Path) (*gpb.SetRequest, error) {
func createSetRequest(ctx context.Context, diff *gpb.Notification) (*gpb.SetRequest, error) {
op := ctx.Value(types.CtxKeyOperation)
req := &gpb.SetRequest{}
if diff.Update != nil {
switch op {
case ppb.ApiOperation_API_OPERATION_UPDATE:
req.Update = []*gpb.Update{{
Path: path,
Val: &gpb.TypedValue{
Value: &gpb.TypedValue_JsonIetfVal{JsonIetfVal: json},
},
}}
req.Update = diff.Update
case ppb.ApiOperation_API_OPERATION_REPLACE:
req.Replace = []*gpb.Update{{
Path: path,
Val: &gpb.TypedValue{
Value: &gpb.TypedValue_JsonIetfVal{JsonIetfVal: json},
},
}}
req.Replace = diff.Update
default:
return nil, &customerrs.OperationNotSupportedError{Op: op}
}
Loading