-
Notifications
You must be signed in to change notification settings - Fork 326
Expand file tree
/
Copy pathtree_data_common.c
More file actions
1751 lines (1500 loc) · 49.5 KB
/
tree_data_common.c
File metadata and controls
1751 lines (1500 loc) · 49.5 KB
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 tree_data_common.c
* @author Radek Krejci <rkrejci@cesnet.cz>
* @author Michal Vasko <mvasko@cesnet.cz>
* @brief Parsing and validation common functions for data trees
*
* Copyright (c) 2015 - 2026 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/
#define _GNU_SOURCE /* asprintf, strdup */
#include <assert.h>
#include <ctype.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "compat.h"
#include "context.h"
#include "dict.h"
#include "hash_table.h"
#include "log.h"
#include "ly_common.h"
#include "lyb.h"
#include "metadata.h"
#include "parser_data.h"
#include "plugins_exts.h"
#include "plugins_internal.h"
#include "printer_data.h"
#include "schema_compile_node.h"
#include "set.h"
#include "tree.h"
#include "tree_data.h"
#include "tree_data_internal.h"
#include "tree_edit.h"
#include "tree_schema.h"
#include "tree_schema_internal.h"
#include "validation.h"
#include "xml.h"
#include "xpath.h"
/**
* @brief Callback for checking first instance hash table values equivalence.
*
* @param[in] val1_p If not @p mod, pointer to the first instance.
* @param[in] val2_p If not @p mod, pointer to the found dup inst item.
*/
static ly_bool
lyht_dup_inst_ht_equal_cb(void *val1_p, void *val2_p, ly_bool mod, void *UNUSED(cb_data))
{
if (mod) {
struct lyd_dup_inst **item1 = val1_p, **item2 = val2_p;
/* equal on 2 dup inst items */
return *item1 == *item2 ? 1 : 0;
} else {
struct lyd_node **first_inst = val1_p;
struct lyd_dup_inst **item = val2_p;
/* equal on dup inst item and a first instance */
return (*item)->set->dnodes[0] == *first_inst ? 1 : 0;
}
}
/**
* @brief Find an entry in duplicate instance cache for an instance. Create it if it does not exist.
*
* @param[in] first_inst First instance of the cache entry.
* @param[in] dup_inst_ht Duplicate instance cache hash table.
* @return Instance cache entry.
*/
static struct lyd_dup_inst *
lyd_dup_inst_get(const struct lyd_node *first_inst, struct ly_ht **dup_inst_ht)
{
struct lyd_dup_inst **item_p, *item;
if (*dup_inst_ht) {
/* find the item of the first instance */
if (!lyht_find(*dup_inst_ht, &first_inst, first_inst->hash, (void **)&item_p)) {
return *item_p;
}
} else {
/* create the hash table */
*dup_inst_ht = lyht_new(2, sizeof item, lyht_dup_inst_ht_equal_cb, NULL, 1);
LY_CHECK_RET(!*dup_inst_ht, NULL);
}
/* first instance has no dup inst item, create it */
item = calloc(1, sizeof *item);
LY_CHECK_RET(!item, NULL);
/* add into the hash table */
if (lyht_insert(*dup_inst_ht, &item, first_inst->hash, NULL)) {
return NULL;
}
return item;
}
LY_ERR
lyd_dup_inst_next(struct lyd_node **inst, struct ly_ht **dup_inst_ht)
{
struct lyd_dup_inst *dup_inst;
if (!*inst) {
/* no match, inst is unchanged */
return LY_SUCCESS;
}
/* there can be more exact same instances (even if not allowed in invalid data) and we must make sure we do not
* match a single node more times */
dup_inst = lyd_dup_inst_get(*inst, dup_inst_ht);
LY_CHECK_ERR_RET(!dup_inst, LOGMEM(LYD_CTX(*inst)), LY_EMEM);
if (!dup_inst->used) {
/* we did not cache these instances yet, do so (use the same inst in case it is from a mount-point) */
lyd_find_sibling_dup_inst_set(*inst, *inst, &dup_inst->set);
assert(dup_inst->set->count && (dup_inst->set->dnodes[0] == *inst));
}
if (dup_inst->used == dup_inst->set->count) {
/* config user-ordered data with several instances are invalid, but it can be supported */
if (lysc_is_dup_inst_list((*inst)->schema) || lysc_is_userordered((*inst)->schema)) {
/* we have used all the instances */
*inst = NULL;
} /* else just keep using the last (ideally only) instance */
} else {
assert(dup_inst->used < dup_inst->set->count);
/* use another instance */
*inst = dup_inst->set->dnodes[dup_inst->used];
++dup_inst->used;
}
return LY_SUCCESS;
}
/**
* @brief Callback for freeing first instance hash table values.
*/
static void
lyht_dup_inst_ht_free_cb(void *val_p)
{
struct lyd_dup_inst **item = val_p;
ly_set_free((*item)->set, NULL);
free(*item);
}
void
lyd_dup_inst_free(struct ly_ht *dup_inst_ht)
{
lyht_free(dup_inst_ht, lyht_dup_inst_ht_free_cb);
}
struct lyd_node *
lys_getnext_data(const struct lyd_node *last, const struct lyd_node *sibling, const struct lysc_node **slast,
const struct lysc_node *parent, const struct lysc_module *module)
{
const struct lysc_node *siter = NULL;
struct lyd_node *match = NULL;
assert(parent || module);
assert(!last || (slast && *slast));
if (slast) {
siter = *slast;
}
if (last && last->next && (last->next->schema == siter)) {
/* return next data instance */
return last->next;
}
/* find next schema node data instance */
while ((siter = lys_getnext(siter, parent, module, 0))) {
if (!lyd_find_sibling_val(sibling, siter, NULL, 0, &match)) {
break;
}
}
if (slast) {
*slast = siter;
}
return match;
}
struct lyd_node **
lyd_node_child_p(struct lyd_node *node)
{
assert(node);
if (!node->schema) {
return &((struct lyd_node_opaq *)node)->child;
} else {
switch (node->schema->nodetype) {
case LYS_CONTAINER:
case LYS_LIST:
case LYS_RPC:
case LYS_ACTION:
case LYS_NOTIF:
return &((struct lyd_node_inner *)node)->child;
default:
return NULL;
}
}
}
LIBYANG_API_DEF LY_ERR
lyxp_vars_set(struct lyxp_var **vars, const char *name, const char *value)
{
LY_ERR ret = LY_SUCCESS;
char *var_name = NULL, *var_value = NULL;
struct lyxp_var *item;
if (!vars || !name || !value) {
return LY_EINVAL;
}
/* if variable is already defined then change its value */
if (*vars && !lyxp_vars_find(NULL, *vars, name, 0, &item)) {
var_value = strdup(value);
LY_CHECK_RET(!var_value, LY_EMEM);
/* update value */
free(item->value);
item->value = var_value;
} else {
var_name = strdup(name);
var_value = strdup(value);
LY_CHECK_ERR_GOTO(!var_name || !var_value, ret = LY_EMEM, error);
/* add new variable */
LY_ARRAY_NEW_GOTO(NULL, *vars, item, ret, error);
item->name = var_name;
item->value = var_value;
}
return LY_SUCCESS;
error:
free(var_name);
free(var_value);
return ret;
}
LIBYANG_API_DEF void
lyxp_vars_free(struct lyxp_var *vars)
{
LY_ARRAY_COUNT_TYPE u;
if (!vars) {
return;
}
LY_ARRAY_FOR(vars, u) {
free(vars[u].name);
free(vars[u].value);
}
LY_ARRAY_FREE(vars);
}
LIBYANG_API_DEF struct lyd_node *
lyd_child_no_keys(const struct lyd_node *node)
{
struct lyd_node **children;
if (!node) {
return NULL;
}
if (!node->schema) {
/* opaq node */
return ((struct lyd_node_opaq *)node)->child;
}
children = lyd_node_child_p((struct lyd_node *)node);
if (children) {
struct lyd_node *child = *children;
while (child && child->schema && (child->schema->flags & LYS_KEY)) {
child = child->next;
}
return child;
} else {
return NULL;
}
}
LIBYANG_API_DEF const struct lys_module *
lyd_owner_module(const struct lyd_node *node)
{
const struct lyd_node_opaq *opaq;
if (!node) {
return NULL;
}
while (!node->schema && node->parent) {
node = node->parent;
}
if (!node->schema) {
/* top-level opaque node */
opaq = (struct lyd_node_opaq *)node;
switch (opaq->format) {
case LY_VALUE_XML:
if (opaq->name.module_ns) {
return ly_ctx_get_module_implemented_ns(LYD_CTX(node), opaq->name.module_ns);
}
break;
case LY_VALUE_JSON:
if (opaq->name.module_name) {
return ly_ctx_get_module_implemented(LYD_CTX(node), opaq->name.module_name);
}
break;
default:
return NULL;
}
return NULL;
}
return lysc_owner_module(node->schema);
}
LIBYANG_API_DEF const struct lys_module *
lyd_node_module(const struct lyd_node *node)
{
const struct lyd_node_opaq *opaq;
while (node) {
/* data node */
if (node->schema) {
return node->schema->module;
}
/* opaque node */
opaq = (struct lyd_node_opaq *)node;
switch (opaq->format) {
case LY_VALUE_XML:
if (opaq->name.module_ns) {
return ly_ctx_get_module_implemented_ns(LYD_CTX(node), opaq->name.module_ns);
}
break;
case LY_VALUE_JSON:
if (opaq->name.module_name) {
return ly_ctx_get_module_implemented(LYD_CTX(node), opaq->name.module_name);
}
break;
default:
break;
}
node = node->parent;
}
return NULL;
}
void
lyd_first_module_sibling(struct lyd_node **node, const struct lys_module *mod)
{
int cmp;
struct lyd_node *first;
const struct lys_module *own_mod;
assert(node && mod);
if (!*node) {
return;
}
first = *node;
own_mod = lyd_owner_module(first);
cmp = own_mod ? strcmp(own_mod->name, mod->name) : 1;
if (cmp > 0) {
/* there may be some preceding data */
while (first->prev->next) {
first = first->prev;
if (lyd_owner_module(first) == mod) {
cmp = 0;
break;
}
}
}
if (cmp == 0) {
/* there may be some preceding data belonging to this module */
while (first->prev->next) {
if (lyd_owner_module(first->prev) != mod) {
break;
}
first = first->prev;
}
}
if (cmp < 0) {
/* there may be some following data */
LY_LIST_FOR(first, first) {
if (lyd_owner_module(first) == mod) {
cmp = 0;
break;
}
}
}
if (cmp == 0) {
/* we have found the first module data node */
*node = first;
}
}
const struct lys_module *
lyd_mod_next_module(struct lyd_node *tree, const struct lys_module *module, const struct ly_ctx *ctx, uint32_t *i,
struct lyd_node **first)
{
struct lyd_node *iter;
const struct lys_module *mod;
/* get the next module */
if (module) {
if (*i) {
mod = NULL;
} else {
mod = module;
++(*i);
}
} else {
do {
mod = ly_ctx_get_module_iter(ctx, i);
} while (mod && !mod->implemented);
}
/* find its data */
*first = NULL;
if (mod) {
LY_LIST_FOR(tree, iter) {
if (lyd_owner_module(iter) == mod) {
*first = iter;
break;
}
}
}
return mod;
}
const struct lys_module *
lyd_data_next_module(struct lyd_node **next, struct lyd_node **first)
{
const struct lys_module *mod;
if (!*next) {
/* all data traversed */
*first = NULL;
return NULL;
}
*first = *next;
/* prepare next */
mod = lyd_owner_module(*next);
LY_LIST_FOR(*next, *next) {
if (lyd_owner_module(*next) != mod) {
break;
}
}
return mod;
}
LY_ERR
lyd_value_store(const struct ly_ctx *ctx, const struct lyd_node *lnode, struct lyd_value *val,
const struct lysc_type *type, const void *value, uint64_t value_size_bits, ly_bool is_utf8, ly_bool store_only,
ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_snode,
ly_bool *incomplete)
{
LY_ERR r;
struct ly_err_item *err = NULL;
uint32_t options = 0;
if (!value) {
value = "";
value_size_bits = 0;
}
if (incomplete) {
*incomplete = 0;
}
if (dynamic && *dynamic) {
options |= LYPLG_TYPE_STORE_DYNAMIC;
}
if (is_utf8) {
options |= LYPLG_TYPE_STORE_IS_UTF8;
}
if (store_only) {
options |= LYPLG_TYPE_STORE_ONLY;
}
r = LYSC_GET_TYPE_PLG(type->plugin_ref)->store(ctx, type, value, value_size_bits, options, format, prefix_data,
hints, ctx_snode, val, NULL, &err);
if (dynamic) {
*dynamic = 0;
}
if (r == LY_EINCOMPLETE) {
if (incomplete) {
*incomplete = 1;
}
} else if (r) {
if (err) {
ly_err_print(ctx, err, lnode, ctx_snode);
ly_err_free(err);
} else {
if (ctx_snode) {
LOG_LOCSET(ctx_snode);
}
LOGVAL(ctx, NULL, LYVE_OTHER, "Storing value failed.");
if (ctx_snode) {
LOG_LOCBACK(1);
}
}
return r;
}
return LY_SUCCESS;
}
LY_ERR
lyd_value_validate_incomplete(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *val,
const struct lyd_node *ctx_node, const struct lyd_node *tree)
{
LY_ERR ret;
struct ly_err_item *err = NULL;
struct lyplg_type *type_plg;
type_plg = LYSC_GET_TYPE_PLG(type->plugin_ref);
assert(type_plg && type_plg->validate_tree);
ret = type_plg->validate_tree(ctx, type, ctx_node, tree, val, &err);
if (ret) {
if (err) {
ly_err_print(ctx, err, ctx_node, NULL);
ly_err_free(err);
} else {
LOGVAL(ctx, ctx_node, LYVE_OTHER, "Resolving value \"%s\" failed.",
(char *)type_plg->print(ctx, val, LY_VALUE_CANON, NULL, NULL, NULL));
}
return ret;
}
return LY_SUCCESS;
}
LY_ERR
ly_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const void *value, uint64_t value_size_bits,
LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints)
{
LY_ERR rc = LY_SUCCESS;
struct ly_err_item *err = NULL;
struct lyd_value storage;
struct lysc_type *type;
LY_CHECK_ARG_RET(ctx, node, LY_EINVAL);
if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
LOGARG(ctx, node);
return LY_EINVAL;
}
type = ((struct lysc_node_leaf *)node)->type;
rc = LYSC_GET_TYPE_PLG(type->plugin_ref)->store(ctx ? ctx : node->module->ctx, type, value,
value_size_bits, 0, format, prefix_data, hints, node, &storage, NULL, &err);
if (rc == LY_EINCOMPLETE) {
/* actually success */
rc = LY_SUCCESS;
} else if (rc && err) {
if (ctx) {
/* log only in case the ctx was provided as input parameter */
ly_err_print(ctx, err, NULL, node);
}
ly_err_free(err);
}
if (!rc) {
LYSC_GET_TYPE_PLG(type->plugin_ref)->free(ctx ? ctx : node->module->ctx, &storage);
}
return rc;
}
LIBYANG_API_DEF LY_ERR
lyd_value_validate(const struct lysc_node *schema, const char *value, uint32_t value_len,
const struct lyd_node *ctx_node, const struct lysc_type **realtype, const char **canonical)
{
LY_CHECK_ARG_RET(NULL, schema, !value_len || value, LY_EINVAL);
return lyd_value_validate3(schema, value, value_len, LY_VALUE_JSON, NULL, LYD_HINT_DATA, ctx_node,
1, realtype, canonical);
}
LIBYANG_API_DEF LY_ERR
lyd_value_validate_dflt(const struct lysc_node *schema, const char *value, struct lysc_prefix *prefixes,
const struct lyd_node *ctx_node, const struct lysc_type **realtype, const char **canonical)
{
LY_CHECK_ARG_RET(NULL, schema, LY_EINVAL);
return lyd_value_validate3(schema, value, value ? strlen(value) : 0, LY_VALUE_SCHEMA_RESOLVED, prefixes,
LYD_HINT_SCHEMA, ctx_node, 1, realtype, canonical);
}
LY_ERR
lyd_value_validate3(const struct lysc_node *schema, const char *value, size_t value_len, LY_VALUE_FORMAT format,
void *prefix_data, uint32_t hints, const struct lyd_node *ctx_node, int log, const struct lysc_type **realtype,
const char **canonical)
{
LY_ERR rc;
const struct ly_ctx *ctx;
struct ly_err_item *err = NULL;
struct lysc_type *type;
struct lyd_value val = {0};
ly_bool stored = 0;
struct lyplg_type *type_plg;
ctx = schema->module->ctx;
if (!value_len) {
value = "";
}
type = ((struct lysc_node_leaf *)schema)->type;
type_plg = LYSC_GET_TYPE_PLG(type->plugin_ref);
/* store */
rc = type_plg->store(ctx, type, value, value_len * 8, 0, format, prefix_data, hints, schema, &val, NULL, &err);
if (!rc || (rc == LY_EINCOMPLETE)) {
stored = 1;
}
if (ctx_node && (rc == LY_EINCOMPLETE)) {
/* resolve */
rc = type_plg->validate_tree(ctx, type, ctx_node, ctx_node, &val, &err);
}
if (rc && (rc != LY_EINCOMPLETE) && err) {
if (log) {
/* log error */
ly_err_print(ctx, err, ctx_node, schema);
}
ly_err_free(err);
}
if (!rc || (rc == LY_EINCOMPLETE)) {
if (realtype) {
/* return realtype */
if (val.realtype->basetype == LY_TYPE_UNION) {
*realtype = val.subvalue->value.realtype;
} else {
*realtype = val.realtype;
}
}
if (canonical) {
/* return canonical value */
lydict_dup(ctx, LYSC_GET_TYPE_PLG(val.realtype->plugin_ref)->print(ctx, &val,
LY_VALUE_CANON, NULL, NULL, NULL), canonical);
}
}
if (stored) {
/* free value */
type_plg->free(ctx, &val);
}
return rc;
}
LIBYANG_API_DEF LY_ERR
lyd_value_compare(const struct lyd_node_term *node, const char *value, uint32_t value_len)
{
LY_ERR ret = LY_SUCCESS;
const struct ly_ctx *ctx;
struct lysc_type *type;
struct lyd_value val = {0};
struct lyplg_type *type_plg;
LY_CHECK_ARG_RET(node ? LYD_CTX(node) : NULL, node, value, LY_EINVAL);
ctx = LYD_CTX(node);
type = ((struct lysc_node_leaf *)node->schema)->type;
/* store the value */
LY_CHECK_RET(lyd_value_store(ctx, &node->node, &val, type, value, value_len * 8, 0, 0, NULL, LY_VALUE_JSON, NULL,
LYD_HINT_DATA, node->schema, NULL));
/* compare values */
type_plg = LYSC_GET_TYPE_PLG(type->plugin_ref);
ret = type_plg->compare(ctx, &node->value, &val);
type_plg->free(ctx, &val);
return ret;
}
LIBYANG_API_DEF ly_bool
lyd_is_default(const struct lyd_node *node)
{
const struct lysc_node_leaf *leaf;
const struct lysc_node_leaflist *llist;
LY_ARRAY_COUNT_TYPE u;
if (!(node->schema->nodetype & LYD_NODE_TERM)) {
return 0;
}
if (node->schema->nodetype == LYS_LEAF) {
leaf = (const struct lysc_node_leaf *)node->schema;
if (!leaf->dflt.str) {
return 0;
}
/* compare with the default value */
if (!lysc_value_cmp(node->schema, node, &leaf->dflt, lyd_get_value(node))) {
return 1;
}
} else {
llist = (const struct lysc_node_leaflist *)node->schema;
if (!llist->dflts) {
return 0;
}
LY_ARRAY_FOR(llist->dflts, u) {
/* compare with each possible default value */
if (!lysc_value_cmp(node->schema, node, &llist->dflts[u], lyd_get_value(node))) {
return 1;
}
}
}
return 0;
}
LIBYANG_API_DEF uint32_t
lyd_list_pos(const struct lyd_node *instance)
{
const struct lyd_node *iter = NULL;
uint32_t pos = 0;
if (!instance || !(instance->schema->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
return 0;
}
/* data instances are ordered, so we can stop when we found instance of other schema node */
for (iter = instance; iter->schema == instance->schema; iter = iter->prev) {
if (pos && (iter->next == NULL)) {
/* overrun to the end of the siblings list */
break;
}
++pos;
}
return pos;
}
LIBYANG_API_DEF struct lyd_node *
lyd_first_sibling(const struct lyd_node *node)
{
struct lyd_node *start;
if (!node) {
return NULL;
}
/* get the first sibling */
if (node->parent) {
return ((struct lyd_node_inner *)node->parent)->child;
} else if (!node->prev->next) {
return (struct lyd_node *)node;
}
for (start = (struct lyd_node *)node->prev; start->prev->next; start = start->prev) {
assert(start != node);
}
return start;
}
/**
* @brief Check list node parsed into an opaque node for the reason.
*
* @param[in] node Opaque node.
* @param[in] snode Schema node of @p opaq.
* @return LY_SUCCESS if the node is valid;
* @return LY_ERR on error.
*/
static LY_ERR
lyd_parse_opaq_list_error(const struct lyd_node *node, const struct lysc_node *snode)
{
LY_ERR ret = LY_SUCCESS;
struct ly_set key_set = {0};
const struct lysc_node *key = NULL;
const struct lyd_node *child;
const struct lyd_node_opaq *opaq_k;
uint32_t i;
assert(!node->schema);
/* get all keys into a set */
while ((key = lys_getnext(key, snode, NULL, 0)) && (key->flags & LYS_KEY)) {
LY_CHECK_GOTO(ret = ly_set_add(&key_set, (void *)key, 1, NULL), cleanup);
}
LY_LIST_FOR(lyd_child(node), child) {
/* find the key schema node */
for (i = 0; i < key_set.count; ++i) {
key = key_set.snodes[i];
if (!strcmp(key->name, LYD_NAME(child))) {
break;
}
}
if (i == key_set.count) {
/* some other node, skip */
continue;
}
/* key found */
ly_set_rm_index(&key_set, i, NULL);
if (child->schema) {
/* valid key */
continue;
}
/* check value */
opaq_k = (struct lyd_node_opaq *)child;
ret = ly_value_validate(LYD_CTX(node), key, opaq_k->value, strlen(opaq_k->value) * 8, opaq_k->format,
opaq_k->val_prefix_data, opaq_k->hints);
LY_CHECK_GOTO(ret, cleanup);
}
if (key_set.count) {
/* missing keys */
LOGVAL(LYD_CTX(node), node->parent, LY_VCODE_NOKEY, key_set.snodes[0]->name);
ret = LY_EVALID;
goto cleanup;
}
cleanup:
ly_set_erase(&key_set, NULL);
return ret;
}
LIBYANG_API_DEF LY_ERR
lyd_parse_opaq_error(const struct lyd_node *node)
{
LY_ERR rc = LY_SUCCESS;
const struct ly_ctx *ctx;
const struct lyd_node_opaq *opaq;
const struct lyd_node *parent;
const struct lys_module *mod;
const struct lysc_node *sparent, *snode;
uint32_t loc_scnode = 0;
LY_CHECK_ARG_RET(LYD_CTX(node), node, !node->schema, LY_EINVAL);
ctx = LYD_CTX(node);
opaq = (struct lyd_node_opaq *)node;
parent = node->parent;
sparent = lyd_node_schema(parent);
if (!opaq->name.module_ns) {
LOGVAL(ctx, parent, LYVE_REFERENCE, "Unknown module of node \"%s\".", opaq->name.name);
rc = LY_EVALID;
goto cleanup;
}
/* module */
switch (opaq->format) {
case LY_VALUE_XML:
if (!sparent || strcmp(opaq->name.module_ns, sparent->module->ns)) {
mod = ly_ctx_get_module_implemented_ns(ctx, opaq->name.module_ns);
if (!mod) {
LOGVAL(ctx, parent, LYVE_REFERENCE, "No (implemented) module with namespace \"%s\" of node \"%s\" in the context.",
opaq->name.module_ns, opaq->name.name);
rc = LY_EVALID;
goto cleanup;
}
} else {
/* inherit */
mod = sparent->module;
}
break;
case LY_VALUE_JSON:
case LY_VALUE_LYB:
if (!sparent || strcmp(opaq->name.module_name, sparent->module->name)) {
mod = ly_ctx_get_module_implemented(ctx, opaq->name.module_name);
if (!mod) {
LOGVAL(ctx, parent, LYVE_REFERENCE, "No (implemented) module named \"%s\" of node \"%s\" in the context.",
opaq->name.module_name, opaq->name.name);
rc = LY_EVALID;
goto cleanup;
}
} else {
/* inherit */
mod = sparent->module;
}
break;
default:
LOGERR(ctx, LY_EINVAL, "Unsupported value format.");
rc = LY_EINVAL;
goto cleanup;
}
/* schema */
snode = lys_find_child(LYD_CTX(node), sparent, mod, NULL, 0, opaq->name.name, 0, 0);
if (!snode && sparent && (sparent->nodetype & (LYS_RPC | LYS_ACTION))) {
/* maybe output node */
snode = lys_find_child(LYD_CTX(node), sparent, mod, NULL, 0, opaq->name.name, 0, LYS_GETNEXT_OUTPUT);
}
if (!snode) {
if (sparent) {
LOGVAL(ctx, parent, LYVE_REFERENCE, "Node \"%s\" not found as a child of \"%s\" node.", opaq->name.name,
sparent->name);
} else {
LOGVAL(ctx, parent, LYVE_REFERENCE, "Node \"%s\" not found in the \"%s\" module.", opaq->name.name, mod->name);
}
rc = LY_EVALID;
goto cleanup;
}
/* schema node exists */
LOG_LOCSET(snode);
loc_scnode = 1;
if (snode->nodetype & LYD_NODE_TERM) {
/* leaf / leaf-list */
rc = ly_value_validate(ctx, snode, opaq->value, strlen(opaq->value) * 8, opaq->format, opaq->val_prefix_data,
opaq->hints);
LY_CHECK_GOTO(rc, cleanup);
} else if (snode->nodetype == LYS_LIST) {
/* list */
rc = lyd_parse_opaq_list_error(node, snode);
LY_CHECK_GOTO(rc, cleanup);
} else if (snode->nodetype & LYD_NODE_INNER) {
/* inner node */
if (opaq->value) {
LOGVAL(ctx, NULL, LYVE_DATA, "Invalid value \"%s\" for %s \"%s\".", opaq->value,
lys_nodetype2str(snode->nodetype), snode->name);
rc = LY_EVALID;
goto cleanup;
}
} else {
LOGERR(ctx, LY_EINVAL, "Unexpected opaque schema node %s \"%s\".", lys_nodetype2str(snode->nodetype), snode->name);
rc = LY_EINVAL;
goto cleanup;
}
LOGERR(ctx, LY_EINVAL, "Unexpected valid opaque node %s \"%s\".", lys_nodetype2str(snode->nodetype), snode->name);
rc = LY_EINVAL;
cleanup:
LOG_LOCBACK(loc_scnode);
return rc;
}
LIBYANG_API_DEF const char *
lyd_value_get_canonical(const struct ly_ctx *ctx, const struct lyd_value *value)
{
LY_CHECK_ARG_RET(ctx, ctx, value, NULL);
return value->_canonical ? value->_canonical :
(const char *)LYSC_GET_TYPE_PLG(value->realtype->plugin_ref)->print(ctx, value, LY_VALUE_CANON, NULL, NULL, NULL);
}
LIBYANG_API_DEF LY_ERR
lyd_any_value_str(const struct lyd_node *any, LYD_FORMAT format, char **value_str)
{
const struct lyd_node_any *a;
LY_CHECK_ARG_RET(NULL, any, value_str, LY_EINVAL);
LY_CHECK_ARG_RET(NULL, any->schema, any->schema->nodetype & LYS_ANYDATA, LY_EINVAL);
*value_str = NULL;
a = (struct lyd_node_any *)any;
if (!a->child && !a->value) {
/* there is no value */
return LY_SUCCESS;
}
if (a->child) {
/* print into a string */
LY_CHECK_RET(lyd_print_mem(value_str, a->child, format, LYD_PRINT_SIBLINGS));
} else {