blob: d6babc2f54b6f11d70036017a885155772969fca [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"
22#include "tree.h"
23#include "tree_schema.h"
24
25struct ly_ctx;
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31/**
32 * @defgroup datatree Data Tree
33 * @{
34 *
35 * Data structures and functions to manipulate and access instance data tree.
36 */
37
38/**
39 * @brief Macro to iterate via all elements in a data tree. This is the opening part
40 * to the #LYD_TREE_DFS_END - they always have to be used together.
41 *
42 * The function follows deep-first search algorithm:
43 * <pre>
44 * 1
45 * / \
46 * 2 4
47 * / / \
48 * 3 5 6
49 * </pre>
50 *
Radek Krejci0935f412019-08-20 16:15:18 +020051 * Use the same parameters for #LYD_TREE_DFS_BEGIN and #LYD_TREE_DFS_END. While
Radek Krejcie7b95092019-05-15 11:03:07 +020052 * START can be any of the lyd_node* types, NEXT and ELEM variables are expected
53 * to be pointers to a generic struct lyd_node.
54 *
55 * Since the next node is selected as part of #LYD_TREE_DFS_END, do not use
56 * continue statement between the #LYD_TREE_DFS_BEGIN and #LYD_TREE_DFS_END.
57 *
58 * Use with opening curly bracket '{' after the macro.
59 *
60 * @param START Pointer to the starting element processed first.
61 * @param NEXT Temporary storage, do not use.
62 * @param ELEM Iterator intended for use in the block.
63 */
64#define LYD_TREE_DFS_BEGIN(START, NEXT, ELEM) \
65 for ((ELEM) = (NEXT) = (START); \
66 (ELEM); \
67 (ELEM) = (NEXT))
68
69/**
70 * @brief Macro to iterate via all elements in a tree. This is the closing part
71 * to the #LYD_TREE_DFS_BEGIN - they always have to be used together.
72 *
73 * Use the same parameters for #LYD_TREE_DFS_BEGIN and #LYD_TREE_DFS_END. While
74 * START can be any of the lyd_node* types, NEXT and ELEM variables are expected
75 * to be pointers to a generic struct lyd_node.
76 *
77 * Use with closing curly bracket '}' after the macro.
78 *
79 * @param START Pointer to the starting element processed first.
80 * @param NEXT Temporary storage, do not use.
81 * @param ELEM Iterator intended for use in the block.
82 */
83
84#define LYD_TREE_DFS_END(START, NEXT, ELEM) \
85 /* select element for the next run - children first */ \
86 (NEXT) = (struct lyd_node*)lyd_node_children((struct lyd_node*)ELEM); \
87 if (!(NEXT)) { \
88 /* no children */ \
89 if ((ELEM) == (struct lyd_node*)(START)) { \
90 /* we are done, (START) has no children */ \
91 break; \
92 } \
93 /* try siblings */ \
94 (NEXT) = (ELEM)->next; \
95 } \
96 while (!(NEXT)) { \
97 /* parent is already processed, go to its sibling */ \
98 (ELEM) = (struct lyd_node*)(ELEM)->parent; \
99 /* no siblings, go back through parents */ \
100 if ((ELEM)->parent == (START)->parent) { \
101 /* we are done, no next element to process */ \
102 break; \
103 } \
104 (NEXT) = (ELEM)->next; \
105 }
106
107/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200108 * @brief Macro to get context from a data tree node.
109 */
110#define LYD_NODE_CTX(node) ((node)->schema->module->ctx)
111
112/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200113 * @brief Data input/output formats supported by libyang [parser](@ref howtodataparsers) and
114 * [printer](@ref howtodataprinters) functions.
115 */
116typedef enum {
117 LYD_UNKNOWN = 0, /**< unknown format, used as return value in case of error */
118 LYD_XML, /**< XML format of the instance data */
Radek Krejcie7b95092019-05-15 11:03:07 +0200119 LYD_JSON, /**< JSON format of the instance data */
Radek Krejci355bf4f2019-07-16 17:14:16 +0200120#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200121 LYD_LYB, /**< LYB format of the instance data */
122#endif
123} LYD_FORMAT;
124
125/**
Radek Krejci59583bb2019-09-11 12:57:55 +0200126 * @brief List of possible value types stored in ::lyd_node_any.
Radek Krejcie7b95092019-05-15 11:03:07 +0200127 */
128typedef enum {
Radek Krejci22ebdba2019-07-25 13:59:43 +0200129 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 +0200130 is directly connected into the anydata node without duplication, caller is supposed to not manipulate
131 with the data after a successful call (including calling lyd_free() on the provided data) */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200132 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 +0200133 as string). XML sensitive characters (such as & or \>) are automatically escaped when the anydata
134 is printed in XML format. */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200135 LYD_ANYDATA_XML, /**< Value is a string containing the serialized XML data. */
136 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 +0200137#if 0 /* TODO LYB format */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200138 LYD_ANYDATA_LYB, /**< Value is a memory chunk with the serialized data tree in LYB format. */
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200139#endif
Radek Krejcie7b95092019-05-15 11:03:07 +0200140} LYD_ANYDATA_VALUETYPE;
141
142/** @} */
143
144/**
145 * @brief YANG data representation
146 */
147struct lyd_value {
Radek Krejci74a3e232019-07-16 10:46:05 +0200148 const char *canonized; /**< Canonical string representation of value (for comparison, printing,...), canonized according to the
149 rules implemented in the type's canonization callback (if any). Note that not all the types
150 have a canonical representation, so this value can be even NULL (identityref or instance-identifiers
151 are built-in examples of such a case). The lyd_value::realtype's print callback provides possibility
152 to get correct string representation of the value for the specific data format. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200153 union {
154 const char *string; /**< original, non-canonized string value. Useful for example for unions where the type (and therefore
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200155 the canonization rules) can change by changing value (e.g. leafref target) somewhere else. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200156 int8_t boolean; /**< 0 as false, 1 as true */
157 int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200158 int8_t int8; /**< 8-bit signed integer */
159 int16_t int16; /**< 16-bit signed integer */
160 int32_t int32; /**< 32-bit signed integer */
161 int64_t int64; /**< 64-bit signed integer */
162 uint8_t uint8; /**< 8-bit unsigned integer */
163 uint16_t uint16; /**< 16-bit signed integer */
164 uint32_t uint32; /**< 32-bit signed integer */
165 uint64_t uint64; /**< 64-bit signed integer */
Radek Krejcie7b95092019-05-15 11:03:07 +0200166 struct lysc_type_bitenum_item *enum_item; /**< pointer to the definition of the enumeration value */
Radek Krejci849a62a2019-05-22 15:29:05 +0200167 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 +0200168 struct lysc_ident *ident; /**< pointer to the schema definition of the identityref value */
Radek Krejciefbb3922019-07-15 12:58:00 +0200169
170 struct lyd_value_subvalue {
171 struct lyd_value_prefix {
172 const char *prefix; /**< prefix string used in the canonized string to identify the mod of the YANG schema */
173 const struct lys_module *mod; /**< YANG schema module identified by the prefix string */
174 } *prefixes; /**< list of mappings between prefix in canonized value to a YANG schema ([sized array](@ref sizedarrays)) */
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200175 struct lyd_value *value; /**< representation of the value according to the selected union's subtype (stored as lyd_value::realpath
176 here, in subvalue structure */
177 } *subvalue; /**< data to represent data with multiple types (union). Original value is stored in the main
178 lyd_value:canonized while the lyd_value_subvalue::value contains representation according to the
179 one of the union's type. The lyd_value_subvalue:prefixes provides (possible) mappings from prefixes
180 in original value to YANG modules. These prefixes are necessary to parse original value to the union's
181 subtypes. */
Radek Krejci084289f2019-07-09 17:35:30 +0200182
183 struct lyd_value_path {
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200184 const struct lysc_node *node; /**< Schema node representing the path segment */
Radek Krejci084289f2019-07-09 17:35:30 +0200185 struct lyd_value_path_predicate {
186 union {
187 struct {
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200188 const struct lysc_node *key; /**< key node of the predicate, in case of the leaf-list-predicate, it is the leaf-list node itself */
189 struct lyd_value *value; /**< value representation according to the key's type */
190 }; /**< key-value pair for leaf-list-predicate and key-predicate (type 1 and 2) */
191 uint64_t position; /**< position value for the position-predicate (type 0) */
Radek Krejci084289f2019-07-09 17:35:30 +0200192 };
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200193 uint8_t type; /**< Predicate types (see YANG ABNF): 0 - position, 1 - key-predicate, 2 - leaf-list-predicate */
194 } *predicates; /**< [Sized array](@ref sizedarrays) of the path segment's predicates */
195 } *target; /**< [Sized array](@ref sizedarrays) of (instance-identifier's) path segments. */
Radek Krejci084289f2019-07-09 17:35:30 +0200196
Radek Krejcie7b95092019-05-15 11:03:07 +0200197 void *ptr; /**< generic data type structure used to store the data */
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200198 }; /**< The union is just a list of shorthands to possible values stored by a type's plugin. libyang itself uses the lyd_value::realtype
199 plugin's callbacks to work with the data. */
Radek Krejci084289f2019-07-09 17:35:30 +0200200
Radek Krejci62903c32019-07-15 14:42:05 +0200201 struct lysc_type *realtype; /**< pointer to the real type of the data stored in the value structure. This type can differ from the type
202 in the schema node of the data node since the type's store plugin can use other types/plugins for
203 storing data. Speaking about built-in types, this is the case of leafref which stores data as its
204 target type. In contrast, union type also use its subtype's callbacks, but inside an internal data
205 lyd_value::subvalue structure, so here is the pointer to the union type.
206 In general, this type is used to get free callback for this lyd_value structure, so it must reflect
207 the type used to store data directly in the same lyd_value instance. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200208};
209
210/**
211 * @brief Attribute structure.
212 *
213 * The structure provides information about attributes of a data element. Such attributes must map to
214 * 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 */
220struct lyd_attr {
221 struct lyd_node *parent; /**< data node where the attribute is placed */
222 struct lyd_attr *next; /**< pointer to the next attribute of the same element */
Radek Krejci38d85362019-09-05 16:26:38 +0200223 struct lysc_ext_instance *annotation; /**< pointer to the attribute/annotation's definition */
Radek Krejcie7b95092019-05-15 11:03:07 +0200224 const char *name; /**< attribute name */
Radek Krejci38d85362019-09-05 16:26:38 +0200225 struct lyd_value value; /**< attribute's value representation */
Radek Krejcie7b95092019-05-15 11:03:07 +0200226};
227
228
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200229#define LYD_NODE_INNER (LYS_CONTAINER|LYS_LIST|LYS_ACTION|LYS_NOTIF) /**< Schema nodetype mask for lyd_node_inner */
Radek Krejcie7b95092019-05-15 11:03:07 +0200230#define LYD_NODE_TERM (LYS_LEAF|LYS_LEAFLIST) /**< Schema nodetype mask for lyd_node_term */
231#define LYD_NODE_ANY (LYS_ANYDATA) /**< Schema nodetype mask for lyd_node_any */
232
233/**
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200234 * @defgroup dnodeflags Data nodes flags
235 * @ingroup datatree
236 * @{
237 *
238 * Various flags of data nodes.
239 *
240 * 1 - container 5 - anydata/anyxml
241 * 2 - list 6 - rpc/action
242 * 3 - leaf 7 - notification
243 * 4 - leaflist
244 *
245 * bit name 1 2 3 4 5 6 7
246 * ---------------------+-+-+-+-+-+-+-+
247 * 1 LYD_DEFAULT |x| |x|x| | | |
248 * +-+-+-+-+-+-+-+
249 * 2 | | | | | | | |
250 * ---------------------+-+-+-+-+-+-+-+
251 *
252 */
253
254#define LYD_DEFAULT 0x01 /**< default (implicit) node; */
Michal Vasko03ff5a72019-09-11 13:49:33 +0200255#define LYD_DUMMY 0x80000000 /**< dummy node (in XPath context, internal flag) */
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200256/** @} */
257
258/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200259 * @brief Generic structure for a data node.
260 */
261struct lyd_node {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200262 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
263 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
264 used to get know that nodes are not equal, it cannot be used to decide that the
265 nodes are equal due to possible collisions. */
266 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200267 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
268 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
269 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
270 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
271 never NULL. If there is no sibling node, pointer points to the node
272 itself. In case of the first node, this pointer points to the last
273 node in the list. */
274 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
275
276#ifdef LY_ENABLED_LYD_PRIV
277 void *priv; /**< private user data, not used by libyang */
278#endif
279};
280
281/**
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200282 * @brief Data node structure for the inner data tree nodes - containers, lists, RPCs, actions and Notifications.
Radek Krejcie7b95092019-05-15 11:03:07 +0200283 */
284struct lyd_node_inner {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200285 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
286 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
287 used to get know that nodes are not equal, it cannot be used to decide that the
288 nodes are equal due to possible collisions. */
289 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200290 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
291 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
292 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
293 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
294 never NULL. If there is no sibling node, pointer points to the node
295 itself. In case of the first node, this pointer points to the last
296 node in the list. */
297 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
298
299#ifdef LY_ENABLED_LYD_PRIV
300 void *priv; /**< private user data, not used by libyang */
301#endif
302
303 struct lyd_node *child; /**< pointer to the first child node. */
304 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 +0200305#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 +0200306};
307
308/**
309 * @brief Data node structure for the terminal data tree nodes - leafs and leaf-lists.
310 */
311struct lyd_node_term {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200312 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
313 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
314 used to get know that nodes are not equal, it cannot be used to decide that the
315 nodes are equal due to possible collisions. */
316 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200317 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
318 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
319 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
320 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
321 never NULL. If there is no sibling node, pointer points to the node
322 itself. In case of the first node, this pointer points to the last
323 node in the list. */
324 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
325
326#ifdef LY_ENABLED_LYD_PRIV
327 void *priv; /**< private user data, not used by libyang */
328#endif
329
330 struct lyd_value value; /**< node's value representation */
331};
332
333/**
334 * @brief Data node structure for the anydata data tree nodes - anydatas and anyxmls.
335 */
336struct lyd_node_any {
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200337 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or
338 hashes of all nodes of subtree in case of keyless list). Note that while hash can be
339 used to get know that nodes are not equal, it cannot be used to decide that the
340 nodes are equal due to possible collisions. */
341 uint32_t flags; /**< [data node flags](@ref dnodeflags) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200342 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
343 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
344 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
345 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
346 never NULL. If there is no sibling node, pointer points to the node
347 itself. In case of the first node, this pointer points to the last
348 node in the list. */
349 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
350
351#ifdef LY_ENABLED_LYD_PRIV
352 void *priv; /**< private user data, not used by libyang */
353#endif
354
Radek Krejciee4cab22019-07-17 17:07:47 +0200355 union {
356 struct lyd_node *tree; /**< data tree */
357 const char *str; /**< Generic string data */
358 const char *xml; /**< Serialized XML data */
359 const char *json; /**< I-JSON encoded string */
360 char *mem; /**< LYD_ANYDATA_LYB memory chunk */
361 } value; /**< pointer to the stored value representation of the anydata/anyxml node */
362 LYD_ANYDATA_VALUETYPE value_type;/**< type of the data stored as lyd_node_any::value */
Radek Krejcie7b95092019-05-15 11:03:07 +0200363};
364
365/**
366 * @defgroup dataparseroptions Data parser options
367 * @ingroup datatree
368 *
369 * Various options to change the data tree parsers behavior.
370 *
371 * Default behavior:
372 * - in case of XML, parser reads all data from its input (file, memory, XML tree) including the case of not well-formed
373 * XML document (multiple top-level elements) and if there is an unknown element, it is skipped including its subtree
374 * (see the next point). This can be changed by the #LYD_OPT_NOSIBLINGS option which make parser to read only a single
375 * tree (with a single root element) from its input.
376 * - parser silently ignores the data without a matching node in schema trees. If the caller want to stop
377 * parsing in case of presence of unknown data, the #LYD_OPT_STRICT can be used. The strict mode is useful for
378 * NETCONF servers, since NETCONF clients should always send data according to the capabilities announced by the server.
379 * On the other hand, the default non-strict mode is useful for clients receiving data from NETCONF server since
380 * clients are not required to understand everything the server does. Of course, the optimal strategy for clients is
381 * to use filtering to get only the required data. Having an unknown element of the known namespace is always an error.
382 * The behavior can be changed by #LYD_OPT_STRICT option.
383 * - using obsolete statements (status set to obsolete) just generates a warning, but the processing continues. The
384 * behavior can be changed by #LYD_OPT_OBSOLETE option.
385 * - parser expects that the provided data provides complete datastore content (both the configuration and state data)
386 * and performs data validation according to all YANG rules. This can be a problem in case of representing NETCONF's
387 * subtree filter data, edit-config's data or other type of data set - such data do not represent a complete data set
388 * and some of the validation rules can fail. Therefore there are other options (within lower 8 bits) to make parser
389 * to accept such a data.
390 * - when parser evaluates when-stmt condition to false, a validation error is raised. If the
391 * #LYD_OPT_WHENAUTODEL is used, the invalid node is silently removed instead of an error. The option (and also this default
392 * behavior) takes effect only in case of #LYD_OPT_DATA or #LYD_OPT_CONFIG type of data.
393 * @{
394 */
395
396#define LYD_OPT_DATA 0x00 /**< Default type of data - complete datastore content with configuration as well as
397 state data. To handle possibly missing (but by default required) ietf-yang-library
398 data, use #LYD_OPT_DATA_NO_YANGLIB or #LYD_OPT_DATA_ADD_YANGLIB options. */
399#define LYD_OPT_CONFIG 0x01 /**< A configuration datastore - complete datastore without state data.
400 Validation modifications:
401 - status data are not allowed */
402#define LYD_OPT_GET 0x02 /**< Data content from a NETCONF reply message to the NETCONF \<get\> operation.
403 Validation modifications:
404 - mandatory nodes can be omitted
405 - leafrefs and instance-identifier resolution is allowed to fail
406 - list's keys/unique nodes are not required (so duplication is not checked)
407 - must and when evaluation skipped */
408#define LYD_OPT_GETCONFIG 0x04 /**< Data content from a NETCONF reply message to the NETCONF \<get-config\> operation
409 Validation modifications:
410 - mandatory nodes can be omitted
411 - leafrefs and instance-identifier resolution is allowed to fail
412 - list's keys/unique nodes are not required (so duplication is not checked)
413 - must and when evaluation skipped
414 - status data are not allowed */
415#define LYD_OPT_EDIT 0x08 /**< Content of the NETCONF \<edit-config\>'s config element.
416 Validation modifications:
417 - mandatory nodes can be omitted
418 - leafrefs and instance-identifier resolution is allowed to fail
419 - must and when evaluation skipped
420 - status data are not allowed */
421#define LYD_OPT_RPC 0x10 /**< Data represents RPC or action input parameters. */
422#define LYD_OPT_RPCREPLY 0x20 /**< Data represents RPC or action output parameters (maps to NETCONF <rpc-reply> data). */
423#define LYD_OPT_NOTIF 0x40 /**< Data represents an event notification data. */
424#define LYD_OPT_NOTIF_FILTER 0x80 /**< Data represents a filtered event notification data.
425 Validation modification:
426 - the only requirement is that the data tree matches the schema tree */
427#define LYD_OPT_TYPEMASK 0x10000ff /**< Mask to filter data type options. Always only a single data type option (only
428 single bit from the lower 8 bits) can be set. */
429
430/* 0x100 reserved, used internally */
431#define LYD_OPT_STRICT 0x0200 /**< Instead of silent ignoring data without schema definition, raise an error. */
432#define LYD_OPT_DESTRUCT 0x0400 /**< Free the provided XML tree during parsing the data. With this option, the
433 provided XML tree is affected and all successfully parsed data are freed.
434 This option is applicable only to lyd_parse_xml() function. */
435#define LYD_OPT_OBSOLETE 0x0800 /**< Raise an error when an obsolete statement (status set to obsolete) is used. */
436#define LYD_OPT_NOSIBLINGS 0x1000 /**< Parse only a single XML tree from the input. This option applies only to
437 XML input data. */
438#define LYD_OPT_TRUSTED 0x2000 /**< Data comes from a trusted source and it is not needed to validate them. Data
439 are connected with the schema, but the most validation checks (mandatory nodes,
440 list instance uniqueness, etc.) are not performed. This option does not make
441 sense for lyd_validate() so it is ignored by this function. */
442#define LYD_OPT_WHENAUTODEL 0x4000 /**< Automatically delete subtrees with false when-stmt condition. The flag is
443 applicable only in combination with #LYD_OPT_DATA and #LYD_OPT_CONFIG flags.
444 If used, libyang will not generate a validation error. */
445#define LYD_OPT_NOEXTDEPS 0x8000 /**< Allow external dependencies (external leafrefs, instance-identifiers, must,
446 and when) to not be resolved/satisfied during validation. */
447#define LYD_OPT_DATA_NO_YANGLIB 0x10000 /**< Ignore (possibly) missing ietf-yang-library data. Applicable only with #LYD_OPT_DATA. */
448#define LYD_OPT_DATA_ADD_YANGLIB 0x20000 /**< Add missing ietf-yang-library data into the validated data tree. Applicable
449 only with #LYD_OPT_DATA. If some ietf-yang-library data are present, they are
450 preserved and option is ignored. */
451#define LYD_OPT_VAL_DIFF 0x40000 /**< Flag only for validation, store all the data node changes performed by the validation
452 in a diff structure. */
453#define LYD_OPT_DATA_TEMPLATE 0x1000000 /**< Data represents YANG data template. */
454
455/**@} dataparseroptions */
456
457/**
458 * @brief Get the node's children list if any.
459 *
460 * Decides the node's type and in case it has a children list, returns it.
461 * @param[in] node Node to check.
462 * @return Pointer to the first child node (if any) of the \p node.
463 */
464const struct lyd_node *lyd_node_children(const struct lyd_node *node);
465
466/**
467 * @brief Find the node, in the list, satisfying the given restrictions.
468 *
469 * @param[in] first Starting child node for search.
470 * @param[in] module Module of the node to find (mandatory argument).
471 * @param[in] name Name of the node to find (mandatory argument).
472 * @param[in] name_len Optional length of the @p name argument in case it is not NULL-terminated string.
473 * @param[in] nodetype Optional mask for the nodetype of the node to find, 0 is understood as all nodetypes.
Radek Krejci084289f2019-07-09 17:35:30 +0200474 * @param[in] value Optional restriction for lyd_node_term nodes to select node with the specific value. Note that this
475 * search restriction is limited to compare canonical representation of the type. Some of the types have no canonical
476 * representation and 2 different strings can represent the same value (e.g. different prefixes of the same namespace in instance-identifiers).
477 * In this case there is more advanced lyd_value_compare() to check if the values matches.
Radek Krejcie7b95092019-05-15 11:03:07 +0200478 * @param[in] value_len Optional length of the @p value argument in case it is not NULL-terminated string.
479 * @return The sibling node of the @p first (or itself), satisfying the given restrictions.
480 * @return NULL in case there is no node satisfying the restrictions.
481 */
482const struct lyd_node *lyd_search(const struct lyd_node *first, const struct lys_module *module,
483 const char *name, size_t name_len, uint16_t nodetype, const char *value, size_t value_len);
484
485/**
486 * @brief Parse (and validate) data from memory.
487 *
488 * In case of LY_XML format, the data string is parsed completely. It means that when it contains
489 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
490 * returned data node is a root of the first tree with other trees connected via the next pointer.
491 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
492 *
493 * @param[in] ctx Context to connect with the data tree being built here.
494 * @param[in] data Serialized data in the specified format.
495 * @param[in] format Format of the input data to be parsed.
496 * @param[in] options Parser options, see @ref parseroptions. \p format LYD_LYB uses #LYD_OPT_TRUSTED implicitly.
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200497 * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees (e.g. when validating RPC/Notification) where the required
498 * data instances (leafref target, instance-identifier, when, must) can be placed. To simply prepare this structure,
499 * use lyd_trees_new(). In case of parsing RPC/action reply (LYD_OPT_RPCREPLY), the first tree in the array MUST be
500 * complete RPC/action data tree (the source request) for the reply.
Radek Krejcie7b95092019-05-15 11:03:07 +0200501 * @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 +0200502 * use lyd_free_all().
503 * @return NULL in case of error. The error information can be then obtained using ly_err* functions.
Radek Krejcie7b95092019-05-15 11:03:07 +0200504 */
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200505struct lyd_node *lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, const struct lyd_node **trees);
Radek Krejcie7b95092019-05-15 11:03:07 +0200506
507/**
508 * @brief Read (and validate) data from the given file descriptor.
509 *
510 * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc.
511 *
512 * In case of LY_XML format, the file content is parsed completely. It means that when it contains
513 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
514 * returned data node is a root of the first tree with other trees connected via the next pointer.
515 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
516 *
517 * @param[in] ctx Context to connect with the data tree being built here.
518 * @param[in] fd The standard file descriptor of the file containing the data tree in the specified format.
519 * @param[in] format Format of the input data to be parsed.
520 * @param[in] options Parser options, see @ref parseroptions. \p format LYD_LYB uses #LYD_OPT_TRUSTED implicitly.
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200521 * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees (e.g. when validating RPC/Notification) where the required
522 * data instances (leafref target, instance-identifier, when, must) can be placed. To simply prepare this structure,
523 * use lyd_trees_new(). In case of parsing RPC/action reply (LYD_OPT_RPCREPLY), the first tree in the array MUST be
524 * complete RPC/action data tree (the source request) for the reply.
Radek Krejcie7b95092019-05-15 11:03:07 +0200525 * @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 +0200526 * use lyd_free_all().
527 * @return NULL in case of error. The error information can be then obtained using ly_err* functions.
Radek Krejcie7b95092019-05-15 11:03:07 +0200528 */
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200529struct lyd_node *lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, const struct lyd_node **trees);
Radek Krejcie7b95092019-05-15 11:03:07 +0200530
531/**
532 * @brief Read (and validate) data from the given file path.
533 *
534 * In case of LY_XML format, the file content is parsed completely. It means that when it contains
535 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
536 * returned data node is a root of the first tree with other trees connected via the next pointer.
537 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
538 *
539 * @param[in] ctx Context to connect with the data tree being built here.
540 * @param[in] path Path to the file containing the data tree in the specified format.
541 * @param[in] format Format of the input data to be parsed.
542 * @param[in] options Parser options, see @ref parseroptions. \p format LYD_LYB uses #LYD_OPT_TRUSTED implicitly.
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200543 * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees (e.g. when validating RPC/Notification) where the required
544 * data instances (leafref target, instance-identifier, when, must) can be placed. To simply prepare this structure,
545 * use lyd_trees_new(). In case of parsing RPC/action reply (LYD_OPT_RPCREPLY), the first tree in the array MUST be
546 * complete RPC/action data tree (the source request) for the reply.
Radek Krejcie7b95092019-05-15 11:03:07 +0200547 * @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 +0200548 * use lyd_free_all().
549 * @return NULL in case of error. The error information can be then obtained using ly_err* functions.
Radek Krejcie7b95092019-05-15 11:03:07 +0200550 */
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200551struct lyd_node *lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options, const struct lyd_node **trees);
Radek Krejcie7b95092019-05-15 11:03:07 +0200552
553/**
Radek Krejcib0849a22019-07-25 12:31:04 +0200554 * @brief Free all the nodes (even parents of the node) in the data tree.
Radek Krejcie7b95092019-05-15 11:03:07 +0200555 *
556 * @param[in] node Any of the nodes inside the tree.
557 */
558void lyd_free_all(struct lyd_node *node);
559
560/**
Radek Krejcib0849a22019-07-25 12:31:04 +0200561 * @brief Free all the sibling nodes.
562 *
563 * @param[in] node Any of the sibling nodes to free.
564 */
565void lyd_free_withsiblings(struct lyd_node *node);
566
567/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200568 * @brief Free (and unlink) the specified data (sub)tree.
569 *
570 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
571 *
572 * @param[in] node Root of the (sub)tree to be freed.
573 */
574void lyd_free_tree(struct lyd_node *node);
575
576/**
577 * @brief Unlink the specified data subtree. All referenced namespaces are copied.
578 *
579 * Note, that the node's connection with the schema tree is kept. Therefore, in case of
580 * reconnecting the node to a data tree using lyd_paste() it is necessary to paste it
581 * to the appropriate place in the data tree following the schema.
582 *
583 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
584 *
585 * @param[in] node Data tree node to be unlinked (together with all children).
586 * @return LY_SUCCESS for success
587 * @return LY_E* values in case of error
588 */
589LY_ERR lyd_unlink_tree(struct lyd_node *node);
590
591/**
592 * @brief Destroy data attribute.
593 *
594 * @param[in] ctx Context where the attribute was created.
595 * @param[in] attr Attribute to destroy
596 * @param[in] recursive Zero to destroy only the attribute (the attribute list is corrected),
597 * non-zero to destroy also all the subsequent attributes in the list.
598 */
599void lyd_free_attr(struct ly_ctx *ctx, struct lyd_attr *attr, int recursive);
600
Radek Krejci084289f2019-07-09 17:35:30 +0200601/**
Radek Krejci576b23f2019-07-12 14:06:32 +0200602 * @brief Prepare ([sized array](@ref sizedarrays)) of data trees required by various (mostly validation) functions.
603 *
604 * @param[in] count Number of trees to include (including the mandatory @p tree).
605 * @param[in] tree First (and mandatory) tree to be included into the resulting ([sized array](@ref sizedarrays)).
606 * @return NULL in case of memory allocation failure or invalid argument, prepared ([sized array](@ref sizedarrays)) otherwise.
607 */
608const struct lyd_node **lyd_trees_new(size_t count, const struct lyd_node *tree, ...);
609
610/**
Radek Krejcif3b6fec2019-07-24 15:53:11 +0200611 * @brief Add tree into the ([sized array](@ref sizedarrays)) of data trees created by lyd_trees_new(),
612 *
613 * @param[in] trees Existing [sized array](@ref sizedarrays)) of data trees to be extended.
614 * @param[in] tree Data tree to be included into the provided @p trees ([sized array](@ref sizedarrays)).
615 * @return NULL in case of memory allocation failure or invalid argument, extended @p trees ([sized array](@ref sizedarrays)) otherwise.
616 */
617const struct lyd_node **lyd_trees_add(const struct lyd_node **trees, const struct lyd_node *tree);
618
619/**
Radek Krejci576b23f2019-07-12 14:06:32 +0200620 * @brief Free the trees ([sized array](@ref sizedarrays)).
621 *
622 * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees.
623 * @param[in] free_data Flag to free also the particular trees in the @p trees ([sized array](@ref sizedarrays)).
624 * If set to zero, only the trees envelope is freed and data are untouched.
625 */
626void lyd_trees_free(const struct lyd_node **trees, int free_data);
627
628/**
Radek Krejci084289f2019-07-09 17:35:30 +0200629 * @brief Check type restrictions applicable to the particular leaf/leaf-list with the given string @p value.
630 *
631 * The given node is not modified in any way - it is just checked if the @p value can be set to the node.
632 *
633 * If there is no data node instance and you are fine with checking just the type's restrictions without the
634 * data tree context (e.g. for the case of require-instance restriction), use lys_value_validate().
635 *
636 * @param[in] ctx libyang context for logging (function does not log errors when @p ctx is NULL)
637 * @param[in] node Data node for the @p value.
638 * @param[in] value String value to be checked.
639 * @param[in] value_len Length of the given @p value (mandatory).
640 * @param[in] get_prefix Callback function to resolve prefixes used in the @p value string.
641 * @param[in] get_prefix_data Private data for the @p get_prefix callback.
642 * @param[in] format Input format of the data.
643 * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees (e.g. when validating RPC/Notification) where the required
644 * data instance (leafref target, instance-identifier) can be placed. NULL in case the data tree are not yet complete,
Radek Krejci576b23f2019-07-12 14:06:32 +0200645 * then LY_EINCOMPLETE can be returned. To simply prepare this structure, use lyd_trees_new().
Radek Krejci084289f2019-07-09 17:35:30 +0200646 * @return LY_SUCCESS on success
647 * @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).
648 * @return LY_ERR value if an error occurred.
649 */
650LY_ERR lyd_value_validate(struct ly_ctx *ctx, const struct lyd_node_term *node, const char *value, size_t value_len,
Radek Krejci576b23f2019-07-12 14:06:32 +0200651 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format, const struct lyd_node **trees);
Radek Krejci084289f2019-07-09 17:35:30 +0200652
653/**
654 * @brief Compare the node's value with the given string value. The string value is first validated according to the node's type.
655 *
656 * @param[in] node Data node to compare.
657 * @param[in] value String value to be compared. It does not need to be in a canonical form - as part of the process,
658 * it is validated and canonized if possible.
659 * @param[in] value_len Length of the given @p value (mandatory).
660 * @param[in] get_prefix Callback function to resolve prefixes used in the @p value string.
661 * @param[in] get_prefix_data Private data for the @p get_prefix callback.
662 * @param[in] format Input format of the data.
663 * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees (e.g. when validating RPC/Notification) where the required
664 * data instance (leafref target, instance-identifier) can be placed. NULL in case the data tree are not yet complete,
Radek Krejci576b23f2019-07-12 14:06:32 +0200665 * then LY_EINCOMPLETE can be returned in case the validation was not completed, but values matches. To simply prepare
666 * this structure, use lyd_trees_new(). To simply prepare this structure, use lyd_trees_new().
Radek Krejci084289f2019-07-09 17:35:30 +0200667 * @return LY_SUCCESS on success
668 * @return LY_EINCOMPLETE in case of success when the @p trees is not provided and it was needed to finish the validation of
669 * the given string @p value (e.g. due to require-instance).
670 * @return LY_ERR value if an error occurred.
671 */
672LY_ERR lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len,
Radek Krejci576b23f2019-07-12 14:06:32 +0200673 ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format, const struct lyd_node **trees);
Radek Krejci084289f2019-07-09 17:35:30 +0200674
Radek Krejci576b23f2019-07-12 14:06:32 +0200675/**
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200676 * @defgroup datacompareoptions Data compare options
677 * @ingroup datatree
678 *
Radek Krejci22ebdba2019-07-25 13:59:43 +0200679 * Various options to change the lyd_compare() behavior.
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200680 */
681#define LYD_COMPARE_FULL_RECURSION 0x01 /* lists and containers are the same only in case all they children
682 (subtree, so direct as well as indirect children) are the same. By default,
683 containers are the same in case of the same schema node and lists are the same
684 in case of equal keys (keyless lists do the full recursion comparison all the time). */
685#define LYD_COMPARE_DEFAULTS 0x02 /* By default, implicit and explicit default nodes are considered to be equal. This flag
686 changes this behavior and implicit (automatically created default node) and explicit
687 (explicitly created node with the default value) default nodes are considered different. */
Radek Krejci22ebdba2019-07-25 13:59:43 +0200688/**@} datacompareoptions */
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200689
690/**
691 * @brief Compare 2 data nodes if they are equivalent.
692 *
693 * @param[in] node1 The first node to compare.
694 * @param[in] node2 The second node to compare.
Radek Krejcic5ad9652019-09-11 11:31:51 +0200695 * @param[in] options Various @ref datacompareoptions.
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200696 * @return LY_SUCCESS if the nodes are equivalent.
697 * @return LY_ENOT if the nodes are not equivalent.
698 */
699LY_ERR lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options);
700
701/**
Radek Krejci22ebdba2019-07-25 13:59:43 +0200702 * @defgroup dupoptions Data duplication options
703 * @ingroup datatree
704 *
705 * Various options to change lyd_dup() behavior.
706 *
707 * Default behavior:
708 * - only the specified node is duplicated without siblings, parents, or children.
709 * - all the attributes of the duplicated nodes are also duplicated.
710 * @{
711 */
712
713#define LYD_DUP_RECURSIVE 0x01 /**< Duplicate not just the node but also all the children. Note that
714 list's keys are always duplicated. */
715#define LYD_DUP_NO_ATTR 0x02 /**< Do not duplicate attributes of any node. */
716#define LYD_DUP_WITH_PARENTS 0x04 /**< If a nested node is being duplicated, duplicate also all the parents.
717 Keys are also duplicated for lists. Return value does not change! */
718#define LYD_DUP_WITH_SIBLINGS 0x08 /**< Duplicate also all the sibling of the given node. */
719#define LYD_DUP_WITH_WHEN 0x10 /**< Also copy any when evaluation state flags. This is useful in case the copied
720 nodes are actually still part of the same datastore meaning no dependency data
721 could have changed. Otherwise nothing is assumed about the copied node when
722 state and it is evaluated from scratch during validation. */
723
724/** @} dupoptions */
725
726/**
727 * @brief Create a copy of the specified data tree \p node. Schema references are kept the same.
728 *
729 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
730 *
731 * @param[in] node Data tree node to be duplicated.
732 * @param[in] parent Optional parent node where to connect the duplicated node(s).
733 * If set in combination with LYD_DUP_WITH_PARENTS, the parents chain is duplicated until it comes to and connect with the @p parent
734 * (if the parents chain does not match at some node the schema node of the provided @p parent, duplication fails).
735 * @param[in] options Bitmask of options flags, see @ref dupoptions.
736 * @return Created copy of the provided data \p node (the first of the duplicated siblings when LYD_DUP_WITH_SIBLINGS used).
737 * Note that in case the parents chain is duplicated for the duplicated node(s) (when LYD_DUP_WITH_PARENTS used), the first duplicated node
738 * is still returned, not a pointer to the duplicated parents.
739 */
740struct lyd_node *lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options);
741
742/**
Radek Krejci576b23f2019-07-12 14:06:32 +0200743 * @brief Resolve instance-identifier defined by lyd_value_path structure.
744 *
745 * @param[in] path Path structure specifying the instance-identifier target.
746 * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees to be searched.
747 * To simply prepare this structure, use lyd_trees_new().
748 * @return Target node of the instance-identifier present in the given data @p trees.
749 */
750const struct lyd_node_term *lyd_target(struct lyd_value_path *path, const struct lyd_node **trees);
Radek Krejci084289f2019-07-09 17:35:30 +0200751
Michal Vasko5ec7cda2019-09-11 13:43:08 +0200752/**
753 * @brief Get string value of a term data \p node.
754 *
755 * @param[in] node Data tree node with the value.
756 * @param[out] dynamic Whether the string value was dynmically allocated.
757 * @return String value of @p node, if @p dynamic, needs to be freed.
758 */
759const char *lyd_value2str(const struct lyd_node_term *node, int *dynamic);
760
761/**
762 * @brief Get string value of an attribute \p attr.
763 *
764 * @param[in] attr Attribute with the value.
765 * @param[out] dynamic Whether the string value was dynmically allocated.
766 * @return String value of @p attr, if @p dynamic, needs to be freed.
767 */
768const char *lyd_attr2str(const struct lyd_attr *attr, int *dynamic);
769
770/**
771 * @brief Types of the different data paths.
772 */
773typedef enum {
774 LYD_PATH_LOG /**< Descriptive path format used in log messages */
775} LYD_PATH_TYPE;
776
777/**
778 * @brief Generate path of the given node in the requested format.
779 *
780 * @param[in] node Schema path of this node will be generated.
781 * @param[in] pathtype Format of the path to generate.
782 * @param[in,out] buffer Prepared buffer of the @p buflen length to store the generated path.
783 * If NULL, memory for the complete path is allocated.
784 * @param[in] buflen Size of the provided @p buffer.
785 * @return NULL in case of memory allocation error, path of the node otherwise.
786 * In case the @p buffer is NULL, the returned string is dynamically allocated and caller is responsible to free it.
787 */
788char *lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen);
789
Radek Krejcie7b95092019-05-15 11:03:07 +0200790#ifdef __cplusplus
791}
792#endif
793
794#endif /* LY_TREE_DATA_H_ */