blob: 1cf0e8f8df65453cae08b012592a38b8f5b057b6 [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 *
51 * Use the same parameters for #LY_TREE_DFS_BEGIN and #LY_TREE_DFS_END. While
52 * 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/**
108 * @brief Data input/output formats supported by libyang [parser](@ref howtodataparsers) and
109 * [printer](@ref howtodataprinters) functions.
110 */
111typedef enum {
112 LYD_UNKNOWN = 0, /**< unknown format, used as return value in case of error */
113 LYD_XML, /**< XML format of the instance data */
Radek Krejcie7b95092019-05-15 11:03:07 +0200114 LYD_JSON, /**< JSON format of the instance data */
Radek Krejci355bf4f2019-07-16 17:14:16 +0200115#if 0
Radek Krejcie7b95092019-05-15 11:03:07 +0200116 LYD_LYB, /**< LYB format of the instance data */
117#endif
118} LYD_FORMAT;
119
120/**
121 * @brief List of possible value types stored in ::lyd_node_anydata.
122 */
123typedef enum {
124 LYD_ANYDATA_CONSTSTRING = 0x00, /**< value is constant string (const char *) which is internally duplicated for
125 storing in the anydata structure; XML sensitive characters (such as & or \>)
126 are automatically escaped when the anydata is printed in XML format. */
127 LYD_ANYDATA_STRING = 0x01, /**< value is dynamically allocated string (char*), so the data are used directly
128 without duplication and caller is supposed to not manipulate with the data
129 after a successful call (including calling free() on the provided data); XML
130 sensitive characters (such as & or \>) are automatically escaped when the
131 anydata is printed in XML format */
132 LYD_ANYDATA_JSON = 0x02, /**< value is string containing the data modeled by YANG and encoded as I-JSON. The
133 string is handled as constant string. In case of using the value as input
134 parameter, the #LYD_ANYDATA_JSOND can be used for dynamically allocated
135 string. */
136 LYD_ANYDATA_JSOND = 0x03, /**< In case of using value as input parameter, this enumeration is supposed to be
137 used for dynamically allocated strings (it is actually combination of
138 #LYD_ANYDATA_JSON and #LYD_ANYDATA_STRING (and it can be also specified as
139 ORed value of the mentioned values. */
140 LYD_ANYDATA_SXML = 0x04, /**< value is string containing the serialized XML data. The string is handled as
141 constant string. In case of using the value as input parameter, the
142 #LYD_ANYDATA_SXMLD can be used for dynamically allocated string. */
143 LYD_ANYDATA_SXMLD = 0x05, /**< In case of using serialized XML value as input parameter, this enumeration is
144 supposed to be used for dynamically allocated strings (it is actually
145 combination of #LYD_ANYDATA_SXML and #LYD_ANYDATA_STRING (and it can be also
146 specified as ORed value of the mentioned values). */
147 LYD_ANYDATA_XML = 0x08, /**< value is struct lyxml_elem*, the structure is directly connected into the
148 anydata node without duplication, caller is supposed to not manipulate with the
149 data after a successful call (including calling lyxml_free() on the provided
150 data) */
151 LYD_ANYDATA_DATATREE = 0x10, /**< value is struct lyd_node* (first sibling), the structure is directly connected
152 into the anydata node without duplication, caller is supposed to not manipulate
153 with the data after a successful call (including calling lyd_free() on the
154 provided data) */
155 LYD_ANYDATA_LYB = 0x20, /**< value is a memory with serialized data tree in LYB format. The data are handled
156 as a constant string. In case of using the value as input parameter,
157 the #LYD_ANYDATA_LYBD can be used for dynamically allocated string. */
158 LYD_ANYDATA_LYBD = 0x21, /**< In case of using LYB value as input parameter, this enumeration is
159 supposed to be used for dynamically allocated strings (it is actually
160 combination of #LYD_ANYDATA_LYB and #LYD_ANYDATA_STRING (and it can be also
161 specified as ORed value of the mentioned values). */
162} LYD_ANYDATA_VALUETYPE;
163
164/** @} */
165
166/**
167 * @brief YANG data representation
168 */
169struct lyd_value {
Radek Krejci74a3e232019-07-16 10:46:05 +0200170 const char *canonized; /**< Canonical string representation of value (for comparison, printing,...), canonized according to the
171 rules implemented in the type's canonization callback (if any). Note that not all the types
172 have a canonical representation, so this value can be even NULL (identityref or instance-identifiers
173 are built-in examples of such a case). The lyd_value::realtype's print callback provides possibility
174 to get correct string representation of the value for the specific data format. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200175 union {
176 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 +0200177 the canonization rules) can change by changing value (e.g. leafref target) somewhere else. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200178 int8_t boolean; /**< 0 as false, 1 as true */
179 int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200180 int8_t int8; /**< 8-bit signed integer */
181 int16_t int16; /**< 16-bit signed integer */
182 int32_t int32; /**< 32-bit signed integer */
183 int64_t int64; /**< 64-bit signed integer */
184 uint8_t uint8; /**< 8-bit unsigned integer */
185 uint16_t uint16; /**< 16-bit signed integer */
186 uint32_t uint32; /**< 32-bit signed integer */
187 uint64_t uint64; /**< 64-bit signed integer */
Radek Krejcie7b95092019-05-15 11:03:07 +0200188 struct lysc_type_bitenum_item *enum_item; /**< pointer to the definition of the enumeration value */
Radek Krejci849a62a2019-05-22 15:29:05 +0200189 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 +0200190 struct lysc_ident *ident; /**< pointer to the schema definition of the identityref value */
Radek Krejciefbb3922019-07-15 12:58:00 +0200191
192 struct lyd_value_subvalue {
193 struct lyd_value_prefix {
194 const char *prefix; /**< prefix string used in the canonized string to identify the mod of the YANG schema */
195 const struct lys_module *mod; /**< YANG schema module identified by the prefix string */
196 } *prefixes; /**< list of mappings between prefix in canonized value to a YANG schema ([sized array](@ref sizedarrays)) */
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200197 struct lyd_value *value; /**< representation of the value according to the selected union's subtype (stored as lyd_value::realpath
198 here, in subvalue structure */
199 } *subvalue; /**< data to represent data with multiple types (union). Original value is stored in the main
200 lyd_value:canonized while the lyd_value_subvalue::value contains representation according to the
201 one of the union's type. The lyd_value_subvalue:prefixes provides (possible) mappings from prefixes
202 in original value to YANG modules. These prefixes are necessary to parse original value to the union's
203 subtypes. */
Radek Krejci084289f2019-07-09 17:35:30 +0200204
205 struct lyd_value_path {
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200206 const struct lysc_node *node; /**< Schema node representing the path segment */
Radek Krejci084289f2019-07-09 17:35:30 +0200207 struct lyd_value_path_predicate {
208 union {
209 struct {
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200210 const struct lysc_node *key; /**< key node of the predicate, in case of the leaf-list-predicate, it is the leaf-list node itself */
211 struct lyd_value *value; /**< value representation according to the key's type */
212 }; /**< key-value pair for leaf-list-predicate and key-predicate (type 1 and 2) */
213 uint64_t position; /**< position value for the position-predicate (type 0) */
Radek Krejci084289f2019-07-09 17:35:30 +0200214 };
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200215 uint8_t type; /**< Predicate types (see YANG ABNF): 0 - position, 1 - key-predicate, 2 - leaf-list-predicate */
216 } *predicates; /**< [Sized array](@ref sizedarrays) of the path segment's predicates */
217 } *target; /**< [Sized array](@ref sizedarrays) of (instance-identifier's) path segments. */
Radek Krejci084289f2019-07-09 17:35:30 +0200218
Radek Krejcie7b95092019-05-15 11:03:07 +0200219 void *ptr; /**< generic data type structure used to store the data */
Radek Krejci8dc4f2d2019-07-16 12:24:00 +0200220 }; /**< The union is just a list of shorthands to possible values stored by a type's plugin. libyang itself uses the lyd_value::realtype
221 plugin's callbacks to work with the data. */
Radek Krejci084289f2019-07-09 17:35:30 +0200222
Radek Krejci62903c32019-07-15 14:42:05 +0200223 struct lysc_type *realtype; /**< pointer to the real type of the data stored in the value structure. This type can differ from the type
224 in the schema node of the data node since the type's store plugin can use other types/plugins for
225 storing data. Speaking about built-in types, this is the case of leafref which stores data as its
226 target type. In contrast, union type also use its subtype's callbacks, but inside an internal data
227 lyd_value::subvalue structure, so here is the pointer to the union type.
228 In general, this type is used to get free callback for this lyd_value structure, so it must reflect
229 the type used to store data directly in the same lyd_value instance. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200230};
231
232/**
233 * @brief Attribute structure.
234 *
235 * The structure provides information about attributes of a data element. Such attributes must map to
236 * annotations as specified in RFC 7952. The only exception is the filter type (in NETCONF get operations)
237 * and edit-config's operation attributes. In XML, they are represented as standard XML attributes. In JSON,
238 * they are represented as JSON elements starting with the '@' character (for more information, see the
239 * YANG metadata RFC.
240 *
241 */
242struct lyd_attr {
243 struct lyd_node *parent; /**< data node where the attribute is placed */
244 struct lyd_attr *next; /**< pointer to the next attribute of the same element */
245 void *annotation; /**< TODO pointer to the attribute/annotation's definition */
246 const char *name; /**< attribute name */
247 struct lyd_value value; /**< attribute's value representation */
248};
249
250
251#define LYD_NODE_INNER (LYS_CONTAINER|LYS_LIST) /**< Schema nodetype mask for lyd_node_inner */
252#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/**
256 * @brief Generic structure for a data node.
257 */
258struct lyd_node {
259 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list) */
260 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
261 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
262 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
263 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
264 never NULL. If there is no sibling node, pointer points to the node
265 itself. In case of the first node, this pointer points to the last
266 node in the list. */
267 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
268
269#ifdef LY_ENABLED_LYD_PRIV
270 void *priv; /**< private user data, not used by libyang */
271#endif
272};
273
274/**
275 * @brief Data node structure for the inner data tree nodes - containers and lists.
276 */
277struct lyd_node_inner {
278 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list) */
279 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
280 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
281 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
282 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
283 never NULL. If there is no sibling node, pointer points to the node
284 itself. In case of the first node, this pointer points to the last
285 node in the list. */
286 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
287
288#ifdef LY_ENABLED_LYD_PRIV
289 void *priv; /**< private user data, not used by libyang */
290#endif
291
292 struct lyd_node *child; /**< pointer to the first child node. */
293 struct hash_table *children_ht; /**< hash table with all the direct children (except keys for a list, lists without keys) */
294};
295
296/**
297 * @brief Data node structure for the terminal data tree nodes - leafs and leaf-lists.
298 */
299struct lyd_node_term {
300 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list) */
301 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
302 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
303 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
304 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
305 never NULL. If there is no sibling node, pointer points to the node
306 itself. In case of the first node, this pointer points to the last
307 node in the list. */
308 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
309
310#ifdef LY_ENABLED_LYD_PRIV
311 void *priv; /**< private user data, not used by libyang */
312#endif
313
314 struct lyd_value value; /**< node's value representation */
315};
316
317/**
318 * @brief Data node structure for the anydata data tree nodes - anydatas and anyxmls.
319 */
320struct lyd_node_any {
321 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list) */
322 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
323 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
324 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
325 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
326 never NULL. If there is no sibling node, pointer points to the node
327 itself. In case of the first node, this pointer points to the last
328 node in the list. */
329 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
330
331#ifdef LY_ENABLED_LYD_PRIV
332 void *priv; /**< private user data, not used by libyang */
333#endif
334
335 /* TODO - anydata representation */
336};
337
338/**
339 * @defgroup dataparseroptions Data parser options
340 * @ingroup datatree
341 *
342 * Various options to change the data tree parsers behavior.
343 *
344 * Default behavior:
345 * - in case of XML, parser reads all data from its input (file, memory, XML tree) including the case of not well-formed
346 * XML document (multiple top-level elements) and if there is an unknown element, it is skipped including its subtree
347 * (see the next point). This can be changed by the #LYD_OPT_NOSIBLINGS option which make parser to read only a single
348 * tree (with a single root element) from its input.
349 * - parser silently ignores the data without a matching node in schema trees. If the caller want to stop
350 * parsing in case of presence of unknown data, the #LYD_OPT_STRICT can be used. The strict mode is useful for
351 * NETCONF servers, since NETCONF clients should always send data according to the capabilities announced by the server.
352 * On the other hand, the default non-strict mode is useful for clients receiving data from NETCONF server since
353 * clients are not required to understand everything the server does. Of course, the optimal strategy for clients is
354 * to use filtering to get only the required data. Having an unknown element of the known namespace is always an error.
355 * The behavior can be changed by #LYD_OPT_STRICT option.
356 * - using obsolete statements (status set to obsolete) just generates a warning, but the processing continues. The
357 * behavior can be changed by #LYD_OPT_OBSOLETE option.
358 * - parser expects that the provided data provides complete datastore content (both the configuration and state data)
359 * and performs data validation according to all YANG rules. This can be a problem in case of representing NETCONF's
360 * subtree filter data, edit-config's data or other type of data set - such data do not represent a complete data set
361 * and some of the validation rules can fail. Therefore there are other options (within lower 8 bits) to make parser
362 * to accept such a data.
363 * - when parser evaluates when-stmt condition to false, a validation error is raised. If the
364 * #LYD_OPT_WHENAUTODEL is used, the invalid node is silently removed instead of an error. The option (and also this default
365 * behavior) takes effect only in case of #LYD_OPT_DATA or #LYD_OPT_CONFIG type of data.
366 * @{
367 */
368
369#define LYD_OPT_DATA 0x00 /**< Default type of data - complete datastore content with configuration as well as
370 state data. To handle possibly missing (but by default required) ietf-yang-library
371 data, use #LYD_OPT_DATA_NO_YANGLIB or #LYD_OPT_DATA_ADD_YANGLIB options. */
372#define LYD_OPT_CONFIG 0x01 /**< A configuration datastore - complete datastore without state data.
373 Validation modifications:
374 - status data are not allowed */
375#define LYD_OPT_GET 0x02 /**< Data content from a NETCONF reply message to the NETCONF \<get\> operation.
376 Validation modifications:
377 - mandatory nodes can be omitted
378 - leafrefs and instance-identifier resolution is allowed to fail
379 - list's keys/unique nodes are not required (so duplication is not checked)
380 - must and when evaluation skipped */
381#define LYD_OPT_GETCONFIG 0x04 /**< Data content from a NETCONF reply message to the NETCONF \<get-config\> operation
382 Validation modifications:
383 - mandatory nodes can be omitted
384 - leafrefs and instance-identifier resolution is allowed to fail
385 - list's keys/unique nodes are not required (so duplication is not checked)
386 - must and when evaluation skipped
387 - status data are not allowed */
388#define LYD_OPT_EDIT 0x08 /**< Content of the NETCONF \<edit-config\>'s config element.
389 Validation modifications:
390 - mandatory nodes can be omitted
391 - leafrefs and instance-identifier resolution is allowed to fail
392 - must and when evaluation skipped
393 - status data are not allowed */
394#define LYD_OPT_RPC 0x10 /**< Data represents RPC or action input parameters. */
395#define LYD_OPT_RPCREPLY 0x20 /**< Data represents RPC or action output parameters (maps to NETCONF <rpc-reply> data). */
396#define LYD_OPT_NOTIF 0x40 /**< Data represents an event notification data. */
397#define LYD_OPT_NOTIF_FILTER 0x80 /**< Data represents a filtered event notification data.
398 Validation modification:
399 - the only requirement is that the data tree matches the schema tree */
400#define LYD_OPT_TYPEMASK 0x10000ff /**< Mask to filter data type options. Always only a single data type option (only
401 single bit from the lower 8 bits) can be set. */
402
403/* 0x100 reserved, used internally */
404#define LYD_OPT_STRICT 0x0200 /**< Instead of silent ignoring data without schema definition, raise an error. */
405#define LYD_OPT_DESTRUCT 0x0400 /**< Free the provided XML tree during parsing the data. With this option, the
406 provided XML tree is affected and all successfully parsed data are freed.
407 This option is applicable only to lyd_parse_xml() function. */
408#define LYD_OPT_OBSOLETE 0x0800 /**< Raise an error when an obsolete statement (status set to obsolete) is used. */
409#define LYD_OPT_NOSIBLINGS 0x1000 /**< Parse only a single XML tree from the input. This option applies only to
410 XML input data. */
411#define LYD_OPT_TRUSTED 0x2000 /**< Data comes from a trusted source and it is not needed to validate them. Data
412 are connected with the schema, but the most validation checks (mandatory nodes,
413 list instance uniqueness, etc.) are not performed. This option does not make
414 sense for lyd_validate() so it is ignored by this function. */
415#define LYD_OPT_WHENAUTODEL 0x4000 /**< Automatically delete subtrees with false when-stmt condition. The flag is
416 applicable only in combination with #LYD_OPT_DATA and #LYD_OPT_CONFIG flags.
417 If used, libyang will not generate a validation error. */
418#define LYD_OPT_NOEXTDEPS 0x8000 /**< Allow external dependencies (external leafrefs, instance-identifiers, must,
419 and when) to not be resolved/satisfied during validation. */
420#define LYD_OPT_DATA_NO_YANGLIB 0x10000 /**< Ignore (possibly) missing ietf-yang-library data. Applicable only with #LYD_OPT_DATA. */
421#define LYD_OPT_DATA_ADD_YANGLIB 0x20000 /**< Add missing ietf-yang-library data into the validated data tree. Applicable
422 only with #LYD_OPT_DATA. If some ietf-yang-library data are present, they are
423 preserved and option is ignored. */
424#define LYD_OPT_VAL_DIFF 0x40000 /**< Flag only for validation, store all the data node changes performed by the validation
425 in a diff structure. */
426#define LYD_OPT_DATA_TEMPLATE 0x1000000 /**< Data represents YANG data template. */
427
428/**@} dataparseroptions */
429
430/**
431 * @brief Get the node's children list if any.
432 *
433 * Decides the node's type and in case it has a children list, returns it.
434 * @param[in] node Node to check.
435 * @return Pointer to the first child node (if any) of the \p node.
436 */
437const struct lyd_node *lyd_node_children(const struct lyd_node *node);
438
439/**
440 * @brief Find the node, in the list, satisfying the given restrictions.
441 *
442 * @param[in] first Starting child node for search.
443 * @param[in] module Module of the node to find (mandatory argument).
444 * @param[in] name Name of the node to find (mandatory argument).
445 * @param[in] name_len Optional length of the @p name argument in case it is not NULL-terminated string.
446 * @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 +0200447 * @param[in] value Optional restriction for lyd_node_term nodes to select node with the specific value. Note that this
448 * search restriction is limited to compare canonical representation of the type. Some of the types have no canonical
449 * representation and 2 different strings can represent the same value (e.g. different prefixes of the same namespace in instance-identifiers).
450 * In this case there is more advanced lyd_value_compare() to check if the values matches.
Radek Krejcie7b95092019-05-15 11:03:07 +0200451 * @param[in] value_len Optional length of the @p value argument in case it is not NULL-terminated string.
452 * @return The sibling node of the @p first (or itself), satisfying the given restrictions.
453 * @return NULL in case there is no node satisfying the restrictions.
454 */
455const struct lyd_node *lyd_search(const struct lyd_node *first, const struct lys_module *module,
456 const char *name, size_t name_len, uint16_t nodetype, const char *value, size_t value_len);
457
458/**
459 * @brief Parse (and validate) data from memory.
460 *
461 * In case of LY_XML format, the data string is parsed completely. It means that when it contains
462 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
463 * returned data node is a root of the first tree with other trees connected via the next pointer.
464 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
465 *
466 * @param[in] ctx Context to connect with the data tree being built here.
467 * @param[in] data Serialized data in the specified format.
468 * @param[in] format Format of the input data to be parsed.
469 * @param[in] options Parser options, see @ref parseroptions. \p format LYD_LYB uses #LYD_OPT_TRUSTED implicitly.
470 * @param[in] ... Variable arguments depend on \p options. If they include:
471 * - #LYD_OPT_DATA:
472 * - #LYD_OPT_CONFIG:
473 * - #LYD_OPT_GET:
474 * - #LYD_OPT_GETCONFIG:
475 * - #LYD_OPT_EDIT:
476 * - no variable arguments expected.
477 * - #LYD_OPT_RPC:
478 * - #LYD_OPT_NOTIF:
479 * - struct lyd_node *data_tree - additional data tree that will be used
480 * when checking any "when" or "must" conditions in the parsed tree that require
481 * some nodes outside their subtree. It must be a list of top-level elements!
482 * - #LYD_OPT_RPCREPLY:
483 * - const struct ::lyd_node *rpc_act - pointer to the whole RPC or (top-level) action operation
484 * data tree (the request) of the reply.
485 * - const struct ::lyd_node *data_tree - additional data tree that will be used
486 * when checking any "when" or "must" conditions in the parsed tree that require
487 * some nodes outside their subtree. It must be a list of top-level elements!
488 * @return Pointer to the built data tree or NULL in case of empty \p data. To free the returned structure,
489 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
490 * #ly_errno contains appropriate error code (see #LY_ERR).
491 */
492struct lyd_node *lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, ...);
493
494/**
495 * @brief Read (and validate) data from the given file descriptor.
496 *
497 * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc.
498 *
499 * In case of LY_XML format, the file content is parsed completely. It means that when it contains
500 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
501 * returned data node is a root of the first tree with other trees connected via the next pointer.
502 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
503 *
504 * @param[in] ctx Context to connect with the data tree being built here.
505 * @param[in] fd The standard file descriptor of the file containing the data tree in the specified format.
506 * @param[in] format Format of the input data to be parsed.
507 * @param[in] options Parser options, see @ref parseroptions. \p format LYD_LYB uses #LYD_OPT_TRUSTED implicitly.
508 * @param[in] ... Variable arguments depend on \p options. If they include:
509 * - #LYD_OPT_DATA:
510 * - #LYD_OPT_CONFIG:
511 * - #LYD_OPT_GET:
512 * - #LYD_OPT_GETCONFIG:
513 * - #LYD_OPT_EDIT:
514 * - no variable arguments expected.
515 * - #LYD_OPT_RPC:
516 * - #LYD_OPT_NOTIF:
517 * - struct lyd_node *data_tree - additional data tree that will be used
518 * when checking any "when" or "must" conditions in the parsed tree that require
519 * some nodes outside their subtree. It must be a list of top-level elements!
520 * - #LYD_OPT_RPCREPLY:
521 * - const struct ::lyd_node *rpc_act - pointer to the whole RPC or action operation data
522 * tree (the request) of the reply.
523 * - const struct ::lyd_node *data_tree - additional data tree that will be used
524 * when checking any "when" or "must" conditions in the parsed tree that require
525 * some nodes outside their subtree. It must be a list of top-level elements!
526 * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure,
527 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
528 * #ly_errno contains appropriate error code (see #LY_ERR).
529 */
530struct lyd_node *lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, ...);
531
532/**
533 * @brief Read (and validate) data from the given file path.
534 *
535 * In case of LY_XML format, the file content is parsed completely. It means that when it contains
536 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
537 * returned data node is a root of the first tree with other trees connected via the next pointer.
538 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
539 *
540 * @param[in] ctx Context to connect with the data tree being built here.
541 * @param[in] path Path to the file containing the data tree in the specified format.
542 * @param[in] format Format of the input data to be parsed.
543 * @param[in] options Parser options, see @ref parseroptions. \p format LYD_LYB uses #LYD_OPT_TRUSTED implicitly.
544 * @param[in] ... Variable arguments depend on \p options. If they include:
545 * - #LYD_OPT_DATA:
546 * - #LYD_OPT_CONFIG:
547 * - #LYD_OPT_GET:
548 * - #LYD_OPT_GETCONFIG:
549 * - #LYD_OPT_EDIT:
550 * - no variable arguments expected.
551 * - #LYD_OPT_RPC:
552 * - #LYD_OPT_NOTIF:
553 * - struct lyd_node *data_tree - additional data tree that will be used
554 * when checking any "when" or "must" conditions in the parsed tree that require
555 * some nodes outside their subtree. It must be a list of top-level elements!
556 * - #LYD_OPT_RPCREPLY:
557 * - const struct ::lyd_node *rpc_act - pointer to the whole RPC or action operation data
558 * tree (the request) of the reply.
559 * - const struct ::lyd_node *data_tree - additional data tree that will be used
560 * when checking any "when" or "must" conditions in the parsed tree that require
561 * some nodes outside their subtree. It must be a list of top-level elements!
562 * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure,
563 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
564 * #ly_errno contains appropriate error code (see #LY_ERR).
565 */
566struct lyd_node *lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options, ...);
567
568/**
569 * @brief Free all the nodes in the data tree.
570 *
571 * @param[in] node Any of the nodes inside the tree.
572 */
573void lyd_free_all(struct lyd_node *node);
574
575/**
576 * @brief Free (and unlink) the specified data (sub)tree.
577 *
578 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
579 *
580 * @param[in] node Root of the (sub)tree to be freed.
581 */
582void lyd_free_tree(struct lyd_node *node);
583
584/**
585 * @brief Unlink the specified data subtree. All referenced namespaces are copied.
586 *
587 * Note, that the node's connection with the schema tree is kept. Therefore, in case of
588 * reconnecting the node to a data tree using lyd_paste() it is necessary to paste it
589 * to the appropriate place in the data tree following the schema.
590 *
591 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
592 *
593 * @param[in] node Data tree node to be unlinked (together with all children).
594 * @return LY_SUCCESS for success
595 * @return LY_E* values in case of error
596 */
597LY_ERR lyd_unlink_tree(struct lyd_node *node);
598
599/**
600 * @brief Destroy data attribute.
601 *
602 * @param[in] ctx Context where the attribute was created.
603 * @param[in] attr Attribute to destroy
604 * @param[in] recursive Zero to destroy only the attribute (the attribute list is corrected),
605 * non-zero to destroy also all the subsequent attributes in the list.
606 */
607void lyd_free_attr(struct ly_ctx *ctx, struct lyd_attr *attr, int recursive);
608
Radek Krejci084289f2019-07-09 17:35:30 +0200609/**
Radek Krejci576b23f2019-07-12 14:06:32 +0200610 * @brief Prepare ([sized array](@ref sizedarrays)) of data trees required by various (mostly validation) functions.
611 *
612 * @param[in] count Number of trees to include (including the mandatory @p tree).
613 * @param[in] tree First (and mandatory) tree to be included into the resulting ([sized array](@ref sizedarrays)).
614 * @return NULL in case of memory allocation failure or invalid argument, prepared ([sized array](@ref sizedarrays)) otherwise.
615 */
616const struct lyd_node **lyd_trees_new(size_t count, const struct lyd_node *tree, ...);
617
618/**
619 * @brief Free the trees ([sized array](@ref sizedarrays)).
620 *
621 * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees.
622 * @param[in] free_data Flag to free also the particular trees in the @p trees ([sized array](@ref sizedarrays)).
623 * If set to zero, only the trees envelope is freed and data are untouched.
624 */
625void lyd_trees_free(const struct lyd_node **trees, int free_data);
626
627/**
Radek Krejci084289f2019-07-09 17:35:30 +0200628 * @brief Check type restrictions applicable to the particular leaf/leaf-list with the given string @p value.
629 *
630 * The given node is not modified in any way - it is just checked if the @p value can be set to the node.
631 *
632 * If there is no data node instance and you are fine with checking just the type's restrictions without the
633 * data tree context (e.g. for the case of require-instance restriction), use lys_value_validate().
634 *
635 * @param[in] ctx libyang context for logging (function does not log errors when @p ctx is NULL)
636 * @param[in] node Data node for the @p value.
637 * @param[in] value String value to be checked.
638 * @param[in] value_len Length of the given @p value (mandatory).
639 * @param[in] get_prefix Callback function to resolve prefixes used in the @p value string.
640 * @param[in] get_prefix_data Private data for the @p get_prefix callback.
641 * @param[in] format Input format of the data.
642 * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees (e.g. when validating RPC/Notification) where the required
643 * 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 +0200644 * then LY_EINCOMPLETE can be returned. To simply prepare this structure, use lyd_trees_new().
Radek Krejci084289f2019-07-09 17:35:30 +0200645 * @return LY_SUCCESS on success
646 * @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).
647 * @return LY_ERR value if an error occurred.
648 */
649LY_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 +0200650 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 +0200651
652/**
653 * @brief Compare the node's value with the given string value. The string value is first validated according to the node's type.
654 *
655 * @param[in] node Data node to compare.
656 * @param[in] value String value to be compared. It does not need to be in a canonical form - as part of the process,
657 * it is validated and canonized if possible.
658 * @param[in] value_len Length of the given @p value (mandatory).
659 * @param[in] get_prefix Callback function to resolve prefixes used in the @p value string.
660 * @param[in] get_prefix_data Private data for the @p get_prefix callback.
661 * @param[in] format Input format of the data.
662 * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees (e.g. when validating RPC/Notification) where the required
663 * 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 +0200664 * then LY_EINCOMPLETE can be returned in case the validation was not completed, but values matches. To simply prepare
665 * this structure, use lyd_trees_new(). To simply prepare this structure, use lyd_trees_new().
Radek Krejci084289f2019-07-09 17:35:30 +0200666 * @return LY_SUCCESS on success
667 * @return LY_EINCOMPLETE in case of success when the @p trees is not provided and it was needed to finish the validation of
668 * the given string @p value (e.g. due to require-instance).
669 * @return LY_ERR value if an error occurred.
670 */
671LY_ERR lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len,
Radek Krejci576b23f2019-07-12 14:06:32 +0200672 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 +0200673
Radek Krejci576b23f2019-07-12 14:06:32 +0200674/**
675 * @brief Resolve instance-identifier defined by lyd_value_path structure.
676 *
677 * @param[in] path Path structure specifying the instance-identifier target.
678 * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees to be searched.
679 * To simply prepare this structure, use lyd_trees_new().
680 * @return Target node of the instance-identifier present in the given data @p trees.
681 */
682const struct lyd_node_term *lyd_target(struct lyd_value_path *path, const struct lyd_node **trees);
Radek Krejci084289f2019-07-09 17:35:30 +0200683
Radek Krejcie7b95092019-05-15 11:03:07 +0200684#ifdef __cplusplus
685}
686#endif
687
688#endif /* LY_TREE_DATA_H_ */