forked from alliedtelesis/apteryx-netconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.c
1523 lines (1400 loc) · 46.7 KB
/
data.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file data.c
* Data translation utilities
*
* Copyright 2019, Allied Telesis Labs New Zealand, Ltd
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>
*/
#include "internal.h"
#include <apteryx-xml.h>
typedef struct _sch_xml_to_gnode_parms_s
{
sch_instance *in_instance;
int in_flags;
char *in_def_op;
bool in_is_edit;
GNode *out_tree;
nc_error_parms out_error;
GList *out_deletes;
GList *out_removes;
GList *out_creates;
GList *out_replaces;
GList *out_merges;
} _sch_xml_to_gnode_parms;
static bool
_sch_node_find_name (xmlNs *ns, sch_node * parent, const char *path_name, GList **path_list)
{
xmlNode *xml = (xmlNode *) parent;
xmlNode *n = NULL;
bool found = false;
n = xml->children;
while (n)
{
if (n->type == XML_ELEMENT_NODE && n->name[0] == 'N')
{
char *name = (char *) xmlGetProp (n, (xmlChar *) "name");
if (sch_match_name (name, path_name) && sch_ns_match (n, ns))
{
xmlFree (name);
found = true;
break;
}
if (n->children)
{
found = _sch_node_find_name (ns, n, path_name, path_list);
if (found)
{
*path_list = g_list_prepend (*path_list, g_strdup (name));
xmlFree (name);
break;
}
}
xmlFree (name);
}
n = n->next;
}
return found;
}
static bool
sch_node_find_name (sch_instance *instance, xmlNs *ns, sch_node *parent, const char *path, int flags, GList **path_list)
{
char *name;
char *next;
char *colon;
bool found = false;
if (path && path[0] == '/')
{
path++;
/* Parse path element */
next = strchr (path, '/');
if (next)
name = g_strndup (path, next - path);
else
name = g_strdup (path);
colon = strchr (name, ':');
if (colon)
{
colon[0] = '\0';
xmlNs *nns = sch_lookup_ns (instance, parent, name, flags, false);
if (!nns)
{
/* No namespace found assume the node is supposed to have a colon in it */
colon[0] = ':';
}
else
{
/* We found a namespace. Remove the prefix */
char *_name = name;
name = g_strdup (colon + 1);
free (_name);
ns = nns;
}
}
found = _sch_node_find_name (ns, parent, name, path_list);
g_free (name);
}
return found;
}
static xmlNode *
_sch_gnode_to_xml (sch_instance * instance, sch_node * schema, sch_ns *ns, xmlNode * parent,
GNode * node, int flags, int depth)
{
sch_node *pschema = schema;
xmlNode *data = NULL;
char *colon = NULL;
char *name;
/* Get the actual node name */
if (depth == 0 && strlen (APTERYX_NAME (node)) == 1)
{
return _sch_gnode_to_xml (instance, schema, ns, parent, node->children, flags, depth);
}
else if (depth == 0 && APTERYX_NAME (node)[0] == '/')
{
name = g_strdup (APTERYX_NAME (node) + 1);
}
else
{
name = g_strdup (APTERYX_NAME (node));
}
colon = strchr (name, ':');
if (colon)
{
colon[0] = '\0';
sch_ns *nns = sch_lookup_ns (instance, schema, name, flags, false);
if (!nns)
{
/* No namespace found assume the node is supposed to have a colon in it */
colon[0] = ':';
}
else
{
/* We found a namespace. Remove the prefix */
char *_name = name;
name = g_strdup (colon + 1);
free (_name);
ns = nns;
}
}
/* Find schema node */
if (sch_is_proxy (schema))
{
/* Two possible cases, the node is a child of the proxy node or we need to
* move to access the remote database via the proxy */
schema = sch_ns_node_child (ns, schema, name);
if (!schema)
{
schema = sch_get_root_schema (instance);
colon = strchr (name, ':');
if (schema && colon)
{
colon[0] = '\0';
xmlNs *nns = sch_lookup_ns (instance, schema, name, flags, false);
if (!nns)
{
/* No namespace found assume the node is supposed to have a colon in it */
colon[0] = ':';
}
else
{
/* We found a namespace. Remove the prefix */
char *_name = name;
name = g_strdup (colon + 1);
free (_name);
ns = nns;
}
}
schema = sch_ns_node_child (ns, schema, name);
}
}
else
{
if (!schema)
schema = sch_get_root_schema (instance);
schema = sch_ns_node_child (ns, schema, name);
}
if (schema == NULL)
{
DEBUG ("No schema match for gnode %s%s%s\n",
ns ? sch_ns_prefix (instance, ns) : "", ns ? ":" : "", name);
free (name);
return NULL;
}
if (!sch_is_readable (schema))
{
ERROR ("Ignoring non-readable node %s%s%s\n",
ns ? sch_ns_prefix (instance, ns) : "", ns ? ":" : "", name);
free (name);
return NULL;
}
if (sch_is_leaf_list (schema))
{
apteryx_sort_children (node, g_strcmp0);
for (GNode * child = node->children; child; child = child->next)
{
GNode *value_node = child->children;
if (value_node)
{
char *leaf_name = APTERYX_NAME (value_node);
data = xmlNewNode (NULL, BAD_CAST name);
xmlNodeSetContent (data, (const xmlChar *) leaf_name);
sch_ns *sns = sch_node_ns (schema);
if (!pschema || !sch_ns_match (pschema, sns))
{
const xmlChar *href = (const xmlChar *) sch_ns_href (instance, sns);
xmlNsPtr nns = xmlNewNs (data, href, NULL);
xmlSetNs (data, nns);
}
xmlAddChildList (parent, data);
DEBUG ("%*s%s = %s\n", depth * 2, " ", APTERYX_NAME (node), leaf_name);
}
}
}
else if (sch_is_list (schema))
{
xmlNode *list_data = NULL;
data = NULL;
apteryx_sort_children (node, g_strcmp0);
for (GNode * child = node->children; child; child = child->next)
{
gboolean has_child = false;
DEBUG ("%*s%s[%s]\n", depth * 2, " ", APTERYX_NAME (node),
APTERYX_NAME (child));
list_data = xmlNewNode (NULL, BAD_CAST name);
sch_gnode_sort_children (sch_node_child_first (schema), child);
for (GNode * field = child->children; field; field = field->next)
{
if (_sch_gnode_to_xml (instance, sch_node_child_first (schema), ns,
list_data, field, flags, depth + 1))
{
has_child = true;
}
}
if (has_child)
{
if ((flags & SCH_F_XPATH))
{
char *key = sch_list_key (schema);
if (key)
{
xmlNode *n = list_data->children;
while (n)
{
if (n->type == XML_ELEMENT_NODE && g_strcmp0 (key, (char *) n->name) == 0)
break;
n = n->next;
}
if (!n)
{
xmlNode *key_data = xmlNewNode (NULL, BAD_CAST key);
xmlNodeSetContent (key_data, (const xmlChar *) APTERYX_NAME (child));
xmlAddPrevSibling (list_data->children, key_data);
}
g_free (key);
}
}
xmlAddChildList (parent, list_data);
data = list_data;
}
else
{
xmlFreeNode (list_data);
list_data = NULL;
}
}
}
else if (!sch_is_leaf (schema))
{
gboolean has_child = false;
DEBUG ("%*s%s\n", depth * 2, " ", APTERYX_NAME (node));
data = xmlNewNode (NULL, BAD_CAST name);
sch_gnode_sort_children (schema, node);
for (GNode * child = node->children; child; child = child->next)
{
if (_sch_gnode_to_xml (instance, schema, ns, data, child, flags, depth + 1))
{
has_child = true;
}
}
/* Add this node if we found children or its an empty presence container */
if (parent && (has_child || !((xmlNode *)schema)->children))
{
xmlAddChild (parent, data);
}
else if (!has_child)
{
xmlFreeNode (data);
data = NULL;
}
}
else if (APTERYX_HAS_VALUE (node))
{
if (!(flags & SCH_F_CONFIG) || sch_is_writable (schema))
{
char *value = g_strdup (APTERYX_VALUE (node) ? APTERYX_VALUE (node) : "");
data = xmlNewNode (NULL, BAD_CAST name);
value = sch_translate_to (schema, value);
xmlNodeSetContent (data, (const xmlChar *) value);
if (parent)
xmlAddChildList (parent, data);
DEBUG ("%*s%s = %s\n", depth * 2, " ", APTERYX_NAME (node), value);
free (value);
}
}
/* Record any changes to the namespace (including the root node) */
if (data && schema && (!pschema || ((xmlNode *)pschema)->ns != ((xmlNode *)schema)->ns))
{
/* Dont store a prefix as we set the default xmlns at each node */
xmlNsPtr nns = xmlNewNs (data, ((xmlNode *)schema)->ns->href, NULL);
xmlSetNs (data, nns);
}
free (name);
return data;
}
xmlNode *
sch_gnode_to_xml (sch_instance * instance, sch_node * schema, GNode * node, int flags)
{
if (node && g_node_n_children (node) > 1 && strlen (APTERYX_NAME (node)) == 1)
{
xmlNode *first = NULL;
xmlNode *last = NULL;
xmlNode *next;
apteryx_sort_children (node, g_strcmp0);
for (GNode * child = node->children; child; child = child->next)
{
next = _sch_gnode_to_xml (instance, schema, NULL, NULL, child, flags, 1);
if (next)
{
if (last)
xmlAddSibling (last, next);
last = next;
if (!first)
first = next;
}
}
return first;
}
else
return _sch_gnode_to_xml (instance, schema, NULL, NULL, node, flags, 0);
}
static bool
xml_node_has_content (xmlNode * xml)
{
char *content = (char *) xmlNodeGetContent (xml);
bool ret = (content && strlen (content) > 0);
free (content);
return ret;
}
/**
* Check XML node for the operation attribute and extract it. Return whether the
* operation is recognised or not.
*/
static bool
_operation_ok (_sch_xml_to_gnode_parms *_parms, xmlNode *xml, char *curr_op, char **new_op)
{
char *attr;
attr = (char *) xmlGetProp (xml, BAD_CAST "operation");
if (attr != NULL)
{
if (!_parms->in_is_edit)
{
_parms->out_error.tag = NC_ERR_TAG_BAD_ATTR;
_parms->out_error.type = NC_ERR_TYPE_PROTOCOL;
g_hash_table_insert (_parms->out_error.info, "bad-element", g_strdup ("operation"));
g_hash_table_insert (_parms->out_error.info, "bad-attribute", g_strdup (attr));
return false;
}
/* Find new attribute. */
if (g_strcmp0 (attr, "delete") == 0)
{
*new_op = "delete";
}
else if (g_strcmp0 (attr, "merge") == 0)
{
*new_op = "merge";
}
else if (g_strcmp0 (attr, "replace") == 0)
{
*new_op = "replace";
}
else if (g_strcmp0 (attr, "create") == 0)
{
*new_op = "create";
}
else if (g_strcmp0 (attr, "remove") == 0)
{
*new_op = "remove";
}
else
{
_parms->out_error.tag = NC_ERR_TAG_UNKNOWN_ATTR;
_parms->out_error.type = NC_ERR_TYPE_PROTOCOL;
g_hash_table_insert (_parms->out_error.info, "bad-element", g_strdup ("operation"));
g_hash_table_insert (_parms->out_error.info, "bad-attribute", g_strdup (attr));
return false;
}
g_free (attr);
/* Check for invalid transitions between sub-operations. We only allow
* merge->anything transitions.
*/
if (g_strcmp0 (curr_op, *new_op) != 0 && g_strcmp0 (curr_op, "merge") != 0 &&
g_strcmp0 (curr_op, "none") != 0)
{
_parms->out_error.tag = NC_ERR_TAG_OPR_NOT_SUPPORTED;
_parms->out_error.type = NC_ERR_TYPE_PROTOCOL;
return false;
}
}
return true;
}
static void
_perform_actions (_sch_xml_to_gnode_parms *_parms, int depth, char *curr_op, char *new_op, char *new_xpath)
{
/* Do nothing if not an edit, or operation not changing, unless depth is 0. */
if (!_parms->in_is_edit || (g_strcmp0 (curr_op, new_op) == 0 && depth != 0))
{
return;
}
/* Handle operations. */
if (g_strcmp0 (new_op, "delete") == 0)
{
_parms->out_deletes = g_list_append (_parms->out_deletes, g_strdup (new_xpath));
DEBUG ("delete <%s>\n", new_xpath);
}
else if (g_strcmp0 (new_op, "remove") == 0)
{
_parms->out_removes = g_list_append (_parms->out_removes, g_strdup (new_xpath));
DEBUG ("remove <%s>\n", new_xpath);
}
else if (g_strcmp0 (new_op, "create") == 0)
{
_parms->out_creates = g_list_append (_parms->out_creates, g_strdup (new_xpath));
DEBUG ("create <%s>\n", new_xpath);
}
else if (g_strcmp0 (new_op, "replace") == 0)
{
_parms->out_replaces = g_list_append (_parms->out_replaces, g_strdup (new_xpath));
DEBUG ("replace <%s>\n", new_xpath);
}
}
static GNode *
_sch_xml_to_gnode (_sch_xml_to_gnode_parms *_parms, sch_node * schema, sch_ns *ns, char * part_xpath,
char * curr_op, GNode * pparent, xmlNode * xml, int depth, sch_node **rschema)
{
sch_instance *instance = _parms->in_instance;
int flags = _parms->in_flags;
char *name = (char *) xml->name;
xmlNode *child;
char *attr;
GNode *tree = NULL;
GNode *node = NULL;
char *key = NULL;
char *new_xpath = NULL;
char *new_op = curr_op;
bool key_valid = false;
bool ret_tree = false;
bool is_proxy = false;
/* Detect change in namespace */
if (xml->ns && xml->ns->href)
{
sch_ns *nns = sch_lookup_ns (instance, schema, (const char *) xml->ns->href, flags, true);
if (nns)
ns = nns;
}
/* Find schema node */
if (schema && sch_is_proxy (schema))
{
/* The schema containing the proxy node can have children */
sch_node *child = sch_ns_node_child (ns, schema, name);
if (!child)
{
is_proxy = sch_is_proxy (schema);
}
}
if (!schema || is_proxy)
{
schema = sch_get_root_schema (instance);
/* Detect change in namespace with the new schema */
if (xml->ns && xml->ns->href)
{
sch_ns *nns = sch_lookup_ns (instance, schema, (const char *) xml->ns->href, flags, true);
if (nns)
ns = nns;
}
}
schema = sch_ns_node_child (ns, schema, name);
if (schema == NULL)
{
ERROR ("No schema match for xml node %s%s%s\n",
ns ? sch_ns_prefix (instance, ns) : "", ns ? ":" : "", name);
_parms->out_error.tag = NC_ERR_TAG_MALFORMED_MSG;
_parms->out_error.type = NC_ERR_TYPE_RPC;
return NULL;
}
if (rschema)
*rschema = schema;
/* Prepend non default namespaces to root nodes */
if ((depth == 0 || is_proxy) && ns && sch_ns_prefix (instance, ns) && !sch_ns_native (instance, ns))
name = g_strdup_printf ("%s:%s", sch_ns_prefix (instance, ns), (const char *) xml->name);
else
name = g_strdup ((char *) xml->name);
/* Update xpath. */
new_xpath = g_strdup_printf ("%s/%s", part_xpath, name);
/* Check operation, error tag set on exit from routine. */
if (!_operation_ok (_parms, xml, curr_op, &new_op))
{
ERROR ("Invalid operation\n");
free (new_xpath);
free (name);
return NULL;
}
/* LIST */
if (sch_is_leaf_list (schema))
{
char *old_xpath = new_xpath;
char *key_value = NULL;
DEBUG ("%*s%s%s\n", depth * 2, " ", depth ? "" : "/", name);
tree = APTERYX_NODE (NULL, g_strdup (name));
schema = sch_node_child_first (schema);
if (xml_node_has_content (xml))
{
key_value = (char *) xmlNodeGetContent (xml);
if (g_strcmp0 (new_op, "delete") == 0 || g_strcmp0 (new_op, "remove") == 0 ||
g_strcmp0 (new_op, "none") == 0)
{
new_xpath = g_strdup_printf ("%s/%s", old_xpath, key_value);
}
else
{
new_xpath = g_strdup_printf ("%s/%s/%s", old_xpath, key_value, key_value);
node = APTERYX_NODE (tree, g_strdup (key_value));
node = APTERYX_NODE (node, g_strdup (key_value));
}
g_free (key_value);
g_free (old_xpath);
ret_tree = true;
}
else
{
node = APTERYX_NODE (tree, g_strdup ("*"));
}
if (rschema)
*rschema = schema;
}
else if (sch_is_list (schema))
{
char *old_xpath = new_xpath;
char *key_value;
key = sch_name (sch_node_child_first (sch_node_child_first (schema)));
DEBUG ("%*s%s%s\n", depth * 2, " ", depth ? "" : "/", name);
depth++;
tree = node = APTERYX_NODE (NULL, g_strdup (name));
attr = (char *) xmlGetProp (xml, BAD_CAST key);
if (attr)
{
node = APTERYX_NODE (node, g_strdup (attr));
DEBUG ("%*s%s\n", depth * 2, " ", APTERYX_NAME (node));
if (!(_parms->in_flags & SCH_F_STRIP_KEY) || xmlFirstElementChild (xml))
{
GNode *_node = APTERYX_NODE (node, g_strdup (key));
DEBUG ("%*s%s\n", (depth + 1) * 2, " ", key);
if (!_parms->in_is_edit)
g_node_prepend_data (_node, NULL);
}
key_value = attr;
}
else if (xmlFirstElementChild (xml) &&
g_strcmp0 ((const char *) xmlFirstElementChild (xml)->name, key) == 0 &&
xml_node_has_content (xmlFirstElementChild (xml)))
{
node =
APTERYX_NODE (node,
(char *) xmlNodeGetContent (xmlFirstElementChild (xml)));
DEBUG ("%*s%s\n", depth * 2, " ", APTERYX_NAME (node));
key_value = (char *) xmlNodeGetContent (xmlFirstElementChild (xml));
}
else
{
node = APTERYX_NODE (node, g_strdup ("*"));
DEBUG ("%*s%s\n", depth * 2, " ", APTERYX_NAME (node));
key_value = g_strdup ("*");
}
schema = sch_node_child_first (schema);
if (rschema)
*rschema = schema;
new_xpath = g_strdup_printf ("%s/%s", old_xpath, key_value);
g_free (old_xpath);
g_free (key_value);
}
/* CONTAINER */
else if (!sch_is_leaf (schema))
{
DEBUG ("%*s%s%s\n", depth * 2, " ", depth ? "" : "/", name);
tree = node = APTERYX_NODE (NULL, g_strdup_printf ("%s%s", depth ? "" : "/", name));
}
/* LEAF */
else
{
/* Check that this leaf is writable */
if (_parms->in_is_edit && !sch_is_writable (schema))
{
DEBUG ("Attempt to edit non-writable node \"%s\"\n", name);
apteryx_free_tree (tree);
_parms->out_error.tag = NC_ERR_TAG_INVALID_VAL;
_parms->out_error.type = NC_ERR_TYPE_PROTOCOL;
tree = NULL;
goto exit;
}
if (g_strcmp0 (new_op, "delete") != 0 && g_strcmp0 (new_op, "remove") != 0 &&
g_strcmp0 (new_op, "none") != 0)
{
sch_node *sch_parent;
gboolean validate = true;
char *value = NULL;
tree = node = APTERYX_NODE (NULL, g_strdup (name));
ret_tree = true;
if (!xml_node_has_content (xml) && !(_parms->in_flags & SCH_F_STRIP_DATA)
&& (_parms->in_is_edit))
{
value = g_strdup ("");
}
else if (xml_node_has_content (xml) && !(_parms->in_flags & SCH_F_STRIP_DATA))
{
value = (char *) xmlNodeGetContent (xml);
value = sch_translate_from (schema, value);
}
else
{
DEBUG ("%*s%s%s\n", depth * 2, " ", depth ? "" : "/", name);
validate = false;
}
/* Can now validate value, whether or not it is an empty string */
if (validate)
{
if (_parms->in_is_edit && !sch_validate_pattern (schema, value))
{
DEBUG ("Invalid value \"%s\" for node \"%s\"\n", value, name);
free (value);
apteryx_free_tree (tree);
_parms->out_error.tag = NC_ERR_TAG_INVALID_VAL;
_parms->out_error.type = NC_ERR_TYPE_PROTOCOL;
tree = NULL;
goto exit;
}
/* Test for RFC6241 section 6.2.5 compliance */
sch_parent = sch_node_parent (sch_node_parent (schema));
if (!_parms->in_is_edit && sch_parent && sch_is_list (sch_parent))
{
xmlNode *xml_parent = xml->parent;
xml_parent = xml_parent->parent;
char * key = sch_name (sch_node_child_first (sch_node_child_first (sch_parent)));
xml_parent = xml->parent;
for (child = xmlFirstElementChild (xml_parent); child; child = xmlNextElementSibling (child))
{
if (key && g_strcmp0 ((const char *) child->name, key) == 0)
{
key_valid = true;
break;
}
}
if (key_valid)
{
node = APTERYX_NODE (tree, value);
DEBUG ("%*s%s = %s\n", depth * 2, " ", name, APTERYX_NAME (node));
}
else
g_free (value);
g_free (key);
}
else
{
node = APTERYX_NODE (tree, value);
DEBUG ("%*s%s = %s\n", depth * 2, " ", name, APTERYX_NAME (node));
if (_parms->in_is_edit && (!sch_parent || !sch_is_list (sch_parent)))
{
_parms->out_merges =
g_list_append (_parms->out_merges, g_strdup_printf ("%s/%s", new_xpath, value));
DEBUG ("merge <%s>\n", new_xpath);
}
}
}
else if (!_parms->in_is_edit)
g_node_prepend_data (node, NULL);
}
}
/* Carry out actions for this operation. Does nothing if not edit-config. */
_perform_actions (_parms, depth, curr_op, new_op, new_xpath);
for (child = xmlFirstElementChild (xml); child; child = xmlNextElementSibling (child))
{
if ((_parms->in_flags & SCH_F_STRIP_KEY) && key &&
g_strcmp0 ((const char *) child->name, key) == 0)
{
/* The only child is the key with value */
if (xmlChildElementCount (xml) == 1)
{
if (xml_node_has_content (child))
{
/* Want all parameters for one entry in list. */
GNode *_node = APTERYX_NODE (node, g_strdup ("*"));
DEBUG ("%*s%s\n", (depth + 1) * 2, " ", "*");
if (!_parms->in_is_edit)
g_node_prepend_data (_node, NULL);
}
else
{
/* Want one field in list element for one or more entries */
APTERYX_NODE (node, g_strdup ((const char *) child->name));
DEBUG ("%*s%s\n", (depth + 1) * 2, " ", child->name);
}
break;
}
/* Multiple children - make sure key appears */
else if (xmlChildElementCount (xml) > 1 && !sch_is_proxy (schema))
{
GNode *_node = APTERYX_NODE (node, g_strdup ((const char *) child->name));
DEBUG ("%*s%s\n", depth * 2, " ", APTERYX_NAME (node));
if (!_parms->in_is_edit)
g_node_prepend_data (_node, NULL);
}
ret_tree = true;
}
else
{
GNode *cn = _sch_xml_to_gnode (_parms, schema, ns, new_xpath, new_op, NULL, child, depth + 1, rschema);
if (_parms->out_error.tag)
{
apteryx_free_tree (tree);
tree = NULL;
ERROR ("recursive call failed: depth=%d\n", depth);
goto exit;
}
if (cn)
{
if (node)
g_node_append (node, cn);
else
tree = cn;
ret_tree = true;
}
}
}
/* If no children added, no point in returning anything. */
if (!ret_tree && _parms->in_is_edit)
{
apteryx_free_tree (tree);
tree = NULL;
goto exit;
}
/* Get everything from here down if a trunk of a subtree */
if (!xmlFirstElementChild (xml) && sch_node_child_first (schema) &&
g_strcmp0 (APTERYX_NAME (node), "*") != 0)
{
node = APTERYX_NODE (node, g_strdup ("*"));
DEBUG ("%*s%s\n", (depth + 1) * 2, " ", "*");
}
if (!_parms->in_is_edit && !key_valid && node && node->data && !node->children)
g_node_prepend_data (node, NULL);
exit:
free (name);
free (key);
if (!tree)
{
DEBUG ("returning NULL: xpath=%s\n", new_xpath);
}
g_free (new_xpath);
return tree;
}
static _sch_xml_to_gnode_parms *
sch_parms_init (sch_instance * instance, int flags, char * def_op, bool is_edit)
{
_sch_xml_to_gnode_parms *_parms = g_malloc (sizeof (*_parms));
_parms->in_instance = instance;
_parms->in_flags = flags;
_parms->in_def_op = def_op;
_parms->in_is_edit = is_edit;
_parms->out_tree = NULL;
_parms->out_error = NC_ERROR_PARMS_INIT;
_parms->out_deletes = NULL;
_parms->out_removes = NULL;
_parms->out_creates = NULL;
_parms->out_replaces = NULL;
_parms->out_merges = NULL;
return _parms;
}
sch_xml_to_gnode_parms
sch_xml_to_gnode (sch_instance * instance, sch_node * schema, xmlNode * xml, int flags,
char * def_op, bool is_edit, sch_node **rschema)
{
_sch_xml_to_gnode_parms *_parms = sch_parms_init(instance, flags, def_op, is_edit);
if (xml)
_parms->out_tree = _sch_xml_to_gnode (_parms, schema, NULL, "", def_op, NULL, xml, 0,
rschema);
else
{
_parms->out_error.tag = NC_ERR_TAG_INVALID_VAL;
_parms->out_error.type = NC_ERR_TYPE_PROTOCOL;
}
return (sch_xml_to_gnode_parms) _parms;
}
GNode *
sch_parm_tree (sch_xml_to_gnode_parms parms)
{
_sch_xml_to_gnode_parms *_parms = parms;
GNode *ret;
if (!_parms)
{
return NULL;
}
ret = _parms->out_tree;
_parms->out_tree = NULL;
return ret;
}
nc_error_parms
sch_parm_error (sch_xml_to_gnode_parms parms)
{
_sch_xml_to_gnode_parms *_parms = parms;
return _parms->out_error;
}
GList *
sch_parm_deletes (sch_xml_to_gnode_parms parms)
{
_sch_xml_to_gnode_parms *_parms = parms;
if (!_parms)
{
return NULL;
}
return _parms->out_deletes;
}
GList *
sch_parm_removes (sch_xml_to_gnode_parms parms)
{
_sch_xml_to_gnode_parms *_parms = parms;
if (!_parms)
{
return NULL;
}
return _parms->out_removes;
}
GList *
sch_parm_creates (sch_xml_to_gnode_parms parms)
{
_sch_xml_to_gnode_parms *_parms = parms;
if (!_parms)
{
return NULL;
}
return _parms->out_creates;
}
GList *
sch_parm_replaces (sch_xml_to_gnode_parms parms)
{
_sch_xml_to_gnode_parms *_parms = parms;
if (!_parms)
{
return NULL;
}
return _parms->out_replaces;
}
GList *
sch_parm_merges (sch_xml_to_gnode_parms parms)
{
_sch_xml_to_gnode_parms *_parms = parms;
if (!_parms)
{
return NULL;
}
return _parms->out_merges;
}
void
sch_parm_free (sch_xml_to_gnode_parms parms)
{
_sch_xml_to_gnode_parms *_parms = parms;
if (_parms)
{
g_list_free_full (_parms->out_deletes, g_free);
g_list_free_full (_parms->out_removes, g_free);
g_list_free_full (_parms->out_creates, g_free);
g_list_free_full (_parms->out_replaces, g_free);
g_list_free_full (_parms->out_merges, g_free);
_parms->out_error.tag = 0;
_parms->out_error.type = 0;
g_string_free (_parms->out_error.msg, TRUE);
g_hash_table_destroy (_parms->out_error.info);
g_free (_parms);
}
}
static char *
sch_xpath_update_path (char *path, const char *prefix)
{
char *new_path = NULL;
char *colon = strchr (path, ':');
if (!colon && prefix)
{
new_path = g_strdup_printf ("%s:%s", prefix, path);
}
else if (colon)
{
/* Check for XPATH keyword operators */
if (strlen (colon) > 1 && *(colon + 1) == ':')
return NULL;
new_path = g_strdup_printf ("%s:%s", prefix, colon + 1);
}
return new_path;
}
static void
sch_xpath_change_ns (sch_instance * instance, sch_node * schema, sch_ns *ns, xmlNode * xml,
int depth, xmlXPathContext *xpath_ctx, gchar **path_split, int count)
{
char *name = (char *) xml->name;
const char *href;
const char *prefix;
bool slash_slash = false;
char *new_xpath = NULL;
sch_ns *nns = sch_lookup_ns (instance, schema, (const char *) xml->ns->href, 0, true);