blob: 24ab7e756f06122c3388e44eec8fd1decc1d5746 [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
43/**
44 * @brief Macro to iterate via all elements in a data tree. This is the opening part
45 * to the #LYD_TREE_DFS_END - they always have to be used together.
46 *
47 * The function follows deep-first search algorithm:
48 * <pre>
49 * 1
50 * / \
Michal Vaskoc193ce92020-03-06 11:04:48 +010051 * 2 4
Radek Krejcie7b95092019-05-15 11:03:07 +020052 * / / \
Michal Vaskoc193ce92020-03-06 11:04:48 +010053 * 3 5 6
Radek Krejcie7b95092019-05-15 11:03:07 +020054 * </pre>
55 *
Radek Krejci0935f412019-08-20 16:15:18 +020056 * Use the same parameters for #LYD_TREE_DFS_BEGIN and #LYD_TREE_DFS_END. While
Michal Vasko56daf732020-08-10 10:57:18 +020057 * START can be any of the lyd_node* types, ELEM variable must be a pointer to
58 * the generic struct lyd_node.
Radek Krejcie7b95092019-05-15 11:03:07 +020059 *
Michal Vasko56daf732020-08-10 10:57:18 +020060 * To skip a particular subtree, instead of the continue statement, set LYD_TREE_DFS_continue
61 * variable to non-zero value.
Radek Krejcie7b95092019-05-15 11:03:07 +020062 *
63 * Use with opening curly bracket '{' after the macro.
64 *
65 * @param START Pointer to the starting element processed first.
Radek Krejcie7b95092019-05-15 11:03:07 +020066 * @param ELEM Iterator intended for use in the block.
67 */
Michal Vasko56daf732020-08-10 10:57:18 +020068#define LYD_TREE_DFS_BEGIN(START, ELEM) \
69 { int LYD_TREE_DFS_continue = 0; struct lyd_node *LYD_TREE_DFS_next; \
70 for ((ELEM) = (LYD_TREE_DFS_next) = (struct lyd_node *)(START); \
Radek Krejcie7b95092019-05-15 11:03:07 +020071 (ELEM); \
Michal Vasko56daf732020-08-10 10:57:18 +020072 (ELEM) = (LYD_TREE_DFS_next), LYD_TREE_DFS_continue = 0)
Radek Krejcie7b95092019-05-15 11:03:07 +020073
74/**
75 * @brief Macro to iterate via all elements in a tree. This is the closing part
76 * to the #LYD_TREE_DFS_BEGIN - they always have to be used together.
77 *
78 * Use the same parameters for #LYD_TREE_DFS_BEGIN and #LYD_TREE_DFS_END. While
Michal Vasko56daf732020-08-10 10:57:18 +020079 * START can be any of the lyd_node* types, ELEM variable must be a pointer
80 * to the generic struct lyd_node.
Radek Krejcie7b95092019-05-15 11:03:07 +020081 *
82 * Use with closing curly bracket '}' after the macro.
83 *
84 * @param START Pointer to the starting element processed first.
Radek Krejcie7b95092019-05-15 11:03:07 +020085 * @param ELEM Iterator intended for use in the block.
86 */
87
Michal Vasko56daf732020-08-10 10:57:18 +020088#define LYD_TREE_DFS_END(START, ELEM) \
Radek Krejcie7b95092019-05-15 11:03:07 +020089 /* select element for the next run - children first */ \
Michal Vasko56daf732020-08-10 10:57:18 +020090 if (LYD_TREE_DFS_continue) { \
91 (LYD_TREE_DFS_next) = NULL; \
92 } else { \
93 (LYD_TREE_DFS_next) = lyd_node_children(ELEM, 0); \
94 }\
95 if (!(LYD_TREE_DFS_next)) { \
Radek Krejcie7b95092019-05-15 11:03:07 +020096 /* no children */ \
97 if ((ELEM) == (struct lyd_node*)(START)) { \
98 /* we are done, (START) has no children */ \
99 break; \
100 } \
101 /* try siblings */ \
Michal Vasko56daf732020-08-10 10:57:18 +0200102 (LYD_TREE_DFS_next) = (ELEM)->next; \
Radek Krejcie7b95092019-05-15 11:03:07 +0200103 } \
Michal Vasko56daf732020-08-10 10:57:18 +0200104 while (!(LYD_TREE_DFS_next)) { \
Radek Krejcie7b95092019-05-15 11:03:07 +0200105 /* parent is already processed, go to its sibling */ \
106 (ELEM) = (struct lyd_node*)(ELEM)->parent; \
107 /* no siblings, go back through parents */ \
108 if ((ELEM)->parent == (START)->parent) { \
109 /* we are done, no next element to process */ \
110 break; \
111 } \
Michal Vasko56daf732020-08-10 10:57:18 +0200112 (LYD_TREE_DFS_next) = (ELEM)->next; \
113 } } \
Radek Krejcie7b95092019-05-15 11:03:07 +0200114
115/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200116 * @brief Macro to get context from a data tree node.
117 */
Michal Vasko52927e22020-03-16 17:26:14 +0100118#define LYD_NODE_CTX(node) ((node)->schema ? (node)->schema->module->ctx : ((struct lyd_node_opaq *)(node))->ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200119
120/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200121 * @brief Data input/output formats supported by libyang [parser](@ref howtodataparsers) and
Radek Krejcid91d4da2020-05-18 11:28:02 +0200122 * [printer](@ref howtodataprinters) functions. Also used for value prefix format (TODO link to prefix formats descriptions).
Radek Krejcie7b95092019-05-15 11:03:07 +0200123 */
124typedef enum {
Michal Vasko52927e22020-03-16 17:26:14 +0100125 LYD_SCHEMA = 0, /**< invalid instance data format, value prefixes map to YANG import prefixes */
126 LYD_XML, /**< XML instance data format, value prefixes map to XML namespace prefixes */
127 LYD_JSON, /**< JSON instance data format, value prefixes map to module names */
Michal Vasko60ea6352020-06-29 13:39:39 +0200128 LYD_LYB, /**< LYB instance data format, invalid value prefix format (same as LYD_JSON) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200129} LYD_FORMAT;
130
131/**
Radek Krejci59583bb2019-09-11 12:57:55 +0200132 * @brief List of possible value types stored in ::lyd_node_any.
Radek Krejcie7b95092019-05-15 11:03:07 +0200133 */
134typedef enum {
Radek Krejci22ebdba2019-07-25 13:59:43 +0200135 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 +0200136 is directly connected into the anydata node without duplication, caller is supposed to not manipulate
137 with the data after a successful call (including calling lyd_free() on the provided data) */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200138 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 +0200139 as string). XML sensitive characters (such as & or \>) are automatically escaped when the anydata
140 is printed in XML format. */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200141 LYD_ANYDATA_XML, /**< Value is a string containing the serialized XML data. */
142 LYD_ANYDATA_JSON, /**< Value is a string containing the data modeled by YANG and encoded as I-JSON. */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200143 LYD_ANYDATA_LYB, /**< Value is a memory chunk with the serialized data tree in LYB format. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200144} LYD_ANYDATA_VALUETYPE;
145
146/** @} */
147
148/**
149 * @brief YANG data representation
150 */
151struct lyd_value {
Radek Krejci950f6a52019-09-12 17:15:32 +0200152 const char *original; /**< Original string representation of the value. It is never NULL, but (canonical) string representation
153 of the value should be always obtained via the type's printer callback (lyd_value::realtype::plugin::print). */
Radek Krejcie7b95092019-05-15 11:03:07 +0200154 union {
Radek Krejcie7b95092019-05-15 11:03:07 +0200155 int8_t boolean; /**< 0 as false, 1 as true */
156 int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200157 int8_t int8; /**< 8-bit signed integer */
158 int16_t int16; /**< 16-bit signed integer */
159 int32_t int32; /**< 32-bit signed integer */
160 int64_t int64; /**< 64-bit signed integer */
161 uint8_t uint8; /**< 8-bit unsigned integer */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200162 uint16_t uint16; /**< 16-bit unsigned integer */
163 uint32_t uint32; /**< 32-bit unsigned integer */
164 uint64_t uint64; /**< 64-bit unsigned integer */
Radek Krejcie7b95092019-05-15 11:03:07 +0200165 struct lysc_type_bitenum_item *enum_item; /**< pointer to the definition of the enumeration value */
Radek Krejci849a62a2019-05-22 15:29:05 +0200166 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 +0200167 struct lysc_ident *ident; /**< pointer to the schema definition of the identityref value */
Radek Krejciefbb3922019-07-15 12:58:00 +0200168
169 struct lyd_value_subvalue {
170 struct lyd_value_prefix {
171 const char *prefix; /**< prefix string used in the canonized string to identify the mod of the YANG schema */
172 const struct lys_module *mod; /**< YANG schema module identified by the prefix string */
173 } *prefixes; /**< list of mappings between prefix in canonized value to a YANG schema ([sized array](@ref sizedarrays)) */
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200174 struct lyd_value *value; /**< representation of the value according to the selected union's subtype (stored as lyd_value::realpath
175 here, in subvalue structure */
176 } *subvalue; /**< data to represent data with multiple types (union). Original value is stored in the main
Michal Vasko9b368d32020-02-14 13:53:31 +0100177 lyd_value:canonical_cache while the lyd_value_subvalue::value contains representation according to the
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200178 one of the union's type. The lyd_value_subvalue:prefixes provides (possible) mappings from prefixes
179 in original value to YANG modules. These prefixes are necessary to parse original value to the union's
180 subtypes. */
Radek Krejci084289f2019-07-09 17:35:30 +0200181
Michal Vasko004d3152020-06-11 19:59:22 +0200182 struct ly_path *target; /**< Instance-identifier's target path. */
Radek Krejci084289f2019-07-09 17:35:30 +0200183
Radek Krejcie7b95092019-05-15 11:03:07 +0200184 void *ptr; /**< generic data type structure used to store the data */
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200185 }; /**< The union is just a list of shorthands to possible values stored by a type's plugin. libyang itself uses the lyd_value::realtype
186 plugin's callbacks to work with the data. */
Radek Krejci084289f2019-07-09 17:35:30 +0200187
Radek Krejci950f6a52019-09-12 17:15:32 +0200188 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 Krejci62903c32019-07-15 14:42:05 +0200189 in the schema node of the data node since the type's store plugin can use other types/plugins for
190 storing data. Speaking about built-in types, this is the case of leafref which stores data as its
191 target type. In contrast, union type also use its subtype's callbacks, but inside an internal data
192 lyd_value::subvalue structure, so here is the pointer to the union type.
193 In general, this type is used to get free callback for this lyd_value structure, so it must reflect
194 the type used to store data directly in the same lyd_value instance. */
Radek Krejci950f6a52019-09-12 17:15:32 +0200195 void *canonical_cache; /**< Generic cache for type plugins to store data necessary to print canonical value. It can be the canonical
196 value itself or anything else useful to print the canonical form of the value. Plugin is responsible for
197 freeing the cache in its free callback. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200198};
199
200/**
Michal Vasko9f96a052020-03-10 09:41:45 +0100201 * @brief Metadata structure.
Radek Krejcie7b95092019-05-15 11:03:07 +0200202 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100203 * The structure provides information about metadata of a data element. Such attributes must map to
Radek Krejcie7b95092019-05-15 11:03:07 +0200204 * annotations as specified in RFC 7952. The only exception is the filter type (in NETCONF get operations)
205 * and edit-config's operation attributes. In XML, they are represented as standard XML attributes. In JSON,
206 * they are represented as JSON elements starting with the '@' character (for more information, see the
207 * YANG metadata RFC.
208 *
209 */
Michal Vasko9f96a052020-03-10 09:41:45 +0100210struct lyd_meta {
211 struct lyd_node *parent; /**< data node where the metadata is placed */
212 struct lyd_meta *next; /**< pointer to the next metadata of the same element */
213 struct lysc_ext_instance *annotation; /**< pointer to the annotation's definition */
214 const char *name; /**< metadata name */
215 struct lyd_value value; /**< metadata value representation */
Radek Krejcie7b95092019-05-15 11:03:07 +0200216};
217
Michal Vasko52927e22020-03-16 17:26:14 +0100218/**
219 * @brief Generic prefix and namespace mapping, meaning depends on the format.
220 */
221struct ly_prefix {
222 const char *pref;
223 const char *ns;
224};
225
226/**
227 * @brief Generic attribute structure.
228 */
229struct ly_attr {
230 struct lyd_node_opaq *parent; /**< data node where the attribute is placed */
231 struct ly_attr *next;
232 struct ly_prefix *val_prefs; /**< list of prefixes in the value ([sized array](@ref sizedarrays)) */
233 const char *name;
234 const char *value;
235
236 LYD_FORMAT format;
237 struct ly_prefix prefix; /**< name prefix, it is stored because they are a real pain to generate properly */
238
239};
Radek Krejcie7b95092019-05-15 11:03:07 +0200240
Michal Vasko1bf09392020-03-27 12:38:10 +0100241#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 +0200242#define LYD_NODE_TERM (LYS_LEAF|LYS_LEAFLIST) /**< Schema nodetype mask for lyd_node_term */
243#define LYD_NODE_ANY (LYS_ANYDATA) /**< Schema nodetype mask for lyd_node_any */
244
245/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100246 * @defgroup dnodeflags Data node flags
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200247 * @ingroup datatree
248 * @{
249 *
250 * Various flags of data nodes.
251 *
252 * 1 - container 5 - anydata/anyxml
253 * 2 - list 6 - rpc/action
254 * 3 - leaf 7 - notification
255 * 4 - leaflist
256 *
257 * bit name 1 2 3 4 5 6 7
258 * ---------------------+-+-+-+-+-+-+-+
259 * 1 LYD_DEFAULT |x| |x|x| | | |
260 * +-+-+-+-+-+-+-+
Michal Vasko5c4e5892019-11-14 12:31:38 +0100261 * 2 LYD_WHEN_TRUE |x|x|x|x|x| | |
Michal Vasko9b368d32020-02-14 13:53:31 +0100262 * +-+-+-+-+-+-+-+
263 * 3 LYD_NEW |x|x|x|x|x|x|x|
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200264 * ---------------------+-+-+-+-+-+-+-+
265 *
266 */
267
Michal Vasko5c4e5892019-11-14 12:31:38 +0100268#define LYD_DEFAULT 0x01 /**< default (implicit) node */
269#define LYD_WHEN_TRUE 0x02 /**< all when conditions of this node were evaluated to true */
Michal Vasko9b368d32020-02-14 13:53:31 +0100270#define LYD_NEW 0x04 /**< node was created after the last validation, is needed for the next validation */
Michal Vasko52927e22020-03-16 17:26:14 +0100271
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200272/** @} */
273
274/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200275 * @brief Generic structure for a data node.
276 */
277struct lyd_node {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200278 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
279 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
280 used to get know that nodes are not equal, it cannot be used to decide that the
281 nodes are equal due to possible collisions. */
282 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Michal Vaskoecd62de2019-11-13 12:35:11 +0100283 const struct lysc_node *schema; /**< pointer to the schema definition of this node, note that the target can be not just
284 ::struct lysc_node but ::struct lysc_action or ::struct lysc_notif as well */
Radek Krejcie7b95092019-05-15 11:03:07 +0200285 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
286 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
287 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
288 never NULL. If there is no sibling node, pointer points to the node
289 itself. In case of the first node, this pointer points to the last
290 node in the list. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100291 struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200292
293#ifdef LY_ENABLED_LYD_PRIV
294 void *priv; /**< private user data, not used by libyang */
295#endif
296};
297
298/**
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200299 * @brief Data node structure for the inner data tree nodes - containers, lists, RPCs, actions and Notifications.
Radek Krejcie7b95092019-05-15 11:03:07 +0200300 */
301struct lyd_node_inner {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200302 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
303 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
304 used to get know that nodes are not equal, it cannot be used to decide that the
305 nodes are equal due to possible collisions. */
306 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200307 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
308 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
309 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
310 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
311 never NULL. If there is no sibling node, pointer points to the node
312 itself. In case of the first node, this pointer points to the last
313 node in the list. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100314 struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200315
316#ifdef LY_ENABLED_LYD_PRIV
317 void *priv; /**< private user data, not used by libyang */
318#endif
319
320 struct lyd_node *child; /**< pointer to the first child node. */
321 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 +0200322#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 +0200323};
324
325/**
Michal Vaskof03ed032020-03-04 13:31:44 +0100326 * @brief Data node structure for the terminal data tree nodes - leaves and leaf-lists.
Radek Krejcie7b95092019-05-15 11:03:07 +0200327 */
328struct lyd_node_term {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200329 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
330 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
331 used to get know that nodes are not equal, it cannot be used to decide that the
332 nodes are equal due to possible collisions. */
333 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200334 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
335 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
336 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
337 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
338 never NULL. If there is no sibling node, pointer points to the node
339 itself. In case of the first node, this pointer points to the last
340 node in the list. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100341 struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200342
343#ifdef LY_ENABLED_LYD_PRIV
344 void *priv; /**< private user data, not used by libyang */
345#endif
346
347 struct lyd_value value; /**< node's value representation */
348};
349
350/**
351 * @brief Data node structure for the anydata data tree nodes - anydatas and anyxmls.
352 */
353struct lyd_node_any {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200354 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
355 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
356 used to get know that nodes are not equal, it cannot be used to decide that the
357 nodes are equal due to possible collisions. */
358 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200359 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
360 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
361 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
362 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
363 never NULL. If there is no sibling node, pointer points to the node
364 itself. In case of the first node, this pointer points to the last
365 node in the list. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100366 struct lyd_meta *meta; /**< pointer to the list of attributes of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200367
368#ifdef LY_ENABLED_LYD_PRIV
369 void *priv; /**< private user data, not used by libyang */
370#endif
371
Michal Vasko00cbf532020-06-15 13:58:47 +0200372 union lyd_any_value {
Radek Krejciee4cab22019-07-17 17:07:47 +0200373 struct lyd_node *tree; /**< data tree */
374 const char *str; /**< Generic string data */
375 const char *xml; /**< Serialized XML data */
376 const char *json; /**< I-JSON encoded string */
377 char *mem; /**< LYD_ANYDATA_LYB memory chunk */
378 } value; /**< pointer to the stored value representation of the anydata/anyxml node */
379 LYD_ANYDATA_VALUETYPE value_type;/**< type of the data stored as lyd_node_any::value */
Radek Krejcie7b95092019-05-15 11:03:07 +0200380};
381
382/**
Michal Vasko52927e22020-03-16 17:26:14 +0100383 * @brief Data node structure for unparsed (opaque) nodes.
384 */
385struct lyd_node_opaq {
386 uint32_t hash; /**< always 0 */
387 uint32_t flags; /**< always 0 */
388 const struct lysc_node *schema; /**< always NULL */
389 struct lyd_node *parent; /**< pointer to the parent node (NULL in case of root node) */
390 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
391 struct lyd_node *prev; /**< pointer to the previous sibling node (last sibling if there is none) */
392 struct ly_attr *attr;
393
394#ifdef LY_ENABLED_LYD_PRIV
395 void *priv; /**< private user data, not used by libyang */
396#endif
397
398 struct lyd_node *child; /**< pointer to the child node (NULL if there are none) */
399 const char *name;
400 LYD_FORMAT format;
401 struct ly_prefix prefix; /**< name prefix */
402 struct ly_prefix *val_prefs; /**< list of prefixes in the value ([sized array](@ref sizedarrays)) */
403 const char *value; /**< original value */
404 const struct ly_ctx *ctx; /**< libyang context */
405};
406
407/**
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200408 * @defgroup children_options Children traversal options.
409 * @ingroup datatree
410 */
411
412#define LYD_CHILDREN_SKIP_KEYS 0x01 /**< If list children are returned, skip its keys. */
413
414/** @} children_options */
415
416/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200417 * @brief Get the node's children list if any.
418 *
419 * Decides the node's type and in case it has a children list, returns it.
420 * @param[in] node Node to check.
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200421 * @param[in] options Bitmask of options, see @ref
Radek Krejcie7b95092019-05-15 11:03:07 +0200422 * @return Pointer to the first child node (if any) of the \p node.
423 */
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200424struct lyd_node *lyd_node_children(const struct lyd_node *node, int options);
Radek Krejcie7b95092019-05-15 11:03:07 +0200425
426/**
Michal Vaskoc193ce92020-03-06 11:04:48 +0100427 * @brief Get the owner module of the data node. It is the module of the top-level schema node. Generally,
428 * in case of augments it is the target module, recursively, otherwise it is the module where the data node is defined.
429 *
430 * @param[in] node Data node to examine.
431 * @return Module owner of the node.
432 */
433const struct lys_module *lyd_owner_module(const struct lyd_node *node);
434
435/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200436 * @brief Learn the length of LYB data.
437 *
438 * @param[in] data LYB data to examine.
439 * @return Length of the LYB data chunk,
440 * @return -1 on error.
441 */
442int lyd_lyb_data_length(const char *data);
443
444/**
Michal Vaskoc0004272020-08-06 08:32:34 +0200445 * @brief Copy anydata value from one node to another. Target value is freed first.
446 *
447 * @param[in,out] trg Target node.
448 * @param[in] value Source value, may be NULL when the target value is only freed.
449 * @param[in] value_type Source value type.
450 * @return LY_ERR value.
451 */
452LY_ERR lyd_any_copy_value(struct lyd_node *trg, const union lyd_any_value *value, LYD_ANYDATA_VALUETYPE value_type);
453
454/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200455 * @brief Create a new inner node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +0100456 *
457 * @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 +0100458 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100459 * @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 +0200460 * @param[out] node Optional created node.
461 * @return LY_ERR value.
Michal Vasko013a8182020-03-03 10:46:53 +0100462 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200463LY_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 +0100464
465/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200466 * @brief Create a new list node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +0100467 *
468 * @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 +0100469 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100470 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200471 * @param[out] node Optional created node.
Michal Vasko013a8182020-03-03 10:46:53 +0100472 * @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 +0200473 * or identityref value, the JSON format is expected (module names instead of prefixes). No keys are expected for
474 * key-less lists.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200475 * @return LY_ERR value.
Michal Vasko013a8182020-03-03 10:46:53 +0100476 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200477LY_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 +0100478
479/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200480 * @brief Create a new list node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +0100481 *
482 * @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 +0100483 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100484 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
485 * @param[in] keys All key values predicate in the form of "[key1='val1'][key2='val2']...", they do not have to be ordered.
486 * 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 +0200487 * Use NULL or string of length 0 in case of key-less list.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200488 * @param[out] node Optional created node.
489 * @return LY_ERR value.
Michal Vasko013a8182020-03-03 10:46:53 +0100490 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200491LY_ERR lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
492 struct lyd_node **node);
Michal Vasko013a8182020-03-03 10:46:53 +0100493
494/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200495 * @brief Create a new term node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +0100496 *
497 * @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 +0100498 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100499 * @param[in] name Schema node name of the new data node. The node can be #LYS_LEAF or #LYS_LEAFLIST.
500 * @param[in] val_str String form of the value of the node being created. In case of an instance-identifier or identityref
501 * value, the JSON format is expected (module names instead of prefixes).
Michal Vasko3a41dff2020-07-15 14:30:28 +0200502 * @param[out] node Optional created node.
503 * @return LY_ERR value.
Michal Vasko013a8182020-03-03 10:46:53 +0100504 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200505LY_ERR lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
506 struct lyd_node **node);
Michal Vasko013a8182020-03-03 10:46:53 +0100507
508/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200509 * @brief Create a new any node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +0100510 *
511 * @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 +0100512 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100513 * @param[in] name Schema node name of the new data node. The node can be #LYS_ANYDATA or #LYS_ANYXML.
514 * @param[in] value Value to be directly assigned to the node. Expected type is determined by @p value_type.
515 * @param[in] value_type Type of the provided value in @p value.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200516 * @param[out] node Optional created node.
517 * @return LY_ERR value.
Michal Vasko013a8182020-03-03 10:46:53 +0100518 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200519LY_ERR lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
520 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node);
Michal Vasko013a8182020-03-03 10:46:53 +0100521
522/**
Michal Vaskod86997b2020-05-26 15:19:54 +0200523 * @brief Create new metadata for a data node.
524 *
525 * @param[in] parent Parent node for the metadata being created.
Michal Vasko00cbf532020-06-15 13:58:47 +0200526 * @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 +0200527 * @param[in] name Annotation name of the new metadata. It can include the annotation module as the prefix.
528 * 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 +0200529 * @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 +0200530 * value, the JSON format is expected (module names instead of prefixes).
Michal Vasko3a41dff2020-07-15 14:30:28 +0200531 * @param[out] meta Optional created metadata.
532 * @return LY_ERR value.
Michal Vaskod86997b2020-05-26 15:19:54 +0200533 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200534LY_ERR lyd_new_meta(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
535 struct lyd_meta **meta);
Michal Vaskod86997b2020-05-26 15:19:54 +0200536
537/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200538 * @brief Create a new opaque node in the data tree.
539 *
540 * @param[in] parent Parent node for the node beaing created. NULL in case of creating a top level element.
541 * @param[in] ctx libyang context. If NULL, @p parent context will be used.
542 * @param[in] name Node name.
543 * @param[in] value Node value, may be NULL.
544 * @param[in] module_name Node module name.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200545 * @param[out] node Optional created node.
546 * @return LY_ERR value.
Michal Vasko00cbf532020-06-15 13:58:47 +0200547 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200548LY_ERR lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
549 const char *module_name, struct lyd_node **node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200550
551/**
552 * @brief Create new attribute for an opaque data node.
553 *
554 * @param[in] parent Parent opaque node for the attribute being created.
555 * @param[in] module Module name of the attribute being created. There may be none.
556 * @param[in] name Attribute name. It can include the module name as the prefix.
557 * @param[in] val_str String value of the attribute. Is stored directly.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200558 * @param[out] attr Optional created attribute.
559 * @return LY_ERR value.
Michal Vasko00cbf532020-06-15 13:58:47 +0200560 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200561LY_ERR lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *val_str,
562 struct ly_attr **attr);
Michal Vasko00cbf532020-06-15 13:58:47 +0200563
564/**
565 * @defgroup pathoptions Data path creation options
566 * @ingroup datatree
567 *
568 * Various options to change lyd_new_path*() behavior.
569 *
570 * Default behavior:
571 * - if the target node already exists (and is not default), an error is returned.
572 * - the whole path to the target node is created (with any missing parents) if necessary.
573 * - RPC output schema children are completely ignored in all modules. Input is searched and nodes created normally.
574 * @{
575 */
576
577#define LYD_NEWOPT_UPDATE 0x01 /**< If the target node exists, is a leaf, and it is updated with a new value or its
578 default flag is changed, it is returned. If the target node exists and is not
579 a leaf or generally no change occurs in the @p parent tree, NULL is returned and
580 no error set. */
581#define LYD_NEWOPT_OUTPUT 0x02 /**< Changes the behavior to ignoring RPC/action input schema nodes and using only
582 output ones. */
583#define LYD_NEWOPT_OPAQ 0x04 /**< Enables the creation of opaque nodes with some specific rules. If the __last node__
584 in the path is not uniquely defined ((leaf-)list without a predicate) or has an
585 invalid value (leaf/leaf-list), it is created as opaque. */
586
587/** @} pathoptions */
588
589/**
590 * @brief Create a new node in the data tree based on a path. Cannot be used for anyxml/anydata nodes,
591 * for those use ::lyd_new_path_any.
592 *
593 * If @p path points to a list key and the list instance does not exist, the key value from the predicate is used
594 * and @p value is ignored. Also, if a leaf-list is being created and both a predicate is defined in @p path
595 * and @p value is set, the predicate is preferred.
596 *
597 * @param[in] parent Data parent to add to/modify, can be NULL.
598 * @param[in] ctx libyang context, must be set if @p parent is NULL.
599 * @param[in] path Path to create (TODO ref path).
600 * @param[in] value Value of the new leaf/leaf-list. For other node types, it is ignored.
601 * @param[in] options Bitmask of options, see @ref pathoptions.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200602 * @param[out] node Optional first created node.
603 * @return LY_ERR value.
Michal Vasko00cbf532020-06-15 13:58:47 +0200604 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200605LY_ERR lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value,
606 int options, struct lyd_node **node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200607
608/**
609 * @brief Create a new node in the data tree based on a path. All node types can be created.
610 *
611 * If @p path points to a list key and the list instance does not exist, the key value from the predicate is used
612 * and @p value is ignored. Also, if a leaf-list is being created and both a predicate is defined in @p path
613 * and @p value is set, the predicate is preferred.
614 *
615 * @param[in] parent Data parent to add to/modify, can be NULL.
616 * @param[in] ctx libyang context, must be set if @p parent is NULL.
617 * @param[in] path Path to create (TODO ref path).
618 * @param[in] value Value of the new leaf/leaf-list/anyxml/anydata. For other node types, it is ignored.
619 * @param[in] value_type Anyxml/anydata node @p value type.
620 * @param[in] options Bitmask of options, see @ref pathoptions.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200621 * @param[out] node Optional first created node.
622 * @return LY_ERR value.
Michal Vasko00cbf532020-06-15 13:58:47 +0200623 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200624LY_ERR lyd_new_path_any(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
625 LYD_ANYDATA_VALUETYPE value_type, int options, struct lyd_node **node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200626
627/**
628 * @brief Create a new node in the data tree based on a path. All node types can be created.
629 *
630 * If @p path points to a list key and the list instance does not exist, the key value from the predicate is used
631 * and @p value is ignored. Also, if a leaf-list is being created and both a predicate is defined in @p path
632 * and @p value is set, the predicate is preferred.
633 *
634 * @param[in] parent Data parent to add to/modify, can be NULL.
635 * @param[in] ctx libyang context, must be set if @p parent is NULL.
636 * @param[in] path Path to create (TODO ref path).
637 * @param[in] value Value of the new leaf/leaf-list/anyxml/anydata. For other node types, it is ignored.
638 * @param[in] value_type Anyxml/anydata node @p value type.
639 * @param[in] options Bitmask of options, see @ref pathoptions.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200640 * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node.
641 * @param[out] new_node Optional last node created.
Michal Vasko00cbf532020-06-15 13:58:47 +0200642 * @return LY_ERR value.
643 */
644LY_ERR lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
645 LYD_ANYDATA_VALUETYPE value_type, int options, struct lyd_node **new_parent, struct lyd_node **new_node);
646
647/**
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200648 * @defgroup implicitoptions Implicit node creation options
649 * @ingroup datatree
650 *
651 * Various options to change lyd_new_implicit*() behavior.
652 *
653 * Default behavior:
654 * - both configuration and state missing implicit nodes are added.
655 * - all implicit node types are added (non-presence containers, default leaves, and default leaf-lists).
656 * @{
657 */
658
Michal Vasko44b19a12020-08-07 09:21:30 +0200659#define LYD_IMPLICIT_NO_STATE 0x01 /**< Do not add any implicit state nodes. */
660#define LYD_IMPLICIT_NO_CONFIG 0x02 /**< Do not add any implicit config nodes. */
661#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 +0200662 containers. */
663
664/** @} implicitoptions */
665
666/**
667 * @brief Add any missing implicit nodes into a data subtree.
668 *
669 * @param[in] tree Tree to add implicit nodes into.
670 * @param[in] implicit_options Options for implicit node creation, see @ref implicitoptions.
671 * @param[out] diff Optional diff with any created nodes.
672 * @return LY_ERR value.
673 */
674LY_ERR lyd_new_implicit_tree(struct lyd_node *tree, int implicit_options, struct lyd_node **diff);
675
676/**
677 * @brief Add any missing implicit nodes.
678 *
679 * @param[in,out] tree Tree to add implicit nodes into.
680 * @param[in] ctx libyang context, must be set only if @p tree is an empty tree.
681 * @param[in] implicit_options Options for implicit node creation, see @ref implicitoptions.
682 * @param[out] diff Optional diff with any created nodes.
683 * @return LY_ERR value.
684 */
685LY_ERR lyd_new_implicit_all(struct lyd_node **tree, const struct ly_ctx *ctx, int implicit_options, struct lyd_node **diff);
686
687/**
688 * @brief Add any missing implicit nodes of one module.
689 *
690 * @param[in,out] tree Tree to add implicit nodes into.
691 * @param[in] module Module whose implicit nodes to create.
692 * @param[in] implicit_options Options for implicit node creation, see @ref implicitoptions.
693 * @param[out] diff Optional diff with any created nodes.
694 * @return LY_ERR value.
695 */
696LY_ERR lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, int implicit_options,
697 struct lyd_node **diff);
698
699/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200700 * @brief Change the value of a term (leaf or leaf-list) node.
701 *
702 * Node changed this way is always considered explicitly set, meaning its default flag
703 * is always cleared.
704 *
705 * @param[in] term Term node to change.
706 * @param[in] val_str New value to set, any prefixes are expected in JSON format.
707 * @return LY_SUCCESS if value was changed,
708 * @return LY_EEXIST if value was the same and only the default flag was cleared,
709 * @return LY_ENOT if the values were equal and no change occured,
710 * @return LY_ERR value on other errors.
711 */
712LY_ERR lyd_change_term(struct lyd_node *term, const char *val_str);
713
714/**
Michal Vasko41586352020-07-13 13:54:25 +0200715 * @brief Change the value of a metadata instance.
716 *
717 * @param[in] ctx libyang context.
718 * @param[in] meta Metadata to change.
719 * @param[in] val_str New value to set, any prefixes are expected in JSON format.
720 * @return LY_SUCCESS if value was changed,
721 * @return LY_ENOT if the values were equal and no change occured,
722 * @return LY_ERR value on other errors.
723 */
724LY_ERR lyd_change_meta(struct lyd_meta *meta, const char *val_str);
725
726/**
Michal Vaskob104f112020-07-17 09:54:54 +0200727 * @brief Insert a child into a parent.
Michal Vaskof03ed032020-03-04 13:31:44 +0100728 *
729 * - if the node is part of some other tree, it is automatically unlinked.
730 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
731 *
732 * @param[in] parent Parent node to insert into.
733 * @param[in] node Node to insert.
734 * @return LY_SUCCESS on success.
735 * @return LY_ERR error on error.
736 */
Michal Vaskob104f112020-07-17 09:54:54 +0200737LY_ERR lyd_insert_child(struct lyd_node *parent, struct lyd_node *node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100738
739/**
Michal Vaskob104f112020-07-17 09:54:54 +0200740 * @brief Insert a node into siblings.
Michal Vaskob1b5c262020-03-05 14:29:47 +0100741 *
742 * - if the node is part of some other tree, it is automatically unlinked.
743 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
744 *
Michal Vaskob104f112020-07-17 09:54:54 +0200745 * @param[in] sibling Siblings to insert into, can even be NULL.
Michal Vaskob1b5c262020-03-05 14:29:47 +0100746 * @param[in] node Node to insert.
Michal Vaskob104f112020-07-17 09:54:54 +0200747 * @param[out] first Optionally return the first sibling after insertion. Can be the address of @p sibling.
Michal Vaskob1b5c262020-03-05 14:29:47 +0100748 * @return LY_SUCCESS on success.
749 * @return LY_ERR error on error.
750 */
Michal Vaskob104f112020-07-17 09:54:54 +0200751LY_ERR lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100752
753/**
Michal Vaskob104f112020-07-17 09:54:54 +0200754 * @brief Insert a node before another node, can be used only for user-ordered nodes.
Michal Vaskof03ed032020-03-04 13:31:44 +0100755 *
756 * - if the node is part of some other tree, it is automatically unlinked.
757 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
758 *
759 * @param[in] sibling Sibling node to insert before.
760 * @param[in] node Node to insert.
761 * @return LY_SUCCESS on success.
762 * @return LY_ERR error on error.
763 */
764LY_ERR lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node);
765
766/**
Michal Vaskob104f112020-07-17 09:54:54 +0200767 * @brief Insert a node after another node, can be used only for user-ordered nodes.
Michal Vaskof03ed032020-03-04 13:31:44 +0100768 *
769 * - if the node is part of some other tree, it is automatically unlinked.
770 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
771 *
772 * @param[in] sibling Sibling node to insert after.
773 * @param[in] node Node to insert.
774 * @return LY_SUCCESS on success.
775 * @return LY_ERR error on error.
776 */
777LY_ERR lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node);
778
779/**
780 * @brief Unlink the specified data subtree.
781 *
782 * @param[in] node Data tree node to be unlinked (together with all the children).
783 */
784void lyd_unlink_tree(struct lyd_node *node);
785
786/**
Radek Krejcib0849a22019-07-25 12:31:04 +0200787 * @brief Free all the nodes (even parents of the node) in the data tree.
Radek Krejcie7b95092019-05-15 11:03:07 +0200788 *
789 * @param[in] node Any of the nodes inside the tree.
790 */
791void lyd_free_all(struct lyd_node *node);
792
793/**
Michal Vasko3a41dff2020-07-15 14:30:28 +0200794 * @brief Free all the sibling nodes (preceding as well as succeeding).
Radek Krejcib0849a22019-07-25 12:31:04 +0200795 *
796 * @param[in] node Any of the sibling nodes to free.
797 */
Michal Vaskof03ed032020-03-04 13:31:44 +0100798void lyd_free_siblings(struct lyd_node *node);
Radek Krejcib0849a22019-07-25 12:31:04 +0200799
800/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200801 * @brief Free (and unlink) the specified data (sub)tree.
802 *
Radek Krejcie7b95092019-05-15 11:03:07 +0200803 * @param[in] node Root of the (sub)tree to be freed.
804 */
805void lyd_free_tree(struct lyd_node *node);
806
807/**
Michal Vasko3a41dff2020-07-15 14:30:28 +0200808 * @brief Free a single metadata instance.
Radek Krejcie7b95092019-05-15 11:03:07 +0200809 *
Michal Vasko3a41dff2020-07-15 14:30:28 +0200810 * @param[in] meta Metadata to free.
Radek Krejcie7b95092019-05-15 11:03:07 +0200811 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200812void lyd_free_meta_single(struct lyd_meta *meta);
Michal Vasko52927e22020-03-16 17:26:14 +0100813
814/**
Michal Vasko3a41dff2020-07-15 14:30:28 +0200815 * @brief Free the metadata instance with any following instances.
816 *
817 * @param[in] meta Metadata to free.
818 */
819void lyd_free_meta_siblings(struct lyd_meta *meta);
820
821/**
822 * @brief Free a single attribute.
Michal Vasko52927e22020-03-16 17:26:14 +0100823 *
824 * @param[in] ctx Context where the attributes were created.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200825 * @param[in] attr Attribute to free.
Michal Vasko52927e22020-03-16 17:26:14 +0100826 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200827void ly_free_attr_single(const struct ly_ctx *ctx, struct ly_attr *attr);
828
829/**
830 * @brief Free the attribute with any following attributes.
831 *
832 * @param[in] ctx Context where the attributes were created.
833 * @param[in] attr First attribute to free.
834 */
835void ly_free_attr_siblings(const struct ly_ctx *ctx, struct ly_attr *attr);
Radek Krejcie7b95092019-05-15 11:03:07 +0200836
Radek Krejci084289f2019-07-09 17:35:30 +0200837/**
838 * @brief Check type restrictions applicable to the particular leaf/leaf-list with the given string @p value.
839 *
840 * The given node is not modified in any way - it is just checked if the @p value can be set to the node.
841 *
842 * If there is no data node instance and you are fine with checking just the type's restrictions without the
843 * data tree context (e.g. for the case of require-instance restriction), use lys_value_validate().
844 *
845 * @param[in] ctx libyang context for logging (function does not log errors when @p ctx is NULL)
846 * @param[in] node Data node for the @p value.
Michal Vaskof937cfe2020-08-03 16:07:12 +0200847 * @param[in] value String value to be checked, it is expected to be in JSON format.
Radek Krejci084289f2019-07-09 17:35:30 +0200848 * @param[in] value_len Length of the given @p value (mandatory).
Michal Vaskof03ed032020-03-04 13:31:44 +0100849 * @param[in] tree Data tree (e.g. when validating RPC/Notification) where the required data instance (leafref target,
850 * instance-identifier) can be placed. NULL in case the data tree is not yet complete,
851 * then LY_EINCOMPLETE can be returned.
Michal Vasko3701af52020-08-03 14:29:38 +0200852 * @param[out] realtype Optional real type of the value.
Radek Krejci084289f2019-07-09 17:35:30 +0200853 * @return LY_SUCCESS on success
854 * @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).
855 * @return LY_ERR value if an error occurred.
856 */
Michal Vasko44685da2020-03-17 15:38:06 +0100857LY_ERR lyd_value_validate(const struct ly_ctx *ctx, const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vasko3701af52020-08-03 14:29:38 +0200858 const struct lyd_node *tree, struct lysc_type **realtype);
Radek Krejci084289f2019-07-09 17:35:30 +0200859
860/**
861 * @brief Compare the node's value with the given string value. The string value is first validated according to the node's type.
862 *
863 * @param[in] node Data node to compare.
864 * @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 +0200865 * it is validated and canonized if possible. But it is expected to be in JSON format.
Radek Krejci084289f2019-07-09 17:35:30 +0200866 * @param[in] value_len Length of the given @p value (mandatory).
Michal Vaskof03ed032020-03-04 13:31:44 +0100867 * @param[in] tree Data tree (e.g. when validating RPC/Notification) where the required data instance (leafref target,
868 * instance-identifier) can be placed. NULL in case the data tree is not yet complete,
869 * then LY_EINCOMPLETE can be returned.
Radek Krejci084289f2019-07-09 17:35:30 +0200870 * @return LY_SUCCESS on success
871 * @return LY_EINCOMPLETE in case of success when the @p trees is not provided and it was needed to finish the validation of
872 * the given string @p value (e.g. due to require-instance).
Michal Vaskob3ddccb2020-07-09 15:43:05 +0200873 * @return LY_ENOT if the values do not match.
Radek Krejci084289f2019-07-09 17:35:30 +0200874 * @return LY_ERR value if an error occurred.
875 */
Michal Vaskof937cfe2020-08-03 16:07:12 +0200876LY_ERR lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len, const struct lyd_node *tree);
Radek Krejci084289f2019-07-09 17:35:30 +0200877
Radek Krejci576b23f2019-07-12 14:06:32 +0200878/**
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200879 * @defgroup datacompareoptions Data compare options
880 * @ingroup datatree
881 *
Radek Krejci22ebdba2019-07-25 13:59:43 +0200882 * Various options to change the lyd_compare() behavior.
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200883 */
884#define LYD_COMPARE_FULL_RECURSION 0x01 /* lists and containers are the same only in case all they children
885 (subtree, so direct as well as indirect children) are the same. By default,
886 containers are the same in case of the same schema node and lists are the same
887 in case of equal keys (keyless lists do the full recursion comparison all the time). */
888#define LYD_COMPARE_DEFAULTS 0x02 /* By default, implicit and explicit default nodes are considered to be equal. This flag
889 changes this behavior and implicit (automatically created default node) and explicit
890 (explicitly created node with the default value) default nodes are considered different. */
Michal Vasko60ea6352020-06-29 13:39:39 +0200891/** @} datacompareoptions */
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200892
893/**
894 * @brief Compare 2 data nodes if they are equivalent.
895 *
896 * @param[in] node1 The first node to compare.
897 * @param[in] node2 The second node to compare.
Radek Krejcic5ad9652019-09-11 11:31:51 +0200898 * @param[in] options Various @ref datacompareoptions.
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200899 * @return LY_SUCCESS if the nodes are equivalent.
900 * @return LY_ENOT if the nodes are not equivalent.
901 */
Michal Vasko8f359bf2020-07-28 10:41:15 +0200902LY_ERR lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, int options);
903
904/**
905 * @brief Compare 2 lists of siblings if they are equivalent.
906 *
907 * @param[in] node1 The first sibling list to compare.
908 * @param[in] node2 The second sibling list to compare.
909 * @param[in] options Various @ref datacompareoptions.
910 * @return LY_SUCCESS if all the siblings are equivalent.
911 * @return LY_ENOT if the siblings are not equivalent.
912 */
913LY_ERR lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, int options);
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200914
915/**
Michal Vasko21725742020-06-29 11:49:25 +0200916 * @brief Compare 2 metadata.
917 *
918 * @param[in] meta1 First metadata.
919 * @param[in] meta2 Second metadata.
920 * @return LY_SUCCESS if the metadata are equivalent.
921 * @return LY_ENOT if not.
922 */
923LY_ERR lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2);
924
925/**
Radek Krejci22ebdba2019-07-25 13:59:43 +0200926 * @defgroup dupoptions Data duplication options
927 * @ingroup datatree
928 *
929 * Various options to change lyd_dup() behavior.
930 *
931 * Default behavior:
932 * - only the specified node is duplicated without siblings, parents, or children.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200933 * - all the metadata of the duplicated nodes are also duplicated.
Radek Krejci22ebdba2019-07-25 13:59:43 +0200934 * @{
935 */
936
937#define LYD_DUP_RECURSIVE 0x01 /**< Duplicate not just the node but also all the children. Note that
938 list's keys are always duplicated. */
Michal Vaskoa29a5762020-06-23 13:28:49 +0200939#define LYD_DUP_NO_META 0x02 /**< Do not duplicate metadata of any node. */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200940#define LYD_DUP_WITH_PARENTS 0x04 /**< If a nested node is being duplicated, duplicate also all the parents.
941 Keys are also duplicated for lists. Return value does not change! */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200942#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 +0200943 its validation/default node state. */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200944
945/** @} dupoptions */
946
947/**
948 * @brief Create a copy of the specified data tree \p node. Schema references are kept the same.
949 *
Radek Krejci22ebdba2019-07-25 13:59:43 +0200950 * @param[in] node Data tree node to be duplicated.
951 * @param[in] parent Optional parent node where to connect the duplicated node(s).
Michal Vasko3a41dff2020-07-15 14:30:28 +0200952 * If set in combination with LYD_DUP_WITH_PARENTS, the parents chain is duplicated until it comes to and connects with
953 * the @p parent.
Radek Krejci22ebdba2019-07-25 13:59:43 +0200954 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200955 * @param[out] dup Optional created copy of the node. Note that in case the parents chain is duplicated for the duplicated
956 * node(s) (when LYD_DUP_WITH_PARENTS used), the first duplicated node is still returned.
957 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +0200958 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200959LY_ERR lyd_dup_single(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup);
960
961/**
962 * @brief Create a copy of the specified data tree \p node with any following siblings. Schema references are kept the same.
963 *
964 * @param[in] node Data tree node to be duplicated.
965 * @param[in] parent Optional parent node where to connect the duplicated node(s).
966 * If set in combination with LYD_DUP_WITH_PARENTS, the parents chain is duplicated until it comes to and connects with
967 * the @p parent.
968 * @param[in] options Bitmask of options flags, see @ref dupoptions.
969 * @param[out] dup Optional created copy of the node. Note that in case the parents chain is duplicated for the duplicated
970 * node(s) (when LYD_DUP_WITH_PARENTS used), the first duplicated node is still returned.
971 * @return LY_ERR value.
972 */
973LY_ERR lyd_dup_siblings(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup);
Radek Krejci22ebdba2019-07-25 13:59:43 +0200974
975/**
Michal Vasko25a32822020-07-09 15:48:22 +0200976 * @brief Create a copy of the metadata.
977 *
978 * @param[in] meta Metadata to copy.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200979 * @param[in] parent Node where to append the new metadata.
980 * @param[out] dup Optional created metadata copy.
981 * @return LY_ERR value.
Michal Vasko25a32822020-07-09 15:48:22 +0200982 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200983LY_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 +0200984
985/**
Michal Vasko4490d312020-06-16 13:08:55 +0200986 * @defgroup mergeoptions Data merge options.
987 * @ingroup datatree
Radek Krejci576b23f2019-07-12 14:06:32 +0200988 *
Michal Vasko4490d312020-06-16 13:08:55 +0200989 * Various options to change lyd_merge() behavior.
990 *
991 * Default behavior:
992 * - source data tree is not modified in any way,
Michal Vasko3a41dff2020-07-15 14:30:28 +0200993 * - any default nodes in the source are ignored if there are explicit nodes in the target.
Michal Vasko4490d312020-06-16 13:08:55 +0200994 * @{
995 */
996
997#define LYD_MERGE_DESTRUCT 0x01 /**< Spend source data tree in the function, it cannot be used afterwards! */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200998#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 +0200999
1000/** @} mergeoptions */
1001
1002/**
Michal Vasko3a41dff2020-07-15 14:30:28 +02001003 * @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 +02001004 * is called on the resulting data tree (data from more cases may be present, default and non-default values).
1005 *
1006 * @param[in,out] target Target data tree to merge into, must be a top-level tree.
1007 * @param[in] source Source data tree to merge, must be a top-level tree.
1008 * @param[in] options Bitmask of option flags, see @ref mergeoptions.
1009 * @return LY_SUCCESS on success,
1010 * @return LY_ERR value on error.
1011 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001012LY_ERR lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, int options);
1013
1014/**
1015 * @brief Merge the source data tree with any following siblings into the target data tree. Merge may not be
1016 * complete until validation called on the resulting data tree (data from more cases may be present, default
1017 * and non-default values).
1018 *
1019 * @param[in,out] target Target data tree to merge into, must be a top-level tree.
1020 * @param[in] source Source data tree to merge, must be a top-level tree.
1021 * @param[in] options Bitmask of option flags, see @ref mergeoptions.
1022 * @return LY_SUCCESS on success,
1023 * @return LY_ERR value on error.
1024 */
1025LY_ERR lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, int options);
Michal Vasko4490d312020-06-16 13:08:55 +02001026
1027/**
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001028 * @defgroup diffoptions Data diff options.
1029 * @ingroup datatree
1030 *
1031 * Various options to change lyd_diff() behavior.
1032 *
1033 * Default behavior:
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001034 * - any default nodes are treated as non-existent and ignored.
1035 * @{
1036 */
1037
Michal Vasko3a41dff2020-07-15 14:30:28 +02001038#define LYD_DIFF_DEFAULTS 0x01 /**< Default nodes in the trees are not ignored but treated similarly to explicit
1039 nodes. Also, leaves and leaf-lists are added into diff even in case only their
1040 default flag (state) was changed. */
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001041
1042/** @} diffoptions */
1043
1044/**
1045 * @brief Learn the differences between 2 data trees.
1046 *
1047 * The resulting diff is represented as a data tree with specific metadata from the internal 'yang'
1048 * module. Most importantly, every node has an effective 'operation' metadata. If there is none
1049 * defined on the node, it inherits the operation from the nearest parent. Top-level nodes must
1050 * always have the 'operation' metadata defined. Additional metadata ('orig-default', 'value',
1051 * 'orig-value', 'key', 'orig-key') are used for storing more information about the value in the first
1052 * or the second tree.
1053 *
1054 * The diff tree is completely independent on the @p first and @p second trees, meaning all
1055 * the information about the change is stored in the diff and the trees are not needed.
1056 *
1057 * !! Caution !!
1058 * The diff tree should never be validated because it may easily not be valid! For example,
1059 * when data from one case branch are deleted and data from another branch created - data from both
1060 * branches are then stored in the diff tree simultaneously.
1061 *
1062 * @param[in] first First data tree.
1063 * @param[in] second Second data tree.
1064 * @param[in] options Bitmask of options flags, see @ref diffoptions.
1065 * @param[out] diff Generated diff.
1066 * @return LY_SUCCESS on success,
1067 * @return LY_ERR on error.
1068 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001069LY_ERR lyd_diff_tree(const struct lyd_node *first, const struct lyd_node *second, int options, struct lyd_node **diff);
1070
1071/**
1072 * @brief Learn the differences between 2 data trees including all the following siblings.
1073 *
1074 * @param[in] first First data tree.
1075 * @param[in] second Second data tree.
1076 * @param[in] options Bitmask of options flags, see @ref diffoptions.
1077 * @param[out] diff Generated diff.
1078 * @return LY_SUCCESS on success,
1079 * @return LY_ERR on error.
1080 */
1081LY_ERR lyd_diff_siblings(const struct lyd_node *first, const struct lyd_node *second, int options, struct lyd_node **diff);
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001082
1083/**
1084 * @brief Callback for diff nodes.
1085 *
1086 * @param[in] diff_node Diff node.
1087 * @param[in] data_node Matching node in data.
1088 * @param[in] cb_data Arbitrary callback data.
1089 * @return LY_ERR value.
1090 */
1091typedef LY_ERR (*lyd_diff_cb)(const struct lyd_node *diff_node, struct lyd_node *data_node, void *cb_data);
1092
1093/**
Michal Vasko3a41dff2020-07-15 14:30:28 +02001094 * @brief Apply the whole diff on a data tree but restrict the operation to one module.
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001095 *
1096 * @param[in,out] data Data to apply the diff on.
1097 * @param[in] diff Diff to apply.
1098 * @param[in] mod Module, whose diff/data only to consider, NULL for all modules.
1099 * @param[in] diff_cb Optional diff callback that will be called for every changed node.
1100 * @param[in] cb_data Arbitrary callback data.
1101 * @return LY_SUCCESS on success,
1102 * @return LY_ERR on error.
1103 */
1104LY_ERR lyd_diff_apply_module(struct lyd_node **data, const struct lyd_node *diff, const struct lys_module *mod,
1105 lyd_diff_cb diff_cb, void *cb_data);
1106
1107/**
Michal Vasko3a41dff2020-07-15 14:30:28 +02001108 * @brief Apply the whole diff tree on a data tree.
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001109 *
1110 * @param[in,out] data Data to apply the diff on.
1111 * @param[in] diff Diff to apply.
1112 * @return LY_SUCCESS on success,
1113 * @return LY_ERR on error.
1114 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001115LY_ERR lyd_diff_apply_all(struct lyd_node **data, const struct lyd_node *diff);
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001116
1117/**
Michal Vaskoe6323f62020-07-09 15:49:02 +02001118 * @brief Merge 2 diffs into each other but restrict the operation to one module.
1119 *
1120 * The diffs must be possible to be merged, which is guaranteed only if the source diff was
1121 * created on data that had the target diff applied on them. In other words, this sequence is legal
1122 *
Michal Vasko04f85912020-08-07 12:14:58 +02001123 * 1) diff1 from data1 and data2 -> data11 from apply diff1 on data1 -> diff2 from data11 and data3 ->
1124 * -> data 33 from apply diff2 on data1
Michal Vaskoe6323f62020-07-09 15:49:02 +02001125 *
1126 * and reusing these diffs
1127 *
Michal Vasko04f85912020-08-07 12:14:58 +02001128 * 2) diff11 from merge diff1 and diff2 -> data33 from apply diff11 on data1
Michal Vaskoe6323f62020-07-09 15:49:02 +02001129 *
Michal Vaskofb737aa2020-08-06 13:53:53 +02001130 * @param[in,out] diff Target diff to merge into.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001131 * @param[in] src_diff Source diff.
1132 * @param[in] mod Module, whose diff only to consider, NULL for all modules.
1133 * @param[in] diff_cb Optional diff callback that will be called for every changed node.
1134 * @param[in] cb_data Arbitrary callback data.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001135 * @return LY_SUCCESS on success,
1136 * @return LY_ERR on error.
1137 */
Michal Vaskofb737aa2020-08-06 13:53:53 +02001138LY_ERR lyd_diff_merge_module(struct lyd_node **diff, const struct lyd_node *src_diff, const struct lys_module *mod,
1139 lyd_diff_cb diff_cb, void *cb_data);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001140
1141/**
Michal Vasko04f85912020-08-07 12:14:58 +02001142 * @brief Merge 2 diff trees into each other.
1143 *
1144 * @param[in,out] diff_first Target diff first sibling to merge into.
1145 * @param[in] diff_parent Target diff parent to merge into.
1146 * @param[in] src_sibling Source diff sibling to merge.
1147 * @param[in] diff_cb Optional diff callback that will be called for every changed node.
1148 * @param[in] cb_data Arbitrary callback data.
1149 * @return LY_SUCCESS on success,
1150 * @return LY_ERR on error.
1151 */
1152LY_ERR lyd_diff_merge_tree(struct lyd_node **diff_first, struct lyd_node *diff_parent, const struct lyd_node *src_sibling,
1153 lyd_diff_cb diff_cb, void *cb_data);
1154
1155/**
Michal Vaskoe6323f62020-07-09 15:49:02 +02001156 * @brief Merge 2 diffs into each other.
1157 *
Michal Vaskoe6323f62020-07-09 15:49:02 +02001158 * @param[in,out] diff Target diff to merge into.
Michal Vaskofb737aa2020-08-06 13:53:53 +02001159 * @param[in] src_diff Source diff.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001160 * @return LY_SUCCESS on success,
1161 * @return LY_ERR on error.
1162 */
Michal Vaskofb737aa2020-08-06 13:53:53 +02001163LY_ERR lyd_diff_merge_all(struct lyd_node **diff, const struct lyd_node *src_diff);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001164
1165/**
Michal Vasko4231fb62020-07-13 13:54:47 +02001166 * @brief Reverse a diff and make the opposite changes. Meaning change create to delete, delete to create,
1167 * or move from place A to B to move from B to A and so on.
1168 *
1169 * @param[in] src_diff Diff to reverse.
1170 * @param[out] diff Reversed diff.
1171 * @return LY_SUCCESS on success.
1172 * @return LY_ERR on error.
1173 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001174LY_ERR lyd_diff_reverse_all(const struct lyd_node *src_diff, struct lyd_node **diff);
Michal Vasko4231fb62020-07-13 13:54:47 +02001175
1176/**
Michal Vasko4490d312020-06-16 13:08:55 +02001177 * @brief Find the target in data of a compiled ly_path structure (instance-identifier).
1178 *
1179 * @param[in] path Compiled path structure.
Michal Vaskof03ed032020-03-04 13:31:44 +01001180 * @param[in] tree Data tree to be searched.
Michal Vasko4490d312020-06-16 13:08:55 +02001181 * @return Found target node,
1182 * @return NULL if not found.
Radek Krejci576b23f2019-07-12 14:06:32 +02001183 */
Michal Vasko004d3152020-06-11 19:59:22 +02001184const struct lyd_node_term *lyd_target(const struct ly_path *path, const struct lyd_node *tree);
Radek Krejci084289f2019-07-09 17:35:30 +02001185
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001186/**
1187 * @brief Get string value of a term data \p node.
1188 *
1189 * @param[in] node Data tree node with the value.
1190 * @param[out] dynamic Whether the string value was dynmically allocated.
1191 * @return String value of @p node, if @p dynamic, needs to be freed.
1192 */
1193const char *lyd_value2str(const struct lyd_node_term *node, int *dynamic);
1194
1195/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001196 * @brief Get string value of a metadata \p meta.
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001197 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001198 * @param[in] meta Metadata with the value.
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001199 * @param[out] dynamic Whether the string value was dynmically allocated.
Michal Vasko9f96a052020-03-10 09:41:45 +01001200 * @return String value of @p meta, if @p dynamic, needs to be freed.
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001201 */
Michal Vasko9f96a052020-03-10 09:41:45 +01001202const char *lyd_meta2str(const struct lyd_meta *meta, int *dynamic);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001203
1204/**
1205 * @brief Types of the different data paths.
1206 */
1207typedef enum {
Michal Vasko14654712020-02-06 08:35:21 +01001208 LYD_PATH_LOG, /**< Descriptive path format used in log messages */
Michal Vasko790b2bc2020-08-03 13:35:06 +02001209 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 +02001210} LYD_PATH_TYPE;
1211
1212/**
1213 * @brief Generate path of the given node in the requested format.
1214 *
1215 * @param[in] node Schema path of this node will be generated.
1216 * @param[in] pathtype Format of the path to generate.
1217 * @param[in,out] buffer Prepared buffer of the @p buflen length to store the generated path.
1218 * If NULL, memory for the complete path is allocated.
1219 * @param[in] buflen Size of the provided @p buffer.
1220 * @return NULL in case of memory allocation error, path of the node otherwise.
1221 * In case the @p buffer is NULL, the returned string is dynamically allocated and caller is responsible to free it.
1222 */
1223char *lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen);
1224
Michal Vaskoe444f752020-02-10 12:20:06 +01001225/**
Michal Vasko25a32822020-07-09 15:48:22 +02001226 * @brief Find a specific metadata.
1227 *
1228 * @param[in] first First metadata to consider.
1229 * @param[in] module Module of the metadata definition, may be NULL if @p name includes a prefix.
1230 * @param[in] name Name of the metadata to find, may not include a prefix (module name) if @p module is set.
1231 * @return Found metadata,
1232 * @return NULL if not found.
1233 */
1234struct lyd_meta *lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name);
1235
1236/**
Michal Vaskoda859032020-07-14 12:20:14 +02001237 * @brief Search in the given siblings (NOT recursively) for the first target instance with the same value.
Michal Vaskoe444f752020-02-10 12:20:06 +01001238 * Uses hashes - should be used whenever possible for best performance.
1239 *
1240 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
1241 * @param[in] target Target node to find.
Michal Vasko9b368d32020-02-14 13:53:31 +01001242 * @param[out] match Can be NULL, otherwise the found data node.
Michal Vaskoe444f752020-02-10 12:20:06 +01001243 * @return LY_SUCCESS on success, @p match set.
1244 * @return LY_ENOTFOUND if not found, @p match set to NULL.
1245 * @return LY_ERR value if another error occurred.
1246 */
1247LY_ERR lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match);
1248
1249/**
Michal Vaskoe444f752020-02-10 12:20:06 +01001250 * @brief Search in the given siblings for the first schema instance.
1251 * Uses hashes - should be used whenever possible for best performance.
1252 *
1253 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
1254 * @param[in] schema Schema node of the data node to find.
Michal Vaskob104f112020-07-17 09:54:54 +02001255 * @param[in] key_or_value If it is NULL, the first schema node data instance is found. For nodes with many
1256 * instances, it can be set based on the type of @p schema:
Michal Vaskoe444f752020-02-10 12:20:06 +01001257 * LYS_LEAFLIST:
1258 * Searched instance value.
1259 * LYS_LIST:
Michal Vasko90932a92020-02-12 14:33:03 +01001260 * Searched instance key values in the form of "[key1='val1'][key2='val2']...".
1261 * The keys do not have to be ordered but all of them must be set.
1262 *
1263 * Note that any explicit values (leaf-list or list key values) will be canonized first
1264 * before comparison. But values that do not have a canonical value are expected to be in the
1265 * JSON format!
Michal Vaskof03ed032020-03-04 13:31:44 +01001266 * @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 +01001267 * @param[out] match Can be NULL, otherwise the found data node.
Michal Vaskoe444f752020-02-10 12:20:06 +01001268 * @return LY_SUCCESS on success, @p match set.
1269 * @return LY_ENOTFOUND if not found, @p match set to NULL.
1270 * @return LY_EINVAL if @p schema is a key-less list.
1271 * @return LY_ERR value if another error occurred.
1272 */
1273LY_ERR lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
1274 size_t val_len, struct lyd_node **match);
1275
Michal Vaskoccc02342020-05-21 10:09:21 +02001276/**
1277 * @brief Search in the given data for instances of nodes matching the provided XPath.
1278 *
Michal Vaskob104f112020-07-17 09:54:54 +02001279 * The expected format of the expression is ::LYD_JSON, meaning the first node in every path
Michal Vasko61ac2f62020-05-25 12:39:51 +02001280 * must have its module name as prefix or be the special `*` value for all the nodes.
1281 *
Michal Vasko19a30602020-05-25 10:40:19 +02001282 * If a list instance is being selected with all its key values specified (but not necessarily ordered)
1283 * in the form `list[key1='val1'][key2='val2'][key3='val3']` or a leaf-list instance in the form
1284 * `leaf-list[.='val']`, these instances are found using hashes with constant (*O(1)*) complexity
1285 * (unless they are defined in top-level). Other predicates can still follow the aforementioned ones.
1286 *
Michal Vaskoccc02342020-05-21 10:09:21 +02001287 * @param[in] ctx_node XPath context node.
1288 * @param[in] xpath Data XPath expression filtering the matching nodes. ::LYD_JSON format is expected.
1289 * @param[out] set Set of found data nodes. In case the result is a number, a string, or a boolean,
1290 * the returned set is empty.
1291 * @return LY_SUCCESS on success, @p set is returned.
1292 * @return LY_ERR value if an error occurred.
1293 */
1294LY_ERR lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set);
1295
Radek Krejcie7b95092019-05-15 11:03:07 +02001296#ifdef __cplusplus
1297}
1298#endif
1299
1300#endif /* LY_TREE_DATA_H_ */