blob: dcadc9d010fdc3c4d8e508c294f0fcf4ccef5814 [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"
Michal Vasko9b368d32020-02-14 13:53:31 +010022#include "set.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include "tree.h"
24#include "tree_schema.h"
25
26struct ly_ctx;
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32/**
33 * @defgroup datatree Data Tree
Radek Krejci2ff0d572020-05-21 15:27:28 +020034 * @ingroup trees
Radek Krejcie7b95092019-05-15 11:03:07 +020035 * @{
36 *
37 * Data structures and functions to manipulate and access instance data tree.
38 */
39
40/**
41 * @brief Macro to iterate via all elements in a data tree. This is the opening part
42 * to the #LYD_TREE_DFS_END - they always have to be used together.
43 *
44 * The function follows deep-first search algorithm:
45 * <pre>
46 * 1
47 * / \
Michal Vaskoc193ce92020-03-06 11:04:48 +010048 * 2 4
Radek Krejcie7b95092019-05-15 11:03:07 +020049 * / / \
Michal Vaskoc193ce92020-03-06 11:04:48 +010050 * 3 5 6
Radek Krejcie7b95092019-05-15 11:03:07 +020051 * </pre>
52 *
Radek Krejci0935f412019-08-20 16:15:18 +020053 * Use the same parameters for #LYD_TREE_DFS_BEGIN and #LYD_TREE_DFS_END. While
Radek Krejcie7b95092019-05-15 11:03:07 +020054 * START can be any of the lyd_node* types, NEXT and ELEM variables are expected
55 * to be pointers to a generic struct lyd_node.
56 *
57 * Since the next node is selected as part of #LYD_TREE_DFS_END, do not use
58 * continue statement between the #LYD_TREE_DFS_BEGIN and #LYD_TREE_DFS_END.
59 *
60 * Use with opening curly bracket '{' after the macro.
61 *
62 * @param START Pointer to the starting element processed first.
63 * @param NEXT Temporary storage, do not use.
64 * @param ELEM Iterator intended for use in the block.
65 */
66#define LYD_TREE_DFS_BEGIN(START, NEXT, ELEM) \
67 for ((ELEM) = (NEXT) = (START); \
68 (ELEM); \
69 (ELEM) = (NEXT))
70
71/**
72 * @brief Macro to iterate via all elements in a tree. This is the closing part
73 * to the #LYD_TREE_DFS_BEGIN - they always have to be used together.
74 *
75 * Use the same parameters for #LYD_TREE_DFS_BEGIN and #LYD_TREE_DFS_END. While
76 * START can be any of the lyd_node* types, NEXT and ELEM variables are expected
77 * to be pointers to a generic struct lyd_node.
78 *
79 * Use with closing curly bracket '}' after the macro.
80 *
81 * @param START Pointer to the starting element processed first.
82 * @param NEXT Temporary storage, do not use.
83 * @param ELEM Iterator intended for use in the block.
84 */
85
86#define LYD_TREE_DFS_END(START, NEXT, ELEM) \
87 /* select element for the next run - children first */ \
Radek Krejcidae0ee82020-05-06 16:53:24 +020088 (NEXT) = lyd_node_children((struct lyd_node*)ELEM); \
Radek Krejcie7b95092019-05-15 11:03:07 +020089 if (!(NEXT)) { \
90 /* no children */ \
91 if ((ELEM) == (struct lyd_node*)(START)) { \
92 /* we are done, (START) has no children */ \
93 break; \
94 } \
95 /* try siblings */ \
96 (NEXT) = (ELEM)->next; \
97 } \
98 while (!(NEXT)) { \
99 /* parent is already processed, go to its sibling */ \
100 (ELEM) = (struct lyd_node*)(ELEM)->parent; \
101 /* no siblings, go back through parents */ \
102 if ((ELEM)->parent == (START)->parent) { \
103 /* we are done, no next element to process */ \
104 break; \
105 } \
106 (NEXT) = (ELEM)->next; \
107 }
108
109/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200110 * @brief Macro to get context from a data tree node.
111 */
Michal Vasko52927e22020-03-16 17:26:14 +0100112#define LYD_NODE_CTX(node) ((node)->schema ? (node)->schema->module->ctx : ((struct lyd_node_opaq *)(node))->ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200113
114/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200115 * @brief Data input/output formats supported by libyang [parser](@ref howtodataparsers) and
Radek Krejcid91d4da2020-05-18 11:28:02 +0200116 * [printer](@ref howtodataprinters) functions. Also used for value prefix format (TODO link to prefix formats descriptions).
Radek Krejcie7b95092019-05-15 11:03:07 +0200117 */
118typedef enum {
Michal Vasko52927e22020-03-16 17:26:14 +0100119 LYD_SCHEMA = 0, /**< invalid instance data format, value prefixes map to YANG import prefixes */
120 LYD_XML, /**< XML instance data format, value prefixes map to XML namespace prefixes */
121 LYD_JSON, /**< JSON instance data format, value prefixes map to module names */
Radek Krejci355bf4f2019-07-16 17:14:16 +0200122#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200123 LYD_LYB, /**< LYB format of the instance data */
124#endif
125} LYD_FORMAT;
126
127/**
Radek Krejci59583bb2019-09-11 12:57:55 +0200128 * @brief List of possible value types stored in ::lyd_node_any.
Radek Krejcie7b95092019-05-15 11:03:07 +0200129 */
130typedef enum {
Radek Krejci22ebdba2019-07-25 13:59:43 +0200131 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 +0200132 is directly connected into the anydata node without duplication, caller is supposed to not manipulate
133 with the data after a successful call (including calling lyd_free() on the provided data) */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200134 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 +0200135 as string). XML sensitive characters (such as & or \>) are automatically escaped when the anydata
136 is printed in XML format. */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200137 LYD_ANYDATA_XML, /**< Value is a string containing the serialized XML data. */
138 LYD_ANYDATA_JSON, /**< Value is a string containing the data modeled by YANG and encoded as I-JSON. */
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200139#if 0 /* TODO LYB format */
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 Krejci1f05b6a2019-07-18 16:15:06 +0200141#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200142} LYD_ANYDATA_VALUETYPE;
143
144/** @} */
145
146/**
147 * @brief YANG data representation
148 */
149struct lyd_value {
Radek Krejci950f6a52019-09-12 17:15:32 +0200150 const char *original; /**< Original string representation of the value. It is never NULL, but (canonical) string representation
151 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 +0200152 union {
Radek Krejcie7b95092019-05-15 11:03:07 +0200153 int8_t boolean; /**< 0 as false, 1 as true */
154 int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200155 int8_t int8; /**< 8-bit signed integer */
156 int16_t int16; /**< 16-bit signed integer */
157 int32_t int32; /**< 32-bit signed integer */
158 int64_t int64; /**< 64-bit signed integer */
159 uint8_t uint8; /**< 8-bit unsigned integer */
160 uint16_t uint16; /**< 16-bit signed integer */
161 uint32_t uint32; /**< 32-bit signed integer */
162 uint64_t uint64; /**< 64-bit signed integer */
Radek Krejcie7b95092019-05-15 11:03:07 +0200163 struct lysc_type_bitenum_item *enum_item; /**< pointer to the definition of the enumeration value */
Radek Krejci849a62a2019-05-22 15:29:05 +0200164 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 +0200165 struct lysc_ident *ident; /**< pointer to the schema definition of the identityref value */
Radek Krejciefbb3922019-07-15 12:58:00 +0200166
167 struct lyd_value_subvalue {
168 struct lyd_value_prefix {
169 const char *prefix; /**< prefix string used in the canonized string to identify the mod of the YANG schema */
170 const struct lys_module *mod; /**< YANG schema module identified by the prefix string */
171 } *prefixes; /**< list of mappings between prefix in canonized value to a YANG schema ([sized array](@ref sizedarrays)) */
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200172 struct lyd_value *value; /**< representation of the value according to the selected union's subtype (stored as lyd_value::realpath
173 here, in subvalue structure */
174 } *subvalue; /**< data to represent data with multiple types (union). Original value is stored in the main
Michal Vasko9b368d32020-02-14 13:53:31 +0100175 lyd_value:canonical_cache while the lyd_value_subvalue::value contains representation according to the
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200176 one of the union's type. The lyd_value_subvalue:prefixes provides (possible) mappings from prefixes
177 in original value to YANG modules. These prefixes are necessary to parse original value to the union's
178 subtypes. */
Radek Krejci084289f2019-07-09 17:35:30 +0200179
180 struct lyd_value_path {
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200181 const struct lysc_node *node; /**< Schema node representing the path segment */
Radek Krejci084289f2019-07-09 17:35:30 +0200182 struct lyd_value_path_predicate {
183 union {
184 struct {
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200185 const struct lysc_node *key; /**< key node of the predicate, in case of the leaf-list-predicate, it is the leaf-list node itself */
186 struct lyd_value *value; /**< value representation according to the key's type */
187 }; /**< key-value pair for leaf-list-predicate and key-predicate (type 1 and 2) */
188 uint64_t position; /**< position value for the position-predicate (type 0) */
Radek Krejci084289f2019-07-09 17:35:30 +0200189 };
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200190 uint8_t type; /**< Predicate types (see YANG ABNF): 0 - position, 1 - key-predicate, 2 - leaf-list-predicate */
191 } *predicates; /**< [Sized array](@ref sizedarrays) of the path segment's predicates */
192 } *target; /**< [Sized array](@ref sizedarrays) of (instance-identifier's) path segments. */
Radek Krejci084289f2019-07-09 17:35:30 +0200193
Radek Krejcie7b95092019-05-15 11:03:07 +0200194 void *ptr; /**< generic data type structure used to store the data */
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200195 }; /**< The union is just a list of shorthands to possible values stored by a type's plugin. libyang itself uses the lyd_value::realtype
196 plugin's callbacks to work with the data. */
Radek Krejci084289f2019-07-09 17:35:30 +0200197
Radek Krejci950f6a52019-09-12 17:15:32 +0200198 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 +0200199 in the schema node of the data node since the type's store plugin can use other types/plugins for
200 storing data. Speaking about built-in types, this is the case of leafref which stores data as its
201 target type. In contrast, union type also use its subtype's callbacks, but inside an internal data
202 lyd_value::subvalue structure, so here is the pointer to the union type.
203 In general, this type is used to get free callback for this lyd_value structure, so it must reflect
204 the type used to store data directly in the same lyd_value instance. */
Radek Krejci950f6a52019-09-12 17:15:32 +0200205 void *canonical_cache; /**< Generic cache for type plugins to store data necessary to print canonical value. It can be the canonical
206 value itself or anything else useful to print the canonical form of the value. Plugin is responsible for
207 freeing the cache in its free callback. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200208};
209
210/**
Michal Vasko9f96a052020-03-10 09:41:45 +0100211 * @brief Metadata structure.
Radek Krejcie7b95092019-05-15 11:03:07 +0200212 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100213 * The structure provides information about metadata of a data element. Such attributes must map to
Radek Krejcie7b95092019-05-15 11:03:07 +0200214 * annotations as specified in RFC 7952. The only exception is the filter type (in NETCONF get operations)
215 * and edit-config's operation attributes. In XML, they are represented as standard XML attributes. In JSON,
216 * they are represented as JSON elements starting with the '@' character (for more information, see the
217 * YANG metadata RFC.
218 *
219 */
Michal Vasko9f96a052020-03-10 09:41:45 +0100220struct lyd_meta {
221 struct lyd_node *parent; /**< data node where the metadata is placed */
222 struct lyd_meta *next; /**< pointer to the next metadata of the same element */
223 struct lysc_ext_instance *annotation; /**< pointer to the annotation's definition */
224 const char *name; /**< metadata name */
225 struct lyd_value value; /**< metadata value representation */
Radek Krejcie7b95092019-05-15 11:03:07 +0200226};
227
Michal Vasko52927e22020-03-16 17:26:14 +0100228/**
229 * @brief Generic prefix and namespace mapping, meaning depends on the format.
230 */
231struct ly_prefix {
232 const char *pref;
233 const char *ns;
234};
235
236/**
237 * @brief Generic attribute structure.
238 */
239struct ly_attr {
240 struct lyd_node_opaq *parent; /**< data node where the attribute is placed */
241 struct ly_attr *next;
242 struct ly_prefix *val_prefs; /**< list of prefixes in the value ([sized array](@ref sizedarrays)) */
243 const char *name;
244 const char *value;
245
246 LYD_FORMAT format;
247 struct ly_prefix prefix; /**< name prefix, it is stored because they are a real pain to generate properly */
248
249};
Radek Krejcie7b95092019-05-15 11:03:07 +0200250
Michal Vasko1bf09392020-03-27 12:38:10 +0100251#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 +0200252#define LYD_NODE_TERM (LYS_LEAF|LYS_LEAFLIST) /**< Schema nodetype mask for lyd_node_term */
253#define LYD_NODE_ANY (LYS_ANYDATA) /**< Schema nodetype mask for lyd_node_any */
254
255/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100256 * @defgroup dnodeflags Data node flags
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200257 * @ingroup datatree
258 * @{
259 *
260 * Various flags of data nodes.
261 *
262 * 1 - container 5 - anydata/anyxml
263 * 2 - list 6 - rpc/action
264 * 3 - leaf 7 - notification
265 * 4 - leaflist
266 *
267 * bit name 1 2 3 4 5 6 7
268 * ---------------------+-+-+-+-+-+-+-+
269 * 1 LYD_DEFAULT |x| |x|x| | | |
270 * +-+-+-+-+-+-+-+
Michal Vasko5c4e5892019-11-14 12:31:38 +0100271 * 2 LYD_WHEN_TRUE |x|x|x|x|x| | |
Michal Vasko9b368d32020-02-14 13:53:31 +0100272 * +-+-+-+-+-+-+-+
273 * 3 LYD_NEW |x|x|x|x|x|x|x|
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200274 * ---------------------+-+-+-+-+-+-+-+
275 *
276 */
277
Michal Vasko5c4e5892019-11-14 12:31:38 +0100278#define LYD_DEFAULT 0x01 /**< default (implicit) node */
279#define LYD_WHEN_TRUE 0x02 /**< all when conditions of this node were evaluated to true */
Michal Vasko9b368d32020-02-14 13:53:31 +0100280#define LYD_NEW 0x04 /**< node was created after the last validation, is needed for the next validation */
Michal Vasko52927e22020-03-16 17:26:14 +0100281
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200282/** @} */
283
284/**
Michal Vasko9f96a052020-03-10 09:41:45 +0100285 * @brief Callback provided by the data/schema parsers to type plugins to resolve (format-specific) mapping between prefixes used
286 * in the value strings to the YANG schemas.
287 *
288 * Reverse function to ly_clb_get_prefix.
289 *
290 * XML uses XML namespaces, JSON uses schema names as prefixes, YIN/YANG uses prefixes of the imports.
291 *
292 * @param[in] ctx libyang context to find the schema.
293 * @param[in] prefix Prefix found in the value string
294 * @param[in] prefix_len Length of the @p prefix.
295 * @param[in] private Internal data needed by the callback.
296 * @return Pointer to the YANG schema identified by the provided prefix or NULL if no mapping found.
297 */
Michal Vasko52927e22020-03-16 17:26:14 +0100298typedef const struct lys_module *(*ly_clb_resolve_prefix)(const struct ly_ctx *ctx, const char *prefix, size_t prefix_len,
299 void *private);
Michal Vasko9f96a052020-03-10 09:41:45 +0100300
301/**
302 * @brief Callback provided by the data/schema printers to type plugins to resolve (format-specific) mapping between YANG module of a data object
303 * to prefixes used in the value strings.
304 *
305 * Reverse function to ly_clb_resolve_prefix.
306 *
307 * XML uses XML namespaces, JSON uses schema names as prefixes, YIN/YANG uses prefixes of the imports.
308 *
309 * @param[in] mod YANG module of the object.
310 * @param[in] private Internal data needed by the callback.
311 * @return String representing prefix for the object of the given YANG module @p mod.
312 */
313typedef const char *(*ly_clb_get_prefix)(const struct lys_module *mod, void *private);
314
315/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200316 * @brief Generic structure for a data node.
317 */
318struct lyd_node {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200319 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
320 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
321 used to get know that nodes are not equal, it cannot be used to decide that the
322 nodes are equal due to possible collisions. */
323 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Michal Vaskoecd62de2019-11-13 12:35:11 +0100324 const struct lysc_node *schema; /**< pointer to the schema definition of this node, note that the target can be not just
325 ::struct lysc_node but ::struct lysc_action or ::struct lysc_notif as well */
Radek Krejcie7b95092019-05-15 11:03:07 +0200326 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
327 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
328 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
329 never NULL. If there is no sibling node, pointer points to the node
330 itself. In case of the first node, this pointer points to the last
331 node in the list. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100332 struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200333
334#ifdef LY_ENABLED_LYD_PRIV
335 void *priv; /**< private user data, not used by libyang */
336#endif
337};
338
339/**
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200340 * @brief Data node structure for the inner data tree nodes - containers, lists, RPCs, actions and Notifications.
Radek Krejcie7b95092019-05-15 11:03:07 +0200341 */
342struct lyd_node_inner {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200343 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
344 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
345 used to get know that nodes are not equal, it cannot be used to decide that the
346 nodes are equal due to possible collisions. */
347 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200348 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
349 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
350 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
351 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
352 never NULL. If there is no sibling node, pointer points to the node
353 itself. In case of the first node, this pointer points to the last
354 node in the list. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100355 struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200356
357#ifdef LY_ENABLED_LYD_PRIV
358 void *priv; /**< private user data, not used by libyang */
359#endif
360
361 struct lyd_node *child; /**< pointer to the first child node. */
362 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 +0200363#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 +0200364};
365
366/**
Michal Vaskof03ed032020-03-04 13:31:44 +0100367 * @brief Data node structure for the terminal data tree nodes - leaves and leaf-lists.
Radek Krejcie7b95092019-05-15 11:03:07 +0200368 */
369struct lyd_node_term {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200370 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
371 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
372 used to get know that nodes are not equal, it cannot be used to decide that the
373 nodes are equal due to possible collisions. */
374 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200375 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
376 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
377 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
378 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
379 never NULL. If there is no sibling node, pointer points to the node
380 itself. In case of the first node, this pointer points to the last
381 node in the list. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100382 struct lyd_meta *meta; /**< pointer to the list of metadata of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200383
384#ifdef LY_ENABLED_LYD_PRIV
385 void *priv; /**< private user data, not used by libyang */
386#endif
387
388 struct lyd_value value; /**< node's value representation */
389};
390
391/**
392 * @brief Data node structure for the anydata data tree nodes - anydatas and anyxmls.
393 */
394struct lyd_node_any {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200395 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
396 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
397 used to get know that nodes are not equal, it cannot be used to decide that the
398 nodes are equal due to possible collisions. */
399 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200400 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
401 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
402 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
403 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
404 never NULL. If there is no sibling node, pointer points to the node
405 itself. In case of the first node, this pointer points to the last
406 node in the list. */
Michal Vasko9f96a052020-03-10 09:41:45 +0100407 struct lyd_meta *meta; /**< pointer to the list of attributes of this node */
Radek Krejcie7b95092019-05-15 11:03:07 +0200408
409#ifdef LY_ENABLED_LYD_PRIV
410 void *priv; /**< private user data, not used by libyang */
411#endif
412
Radek Krejciee4cab22019-07-17 17:07:47 +0200413 union {
414 struct lyd_node *tree; /**< data tree */
415 const char *str; /**< Generic string data */
416 const char *xml; /**< Serialized XML data */
417 const char *json; /**< I-JSON encoded string */
418 char *mem; /**< LYD_ANYDATA_LYB memory chunk */
419 } value; /**< pointer to the stored value representation of the anydata/anyxml node */
420 LYD_ANYDATA_VALUETYPE value_type;/**< type of the data stored as lyd_node_any::value */
Radek Krejcie7b95092019-05-15 11:03:07 +0200421};
422
423/**
Michal Vasko52927e22020-03-16 17:26:14 +0100424 * @brief Data node structure for unparsed (opaque) nodes.
425 */
426struct lyd_node_opaq {
427 uint32_t hash; /**< always 0 */
428 uint32_t flags; /**< always 0 */
429 const struct lysc_node *schema; /**< always NULL */
430 struct lyd_node *parent; /**< pointer to the parent node (NULL in case of root node) */
431 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
432 struct lyd_node *prev; /**< pointer to the previous sibling node (last sibling if there is none) */
433 struct ly_attr *attr;
434
435#ifdef LY_ENABLED_LYD_PRIV
436 void *priv; /**< private user data, not used by libyang */
437#endif
438
439 struct lyd_node *child; /**< pointer to the child node (NULL if there are none) */
440 const char *name;
441 LYD_FORMAT format;
442 struct ly_prefix prefix; /**< name prefix */
443 struct ly_prefix *val_prefs; /**< list of prefixes in the value ([sized array](@ref sizedarrays)) */
444 const char *value; /**< original value */
445 const struct ly_ctx *ctx; /**< libyang context */
446};
447
448/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200449 * @defgroup dataparseroptions Data parser options
450 * @ingroup datatree
451 *
452 * Various options to change the data tree parsers behavior.
453 *
Michal Vasko9b368d32020-02-14 13:53:31 +0100454 * Default parser behavior:
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100455 * - complete input file is always parsed. In case of XML, even not well-formed XML document (multiple top-level
456 * elements) is parsed in its entirety,
Michal Vasko9f96a052020-03-10 09:41:45 +0100457 * - parser silently ignores data without matching schema node definition,
458 * - list instances are checked whether they have all the keys, error is raised if not.
Michal Vasko9b368d32020-02-14 13:53:31 +0100459 *
460 * Default parser validation behavior:
461 * - the provided data are expected to provide complete datastore content (both the configuration and state data)
462 * and performs data validation according to all YANG rules, specifics follow,
Michal Vasko9f96a052020-03-10 09:41:45 +0100463 * - list instances are expected to have all the keys (it is not checked),
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100464 * - instantiated (status) obsolete data print a warning,
Michal Vasko9b368d32020-02-14 13:53:31 +0100465 * - all types are fully resolved (leafref/instance-identifier targets, unions) and must be valid (lists have
466 * all the keys, leaf(-lists) correct values),
467 * - when statements on existing nodes are evaluated, if not satisfied, a validation error is raised,
Michal Vaskoc193ce92020-03-06 11:04:48 +0100468 * - if-feature statements are evaluated,
469 * - invalid multiple data instances/data from several cases cause a validation error,
Michal Vasko9b368d32020-02-14 13:53:31 +0100470 * - default values are added.
Radek Krejcie7b95092019-05-15 11:03:07 +0200471 * @{
472 */
473
Michal Vasko52927e22020-03-16 17:26:14 +0100474#define LYD_OPT_PARSE_ONLY 0x0001 /**< Data will be only parsed and no validation will be performed. When statements
475 are kept unevaluated, union types may not be fully resolved, if-feature
476 statements are not checked, and default values are not added (only the ones
477 parsed are present). */
478#define LYD_OPT_TRUSTED 0x0002 /**< Data are considered trusted so they will be parsed as validated. If the parsed
479 data are not valid, using this flag may lead to some unexpected behavior!
480 This flag can be used only with #LYD_OPT_PARSE_ONLY. */
481#define LYD_OPT_STRICT 0x0004 /**< Instead of silently ignoring data without schema definition raise an error.
482 Do not combine with #LYD_OPT_OPAQ. */
483#define LYD_OPT_OPAQ 0x0008 /**< Instead of silently ignoring data without definition, parse them into
484 an opaq node. Do not combine with #LYD_OPT_STRICT. */
485#define LYD_OPT_NO_STATE 0x0010 /**< Forbid state data in the parsed data. */
486
487#define LYD_OPT_MASK 0xFFFF /**< Mask for all the parser options. */
Michal Vasko5b37a352020-03-06 13:38:33 +0100488
Michal Vasko9b368d32020-02-14 13:53:31 +0100489/** @} dataparseroptions */
490
491/**
492 * @defgroup datavalidationoptions Data validation options
493 * @ingroup datatree
494 *
495 * Various options to change data validation behaviour, both for the parser and separate validation.
496 *
497 * Default separate validation behavior:
498 * - the provided data are expected to provide complete datastore content (both the configuration and state data)
499 * and performs data validation according to all YANG rules, specifics follow,
Michal Vaskoe75ecfd2020-03-06 14:12:28 +0100500 * - instantiated (status) obsolete data print a warning,
Michal Vasko9b368d32020-02-14 13:53:31 +0100501 * - all types are fully resolved (leafref/instance-identifier targets, unions) and must be valid (lists have
502 * all the keys, leaf(-lists) correct values),
503 * - when statements on existing nodes are evaluated. Depending on the previous when state (from previous validation
504 * or parsing), the node is silently auto-deleted if the state changed from true to false, otherwise a validation error
505 * is raised if it evaluates to false,
Michal Vaskoc193ce92020-03-06 11:04:48 +0100506 * - if-feature statements are evaluated,
Michal Vasko9b368d32020-02-14 13:53:31 +0100507 * - data from several cases behave based on their previous state (from previous validation or parsing). If there existed
508 * already a case and another one was added, the previous one is silently auto-deleted. Otherwise (if data from 2 or
509 * more cases were created) a validation error is raised,
510 * - default values are added.
511 *
512 * @{
513 */
514
515#define LYD_VALOPT_NO_STATE 0x00010000 /**< Consider state data not allowed and raise an error if they are found. */
516#define LYD_VALOPT_DATA_ONLY 0x00020000 /**< Validate only modules whose data actually exist. */
Michal Vaskocb7526d2020-03-30 15:08:26 +0200517#define LYD_VALOPT_INPUT 0x00040000 /**< Validate RPC/action request (input parameters). */
518#define LYD_VALOPT_OUTPUT 0x00080000 /**< Validate RPC/action reply (output parameters). */
Michal Vasko9b368d32020-02-14 13:53:31 +0100519
Michal Vasko5b37a352020-03-06 13:38:33 +0100520#define LYD_VALOPT_MASK 0xFFFF0000 /**< Mask for all the validation options. */
521
Michal Vasko9b368d32020-02-14 13:53:31 +0100522/** @} datavalidationoptions */
523
Michal Vaskoa3881362020-01-21 15:57:35 +0100524//#define LYD_OPT_VAL_DIFF 0x40000 /**< Flag only for validation, store all the data node changes performed by the validation
525// in a diff structure. */
526//#define LYD_OPT_DATA_TEMPLATE 0x1000000 /**< Data represents YANG data template. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200527
Radek Krejcie7b95092019-05-15 11:03:07 +0200528/**
529 * @brief Get the node's children list if any.
530 *
531 * Decides the node's type and in case it has a children list, returns it.
532 * @param[in] node Node to check.
533 * @return Pointer to the first child node (if any) of the \p node.
534 */
Radek Krejcidae0ee82020-05-06 16:53:24 +0200535struct lyd_node *lyd_node_children(const struct lyd_node *node);
Radek Krejcie7b95092019-05-15 11:03:07 +0200536
537/**
Michal Vaskoc193ce92020-03-06 11:04:48 +0100538 * @brief Get the owner module of the data node. It is the module of the top-level schema node. Generally,
539 * in case of augments it is the target module, recursively, otherwise it is the module where the data node is defined.
540 *
541 * @param[in] node Data node to examine.
542 * @return Module owner of the node.
543 */
544const struct lys_module *lyd_owner_module(const struct lyd_node *node);
545
546/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200547 * @brief Parse (and validate) data from memory.
548 *
549 * In case of LY_XML format, the data string is parsed completely. It means that when it contains
550 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
551 * returned data node is a root of the first tree with other trees connected via the next pointer.
552 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
553 *
554 * @param[in] ctx Context to connect with the data tree being built here.
555 * @param[in] data Serialized data in the specified format.
556 * @param[in] format Format of the input data to be parsed.
Michal Vasko5b37a352020-03-06 13:38:33 +0100557 * @param[in] options Parser and validation options, see @ref parseroptions.
Radek Krejcie7b95092019-05-15 11:03:07 +0200558 * @return Pointer to the built data tree or NULL in case of empty \p data. To free the returned structure,
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200559 * use lyd_free_all().
560 * @return NULL in case of error. The error information can be then obtained using ly_err* functions.
Radek Krejcie7b95092019-05-15 11:03:07 +0200561 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100562struct lyd_node *lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options);
Radek Krejcie7b95092019-05-15 11:03:07 +0200563
564/**
565 * @brief Read (and validate) data from the given file descriptor.
566 *
567 * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc.
568 *
569 * In case of LY_XML format, the file content is parsed completely. It means that when it contains
570 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
571 * returned data node is a root of the first tree with other trees connected via the next pointer.
572 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
573 *
574 * @param[in] ctx Context to connect with the data tree being built here.
575 * @param[in] fd The standard file descriptor of the file containing the data tree in the specified format.
576 * @param[in] format Format of the input data to be parsed.
Michal Vaskoa3881362020-01-21 15:57:35 +0100577 * @param[in] options Parser options, see @ref parseroptions. \p format LYD_LYB uses #LYD_OPT_PARSE_ONLY implicitly.
Radek Krejcie7b95092019-05-15 11:03:07 +0200578 * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure,
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200579 * use lyd_free_all().
580 * @return NULL in case of error. The error information can be then obtained using ly_err* functions.
Radek Krejcie7b95092019-05-15 11:03:07 +0200581 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100582struct lyd_node *lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options);
Radek Krejcie7b95092019-05-15 11:03:07 +0200583
584/**
585 * @brief Read (and validate) data from the given file path.
586 *
587 * In case of LY_XML format, the file content is parsed completely. It means that when it contains
588 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
589 * returned data node is a root of the first tree with other trees connected via the next pointer.
590 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
591 *
592 * @param[in] ctx Context to connect with the data tree being built here.
593 * @param[in] path Path to the file containing the data tree in the specified format.
594 * @param[in] format Format of the input data to be parsed.
Michal Vaskoa3881362020-01-21 15:57:35 +0100595 * @param[in] options Parser options, see @ref parseroptions. \p format LYD_LYB uses #LYD_OPT_PARSE_ONLY implicitly.
Radek Krejcie7b95092019-05-15 11:03:07 +0200596 * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure,
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200597 * use lyd_free_all().
598 * @return NULL in case of error. The error information can be then obtained using ly_err* functions.
Radek Krejcie7b95092019-05-15 11:03:07 +0200599 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100600struct lyd_node *lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options);
Radek Krejcie7b95092019-05-15 11:03:07 +0200601
602/**
Michal Vaskof03ed032020-03-04 13:31:44 +0100603 * @brief Fully validate a data tree.
604 *
Michal Vaskob1b5c262020-03-05 14:29:47 +0100605 * @param[in,out] tree Data tree to recursively validate. May be changed by validation.
606 * @param[in] ctx libyang context. Can be NULL if @p tree is set.
Michal Vaskof03ed032020-03-04 13:31:44 +0100607 * @param[in] val_opts Validation options (@ref datavalidationoptions).
608 * @return LY_SUCCESS on success.
609 * @return LY_ERR error on error.
610 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100611LY_ERR lyd_validate(struct lyd_node **tree, const struct ly_ctx *ctx, int val_opts);
612
613/**
614 * @brief Fully validate a data tree.
615 *
616 * @param[in,out] tree Data tree to recursively validate. May be changed by validation.
617 * @param[in] modules Array of modules to validate.
618 * @param[in] mod_count Number of @p modules.
619 * @param[in] val_opts Validation options (@ref datavalidationoptions).
620 * @return LY_SUCCESS on success.
621 * @return LY_ERR error on error.
622 */
623LY_ERR lyd_validate_modules(struct lyd_node **tree, const struct lys_module **modules, int mod_count, int val_opts);
Michal Vaskof03ed032020-03-04 13:31:44 +0100624
625/**
Michal Vaskocb7526d2020-03-30 15:08:26 +0200626 * @brief Validate an RPC/action, notification, or RPC/action reply.
Michal Vaskofea12c62020-03-30 11:00:15 +0200627 *
Michal Vaskocb7526d2020-03-30 15:08:26 +0200628 * @param[in,out] op_tree Operation tree with any parents. It can point to the operation itself or any of
629 * its parents, only the operation subtree is actually validated.
630 * @param[in] tree Tree to be used for validating references from the operation subtree.
631 * @param[in] val_opts Specific validation option (@ref datavalidationoptions):
632 * 0 - no validation option for validation notifications,
633 * ::LYD_VALOPT_INPUT - for validating RPC/action request (input),
634 * ::LYD_VALOPT_OUTPUT - for validatin RPC/action reply (output).
Michal Vaskofea12c62020-03-30 11:00:15 +0200635 * @return LY_SUCCESS on success.
636 * @return LY_ERR error on error.
637 */
Michal Vaskocb7526d2020-03-30 15:08:26 +0200638LY_ERR lyd_validate_op(struct lyd_node *op_tree, const struct lyd_node *tree, int val_opts);
Michal Vaskofea12c62020-03-30 11:00:15 +0200639
640/**
Michal Vasko013a8182020-03-03 10:46:53 +0100641 * @brief Create a new inner node in a data tree.
642 *
643 * @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 +0100644 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100645 * @param[in] name Schema node name of the new data node. The node can be #LYS_CONTAINER, #LYS_NOTIF, #LYS_RPC, or #LYS_ACTION.
646 * @return New created node.
647 * @return NULL on error.
648 */
649struct lyd_node *lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name);
650
651/**
652 * @brief Create a new list node in a data tree.
653 *
654 * @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 +0100655 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100656 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
657 * @param[in] ... Ordered key values of the new list instance, all must be set. In case of an instance-identifier
658 * or identityref value, the JSON format is expected (module names instead of prefixes).
659 * @return New created node.
660 * @return NULL on error.
661 */
662struct lyd_node *lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ...);
663
664/**
665 * @brief Create a new list node in a data tree.
666 *
667 * @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 +0100668 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100669 * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST.
670 * @param[in] keys All key values predicate in the form of "[key1='val1'][key2='val2']...", they do not have to be ordered.
671 * In case of an instance-identifier or identityref value, the JSON format is expected (module names instead of prefixes).
672 * @return New created node.
673 * @return NULL on error.
674 */
675struct lyd_node *lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys);
676
677/**
678 * @brief Create a new term node in a data tree.
679 *
680 * @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 +0100681 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100682 * @param[in] name Schema node name of the new data node. The node can be #LYS_LEAF or #LYS_LEAFLIST.
683 * @param[in] val_str String form of the value of the node being created. In case of an instance-identifier or identityref
684 * value, the JSON format is expected (module names instead of prefixes).
685 * @return New created node.
686 * @return NULL on error.
687 */
688struct lyd_node *lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str);
689
690/**
691 * @brief Create a new any node in a data tree.
692 *
693 * @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 +0100694 * @param[in] module Module of the node being created. If NULL, @p parent module will be used.
Michal Vasko013a8182020-03-03 10:46:53 +0100695 * @param[in] name Schema node name of the new data node. The node can be #LYS_ANYDATA or #LYS_ANYXML.
696 * @param[in] value Value to be directly assigned to the node. Expected type is determined by @p value_type.
697 * @param[in] value_type Type of the provided value in @p value.
698 * @return New created node.
699 * @return NULL on error.
700 */
701struct lyd_node *lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name,
702 const void *value, LYD_ANYDATA_VALUETYPE value_type);
703
704/**
Michal Vaskof03ed032020-03-04 13:31:44 +0100705 * @brief Insert a child into a parent. It is inserted as the last child.
706 *
707 * - if the node is part of some other tree, it is automatically unlinked.
708 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
709 *
710 * @param[in] parent Parent node to insert into.
711 * @param[in] node Node to insert.
712 * @return LY_SUCCESS on success.
713 * @return LY_ERR error on error.
714 */
715LY_ERR lyd_insert(struct lyd_node *parent, struct lyd_node *node);
716
717/**
Michal Vaskob1b5c262020-03-05 14:29:47 +0100718 * @brief Insert a node into siblings. It is inserted as the last sibling.
719 *
720 * - if the node is part of some other tree, it is automatically unlinked.
721 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
722 *
723 * @param[in] sibling Siblings to insert into.
724 * @param[in] node Node to insert.
725 * @return LY_SUCCESS on success.
726 * @return LY_ERR error on error.
727 */
728LY_ERR lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node);
729
730/**
Michal Vaskof03ed032020-03-04 13:31:44 +0100731 * @brief Insert a node before another node that is its schema sibling.
732 *
733 * - if the node is part of some other tree, it is automatically unlinked.
734 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
735 *
736 * @param[in] sibling Sibling node to insert before.
737 * @param[in] node Node to insert.
738 * @return LY_SUCCESS on success.
739 * @return LY_ERR error on error.
740 */
741LY_ERR lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node);
742
743/**
744 * @brief Insert a node after another node that is its schema sibling.
745 *
746 * - if the node is part of some other tree, it is automatically unlinked.
747 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
748 *
749 * @param[in] sibling Sibling node to insert after.
750 * @param[in] node Node to insert.
751 * @return LY_SUCCESS on success.
752 * @return LY_ERR error on error.
753 */
754LY_ERR lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node);
755
756/**
757 * @brief Unlink the specified data subtree.
758 *
759 * @param[in] node Data tree node to be unlinked (together with all the children).
760 */
761void lyd_unlink_tree(struct lyd_node *node);
762
763/**
Radek Krejcib0849a22019-07-25 12:31:04 +0200764 * @brief Free all the nodes (even parents of the node) in the data tree.
Radek Krejcie7b95092019-05-15 11:03:07 +0200765 *
766 * @param[in] node Any of the nodes inside the tree.
767 */
768void lyd_free_all(struct lyd_node *node);
769
770/**
Radek Krejcib0849a22019-07-25 12:31:04 +0200771 * @brief Free all the sibling nodes.
772 *
773 * @param[in] node Any of the sibling nodes to free.
774 */
Michal Vaskof03ed032020-03-04 13:31:44 +0100775void lyd_free_siblings(struct lyd_node *node);
Radek Krejcib0849a22019-07-25 12:31:04 +0200776
777/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200778 * @brief Free (and unlink) the specified data (sub)tree.
779 *
Radek Krejcie7b95092019-05-15 11:03:07 +0200780 * @param[in] node Root of the (sub)tree to be freed.
781 */
782void lyd_free_tree(struct lyd_node *node);
783
784/**
Michal Vasko9f96a052020-03-10 09:41:45 +0100785 * @brief Destroy metadata.
Radek Krejcie7b95092019-05-15 11:03:07 +0200786 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100787 * @param[in] ctx Context where the metadata was created.
788 * @param[in] meta Metadata to destroy
Michal Vasko52927e22020-03-16 17:26:14 +0100789 * @param[in] recursive Zero to destroy only the single metadata (the metadata list is corrected),
Michal Vasko9f96a052020-03-10 09:41:45 +0100790 * non-zero to destroy also all the subsequent metadata in the list.
Radek Krejcie7b95092019-05-15 11:03:07 +0200791 */
Michal Vasko52927e22020-03-16 17:26:14 +0100792void lyd_free_meta(const struct ly_ctx *ctx, struct lyd_meta *meta, int recursive);
793
794/**
795 * @brief Destroy attributes.
796 *
797 * @param[in] ctx Context where the attributes were created.
798 * @param[in] attr Attributes to destroy.
799 * @param[in] recursive Zero to destroy only the single attribute (the attribute list is corrected),
800 * non-zero to destroy also all the subsequent attributes in the list.
801 */
802void ly_free_attr(const struct ly_ctx *ctx, struct ly_attr *attr, int recursive);
Radek Krejcie7b95092019-05-15 11:03:07 +0200803
Radek Krejci084289f2019-07-09 17:35:30 +0200804/**
805 * @brief Check type restrictions applicable to the particular leaf/leaf-list with the given string @p value.
806 *
807 * The given node is not modified in any way - it is just checked if the @p value can be set to the node.
808 *
809 * If there is no data node instance and you are fine with checking just the type's restrictions without the
810 * data tree context (e.g. for the case of require-instance restriction), use lys_value_validate().
811 *
812 * @param[in] ctx libyang context for logging (function does not log errors when @p ctx is NULL)
813 * @param[in] node Data node for the @p value.
814 * @param[in] value String value to be checked.
815 * @param[in] value_len Length of the given @p value (mandatory).
816 * @param[in] get_prefix Callback function to resolve prefixes used in the @p value string.
817 * @param[in] get_prefix_data Private data for the @p get_prefix callback.
818 * @param[in] format Input format of the data.
Michal Vaskof03ed032020-03-04 13:31:44 +0100819 * @param[in] tree Data tree (e.g. when validating RPC/Notification) where the required data instance (leafref target,
820 * instance-identifier) can be placed. NULL in case the data tree is not yet complete,
821 * then LY_EINCOMPLETE can be returned.
Radek Krejci084289f2019-07-09 17:35:30 +0200822 * @return LY_SUCCESS on success
823 * @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).
824 * @return LY_ERR value if an error occurred.
825 */
Michal Vasko44685da2020-03-17 15:38:06 +0100826LY_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 +0100827 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 +0200828
829/**
830 * @brief Compare the node's value with the given string value. The string value is first validated according to the node's type.
831 *
832 * @param[in] node Data node to compare.
833 * @param[in] value String value to be compared. It does not need to be in a canonical form - as part of the process,
834 * it is validated and canonized if possible.
835 * @param[in] value_len Length of the given @p value (mandatory).
836 * @param[in] get_prefix Callback function to resolve prefixes used in the @p value string.
837 * @param[in] get_prefix_data Private data for the @p get_prefix callback.
838 * @param[in] format Input format of the data.
Michal Vaskof03ed032020-03-04 13:31:44 +0100839 * @param[in] tree Data tree (e.g. when validating RPC/Notification) where the required data instance (leafref target,
840 * instance-identifier) can be placed. NULL in case the data tree is not yet complete,
841 * then LY_EINCOMPLETE can be returned.
Radek Krejci084289f2019-07-09 17:35:30 +0200842 * @return LY_SUCCESS on success
843 * @return LY_EINCOMPLETE in case of success when the @p trees is not provided and it was needed to finish the validation of
844 * the given string @p value (e.g. due to require-instance).
845 * @return LY_ERR value if an error occurred.
846 */
847LY_ERR lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len,
Michal Vaskof03ed032020-03-04 13:31:44 +0100848 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 +0200849
Radek Krejci576b23f2019-07-12 14:06:32 +0200850/**
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200851 * @defgroup datacompareoptions Data compare options
852 * @ingroup datatree
853 *
Radek Krejci22ebdba2019-07-25 13:59:43 +0200854 * Various options to change the lyd_compare() behavior.
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200855 */
856#define LYD_COMPARE_FULL_RECURSION 0x01 /* lists and containers are the same only in case all they children
857 (subtree, so direct as well as indirect children) are the same. By default,
858 containers are the same in case of the same schema node and lists are the same
859 in case of equal keys (keyless lists do the full recursion comparison all the time). */
860#define LYD_COMPARE_DEFAULTS 0x02 /* By default, implicit and explicit default nodes are considered to be equal. This flag
861 changes this behavior and implicit (automatically created default node) and explicit
862 (explicitly created node with the default value) default nodes are considered different. */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200863/**@} datacompareoptions */
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200864
865/**
866 * @brief Compare 2 data nodes if they are equivalent.
867 *
868 * @param[in] node1 The first node to compare.
869 * @param[in] node2 The second node to compare.
Radek Krejcic5ad9652019-09-11 11:31:51 +0200870 * @param[in] options Various @ref datacompareoptions.
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200871 * @return LY_SUCCESS if the nodes are equivalent.
872 * @return LY_ENOT if the nodes are not equivalent.
873 */
874LY_ERR lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options);
875
876/**
Radek Krejci22ebdba2019-07-25 13:59:43 +0200877 * @defgroup dupoptions Data duplication options
878 * @ingroup datatree
879 *
880 * Various options to change lyd_dup() behavior.
881 *
882 * Default behavior:
883 * - only the specified node is duplicated without siblings, parents, or children.
884 * - all the attributes of the duplicated nodes are also duplicated.
885 * @{
886 */
887
888#define LYD_DUP_RECURSIVE 0x01 /**< Duplicate not just the node but also all the children. Note that
889 list's keys are always duplicated. */
890#define LYD_DUP_NO_ATTR 0x02 /**< Do not duplicate attributes of any node. */
891#define LYD_DUP_WITH_PARENTS 0x04 /**< If a nested node is being duplicated, duplicate also all the parents.
892 Keys are also duplicated for lists. Return value does not change! */
893#define LYD_DUP_WITH_SIBLINGS 0x08 /**< Duplicate also all the sibling of the given node. */
894#define LYD_DUP_WITH_WHEN 0x10 /**< Also copy any when evaluation state flags. This is useful in case the copied
895 nodes are actually still part of the same datastore meaning no dependency data
896 could have changed. Otherwise nothing is assumed about the copied node when
897 state and it is evaluated from scratch during validation. */
898
899/** @} dupoptions */
900
901/**
902 * @brief Create a copy of the specified data tree \p node. Schema references are kept the same.
903 *
904 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
905 *
906 * @param[in] node Data tree node to be duplicated.
907 * @param[in] parent Optional parent node where to connect the duplicated node(s).
908 * If set in combination with LYD_DUP_WITH_PARENTS, the parents chain is duplicated until it comes to and connect with the @p parent
909 * (if the parents chain does not match at some node the schema node of the provided @p parent, duplication fails).
910 * @param[in] options Bitmask of options flags, see @ref dupoptions.
911 * @return Created copy of the provided data \p node (the first of the duplicated siblings when LYD_DUP_WITH_SIBLINGS used).
912 * Note that in case the parents chain is duplicated for the duplicated node(s) (when LYD_DUP_WITH_PARENTS used), the first duplicated node
913 * is still returned, not a pointer to the duplicated parents.
914 */
915struct lyd_node *lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options);
916
917/**
Radek Krejci576b23f2019-07-12 14:06:32 +0200918 * @brief Resolve instance-identifier defined by lyd_value_path structure.
919 *
920 * @param[in] path Path structure specifying the instance-identifier target.
Michal Vaskof03ed032020-03-04 13:31:44 +0100921 * @param[in] tree Data tree to be searched.
922 * @return Target node of the instance-identifier present in the given data @p tree.
Radek Krejci576b23f2019-07-12 14:06:32 +0200923 */
Michal Vaskof03ed032020-03-04 13:31:44 +0100924const struct lyd_node_term *lyd_target(struct lyd_value_path *path, const struct lyd_node *tree);
Radek Krejci084289f2019-07-09 17:35:30 +0200925
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200926/**
927 * @brief Get string value of a term data \p node.
928 *
929 * @param[in] node Data tree node with the value.
930 * @param[out] dynamic Whether the string value was dynmically allocated.
931 * @return String value of @p node, if @p dynamic, needs to be freed.
932 */
933const char *lyd_value2str(const struct lyd_node_term *node, int *dynamic);
934
935/**
Michal Vasko9f96a052020-03-10 09:41:45 +0100936 * @brief Get string value of a metadata \p meta.
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200937 *
Michal Vasko9f96a052020-03-10 09:41:45 +0100938 * @param[in] meta Metadata with the value.
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200939 * @param[out] dynamic Whether the string value was dynmically allocated.
Michal Vasko9f96a052020-03-10 09:41:45 +0100940 * @return String value of @p meta, if @p dynamic, needs to be freed.
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200941 */
Michal Vasko9f96a052020-03-10 09:41:45 +0100942const char *lyd_meta2str(const struct lyd_meta *meta, int *dynamic);
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200943
944/**
945 * @brief Types of the different data paths.
946 */
947typedef enum {
Michal Vasko14654712020-02-06 08:35:21 +0100948 LYD_PATH_LOG, /**< Descriptive path format used in log messages */
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200949} LYD_PATH_TYPE;
950
951/**
952 * @brief Generate path of the given node in the requested format.
953 *
954 * @param[in] node Schema path of this node will be generated.
955 * @param[in] pathtype Format of the path to generate.
956 * @param[in,out] buffer Prepared buffer of the @p buflen length to store the generated path.
957 * If NULL, memory for the complete path is allocated.
958 * @param[in] buflen Size of the provided @p buffer.
959 * @return NULL in case of memory allocation error, path of the node otherwise.
960 * In case the @p buffer is NULL, the returned string is dynamically allocated and caller is responsible to free it.
961 */
962char *lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen);
963
Michal Vaskoe444f752020-02-10 12:20:06 +0100964/**
Michal Vaskoe444f752020-02-10 12:20:06 +0100965 * @brief Find the node, in the list, satisfying the given restrictions.
966 * Does **not** use hashes - should not be used unless necessary for best performance.
967 *
968 * @param[in] first Starting sibling node for search, only succeeding ones are searched.
969 * @param[in] module Module of the node to find.
970 * @param[in] name Name of the node to find.
Michal Vaskof03ed032020-03-04 13:31:44 +0100971 * @param[in] name_len Optional length of @p name in case it is not 0-terminated string.
Michal Vaskoe444f752020-02-10 12:20:06 +0100972 * @param[in] key_or_value Expected value depends on the type of @p name node:
973 * LYS_CONTAINER:
974 * LYS_ANYXML:
975 * LYS_ANYDATA:
976 * LYS_NOTIF:
977 * LYS_RPC:
978 * LYS_ACTION:
979 * NULL should be always set, will be ignored.
980 * LYS_LEAF:
981 * LYS_LEAFLIST:
Michal Vasko90932a92020-02-12 14:33:03 +0100982 * Optional restriction on the specific leaf(-list) value.
Michal Vaskoe444f752020-02-10 12:20:06 +0100983 * LYS_LIST:
984 * Optional keys values of the matching list instances in the form of "[key1='val1'][key2='val2']...".
Michal Vasko90932a92020-02-12 14:33:03 +0100985 * The keys do not have to be ordered and not all keys need to be specified.
986 *
987 * Note that any explicit values (leaf, leaf-list or list key values) will be canonized first
988 * before comparison. But values that do not have a canonical value are expected to be in the
989 * JSON format!
Michal Vaskof03ed032020-03-04 13:31:44 +0100990 * @param[in] val_len Optional length of @p key_or_value in case it is not 0-terminated string.
Michal Vaskoe444f752020-02-10 12:20:06 +0100991 * @param[out] match Found data node.
992 * @return LY_SUCCESS on success, @p match set.
993 * @return LY_ENOTFOUND if not found, @p match set to NULL.
994 * @return LY_ERR value if another error occurred.
995 */
996LY_ERR lyd_find_sibling_next(const struct lyd_node *first, const struct lys_module *module, const char *name,
997 size_t name_len, const char *key_or_value, size_t val_len, struct lyd_node **match);
998
999/**
1000 * @brief Search in the given siblings (NOT recursively) for the first target instance.
1001 * Uses hashes - should be used whenever possible for best performance.
1002 *
1003 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
1004 * @param[in] target Target node to find.
Michal Vasko9b368d32020-02-14 13:53:31 +01001005 * @param[out] match Can be NULL, otherwise the found data node.
Michal Vaskoe444f752020-02-10 12:20:06 +01001006 * @return LY_SUCCESS on success, @p match set.
1007 * @return LY_ENOTFOUND if not found, @p match set to NULL.
1008 * @return LY_ERR value if another error occurred.
1009 */
1010LY_ERR lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match);
1011
1012/**
1013 * @brief Search in the given siblings for all target instances.
1014 * Uses hashes - should be used whenever possible for best performance.
1015 *
1016 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
1017 * @param[in] target Target node to find. Key-less lists are compared based on
1018 * all its descendants (both direct and indirect).
1019 * @param[out] set Found nodes in a set in case of success.
1020 * @return LY_SUCCESS on success.
1021 * @return LY_ENOTFOUND if no matching siblings found.
1022 * @return LY_ERR value if another error occurred.
1023 */
1024LY_ERR lyd_find_sibling_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set);
1025
1026/**
1027 * @brief Search in the given siblings for the first schema instance.
1028 * Uses hashes - should be used whenever possible for best performance.
1029 *
1030 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
1031 * @param[in] schema Schema node of the data node to find.
1032 * @param[in] key_or_value Expected value depends on the type of \p schema:
1033 * LYS_CONTAINER:
1034 * LYS_LEAF:
1035 * LYS_ANYXML:
1036 * LYS_ANYDATA:
1037 * LYS_NOTIF:
1038 * LYS_RPC:
1039 * LYS_ACTION:
1040 * NULL should be always set, will be ignored.
1041 * LYS_LEAFLIST:
1042 * Searched instance value.
1043 * LYS_LIST:
Michal Vasko90932a92020-02-12 14:33:03 +01001044 * Searched instance key values in the form of "[key1='val1'][key2='val2']...".
1045 * The keys do not have to be ordered but all of them must be set.
1046 *
1047 * Note that any explicit values (leaf-list or list key values) will be canonized first
1048 * before comparison. But values that do not have a canonical value are expected to be in the
1049 * JSON format!
Michal Vaskof03ed032020-03-04 13:31:44 +01001050 * @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 +01001051 * @param[out] match Can be NULL, otherwise the found data node.
Michal Vaskoe444f752020-02-10 12:20:06 +01001052 * @return LY_SUCCESS on success, @p match set.
1053 * @return LY_ENOTFOUND if not found, @p match set to NULL.
1054 * @return LY_EINVAL if @p schema is a key-less list.
1055 * @return LY_ERR value if another error occurred.
1056 */
1057LY_ERR lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value,
1058 size_t val_len, struct lyd_node **match);
1059
Michal Vaskoccc02342020-05-21 10:09:21 +02001060/**
1061 * @brief Search in the given data for instances of nodes matching the provided XPath.
1062 *
Michal Vasko19a30602020-05-25 10:40:19 +02001063 * If a list instance is being selected with all its key values specified (but not necessarily ordered)
1064 * in the form `list[key1='val1'][key2='val2'][key3='val3']` or a leaf-list instance in the form
1065 * `leaf-list[.='val']`, these instances are found using hashes with constant (*O(1)*) complexity
1066 * (unless they are defined in top-level). Other predicates can still follow the aforementioned ones.
1067 *
Michal Vaskoccc02342020-05-21 10:09:21 +02001068 * @param[in] ctx_node XPath context node.
1069 * @param[in] xpath Data XPath expression filtering the matching nodes. ::LYD_JSON format is expected.
1070 * @param[out] set Set of found data nodes. In case the result is a number, a string, or a boolean,
1071 * the returned set is empty.
1072 * @return LY_SUCCESS on success, @p set is returned.
1073 * @return LY_ERR value if an error occurred.
1074 */
1075LY_ERR lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set);
1076
Radek Krejcie7b95092019-05-15 11:03:07 +02001077#ifdef __cplusplus
1078}
1079#endif
1080
1081#endif /* LY_TREE_DATA_H_ */