blob: 553f4ccdf8db58fed2fa0e4083f535211fe8f2c9 [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
Radek Krejcie7b95092019-05-15 11:03:07 +020057 * START can be any of the lyd_node* types, NEXT and ELEM variables are expected
58 * to be pointers to a generic struct lyd_node.
59 *
60 * Since the next node is selected as part of #LYD_TREE_DFS_END, do not use
61 * continue statement between the #LYD_TREE_DFS_BEGIN and #LYD_TREE_DFS_END.
62 *
63 * Use with opening curly bracket '{' after the macro.
64 *
65 * @param START Pointer to the starting element processed first.
66 * @param NEXT Temporary storage, do not use.
67 * @param ELEM Iterator intended for use in the block.
68 */
69#define LYD_TREE_DFS_BEGIN(START, NEXT, ELEM) \
70 for ((ELEM) = (NEXT) = (START); \
71 (ELEM); \
72 (ELEM) = (NEXT))
73
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
79 * START can be any of the lyd_node* types, NEXT and ELEM variables are expected
80 * to be pointers to a generic struct lyd_node.
81 *
82 * Use with closing curly bracket '}' after the macro.
83 *
84 * @param START Pointer to the starting element processed first.
85 * @param NEXT Temporary storage, do not use.
86 * @param ELEM Iterator intended for use in the block.
87 */
88
89#define LYD_TREE_DFS_END(START, NEXT, ELEM) \
90 /* select element for the next run - children first */ \
Michal Vasko5bfd4be2020-06-23 13:26:19 +020091 (NEXT) = lyd_node_children((struct lyd_node*)ELEM, 0); \
Radek Krejcie7b95092019-05-15 11:03:07 +020092 if (!(NEXT)) { \
93 /* no children */ \
94 if ((ELEM) == (struct lyd_node*)(START)) { \
95 /* we are done, (START) has no children */ \
96 break; \
97 } \
98 /* try siblings */ \
99 (NEXT) = (ELEM)->next; \
100 } \
101 while (!(NEXT)) { \
102 /* parent is already processed, go to its sibling */ \
103 (ELEM) = (struct lyd_node*)(ELEM)->parent; \
104 /* no siblings, go back through parents */ \
105 if ((ELEM)->parent == (START)->parent) { \
106 /* we are done, no next element to process */ \
107 break; \
108 } \
109 (NEXT) = (ELEM)->next; \
110 }
111
112/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200113 * @brief Macro to get context from a data tree node.
114 */
Michal Vasko52927e22020-03-16 17:26:14 +0100115#define LYD_NODE_CTX(node) ((node)->schema ? (node)->schema->module->ctx : ((struct lyd_node_opaq *)(node))->ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200116
117/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200118 * @brief Data input/output formats supported by libyang [parser](@ref howtodataparsers) and
Radek Krejcid91d4da2020-05-18 11:28:02 +0200119 * [printer](@ref howtodataprinters) functions. Also used for value prefix format (TODO link to prefix formats descriptions).
Radek Krejcie7b95092019-05-15 11:03:07 +0200120 */
121typedef enum {
Michal Vasko52927e22020-03-16 17:26:14 +0100122 LYD_SCHEMA = 0, /**< invalid instance data format, value prefixes map to YANG import prefixes */
123 LYD_XML, /**< XML instance data format, value prefixes map to XML namespace prefixes */
124 LYD_JSON, /**< JSON instance data format, value prefixes map to module names */
Michal Vasko60ea6352020-06-29 13:39:39 +0200125 LYD_LYB, /**< LYB instance data format, invalid value prefix format (same as LYD_JSON) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200126} LYD_FORMAT;
127
128/**
Radek Krejci59583bb2019-09-11 12:57:55 +0200129 * @brief List of possible value types stored in ::lyd_node_any.
Radek Krejcie7b95092019-05-15 11:03:07 +0200130 */
131typedef enum {
Radek Krejci22ebdba2019-07-25 13:59:43 +0200132 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 +0200133 is directly connected into the anydata node without duplication, caller is supposed to not manipulate
134 with the data after a successful call (including calling lyd_free() on the provided data) */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200135 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 +0200136 as string). XML sensitive characters (such as & or \>) are automatically escaped when the anydata
137 is printed in XML format. */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200138 LYD_ANYDATA_XML, /**< Value is a string containing the serialized XML data. */
139 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 +0200140 LYD_ANYDATA_LYB, /**< Value is a memory chunk with the serialized data tree in LYB format. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200141} LYD_ANYDATA_VALUETYPE;
142
143/** @} */
144
145/**
146 * @brief YANG data representation
147 */
148struct lyd_value {
Radek Krejci950f6a52019-09-12 17:15:32 +0200149 const char *original; /**< Original string representation of the value. It is never NULL, but (canonical) string representation
150 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 +0200151 union {
Radek Krejcie7b95092019-05-15 11:03:07 +0200152 int8_t boolean; /**< 0 as false, 1 as true */
153 int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200154 int8_t int8; /**< 8-bit signed integer */
155 int16_t int16; /**< 16-bit signed integer */
156 int32_t int32; /**< 32-bit signed integer */
157 int64_t int64; /**< 64-bit signed integer */
158 uint8_t uint8; /**< 8-bit unsigned integer */
159 uint16_t uint16; /**< 16-bit signed integer */
160 uint32_t uint32; /**< 32-bit signed integer */
161 uint64_t uint64; /**< 64-bit signed integer */
Radek Krejcie7b95092019-05-15 11:03:07 +0200162 struct lysc_type_bitenum_item *enum_item; /**< pointer to the definition of the enumeration value */
Radek Krejci849a62a2019-05-22 15:29:05 +0200163 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 +0200164 struct lysc_ident *ident; /**< pointer to the schema definition of the identityref value */
Radek Krejciefbb3922019-07-15 12:58:00 +0200165
166 struct lyd_value_subvalue {
167 struct lyd_value_prefix {
168 const char *prefix; /**< prefix string used in the canonized string to identify the mod of the YANG schema */
169 const struct lys_module *mod; /**< YANG schema module identified by the prefix string */
170 } *prefixes; /**< list of mappings between prefix in canonized value to a YANG schema ([sized array](@ref sizedarrays)) */
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200171 struct lyd_value *value; /**< representation of the value according to the selected union's subtype (stored as lyd_value::realpath
172 here, in subvalue structure */
173 } *subvalue; /**< data to represent data with multiple types (union). Original value is stored in the main
Michal Vasko9b368d32020-02-14 13:53:31 +0100174 lyd_value:canonical_cache while the lyd_value_subvalue::value contains representation according to the
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200175 one of the union's type. The lyd_value_subvalue:prefixes provides (possible) mappings from prefixes
176 in original value to YANG modules. These prefixes are necessary to parse original value to the union's
177 subtypes. */
Radek Krejci084289f2019-07-09 17:35:30 +0200178
Michal Vasko004d3152020-06-11 19:59:22 +0200179 struct ly_path *target; /**< Instance-identifier's target path. */
Radek Krejci084289f2019-07-09 17:35:30 +0200180
Radek Krejcie7b95092019-05-15 11:03:07 +0200181 void *ptr; /**< generic data type structure used to store the data */
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200182 }; /**< The union is just a list of shorthands to possible values stored by a type's plugin. libyang itself uses the lyd_value::realtype
183 plugin's callbacks to work with the data. */
Radek Krejci084289f2019-07-09 17:35:30 +0200184
Radek Krejci950f6a52019-09-12 17:15:32 +0200185 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 +0200186 in the schema node of the data node since the type's store plugin can use other types/plugins for
187 storing data. Speaking about built-in types, this is the case of leafref which stores data as its
188 target type. In contrast, union type also use its subtype's callbacks, but inside an internal data
189 lyd_value::subvalue structure, so here is the pointer to the union type.
190 In general, this type is used to get free callback for this lyd_value structure, so it must reflect
191 the type used to store data directly in the same lyd_value instance. */
Radek Krejci950f6a52019-09-12 17:15:32 +0200192 void *canonical_cache; /**< Generic cache for type plugins to store data necessary to print canonical value. It can be the canonical
193 value itself or anything else useful to print the canonical form of the value. Plugin is responsible for
194 freeing the cache in its free callback. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200195};
196
197/**
Michal Vasko9f96a052020-03-10 09:41:45 +0100198 * @brief Metadata structure.
Radek Krejcie7b95092019-05-15 11:03:07 +0200199 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100200 * The structure provides information about metadata of a data element. Such attributes must map to
Radek Krejcie7b95092019-05-15 11:03:07 +0200201 * annotations as specified in RFC 7952. The only exception is the filter type (in NETCONF get operations)
202 * and edit-config's operation attributes. In XML, they are represented as standard XML attributes. In JSON,
203 * they are represented as JSON elements starting with the '@' character (for more information, see the
204 * YANG metadata RFC.
205 *
206 */
Michal Vasko9f96a052020-03-10 09:41:45 +0100207struct lyd_meta {
208 struct lyd_node *parent; /**< data node where the metadata is placed */
209 struct lyd_meta *next; /**< pointer to the next metadata of the same element */
210 struct lysc_ext_instance *annotation; /**< pointer to the annotation's definition */
211 const char *name; /**< metadata name */
212 struct lyd_value value; /**< metadata value representation */
Radek Krejcie7b95092019-05-15 11:03:07 +0200213};
214
Michal Vasko52927e22020-03-16 17:26:14 +0100215/**
216 * @brief Generic prefix and namespace mapping, meaning depends on the format.
217 */
218struct ly_prefix {
219 const char *pref;
220 const char *ns;
221};
222
223/**
224 * @brief Generic attribute structure.
225 */
226struct ly_attr {
227 struct lyd_node_opaq *parent; /**< data node where the attribute is placed */
228 struct ly_attr *next;
229 struct ly_prefix *val_prefs; /**< list of prefixes in the value ([sized array](@ref sizedarrays)) */
230 const char *name;
231 const char *value;
232
233 LYD_FORMAT format;
234 struct ly_prefix prefix; /**< name prefix, it is stored because they are a real pain to generate properly */
235
236};
Radek Krejcie7b95092019-05-15 11:03:07 +0200237
Michal Vasko1bf09392020-03-27 12:38:10 +0100238#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 +0200239#define LYD_NODE_TERM (LYS_LEAF|LYS_LEAFLIST) /**< Schema nodetype mask for lyd_node_term */
240#define LYD_NODE_ANY (LYS_ANYDATA) /**< Schema nodetype mask for lyd_node_any */
241
242/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100243 * @defgroup dnodeflags Data node flags
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200244 * @ingroup datatree
245 * @{
246 *
247 * Various flags of data nodes.
248 *
249 * 1 - container 5 - anydata/anyxml
250 * 2 - list 6 - rpc/action
251 * 3 - leaf 7 - notification
252 * 4 - leaflist
253 *
254 * bit name 1 2 3 4 5 6 7
255 * ---------------------+-+-+-+-+-+-+-+
256 * 1 LYD_DEFAULT |x| |x|x| | | |
257 * +-+-+-+-+-+-+-+
Michal Vasko5c4e5892019-11-14 12:31:38 +0100258 * 2 LYD_WHEN_TRUE |x|x|x|x|x| | |
Michal Vasko9b368d32020-02-14 13:53:31 +0100259 * +-+-+-+-+-+-+-+
260 * 3 LYD_NEW |x|x|x|x|x|x|x|
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200261 * ---------------------+-+-+-+-+-+-+-+
262 *
263 */
264
Michal Vasko5c4e5892019-11-14 12:31:38 +0100265#define LYD_DEFAULT 0x01 /**< default (implicit) node */
266#define LYD_WHEN_TRUE 0x02 /**< all when conditions of this node were evaluated to true */
Michal Vasko9b368d32020-02-14 13:53:31 +0100267#define LYD_NEW 0x04 /**< node was created after the last validation, is needed for the next validation */
Michal Vasko52927e22020-03-16 17:26:14 +0100268
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200269/** @} */
270
271/**
Michal Vasko9f96a052020-03-10 09:41:45 +0100272 * @brief Callback provided by the data/schema parsers to type plugins to resolve (format-specific) mapping between prefixes used
273 * in the value strings to the YANG schemas.
274 *
275 * Reverse function to ly_clb_get_prefix.
276 *
277 * XML uses XML namespaces, JSON uses schema names as prefixes, YIN/YANG uses prefixes of the imports.
278 *
279 * @param[in] ctx libyang context to find the schema.
280 * @param[in] prefix Prefix found in the value string
281 * @param[in] prefix_len Length of the @p prefix.
282 * @param[in] private Internal data needed by the callback.
283 * @return Pointer to the YANG schema identified by the provided prefix or NULL if no mapping found.
284 */
Michal Vasko52927e22020-03-16 17:26:14 +0100285typedef const struct lys_module *(*ly_clb_resolve_prefix)(const struct ly_ctx *ctx, const char *prefix, size_t prefix_len,
286 void *private);
Michal Vasko9f96a052020-03-10 09:41:45 +0100287
288/**
289 * @brief Callback provided by the data/schema printers to type plugins to resolve (format-specific) mapping between YANG module of a data object
290 * to prefixes used in the value strings.
291 *
292 * Reverse function to ly_clb_resolve_prefix.
293 *
294 * XML uses XML namespaces, JSON uses schema names as prefixes, YIN/YANG uses prefixes of the imports.
295 *
296 * @param[in] mod YANG module of the object.
297 * @param[in] private Internal data needed by the callback.
298 * @return String representing prefix for the object of the given YANG module @p mod.
299 */
300typedef const char *(*ly_clb_get_prefix)(const struct lys_module *mod, void *private);
301
302/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200303 * @brief Generic structure for a data node.
304 */
305struct lyd_node {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200306 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
307 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
308 used to get know that nodes are not equal, it cannot be used to decide that the
309 nodes are equal due to possible collisions. */
310 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Michal Vaskoecd62de2019-11-13 12:35:11 +0100311 const struct lysc_node *schema; /**< pointer to the schema definition of this node, note that the target can be not just
312 ::struct lysc_node but ::struct lysc_action or ::struct lysc_notif as well */
Radek Krejcie7b95092019-05-15 11:03:07 +0200313 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
314 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
315 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
316 never NULL. If there is no sibling node, pointer points to the node
317 itself. In case of the first node, this pointer points to the last
318 node in the list. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100319 struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200320
321#ifdef LY_ENABLED_LYD_PRIV
322 void *priv; /**< private user data, not used by libyang */
323#endif
324};
325
326/**
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200327 * @brief Data node structure for the inner data tree nodes - containers, lists, RPCs, actions and Notifications.
Radek Krejcie7b95092019-05-15 11:03:07 +0200328 */
329struct lyd_node_inner {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200330 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
331 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
332 used to get know that nodes are not equal, it cannot be used to decide that the
333 nodes are equal due to possible collisions. */
334 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200335 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
336 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
337 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
338 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
339 never NULL. If there is no sibling node, pointer points to the node
340 itself. In case of the first node, this pointer points to the last
341 node in the list. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100342 struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200343
344#ifdef LY_ENABLED_LYD_PRIV
345 void *priv; /**< private user data, not used by libyang */
346#endif
347
348 struct lyd_node *child; /**< pointer to the first child node. */
349 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 +0200350#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 +0200351};
352
353/**
Michal Vaskof03ed032020-03-04 13:31:44 +0100354 * @brief Data node structure for the terminal data tree nodes - leaves and leaf-lists.
Radek Krejcie7b95092019-05-15 11:03:07 +0200355 */
356struct lyd_node_term {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200357 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
358 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
359 used to get know that nodes are not equal, it cannot be used to decide that the
360 nodes are equal due to possible collisions. */
361 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200362 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
363 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
364 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
365 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
366 never NULL. If there is no sibling node, pointer points to the node
367 itself. In case of the first node, this pointer points to the last
368 node in the list. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100369 struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200370
371#ifdef LY_ENABLED_LYD_PRIV
372 void *priv; /**< private user data, not used by libyang */
373#endif
374
375 struct lyd_value value; /**< node's value representation */
376};
377
378/**
379 * @brief Data node structure for the anydata data tree nodes - anydatas and anyxmls.
380 */
381struct lyd_node_any {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200382 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
383 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
384 used to get know that nodes are not equal, it cannot be used to decide that the
385 nodes are equal due to possible collisions. */
386 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200387 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
388 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
389 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
390 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
391 never NULL. If there is no sibling node, pointer points to the node
392 itself. In case of the first node, this pointer points to the last
393 node in the list. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100394 struct lyd_meta *meta; /**< pointer to the list of attributes of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200395
396#ifdef LY_ENABLED_LYD_PRIV
397 void *priv; /**< private user data, not used by libyang */
398#endif
399
Michal Vasko00cbf532020-06-15 13:58:47 +0200400 union lyd_any_value {
Radek Krejciee4cab22019-07-17 17:07:47 +0200401 struct lyd_node *tree; /**< data tree */
402 const char *str; /**< Generic string data */
403 const char *xml; /**< Serialized XML data */
404 const char *json; /**< I-JSON encoded string */
405 char *mem; /**< LYD_ANYDATA_LYB memory chunk */
406 } value; /**< pointer to the stored value representation of the anydata/anyxml node */
407 LYD_ANYDATA_VALUETYPE value_type;/**< type of the data stored as lyd_node_any::value */
Radek Krejcie7b95092019-05-15 11:03:07 +0200408};
409
410/**
Michal Vasko52927e22020-03-16 17:26:14 +0100411 * @brief Data node structure for unparsed (opaque) nodes.
412 */
413struct lyd_node_opaq {
414 uint32_t hash; /**< always 0 */
415 uint32_t flags; /**< always 0 */
416 const struct lysc_node *schema; /**< always NULL */
417 struct lyd_node *parent; /**< pointer to the parent node (NULL in case of root node) */
418 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
419 struct lyd_node *prev; /**< pointer to the previous sibling node (last sibling if there is none) */
420 struct ly_attr *attr;
421
422#ifdef LY_ENABLED_LYD_PRIV
423 void *priv; /**< private user data, not used by libyang */
424#endif
425
426 struct lyd_node *child; /**< pointer to the child node (NULL if there are none) */
427 const char *name;
428 LYD_FORMAT format;
429 struct ly_prefix prefix; /**< name prefix */
430 struct ly_prefix *val_prefs; /**< list of prefixes in the value ([sized array](@ref sizedarrays)) */
431 const char *value; /**< original value */
432 const struct ly_ctx *ctx; /**< libyang context */
433};
434
435/**
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200436 * @defgroup children_options Children traversal options.
437 * @ingroup datatree
438 */
439
440#define LYD_CHILDREN_SKIP_KEYS 0x01 /**< If list children are returned, skip its keys. */
441
442/** @} children_options */
443
444/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200445 * @brief Get the node's children list if any.
446 *
447 * Decides the node's type and in case it has a children list, returns it.
448 * @param[in] node Node to check.
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200449 * @param[in] options Bitmask of options, see @ref
Radek Krejcie7b95092019-05-15 11:03:07 +0200450 * @return Pointer to the first child node (if any) of the \p node.
451 */
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200452struct lyd_node *lyd_node_children(const struct lyd_node *node, int options);
Radek Krejcie7b95092019-05-15 11:03:07 +0200453
454/**
Michal Vaskoc193ce92020-03-06 11:04:48 +0100455 * @brief Get the owner module of the data node. It is the module of the top-level schema node. Generally,
456 * in case of augments it is the target module, recursively, otherwise it is the module where the data node is defined.
457 *
458 * @param[in] node Data node to examine.
459 * @return Module owner of the node.
460 */
461const struct lys_module *lyd_owner_module(const struct lyd_node *node);
462
463/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200464 * @brief Learn the length of LYB data.
465 *
466 * @param[in] data LYB data to examine.
467 * @return Length of the LYB data chunk,
468 * @return -1 on error.
469 */
470int lyd_lyb_data_length(const char *data);
471
472/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200473 * @brief Create a new inner node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +0100474 *
475 * @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 +0100476 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100477 * @param[in] name Schema node name of the new data node. The node can be #LYS_CONTAINER, #LYS_NOTIF, #LYS_RPC, or #LYS_ACTION.
478 * @return New created node.
479 * @return NULL on error.
480 */
481struct lyd_node *lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name);
482
483/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200484 * @brief Create a new list node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +0100485 *
486 * @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 +0100487 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100488 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
489 * @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 +0200490 * or identityref value, the JSON format is expected (module names instead of prefixes). No keys are expected for
491 * key-less lists.
Michal Vasko013a8182020-03-03 10:46:53 +0100492 * @return New created node.
493 * @return NULL on error.
494 */
495struct lyd_node *lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ...);
496
497/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200498 * @brief Create a new list node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +0100499 *
500 * @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 +0100501 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100502 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
503 * @param[in] keys All key values predicate in the form of "[key1='val1'][key2='val2']...", they do not have to be ordered.
504 * 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 +0200505 * Use NULL or string of length 0 in case of key-less list.
Michal Vasko013a8182020-03-03 10:46:53 +0100506 * @return New created node.
507 * @return NULL on error.
508 */
509struct lyd_node *lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys);
510
511/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200512 * @brief Create a new term node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +0100513 *
514 * @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 +0100515 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100516 * @param[in] name Schema node name of the new data node. The node can be #LYS_LEAF or #LYS_LEAFLIST.
517 * @param[in] val_str String form of the value of the node being created. In case of an instance-identifier or identityref
518 * value, the JSON format is expected (module names instead of prefixes).
519 * @return New created node.
520 * @return NULL on error.
521 */
522struct lyd_node *lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str);
523
524/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200525 * @brief Create a new any node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +0100526 *
527 * @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 +0100528 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100529 * @param[in] name Schema node name of the new data node. The node can be #LYS_ANYDATA or #LYS_ANYXML.
530 * @param[in] value Value to be directly assigned to the node. Expected type is determined by @p value_type.
531 * @param[in] value_type Type of the provided value in @p value.
532 * @return New created node.
533 * @return NULL on error.
534 */
535struct lyd_node *lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name,
536 const void *value, LYD_ANYDATA_VALUETYPE value_type);
537
538/**
Michal Vaskod86997b2020-05-26 15:19:54 +0200539 * @brief Create new metadata for a data node.
540 *
541 * @param[in] parent Parent node for the metadata being created.
Michal Vasko00cbf532020-06-15 13:58:47 +0200542 * @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 +0200543 * @param[in] name Annotation name of the new metadata. It can include the annotation module as the prefix.
544 * 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 +0200545 * @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 +0200546 * value, the JSON format is expected (module names instead of prefixes).
Michal Vasko00cbf532020-06-15 13:58:47 +0200547 * @return New created metadata of @p parent.
Michal Vaskod86997b2020-05-26 15:19:54 +0200548 * @return NULL on error.
549 */
550struct lyd_meta *lyd_new_meta(struct lyd_node *parent, const struct lys_module *module, const char *name,
551 const char *val_str);
552
553/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200554 * @brief Create a new opaque node in the data tree.
555 *
556 * @param[in] parent Parent node for the node beaing created. NULL in case of creating a top level element.
557 * @param[in] ctx libyang context. If NULL, @p parent context will be used.
558 * @param[in] name Node name.
559 * @param[in] value Node value, may be NULL.
560 * @param[in] module_name Node module name.
561 * @return New created node.
562 * @return NULL on error.
563 */
564struct lyd_node *lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
565 const char *module_name);
566
567/**
568 * @brief Create new attribute for an opaque data node.
569 *
570 * @param[in] parent Parent opaque node for the attribute being created.
571 * @param[in] module Module name of the attribute being created. There may be none.
572 * @param[in] name Attribute name. It can include the module name as the prefix.
573 * @param[in] val_str String value of the attribute. Is stored directly.
574 * @return New created attribute of @p parent.
575 * @return NULL on error.
576 */
577struct ly_attr *lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *val_str);
578
579/**
580 * @defgroup pathoptions Data path creation options
581 * @ingroup datatree
582 *
583 * Various options to change lyd_new_path*() behavior.
584 *
585 * Default behavior:
586 * - if the target node already exists (and is not default), an error is returned.
587 * - the whole path to the target node is created (with any missing parents) if necessary.
588 * - RPC output schema children are completely ignored in all modules. Input is searched and nodes created normally.
589 * @{
590 */
591
592#define LYD_NEWOPT_UPDATE 0x01 /**< If the target node exists, is a leaf, and it is updated with a new value or its
593 default flag is changed, it is returned. If the target node exists and is not
594 a leaf or generally no change occurs in the @p parent tree, NULL is returned and
595 no error set. */
596#define LYD_NEWOPT_OUTPUT 0x02 /**< Changes the behavior to ignoring RPC/action input schema nodes and using only
597 output ones. */
598#define LYD_NEWOPT_OPAQ 0x04 /**< Enables the creation of opaque nodes with some specific rules. If the __last node__
599 in the path is not uniquely defined ((leaf-)list without a predicate) or has an
600 invalid value (leaf/leaf-list), it is created as opaque. */
601
602/** @} pathoptions */
603
604/**
605 * @brief Create a new node in the data tree based on a path. Cannot be used for anyxml/anydata nodes,
606 * for those use ::lyd_new_path_any.
607 *
608 * If @p path points to a list key and the list instance does not exist, the key value from the predicate is used
609 * and @p value is ignored. Also, if a leaf-list is being created and both a predicate is defined in @p path
610 * and @p value is set, the predicate is preferred.
611 *
612 * @param[in] parent Data parent to add to/modify, can be NULL.
613 * @param[in] ctx libyang context, must be set if @p parent is NULL.
614 * @param[in] path Path to create (TODO ref path).
615 * @param[in] value Value of the new leaf/leaf-list. For other node types, it is ignored.
616 * @param[in] options Bitmask of options, see @ref pathoptions.
Michal Vaskoa8f9eb32020-06-16 13:07:12 +0200617 * @return First created node.
Michal Vasko00cbf532020-06-15 13:58:47 +0200618 * @return NULL on error.
619 */
620struct lyd_node *lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value,
621 int options);
622
623/**
624 * @brief Create a new node in the data tree based on a path. All node types can be created.
625 *
626 * If @p path points to a list key and the list instance does not exist, the key value from the predicate is used
627 * and @p value is ignored. Also, if a leaf-list is being created and both a predicate is defined in @p path
628 * and @p value is set, the predicate is preferred.
629 *
630 * @param[in] parent Data parent to add to/modify, can be NULL.
631 * @param[in] ctx libyang context, must be set if @p parent is NULL.
632 * @param[in] path Path to create (TODO ref path).
633 * @param[in] value Value of the new leaf/leaf-list/anyxml/anydata. For other node types, it is ignored.
634 * @param[in] value_type Anyxml/anydata node @p value type.
635 * @param[in] options Bitmask of options, see @ref pathoptions.
Michal Vaskoa8f9eb32020-06-16 13:07:12 +0200636 * @return First created node.
Michal Vasko00cbf532020-06-15 13:58:47 +0200637 * @return NULL on error.
638 */
639struct lyd_node *lyd_new_path_any(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
640 LYD_ANYDATA_VALUETYPE value_type, int options);
641
642/**
643 * @brief Create a new node in the data tree based on a path. All node types can be created.
644 *
645 * If @p path points to a list key and the list instance does not exist, the key value from the predicate is used
646 * and @p value is ignored. Also, if a leaf-list is being created and both a predicate is defined in @p path
647 * and @p value is set, the predicate is preferred.
648 *
649 * @param[in] parent Data parent to add to/modify, can be NULL.
650 * @param[in] ctx libyang context, must be set if @p parent is NULL.
651 * @param[in] path Path to create (TODO ref path).
652 * @param[in] value Value of the new leaf/leaf-list/anyxml/anydata. For other node types, it is ignored.
653 * @param[in] value_type Anyxml/anydata node @p value type.
654 * @param[in] options Bitmask of options, see @ref pathoptions.
655 * @param[out] new_parent First parent node created, can be NULL. If only one node was created, equals to @p new_node.
656 * @param[out] new_node Last node created, can be NULL.
657 * @return LY_ERR value.
658 */
659LY_ERR lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
660 LYD_ANYDATA_VALUETYPE value_type, int options, struct lyd_node **new_parent, struct lyd_node **new_node);
661
662/**
663 * @brief Change the value of a term (leaf or leaf-list) node.
664 *
665 * Node changed this way is always considered explicitly set, meaning its default flag
666 * is always cleared.
667 *
668 * @param[in] term Term node to change.
669 * @param[in] val_str New value to set, any prefixes are expected in JSON format.
670 * @return LY_SUCCESS if value was changed,
671 * @return LY_EEXIST if value was the same and only the default flag was cleared,
672 * @return LY_ENOT if the values were equal and no change occured,
673 * @return LY_ERR value on other errors.
674 */
675LY_ERR lyd_change_term(struct lyd_node *term, const char *val_str);
676
677/**
Michal Vaskof03ed032020-03-04 13:31:44 +0100678 * @brief Insert a child into a parent. It is inserted as the last child.
679 *
680 * - if the node is part of some other tree, it is automatically unlinked.
681 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
682 *
683 * @param[in] parent Parent node to insert into.
684 * @param[in] node Node to insert.
685 * @return LY_SUCCESS on success.
686 * @return LY_ERR error on error.
687 */
688LY_ERR lyd_insert(struct lyd_node *parent, struct lyd_node *node);
689
690/**
Michal Vaskob1b5c262020-03-05 14:29:47 +0100691 * @brief Insert a node into siblings. It is inserted as the last sibling.
692 *
693 * - if the node is part of some other tree, it is automatically unlinked.
694 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
695 *
696 * @param[in] sibling Siblings to insert into.
697 * @param[in] node Node to insert.
698 * @return LY_SUCCESS on success.
699 * @return LY_ERR error on error.
700 */
701LY_ERR lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node);
702
703/**
Michal Vaskof03ed032020-03-04 13:31:44 +0100704 * @brief Insert a node before another node that is its schema sibling.
705 *
706 * - if the node is part of some other tree, it is automatically unlinked.
707 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
708 *
709 * @param[in] sibling Sibling node to insert before.
710 * @param[in] node Node to insert.
711 * @return LY_SUCCESS on success.
712 * @return LY_ERR error on error.
713 */
714LY_ERR lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node);
715
716/**
717 * @brief Insert a node after another node that is its schema sibling.
718 *
719 * - if the node is part of some other tree, it is automatically unlinked.
720 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
721 *
722 * @param[in] sibling Sibling node to insert after.
723 * @param[in] node Node to insert.
724 * @return LY_SUCCESS on success.
725 * @return LY_ERR error on error.
726 */
727LY_ERR lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node);
728
729/**
730 * @brief Unlink the specified data subtree.
731 *
732 * @param[in] node Data tree node to be unlinked (together with all the children).
733 */
734void lyd_unlink_tree(struct lyd_node *node);
735
736/**
Radek Krejcib0849a22019-07-25 12:31:04 +0200737 * @brief Free all the nodes (even parents of the node) in the data tree.
Radek Krejcie7b95092019-05-15 11:03:07 +0200738 *
739 * @param[in] node Any of the nodes inside the tree.
740 */
741void lyd_free_all(struct lyd_node *node);
742
743/**
Radek Krejcib0849a22019-07-25 12:31:04 +0200744 * @brief Free all the sibling nodes.
745 *
746 * @param[in] node Any of the sibling nodes to free.
747 */
Michal Vaskof03ed032020-03-04 13:31:44 +0100748void lyd_free_siblings(struct lyd_node *node);
Radek Krejcib0849a22019-07-25 12:31:04 +0200749
750/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200751 * @brief Free (and unlink) the specified data (sub)tree.
752 *
Radek Krejcie7b95092019-05-15 11:03:07 +0200753 * @param[in] node Root of the (sub)tree to be freed.
754 */
755void lyd_free_tree(struct lyd_node *node);
756
757/**
Michal Vasko9f96a052020-03-10 09:41:45 +0100758 * @brief Destroy metadata.
Radek Krejcie7b95092019-05-15 11:03:07 +0200759 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100760 * @param[in] ctx Context where the metadata was created.
761 * @param[in] meta Metadata to destroy
Michal Vasko52927e22020-03-16 17:26:14 +0100762 * @param[in] recursive Zero to destroy only the single metadata (the metadata list is corrected),
Michal Vasko9f96a052020-03-10 09:41:45 +0100763 * non-zero to destroy also all the subsequent metadata in the list.
Radek Krejcie7b95092019-05-15 11:03:07 +0200764 */
Michal Vasko52927e22020-03-16 17:26:14 +0100765void lyd_free_meta(const struct ly_ctx *ctx, struct lyd_meta *meta, int recursive);
766
767/**
768 * @brief Destroy attributes.
769 *
770 * @param[in] ctx Context where the attributes were created.
771 * @param[in] attr Attributes to destroy.
772 * @param[in] recursive Zero to destroy only the single attribute (the attribute list is corrected),
773 * non-zero to destroy also all the subsequent attributes in the list.
774 */
775void ly_free_attr(const struct ly_ctx *ctx, struct ly_attr *attr, int recursive);
Radek Krejcie7b95092019-05-15 11:03:07 +0200776
Radek Krejci084289f2019-07-09 17:35:30 +0200777/**
778 * @brief Check type restrictions applicable to the particular leaf/leaf-list with the given string @p value.
779 *
780 * The given node is not modified in any way - it is just checked if the @p value can be set to the node.
781 *
782 * If there is no data node instance and you are fine with checking just the type's restrictions without the
783 * data tree context (e.g. for the case of require-instance restriction), use lys_value_validate().
784 *
785 * @param[in] ctx libyang context for logging (function does not log errors when @p ctx is NULL)
786 * @param[in] node Data node for the @p value.
787 * @param[in] value String value to be checked.
788 * @param[in] value_len Length of the given @p value (mandatory).
789 * @param[in] get_prefix Callback function to resolve prefixes used in the @p value string.
790 * @param[in] get_prefix_data Private data for the @p get_prefix callback.
791 * @param[in] format Input format of the data.
Michal Vaskof03ed032020-03-04 13:31:44 +0100792 * @param[in] tree Data tree (e.g. when validating RPC/Notification) where the required data instance (leafref target,
793 * instance-identifier) can be placed. NULL in case the data tree is not yet complete,
794 * then LY_EINCOMPLETE can be returned.
Radek Krejci084289f2019-07-09 17:35:30 +0200795 * @return LY_SUCCESS on success
796 * @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).
797 * @return LY_ERR value if an error occurred.
798 */
Michal Vasko44685da2020-03-17 15:38:06 +0100799LY_ERR lyd_value_validate(const struct ly_ctx *ctx, const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vaskof03ed032020-03-04 13:31:44 +0100800 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format, const struct lyd_node *tree);
Radek Krejci084289f2019-07-09 17:35:30 +0200801
802/**
803 * @brief Compare the node's value with the given string value. The string value is first validated according to the node's type.
804 *
805 * @param[in] node Data node to compare.
806 * @param[in] value String value to be compared. It does not need to be in a canonical form - as part of the process,
807 * it is validated and canonized if possible.
808 * @param[in] value_len Length of the given @p value (mandatory).
809 * @param[in] get_prefix Callback function to resolve prefixes used in the @p value string.
810 * @param[in] get_prefix_data Private data for the @p get_prefix callback.
811 * @param[in] format Input format of the data.
Michal Vaskof03ed032020-03-04 13:31:44 +0100812 * @param[in] tree Data tree (e.g. when validating RPC/Notification) where the required data instance (leafref target,
813 * instance-identifier) can be placed. NULL in case the data tree is not yet complete,
814 * then LY_EINCOMPLETE can be returned.
Radek Krejci084289f2019-07-09 17:35:30 +0200815 * @return LY_SUCCESS on success
816 * @return LY_EINCOMPLETE in case of success when the @p trees is not provided and it was needed to finish the validation of
817 * the given string @p value (e.g. due to require-instance).
Michal Vaskob3ddccb2020-07-09 15:43:05 +0200818 * @return LY_ENOT if the values do not match.
Radek Krejci084289f2019-07-09 17:35:30 +0200819 * @return LY_ERR value if an error occurred.
820 */
821LY_ERR lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vaskof03ed032020-03-04 13:31:44 +0100822 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format, const struct lyd_node *tree);
Radek Krejci084289f2019-07-09 17:35:30 +0200823
Radek Krejci576b23f2019-07-12 14:06:32 +0200824/**
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200825 * @defgroup datacompareoptions Data compare options
826 * @ingroup datatree
827 *
Radek Krejci22ebdba2019-07-25 13:59:43 +0200828 * Various options to change the lyd_compare() behavior.
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200829 */
830#define LYD_COMPARE_FULL_RECURSION 0x01 /* lists and containers are the same only in case all they children
831 (subtree, so direct as well as indirect children) are the same. By default,
832 containers are the same in case of the same schema node and lists are the same
833 in case of equal keys (keyless lists do the full recursion comparison all the time). */
834#define LYD_COMPARE_DEFAULTS 0x02 /* By default, implicit and explicit default nodes are considered to be equal. This flag
835 changes this behavior and implicit (automatically created default node) and explicit
836 (explicitly created node with the default value) default nodes are considered different. */
Michal Vasko60ea6352020-06-29 13:39:39 +0200837/** @} datacompareoptions */
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200838
839/**
840 * @brief Compare 2 data nodes if they are equivalent.
841 *
842 * @param[in] node1 The first node to compare.
843 * @param[in] node2 The second node to compare.
Radek Krejcic5ad9652019-09-11 11:31:51 +0200844 * @param[in] options Various @ref datacompareoptions.
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200845 * @return LY_SUCCESS if the nodes are equivalent.
846 * @return LY_ENOT if the nodes are not equivalent.
847 */
848LY_ERR lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options);
849
850/**
Michal Vasko21725742020-06-29 11:49:25 +0200851 * @brief Compare 2 metadata.
852 *
853 * @param[in] meta1 First metadata.
854 * @param[in] meta2 Second metadata.
855 * @return LY_SUCCESS if the metadata are equivalent.
856 * @return LY_ENOT if not.
857 */
858LY_ERR lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2);
859
860/**
Radek Krejci22ebdba2019-07-25 13:59:43 +0200861 * @defgroup dupoptions Data duplication options
862 * @ingroup datatree
863 *
864 * Various options to change lyd_dup() behavior.
865 *
866 * Default behavior:
867 * - only the specified node is duplicated without siblings, parents, or children.
868 * - all the attributes of the duplicated nodes are also duplicated.
869 * @{
870 */
871
872#define LYD_DUP_RECURSIVE 0x01 /**< Duplicate not just the node but also all the children. Note that
873 list's keys are always duplicated. */
Michal Vaskoa29a5762020-06-23 13:28:49 +0200874#define LYD_DUP_NO_META 0x02 /**< Do not duplicate metadata of any node. */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200875#define LYD_DUP_WITH_PARENTS 0x04 /**< If a nested node is being duplicated, duplicate also all the parents.
876 Keys are also duplicated for lists. Return value does not change! */
877#define LYD_DUP_WITH_SIBLINGS 0x08 /**< Duplicate also all the sibling of the given node. */
Michal Vaskof6df0a02020-06-16 13:08:34 +0200878#define LYD_DUP_WITH_FLAGS 0x10 /**< Also copy any data node flags. That will cause the duplicated data to preserve
879 its validation/default node state. */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200880
881/** @} dupoptions */
882
883/**
884 * @brief Create a copy of the specified data tree \p node. Schema references are kept the same.
885 *
Radek Krejci22ebdba2019-07-25 13:59:43 +0200886 * @param[in] node Data tree node to be duplicated.
887 * @param[in] parent Optional parent node where to connect the duplicated node(s).
888 * If set in combination with LYD_DUP_WITH_PARENTS, the parents chain is duplicated until it comes to and connect with the @p parent
889 * (if the parents chain does not match at some node the schema node of the provided @p parent, duplication fails).
890 * @param[in] options Bitmask of options flags, see @ref dupoptions.
891 * @return Created copy of the provided data \p node (the first of the duplicated siblings when LYD_DUP_WITH_SIBLINGS used).
892 * Note that in case the parents chain is duplicated for the duplicated node(s) (when LYD_DUP_WITH_PARENTS used), the first duplicated node
893 * is still returned, not a pointer to the duplicated parents.
894 */
895struct lyd_node *lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options);
896
897/**
Michal Vasko25a32822020-07-09 15:48:22 +0200898 * @brief Create a copy of the metadata.
899 *
900 * @param[in] meta Metadata to copy.
901 * @param[in] node Node where to append the new metadata.
902 * @return Created metadata copy,
903 * @return NULL on error.
904 */
905struct lyd_meta *lyd_dup_meta(const struct lyd_meta *meta, struct lyd_node *node);
906
907/**
Michal Vasko4490d312020-06-16 13:08:55 +0200908 * @defgroup mergeoptions Data merge options.
909 * @ingroup datatree
Radek Krejci576b23f2019-07-12 14:06:32 +0200910 *
Michal Vasko4490d312020-06-16 13:08:55 +0200911 * Various options to change lyd_merge() behavior.
912 *
913 * Default behavior:
914 * - source data tree is not modified in any way,
915 * - source data tree is merged with any succeeding siblings,
916 * - any default nodes from source replace explicit nodes in the target.
917 * @{
918 */
919
920#define LYD_MERGE_DESTRUCT 0x01 /**< Spend source data tree in the function, it cannot be used afterwards! */
921#define LYD_MERGE_NOSIBLINGS 0x02 /**< Merge only the single source data tree, no siblings. */
922#define LYD_MERGE_EXPLICIT 0x04 /**< Default nodes in the source tree are ignored if there are explicit nodes
923 in the target tree. */
924
925/** @} mergeoptions */
926
927/**
928 * @brief Merge the source data tree into the target data tree. Merge may not be complete until validation
929 * is called on the resulting data tree (data from more cases may be present, default and non-default values).
930 *
931 * @param[in,out] target Target data tree to merge into, must be a top-level tree.
932 * @param[in] source Source data tree to merge, must be a top-level tree.
933 * @param[in] options Bitmask of option flags, see @ref mergeoptions.
934 * @return LY_SUCCESS on success,
935 * @return LY_ERR value on error.
936 */
937LY_ERR lyd_merge(struct lyd_node **target, const struct lyd_node *source, int options);
938
939/**
Michal Vaskoe893ddd2020-06-23 13:35:20 +0200940 * @defgroup diffoptions Data diff options.
941 * @ingroup datatree
942 *
943 * Various options to change lyd_diff() behavior.
944 *
945 * Default behavior:
946 * - data trees are compared with all the siblings,
947 * - any default nodes are treated as non-existent and ignored.
948 * @{
949 */
950
951#define LYD_DIFF_NOSIBLINGS 0x01 /**< Only the single subtree is compared, no siblings. */
952#define LYD_DIFF_WITHDEFAULTS 0x02 /**< Default nodes in the trees are not ignored but treated similarly to explicit
953 nodes. Also, leaves and leaf-lists are added into diff even in case only their
954 default flag (state) was changed. */
955
956/** @} diffoptions */
957
958/**
959 * @brief Learn the differences between 2 data trees.
960 *
961 * The resulting diff is represented as a data tree with specific metadata from the internal 'yang'
962 * module. Most importantly, every node has an effective 'operation' metadata. If there is none
963 * defined on the node, it inherits the operation from the nearest parent. Top-level nodes must
964 * always have the 'operation' metadata defined. Additional metadata ('orig-default', 'value',
965 * 'orig-value', 'key', 'orig-key') are used for storing more information about the value in the first
966 * or the second tree.
967 *
968 * The diff tree is completely independent on the @p first and @p second trees, meaning all
969 * the information about the change is stored in the diff and the trees are not needed.
970 *
971 * !! Caution !!
972 * The diff tree should never be validated because it may easily not be valid! For example,
973 * when data from one case branch are deleted and data from another branch created - data from both
974 * branches are then stored in the diff tree simultaneously.
975 *
976 * @param[in] first First data tree.
977 * @param[in] second Second data tree.
978 * @param[in] options Bitmask of options flags, see @ref diffoptions.
979 * @param[out] diff Generated diff.
980 * @return LY_SUCCESS on success,
981 * @return LY_ERR on error.
982 */
983LY_ERR lyd_diff(const struct lyd_node *first, const struct lyd_node *second, int options, struct lyd_node **diff);
984
985/**
986 * @brief Callback for diff nodes.
987 *
988 * @param[in] diff_node Diff node.
989 * @param[in] data_node Matching node in data.
990 * @param[in] cb_data Arbitrary callback data.
991 * @return LY_ERR value.
992 */
993typedef LY_ERR (*lyd_diff_cb)(const struct lyd_node *diff_node, struct lyd_node *data_node, void *cb_data);
994
995/**
996 * @brief Apply a difference on a data tree but restrict the operation to one module.
997 *
998 * @param[in,out] data Data to apply the diff on.
999 * @param[in] diff Diff to apply.
1000 * @param[in] mod Module, whose diff/data only to consider, NULL for all modules.
1001 * @param[in] diff_cb Optional diff callback that will be called for every changed node.
1002 * @param[in] cb_data Arbitrary callback data.
1003 * @return LY_SUCCESS on success,
1004 * @return LY_ERR on error.
1005 */
1006LY_ERR lyd_diff_apply_module(struct lyd_node **data, const struct lyd_node *diff, const struct lys_module *mod,
1007 lyd_diff_cb diff_cb, void *cb_data);
1008
1009/**
1010 * @brief Apply a difference on a data tree.
1011 *
1012 * @param[in,out] data Data to apply the diff on.
1013 * @param[in] diff Diff to apply.
1014 * @return LY_SUCCESS on success,
1015 * @return LY_ERR on error.
1016 */
1017LY_ERR lyd_diff_apply(struct lyd_node **data, const struct lyd_node *diff);
1018
1019/**
Michal Vaskoe6323f62020-07-09 15:49:02 +02001020 * @brief Merge 2 diffs into each other but restrict the operation to one module.
1021 *
1022 * The diffs must be possible to be merged, which is guaranteed only if the source diff was
1023 * created on data that had the target diff applied on them. In other words, this sequence is legal
1024 *
1025 * diff1 from data1 and data2 -> data11 from apply diff1 on data1 -> diff2 from data11 and data3 ->
1026 * -> data 33 frm apply diff2 on data1
1027 *
1028 * and reusing these diffs
1029 *
1030 * diff11 from merge diff1 and diff2 -> data33 from apply diff11 on data1
1031 *
1032 * @param[in] src_diff Source diff.
1033 * @param[in] mod Module, whose diff only to consider, NULL for all modules.
1034 * @param[in] diff_cb Optional diff callback that will be called for every changed node.
1035 * @param[in] cb_data Arbitrary callback data.
1036 * @param[in,out] diff Target diff to merge into.
1037 * @return LY_SUCCESS on success,
1038 * @return LY_ERR on error.
1039 */
1040LY_ERR lyd_diff_merge_module(const struct lyd_node *src_diff, const struct lys_module *mod, lyd_diff_cb diff_cb,
1041 void *cb_data, struct lyd_node **diff);
1042
1043/**
1044 * @brief Merge 2 diffs into each other.
1045 *
1046 * @param[in] src_diff Source diff.
1047 * @param[in,out] diff Target diff to merge into.
1048 * @return LY_SUCCESS on success,
1049 * @return LY_ERR on error.
1050 */
1051LY_ERR lyd_diff_merge(const struct lyd_node *src_diff, struct lyd_node **diff);
1052
1053/**
Michal Vasko4490d312020-06-16 13:08:55 +02001054 * @brief Find the target in data of a compiled ly_path structure (instance-identifier).
1055 *
1056 * @param[in] path Compiled path structure.
Michal Vaskof03ed032020-03-04 13:31:44 +01001057 * @param[in] tree Data tree to be searched.
Michal Vasko4490d312020-06-16 13:08:55 +02001058 * @return Found target node,
1059 * @return NULL if not found.
Radek Krejci576b23f2019-07-12 14:06:32 +02001060 */
Michal Vasko004d3152020-06-11 19:59:22 +02001061const struct lyd_node_term *lyd_target(const struct ly_path *path, const struct lyd_node *tree);
Radek Krejci084289f2019-07-09 17:35:30 +02001062
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001063/**
1064 * @brief Get string value of a term data \p node.
1065 *
1066 * @param[in] node Data tree node with the value.
1067 * @param[out] dynamic Whether the string value was dynmically allocated.
1068 * @return String value of @p node, if @p dynamic, needs to be freed.
1069 */
1070const char *lyd_value2str(const struct lyd_node_term *node, int *dynamic);
1071
1072/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001073 * @brief Get string value of a metadata \p meta.
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001074 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001075 * @param[in] meta Metadata with the value.
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001076 * @param[out] dynamic Whether the string value was dynmically allocated.
Michal Vasko9f96a052020-03-10 09:41:45 +01001077 * @return String value of @p meta, if @p dynamic, needs to be freed.
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001078 */
Michal Vasko9f96a052020-03-10 09:41:45 +01001079const char *lyd_meta2str(const struct lyd_meta *meta, int *dynamic);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001080
1081/**
1082 * @brief Types of the different data paths.
1083 */
1084typedef enum {
Michal Vasko14654712020-02-06 08:35:21 +01001085 LYD_PATH_LOG, /**< Descriptive path format used in log messages */
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001086} LYD_PATH_TYPE;
1087
1088/**
1089 * @brief Generate path of the given node in the requested format.
1090 *
1091 * @param[in] node Schema path of this node will be generated.
1092 * @param[in] pathtype Format of the path to generate.
1093 * @param[in,out] buffer Prepared buffer of the @p buflen length to store the generated path.
1094 * If NULL, memory for the complete path is allocated.
1095 * @param[in] buflen Size of the provided @p buffer.
1096 * @return NULL in case of memory allocation error, path of the node otherwise.
1097 * In case the @p buffer is NULL, the returned string is dynamically allocated and caller is responsible to free it.
1098 */
1099char *lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen);
1100
Michal Vaskoe444f752020-02-10 12:20:06 +01001101/**
Michal Vasko25a32822020-07-09 15:48:22 +02001102 * @brief Find a specific metadata.
1103 *
1104 * @param[in] first First metadata to consider.
1105 * @param[in] module Module of the metadata definition, may be NULL if @p name includes a prefix.
1106 * @param[in] name Name of the metadata to find, may not include a prefix (module name) if @p module is set.
1107 * @return Found metadata,
1108 * @return NULL if not found.
1109 */
1110struct lyd_meta *lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name);
1111
1112/**
Michal Vaskoe444f752020-02-10 12:20:06 +01001113 * @brief Find the node, in the list, satisfying the given restrictions.
1114 * Does **not** use hashes - should not be used unless necessary for best performance.
1115 *
1116 * @param[in] first Starting sibling node for search, only succeeding ones are searched.
1117 * @param[in] module Module of the node to find.
1118 * @param[in] name Name of the node to find.
Michal Vaskof03ed032020-03-04 13:31:44 +01001119 * @param[in] name_len Optional length of @p name in case it is not 0-terminated string.
Michal Vaskoe444f752020-02-10 12:20:06 +01001120 * @param[in] key_or_value Expected value depends on the type of @p name node:
1121 * LYS_CONTAINER:
1122 * LYS_ANYXML:
1123 * LYS_ANYDATA:
1124 * LYS_NOTIF:
1125 * LYS_RPC:
1126 * LYS_ACTION:
1127 * NULL should be always set, will be ignored.
1128 * LYS_LEAF:
1129 * LYS_LEAFLIST:
Michal Vasko90932a92020-02-12 14:33:03 +01001130 * Optional restriction on the specific leaf(-list) value.
Michal Vaskoe444f752020-02-10 12:20:06 +01001131 * LYS_LIST:
1132 * Optional keys values of the matching list instances in the form of "[key1='val1'][key2='val2']...".
Michal Vasko90932a92020-02-12 14:33:03 +01001133 * The keys do not have to be ordered and not all keys need to be specified.
1134 *
1135 * Note that any explicit values (leaf, leaf-list or list key values) will be canonized first
1136 * before comparison. But values that do not have a canonical value are expected to be in the
1137 * JSON format!
Michal Vasko25a32822020-07-09 15:48:22 +02001138 * @param[in] val_len Optional length of @p key_or_value in case it is not a 0-terminated string.
Michal Vaskoe444f752020-02-10 12:20:06 +01001139 * @param[out] match Found data node.
1140 * @return LY_SUCCESS on success, @p match set.
1141 * @return LY_ENOTFOUND if not found, @p match set to NULL.
1142 * @return LY_ERR value if another error occurred.
1143 */
1144LY_ERR lyd_find_sibling_next(const struct lyd_node *first, const struct lys_module *module, const char *name,
1145 size_t name_len, const char *key_or_value, size_t val_len, struct lyd_node **match);
1146
1147/**
1148 * @brief Search in the given siblings (NOT recursively) for the first target instance.
1149 * Uses hashes - should be used whenever possible for best performance.
1150 *
1151 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
1152 * @param[in] target Target node to find.
Michal Vasko9b368d32020-02-14 13:53:31 +01001153 * @param[out] match Can be NULL, otherwise the found data node.
Michal Vaskoe444f752020-02-10 12:20:06 +01001154 * @return LY_SUCCESS on success, @p match set.
1155 * @return LY_ENOTFOUND if not found, @p match set to NULL.
1156 * @return LY_ERR value if another error occurred.
1157 */
1158LY_ERR lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match);
1159
1160/**
1161 * @brief Search in the given siblings for all target instances.
1162 * Uses hashes - should be used whenever possible for best performance.
1163 *
1164 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
1165 * @param[in] target Target node to find. Key-less lists are compared based on
1166 * all its descendants (both direct and indirect).
1167 * @param[out] set Found nodes in a set in case of success.
1168 * @return LY_SUCCESS on success.
1169 * @return LY_ENOTFOUND if no matching siblings found.
1170 * @return LY_ERR value if another error occurred.
1171 */
1172LY_ERR lyd_find_sibling_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set);
1173
1174/**
1175 * @brief Search in the given siblings for the first schema instance.
1176 * Uses hashes - should be used whenever possible for best performance.
1177 *
1178 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
1179 * @param[in] schema Schema node of the data node to find.
1180 * @param[in] key_or_value Expected value depends on the type of \p schema:
1181 * LYS_CONTAINER:
1182 * LYS_LEAF:
1183 * LYS_ANYXML:
1184 * LYS_ANYDATA:
1185 * LYS_NOTIF:
1186 * LYS_RPC:
1187 * LYS_ACTION:
1188 * NULL should be always set, will be ignored.
1189 * LYS_LEAFLIST:
1190 * Searched instance value.
1191 * LYS_LIST:
Michal Vasko90932a92020-02-12 14:33:03 +01001192 * Searched instance key values in the form of "[key1='val1'][key2='val2']...".
1193 * The keys do not have to be ordered but all of them must be set.
1194 *
1195 * Note that any explicit values (leaf-list or list key values) will be canonized first
1196 * before comparison. But values that do not have a canonical value are expected to be in the
1197 * JSON format!
Michal Vaskof03ed032020-03-04 13:31:44 +01001198 * @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 +01001199 * @param[out] match Can be NULL, otherwise the found data node.
Michal Vaskoe444f752020-02-10 12:20:06 +01001200 * @return LY_SUCCESS on success, @p match set.
1201 * @return LY_ENOTFOUND if not found, @p match set to NULL.
1202 * @return LY_EINVAL if @p schema is a key-less list.
1203 * @return LY_ERR value if another error occurred.
1204 */
1205LY_ERR lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
1206 size_t val_len, struct lyd_node **match);
1207
Michal Vaskoccc02342020-05-21 10:09:21 +02001208/**
1209 * @brief Search in the given data for instances of nodes matching the provided XPath.
1210 *
Michal Vasko61ac2f62020-05-25 12:39:51 +02001211 * The expected format of the expression is JSON (::LYD_JSON) meaning the first node in every path
1212 * must have its module name as prefix or be the special `*` value for all the nodes.
1213 *
Michal Vasko19a30602020-05-25 10:40:19 +02001214 * If a list instance is being selected with all its key values specified (but not necessarily ordered)
1215 * in the form `list[key1='val1'][key2='val2'][key3='val3']` or a leaf-list instance in the form
1216 * `leaf-list[.='val']`, these instances are found using hashes with constant (*O(1)*) complexity
1217 * (unless they are defined in top-level). Other predicates can still follow the aforementioned ones.
1218 *
Michal Vaskoccc02342020-05-21 10:09:21 +02001219 * @param[in] ctx_node XPath context node.
1220 * @param[in] xpath Data XPath expression filtering the matching nodes. ::LYD_JSON format is expected.
1221 * @param[out] set Set of found data nodes. In case the result is a number, a string, or a boolean,
1222 * the returned set is empty.
1223 * @return LY_SUCCESS on success, @p set is returned.
1224 * @return LY_ERR value if an error occurred.
1225 */
1226LY_ERR lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set);
1227
Radek Krejcie7b95092019-05-15 11:03:07 +02001228#ifdef __cplusplus
1229}
1230#endif
1231
1232#endif /* LY_TREE_DATA_H_ */