-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy pathtree_data.h
More file actions
2706 lines (2500 loc) · 128 KB
/
Copy pathtree_data.h
File metadata and controls
2706 lines (2500 loc) · 128 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.h
* @author Radek Krejci <rkrejci@cesnet.cz>
* @author Michal Vasko <mvasko@cesnet.cz>
* @brief libyang representation of YANG 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
*/
#ifndef LY_TREE_DATA_H_
#define LY_TREE_DATA_H_
/* socket/ip includes in ly_config.h */
#include <stddef.h>
#include <stdint.h>
#include <time.h>
#include "log.h"
#include "ly_config.h"
#include "tree.h"
#include "tree_schema.h"
#ifdef __cplusplus
extern "C" {
#endif
struct ly_ctx;
struct ly_path;
struct ly_set;
struct lyd_node;
struct lyd_node_opaq;
struct lyd_node_term;
struct timespec;
struct lyxp_var;
struct rb_node;
/**
* @page howtoData Data Instances
*
* All the nodes in data tree comes are based on ::lyd_node structure. According to the content of the ::lyd_node.schema
* it can be cast to several other structures.
*
* In case the ::lyd_node.schema pointer is NULL, the node is actually __opaq__ and can be safely cast to ::lyd_node_opaq.
* The opaq node represents an **unknown node** which wasn't mapped to any [(compiled) schema](@ref howtoSchema) node in the
* context. But it may represent a schema node data instance which is invalid. That may happen if the value (leaf/leaf-list)
* is invalid or there are invalid/missing keys of a list instance. Such a node can appear in several places in the data tree.
* - As a part of the tree structure, but only in the case the ::LYD_PARSE_OPAQ option was used when input data were
* [parsed](@ref howtoDataParsers), because unknown data instances are ignored and invalid data produce errors by default.
* - As a representation of YANG anydata/anyxml content.
* - As envelopes of standard data tree instances (RPCs, actions or Notifications).
*
* In case the data node has its definition in a [compiled schema tree](@ref howtoSchema), the structure of the data node is
* actually one of the followings according to the schema node's nodetype (::lysc_node.nodetype).
* - ::lyd_node_inner - represents data nodes corresponding to schema nodes matching ::LYD_NODE_INNER nodetypes. They provide
* structure of the tree by having children nodes.
* - ::lyd_node_term - represents data nodes corresponding to schema nodes matching ::LYD_NODE_TERM nodetypes. The terminal
* nodes provide values of the particular configuration/status information. The values are represented as ::lyd_value
* structure with string representation of the value (retrieved by ::lyd_get_value() and ::lyd_get_meta_value()) and
* the type specific data stored in the structure's union according to the real type of the value (::lyd_value.realtype).
* The string representation provides canonical representation of the value in case the type has the canonical
* representation specified. Otherwise, it is the original value or, in case the value can contain prefixes, the JSON
* format is used to make the value unambiguous.
* - ::lyd_node_any - represents data nodes corresponding to schema nodes matching ::LYD_NODE_ANY nodetypes.
*
* Despite all the aforementioned structures and their members are available as part of the libyang API and callers can use
* it to navigate through the data tree structure or to obtain various information, we recommend to use the following macros
* and functions.
* - ::lyd_child() (or ::lyd_child_no_keys()) and ::lyd_parent() to get the node's child/parent node.
* - ::LYD_CTX to get libyang context from a data node.
* - ::lyd_get_value()/::lyd_get_meta_value() to get canonical string value from a terminal node/metadata instance.
* - ::LYD_TREE_DFS_BEGIN and ::LYD_TREE_DFS_END to traverse the data tree (depth-first).
* - ::LY_LIST_FOR and ::LY_ARRAY_FOR as described on @ref howtoStructures page.
*
* Instead of going through the data tree on your own, a specific data node can be also located using a wide set of
* \b lyd_find_*() functions.
*
* More information about specific operations with data instances can be found on the following pages:
* - @subpage howtoDataParsers
* - @subpage howtoDataValidation
* - @subpage howtoDataWD
* - @subpage howtoDataManipulation
* - @subpage howtoDataPrinters
* - @subpage howtoDataLYB
*
* \note API for this group of functions is described in the [Data Instances module](@ref datatree).
*
* Functions List (not assigned to above subsections)
* --------------------------------------------------
* - ::lyd_child()
* - ::lyd_child_no_keys()
* - ::lyd_parent()
* - ::lyd_owner_module()
* - ::lyd_get_value()
* - ::lyd_get_meta_value()
* - ::lyd_find_xpath()
* - ::lyd_find_path()
* - ::lyd_find_target()
* - ::lyd_find_sibling_val()
* - ::lyd_find_sibling_first()
* - ::lyd_find_sibling_opaq_next()
* - ::lyd_find_meta()
*
* - ::lyd_path()
* - ::lyd_find_target()
*
* @section howtoDataMetadata Metadata Support
*
* YANG Metadata annotations are defined in [RFC 7952](https://tools.ietf.org/html/rfc7952) as YANG extension (and libyang
* [implements them as internal extension plugin](@ref howtoPluginsExtensions)). In practice, it allows to have XML
* attributes (there is also a special encoding for JSON) in YANG modeled data. libyang does not allow to have any XML
* attribute without the appropriate annotation definition describing the data as it is done e.g. for leafs. When an
* attribute without a matching annotation definition is found in the input data, it is:
* - silently dropped (with warning) or
* - an error is reported in case the ::LYD_PARSE_STRICT parser option is provided to the
* [parser function](@ref howtoDataParsers) or
* - stored into a generic ::lyd_attr structure without a connection with any YANG module in case the ::LYD_PARSE_OPAQ
* parser options is provided to the [parser function](@ref howtoDataParsers).
*
* There are some XML attributes, described by [YANG](https://tools.ietf.org/html/rfc7950) and
* [NETCONF](https://tools.ietf.org/html/rfc6241) specifications, which are not defined as annotations, but libyang
* implements them this way. In case of attributes in the YANG namespace (`insert`, `value` and `key` attributes
* for the NETCONF edit-config operation), they are defined in special libyang's internal module `yang`, which is
* available in each context and the content of this schema can be printed via
* [schema printers](@ref howtoSchemaPrinters).
*
* In case of the attributes described in [NETCONF specification](https://tools.ietf.org/html/rfc6241), the libyang's
* annotations structures are hidden and cannot be printed despite, internally, they are part of the `ietf-netconf`'s
* schema structure. Therefore, these attributes are available only when the `ietf-netconf` schema is loaded in the
* context. The definitions of these annotations are as follows:
*
* md:annotation operation {
* type enumeration {
* enum merge;
* enum replace;
* enum create;
* enum delete;
* enum remove;
* }
* }
*
* md:annotation type {
* type enumeration {
* enum subtree;
* enum xpath {
* if-feature "nc:xpath";
* }
* }
* }
*
* md:annotation select {
* type string;
* }
*
* Note, that, following the specification,
* - the `type` and `select` XML attributes are supposed to be unqualified (without namespace) and that
* - the `select`'s content is XPath and it is internally transformed by libyang into the format where the
* XML namespace prefixes are replaced by the YANG module names.
*
*
* @section howtoDataYangdata yang-data Support
*
* [RFC 8040](https://tools.ietf.org/html/rfc8040) defines ietf-restconf module, which includes yang-data extension. Despite
* the definition in the RESTCONF YANG module, the yang-data concept is quite generic and used even in modules without a
* connection to RESTCONF protocol. The extension allows to define a separated YANG trees usable separately from any
* datastore.
*
* libyang implements support for yang-data internally as an [extension plugin](@ref howtoPluginsExtensions).
*
* @section howtoDataMountpoint mount-point Support
*
* [RFC 8528](https://tools.ietf.org/html/rfc8528) defines mount-point extension in ietf-yang-schema-mount YANG module.
* This extension is supported out-of-the-box but to be able to parse data in a mount point, additional run-time data
* need to be provided by a callback:
*
* - ::ly_ctx_set_ext_data_clb()
*
* The mounted data can be parsed directly from data files or created manually using the standard functions. However,
* note that the mounted data use **their own context** created as needed. For *inline* data this means that any new
* request for a mount-point schema node results in a new context creation because it is impossible to determine
* whether any existing context can be used. Also, all these contexts created for the mounted data are **never**
* freed automatically except when the parent context is being freed. So, to avoid redundant context creation, it is
* always advised to use *shared-schema* for mount-points.
*
* In case it is not possible and *inline* mount point must be defined, it is still possible to avoid creating
* additional contexts. When the top-level node right under a schema node with a mount-point is created, always use
* this node for creation of any descendants. So, when using ::lyd_new_path(), use the node as `parent` and specify
* relative `path`.
*
* There is an example (in `libyang/examples/schema_mount`) showing simple YANG data parse and print that include some
* mounted data.
*/
/**
* @page howtoDataManipulation Manipulating Data
*
* There are many functions to create or modify an existing data tree. You can add new nodes, reconnect nodes from
* one tree to another (or e.g. from one list instance to another) or remove nodes. The functions doesn't allow you
* to put a node to a wrong place (by checking the YANG module structure), but not all validation checks can be made directly
* (or you have to make a valid change by multiple tree modifications) when the tree is being changed. Therefore,
* the [validation process](@ref howtoDataValidation) is expected to be invoked after changing the data tree to make sure
* that the changed data tree is valid.
*
* When inserting a node into data tree (no matter if the node already exists, via ::lyd_insert_child() and
* ::lyd_insert_sibling(), or a new node is being created), the node is automatically inserted to the place respecting the
* nodes order from the YANG schema. A leaf-list instances are sorted based on the value and the ::lyplg_type_sort_clb
* function defined in the given datatype. A list instances are ordered similarly based on keys. In case the node is opaq
* (it is not connected with any schema node), it is placed to the end of the sibling node in the order they are inserted in.
* The only situation when it is possible to influence the order of the nodes is the order of user-ordered list/leaf-list
* instances. In such a case the ::lyd_insert_after(), ::lyd_insert_before() can be used and ::lyd_insert_child(),
* ::lyd_insert_sibling() adds the node after the existing instance of the closest preceding sibling node from the schema.
*
* Creating data is generally possible in two ways, they can be combined. You can add nodes one-by-one based on
* the node name and/or its parent (::lyd_new_inner(), ::lyd_new_term(), ::lyd_new_any(), ::lyd_new_list(), ::lyd_new_list2()
* and ::lyd_new_opaq()) or address the nodes using a [simple XPath addressing](@ref howtoXPath) (::lyd_new_path() and
* ::lyd_new_path2()). The latter enables to create a whole path of nodes, requires less information
* about the modified data, and is generally simpler to use. Actually the third way is duplicating the existing data using
* ::lyd_dup_single(), ::lyd_dup_siblings() and ::lyd_dup_meta_single().
*
* The [metadata](@ref howtoDataMetadata) (and attributes in opaq nodes) can be created with ::lyd_new_meta()
* and ::lyd_new_attr().
*
* Changing value of a terminal node (leaf, leaf-list) is possible with ::lyd_change_term(). Similarly, the metadata value
* can be changed with ::lyd_change_meta(). Before changing the value, it might be useful to compare the node's value
* with a string value (::lyd_value_compare()) or verify that the new string value is correct for the specific data node
* (::lyd_value_validate()).
*
* Working with two existing subtrees can also be performed two ways. Usually, you would use lyd_insert*() functions.
* They are generally meant for simple inserts of a node into a data tree. For more complicated inserts and when
* merging 2 trees use ::lyd_merge_tree() or ::lyd_merge_siblings(). It offers additional options and is basically a more
* powerful insert.
*
* Besides merging, libyang is also capable to provide information about differences between two data trees. For this purpose,
* ::lyd_diff_tree() and ::lyd_diff_siblings() generates annotated data trees which can be, in addition, used to change one
* data tree to another one using ::lyd_diff_apply_all(), ::lyd_diff_apply_module() and ::lyd_diff_reverse_all(). Multiple
* diff data trees can be also put together for further work using ::lyd_diff_merge_all(), ::lyd_diff_merge_module() and
* ::lyd_diff_merge_tree() functions. To just check equivalence of the data nodes, ::lyd_compare_single(),
* ::lyd_compare_siblings() and ::lyd_compare_meta() can be used.
*
* To remove a node or subtree from a data tree, use ::lyd_unlink_tree() and then free the unwanted data using
* ::lyd_free_all() (or other \b lyd_free_*() functions).
*
* Also remember, that when you are creating/inserting a node, all the objects in that operation must belong to the
* same context.
*
* Modifying the single data tree in multiple threads is not safe.
*
* Functions List
* --------------
* - ::lyd_new_inner()
* - ::lyd_new_term()
* - ::lyd_new_list()
* - ::lyd_new_list2()
* - ::lyd_new_list3()
* - ::lyd_new_any()
* - ::lyd_new_opaq()
* - ::lyd_new_opaq2()
* - ::lyd_new_attr()
* - ::lyd_new_attr2()
* - ::lyd_new_meta()
* - ::lyd_new_path()
* - ::lyd_new_path2()
*
* - ::lyd_dup_single()
* - ::lyd_dup_siblings()
* - ::lyd_dup_meta_single()
*
* - ::lyd_insert_child()
* - ::lyd_insert_sibling()
* - ::lyd_insert_after()
* - ::lyd_insert_before()
*
* - ::lyd_value_compare()
* - ::lyd_value_validate()
*
* - ::lyd_change_term()
* - ::lyd_change_term_canon()
* - ::lyd_change_meta()
*
* - ::lyd_compare_single()
* - ::lyd_compare_siblings()
* - ::lyd_compare_meta()
* - ::lyd_diff_tree()
* - ::lyd_diff_siblings()
* - ::lyd_diff_apply_all()
* - ::lyd_diff_apply_module()
* - ::lyd_diff_reverse_all()
* - ::lyd_diff_merge_all()
* - ::lyd_diff_merge_module()
* - ::lyd_diff_merge_tree()
*
* - ::lyd_merge_tree()
* - ::lyd_merge_siblings()
* - ::lyd_merge_module()
*
* - ::lyd_unlink_tree()
*
* - ::lyd_free_all()
* - ::lyd_free_siblings()
* - ::lyd_free_tree()
* - ::lyd_free_meta_single()
* - ::lyd_free_meta_siblings()
* - ::lyd_free_attr_single()
* - ::lyd_free_attr_siblings()
*
* - ::lyd_any_value_str()
* - ::lyd_any_copy_value()
*/
/**
* @page howtoDataWD Default Values
*
* libyang provides support for work with default values as defined in [RFC 6243](https://tools.ietf.org/html/rfc6243).
* However, libyang context do not contains the *ietf-netconf-with-defaults* module on its own and caller is supposed to
* add this YANG module to enable full support of the *with-defaults* features described below. Without presence of the
* mentioned module in the context, the default nodes are still present and handled in the data trees, but the metadata
* providing the information about the default values cannot be used. It means that when parsing data, the default nodes
* marked with the metadata as implicit default nodes are handled as explicit data and when printing data tree, the expected
* nodes are printed without the ietf-netconf-with-defaults metadata.
*
* The RFC document defines 4 modes for handling default nodes in a data tree, libyang adds the fifth mode and use them
* via @ref dataprinterflags when printing data trees.
* - \b explicit - Only the explicitly set configuration data. But in the case of status data, missing default
* data are added into the tree. In libyang, this mode is represented by ::LYD_PRINT_WD_EXPLICIT option.
* This is the default with-defaults mode of the printer. The data nodes do not contain any additional
* metadata information.
* - \b trim - Data nodes containing the default value are removed. This mode is applied with ::LYD_PRINT_WD_TRIM option.
* - \b report-all - This mode provides all the default data nodes despite they were explicitly present in source data or
* they were added by libyang's [validation process](@ref howtoDataValidation). This mode is activated by
* ::LYD_PRINT_WD_ALL option.
* - \b report-all-tagged - In this case, all the data nodes (implicit as well the explicit) containing the default value
* are printed and tagged (see the note below). Printers accept ::LYD_PRINT_WD_ALL_TAG option for this mode.
* - \b report-implicit-tagged - The last mode is similar to the previous one, except only the implicitly added nodes
* are tagged. This is the libyang's extension and it is activated by ::LYD_PRINT_WD_IMPL_TAG option.
*
* Internally, libyang adds the default nodes into the data tree as part of the [validation process](@ref howtoDataValidation).
* When [parsing data](@ref howtoDataParsers) from an input source, adding default nodes can be avoided only by avoiding
* the whole [validation process](@ref howtoDataValidation). In case the ietf-netconf-with-defaults module is present in the
* context, the [parser process](@ref howtoDataParsers) also supports to recognize the implicit default nodes marked with the
* appropriate metadata.
*
* Note, that in a modified data tree (via e.g. \b lyd_insert_*() or \b lyd_free_*() functions), some of the default nodes
* can be missing or they can be present by mistake. Such a data tree is again corrected during the next run of the
* [validation process](@ref howtoDataValidation) or manualy using \b lyd_new_implicit_*() functions.
*
* The implicit (default) nodes, created by libyang, are marked with the ::LYD_DEFAULT flag in ::lyd_node.flags member
* Note, that besides leafs and leaf-lists, the flag can appear also in containers, where it means that the container
* holds only a default node(s) or it is implicitly added empty container (according to YANG 1.1 spec, all such containers are part of
* the accessible data tree). When printing data trees, the presence of empty containers (despite they were added
* explicitly or implicitly as part of accessible data tree) depends on ::LYD_PRINT_KEEPEMPTYCONT option.
*
* To get know if the particular leaf or leaf-list node contains default value (despite implicit or explicit), you can
* use ::lyd_is_default() function.
*
* Functions List
* --------------
* - ::lyd_is_default()
* - ::lyd_new_implicit_all()
* - ::lyd_new_implicit_module()
* - ::lyd_new_implicit_tree()
*/
/**
* @page howtoDataLYB LYB Binary Format
*
* LYB (LibYang Binary) is a proprietary libyang binary data and file format. Its primary purpose is efficient
* serialization (printing) and deserialization (parsing). With this goal in mind, every term node value is stored
* in its new binary format specification according to its type. Following is the format for all types with explicit
* support out-of-the-box (meaning that have a special type plugin). Any derived types inherit the format of its
* closest type with explicit support (up to a built-in type).
*
* @section howtoDataLYBTypes Format of specific data type values
*/
/**
* @ingroup trees
* @defgroup datatree Data Tree
* @{
*
* Data structures and functions to manipulate and access instance data tree.
*/
/* *INDENT-OFF* */
/**
* @brief Macro to iterate via all elements in a data tree. This is the opening part
* to the #LYD_TREE_DFS_END or #LYD_TREE_ANY_DFS_END - either must always be used with this macro.
*
* The function follows deep-first search algorithm:
* <pre>
* 1
* / \
* 2 4
* / / \
* 3 5 6
* </pre>
*
* Use the same parameters for #LYD_TREE_DFS_BEGIN and #LYD_TREE_DFS_END. While
* START can be any of the lyd_node* types, ELEM variable must be a pointer to
* the generic struct lyd_node.
*
* To skip a particular subtree, instead of the continue statement, set LYD_TREE_DFS_continue
* variable to non-zero value.
*
* Use with opening curly bracket '{' after the macro.
*
* @param START Pointer to the starting element processed first.
* @param ELEM Iterator intended for use in the block.
*/
#define LYD_TREE_DFS_BEGIN(START, ELEM) \
{ ly_bool LYD_TREE_DFS_continue = 0; struct lyd_node *LYD_TREE_DFS_next; \
for ((ELEM) = (LYD_TREE_DFS_next) = (struct lyd_node *)(START); \
(ELEM); \
(ELEM) = (LYD_TREE_DFS_next), LYD_TREE_DFS_continue = 0)
/**
* @brief Internal DFS END macro not to be used directly.
*/
#define _LYD_TREE_DFS_END(START, ELEM, FUNC) \
/* select element for the next run - children first */ \
if (LYD_TREE_DFS_continue) { \
(LYD_TREE_DFS_next) = NULL; \
} else { \
(LYD_TREE_DFS_next) = FUNC(ELEM); \
}\
if (!(LYD_TREE_DFS_next)) { \
/* no children */ \
if ((ELEM) == (struct lyd_node *)(START)) { \
/* we are done, (START) has no children */ \
break; \
} \
/* try siblings */ \
(LYD_TREE_DFS_next) = (ELEM)->next; \
} \
while (!(LYD_TREE_DFS_next)) { \
/* parent is already processed, go to its sibling */ \
(ELEM) = (struct lyd_node *)(ELEM)->parent; \
/* no siblings, go back through parents */ \
if ((ELEM)->parent == (START)->parent) { \
/* we are done, no next element to process */ \
break; \
} \
(LYD_TREE_DFS_next) = (ELEM)->next; \
} }
/**
* @brief Macro to iterate via all elements in a tree. This is the closing part
* to the #LYD_TREE_DFS_BEGIN - they always have to be used together.
*
* Returns children using ::lyd_child().
*
* Use the same parameters for #LYD_TREE_DFS_BEGIN and #LYD_TREE_DFS_END. While
* START can be any of the lyd_node* types, ELEM variable must be a pointer
* to the generic struct lyd_node.
*
* Use with closing curly bracket '}' after the macro.
*
* @param START Pointer to the starting element processed first.
* @param ELEM Iterator intended for use in the block.
*/
#define LYD_TREE_DFS_END(START, ELEM) \
_LYD_TREE_DFS_END(START, ELEM, lyd_child)
/**
* @brief Macro to iterate via all elements in a tree including anyxml and anydata children. This is the closing part
* to the #LYD_TREE_DFS_BEGIN - they always have to be used together.
*
* Returns children using ::lyd_child_any().
*
* Use the same parameters for #LYD_TREE_DFS_BEGIN and #LYD_TREE_ANY_DFS_END. While
* START can be any of the lyd_node* types, ELEM variable must be a pointer
* to the generic struct lyd_node.
*
* Use with closing curly bracket '}' after the macro.
*
* @param START Pointer to the starting element processed first.
* @param ELEM Iterator intended for use in the block.
*/
#define LYD_TREE_ANY_DFS_END(START, ELEM) \
_LYD_TREE_DFS_END(START, ELEM, lyd_child_any)
/**
* @brief Macro to iterate via all schema node data instances in data siblings.
*
* @param START Pointer to the starting sibling. Even if it is not first, all the siblings are searched.
* @param SCHEMA Schema node of the searched instances.
* @param ELEM Iterator.
*/
#define LYD_LIST_FOR_INST(START, SCHEMA, ELEM) \
for (lyd_find_sibling_val(START, SCHEMA, NULL, 0, &(ELEM)); \
(ELEM) && ((ELEM)->schema == (SCHEMA)); \
(ELEM) = (ELEM)->next)
/**
* @brief Macro to iterate via all schema node data instances in data siblings allowing to modify the list itself.
*
* @param START Pointer to the starting sibling. Even if it is not first, all the siblings are searched.
* @param SCHEMA Schema node of the searched instances.
* @param NEXT Temporary storage to allow removing of the current iterator content.
* @param ELEM Iterator.
*/
#define LYD_LIST_FOR_INST_SAFE(START, SCHEMA, NEXT, ELEM) \
for ((NEXT) = (ELEM) = NULL, lyd_find_sibling_val(START, SCHEMA, NULL, 0, &(ELEM)); \
(ELEM) && ((ELEM)->schema == (SCHEMA)) ? ((NEXT) = (ELEM)->next, 1) : 0; \
(ELEM) = (NEXT))
/* *INDENT-ON* */
/**
* @brief Macro to get context from a data tree node.
*/
#define LYD_CTX(node) ((node)->schema ? (node)->schema->module->ctx : ((const struct lyd_node_opaq *)(node))->ctx)
/**
* @brief Find out if the node is the only instance, i.e. it has no siblings with the same schema.
*
* @param[in] NODE Pointer to the struct lyd_node.
* @return 1 @p NODE is a single instance (is alone).
* @return 0 @p NODE is not alone.
*/
#define LYD_NODE_IS_ALONE(NODE) \
(((NODE)->prev == NODE) || \
(((NODE)->prev->schema != (NODE)->schema) && (!(NODE)->next || ((NODE)->schema != (NODE)->next->schema))))
/**
* @brief Data input/output formats supported by libyang [parser](@ref howtoDataParsers) and
* [printer](@ref howtoDataPrinters) functions.
*/
typedef enum {
LYD_UNKNOWN = 0, /**< unknown data format, invalid value */
LYD_XML, /**< XML instance data format */
LYD_JSON, /**< JSON instance data format */
LYD_LYB /**< LYB instance data format */
} LYD_FORMAT;
/** @} */
/**
* @brief YANG data representation
*/
struct lyd_value {
const char *_canonical; /**< Should never be accessed directly, instead ::lyd_get_value() and ::lyd_get_meta_value()
should be used. Serves as a cache for the canonical value or the JSON
representation if no canonical value is defined. */
const struct lysc_type *realtype; /**< pointer to the real type of the data stored in the value structure. This type can differ from the type
in the schema node of the data node since the type's store plugin can use other types/plugins for
storing data. Speaking about built-in types, this is the case of leafref which stores data as its
target type. In contrast, union type also uses its subtype's callbacks, but inside an internal data
stored in subvalue member of ::lyd_value structure, so here is the pointer to the union type.
In general, this type is used to get free callback for this lyd_value structure, so it must reflect
the type used to store data directly in the same lyd_value instance. */
union {
int8_t boolean; /**< 0 as false, 1 as true */
int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */
int8_t int8; /**< 8-bit signed integer */
int16_t int16; /**< 16-bit signed integer */
int32_t int32; /**< 32-bit signed integer */
int64_t int64; /**< 64-bit signed integer */
uint8_t uint8; /**< 8-bit unsigned integer */
uint16_t uint16; /**< 16-bit unsigned integer */
uint32_t uint32; /**< 32-bit unsigned integer */
uint64_t uint64; /**< 64-bit unsigned integer */
struct lysc_type_bitenum_item *enum_item; /**< pointer to the definition of the enumeration value */
struct lysc_ident *ident; /**< pointer to the schema definition of the identityref value */
struct ly_path *target; /**< Instance-identifier target path, use ::lyd_find_target() to evaluate
it on data. */
struct lyd_value_union *subvalue; /** Union value with some metadata. */
void *dyn_mem; /**< pointer to generic data type value stored in dynamic memory */
uint8_t fixed_mem[LYD_VALUE_FIXED_MEM_SIZE]; /**< fixed-size buffer for a generic data type value */
}; /**< The union is just a list of shorthands to possible values stored by a type's plugin. libyang itself uses the ::lyd_value.realtype
plugin's callbacks to work with the data.*/
};
/**
* @brief Get the value in format specific to the type.
*
* Should be used for any types that do not have their specific representation in the ::lyd_value union.
*
* @param[in] value Pointer to the value structure to read from (struct ::lyd_value *).
* @param[out] type_val Pointer to the type-specific value structure.
*/
#define LYD_VALUE_GET(value, type_val) \
((sizeof *(type_val) > LYD_VALUE_FIXED_MEM_SIZE) \
? ((type_val) = (((value)->dyn_mem))) \
: ((type_val) = ((void *)((value)->fixed_mem))))
/**
* @brief Special lyd_value structure for built-in union values.
*
* Represents data with multiple types (union). The ::lyd_value_union.value contains representation according to
* one of the union's types. The ::lyd_value_union.prefix_data provides (possible) mappings from prefixes in
* the original value to YANG modules. These prefixes are necessary to parse original value to the union's subtypes.
*/
struct lyd_value_union {
struct lyd_value value; /**< representation of the value according to the selected union's subtype
(stored as ::lyd_value.realtype here) */
void *original; /**< Original value. */
uint64_t orig_size_bits; /**< Original value size in bits. */
uint32_t hints; /**< [Value hints](@ref lydvalhints) from the parser */
LY_VALUE_FORMAT format; /**< Prefix format of the value. However, this information is also used to decide
whether a value is valid for the specific format or not on later validations
(instance-identifier in XML looks different than in JSON). */
void *prefix_data; /**< Format-specific data for prefix resolution (see ly_resolve_prefix()) */
const struct lysc_node *ctx_node; /**< Context schema node. */
};
/**
* @brief Special lyd_value structure for built-in bits values.
*/
struct lyd_value_bits {
char *bitmap; /**< bitmap of size ::lyplg_type_bits_bitmap_size(), if its value is
cast to an integer type of the corresponding size, can be used
directly as a bitmap */
struct lysc_type_bitenum_item **items; /**< list of set pointers to the specification of the set
bits ([sized array](@ref sizedarrays)) */
};
/**
* @brief Special lyd_value structure for built-in binary values.
*/
struct lyd_value_binary {
void *data; /**< pointer to the binary value */
uint32_t size; /**< size of @p data value in bytes */
};
/**
* @brief Special lyd_value structure for ietf-inet-types ipv4-address-no-zone values.
*/
struct lyd_value_ipv4_address_no_zone {
struct in_addr addr; /**< IPv4 address in binary */
};
/**
* @brief Special lyd_value structure for ietf-inet-types ipv4-address values.
*/
struct lyd_value_ipv4_address {
struct in_addr addr; /**< IPv4 address in binary */
const char *zone; /**< Optional address zone */
};
/**
* @brief Special lyd_value structure for ietf-inet-types ipv4-prefix values.
*/
struct lyd_value_ipv4_prefix {
struct in_addr addr; /**< IPv4 host address in binary */
uint8_t prefix; /**< prefix length (0 - 32) */
};
/**
* @brief Special lyd_value structure for ietf-inet-types ipv6-address-no-zone values.
*/
struct lyd_value_ipv6_address_no_zone {
struct in6_addr addr; /**< IPv6 address in binary */
};
/**
* @brief Special lyd_value structure for ietf-inet-types ipv6-address values.
*/
struct lyd_value_ipv6_address {
struct in6_addr addr; /**< IPv6 address in binary */
const char *zone; /**< Optional address zone */
};
/**
* @brief Special lyd_value structure for ietf-inet-types ipv6-prefix values.
*/
struct lyd_value_ipv6_prefix {
struct in6_addr addr; /**< IPv6 host address in binary */
uint8_t prefix; /**< prefix length (0 - 128) */
};
/**
* @brief Special lyd_value structure for ietf-yang-types date-and-time values.
*/
struct lyd_value_date_and_time {
time_t time; /**< UNIX timestamp */
char *fractions_s; /**< Optional fractions of a second */
ly_bool unknown_tz; /**< Whether the value is in the special Z/-00:00 timezone. */
};
/**
* @brief Special lyd_value structure for ietf-yang-types date values.
*/
struct lyd_value_date {
time_t time; /**< UNIX timestamp */
ly_bool unknown_tz; /**< Whether the value is in the special Z/-00:00 timezone. */
};
/**
* @brief Special lyd_value structure for ietf-yang-types date-no-zone values.
*/
struct lyd_value_date_nz {
time_t time; /**< UNIX timestamp */
};
/**
* @brief Special lyd_value structure for ietf-yang-types time values.
*/
struct lyd_value_time {
uint32_t seconds; /**< Timestamp converted into seconds */
char *fractions_s; /**< Optional fractions of a second */
ly_bool unknown_tz; /**< Whether the value is in the special Z/-00:00 timezone. */
};
/**
* @brief Special lyd_value structure for ietf-yang-types time-no-zone values.
*/
struct lyd_value_time_nz {
uint32_t seconds; /**< Timestamp converted into seconds */
char *fractions_s; /**< Optional fractions of a second */
};
/**
* @brief Special lyd_value structure for ietf-yang-types xpath1.0 values.
*/
struct lyd_value_xpath10 {
struct lyxp_expr *exp;
const struct ly_ctx *ctx;
void *prefix_data;
LY_VALUE_FORMAT format;
};
/**
* @brief Special lyd_value structure for lyds tree value.
*/
struct lyd_value_lyds_tree {
struct rb_node *rbt; /**< Root of the Red-black tree. */
};
/**
* @brief Generic prefix and namespace mapping, meaning depends on the format.
*
* The union is used as a reference to the data's module and according to the format, it can be used as a key for
* ::ly_ctx_get_module_implemented_ns() or ::ly_ctx_get_module_implemented(). While the module reference is always present,
* the prefix member can be omitted in case it is not present in the source data as a reference to the default module/namespace.
*/
struct ly_opaq_name {
const char *name; /**< node name, without prefix if any was defined */
const char *prefix; /**< identifier used in the qualified name as the prefix, can be NULL */
union {
const char *module_ns; /**< format ::LY_VALUE_XML - XML namespace of the node element */
const char *module_name; /**< format ::LY_VALUE_JSON - (inherited) name of the module of the element */
};
};
/**
* @brief Generic attribute structure.
*/
struct lyd_attr {
struct lyd_node_opaq *parent; /**< data node where the attribute is placed */
struct lyd_attr *next; /**< pointer to the next attribute */
struct ly_opaq_name name; /**< attribute name with module information */
const char *value; /**< attribute value */
uint32_t hints; /**< additional information about from the data source, see the [hints list](@ref lydhints) */
LY_VALUE_FORMAT format; /**< format of the attribute and any prefixes, ::LY_VALUE_XML or ::LY_VALUE_JSON */
void *val_prefix_data; /**< format-specific prefix data */
};
#define LYD_NODE_INNER (LYS_CONTAINER|LYS_LIST|LYS_RPC|LYS_ACTION|LYS_NOTIF) /**< Schema nodetype mask for lyd_node_inner */
#define LYD_NODE_TERM (LYS_LEAF|LYS_LEAFLIST) /**< Schema nodetype mask for lyd_node_term */
#define LYD_NODE_ANY (LYS_ANYDATA) /**< Schema nodetype mask for lyd_node_any */
/**
* @ingroup datatree
* @defgroup dnodeflags Data node flags
* @{
*
* Various flags of data nodes.
*
* 1 - container 5 - anydata/anyxml
* 2 - list 6 - rpc/action
* 3 - leaf 7 - notification
* 4 - leaflist
*
* bit name 1 2 3 4 5 6 7
* ---------------------+-+-+-+-+-+-+-+
* 1 LYD_DEFAULT |x| |x|x| | | |
* +-+-+-+-+-+-+-+
* 2 LYD_WHEN_TRUE |x|x|x|x|x| | |
* +-+-+-+-+-+-+-+
* 3 LYD_NEW |x|x|x|x|x|x|x|
* +-+-+-+-+-+-+-+
* 4 LYD_EXT |x|x|x|x|x|x|x|
* +-+-+-+-+-+-+-+
* 5 LYD_WHEN_FALSE |x|x|x|x|x| | |
* ---------------------+-+-+-+-+-+-+-+
*
*/
#define LYD_DEFAULT 0x01 /**< default (implicit) node */
#define LYD_WHEN_TRUE 0x02 /**< all when conditions of this node were evaluated to true */
#define LYD_NEW 0x04 /**< node was created after the last validation, is needed for the next validation */
#define LYD_EXT 0x08 /**< node is the first sibling parsed as extension instance data */
#define LYD_WHEN_FALSE 0x10 /**< when condition of this node was evaluated to false; the node is kept in the
tree so multi-error validation can continue, but XPath evaluation must treat
the node as non-existent */
/** @} */
/**
* @brief Generic structure for a data node.
*/
struct lyd_node {
uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
hashes of all nodes of subtree in case of keyless list). Note that while hash can be
used to get know that nodes are not equal, it cannot be used to decide that the
nodes are equal due to possible collisions. */
uint32_t flags; /**< [data node flags](@ref dnodeflags) */
const struct lysc_node *schema; /**< pointer to the schema definition of this node */
struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
never NULL. If there is no sibling node, pointer points to the node
itself. In case of the first node, this pointer points to the last
node in the list. */
struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
void *priv; /**< private user data, not used by libyang */
};
/**
* @brief Data node structure for the inner data tree nodes - containers, lists, RPCs, actions and Notifications.
*/
struct lyd_node_inner {
union {
struct lyd_node node; /**< implicit cast for the members compatible with ::lyd_node */
struct {
uint32_t hash; /**< hash of this particular node (module name + schema name + key string
values if list or hashes of all nodes of subtree in case of keyless
list). Note that while hash can be used to get know that nodes are
not equal, it cannot be used to decide that the nodes are equal due
to possible collisions. */
uint32_t flags; /**< [data node flags](@ref dnodeflags) */
const struct lysc_node *schema; /**< pointer to the schema definition of this node */
struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
never NULL. If there is no sibling node, pointer points to the node
itself. In case of the first node, this pointer points to the last
node in the list. */
struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
void *priv; /**< private user data, not used by libyang */
};
}; /**< common part corresponding to ::lyd_node */
struct lyd_node *child; /**< pointer to the first child node. */
struct ly_ht *children_ht; /**< hash table with all the direct children (except keys for a list, lists without keys) */
#define LYD_HT_MIN_ITEMS 4 /**< minimal number of children to create ::lyd_node_inner.children_ht hash table. */
};
/**
* @brief Data node structure for the terminal data tree nodes - leaves and leaf-lists.
*/
struct lyd_node_term {
union {
struct lyd_node node; /**< implicit cast for the members compatible with ::lyd_node */
struct {
uint32_t hash; /**< hash of this particular node (module name + schema name + key string
values if list or hashes of all nodes of subtree in case of keyless
list). Note that while hash can be used to get know that nodes are
not equal, it cannot be used to decide that the nodes are equal due
to possible collisions. */
uint32_t flags; /**< [data node flags](@ref dnodeflags) */
const struct lysc_node *schema; /**< pointer to the schema definition of this node */
struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
never NULL. If there is no sibling node, pointer points to the node
itself. In case of the first node, this pointer points to the last
node in the list. */
struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
void *priv; /**< private user data, not used by libyang */
};
}; /**< common part corresponding to ::lyd_node */
struct lyd_value value; /**< node's value representation */
};
/**
* @brief Data node structure for the anydata data tree nodes - anydata or anyxml.
*/
struct lyd_node_any {
union {
struct lyd_node node; /**< implicit cast for the members compatible with ::lyd_node */
struct {
uint32_t hash; /**< hash of this particular node (module name + schema name + key string
values if list or hashes of all nodes of subtree in case of keyless
list). Note that while hash can be used to get know that nodes are
not equal, it cannot be used to decide that the nodes are equal due
to possible collisions. */
uint32_t flags; /**< [data node flags](@ref dnodeflags) */
const struct lysc_node *schema; /**< pointer to the schema definition of this node */
struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
never NULL. If there is no sibling node, pointer points to the node
itself. In case of the first node, this pointer points to the last
node in the list. */
struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
void *priv; /**< private user data, not used by libyang */
};
}; /**< common part corresponding to ::lyd_node */
struct lyd_node *child; /**< pointer to the first child node, if any */
struct ly_ht *children_ht; /**< unused, always NULL */
const char *value; /**< pointer to the string value, if any */
uint32_t hints; /**< additional value format information, see the [val hints](@ref lydvalhints)
and [node hints](@ref lydnodehints) */
};
/**
* @brief Get the name (associated with) of a data node. Works for opaque nodes as well.
*
* @param[in] node Node to examine.
* @return Data node name.
*/
#define LYD_NAME(node) ((node)->schema ? (node)->schema->name : ((struct lyd_node_opaq *)node)->name.name)
/**
* @ingroup datatree
* @defgroup lydvalhints Value format hints.
* @{
*
* Hints for the type of the data value.
*
* Any information about value types encoded in the format is hinted by these values.
*/
#define LYD_VALHINT_STRING 0x0001 /**< value is allowed to be a string */
#define LYD_VALHINT_DECNUM 0x0002 /**< value is allowed to be a decimal number */
#define LYD_VALHINT_OCTNUM 0x0004 /**< value is allowed to be an octal number */
#define LYD_VALHINT_HEXNUM 0x0008 /**< value is allowed to be a hexadecimal number */
#define LYD_VALHINT_NUM64 0x0010 /**< value is allowed to be an int64 or uint64 */
#define LYD_VALHINT_BOOLEAN 0x0020 /**< value is allowed to be a boolean */
#define LYD_VALHINT_EMPTY 0x0040 /**< value is allowed to be empty */
#define LYD_VALHINT_STRING_DATATYPES 0x0080 /**< boolean and numeric fields are allowed to be quoted */
/**
* @} lydvalhints
*/
/**
* @ingroup datatree
* @defgroup lydnodehints Node type format hints
* @{
*
* Hints for the type of the data node.
*
* Any information about node types encoded in the format is hinted by these values.
*/
#define LYD_NODEHINT_LIST 0x1000 /**< node is allowed to be a list instance */
#define LYD_NODEHINT_LEAFLIST 0x2000 /**< node is allowed to be a leaf-list instance */
#define LYD_NODEHINT_CONTAINER 0x4000 /**< node is allowed to be a container instance */
/**
* @} lydnodehints
*/
/**
* @ingroup datatree
* @defgroup lydhints Value and node type format hints
* @{
*
* Hints for the types of data node and its value.
*
* Any information about value and node types encoded in the format is hinted by these values.
* It combines [value hints](@ref lydvalhints) and [node hints](@ref lydnodehints).
*/
#define LYD_HINT_DATA 0xFFF3 /**< special node/value hint to be used for generic data node/value (for cases when
there is no encoding or it does not provide any additional information about
a node/value type); do not combine with specific [value hints](@ref lydvalhints)
or [node hints](@ref lydnodehints). */
#define LYD_HINT_SCHEMA 0xFFFF /**< special node/value hint to be used for generic schema node/value(for cases when
there is no encoding or it does not provide any additional information about
a node/value type); do not combine with specific [value hints](@ref lydvalhints)
or [node hints](@ref lydnodehints). */
/**
* @} lydhints
*/
/**
* @brief Data node structure for unparsed (opaque) nodes.
*/
struct lyd_node_opaq {
union {
struct lyd_node node; /**< implicit cast for the members compatible with ::lyd_node */
struct {
uint32_t hash; /**< always 0 */
uint32_t flags; /**< always 0 */
const struct lysc_node *schema; /**< always NULL */