blob: 01a9e7b544ffd0b5d78e01d67532232fc74c69d9 [file] [log] [blame]
Michal Vasko2d162e12015-09-24 14:33:29 +02001/**
Radek Krejciaa429e42015-10-09 15:52:37 +02002 * @file tree_data.h
Michal Vasko2d162e12015-09-24 14:33:29 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Radek Krejciaa429e42015-10-09 15:52:37 +02004 * @brief libyang representation of data trees.
Michal Vasko2d162e12015-09-24 14:33:29 +02005 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci54f6fb32016-02-24 12:56:39 +01008 * 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
Michal Vasko8de098c2016-02-26 10:00:25 +010011 *
Radek Krejci54f6fb32016-02-24 12:56:39 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko2d162e12015-09-24 14:33:29 +020013 */
14
15#ifndef LY_TREE_DATA_H_
16#define LY_TREE_DATA_H_
17
18#include <stddef.h>
19#include <stdint.h>
20
Mislav Novakovice251a652015-09-29 08:40:12 +020021#include "tree_schema.h"
Radek Krejcidef50022016-02-01 16:38:32 +010022#include "xml.h"
Mislav Novakovice251a652015-09-29 08:40:12 +020023
Michal Vasko2d162e12015-09-24 14:33:29 +020024#ifdef __cplusplus
25extern "C" {
26#endif
27
28/**
Radek Krejcidef50022016-02-01 16:38:32 +010029 * @defgroup datatree Data Tree
Michal Vasko2d162e12015-09-24 14:33:29 +020030 * @{
Radek Krejcidef50022016-02-01 16:38:32 +010031 *
32 * Data structures and functions to manipulate and access instance data tree.
Michal Vasko2d162e12015-09-24 14:33:29 +020033 */
34
35/**
Radek Krejcidef50022016-02-01 16:38:32 +010036 * @brief Data input/output formats supported by libyang [parser](@ref howtodataparsers) and
37 * [printer](@ref howtodataprinters) functions.
Michal Vasko2d162e12015-09-24 14:33:29 +020038 */
39typedef enum {
40 LYD_UNKNOWN, /**< unknown format, used as return value in case of error */
41 LYD_XML, /**< XML format of the instance data */
42 LYD_JSON, /**< JSON format of the instance data */
43} LYD_FORMAT;
44
45/**
Michal Vasko2d162e12015-09-24 14:33:29 +020046 * @brief Attribute structure.
47 *
Radek Krejci5f9e8c92015-10-30 10:01:06 +010048 * The structure provides information about attributes of a data element. Such attributes partially
49 * maps to annotations from draft-ietf-netmod-yang-metadata. In XML, they are represented as standard
50 * XML attrbutes. In JSON, they are represented as JSON elements starting with the '@' character
51 * (for more information, see the yang metadata draft.
52 *
Michal Vasko2d162e12015-09-24 14:33:29 +020053 */
54struct lyd_attr {
Radek Krejci5f9e8c92015-10-30 10:01:06 +010055 struct lyd_attr *next; /**< pointer to the next attribute of the same element */
56 struct lys_module *module; /**< pointer to the attribute's module.
57 TODO when annotations will be supported, point to the annotation definition
58 and validate that the attribute is really defined there. Currently, we just
59 believe that it is defined in the module it says */
Michal Vasko2d162e12015-09-24 14:33:29 +020060 const char *name; /**< attribute name */
61 const char *value; /**< attribute value */
62};
63
64/**
65 * @brief node's value representation
66 */
67typedef union lyd_value_u {
68 const char *binary; /**< base64 encoded, NULL terminated string */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +020069 struct lys_type_bit **bit; /**< bitmap of pointers to the schema definition of the bit value that are set,
70 its size is always the number of defined bits in the schema */
Radek Krejci489773c2015-12-17 13:20:03 +010071 int8_t bln; /**< 0 as false, 1 as true */
Michal Vasko2d162e12015-09-24 14:33:29 +020072 int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */
73 struct lys_type_enum *enm; /**< pointer to the schema definition of the enumeration value */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +020074 struct lys_ident *ident; /**< pointer to the schema definition of the identityref value */
Radek Krejci40f17b92016-02-03 14:30:43 +010075 struct lyd_node *instance; /**< pointer to the instance-identifier target, note that if the tree was modified,
76 the target (address) can be invalid - the pointer is correctly checked and updated
77 by lyd_validate() */
Michal Vasko2d162e12015-09-24 14:33:29 +020078 int8_t int8; /**< 8-bit signed integer */
79 int16_t int16; /**< 16-bit signed integer */
80 int32_t int32; /**< 32-bit signed integer */
81 int64_t int64; /**< 64-bit signed integer */
82 struct lyd_node *leafref; /**< pointer to the referenced leaf/leaflist instance in data tree */
83 const char *string; /**< string */
84 uint8_t uint8; /**< 8-bit unsigned integer */
85 uint16_t uint16; /**< 16-bit signed integer */
86 uint32_t uint32; /**< 32-bit signed integer */
87 uint64_t uint64; /**< 64-bit signed integer */
88} lyd_val;
89
90/**
Radek Krejcica7efb72016-01-18 13:06:01 +010091 * @defgroup validityflags Validity flags
92 * @ingroup datatree
93 *
94 * Validity flags for data nodes.
95 *
96 * @{
97 */
98#define LYD_VAL_OK 0x00 /**< node is successfully validated including whole subtree */
99#define LYD_VAL_UNIQUE 0x01 /**< Unique value(s) changed, applicable only to ::lys_node_list data nodes */
Radek Krejci1eefeb32016-04-15 16:01:46 +0200100#define LYD_VAL_NOT 0x0f /**< node was not validated yet */
Radek Krejcica7efb72016-01-18 13:06:01 +0100101/**
102 * @}
103 */
104
105/**
Michal Vasko2d162e12015-09-24 14:33:29 +0200106 * @brief Generic structure for a data node, directly applicable to the data nodes defined as #LYS_CONTAINER, #LYS_LIST
107 * and #LYS_CHOICE.
108 *
109 * Completely fits to containers and choices and is compatible (can be used interchangeably except the #child member)
110 * with all other lyd_node_* structures. All data nodes are provides as ::lyd_node structure by default.
111 * According to the schema's ::lys_node#nodetype member, the specific object is supposed to be cast to
Radek Krejcica7efb72016-01-18 13:06:01 +0100112 * ::lyd_node_leaf_list or ::lyd_node_anyxml structures. This structure fits only to #LYS_CONTAINER, #LYS_LIST and
113 * #LYS_CHOICE values.
Michal Vasko2d162e12015-09-24 14:33:29 +0200114 *
115 * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro.
116 */
117struct lyd_node {
118 struct lys_node *schema; /**< pointer to the schema definition of this node */
Radek Krejci1eefeb32016-04-15 16:01:46 +0200119 uint8_t validity:4; /**< [validity flags](@ref validityflags) */
120 uint8_t dflt:1; /**< flag for default node (applicable only on leafs) to be marked with default attribute */
Radek Krejci0b7704f2016-03-18 12:16:14 +0100121 uint8_t when_status:3; /**< bit for checking if the when-stmt condition is resolved - internal use only,
Radek Krejci03b71f72016-03-16 11:10:09 +0100122 do not use this value! */
Michal Vasko2d162e12015-09-24 14:33:29 +0200123
124 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
125 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
126 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
127 never NULL. If there is no sibling node, pointer points to the node
128 itself. In case of the first node, this pointer points to the last
129 node in the list. */
130 struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
131 struct lyd_node *child; /**< pointer to the first child node \note Since other lyd_node_*
Radek Krejciee360892015-10-06 11:23:14 +0200132 structures represent end nodes, this member
Michal Vasko2d162e12015-09-24 14:33:29 +0200133 is replaced in those structures. Therefore, be careful with accessing
134 this member without having information about the node type from the schema's
135 ::lys_node#nodetype member. */
136};
137
138/**
Michal Vasko4c183312015-09-25 10:41:47 +0200139 * @brief Structure for data nodes defined as #LYS_LEAF or #LYS_LEAFLIST.
Michal Vasko2d162e12015-09-24 14:33:29 +0200140 *
Michal Vasko4c183312015-09-25 10:41:47 +0200141 * Extension for ::lyd_node structure. It replaces the ::lyd_node#child member by
142 * three new members (#value, #value_str and #value_type) to provide
143 * information about the value. The first five members (#schema, #attr, #next,
Michal Vasko2d162e12015-09-24 14:33:29 +0200144 * #prev and #parent) are compatible with the ::lyd_node's members.
145 *
146 * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro.
147 */
Michal Vasko4c183312015-09-25 10:41:47 +0200148struct lyd_node_leaf_list {
Michal Vasko2d162e12015-09-24 14:33:29 +0200149 struct lys_node *schema; /**< pointer to the schema definition of this node which is ::lys_node_leaflist
150 structure */
Radek Krejci1eefeb32016-04-15 16:01:46 +0200151 uint8_t validity:4; /**< [validity flags](@ref validityflags) */
152 uint8_t dflt:1; /**< flag for default node (applicable only on leafs) to be marked with default attribute */
Radek Krejci0b7704f2016-03-18 12:16:14 +0100153 uint8_t when_status:3; /**< bit for checking if the when-stmt condition is resolved - internal use only,
Radek Krejci03b71f72016-03-16 11:10:09 +0100154 do not use this value! */
Michal Vasko2d162e12015-09-24 14:33:29 +0200155
156 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
157 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
158 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
159 never NULL. If there is no sibling node, pointer points to the node
160 itself. In case of the first node, this pointer points to the last
161 node in the list. */
162 struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
163
164 /* struct lyd_node *child; should be here, but is not */
165
166 /* leaflist's specific members */
Michal Vasko2d162e12015-09-24 14:33:29 +0200167 const char *value_str; /**< string representation of value (for comparison, printing,...) */
Radek Krejci23238922015-10-27 17:13:34 +0100168 lyd_val value; /**< node's value representation */
Michal Vasko2d162e12015-09-24 14:33:29 +0200169 LY_DATA_TYPE value_type; /**< type of the value in the node, mainly for union to avoid repeating of type detection */
170};
171
Michal Vaskof748dbc2016-04-05 11:27:47 +0200172union lyd_node_anyxml_value {
173 const char *str;
174 struct lyxml_elem *xml;
175};
176
Michal Vasko2d162e12015-09-24 14:33:29 +0200177/**
178 * @brief Structure for data nodes defined as #LYS_ANYXML.
179 *
180 * Extension for ::lyd_node structure - replaces the ::lyd_node#child member by new #value member. The first five
181 * members (#schema, #attr, #next, #prev and #parent) are compatible with the ::lyd_node's members.
182 *
183 * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro.
184 */
185struct lyd_node_anyxml {
186 struct lys_node *schema; /**< pointer to the schema definition of this node which is ::lys_node_anyxml
187 structure */
Radek Krejci1eefeb32016-04-15 16:01:46 +0200188 uint8_t validity:4; /**< [validity flags](@ref validityflags) */
189 uint8_t dflt:1; /**< flag for default node (applicable only on leafs) to be marked with default attribute */
Radek Krejci0b7704f2016-03-18 12:16:14 +0100190 uint8_t when_status:3; /**< bit for checking if the when-stmt condition is resolved - internal use only,
Radek Krejci03b71f72016-03-16 11:10:09 +0100191 do not use this value! */
Michal Vasko2d162e12015-09-24 14:33:29 +0200192
193 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
194 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
195 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
196 never NULL. If there is no sibling node, pointer points to the node
197 itself. In case of the first node, this pointer points to the last
198 node in the list. */
199 struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
200
201 /* struct lyd_node *child; should be here, but is not */
202
203 /* anyxml's specific members */
Michal Vaskof748dbc2016-04-05 11:27:47 +0200204 uint8_t xml_struct; /**< 1 for value.xml, 0 for value.str */
205 union lyd_node_anyxml_value value; /**< anyxml value, everything is in the dictionary, there can be more XML siblings */
Michal Vasko2d162e12015-09-24 14:33:29 +0200206};
207
208/**
Radek Krejcidef50022016-02-01 16:38:32 +0100209 * @defgroup parseroptions Data parser options
210 * @ingroup datatree
211 *
212 * Various options to change the data tree parsers behavior.
213 *
214 * Default behavior:
215 * - in case of XML, parser reads all data from its input (file, memory, XML tree) including the case of not well-formed
216 * XML document (multiple top-level elements) and if there is an unknown element, it is skipped including its subtree
217 * (see the next point). This can be changed by the #LYD_OPT_NOSIBLINGS option which make parser to read only a single
218 * tree (with a single root element) from its input.
219 * - parser silently ignores the data without a matching node in schema trees. If the caller want to stop
220 * parsing in case of presence of unknown data, the #LYD_OPT_STRICT can be used. The strict mode is useful for
221 * NETCONF servers, since NETCONF clients should always send data according to the capabilities announced by the server.
222 * On the other hand, the default non-strict mode is useful for clients receiving data from NETCONF server since
223 * clients are not required to understand everything the server does. Of course, the optimal strategy for clients is
224 * to use filtering to get only the required data. Having an unknown element of the known namespace is always an error.
225 * The behavior can be changed by #LYD_OPT_STRICT option.
226 * - using obsolete statements (status set to obsolete) just generates a warning, but the processing continues. The
227 * behavior can be changed by #LYD_OPT_OBSOLETE option.
228 * - parser expects that the provided data provides complete datastore content (both the configuration and state data)
229 * and performs data validation according to all YANG rules. This can be a problem in case of representing NETCONF's
230 * subtree filter data, edit-config's data or other type of data set - such data do not represent a complete data set
231 * and some of the validation rules can fail. Therefore there are other options (within lower 8 bits) to make parser
232 * to accept such a data.
Radek Krejcif3c218d2016-03-24 12:40:08 +0100233 * - when parser evaluates when-stmt condition to false, the constrained subtree is automatically removed. If the
234 * #LYD_OPT_NOAUTODEL is used, error is raised instead of silent auto delete. The option (and also this default
235 * behavior) takes effect only in case of #LYD_OPT_DATA or #LYD_OPT_CONFIG type of data.
Radek Krejci0c0086a2016-03-24 15:20:28 +0100236 * - whenever the parser see empty non-presence container, it is automatically removed to minimize memory usage. This
237 * behavior can be changed by #LYD_OPT_KEEPEMPTYCONT.
Radek Krejci7b4309c2016-03-23 10:30:29 +0100238 * - for validation, parser needs to add default nodes into the data tree. By default, these additional (implicit)
239 * nodes are removed before the parser returns. However, if caller use one of the LYD_WD_* option, the default nodes
240 * added by parser are kept in the resulting tree or even the explicit nodes with the default values can be removed
241 * (in case of #LYD_WD_TRIM option).
Radek Krejcidef50022016-02-01 16:38:32 +0100242 * @{
243 */
244
245#define LYD_OPT_DATA 0x00 /**< Default type of data - complete datastore content with configuration as well as
246 state data. */
247#define LYD_OPT_CONFIG 0x01 /**< A configuration datastore - complete datastore without state data.
248 Validation modifications:
249 - status data are not allowed */
250#define LYD_OPT_GET 0x02 /**< Data content from a NETCONF reply message to the NETCONF \<get\> operation.
251 Validation modifications:
252 - mandatory nodes can be omitted
253 - leafrefs and instance-identifier are not resolved
254 - list's keys/unique nodes are not required (so duplication is not checked) */
255#define LYD_OPT_GETCONFIG 0x04 /**< Data content from a NETCONF reply message to the NETCONF \<get-config\> operation
256 Validation modifications:
257 - mandatory nodes can be omitted
258 - leafrefs and instance-identifier are not resolved
259 - list's keys/unique nodes are not required (so duplication is not checked)
260 - status data are not allowed */
261#define LYD_OPT_EDIT 0x08 /**< Content of the NETCONF \<edit-config\>'s config element.
262 Validation modifications:
263 - mandatory nodes can be omitted
264 - leafrefs and instance-identifier are not resolved
265 - status data are not allowed */
266#define LYD_OPT_RPC 0x10 /**< Data represents RPC's input parameters. */
267#define LYD_OPT_RPCREPLY 0x20 /**< Data represents RPC's output parameters (maps to NETCONF <rpc-reply> data). */
268#define LYD_OPT_NOTIF 0x40 /**< Data represents an event notification data. */
Radek Krejci92ece002016-04-04 15:45:05 +0200269/* 0x80 reserved, formerly LYD_OPT_FILTER */
Radek Krejcidef50022016-02-01 16:38:32 +0100270#define LYD_OPT_TYPEMASK 0xff /**< Mask to filter data type options. Always only a single data type option (only
271 single bit from the lower 8 bits) can be set. */
272
273#define LYD_OPT_STRICT 0x0100 /**< Instead of silent ignoring data without schema definition, raise an error. */
274#define LYD_OPT_DESTRUCT 0x0200 /**< Free the provided XML tree during parsing the data. With this option, the
275 provided XML tree is affected and all succesfully parsed data are freed.
276 This option is applicable only to lyd_parse_xml() function. */
277#define LYD_OPT_OBSOLETE 0x0400 /**< Raise an error when an obsolete statement (status set to obsolete) is used. */
278#define LYD_OPT_NOSIBLINGS 0x0800 /**< Parse only a single XML tree from the input. This option applies only to
279 XML input data. */
Radek Krejci93fab982016-02-03 15:58:19 +0100280#define LYD_OPT_TRUSTED 0x1000 /**< Data comes from a trusted source and it is not needed to validate them. Data
281 are connected with the schema, but the most validation checks (mandatory nodes,
282 list instance uniqueness, etc.) are not performed. This option does not make
283 sense for lyd_validate() so it is ignored by this function. */
Radek Krejci03b71f72016-03-16 11:10:09 +0100284#define LYD_OPT_NOAUTODEL 0x2000 /**< Avoid automatic delete of subtrees with false when-stmt condition. The flag is
285 applicable only in combination with LYD_OPT_DATA and LYD_OPT_CONFIG flags.
286 If used, libyang generates validation error instead of silently removing the
287 constrained subtree. */
Radek Krejci0c0086a2016-03-24 15:20:28 +0100288#define LYD_OPT_KEEPEMPTYCONT 0x4000 /**< Do not automatically delete empty non-presence containers. */
Radek Krejcidef50022016-02-01 16:38:32 +0100289
Radek Krejciaa3c18d2016-04-15 11:29:10 +0200290#define LYD_WD_MASK 0x1F0000 /**< Mask for with-defaults modes */
291#define LYD_WD_EXPLICIT 0x100000 /**< Explicit mode - add missing default status data, but only in case the data
292 type is supposed to include status data (all except #LYD_OPT_CONFIG,
Radek Krejci9c302312016-04-18 12:32:16 +0200293 #LYD_OPT_GETCONFIG and #LYD_OPT_EDIT */
Radek Krejciaa3c18d2016-04-15 11:29:10 +0200294#define LYD_WD_TRIM 0x010000 /**< Remove all nodes with the value equal to their default value */
295#define LYD_WD_ALL 0x020000 /**< Explicitly add all missing nodes with their default value */
296#define LYD_WD_ALL_TAG 0x040000 /**< Same as LYD_WD_ALL but also adds attribute 'default' with value 'true' to
297 all nodes that has its default value. The 'default' attribute has namespace:
298 urn:ietf:params:xml:ns:netconf:default:1.0 and thus the attributes are
299 created only when the ietf-netconf-with-defaults module is present in libyang
300 context. */
301#define LYD_WD_IMPL_TAG 0x080000 /**< Same as LYD_WD_ALL_TAG but the attributes are added only to the nodes that
302 are being created and were not part of the original data tree despite their
303 value is equal to their default value. There is the same limitation regarding
304 the presence of ietf-netconf-with-defaults module in libyang context. */
Radek Krejci7b4309c2016-03-23 10:30:29 +0100305
Radek Krejcidef50022016-02-01 16:38:32 +0100306/**@} parseroptions */
307
308/**
309 * @brief Parse (and validate according to appropriate schema from the given context) data.
310 *
311 * In case of LY_XML format, the data string is parsed completely. It means that when it contains
312 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
313 * returned data node is a root of the first tree with other trees connected via the next pointer.
314 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
315 *
316 * @param[in] ctx Context to connect with the data tree being built here.
317 * @param[in] data Serialized data in the specified format.
318 * @param[in] format Format of the input data to be parsed.
319 * @param[in] options Parser options, see @ref parseroptions.
320 * @param[in] ... Additional argument must be supplied when #LYD_OPT_RPCREPLY value is specified in \p options. The
321 * argument is supposed to provide pointer to the RPC schema node for the reply's request
322 * (const struct ::lys_node* rpc).
323 * @return Pointer to the built data tree or NULL in case of empty \p data. To free the returned structure,
324 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
325 * #ly_errno contains appropriate error code (see #LY_ERR).
326 */
Radek Krejci722b0072016-02-01 17:09:45 +0100327struct lyd_node *lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, ...);
Radek Krejcidef50022016-02-01 16:38:32 +0100328
329/**
330 * @brief Read data from the given file descriptor.
331 *
332 * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc.
333 *
334 * In case of LY_XML format, the file content is parsed completely. It means that when it contains
335 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
336 * returned data node is a root of the first tree with other trees connected via the next pointer.
337 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
338 *
339 * @param[in] ctx Context to connect with the data tree being built here.
340 * @param[in] fd The standard file descriptor of the file containing the data tree in the specified format.
341 * @param[in] format Format of the input data to be parsed.
342 * @param[in] options Parser options, see @ref parseroptions.
343 * @param[in] ... Additional argument must be supplied when #LYD_OPT_RPCREPLY value is specified in \p options. The
344 * argument is supposed to provide pointer to the RPC schema node for the reply's request
345 * (const struct ::lys_node* rpc).
346 * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure,
347 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
348 * #ly_errno contains appropriate error code (see #LY_ERR).
349 */
350struct lyd_node *lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, ...);
351
352/**
353 * @brief Read data from the given file path.
354 *
355 * In case of LY_XML format, the file content is parsed completely. It means that when it contains
356 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
357 * returned data node is a root of the first tree with other trees connected via the next pointer.
358 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
359 *
360 * @param[in] ctx Context to connect with the data tree being built here.
361 * @param[in] path Path to the file containing the data tree in the specified format.
362 * @param[in] format Format of the input data to be parsed.
363 * @param[in] options Parser options, see @ref parseroptions.
364 * @param[in] ... Additional argument must be supplied when #LYD_OPT_RPCREPLY value is specified in \p options. The
365 * argument is supposed to provide pointer to the RPC schema node for the reply's request
366 * (const struct ::lys_node* rpc).
367 * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure,
368 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
369 * #ly_errno contains appropriate error code (see #LY_ERR).
370 */
371struct lyd_node *lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options, ...);
372
373/**
374 * @brief Parse (and validate according to appropriate schema from the given context) XML tree.
375 *
376 * The output data tree is parsed from the given XML tree previously parsed by one of the
377 * lyxml_read* functions.
378 *
Radek Krejci722b0072016-02-01 17:09:45 +0100379 * If there are some sibling elements of the \p root (data were read with #LYXML_PARSE_MULTIROOT option
Radek Krejcidef50022016-02-01 16:38:32 +0100380 * or the provided root is a root element of a subtree), all the sibling nodes (previous as well as
381 * following) are processed as well. The returned data node is a root of the first tree with other
382 * trees connected via the next pointer. This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
383 *
384 * When the function is used with #LYD_OPT_DESTRUCT, all the successfully parsed data including the
385 * XML \p root and all its siblings (if #LYD_OPT_NOSIBLINGS is not used) are freed. Only with
386 * #LYD_OPT_DESTRUCT option the \p root pointer is changed - if all the data are parsed, it is set
387 * to NULL, otherwise it will hold the XML tree without the successfully parsed elements.
388 *
389 * The context must be the same as the context used to parse XML tree by lyxml_read* function.
390 *
391 * @param[in] ctx Context to connect with the data tree being built here.
392 * @param[in,out] root XML tree to parse (convert) to data tree. By default, parser do not change the XML tree. However,
393 * when #LYD_OPT_DESTRUCT is specified in \p options, parser frees all successfully parsed data.
394 * @param[in] options Parser options, see @ref parseroptions.
395 * @param[in] ... Additional argument must be supplied when #LYD_OPT_RPCREPLY value is specified in \p options. The
396 * argument is supposed to provide pointer to the RPC schema node for the reply's request
397 * (const struct ::lys_node* rpc).
398 * @return Pointer to the built data tree or NULL in case of empty \p root. To free the returned structure,
399 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
400 * #ly_errno contains appropriate error code (see #LY_ERR).
401 */
402struct lyd_node *lyd_parse_xml(struct ly_ctx *ctx, struct lyxml_elem **root, int options,...);
403
404/**
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200405 * @brief Create a new container node in a data tree.
406 *
407 * @param[in] parent Parent node for the node being created. NULL in case of creating top level element.
Radek Krejciee360892015-10-06 11:23:14 +0200408 * @param[in] module Module with the node being created.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200409 * @param[in] name Schema node name of the new data node. The node can be #LYS_CONTAINER, #LYS_LIST,
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200410 * #LYS_NOTIF, or #LYS_RPC.
Michal Vasko1dca6882015-10-22 14:29:42 +0200411 * @return New node, NULL on error.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200412 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100413struct lyd_node *lyd_new(struct lyd_node *parent, const struct lys_module *module, const char *name);
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200414
415/**
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200416 * @brief Create a new leaf or leaflist node in a data tree with a string value that is converted to
417 * the actual value.
418 *
419 * @param[in] parent Parent node for the node being created. NULL in case of creating top level element.
Radek Krejciee360892015-10-06 11:23:14 +0200420 * @param[in] module Module with the node being created.
421 * @param[in] name Schema node name of the new data node.
Michal Vasko3e671b52015-10-23 16:23:15 +0200422 * @param[in] val_str String form of the value of the node being created. In case the type is #LY_TYPE_INST
423 * or #LY_TYPE_IDENT, JSON node-id format is expected (nodes are prefixed with module names, not XML namespaces).
Michal Vasko1dca6882015-10-22 14:29:42 +0200424 * @return New node, NULL on error.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200425 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100426struct lyd_node *lyd_new_leaf(struct lyd_node *parent, const struct lys_module *module, const char *name,
Michal Vasko3e671b52015-10-23 16:23:15 +0200427 const char *val_str);
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200428
429/**
Radek Krejcib9b4d002016-01-18 13:08:51 +0100430 * @brief Change value of a leaf node.
431 *
432 * Despite the prototype allows to provide a leaflist node as \p leaf parameter, only leafs are accepted.
Michal Vasko3a55a8a2016-04-13 14:19:53 +0200433 * Also, changing the value of a list key is prohibited.
Radek Krejcib9b4d002016-01-18 13:08:51 +0100434 *
Radek Krejci0562dbc2016-04-18 14:18:26 +0200435 * As for the other data tree manipulation functions, the change is not fully validated to allow multiple changes
436 * in the data tree. Therefore, when all changes on the data tree are done, caller is supposed to call lyd_validate()
437 * to check that the result is valid data tree. Specifically, if a leafref leaf is changed, it is not checked that
438 * the (leafref) value is correct.
439 *
Radek Krejcib9b4d002016-01-18 13:08:51 +0100440 * @param[in] leaf A leaf node to change.
441 * @param[in] val_str String form of the new value to be set to the \p leaf. In case the type is #LY_TYPE_INST
442 * or #LY_TYPE_IDENT, JSON node-id format is expected (nodes are prefixed with module names, not XML namespaces).
443 * @return 0 on success, non-zero on error.
444 */
445int lyd_change_leaf(struct lyd_node_leaf_list *leaf, const char *val_str);
446
447/**
Michal Vaskof748dbc2016-04-05 11:27:47 +0200448 * @brief Create a new anyxml node in a data tree with a string value.
Michal Vasko2d162e12015-09-24 14:33:29 +0200449 *
Michal Vasko2d162e12015-09-24 14:33:29 +0200450 * @param[in] parent Parent node for the node being created. NULL in case of creating top level element.
Radek Krejciee360892015-10-06 11:23:14 +0200451 * @param[in] module Module with the node being created.
452 * @param[in] name Schema node name of the new data node.
Michal Vaskof748dbc2016-04-05 11:27:47 +0200453 * @param[in] val_str Well-formed XML string value of the node being created. Must be dynamically allocated
454 * and is freed with the data.
Michal Vasko1dca6882015-10-22 14:29:42 +0200455 * @return New node, NULL on error.
Michal Vasko2d162e12015-09-24 14:33:29 +0200456 */
Michal Vaskof748dbc2016-04-05 11:27:47 +0200457struct lyd_node *lyd_new_anyxml_str(struct lyd_node *parent, const struct lys_module *module, const char *name,
458 char *val_str);
459
460/**
461 * @brief Create a new anyxml node in a data tree with an XML structure value.
462 *
463 * @param[in] parent Parent node for the node being created. NULL in case of creating top level element.
464 * @param[in] module Module with the node being created.
465 * @param[in] name Schema node name of the new data node.
466 * @param[in] val_xml XML structure value of the node being created. There can be more siblings,
467 * they are freed with the data.
468 * @return New node, NULL on error.
469 */
470struct lyd_node *lyd_new_anyxml_xml(struct lyd_node *parent, const struct lys_module *module, const char *name,
471 struct lyxml_elem *val_xml);
Michal Vasko2d162e12015-09-24 14:33:29 +0200472
473/**
Michal Vasko50c0a872016-01-13 14:34:11 +0100474 * @brief Create a new container node in a data tree, whose schema parent is #LYS_OUTPUT.
Michal Vasko0df122f2015-12-14 13:38:21 +0100475 *
Michal Vasko50c0a872016-01-13 14:34:11 +0100476 * @param[in] schema Schema node of the container.
Michal Vasko0df122f2015-12-14 13:38:21 +0100477 * @return New node, NULL on error.
478 */
Michal Vasko50c0a872016-01-13 14:34:11 +0100479struct lyd_node *lyd_output_new(const struct lys_node *schema);
480
481/**
482 * @brief Create a new leaf or leaflist node in a data tree, whose schema parent is #LYS_OUTPUT.
483 *
484 * @param[in] schema Schema node of the leaf.
485 * @param[in] val_str String form of the value of the node being created. In case the type is #LY_TYPE_INST
486 * or #LY_TYPE_IDENT, JSON node-id format is expected (nodes are prefixed with module names, not XML namespaces).
487 * @return New node, NULL on error.
488 */
489struct lyd_node *lyd_output_new_leaf(const struct lys_node *schema, const char *val_str);
490
491/**
Michal Vaskof748dbc2016-04-05 11:27:47 +0200492 * @brief Create a new anyxml node in a data tree, whose schema parent is #LYS_OUTPUT
493 * and has a string value.
Michal Vasko50c0a872016-01-13 14:34:11 +0100494 *
495 * @param[in] schema Schema node of the leaf.
Michal Vaskof748dbc2016-04-05 11:27:47 +0200496 * @param[in] val_str Well-formed XML string value of the node being created. Must be dynamically allocated
497 * and is freed with the data.
Michal Vasko50c0a872016-01-13 14:34:11 +0100498 * @return New node, NULL on error.
499 */
Michal Vaskof748dbc2016-04-05 11:27:47 +0200500struct lyd_node *lyd_output_new_anyxml_str(const struct lys_node *schema, char *val_str);
501
502/**
503 * @brief Create a new anyxml node in a data tree, whose schema parent is #LYS_OUTPUT
504 * and has an XML structure value.
505 *
506 * @param[in] schema Schema node of the leaf.
507 * @param[in] val_xml XML structure value of the node being created. There can be more siblings,
508 * they are freed with the data.
509 * @return New node, NULL on error.
510 */
511struct lyd_node *lyd_output_new_anyxml_xml(const struct lys_node *schema, struct lyxml_elem *val_xml);
Michal Vasko0df122f2015-12-14 13:38:21 +0100512
513/**
Michal Vaskof5299282016-03-16 13:32:02 +0100514 * @defgroup pathoptions Data path creation options
515 * @ingroup datatree
516 *
517 * Various options to change lyd_new_path() behavior.
518 *
519 * Default behavior:
Michal Vaskof5299282016-03-16 13:32:02 +0100520 * - if the target node already exists, an error is returned.
Michal Vasko9db078d2016-03-23 11:08:51 +0100521 * - the whole path to the target node is created (with any missing parents) if necessary.
Michal Vasko2411b942016-03-23 13:50:03 +0100522 * - RPC output schema children are completely ignored in all modules. Input is searched and nodes created normally.
Michal Vaskof5299282016-03-16 13:32:02 +0100523 * @{
524 */
525
Michal Vasko72d35102016-03-31 10:03:38 +0200526#define LYD_PATH_OPT_UPDATE 0x01 /**< If the target node exists and is a leaf, it is updated with the new value and returned.
527 If the target node exists and is not a leaf, NULL is returned and no error set. */
Michal Vasko9db078d2016-03-23 11:08:51 +0100528#define LYD_PATH_OPT_NOPARENT 0x02 /**< If any parents of the target node exist, return an error. */
Michal Vasko2411b942016-03-23 13:50:03 +0100529#define LYD_PATH_OPT_OUTPUT 0x04 /**< Changes the behavior to ignoring RPC input schema nodes and using only output ones. */
Michal Vaskof5299282016-03-16 13:32:02 +0100530
531/** @} pathoptions */
532
533/**
534 * @brief Create a new data node based on a simple XPath.
535 *
Michal Vasko8d18ef52016-04-06 12:21:46 +0200536 * The new node is normally inserted at the end, either as the last child of a parent or as the last sibling
537 * if working with top-level elements. However, when manipulating RPC input or output, schema ordering is
538 * required and always guaranteed. Specially, when working with RPC output (using #LYD_PATH_OPT_OUTPUT flag),
539 * it can therefore happen that a node is created and inserted before \p data_tree.
Michal Vasko58f74f12016-03-24 13:26:06 +0100540 *
Michal Vasko8c419642016-04-13 14:22:01 +0200541 * If \p path points to a list key and the list does not exist, the key value from the predicate is used
542 * and \p value is ignored.
543 *
Michal Vasko2411b942016-03-23 13:50:03 +0100544 * @param[in] data_tree Existing data tree to add to/modify. It is expected to be valid. If creating RPCs,
Michal Vasko58f74f12016-03-24 13:26:06 +0100545 * there should only be one RPC and either input or output. Can be NULL.
Michal Vaskof5299282016-03-16 13:32:02 +0100546 * @param[in] ctx Context to use. Mandatory if \p data_tree is NULL.
Michal Vasko9db078d2016-03-23 11:08:51 +0100547 * @param[in] path Simple data XPath of the new node. It can contain only simple node addressing with optional
Michal Vaskof5299282016-03-16 13:32:02 +0100548 * module names as prefixes. List nodes must have predicates, one for each list key in the correct order and
Michal Vasko1acf8502016-05-05 09:14:07 +0200549 * with its value as well, leaves and leaf-lists can have predicates too that have preference over \p value,
550 * see @ref howtoxpath.
Michal Vaskof748dbc2016-04-05 11:27:47 +0200551 * @param[in] value Value of the new leaf/lealf-list. If creating anyxml, this value is internally duplicated
552 * (for other options use lyd_*_new_anyxml_*()). If creating nodes of other types, set to NULL.
Michal Vaskof5299282016-03-16 13:32:02 +0100553 * @param[in] options Bitmask of options flags, see @ref pathoptions.
Michal Vasko8c419642016-04-13 14:22:01 +0200554 * @return First created (or updated with #LYD_PATH_OPT_UPDATE) node,
Michal Vasko17bb4902016-04-05 15:20:51 +0200555 * NULL if #LYD_PATH_OPT_UPDATE was used and the full path exists or the leaf original value matches \p value,
Michal Vasko72d35102016-03-31 10:03:38 +0200556 * NULL and ly_errno is set on error.
Michal Vaskof5299282016-03-16 13:32:02 +0100557 */
Michal Vaskof748dbc2016-04-05 11:27:47 +0200558struct lyd_node *lyd_new_path(struct lyd_node *data_tree, struct ly_ctx *ctx, const char *path, const char *value,
559 int options);
Michal Vaskof5299282016-03-16 13:32:02 +0100560
561/**
Michal Vaskoc0797f82015-10-14 15:51:25 +0200562 * @brief Create a copy of the specified data tree \p node. Namespaces are copied as needed,
563 * schema references are kept the same.
Michal Vasko2d162e12015-09-24 14:33:29 +0200564 *
565 * @param[in] node Data tree node to be duplicated.
566 * @param[in] recursive 1 if all children are supposed to be also duplicated.
567 * @return Created copy of the provided data \p node.
568 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100569struct lyd_node *lyd_dup(const struct lyd_node *node, int recursive);
Michal Vasko2d162e12015-09-24 14:33:29 +0200570
571/**
Michal Vasko45fb2822016-04-18 13:32:17 +0200572 * @brief Merge a (sub)tree into a data tree. Missing nodes are merged, leaf values updated.
573 * If \p target and \p source do not share the top-level schema node, even if they
574 * are from different modules, \p source parents up to top-level node will be created and
575 * linked to the \p target (but only containers can be created this way, lists need keys,
576 * so if lists are missing, an error will be returned).
577 *
578 * In short, this function will always try to return a fully valid data tree and will fail
Michal Vaskocf6dc7e2016-04-18 16:00:37 +0200579 * if it is not possible. Also, in some less common cases, despite both trees \p target and
580 * \p source are valid, the resulting tree may be invalid and this function will succeed.
581 * If you know there are such possibilities in your data trees or you are not sure, always
582 * validate the resulting merged \p target tree.
Michal Vasko45fb2822016-04-18 13:32:17 +0200583 *
Michal Vaskocf6dc7e2016-04-18 16:00:37 +0200584 * @param[in] target Top-level (or an RPC output child) data tree to merge to. Must be valid.
Michal Vasko45fb2822016-04-18 13:32:17 +0200585 * @param[in] source Data tree to merge \p target with. Must be valid (at least as a subtree).
586 * @param[in] options Bitmask of 2 option flags:
587 * LYD_OPT_DESTRUCT - spend \p source in the function, otherwise \p source is left untouched,
588 * LYD_OPT_NOSIBLINGS - merge only the \p source subtree (ignore siblings), otherwise merge
589 * \p source and all its succeeding siblings (preceeding ones are still ignored!).
590 * @return 0 on success, nonzero in case of an error.
591 */
592int lyd_merge(struct lyd_node *target, const struct lyd_node *source, int options);
593
594/**
Michal Vasko2d162e12015-09-24 14:33:29 +0200595 * @brief Insert the \p node element as child to the \p parent element. The \p node is inserted as a last child of the
596 * \p parent.
597 *
598 * If the node is part of some other tree, it is automatically unlinked.
599 * If the node is the first node of a node list (with no parent), all
600 * the subsequent nodes are also inserted.
601 *
602 * @param[in] parent Parent node for the \p node being inserted.
603 * @param[in] node The node being inserted.
Michal Vasko24337392015-10-16 09:58:16 +0200604 * @return 0 on success, nonzero in case of error, e.g. when the node is being inserted to an inappropriate place
Michal Vasko2d162e12015-09-24 14:33:29 +0200605 * in the data tree.
606 */
Michal Vasko24337392015-10-16 09:58:16 +0200607int lyd_insert(struct lyd_node *parent, struct lyd_node *node);
Michal Vasko2d162e12015-09-24 14:33:29 +0200608
609/**
Michal Vasko3f7dba12015-10-15 13:09:27 +0200610 * @brief Insert the \p node element after the \p sibling element. If \p node and \p siblings are already
Radek Krejcica7efb72016-01-18 13:06:01 +0100611 * siblings (just moving \p node position), skip validation.
Michal Vasko2d162e12015-09-24 14:33:29 +0200612 *
Michal Vasko2d162e12015-09-24 14:33:29 +0200613 * @param[in] sibling The data tree node before which the \p node will be inserted.
Radek Krejci20a5f292016-02-09 15:04:49 +0100614 * @param[in] node The data tree node to be inserted. If the node is connected somewhere, it is unlinked first.
Michal Vasko24337392015-10-16 09:58:16 +0200615 * @return 0 on success, nonzero in case of error, e.g. when the node is being inserted to an inappropriate place
Michal Vasko2d162e12015-09-24 14:33:29 +0200616 * in the data tree.
617 */
Michal Vasko24337392015-10-16 09:58:16 +0200618int lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node);
Michal Vasko2d162e12015-09-24 14:33:29 +0200619
620/**
Radek Krejci20a5f292016-02-09 15:04:49 +0100621 * @brief Insert the \p node element after the \p sibling element. If \p node and \p siblings are already
622 * siblings (just moving \p node position), skip validation.
Michal Vasko2d162e12015-09-24 14:33:29 +0200623 *
Michal Vasko3f7dba12015-10-15 13:09:27 +0200624 * @param[in] sibling The data tree node before which the \p node will be inserted. If \p node and \p siblings
Radek Krejcica7efb72016-01-18 13:06:01 +0100625 * are already siblings (just moving \p node position), skip validation.
Radek Krejci20a5f292016-02-09 15:04:49 +0100626 * @param[in] node The data tree node to be inserted. If the node is connected somewhere, it is unlinked first.
Michal Vasko24337392015-10-16 09:58:16 +0200627 * @return 0 on success, nonzero in case of error, e.g. when the node is being inserted to an inappropriate place
Michal Vasko2d162e12015-09-24 14:33:29 +0200628 * in the data tree.
629 */
Michal Vasko24337392015-10-16 09:58:16 +0200630int lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node);
631
632/**
Michal Vasko2411b942016-03-23 13:50:03 +0100633 * @brief Order siblings according to the schema node ordering.
634 *
Michal Vasko58f74f12016-03-24 13:26:06 +0100635 * If the siblings include data nodes from other modules, they are
636 * sorted based on the module order in the context.
637 *
638 * @param[in] sibling Node, whose siblings will be sorted.
639 * @param[in] recursive Whether sort all siblings of siblings, recursively.
640 * @return 0 on success, nonzero in case of an error.
Michal Vasko2411b942016-03-23 13:50:03 +0100641 */
Michal Vasko58f74f12016-03-24 13:26:06 +0100642int lyd_schema_sort(struct lyd_node *sibling, int recursive);
Michal Vasko2411b942016-03-23 13:50:03 +0100643
644/**
Michal Vasko105cef12016-02-04 12:06:26 +0100645 * @brief Search in the given data for instances of nodes matching the provided XPath expression.
646 *
647 * The \p data is used to find the data root and function then searches in the whole tree and all sibling trees.
Michal Vasko7fdf9b32016-03-01 15:59:48 +0100648 * The XPath expression is evaluated on data -> skip all non-data nodes (input, output, choice, case).
Michal Vasko105cef12016-02-04 12:06:26 +0100649 *
Michal Vasko7fdf9b32016-03-01 15:59:48 +0100650 * Expr examples:
651 * "/ietf-yang-library:modules-state/module[name = 'ietf-yang-library']/namespace"
652 * "/ietf-netconf:get-config/source"
653 *
654 * @param[in] data Node in the data tree considered the context node. If the node is a configuration one,
Michal Vasko105cef12016-02-04 12:06:26 +0100655 * any state nodes in its tree are not accessible!
656 * @param[in] expr XPath expression filtering the matching nodes.
657 * @return Set of found data nodes (use dset member of ::ly_set). If no nodes are matching \p expr or the result
658 * would be a number, a string, or a boolean, the returned set is empty. In case of an error, NULL is returned.
659 */
660struct ly_set *lyd_get_node(const struct lyd_node *data, const char *expr);
661
662/**
Radek Krejcic5b6b912016-01-18 16:35:35 +0100663 * @brief Search in the given data for instances of the provided schema node.
664 *
665 * The \p data is used to find the data root and function then searches in the whole tree and all sibling trees.
666 *
667 * @param[in] data A node in the data tree to search.
668 * @param[in] schema Schema node of the data nodes caller want to find.
Radek Krejci2342cf62016-01-29 16:48:23 +0100669 * @return Set of found data nodes (use dset member of ::ly_set). If no data node is found, the returned set is empty.
Radek Krejcic5b6b912016-01-18 16:35:35 +0100670 * In case of error, NULL is returned.
671 */
Michal Vasko105cef12016-02-04 12:06:26 +0100672struct ly_set *lyd_get_node2(const struct lyd_node *data, const struct lys_node *schema);
Radek Krejcic5b6b912016-01-18 16:35:35 +0100673
674/**
Michal Vasko24337392015-10-16 09:58:16 +0200675 * @brief Validate \p node data subtree.
676 *
Michal Vaskodedea832016-04-19 11:24:45 +0200677 * @param[in,out] node Data tree to be validated. In case the \p options does not includes #LYD_OPT_NOAUTODEL, libyang
Radek Krejci4e941112016-03-23 10:44:30 +0100678 * can modify the provided tree including the root \p node.
Michal Vasko24337392015-10-16 09:58:16 +0200679 * @param[in] options Options for the inserting data to the target data tree options, see @ref parseroptions.
Michal Vaskodedea832016-04-19 11:24:45 +0200680 * @param[in] ... if \p options include #LYD_OPT_RPCREPLY then RPC schema node of the data (unsed ony when \p node is NULL),
681 * otherwise libyang context for the data (used only in case the \p node is NULL, so in case of checking empty data tree).
Radek Krejci92ece002016-04-04 15:45:05 +0200682 * @return 0 on success, nonzero in case of an error.
Michal Vasko24337392015-10-16 09:58:16 +0200683 */
Radek Krejci03b71f72016-03-16 11:10:09 +0100684int lyd_validate(struct lyd_node **node, int options, ...);
Michal Vasko2d162e12015-09-24 14:33:29 +0200685
686/**
Radek Krejci7b4309c2016-03-23 10:30:29 +0100687 * @brief Add default nodes into the data tree.
688 *
Radek Krejci8100ca22016-03-29 10:32:58 +0200689 * The function expects that the provided data tree is valid. If not, the result is undefined - in general, the
690 * result is not more invalid than the provided data tree input, so if the input data tree is invalid, result will
691 * be also invalid and the process of adding default values could be incomplete.
692 *
693 * Since default nodes are also needed by the validation process, to optimize your application you can add default
694 * values directly in lyd_validate() and lyd_parse*() functions using appropriate options value. By default, these
Michal Vaskod88863c2016-05-04 13:17:44 +0200695 * functions remove the default nodes at the end of their processing.
Radek Krejci8100ca22016-03-29 10:32:58 +0200696 *
Michal Vaskod88863c2016-05-04 13:17:44 +0200697 * \p ctx parameter and \p options with #LYD_OPT_NOSIBLINGS values can result in 4 different scenarios:
698 *
699 * - If \p ctx is set and \p options include #LYD_OPT_NOSIBLINGS, tree default nodes will be added ONLY to \p root,
700 * top-level default nodes will be added from ALL the modules (so it has no effect for #LYD_WD_TRIM).
701 * - If \p ctx is set and \p options do not include #LYD_OPT_NOSIBLINGS, tree default values will be added to ALL
702 * the \p root siblings, top-level nodes will be added from ALL the modules.
703 * - If \p ctx is NULL and \p options include #LYD_OPT_NOSIBLINGS, tree default nodes will be added ONLY to \p root,
704 * top-level default nodes will be added ONLY from the module of \p root.
705 * - If \p ctx is NULL and \p options do not include #LYD_OPT_NOSIBLINGS, tree default nodes will be added to ALL
706 * the \p root siblings, top-level default nodes will be added from ALL the \p root siblings modules.
707 *
708 * @param[in] ctx Optional parameter. Exact meaning described in this function description last paragraph.
Radek Krejci7b4309c2016-03-23 10:30:29 +0100709 * @param[in] root Data tree root. In case of #LYD_WD_TRIM the data tree can be modified so the root can be changed or
710 * removed. In other modes and with empty data tree, new default nodes can be created so the root pointer
711 * will contain/return the newly created data tree.
Radek Krejci8100ca22016-03-29 10:32:58 +0200712 * @param[in] options Options for the inserting data to the target data tree options, see @ref parseroptions - only the
Radek Krejci7b4309c2016-03-23 10:30:29 +0100713 * LYD_WD_* options are used to select functionality:
714 * - #LYD_WD_TRIM - remove all nodes that have value equal to their default value
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200715 * - #LYD_WD_EXPLICIT - add only status default nodes
716 * - #LYD_WD_ALL - add all (status as well as config) default nodes
Radek Krejci7b4309c2016-03-23 10:30:29 +0100717 * - #LYD_WD_ALL_TAG - add default nodes and add attribute 'default' with value 'true' to all nodes having their default value
718 * - #LYD_WD_IMPL_TAG - add default nodes, but add attribute 'default' only to the added nodes
Radek Krejci8100ca22016-03-29 10:32:58 +0200719 * @note The LYD_WD_*_TAG modes require to have ietf-netconf-with-defaults module in the context of the data tree.
Radek Krejci1eefeb32016-04-15 16:01:46 +0200720 * @note If you already added some tagged default nodes (by parser or previous call of lyd_validate()), you should
721 * specify the same LYD_WD_*_TAG in all subsequent call to lyd_validate(). Otherwise, the tagged nodes will be removed.
Radek Krejci7b4309c2016-03-23 10:30:29 +0100722 * @return EXIT_SUCCESS ot EXIT_FAILURE
723 */
724int lyd_wd_add(struct ly_ctx *ctx, struct lyd_node **root, int options);
725
726/**
Radek Krejci1eefeb32016-04-15 16:01:46 +0200727 * @brief Remove all default nodes, respectively all nodes with set ::lyd_node#dflt added by
728 * #LYD_WD_ALL_TAG or #LYD_WD_IMPL_TAG options in lyd_wd_add(), lyd_validate() or lyd_parse_*() functions.
Radek Krejci6b8f6ac2016-03-23 12:33:04 +0100729 *
730 * @param[in] root Data tree root. The data tree can be modified so the root can be changed or completely removed.
Radek Krejci74150cd2016-03-29 15:53:16 +0200731 * @param[in] options Options for the inserting data to the target data tree options, see @ref parseroptions.
Radek Krejci1eefeb32016-04-15 16:01:46 +0200732 * If #LYD_WD_EXPLICIT is found in options, the default status nodes are kept, so it is better to
733 * erase all LYD_WD_* flags from the value.
Radek Krejci6b8f6ac2016-03-23 12:33:04 +0100734 * @return EXIT_SUCCESS or EXIT_FAILURE
735 */
Radek Krejci74150cd2016-03-29 15:53:16 +0200736int lyd_wd_cleanup(struct lyd_node **root, int options);
Radek Krejci6b8f6ac2016-03-23 12:33:04 +0100737
738/**
Michal Vasko55f60be2015-10-14 13:12:58 +0200739 * @brief Unlink the specified data subtree. All referenced namespaces are copied.
Michal Vasko2d162e12015-09-24 14:33:29 +0200740 *
741 * Note, that the node's connection with the schema tree is kept. Therefore, in case of
742 * reconnecting the node to a data tree using lyd_paste() it is necessary to paste it
743 * to the appropriate place in the data tree following the schema.
744 *
745 * @param[in] node Data tree node to be unlinked (together with all children).
746 * @return 0 for success, nonzero for error
747 */
748int lyd_unlink(struct lyd_node *node);
749
750/**
751 * @brief Free (and unlink) the specified data (sub)tree.
752 *
753 * @param[in] node Root of the (sub)tree to be freed.
754 */
755void lyd_free(struct lyd_node *node);
756
757/**
Radek Krejci81468402016-01-07 13:52:40 +0100758 * @brief Free (and unlink) the specified data (sub)tree and all its siblings (preceding as well as following).
759 *
760 * @param[in] node One of the siblings root element of the (sub)trees to be freed.
761 */
762void lyd_free_withsiblings(struct lyd_node *node);
763
764/**
Radek Krejci134610e2015-10-20 17:15:34 +0200765 * @brief Insert attribute into the data node.
766 *
767 * @param[in] parent Data node where to place the attribute
Radek Krejci70ecd722016-03-21 09:04:00 +0100768 * @param[in] mod An alternative way to specify attribute's module (namespace) used in case the \p name does
769 * not include prefix. If neither prefix in the \p name nor mod is specified, the attribute's
770 * module is inherited from the \p parent node. It is not allowed to have attributes with no
771 * module (namespace).
772 * @param[in] name Attribute name. The string can include the attribute's module (namespace) as the name's
773 * prefix (prefix:name). Prefix must be the name of one of the schema in the \p parent's context.
774 * If the prefix is not specified, the \p mod parameter is used. If neither of these parameters is
775 * usable, attribute inherits module (namespace) from the \p parent node. It is not allowed to
776 * have attributes with no module (namespace).
Radek Krejci134610e2015-10-20 17:15:34 +0200777 * @param[in] value Attribute value
778 * @return pointer to the created attribute (which is already connected in \p parent) or NULL on error.
779 */
Radek Krejci70ecd722016-03-21 09:04:00 +0100780struct lyd_attr *lyd_insert_attr(struct lyd_node *parent, const struct lys_module *mod, const char *name,
781 const char *value);
Radek Krejci134610e2015-10-20 17:15:34 +0200782
783/**
Radek Krejci88f29302015-10-30 15:42:33 +0100784 * @brief Destroy data attribute
785 *
786 * If the attribute to destroy is a member of a node attribute list, it is necessary to
787 * provide the node itself as \p parent to keep the list consistent.
788 *
789 * @param[in] ctx Context where the attribute was created (usually it is the context of the \p parent)
790 * @param[in] parent Parent node where the attribute is placed
791 * @param[in] attr Attribute to destroy
792 * @param[in] recursive Zero to destroy only the attribute, non-zero to destroy also all the subsequent attributes
793 * in the list.
794 */
795void lyd_free_attr(struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_attr *attr, int recursive);
796
797/**
Radek Krejci6910a032016-04-13 10:06:21 +0200798 * @brief Return main module of the data tree node.
799 *
800 * In case of regular YANG module, it returns ::lys_node#module pointer,
801 * but in case of submodule, it returns pointer to the main module.
802 *
803 * @param[in] node Data tree node to be examined
804 * @return pointer to the main module (schema structure), NULL in case of error.
805 */
806struct lys_module *lyd_node_module(const struct lyd_node *node);
807
808/**
Radek Krejcidef50022016-02-01 16:38:32 +0100809* @brief Print data tree in the specified format.
810*
811* Same as lyd_print(), but it allocates memory and store the data into it.
812* It is up to caller to free the returned string by free().
813*
814* @param[out] strp Pointer to store the resulting dump.
815* @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
816* node of the data tree to print the specific subtree.
817* @param[in] format Data output format.
818* @param[in] options [printer flags](@ref printerflags).
819* @return 0 on success, 1 on failure (#ly_errno is set).
820*/
821int lyd_print_mem(char **strp, const struct lyd_node *root, LYD_FORMAT format, int options);
Michal Vasko2d162e12015-09-24 14:33:29 +0200822
823/**
Radek Krejcidef50022016-02-01 16:38:32 +0100824 * @brief Print data tree in the specified format.
Michal Vasko2d162e12015-09-24 14:33:29 +0200825 *
Radek Krejcidef50022016-02-01 16:38:32 +0100826 * Same as lyd_print(), but output is written into the specified file descriptor.
827 *
828 * @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
829 * node of the data tree to print the specific subtree.
830 * @param[in] fd File descriptor where to print the data.
831 * @param[in] format Data output format.
832 * @param[in] options [printer flags](@ref printerflags).
833 * @return 0 on success, 1 on failure (#ly_errno is set).
Michal Vasko2d162e12015-09-24 14:33:29 +0200834 */
Radek Krejcidef50022016-02-01 16:38:32 +0100835int lyd_print_fd(int fd, const struct lyd_node *root, LYD_FORMAT format, int options);
836
837/**
838 * @brief Print data tree in the specified format.
839 *
840 * To write data into a file descriptor, use lyd_print_fd().
841 *
842 * @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
843 * node of the data tree to print the specific subtree.
844 * @param[in] f File stream where to print the data.
845 * @param[in] format Data output format.
846 * @param[in] options [printer flags](@ref printerflags).
847 * @return 0 on success, 1 on failure (#ly_errno is set).
848 */
849int lyd_print_file(FILE *f, const struct lyd_node *root, LYD_FORMAT format, int options);
850
851/**
852 * @brief Print data tree in the specified format.
853 *
854 * Same as lyd_print(), but output is written via provided callback.
855 *
856 * @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
857 * node of the data tree to print the specific subtree.
858 * @param[in] writeclb Callback function to write the data (see write(1)).
859 * @param[in] arg Optional caller-specific argument to be passed to the \p writeclb callback.
860 * @param[in] format Data output format.
861 * @param[in] options [printer flags](@ref printerflags).
862 * @return 0 on success, 1 on failure (#ly_errno is set).
863 */
864int lyd_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg,
865 const struct lyd_node *root, LYD_FORMAT format, int options);
Michal Vasko2d162e12015-09-24 14:33:29 +0200866
Michal Vasko2d162e12015-09-24 14:33:29 +0200867/**@} */
868
869#ifdef __cplusplus
870}
871#endif
872
873#endif /* LY_TREE_DATA_H_ */