Skip to content

Commit 705b571

Browse files
committed
support netconf responses with multiple root nodes
1 parent 673615e commit 705b571

File tree

3 files changed

+125
-5
lines changed

3 files changed

+125
-5
lines changed

pkg/datastore/target/nc.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (t *ncTarget) Get(ctx context.Context, req *sdcpb.GetDataRequest) (*sdcpb.G
123123
// building the resulting sdcpb.GetDataResponse struct
124124
result := &sdcpb.GetDataResponse{
125125
Notification: []*sdcpb.Notification{
126-
noti,
126+
stripRootDataContainer(noti),
127127
},
128128
}
129129
return result, nil
@@ -363,7 +363,7 @@ func (t *ncTarget) setCandidate(ctx context.Context, req *sdcpb.SetDataRequest)
363363
return nil, err
364364
}
365365
err2 := t.driver.Discard()
366-
if err != nil {
366+
if err2 != nil {
367367
// log failed discard
368368
log.Errorf("failed with %v while discarding pending changes after error %v", err2, err)
369369
}
@@ -386,3 +386,16 @@ func (t *ncTarget) setCandidate(ctx context.Context, req *sdcpb.SetDataRequest)
386386
Timestamp: time.Now().UnixNano(),
387387
}, nil
388388
}
389+
390+
func stripRootDataContainer(n *sdcpb.Notification) *sdcpb.Notification {
391+
for i := range n.GetUpdate() {
392+
numElem := len(n.Update[i].GetPath().GetElem())
393+
if numElem == 0 {
394+
continue
395+
}
396+
if n.Update[i].GetPath().GetElem()[0].GetName() == "data" {
397+
n.Update[i].GetPath().Elem = n.Update[i].GetPath().Elem[1:]
398+
}
399+
}
400+
return n
401+
}

pkg/datastore/target/nc_test.go

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"testing"
2222

2323
"github.com/beevik/etree"
24-
2524
"github.com/sdcio/data-server/mocks/mocknetconf"
2625
"github.com/sdcio/data-server/mocks/mockschema"
2726
"github.com/sdcio/data-server/pkg/config"
@@ -272,3 +271,111 @@ func Test_ncTarget_Get(t *testing.T) {
272271
})
273272
}
274273
}
274+
275+
func Test_stripDataContainerRoot(t *testing.T) {
276+
type args struct {
277+
n *sdcpb.Notification
278+
}
279+
tests := []struct {
280+
name string
281+
args args
282+
want *sdcpb.Notification
283+
}{
284+
{
285+
name: "test0",
286+
args: args{
287+
n: &sdcpb.Notification{
288+
Timestamp: 42,
289+
Update: []*sdcpb.Update{{
290+
Path: &sdcpb.Path{},
291+
Value: &sdcpb.TypedValue{},
292+
}},
293+
},
294+
},
295+
want: &sdcpb.Notification{
296+
Timestamp: 42,
297+
Update: []*sdcpb.Update{{
298+
Path: &sdcpb.Path{},
299+
Value: &sdcpb.TypedValue{},
300+
}},
301+
},
302+
},
303+
{
304+
name: "test1",
305+
args: args{
306+
n: &sdcpb.Notification{
307+
Timestamp: 42,
308+
Update: []*sdcpb.Update{{
309+
Path: &sdcpb.Path{
310+
Elem: []*sdcpb.PathElem{
311+
{
312+
Name: "data",
313+
},
314+
{
315+
Name: "configure"},
316+
},
317+
},
318+
Value: &sdcpb.TypedValue{},
319+
}},
320+
},
321+
},
322+
want: &sdcpb.Notification{
323+
Timestamp: 42,
324+
Update: []*sdcpb.Update{{
325+
Path: &sdcpb.Path{
326+
Elem: []*sdcpb.PathElem{
327+
{
328+
Name: "configure",
329+
},
330+
},
331+
},
332+
Value: &sdcpb.TypedValue{},
333+
}},
334+
},
335+
},
336+
{
337+
name: "test2",
338+
args: args{
339+
n: &sdcpb.Notification{
340+
Timestamp: 42,
341+
Update: []*sdcpb.Update{{
342+
Path: &sdcpb.Path{
343+
Elem: []*sdcpb.PathElem{
344+
{
345+
Name: "not_data",
346+
},
347+
{
348+
Name: "configure",
349+
},
350+
},
351+
},
352+
Value: &sdcpb.TypedValue{},
353+
}},
354+
},
355+
},
356+
want: &sdcpb.Notification{
357+
Timestamp: 42,
358+
Update: []*sdcpb.Update{{
359+
Path: &sdcpb.Path{
360+
Elem: []*sdcpb.PathElem{
361+
{
362+
Name: "not_data",
363+
},
364+
{
365+
Name: "configure",
366+
},
367+
},
368+
},
369+
Value: &sdcpb.TypedValue{},
370+
}},
371+
},
372+
},
373+
}
374+
for _, tt := range tests {
375+
t.Run(tt.name, func(t *testing.T) {
376+
if got := stripRootDataContainer(tt.args.n); !reflect.DeepEqual(got, tt.want) {
377+
t.Errorf("stripDataContainerRoot() = %v, want %v", got, tt.want)
378+
}
379+
})
380+
}
381+
}

pkg/datastore/target/netconf/driver/scrapligo/scrapligo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (snt *ScrapligoNetconfTarget) GetConfig(source string, filter string) (*typ
144144

145145
// the actual config is contained under /rpc-reply/data/ in the result document.
146146
// so we are extracting that portion
147-
newRootXpath := "/rpc-reply/data/*"
147+
newRootXpath := "/rpc-reply/data"
148148
r := x.FindElement(newRootXpath)
149149
if r == nil {
150150
return nil, fmt.Errorf("unable to find %q in %s", newRootXpath, resp.Result)
@@ -194,7 +194,7 @@ func (snt *ScrapligoNetconfTarget) Get(filter string) (*types.NetconfResponse, e
194194
return nil, err
195195
}
196196

197-
newRootXpath := "/rpc-reply/data/*"
197+
newRootXpath := "/rpc-reply/data"
198198
r := x.FindElement(newRootXpath)
199199
if r == nil {
200200
return nil, fmt.Errorf("unable to find %q in %s", newRootXpath, resp.Result)

0 commit comments

Comments
 (0)