blob: a9e2f02787b64ecfb40d5db659da08f8e19035f7 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
2 * @file tree_data.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief libyang representation of YANG data trees.
5 *
6 * Copyright (c) 2015 - 2019 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#ifndef LY_TREE_DATA_H_
16#define LY_TREE_DATA_H_
17
18#include <stddef.h>
19#include <stdint.h>
20
21#include "log.h"
Christian Hopps59618972021-02-01 05:01:35 -050022#include "tree.h"
23#include "tree_schema.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020024
Radek Krejcica376bd2020-06-11 16:04:06 +020025#ifdef __cplusplus
26extern "C" {
27#endif
28
Radek Krejcie7b95092019-05-15 11:03:07 +020029struct ly_ctx;
Michal Vasko004d3152020-06-11 19:59:22 +020030struct ly_path;
Radek Krejci535ea9f2020-05-29 16:01:05 +020031struct ly_set;
32struct lyd_node;
33struct lyd_node_opaq;
Radek Krejci47fab892020-11-05 17:02:41 +010034struct lyd_node_term;
Radek Krejcie7b95092019-05-15 11:03:07 +020035
Radek Krejcie7b95092019-05-15 11:03:07 +020036/**
Radek Krejci8678fa42020-08-18 16:07:28 +020037 * @page howtoData Data Instances
38 *
39 * All the nodes in data tree comes are based on ::lyd_node structure. According to the content of the ::lyd_node.schema
40 * it can be cast to several other structures.
41 *
42 * In case the ::lyd_node.schema pointer is NULL, the node is actually __opaq__ and can be safely cast to ::lyd_node_opaq.
43 * The opaq node represent an unknown node which wasn't mapped to any [(compiled) schema](@ref howtoSchema) node in the
44 * context. Such a node can appear in several places in the data tree.
45 * - As a part of the tree structure, but only in the case the ::LYD_PARSE_OPAQ option was used when input data were
46 * [parsed](@ref howtoDataParsers), because unknown data instances are ignored by default. The same way, the opaq nodes can
47 * appear as a node's attributes.
48 * - As a representation of YANG anydata/anyxml content.
49 * - As envelopes of standard data tree instances (RPCs, actions or Notifications).
50 *
51 * In case the data node has its definition in a [compiled schema tree](@ref howtoSchema), the structure of the data node is
52 * actually one of the followings according to the schema node's nodetype (::lysc_node.nodetype).
53 * - ::lyd_node_inner - represents data nodes corresponding to schema nodes matching ::LYD_NODE_INNER nodetypes. They provide
54 * structure of the tree by having children nodes.
55 * - ::lyd_node_term - represents data nodes corresponding to schema nodes matching ::LYD_NODE_TERM nodetypes. The terminal
56 * nodes provide values of the particular configuration/status information. The values are represented as ::lyd_value
Radek Krejci6d5ba0c2021-04-26 07:49:59 +020057 * structure with string representation of the value (retrieved by ::lyd_get_value() and ::lyd_get_meta_value()) and
58 * the type specific data stored in the structure's union according to the real type of the value (::lyd_value.realtype).
59 * The string representation provides canonical representation of the value in case the type has the canonical
60 * representation specified. Otherwise, it is the original value or, in case the value can contain prefixes, the JSON
61 * format is used to make the value unambiguous.
Radek Krejci8678fa42020-08-18 16:07:28 +020062 * - ::lyd_node_any - represents data nodes corresponding to schema nodes matching ::LYD_NODE_ANY nodetypes.
63 *
64 * Despite all the aforementioned structures and their members are available as part of the libyang API and callers can use
65 * it to navigate through the data tree structure or to obtain various information, we recommend to use the following macros
66 * and functions.
67 * - ::lyd_child() (or ::lyd_child_no_keys()) and ::lyd_parent() to get the node's child/parent node.
68 * - ::LYD_CTX to get libyang context from a data node.
Radek Krejci6d5ba0c2021-04-26 07:49:59 +020069 * - ::lyd_get_value()/::lyd_get_meta_value() to get canonical string value from a terminal node/metadata instance.
Radek Krejci8678fa42020-08-18 16:07:28 +020070 * - ::LYD_TREE_DFS_BEGIN and ::LYD_TREE_DFS_END to traverse the data tree (depth-first).
71 * - ::LY_LIST_FOR and ::LY_ARRAY_FOR as described on @ref howtoStructures page.
72 *
73 * Instead of going through the data tree on your own, a specific data node can be also located using a wide set of
74 * \b lyd_find_*() functions.
75 *
76 * More information about specific operations with data instances can be found on the following pages:
77 * - @subpage howtoDataParsers
78 * - @subpage howtoDataValidation
79 * - @subpage howtoDataWD
80 * - @subpage howtoDataManipulation
81 * - @subpage howtoDataPrinters
Radek Krejcif9943642021-04-26 10:18:21 +020082 * - @subpage howtoDataLYB
Radek Krejci8678fa42020-08-18 16:07:28 +020083 *
84 * \note API for this group of functions is described in the [Data Instances module](@ref datatree).
85 *
86 * Functions List (not assigned to above subsections)
87 * --------------------------------------------------
88 * - ::lyd_child()
89 * - ::lyd_child_no_keys()
90 * - ::lyd_parent()
91 * - ::lyd_owner_module()
Radek Krejci6d5ba0c2021-04-26 07:49:59 +020092 * - ::lyd_get_value()
93 * - ::lyd_get_meta_value()
Radek Krejci8678fa42020-08-18 16:07:28 +020094 * - ::lyd_find_xpath()
Michal Vasko3e1f6552021-01-14 09:27:55 +010095 * - ::lyd_find_path()
Radek Krejci8678fa42020-08-18 16:07:28 +020096 * - ::lyd_find_sibling_val()
97 * - ::lyd_find_sibling_first()
Michal Vasko1d4af6c2021-02-22 13:31:26 +010098 * - ::lyd_find_sibling_opaq_next()
Radek Krejci8678fa42020-08-18 16:07:28 +020099 * - ::lyd_find_meta()
100 *
101 * - ::lyd_path()
102 * - ::lyd_target()
103 *
104 * - ::lyd_lyb_data_length()
Radek Krejci75104122021-04-01 15:37:45 +0200105 *
106 *
107 * @section howtoDataMetadata Metadata Support
108 *
109 * YANG Metadata annotations are defined in [RFC 7952](https://tools.ietf.org/html/rfc7952) as YANG extension (and libyang
110 * [implements them as internal extension plugin](@ref howtoPluginsExtensions)). In practice, it allows to have XML
111 * attributes (there is also a special encoding for JSON) in YANG modeled data. libyang does not allow to have any XML
112 * attribute without the appropriate annotation definition describing the data as it is done e.g. for leafs. When an
113 * attribute without a matching annotation definition is found in the input data, it is:
114 * - silently dropped (with warning) or
115 * - an error is reported in case the ::LYD_PARSE_STRICT parser option is provided to the
116 * [parser function](@ref howtoDataParsers) or
117 * - stored into a generic ::lyd_attr structure without a connection with any YANG module in case the ::LYD_PARSE_OPAQ
118 * parser options is provided to the [parser function](@ref howtoDataParsers).
119 *
120 * There are some XML attributes, described by [YANG](https://tools.ietf.org/html/rfc7950) and
121 * [NETCONF](https://tools.ietf.org/html/rfc6241) specifications, which are not defined as annotations, but libyang
122 * implements them this way. In case of attributes in the YANG namespace (`insert`, `value` and `key` attributes
123 * for the NETCONF edit-config operation), they are defined in special libyang's internal module `yang`, which is
124 * available in each context and the content of this schema can be printed via
125 * [schema printers](@ref howtoSchemaPrinters).
126 *
127 * In case of the attributes described in [NETCONF specification](https://tools.ietf.org/html/rfc6241), the libyang's
128 * annotations structures are hidden and cannot be printed despite, internally, they are part of the `ietf-netconf`'s
129 * schema structure. Therefore, these attributes are available only when the `ietf-netconf` schema is loaded in the
130 * context. The definitions of these annotations are as follows:
131 *
132 * md:annotation operation {
133 * type enumeration {
134 * enum merge;
135 * enum replace;
136 * enum create;
137 * enum delete;
138 * enum remove;
139 * }
140 * }
141 *
142 * md:annotation type {
143 * type enumeration {
144 * enum subtree;
145 * enum xpath {
146 * if-feature "nc:xpath";
147 * }
148 * }
149 * }
150 *
151 * md:annotation select {
152 * type string;
153 * }
154 *
155 * Note, that, following the specification,
156 * - the `type` and `select` XML attributes are supposed to be unqualified (without namespace) and that
157 * - the `select`'s content is XPath and it is internally transformed by libyang into the format where the
158 * XML namespace prefixes are replaced by the YANG module names.
159 *
160 *
161 * @section howtoDataYangdata yang-data Support
162 *
163 * [RFC 8040](https://tools.ietf.org/html/rfc8040) defines ietf-restconf module, which includes yang-data extension. Despite
164 * the definition in the RESTCONF YANG module, the yang-data concept is quite generic and used even in modules without a
165 * connection to RESTCONF protocol. The extension allows to define a separated YANG trees usable separately from any
166 * datastore.
167 *
168 * libyang implements support for yang-data internally as an [extension plugin](@ref howtoPluginsExtensions). To ease the
169 * use of yang-data with libyang, there are several generic functions, which are usable for yang-data:
170 *
171 * - ::lyd_parse_ext_data()
172 * - ::lyd_parse_ext_op()
173 *
174 * - ::lys_getnext_ext()
175 *
176 * - ::lyd_new_ext_inner()
177 * - ::lyd_new_ext_list()
178 * - ::lyd_new_ext_term()
179 * - ::lyd_new_ext_any()
180 * - ::lyd_new_ext_path()
Radek Krejci8678fa42020-08-18 16:07:28 +0200181 */
182
183/**
184 * @page howtoDataManipulation Manipulating Data
185 *
186 * There are many functions to create or modify an existing data tree. You can add new nodes, reconnect nodes from
187 * one tree to another (or e.g. from one list instance to another) or remove nodes. The functions doesn't allow you
188 * to put a node to a wrong place (by checking the YANG module structure), but not all validation checks can be made directly
189 * (or you have to make a valid change by multiple tree modifications) when the tree is being changed. Therefore,
190 * the [validation process](@ref howtoDataValidation) is expected to be invoked after changing the data tree to make sure
191 * that the changed data tree is valid.
192 *
193 * When inserting a node into data tree (no matter if the node already exists, via ::lyd_insert_child() and
194 * ::lyd_insert_sibling(), or a new node is being created), the node is automatically inserted to the place respecting the
195 * nodes order from the YANG schema. So the node is not inserted to the end or beginning of the siblings list, but after the
196 * existing instance of the closest preceding sibling node from the schema. In case the node is opaq (it is not connected
197 * with any schema node), it is placed to the end of the sibling node in the order they are inserted in. The only situation
198 * when it is possible to influence the order of the nodes is the order of user-ordered list/leaf-list instances. In such
199 * a case the ::lyd_insert_after() or ::lyd_insert_before() can be used.
200 *
201 * Creating data is generally possible in two ways, they can be combined. You can add nodes one-by-one based on
202 * the node name and/or its parent (::lyd_new_inner(), ::lyd_new_term(), ::lyd_new_any(), ::lyd_new_list(), ::lyd_new_list2()
Michal Vasko493900b2020-12-08 10:23:41 +0100203 * and ::lyd_new_opaq()) or address the nodes using a [simple XPath addressing](@ref howtoXPath) (::lyd_new_path() and
204 * ::lyd_new_path2()). The latter enables to create a whole path of nodes, requires less information
Radek Krejci8678fa42020-08-18 16:07:28 +0200205 * about the modified data, and is generally simpler to use. Actually the third way is duplicating the existing data using
206 * ::lyd_dup_single(), ::lyd_dup_siblings() and ::lyd_dup_meta_single().
207 *
Radek Krejci8a5afc22021-03-12 11:10:47 +0100208 * Note, that in case the node is defined in an extension instance, the functions mentioned above do not work until you
209 * provide parent where the new node is supposed to be inserted. The reason is that all the functions searches for the
210 * top-level nodes directly inside modules. To create a top-level node defined in an extension instance, use
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100211 * ::lyd_new_ext_inner(), ::lyd_new_ext_term(), ::lyd_new_ext_any(), ::lyd_new_ext_list() and ::lyd_new_ext_path()
212 * functions.
Radek Krejci8a5afc22021-03-12 11:10:47 +0100213 *
Radek Krejci75104122021-04-01 15:37:45 +0200214 * The [metadata](@ref howtoDataMetadata) (and attributes in opaq nodes) can be created with ::lyd_new_meta()
Radek Krejci8678fa42020-08-18 16:07:28 +0200215 * and ::lyd_new_attr().
216 *
217 * Changing value of a terminal node (leaf, leaf-list) is possible with ::lyd_change_term(). Similarly, the metadata value
218 * can be changed with ::lyd_change_meta(). Before changing the value, it might be useful to compare the node's value
219 * with a string value (::lyd_value_compare()) or verify that the new string value is correct for the specific data node
220 * (::lyd_value_validate()).
221 *
222 * Working with two existing subtrees can also be performed two ways. Usually, you would use lyd_insert*() functions.
223 * They are generally meant for simple inserts of a node into a data tree. For more complicated inserts and when
224 * merging 2 trees use ::lyd_merge_tree() or ::lyd_merge_siblings(). It offers additional options and is basically a more
225 * powerful insert.
226 *
227 * Besides merging, libyang is also capable to provide information about differences between two data trees. For this purpose,
228 * ::lyd_diff_tree() and ::lyd_diff_siblings() generates annotated data trees which can be, in addition, used to change one
229 * data tree to another one using ::lyd_diff_apply_all(), ::lyd_diff_apply_module() and ::lyd_diff_reverse_all(). Multiple
230 * diff data trees can be also put together for further work using ::lyd_diff_merge_all(), ::lyd_diff_merge_module() and
231 * ::lyd_diff_merge_tree() functions. To just check equivalence of the data nodes, ::lyd_compare_single(),
232 * ::lyd_compare_siblings() and ::lyd_compare_meta() can be used.
233 *
234 * To remove a node or subtree from a data tree, use ::lyd_unlink_tree() and then free the unwanted data using
235 * ::lyd_free_all() (or other \b lyd_free_*() functions).
236 *
237 * Also remember, that when you are creating/inserting a node, all the objects in that operation must belong to the
238 * same context.
239 *
240 * Modifying the single data tree in multiple threads is not safe.
241 *
242 * Functions List
243 * --------------
244 * - ::lyd_new_inner()
245 * - ::lyd_new_term()
Radek Krejci09c77442021-04-26 11:10:34 +0200246 * - ::lyd_new_term_bin()
247 * - ::lyd_new_term_canon()
Radek Krejci8678fa42020-08-18 16:07:28 +0200248 * - ::lyd_new_list()
249 * - ::lyd_new_list2()
250 * - ::lyd_new_any()
251 * - ::lyd_new_opaq()
Michal Vasko8d65f852021-02-17 11:28:13 +0100252 * - ::lyd_new_opaq2()
Radek Krejci8678fa42020-08-18 16:07:28 +0200253 * - ::lyd_new_attr()
Michal Vasko8d65f852021-02-17 11:28:13 +0100254 * - ::lyd_new_attr2()
Radek Krejci8678fa42020-08-18 16:07:28 +0200255 * - ::lyd_new_meta()
256 * - ::lyd_new_path()
257 * - ::lyd_new_path2()
Radek Krejci8678fa42020-08-18 16:07:28 +0200258 *
Radek Krejcidd2a7662021-03-12 11:26:34 +0100259 * - ::lyd_new_ext_inner()
Radek Krejci8a5afc22021-03-12 11:10:47 +0100260 * - ::lyd_new_ext_term()
Radek Krejci8247bae2021-03-12 11:47:02 +0100261 * - ::lyd_new_ext_list()
Radek Krejci0b963da2021-03-12 13:23:44 +0100262 * - ::lyd_new_ext_any()
Radek Krejci95ccd1b2021-03-12 14:57:22 +0100263 * - ::lyd_new_ext_path()
Radek Krejci8a5afc22021-03-12 11:10:47 +0100264 *
Radek Krejci8678fa42020-08-18 16:07:28 +0200265 * - ::lyd_dup_single()
266 * - ::lyd_dup_siblings()
267 * - ::lyd_dup_meta_single()
268 *
269 * - ::lyd_insert_child()
270 * - ::lyd_insert_sibling()
271 * - ::lyd_insert_after()
272 * - ::lyd_insert_before()
273 *
274 * - ::lyd_value_compare()
275 * - ::lyd_value_validate()
276 *
277 * - ::lyd_change_term()
Radek Krejci09c77442021-04-26 11:10:34 +0200278 * - ::lyd_change_term_bin()
279 * - ::lyd_change_term_canon()
Radek Krejci8678fa42020-08-18 16:07:28 +0200280 * - ::lyd_change_meta()
281 *
282 * - ::lyd_compare_single()
283 * - ::lyd_compare_siblings()
284 * - ::lyd_compare_meta()
285 * - ::lyd_diff_tree()
286 * - ::lyd_diff_siblings()
287 * - ::lyd_diff_apply_all()
288 * - ::lyd_diff_apply_module()
289 * - ::lyd_diff_reverse_all()
290 * - ::lyd_diff_merge_all()
291 * - ::lyd_diff_merge_module()
292 * - ::lyd_diff_merge_tree()
293 *
294 * - ::lyd_merge_tree()
295 * - ::lyd_merge_siblings()
296 *
297 * - ::lyd_unlink_tree()
298 *
299 * - ::lyd_free_all()
300 * - ::lyd_free_siblings()
301 * - ::lyd_free_tree()
302 * - ::lyd_free_meta_single()
303 * - ::lyd_free_meta_siblings()
304 * - ::lyd_free_attr_single()
305 * - ::lyd_free_attr_siblings()
306 *
Michal Vaskoa820c312021-02-05 16:33:00 +0100307 * - ::lyd_any_value_str()
Radek Krejci8678fa42020-08-18 16:07:28 +0200308 * - ::lyd_any_copy_value()
309 */
310
311/**
312 * @page howtoDataWD Default Values
313 *
314 * libyang provides support for work with default values as defined in [RFC 6243](https://tools.ietf.org/html/rfc6243).
315 * However, libyang context do not contains the *ietf-netconf-with-defaults* module on its own and caller is supposed to
316 * add this YANG module to enable full support of the *with-defaults* features described below. Without presence of the
317 * mentioned module in the context, the default nodes are still present and handled in the data trees, but the metadata
318 * providing the information about the default values cannot be used. It means that when parsing data, the default nodes
319 * marked with the metadata as implicit default nodes are handled as explicit data and when printing data tree, the expected
320 * nodes are printed without the ietf-netconf-with-defaults metadata.
321 *
322 * The RFC document defines 4 modes for handling default nodes in a data tree, libyang adds the fifth mode and use them
323 * via @ref dataprinterflags when printing data trees.
324 * - \b explicit - Only the explicitly set configuration data. But in the case of status data, missing default
325 * data are added into the tree. In libyang, this mode is represented by ::LYD_PRINT_WD_EXPLICIT option.
326 * This is the default with-defaults mode of the printer. The data nodes do not contain any additional
327 * metadata information.
328 * - \b trim - Data nodes containing the default value are removed. This mode is applied with ::LYD_PRINT_WD_TRIM option.
329 * - \b report-all - This mode provides all the default data nodes despite they were explicitly present in source data or
330 * they were added by libyang's [validation process](@ref howtoDataValidation). This mode is activated by
331 * ::LYD_PRINT_WD_ALL option.
332 * - \b report-all-tagged - In this case, all the data nodes (implicit as well the explicit) containing the default value
333 * are printed and tagged (see the note below). Printers accept ::LYD_PRINT_WD_ALL_TAG option for this mode.
334 * - \b report-implicit-tagged - The last mode is similar to the previous one, except only the implicitly added nodes
335 * are tagged. This is the libyang's extension and it is activated by ::LYD_PRINT_WD_IMPL_TAG option.
336 *
337 * Internally, libyang adds the default nodes into the data tree as part of the [validation process](@ref howtoDataValidation).
338 * When [parsing data](@ref howtoDataParsers) from an input source, adding default nodes can be avoided only by avoiding
339 * the whole [validation process](@ref howtoDataValidation). In case the ietf-netconf-with-defaults module is present in the
340 * context, the [parser process](@ref howtoDataParsers) also supports to recognize the implicit default nodes marked with the
341 * appropriate metadata.
342 *
343 * Note, that in a modified data tree (via e.g. \b lyd_insert_*() or \b lyd_free_*() functions), some of the default nodes
344 * can be missing or they can be present by mistake. Such a data tree is again corrected during the next run of the
345 * [validation process](@ref howtoDataValidation) or manualy using \b lyd_new_implicit_*() functions.
346 *
347 * The implicit (default) nodes, created by libyang, are marked with the ::LYD_DEFAULT flag in ::lyd_node.flags member
348 * Note, that besides leafs and leaf-lists, the flag can appear also in containers, where it means that the container
349 * 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
350 * the accessible data tree). When printing data trees, the presence of empty containers (despite they were added
351 * explicitly or implicitly as part of accessible data tree) depends on ::LYD_PRINT_KEEPEMPTYCONT option.
352 *
353 * To get know if the particular leaf or leaf-list node contains default value (despite implicit or explicit), you can
354 * use ::lyd_is_default() function.
355 *
356 * Functions List
357 * --------------
358 * - ::lyd_is_default()
359 * - ::lyd_new_implicit_all()
360 * - ::lyd_new_implicit_module()
361 * - ::lyd_new_implicit_tree()
362 */
363
364/**
Radek Krejcif9943642021-04-26 10:18:21 +0200365 * @page howtoDataLYB LYB Binary Format
366 *
367 * LYB (LibYang Binary) is a proprietary libyang binary data and file format. Its primary purpose is efficient
368 * serialization (printing) and deserialization (parsing). With this goal in mind, every term node value is stored
369 * in its new binary format specification according to its type. Following is the format for all types with explicit
370 * support out-of-the-box (meaning that have a special type plugin). Any derived types inherit the format of its
371 * closest type with explicit support (up to a built-in type).
Radek Krejci09c77442021-04-26 11:10:34 +0200372 *
373 * @subpage lybformat_ipv6_address
Radek Krejcif9943642021-04-26 10:18:21 +0200374 */
375
376/**
Radek Krejci2ff0d572020-05-21 15:27:28 +0200377 * @ingroup trees
Radek Krejci8678fa42020-08-18 16:07:28 +0200378 * @defgroup datatree Data Tree
Radek Krejcie7b95092019-05-15 11:03:07 +0200379 * @{
380 *
381 * Data structures and functions to manipulate and access instance data tree.
382 */
383
Michal Vasko64246d82020-08-19 12:35:00 +0200384/* *INDENT-OFF* */
385
Michal Vaskoa276cd92020-08-10 15:10:08 +0200386/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200387 * @brief Macro to iterate via all elements in a data tree. This is the opening part
388 * to the #LYD_TREE_DFS_END - they always have to be used together.
389 *
390 * The function follows deep-first search algorithm:
391 * <pre>
392 * 1
393 * / \
Michal Vaskoc193ce92020-03-06 11:04:48 +0100394 * 2 4
Radek Krejcie7b95092019-05-15 11:03:07 +0200395 * / / \
Michal Vaskoc193ce92020-03-06 11:04:48 +0100396 * 3 5 6
Radek Krejcie7b95092019-05-15 11:03:07 +0200397 * </pre>
398 *
Radek Krejci0935f412019-08-20 16:15:18 +0200399 * Use the same parameters for #LYD_TREE_DFS_BEGIN and #LYD_TREE_DFS_END. While
Michal Vasko56daf732020-08-10 10:57:18 +0200400 * START can be any of the lyd_node* types, ELEM variable must be a pointer to
401 * the generic struct lyd_node.
Radek Krejcie7b95092019-05-15 11:03:07 +0200402 *
Michal Vasko56daf732020-08-10 10:57:18 +0200403 * To skip a particular subtree, instead of the continue statement, set LYD_TREE_DFS_continue
404 * variable to non-zero value.
Radek Krejcie7b95092019-05-15 11:03:07 +0200405 *
406 * Use with opening curly bracket '{' after the macro.
407 *
408 * @param START Pointer to the starting element processed first.
Radek Krejcie7b95092019-05-15 11:03:07 +0200409 * @param ELEM Iterator intended for use in the block.
410 */
Michal Vasko56daf732020-08-10 10:57:18 +0200411#define LYD_TREE_DFS_BEGIN(START, ELEM) \
Radek Krejci857189e2020-09-01 13:26:36 +0200412 { ly_bool LYD_TREE_DFS_continue = 0; struct lyd_node *LYD_TREE_DFS_next; \
Michal Vasko56daf732020-08-10 10:57:18 +0200413 for ((ELEM) = (LYD_TREE_DFS_next) = (struct lyd_node *)(START); \
Radek Krejcie7b95092019-05-15 11:03:07 +0200414 (ELEM); \
Michal Vasko56daf732020-08-10 10:57:18 +0200415 (ELEM) = (LYD_TREE_DFS_next), LYD_TREE_DFS_continue = 0)
Radek Krejcie7b95092019-05-15 11:03:07 +0200416
417/**
418 * @brief Macro to iterate via all elements in a tree. This is the closing part
419 * to the #LYD_TREE_DFS_BEGIN - they always have to be used together.
420 *
421 * Use the same parameters for #LYD_TREE_DFS_BEGIN and #LYD_TREE_DFS_END. While
Michal Vasko56daf732020-08-10 10:57:18 +0200422 * START can be any of the lyd_node* types, ELEM variable must be a pointer
423 * to the generic struct lyd_node.
Radek Krejcie7b95092019-05-15 11:03:07 +0200424 *
425 * Use with closing curly bracket '}' after the macro.
426 *
427 * @param START Pointer to the starting element processed first.
Radek Krejcie7b95092019-05-15 11:03:07 +0200428 * @param ELEM Iterator intended for use in the block.
429 */
430
Michal Vasko56daf732020-08-10 10:57:18 +0200431#define LYD_TREE_DFS_END(START, ELEM) \
Radek Krejcie7b95092019-05-15 11:03:07 +0200432 /* select element for the next run - children first */ \
Michal Vasko56daf732020-08-10 10:57:18 +0200433 if (LYD_TREE_DFS_continue) { \
434 (LYD_TREE_DFS_next) = NULL; \
435 } else { \
Radek Krejcia1c1e542020-09-29 16:06:52 +0200436 (LYD_TREE_DFS_next) = lyd_child(ELEM); \
Michal Vasko56daf732020-08-10 10:57:18 +0200437 }\
438 if (!(LYD_TREE_DFS_next)) { \
Radek Krejcie7b95092019-05-15 11:03:07 +0200439 /* no children */ \
Michal Vasko9e685082021-01-29 14:49:09 +0100440 if ((ELEM) == (struct lyd_node *)(START)) { \
Radek Krejcie7b95092019-05-15 11:03:07 +0200441 /* we are done, (START) has no children */ \
442 break; \
443 } \
444 /* try siblings */ \
Michal Vasko56daf732020-08-10 10:57:18 +0200445 (LYD_TREE_DFS_next) = (ELEM)->next; \
Radek Krejcie7b95092019-05-15 11:03:07 +0200446 } \
Michal Vasko56daf732020-08-10 10:57:18 +0200447 while (!(LYD_TREE_DFS_next)) { \
Radek Krejcie7b95092019-05-15 11:03:07 +0200448 /* parent is already processed, go to its sibling */ \
Michal Vasko9e685082021-01-29 14:49:09 +0100449 (ELEM) = (struct lyd_node *)(ELEM)->parent; \
Radek Krejcie7b95092019-05-15 11:03:07 +0200450 /* no siblings, go back through parents */ \
451 if ((ELEM)->parent == (START)->parent) { \
452 /* we are done, no next element to process */ \
453 break; \
454 } \
Michal Vasko56daf732020-08-10 10:57:18 +0200455 (LYD_TREE_DFS_next) = (ELEM)->next; \
Christian Hopps59618972021-02-01 05:01:35 -0500456 } }
457
458/**
459 * @brief Macro to iterate via all schema node data instances in data siblings.
460 *
461 * @param START Pointer to the starting sibling. Even if it is not first, all the siblings are searched.
462 * @param SCHEMA Schema node of the searched instances.
463 * @param ELEM Iterator.
464 */
465#define LYD_LIST_FOR_INST(START, SCHEMA, ELEM) \
466 for (lyd_find_sibling_val(START, SCHEMA, NULL, 0, &(ELEM)); \
467 (ELEM) && ((ELEM)->schema == (SCHEMA)); \
468 (ELEM) = (ELEM)->next)
469
470/**
471 * @brief Macro to iterate via all schema node data instances in data siblings allowing to modify the list itself.
472 *
473 * @param START Pointer to the starting sibling. Even if it is not first, all the siblings are searched.
474 * @param SCHEMA Schema node of the searched instances.
475 * @param NEXT Temporary storage to allow removing of the current iterator content.
476 * @param ELEM Iterator.
477 */
478#define LYD_LIST_FOR_INST_SAFE(START, SCHEMA, NEXT, ELEM) \
479 for (lyd_find_sibling_val(START, SCHEMA, NULL, 0, &(ELEM)); \
480 (ELEM) && ((ELEM)->schema == (SCHEMA)) ? ((NEXT) = (ELEM)->next, 1) : 0; \
481 (ELEM) = (NEXT))
Radek Krejcie7b95092019-05-15 11:03:07 +0200482
Michal Vasko64246d82020-08-19 12:35:00 +0200483/* *INDENT-ON* */
484
Radek Krejcie7b95092019-05-15 11:03:07 +0200485/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200486 * @brief Macro to get context from a data tree node.
487 */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200488#define LYD_CTX(node) ((node)->schema ? (node)->schema->module->ctx : ((struct lyd_node_opaq *)(node))->ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200489
490/**
Radek Krejci8678fa42020-08-18 16:07:28 +0200491 * @brief Data input/output formats supported by libyang [parser](@ref howtoDataParsers) and
492 * [printer](@ref howtoDataPrinters) functions.
Radek Krejcie7b95092019-05-15 11:03:07 +0200493 */
494typedef enum {
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200495 LYD_UNKNOWN = 0, /**< unknown data format, invalid value */
496 LYD_XML, /**< XML instance data format */
497 LYD_JSON, /**< JSON instance data format */
Michal Vasko69730152020-10-09 16:30:07 +0200498 LYD_LYB /**< LYB instance data format */
Radek Krejcie7b95092019-05-15 11:03:07 +0200499} LYD_FORMAT;
500
501/**
Radek Krejci59583bb2019-09-11 12:57:55 +0200502 * @brief List of possible value types stored in ::lyd_node_any.
Radek Krejcie7b95092019-05-15 11:03:07 +0200503 */
504typedef enum {
Radek Krejci8678fa42020-08-18 16:07:28 +0200505 LYD_ANYDATA_DATATREE, /**< Value is a pointer to ::lyd_node structure (first sibling). When provided as input parameter, the pointer
Radek Krejciee4cab22019-07-17 17:07:47 +0200506 is directly connected into the anydata node without duplication, caller is supposed to not manipulate
Radek Krejci8678fa42020-08-18 16:07:28 +0200507 with the data after a successful call (including calling ::lyd_free_all() on the provided data) */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200508 LYD_ANYDATA_STRING, /**< Value is a generic string without any knowledge about its format (e.g. anyxml value in JSON encoded
Radek Krejciee4cab22019-07-17 17:07:47 +0200509 as string). XML sensitive characters (such as & or \>) are automatically escaped when the anydata
510 is printed in XML format. */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200511 LYD_ANYDATA_XML, /**< Value is a string containing the serialized XML data. */
512 LYD_ANYDATA_JSON, /**< Value is a string containing the data modeled by YANG and encoded as I-JSON. */
Michal Vasko69730152020-10-09 16:30:07 +0200513 LYD_ANYDATA_LYB /**< Value is a memory chunk with the serialized data tree in LYB format. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200514} LYD_ANYDATA_VALUETYPE;
515
516/** @} */
517
518/**
519 * @brief YANG data representation
520 */
521struct lyd_value {
Radek Krejci995784f2021-04-26 08:02:13 +0200522 const char *_canonical; /**< Should never be accessed directly, instead ::lyd_get_value() and ::lyd_get_meta_value()
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200523 should be used. Serves as a cache for the canonical value or the JSON
524 representation if no canonical value is defined. */
Radek Krejci8678fa42020-08-18 16:07:28 +0200525
Radek Krejcie7b95092019-05-15 11:03:07 +0200526 union {
Radek Krejcie7b95092019-05-15 11:03:07 +0200527 int8_t boolean; /**< 0 as false, 1 as true */
528 int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200529 int8_t int8; /**< 8-bit signed integer */
530 int16_t int16; /**< 16-bit signed integer */
531 int32_t int32; /**< 32-bit signed integer */
532 int64_t int64; /**< 64-bit signed integer */
533 uint8_t uint8; /**< 8-bit unsigned integer */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200534 uint16_t uint16; /**< 16-bit unsigned integer */
535 uint32_t uint32; /**< 32-bit unsigned integer */
536 uint64_t uint64; /**< 64-bit unsigned integer */
Radek Krejcie7b95092019-05-15 11:03:07 +0200537 struct lysc_type_bitenum_item *enum_item; /**< pointer to the definition of the enumeration value */
Radek Krejci849a62a2019-05-22 15:29:05 +0200538 struct lysc_type_bitenum_item **bits_items; /**< list of set pointers to the specification of the set bits ([sized array](@ref sizedarrays)) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200539 struct lysc_ident *ident; /**< pointer to the schema definition of the identityref value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200540 struct ly_path *target; /**< Instance-identifier target path. */
541 struct lyd_value_subvalue *subvalue; /** Union value with some metadata. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200542 void *ptr; /**< generic data type structure used to store the data */
Radek Krejci8678fa42020-08-18 16:07:28 +0200543 }; /**< The union is just a list of shorthands to possible values stored by a type's plugin. libyang itself uses the ::lyd_value.realtype
544 plugin's callbacks to work with the data.*/
Radek Krejci084289f2019-07-09 17:35:30 +0200545
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200546 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
Radek Krejci8678fa42020-08-18 16:07:28 +0200547 in the schema node of the data node since the type's store plugin can use other types/plugins for
548 storing data. Speaking about built-in types, this is the case of leafref which stores data as its
549 target type. In contrast, union type also uses its subtype's callbacks, but inside an internal data
550 stored in subvalue member of ::lyd_value structure, so here is the pointer to the union type.
551 In general, this type is used to get free callback for this lyd_value structure, so it must reflect
552 the type used to store data directly in the same lyd_value instance. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200553};
554
555/**
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200556 * @brief Special lyd_value structure for union.
557 *
558 * Represents data with multiple types (union). Original value is stored in the main lyd_value:canonical_cache while
Radek Krejci8678fa42020-08-18 16:07:28 +0200559 * the ::lyd_value_subvalue.value contains representation according to one of the union's types.
560 * The ::lyd_value_subvalue.prefix_data provides (possible) mappings from prefixes in the original value to YANG modules.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200561 * These prefixes are necessary to parse original value to the union's subtypes.
562 */
563struct lyd_value_subvalue {
564 struct lyd_value value; /**< representation of the value according to the selected union's subtype
Radek Krejci8678fa42020-08-18 16:07:28 +0200565 (stored as ::lyd_value.realtype here, in subvalue structure */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200566 const char *original; /**< Original value in the dictionary. */
Radek Krejci8df109d2021-04-23 12:19:08 +0200567 LY_VALUE_FORMAT format; /**< Prefix format of the value. However, this information is also used to decide
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200568 whether a value is valid for the specific format or not on later validations
569 (instance-identifier in XML looks different than in JSON). */
Radek Krejci0b013302021-03-29 15:22:32 +0200570 void *prefix_data; /**< Format-specific data for prefix resolution (see ly_resolve_prefix()) */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200571 uint32_t hints; /**< [Value hints](@ref lydvalhints) from the parser */
572 const struct lysc_node *ctx_node; /**< Context schema node. */
573};
574
575/**
Michal Vasko9f96a052020-03-10 09:41:45 +0100576 * @brief Metadata structure.
Radek Krejcie7b95092019-05-15 11:03:07 +0200577 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100578 * The structure provides information about metadata of a data element. Such attributes must map to
Radek Krejcie7b95092019-05-15 11:03:07 +0200579 * annotations as specified in RFC 7952. The only exception is the filter type (in NETCONF get operations)
580 * and edit-config's operation attributes. In XML, they are represented as standard XML attributes. In JSON,
581 * they are represented as JSON elements starting with the '@' character (for more information, see the
582 * YANG metadata RFC.
583 *
584 */
Michal Vasko9f96a052020-03-10 09:41:45 +0100585struct lyd_meta {
586 struct lyd_node *parent; /**< data node where the metadata is placed */
587 struct lyd_meta *next; /**< pointer to the next metadata of the same element */
588 struct lysc_ext_instance *annotation; /**< pointer to the annotation's definition */
589 const char *name; /**< metadata name */
590 struct lyd_value value; /**< metadata value representation */
Radek Krejcie7b95092019-05-15 11:03:07 +0200591};
592
Michal Vasko52927e22020-03-16 17:26:14 +0100593/**
594 * @brief Generic prefix and namespace mapping, meaning depends on the format.
Radek Krejci1798aae2020-07-14 13:26:06 +0200595 *
596 * 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
Radek Krejci8678fa42020-08-18 16:07:28 +0200597 * ::ly_ctx_get_module_implemented_ns() or ::ly_ctx_get_module_implemented(). While the module reference is always present,
Michal Vaskoad92b672020-11-12 13:11:31 +0100598 * the prefix member can be omitted in case it is not present in the source data as a reference to the default module/namespace.
Michal Vasko52927e22020-03-16 17:26:14 +0100599 */
Michal Vaskoad92b672020-11-12 13:11:31 +0100600struct ly_opaq_name {
601 const char *name; /**< node name, without prefix if any was defined */
602 const char *prefix; /**< identifier used in the qualified name as the prefix, can be NULL */
Radek Krejci1798aae2020-07-14 13:26:06 +0200603 union {
Radek Krejci8df109d2021-04-23 12:19:08 +0200604 const char *module_ns; /**< format ::LY_VALUE_XML - XML namespace of the node element */
605 const char *module_name; /**< format ::LY_VALUE_JSON - (inherited) name of the module of the element */
Radek Krejci1798aae2020-07-14 13:26:06 +0200606 };
Michal Vasko52927e22020-03-16 17:26:14 +0100607};
608
609/**
610 * @brief Generic attribute structure.
611 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200612struct lyd_attr {
Michal Vasko52927e22020-03-16 17:26:14 +0100613 struct lyd_node_opaq *parent; /**< data node where the attribute is placed */
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100614 struct lyd_attr *next; /**< pointer to the next attribute */
Michal Vaskoad92b672020-11-12 13:11:31 +0100615 struct ly_opaq_name name; /**< attribute name with module information */
Michal Vasko501af032020-11-11 20:27:44 +0100616 const char *value; /**< attribute value */
Radek Krejci8df109d2021-04-23 12:19:08 +0200617 LY_VALUE_FORMAT format; /**< format of the attribute and any prefixes, ::LY_VALUE_XML or ::LY_VALUE_JSON */
Radek Krejciec9ad602021-01-04 10:46:30 +0100618 void *val_prefix_data; /**< format-specific prefix data */
Michal Vasko501af032020-11-11 20:27:44 +0100619 uint32_t hints; /**< additional information about from the data source, see the [hints list](@ref lydhints) */
Michal Vasko52927e22020-03-16 17:26:14 +0100620};
Radek Krejcie7b95092019-05-15 11:03:07 +0200621
Michal Vasko1bf09392020-03-27 12:38:10 +0100622#define LYD_NODE_INNER (LYS_CONTAINER|LYS_LIST|LYS_RPC|LYS_ACTION|LYS_NOTIF) /**< Schema nodetype mask for lyd_node_inner */
Radek Krejcie7b95092019-05-15 11:03:07 +0200623#define LYD_NODE_TERM (LYS_LEAF|LYS_LEAFLIST) /**< Schema nodetype mask for lyd_node_term */
624#define LYD_NODE_ANY (LYS_ANYDATA) /**< Schema nodetype mask for lyd_node_any */
625
626/**
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200627 * @ingroup datatree
Radek Krejci8678fa42020-08-18 16:07:28 +0200628 * @defgroup dnodeflags Data node flags
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200629 * @{
630 *
631 * Various flags of data nodes.
632 *
633 * 1 - container 5 - anydata/anyxml
634 * 2 - list 6 - rpc/action
635 * 3 - leaf 7 - notification
636 * 4 - leaflist
637 *
638 * bit name 1 2 3 4 5 6 7
639 * ---------------------+-+-+-+-+-+-+-+
640 * 1 LYD_DEFAULT |x| |x|x| | | |
641 * +-+-+-+-+-+-+-+
Michal Vasko5c4e5892019-11-14 12:31:38 +0100642 * 2 LYD_WHEN_TRUE |x|x|x|x|x| | |
Michal Vasko9b368d32020-02-14 13:53:31 +0100643 * +-+-+-+-+-+-+-+
644 * 3 LYD_NEW |x|x|x|x|x|x|x|
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200645 * ---------------------+-+-+-+-+-+-+-+
646 *
647 */
648
Michal Vasko5c4e5892019-11-14 12:31:38 +0100649#define LYD_DEFAULT 0x01 /**< default (implicit) node */
650#define LYD_WHEN_TRUE 0x02 /**< all when conditions of this node were evaluated to true */
Michal Vasko9b368d32020-02-14 13:53:31 +0100651#define LYD_NEW 0x04 /**< node was created after the last validation, is needed for the next validation */
Michal Vasko52927e22020-03-16 17:26:14 +0100652
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200653/** @} */
654
655/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200656 * @brief Generic structure for a data node.
657 */
658struct lyd_node {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200659 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
660 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
661 used to get know that nodes are not equal, it cannot be used to decide that the
662 nodes are equal due to possible collisions. */
663 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Radek Krejci2a9fc652021-01-22 17:44:34 +0100664 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200665 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
666 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
667 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
668 never NULL. If there is no sibling node, pointer points to the node
669 itself. In case of the first node, this pointer points to the last
670 node in the list. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100671 struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200672 void *priv; /**< private user data, not used by libyang */
Radek Krejcie7b95092019-05-15 11:03:07 +0200673};
674
675/**
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200676 * @brief Data node structure for the inner data tree nodes - containers, lists, RPCs, actions and Notifications.
Radek Krejcie7b95092019-05-15 11:03:07 +0200677 */
678struct lyd_node_inner {
Michal Vasko9e685082021-01-29 14:49:09 +0100679 union {
680 struct lyd_node node; /**< implicit cast for the members compatible with ::lyd_node */
681 struct {
682 uint32_t hash; /**< hash of this particular node (module name + schema name + key string
683 values if list or hashes of all nodes of subtree in case of keyless
684 list). Note that while hash can be used to get know that nodes are
685 not equal, it cannot be used to decide that the nodes are equal due
686 to possible collisions. */
687 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
688 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
689 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
690 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
691 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
692 never NULL. If there is no sibling node, pointer points to the node
693 itself. In case of the first node, this pointer points to the last
694 node in the list. */
695 struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
696 void *priv; /**< private user data, not used by libyang */
697 };
698 }; /**< common part corresponding to ::lyd_node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200699
700 struct lyd_node *child; /**< pointer to the first child node. */
701 struct hash_table *children_ht; /**< hash table with all the direct children (except keys for a list, lists without keys) */
Radek Krejci8678fa42020-08-18 16:07:28 +0200702#define LYD_HT_MIN_ITEMS 4 /**< minimal number of children to create ::lyd_node_inner.children_ht hash table. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200703};
704
705/**
Michal Vaskof03ed032020-03-04 13:31:44 +0100706 * @brief Data node structure for the terminal data tree nodes - leaves and leaf-lists.
Radek Krejcie7b95092019-05-15 11:03:07 +0200707 */
708struct lyd_node_term {
Michal Vasko9e685082021-01-29 14:49:09 +0100709 union {
710 struct lyd_node node; /**< implicit cast for the members compatible with ::lyd_node */
711 struct {
712 uint32_t hash; /**< hash of this particular node (module name + schema name + key string
713 values if list or hashes of all nodes of subtree in case of keyless
714 list). Note that while hash can be used to get know that nodes are
715 not equal, it cannot be used to decide that the nodes are equal due
716 to possible collisions. */
717 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
718 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
719 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
720 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
721 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
722 never NULL. If there is no sibling node, pointer points to the node
723 itself. In case of the first node, this pointer points to the last
724 node in the list. */
725 struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
726 void *priv; /**< private user data, not used by libyang */
727 };
728 }; /**< common part corresponding to ::lyd_node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200729
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200730 struct lyd_value value; /**< node's value representation */
Radek Krejcie7b95092019-05-15 11:03:07 +0200731};
732
733/**
Christian Hopps61213e02021-04-12 20:41:32 -0400734 * @brief union for anydata/anyxml value representation.
735 */
736union lyd_any_value {
737 struct lyd_node *tree; /**< data tree */
738 const char *str; /**< Generic string data */
739 const char *xml; /**< Serialized XML data */
740 const char *json; /**< I-JSON encoded string */
741 char *mem; /**< LYD_ANYDATA_LYB memory chunk */
742};
743
744/**
745 * @brief Data node structure for the anydata data tree nodes - anydata or
746 * anyxml.
Radek Krejcie7b95092019-05-15 11:03:07 +0200747 */
748struct lyd_node_any {
Michal Vasko9e685082021-01-29 14:49:09 +0100749 union {
750 struct lyd_node node; /**< implicit cast for the members compatible with ::lyd_node */
751 struct {
752 uint32_t hash; /**< hash of this particular node (module name + schema name + key string
753 values if list or hashes of all nodes of subtree in case of keyless
754 list). Note that while hash can be used to get know that nodes are
755 not equal, it cannot be used to decide that the nodes are equal due
756 to possible collisions. */
757 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
758 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
759 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
760 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
761 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
762 never NULL. If there is no sibling node, pointer points to the node
763 itself. In case of the first node, this pointer points to the last
764 node in the list. */
765 struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
766 void *priv; /**< private user data, not used by libyang */
767 };
768 }; /**< common part corresponding to ::lyd_node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200769
Christian Hopps61213e02021-04-12 20:41:32 -0400770 union lyd_any_value value; /**< pointer to the stored value representation of the anydata/anyxml node */
Michal Vasko9e685082021-01-29 14:49:09 +0100771 LYD_ANYDATA_VALUETYPE value_type; /**< type of the data stored as ::lyd_node_any.value */
Radek Krejcie7b95092019-05-15 11:03:07 +0200772};
773
774/**
Michal Vasko1d4af6c2021-02-22 13:31:26 +0100775 * @brief Get the name (associated with) of a data node. Works for opaque nodes as well.
776 *
777 * @param[in] node Node to examine.
778 * @return Data node name.
779 */
780#define LYD_NAME(node) ((node)->schema ? (node)->schema->name : ((struct lyd_node_opaq *)node)->name.name)
781
782/**
Radek Krejci8678fa42020-08-18 16:07:28 +0200783 * @ingroup datatree
784 * @defgroup lydvalhints Value format hints.
Radek Krejci1798aae2020-07-14 13:26:06 +0200785 * @{
Radek Krejci8678fa42020-08-18 16:07:28 +0200786 *
787 * Hints for the type of the data value.
788 *
789 * Any information about value types encoded in the format is hinted by these values.
Radek Krejci1798aae2020-07-14 13:26:06 +0200790 */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200791#define LYD_VALHINT_STRING 0x0001 /**< value is allowed to be a string */
792#define LYD_VALHINT_DECNUM 0x0002 /**< value is allowed to be a decimal number */
793#define LYD_VALHINT_OCTNUM 0x0004 /**< value is allowed to be an octal number */
794#define LYD_VALHINT_HEXNUM 0x0008 /**< value is allowed to be a hexadecimal number */
795#define LYD_VALHINT_NUM64 0x0010 /**< value is allowed to be an int64 or uint64 */
796#define LYD_VALHINT_BOOLEAN 0x0020 /**< value is allowed to be a boolean */
797#define LYD_VALHINT_EMPTY 0x0040 /**< value is allowed to be empty */
798/**
799 * @} lydvalhints
800 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200801
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200802/**
Radek Krejci8678fa42020-08-18 16:07:28 +0200803 * @ingroup datatree
804 * @defgroup lydnodehints Node type format hints
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200805 * @{
Radek Krejci8678fa42020-08-18 16:07:28 +0200806 *
807 * Hints for the type of the data node.
808 *
809 * Any information about node types encoded in the format is hinted by these values.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200810 */
811#define LYD_NODEHINT_LIST 0x0080 /**< node is allowed to be a list instance */
812#define LYD_NODEHINT_LEAFLIST 0x0100 /**< node is allowed to be a leaf-list instance */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200813/**
814 * @} lydnodehints
815 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200816
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200817/**
Radek Krejci8678fa42020-08-18 16:07:28 +0200818 * @ingroup datatree
819 * @defgroup lydhints Value and node type format hints
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200820 * @{
Radek Krejci8678fa42020-08-18 16:07:28 +0200821 *
822 * Hints for the types of data node and its value.
823 *
824 * Any information about value and node types encoded in the format is hinted by these values.
825 * It combines [value hints](@ref lydvalhints) and [node hints](@ref lydnodehints).
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200826 */
827#define LYD_HINT_DATA 0x01F3 /**< special node/value hint to be used for generic data node/value (for cases when
828 there is no encoding or it does not provide any additional information about
829 a node/value type); do not combine with specific [value hints](@ref lydvalhints)
830 or [node hints](@ref lydnodehints). */
831#define LYD_HINT_SCHEMA 0x01FF /**< special node/value hint to be used for generic schema node/value(for cases when
832 there is no encoding or it does not provide any additional information about
833 a node/value type); do not combine with specific [value hints](@ref lydvalhints)
834 or [node hints](@ref lydnodehints). */
835/**
836 * @} lydhints
837 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200838
839/**
Michal Vasko52927e22020-03-16 17:26:14 +0100840 * @brief Data node structure for unparsed (opaque) nodes.
841 */
842struct lyd_node_opaq {
Michal Vasko9e685082021-01-29 14:49:09 +0100843 union {
844 struct lyd_node node; /**< implicit cast for the members compatible with ::lyd_node */
845 struct {
846 uint32_t hash; /**< always 0 */
847 uint32_t flags; /**< always 0 */
848 const struct lysc_node *schema; /**< always NULL */
849 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
850 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
851 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
852 never NULL. If there is no sibling node, pointer points to the node
853 itself. In case of the first node, this pointer points to the last
854 node in the list. */
855 struct lyd_meta *meta; /**< always NULL */
856 void *priv; /**< private user data, not used by libyang */
857 };
858 }; /**< common part corresponding to ::lyd_node */
Michal Vasko52927e22020-03-16 17:26:14 +0100859
Michal Vasko501af032020-11-11 20:27:44 +0100860 struct lyd_node *child; /**< pointer to the child node (compatible with ::lyd_node_inner) */
861
Michal Vaskoad92b672020-11-12 13:11:31 +0100862 struct ly_opaq_name name; /**< node name with module information */
Michal Vasko52927e22020-03-16 17:26:14 +0100863 const char *value; /**< original value */
Radek Krejci8df109d2021-04-23 12:19:08 +0200864 LY_VALUE_FORMAT format; /**< format of the node and any prefixes, ::LY_VALUE_XML or ::LY_VALUE_JSON */
Radek Krejciec9ad602021-01-04 10:46:30 +0100865 void *val_prefix_data; /**< format-specific prefix data */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200866 uint32_t hints; /**< additional information about from the data source, see the [hints list](@ref lydhints) */
Michal Vasko501af032020-11-11 20:27:44 +0100867
Michal Vasko9e685082021-01-29 14:49:09 +0100868 struct lyd_attr *attr; /**< pointer to the list of generic attributes of this node */
Michal Vasko52927e22020-03-16 17:26:14 +0100869 const struct ly_ctx *ctx; /**< libyang context */
870};
871
872/**
Radek Krejcia1c1e542020-09-29 16:06:52 +0200873 * @brief Get the generic parent pointer of a data node.
874 *
875 * @param[in] node Node whose parent pointer to get.
876 * @return Pointer to the parent node of the @p node.
877 * @return NULL in case of the top-level node or if the @p node is NULL itself.
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200878 */
Christian Hopps59618972021-02-01 05:01:35 -0500879static inline struct lyd_node *
880lyd_parent(const struct lyd_node *node)
881{
882 if (!node) {
883 return NULL;
884 }
885
886 return &node->parent->node;
887}
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200888
889/**
Radek Krejcia1c1e542020-09-29 16:06:52 +0200890 * @brief Get the child pointer of a generic data node.
Radek Krejcie7b95092019-05-15 11:03:07 +0200891 *
Radek Krejcia1c1e542020-09-29 16:06:52 +0200892 * Decides the node's type and in case it has a children list, returns it. Supports even the opaq nodes (::lyd_node_opaq).
893 *
894 * If you need to skip key children, use ::lyd_child_no_keys().
895 *
896 * @param[in] node Node to use.
897 * @return Pointer to the first child node (if any) of the @p node.
Radek Krejcie7b95092019-05-15 11:03:07 +0200898 */
Christian Hopps59618972021-02-01 05:01:35 -0500899static inline struct lyd_node *
900lyd_child(const struct lyd_node *node)
901{
902 if (!node) {
903 return NULL;
904 }
905
906 if (!node->schema) {
907 /* opaq node */
908 return ((struct lyd_node_opaq *)node)->child;
909 }
910
911 switch (node->schema->nodetype) {
912 case LYS_CONTAINER:
913 case LYS_LIST:
914 case LYS_RPC:
915 case LYS_ACTION:
916 case LYS_NOTIF:
917 return ((struct lyd_node_inner *)node)->child;
918 default:
919 return NULL;
920 }
921}
Radek Krejcia1c1e542020-09-29 16:06:52 +0200922
923/**
924 * @brief Get the child pointer of a generic data node but skip its keys in case it is ::LYS_LIST.
925 *
926 * Decides the node's type and in case it has a children list, returns it. Supports even the opaq nodes (::lyd_node_opaq).
927 *
928 * If you need to take key children into account, use ::lyd_child().
929 *
930 * @param[in] node Node to use.
931 * @return Pointer to the first child node (if any) of the @p node.
932 */
933struct lyd_node *lyd_child_no_keys(const struct lyd_node *node);
Radek Krejcie7b95092019-05-15 11:03:07 +0200934
935/**
Michal Vaskoc193ce92020-03-06 11:04:48 +0100936 * @brief Get the owner module of the data node. It is the module of the top-level schema node. Generally,
937 * in case of augments it is the target module, recursively, otherwise it is the module where the data node is defined.
938 *
Michal Vaskofcdf3012020-11-23 16:57:03 +0100939 * Also works for opaque nodes, if it is possible to resolve the module.
940 *
Michal Vaskoc193ce92020-03-06 11:04:48 +0100941 * @param[in] node Data node to examine.
942 * @return Module owner of the node.
943 */
944const struct lys_module *lyd_owner_module(const struct lyd_node *node);
945
946/**
Radek Krejci19611252020-10-04 13:54:53 +0200947 * @brief Check whether a node value equals to its default one.
948 *
949 * @param[in] node Term node to test.
950 * @return false (no, it is not a default node) or true (yes, it is default)
951 */
952ly_bool lyd_is_default(const struct lyd_node *node);
953
954/**
Radek Krejcica989142020-11-05 11:32:22 +0100955 * @brief Learn the relative position of a list or leaf-list instance within other instances of the same schema node.
956 *
957 * @param[in] instance List or leaf-list instance to get the position of.
Michal Vaskoe78faec2021-04-08 17:24:43 +0200958 * @return 0 on error.
959 * @return Positive integer of the @p instance position.
Radek Krejcica989142020-11-05 11:32:22 +0100960 */
961uint32_t lyd_list_pos(const struct lyd_node *instance);
962
963/**
Radek Krejci4233f9b2020-11-05 12:38:35 +0100964 * @brief Get the first sibling of the given node.
965 *
966 * @param[in] node Node which first sibling is going to be the result.
967 * @return The first sibling of the given node or the node itself if it is the first child of the parent.
968 */
Michal Vasko6ae16d62020-11-06 17:23:23 +0100969struct lyd_node *lyd_first_sibling(const struct lyd_node *node);
Radek Krejci4233f9b2020-11-05 12:38:35 +0100970
971/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200972 * @brief Learn the length of LYB data.
973 *
974 * @param[in] data LYB data to examine.
975 * @return Length of the LYB data chunk,
976 * @return -1 on error.
977 */
978int lyd_lyb_data_length(const char *data);
979
980/**
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200981 * @brief Get the (canonical) value of a data node.
982 *
983 * @param[in] node Data node to use.
984 * @return Canonical value.
985 */
986const char *lyd_get_value(const struct lyd_node *node);
987
988/**
989 * @brief Get the (canonical) value of a metadata node.
990 *
991 * @param[in] meta Metadata node to use.
992 * @return Canonical value.
993 */
994const char *lyd_get_meta_value(const struct lyd_meta *meta);
995
996/**
Michal Vaskoa820c312021-02-05 16:33:00 +0100997 * @brief Get anydata string value.
998 *
999 * @param[in] any Anyxml/anydata node to read from.
1000 * @param[out] value_str String representation of the value.
1001 * @return LY_ERR value.
1002 */
1003LY_ERR lyd_any_value_str(const struct lyd_node *any, char **value_str);
1004
1005/**
Michal Vaskoc0004272020-08-06 08:32:34 +02001006 * @brief Copy anydata value from one node to another. Target value is freed first.
1007 *
1008 * @param[in,out] trg Target node.
1009 * @param[in] value Source value, may be NULL when the target value is only freed.
1010 * @param[in] value_type Source value type.
1011 * @return LY_ERR value.
1012 */
1013LY_ERR lyd_any_copy_value(struct lyd_node *trg, const union lyd_any_value *value, LYD_ANYDATA_VALUETYPE value_type);
1014
1015/**
Michal Vasko00cbf532020-06-15 13:58:47 +02001016 * @brief Create a new inner node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +01001017 *
Radek Krejcidd2a7662021-03-12 11:26:34 +01001018 * To create list, use ::lyd_new_list() or ::lyd_new_list2().
1019 *
1020 * To create a top-level inner node defined in an extension instance, use ::lyd_new_ext_inner().
1021 *
Michal Vasko013a8182020-03-03 10:46:53 +01001022 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
Michal Vaskof03ed032020-03-04 13:31:44 +01001023 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +01001024 * @param[in] name Schema node name of the new data node. The node can be #LYS_CONTAINER, #LYS_NOTIF, #LYS_RPC, or #LYS_ACTION.
Radek Krejci41ac9942020-11-02 14:47:56 +01001025 * @param[in] output Flag in case the @p parent is RPC/Action. If value is 0, the input's data nodes of the RPC/Action are
1026 * taken into consideration. Otherwise, the output's data node is going to be created.
Michal Vasko3a41dff2020-07-15 14:30:28 +02001027 * @param[out] node Optional created node.
1028 * @return LY_ERR value.
Michal Vasko013a8182020-03-03 10:46:53 +01001029 */
Michal Vasko53587422021-02-05 16:34:13 +01001030LY_ERR lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
1031 struct lyd_node **node);
Michal Vasko013a8182020-03-03 10:46:53 +01001032
1033/**
Radek Krejcidd2a7662021-03-12 11:26:34 +01001034 * @brief Create a new top-level inner node defined in the given extension instance.
1035 *
1036 * To create list, use ::lyd_new_list() or ::lyd_new_list2().
1037 *
1038 * To create an inner node with parent (no matter if defined inside extension instance or a standard tree) or a top-level
1039 * node of a standard module's tree, use ::lyd_new_inner().
1040 *
1041 * @param[in] ext Extension instance where the inner node being created is defined.
1042 * @param[in] name Schema node name of the new data node. The node can be #LYS_CONTAINER, #LYS_NOTIF, #LYS_RPC, or #LYS_ACTION.
1043 * @param[out] node The created node.
1044 * @return LY_ERR value.
1045 */
1046LY_ERR lyd_new_ext_inner(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node);
1047
1048/**
Michal Vasko00cbf532020-06-15 13:58:47 +02001049 * @brief Create a new list node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +01001050 *
1051 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
Michal Vaskof03ed032020-03-04 13:31:44 +01001052 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +01001053 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
Radek Krejci41ac9942020-11-02 14:47:56 +01001054 * @param[in] output Flag in case the @p parent is RPC/Action. If value is 0, the input's data nodes of the RPC/Action are
1055 * taken into consideration. Otherwise, the output's data node is going to be created.
Michal Vasko3a41dff2020-07-15 14:30:28 +02001056 * @param[out] node Optional created node.
Michal Vasko013a8182020-03-03 10:46:53 +01001057 * @param[in] ... Ordered key values of the new list instance, all must be set. In case of an instance-identifier
Michal Vasko004d3152020-06-11 19:59:22 +02001058 * or identityref value, the JSON format is expected (module names instead of prefixes). No keys are expected for
1059 * key-less lists.
Michal Vasko3a41dff2020-07-15 14:30:28 +02001060 * @return LY_ERR value.
Michal Vasko013a8182020-03-03 10:46:53 +01001061 */
Radek Krejci55c4bd22021-04-26 08:09:04 +02001062LY_ERR lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ly_bool output,
1063 struct lyd_node **node, ...);
Michal Vasko013a8182020-03-03 10:46:53 +01001064
1065/**
Radek Krejci8247bae2021-03-12 11:47:02 +01001066 * @brief Create a new top-level list node defined in the given extension instance.
1067 *
1068 * To create a list node with parent (no matter if defined inside extension instance or a standard tree) or a top-level
1069 * list node of a standard module's tree, use ::lyd_new_list() or ::lyd_new_list2().
1070 *
1071 * @param[in] ext Extension instance where the list node being created is defined.
1072 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
1073 * @param[out] node The created node.
1074 * @param[in] ... Ordered key values of the new list instance, all must be set. In case of an instance-identifier
1075 * or identityref value, the JSON format is expected (module names instead of prefixes). No keys are expected for
1076 * key-less lists.
1077 * @return LY_ERR value.
1078 */
1079LY_ERR lyd_new_ext_list(const struct lysc_ext_instance *ext, const char *name, struct lyd_node **node, ...);
1080
1081/**
Michal Vasko00cbf532020-06-15 13:58:47 +02001082 * @brief Create a new list node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +01001083 *
1084 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
Michal Vaskof03ed032020-03-04 13:31:44 +01001085 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +01001086 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
1087 * @param[in] keys All key values predicate in the form of "[key1='val1'][key2='val2']...", they do not have to be ordered.
1088 * In case of an instance-identifier or identityref value, the JSON format is expected (module names instead of prefixes).
Michal Vasko004d3152020-06-11 19:59:22 +02001089 * Use NULL or string of length 0 in case of key-less list.
Radek Krejci41ac9942020-11-02 14:47:56 +01001090 * @param[in] output Flag in case the @p parent is RPC/Action. If value is 0, the input's data nodes of the RPC/Action are
1091 * taken into consideration. Otherwise, the output's data node is going to be created.
Michal Vasko3a41dff2020-07-15 14:30:28 +02001092 * @param[out] node Optional created node.
1093 * @return LY_ERR value.
Michal Vasko013a8182020-03-03 10:46:53 +01001094 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001095LY_ERR lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
Radek Krejci41ac9942020-11-02 14:47:56 +01001096 ly_bool output, struct lyd_node **node);
Michal Vasko013a8182020-03-03 10:46:53 +01001097
1098/**
Michal Vasko00cbf532020-06-15 13:58:47 +02001099 * @brief Create a new term node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +01001100 *
Radek Krejci8a5afc22021-03-12 11:10:47 +01001101 * To create a top-level term node defined in an extension instance, use ::lyd_new_ext_term().
1102 *
Michal Vasko013a8182020-03-03 10:46:53 +01001103 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
Michal Vaskof03ed032020-03-04 13:31:44 +01001104 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +01001105 * @param[in] name Schema node name of the new data node. The node can be #LYS_LEAF or #LYS_LEAFLIST.
Radek Krejci09c77442021-04-26 11:10:34 +02001106 * @param[in] val_str String value of the node. If it varies based on the format, ::LY_VALUE_JSON is expected.
Radek Krejci41ac9942020-11-02 14:47:56 +01001107 * @param[in] output Flag in case the @p parent is RPC/Action. If value is 0, the input's data nodes of the RPC/Action are
1108 * taken into consideration. Otherwise, the output's data node is going to be created.
Michal Vasko3a41dff2020-07-15 14:30:28 +02001109 * @param[out] node Optional created node.
1110 * @return LY_ERR value.
Michal Vasko013a8182020-03-03 10:46:53 +01001111 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001112LY_ERR lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
Radek Krejci41ac9942020-11-02 14:47:56 +01001113 ly_bool output, struct lyd_node **node);
Michal Vasko013a8182020-03-03 10:46:53 +01001114
1115/**
Radek Krejci09c77442021-04-26 11:10:34 +02001116 * @brief Create a new term node in the data tree.
1117 *
1118 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
1119 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
1120 * @param[in] name Schema node name of the new data node. The node can be #LYS_LEAF or #LYS_LEAFLIST.
1121 * @param[in] value Binary value of the node. To learn what exactly is expected see @ref howtoDataLYB.
1122 * @param[in] value_len Length of @p value.
1123 * @param[in] output Flag in case the @p parent is RPC/Action. If value is 0, the input's data nodes of the RPC/Action are
1124 * taken into consideration. Otherwise, the output's data node is going to be created.
1125 * @param[out] node Optional created node.
1126 * @return LY_ERR value.
1127 */
1128LY_ERR lyd_new_term_bin(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1129 size_t value_len, ly_bool output, struct lyd_node **node);
1130
1131/**
1132 * @brief Create a new term node in the data tree.
1133 *
1134 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
1135 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
1136 * @param[in] name Schema node name of the new data node. The node can be #LYS_LEAF or #LYS_LEAFLIST.
1137 * @param[in] val_str Canonical string value of the node. If it is not, it may lead to unexpected behavior.
1138 * @param[in] output Flag in case the @p parent is RPC/Action. If value is 0, the input's data nodes of the RPC/Action are
1139 * taken into consideration. Otherwise, the output's data node is going to be created.
1140 * @param[out] node Optional created node.
1141 * @return LY_ERR value.
1142 */
1143LY_ERR lyd_new_term_canon(struct lyd_node *parent, const struct lys_module *module, const char *name,
1144 const char *val_str, ly_bool output, struct lyd_node **node);
1145
1146/**
Radek Krejci8a5afc22021-03-12 11:10:47 +01001147 * @brief Create a new top-level term node defined in the given extension instance.
1148 *
1149 * To create a term node with parent (no matter if defined inside extension instance or a standard tree) or a top-level
1150 * node of a standard module's tree, use ::lyd_new_term().
1151 *
1152 * @param[in] ext Extension instance where the term node being created is defined.
1153 * @param[in] name Schema node name of the new data node. The node can be #LYS_LEAF or #LYS_LEAFLIST.
1154 * @param[in] val_str String form of the value of the node being created. In case of an instance-identifier or identityref
1155 * value, the JSON format is expected (module names instead of prefixes).
1156 * @param[out] node The created node.
1157 * @return LY_ERR value.
1158 */
1159LY_ERR lyd_new_ext_term(const struct lysc_ext_instance *ext, const char *name, const char *val_str, struct lyd_node **node);
1160
1161/**
Michal Vasko00cbf532020-06-15 13:58:47 +02001162 * @brief Create a new any node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +01001163 *
Radek Krejci0b963da2021-03-12 13:23:44 +01001164 * To create a top-level any node defined in an extension instance, use ::lyd_new_ext_any().
1165 *
Michal Vasko013a8182020-03-03 10:46:53 +01001166 * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element.
Michal Vaskof03ed032020-03-04 13:31:44 +01001167 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +01001168 * @param[in] name Schema node name of the new data node. The node can be #LYS_ANYDATA or #LYS_ANYXML.
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001169 * @param[in] value Value for the node. Expected type is determined by @p value_type.
1170 * @param[in] use_value Whether to directly take @p value and assign it to the node or make a copy.
Michal Vasko013a8182020-03-03 10:46:53 +01001171 * @param[in] value_type Type of the provided value in @p value.
Radek Krejci41ac9942020-11-02 14:47:56 +01001172 * @param[in] output Flag in case the @p parent is RPC/Action. If value is 0, the input's data nodes of the RPC/Action are
1173 * taken into consideration. Otherwise, the output's data node is going to be created.
Michal Vasko3a41dff2020-07-15 14:30:28 +02001174 * @param[out] node Optional created node.
1175 * @return LY_ERR value.
Michal Vasko013a8182020-03-03 10:46:53 +01001176 */
Michal Vasko2a4ab2b2021-03-04 15:52:40 +01001177LY_ERR lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
1178 ly_bool use_value, LYD_ANYDATA_VALUETYPE value_type, ly_bool output, struct lyd_node **node);
Michal Vasko013a8182020-03-03 10:46:53 +01001179
1180/**
Radek Krejci0b963da2021-03-12 13:23:44 +01001181 * @brief Create a new top-level any node defined in the given extension instance.
1182 *
1183 * To create an any node with parent (no matter if defined inside extension instance or a standard tree) or a top-level
1184 * any node of a standard module's tree, use ::lyd_new_any().
1185 *
1186 * @param[in] ext Extension instance where the any node being created is defined.
1187 * @param[in] name Schema node name of the new data node. The node can be #LYS_ANYDATA or #LYS_ANYXML.
1188 * @param[in] value Value for the node. Expected type is determined by @p value_type.
1189 * @param[in] use_value Whether to directly take @p value and assign it to the node or make a copy.
1190 * @param[in] value_type Type of the provided value in @p value.
1191 * @param[out] node The created node.
1192 * @return LY_ERR value.
1193 */
1194LY_ERR lyd_new_ext_any(const struct lysc_ext_instance *ext, const char *name, const void *value, ly_bool use_value,
1195 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node);
1196
1197/**
Michal Vasko871a0252020-11-11 18:35:24 +01001198 * @brief Create new metadata.
Michal Vaskod86997b2020-05-26 15:19:54 +02001199 *
Michal Vasko871a0252020-11-11 18:35:24 +01001200 * @param[in] ctx libyang context,
1201 * @param[in] parent Optional parent node for the metadata being created. Must be set if @p meta is NULL.
Michal Vasko00cbf532020-06-15 13:58:47 +02001202 * @param[in] module Module of the metadata being created. If NULL, @p name must include module name as the prefix.
Michal Vaskod86997b2020-05-26 15:19:54 +02001203 * @param[in] name Annotation name of the new metadata. It can include the annotation module as the prefix.
1204 * If the prefix is specified it is always used but if not specified, @p module must be set.
Michal Vasko00cbf532020-06-15 13:58:47 +02001205 * @param[in] val_str String form of the value of the metadata. In case of an instance-identifier or identityref
Michal Vaskod86997b2020-05-26 15:19:54 +02001206 * value, the JSON format is expected (module names instead of prefixes).
Michal Vasko871a0252020-11-11 18:35:24 +01001207 * @param[in] clear_dflt Whether to clear the default flag starting from @p parent, recursively all NP containers.
1208 * @param[out] meta Optional created metadata. Must be set if @p parent is NULL.
Michal Vasko3a41dff2020-07-15 14:30:28 +02001209 * @return LY_ERR value.
Michal Vaskod86997b2020-05-26 15:19:54 +02001210 */
Michal Vasko871a0252020-11-11 18:35:24 +01001211LY_ERR lyd_new_meta(const struct ly_ctx *ctx, struct lyd_node *parent, const struct lys_module *module, const char *name,
1212 const char *val_str, ly_bool clear_dflt, struct lyd_meta **meta);
Michal Vaskod86997b2020-05-26 15:19:54 +02001213
1214/**
Michal Vaskoba696702020-11-11 19:12:45 +01001215 * @brief Create new metadata from an opaque node attribute if possible.
1216 *
1217 * @param[in] ctx libyang context.
1218 * @param[in] parent Optional parent node for the metadata being created. Must be set if @p meta is NULL.
1219 * @param[in] clear_dflt Whether to clear the default flag starting from @p parent, recursively all NP containers.
1220 * @param[in] attr Opaque node attribute to parse into metadata.
1221 * @param[out] meta Optional created metadata. Must be set if @p parent is NULL.
1222 * @return LY_SUCCESS on success.
1223 * @return LY_ENOT if the attribute could not be parsed into any metadata.
1224 * @return LY_ERR on error.
1225 */
1226LY_ERR lyd_new_meta2(const struct ly_ctx *ctx, struct lyd_node *parent, ly_bool clear_dflt, const struct lyd_attr *attr,
1227 struct lyd_meta **meta);
1228
1229/**
Michal Vasko8d65f852021-02-17 11:28:13 +01001230 * @brief Create a new JSON opaque node in the data tree. To create an XML opaque node, use ::lyd_new_opaq2().
Michal Vasko00cbf532020-06-15 13:58:47 +02001231 *
1232 * @param[in] parent Parent node for the node beaing created. NULL in case of creating a top level element.
1233 * @param[in] ctx libyang context. If NULL, @p parent context will be used.
1234 * @param[in] name Node name.
Michal Vasko0ab974d2021-02-24 13:18:26 +01001235 * @param[in] value Optional node value.
1236 * @param[in] prefix Optional node prefix, must be equal to @p module_name if set.
Michal Vasko00cbf532020-06-15 13:58:47 +02001237 * @param[in] module_name Node module name.
Michal Vasko3a41dff2020-07-15 14:30:28 +02001238 * @param[out] node Optional created node.
1239 * @return LY_ERR value.
Michal Vasko00cbf532020-06-15 13:58:47 +02001240 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001241LY_ERR lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001242 const char *prefix, const char *module_name, struct lyd_node **node);
Michal Vasko00cbf532020-06-15 13:58:47 +02001243
1244/**
Michal Vasko8d65f852021-02-17 11:28:13 +01001245 * @brief Create a new XML opaque node in the data tree. To create a JSON opaque node, use ::lyd_new_opaq().
1246 *
1247 * @param[in] parent Parent node for the node beaing created. NULL in case of creating a top level element.
1248 * @param[in] ctx libyang context. If NULL, @p parent context will be used.
1249 * @param[in] name Node name.
Michal Vasko0ab974d2021-02-24 13:18:26 +01001250 * @param[in] value Optional node value.
1251 * @param[in] prefix Optional node prefix.
Michal Vasko8d65f852021-02-17 11:28:13 +01001252 * @param[in] module_ns Node module namespace.
1253 * @param[out] node Optional created node.
1254 * @return LY_ERR value.
1255 */
1256LY_ERR lyd_new_opaq2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Michal Vasko0ab974d2021-02-24 13:18:26 +01001257 const char *prefix, const char *module_ns, struct lyd_node **node);
Michal Vasko8d65f852021-02-17 11:28:13 +01001258
1259/**
1260 * @brief Create new JSON attribute for an opaque data node. To create an XML attribute, use ::lyd_new_attr2().
Michal Vasko00cbf532020-06-15 13:58:47 +02001261 *
1262 * @param[in] parent Parent opaque node for the attribute being created.
Radek Krejci8678fa42020-08-18 16:07:28 +02001263 * @param[in] module_name Name of the module of the attribute being created. There may be none.
Michal Vasko00cbf532020-06-15 13:58:47 +02001264 * @param[in] name Attribute name. It can include the module name as the prefix.
Michal Vasko8d65f852021-02-17 11:28:13 +01001265 * @param[in] value Attribute value, may be NULL.
Michal Vasko3a41dff2020-07-15 14:30:28 +02001266 * @param[out] attr Optional created attribute.
1267 * @return LY_ERR value.
Michal Vasko00cbf532020-06-15 13:58:47 +02001268 */
Michal Vasko8d65f852021-02-17 11:28:13 +01001269LY_ERR lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *value,
1270 struct lyd_attr **attr);
1271
1272/**
1273 * @brief Create new XML attribute for an opaque data node. To create a JSON attribute, use ::lyd_new_attr().
1274 *
1275 * @param[in] parent Parent opaque node for the attribute being created.
1276 * @param[in] module_ns Namespace of the module of the attribute being created. There may be none.
1277 * @param[in] name Attribute name. It can include an XML prefix.
1278 * @param[in] value Attribute value, may be NULL.
1279 * @param[out] attr Optional created attribute.
1280 * @return LY_ERR value.
1281 */
1282LY_ERR lyd_new_attr2(struct lyd_node *parent, const char *module_ns, const char *name, const char *value,
Radek Krejci0f969882020-08-21 16:56:47 +02001283 struct lyd_attr **attr);
Michal Vasko00cbf532020-06-15 13:58:47 +02001284
1285/**
Michal Vasko00cbf532020-06-15 13:58:47 +02001286 * @ingroup datatree
Radek Krejci8678fa42020-08-18 16:07:28 +02001287 * @defgroup pathoptions Data path creation options
Michal Vasko00cbf532020-06-15 13:58:47 +02001288 *
1289 * Various options to change lyd_new_path*() behavior.
1290 *
1291 * Default behavior:
1292 * - if the target node already exists (and is not default), an error is returned.
1293 * - the whole path to the target node is created (with any missing parents) if necessary.
1294 * - RPC output schema children are completely ignored in all modules. Input is searched and nodes created normally.
1295 * @{
1296 */
1297
Radek Krejci09c77442021-04-26 11:10:34 +02001298#define LYD_NEW_PATH_UPDATE 0x01 /**< If the target node exists, is a leaf, and it is updated with a new value or its
1299 default flag is changed, it is returned. If the target node exists and is not
1300 a leaf or generally no change occurs in the @p parent tree, NULL is returned and
1301 no error set. */
1302#define LYD_NEW_PATH_OUTPUT 0x02 /**< Changes the behavior to ignoring RPC/action input schema nodes and using only
1303 output ones. */
1304#define LYD_NEW_PATH_OPAQ 0x04 /**< Enables the creation of opaque nodes with some specific rules. If the __last node__
1305 in the path is not uniquely defined ((leaf-)list without a predicate) or has an
1306 invalid value (leaf/leaf-list), it is created as opaque. */
1307#define LYD_NEW_PATH_BIN_VALUE 0x08 /**< Interpret the provided leaf/leaf-list @p value as being in the binary
1308 ::LY_VALUE_LYB format, to learn what exactly is expected see @ref howtoDataLYB. */
1309#define LYD_NEW_PATH_CANON_VALUE 0x10 /**< Interpret the provided leaf/leaf-list @p value as being in the canonical
1310 (or JSON if no defined) ::LY_VALUE_CANON format. If it is not, it may lead
1311 to unexpected behavior. */
Michal Vasko00cbf532020-06-15 13:58:47 +02001312
1313/** @} pathoptions */
1314
1315/**
Michal Vaskod5e67442021-03-04 15:56:31 +01001316 * @brief Create a new node in the data tree based on a path. If creating anyxml/anydata nodes, ::lyd_new_path2
1317 * should be used instead, this function expects the value as string.
Michal Vasko00cbf532020-06-15 13:58:47 +02001318 *
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001319 * If creating data nodes defined inside an extension instance, use ::lyd_new_ext_path().
1320 *
Michal Vasko00cbf532020-06-15 13:58:47 +02001321 * If @p path points to a list key and the list instance does not exist, the key value from the predicate is used
1322 * and @p value is ignored. Also, if a leaf-list is being created and both a predicate is defined in @p path
1323 * and @p value is set, the predicate is preferred.
1324 *
Michal Vasko65591ab2021-04-09 14:29:34 +02001325 * For key-less lists and non-configuration leaf-lists, positional predicates should be used. If no predicate is used
1326 * for these nodes, they are always created.
Michal Vasko6741dc62021-02-04 09:27:45 +01001327 *
Michal Vasko104fe962020-11-06 17:23:44 +01001328 * @param[in] parent Data parent to add to/modify, can be NULL. Note that in case a first top-level sibling is used,
1329 * it may no longer be first if @p path is absolute and starts with a non-existing top-level node inserted
1330 * before @p parent. Use ::lyd_first_sibling() to adjust @p parent in these cases.
Michal Vasko00cbf532020-06-15 13:58:47 +02001331 * @param[in] ctx libyang context, must be set if @p parent is NULL.
Michal Vasko104fe962020-11-06 17:23:44 +01001332 * @param[in] path [Path](@ref howtoXPath) to create.
Radek Krejci09c77442021-04-26 11:10:34 +02001333 * @param[in] value String value of the new leaf/leaf-list. If it varies based on the format, ::LY_VALUE_JSON is expected.
1334 * For other node types, it should be NULL.
Michal Vasko00cbf532020-06-15 13:58:47 +02001335 * @param[in] options Bitmask of options, see @ref pathoptions.
Michal Vasko3a41dff2020-07-15 14:30:28 +02001336 * @param[out] node Optional first created node.
1337 * @return LY_ERR value.
Michal Vasko00cbf532020-06-15 13:58:47 +02001338 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001339LY_ERR lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value,
Radek Krejci1deb5be2020-08-26 16:43:36 +02001340 uint32_t options, struct lyd_node **node);
Michal Vasko00cbf532020-06-15 13:58:47 +02001341
1342/**
1343 * @brief Create a new node in the data tree based on a path. All node types can be created.
1344 *
Michal Vasko65591ab2021-04-09 14:29:34 +02001345 * Details are mentioned in ::lyd_new_path().
Michal Vasko6741dc62021-02-04 09:27:45 +01001346 *
Michal Vasko104fe962020-11-06 17:23:44 +01001347 * @param[in] parent Data parent to add to/modify, can be NULL. Note that in case a first top-level sibling is used,
1348 * it may no longer be first if @p path is absolute and starts with a non-existing top-level node inserted
1349 * before @p parent. Use ::lyd_first_sibling() to adjust @p parent in these cases.
Michal Vasko00cbf532020-06-15 13:58:47 +02001350 * @param[in] ctx libyang context, must be set if @p parent is NULL.
Michal Vasko104fe962020-11-06 17:23:44 +01001351 * @param[in] path [Path](@ref howtoXPath) to create.
Radek Krejci09c77442021-04-26 11:10:34 +02001352 * @param[in] value Value of the new leaf/leaf-list (const char *) in ::LY_VALUE_JSON format. If creating an
1353 * anyxml/anydata node, the expected type depends on @p value_type. For other node types, it should be NULL.
1354 * @param[in] value_len Length of @p value in bytes. May be 0 if @p value is a zero-terminated string. Ignored when
1355 * creating anyxml/anydata nodes.
Michal Vasko00cbf532020-06-15 13:58:47 +02001356 * @param[in] value_type Anyxml/anydata node @p value type.
1357 * @param[in] options Bitmask of options, see @ref pathoptions.
Michal Vasko3a41dff2020-07-15 14:30:28 +02001358 * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node.
1359 * @param[out] new_node Optional last node created.
Michal Vasko00cbf532020-06-15 13:58:47 +02001360 * @return LY_ERR value.
1361 */
1362LY_ERR lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
Radek Krejci09c77442021-04-26 11:10:34 +02001363 size_t value_len, LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent,
1364 struct lyd_node **new_node);
Michal Vasko00cbf532020-06-15 13:58:47 +02001365
1366/**
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001367 * @brief Create a new node defined in the given extension instance. In case of anyxml/anydata nodes, this function expects
1368 * the @p value as string.
1369 *
1370 * If creating data nodes defined in a module's standard tree, use ::lyd_new_path() or ::lyd_new_path2().
1371 *
Michal Vasko65591ab2021-04-09 14:29:34 +02001372 * Details are mentioned in ::lyd_new_path().
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001373 *
1374 * @param[in] parent Data parent to add to/modify, can be NULL. Note that in case a first top-level sibling is used,
1375 * it may no longer be first if @p path is absolute and starts with a non-existing top-level node inserted
1376 * before @p parent. Use ::lyd_first_sibling() to adjust @p parent in these cases.
1377 * @param[in] ext Extension instance where the node being created is defined.
1378 * @param[in] path [Path](@ref howtoXPath) to create.
Radek Krejci09c77442021-04-26 11:10:34 +02001379 * @param[in] value Value of the new leaf/leaf-list. For other node types, it should be NULL.
Radek Krejci95ccd1b2021-03-12 14:57:22 +01001380 * @param[in] options Bitmask of options, see @ref pathoptions.
1381 * @param[out] node Optional first created node.
1382 * @return LY_ERR value.
1383 */
1384LY_ERR lyd_new_ext_path(struct lyd_node *parent, const struct lysc_ext_instance *ext, const char *path, const void *value,
1385 uint32_t options, struct lyd_node **node);
1386
1387/**
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001388 * @ingroup datatree
Radek Krejci8678fa42020-08-18 16:07:28 +02001389 * @defgroup implicitoptions Implicit node creation options
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001390 *
1391 * Various options to change lyd_new_implicit*() behavior.
1392 *
1393 * Default behavior:
1394 * - both configuration and state missing implicit nodes are added.
Michal Vasko630d9892020-12-08 17:11:08 +01001395 * - for existing RPC/action nodes, input implicit nodes are added.
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001396 * - all implicit node types are added (non-presence containers, default leaves, and default leaf-lists).
1397 * @{
1398 */
1399
Michal Vasko44b19a12020-08-07 09:21:30 +02001400#define LYD_IMPLICIT_NO_STATE 0x01 /**< Do not add any implicit state nodes. */
1401#define LYD_IMPLICIT_NO_CONFIG 0x02 /**< Do not add any implicit config nodes. */
Michal Vasko630d9892020-12-08 17:11:08 +01001402#define LYD_IMPLICIT_OUTPUT 0x04 /**< For RPC/action nodes, add output implicit nodes instead of input. */
1403#define LYD_IMPLICIT_NO_DEFAULTS 0x08 /**< Do not add any default nodes (leaves/leaf-lists), only non-presence
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001404 containers. */
1405
1406/** @} implicitoptions */
1407
1408/**
Michal Vasko3488ada2020-12-03 14:18:19 +01001409 * @brief Add any missing implicit nodes into a data subtree. Default nodes with a false "when" are not added.
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001410 *
1411 * @param[in] tree Tree to add implicit nodes into.
1412 * @param[in] implicit_options Options for implicit node creation, see @ref implicitoptions.
1413 * @param[out] diff Optional diff with any created nodes.
1414 * @return LY_ERR value.
1415 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02001416LY_ERR lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001417
1418/**
Michal Vasko3488ada2020-12-03 14:18:19 +01001419 * @brief Add any missing implicit nodes. Default nodes with a false "when" are not added.
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001420 *
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001421 * @param[in,out] tree Tree to add implicit nodes into. Note that in case a first top-level sibling is used,
1422 * it may no longer be first if an implicit node was inserted before @p tree. Use ::lyd_first_sibling() to
1423 * adjust @p tree in these cases.
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001424 * @param[in] ctx libyang context, must be set only if @p tree is an empty tree.
1425 * @param[in] implicit_options Options for implicit node creation, see @ref implicitoptions.
1426 * @param[out] diff Optional diff with any created nodes.
1427 * @return LY_ERR value.
1428 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02001429LY_ERR lyd_new_implicit_all(struct lyd_node **tree, const struct ly_ctx *ctx, uint32_t implicit_options, struct lyd_node **diff);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001430
1431/**
Michal Vasko3488ada2020-12-03 14:18:19 +01001432 * @brief Add any missing implicit nodes of one module. Default nodes with a false "when" are not added.
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001433 *
Michal Vaskod3bb12f2020-12-04 14:33:09 +01001434 * @param[in,out] tree Tree to add implicit nodes into. Note that in case a first top-level sibling is used,
1435 * it may no longer be first if an implicit node was inserted before @p tree. Use ::lyd_first_sibling() to
1436 * adjust @p tree in these cases.
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001437 * @param[in] module Module whose implicit nodes to create.
1438 * @param[in] implicit_options Options for implicit node creation, see @ref implicitoptions.
1439 * @param[out] diff Optional diff with any created nodes.
1440 * @return LY_ERR value.
1441 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02001442LY_ERR lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, uint32_t implicit_options,
Radek Krejci0f969882020-08-21 16:56:47 +02001443 struct lyd_node **diff);
Michal Vaskoa6669ba2020-08-06 16:14:26 +02001444
1445/**
Radek Krejci09c77442021-04-26 11:10:34 +02001446 * @brief Change the value of a term (leaf or leaf-list) node to a string value.
Michal Vasko00cbf532020-06-15 13:58:47 +02001447 *
1448 * Node changed this way is always considered explicitly set, meaning its default flag
1449 * is always cleared.
1450 *
1451 * @param[in] term Term node to change.
1452 * @param[in] val_str New value to set, any prefixes are expected in JSON format.
1453 * @return LY_SUCCESS if value was changed,
1454 * @return LY_EEXIST if value was the same and only the default flag was cleared,
1455 * @return LY_ENOT if the values were equal and no change occured,
1456 * @return LY_ERR value on other errors.
1457 */
1458LY_ERR lyd_change_term(struct lyd_node *term, const char *val_str);
1459
1460/**
Radek Krejci09c77442021-04-26 11:10:34 +02001461 * @brief Change the value of a term (leaf or leaf-list) node to a binary value.
1462 *
1463 * Node changed this way is always considered explicitly set, meaning its default flag
1464 * is always cleared.
1465 *
1466 * @param[in] term Term node to change.
1467 * @param[in] value New value to set in binary format, see @ref howtoDataLYB.
1468 * @param[in] value_len Length of @p value.
1469 * @return LY_SUCCESS if value was changed,
1470 * @return LY_EEXIST if value was the same and only the default flag was cleared,
1471 * @return LY_ENOT if the values were equal and no change occured,
1472 * @return LY_ERR value on other errors.
1473 */
1474LY_ERR lyd_change_term_bin(struct lyd_node *term, const void *value, size_t value_len);
1475
1476/**
1477 * @brief Change the value of a term (leaf or leaf-list) node to a canonical string value.
1478 *
1479 * Node changed this way is always considered explicitly set, meaning its default flag
1480 * is always cleared.
1481 *
1482 * @param[in] term Term node to change.
1483 * @param[in] val_str New value to set in canonical (or JSON if no defined) format. If the value is not
1484 * canonical, it may lead to unexpected behavior.
1485 * @return LY_SUCCESS if value was changed,
1486 * @return LY_EEXIST if value was the same and only the default flag was cleared,
1487 * @return LY_ENOT if the values were equal and no change occured,
1488 * @return LY_ERR value on other errors.
1489 */
1490LY_ERR lyd_change_term_canon(struct lyd_node *term, const char *val_str);
1491
1492/**
Michal Vasko41586352020-07-13 13:54:25 +02001493 * @brief Change the value of a metadata instance.
1494 *
Michal Vasko41586352020-07-13 13:54:25 +02001495 * @param[in] meta Metadata to change.
1496 * @param[in] val_str New value to set, any prefixes are expected in JSON format.
1497 * @return LY_SUCCESS if value was changed,
1498 * @return LY_ENOT if the values were equal and no change occured,
1499 * @return LY_ERR value on other errors.
1500 */
1501LY_ERR lyd_change_meta(struct lyd_meta *meta, const char *val_str);
1502
1503/**
Michal Vaskob104f112020-07-17 09:54:54 +02001504 * @brief Insert a child into a parent.
Michal Vaskof03ed032020-03-04 13:31:44 +01001505 *
1506 * - if the node is part of some other tree, it is automatically unlinked.
1507 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
1508 *
1509 * @param[in] parent Parent node to insert into.
1510 * @param[in] node Node to insert.
1511 * @return LY_SUCCESS on success.
1512 * @return LY_ERR error on error.
1513 */
Michal Vaskob104f112020-07-17 09:54:54 +02001514LY_ERR lyd_insert_child(struct lyd_node *parent, struct lyd_node *node);
Michal Vaskof03ed032020-03-04 13:31:44 +01001515
1516/**
Michal Vaskob104f112020-07-17 09:54:54 +02001517 * @brief Insert a node into siblings.
Michal Vaskob1b5c262020-03-05 14:29:47 +01001518 *
1519 * - if the node is part of some other tree, it is automatically unlinked.
1520 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
1521 *
Michal Vaskob104f112020-07-17 09:54:54 +02001522 * @param[in] sibling Siblings to insert into, can even be NULL.
Michal Vaskob1b5c262020-03-05 14:29:47 +01001523 * @param[in] node Node to insert.
Michal Vaskob104f112020-07-17 09:54:54 +02001524 * @param[out] first Optionally return the first sibling after insertion. Can be the address of @p sibling.
Michal Vaskob1b5c262020-03-05 14:29:47 +01001525 * @return LY_SUCCESS on success.
1526 * @return LY_ERR error on error.
1527 */
Michal Vaskob104f112020-07-17 09:54:54 +02001528LY_ERR lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first);
Michal Vaskob1b5c262020-03-05 14:29:47 +01001529
1530/**
Michal Vaskob104f112020-07-17 09:54:54 +02001531 * @brief Insert a node before another node, can be used only for user-ordered nodes.
Michal Vasko4bc2ce32020-12-08 10:06:16 +01001532 * If inserting several siblings, each of them must be inserted individually.
Michal Vaskof03ed032020-03-04 13:31:44 +01001533 *
1534 * - if the node is part of some other tree, it is automatically unlinked.
Michal Vaskof03ed032020-03-04 13:31:44 +01001535 *
1536 * @param[in] sibling Sibling node to insert before.
1537 * @param[in] node Node to insert.
1538 * @return LY_SUCCESS on success.
1539 * @return LY_ERR error on error.
1540 */
1541LY_ERR lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node);
1542
1543/**
Michal Vaskob104f112020-07-17 09:54:54 +02001544 * @brief Insert a node after another node, can be used only for user-ordered nodes.
Michal Vasko4bc2ce32020-12-08 10:06:16 +01001545 * If inserting several siblings, each of them must be inserted individually.
Michal Vaskof03ed032020-03-04 13:31:44 +01001546 *
1547 * - if the node is part of some other tree, it is automatically unlinked.
Michal Vaskof03ed032020-03-04 13:31:44 +01001548 *
1549 * @param[in] sibling Sibling node to insert after.
1550 * @param[in] node Node to insert.
1551 * @return LY_SUCCESS on success.
1552 * @return LY_ERR error on error.
1553 */
1554LY_ERR lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node);
1555
1556/**
1557 * @brief Unlink the specified data subtree.
1558 *
1559 * @param[in] node Data tree node to be unlinked (together with all the children).
1560 */
1561void lyd_unlink_tree(struct lyd_node *node);
1562
1563/**
Radek Krejcib0849a22019-07-25 12:31:04 +02001564 * @brief Free all the nodes (even parents of the node) in the data tree.
Radek Krejcie7b95092019-05-15 11:03:07 +02001565 *
1566 * @param[in] node Any of the nodes inside the tree.
1567 */
1568void lyd_free_all(struct lyd_node *node);
1569
1570/**
Michal Vasko3a41dff2020-07-15 14:30:28 +02001571 * @brief Free all the sibling nodes (preceding as well as succeeding).
Radek Krejcib0849a22019-07-25 12:31:04 +02001572 *
1573 * @param[in] node Any of the sibling nodes to free.
1574 */
Michal Vaskof03ed032020-03-04 13:31:44 +01001575void lyd_free_siblings(struct lyd_node *node);
Radek Krejcib0849a22019-07-25 12:31:04 +02001576
1577/**
Radek Krejcie7b95092019-05-15 11:03:07 +02001578 * @brief Free (and unlink) the specified data (sub)tree.
1579 *
Radek Krejcie7b95092019-05-15 11:03:07 +02001580 * @param[in] node Root of the (sub)tree to be freed.
1581 */
1582void lyd_free_tree(struct lyd_node *node);
1583
1584/**
Michal Vasko3a41dff2020-07-15 14:30:28 +02001585 * @brief Free a single metadata instance.
Radek Krejcie7b95092019-05-15 11:03:07 +02001586 *
Michal Vasko3a41dff2020-07-15 14:30:28 +02001587 * @param[in] meta Metadata to free.
Radek Krejcie7b95092019-05-15 11:03:07 +02001588 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001589void lyd_free_meta_single(struct lyd_meta *meta);
Michal Vasko52927e22020-03-16 17:26:14 +01001590
1591/**
Michal Vasko3a41dff2020-07-15 14:30:28 +02001592 * @brief Free the metadata instance with any following instances.
1593 *
1594 * @param[in] meta Metadata to free.
1595 */
1596void lyd_free_meta_siblings(struct lyd_meta *meta);
1597
1598/**
1599 * @brief Free a single attribute.
Michal Vasko52927e22020-03-16 17:26:14 +01001600 *
1601 * @param[in] ctx Context where the attributes were created.
Michal Vasko3a41dff2020-07-15 14:30:28 +02001602 * @param[in] attr Attribute to free.
Michal Vasko52927e22020-03-16 17:26:14 +01001603 */
Radek Krejci011e4aa2020-09-04 15:22:31 +02001604void lyd_free_attr_single(const struct ly_ctx *ctx, struct lyd_attr *attr);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001605
1606/**
1607 * @brief Free the attribute with any following attributes.
1608 *
1609 * @param[in] ctx Context where the attributes were created.
1610 * @param[in] attr First attribute to free.
1611 */
Radek Krejci011e4aa2020-09-04 15:22:31 +02001612void lyd_free_attr_siblings(const struct ly_ctx *ctx, struct lyd_attr *attr);
Radek Krejcie7b95092019-05-15 11:03:07 +02001613
Radek Krejci084289f2019-07-09 17:35:30 +02001614/**
1615 * @brief Check type restrictions applicable to the particular leaf/leaf-list with the given string @p value.
1616 *
1617 * The given node is not modified in any way - it is just checked if the @p value can be set to the node.
1618 *
Radek Krejci084289f2019-07-09 17:35:30 +02001619 * @param[in] ctx libyang context for logging (function does not log errors when @p ctx is NULL)
Michal Vaskoaebbce02021-04-06 09:23:37 +02001620 * @param[in] schema Schema node of the @p value.
Michal Vaskof937cfe2020-08-03 16:07:12 +02001621 * @param[in] value String value to be checked, it is expected to be in JSON format.
Radek Krejci084289f2019-07-09 17:35:30 +02001622 * @param[in] value_len Length of the given @p value (mandatory).
Michal Vaskoaebbce02021-04-06 09:23:37 +02001623 * @param[in] ctx_node Optional data tree context node for the value (leafref target, instance-identifier).
1624 * If not set and is required for the validation to complete, ::LY_EINCOMPLETE is be returned.
1625 * @param[out] realtype Optional real type of @p value.
1626 * @param[out] canonical Optional canonical value of @p value (in the dictionary).
Radek Krejci084289f2019-07-09 17:35:30 +02001627 * @return LY_SUCCESS on success
Michal Vaskoaebbce02021-04-06 09:23:37 +02001628 * @return LY_EINCOMPLETE in case the @p ctx_node is not provided and it was needed to finish the validation
1629 * (e.g. due to require-instance).
Radek Krejci084289f2019-07-09 17:35:30 +02001630 * @return LY_ERR value if an error occurred.
1631 */
Michal Vaskoaebbce02021-04-06 09:23:37 +02001632LY_ERR lyd_value_validate(const struct ly_ctx *ctx, const struct lysc_node *schema, const char *value, size_t value_len,
1633 const struct lyd_node *ctx_node, const struct lysc_type **realtype, const char **canonical);
Radek Krejci084289f2019-07-09 17:35:30 +02001634
1635/**
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001636 * @brief Compare the node's value with the given string value. The string value is first validated according to
1637 * the (current) node's type.
Radek Krejci084289f2019-07-09 17:35:30 +02001638 *
1639 * @param[in] node Data node to compare.
1640 * @param[in] value String value to be compared. It does not need to be in a canonical form - as part of the process,
Michal Vaskof937cfe2020-08-03 16:07:12 +02001641 * it is validated and canonized if possible. But it is expected to be in JSON format.
Radek Krejci084289f2019-07-09 17:35:30 +02001642 * @param[in] value_len Length of the given @p value (mandatory).
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001643 * @return LY_SUCCESS on success,
1644 * @return LY_ENOT if the values do not match,
Radek Krejci084289f2019-07-09 17:35:30 +02001645 * @return LY_ERR value if an error occurred.
1646 */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001647LY_ERR lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len);
Radek Krejci084289f2019-07-09 17:35:30 +02001648
Radek Krejci576b23f2019-07-12 14:06:32 +02001649/**
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001650 * @ingroup datatree
Radek Krejci8678fa42020-08-18 16:07:28 +02001651 * @defgroup datacompareoptions Data compare options
1652 * @{
1653 * Various options to change the ::lyd_compare_single() and ::lyd_compare_siblings() behavior.
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001654 */
1655#define LYD_COMPARE_FULL_RECURSION 0x01 /* lists and containers are the same only in case all they children
1656 (subtree, so direct as well as indirect children) are the same. By default,
1657 containers are the same in case of the same schema node and lists are the same
1658 in case of equal keys (keyless lists do the full recursion comparison all the time). */
1659#define LYD_COMPARE_DEFAULTS 0x02 /* By default, implicit and explicit default nodes are considered to be equal. This flag
1660 changes this behavior and implicit (automatically created default node) and explicit
1661 (explicitly created node with the default value) default nodes are considered different. */
Michal Vasko60ea6352020-06-29 13:39:39 +02001662/** @} datacompareoptions */
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001663
1664/**
1665 * @brief Compare 2 data nodes if they are equivalent.
1666 *
1667 * @param[in] node1 The first node to compare.
1668 * @param[in] node2 The second node to compare.
Radek Krejcic5ad9652019-09-11 11:31:51 +02001669 * @param[in] options Various @ref datacompareoptions.
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001670 * @return LY_SUCCESS if the nodes are equivalent.
1671 * @return LY_ENOT if the nodes are not equivalent.
1672 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02001673LY_ERR lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options);
Michal Vasko8f359bf2020-07-28 10:41:15 +02001674
1675/**
1676 * @brief Compare 2 lists of siblings if they are equivalent.
1677 *
1678 * @param[in] node1 The first sibling list to compare.
1679 * @param[in] node2 The second sibling list to compare.
1680 * @param[in] options Various @ref datacompareoptions.
1681 * @return LY_SUCCESS if all the siblings are equivalent.
1682 * @return LY_ENOT if the siblings are not equivalent.
1683 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02001684LY_ERR lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options);
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001685
1686/**
Michal Vasko21725742020-06-29 11:49:25 +02001687 * @brief Compare 2 metadata.
1688 *
1689 * @param[in] meta1 First metadata.
1690 * @param[in] meta2 Second metadata.
1691 * @return LY_SUCCESS if the metadata are equivalent.
1692 * @return LY_ENOT if not.
1693 */
1694LY_ERR lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2);
1695
1696/**
Radek Krejci22ebdba2019-07-25 13:59:43 +02001697 * @ingroup datatree
Radek Krejci8678fa42020-08-18 16:07:28 +02001698 * @defgroup dupoptions Data duplication options
Radek Krejci22ebdba2019-07-25 13:59:43 +02001699 *
Michal Vaskoe78faec2021-04-08 17:24:43 +02001700 * Various options to change ::lyd_dup_single() and ::lyd_dup_siblings() behavior.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001701 *
1702 * Default behavior:
1703 * - only the specified node is duplicated without siblings, parents, or children.
Michal Vasko3a41dff2020-07-15 14:30:28 +02001704 * - all the metadata of the duplicated nodes are also duplicated.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001705 * @{
1706 */
1707
1708#define LYD_DUP_RECURSIVE 0x01 /**< Duplicate not just the node but also all the children. Note that
1709 list's keys are always duplicated. */
Michal Vaskoa29a5762020-06-23 13:28:49 +02001710#define LYD_DUP_NO_META 0x02 /**< Do not duplicate metadata of any node. */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001711#define LYD_DUP_WITH_PARENTS 0x04 /**< If a nested node is being duplicated, duplicate also all the parents.
1712 Keys are also duplicated for lists. Return value does not change! */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001713#define LYD_DUP_WITH_FLAGS 0x08 /**< Also copy any data node flags. That will cause the duplicated data to preserve
Michal Vaskof6df0a02020-06-16 13:08:34 +02001714 its validation/default node state. */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001715
1716/** @} dupoptions */
1717
1718/**
1719 * @brief Create a copy of the specified data tree \p node. Schema references are kept the same.
1720 *
Radek Krejci22ebdba2019-07-25 13:59:43 +02001721 * @param[in] node Data tree node to be duplicated.
1722 * @param[in] parent Optional parent node where to connect the duplicated node(s).
Michal Vasko3a41dff2020-07-15 14:30:28 +02001723 * If set in combination with LYD_DUP_WITH_PARENTS, the parents chain is duplicated until it comes to and connects with
1724 * the @p parent.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001725 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vasko3a41dff2020-07-15 14:30:28 +02001726 * @param[out] dup Optional created copy of the node. Note that in case the parents chain is duplicated for the duplicated
1727 * node(s) (when LYD_DUP_WITH_PARENTS used), the first duplicated node is still returned.
1728 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001729 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02001730LY_ERR lyd_dup_single(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, struct lyd_node **dup);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001731
1732/**
1733 * @brief Create a copy of the specified data tree \p node with any following siblings. Schema references are kept the same.
1734 *
1735 * @param[in] node Data tree node to be duplicated.
1736 * @param[in] parent Optional parent node where to connect the duplicated node(s).
1737 * If set in combination with LYD_DUP_WITH_PARENTS, the parents chain is duplicated until it comes to and connects with
1738 * the @p parent.
1739 * @param[in] options Bitmask of options flags, see @ref dupoptions.
1740 * @param[out] dup Optional created copy of the node. Note that in case the parents chain is duplicated for the duplicated
1741 * node(s) (when LYD_DUP_WITH_PARENTS used), the first duplicated node is still returned.
1742 * @return LY_ERR value.
1743 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02001744LY_ERR lyd_dup_siblings(const struct lyd_node *node, struct lyd_node_inner *parent, uint32_t options, struct lyd_node **dup);
Radek Krejci22ebdba2019-07-25 13:59:43 +02001745
1746/**
Michal Vasko25a32822020-07-09 15:48:22 +02001747 * @brief Create a copy of the metadata.
1748 *
1749 * @param[in] meta Metadata to copy.
Michal Vasko3a41dff2020-07-15 14:30:28 +02001750 * @param[in] parent Node where to append the new metadata.
1751 * @param[out] dup Optional created metadata copy.
1752 * @return LY_ERR value.
Michal Vasko25a32822020-07-09 15:48:22 +02001753 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001754LY_ERR lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *parent, struct lyd_meta **dup);
Michal Vasko25a32822020-07-09 15:48:22 +02001755
1756/**
Michal Vasko4490d312020-06-16 13:08:55 +02001757 * @ingroup datatree
Radek Krejci8678fa42020-08-18 16:07:28 +02001758 * @defgroup mergeoptions Data merge options.
Radek Krejci576b23f2019-07-12 14:06:32 +02001759 *
Radek Krejci8678fa42020-08-18 16:07:28 +02001760 * Various options to change ::lyd_merge_tree() and ::lyd_merge_siblings() behavior.
Michal Vasko4490d312020-06-16 13:08:55 +02001761 *
1762 * Default behavior:
1763 * - source data tree is not modified in any way,
Michal Vasko745d6f62021-04-12 16:55:23 +02001764 * - any default nodes in the source are ignored if there are explicit nodes in the target,
1765 * - any metadata are ignored - those present in the target are kept, those in the source are not merged.
Michal Vasko4490d312020-06-16 13:08:55 +02001766 * @{
1767 */
1768
1769#define LYD_MERGE_DESTRUCT 0x01 /**< Spend source data tree in the function, it cannot be used afterwards! */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001770#define LYD_MERGE_DEFAULTS 0x02 /**< Default nodes in the source tree replace even explicit nodes in the target. */
Michal Vasko4490d312020-06-16 13:08:55 +02001771
1772/** @} mergeoptions */
1773
1774/**
Michal Vasko3a41dff2020-07-15 14:30:28 +02001775 * @brief Merge the source data subtree into the target data tree. Merge may not be complete until validation
Michal Vasko4490d312020-06-16 13:08:55 +02001776 * is called on the resulting data tree (data from more cases may be present, default and non-default values).
1777 *
Michal Vasko2f510942020-11-13 10:26:25 +01001778 * Example input:
1779 *
1780 * source (A1) - A2 - A3 target (B1) - B2 - B3
1781 * /\ /\ /\ /\ /\ /\
1782 * .... .... .... .... .... ....
1783 *
1784 * result target (A1) - B1 - B2 - B3
1785 * /\ /\ /\ /\
1786 * .... .... .... ....
1787 *
Michal Vasko4490d312020-06-16 13:08:55 +02001788 * @param[in,out] target Target data tree to merge into, must be a top-level tree.
1789 * @param[in] source Source data tree to merge, must be a top-level tree.
1790 * @param[in] options Bitmask of option flags, see @ref mergeoptions.
1791 * @return LY_SUCCESS on success,
1792 * @return LY_ERR value on error.
1793 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02001794LY_ERR lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001795
1796/**
1797 * @brief Merge the source data tree with any following siblings into the target data tree. Merge may not be
1798 * complete until validation called on the resulting data tree (data from more cases may be present, default
1799 * and non-default values).
1800 *
Michal Vasko2f510942020-11-13 10:26:25 +01001801 * Example input:
1802 *
1803 * source (A1) - A2 - A3 target (B1) - B2 - B3
1804 * /\ /\ /\ /\ /\ /\
1805 * .... .... .... .... .... ....
1806 *
1807 * result target (A1) - A2 - A3 - B1 - B2 - B3
1808 * /\ /\ /\ /\ /\ /\
1809 * .... .... .... .... .... ....
1810 *
Michal Vasko3a41dff2020-07-15 14:30:28 +02001811 * @param[in,out] target Target data tree to merge into, must be a top-level tree.
1812 * @param[in] source Source data tree to merge, must be a top-level tree.
1813 * @param[in] options Bitmask of option flags, see @ref mergeoptions.
1814 * @return LY_SUCCESS on success,
1815 * @return LY_ERR value on error.
1816 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02001817LY_ERR lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options);
Michal Vasko4490d312020-06-16 13:08:55 +02001818
1819/**
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001820 * @ingroup datatree
Radek Krejci8678fa42020-08-18 16:07:28 +02001821 * @defgroup diffoptions Data diff options.
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001822 *
Radek Krejci8678fa42020-08-18 16:07:28 +02001823 * Various options to change ::lyd_diff_tree() and ::lyd_diff_siblings() behavior.
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001824 *
1825 * Default behavior:
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001826 * - any default nodes are treated as non-existent and ignored.
1827 * @{
1828 */
1829
Michal Vasko3a41dff2020-07-15 14:30:28 +02001830#define LYD_DIFF_DEFAULTS 0x01 /**< Default nodes in the trees are not ignored but treated similarly to explicit
1831 nodes. Also, leaves and leaf-lists are added into diff even in case only their
1832 default flag (state) was changed. */
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001833
1834/** @} diffoptions */
1835
1836/**
1837 * @brief Learn the differences between 2 data trees.
1838 *
1839 * The resulting diff is represented as a data tree with specific metadata from the internal 'yang'
1840 * module. Most importantly, every node has an effective 'operation' metadata. If there is none
1841 * defined on the node, it inherits the operation from the nearest parent. Top-level nodes must
1842 * always have the 'operation' metadata defined. Additional metadata ('orig-default', 'value',
1843 * 'orig-value', 'key', 'orig-key') are used for storing more information about the value in the first
1844 * or the second tree.
1845 *
1846 * The diff tree is completely independent on the @p first and @p second trees, meaning all
1847 * the information about the change is stored in the diff and the trees are not needed.
1848 *
Michal Vaskoff857812021-03-05 17:03:52 +01001849 * __!! Caution !!__
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001850 * The diff tree should never be validated because it may easily not be valid! For example,
1851 * when data from one case branch are deleted and data from another branch created - data from both
1852 * branches are then stored in the diff tree simultaneously.
1853 *
1854 * @param[in] first First data tree.
1855 * @param[in] second Second data tree.
1856 * @param[in] options Bitmask of options flags, see @ref diffoptions.
1857 * @param[out] diff Generated diff.
1858 * @return LY_SUCCESS on success,
1859 * @return LY_ERR on error.
1860 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02001861LY_ERR lyd_diff_tree(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, struct lyd_node **diff);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001862
1863/**
1864 * @brief Learn the differences between 2 data trees including all the following siblings.
1865 *
Michal Vaskoff857812021-03-05 17:03:52 +01001866 * Details are mentioned in ::lyd_diff_tree().
1867 *
Michal Vasko3a41dff2020-07-15 14:30:28 +02001868 * @param[in] first First data tree.
1869 * @param[in] second Second data tree.
1870 * @param[in] options Bitmask of options flags, see @ref diffoptions.
1871 * @param[out] diff Generated diff.
1872 * @return LY_SUCCESS on success,
1873 * @return LY_ERR on error.
1874 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02001875LY_ERR lyd_diff_siblings(const struct lyd_node *first, const struct lyd_node *second, uint16_t options, struct lyd_node **diff);
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001876
1877/**
1878 * @brief Callback for diff nodes.
1879 *
1880 * @param[in] diff_node Diff node.
1881 * @param[in] data_node Matching node in data.
1882 * @param[in] cb_data Arbitrary callback data.
1883 * @return LY_ERR value.
1884 */
1885typedef LY_ERR (*lyd_diff_cb)(const struct lyd_node *diff_node, struct lyd_node *data_node, void *cb_data);
1886
1887/**
Michal Vasko3a41dff2020-07-15 14:30:28 +02001888 * @brief Apply the whole diff on a data tree but restrict the operation to one module.
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001889 *
Michal Vaskoa98dcba2021-03-05 17:04:14 +01001890 * __!! Caution !!__
1891 * If applying a diff that was created __without__ the ::LYD_DIFF_DEFAULTS flag, there may be some duplicate values
1892 * created. Unless the resulting tree is validated (and default values thus consolidated), using it further
1893 * (such as applying another diff) may cause unexpected results or errors.
1894 *
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001895 * @param[in,out] data Data to apply the diff on.
1896 * @param[in] diff Diff to apply.
1897 * @param[in] mod Module, whose diff/data only to consider, NULL for all modules.
1898 * @param[in] diff_cb Optional diff callback that will be called for every changed node.
1899 * @param[in] cb_data Arbitrary callback data.
1900 * @return LY_SUCCESS on success,
1901 * @return LY_ERR on error.
1902 */
1903LY_ERR lyd_diff_apply_module(struct lyd_node **data, const struct lyd_node *diff, const struct lys_module *mod,
Radek Krejci0f969882020-08-21 16:56:47 +02001904 lyd_diff_cb diff_cb, void *cb_data);
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001905
1906/**
Michal Vasko3a41dff2020-07-15 14:30:28 +02001907 * @brief Apply the whole diff tree on a data tree.
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001908 *
Michal Vaskoff857812021-03-05 17:03:52 +01001909 * Details are mentioned in ::lyd_diff_apply_module().
1910 *
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001911 * @param[in,out] data Data to apply the diff on.
1912 * @param[in] diff Diff to apply.
1913 * @return LY_SUCCESS on success,
1914 * @return LY_ERR on error.
1915 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001916LY_ERR lyd_diff_apply_all(struct lyd_node **data, const struct lyd_node *diff);
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001917
1918/**
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001919 * @ingroup datatree
1920 * @defgroup diffmergeoptions Data diff merge options.
1921 *
1922 * Various options to change ::lyd_diff_merge_module(), ::lyd_diff_merge_tree(), and ::lyd_diff_merge_all() behavior.
1923 *
1924 * Default behavior:
1925 * - any default nodes are expected to be a result of validation corrections and not explicitly modified.
1926 * @{
1927 */
1928
1929#define LYD_DIFF_MERGE_DEFAULTS 0x01 /**< Default nodes in the diffs are treated as possibly explicitly modified. */
1930
Michal Vaskoff857812021-03-05 17:03:52 +01001931/** @} diffmergeoptions */
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001932
1933/**
Michal Vaskoe6323f62020-07-09 15:49:02 +02001934 * @brief Merge 2 diffs into each other but restrict the operation to one module.
1935 *
1936 * The diffs must be possible to be merged, which is guaranteed only if the source diff was
1937 * created on data that had the target diff applied on them. In other words, this sequence is legal
1938 *
Michal Vaskoff857812021-03-05 17:03:52 +01001939 * 1) get diff1 from data1 and data2 -> get data11 from apply diff1 on data1 -> get diff2 from data11 and data3 ->
1940 * -> get data 33 from apply diff2 on data1
Michal Vaskoe6323f62020-07-09 15:49:02 +02001941 *
1942 * and reusing these diffs
1943 *
Michal Vaskoff857812021-03-05 17:03:52 +01001944 * 2) get diff11 from merge diff1 and diff2 -> get data33 from apply diff11 on data1
Michal Vaskoe6323f62020-07-09 15:49:02 +02001945 *
Michal Vaskofb737aa2020-08-06 13:53:53 +02001946 * @param[in,out] diff Target diff to merge into.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001947 * @param[in] src_diff Source diff.
1948 * @param[in] mod Module, whose diff only to consider, NULL for all modules.
Michal Vaskoe2af8412020-12-03 14:11:38 +01001949 * @param[in] diff_cb Optional diff callback that will be called for every merged node. Param @p diff_node is the source
1950 * diff node while @p data_node is the updated target diff node. In case a whole subtree is added, the callback is
1951 * called on the root with @p diff_node being NULL.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001952 * @param[in] cb_data Arbitrary callback data.
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001953 * @param[in] options Bitmask of options flags, see @ref diffmergeoptions.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001954 * @return LY_SUCCESS on success,
1955 * @return LY_ERR on error.
1956 */
Michal Vaskofb737aa2020-08-06 13:53:53 +02001957LY_ERR lyd_diff_merge_module(struct lyd_node **diff, const struct lyd_node *src_diff, const struct lys_module *mod,
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001958 lyd_diff_cb diff_cb, void *cb_data, uint16_t options);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001959
1960/**
Michal Vasko04f85912020-08-07 12:14:58 +02001961 * @brief Merge 2 diff trees into each other.
1962 *
Michal Vaskoff857812021-03-05 17:03:52 +01001963 * Details are mentioned in ::lyd_diff_merge_module().
1964 *
Michal Vasko04f85912020-08-07 12:14:58 +02001965 * @param[in,out] diff_first Target diff first sibling to merge into.
1966 * @param[in] diff_parent Target diff parent to merge into.
1967 * @param[in] src_sibling Source diff sibling to merge.
Michal Vaskoe2af8412020-12-03 14:11:38 +01001968 * @param[in] diff_cb Optional diff callback that will be called for every merged node. Param @p diff_node is the source
1969 * diff node while @p data_node is the updated target diff node. In case a whole subtree is added, the callback is
1970 * called on the root with @p diff_node being NULL.
Michal Vasko04f85912020-08-07 12:14:58 +02001971 * @param[in] cb_data Arbitrary callback data.
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001972 * @param[in] options Bitmask of options flags, see @ref diffmergeoptions.
Michal Vasko04f85912020-08-07 12:14:58 +02001973 * @return LY_SUCCESS on success,
1974 * @return LY_ERR on error.
1975 */
1976LY_ERR lyd_diff_merge_tree(struct lyd_node **diff_first, struct lyd_node *diff_parent, const struct lyd_node *src_sibling,
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001977 lyd_diff_cb diff_cb, void *cb_data, uint16_t options);
Michal Vasko04f85912020-08-07 12:14:58 +02001978
1979/**
Michal Vaskoe6323f62020-07-09 15:49:02 +02001980 * @brief Merge 2 diffs into each other.
1981 *
Michal Vaskoff857812021-03-05 17:03:52 +01001982 * Details are mentioned in ::lyd_diff_merge_module().
1983 *
Michal Vaskoe6323f62020-07-09 15:49:02 +02001984 * @param[in,out] diff Target diff to merge into.
Michal Vaskofb737aa2020-08-06 13:53:53 +02001985 * @param[in] src_diff Source diff.
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001986 * @param[in] options Bitmask of options flags, see @ref diffmergeoptions.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001987 * @return LY_SUCCESS on success,
1988 * @return LY_ERR on error.
1989 */
Michal Vaskoc0e58e82020-11-11 19:04:33 +01001990LY_ERR lyd_diff_merge_all(struct lyd_node **diff, const struct lyd_node *src_diff, uint16_t options);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001991
1992/**
Michal Vasko4231fb62020-07-13 13:54:47 +02001993 * @brief Reverse a diff and make the opposite changes. Meaning change create to delete, delete to create,
1994 * or move from place A to B to move from B to A and so on.
1995 *
1996 * @param[in] src_diff Diff to reverse.
1997 * @param[out] diff Reversed diff.
1998 * @return LY_SUCCESS on success.
1999 * @return LY_ERR on error.
2000 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02002001LY_ERR lyd_diff_reverse_all(const struct lyd_node *src_diff, struct lyd_node **diff);
Michal Vasko4231fb62020-07-13 13:54:47 +02002002
2003/**
Radek Krejcifba9c622020-10-30 08:28:54 +01002004 * @brief Find the target in data of a compiled instance-identifier path (the target member in ::lyd_value).
Michal Vasko4490d312020-06-16 13:08:55 +02002005 *
2006 * @param[in] path Compiled path structure.
Michal Vaskof03ed032020-03-04 13:31:44 +01002007 * @param[in] tree Data tree to be searched.
Michal Vasko4490d312020-06-16 13:08:55 +02002008 * @return Found target node,
2009 * @return NULL if not found.
Radek Krejci576b23f2019-07-12 14:06:32 +02002010 */
Michal Vasko004d3152020-06-11 19:59:22 +02002011const struct lyd_node_term *lyd_target(const struct ly_path *path, const struct lyd_node *tree);
Radek Krejci084289f2019-07-09 17:35:30 +02002012
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002013/**
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002014 * @brief Types of the different data paths.
2015 */
2016typedef enum {
Radek Krejci635d2b82021-01-04 11:26:51 +01002017 LYD_PATH_STD, /**< Generic data path used for logging, node searching (::lyd_find_xpath(), ::lys_find_path()) as well as
Radek Krejci95ccd1b2021-03-12 14:57:22 +01002018 creating new nodes (::lyd_new_path(), ::lyd_new_path2(), ::lyd_new_ext_path()). */
Radek Krejci635d2b82021-01-04 11:26:51 +01002019 LYD_PATH_STD_NO_LAST_PRED /**< Similar to ::LYD_PATH_STD except there is never a predicate on the last node. While it
2020 can be used to search for nodes, do not use it to create new data nodes (lists). */
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002021} LYD_PATH_TYPE;
2022
2023/**
2024 * @brief Generate path of the given node in the requested format.
2025 *
Radek Krejci635d2b82021-01-04 11:26:51 +01002026 * @param[in] node Data path of this node will be generated.
Michal Vasko5ec7cda2019-09-11 13:43:08 +02002027 * @param[in] pathtype Format of the path to generate.
2028 * @param[in,out] buffer Prepared buffer of the @p buflen length to store the generated path.
2029 * If NULL, memory for the complete path is allocated.
2030 * @param[in] buflen Size of the provided @p buffer.
2031 * @return NULL in case of memory allocation error, path of the node otherwise.
2032 * In case the @p buffer is NULL, the returned string is dynamically allocated and caller is responsible to free it.
2033 */
2034char *lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen);
2035
Michal Vaskoe444f752020-02-10 12:20:06 +01002036/**
Michal Vasko25a32822020-07-09 15:48:22 +02002037 * @brief Find a specific metadata.
2038 *
2039 * @param[in] first First metadata to consider.
2040 * @param[in] module Module of the metadata definition, may be NULL if @p name includes a prefix.
2041 * @param[in] name Name of the metadata to find, may not include a prefix (module name) if @p module is set.
2042 * @return Found metadata,
2043 * @return NULL if not found.
2044 */
2045struct lyd_meta *lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name);
2046
2047/**
Michal Vaskoda859032020-07-14 12:20:14 +02002048 * @brief Search in the given siblings (NOT recursively) for the first target instance with the same value.
Michal Vaskoe444f752020-02-10 12:20:06 +01002049 * Uses hashes - should be used whenever possible for best performance.
2050 *
2051 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
2052 * @param[in] target Target node to find.
Michal Vasko9b368d32020-02-14 13:53:31 +01002053 * @param[out] match Can be NULL, otherwise the found data node.
Michal Vaskoe444f752020-02-10 12:20:06 +01002054 * @return LY_SUCCESS on success, @p match set.
2055 * @return LY_ENOTFOUND if not found, @p match set to NULL.
2056 * @return LY_ERR value if another error occurred.
2057 */
2058LY_ERR lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match);
2059
2060/**
Michal Vaskoe444f752020-02-10 12:20:06 +01002061 * @brief Search in the given siblings for the first schema instance.
2062 * Uses hashes - should be used whenever possible for best performance.
2063 *
2064 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
2065 * @param[in] schema Schema node of the data node to find.
Michal Vaskob104f112020-07-17 09:54:54 +02002066 * @param[in] key_or_value If it is NULL, the first schema node data instance is found. For nodes with many
2067 * instances, it can be set based on the type of @p schema:
Michal Vaskoe444f752020-02-10 12:20:06 +01002068 * LYS_LEAFLIST:
2069 * Searched instance value.
2070 * LYS_LIST:
Michal Vasko90932a92020-02-12 14:33:03 +01002071 * Searched instance key values in the form of "[key1='val1'][key2='val2']...".
2072 * The keys do not have to be ordered but all of them must be set.
2073 *
2074 * Note that any explicit values (leaf-list or list key values) will be canonized first
2075 * before comparison. But values that do not have a canonical value are expected to be in the
2076 * JSON format!
Michal Vaskof03ed032020-03-04 13:31:44 +01002077 * @param[in] val_len Optional length of @p key_or_value in case it is not 0-terminated.
Michal Vasko9b368d32020-02-14 13:53:31 +01002078 * @param[out] match Can be NULL, otherwise the found data node.
Michal Vaskoe444f752020-02-10 12:20:06 +01002079 * @return LY_SUCCESS on success, @p match set.
2080 * @return LY_ENOTFOUND if not found, @p match set to NULL.
2081 * @return LY_EINVAL if @p schema is a key-less list.
2082 * @return LY_ERR value if another error occurred.
2083 */
2084LY_ERR lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
Radek Krejci0f969882020-08-21 16:56:47 +02002085 size_t val_len, struct lyd_node **match);
Michal Vaskoe444f752020-02-10 12:20:06 +01002086
Michal Vaskoccc02342020-05-21 10:09:21 +02002087/**
Michal Vaskoe78faec2021-04-08 17:24:43 +02002088 * @brief Search the given siblings for all the exact same instances of a specific node instance. Accepts only nodes
2089 * that are allowed to have several exact same instances. Uses hashes to whatever extent possible.
2090 *
2091 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
2092 * @param[in] target Target node instance to find.
2093 * @param[out] set Set with all the found instances. The first item is always the first instance.
2094 * @return LY_SUCCESS on success, @p set returned.
2095 * @return LY_ENOTFOUND if not found, empty @p set returned.
2096 * @return LY_ERR value if another error occurred.
2097 */
2098LY_ERR lyd_find_sibling_dup_inst_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set);
2099
2100/**
Michal Vasko1d4af6c2021-02-22 13:31:26 +01002101 * @brief Search the given siblings for an opaque node with a specific name.
2102 *
2103 * @param[in] first First sibling to consider.
2104 * @param[in] name Opaque node name to find.
2105 * @param[out] match Can be NULL, otherwise the found data node.
2106 * @return LY_SUCCESS on success, @p match set.
2107 * @return LY_ENOTFOUND if not found, @p match set to NULL.
2108 * @return LY_ERR value is an error occurred.
2109 */
2110LY_ERR lyd_find_sibling_opaq_next(const struct lyd_node *first, const char *name, struct lyd_node **match);
2111
2112/**
Michal Vaskoccc02342020-05-21 10:09:21 +02002113 * @brief Search in the given data for instances of nodes matching the provided XPath.
2114 *
Michal Vasko19a30602020-05-25 10:40:19 +02002115 * If a list instance is being selected with all its key values specified (but not necessarily ordered)
2116 * in the form `list[key1='val1'][key2='val2'][key3='val3']` or a leaf-list instance in the form
2117 * `leaf-list[.='val']`, these instances are found using hashes with constant (*O(1)*) complexity
2118 * (unless they are defined in top-level). Other predicates can still follow the aforementioned ones.
2119 *
Michal Vaskoccc02342020-05-21 10:09:21 +02002120 * @param[in] ctx_node XPath context node.
Michal Vasko3e1f6552021-01-14 09:27:55 +01002121 * @param[in] xpath [XPath](@ref howtoXPath) to select.
Michal Vaskoccc02342020-05-21 10:09:21 +02002122 * @param[out] set Set of found data nodes. In case the result is a number, a string, or a boolean,
2123 * the returned set is empty.
2124 * @return LY_SUCCESS on success, @p set is returned.
2125 * @return LY_ERR value if an error occurred.
2126 */
2127LY_ERR lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set);
2128
Michal Vasko3e1f6552021-01-14 09:27:55 +01002129/**
2130 * @brief Search in given data for a node uniquely identifier by a path.
2131 *
Michal Vasko257bdcf2021-01-14 13:15:30 +01002132 * Always works in constant (*O(1)*) complexity. To be exact, it is *O(n)* where *n* is the depth
2133 * of the path used.
2134 *
Michal Vasko3e1f6552021-01-14 09:27:55 +01002135 * @param[in] ctx_node Path context node.
2136 * @param[in] path [Path](@ref howtoXPath) to find.
2137 * @param[in] output Whether to search in RPC/action output nodes or in input nodes.
2138 * @param[out] match Can be NULL, otherwise the found data node.
2139 * @return LY_SUCCESS on success, @p match is set to the found node.
2140 * @return LY_EINCOMPLETE if only a parent of the node was found, @p match is set to this parent node.
2141 * @return LY_ENOTFOUND if no nodes in the path were found.
2142 * @return LY_ERR on other errors.
2143 */
2144LY_ERR lyd_find_path(const struct lyd_node *ctx_node, const char *path, ly_bool output, struct lyd_node **match);
2145
Radek Krejcie7b95092019-05-15 11:03:07 +02002146#ifdef __cplusplus
2147}
2148#endif
2149
2150#endif /* LY_TREE_DATA_H_ */