-
Notifications
You must be signed in to change notification settings - Fork 28
/
edit-config.c
1849 lines (1702 loc) · 66.5 KB
/
edit-config.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
/* Copyright (c) 2015 Open Networking Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <ctype.h>
#include <string.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include <libnetconf.h>
#include "data.h"
#define NC_NS_BASE10 "urn:ietf:params:xml:ns:netconf:base:1.0"
#define XPATH_BUFFER 1024
static int edit_replace(xmlDocPtr, xmlNodePtr, int, struct nc_err **);
static int edit_delete(xmlNodePtr, int, int, struct nc_err **);
static int edit_remove(xmlDocPtr, xmlNodePtr, int, struct nc_err **);
static int edit_create(xmlDocPtr, xmlNodePtr, int, struct nc_err **);
static int edit_merge(xmlDocPtr, xmlNodePtr, int, struct nc_err **);
/*
* Remove leading and trailing whitespaces from the string.
*
* @param[in] in Input string to clear.
* @return copy of the input string without leading/trailing whitespaces,
* NULL on memory allocation error. Caller is supposed to free the returned
* string.
*/
static char *
clrwspace(const char *in)
{
int i, j = 0, len = strlen(in);
char *retval = strdup(in);
if (retval == NULL) {
nc_verb_error("Memory allocation failed (%s).", __func__);
return (NULL);
}
if (isspace(retval[0])) {
/* remove leading whitespace characters */
for (i = 0, j = 0; i < len; i++, j++) {
while (retval[i] != '\0' && isspace(retval[i])) {
i++;
}
retval[j] = retval[i];
}
}
/* remove trailing whitespace characters */
while (j >= 0 && isspace(retval[j])) {
retval[j] = '\0';
j--;
}
return (retval);
}
/*
* Compare namespaces of the 2 nodes. The function includes XML namespace
* wildcard mechanism defined by RFC 6241. It means, that if the client does
* not specify the namespace of the (edit-config) data, the namespaces always
* match.
*
* @param[in] reference XML element node to compare (the one from edit-config)
* @param[in] node XML element node to compare
* @return 0 for the case that namespaces match, 1 otherwise.
*/
static int
nscmp(xmlNodePtr reference, xmlNodePtr node)
{
int in_ns = 1;
char *s = NULL;
if (reference->ns != NULL && reference->ns->href != NULL) {
/* XML namespace wildcard mechanism:
* 1) no namespace defined and namespace is inherited from message
* so it is NETCONF base namespace
* 2) namespace is empty: xmlns=""
*/
if (!strcmp((char *) reference->ns->href, NC_NS_BASE10) ||
strlen(s = clrwspace((char *) (reference->ns->href))) == 0) {
free(s);
return 0;
}
free(s);
in_ns = 0;
if (node->ns != NULL) {
if (!strcmp((char *) reference->ns->href, (char *) node->ns->href)) {
in_ns = 1;
}
}
}
return (in_ns == 1 ? 0 : 1);
}
/**
* @brief Learn whether the namespace definition is used as namespace in the
* subtree.
* @param[in] node Node where to start checking.
* @param[in] ns Namespace to find.
* @return 0 if the namespace is not used, 1 if the usage of the ns was found
*/
static int
find_namespace_usage(xmlNodePtr node, xmlNsPtr ns)
{
xmlNodePtr child;
xmlAttrPtr prop;
/* check the element itself */
if (node->ns == ns) {
return 1;
} else {
/* check attributes of the element */
for (prop = node->properties; prop != NULL; prop = prop->next) {
if (prop->ns == ns) {
return 1;
}
}
/* go recursive into children */
for (child = node->children; child != NULL; child = child->next) {
if (child->type == XML_ELEMENT_NODE
&& find_namespace_usage(child, ns) == 1) {
return 1;
}
}
}
return 0;
}
/**
* @brief Remove namespace definition from the node which are no longer used.
* @param[in] node XML element node where to check for namespace definitions
*/
static void
clrns(xmlNodePtr node)
{
xmlNsPtr ns, prev = NULL;
if (node == NULL || node->type != XML_ELEMENT_NODE) {
return;
}
for (ns = node->nsDef; ns != NULL;) {
if (find_namespace_usage(node, ns) == 0) {
/* no one use the namespace - remove it */
if (prev == NULL) {
node->nsDef = ns->next;
xmlFreeNs(ns);
ns = node->nsDef;
} else {
prev->next = ns->next;
xmlFreeNs(ns);
ns = prev->next;
}
} else {
/* check another namespace definition */
prev = ns;
ns = ns->next;
}
}
}
xmlNodePtr
go2node(xmlNodePtr parent, xmlChar *name)
{
xmlNodePtr child;
if (!parent) {
return NULL;
}
for (child = parent->children; child; child = child->next) {
if (child->type != XML_ELEMENT_NODE) {
continue;
}
if (xmlStrEqual(child->name, name)) {
break;
}
}
return child;
}
const xmlChar *
get_key(xmlNodePtr parent, const char *name)
{
xmlNodePtr node;
node = go2node(parent, BAD_CAST name);
if (!node || !node->children || !node->children->content
|| xmlIsBlankNode(node->children)) {
nc_verb_error("Invalid key of %s", parent->name);
return NULL;
}
return node->children->content;
}
/**
* @brief Get the default value of the node
* @param[in] node XML element whose default value we want to get
* @return Default value of the node, NULL if no default value is defined
*/
static xmlChar *
get_defval(xmlNodePtr node)
{
if (!node) {
return NULL;
}
if (xmlStrEqual(node->name, BAD_CAST "lost-connection-behavior")) {
return (xmlChar *) "failSecureMode";
} else if (xmlStrEqual(node->name, BAD_CAST "port")
&& xmlStrEqual(node->parent->name, BAD_CAST "controller")) {
return (xmlChar *) "6633";
} else if (xmlStrEqual(node->name, BAD_CAST "protocol")) {
return (xmlChar *) "tls";
} else if (xmlStrEqual(node->name, BAD_CAST "checksum-present")
|| xmlStrEqual(node->name, BAD_CAST "key-present")
|| xmlStrEqual(node->name, BAD_CAST "auto-negotiate")) {
return (xmlChar *) "true";
} else if (xmlStrEqual(node->name, BAD_CAST "no-receive")
|| xmlStrEqual(node->name, BAD_CAST "no-forward")
|| xmlStrEqual(node->name, BAD_CAST "no-packet-in")) {
return (xmlChar *) "false";
} else if (xmlStrEqual(node->name, BAD_CAST "admin-state")) {
return (xmlChar *) "up";
}
return NULL;
}
/**
* \brief Get the list of elements with the specified selected edit-config's
* operation.
*
* \param[in] op edit-config's operation type to search for.
* \param[in] edit XML document covering edit-config's \<config\> element. The
* elements with specified operation will be searched for in
* this document.
*/
static xmlXPathObjectPtr
get_operation_elements(NC_EDIT_OP_TYPE op, xmlDocPtr edit)
{
xmlXPathContextPtr edit_ctxt = NULL;
xmlXPathObjectPtr operation_nodes = NULL;
char xpath[XPATH_BUFFER];
char *opstring;
switch (op) {
case NC_EDIT_OP_MERGE:
opstring = "merge";
break;
case NC_EDIT_OP_REPLACE:
opstring = "replace";
break;
case NC_EDIT_OP_CREATE:
opstring = "create";
break;
case NC_EDIT_OP_DELETE:
opstring = "delete";
break;
case NC_EDIT_OP_REMOVE:
opstring = "remove";
break;
default:
nc_verb_error("Unsupported edit operation %d (%s)", op, __func__);
return (NULL);
}
/* create xpath evaluation context */
edit_ctxt = xmlXPathNewContext(edit);
if (edit_ctxt == NULL) {
if (edit_ctxt != NULL) {
xmlXPathFreeContext(edit_ctxt);
}
nc_verb_error("Creating the XPath evaluation context failed (%s).",
__func__);
return (NULL);
}
if (xmlXPathRegisterNs(edit_ctxt, BAD_CAST "nc", BAD_CAST NC_NS_BASE10)) {
xmlXPathFreeContext(edit_ctxt);
nc_verb_error("Registering a namespace for XPath failed (%s).",
__func__);
return (NULL);
}
if (snprintf(xpath, XPATH_BUFFER, "//*[@nc:operation='%s']", opstring) <= 0) {
xmlXPathFreeContext(edit_ctxt);
nc_verb_error("Preparing the XPath query failed (%s).", __func__);
return (NULL);
}
operation_nodes = xmlXPathEvalExpression(BAD_CAST xpath, edit_ctxt);
/* clean up */
xmlXPathFreeContext(edit_ctxt);
return (operation_nodes);
}
/**
* \brief Get value of the operation attribute of the \<node\> element.
* If no such attribute is present, defop parameter is used and returned.
*
* \param[in] node XML element to analyze
* \param[in] defop Default operation to use if no specific operation is present
* \param[out] err NETCONF error structure to store the error description in
*
* \return NC_OP_TYPE_ERROR on error, valid NC_OP_TYPE values otherwise
*/
static NC_EDIT_OP_TYPE
get_operation(xmlNodePtr node, NC_EDIT_DEFOP_TYPE defop, struct nc_err **error)
{
xmlChar *operation = NULL;
NC_EDIT_OP_TYPE op;
/* get specific operation the node */
operation =
xmlGetNsProp(node, BAD_CAST "operation", BAD_CAST NC_NS_BASE10);
if (operation) {
if (xmlStrEqual(operation, BAD_CAST "merge")) {
op = NC_EDIT_OP_MERGE;
} else if (xmlStrEqual(operation, BAD_CAST "replace")) {
op = NC_EDIT_OP_REPLACE;
} else if (xmlStrEqual(operation, BAD_CAST "create")) {
op = NC_EDIT_OP_CREATE;
} else if (xmlStrEqual(operation, BAD_CAST "delete")) {
op = NC_EDIT_OP_DELETE;
} else if (xmlStrEqual(operation, BAD_CAST "remove")) {
op = NC_EDIT_OP_REMOVE;
} else {
*error = nc_err_new(NC_ERR_BAD_ATTR);
nc_err_set(*error, NC_ERR_PARAM_INFO_BADATTR, "operation");
op = NC_EDIT_OP_ERROR;
}
} else {
op = (NC_EDIT_OP_TYPE) defop;
}
xmlFree(operation);
return op;
}
/*
* Tell if the specified node is a key of a list instance
*
* @param[in] node XML element to analyze
* @return 0 as false, 1 as true
*/
static int
is_key(xmlNodePtr node)
{
if (xmlStrEqual(node->name, BAD_CAST "id")) {
if (xmlStrEqual(node->parent->name, BAD_CAST "switch")
|| xmlStrEqual(node->parent->name, BAD_CAST "controller")) {
return 1;
}
} else if (xmlStrEqual(node->name, BAD_CAST "table-id")) {
return 1;
} else if (xmlStrEqual(node->name, BAD_CAST "name")
&& !xmlStrEqual(node->parent->name, BAD_CAST "flow-table")) {
return 1;
} else if (xmlStrEqual(node->name, BAD_CAST "resource-id")
&& !xmlStrEqual(node->parent->name, BAD_CAST "flow-table")) {
return 1;
}
return 0;
}
/**
* \brief Compare 2 elements and decide if they are equal for NETCONF.
*
* Matching does not include attributes and children match (only key children
* are checked). Furthermore, XML node types and namespaces are also checked.
*
* Supported XML node types are XML_TEXT_NODE and XML_ELEMENT_NODE.
*
* \param[in] node1 First node to compare (from edit-config data).
* \param[in] node2 Second node to compare.
*
* \return 0 - false, 1 - true (matching elements), -1 - error.
*/
static int
matching_elements(xmlNodePtr node1, xmlNodePtr node2)
{
xmlNodePtr key1, key2;
char *aux1, *aux2;
int ret;
if (!node1 || !node2) {
return -1;
}
/* compare text nodes */
if (node1->type == XML_TEXT_NODE && node2->type == XML_TEXT_NODE) {
aux1 = clrwspace((char *) (node1->content));
aux2 = clrwspace((char *) (node2->content));
if (strcmp(aux1, aux2) == 0) {
ret = 1;
} else {
ret = 0;
}
free(aux1);
free(aux2);
return ret;
}
/* check element types - only element nodes are processed */
if ((node1->type != XML_ELEMENT_NODE) || (node2->type != XML_ELEMENT_NODE)) {
return 0;
}
/* check element names */
if (xmlStrcmp(node1->name, node2->name) != 0) {
return 0;
}
/* check element namespace */
if (nscmp(node1, node2) != 0) {
return 0;
}
/*
* if required, check children text node if exists, this is usually needed
* for leaf-list's items
*/
if (xmlStrEqual(node2->name, BAD_CAST "queue")
|| xmlStrEqual(node2->name, BAD_CAST "flow-table")
|| xmlStrEqual(node2->name, BAD_CAST "rate")
|| xmlStrEqual(node2->name, BAD_CAST "medium")
|| (xmlStrEqual(node2->name, BAD_CAST "port")
&& xmlStrEqual(node2->parent->name, BAD_CAST "resources"))) {
if (node1->children != NULL && node1->children->type == XML_TEXT_NODE
&& node2->children != NULL
&& node2->children->type == XML_TEXT_NODE) {
/*
* we do not need to continue to keys checking since compared elements
* do not contain any children that can serve as a key
*/
return (matching_elements(node1->children, node2->children));
}
}
/* check keys in lists */
aux1 = NULL;
if (xmlStrEqual(node2->name, BAD_CAST "controller")
|| xmlStrEqual(node2->name, BAD_CAST "switch")) {
aux1 = "id";
} else if (xmlStrEqual(node2->name, BAD_CAST "port")
&& xmlStrEqual(node2->parent->parent->name,
BAD_CAST "capable-switch")) {
aux1 = "name";
} else if (xmlStrEqual(node2->name, BAD_CAST "flow-table")
&& xmlStrEqual(node2->parent->parent->name,
BAD_CAST "capable-switch")) {
aux1 = "table-id";
} else if ((xmlStrEqual(node2->name, BAD_CAST "queue")
|| xmlStrEqual(node2->name, BAD_CAST "owned-certificate")
|| xmlStrEqual(node2->name, BAD_CAST "external-certificate"))
&& xmlStrEqual(node2->parent->parent->name,
BAD_CAST "capable-switch")) {
aux1 = "resource-id";
} else {
return 1;
}
/* evaluate keys */
key1 = go2node(node1, BAD_CAST aux1);
key2 = go2node(node2, BAD_CAST aux1);
if (!key1 || !key2) {
return 0;
} else {
/* compare key's text nodes, not the key elements itself */
return matching_elements(key1->children, key2->children);
}
}
/**
* \brief Find an equivalent of the given edit node in the orig_doc document.
*
* \param[in] orig_doc Original configuration document to edit.
* \param[in] edit Element from the edit-config's \<config\>. Its equivalent in
* orig_doc should be found.
* \return Found equivalent element, NULL if no such element exists.
*/
static xmlNodePtr
find_element_equiv(xmlDocPtr orig_doc, xmlNodePtr edit)
{
xmlNodePtr orig_parent, node;
if (edit == NULL || orig_doc == NULL) {
return (NULL);
}
/* go recursively to the root */
if (edit->parent->type != XML_DOCUMENT_NODE) {
orig_parent = find_element_equiv(orig_doc, edit->parent);
} else {
if (orig_doc->children == NULL) {
orig_parent = NULL;
} else {
orig_parent = orig_doc->children->parent;
}
}
if (orig_parent == NULL) {
return (NULL);
}
/* element check */
node = orig_parent->children;
while (node != NULL) {
/* compare edit and node */
if (matching_elements(edit, node) == 0) {
/* non matching nodes */
node = node->next;
continue;
} else {
/* matching nodes found */
return (node);
}
}
/* no corresponding node found */
return (NULL);
}
/*
* Check, that all list instances include thair key necessary to identify them.
*
* @param[in] doc XML to check
* @param[out] e NETCONF error structure to fill in case of error
*
* @return EXIT_SUCCESS or EXIT_FAILURE
*/
int
check_keys(xmlDocPtr doc, struct nc_err **e)
{
xmlNodePtr l1, l2, l3, root, enode;
if (!doc || !(root = xmlDocGetRootElement(doc))) {
return EXIT_SUCCESS;
}
for (l1 = root->children; l1; l1 = l1->next) {
if (l1->type != XML_ELEMENT_NODE) {
continue;
}
if (xmlStrEqual(l1->name, BAD_CAST "resources")) {
for (l2 = l1->children; l2; l2 = l2->next) {
if (l2->type != XML_ELEMENT_NODE) {
continue;
}
if (xmlStrEqual(l2->name, BAD_CAST "port")) {
/* port must have name */
if (!go2node(l2, BAD_CAST "name")) {
enode = l2;
goto error;
}
} else if (xmlStrEqual(l2->name, BAD_CAST "queue")
|| xmlStrEqual(l2->name, BAD_CAST "owned-certificate")
|| xmlStrEqual(l2->name,
BAD_CAST "external-certificate")) {
/* here must be resource-id */
if (!go2node(l2, BAD_CAST "resource-id")) {
enode = l2;
goto error;
}
} else if (xmlStrEqual(l2->name, BAD_CAST "flow-table")) {
/* flow-table must have table-id */
if (!go2node(l2, BAD_CAST "table-id")) {
enode = l2;
goto error;
}
}
}
} else if (xmlStrEqual(l1->name, BAD_CAST "logical-switches")) {
for (l2 = l1->children; l2; l2 = l2->next) {
if (l2->type != XML_ELEMENT_NODE
|| !xmlStrEqual(l2->name, BAD_CAST "switch")) {
continue;
}
/* switch must have id */
if (!go2node(l2, BAD_CAST "id")) {
enode = l2;
goto error;
}
/* check controllers if any */
l3 = go2node(l2, BAD_CAST "controllers");
if (!l3) {
continue;
}
for (l3 = l3->children; l3; l3 = l3->next) {
if (l3->type != XML_ELEMENT_NODE
|| !xmlStrEqual(l3->name, BAD_CAST "controller")) {
continue;
}
/* controller must have id */
if (!go2node(l3, BAD_CAST "id")) {
enode = l3;
goto error;
}
}
}
}
}
return EXIT_SUCCESS;
error:
*e = nc_err_new(NC_ERR_BAD_ELEM);
nc_err_set(*e, NC_ERR_PARAM_INFO_BADELEM, (char *) enode->name);
nc_err_set(*e, NC_ERR_PARAM_MSG, "The list element misses key.");
return EXIT_FAILURE;
}
/**
* \brief Check edit-config's node operations hierarchy.
*
* In case of the removal ("remove" and "delete") operations, the supreme
* operation (including the default operation) cannot be the creation ("create
* or "replace") operation.
*
* In case of the creation operations, the supreme operation cannot be a removal
* operation.
*
* \param[in] edit XML node from edit-config's \<config\> whose hierarchy
* (supreme operations) will be checked.
* \param[in] defop Default edit-config's operation for this edit-config call.
* \param[out] err NETCONF error structure.
* \return On error, non-zero is returned and err structure is filled. Zero is
* returned on success.
*/
static int
check_edit_ops_hierarchy(xmlNodePtr edit, NC_EDIT_DEFOP_TYPE defop,
struct nc_err **error)
{
xmlNodePtr parent;
NC_EDIT_OP_TYPE op, parent_op;
op = get_operation(edit, NC_EDIT_DEFOP_NOTSET, error);
if (op == (NC_EDIT_OP_TYPE) NC_EDIT_DEFOP_NOTSET) {
/* no operation defined for this node */
return EXIT_SUCCESS;
} else if (op == NC_EDIT_OP_ERROR) {
return EXIT_FAILURE;
} else if (op == NC_EDIT_OP_DELETE || op == NC_EDIT_OP_REMOVE) {
if (defop == NC_EDIT_DEFOP_REPLACE) {
if (error != NULL) {
if (error != NULL) {
*error = nc_err_new(NC_ERR_OP_FAILED);
}
}
return EXIT_FAILURE;
}
/* check parent elements for operation compatibility */
parent = edit->parent;
while (parent->type != XML_DOCUMENT_NODE) {
parent_op = get_operation(parent, NC_EDIT_DEFOP_NOTSET, error);
if (parent_op == NC_EDIT_OP_ERROR) {
return EXIT_FAILURE;
} else if (parent_op == NC_EDIT_OP_CREATE
|| parent_op == NC_EDIT_OP_REPLACE) {
if (error != NULL) {
*error = nc_err_new(NC_ERR_OP_FAILED);
}
return EXIT_FAILURE;
}
parent = parent->parent;
}
} else if (op == NC_EDIT_OP_CREATE || op == NC_EDIT_OP_REPLACE) {
/* check parent elements for operation compatibility */
parent = edit->parent;
while (parent->type != XML_DOCUMENT_NODE) {
parent_op = get_operation(parent, NC_EDIT_DEFOP_NOTSET, error);
if (parent_op == NC_EDIT_OP_ERROR) {
return EXIT_FAILURE;
} else if (parent_op == NC_EDIT_OP_DELETE
|| parent_op == NC_EDIT_OP_REMOVE) {
if (error != NULL) {
*error = nc_err_new(NC_ERR_OP_FAILED);
}
return EXIT_FAILURE;
}
parent = parent->parent;
}
}
return EXIT_SUCCESS;
}
/**
* \brief Check edit-config's operation rules.
*
* In case of the "create" operation, if the configuration data exists, the
* "data-exists" error is generated.
*
* In case of the "delete" operation, if the configuration data does not exist,
* the "data-missing" error is generated.
*
* Operation hierarchy check check_edit_ops_hierarchy() is also applied.
*
* \param[in] op Operation type to check (only the "delete" and "create"
* operation types are valid).
* \param[in] defop Default edit-config's operation for this edit-config call.
* \param[in] orig Original configuration document to edit.
* \param[in] edit XML document covering edit-config's \<config\> element
* supposed to edit the orig configuration data.
* \param[out] err NETCONF error structure.
* \return On error, non-zero is returned and an err structure is filled.
* 0 is returned on success.
*/
int
check_edit_ops(NC_EDIT_OP_TYPE op, NC_EDIT_DEFOP_TYPE defop, xmlDocPtr orig,
xmlDocPtr edit, struct nc_err **error)
{
xmlXPathObjectPtr operation_nodes = NULL;
xmlNodePtr node_to_process = NULL, n;
xmlChar *defval = NULL, *value = NULL;
int i, r;
operation_nodes = get_operation_elements(op, edit);
if (operation_nodes == NULL) {
*error = nc_err_new(NC_ERR_OP_FAILED);
return EXIT_FAILURE;
}
if (xmlXPathNodeSetIsEmpty(operation_nodes->nodesetval)) {
xmlXPathFreeObject(operation_nodes);
return EXIT_SUCCESS;
}
*error = NULL;
for (i = 0; i < operation_nodes->nodesetval->nodeNr; i++) {
node_to_process = operation_nodes->nodesetval->nodeTab[i];
r = check_edit_ops_hierarchy(node_to_process, defop, error);
if (r != EXIT_SUCCESS) {
xmlXPathFreeObject(operation_nodes);
return EXIT_FAILURE;
}
/* TODO namespace handlings */
n = find_element_equiv(orig, node_to_process);
if (op == NC_EDIT_OP_DELETE && n == NULL) {
if (ncdflt_get_basic_mode() == NCWD_MODE_ALL) {
/* A valid 'delete' operation attribute for a data node that
* contains its schema default value MUST succeed, even though
* the data node is immediately replaced by the server with
* the default value. */
defval = get_defval(node_to_process);
if (defval == NULL) {
/* no default value for this node */
*error = nc_err_new(NC_ERR_DATA_MISSING);
break;
}
value = xmlNodeGetContent(node_to_process);
if (value == NULL) {
*error = nc_err_new(NC_ERR_DATA_MISSING);
break;
}
if (xmlStrcmp(defval, value) != 0) {
/* node do not contain default value */
*error = nc_err_new(NC_ERR_DATA_MISSING);
break;
} else {
/* remove delete operation - it is valid but there is no
* reason to really perform it */
xmlUnlinkNode(node_to_process);
xmlFreeNode(node_to_process);
}
xmlFree(defval);
defval = NULL;
xmlFree(value);
value = NULL;
} else {
*error = nc_err_new(NC_ERR_DATA_MISSING);
break;
}
} else if (op == NC_EDIT_OP_CREATE && n != NULL) {
if (ncdflt_get_basic_mode() == NCWD_MODE_TRIM) {
/* A valid 'create' operation attribute for a data node that
* has a schema default value defined MUST succeed. */
defval = get_defval(node_to_process);
if (defval == NULL) {
/* no default value for this node */
*error = nc_err_new(NC_ERR_DATA_EXISTS);
break;
}
value = xmlNodeGetContent(node_to_process);
if (value == NULL) {
*error = nc_err_new(NC_ERR_DATA_EXISTS);
break;
}
if (xmlStrcmp(defval, value) != 0) {
/* node do not contain default value */
*error = nc_err_new(NC_ERR_DATA_MISSING);
break;
} else {
/* remove old node in configuration to allow recreate it
* by the new one with the default value */
xmlUnlinkNode(n);
xmlFreeNode(n);
}
xmlFree(defval);
defval = NULL;
xmlFree(value);
value = NULL;
} else {
*error = nc_err_new(NC_ERR_DATA_EXISTS);
break;
}
}
}
xmlXPathFreeObject(operation_nodes);
if (defval != NULL) {
xmlFree(defval);
}
if (value != NULL) {
xmlFree(value);
}
if (*error != NULL) {
return (EXIT_FAILURE);
} else {
return EXIT_SUCCESS;
}
}
/*
* Recursive follow-up of the compact_edit_operations()
*
* @param[in] node XML element to process (recursively)
* @param[in] supreme_op The operation type somehow (explicitelly or as a
* default operation) inherited from the parent node.
*
* @return EXIT_SUCCESS or EXIT_FAILURE
*/
static int
compact_edit_operations_r(xmlNodePtr node, NC_EDIT_OP_TYPE supreme_op)
{
NC_EDIT_OP_TYPE op;
xmlNodePtr children;
int ret;
op = get_operation(node, NC_EDIT_DEFOP_NOTSET, NULL);
switch ((int) op) {
case NC_EDIT_OP_ERROR:
return EXIT_FAILURE;
break;
case 0:
/* no operation defined -> go recursively, but use supreme operation,
* it may be the default operation and in such a case remove it */
op = supreme_op;
break;
default:
/* any operation specified */
if (op == supreme_op) {
/* operation duplicity -> remove subordinate duplicated operation */
/* remove operation attribute */
xmlRemoveProp(xmlHasNsProp(node, BAD_CAST "operation",
BAD_CAST NC_NS_BASE10));
clrns(node);
}
break;
}
/* go recursive */
for (children = node->children; children != NULL;
children = children->next) {
ret = compact_edit_operations_r(children, op);
if (ret == EXIT_FAILURE) {
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
/*
* Keep as a few explicit operations in edit-config as possible. It avoids a
* duplication of the same operation in a subtree.
*
* @param[in] edit_doc XML doc to process
* @param[in] def_op The default operation type to inherit in case no operation
* is explicitely specified.
*
* @return EXIT_SUCCESS or EXIT_FAILURE
*/
int
compact_edit_operations(xmlDocPtr edit_doc, NC_EDIT_DEFOP_TYPE defop)
{
xmlNodePtr root;
int r;
if (edit_doc == NULL) {
return EXIT_FAILURE;
}
/* to start recursive check, use defop as root's supreme operation */
for (root = edit_doc->children; root != NULL; root = root->next) {
if (root->type != XML_ELEMENT_NODE) {
continue;
}
r = compact_edit_operations_r(root, (NC_EDIT_OP_TYPE) defop);
if (r != EXIT_SUCCESS) {
return (EXIT_FAILURE);
}
}
return EXIT_SUCCESS;
}
/**
* \brief Perform all the edit-config's operations specified in the edit_doc.
*
* \param[in] orig_doc Original configuration document to edit.
* \param[in] edit_doc XML document covering edit-config's \<config\> element
* supposed to edit orig_doc configuration data.
* \param[in] defop Default edit-config's operation for this edit-config call.
* \param[out] err NETCONF error structure.
*
* \return On error, non-zero is returned and err structure is filled. Zero is
* returned on success.
*/
int
edit_operations(xmlDocPtr orig_doc, xmlDocPtr edit_doc,
NC_EDIT_DEFOP_TYPE defop, int running, struct nc_err **error)
{
xmlXPathObjectPtr nodes;
int i;
xmlNodePtr orig_node, edit_node;
*error = NULL;
if (!orig_doc) {
nc_verb_error("No data to edit");
return EXIT_FAILURE;
}
/* default replace */
if (defop == NC_EDIT_DEFOP_REPLACE) {
/* replace whole document */
nc_verb_verbose("edit-config: default replace");
for (edit_node = edit_doc->children; edit_node != NULL;
edit_node = edit_doc->children) {
edit_replace(orig_doc, edit_node, running, error);
}
}
/* delete operations */
nodes = get_operation_elements(NC_EDIT_OP_DELETE, edit_doc);
if (nodes != NULL) {
if (!xmlXPathNodeSetIsEmpty(nodes->nodesetval)) {
/* something to delete */
nc_verb_verbose("edit-config: delete");
for (i = 0; i < nodes->nodesetval->nodeNr; i++) {
edit_node = nodes->nodesetval->nodeTab[i];
orig_node = find_element_equiv(orig_doc, edit_node);
if (orig_node == NULL) {
xmlXPathFreeObject(nodes);
if (error != NULL) {
*error = nc_err_new(NC_ERR_DATA_MISSING);
}
goto error;
}
for (; orig_node != NULL;
orig_node = find_element_equiv(orig_doc, edit_node)) {
/* remove the edit node's equivalent from the original
* document */