blob: 96e0e818f3986c94a34a79802b2b2f2e51bebbf5 [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 */
114#if 0
115 LYD_JSON, /**< JSON format of the instance data */
116 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 {
170 const char *canonized; /**< string representation of value (for comparison, printing,...), canonized according to the
171 rules implemented in the type's canonization callback (if any). */
172 union {
173 const char *string; /**< original, non-canonized string value. Useful for example for unions where the type (and therefore
174 the cannonization rules) can change by changing value (e.g. leafref target) somewhere else. */
175 int8_t boolean; /**< 0 as false, 1 as true */
176 int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */
177 struct lysc_type_bitenum_item *enum_item; /**< pointer to the definition of the enumeration value */
Radek Krejci849a62a2019-05-22 15:29:05 +0200178 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 +0200179 struct lysc_ident *ident; /**< pointer to the schema definition of the identityref value */
Radek Krejciefbb3922019-07-15 12:58:00 +0200180
181 struct lyd_value_subvalue {
182 struct lyd_value_prefix {
183 const char *prefix; /**< prefix string used in the canonized string to identify the mod of the YANG schema */
184 const struct lys_module *mod; /**< YANG schema module identified by the prefix string */
185 } *prefixes; /**< list of mappings between prefix in canonized value to a YANG schema ([sized array](@ref sizedarrays)) */
Radek Krejciefbb3922019-07-15 12:58:00 +0200186 struct lyd_value *value;
187 } *subvalue;
Radek Krejci084289f2019-07-09 17:35:30 +0200188
189 struct lyd_value_path {
190 const struct lysc_node *node;
191 struct lyd_value_path_predicate {
192 union {
193 struct {
194 const struct lysc_node *key;
195 struct lyd_value *value;
196 };
197 uint64_t position;
198 };
199 uint8_t type; /**< 0 - position, 1 - key-predicate, 2 - leaf-list-predicate */
200 } *predicates;
201 } *target;
202
Radek Krejcie7b95092019-05-15 11:03:07 +0200203 int8_t int8; /**< 8-bit signed integer */
204 int16_t int16; /**< 16-bit signed integer */
205 int32_t int32; /**< 32-bit signed integer */
206 int64_t int64; /**< 64-bit signed integer */
207 uint8_t uint8; /**< 8-bit unsigned integer */
208 uint16_t uint16; /**< 16-bit signed integer */
209 uint32_t uint32; /**< 32-bit signed integer */
210 uint64_t uint64; /**< 64-bit signed integer */
211 void *ptr; /**< generic data type structure used to store the data */
212 }; /**< The union is just a list of shorthands to possible values stored by a type's plugin. libyang works only with the canonized string,
213 this specific data type storage is just to simplify use of the values by the libyang users. */
Radek Krejci084289f2019-07-09 17:35:30 +0200214
Radek Krejci62903c32019-07-15 14:42:05 +0200215 struct lysc_type *realtype; /**< pointer to the real type of the data stored in the value structure. This type can differ from the type
216 in the schema node of the data node since the type's store plugin can use other types/plugins for
217 storing data. Speaking about built-in types, this is the case of leafref which stores data as its
218 target type. In contrast, union type also use its subtype's callbacks, but inside an internal data
219 lyd_value::subvalue structure, so here is the pointer to the union type.
220 In general, this type is used to get free callback for this lyd_value structure, so it must reflect
221 the type used to store data directly in the same lyd_value instance. */
Radek Krejcie7b95092019-05-15 11:03:07 +0200222};
223
224/**
225 * @brief Attribute structure.
226 *
227 * The structure provides information about attributes of a data element. Such attributes must map to
228 * annotations as specified in RFC 7952. The only exception is the filter type (in NETCONF get operations)
229 * and edit-config's operation attributes. In XML, they are represented as standard XML attributes. In JSON,
230 * they are represented as JSON elements starting with the '@' character (for more information, see the
231 * YANG metadata RFC.
232 *
233 */
234struct lyd_attr {
235 struct lyd_node *parent; /**< data node where the attribute is placed */
236 struct lyd_attr *next; /**< pointer to the next attribute of the same element */
237 void *annotation; /**< TODO pointer to the attribute/annotation's definition */
238 const char *name; /**< attribute name */
239 struct lyd_value value; /**< attribute's value representation */
240};
241
242
243#define LYD_NODE_INNER (LYS_CONTAINER|LYS_LIST) /**< Schema nodetype mask for lyd_node_inner */
244#define LYD_NODE_TERM (LYS_LEAF|LYS_LEAFLIST) /**< Schema nodetype mask for lyd_node_term */
245#define LYD_NODE_ANY (LYS_ANYDATA) /**< Schema nodetype mask for lyd_node_any */
246
247/**
248 * @brief Generic structure for a data node.
249 */
250struct lyd_node {
251 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list) */
252 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
253 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
254 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
255 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
256 never NULL. If there is no sibling node, pointer points to the node
257 itself. In case of the first node, this pointer points to the last
258 node in the list. */
259 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
260
261#ifdef LY_ENABLED_LYD_PRIV
262 void *priv; /**< private user data, not used by libyang */
263#endif
264};
265
266/**
267 * @brief Data node structure for the inner data tree nodes - containers and lists.
268 */
269struct lyd_node_inner {
270 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list) */
271 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
272 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
273 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
274 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
275 never NULL. If there is no sibling node, pointer points to the node
276 itself. In case of the first node, this pointer points to the last
277 node in the list. */
278 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
279
280#ifdef LY_ENABLED_LYD_PRIV
281 void *priv; /**< private user data, not used by libyang */
282#endif
283
284 struct lyd_node *child; /**< pointer to the first child node. */
285 struct hash_table *children_ht; /**< hash table with all the direct children (except keys for a list, lists without keys) */
286};
287
288/**
289 * @brief Data node structure for the terminal data tree nodes - leafs and leaf-lists.
290 */
291struct lyd_node_term {
292 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list) */
293 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
294 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
295 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
296 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
297 never NULL. If there is no sibling node, pointer points to the node
298 itself. In case of the first node, this pointer points to the last
299 node in the list. */
300 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
301
302#ifdef LY_ENABLED_LYD_PRIV
303 void *priv; /**< private user data, not used by libyang */
304#endif
305
306 struct lyd_value value; /**< node's value representation */
307};
308
309/**
310 * @brief Data node structure for the anydata data tree nodes - anydatas and anyxmls.
311 */
312struct lyd_node_any {
313 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list) */
314 const struct lysc_node *schema; /**< pointer to the schema definition of this node */
315 struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */
316 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
317 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
318 never NULL. If there is no sibling node, pointer points to the node
319 itself. In case of the first node, this pointer points to the last
320 node in the list. */
321 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
322
323#ifdef LY_ENABLED_LYD_PRIV
324 void *priv; /**< private user data, not used by libyang */
325#endif
326
327 /* TODO - anydata representation */
328};
329
330/**
331 * @defgroup dataparseroptions Data parser options
332 * @ingroup datatree
333 *
334 * Various options to change the data tree parsers behavior.
335 *
336 * Default behavior:
337 * - in case of XML, parser reads all data from its input (file, memory, XML tree) including the case of not well-formed
338 * XML document (multiple top-level elements) and if there is an unknown element, it is skipped including its subtree
339 * (see the next point). This can be changed by the #LYD_OPT_NOSIBLINGS option which make parser to read only a single
340 * tree (with a single root element) from its input.
341 * - parser silently ignores the data without a matching node in schema trees. If the caller want to stop
342 * parsing in case of presence of unknown data, the #LYD_OPT_STRICT can be used. The strict mode is useful for
343 * NETCONF servers, since NETCONF clients should always send data according to the capabilities announced by the server.
344 * On the other hand, the default non-strict mode is useful for clients receiving data from NETCONF server since
345 * clients are not required to understand everything the server does. Of course, the optimal strategy for clients is
346 * to use filtering to get only the required data. Having an unknown element of the known namespace is always an error.
347 * The behavior can be changed by #LYD_OPT_STRICT option.
348 * - using obsolete statements (status set to obsolete) just generates a warning, but the processing continues. The
349 * behavior can be changed by #LYD_OPT_OBSOLETE option.
350 * - parser expects that the provided data provides complete datastore content (both the configuration and state data)
351 * and performs data validation according to all YANG rules. This can be a problem in case of representing NETCONF's
352 * subtree filter data, edit-config's data or other type of data set - such data do not represent a complete data set
353 * and some of the validation rules can fail. Therefore there are other options (within lower 8 bits) to make parser
354 * to accept such a data.
355 * - when parser evaluates when-stmt condition to false, a validation error is raised. If the
356 * #LYD_OPT_WHENAUTODEL is used, the invalid node is silently removed instead of an error. The option (and also this default
357 * behavior) takes effect only in case of #LYD_OPT_DATA or #LYD_OPT_CONFIG type of data.
358 * @{
359 */
360
361#define LYD_OPT_DATA 0x00 /**< Default type of data - complete datastore content with configuration as well as
362 state data. To handle possibly missing (but by default required) ietf-yang-library
363 data, use #LYD_OPT_DATA_NO_YANGLIB or #LYD_OPT_DATA_ADD_YANGLIB options. */
364#define LYD_OPT_CONFIG 0x01 /**< A configuration datastore - complete datastore without state data.
365 Validation modifications:
366 - status data are not allowed */
367#define LYD_OPT_GET 0x02 /**< Data content from a NETCONF reply message to the NETCONF \<get\> operation.
368 Validation modifications:
369 - mandatory nodes can be omitted
370 - leafrefs and instance-identifier resolution is allowed to fail
371 - list's keys/unique nodes are not required (so duplication is not checked)
372 - must and when evaluation skipped */
373#define LYD_OPT_GETCONFIG 0x04 /**< Data content from a NETCONF reply message to the NETCONF \<get-config\> operation
374 Validation modifications:
375 - mandatory nodes can be omitted
376 - leafrefs and instance-identifier resolution is allowed to fail
377 - list's keys/unique nodes are not required (so duplication is not checked)
378 - must and when evaluation skipped
379 - status data are not allowed */
380#define LYD_OPT_EDIT 0x08 /**< Content of the NETCONF \<edit-config\>'s config element.
381 Validation modifications:
382 - mandatory nodes can be omitted
383 - leafrefs and instance-identifier resolution is allowed to fail
384 - must and when evaluation skipped
385 - status data are not allowed */
386#define LYD_OPT_RPC 0x10 /**< Data represents RPC or action input parameters. */
387#define LYD_OPT_RPCREPLY 0x20 /**< Data represents RPC or action output parameters (maps to NETCONF <rpc-reply> data). */
388#define LYD_OPT_NOTIF 0x40 /**< Data represents an event notification data. */
389#define LYD_OPT_NOTIF_FILTER 0x80 /**< Data represents a filtered event notification data.
390 Validation modification:
391 - the only requirement is that the data tree matches the schema tree */
392#define LYD_OPT_TYPEMASK 0x10000ff /**< Mask to filter data type options. Always only a single data type option (only
393 single bit from the lower 8 bits) can be set. */
394
395/* 0x100 reserved, used internally */
396#define LYD_OPT_STRICT 0x0200 /**< Instead of silent ignoring data without schema definition, raise an error. */
397#define LYD_OPT_DESTRUCT 0x0400 /**< Free the provided XML tree during parsing the data. With this option, the
398 provided XML tree is affected and all successfully parsed data are freed.
399 This option is applicable only to lyd_parse_xml() function. */
400#define LYD_OPT_OBSOLETE 0x0800 /**< Raise an error when an obsolete statement (status set to obsolete) is used. */
401#define LYD_OPT_NOSIBLINGS 0x1000 /**< Parse only a single XML tree from the input. This option applies only to
402 XML input data. */
403#define LYD_OPT_TRUSTED 0x2000 /**< Data comes from a trusted source and it is not needed to validate them. Data
404 are connected with the schema, but the most validation checks (mandatory nodes,
405 list instance uniqueness, etc.) are not performed. This option does not make
406 sense for lyd_validate() so it is ignored by this function. */
407#define LYD_OPT_WHENAUTODEL 0x4000 /**< Automatically delete subtrees with false when-stmt condition. The flag is
408 applicable only in combination with #LYD_OPT_DATA and #LYD_OPT_CONFIG flags.
409 If used, libyang will not generate a validation error. */
410#define LYD_OPT_NOEXTDEPS 0x8000 /**< Allow external dependencies (external leafrefs, instance-identifiers, must,
411 and when) to not be resolved/satisfied during validation. */
412#define LYD_OPT_DATA_NO_YANGLIB 0x10000 /**< Ignore (possibly) missing ietf-yang-library data. Applicable only with #LYD_OPT_DATA. */
413#define LYD_OPT_DATA_ADD_YANGLIB 0x20000 /**< Add missing ietf-yang-library data into the validated data tree. Applicable
414 only with #LYD_OPT_DATA. If some ietf-yang-library data are present, they are
415 preserved and option is ignored. */
416#define LYD_OPT_VAL_DIFF 0x40000 /**< Flag only for validation, store all the data node changes performed by the validation
417 in a diff structure. */
418#define LYD_OPT_DATA_TEMPLATE 0x1000000 /**< Data represents YANG data template. */
419
420/**@} dataparseroptions */
421
422/**
423 * @brief Get the node's children list if any.
424 *
425 * Decides the node's type and in case it has a children list, returns it.
426 * @param[in] node Node to check.
427 * @return Pointer to the first child node (if any) of the \p node.
428 */
429const struct lyd_node *lyd_node_children(const struct lyd_node *node);
430
431/**
432 * @brief Find the node, in the list, satisfying the given restrictions.
433 *
434 * @param[in] first Starting child node for search.
435 * @param[in] module Module of the node to find (mandatory argument).
436 * @param[in] name Name of the node to find (mandatory argument).
437 * @param[in] name_len Optional length of the @p name argument in case it is not NULL-terminated string.
438 * @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 +0200439 * @param[in] value Optional restriction for lyd_node_term nodes to select node with the specific value. Note that this
440 * search restriction is limited to compare canonical representation of the type. Some of the types have no canonical
441 * representation and 2 different strings can represent the same value (e.g. different prefixes of the same namespace in instance-identifiers).
442 * In this case there is more advanced lyd_value_compare() to check if the values matches.
Radek Krejcie7b95092019-05-15 11:03:07 +0200443 * @param[in] value_len Optional length of the @p value argument in case it is not NULL-terminated string.
444 * @return The sibling node of the @p first (or itself), satisfying the given restrictions.
445 * @return NULL in case there is no node satisfying the restrictions.
446 */
447const struct lyd_node *lyd_search(const struct lyd_node *first, const struct lys_module *module,
448 const char *name, size_t name_len, uint16_t nodetype, const char *value, size_t value_len);
449
450/**
451 * @brief Parse (and validate) data from memory.
452 *
453 * In case of LY_XML format, the data string is parsed completely. It means that when it contains
454 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
455 * returned data node is a root of the first tree with other trees connected via the next pointer.
456 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
457 *
458 * @param[in] ctx Context to connect with the data tree being built here.
459 * @param[in] data Serialized data in the specified format.
460 * @param[in] format Format of the input data to be parsed.
461 * @param[in] options Parser options, see @ref parseroptions. \p format LYD_LYB uses #LYD_OPT_TRUSTED implicitly.
462 * @param[in] ... Variable arguments depend on \p options. If they include:
463 * - #LYD_OPT_DATA:
464 * - #LYD_OPT_CONFIG:
465 * - #LYD_OPT_GET:
466 * - #LYD_OPT_GETCONFIG:
467 * - #LYD_OPT_EDIT:
468 * - no variable arguments expected.
469 * - #LYD_OPT_RPC:
470 * - #LYD_OPT_NOTIF:
471 * - struct lyd_node *data_tree - additional data tree that will be used
472 * when checking any "when" or "must" conditions in the parsed tree that require
473 * some nodes outside their subtree. It must be a list of top-level elements!
474 * - #LYD_OPT_RPCREPLY:
475 * - const struct ::lyd_node *rpc_act - pointer to the whole RPC or (top-level) action operation
476 * data tree (the request) of the reply.
477 * - const struct ::lyd_node *data_tree - additional data tree that will be used
478 * when checking any "when" or "must" conditions in the parsed tree that require
479 * some nodes outside their subtree. It must be a list of top-level elements!
480 * @return Pointer to the built data tree or NULL in case of empty \p data. To free the returned structure,
481 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
482 * #ly_errno contains appropriate error code (see #LY_ERR).
483 */
484struct lyd_node *lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, ...);
485
486/**
487 * @brief Read (and validate) data from the given file descriptor.
488 *
489 * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc.
490 *
491 * In case of LY_XML format, the file content is parsed completely. It means that when it contains
492 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
493 * returned data node is a root of the first tree with other trees connected via the next pointer.
494 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
495 *
496 * @param[in] ctx Context to connect with the data tree being built here.
497 * @param[in] fd The standard file descriptor of the file containing the data tree in the specified format.
498 * @param[in] format Format of the input data to be parsed.
499 * @param[in] options Parser options, see @ref parseroptions. \p format LYD_LYB uses #LYD_OPT_TRUSTED implicitly.
500 * @param[in] ... Variable arguments depend on \p options. If they include:
501 * - #LYD_OPT_DATA:
502 * - #LYD_OPT_CONFIG:
503 * - #LYD_OPT_GET:
504 * - #LYD_OPT_GETCONFIG:
505 * - #LYD_OPT_EDIT:
506 * - no variable arguments expected.
507 * - #LYD_OPT_RPC:
508 * - #LYD_OPT_NOTIF:
509 * - struct lyd_node *data_tree - additional data tree that will be used
510 * when checking any "when" or "must" conditions in the parsed tree that require
511 * some nodes outside their subtree. It must be a list of top-level elements!
512 * - #LYD_OPT_RPCREPLY:
513 * - const struct ::lyd_node *rpc_act - pointer to the whole RPC or action operation data
514 * tree (the request) of the reply.
515 * - const struct ::lyd_node *data_tree - additional data tree that will be used
516 * when checking any "when" or "must" conditions in the parsed tree that require
517 * some nodes outside their subtree. It must be a list of top-level elements!
518 * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure,
519 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
520 * #ly_errno contains appropriate error code (see #LY_ERR).
521 */
522struct lyd_node *lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, ...);
523
524/**
525 * @brief Read (and validate) data from the given file path.
526 *
527 * In case of LY_XML format, the file content is parsed completely. It means that when it contains
528 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
529 * returned data node is a root of the first tree with other trees connected via the next pointer.
530 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
531 *
532 * @param[in] ctx Context to connect with the data tree being built here.
533 * @param[in] path Path to the file containing the data tree in the specified format.
534 * @param[in] format Format of the input data to be parsed.
535 * @param[in] options Parser options, see @ref parseroptions. \p format LYD_LYB uses #LYD_OPT_TRUSTED implicitly.
536 * @param[in] ... Variable arguments depend on \p options. If they include:
537 * - #LYD_OPT_DATA:
538 * - #LYD_OPT_CONFIG:
539 * - #LYD_OPT_GET:
540 * - #LYD_OPT_GETCONFIG:
541 * - #LYD_OPT_EDIT:
542 * - no variable arguments expected.
543 * - #LYD_OPT_RPC:
544 * - #LYD_OPT_NOTIF:
545 * - struct lyd_node *data_tree - additional data tree that will be used
546 * when checking any "when" or "must" conditions in the parsed tree that require
547 * some nodes outside their subtree. It must be a list of top-level elements!
548 * - #LYD_OPT_RPCREPLY:
549 * - const struct ::lyd_node *rpc_act - pointer to the whole RPC or action operation data
550 * tree (the request) of the reply.
551 * - const struct ::lyd_node *data_tree - additional data tree that will be used
552 * when checking any "when" or "must" conditions in the parsed tree that require
553 * some nodes outside their subtree. It must be a list of top-level elements!
554 * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure,
555 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
556 * #ly_errno contains appropriate error code (see #LY_ERR).
557 */
558struct lyd_node *lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options, ...);
559
560/**
561 * @brief Free all the nodes in the data tree.
562 *
563 * @param[in] node Any of the nodes inside the tree.
564 */
565void lyd_free_all(struct lyd_node *node);
566
567/**
568 * @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/**
611 * @brief Free the trees ([sized array](@ref sizedarrays)).
612 *
613 * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees.
614 * @param[in] free_data Flag to free also the particular trees in the @p trees ([sized array](@ref sizedarrays)).
615 * If set to zero, only the trees envelope is freed and data are untouched.
616 */
617void lyd_trees_free(const struct lyd_node **trees, int free_data);
618
619/**
Radek Krejci084289f2019-07-09 17:35:30 +0200620 * @brief Check type restrictions applicable to the particular leaf/leaf-list with the given string @p value.
621 *
622 * The given node is not modified in any way - it is just checked if the @p value can be set to the node.
623 *
624 * If there is no data node instance and you are fine with checking just the type's restrictions without the
625 * data tree context (e.g. for the case of require-instance restriction), use lys_value_validate().
626 *
627 * @param[in] ctx libyang context for logging (function does not log errors when @p ctx is NULL)
628 * @param[in] node Data node for the @p value.
629 * @param[in] value String value to be checked.
630 * @param[in] value_len Length of the given @p value (mandatory).
631 * @param[in] get_prefix Callback function to resolve prefixes used in the @p value string.
632 * @param[in] get_prefix_data Private data for the @p get_prefix callback.
633 * @param[in] format Input format of the data.
634 * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees (e.g. when validating RPC/Notification) where the required
635 * 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 +0200636 * then LY_EINCOMPLETE can be returned. To simply prepare this structure, use lyd_trees_new().
Radek Krejci084289f2019-07-09 17:35:30 +0200637 * @return LY_SUCCESS on success
638 * @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).
639 * @return LY_ERR value if an error occurred.
640 */
641LY_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 +0200642 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 +0200643
644/**
645 * @brief Compare the node's value with the given string value. The string value is first validated according to the node's type.
646 *
647 * @param[in] node Data node to compare.
648 * @param[in] value String value to be compared. It does not need to be in a canonical form - as part of the process,
649 * it is validated and canonized if possible.
650 * @param[in] value_len Length of the given @p value (mandatory).
651 * @param[in] get_prefix Callback function to resolve prefixes used in the @p value string.
652 * @param[in] get_prefix_data Private data for the @p get_prefix callback.
653 * @param[in] format Input format of the data.
654 * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees (e.g. when validating RPC/Notification) where the required
655 * 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 +0200656 * then LY_EINCOMPLETE can be returned in case the validation was not completed, but values matches. To simply prepare
657 * this structure, use lyd_trees_new(). To simply prepare this structure, use lyd_trees_new().
Radek Krejci084289f2019-07-09 17:35:30 +0200658 * @return LY_SUCCESS on success
659 * @return LY_EINCOMPLETE in case of success when the @p trees is not provided and it was needed to finish the validation of
660 * the given string @p value (e.g. due to require-instance).
661 * @return LY_ERR value if an error occurred.
662 */
663LY_ERR lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len,
Radek Krejci576b23f2019-07-12 14:06:32 +0200664 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 +0200665
Radek Krejci576b23f2019-07-12 14:06:32 +0200666/**
667 * @brief Resolve instance-identifier defined by lyd_value_path structure.
668 *
669 * @param[in] path Path structure specifying the instance-identifier target.
670 * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees to be searched.
671 * To simply prepare this structure, use lyd_trees_new().
672 * @return Target node of the instance-identifier present in the given data @p trees.
673 */
674const struct lyd_node_term *lyd_target(struct lyd_value_path *path, const struct lyd_node **trees);
Radek Krejci084289f2019-07-09 17:35:30 +0200675
Radek Krejcie7b95092019-05-15 11:03:07 +0200676#ifdef __cplusplus
677}
678#endif
679
680#endif /* LY_TREE_DATA_H_ */