blob: 58f5aebfe450c7254ea893a8427cce2ecfb28560 [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/**
Michal Vaskobd755db2020-08-11 10:36:33 +020044 * @brief Macro for getting child pointer of a generic data node.
Michal Vaskoa276cd92020-08-10 15:10:08 +020045 *
Michal Vaskobd755db2020-08-11 10:36:33 +020046 * @param[in] node Node whose child pointer to get.
Michal Vaskoa276cd92020-08-10 15:10:08 +020047 */
Michal Vaskobd755db2020-08-11 10:36:33 +020048#define LYD_CHILD(node) ((node)->schema ? (lyd_node_children(node, 0)) : ((struct lyd_node_opaq *)(node))->child)
49
50/**
51 * @brief Macro for getting child pointer of a generic data node but skipping its keys in case it is ::LYS_LIST.
52 *
53 * @param[in] node Node whose child pointer to get.
54 */
55#define LYD_CHILD_NO_KEYS(node) ((node)->schema ? (lyd_node_children(node, LYD_CHILDREN_SKIP_KEYS)) : ((struct lyd_node_opaq *)(node))->child)
56
57/**
58 * @brief Macro for getting generic parent pointer of a node.
59 *
60 * @param[in] node Node whose parent pointer to get.
61 */
62#define LYD_PARENT(node) ((struct lyd_node *)(node)->parent)
Michal Vaskoa276cd92020-08-10 15:10:08 +020063
64/**
Radek Krejcie7b95092019-05-15 11:03:07 +020065 * @brief Macro to iterate via all elements in a data tree. This is the opening part
66 * to the #LYD_TREE_DFS_END - they always have to be used together.
67 *
68 * The function follows deep-first search algorithm:
69 * <pre>
70 * 1
71 * / \
Michal Vaskoc193ce92020-03-06 11:04:48 +010072 * 2 4
Radek Krejcie7b95092019-05-15 11:03:07 +020073 * / / \
Michal Vaskoc193ce92020-03-06 11:04:48 +010074 * 3 5 6
Radek Krejcie7b95092019-05-15 11:03:07 +020075 * </pre>
76 *
Radek Krejci0935f412019-08-20 16:15:18 +020077 * Use the same parameters for #LYD_TREE_DFS_BEGIN and #LYD_TREE_DFS_END. While
Michal Vasko56daf732020-08-10 10:57:18 +020078 * START can be any of the lyd_node* types, ELEM variable must be a pointer to
79 * the generic struct lyd_node.
Radek Krejcie7b95092019-05-15 11:03:07 +020080 *
Michal Vasko56daf732020-08-10 10:57:18 +020081 * To skip a particular subtree, instead of the continue statement, set LYD_TREE_DFS_continue
82 * variable to non-zero value.
Radek Krejcie7b95092019-05-15 11:03:07 +020083 *
84 * Use with opening 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 */
Michal Vasko56daf732020-08-10 10:57:18 +020089#define LYD_TREE_DFS_BEGIN(START, ELEM) \
90 { int LYD_TREE_DFS_continue = 0; struct lyd_node *LYD_TREE_DFS_next; \
91 for ((ELEM) = (LYD_TREE_DFS_next) = (struct lyd_node *)(START); \
Radek Krejcie7b95092019-05-15 11:03:07 +020092 (ELEM); \
Michal Vasko56daf732020-08-10 10:57:18 +020093 (ELEM) = (LYD_TREE_DFS_next), LYD_TREE_DFS_continue = 0)
Radek Krejcie7b95092019-05-15 11:03:07 +020094
95/**
96 * @brief Macro to iterate via all elements in a tree. This is the closing part
97 * to the #LYD_TREE_DFS_BEGIN - they always have to be used together.
98 *
99 * Use the same parameters for #LYD_TREE_DFS_BEGIN and #LYD_TREE_DFS_END. While
Michal Vasko56daf732020-08-10 10:57:18 +0200100 * START can be any of the lyd_node* types, ELEM variable must be a pointer
101 * to the generic struct lyd_node.
Radek Krejcie7b95092019-05-15 11:03:07 +0200102 *
103 * Use with closing curly bracket '}' after the macro.
104 *
105 * @param START Pointer to the starting element processed first.
Radek Krejcie7b95092019-05-15 11:03:07 +0200106 * @param ELEM Iterator intended for use in the block.
107 */
108
Michal Vasko56daf732020-08-10 10:57:18 +0200109#define LYD_TREE_DFS_END(START, ELEM) \
Radek Krejcie7b95092019-05-15 11:03:07 +0200110 /* select element for the next run - children first */ \
Michal Vasko56daf732020-08-10 10:57:18 +0200111 if (LYD_TREE_DFS_continue) { \
112 (LYD_TREE_DFS_next) = NULL; \
113 } else { \
114 (LYD_TREE_DFS_next) = lyd_node_children(ELEM, 0); \
115 }\
116 if (!(LYD_TREE_DFS_next)) { \
Radek Krejcie7b95092019-05-15 11:03:07 +0200117 /* no children */ \
118 if ((ELEM) == (struct lyd_node*)(START)) { \
119 /* we are done, (START) has no children */ \
120 break; \
121 } \
122 /* try siblings */ \
Michal Vasko56daf732020-08-10 10:57:18 +0200123 (LYD_TREE_DFS_next) = (ELEM)->next; \
Radek Krejcie7b95092019-05-15 11:03:07 +0200124 } \
Michal Vasko56daf732020-08-10 10:57:18 +0200125 while (!(LYD_TREE_DFS_next)) { \
Radek Krejcie7b95092019-05-15 11:03:07 +0200126 /* parent is already processed, go to its sibling */ \
127 (ELEM) = (struct lyd_node*)(ELEM)->parent; \
128 /* no siblings, go back through parents */ \
129 if ((ELEM)->parent == (START)->parent) { \
130 /* we are done, no next element to process */ \
131 break; \
132 } \
Michal Vasko56daf732020-08-10 10:57:18 +0200133 (LYD_TREE_DFS_next) = (ELEM)->next; \
134 } } \
Radek Krejcie7b95092019-05-15 11:03:07 +0200135
136/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200137 * @brief Macro to get context from a data tree node.
138 */
Michal Vasko52927e22020-03-16 17:26:14 +0100139#define LYD_NODE_CTX(node) ((node)->schema ? (node)->schema->module->ctx : ((struct lyd_node_opaq *)(node))->ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200140
141/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200142 * @brief Data input/output formats supported by libyang [parser](@ref howtodataparsers) and
Radek Krejcid91d4da2020-05-18 11:28:02 +0200143 * [printer](@ref howtodataprinters) functions. Also used for value prefix format (TODO link to prefix formats descriptions).
Radek Krejcie7b95092019-05-15 11:03:07 +0200144 */
145typedef enum {
Michal Vasko52927e22020-03-16 17:26:14 +0100146 LYD_SCHEMA = 0, /**< invalid instance data format, value prefixes map to YANG import prefixes */
147 LYD_XML, /**< XML instance data format, value prefixes map to XML namespace prefixes */
148 LYD_JSON, /**< JSON instance data format, value prefixes map to module names */
Michal Vasko60ea6352020-06-29 13:39:39 +0200149 LYD_LYB, /**< LYB instance data format, invalid value prefix format (same as LYD_JSON) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200150} LYD_FORMAT;
151
152/**
Radek Krejci59583bb2019-09-11 12:57:55 +0200153 * @brief List of possible value types stored in ::lyd_node_any.
Radek Krejcie7b95092019-05-15 11:03:07 +0200154 */
155typedef enum {
Radek Krejci22ebdba2019-07-25 13:59:43 +0200156 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 +0200157 is directly connected into the anydata node without duplication, caller is supposed to not manipulate
158 with the data after a successful call (including calling lyd_free() on the provided data) */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200159 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 +0200160 as string). XML sensitive characters (such as & or \>) are automatically escaped when the anydata
161 is printed in XML format. */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200162 LYD_ANYDATA_XML, /**< Value is a string containing the serialized XML data. */
163 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 +0200164 LYD_ANYDATA_LYB, /**< Value is a memory chunk with the serialized data tree in LYB format. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200165} LYD_ANYDATA_VALUETYPE;
166
167/** @} */
168
169/**
170 * @brief YANG data representation
171 */
172struct lyd_value {
Radek Krejci950f6a52019-09-12 17:15:32 +0200173 const char *original; /**< Original string representation of the value. It is never NULL, but (canonical) string representation
174 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 +0200175 union {
Radek Krejcie7b95092019-05-15 11:03:07 +0200176 int8_t boolean; /**< 0 as false, 1 as true */
177 int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200178 int8_t int8; /**< 8-bit signed integer */
179 int16_t int16; /**< 16-bit signed integer */
180 int32_t int32; /**< 32-bit signed integer */
181 int64_t int64; /**< 64-bit signed integer */
182 uint8_t uint8; /**< 8-bit unsigned integer */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200183 uint16_t uint16; /**< 16-bit unsigned integer */
184 uint32_t uint32; /**< 32-bit unsigned integer */
185 uint64_t uint64; /**< 64-bit unsigned integer */
Radek Krejcie7b95092019-05-15 11:03:07 +0200186 struct lysc_type_bitenum_item *enum_item; /**< pointer to the definition of the enumeration value */
Radek Krejci849a62a2019-05-22 15:29:05 +0200187 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 +0200188 struct lysc_ident *ident; /**< pointer to the schema definition of the identityref value */
Radek Krejciefbb3922019-07-15 12:58:00 +0200189
190 struct lyd_value_subvalue {
191 struct lyd_value_prefix {
192 const char *prefix; /**< prefix string used in the canonized string to identify the mod of the YANG schema */
193 const struct lys_module *mod; /**< YANG schema module identified by the prefix string */
194 } *prefixes; /**< list of mappings between prefix in canonized value to a YANG schema ([sized array](@ref sizedarrays)) */
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200195 struct lyd_value *value; /**< representation of the value according to the selected union's subtype (stored as lyd_value::realpath
196 here, in subvalue structure */
197 } *subvalue; /**< data to represent data with multiple types (union). Original value is stored in the main
Michal Vasko9b368d32020-02-14 13:53:31 +0100198 lyd_value:canonical_cache while the lyd_value_subvalue::value contains representation according to the
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200199 one of the union's type. The lyd_value_subvalue:prefixes provides (possible) mappings from prefixes
200 in original value to YANG modules. These prefixes are necessary to parse original value to the union's
201 subtypes. */
Radek Krejci084289f2019-07-09 17:35:30 +0200202
Michal Vasko004d3152020-06-11 19:59:22 +0200203 struct ly_path *target; /**< Instance-identifier's target path. */
Radek Krejci084289f2019-07-09 17:35:30 +0200204
Radek Krejcie7b95092019-05-15 11:03:07 +0200205 void *ptr; /**< generic data type structure used to store the data */
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200206 }; /**< The union is just a list of shorthands to possible values stored by a type's plugin. libyang itself uses the lyd_value::realtype
207 plugin's callbacks to work with the data. */
Radek Krejci084289f2019-07-09 17:35:30 +0200208
Radek Krejci950f6a52019-09-12 17:15:32 +0200209 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 +0200210 in the schema node of the data node since the type's store plugin can use other types/plugins for
211 storing data. Speaking about built-in types, this is the case of leafref which stores data as its
212 target type. In contrast, union type also use its subtype's callbacks, but inside an internal data
213 lyd_value::subvalue structure, so here is the pointer to the union type.
214 In general, this type is used to get free callback for this lyd_value structure, so it must reflect
215 the type used to store data directly in the same lyd_value instance. */
Radek Krejci950f6a52019-09-12 17:15:32 +0200216 void *canonical_cache; /**< Generic cache for type plugins to store data necessary to print canonical value. It can be the canonical
217 value itself or anything else useful to print the canonical form of the value. Plugin is responsible for
218 freeing the cache in its free callback. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200219};
220
221/**
Michal Vasko9f96a052020-03-10 09:41:45 +0100222 * @brief Metadata structure.
Radek Krejcie7b95092019-05-15 11:03:07 +0200223 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100224 * The structure provides information about metadata of a data element. Such attributes must map to
Radek Krejcie7b95092019-05-15 11:03:07 +0200225 * annotations as specified in RFC 7952. The only exception is the filter type (in NETCONF get operations)
226 * and edit-config's operation attributes. In XML, they are represented as standard XML attributes. In JSON,
227 * they are represented as JSON elements starting with the '@' character (for more information, see the
228 * YANG metadata RFC.
229 *
230 */
Michal Vasko9f96a052020-03-10 09:41:45 +0100231struct lyd_meta {
232 struct lyd_node *parent; /**< data node where the metadata is placed */
233 struct lyd_meta *next; /**< pointer to the next metadata of the same element */
234 struct lysc_ext_instance *annotation; /**< pointer to the annotation's definition */
235 const char *name; /**< metadata name */
236 struct lyd_value value; /**< metadata value representation */
Radek Krejcie7b95092019-05-15 11:03:07 +0200237};
238
Michal Vasko52927e22020-03-16 17:26:14 +0100239/**
240 * @brief Generic prefix and namespace mapping, meaning depends on the format.
241 */
242struct ly_prefix {
243 const char *pref;
244 const char *ns;
245};
246
247/**
248 * @brief Generic attribute structure.
249 */
250struct ly_attr {
251 struct lyd_node_opaq *parent; /**< data node where the attribute is placed */
252 struct ly_attr *next;
253 struct ly_prefix *val_prefs; /**< list of prefixes in the value ([sized array](@ref sizedarrays)) */
254 const char *name;
255 const char *value;
256
257 LYD_FORMAT format;
258 struct ly_prefix prefix; /**< name prefix, it is stored because they are a real pain to generate properly */
259
260};
Radek Krejcie7b95092019-05-15 11:03:07 +0200261
Michal Vasko1bf09392020-03-27 12:38:10 +0100262#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 +0200263#define LYD_NODE_TERM (LYS_LEAF|LYS_LEAFLIST) /**< Schema nodetype mask for lyd_node_term */
264#define LYD_NODE_ANY (LYS_ANYDATA) /**< Schema nodetype mask for lyd_node_any */
265
266/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100267 * @defgroup dnodeflags Data node flags
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200268 * @ingroup datatree
269 * @{
270 *
271 * Various flags of data nodes.
272 *
273 * 1 - container 5 - anydata/anyxml
274 * 2 - list 6 - rpc/action
275 * 3 - leaf 7 - notification
276 * 4 - leaflist
277 *
278 * bit name 1 2 3 4 5 6 7
279 * ---------------------+-+-+-+-+-+-+-+
280 * 1 LYD_DEFAULT |x| |x|x| | | |
281 * +-+-+-+-+-+-+-+
Michal Vasko5c4e5892019-11-14 12:31:38 +0100282 * 2 LYD_WHEN_TRUE |x|x|x|x|x| | |
Michal Vasko9b368d32020-02-14 13:53:31 +0100283 * +-+-+-+-+-+-+-+
284 * 3 LYD_NEW |x|x|x|x|x|x|x|
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200285 * ---------------------+-+-+-+-+-+-+-+
286 *
287 */
288
Michal Vasko5c4e5892019-11-14 12:31:38 +0100289#define LYD_DEFAULT 0x01 /**< default (implicit) node */
290#define LYD_WHEN_TRUE 0x02 /**< all when conditions of this node were evaluated to true */
Michal Vasko9b368d32020-02-14 13:53:31 +0100291#define LYD_NEW 0x04 /**< node was created after the last validation, is needed for the next validation */
Michal Vasko52927e22020-03-16 17:26:14 +0100292
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200293/** @} */
294
295/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200296 * @brief Generic structure for a data node.
297 */
298struct lyd_node {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200299 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
300 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
301 used to get know that nodes are not equal, it cannot be used to decide that the
302 nodes are equal due to possible collisions. */
303 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Michal Vaskoecd62de2019-11-13 12:35:11 +0100304 const struct lysc_node *schema; /**< pointer to the schema definition of this node, note that the target can be not just
305 ::struct lysc_node but ::struct lysc_action or ::struct lysc_notif as well */
Radek Krejcie7b95092019-05-15 11:03:07 +0200306 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
307 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
308 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
309 never NULL. If there is no sibling node, pointer points to the node
310 itself. In case of the first node, this pointer points to the last
311 node in the list. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100312 struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200313
314#ifdef LY_ENABLED_LYD_PRIV
315 void *priv; /**< private user data, not used by libyang */
316#endif
317};
318
319/**
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200320 * @brief Data node structure for the inner data tree nodes - containers, lists, RPCs, actions and Notifications.
Radek Krejcie7b95092019-05-15 11:03:07 +0200321 */
322struct lyd_node_inner {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200323 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
324 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
325 used to get know that nodes are not equal, it cannot be used to decide that the
326 nodes are equal due to possible collisions. */
327 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200328 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
329 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
330 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
331 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
332 never NULL. If there is no sibling node, pointer points to the node
333 itself. In case of the first node, this pointer points to the last
334 node in the list. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100335 struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200336
337#ifdef LY_ENABLED_LYD_PRIV
338 void *priv; /**< private user data, not used by libyang */
339#endif
340
341 struct lyd_node *child; /**< pointer to the first child node. */
342 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 +0200343#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 +0200344};
345
346/**
Michal Vaskof03ed032020-03-04 13:31:44 +0100347 * @brief Data node structure for the terminal data tree nodes - leaves and leaf-lists.
Radek Krejcie7b95092019-05-15 11:03:07 +0200348 */
349struct lyd_node_term {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200350 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
351 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
352 used to get know that nodes are not equal, it cannot be used to decide that the
353 nodes are equal due to possible collisions. */
354 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200355 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
356 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
357 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
358 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
359 never NULL. If there is no sibling node, pointer points to the node
360 itself. In case of the first node, this pointer points to the last
361 node in the list. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100362 struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200363
364#ifdef LY_ENABLED_LYD_PRIV
365 void *priv; /**< private user data, not used by libyang */
366#endif
367
368 struct lyd_value value; /**< node's value representation */
369};
370
371/**
372 * @brief Data node structure for the anydata data tree nodes - anydatas and anyxmls.
373 */
374struct lyd_node_any {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200375 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
376 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
377 used to get know that nodes are not equal, it cannot be used to decide that the
378 nodes are equal due to possible collisions. */
379 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200380 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
381 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
382 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
383 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
384 never NULL. If there is no sibling node, pointer points to the node
385 itself. In case of the first node, this pointer points to the last
386 node in the list. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100387 struct lyd_meta *meta; /**< pointer to the list of attributes of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200388
389#ifdef LY_ENABLED_LYD_PRIV
390 void *priv; /**< private user data, not used by libyang */
391#endif
392
Michal Vasko00cbf532020-06-15 13:58:47 +0200393 union lyd_any_value {
Radek Krejciee4cab22019-07-17 17:07:47 +0200394 struct lyd_node *tree; /**< data tree */
395 const char *str; /**< Generic string data */
396 const char *xml; /**< Serialized XML data */
397 const char *json; /**< I-JSON encoded string */
398 char *mem; /**< LYD_ANYDATA_LYB memory chunk */
399 } value; /**< pointer to the stored value representation of the anydata/anyxml node */
400 LYD_ANYDATA_VALUETYPE value_type;/**< type of the data stored as lyd_node_any::value */
Radek Krejcie7b95092019-05-15 11:03:07 +0200401};
402
403/**
Michal Vasko52927e22020-03-16 17:26:14 +0100404 * @brief Data node structure for unparsed (opaque) nodes.
405 */
406struct lyd_node_opaq {
407 uint32_t hash; /**< always 0 */
408 uint32_t flags; /**< always 0 */
409 const struct lysc_node *schema; /**< always NULL */
410 struct lyd_node *parent; /**< pointer to the parent node (NULL in case of root node) */
411 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
412 struct lyd_node *prev; /**< pointer to the previous sibling node (last sibling if there is none) */
413 struct ly_attr *attr;
414
415#ifdef LY_ENABLED_LYD_PRIV
416 void *priv; /**< private user data, not used by libyang */
417#endif
418
419 struct lyd_node *child; /**< pointer to the child node (NULL if there are none) */
420 const char *name;
421 LYD_FORMAT format;
422 struct ly_prefix prefix; /**< name prefix */
423 struct ly_prefix *val_prefs; /**< list of prefixes in the value ([sized array](@ref sizedarrays)) */
424 const char *value; /**< original value */
425 const struct ly_ctx *ctx; /**< libyang context */
426};
427
428/**
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200429 * @defgroup children_options Children traversal options.
430 * @ingroup datatree
431 */
432
433#define LYD_CHILDREN_SKIP_KEYS 0x01 /**< If list children are returned, skip its keys. */
434
435/** @} children_options */
436
437/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200438 * @brief Get the node's children list if any.
439 *
440 * Decides the node's type and in case it has a children list, returns it.
441 * @param[in] node Node to check.
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200442 * @param[in] options Bitmask of options, see @ref
Radek Krejcie7b95092019-05-15 11:03:07 +0200443 * @return Pointer to the first child node (if any) of the \p node.
444 */
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200445struct lyd_node *lyd_node_children(const struct lyd_node *node, int options);
Radek Krejcie7b95092019-05-15 11:03:07 +0200446
447/**
Michal Vaskoc193ce92020-03-06 11:04:48 +0100448 * @brief Get the owner module of the data node. It is the module of the top-level schema node. Generally,
449 * in case of augments it is the target module, recursively, otherwise it is the module where the data node is defined.
450 *
451 * @param[in] node Data node to examine.
452 * @return Module owner of the node.
453 */
454const struct lys_module *lyd_owner_module(const struct lyd_node *node);
455
456/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200457 * @brief Learn the length of LYB data.
458 *
459 * @param[in] data LYB data to examine.
460 * @return Length of the LYB data chunk,
461 * @return -1 on error.
462 */
463int lyd_lyb_data_length(const char *data);
464
465/**
Michal Vaskoc0004272020-08-06 08:32:34 +0200466 * @brief Copy anydata value from one node to another. Target value is freed first.
467 *
468 * @param[in,out] trg Target node.
469 * @param[in] value Source value, may be NULL when the target value is only freed.
470 * @param[in] value_type Source value type.
471 * @return LY_ERR value.
472 */
473LY_ERR lyd_any_copy_value(struct lyd_node *trg, const union lyd_any_value *value, LYD_ANYDATA_VALUETYPE value_type);
474
475/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200476 * @brief Create a new inner node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +0100477 *
478 * @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 +0100479 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100480 * @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 +0200481 * @param[out] node Optional created node.
482 * @return LY_ERR value.
Michal Vasko013a8182020-03-03 10:46:53 +0100483 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200484LY_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 +0100485
486/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200487 * @brief Create a new list node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +0100488 *
489 * @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 +0100490 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100491 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200492 * @param[out] node Optional created node.
Michal Vasko013a8182020-03-03 10:46:53 +0100493 * @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 +0200494 * or identityref value, the JSON format is expected (module names instead of prefixes). No keys are expected for
495 * key-less lists.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200496 * @return LY_ERR value.
Michal Vasko013a8182020-03-03 10:46:53 +0100497 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200498LY_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 +0100499
500/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200501 * @brief Create a new list node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +0100502 *
503 * @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 +0100504 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100505 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
506 * @param[in] keys All key values predicate in the form of "[key1='val1'][key2='val2']...", they do not have to be ordered.
507 * 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 +0200508 * Use NULL or string of length 0 in case of key-less list.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200509 * @param[out] node Optional created node.
510 * @return LY_ERR value.
Michal Vasko013a8182020-03-03 10:46:53 +0100511 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200512LY_ERR lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
513 struct lyd_node **node);
Michal Vasko013a8182020-03-03 10:46:53 +0100514
515/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200516 * @brief Create a new term node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +0100517 *
518 * @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 +0100519 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100520 * @param[in] name Schema node name of the new data node. The node can be #LYS_LEAF or #LYS_LEAFLIST.
521 * @param[in] val_str String form of the value of the node being created. In case of an instance-identifier or identityref
522 * value, the JSON format is expected (module names instead of prefixes).
Michal Vasko3a41dff2020-07-15 14:30:28 +0200523 * @param[out] node Optional created node.
524 * @return LY_ERR value.
Michal Vasko013a8182020-03-03 10:46:53 +0100525 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200526LY_ERR lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
527 struct lyd_node **node);
Michal Vasko013a8182020-03-03 10:46:53 +0100528
529/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200530 * @brief Create a new any node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +0100531 *
532 * @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 +0100533 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100534 * @param[in] name Schema node name of the new data node. The node can be #LYS_ANYDATA or #LYS_ANYXML.
535 * @param[in] value Value to be directly assigned to the node. Expected type is determined by @p value_type.
536 * @param[in] value_type Type of the provided value in @p value.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200537 * @param[out] node Optional created node.
538 * @return LY_ERR value.
Michal Vasko013a8182020-03-03 10:46:53 +0100539 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200540LY_ERR lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
541 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node);
Michal Vasko013a8182020-03-03 10:46:53 +0100542
543/**
Michal Vaskod86997b2020-05-26 15:19:54 +0200544 * @brief Create new metadata for a data node.
545 *
546 * @param[in] parent Parent node for the metadata being created.
Michal Vasko00cbf532020-06-15 13:58:47 +0200547 * @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 +0200548 * @param[in] name Annotation name of the new metadata. It can include the annotation module as the prefix.
549 * 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 +0200550 * @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 +0200551 * value, the JSON format is expected (module names instead of prefixes).
Michal Vasko3a41dff2020-07-15 14:30:28 +0200552 * @param[out] meta Optional created metadata.
553 * @return LY_ERR value.
Michal Vaskod86997b2020-05-26 15:19:54 +0200554 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200555LY_ERR lyd_new_meta(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
556 struct lyd_meta **meta);
Michal Vaskod86997b2020-05-26 15:19:54 +0200557
558/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200559 * @brief Create a new opaque node in the data tree.
560 *
561 * @param[in] parent Parent node for the node beaing created. NULL in case of creating a top level element.
562 * @param[in] ctx libyang context. If NULL, @p parent context will be used.
563 * @param[in] name Node name.
564 * @param[in] value Node value, may be NULL.
565 * @param[in] module_name Node module name.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200566 * @param[out] node Optional created node.
567 * @return LY_ERR value.
Michal Vasko00cbf532020-06-15 13:58:47 +0200568 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200569LY_ERR lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
570 const char *module_name, struct lyd_node **node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200571
572/**
573 * @brief Create new attribute for an opaque data node.
574 *
575 * @param[in] parent Parent opaque node for the attribute being created.
576 * @param[in] module Module name of the attribute being created. There may be none.
577 * @param[in] name Attribute name. It can include the module name as the prefix.
578 * @param[in] val_str String value of the attribute. Is stored directly.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200579 * @param[out] attr Optional created attribute.
580 * @return LY_ERR value.
Michal Vasko00cbf532020-06-15 13:58:47 +0200581 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200582LY_ERR lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *val_str,
583 struct ly_attr **attr);
Michal Vasko00cbf532020-06-15 13:58:47 +0200584
585/**
586 * @defgroup pathoptions Data path creation options
587 * @ingroup datatree
588 *
589 * Various options to change lyd_new_path*() behavior.
590 *
591 * Default behavior:
592 * - if the target node already exists (and is not default), an error is returned.
593 * - the whole path to the target node is created (with any missing parents) if necessary.
594 * - RPC output schema children are completely ignored in all modules. Input is searched and nodes created normally.
595 * @{
596 */
597
598#define LYD_NEWOPT_UPDATE 0x01 /**< If the target node exists, is a leaf, and it is updated with a new value or its
599 default flag is changed, it is returned. If the target node exists and is not
600 a leaf or generally no change occurs in the @p parent tree, NULL is returned and
601 no error set. */
602#define LYD_NEWOPT_OUTPUT 0x02 /**< Changes the behavior to ignoring RPC/action input schema nodes and using only
603 output ones. */
604#define LYD_NEWOPT_OPAQ 0x04 /**< Enables the creation of opaque nodes with some specific rules. If the __last node__
605 in the path is not uniquely defined ((leaf-)list without a predicate) or has an
606 invalid value (leaf/leaf-list), it is created as opaque. */
607
608/** @} pathoptions */
609
610/**
611 * @brief Create a new node in the data tree based on a path. Cannot be used for anyxml/anydata nodes,
612 * for those use ::lyd_new_path_any.
613 *
614 * If @p path points to a list key and the list instance does not exist, the key value from the predicate is used
615 * and @p value is ignored. Also, if a leaf-list is being created and both a predicate is defined in @p path
616 * and @p value is set, the predicate is preferred.
617 *
618 * @param[in] parent Data parent to add to/modify, can be NULL.
619 * @param[in] ctx libyang context, must be set if @p parent is NULL.
620 * @param[in] path Path to create (TODO ref path).
621 * @param[in] value Value of the new leaf/leaf-list. For other node types, it is ignored.
622 * @param[in] options Bitmask of options, see @ref pathoptions.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200623 * @param[out] node Optional first created node.
624 * @return LY_ERR value.
Michal Vasko00cbf532020-06-15 13:58:47 +0200625 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200626LY_ERR lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value,
627 int options, struct lyd_node **node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200628
629/**
630 * @brief Create a new node in the data tree based on a path. All node types can be created.
631 *
632 * If @p path points to a list key and the list instance does not exist, the key value from the predicate is used
633 * and @p value is ignored. Also, if a leaf-list is being created and both a predicate is defined in @p path
634 * and @p value is set, the predicate is preferred.
635 *
636 * @param[in] parent Data parent to add to/modify, can be NULL.
637 * @param[in] ctx libyang context, must be set if @p parent is NULL.
638 * @param[in] path Path to create (TODO ref path).
639 * @param[in] value Value of the new leaf/leaf-list/anyxml/anydata. For other node types, it is ignored.
640 * @param[in] value_type Anyxml/anydata node @p value type.
641 * @param[in] options Bitmask of options, see @ref pathoptions.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200642 * @param[out] node Optional first created node.
643 * @return LY_ERR value.
Michal Vasko00cbf532020-06-15 13:58:47 +0200644 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200645LY_ERR lyd_new_path_any(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
646 LYD_ANYDATA_VALUETYPE value_type, int options, struct lyd_node **node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200647
648/**
649 * @brief Create a new node in the data tree based on a path. All node types can be created.
650 *
651 * If @p path points to a list key and the list instance does not exist, the key value from the predicate is used
652 * and @p value is ignored. Also, if a leaf-list is being created and both a predicate is defined in @p path
653 * and @p value is set, the predicate is preferred.
654 *
655 * @param[in] parent Data parent to add to/modify, can be NULL.
656 * @param[in] ctx libyang context, must be set if @p parent is NULL.
657 * @param[in] path Path to create (TODO ref path).
658 * @param[in] value Value of the new leaf/leaf-list/anyxml/anydata. For other node types, it is ignored.
659 * @param[in] value_type Anyxml/anydata node @p value type.
660 * @param[in] options Bitmask of options, see @ref pathoptions.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200661 * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node.
662 * @param[out] new_node Optional last node created.
Michal Vasko00cbf532020-06-15 13:58:47 +0200663 * @return LY_ERR value.
664 */
665LY_ERR lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
666 LYD_ANYDATA_VALUETYPE value_type, int options, struct lyd_node **new_parent, struct lyd_node **new_node);
667
668/**
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200669 * @defgroup implicitoptions Implicit node creation options
670 * @ingroup datatree
671 *
672 * Various options to change lyd_new_implicit*() behavior.
673 *
674 * Default behavior:
675 * - both configuration and state missing implicit nodes are added.
676 * - all implicit node types are added (non-presence containers, default leaves, and default leaf-lists).
677 * @{
678 */
679
Michal Vasko44b19a12020-08-07 09:21:30 +0200680#define LYD_IMPLICIT_NO_STATE 0x01 /**< Do not add any implicit state nodes. */
681#define LYD_IMPLICIT_NO_CONFIG 0x02 /**< Do not add any implicit config nodes. */
682#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 +0200683 containers. */
684
685/** @} implicitoptions */
686
687/**
688 * @brief Add any missing implicit nodes into a data subtree.
689 *
690 * @param[in] tree Tree to add implicit nodes into.
691 * @param[in] implicit_options Options for implicit node creation, see @ref implicitoptions.
692 * @param[out] diff Optional diff with any created nodes.
693 * @return LY_ERR value.
694 */
695LY_ERR lyd_new_implicit_tree(struct lyd_node *tree, int implicit_options, struct lyd_node **diff);
696
697/**
698 * @brief Add any missing implicit nodes.
699 *
700 * @param[in,out] tree Tree to add implicit nodes into.
701 * @param[in] ctx libyang context, must be set only if @p tree is an empty tree.
702 * @param[in] implicit_options Options for implicit node creation, see @ref implicitoptions.
703 * @param[out] diff Optional diff with any created nodes.
704 * @return LY_ERR value.
705 */
706LY_ERR lyd_new_implicit_all(struct lyd_node **tree, const struct ly_ctx *ctx, int implicit_options, struct lyd_node **diff);
707
708/**
709 * @brief Add any missing implicit nodes of one module.
710 *
711 * @param[in,out] tree Tree to add implicit nodes into.
712 * @param[in] module Module whose implicit nodes to create.
713 * @param[in] implicit_options Options for implicit node creation, see @ref implicitoptions.
714 * @param[out] diff Optional diff with any created nodes.
715 * @return LY_ERR value.
716 */
717LY_ERR lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, int implicit_options,
718 struct lyd_node **diff);
719
720/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200721 * @brief Change the value of a term (leaf or leaf-list) node.
722 *
723 * Node changed this way is always considered explicitly set, meaning its default flag
724 * is always cleared.
725 *
726 * @param[in] term Term node to change.
727 * @param[in] val_str New value to set, any prefixes are expected in JSON format.
728 * @return LY_SUCCESS if value was changed,
729 * @return LY_EEXIST if value was the same and only the default flag was cleared,
730 * @return LY_ENOT if the values were equal and no change occured,
731 * @return LY_ERR value on other errors.
732 */
733LY_ERR lyd_change_term(struct lyd_node *term, const char *val_str);
734
735/**
Michal Vasko41586352020-07-13 13:54:25 +0200736 * @brief Change the value of a metadata instance.
737 *
738 * @param[in] ctx libyang context.
739 * @param[in] meta Metadata to change.
740 * @param[in] val_str New value to set, any prefixes are expected in JSON format.
741 * @return LY_SUCCESS if value was changed,
742 * @return LY_ENOT if the values were equal and no change occured,
743 * @return LY_ERR value on other errors.
744 */
745LY_ERR lyd_change_meta(struct lyd_meta *meta, const char *val_str);
746
747/**
Michal Vaskob104f112020-07-17 09:54:54 +0200748 * @brief Insert a child into a parent.
Michal Vaskof03ed032020-03-04 13:31:44 +0100749 *
750 * - if the node is part of some other tree, it is automatically unlinked.
751 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
752 *
753 * @param[in] parent Parent node to insert into.
754 * @param[in] node Node to insert.
755 * @return LY_SUCCESS on success.
756 * @return LY_ERR error on error.
757 */
Michal Vaskob104f112020-07-17 09:54:54 +0200758LY_ERR lyd_insert_child(struct lyd_node *parent, struct lyd_node *node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100759
760/**
Michal Vaskob104f112020-07-17 09:54:54 +0200761 * @brief Insert a node into siblings.
Michal Vaskob1b5c262020-03-05 14:29:47 +0100762 *
763 * - if the node is part of some other tree, it is automatically unlinked.
764 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
765 *
Michal Vaskob104f112020-07-17 09:54:54 +0200766 * @param[in] sibling Siblings to insert into, can even be NULL.
Michal Vaskob1b5c262020-03-05 14:29:47 +0100767 * @param[in] node Node to insert.
Michal Vaskob104f112020-07-17 09:54:54 +0200768 * @param[out] first Optionally return the first sibling after insertion. Can be the address of @p sibling.
Michal Vaskob1b5c262020-03-05 14:29:47 +0100769 * @return LY_SUCCESS on success.
770 * @return LY_ERR error on error.
771 */
Michal Vaskob104f112020-07-17 09:54:54 +0200772LY_ERR lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100773
774/**
Michal Vaskob104f112020-07-17 09:54:54 +0200775 * @brief Insert a node before another node, can be used only for user-ordered nodes.
Michal Vaskof03ed032020-03-04 13:31:44 +0100776 *
777 * - if the node is part of some other tree, it is automatically unlinked.
778 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
779 *
780 * @param[in] sibling Sibling node to insert before.
781 * @param[in] node Node to insert.
782 * @return LY_SUCCESS on success.
783 * @return LY_ERR error on error.
784 */
785LY_ERR lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node);
786
787/**
Michal Vaskob104f112020-07-17 09:54:54 +0200788 * @brief Insert a node after another node, can be used only for user-ordered nodes.
Michal Vaskof03ed032020-03-04 13:31:44 +0100789 *
790 * - if the node is part of some other tree, it is automatically unlinked.
791 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
792 *
793 * @param[in] sibling Sibling node to insert after.
794 * @param[in] node Node to insert.
795 * @return LY_SUCCESS on success.
796 * @return LY_ERR error on error.
797 */
798LY_ERR lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node);
799
800/**
801 * @brief Unlink the specified data subtree.
802 *
803 * @param[in] node Data tree node to be unlinked (together with all the children).
804 */
805void lyd_unlink_tree(struct lyd_node *node);
806
807/**
Radek Krejcib0849a22019-07-25 12:31:04 +0200808 * @brief Free all the nodes (even parents of the node) in the data tree.
Radek Krejcie7b95092019-05-15 11:03:07 +0200809 *
810 * @param[in] node Any of the nodes inside the tree.
811 */
812void lyd_free_all(struct lyd_node *node);
813
814/**
Michal Vasko3a41dff2020-07-15 14:30:28 +0200815 * @brief Free all the sibling nodes (preceding as well as succeeding).
Radek Krejcib0849a22019-07-25 12:31:04 +0200816 *
817 * @param[in] node Any of the sibling nodes to free.
818 */
Michal Vaskof03ed032020-03-04 13:31:44 +0100819void lyd_free_siblings(struct lyd_node *node);
Radek Krejcib0849a22019-07-25 12:31:04 +0200820
821/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200822 * @brief Free (and unlink) the specified data (sub)tree.
823 *
Radek Krejcie7b95092019-05-15 11:03:07 +0200824 * @param[in] node Root of the (sub)tree to be freed.
825 */
826void lyd_free_tree(struct lyd_node *node);
827
828/**
Michal Vasko3a41dff2020-07-15 14:30:28 +0200829 * @brief Free a single metadata instance.
Radek Krejcie7b95092019-05-15 11:03:07 +0200830 *
Michal Vasko3a41dff2020-07-15 14:30:28 +0200831 * @param[in] meta Metadata to free.
Radek Krejcie7b95092019-05-15 11:03:07 +0200832 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200833void lyd_free_meta_single(struct lyd_meta *meta);
Michal Vasko52927e22020-03-16 17:26:14 +0100834
835/**
Michal Vasko3a41dff2020-07-15 14:30:28 +0200836 * @brief Free the metadata instance with any following instances.
837 *
838 * @param[in] meta Metadata to free.
839 */
840void lyd_free_meta_siblings(struct lyd_meta *meta);
841
842/**
843 * @brief Free a single attribute.
Michal Vasko52927e22020-03-16 17:26:14 +0100844 *
845 * @param[in] ctx Context where the attributes were created.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200846 * @param[in] attr Attribute to free.
Michal Vasko52927e22020-03-16 17:26:14 +0100847 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200848void ly_free_attr_single(const struct ly_ctx *ctx, struct ly_attr *attr);
849
850/**
851 * @brief Free the attribute with any following attributes.
852 *
853 * @param[in] ctx Context where the attributes were created.
854 * @param[in] attr First attribute to free.
855 */
856void ly_free_attr_siblings(const struct ly_ctx *ctx, struct ly_attr *attr);
Radek Krejcie7b95092019-05-15 11:03:07 +0200857
Radek Krejci084289f2019-07-09 17:35:30 +0200858/**
859 * @brief Check type restrictions applicable to the particular leaf/leaf-list with the given string @p value.
860 *
861 * The given node is not modified in any way - it is just checked if the @p value can be set to the node.
862 *
863 * If there is no data node instance and you are fine with checking just the type's restrictions without the
864 * data tree context (e.g. for the case of require-instance restriction), use lys_value_validate().
865 *
866 * @param[in] ctx libyang context for logging (function does not log errors when @p ctx is NULL)
867 * @param[in] node Data node for the @p value.
Michal Vaskof937cfe2020-08-03 16:07:12 +0200868 * @param[in] value String value to be checked, it is expected to be in JSON format.
Radek Krejci084289f2019-07-09 17:35:30 +0200869 * @param[in] value_len Length of the given @p value (mandatory).
Michal Vaskof03ed032020-03-04 13:31:44 +0100870 * @param[in] tree Data tree (e.g. when validating RPC/Notification) where the required data instance (leafref target,
871 * instance-identifier) can be placed. NULL in case the data tree is not yet complete,
872 * then LY_EINCOMPLETE can be returned.
Michal Vasko3701af52020-08-03 14:29:38 +0200873 * @param[out] realtype Optional real type of the value.
Radek Krejci084289f2019-07-09 17:35:30 +0200874 * @return LY_SUCCESS on success
875 * @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).
876 * @return LY_ERR value if an error occurred.
877 */
Michal Vasko44685da2020-03-17 15:38:06 +0100878LY_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 +0200879 const struct lyd_node *tree, struct lysc_type **realtype);
Radek Krejci084289f2019-07-09 17:35:30 +0200880
881/**
882 * @brief Compare the node's value with the given string value. The string value is first validated according to the node's type.
883 *
884 * @param[in] node Data node to compare.
885 * @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 +0200886 * it is validated and canonized if possible. But it is expected to be in JSON format.
Radek Krejci084289f2019-07-09 17:35:30 +0200887 * @param[in] value_len Length of the given @p value (mandatory).
Michal Vaskof03ed032020-03-04 13:31:44 +0100888 * @param[in] tree Data tree (e.g. when validating RPC/Notification) where the required data instance (leafref target,
889 * instance-identifier) can be placed. NULL in case the data tree is not yet complete,
890 * then LY_EINCOMPLETE can be returned.
Radek Krejci084289f2019-07-09 17:35:30 +0200891 * @return LY_SUCCESS on success
892 * @return LY_EINCOMPLETE in case of success when the @p trees is not provided and it was needed to finish the validation of
893 * the given string @p value (e.g. due to require-instance).
Michal Vaskob3ddccb2020-07-09 15:43:05 +0200894 * @return LY_ENOT if the values do not match.
Radek Krejci084289f2019-07-09 17:35:30 +0200895 * @return LY_ERR value if an error occurred.
896 */
Michal Vaskof937cfe2020-08-03 16:07:12 +0200897LY_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 +0200898
Radek Krejci576b23f2019-07-12 14:06:32 +0200899/**
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200900 * @defgroup datacompareoptions Data compare options
901 * @ingroup datatree
902 *
Radek Krejci22ebdba2019-07-25 13:59:43 +0200903 * Various options to change the lyd_compare() behavior.
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200904 */
905#define LYD_COMPARE_FULL_RECURSION 0x01 /* lists and containers are the same only in case all they children
906 (subtree, so direct as well as indirect children) are the same. By default,
907 containers are the same in case of the same schema node and lists are the same
908 in case of equal keys (keyless lists do the full recursion comparison all the time). */
909#define LYD_COMPARE_DEFAULTS 0x02 /* By default, implicit and explicit default nodes are considered to be equal. This flag
910 changes this behavior and implicit (automatically created default node) and explicit
911 (explicitly created node with the default value) default nodes are considered different. */
Michal Vasko60ea6352020-06-29 13:39:39 +0200912/** @} datacompareoptions */
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200913
914/**
915 * @brief Compare 2 data nodes if they are equivalent.
916 *
917 * @param[in] node1 The first node to compare.
918 * @param[in] node2 The second node to compare.
Radek Krejcic5ad9652019-09-11 11:31:51 +0200919 * @param[in] options Various @ref datacompareoptions.
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200920 * @return LY_SUCCESS if the nodes are equivalent.
921 * @return LY_ENOT if the nodes are not equivalent.
922 */
Michal Vasko8f359bf2020-07-28 10:41:15 +0200923LY_ERR lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, int options);
924
925/**
926 * @brief Compare 2 lists of siblings if they are equivalent.
927 *
928 * @param[in] node1 The first sibling list to compare.
929 * @param[in] node2 The second sibling list to compare.
930 * @param[in] options Various @ref datacompareoptions.
931 * @return LY_SUCCESS if all the siblings are equivalent.
932 * @return LY_ENOT if the siblings are not equivalent.
933 */
934LY_ERR lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, int options);
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200935
936/**
Michal Vasko21725742020-06-29 11:49:25 +0200937 * @brief Compare 2 metadata.
938 *
939 * @param[in] meta1 First metadata.
940 * @param[in] meta2 Second metadata.
941 * @return LY_SUCCESS if the metadata are equivalent.
942 * @return LY_ENOT if not.
943 */
944LY_ERR lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2);
945
946/**
Radek Krejci22ebdba2019-07-25 13:59:43 +0200947 * @defgroup dupoptions Data duplication options
948 * @ingroup datatree
949 *
950 * Various options to change lyd_dup() behavior.
951 *
952 * Default behavior:
953 * - only the specified node is duplicated without siblings, parents, or children.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200954 * - all the metadata of the duplicated nodes are also duplicated.
Radek Krejci22ebdba2019-07-25 13:59:43 +0200955 * @{
956 */
957
958#define LYD_DUP_RECURSIVE 0x01 /**< Duplicate not just the node but also all the children. Note that
959 list's keys are always duplicated. */
Michal Vaskoa29a5762020-06-23 13:28:49 +0200960#define LYD_DUP_NO_META 0x02 /**< Do not duplicate metadata of any node. */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200961#define LYD_DUP_WITH_PARENTS 0x04 /**< If a nested node is being duplicated, duplicate also all the parents.
962 Keys are also duplicated for lists. Return value does not change! */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200963#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 +0200964 its validation/default node state. */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200965
966/** @} dupoptions */
967
968/**
969 * @brief Create a copy of the specified data tree \p node. Schema references are kept the same.
970 *
Radek Krejci22ebdba2019-07-25 13:59:43 +0200971 * @param[in] node Data tree node to be duplicated.
972 * @param[in] parent Optional parent node where to connect the duplicated node(s).
Michal Vasko3a41dff2020-07-15 14:30:28 +0200973 * If set in combination with LYD_DUP_WITH_PARENTS, the parents chain is duplicated until it comes to and connects with
974 * the @p parent.
Radek Krejci22ebdba2019-07-25 13:59:43 +0200975 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200976 * @param[out] dup Optional created copy of the node. Note that in case the parents chain is duplicated for the duplicated
977 * node(s) (when LYD_DUP_WITH_PARENTS used), the first duplicated node is still returned.
978 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +0200979 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200980LY_ERR lyd_dup_single(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup);
981
982/**
983 * @brief Create a copy of the specified data tree \p node with any following siblings. Schema references are kept the same.
984 *
985 * @param[in] node Data tree node to be duplicated.
986 * @param[in] parent Optional parent node where to connect the duplicated node(s).
987 * If set in combination with LYD_DUP_WITH_PARENTS, the parents chain is duplicated until it comes to and connects with
988 * the @p parent.
989 * @param[in] options Bitmask of options flags, see @ref dupoptions.
990 * @param[out] dup Optional created copy of the node. Note that in case the parents chain is duplicated for the duplicated
991 * node(s) (when LYD_DUP_WITH_PARENTS used), the first duplicated node is still returned.
992 * @return LY_ERR value.
993 */
994LY_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 +0200995
996/**
Michal Vasko25a32822020-07-09 15:48:22 +0200997 * @brief Create a copy of the metadata.
998 *
999 * @param[in] meta Metadata to copy.
Michal Vasko3a41dff2020-07-15 14:30:28 +02001000 * @param[in] parent Node where to append the new metadata.
1001 * @param[out] dup Optional created metadata copy.
1002 * @return LY_ERR value.
Michal Vasko25a32822020-07-09 15:48:22 +02001003 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001004LY_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 +02001005
1006/**
Michal Vasko4490d312020-06-16 13:08:55 +02001007 * @defgroup mergeoptions Data merge options.
1008 * @ingroup datatree
Radek Krejci576b23f2019-07-12 14:06:32 +02001009 *
Michal Vasko4490d312020-06-16 13:08:55 +02001010 * Various options to change lyd_merge() behavior.
1011 *
1012 * Default behavior:
1013 * - source data tree is not modified in any way,
Michal Vasko3a41dff2020-07-15 14:30:28 +02001014 * - any default nodes in the source are ignored if there are explicit nodes in the target.
Michal Vasko4490d312020-06-16 13:08:55 +02001015 * @{
1016 */
1017
1018#define LYD_MERGE_DESTRUCT 0x01 /**< Spend source data tree in the function, it cannot be used afterwards! */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001019#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 +02001020
1021/** @} mergeoptions */
1022
1023/**
Michal Vasko3a41dff2020-07-15 14:30:28 +02001024 * @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 +02001025 * is called on the resulting data tree (data from more cases may be present, default and non-default values).
1026 *
1027 * @param[in,out] target Target data tree to merge into, must be a top-level tree.
1028 * @param[in] source Source data tree to merge, must be a top-level tree.
1029 * @param[in] options Bitmask of option flags, see @ref mergeoptions.
1030 * @return LY_SUCCESS on success,
1031 * @return LY_ERR value on error.
1032 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001033LY_ERR lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, int options);
1034
1035/**
1036 * @brief Merge the source data tree with any following siblings into the target data tree. Merge may not be
1037 * complete until validation called on the resulting data tree (data from more cases may be present, default
1038 * and non-default values).
1039 *
1040 * @param[in,out] target Target data tree to merge into, must be a top-level tree.
1041 * @param[in] source Source data tree to merge, must be a top-level tree.
1042 * @param[in] options Bitmask of option flags, see @ref mergeoptions.
1043 * @return LY_SUCCESS on success,
1044 * @return LY_ERR value on error.
1045 */
1046LY_ERR lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, int options);
Michal Vasko4490d312020-06-16 13:08:55 +02001047
1048/**
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001049 * @defgroup diffoptions Data diff options.
1050 * @ingroup datatree
1051 *
1052 * Various options to change lyd_diff() behavior.
1053 *
1054 * Default behavior:
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001055 * - any default nodes are treated as non-existent and ignored.
1056 * @{
1057 */
1058
Michal Vasko3a41dff2020-07-15 14:30:28 +02001059#define LYD_DIFF_DEFAULTS 0x01 /**< Default nodes in the trees are not ignored but treated similarly to explicit
1060 nodes. Also, leaves and leaf-lists are added into diff even in case only their
1061 default flag (state) was changed. */
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001062
1063/** @} diffoptions */
1064
1065/**
1066 * @brief Learn the differences between 2 data trees.
1067 *
1068 * The resulting diff is represented as a data tree with specific metadata from the internal 'yang'
1069 * module. Most importantly, every node has an effective 'operation' metadata. If there is none
1070 * defined on the node, it inherits the operation from the nearest parent. Top-level nodes must
1071 * always have the 'operation' metadata defined. Additional metadata ('orig-default', 'value',
1072 * 'orig-value', 'key', 'orig-key') are used for storing more information about the value in the first
1073 * or the second tree.
1074 *
1075 * The diff tree is completely independent on the @p first and @p second trees, meaning all
1076 * the information about the change is stored in the diff and the trees are not needed.
1077 *
1078 * !! Caution !!
1079 * The diff tree should never be validated because it may easily not be valid! For example,
1080 * when data from one case branch are deleted and data from another branch created - data from both
1081 * branches are then stored in the diff tree simultaneously.
1082 *
1083 * @param[in] first First data tree.
1084 * @param[in] second Second data tree.
1085 * @param[in] options Bitmask of options flags, see @ref diffoptions.
1086 * @param[out] diff Generated diff.
1087 * @return LY_SUCCESS on success,
1088 * @return LY_ERR on error.
1089 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001090LY_ERR lyd_diff_tree(const struct lyd_node *first, const struct lyd_node *second, int options, struct lyd_node **diff);
1091
1092/**
1093 * @brief Learn the differences between 2 data trees including all the following siblings.
1094 *
1095 * @param[in] first First data tree.
1096 * @param[in] second Second data tree.
1097 * @param[in] options Bitmask of options flags, see @ref diffoptions.
1098 * @param[out] diff Generated diff.
1099 * @return LY_SUCCESS on success,
1100 * @return LY_ERR on error.
1101 */
1102LY_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 +02001103
1104/**
1105 * @brief Callback for diff nodes.
1106 *
1107 * @param[in] diff_node Diff node.
1108 * @param[in] data_node Matching node in data.
1109 * @param[in] cb_data Arbitrary callback data.
1110 * @return LY_ERR value.
1111 */
1112typedef LY_ERR (*lyd_diff_cb)(const struct lyd_node *diff_node, struct lyd_node *data_node, void *cb_data);
1113
1114/**
Michal Vasko3a41dff2020-07-15 14:30:28 +02001115 * @brief Apply the whole diff on a data tree but restrict the operation to one module.
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001116 *
1117 * @param[in,out] data Data to apply the diff on.
1118 * @param[in] diff Diff to apply.
1119 * @param[in] mod Module, whose diff/data only to consider, NULL for all modules.
1120 * @param[in] diff_cb Optional diff callback that will be called for every changed node.
1121 * @param[in] cb_data Arbitrary callback data.
1122 * @return LY_SUCCESS on success,
1123 * @return LY_ERR on error.
1124 */
1125LY_ERR lyd_diff_apply_module(struct lyd_node **data, const struct lyd_node *diff, const struct lys_module *mod,
1126 lyd_diff_cb diff_cb, void *cb_data);
1127
1128/**
Michal Vasko3a41dff2020-07-15 14:30:28 +02001129 * @brief Apply the whole diff tree on a data tree.
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001130 *
1131 * @param[in,out] data Data to apply the diff on.
1132 * @param[in] diff Diff to apply.
1133 * @return LY_SUCCESS on success,
1134 * @return LY_ERR on error.
1135 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001136LY_ERR lyd_diff_apply_all(struct lyd_node **data, const struct lyd_node *diff);
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001137
1138/**
Michal Vaskoe6323f62020-07-09 15:49:02 +02001139 * @brief Merge 2 diffs into each other but restrict the operation to one module.
1140 *
1141 * The diffs must be possible to be merged, which is guaranteed only if the source diff was
1142 * created on data that had the target diff applied on them. In other words, this sequence is legal
1143 *
Michal Vasko04f85912020-08-07 12:14:58 +02001144 * 1) diff1 from data1 and data2 -> data11 from apply diff1 on data1 -> diff2 from data11 and data3 ->
1145 * -> data 33 from apply diff2 on data1
Michal Vaskoe6323f62020-07-09 15:49:02 +02001146 *
1147 * and reusing these diffs
1148 *
Michal Vasko04f85912020-08-07 12:14:58 +02001149 * 2) diff11 from merge diff1 and diff2 -> data33 from apply diff11 on data1
Michal Vaskoe6323f62020-07-09 15:49:02 +02001150 *
Michal Vaskofb737aa2020-08-06 13:53:53 +02001151 * @param[in,out] diff Target diff to merge into.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001152 * @param[in] src_diff Source diff.
1153 * @param[in] mod Module, whose diff only to consider, NULL for all modules.
1154 * @param[in] diff_cb Optional diff callback that will be called for every changed node.
1155 * @param[in] cb_data Arbitrary callback data.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001156 * @return LY_SUCCESS on success,
1157 * @return LY_ERR on error.
1158 */
Michal Vaskofb737aa2020-08-06 13:53:53 +02001159LY_ERR lyd_diff_merge_module(struct lyd_node **diff, const struct lyd_node *src_diff, const struct lys_module *mod,
1160 lyd_diff_cb diff_cb, void *cb_data);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001161
1162/**
Michal Vasko04f85912020-08-07 12:14:58 +02001163 * @brief Merge 2 diff trees into each other.
1164 *
1165 * @param[in,out] diff_first Target diff first sibling to merge into.
1166 * @param[in] diff_parent Target diff parent to merge into.
1167 * @param[in] src_sibling Source diff sibling to merge.
1168 * @param[in] diff_cb Optional diff callback that will be called for every changed node.
1169 * @param[in] cb_data Arbitrary callback data.
1170 * @return LY_SUCCESS on success,
1171 * @return LY_ERR on error.
1172 */
1173LY_ERR lyd_diff_merge_tree(struct lyd_node **diff_first, struct lyd_node *diff_parent, const struct lyd_node *src_sibling,
1174 lyd_diff_cb diff_cb, void *cb_data);
1175
1176/**
Michal Vaskoe6323f62020-07-09 15:49:02 +02001177 * @brief Merge 2 diffs into each other.
1178 *
Michal Vaskoe6323f62020-07-09 15:49:02 +02001179 * @param[in,out] diff Target diff to merge into.
Michal Vaskofb737aa2020-08-06 13:53:53 +02001180 * @param[in] src_diff Source diff.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001181 * @return LY_SUCCESS on success,
1182 * @return LY_ERR on error.
1183 */
Michal Vaskofb737aa2020-08-06 13:53:53 +02001184LY_ERR lyd_diff_merge_all(struct lyd_node **diff, const struct lyd_node *src_diff);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001185
1186/**
Michal Vasko4231fb62020-07-13 13:54:47 +02001187 * @brief Reverse a diff and make the opposite changes. Meaning change create to delete, delete to create,
1188 * or move from place A to B to move from B to A and so on.
1189 *
1190 * @param[in] src_diff Diff to reverse.
1191 * @param[out] diff Reversed diff.
1192 * @return LY_SUCCESS on success.
1193 * @return LY_ERR on error.
1194 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001195LY_ERR lyd_diff_reverse_all(const struct lyd_node *src_diff, struct lyd_node **diff);
Michal Vasko4231fb62020-07-13 13:54:47 +02001196
1197/**
Michal Vasko4490d312020-06-16 13:08:55 +02001198 * @brief Find the target in data of a compiled ly_path structure (instance-identifier).
1199 *
1200 * @param[in] path Compiled path structure.
Michal Vaskof03ed032020-03-04 13:31:44 +01001201 * @param[in] tree Data tree to be searched.
Michal Vasko4490d312020-06-16 13:08:55 +02001202 * @return Found target node,
1203 * @return NULL if not found.
Radek Krejci576b23f2019-07-12 14:06:32 +02001204 */
Michal Vasko004d3152020-06-11 19:59:22 +02001205const struct lyd_node_term *lyd_target(const struct ly_path *path, const struct lyd_node *tree);
Radek Krejci084289f2019-07-09 17:35:30 +02001206
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001207/**
1208 * @brief Get string value of a term data \p node.
1209 *
1210 * @param[in] node Data tree node with the value.
1211 * @param[out] dynamic Whether the string value was dynmically allocated.
1212 * @return String value of @p node, if @p dynamic, needs to be freed.
1213 */
1214const char *lyd_value2str(const struct lyd_node_term *node, int *dynamic);
1215
1216/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001217 * @brief Get string value of a metadata \p meta.
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001218 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001219 * @param[in] meta Metadata with the value.
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001220 * @param[out] dynamic Whether the string value was dynmically allocated.
Michal Vasko9f96a052020-03-10 09:41:45 +01001221 * @return String value of @p meta, if @p dynamic, needs to be freed.
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001222 */
Michal Vasko9f96a052020-03-10 09:41:45 +01001223const char *lyd_meta2str(const struct lyd_meta *meta, int *dynamic);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001224
1225/**
1226 * @brief Types of the different data paths.
1227 */
1228typedef enum {
Michal Vasko14654712020-02-06 08:35:21 +01001229 LYD_PATH_LOG, /**< Descriptive path format used in log messages */
Michal Vasko790b2bc2020-08-03 13:35:06 +02001230 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 +02001231} LYD_PATH_TYPE;
1232
1233/**
1234 * @brief Generate path of the given node in the requested format.
1235 *
1236 * @param[in] node Schema path of this node will be generated.
1237 * @param[in] pathtype Format of the path to generate.
1238 * @param[in,out] buffer Prepared buffer of the @p buflen length to store the generated path.
1239 * If NULL, memory for the complete path is allocated.
1240 * @param[in] buflen Size of the provided @p buffer.
1241 * @return NULL in case of memory allocation error, path of the node otherwise.
1242 * In case the @p buffer is NULL, the returned string is dynamically allocated and caller is responsible to free it.
1243 */
1244char *lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen);
1245
Michal Vaskoe444f752020-02-10 12:20:06 +01001246/**
Michal Vasko25a32822020-07-09 15:48:22 +02001247 * @brief Find a specific metadata.
1248 *
1249 * @param[in] first First metadata to consider.
1250 * @param[in] module Module of the metadata definition, may be NULL if @p name includes a prefix.
1251 * @param[in] name Name of the metadata to find, may not include a prefix (module name) if @p module is set.
1252 * @return Found metadata,
1253 * @return NULL if not found.
1254 */
1255struct lyd_meta *lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name);
1256
1257/**
Michal Vaskoda859032020-07-14 12:20:14 +02001258 * @brief Search in the given siblings (NOT recursively) for the first target instance with the same value.
Michal Vaskoe444f752020-02-10 12:20:06 +01001259 * Uses hashes - should be used whenever possible for best performance.
1260 *
1261 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
1262 * @param[in] target Target node to find.
Michal Vasko9b368d32020-02-14 13:53:31 +01001263 * @param[out] match Can be NULL, otherwise the found data node.
Michal Vaskoe444f752020-02-10 12:20:06 +01001264 * @return LY_SUCCESS on success, @p match set.
1265 * @return LY_ENOTFOUND if not found, @p match set to NULL.
1266 * @return LY_ERR value if another error occurred.
1267 */
1268LY_ERR lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match);
1269
1270/**
Michal Vaskoe444f752020-02-10 12:20:06 +01001271 * @brief Search in the given siblings for the first schema instance.
1272 * Uses hashes - should be used whenever possible for best performance.
1273 *
1274 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
1275 * @param[in] schema Schema node of the data node to find.
Michal Vaskob104f112020-07-17 09:54:54 +02001276 * @param[in] key_or_value If it is NULL, the first schema node data instance is found. For nodes with many
1277 * instances, it can be set based on the type of @p schema:
Michal Vaskoe444f752020-02-10 12:20:06 +01001278 * LYS_LEAFLIST:
1279 * Searched instance value.
1280 * LYS_LIST:
Michal Vasko90932a92020-02-12 14:33:03 +01001281 * Searched instance key values in the form of "[key1='val1'][key2='val2']...".
1282 * The keys do not have to be ordered but all of them must be set.
1283 *
1284 * Note that any explicit values (leaf-list or list key values) will be canonized first
1285 * before comparison. But values that do not have a canonical value are expected to be in the
1286 * JSON format!
Michal Vaskof03ed032020-03-04 13:31:44 +01001287 * @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 +01001288 * @param[out] match Can be NULL, otherwise the found data node.
Michal Vaskoe444f752020-02-10 12:20:06 +01001289 * @return LY_SUCCESS on success, @p match set.
1290 * @return LY_ENOTFOUND if not found, @p match set to NULL.
1291 * @return LY_EINVAL if @p schema is a key-less list.
1292 * @return LY_ERR value if another error occurred.
1293 */
1294LY_ERR lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
1295 size_t val_len, struct lyd_node **match);
1296
Michal Vaskoccc02342020-05-21 10:09:21 +02001297/**
1298 * @brief Search in the given data for instances of nodes matching the provided XPath.
1299 *
Michal Vaskob104f112020-07-17 09:54:54 +02001300 * The expected format of the expression is ::LYD_JSON, meaning the first node in every path
Michal Vasko61ac2f62020-05-25 12:39:51 +02001301 * must have its module name as prefix or be the special `*` value for all the nodes.
1302 *
Michal Vasko19a30602020-05-25 10:40:19 +02001303 * If a list instance is being selected with all its key values specified (but not necessarily ordered)
1304 * in the form `list[key1='val1'][key2='val2'][key3='val3']` or a leaf-list instance in the form
1305 * `leaf-list[.='val']`, these instances are found using hashes with constant (*O(1)*) complexity
1306 * (unless they are defined in top-level). Other predicates can still follow the aforementioned ones.
1307 *
Michal Vaskoccc02342020-05-21 10:09:21 +02001308 * @param[in] ctx_node XPath context node.
1309 * @param[in] xpath Data XPath expression filtering the matching nodes. ::LYD_JSON format is expected.
1310 * @param[out] set Set of found data nodes. In case the result is a number, a string, or a boolean,
1311 * the returned set is empty.
1312 * @return LY_SUCCESS on success, @p set is returned.
1313 * @return LY_ERR value if an error occurred.
1314 */
1315LY_ERR lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set);
1316
Radek Krejcie7b95092019-05-15 11:03:07 +02001317#ifdef __cplusplus
1318}
1319#endif
1320
1321#endif /* LY_TREE_DATA_H_ */