Skip to content

Commit 8b363a8

Browse files
committed
second try
1 parent 705b571 commit 8b363a8

File tree

4 files changed

+40
-152
lines changed

4 files changed

+40
-152
lines changed

pkg/datastore/target/nc.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,7 @@ func (t *ncTarget) Get(ctx context.Context, req *sdcpb.GetDataRequest) (*sdcpb.G
122122

123123
// building the resulting sdcpb.GetDataResponse struct
124124
result := &sdcpb.GetDataResponse{
125-
Notification: []*sdcpb.Notification{
126-
stripRootDataContainer(noti),
127-
},
125+
Notification: noti,
128126
}
129127
return result, nil
130128
}
@@ -386,16 +384,3 @@ func (t *ncTarget) setCandidate(ctx context.Context, req *sdcpb.SetDataRequest)
386384
Timestamp: time.Now().UnixNano(),
387385
}, nil
388386
}
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: 0 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -271,111 +271,3 @@ func Test_ncTarget_Get(t *testing.T) {
271271
})
272272
}
273273
}
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/xml2SchemapbAdapter.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,18 @@ func NewXML2sdcpbConfigAdapter(ssc schema.Client, schema *sdcpb.Schema) *XML2sdc
4141
}
4242

4343
// Transform takes an etree.Document and transforms the content into a sdcpb based Notification
44-
func (x *XML2sdcpbConfigAdapter) Transform(ctx context.Context, doc *etree.Document) (*sdcpb.Notification, error) {
45-
result := &sdcpb.Notification{}
46-
err := x.transformRecursive(ctx, doc.Root(), []*sdcpb.PathElem{}, result, nil)
44+
func (x *XML2sdcpbConfigAdapter) Transform(ctx context.Context, doc *etree.Document) ([]*sdcpb.Notification, error) {
45+
result := make([]*sdcpb.Notification, 0, len(doc.ChildElements()))
46+
for _, e := range doc.ChildElements() {
47+
r := &sdcpb.Notification{}
48+
err := x.transformRecursive(ctx, e, []*sdcpb.PathElem{}, r, nil)
49+
if err != nil {
50+
return nil, err
51+
}
52+
result = append(result, r)
53+
}
4754

48-
return result, err
55+
return result, nil
4956
}
5057

5158
func (x *XML2sdcpbConfigAdapter) transformRecursive(ctx context.Context, e *etree.Element, pelems []*sdcpb.PathElem, result *sdcpb.Notification, tc *TransformationContext) error {

pkg/datastore/target/netconf/xml2SchemapbAdapter_test.go

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func TestXML2sdcpbConfigAdapter_Transform(t *testing.T) {
5757
name string
5858
getXML2sdcpbConfigAdapter func(ctrl *gomock.Controller, t *testing.T) *XML2sdcpbConfigAdapter
5959
args args
60-
want *sdcpb.Notification
60+
want []*sdcpb.Notification
6161
wantErr bool
6262
}{
6363
{
@@ -124,28 +124,30 @@ func TestXML2sdcpbConfigAdapter_Transform(t *testing.T) {
124124
)
125125
return NewXML2sdcpbConfigAdapter(schemaClientMock, TestSchema)
126126
},
127-
want: &sdcpb.Notification{
128-
Update: []*sdcpb.Update{
129-
{
130-
Path: &sdcpb.Path{
131-
Elem: []*sdcpb.PathElem{
132-
{
133-
Name: "interfaces",
134-
},
135-
{
136-
Name: "interface",
137-
Key: map[string]string{
138-
"name": "eth0",
127+
want: []*sdcpb.Notification{
128+
{
129+
Update: []*sdcpb.Update{
130+
{
131+
Path: &sdcpb.Path{
132+
Elem: []*sdcpb.PathElem{
133+
{
134+
Name: "interfaces",
135+
},
136+
{
137+
Name: "interface",
138+
Key: map[string]string{
139+
"name": "eth0",
140+
},
141+
},
142+
{
143+
Name: "name",
139144
},
140-
},
141-
{
142-
Name: "name",
143145
},
144146
},
145-
},
146-
Value: &sdcpb.TypedValue{
147-
Value: &sdcpb.TypedValue_StringVal{
148-
StringVal: "eth0",
147+
Value: &sdcpb.TypedValue{
148+
Value: &sdcpb.TypedValue_StringVal{
149+
StringVal: "eth0",
150+
},
149151
},
150152
},
151153
},
@@ -159,17 +161,19 @@ func TestXML2sdcpbConfigAdapter_Transform(t *testing.T) {
159161
mockCtrl := gomock.NewController(t)
160162

161163
x := tt.getXML2sdcpbConfigAdapter(mockCtrl, t)
162-
// tt.args.doc.Indent(2)
163-
// s, _ := tt.args.doc.WriteToString()
164-
// fmt.Println(s)
165164
got, err := x.Transform(tt.args.ctx, tt.args.doc)
166165
if (err != nil) != tt.wantErr {
167166
t.Errorf("XML2sdcpbConfigAdapter.Transform() error = %v, wantErr %v", err, tt.wantErr)
168167
return
169168
}
170-
if !utils.NotificationsEqual(got, tt.want) {
169+
if len(got) != len(tt.want) {
171170
t.Errorf("XML2sdcpbConfigAdapter.Transform() = %v, want %v", got, tt.want)
172171
}
172+
for i := range got {
173+
if !utils.NotificationsEqual(got[i], tt.want[i]) {
174+
t.Errorf("XML2sdcpbConfigAdapter.Transform() = %v, want %v", got[i], tt.want[i])
175+
}
176+
}
173177
})
174178
}
175179
}

0 commit comments

Comments
 (0)