blob: 4f261ef3423954cb4e0b31a8891b7712faa41a69 [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
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200143 * [printer](@ref howtodataprinters) functions.
Radek Krejcie7b95092019-05-15 11:03:07 +0200144 */
145typedef enum {
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200146 LYD_UNKNOWN = 0, /**< unknown data format, invalid value */
147 LYD_XML, /**< XML instance data format */
148 LYD_JSON, /**< JSON instance data format */
149 LYD_LYB, /**< LYB instance data format */
Radek Krejcie7b95092019-05-15 11:03:07 +0200150} LYD_FORMAT;
151
152/**
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200153 * @brief All kinds of supported prefix mappings to modules.
154 */
155typedef enum {
156 LY_PREF_SCHEMA, /**< value prefixes map to YANG import prefixes */
157 LY_PREF_XML, /**< value prefixes map to XML namespace prefixes */
158 LY_PREF_JSON, /**< value prefixes map to module names */
159} LY_PREFIX_FORMAT;
160
161/**
Radek Krejci59583bb2019-09-11 12:57:55 +0200162 * @brief List of possible value types stored in ::lyd_node_any.
Radek Krejcie7b95092019-05-15 11:03:07 +0200163 */
164typedef enum {
Radek Krejci22ebdba2019-07-25 13:59:43 +0200165 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 +0200166 is directly connected into the anydata node without duplication, caller is supposed to not manipulate
167 with the data after a successful call (including calling lyd_free() on the provided data) */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200168 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 +0200169 as string). XML sensitive characters (such as & or \>) are automatically escaped when the anydata
170 is printed in XML format. */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200171 LYD_ANYDATA_XML, /**< Value is a string containing the serialized XML data. */
172 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 +0200173 LYD_ANYDATA_LYB, /**< Value is a memory chunk with the serialized data tree in LYB format. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200174} LYD_ANYDATA_VALUETYPE;
175
176/** @} */
177
178/**
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200179 * @brief Special lyd_value structure for union.
180 *
181 * Represents data with multiple types (union). Original value is stored in the main lyd_value:canonical_cache while
182 * the lyd_value_subvalue::value contains representation according to one of the union's types.
183 * The lyd_value_subvalue:prefixes provides (possible) mappings from prefixes in the original value to YANG modules.
184 * These prefixes are necessary to parse original value to the union's subtypes.
185 */
186struct lyd_value_subvalue {
187 struct lyd_value *value; /**< representation of the value according to the selected union's subtype
188 (stored as lyd_value::realpath here, in subvalue structure */
189 LY_PREFIX_FORMAT format; /**< Prefix format of the value. However, this information is also used to decide
190 whether a value is valid for the specific format or not on later validations
191 (instance-identifier in XML looks different than in JSON). */
192 void *prefix_data; /**< Format-specific data for prefix resolution (see ::ly_resolve_prefix) */
193 int parser_hint; /**< Hint options from the parser */
194};
195
196/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200197 * @brief YANG data representation
198 */
199struct lyd_value {
Radek Krejci950f6a52019-09-12 17:15:32 +0200200 const char *original; /**< Original string representation of the value. It is never NULL, but (canonical) string representation
201 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 +0200202 union {
Radek Krejcie7b95092019-05-15 11:03:07 +0200203 int8_t boolean; /**< 0 as false, 1 as true */
204 int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200205 int8_t int8; /**< 8-bit signed integer */
206 int16_t int16; /**< 16-bit signed integer */
207 int32_t int32; /**< 32-bit signed integer */
208 int64_t int64; /**< 64-bit signed integer */
209 uint8_t uint8; /**< 8-bit unsigned integer */
Michal Vasko7c8439f2020-08-05 13:25:19 +0200210 uint16_t uint16; /**< 16-bit unsigned integer */
211 uint32_t uint32; /**< 32-bit unsigned integer */
212 uint64_t uint64; /**< 64-bit unsigned integer */
Radek Krejcie7b95092019-05-15 11:03:07 +0200213 struct lysc_type_bitenum_item *enum_item; /**< pointer to the definition of the enumeration value */
Radek Krejci849a62a2019-05-22 15:29:05 +0200214 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 +0200215 struct lysc_ident *ident; /**< pointer to the schema definition of the identityref value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200216 struct ly_path *target; /**< Instance-identifier target path. */
217 struct lyd_value_subvalue *subvalue; /** Union value with some metadata. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200218 void *ptr; /**< generic data type structure used to store the data */
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200219 }; /**< The union is just a list of shorthands to possible values stored by a type's plugin. libyang itself uses the lyd_value::realtype
220 plugin's callbacks to work with the data. */
Radek Krejci084289f2019-07-09 17:35:30 +0200221
Radek Krejci950f6a52019-09-12 17:15:32 +0200222 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 +0200223 in the schema node of the data node since the type's store plugin can use other types/plugins for
224 storing data. Speaking about built-in types, this is the case of leafref which stores data as its
225 target type. In contrast, union type also use its subtype's callbacks, but inside an internal data
226 lyd_value::subvalue structure, so here is the pointer to the union type.
227 In general, this type is used to get free callback for this lyd_value structure, so it must reflect
228 the type used to store data directly in the same lyd_value instance. */
Radek Krejci950f6a52019-09-12 17:15:32 +0200229 void *canonical_cache; /**< Generic cache for type plugins to store data necessary to print canonical value. It can be the canonical
230 value itself or anything else useful to print the canonical form of the value. Plugin is responsible for
231 freeing the cache in its free callback. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200232};
233
234/**
Michal Vasko9f96a052020-03-10 09:41:45 +0100235 * @brief Metadata structure.
Radek Krejcie7b95092019-05-15 11:03:07 +0200236 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100237 * The structure provides information about metadata of a data element. Such attributes must map to
Radek Krejcie7b95092019-05-15 11:03:07 +0200238 * annotations as specified in RFC 7952. The only exception is the filter type (in NETCONF get operations)
239 * and edit-config's operation attributes. In XML, they are represented as standard XML attributes. In JSON,
240 * they are represented as JSON elements starting with the '@' character (for more information, see the
241 * YANG metadata RFC.
242 *
243 */
Michal Vasko9f96a052020-03-10 09:41:45 +0100244struct lyd_meta {
245 struct lyd_node *parent; /**< data node where the metadata is placed */
246 struct lyd_meta *next; /**< pointer to the next metadata of the same element */
247 struct lysc_ext_instance *annotation; /**< pointer to the annotation's definition */
248 const char *name; /**< metadata name */
249 struct lyd_value value; /**< metadata value representation */
Radek Krejcie7b95092019-05-15 11:03:07 +0200250};
251
Michal Vasko52927e22020-03-16 17:26:14 +0100252/**
253 * @brief Generic prefix and namespace mapping, meaning depends on the format.
Radek Krejci1798aae2020-07-14 13:26:06 +0200254 *
255 * The union is used as a reference to the data's module and according to the format, it can be used as a key for
256 * ly_ctx_get_module_implemented_ns() or ly_ctx_get_module_implemented(). While the module reference is always present,
257 * the id member can be omitted in case it is not present in the source data as a reference to the default namespace.
Michal Vasko52927e22020-03-16 17:26:14 +0100258 */
259struct ly_prefix {
Radek Krejci1798aae2020-07-14 13:26:06 +0200260 const char *id; /**< identifier used in the qualified name of the item to reference data node namespace */
261 union {
262 const char *module_ns; /**< namespace of the module where the data are supposed to belongs to, used for LYD_XML format. */
263 const char *module_name; /**< name of the module where the data are supposed to belongs to, used for LYD_JSON format. */
264 };
Michal Vasko52927e22020-03-16 17:26:14 +0100265};
266
267/**
268 * @brief Generic attribute structure.
269 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200270struct lyd_attr {
Michal Vasko52927e22020-03-16 17:26:14 +0100271 struct lyd_node_opaq *parent; /**< data node where the attribute is placed */
Radek Krejci1798aae2020-07-14 13:26:06 +0200272 struct lyd_attr *next;
Michal Vasko52927e22020-03-16 17:26:14 +0100273 struct ly_prefix *val_prefs; /**< list of prefixes in the value ([sized array](@ref sizedarrays)) */
274 const char *name;
275 const char *value;
276
Radek Krejci5536d282020-08-04 23:27:44 +0200277 LYD_FORMAT format; /**< format of the prefixes, only LYD_XML and LYD_JSON values can appear at this place */
Radek Krejci1798aae2020-07-14 13:26:06 +0200278 int hint; /**< additional information about from the data source, see the [hints list](@ref lydopaqhints) */
Michal Vasko52927e22020-03-16 17:26:14 +0100279 struct ly_prefix prefix; /**< name prefix, it is stored because they are a real pain to generate properly */
280
281};
Radek Krejcie7b95092019-05-15 11:03:07 +0200282
Michal Vasko1bf09392020-03-27 12:38:10 +0100283#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 +0200284#define LYD_NODE_TERM (LYS_LEAF|LYS_LEAFLIST) /**< Schema nodetype mask for lyd_node_term */
285#define LYD_NODE_ANY (LYS_ANYDATA) /**< Schema nodetype mask for lyd_node_any */
286
287/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100288 * @defgroup dnodeflags Data node flags
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200289 * @ingroup datatree
290 * @{
291 *
292 * Various flags of data nodes.
293 *
294 * 1 - container 5 - anydata/anyxml
295 * 2 - list 6 - rpc/action
296 * 3 - leaf 7 - notification
297 * 4 - leaflist
298 *
299 * bit name 1 2 3 4 5 6 7
300 * ---------------------+-+-+-+-+-+-+-+
301 * 1 LYD_DEFAULT |x| |x|x| | | |
302 * +-+-+-+-+-+-+-+
Michal Vasko5c4e5892019-11-14 12:31:38 +0100303 * 2 LYD_WHEN_TRUE |x|x|x|x|x| | |
Michal Vasko9b368d32020-02-14 13:53:31 +0100304 * +-+-+-+-+-+-+-+
305 * 3 LYD_NEW |x|x|x|x|x|x|x|
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200306 * ---------------------+-+-+-+-+-+-+-+
307 *
308 */
309
Michal Vasko5c4e5892019-11-14 12:31:38 +0100310#define LYD_DEFAULT 0x01 /**< default (implicit) node */
311#define LYD_WHEN_TRUE 0x02 /**< all when conditions of this node were evaluated to true */
Michal Vasko9b368d32020-02-14 13:53:31 +0100312#define LYD_NEW 0x04 /**< node was created after the last validation, is needed for the next validation */
Michal Vasko52927e22020-03-16 17:26:14 +0100313
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200314/** @} */
315
316/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200317 * @brief Generic structure for a data node.
318 */
319struct lyd_node {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200320 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
321 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
322 used to get know that nodes are not equal, it cannot be used to decide that the
323 nodes are equal due to possible collisions. */
324 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Michal Vaskoecd62de2019-11-13 12:35:11 +0100325 const struct lysc_node *schema; /**< pointer to the schema definition of this node, note that the target can be not just
326 ::struct lysc_node but ::struct lysc_action or ::struct lysc_notif as well */
Radek Krejcie7b95092019-05-15 11:03:07 +0200327 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
328 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
329 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
330 never NULL. If there is no sibling node, pointer points to the node
331 itself. In case of the first node, this pointer points to the last
332 node in the list. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100333 struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200334
335#ifdef LY_ENABLED_LYD_PRIV
336 void *priv; /**< private user data, not used by libyang */
337#endif
338};
339
340/**
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200341 * @brief Data node structure for the inner data tree nodes - containers, lists, RPCs, actions and Notifications.
Radek Krejcie7b95092019-05-15 11:03:07 +0200342 */
343struct lyd_node_inner {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200344 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
345 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
346 used to get know that nodes are not equal, it cannot be used to decide that the
347 nodes are equal due to possible collisions. */
348 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200349 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
350 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
351 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
352 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
353 never NULL. If there is no sibling node, pointer points to the node
354 itself. In case of the first node, this pointer points to the last
355 node in the list. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100356 struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200357
358#ifdef LY_ENABLED_LYD_PRIV
359 void *priv; /**< private user data, not used by libyang */
360#endif
361
362 struct lyd_node *child; /**< pointer to the first child node. */
363 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 +0200364#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 +0200365};
366
367/**
Michal Vaskof03ed032020-03-04 13:31:44 +0100368 * @brief Data node structure for the terminal data tree nodes - leaves and leaf-lists.
Radek Krejcie7b95092019-05-15 11:03:07 +0200369 */
370struct lyd_node_term {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200371 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
372 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
373 used to get know that nodes are not equal, it cannot be used to decide that the
374 nodes are equal due to possible collisions. */
375 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200376 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
377 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
378 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
379 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
380 never NULL. If there is no sibling node, pointer points to the node
381 itself. In case of the first node, this pointer points to the last
382 node in the list. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100383 struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200384
385#ifdef LY_ENABLED_LYD_PRIV
386 void *priv; /**< private user data, not used by libyang */
387#endif
388
389 struct lyd_value value; /**< node's value representation */
390};
391
392/**
393 * @brief Data node structure for the anydata data tree nodes - anydatas and anyxmls.
394 */
395struct lyd_node_any {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200396 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
397 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
398 used to get know that nodes are not equal, it cannot be used to decide that the
399 nodes are equal due to possible collisions. */
400 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200401 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
402 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
403 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
404 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
405 never NULL. If there is no sibling node, pointer points to the node
406 itself. In case of the first node, this pointer points to the last
407 node in the list. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100408 struct lyd_meta *meta; /**< pointer to the list of attributes of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200409
410#ifdef LY_ENABLED_LYD_PRIV
411 void *priv; /**< private user data, not used by libyang */
412#endif
413
Michal Vasko00cbf532020-06-15 13:58:47 +0200414 union lyd_any_value {
Radek Krejciee4cab22019-07-17 17:07:47 +0200415 struct lyd_node *tree; /**< data tree */
416 const char *str; /**< Generic string data */
417 const char *xml; /**< Serialized XML data */
418 const char *json; /**< I-JSON encoded string */
419 char *mem; /**< LYD_ANYDATA_LYB memory chunk */
420 } value; /**< pointer to the stored value representation of the anydata/anyxml node */
421 LYD_ANYDATA_VALUETYPE value_type;/**< type of the data stored as lyd_node_any::value */
Radek Krejcie7b95092019-05-15 11:03:07 +0200422};
423
424/**
Radek Krejci1798aae2020-07-14 13:26:06 +0200425 * @defgroup lydopaqhints Opaq data node hints.
426 *
427 * Additional information about the opaq nodes from their source. The flags are stored in lyd_node_opaq::hint
428 * and they can have a slightly different meaning for the specific lyd_node_opaq::format.
429 * @{
430 */
431#define LYD_NODE_OPAQ_ISLIST 0x001 /**< LYD_JSON: node is expected to be a list or leaf-list */
432#define LYD_NODE_OPAQ_ISENVELOPE 0x002 /**< LYD_JSON, LYD_XML: RPC/Action/Notification envelope out of the YANG schemas */
433
434#define LYD_NODE_OPAQ_ISSTRING 0x100 /**< LYD_JSON: value is expected to be string as defined in JSON encoding. */
435#define LYD_NODE_OPAQ_ISNUMBER 0x200 /**< LYD_JSON: value is expected to be number as defined in JSON encoding. */
436#define LYD_NODE_OPAQ_ISBOOLEAN 0x400 /**< LYD_JSON: value is expected to be boolean as defined in JSON encoding. */
437#define LYD_NODE_OPAQ_ISEMPTY 0x800 /**< LYD_JSON: value is expected to be empty as defined in JSON encoding. */
438
439/** @} lydopaqhints */
440
441/**
Michal Vasko52927e22020-03-16 17:26:14 +0100442 * @brief Data node structure for unparsed (opaque) nodes.
443 */
444struct lyd_node_opaq {
445 uint32_t hash; /**< always 0 */
446 uint32_t flags; /**< always 0 */
447 const struct lysc_node *schema; /**< always NULL */
448 struct lyd_node *parent; /**< pointer to the parent node (NULL in case of root node) */
449 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
450 struct lyd_node *prev; /**< pointer to the previous sibling node (last sibling if there is none) */
Radek Krejci1798aae2020-07-14 13:26:06 +0200451 struct lyd_attr *attr;
Michal Vasko52927e22020-03-16 17:26:14 +0100452
453#ifdef LY_ENABLED_LYD_PRIV
454 void *priv; /**< private user data, not used by libyang */
455#endif
456
457 struct lyd_node *child; /**< pointer to the child node (NULL if there are none) */
458 const char *name;
459 LYD_FORMAT format;
460 struct ly_prefix prefix; /**< name prefix */
461 struct ly_prefix *val_prefs; /**< list of prefixes in the value ([sized array](@ref sizedarrays)) */
462 const char *value; /**< original value */
Radek Krejci1798aae2020-07-14 13:26:06 +0200463 int hint; /**< additional information about from the data source, see the [hints list](@ref lydopaqhints) */
Michal Vasko52927e22020-03-16 17:26:14 +0100464 const struct ly_ctx *ctx; /**< libyang context */
465};
466
467/**
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200468 * @defgroup children_options Children traversal options.
469 * @ingroup datatree
470 */
471
472#define LYD_CHILDREN_SKIP_KEYS 0x01 /**< If list children are returned, skip its keys. */
473
474/** @} children_options */
475
476/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200477 * @brief Get the node's children list if any.
478 *
479 * Decides the node's type and in case it has a children list, returns it.
480 * @param[in] node Node to check.
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200481 * @param[in] options Bitmask of options, see @ref
Radek Krejcie7b95092019-05-15 11:03:07 +0200482 * @return Pointer to the first child node (if any) of the \p node.
483 */
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200484struct lyd_node *lyd_node_children(const struct lyd_node *node, int options);
Radek Krejcie7b95092019-05-15 11:03:07 +0200485
486/**
Michal Vaskoc193ce92020-03-06 11:04:48 +0100487 * @brief Get the owner module of the data node. It is the module of the top-level schema node. Generally,
488 * in case of augments it is the target module, recursively, otherwise it is the module where the data node is defined.
489 *
490 * @param[in] node Data node to examine.
491 * @return Module owner of the node.
492 */
493const struct lys_module *lyd_owner_module(const struct lyd_node *node);
494
495/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200496 * @brief Learn the length of LYB data.
497 *
498 * @param[in] data LYB data to examine.
499 * @return Length of the LYB data chunk,
500 * @return -1 on error.
501 */
502int lyd_lyb_data_length(const char *data);
503
504/**
Michal Vaskoc0004272020-08-06 08:32:34 +0200505 * @brief Copy anydata value from one node to another. Target value is freed first.
506 *
507 * @param[in,out] trg Target node.
508 * @param[in] value Source value, may be NULL when the target value is only freed.
509 * @param[in] value_type Source value type.
510 * @return LY_ERR value.
511 */
512LY_ERR lyd_any_copy_value(struct lyd_node *trg, const union lyd_any_value *value, LYD_ANYDATA_VALUETYPE value_type);
513
514/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200515 * @brief Create a new inner node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +0100516 *
517 * @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 +0100518 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100519 * @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 +0200520 * @param[out] node Optional created node.
521 * @return LY_ERR value.
Michal Vasko013a8182020-03-03 10:46:53 +0100522 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200523LY_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 +0100524
525/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200526 * @brief Create a new list node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +0100527 *
528 * @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 +0100529 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100530 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200531 * @param[out] node Optional created node.
Michal Vasko013a8182020-03-03 10:46:53 +0100532 * @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 +0200533 * or identityref value, the JSON format is expected (module names instead of prefixes). No keys are expected for
534 * key-less lists.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200535 * @return LY_ERR value.
Michal Vasko013a8182020-03-03 10:46:53 +0100536 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200537LY_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 +0100538
539/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200540 * @brief Create a new list node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +0100541 *
542 * @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 +0100543 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100544 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
545 * @param[in] keys All key values predicate in the form of "[key1='val1'][key2='val2']...", they do not have to be ordered.
546 * 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 +0200547 * Use NULL or string of length 0 in case of key-less list.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200548 * @param[out] node Optional created node.
549 * @return LY_ERR value.
Michal Vasko013a8182020-03-03 10:46:53 +0100550 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200551LY_ERR lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys,
552 struct lyd_node **node);
Michal Vasko013a8182020-03-03 10:46:53 +0100553
554/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200555 * @brief Create a new term node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +0100556 *
557 * @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 +0100558 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100559 * @param[in] name Schema node name of the new data node. The node can be #LYS_LEAF or #LYS_LEAFLIST.
560 * @param[in] val_str String form of the value of the node being created. In case of an instance-identifier or identityref
561 * value, the JSON format is expected (module names instead of prefixes).
Michal Vasko3a41dff2020-07-15 14:30:28 +0200562 * @param[out] node Optional created node.
563 * @return LY_ERR value.
Michal Vasko013a8182020-03-03 10:46:53 +0100564 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200565LY_ERR lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
566 struct lyd_node **node);
Michal Vasko013a8182020-03-03 10:46:53 +0100567
568/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200569 * @brief Create a new any node in the data tree.
Michal Vasko013a8182020-03-03 10:46:53 +0100570 *
571 * @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 +0100572 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100573 * @param[in] name Schema node name of the new data node. The node can be #LYS_ANYDATA or #LYS_ANYXML.
574 * @param[in] value Value to be directly assigned to the node. Expected type is determined by @p value_type.
575 * @param[in] value_type Type of the provided value in @p value.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200576 * @param[out] node Optional created node.
577 * @return LY_ERR value.
Michal Vasko013a8182020-03-03 10:46:53 +0100578 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200579LY_ERR lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value,
580 LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node);
Michal Vasko013a8182020-03-03 10:46:53 +0100581
582/**
Michal Vaskod86997b2020-05-26 15:19:54 +0200583 * @brief Create new metadata for a data node.
584 *
585 * @param[in] parent Parent node for the metadata being created.
Michal Vasko00cbf532020-06-15 13:58:47 +0200586 * @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 +0200587 * @param[in] name Annotation name of the new metadata. It can include the annotation module as the prefix.
588 * 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 +0200589 * @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 +0200590 * value, the JSON format is expected (module names instead of prefixes).
Michal Vasko3a41dff2020-07-15 14:30:28 +0200591 * @param[out] meta Optional created metadata.
592 * @return LY_ERR value.
Michal Vaskod86997b2020-05-26 15:19:54 +0200593 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200594LY_ERR lyd_new_meta(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str,
595 struct lyd_meta **meta);
Michal Vaskod86997b2020-05-26 15:19:54 +0200596
597/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200598 * @brief Create a new opaque node in the data tree.
599 *
600 * @param[in] parent Parent node for the node beaing created. NULL in case of creating a top level element.
601 * @param[in] ctx libyang context. If NULL, @p parent context will be used.
602 * @param[in] name Node name.
603 * @param[in] value Node value, may be NULL.
604 * @param[in] module_name Node module name.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200605 * @param[out] node Optional created node.
606 * @return LY_ERR value.
Michal Vasko00cbf532020-06-15 13:58:47 +0200607 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200608LY_ERR lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value,
609 const char *module_name, struct lyd_node **node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200610
611/**
612 * @brief Create new attribute for an opaque data node.
613 *
614 * @param[in] parent Parent opaque node for the attribute being created.
615 * @param[in] module Module name of the attribute being created. There may be none.
616 * @param[in] name Attribute name. It can include the module name as the prefix.
617 * @param[in] val_str String value of the attribute. Is stored directly.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200618 * @param[out] attr Optional created attribute.
619 * @return LY_ERR value.
Michal Vasko00cbf532020-06-15 13:58:47 +0200620 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200621LY_ERR lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *val_str,
Radek Krejci1798aae2020-07-14 13:26:06 +0200622 struct lyd_attr **attr);
Michal Vasko00cbf532020-06-15 13:58:47 +0200623
624/**
625 * @defgroup pathoptions Data path creation options
626 * @ingroup datatree
627 *
628 * Various options to change lyd_new_path*() behavior.
629 *
630 * Default behavior:
631 * - if the target node already exists (and is not default), an error is returned.
632 * - the whole path to the target node is created (with any missing parents) if necessary.
633 * - RPC output schema children are completely ignored in all modules. Input is searched and nodes created normally.
634 * @{
635 */
636
637#define LYD_NEWOPT_UPDATE 0x01 /**< If the target node exists, is a leaf, and it is updated with a new value or its
638 default flag is changed, it is returned. If the target node exists and is not
639 a leaf or generally no change occurs in the @p parent tree, NULL is returned and
640 no error set. */
641#define LYD_NEWOPT_OUTPUT 0x02 /**< Changes the behavior to ignoring RPC/action input schema nodes and using only
642 output ones. */
643#define LYD_NEWOPT_OPAQ 0x04 /**< Enables the creation of opaque nodes with some specific rules. If the __last node__
644 in the path is not uniquely defined ((leaf-)list without a predicate) or has an
645 invalid value (leaf/leaf-list), it is created as opaque. */
646
647/** @} pathoptions */
648
649/**
650 * @brief Create a new node in the data tree based on a path. Cannot be used for anyxml/anydata nodes,
651 * for those use ::lyd_new_path_any.
652 *
653 * If @p path points to a list key and the list instance does not exist, the key value from the predicate is used
654 * and @p value is ignored. Also, if a leaf-list is being created and both a predicate is defined in @p path
655 * and @p value is set, the predicate is preferred.
656 *
657 * @param[in] parent Data parent to add to/modify, can be NULL.
658 * @param[in] ctx libyang context, must be set if @p parent is NULL.
659 * @param[in] path Path to create (TODO ref path).
660 * @param[in] value Value of the new leaf/leaf-list. For other node types, it is ignored.
661 * @param[in] options Bitmask of options, see @ref pathoptions.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200662 * @param[out] node Optional first created node.
663 * @return LY_ERR value.
Michal Vasko00cbf532020-06-15 13:58:47 +0200664 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200665LY_ERR lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value,
666 int options, struct lyd_node **node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200667
668/**
669 * @brief Create a new node in the data tree based on a path. All node types can be created.
670 *
671 * If @p path points to a list key and the list instance does not exist, the key value from the predicate is used
672 * and @p value is ignored. Also, if a leaf-list is being created and both a predicate is defined in @p path
673 * and @p value is set, the predicate is preferred.
674 *
675 * @param[in] parent Data parent to add to/modify, can be NULL.
676 * @param[in] ctx libyang context, must be set if @p parent is NULL.
677 * @param[in] path Path to create (TODO ref path).
678 * @param[in] value Value of the new leaf/leaf-list/anyxml/anydata. For other node types, it is ignored.
679 * @param[in] value_type Anyxml/anydata node @p value type.
680 * @param[in] options Bitmask of options, see @ref pathoptions.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200681 * @param[out] node Optional first created node.
682 * @return LY_ERR value.
Michal Vasko00cbf532020-06-15 13:58:47 +0200683 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200684LY_ERR lyd_new_path_any(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
685 LYD_ANYDATA_VALUETYPE value_type, int options, struct lyd_node **node);
Michal Vasko00cbf532020-06-15 13:58:47 +0200686
687/**
688 * @brief Create a new node in the data tree based on a path. All node types can be created.
689 *
690 * If @p path points to a list key and the list instance does not exist, the key value from the predicate is used
691 * and @p value is ignored. Also, if a leaf-list is being created and both a predicate is defined in @p path
692 * and @p value is set, the predicate is preferred.
693 *
694 * @param[in] parent Data parent to add to/modify, can be NULL.
695 * @param[in] ctx libyang context, must be set if @p parent is NULL.
696 * @param[in] path Path to create (TODO ref path).
697 * @param[in] value Value of the new leaf/leaf-list/anyxml/anydata. For other node types, it is ignored.
698 * @param[in] value_type Anyxml/anydata node @p value type.
699 * @param[in] options Bitmask of options, see @ref pathoptions.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200700 * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node.
701 * @param[out] new_node Optional last node created.
Michal Vasko00cbf532020-06-15 13:58:47 +0200702 * @return LY_ERR value.
703 */
704LY_ERR lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value,
705 LYD_ANYDATA_VALUETYPE value_type, int options, struct lyd_node **new_parent, struct lyd_node **new_node);
706
707/**
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200708 * @defgroup implicitoptions Implicit node creation options
709 * @ingroup datatree
710 *
711 * Various options to change lyd_new_implicit*() behavior.
712 *
713 * Default behavior:
714 * - both configuration and state missing implicit nodes are added.
715 * - all implicit node types are added (non-presence containers, default leaves, and default leaf-lists).
716 * @{
717 */
718
Michal Vasko44b19a12020-08-07 09:21:30 +0200719#define LYD_IMPLICIT_NO_STATE 0x01 /**< Do not add any implicit state nodes. */
720#define LYD_IMPLICIT_NO_CONFIG 0x02 /**< Do not add any implicit config nodes. */
721#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 +0200722 containers. */
723
724/** @} implicitoptions */
725
726/**
727 * @brief Add any missing implicit nodes into a data subtree.
728 *
729 * @param[in] tree Tree to add implicit nodes into.
730 * @param[in] implicit_options Options for implicit node creation, see @ref implicitoptions.
731 * @param[out] diff Optional diff with any created nodes.
732 * @return LY_ERR value.
733 */
734LY_ERR lyd_new_implicit_tree(struct lyd_node *tree, int implicit_options, struct lyd_node **diff);
735
736/**
737 * @brief Add any missing implicit nodes.
738 *
739 * @param[in,out] tree Tree to add implicit nodes into.
740 * @param[in] ctx libyang context, must be set only if @p tree is an empty tree.
741 * @param[in] implicit_options Options for implicit node creation, see @ref implicitoptions.
742 * @param[out] diff Optional diff with any created nodes.
743 * @return LY_ERR value.
744 */
745LY_ERR lyd_new_implicit_all(struct lyd_node **tree, const struct ly_ctx *ctx, int implicit_options, struct lyd_node **diff);
746
747/**
748 * @brief Add any missing implicit nodes of one module.
749 *
750 * @param[in,out] tree Tree to add implicit nodes into.
751 * @param[in] module Module whose implicit nodes to create.
752 * @param[in] implicit_options Options for implicit node creation, see @ref implicitoptions.
753 * @param[out] diff Optional diff with any created nodes.
754 * @return LY_ERR value.
755 */
756LY_ERR lyd_new_implicit_module(struct lyd_node **tree, const struct lys_module *module, int implicit_options,
757 struct lyd_node **diff);
758
759/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200760 * @brief Change the value of a term (leaf or leaf-list) node.
761 *
762 * Node changed this way is always considered explicitly set, meaning its default flag
763 * is always cleared.
764 *
765 * @param[in] term Term node to change.
766 * @param[in] val_str New value to set, any prefixes are expected in JSON format.
767 * @return LY_SUCCESS if value was changed,
768 * @return LY_EEXIST if value was the same and only the default flag was cleared,
769 * @return LY_ENOT if the values were equal and no change occured,
770 * @return LY_ERR value on other errors.
771 */
772LY_ERR lyd_change_term(struct lyd_node *term, const char *val_str);
773
774/**
Michal Vasko41586352020-07-13 13:54:25 +0200775 * @brief Change the value of a metadata instance.
776 *
777 * @param[in] ctx libyang context.
778 * @param[in] meta Metadata to change.
779 * @param[in] val_str New value to set, any prefixes are expected in JSON format.
780 * @return LY_SUCCESS if value was changed,
781 * @return LY_ENOT if the values were equal and no change occured,
782 * @return LY_ERR value on other errors.
783 */
784LY_ERR lyd_change_meta(struct lyd_meta *meta, const char *val_str);
785
786/**
Michal Vaskob104f112020-07-17 09:54:54 +0200787 * @brief Insert a child into a parent.
Michal Vaskof03ed032020-03-04 13:31:44 +0100788 *
789 * - if the node is part of some other tree, it is automatically unlinked.
790 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
791 *
792 * @param[in] parent Parent node to insert into.
793 * @param[in] node Node to insert.
794 * @return LY_SUCCESS on success.
795 * @return LY_ERR error on error.
796 */
Michal Vaskob104f112020-07-17 09:54:54 +0200797LY_ERR lyd_insert_child(struct lyd_node *parent, struct lyd_node *node);
Michal Vaskof03ed032020-03-04 13:31:44 +0100798
799/**
Michal Vaskob104f112020-07-17 09:54:54 +0200800 * @brief Insert a node into siblings.
Michal Vaskob1b5c262020-03-05 14:29:47 +0100801 *
802 * - if the node is part of some other tree, it is automatically unlinked.
803 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
804 *
Michal Vaskob104f112020-07-17 09:54:54 +0200805 * @param[in] sibling Siblings to insert into, can even be NULL.
Michal Vaskob1b5c262020-03-05 14:29:47 +0100806 * @param[in] node Node to insert.
Michal Vaskob104f112020-07-17 09:54:54 +0200807 * @param[out] first Optionally return the first sibling after insertion. Can be the address of @p sibling.
Michal Vaskob1b5c262020-03-05 14:29:47 +0100808 * @return LY_SUCCESS on success.
809 * @return LY_ERR error on error.
810 */
Michal Vaskob104f112020-07-17 09:54:54 +0200811LY_ERR lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100812
813/**
Michal Vaskob104f112020-07-17 09:54:54 +0200814 * @brief Insert a node before another node, can be used only for user-ordered nodes.
Michal Vaskof03ed032020-03-04 13:31:44 +0100815 *
816 * - if the node is part of some other tree, it is automatically unlinked.
817 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
818 *
819 * @param[in] sibling Sibling node to insert before.
820 * @param[in] node Node to insert.
821 * @return LY_SUCCESS on success.
822 * @return LY_ERR error on error.
823 */
824LY_ERR lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node);
825
826/**
Michal Vaskob104f112020-07-17 09:54:54 +0200827 * @brief Insert a node after another node, can be used only for user-ordered nodes.
Michal Vaskof03ed032020-03-04 13:31:44 +0100828 *
829 * - if the node is part of some other tree, it is automatically unlinked.
830 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
831 *
832 * @param[in] sibling Sibling node to insert after.
833 * @param[in] node Node to insert.
834 * @return LY_SUCCESS on success.
835 * @return LY_ERR error on error.
836 */
837LY_ERR lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node);
838
839/**
840 * @brief Unlink the specified data subtree.
841 *
842 * @param[in] node Data tree node to be unlinked (together with all the children).
843 */
844void lyd_unlink_tree(struct lyd_node *node);
845
846/**
Radek Krejcib0849a22019-07-25 12:31:04 +0200847 * @brief Free all the nodes (even parents of the node) in the data tree.
Radek Krejcie7b95092019-05-15 11:03:07 +0200848 *
849 * @param[in] node Any of the nodes inside the tree.
850 */
851void lyd_free_all(struct lyd_node *node);
852
853/**
Michal Vasko3a41dff2020-07-15 14:30:28 +0200854 * @brief Free all the sibling nodes (preceding as well as succeeding).
Radek Krejcib0849a22019-07-25 12:31:04 +0200855 *
856 * @param[in] node Any of the sibling nodes to free.
857 */
Michal Vaskof03ed032020-03-04 13:31:44 +0100858void lyd_free_siblings(struct lyd_node *node);
Radek Krejcib0849a22019-07-25 12:31:04 +0200859
860/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200861 * @brief Free (and unlink) the specified data (sub)tree.
862 *
Radek Krejcie7b95092019-05-15 11:03:07 +0200863 * @param[in] node Root of the (sub)tree to be freed.
864 */
865void lyd_free_tree(struct lyd_node *node);
866
867/**
Michal Vasko3a41dff2020-07-15 14:30:28 +0200868 * @brief Free a single metadata instance.
Radek Krejcie7b95092019-05-15 11:03:07 +0200869 *
Michal Vasko3a41dff2020-07-15 14:30:28 +0200870 * @param[in] meta Metadata to free.
Radek Krejcie7b95092019-05-15 11:03:07 +0200871 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200872void lyd_free_meta_single(struct lyd_meta *meta);
Michal Vasko52927e22020-03-16 17:26:14 +0100873
874/**
Michal Vasko3a41dff2020-07-15 14:30:28 +0200875 * @brief Free the metadata instance with any following instances.
876 *
877 * @param[in] meta Metadata to free.
878 */
879void lyd_free_meta_siblings(struct lyd_meta *meta);
880
881/**
882 * @brief Free a single attribute.
Michal Vasko52927e22020-03-16 17:26:14 +0100883 *
884 * @param[in] ctx Context where the attributes were created.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200885 * @param[in] attr Attribute to free.
Michal Vasko52927e22020-03-16 17:26:14 +0100886 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200887void ly_free_attr_single(const struct ly_ctx *ctx, struct lyd_attr *attr);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200888
889/**
890 * @brief Free the attribute with any following attributes.
891 *
892 * @param[in] ctx Context where the attributes were created.
893 * @param[in] attr First attribute to free.
894 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200895void ly_free_attr_siblings(const struct ly_ctx *ctx, struct lyd_attr *attr);
Radek Krejcie7b95092019-05-15 11:03:07 +0200896
Radek Krejci084289f2019-07-09 17:35:30 +0200897/**
898 * @brief Check type restrictions applicable to the particular leaf/leaf-list with the given string @p value.
899 *
900 * The given node is not modified in any way - it is just checked if the @p value can be set to the node.
901 *
902 * If there is no data node instance and you are fine with checking just the type's restrictions without the
903 * data tree context (e.g. for the case of require-instance restriction), use lys_value_validate().
904 *
905 * @param[in] ctx libyang context for logging (function does not log errors when @p ctx is NULL)
906 * @param[in] node Data node for the @p value.
Michal Vaskof937cfe2020-08-03 16:07:12 +0200907 * @param[in] value String value to be checked, it is expected to be in JSON format.
Radek Krejci084289f2019-07-09 17:35:30 +0200908 * @param[in] value_len Length of the given @p value (mandatory).
Michal Vaskof03ed032020-03-04 13:31:44 +0100909 * @param[in] tree Data tree (e.g. when validating RPC/Notification) where the required data instance (leafref target,
910 * instance-identifier) can be placed. NULL in case the data tree is not yet complete,
911 * then LY_EINCOMPLETE can be returned.
Michal Vasko3701af52020-08-03 14:29:38 +0200912 * @param[out] realtype Optional real type of the value.
Radek Krejci084289f2019-07-09 17:35:30 +0200913 * @return LY_SUCCESS on success
914 * @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).
915 * @return LY_ERR value if an error occurred.
916 */
Michal Vasko44685da2020-03-17 15:38:06 +0100917LY_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 +0200918 const struct lyd_node *tree, struct lysc_type **realtype);
Radek Krejci084289f2019-07-09 17:35:30 +0200919
920/**
921 * @brief Compare the node's value with the given string value. The string value is first validated according to the node's type.
922 *
923 * @param[in] node Data node to compare.
924 * @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 +0200925 * it is validated and canonized if possible. But it is expected to be in JSON format.
Radek Krejci084289f2019-07-09 17:35:30 +0200926 * @param[in] value_len Length of the given @p value (mandatory).
Michal Vaskof03ed032020-03-04 13:31:44 +0100927 * @param[in] tree Data tree (e.g. when validating RPC/Notification) where the required data instance (leafref target,
928 * instance-identifier) can be placed. NULL in case the data tree is not yet complete,
929 * then LY_EINCOMPLETE can be returned.
Radek Krejci084289f2019-07-09 17:35:30 +0200930 * @return LY_SUCCESS on success
931 * @return LY_EINCOMPLETE in case of success when the @p trees is not provided and it was needed to finish the validation of
932 * the given string @p value (e.g. due to require-instance).
Michal Vaskob3ddccb2020-07-09 15:43:05 +0200933 * @return LY_ENOT if the values do not match.
Radek Krejci084289f2019-07-09 17:35:30 +0200934 * @return LY_ERR value if an error occurred.
935 */
Michal Vaskof937cfe2020-08-03 16:07:12 +0200936LY_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 +0200937
Radek Krejci576b23f2019-07-12 14:06:32 +0200938/**
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200939 * @defgroup datacompareoptions Data compare options
940 * @ingroup datatree
941 *
Radek Krejci22ebdba2019-07-25 13:59:43 +0200942 * Various options to change the lyd_compare() behavior.
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200943 */
944#define LYD_COMPARE_FULL_RECURSION 0x01 /* lists and containers are the same only in case all they children
945 (subtree, so direct as well as indirect children) are the same. By default,
946 containers are the same in case of the same schema node and lists are the same
947 in case of equal keys (keyless lists do the full recursion comparison all the time). */
948#define LYD_COMPARE_DEFAULTS 0x02 /* By default, implicit and explicit default nodes are considered to be equal. This flag
949 changes this behavior and implicit (automatically created default node) and explicit
950 (explicitly created node with the default value) default nodes are considered different. */
Michal Vasko60ea6352020-06-29 13:39:39 +0200951/** @} datacompareoptions */
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200952
953/**
954 * @brief Compare 2 data nodes if they are equivalent.
955 *
956 * @param[in] node1 The first node to compare.
957 * @param[in] node2 The second node to compare.
Radek Krejcic5ad9652019-09-11 11:31:51 +0200958 * @param[in] options Various @ref datacompareoptions.
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200959 * @return LY_SUCCESS if the nodes are equivalent.
960 * @return LY_ENOT if the nodes are not equivalent.
961 */
Michal Vasko8f359bf2020-07-28 10:41:15 +0200962LY_ERR lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, int options);
963
964/**
965 * @brief Compare 2 lists of siblings if they are equivalent.
966 *
967 * @param[in] node1 The first sibling list to compare.
968 * @param[in] node2 The second sibling list to compare.
969 * @param[in] options Various @ref datacompareoptions.
970 * @return LY_SUCCESS if all the siblings are equivalent.
971 * @return LY_ENOT if the siblings are not equivalent.
972 */
973LY_ERR lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, int options);
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200974
975/**
Michal Vasko21725742020-06-29 11:49:25 +0200976 * @brief Compare 2 metadata.
977 *
978 * @param[in] meta1 First metadata.
979 * @param[in] meta2 Second metadata.
980 * @return LY_SUCCESS if the metadata are equivalent.
981 * @return LY_ENOT if not.
982 */
983LY_ERR lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2);
984
985/**
Radek Krejci22ebdba2019-07-25 13:59:43 +0200986 * @defgroup dupoptions Data duplication options
987 * @ingroup datatree
988 *
989 * Various options to change lyd_dup() behavior.
990 *
991 * Default behavior:
992 * - only the specified node is duplicated without siblings, parents, or children.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200993 * - all the metadata of the duplicated nodes are also duplicated.
Radek Krejci22ebdba2019-07-25 13:59:43 +0200994 * @{
995 */
996
997#define LYD_DUP_RECURSIVE 0x01 /**< Duplicate not just the node but also all the children. Note that
998 list's keys are always duplicated. */
Michal Vaskoa29a5762020-06-23 13:28:49 +0200999#define LYD_DUP_NO_META 0x02 /**< Do not duplicate metadata of any node. */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001000#define LYD_DUP_WITH_PARENTS 0x04 /**< If a nested node is being duplicated, duplicate also all the parents.
1001 Keys are also duplicated for lists. Return value does not change! */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001002#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 +02001003 its validation/default node state. */
Radek Krejci22ebdba2019-07-25 13:59:43 +02001004
1005/** @} dupoptions */
1006
1007/**
1008 * @brief Create a copy of the specified data tree \p node. Schema references are kept the same.
1009 *
Radek Krejci22ebdba2019-07-25 13:59:43 +02001010 * @param[in] node Data tree node to be duplicated.
1011 * @param[in] parent Optional parent node where to connect the duplicated node(s).
Michal Vasko3a41dff2020-07-15 14:30:28 +02001012 * If set in combination with LYD_DUP_WITH_PARENTS, the parents chain is duplicated until it comes to and connects with
1013 * the @p parent.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001014 * @param[in] options Bitmask of options flags, see @ref dupoptions.
Michal Vasko3a41dff2020-07-15 14:30:28 +02001015 * @param[out] dup Optional created copy of the node. Note that in case the parents chain is duplicated for the duplicated
1016 * node(s) (when LYD_DUP_WITH_PARENTS used), the first duplicated node is still returned.
1017 * @return LY_ERR value.
Radek Krejci22ebdba2019-07-25 13:59:43 +02001018 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001019LY_ERR lyd_dup_single(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup);
1020
1021/**
1022 * @brief Create a copy of the specified data tree \p node with any following siblings. Schema references are kept the same.
1023 *
1024 * @param[in] node Data tree node to be duplicated.
1025 * @param[in] parent Optional parent node where to connect the duplicated node(s).
1026 * If set in combination with LYD_DUP_WITH_PARENTS, the parents chain is duplicated until it comes to and connects with
1027 * the @p parent.
1028 * @param[in] options Bitmask of options flags, see @ref dupoptions.
1029 * @param[out] dup Optional created copy of the node. Note that in case the parents chain is duplicated for the duplicated
1030 * node(s) (when LYD_DUP_WITH_PARENTS used), the first duplicated node is still returned.
1031 * @return LY_ERR value.
1032 */
1033LY_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 +02001034
1035/**
Michal Vasko25a32822020-07-09 15:48:22 +02001036 * @brief Create a copy of the metadata.
1037 *
1038 * @param[in] meta Metadata to copy.
Michal Vasko3a41dff2020-07-15 14:30:28 +02001039 * @param[in] parent Node where to append the new metadata.
1040 * @param[out] dup Optional created metadata copy.
1041 * @return LY_ERR value.
Michal Vasko25a32822020-07-09 15:48:22 +02001042 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001043LY_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 +02001044
1045/**
Michal Vasko4490d312020-06-16 13:08:55 +02001046 * @defgroup mergeoptions Data merge options.
1047 * @ingroup datatree
Radek Krejci576b23f2019-07-12 14:06:32 +02001048 *
Michal Vasko4490d312020-06-16 13:08:55 +02001049 * Various options to change lyd_merge() behavior.
1050 *
1051 * Default behavior:
1052 * - source data tree is not modified in any way,
Michal Vasko3a41dff2020-07-15 14:30:28 +02001053 * - any default nodes in the source are ignored if there are explicit nodes in the target.
Michal Vasko4490d312020-06-16 13:08:55 +02001054 * @{
1055 */
1056
1057#define LYD_MERGE_DESTRUCT 0x01 /**< Spend source data tree in the function, it cannot be used afterwards! */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001058#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 +02001059
1060/** @} mergeoptions */
1061
1062/**
Michal Vasko3a41dff2020-07-15 14:30:28 +02001063 * @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 +02001064 * is called on the resulting data tree (data from more cases may be present, default and non-default values).
1065 *
1066 * @param[in,out] target Target data tree to merge into, must be a top-level tree.
1067 * @param[in] source Source data tree to merge, must be a top-level tree.
1068 * @param[in] options Bitmask of option flags, see @ref mergeoptions.
1069 * @return LY_SUCCESS on success,
1070 * @return LY_ERR value on error.
1071 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001072LY_ERR lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, int options);
1073
1074/**
1075 * @brief Merge the source data tree with any following siblings into the target data tree. Merge may not be
1076 * complete until validation called on the resulting data tree (data from more cases may be present, default
1077 * and non-default values).
1078 *
1079 * @param[in,out] target Target data tree to merge into, must be a top-level tree.
1080 * @param[in] source Source data tree to merge, must be a top-level tree.
1081 * @param[in] options Bitmask of option flags, see @ref mergeoptions.
1082 * @return LY_SUCCESS on success,
1083 * @return LY_ERR value on error.
1084 */
1085LY_ERR lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, int options);
Michal Vasko4490d312020-06-16 13:08:55 +02001086
1087/**
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001088 * @defgroup diffoptions Data diff options.
1089 * @ingroup datatree
1090 *
1091 * Various options to change lyd_diff() behavior.
1092 *
1093 * Default behavior:
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001094 * - any default nodes are treated as non-existent and ignored.
1095 * @{
1096 */
1097
Michal Vasko3a41dff2020-07-15 14:30:28 +02001098#define LYD_DIFF_DEFAULTS 0x01 /**< Default nodes in the trees are not ignored but treated similarly to explicit
1099 nodes. Also, leaves and leaf-lists are added into diff even in case only their
1100 default flag (state) was changed. */
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001101
1102/** @} diffoptions */
1103
1104/**
1105 * @brief Learn the differences between 2 data trees.
1106 *
1107 * The resulting diff is represented as a data tree with specific metadata from the internal 'yang'
1108 * module. Most importantly, every node has an effective 'operation' metadata. If there is none
1109 * defined on the node, it inherits the operation from the nearest parent. Top-level nodes must
1110 * always have the 'operation' metadata defined. Additional metadata ('orig-default', 'value',
1111 * 'orig-value', 'key', 'orig-key') are used for storing more information about the value in the first
1112 * or the second tree.
1113 *
1114 * The diff tree is completely independent on the @p first and @p second trees, meaning all
1115 * the information about the change is stored in the diff and the trees are not needed.
1116 *
1117 * !! Caution !!
1118 * The diff tree should never be validated because it may easily not be valid! For example,
1119 * when data from one case branch are deleted and data from another branch created - data from both
1120 * branches are then stored in the diff tree simultaneously.
1121 *
1122 * @param[in] first First data tree.
1123 * @param[in] second Second data tree.
1124 * @param[in] options Bitmask of options flags, see @ref diffoptions.
1125 * @param[out] diff Generated diff.
1126 * @return LY_SUCCESS on success,
1127 * @return LY_ERR on error.
1128 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001129LY_ERR lyd_diff_tree(const struct lyd_node *first, const struct lyd_node *second, int options, struct lyd_node **diff);
1130
1131/**
1132 * @brief Learn the differences between 2 data trees including all the following siblings.
1133 *
1134 * @param[in] first First data tree.
1135 * @param[in] second Second data tree.
1136 * @param[in] options Bitmask of options flags, see @ref diffoptions.
1137 * @param[out] diff Generated diff.
1138 * @return LY_SUCCESS on success,
1139 * @return LY_ERR on error.
1140 */
1141LY_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 +02001142
1143/**
1144 * @brief Callback for diff nodes.
1145 *
1146 * @param[in] diff_node Diff node.
1147 * @param[in] data_node Matching node in data.
1148 * @param[in] cb_data Arbitrary callback data.
1149 * @return LY_ERR value.
1150 */
1151typedef LY_ERR (*lyd_diff_cb)(const struct lyd_node *diff_node, struct lyd_node *data_node, void *cb_data);
1152
1153/**
Michal Vasko3a41dff2020-07-15 14:30:28 +02001154 * @brief Apply the whole diff on a data tree but restrict the operation to one module.
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001155 *
1156 * @param[in,out] data Data to apply the diff on.
1157 * @param[in] diff Diff to apply.
1158 * @param[in] mod Module, whose diff/data only to consider, NULL for all modules.
1159 * @param[in] diff_cb Optional diff callback that will be called for every changed node.
1160 * @param[in] cb_data Arbitrary callback data.
1161 * @return LY_SUCCESS on success,
1162 * @return LY_ERR on error.
1163 */
1164LY_ERR lyd_diff_apply_module(struct lyd_node **data, const struct lyd_node *diff, const struct lys_module *mod,
1165 lyd_diff_cb diff_cb, void *cb_data);
1166
1167/**
Michal Vasko3a41dff2020-07-15 14:30:28 +02001168 * @brief Apply the whole diff tree on a data tree.
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001169 *
1170 * @param[in,out] data Data to apply the diff on.
1171 * @param[in] diff Diff to apply.
1172 * @return LY_SUCCESS on success,
1173 * @return LY_ERR on error.
1174 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001175LY_ERR lyd_diff_apply_all(struct lyd_node **data, const struct lyd_node *diff);
Michal Vaskoe893ddd2020-06-23 13:35:20 +02001176
1177/**
Michal Vaskoe6323f62020-07-09 15:49:02 +02001178 * @brief Merge 2 diffs into each other but restrict the operation to one module.
1179 *
1180 * The diffs must be possible to be merged, which is guaranteed only if the source diff was
1181 * created on data that had the target diff applied on them. In other words, this sequence is legal
1182 *
Michal Vasko04f85912020-08-07 12:14:58 +02001183 * 1) diff1 from data1 and data2 -> data11 from apply diff1 on data1 -> diff2 from data11 and data3 ->
1184 * -> data 33 from apply diff2 on data1
Michal Vaskoe6323f62020-07-09 15:49:02 +02001185 *
1186 * and reusing these diffs
1187 *
Michal Vasko04f85912020-08-07 12:14:58 +02001188 * 2) diff11 from merge diff1 and diff2 -> data33 from apply diff11 on data1
Michal Vaskoe6323f62020-07-09 15:49:02 +02001189 *
Michal Vaskofb737aa2020-08-06 13:53:53 +02001190 * @param[in,out] diff Target diff to merge into.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001191 * @param[in] src_diff Source diff.
1192 * @param[in] mod Module, whose diff only to consider, NULL for all modules.
1193 * @param[in] diff_cb Optional diff callback that will be called for every changed node.
1194 * @param[in] cb_data Arbitrary callback data.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001195 * @return LY_SUCCESS on success,
1196 * @return LY_ERR on error.
1197 */
Michal Vaskofb737aa2020-08-06 13:53:53 +02001198LY_ERR lyd_diff_merge_module(struct lyd_node **diff, const struct lyd_node *src_diff, const struct lys_module *mod,
1199 lyd_diff_cb diff_cb, void *cb_data);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001200
1201/**
Michal Vasko04f85912020-08-07 12:14:58 +02001202 * @brief Merge 2 diff trees into each other.
1203 *
1204 * @param[in,out] diff_first Target diff first sibling to merge into.
1205 * @param[in] diff_parent Target diff parent to merge into.
1206 * @param[in] src_sibling Source diff sibling to merge.
1207 * @param[in] diff_cb Optional diff callback that will be called for every changed node.
1208 * @param[in] cb_data Arbitrary callback data.
1209 * @return LY_SUCCESS on success,
1210 * @return LY_ERR on error.
1211 */
1212LY_ERR lyd_diff_merge_tree(struct lyd_node **diff_first, struct lyd_node *diff_parent, const struct lyd_node *src_sibling,
1213 lyd_diff_cb diff_cb, void *cb_data);
1214
1215/**
Michal Vaskoe6323f62020-07-09 15:49:02 +02001216 * @brief Merge 2 diffs into each other.
1217 *
Michal Vaskoe6323f62020-07-09 15:49:02 +02001218 * @param[in,out] diff Target diff to merge into.
Michal Vaskofb737aa2020-08-06 13:53:53 +02001219 * @param[in] src_diff Source diff.
Michal Vaskoe6323f62020-07-09 15:49:02 +02001220 * @return LY_SUCCESS on success,
1221 * @return LY_ERR on error.
1222 */
Michal Vaskofb737aa2020-08-06 13:53:53 +02001223LY_ERR lyd_diff_merge_all(struct lyd_node **diff, const struct lyd_node *src_diff);
Michal Vaskoe6323f62020-07-09 15:49:02 +02001224
1225/**
Michal Vasko4231fb62020-07-13 13:54:47 +02001226 * @brief Reverse a diff and make the opposite changes. Meaning change create to delete, delete to create,
1227 * or move from place A to B to move from B to A and so on.
1228 *
1229 * @param[in] src_diff Diff to reverse.
1230 * @param[out] diff Reversed diff.
1231 * @return LY_SUCCESS on success.
1232 * @return LY_ERR on error.
1233 */
Michal Vasko3a41dff2020-07-15 14:30:28 +02001234LY_ERR lyd_diff_reverse_all(const struct lyd_node *src_diff, struct lyd_node **diff);
Michal Vasko4231fb62020-07-13 13:54:47 +02001235
1236/**
Michal Vasko4490d312020-06-16 13:08:55 +02001237 * @brief Find the target in data of a compiled ly_path structure (instance-identifier).
1238 *
1239 * @param[in] path Compiled path structure.
Michal Vaskof03ed032020-03-04 13:31:44 +01001240 * @param[in] tree Data tree to be searched.
Michal Vasko4490d312020-06-16 13:08:55 +02001241 * @return Found target node,
1242 * @return NULL if not found.
Radek Krejci576b23f2019-07-12 14:06:32 +02001243 */
Michal Vasko004d3152020-06-11 19:59:22 +02001244const struct lyd_node_term *lyd_target(const struct ly_path *path, const struct lyd_node *tree);
Radek Krejci084289f2019-07-09 17:35:30 +02001245
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001246/**
1247 * @brief Get string value of a term data \p node.
1248 *
1249 * @param[in] node Data tree node with the value.
1250 * @param[out] dynamic Whether the string value was dynmically allocated.
1251 * @return String value of @p node, if @p dynamic, needs to be freed.
1252 */
1253const char *lyd_value2str(const struct lyd_node_term *node, int *dynamic);
1254
1255/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001256 * @brief Get string value of a metadata \p meta.
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001257 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001258 * @param[in] meta Metadata with the value.
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001259 * @param[out] dynamic Whether the string value was dynmically allocated.
Michal Vasko9f96a052020-03-10 09:41:45 +01001260 * @return String value of @p meta, if @p dynamic, needs to be freed.
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001261 */
Michal Vasko9f96a052020-03-10 09:41:45 +01001262const char *lyd_meta2str(const struct lyd_meta *meta, int *dynamic);
Michal Vasko5ec7cda2019-09-11 13:43:08 +02001263
1264/**
1265 * @brief Types of the different data paths.
1266 */
1267typedef enum {
Michal Vasko14654712020-02-06 08:35:21 +01001268 LYD_PATH_LOG, /**< Descriptive path format used in log messages */
Michal Vasko790b2bc2020-08-03 13:35:06 +02001269 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 +02001270} LYD_PATH_TYPE;
1271
1272/**
1273 * @brief Generate path of the given node in the requested format.
1274 *
1275 * @param[in] node Schema path of this node will be generated.
1276 * @param[in] pathtype Format of the path to generate.
1277 * @param[in,out] buffer Prepared buffer of the @p buflen length to store the generated path.
1278 * If NULL, memory for the complete path is allocated.
1279 * @param[in] buflen Size of the provided @p buffer.
1280 * @return NULL in case of memory allocation error, path of the node otherwise.
1281 * In case the @p buffer is NULL, the returned string is dynamically allocated and caller is responsible to free it.
1282 */
1283char *lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen);
1284
Michal Vaskoe444f752020-02-10 12:20:06 +01001285/**
Michal Vasko25a32822020-07-09 15:48:22 +02001286 * @brief Find a specific metadata.
1287 *
1288 * @param[in] first First metadata to consider.
1289 * @param[in] module Module of the metadata definition, may be NULL if @p name includes a prefix.
1290 * @param[in] name Name of the metadata to find, may not include a prefix (module name) if @p module is set.
1291 * @return Found metadata,
1292 * @return NULL if not found.
1293 */
1294struct lyd_meta *lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name);
1295
1296/**
Michal Vaskoda859032020-07-14 12:20:14 +02001297 * @brief Search in the given siblings (NOT recursively) for the first target instance with the same value.
Michal Vaskoe444f752020-02-10 12:20:06 +01001298 * Uses hashes - should be used whenever possible for best performance.
1299 *
1300 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
1301 * @param[in] target Target node to find.
Michal Vasko9b368d32020-02-14 13:53:31 +01001302 * @param[out] match Can be NULL, otherwise the found data node.
Michal Vaskoe444f752020-02-10 12:20:06 +01001303 * @return LY_SUCCESS on success, @p match set.
1304 * @return LY_ENOTFOUND if not found, @p match set to NULL.
1305 * @return LY_ERR value if another error occurred.
1306 */
1307LY_ERR lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match);
1308
1309/**
Michal Vaskoe444f752020-02-10 12:20:06 +01001310 * @brief Search in the given siblings for the first schema instance.
1311 * Uses hashes - should be used whenever possible for best performance.
1312 *
1313 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
1314 * @param[in] schema Schema node of the data node to find.
Michal Vaskob104f112020-07-17 09:54:54 +02001315 * @param[in] key_or_value If it is NULL, the first schema node data instance is found. For nodes with many
1316 * instances, it can be set based on the type of @p schema:
Michal Vaskoe444f752020-02-10 12:20:06 +01001317 * LYS_LEAFLIST:
1318 * Searched instance value.
1319 * LYS_LIST:
Michal Vasko90932a92020-02-12 14:33:03 +01001320 * Searched instance key values in the form of "[key1='val1'][key2='val2']...".
1321 * The keys do not have to be ordered but all of them must be set.
1322 *
1323 * Note that any explicit values (leaf-list or list key values) will be canonized first
1324 * before comparison. But values that do not have a canonical value are expected to be in the
1325 * JSON format!
Michal Vaskof03ed032020-03-04 13:31:44 +01001326 * @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 +01001327 * @param[out] match Can be NULL, otherwise the found data node.
Michal Vaskoe444f752020-02-10 12:20:06 +01001328 * @return LY_SUCCESS on success, @p match set.
1329 * @return LY_ENOTFOUND if not found, @p match set to NULL.
1330 * @return LY_EINVAL if @p schema is a key-less list.
1331 * @return LY_ERR value if another error occurred.
1332 */
1333LY_ERR lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
1334 size_t val_len, struct lyd_node **match);
1335
Michal Vaskoccc02342020-05-21 10:09:21 +02001336/**
1337 * @brief Search in the given data for instances of nodes matching the provided XPath.
1338 *
Michal Vaskob104f112020-07-17 09:54:54 +02001339 * The expected format of the expression is ::LYD_JSON, meaning the first node in every path
Michal Vasko61ac2f62020-05-25 12:39:51 +02001340 * must have its module name as prefix or be the special `*` value for all the nodes.
1341 *
Michal Vasko19a30602020-05-25 10:40:19 +02001342 * If a list instance is being selected with all its key values specified (but not necessarily ordered)
1343 * in the form `list[key1='val1'][key2='val2'][key3='val3']` or a leaf-list instance in the form
1344 * `leaf-list[.='val']`, these instances are found using hashes with constant (*O(1)*) complexity
1345 * (unless they are defined in top-level). Other predicates can still follow the aforementioned ones.
1346 *
Michal Vaskoccc02342020-05-21 10:09:21 +02001347 * @param[in] ctx_node XPath context node.
1348 * @param[in] xpath Data XPath expression filtering the matching nodes. ::LYD_JSON format is expected.
1349 * @param[out] set Set of found data nodes. In case the result is a number, a string, or a boolean,
1350 * the returned set is empty.
1351 * @return LY_SUCCESS on success, @p set is returned.
1352 * @return LY_ERR value if an error occurred.
1353 */
1354LY_ERR lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set);
1355
Radek Krejcie7b95092019-05-15 11:03:07 +02001356#ifdef __cplusplus
1357}
1358#endif
1359
1360#endif /* LY_TREE_DATA_H_ */