blob: 1380bf08afe19daf821d39a0a03e7a1f0e5fde0b [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"
Radek Krejcie7b95092019-05-15 11:03:07 +020022
Radek Krejcica376bd2020-06-11 16:04:06 +020023#ifdef __cplusplus
24extern "C" {
25#endif
26
Radek Krejcie7b95092019-05-15 11:03:07 +020027struct ly_ctx;
Michal Vasko004d3152020-06-11 19:59:22 +020028struct ly_path;
Radek Krejci535ea9f2020-05-29 16:01:05 +020029struct ly_set;
30struct lyd_node;
31struct lyd_node_opaq;
32struct lys_module;
33struct lysc_node;
Radek Krejcie7b95092019-05-15 11:03:07 +020034
Radek Krejcie7b95092019-05-15 11:03:07 +020035/**
36 * @defgroup datatree Data Tree
Radek Krejci2ff0d572020-05-21 15:27:28 +020037 * @ingroup trees
Radek Krejcie7b95092019-05-15 11:03:07 +020038 * @{
39 *
40 * Data structures and functions to manipulate and access instance data tree.
41 */
42
Michal Vasko64246d82020-08-19 12:35:00 +020043/* *INDENT-OFF* */
44
Michal Vaskoa276cd92020-08-10 15:10:08 +020045/**
Radek Krejcie7b95092019-05-15 11:03:07 +020046 * @brief Macro to iterate via all elements in a data tree. This is the opening part
47 * to the #LYD_TREE_DFS_END - they always have to be used together.
48 *
49 * The function follows deep-first search algorithm:
50 * <pre>
51 * 1
52 * / \
Michal Vaskoc193ce92020-03-06 11:04:48 +010053 * 2 4
Radek Krejcie7b95092019-05-15 11:03:07 +020054 * / / \
Michal Vaskoc193ce92020-03-06 11:04:48 +010055 * 3 5 6
Radek Krejcie7b95092019-05-15 11:03:07 +020056 * </pre>
57 *
Radek Krejci0935f412019-08-20 16:15:18 +020058 * Use the same parameters for #LYD_TREE_DFS_BEGIN and #LYD_TREE_DFS_END. While
Michal Vasko56daf732020-08-10 10:57:18 +020059 * START can be any of the lyd_node* types, ELEM variable must be a pointer to
60 * the generic struct lyd_node.
Radek Krejcie7b95092019-05-15 11:03:07 +020061 *
Michal Vasko56daf732020-08-10 10:57:18 +020062 * To skip a particular subtree, instead of the continue statement, set LYD_TREE_DFS_continue
63 * variable to non-zero value.
Radek Krejcie7b95092019-05-15 11:03:07 +020064 *
65 * Use with opening curly bracket '{' after the macro.
66 *
67 * @param START Pointer to the starting element processed first.
Radek Krejcie7b95092019-05-15 11:03:07 +020068 * @param ELEM Iterator intended for use in the block.
69 */
Michal Vasko56daf732020-08-10 10:57:18 +020070#define LYD_TREE_DFS_BEGIN(START, ELEM) \
Radek Krejci857189e2020-09-01 13:26:36 +020071 { ly_bool LYD_TREE_DFS_continue = 0; struct lyd_node *LYD_TREE_DFS_next; \
Michal Vasko56daf732020-08-10 10:57:18 +020072 for ((ELEM) = (LYD_TREE_DFS_next) = (struct lyd_node *)(START); \
Radek Krejcie7b95092019-05-15 11:03:07 +020073 (ELEM); \
Michal Vasko56daf732020-08-10 10:57:18 +020074 (ELEM) = (LYD_TREE_DFS_next), LYD_TREE_DFS_continue = 0)
Radek Krejcie7b95092019-05-15 11:03:07 +020075
76/**
77 * @brief Macro to iterate via all elements in a tree. This is the closing part
78 * to the #LYD_TREE_DFS_BEGIN - they always have to be used together.
79 *
80 * Use the same parameters for #LYD_TREE_DFS_BEGIN and #LYD_TREE_DFS_END. While
Michal Vasko56daf732020-08-10 10:57:18 +020081 * START can be any of the lyd_node* types, ELEM variable must be a pointer
82 * to the generic struct lyd_node.
Radek Krejcie7b95092019-05-15 11:03:07 +020083 *
84 * Use with closing curly bracket '}' after the macro.
85 *
86 * @param START Pointer to the starting element processed first.
Radek Krejcie7b95092019-05-15 11:03:07 +020087 * @param ELEM Iterator intended for use in the block.
88 */
89
Michal Vasko56daf732020-08-10 10:57:18 +020090#define LYD_TREE_DFS_END(START, ELEM) \
Radek Krejcie7b95092019-05-15 11:03:07 +020091 /* select element for the next run - children first */ \
Michal Vasko56daf732020-08-10 10:57:18 +020092 if (LYD_TREE_DFS_continue) { \
93 (LYD_TREE_DFS_next) = NULL; \
94 } else { \
Radek Krejcia1c1e542020-09-29 16:06:52 +020095 (LYD_TREE_DFS_next) = lyd_child(ELEM); \
Michal Vasko56daf732020-08-10 10:57:18 +020096 }\
97 if (!(LYD_TREE_DFS_next)) { \
Radek Krejcie7b95092019-05-15 11:03:07 +020098 /* no children */ \
99 if ((ELEM) == (struct lyd_node*)(START)) { \
100 /* we are done, (START) has no children */ \
101 break; \
102 } \
103 /* try siblings */ \
Michal Vasko56daf732020-08-10 10:57:18 +0200104 (LYD_TREE_DFS_next) = (ELEM)->next; \
Radek Krejcie7b95092019-05-15 11:03:07 +0200105 } \
Michal Vasko56daf732020-08-10 10:57:18 +0200106 while (!(LYD_TREE_DFS_next)) { \
Radek Krejcie7b95092019-05-15 11:03:07 +0200107 /* parent is already processed, go to its sibling */ \
108 (ELEM) = (struct lyd_node*)(ELEM)->parent; \
109 /* no siblings, go back through parents */ \
110 if ((ELEM)->parent == (START)->parent) { \
111 /* we are done, no next element to process */ \
112 break; \
113 } \
Michal Vasko56daf732020-08-10 10:57:18 +0200114 (LYD_TREE_DFS_next) = (ELEM)->next; \
115 } } \
Radek Krejcie7b95092019-05-15 11:03:07 +0200116
Michal Vasko64246d82020-08-19 12:35:00 +0200117/* *INDENT-ON* */
118
Radek Krejcie7b95092019-05-15 11:03:07 +0200119/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200120 * @brief Macro to get context from a data tree node.
121 */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200122#define LYD_CTX(node) ((node)->schema ? (node)->schema->module->ctx : ((struct lyd_node_opaq *)(node))->ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200123
124/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200125 * @brief Data input/output formats supported by libyang [parser](@ref howtodataparsers) and
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200126 * [printer](@ref howtodataprinters) functions.
Radek Krejcie7b95092019-05-15 11:03:07 +0200127 */
128typedef enum {
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200129 LYD_UNKNOWN = 0, /**< unknown data format, invalid value */
130 LYD_XML, /**< XML instance data format */
131 LYD_JSON, /**< JSON instance data format */
Michal Vasko69730152020-10-09 16:30:07 +0200132 LYD_LYB /**< LYB instance data format */
Radek Krejcie7b95092019-05-15 11:03:07 +0200133} LYD_FORMAT;
134
135/**
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200136 * @brief All kinds of supported prefix mappings to modules.
137 */
138typedef enum {
139 LY_PREF_SCHEMA, /**< value prefixes map to YANG import prefixes */
140 LY_PREF_XML, /**< value prefixes map to XML namespace prefixes */
Michal Vasko69730152020-10-09 16:30:07 +0200141 LY_PREF_JSON /**< value prefixes map to module names */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200142} LY_PREFIX_FORMAT;
143
144/**
Radek Krejci59583bb2019-09-11 12:57:55 +0200145 * @brief List of possible value types stored in ::lyd_node_any.
Radek Krejcie7b95092019-05-15 11:03:07 +0200146 */
147typedef enum {
Radek Krejci22ebdba2019-07-25 13:59:43 +0200148 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 +0200149 is directly connected into the anydata node without duplication, caller is supposed to not manipulate
150 with the data after a successful call (including calling lyd_free() on the provided data) */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200151 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 +0200152 as string). XML sensitive characters (such as & or \>) are automatically escaped when the anydata
153 is printed in XML format. */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200154 LYD_ANYDATA_XML, /**< Value is a string containing the serialized XML data. */
155 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 +0200156 LYD_ANYDATA_LYB /**< Value is a memory chunk with the serialized data tree in LYB format. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200157} LYD_ANYDATA_VALUETYPE;
158
159/** @} */
160
161/**
162 * @brief YANG data representation
163 */
164struct lyd_value {
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200165 const char *canonical; /**< Canonical string representation of the value in the dictionary. It is never
166 NULL and in case of no canonical value, its JSON representation is used instead. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200167 union {
Radek Krejcie7b95092019-05-15 11:03:07 +0200168 int8_t boolean; /**< 0 as false, 1 as true */
169 int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200170 int8_t int8; /**< 8-bit signed integer */
171 int16_t int16; /**< 16-bit signed integer */
172 int32_t int32; /**< 32-bit signed integer */
173 int64_t int64; /**< 64-bit signed integer */
174 uint8_t uint8; /**< 8-bit unsigned integer */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200175 uint16_t uint16; /**< 16-bit unsigned integer */
176 uint32_t uint32; /**< 32-bit unsigned integer */
177 uint64_t uint64; /**< 64-bit unsigned integer */
Radek Krejcie7b95092019-05-15 11:03:07 +0200178 struct lysc_type_bitenum_item *enum_item; /**< pointer to the definition of the enumeration value */
Radek Krejci849a62a2019-05-22 15:29:05 +0200179 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 +0200180 struct lysc_ident *ident; /**< pointer to the schema definition of the identityref value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200181 struct ly_path *target; /**< Instance-identifier target path. */
182 struct lyd_value_subvalue *subvalue; /** Union value with some metadata. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200183 void *ptr; /**< generic data type structure used to store the data */
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200184 }; /**< The union is just a list of shorthands to possible values stored by a type's plugin. libyang itself uses the lyd_value::realtype
185 plugin's callbacks to work with the data. */
Radek Krejci084289f2019-07-09 17:35:30 +0200186
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200187 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
188 in the schema node of the data node since the type's store plugin can use other types/plugins for
189 storing data. Speaking about built-in types, this is the case of leafref which stores data as its
190 target type. In contrast, union type also use its subtype's callbacks, but inside an internal data
191 lyd_value::subvalue structure, so here is the pointer to the union type.
192 In general, this type is used to get free callback for this lyd_value structure, so it must reflect
193 the type used to store data directly in the same lyd_value instance. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200194};
195
196/**
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200197 * @brief Macro for getting the string canonical value from a term node.
198 *
199 * @param[in] node Term node with the value.
200 * @return Canonical value.
201 */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200202#define LYD_CANON_VALUE(node) ((struct lyd_node_term *)(node))->value.canonical
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200203
204/**
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200205 * @brief Special lyd_value structure for union.
206 *
207 * Represents data with multiple types (union). Original value is stored in the main lyd_value:canonical_cache while
208 * the lyd_value_subvalue::value contains representation according to one of the union's types.
209 * The lyd_value_subvalue:prefixes provides (possible) mappings from prefixes in the original value to YANG modules.
210 * These prefixes are necessary to parse original value to the union's subtypes.
211 */
212struct lyd_value_subvalue {
213 struct lyd_value value; /**< representation of the value according to the selected union's subtype
214 (stored as lyd_value::realtype here, in subvalue structure */
215 const char *original; /**< Original value in the dictionary. */
216 LY_PREFIX_FORMAT format; /**< Prefix format of the value. However, this information is also used to decide
217 whether a value is valid for the specific format or not on later validations
218 (instance-identifier in XML looks different than in JSON). */
219 void *prefix_data; /**< Format-specific data for prefix resolution (see ::ly_resolve_prefix) */
220 uint32_t hints; /**< [Value hints](@ref lydvalhints) from the parser */
221 const struct lysc_node *ctx_node; /**< Context schema node. */
222};
223
224/**
Michal Vasko9f96a052020-03-10 09:41:45 +0100225 * @brief Metadata structure.
Radek Krejcie7b95092019-05-15 11:03:07 +0200226 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100227 * The structure provides information about metadata of a data element. Such attributes must map to
Radek Krejcie7b95092019-05-15 11:03:07 +0200228 * annotations as specified in RFC 7952. The only exception is the filter type (in NETCONF get operations)
229 * and edit-config's operation attributes. In XML, they are represented as standard XML attributes. In JSON,
230 * they are represented as JSON elements starting with the '@' character (for more information, see the
231 * YANG metadata RFC.
232 *
233 */
Michal Vasko9f96a052020-03-10 09:41:45 +0100234struct lyd_meta {
235 struct lyd_node *parent; /**< data node where the metadata is placed */
236 struct lyd_meta *next; /**< pointer to the next metadata of the same element */
237 struct lysc_ext_instance *annotation; /**< pointer to the annotation's definition */
238 const char *name; /**< metadata name */
239 struct lyd_value value; /**< metadata value representation */
Radek Krejcie7b95092019-05-15 11:03:07 +0200240};
241
Michal Vasko52927e22020-03-16 17:26:14 +0100242/**
243 * @brief Generic prefix and namespace mapping, meaning depends on the format.
Radek Krejci1798aae2020-07-14 13:26:06 +0200244 *
245 * 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
246 * ly_ctx_get_module_implemented_ns() or ly_ctx_get_module_implemented(). While the module reference is always present,
247 * the id member can be omitted in case it is not present in the source data as a reference to the default namespace.
Michal Vasko52927e22020-03-16 17:26:14 +0100248 */
249struct ly_prefix {
Radek Krejci1798aae2020-07-14 13:26:06 +0200250 const char *id; /**< identifier used in the qualified name of the item to reference data node namespace */
251 union {
252 const char *module_ns; /**< namespace of the module where the data are supposed to belongs to, used for LYD_XML format. */
253 const char *module_name; /**< name of the module where the data are supposed to belongs to, used for LYD_JSON format. */
254 };
Michal Vasko52927e22020-03-16 17:26:14 +0100255};
256
257/**
258 * @brief Generic attribute structure.
259 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200260struct lyd_attr {
Michal Vasko52927e22020-03-16 17:26:14 +0100261 struct lyd_node_opaq *parent; /**< data node where the attribute is placed */
Radek Krejci1798aae2020-07-14 13:26:06 +0200262 struct lyd_attr *next;
Michal Vasko52927e22020-03-16 17:26:14 +0100263 struct ly_prefix *val_prefs; /**< list of prefixes in the value ([sized array](@ref sizedarrays)) */
264 const char *name;
265 const char *value;
266
Radek Krejci5536d282020-08-04 23:27:44 +0200267 LYD_FORMAT format; /**< format of the prefixes, only LYD_XML and LYD_JSON values can appear at this place */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200268 uint32_t hints; /**< additional information about from the data source, see the [hints list](@ref lydhints) */
Michal Vasko52927e22020-03-16 17:26:14 +0100269 struct ly_prefix prefix; /**< name prefix, it is stored because they are a real pain to generate properly */
270
271};
Radek Krejcie7b95092019-05-15 11:03:07 +0200272
Michal Vasko1bf09392020-03-27 12:38:10 +0100273#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 +0200274#define LYD_NODE_TERM (LYS_LEAF|LYS_LEAFLIST) /**< Schema nodetype mask for lyd_node_term */
275#define LYD_NODE_ANY (LYS_ANYDATA) /**< Schema nodetype mask for lyd_node_any */
276
277/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100278 * @defgroup dnodeflags Data node flags
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200279 * @ingroup datatree
280 * @{
281 *
282 * Various flags of data nodes.
283 *
284 * 1 - container 5 - anydata/anyxml
285 * 2 - list 6 - rpc/action
286 * 3 - leaf 7 - notification
287 * 4 - leaflist
288 *
289 * bit name 1 2 3 4 5 6 7
290 * ---------------------+-+-+-+-+-+-+-+
291 * 1 LYD_DEFAULT |x| |x|x| | | |
292 * +-+-+-+-+-+-+-+
Michal Vasko5c4e5892019-11-14 12:31:38 +0100293 * 2 LYD_WHEN_TRUE |x|x|x|x|x| | |
Michal Vasko9b368d32020-02-14 13:53:31 +0100294 * +-+-+-+-+-+-+-+
295 * 3 LYD_NEW |x|x|x|x|x|x|x|
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200296 * ---------------------+-+-+-+-+-+-+-+
297 *
298 */
299
Michal Vasko5c4e5892019-11-14 12:31:38 +0100300#define LYD_DEFAULT 0x01 /**< default (implicit) node */
301#define LYD_WHEN_TRUE 0x02 /**< all when conditions of this node were evaluated to true */
Michal Vasko9b368d32020-02-14 13:53:31 +0100302#define LYD_NEW 0x04 /**< node was created after the last validation, is needed for the next validation */
Michal Vasko52927e22020-03-16 17:26:14 +0100303
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200304/** @} */
305
306/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200307 * @brief Generic structure for a data node.
308 */
309struct lyd_node {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200310 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
311 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
312 used to get know that nodes are not equal, it cannot be used to decide that the
313 nodes are equal due to possible collisions. */
314 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Michal Vaskoecd62de2019-11-13 12:35:11 +0100315 const struct lysc_node *schema; /**< pointer to the schema definition of this node, note that the target can be not just
316 ::struct lysc_node but ::struct lysc_action or ::struct lysc_notif as well */
Radek Krejcie7b95092019-05-15 11:03:07 +0200317 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
318 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
319 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
320 never NULL. If there is no sibling node, pointer points to the node
321 itself. In case of the first node, this pointer points to the last
322 node in the list. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100323 struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200324
325#ifdef LY_ENABLED_LYD_PRIV
326 void *priv; /**< private user data, not used by libyang */
327#endif
328};
329
330/**
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200331 * @brief Data node structure for the inner data tree nodes - containers, lists, RPCs, actions and Notifications.
Radek Krejcie7b95092019-05-15 11:03:07 +0200332 */
333struct lyd_node_inner {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200334 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
335 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
336 used to get know that nodes are not equal, it cannot be used to decide that the
337 nodes are equal due to possible collisions. */
338 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200339 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
340 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
341 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
342 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
343 never NULL. If there is no sibling node, pointer points to the node
344 itself. In case of the first node, this pointer points to the last
345 node in the list. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100346 struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200347
348#ifdef LY_ENABLED_LYD_PRIV
349 void *priv; /**< private user data, not used by libyang */
350#endif
351
352 struct lyd_node *child; /**< pointer to the first child node. */
353 struct hash_table *children_ht; /**< hash table with all the direct children (except keys for a list, lists without keys) */
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200354#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 +0200355};
356
357/**
Michal Vaskof03ed032020-03-04 13:31:44 +0100358 * @brief Data node structure for the terminal data tree nodes - leaves and leaf-lists.
Radek Krejcie7b95092019-05-15 11:03:07 +0200359 */
360struct lyd_node_term {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200361 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
362 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
363 used to get know that nodes are not equal, it cannot be used to decide that the
364 nodes are equal due to possible collisions. */
365 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200366 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
367 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
368 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
369 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
370 never NULL. If there is no sibling node, pointer points to the node
371 itself. In case of the first node, this pointer points to the last
372 node in the list. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100373 struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200374
375#ifdef LY_ENABLED_LYD_PRIV
376 void *priv; /**< private user data, not used by libyang */
377#endif
378
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200379 struct lyd_value value; /**< node's value representation */
Radek Krejcie7b95092019-05-15 11:03:07 +0200380};
381
382/**
383 * @brief Data node structure for the anydata data tree nodes - anydatas and anyxmls.
384 */
385struct lyd_node_any {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200386 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
387 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
388 used to get know that nodes are not equal, it cannot be used to decide that the
389 nodes are equal due to possible collisions. */
390 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200391 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
392 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
393 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
394 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
395 never NULL. If there is no sibling node, pointer points to the node
396 itself. In case of the first node, this pointer points to the last
397 node in the list. */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200398 struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200399
400#ifdef LY_ENABLED_LYD_PRIV
401 void *priv; /**< private user data, not used by libyang */
402#endif
403
Michal Vasko00cbf532020-06-15 13:58:47 +0200404 union lyd_any_value {
Radek Krejciee4cab22019-07-17 17:07:47 +0200405 struct lyd_node *tree; /**< data tree */
406 const char *str; /**< Generic string data */
407 const char *xml; /**< Serialized XML data */
408 const char *json; /**< I-JSON encoded string */
409 char *mem; /**< LYD_ANYDATA_LYB memory chunk */
410 } value; /**< pointer to the stored value representation of the anydata/anyxml node */
411 LYD_ANYDATA_VALUETYPE value_type;/**< type of the data stored as lyd_node_any::value */
Radek Krejcie7b95092019-05-15 11:03:07 +0200412};
413
414/**
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200415 * @defgroup lydvalhints Value format hints. Any information about value types encoded in the format
416 * is hinted this way.
Radek Krejci1798aae2020-07-14 13:26:06 +0200417 *
Radek Krejci1798aae2020-07-14 13:26:06 +0200418 * @{
419 */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200420#define LYD_VALHINT_STRING 0x0001 /**< value is allowed to be a string */
421#define LYD_VALHINT_DECNUM 0x0002 /**< value is allowed to be a decimal number */
422#define LYD_VALHINT_OCTNUM 0x0004 /**< value is allowed to be an octal number */
423#define LYD_VALHINT_HEXNUM 0x0008 /**< value is allowed to be a hexadecimal number */
424#define LYD_VALHINT_NUM64 0x0010 /**< value is allowed to be an int64 or uint64 */
425#define LYD_VALHINT_BOOLEAN 0x0020 /**< value is allowed to be a boolean */
426#define LYD_VALHINT_EMPTY 0x0040 /**< value is allowed to be empty */
427/**
428 * @} lydvalhints
429 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200430
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200431/**
432 * @defgroup lydnodehints Node type format hints. Any information about node types encoded in the format
433 * is hinted this way.
434 *
435 * @{
436 */
437#define LYD_NODEHINT_LIST 0x0080 /**< node is allowed to be a list instance */
438#define LYD_NODEHINT_LEAFLIST 0x0100 /**< node is allowed to be a leaf-list instance */
439#define LYD_NODEHINT_ENVELOPE 0x8000 /**< only found in opaque node hints; node is a special protocol-dependent
440 RPC/Action/Notification envelope */
441/**
442 * @} lydnodehints
443 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200444
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200445/**
446 * @defgroup lydhints Value and node type format hints. Any information about value and node types encoded in the format
447 * is hinted this way. It combines [value hints](@ref lydvalhints) and [node hints](@ref lydnodehints).
448 *
449 * @{
450 */
451#define LYD_HINT_DATA 0x01F3 /**< special node/value hint to be used for generic data node/value (for cases when
452 there is no encoding or it does not provide any additional information about
453 a node/value type); do not combine with specific [value hints](@ref lydvalhints)
454 or [node hints](@ref lydnodehints). */
455#define LYD_HINT_SCHEMA 0x01FF /**< special node/value hint to be used for generic schema node/value(for cases when
456 there is no encoding or it does not provide any additional information about
457 a node/value type); do not combine with specific [value hints](@ref lydvalhints)
458 or [node hints](@ref lydnodehints). */
459/**
460 * @} lydhints
461 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200462
463/**
Michal Vasko52927e22020-03-16 17:26:14 +0100464 * @brief Data node structure for unparsed (opaque) nodes.
465 */
466struct lyd_node_opaq {
467 uint32_t hash; /**< always 0 */
468 uint32_t flags; /**< always 0 */
469 const struct lysc_node *schema; /**< always NULL */
470 struct lyd_node *parent; /**< pointer to the parent node (NULL in case of root node) */
471 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
472 struct lyd_node *prev; /**< pointer to the previous sibling node (last sibling if there is none) */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200473 struct lyd_attr *attr; /**< pointer to the list of generic attributes of this node */
Michal Vasko52927e22020-03-16 17:26:14 +0100474
475#ifdef LY_ENABLED_LYD_PRIV
476 void *priv; /**< private user data, not used by libyang */
477#endif
478
479 struct lyd_node *child; /**< pointer to the child node (NULL if there are none) */
480 const char *name;
481 LYD_FORMAT format;
482 struct ly_prefix prefix; /**< name prefix */
483 struct ly_prefix *val_prefs; /**< list of prefixes in the value ([sized array](@ref sizedarrays)) */
484 const char *value; /**< original value */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200485 uint32_t hints; /**< additional information about from the data source, see the [hints list](@ref lydhints) */
Michal Vasko52927e22020-03-16 17:26:14 +0100486 const struct ly_ctx *ctx; /**< libyang context */
487};
488
489/**
Radek Krejcia1c1e542020-09-29 16:06:52 +0200490 * @brief Get the generic parent pointer of a data node.
491 *
492 * @param[in] node Node whose parent pointer to get.
493 * @return Pointer to the parent node of the @p node.
494 * @return NULL in case of the top-level node or if the @p node is NULL itself.
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200495 */
Radek Krejcia1c1e542020-09-29 16:06:52 +0200496struct lyd_node *lyd_parent(const struct lyd_node *node);
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200497
498/**
Radek Krejcia1c1e542020-09-29 16:06:52 +0200499 * @brief Get the child pointer of a generic data node.
Radek Krejcie7b95092019-05-15 11:03:07 +0200500 *
Radek Krejcia1c1e542020-09-29 16:06:52 +0200501 * Decides the node's type and in case it has a children list, returns it. Supports even the opaq nodes (::lyd_node_opaq).
502 *
503 * If you need to skip key children, use ::lyd_child_no_keys().
504 *
505 * @param[in] node Node to use.
506 * @return Pointer to the first child node (if any) of the @p node.
Radek Krejcie7b95092019-05-15 11:03:07 +0200507 */
Radek Krejcia1c1e542020-09-29 16:06:52 +0200508struct lyd_node *lyd_child(const struct lyd_node *node);
509
510/**
511 * @brief Get the child pointer of a generic data node but skip its keys in case it is ::LYS_LIST.
512 *
513 * Decides the node's type and in case it has a children list, returns it. Supports even the opaq nodes (::lyd_node_opaq).
514 *
515 * If you need to take key children into account, use ::lyd_child().
516 *
517 * @param[in] node Node to use.
518 * @return Pointer to the first child node (if any) of the @p node.
519 */
520struct lyd_node *lyd_child_no_keys(const struct lyd_node *node);
Radek Krejcie7b95092019-05-15 11:03:07 +0200521
522/**
Michal Vaskoc193ce92020-03-06 11:04:48 +0100523 * @brief Get the owner module of the data node. It is the module of the top-level schema node. Generally,
524 * in case of augments it is the target module, recursively, otherwise it is the module where the data node is defined.
525 *
526 * @param[in] node Data node to examine.
527 * @return Module owner of the node.
528 */
529const struct lys_module *lyd_owner_module(const struct lyd_node *node);
530
531/**
Radek Krejci19611252020-10-04 13:54:53 +0200532 * @brief Check whether a node value equals to its default one.
533 *
534 * @param[in] node Term node to test.
535 * @return false (no, it is not a default node) or true (yes, it is default)
536 */
537ly_bool lyd_is_default(const struct lyd_node *node);
538
539/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200540 * @brief Learn the length of LYB data.
541 *
542 * @param[in] data LYB data to examine.
543 * @return Length of the LYB data chunk,
544 * @return -1 on error.
545 */
546int lyd_lyb_data_length(const char *data);
547
548/**
Michal Vaskoc0004272020-08-06 08:32:34 +0200549 * @brief Copy anydata value from one node to another. Target value is freed first.
550 *
551 * @param[in,out] trg Target node.
552 * @param[in] value Source value, may be NULL when the target value is only freed.
553 * @param[in] value_type Source value type.
554 * @return LY_ERR value.
555 */
556LY_ERR lyd_any_copy_value(struct lyd_node *trg, const union lyd_any_value *value, LYD_ANYDATA_VALUETYPE value_type);
557
558/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200559 * @brief Create a new inner node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +0100560 *
561 * @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 +0100562 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100563 * @param[in] name Schema node name of the new data node. The node can be #LYS_CONTAINER, #LYS_NOTIF, #LYS_RPC, or #LYS_ACTION.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200564 * @param[out] node Optional created node.
565 * @return LY_ERR value.
Michal Vasko013a8182020-03-03 10:46:53 +0100566 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200567LY_ERR lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, struct lyd_node **node);
Michal Vasko013a8182020-03-03 10:46:53 +0100568
569/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200570 * @brief Create a new list node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +0100571 *
572 * @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 +0100573 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100574 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200575 * @param[out] node Optional created node.
Michal Vasko013a8182020-03-03 10:46:53 +0100576 * @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 +0200577 * or identityref value, the JSON format is expected (module names instead of prefixes). No keys are expected for
578 * key-less lists.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200579 * @return LY_ERR value.
Michal Vasko013a8182020-03-03 10:46:53 +0100580 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200581LY_ERR lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, struct lyd_node **node, ...);
Michal Vasko013a8182020-03-03 10:46:53 +0100582
583/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200584 * @brief Create a new list node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +0100585 *
586 * @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 +0100587 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100588 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
589 * @param[in] keys All key values predicate in the form of "[key1='val1'][key2='val2']...", they do not have to be ordered.
590 * 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 +0200591 * Use NULL or string of length 0 in case of key-less list.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200592 * @param[out] node Optional created node.
593 * @return LY_ERR value.
Michal Vasko013a8182020-03-03 10:46:53 +0100594 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200595LY_ERR lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
Radek Krejci0f969882020-08-21 16:56:47 +0200596 struct lyd_node **node);
Michal Vasko013a8182020-03-03 10:46:53 +0100597
598/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200599 * @brief Create a new term node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +0100600 *
601 * @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 +0100602 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100603 * @param[in] name Schema node name of the new data node. The node can be #LYS_LEAF or #LYS_LEAFLIST.
604 * @param[in] val_str String form of the value of the node being created. In case of an instance-identifier or identityref
605 * value, the JSON format is expected (module names instead of prefixes).
Michal Vasko3a41dff2020-07-15 14:30:28 +0200606 * @param[out] node Optional created node.
607 * @return LY_ERR value.
Michal Vasko013a8182020-03-03 10:46:53 +0100608 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200609LY_ERR lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
Radek Krejci0f969882020-08-21 16:56:47 +0200610 struct lyd_node **node);
Michal Vasko013a8182020-03-03 10:46:53 +0100611
612/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200613 * @brief Create a new any node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +0100614 *
615 * @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 +0100616 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100617 * @param[in] name Schema node name of the new data node. The node can be #LYS_ANYDATA or #LYS_ANYXML.
618 * @param[in] value Value to be directly assigned to the node. Expected type is determined by @p value_type.
619 * @param[in] value_type Type of the provided value in @p value.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200620 * @param[out] node Optional created node.
621 * @return LY_ERR value.
Michal Vasko013a8182020-03-03 10:46:53 +0100622 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200623LY_ERR lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
Radek Krejci0f969882020-08-21 16:56:47 +0200624 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node);
Michal Vasko013a8182020-03-03 10:46:53 +0100625
626/**
Michal Vaskod86997b2020-05-26 15:19:54 +0200627 * @brief Create new metadata for a data node.
628 *
629 * @param[in] parent Parent node for the metadata being created.
Michal Vasko00cbf532020-06-15 13:58:47 +0200630 * @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 +0200631 * @param[in] name Annotation name of the new metadata. It can include the annotation module as the prefix.
632 * 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 +0200633 * @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 +0200634 * value, the JSON format is expected (module names instead of prefixes).
Michal Vasko3a41dff2020-07-15 14:30:28 +0200635 * @param[out] meta Optional created metadata.
636 * @return LY_ERR value.
Michal Vaskod86997b2020-05-26 15:19:54 +0200637 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200638LY_ERR lyd_new_meta(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
Radek Krejci0f969882020-08-21 16:56:47 +0200639 struct lyd_meta **meta);
Michal Vaskod86997b2020-05-26 15:19:54 +0200640
641/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200642 * @brief Create a new opaque node in the data tree.
643 *
644 * @param[in] parent Parent node for the node beaing created. NULL in case of creating a top level element.
645 * @param[in] ctx libyang context. If NULL, @p parent context will be used.
646 * @param[in] name Node name.
647 * @param[in] value Node value, may be NULL.
648 * @param[in] module_name Node module name.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200649 * @param[out] node Optional created node.
650 * @return LY_ERR value.
Michal Vasko00cbf532020-06-15 13:58:47 +0200651 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200652LY_ERR lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
Radek Krejci0f969882020-08-21 16:56:47 +0200653 const char *module_name, struct lyd_node **node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200654
655/**
656 * @brief Create new attribute for an opaque data node.
657 *
658 * @param[in] parent Parent opaque node for the attribute being created.
659 * @param[in] module Module name of the attribute being created. There may be none.
660 * @param[in] name Attribute name. It can include the module name as the prefix.
661 * @param[in] val_str String value of the attribute. Is stored directly.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200662 * @param[out] attr Optional created attribute.
663 * @return LY_ERR value.
Michal Vasko00cbf532020-06-15 13:58:47 +0200664 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200665LY_ERR lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *val_str,
Radek Krejci0f969882020-08-21 16:56:47 +0200666 struct lyd_attr **attr);
Michal Vasko00cbf532020-06-15 13:58:47 +0200667
668/**
669 * @defgroup pathoptions Data path creation options
670 * @ingroup datatree
671 *
672 * Various options to change lyd_new_path*() behavior.
673 *
674 * Default behavior:
675 * - if the target node already exists (and is not default), an error is returned.
676 * - the whole path to the target node is created (with any missing parents) if necessary.
677 * - RPC output schema children are completely ignored in all modules. Input is searched and nodes created normally.
678 * @{
679 */
680
681#define LYD_NEWOPT_UPDATE 0x01 /**< If the target node exists, is a leaf, and it is updated with a new value or its
682 default flag is changed, it is returned. If the target node exists and is not
683 a leaf or generally no change occurs in the @p parent tree, NULL is returned and
684 no error set. */
685#define LYD_NEWOPT_OUTPUT 0x02 /**< Changes the behavior to ignoring RPC/action input schema nodes and using only
686 output ones. */
687#define LYD_NEWOPT_OPAQ 0x04 /**< Enables the creation of opaque nodes with some specific rules. If the __last node__
688 in the path is not uniquely defined ((leaf-)list without a predicate) or has an
689 invalid value (leaf/leaf-list), it is created as opaque. */
690
691/** @} pathoptions */
692
693/**
694 * @brief Create a new node in the data tree based on a path. Cannot be used for anyxml/anydata nodes,
695 * for those use ::lyd_new_path_any.
696 *
697 * If @p path points to a list key and the list instance does not exist, the key value from the predicate is used
698 * and @p value is ignored. Also, if a leaf-list is being created and both a predicate is defined in @p path
699 * and @p value is set, the predicate is preferred.
700 *
701 * @param[in] parent Data parent to add to/modify, can be NULL.
702 * @param[in] ctx libyang context, must be set if @p parent is NULL.
703 * @param[in] path Path to create (TODO ref path).
704 * @param[in] value Value of the new leaf/leaf-list. For other node types, it is ignored.
705 * @param[in] options Bitmask of options, see @ref pathoptions.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200706 * @param[out] node Optional first created node.
707 * @return LY_ERR value.
Michal Vasko00cbf532020-06-15 13:58:47 +0200708 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200709LY_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 +0200710 uint32_t options, struct lyd_node **node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200711
712/**
713 * @brief Create a new node in the data tree based on a path. All node types can be created.
714 *
715 * If @p path points to a list key and the list instance does not exist, the key value from the predicate is used
716 * and @p value is ignored. Also, if a leaf-list is being created and both a predicate is defined in @p path
717 * and @p value is set, the predicate is preferred.
718 *
719 * @param[in] parent Data parent to add to/modify, can be NULL.
720 * @param[in] ctx libyang context, must be set if @p parent is NULL.
721 * @param[in] path Path to create (TODO ref path).
722 * @param[in] value Value of the new leaf/leaf-list/anyxml/anydata. For other node types, it is ignored.
723 * @param[in] value_type Anyxml/anydata node @p value type.
724 * @param[in] options Bitmask of options, see @ref pathoptions.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200725 * @param[out] node Optional first created node.
726 * @return LY_ERR value.
Michal Vasko00cbf532020-06-15 13:58:47 +0200727 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200728LY_ERR lyd_new_path_any(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200729 LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200730
731/**
732 * @brief Create a new node in the data tree based on a path. All node types can be created.
733 *
734 * If @p path points to a list key and the list instance does not exist, the key value from the predicate is used
735 * and @p value is ignored. Also, if a leaf-list is being created and both a predicate is defined in @p path
736 * and @p value is set, the predicate is preferred.
737 *
738 * @param[in] parent Data parent to add to/modify, can be NULL.
739 * @param[in] ctx libyang context, must be set if @p parent is NULL.
740 * @param[in] path Path to create (TODO ref path).
741 * @param[in] value Value of the new leaf/leaf-list/anyxml/anydata. For other node types, it is ignored.
742 * @param[in] value_type Anyxml/anydata node @p value type.
743 * @param[in] options Bitmask of options, see @ref pathoptions.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200744 * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node.
745 * @param[out] new_node Optional last node created.
Michal Vasko00cbf532020-06-15 13:58:47 +0200746 * @return LY_ERR value.
747 */
748LY_ERR lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200749 LYD_ANYDATA_VALUETYPE value_type, uint32_t options, struct lyd_node **new_parent, struct lyd_node **new_node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200750
751/**
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200752 * @defgroup implicitoptions Implicit node creation options
753 * @ingroup datatree
754 *
755 * Various options to change lyd_new_implicit*() behavior.
756 *
757 * Default behavior:
758 * - both configuration and state missing implicit nodes are added.
759 * - all implicit node types are added (non-presence containers, default leaves, and default leaf-lists).
760 * @{
761 */
762
Michal Vasko44b19a12020-08-07 09:21:30 +0200763#define LYD_IMPLICIT_NO_STATE 0x01 /**< Do not add any implicit state nodes. */
764#define LYD_IMPLICIT_NO_CONFIG 0x02 /**< Do not add any implicit config nodes. */
765#define LYD_IMPLICIT_NO_DEFAULTS 0x04 /**< Do not add any default nodes (leaves/leaf-lists), only non-presence
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200766 containers. */
767
768/** @} implicitoptions */
769
770/**
771 * @brief Add any missing implicit nodes into a data subtree.
772 *
773 * @param[in] tree Tree to add implicit nodes into.
774 * @param[in] implicit_options Options for implicit node creation, see @ref implicitoptions.
775 * @param[out] diff Optional diff with any created nodes.
776 * @return LY_ERR value.
777 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200778LY_ERR lyd_new_implicit_tree(struct lyd_node *tree, uint32_t implicit_options, struct lyd_node **diff);
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200779
780/**
781 * @brief Add any missing implicit nodes.
782 *
783 * @param[in,out] tree Tree to add implicit nodes into.
784 * @param[in] ctx libyang context, must be set only if @p tree is an empty tree.
785 * @param[in] implicit_options Options for implicit node creation, see @ref implicitoptions.
786 * @param[out] diff Optional diff with any created nodes.
787 * @return LY_ERR value.
788 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200789LY_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 +0200790
791/**
792 * @brief Add any missing implicit nodes of one module.
793 *
794 * @param[in,out] tree Tree to add implicit nodes into.
795 * @param[in] module Module whose implicit nodes to create.
796 * @param[in] implicit_options Options for implicit node creation, see @ref implicitoptions.
797 * @param[out] diff Optional diff with any created nodes.
798 * @return LY_ERR value.
799 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200800LY_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 +0200801 struct lyd_node **diff);
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200802
803/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200804 * @brief Change the value of a term (leaf or leaf-list) node.
805 *
806 * Node changed this way is always considered explicitly set, meaning its default flag
807 * is always cleared.
808 *
809 * @param[in] term Term node to change.
810 * @param[in] val_str New value to set, any prefixes are expected in JSON format.
811 * @return LY_SUCCESS if value was changed,
812 * @return LY_EEXIST if value was the same and only the default flag was cleared,
813 * @return LY_ENOT if the values were equal and no change occured,
814 * @return LY_ERR value on other errors.
815 */
816LY_ERR lyd_change_term(struct lyd_node *term, const char *val_str);
817
818/**
Michal Vasko41586352020-07-13 13:54:25 +0200819 * @brief Change the value of a metadata instance.
820 *
821 * @param[in] ctx libyang context.
822 * @param[in] meta Metadata to change.
823 * @param[in] val_str New value to set, any prefixes are expected in JSON format.
824 * @return LY_SUCCESS if value was changed,
825 * @return LY_ENOT if the values were equal and no change occured,
826 * @return LY_ERR value on other errors.
827 */
828LY_ERR lyd_change_meta(struct lyd_meta *meta, const char *val_str);
829
830/**
Michal Vaskob104f112020-07-17 09:54:54 +0200831 * @brief Insert a child into a parent.
Michal Vaskof03ed032020-03-04 13:31:44 +0100832 *
833 * - if the node is part of some other tree, it is automatically unlinked.
834 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
835 *
836 * @param[in] parent Parent node to insert into.
837 * @param[in] node Node to insert.
838 * @return LY_SUCCESS on success.
839 * @return LY_ERR error on error.
840 */
Michal Vaskob104f112020-07-17 09:54:54 +0200841LY_ERR lyd_insert_child(struct lyd_node *parent, struct lyd_node *node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100842
843/**
Michal Vaskob104f112020-07-17 09:54:54 +0200844 * @brief Insert a node into siblings.
Michal Vaskob1b5c262020-03-05 14:29:47 +0100845 *
846 * - if the node is part of some other tree, it is automatically unlinked.
847 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
848 *
Michal Vaskob104f112020-07-17 09:54:54 +0200849 * @param[in] sibling Siblings to insert into, can even be NULL.
Michal Vaskob1b5c262020-03-05 14:29:47 +0100850 * @param[in] node Node to insert.
Michal Vaskob104f112020-07-17 09:54:54 +0200851 * @param[out] first Optionally return the first sibling after insertion. Can be the address of @p sibling.
Michal Vaskob1b5c262020-03-05 14:29:47 +0100852 * @return LY_SUCCESS on success.
853 * @return LY_ERR error on error.
854 */
Michal Vaskob104f112020-07-17 09:54:54 +0200855LY_ERR lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100856
857/**
Michal Vaskob104f112020-07-17 09:54:54 +0200858 * @brief Insert a node before another node, can be used only for user-ordered nodes.
Michal Vaskof03ed032020-03-04 13:31:44 +0100859 *
860 * - if the node is part of some other tree, it is automatically unlinked.
861 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
862 *
863 * @param[in] sibling Sibling node to insert before.
864 * @param[in] node Node to insert.
865 * @return LY_SUCCESS on success.
866 * @return LY_ERR error on error.
867 */
868LY_ERR lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node);
869
870/**
Michal Vaskob104f112020-07-17 09:54:54 +0200871 * @brief Insert a node after another node, can be used only for user-ordered nodes.
Michal Vaskof03ed032020-03-04 13:31:44 +0100872 *
873 * - if the node is part of some other tree, it is automatically unlinked.
874 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
875 *
876 * @param[in] sibling Sibling node to insert after.
877 * @param[in] node Node to insert.
878 * @return LY_SUCCESS on success.
879 * @return LY_ERR error on error.
880 */
881LY_ERR lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node);
882
883/**
884 * @brief Unlink the specified data subtree.
885 *
886 * @param[in] node Data tree node to be unlinked (together with all the children).
887 */
888void lyd_unlink_tree(struct lyd_node *node);
889
890/**
Radek Krejcib0849a22019-07-25 12:31:04 +0200891 * @brief Free all the nodes (even parents of the node) in the data tree.
Radek Krejcie7b95092019-05-15 11:03:07 +0200892 *
893 * @param[in] node Any of the nodes inside the tree.
894 */
895void lyd_free_all(struct lyd_node *node);
896
897/**
Michal Vasko3a41dff2020-07-15 14:30:28 +0200898 * @brief Free all the sibling nodes (preceding as well as succeeding).
Radek Krejcib0849a22019-07-25 12:31:04 +0200899 *
900 * @param[in] node Any of the sibling nodes to free.
901 */
Michal Vaskof03ed032020-03-04 13:31:44 +0100902void lyd_free_siblings(struct lyd_node *node);
Radek Krejcib0849a22019-07-25 12:31:04 +0200903
904/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200905 * @brief Free (and unlink) the specified data (sub)tree.
906 *
Radek Krejcie7b95092019-05-15 11:03:07 +0200907 * @param[in] node Root of the (sub)tree to be freed.
908 */
909void lyd_free_tree(struct lyd_node *node);
910
911/**
Michal Vasko3a41dff2020-07-15 14:30:28 +0200912 * @brief Free a single metadata instance.
Radek Krejcie7b95092019-05-15 11:03:07 +0200913 *
Michal Vasko3a41dff2020-07-15 14:30:28 +0200914 * @param[in] meta Metadata to free.
Radek Krejcie7b95092019-05-15 11:03:07 +0200915 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200916void lyd_free_meta_single(struct lyd_meta *meta);
Michal Vasko52927e22020-03-16 17:26:14 +0100917
918/**
Michal Vasko3a41dff2020-07-15 14:30:28 +0200919 * @brief Free the metadata instance with any following instances.
920 *
921 * @param[in] meta Metadata to free.
922 */
923void lyd_free_meta_siblings(struct lyd_meta *meta);
924
925/**
926 * @brief Free a single attribute.
Michal Vasko52927e22020-03-16 17:26:14 +0100927 *
928 * @param[in] ctx Context where the attributes were created.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200929 * @param[in] attr Attribute to free.
Michal Vasko52927e22020-03-16 17:26:14 +0100930 */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200931void lyd_free_attr_single(const struct ly_ctx *ctx, struct lyd_attr *attr);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200932
933/**
934 * @brief Free the attribute with any following attributes.
935 *
936 * @param[in] ctx Context where the attributes were created.
937 * @param[in] attr First attribute to free.
938 */
Radek Krejci011e4aa2020-09-04 15:22:31 +0200939void lyd_free_attr_siblings(const struct ly_ctx *ctx, struct lyd_attr *attr);
Radek Krejcie7b95092019-05-15 11:03:07 +0200940
Radek Krejci084289f2019-07-09 17:35:30 +0200941/**
942 * @brief Check type restrictions applicable to the particular leaf/leaf-list with the given string @p value.
943 *
944 * The given node is not modified in any way - it is just checked if the @p value can be set to the node.
945 *
946 * If there is no data node instance and you are fine with checking just the type's restrictions without the
947 * data tree context (e.g. for the case of require-instance restriction), use lys_value_validate().
948 *
949 * @param[in] ctx libyang context for logging (function does not log errors when @p ctx is NULL)
950 * @param[in] node Data node for the @p value.
Michal Vaskof937cfe2020-08-03 16:07:12 +0200951 * @param[in] value String value to be checked, it is expected to be in JSON format.
Radek Krejci084289f2019-07-09 17:35:30 +0200952 * @param[in] value_len Length of the given @p value (mandatory).
Michal Vaskof03ed032020-03-04 13:31:44 +0100953 * @param[in] tree Data tree (e.g. when validating RPC/Notification) where the required data instance (leafref target,
954 * instance-identifier) can be placed. NULL in case the data tree is not yet complete,
955 * then LY_EINCOMPLETE can be returned.
Michal Vasko3701af52020-08-03 14:29:38 +0200956 * @param[out] realtype Optional real type of the value.
Radek Krejci084289f2019-07-09 17:35:30 +0200957 * @return LY_SUCCESS on success
958 * @return LY_EINCOMPLETE in case the @p trees is not provided and it was needed to finish the validation (e.g. due to require-instance).
959 * @return LY_ERR value if an error occurred.
960 */
Michal Vasko44685da2020-03-17 15:38:06 +0100961LY_ERR lyd_value_validate(const struct ly_ctx *ctx, const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200962 const struct lyd_node *tree, const struct lysc_type **realtype);
Radek Krejci084289f2019-07-09 17:35:30 +0200963
964/**
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200965 * @brief Compare the node's value with the given string value. The string value is first validated according to
966 * the (current) node's type.
Radek Krejci084289f2019-07-09 17:35:30 +0200967 *
968 * @param[in] node Data node to compare.
969 * @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 +0200970 * it is validated and canonized if possible. But it is expected to be in JSON format.
Radek Krejci084289f2019-07-09 17:35:30 +0200971 * @param[in] value_len Length of the given @p value (mandatory).
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200972 * @return LY_SUCCESS on success,
973 * @return LY_ENOT if the values do not match,
Radek Krejci084289f2019-07-09 17:35:30 +0200974 * @return LY_ERR value if an error occurred.
975 */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200976LY_ERR lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len);
Radek Krejci084289f2019-07-09 17:35:30 +0200977
Radek Krejci576b23f2019-07-12 14:06:32 +0200978/**
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200979 * @defgroup datacompareoptions Data compare options
980 * @ingroup datatree
981 *
Radek Krejci22ebdba2019-07-25 13:59:43 +0200982 * Various options to change the lyd_compare() behavior.
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200983 */
984#define LYD_COMPARE_FULL_RECURSION 0x01 /* lists and containers are the same only in case all they children
985 (subtree, so direct as well as indirect children) are the same. By default,
986 containers are the same in case of the same schema node and lists are the same
987 in case of equal keys (keyless lists do the full recursion comparison all the time). */
988#define LYD_COMPARE_DEFAULTS 0x02 /* By default, implicit and explicit default nodes are considered to be equal. This flag
989 changes this behavior and implicit (automatically created default node) and explicit
990 (explicitly created node with the default value) default nodes are considered different. */
Michal Vasko60ea6352020-06-29 13:39:39 +0200991/** @} datacompareoptions */
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200992
993/**
994 * @brief Compare 2 data nodes if they are equivalent.
995 *
996 * @param[in] node1 The first node to compare.
997 * @param[in] node2 The second node to compare.
Radek Krejcic5ad9652019-09-11 11:31:51 +0200998 * @param[in] options Various @ref datacompareoptions.
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200999 * @return LY_SUCCESS if the nodes are equivalent.
1000 * @return LY_ENOT if the nodes are not equivalent.
1001 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02001002LY_ERR lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options);
Michal Vasko8f359bf2020-07-28 10:41:15 +02001003
1004/**
1005 * @brief Compare 2 lists of siblings if they are equivalent.
1006 *
1007 * @param[in] node1 The first sibling list to compare.
1008 * @param[in] node2 The second sibling list to compare.
1009 * @param[in] options Various @ref datacompareoptions.
1010 * @return LY_SUCCESS if all the siblings are equivalent.
1011 * @return LY_ENOT if the siblings are not equivalent.
1012 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02001013LY_ERR lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options);
Radek Krejci1f05b6a2019-07-18 16:15:06 +02001014
1015/**
Michal Vasko21725742020-06-29 11:49:25 +02001016 * @brief Compare 2 metadata.
1017 *
1018 * @param[in] meta1 First metadata.
1019 * @param[in] meta2 Second metadata.
1020 * @return LY_SUCCESS if the metadata are equivalent.
1021 * @return LY_ENOT if not.
1022 */
1023LY_ERR lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2);
1024
1025/**
Radek Krejci22ebdba2019-07-25 13:59:43 +02001026 * @defgroup dupoptions Data duplication options
1027 * @ingroup datatree
1028 *
1029 * Various options to change lyd_dup() behavior.
1030 *
1031 * Default behavior:
1032 * - only the specified node is duplicated without siblings, parents, or children.
Michal Vasko3a41dff2020-07-15 14:30:28 +02001033 * - all the metadata of the duplicated nodes are also duplicated.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001034 * @{
1035 */
1036
1037#define LYD_DUP_RECURSIVE 0x01 /**< Duplicate not just the node but also all the children. Note that
1038 list's keys are always duplicated. */
Michal Vaskoa29a5762020-06-23 13:28:49 +02001039#define LYD_DUP_NO_META 0x02 /**< Do not duplicate metadata of any node. */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001040#define LYD_DUP_WITH_PARENTS 0x04 /**< If a nested node is being duplicated, duplicate also all the parents.
1041 Keys are also duplicated for lists. Return value does not change! */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001042#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 +02001043 its validation/default node state. */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001044
1045/** @} dupoptions */
1046
1047/**
1048 * @brief Create a copy of the specified data tree \p node. Schema references are kept the same.
1049 *
Radek Krejci22ebdba2019-07-25 13:59:43 +02001050 * @param[in] node Data tree node to be duplicated.
1051 * @param[in] parent Optional parent node where to connect the duplicated node(s).
Michal Vasko3a41dff2020-07-15 14:30:28 +02001052 * If set in combination with LYD_DUP_WITH_PARENTS, the parents chain is duplicated until it comes to and connects with
1053 * the @p parent.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001054 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vasko3a41dff2020-07-15 14:30:28 +02001055 * @param[out] dup Optional created copy of the node. Note that in case the parents chain is duplicated for the duplicated
1056 * node(s) (when LYD_DUP_WITH_PARENTS used), the first duplicated node is still returned.
1057 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001058 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02001059LY_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 +02001060
1061/**
1062 * @brief Create a copy of the specified data tree \p node with any following siblings. Schema references are kept the same.
1063 *
1064 * @param[in] node Data tree node to be duplicated.
1065 * @param[in] parent Optional parent node where to connect the duplicated node(s).
1066 * If set in combination with LYD_DUP_WITH_PARENTS, the parents chain is duplicated until it comes to and connects with
1067 * the @p parent.
1068 * @param[in] options Bitmask of options flags, see @ref dupoptions.
1069 * @param[out] dup Optional created copy of the node. Note that in case the parents chain is duplicated for the duplicated
1070 * node(s) (when LYD_DUP_WITH_PARENTS used), the first duplicated node is still returned.
1071 * @return LY_ERR value.
1072 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02001073LY_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 +02001074
1075/**
Michal Vasko25a32822020-07-09 15:48:22 +02001076 * @brief Create a copy of the metadata.
1077 *
1078 * @param[in] meta Metadata to copy.
Michal Vasko3a41dff2020-07-15 14:30:28 +02001079 * @param[in] parent Node where to append the new metadata.
1080 * @param[out] dup Optional created metadata copy.
1081 * @return LY_ERR value.
Michal Vasko25a32822020-07-09 15:48:22 +02001082 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001083LY_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 +02001084
1085/**
Michal Vasko4490d312020-06-16 13:08:55 +02001086 * @defgroup mergeoptions Data merge options.
1087 * @ingroup datatree
Radek Krejci576b23f2019-07-12 14:06:32 +02001088 *
Michal Vasko4490d312020-06-16 13:08:55 +02001089 * Various options to change lyd_merge() behavior.
1090 *
1091 * Default behavior:
1092 * - source data tree is not modified in any way,
Michal Vasko3a41dff2020-07-15 14:30:28 +02001093 * - any default nodes in the source are ignored if there are explicit nodes in the target.
Michal Vasko4490d312020-06-16 13:08:55 +02001094 * @{
1095 */
1096
1097#define LYD_MERGE_DESTRUCT 0x01 /**< Spend source data tree in the function, it cannot be used afterwards! */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001098#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 +02001099
1100/** @} mergeoptions */
1101
1102/**
Michal Vasko3a41dff2020-07-15 14:30:28 +02001103 * @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 +02001104 * is called on the resulting data tree (data from more cases may be present, default and non-default values).
1105 *
1106 * @param[in,out] target Target data tree to merge into, must be a top-level tree.
1107 * @param[in] source Source data tree to merge, must be a top-level tree.
1108 * @param[in] options Bitmask of option flags, see @ref mergeoptions.
1109 * @return LY_SUCCESS on success,
1110 * @return LY_ERR value on error.
1111 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02001112LY_ERR lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, uint16_t options);
Michal Vasko3a41dff2020-07-15 14:30:28 +02001113
1114/**
1115 * @brief Merge the source data tree with any following siblings into the target data tree. Merge may not be
1116 * complete until validation called on the resulting data tree (data from more cases may be present, default
1117 * and non-default values).
1118 *
1119 * @param[in,out] target Target data tree to merge into, must be a top-level tree.
1120 * @param[in] source Source data tree to merge, must be a top-level tree.
1121 * @param[in] options Bitmask of option flags, see @ref mergeoptions.
1122 * @return LY_SUCCESS on success,
1123 * @return LY_ERR value on error.
1124 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02001125LY_ERR lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, uint16_t options);
Michal Vasko4490d312020-06-16 13:08:55 +02001126
1127/**
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001128 * @defgroup diffoptions Data diff options.
1129 * @ingroup datatree
1130 *
1131 * Various options to change lyd_diff() behavior.
1132 *
1133 * Default behavior:
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001134 * - any default nodes are treated as non-existent and ignored.
1135 * @{
1136 */
1137
Michal Vasko3a41dff2020-07-15 14:30:28 +02001138#define LYD_DIFF_DEFAULTS 0x01 /**< Default nodes in the trees are not ignored but treated similarly to explicit
1139 nodes. Also, leaves and leaf-lists are added into diff even in case only their
1140 default flag (state) was changed. */
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001141
1142/** @} diffoptions */
1143
1144/**
1145 * @brief Learn the differences between 2 data trees.
1146 *
1147 * The resulting diff is represented as a data tree with specific metadata from the internal 'yang'
1148 * module. Most importantly, every node has an effective 'operation' metadata. If there is none
1149 * defined on the node, it inherits the operation from the nearest parent. Top-level nodes must
1150 * always have the 'operation' metadata defined. Additional metadata ('orig-default', 'value',
1151 * 'orig-value', 'key', 'orig-key') are used for storing more information about the value in the first
1152 * or the second tree.
1153 *
1154 * The diff tree is completely independent on the @p first and @p second trees, meaning all
1155 * the information about the change is stored in the diff and the trees are not needed.
1156 *
1157 * !! Caution !!
1158 * The diff tree should never be validated because it may easily not be valid! For example,
1159 * when data from one case branch are deleted and data from another branch created - data from both
1160 * branches are then stored in the diff tree simultaneously.
1161 *
1162 * @param[in] first First data tree.
1163 * @param[in] second Second data tree.
1164 * @param[in] options Bitmask of options flags, see @ref diffoptions.
1165 * @param[out] diff Generated diff.
1166 * @return LY_SUCCESS on success,
1167 * @return LY_ERR on error.
1168 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02001169LY_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 +02001170
1171/**
1172 * @brief Learn the differences between 2 data trees including all the following siblings.
1173 *
1174 * @param[in] first First data tree.
1175 * @param[in] second Second data tree.
1176 * @param[in] options Bitmask of options flags, see @ref diffoptions.
1177 * @param[out] diff Generated diff.
1178 * @return LY_SUCCESS on success,
1179 * @return LY_ERR on error.
1180 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02001181LY_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 +02001182
1183/**
1184 * @brief Callback for diff nodes.
1185 *
1186 * @param[in] diff_node Diff node.
1187 * @param[in] data_node Matching node in data.
1188 * @param[in] cb_data Arbitrary callback data.
1189 * @return LY_ERR value.
1190 */
1191typedef LY_ERR (*lyd_diff_cb)(const struct lyd_node *diff_node, struct lyd_node *data_node, void *cb_data);
1192
1193/**
Michal Vasko3a41dff2020-07-15 14:30:28 +02001194 * @brief Apply the whole diff on a data tree but restrict the operation to one module.
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001195 *
1196 * @param[in,out] data Data to apply the diff on.
1197 * @param[in] diff Diff to apply.
1198 * @param[in] mod Module, whose diff/data only to consider, NULL for all modules.
1199 * @param[in] diff_cb Optional diff callback that will be called for every changed node.
1200 * @param[in] cb_data Arbitrary callback data.
1201 * @return LY_SUCCESS on success,
1202 * @return LY_ERR on error.
1203 */
1204LY_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 +02001205 lyd_diff_cb diff_cb, void *cb_data);
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001206
1207/**
Michal Vasko3a41dff2020-07-15 14:30:28 +02001208 * @brief Apply the whole diff tree on a data tree.
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001209 *
1210 * @param[in,out] data Data to apply the diff on.
1211 * @param[in] diff Diff to apply.
1212 * @return LY_SUCCESS on success,
1213 * @return LY_ERR on error.
1214 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001215LY_ERR lyd_diff_apply_all(struct lyd_node **data, const struct lyd_node *diff);
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001216
1217/**
Michal Vaskoe6323f62020-07-09 15:49:02 +02001218 * @brief Merge 2 diffs into each other but restrict the operation to one module.
1219 *
1220 * The diffs must be possible to be merged, which is guaranteed only if the source diff was
1221 * created on data that had the target diff applied on them. In other words, this sequence is legal
1222 *
Michal Vasko04f85912020-08-07 12:14:58 +02001223 * 1) diff1 from data1 and data2 -> data11 from apply diff1 on data1 -> diff2 from data11 and data3 ->
1224 * -> data 33 from apply diff2 on data1
Michal Vaskoe6323f62020-07-09 15:49:02 +02001225 *
1226 * and reusing these diffs
1227 *
Michal Vasko04f85912020-08-07 12:14:58 +02001228 * 2) diff11 from merge diff1 and diff2 -> data33 from apply diff11 on data1
Michal Vaskoe6323f62020-07-09 15:49:02 +02001229 *
Michal Vaskofb737aa2020-08-06 13:53:53 +02001230 * @param[in,out] diff Target diff to merge into.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001231 * @param[in] src_diff Source diff.
1232 * @param[in] mod Module, whose diff only to consider, NULL for all modules.
1233 * @param[in] diff_cb Optional diff callback that will be called for every changed node.
1234 * @param[in] cb_data Arbitrary callback data.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001235 * @return LY_SUCCESS on success,
1236 * @return LY_ERR on error.
1237 */
Michal Vaskofb737aa2020-08-06 13:53:53 +02001238LY_ERR lyd_diff_merge_module(struct lyd_node **diff, const struct lyd_node *src_diff, const struct lys_module *mod,
Radek Krejci0f969882020-08-21 16:56:47 +02001239 lyd_diff_cb diff_cb, void *cb_data);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001240
1241/**
Michal Vasko04f85912020-08-07 12:14:58 +02001242 * @brief Merge 2 diff trees into each other.
1243 *
1244 * @param[in,out] diff_first Target diff first sibling to merge into.
1245 * @param[in] diff_parent Target diff parent to merge into.
1246 * @param[in] src_sibling Source diff sibling to merge.
1247 * @param[in] diff_cb Optional diff callback that will be called for every changed node.
1248 * @param[in] cb_data Arbitrary callback data.
1249 * @return LY_SUCCESS on success,
1250 * @return LY_ERR on error.
1251 */
1252LY_ERR lyd_diff_merge_tree(struct lyd_node **diff_first, struct lyd_node *diff_parent, const struct lyd_node *src_sibling,
Radek Krejci0f969882020-08-21 16:56:47 +02001253 lyd_diff_cb diff_cb, void *cb_data);
Michal Vasko04f85912020-08-07 12:14:58 +02001254
1255/**
Michal Vaskoe6323f62020-07-09 15:49:02 +02001256 * @brief Merge 2 diffs into each other.
1257 *
Michal Vaskoe6323f62020-07-09 15:49:02 +02001258 * @param[in,out] diff Target diff to merge into.
Michal Vaskofb737aa2020-08-06 13:53:53 +02001259 * @param[in] src_diff Source diff.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001260 * @return LY_SUCCESS on success,
1261 * @return LY_ERR on error.
1262 */
Michal Vaskofb737aa2020-08-06 13:53:53 +02001263LY_ERR lyd_diff_merge_all(struct lyd_node **diff, const struct lyd_node *src_diff);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001264
1265/**
Michal Vasko4231fb62020-07-13 13:54:47 +02001266 * @brief Reverse a diff and make the opposite changes. Meaning change create to delete, delete to create,
1267 * or move from place A to B to move from B to A and so on.
1268 *
1269 * @param[in] src_diff Diff to reverse.
1270 * @param[out] diff Reversed diff.
1271 * @return LY_SUCCESS on success.
1272 * @return LY_ERR on error.
1273 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001274LY_ERR lyd_diff_reverse_all(const struct lyd_node *src_diff, struct lyd_node **diff);
Michal Vasko4231fb62020-07-13 13:54:47 +02001275
1276/**
Michal Vasko4490d312020-06-16 13:08:55 +02001277 * @brief Find the target in data of a compiled ly_path structure (instance-identifier).
1278 *
1279 * @param[in] path Compiled path structure.
Michal Vaskof03ed032020-03-04 13:31:44 +01001280 * @param[in] tree Data tree to be searched.
Michal Vasko4490d312020-06-16 13:08:55 +02001281 * @return Found target node,
1282 * @return NULL if not found.
Radek Krejci576b23f2019-07-12 14:06:32 +02001283 */
Michal Vasko004d3152020-06-11 19:59:22 +02001284const struct lyd_node_term *lyd_target(const struct ly_path *path, const struct lyd_node *tree);
Radek Krejci084289f2019-07-09 17:35:30 +02001285
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001286/**
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001287 * @brief Types of the different data paths.
1288 */
1289typedef enum {
Michal Vasko14654712020-02-06 08:35:21 +01001290 LYD_PATH_LOG, /**< Descriptive path format used in log messages */
Michal Vasko69730152020-10-09 16:30:07 +02001291 LYD_PATH_LOG_NO_LAST_PRED /**< Similar to ::LYD_PATH_LOG except there is never a predicate on the last node */
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001292} LYD_PATH_TYPE;
1293
1294/**
1295 * @brief Generate path of the given node in the requested format.
1296 *
1297 * @param[in] node Schema path of this node will be generated.
1298 * @param[in] pathtype Format of the path to generate.
1299 * @param[in,out] buffer Prepared buffer of the @p buflen length to store the generated path.
1300 * If NULL, memory for the complete path is allocated.
1301 * @param[in] buflen Size of the provided @p buffer.
1302 * @return NULL in case of memory allocation error, path of the node otherwise.
1303 * In case the @p buffer is NULL, the returned string is dynamically allocated and caller is responsible to free it.
1304 */
1305char *lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen);
1306
Michal Vaskoe444f752020-02-10 12:20:06 +01001307/**
Michal Vasko25a32822020-07-09 15:48:22 +02001308 * @brief Find a specific metadata.
1309 *
1310 * @param[in] first First metadata to consider.
1311 * @param[in] module Module of the metadata definition, may be NULL if @p name includes a prefix.
1312 * @param[in] name Name of the metadata to find, may not include a prefix (module name) if @p module is set.
1313 * @return Found metadata,
1314 * @return NULL if not found.
1315 */
1316struct lyd_meta *lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name);
1317
1318/**
Michal Vaskoda859032020-07-14 12:20:14 +02001319 * @brief Search in the given siblings (NOT recursively) for the first target instance with the same value.
Michal Vaskoe444f752020-02-10 12:20:06 +01001320 * Uses hashes - should be used whenever possible for best performance.
1321 *
1322 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
1323 * @param[in] target Target node to find.
Michal Vasko9b368d32020-02-14 13:53:31 +01001324 * @param[out] match Can be NULL, otherwise the found data node.
Michal Vaskoe444f752020-02-10 12:20:06 +01001325 * @return LY_SUCCESS on success, @p match set.
1326 * @return LY_ENOTFOUND if not found, @p match set to NULL.
1327 * @return LY_ERR value if another error occurred.
1328 */
1329LY_ERR lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match);
1330
1331/**
Michal Vaskoe444f752020-02-10 12:20:06 +01001332 * @brief Search in the given siblings for the first schema instance.
1333 * Uses hashes - should be used whenever possible for best performance.
1334 *
1335 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
1336 * @param[in] schema Schema node of the data node to find.
Michal Vaskob104f112020-07-17 09:54:54 +02001337 * @param[in] key_or_value If it is NULL, the first schema node data instance is found. For nodes with many
1338 * instances, it can be set based on the type of @p schema:
Michal Vaskoe444f752020-02-10 12:20:06 +01001339 * LYS_LEAFLIST:
1340 * Searched instance value.
1341 * LYS_LIST:
Michal Vasko90932a92020-02-12 14:33:03 +01001342 * Searched instance key values in the form of "[key1='val1'][key2='val2']...".
1343 * The keys do not have to be ordered but all of them must be set.
1344 *
1345 * Note that any explicit values (leaf-list or list key values) will be canonized first
1346 * before comparison. But values that do not have a canonical value are expected to be in the
1347 * JSON format!
Michal Vaskof03ed032020-03-04 13:31:44 +01001348 * @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 +01001349 * @param[out] match Can be NULL, otherwise the found data node.
Michal Vaskoe444f752020-02-10 12:20:06 +01001350 * @return LY_SUCCESS on success, @p match set.
1351 * @return LY_ENOTFOUND if not found, @p match set to NULL.
1352 * @return LY_EINVAL if @p schema is a key-less list.
1353 * @return LY_ERR value if another error occurred.
1354 */
1355LY_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 +02001356 size_t val_len, struct lyd_node **match);
Michal Vaskoe444f752020-02-10 12:20:06 +01001357
Michal Vaskoccc02342020-05-21 10:09:21 +02001358/**
1359 * @brief Search in the given data for instances of nodes matching the provided XPath.
1360 *
Michal Vaskob104f112020-07-17 09:54:54 +02001361 * The expected format of the expression is ::LYD_JSON, meaning the first node in every path
Michal Vasko61ac2f62020-05-25 12:39:51 +02001362 * must have its module name as prefix or be the special `*` value for all the nodes.
1363 *
Michal Vasko19a30602020-05-25 10:40:19 +02001364 * If a list instance is being selected with all its key values specified (but not necessarily ordered)
1365 * in the form `list[key1='val1'][key2='val2'][key3='val3']` or a leaf-list instance in the form
1366 * `leaf-list[.='val']`, these instances are found using hashes with constant (*O(1)*) complexity
1367 * (unless they are defined in top-level). Other predicates can still follow the aforementioned ones.
1368 *
Michal Vaskoccc02342020-05-21 10:09:21 +02001369 * @param[in] ctx_node XPath context node.
1370 * @param[in] xpath Data XPath expression filtering the matching nodes. ::LYD_JSON format is expected.
1371 * @param[out] set Set of found data nodes. In case the result is a number, a string, or a boolean,
1372 * the returned set is empty.
1373 * @return LY_SUCCESS on success, @p set is returned.
1374 * @return LY_ERR value if an error occurred.
1375 */
1376LY_ERR lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set);
1377
Radek Krejcie7b95092019-05-15 11:03:07 +02001378#ifdef __cplusplus
1379}
1380#endif
1381
1382#endif /* LY_TREE_DATA_H_ */