blob: 952fe383a1b9245d5b6c9a2be3e926ace1408c45 [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 */
Radek Krejci452fb952015-10-02 16:07:46 +020042 LYD_XML_FORMAT, /**< For input data, it is interchangeable with #LYD_XML, for output it formats XML with indentantion */
Michal Vasko2d162e12015-09-24 14:33:29 +020043 LYD_JSON, /**< JSON format of the instance data */
44} LYD_FORMAT;
45
46/**
Michal Vasko2d162e12015-09-24 14:33:29 +020047 * @brief Attribute structure.
48 *
Radek Krejci5f9e8c92015-10-30 10:01:06 +010049 * The structure provides information about attributes of a data element. Such attributes partially
50 * maps to annotations from draft-ietf-netmod-yang-metadata. In XML, they are represented as standard
51 * XML attrbutes. In JSON, they are represented as JSON elements starting with the '@' character
52 * (for more information, see the yang metadata draft.
53 *
Michal Vasko2d162e12015-09-24 14:33:29 +020054 */
55struct lyd_attr {
Radek Krejci5f9e8c92015-10-30 10:01:06 +010056 struct lyd_attr *next; /**< pointer to the next attribute of the same element */
57 struct lys_module *module; /**< pointer to the attribute's module.
58 TODO when annotations will be supported, point to the annotation definition
59 and validate that the attribute is really defined there. Currently, we just
60 believe that it is defined in the module it says */
Michal Vasko2d162e12015-09-24 14:33:29 +020061 const char *name; /**< attribute name */
62 const char *value; /**< attribute value */
63};
64
65/**
66 * @brief node's value representation
67 */
68typedef union lyd_value_u {
69 const char *binary; /**< base64 encoded, NULL terminated string */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +020070 struct lys_type_bit **bit; /**< bitmap of pointers to the schema definition of the bit value that are set,
71 its size is always the number of defined bits in the schema */
Radek Krejci489773c2015-12-17 13:20:03 +010072 int8_t bln; /**< 0 as false, 1 as true */
Michal Vasko2d162e12015-09-24 14:33:29 +020073 int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */
74 struct lys_type_enum *enm; /**< pointer to the schema definition of the enumeration value */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +020075 struct lys_ident *ident; /**< pointer to the schema definition of the identityref value */
Radek Krejci40f17b92016-02-03 14:30:43 +010076 struct lyd_node *instance; /**< pointer to the instance-identifier target, note that if the tree was modified,
77 the target (address) can be invalid - the pointer is correctly checked and updated
78 by lyd_validate() */
Michal Vasko2d162e12015-09-24 14:33:29 +020079 int8_t int8; /**< 8-bit signed integer */
80 int16_t int16; /**< 16-bit signed integer */
81 int32_t int32; /**< 32-bit signed integer */
82 int64_t int64; /**< 64-bit signed integer */
83 struct lyd_node *leafref; /**< pointer to the referenced leaf/leaflist instance in data tree */
84 const char *string; /**< string */
85 uint8_t uint8; /**< 8-bit unsigned integer */
86 uint16_t uint16; /**< 16-bit signed integer */
87 uint32_t uint32; /**< 32-bit signed integer */
88 uint64_t uint64; /**< 64-bit signed integer */
89} lyd_val;
90
91/**
Radek Krejcica7efb72016-01-18 13:06:01 +010092 * @defgroup validityflags Validity flags
93 * @ingroup datatree
94 *
95 * Validity flags for data nodes.
96 *
97 * @{
98 */
99#define LYD_VAL_OK 0x00 /**< node is successfully validated including whole subtree */
100#define LYD_VAL_UNIQUE 0x01 /**< Unique value(s) changed, applicable only to ::lys_node_list data nodes */
Radek Krejci0b7704f2016-03-18 12:16:14 +0100101#define LYD_VAL_NOT 0x1f /**< node was not validated yet */
Radek Krejcica7efb72016-01-18 13:06:01 +0100102/**
103 * @}
104 */
105
106/**
Michal Vasko2d162e12015-09-24 14:33:29 +0200107 * @brief Generic structure for a data node, directly applicable to the data nodes defined as #LYS_CONTAINER, #LYS_LIST
108 * and #LYS_CHOICE.
109 *
110 * Completely fits to containers and choices and is compatible (can be used interchangeably except the #child member)
111 * with all other lyd_node_* structures. All data nodes are provides as ::lyd_node structure by default.
112 * 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 +0100113 * ::lyd_node_leaf_list or ::lyd_node_anyxml structures. This structure fits only to #LYS_CONTAINER, #LYS_LIST and
114 * #LYS_CHOICE values.
Michal Vasko2d162e12015-09-24 14:33:29 +0200115 *
116 * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro.
117 */
118struct lyd_node {
119 struct lys_node *schema; /**< pointer to the schema definition of this node */
Radek Krejci0b7704f2016-03-18 12:16:14 +0100120 uint8_t validity:5; /**< [validity flags](@ref validityflags) */
121 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 Krejci0b7704f2016-03-18 12:16:14 +0100151 uint8_t validity:5; /**< [validity flags](@ref validityflags) */
152 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 +0100153 do not use this value! */
Michal Vasko2d162e12015-09-24 14:33:29 +0200154
155 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
156 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
157 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
158 never NULL. If there is no sibling node, pointer points to the node
159 itself. In case of the first node, this pointer points to the last
160 node in the list. */
161 struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
162
163 /* struct lyd_node *child; should be here, but is not */
164
165 /* leaflist's specific members */
Michal Vasko2d162e12015-09-24 14:33:29 +0200166 const char *value_str; /**< string representation of value (for comparison, printing,...) */
Radek Krejci23238922015-10-27 17:13:34 +0100167 lyd_val value; /**< node's value representation */
Michal Vasko2d162e12015-09-24 14:33:29 +0200168 LY_DATA_TYPE value_type; /**< type of the value in the node, mainly for union to avoid repeating of type detection */
169};
170
171/**
172 * @brief Structure for data nodes defined as #LYS_ANYXML.
173 *
174 * Extension for ::lyd_node structure - replaces the ::lyd_node#child member by new #value member. The first five
175 * members (#schema, #attr, #next, #prev and #parent) are compatible with the ::lyd_node's members.
176 *
177 * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro.
178 */
179struct lyd_node_anyxml {
180 struct lys_node *schema; /**< pointer to the schema definition of this node which is ::lys_node_anyxml
181 structure */
Radek Krejci0b7704f2016-03-18 12:16:14 +0100182 uint8_t validity:5; /**< [validity flags](@ref validityflags) */
183 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 +0100184 do not use this value! */
Michal Vasko2d162e12015-09-24 14:33:29 +0200185
186 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
187 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
188 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
189 never NULL. If there is no sibling node, pointer points to the node
190 itself. In case of the first node, this pointer points to the last
191 node in the list. */
192 struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
193
194 /* struct lyd_node *child; should be here, but is not */
195
196 /* anyxml's specific members */
197 struct lyxml_elem *value; /**< anyxml name is the root element of value! */
198};
199
200/**
Radek Krejcidef50022016-02-01 16:38:32 +0100201 * @defgroup parseroptions Data parser options
202 * @ingroup datatree
203 *
204 * Various options to change the data tree parsers behavior.
205 *
206 * Default behavior:
207 * - in case of XML, parser reads all data from its input (file, memory, XML tree) including the case of not well-formed
208 * XML document (multiple top-level elements) and if there is an unknown element, it is skipped including its subtree
209 * (see the next point). This can be changed by the #LYD_OPT_NOSIBLINGS option which make parser to read only a single
210 * tree (with a single root element) from its input.
211 * - parser silently ignores the data without a matching node in schema trees. If the caller want to stop
212 * parsing in case of presence of unknown data, the #LYD_OPT_STRICT can be used. The strict mode is useful for
213 * NETCONF servers, since NETCONF clients should always send data according to the capabilities announced by the server.
214 * On the other hand, the default non-strict mode is useful for clients receiving data from NETCONF server since
215 * clients are not required to understand everything the server does. Of course, the optimal strategy for clients is
216 * to use filtering to get only the required data. Having an unknown element of the known namespace is always an error.
217 * The behavior can be changed by #LYD_OPT_STRICT option.
218 * - using obsolete statements (status set to obsolete) just generates a warning, but the processing continues. The
219 * behavior can be changed by #LYD_OPT_OBSOLETE option.
220 * - parser expects that the provided data provides complete datastore content (both the configuration and state data)
221 * and performs data validation according to all YANG rules. This can be a problem in case of representing NETCONF's
222 * subtree filter data, edit-config's data or other type of data set - such data do not represent a complete data set
223 * and some of the validation rules can fail. Therefore there are other options (within lower 8 bits) to make parser
224 * to accept such a data.
225 * @{
226 */
227
228#define LYD_OPT_DATA 0x00 /**< Default type of data - complete datastore content with configuration as well as
229 state data. */
230#define LYD_OPT_CONFIG 0x01 /**< A configuration datastore - complete datastore without state data.
231 Validation modifications:
232 - status data are not allowed */
233#define LYD_OPT_GET 0x02 /**< Data content from a NETCONF reply message to the NETCONF \<get\> operation.
234 Validation modifications:
235 - mandatory nodes can be omitted
236 - leafrefs and instance-identifier are not resolved
237 - list's keys/unique nodes are not required (so duplication is not checked) */
238#define LYD_OPT_GETCONFIG 0x04 /**< Data content from a NETCONF reply message to the NETCONF \<get-config\> operation
239 Validation modifications:
240 - mandatory nodes can be omitted
241 - leafrefs and instance-identifier are not resolved
242 - list's keys/unique nodes are not required (so duplication is not checked)
243 - status data are not allowed */
244#define LYD_OPT_EDIT 0x08 /**< Content of the NETCONF \<edit-config\>'s config element.
245 Validation modifications:
246 - mandatory nodes can be omitted
247 - leafrefs and instance-identifier are not resolved
248 - status data are not allowed */
249#define LYD_OPT_RPC 0x10 /**< Data represents RPC's input parameters. */
250#define LYD_OPT_RPCREPLY 0x20 /**< Data represents RPC's output parameters (maps to NETCONF <rpc-reply> data). */
251#define LYD_OPT_NOTIF 0x40 /**< Data represents an event notification data. */
252#define LYD_OPT_FILTER 0x80 /**< Data represents NETCONF subtree filter. Validation modifications:
253 - leafs/leaf-lists with no data are allowed (even not allowed e.g. by length restriction)
254 - multiple instances of container/leaf/.. are allowed
255 - list's keys/unique nodes are not required
256 - mandatory nodes can be omitted
257 - leafrefs and instance-identifier are not resolved
258 - data from different choice's branches are allowed */
259#define LYD_OPT_TYPEMASK 0xff /**< Mask to filter data type options. Always only a single data type option (only
260 single bit from the lower 8 bits) can be set. */
261
262#define LYD_OPT_STRICT 0x0100 /**< Instead of silent ignoring data without schema definition, raise an error. */
263#define LYD_OPT_DESTRUCT 0x0200 /**< Free the provided XML tree during parsing the data. With this option, the
264 provided XML tree is affected and all succesfully parsed data are freed.
265 This option is applicable only to lyd_parse_xml() function. */
266#define LYD_OPT_OBSOLETE 0x0400 /**< Raise an error when an obsolete statement (status set to obsolete) is used. */
267#define LYD_OPT_NOSIBLINGS 0x0800 /**< Parse only a single XML tree from the input. This option applies only to
268 XML input data. */
Radek Krejci93fab982016-02-03 15:58:19 +0100269#define LYD_OPT_TRUSTED 0x1000 /**< Data comes from a trusted source and it is not needed to validate them. Data
270 are connected with the schema, but the most validation checks (mandatory nodes,
271 list instance uniqueness, etc.) are not performed. This option does not make
272 sense for lyd_validate() so it is ignored by this function. */
Radek Krejci03b71f72016-03-16 11:10:09 +0100273#define LYD_OPT_NOAUTODEL 0x2000 /**< Avoid automatic delete of subtrees with false when-stmt condition. The flag is
274 applicable only in combination with LYD_OPT_DATA and LYD_OPT_CONFIG flags.
275 If used, libyang generates validation error instead of silently removing the
276 constrained subtree. */
Radek Krejcidef50022016-02-01 16:38:32 +0100277
278/**@} parseroptions */
279
280/**
281 * @brief Parse (and validate according to appropriate schema from the given context) data.
282 *
283 * In case of LY_XML format, the data string is parsed completely. It means that when it contains
284 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
285 * returned data node is a root of the first tree with other trees connected via the next pointer.
286 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
287 *
288 * @param[in] ctx Context to connect with the data tree being built here.
289 * @param[in] data Serialized data in the specified format.
290 * @param[in] format Format of the input data to be parsed.
291 * @param[in] options Parser options, see @ref parseroptions.
292 * @param[in] ... Additional argument must be supplied when #LYD_OPT_RPCREPLY value is specified in \p options. The
293 * argument is supposed to provide pointer to the RPC schema node for the reply's request
294 * (const struct ::lys_node* rpc).
295 * @return Pointer to the built data tree or NULL in case of empty \p data. To free the returned structure,
296 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
297 * #ly_errno contains appropriate error code (see #LY_ERR).
298 */
Radek Krejci722b0072016-02-01 17:09:45 +0100299struct lyd_node *lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, ...);
Radek Krejcidef50022016-02-01 16:38:32 +0100300
301/**
302 * @brief Read data from the given file descriptor.
303 *
304 * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc.
305 *
306 * In case of LY_XML format, the file content is parsed completely. It means that when it contains
307 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
308 * returned data node is a root of the first tree with other trees connected via the next pointer.
309 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
310 *
311 * @param[in] ctx Context to connect with the data tree being built here.
312 * @param[in] fd The standard file descriptor of the file containing the data tree in the specified format.
313 * @param[in] format Format of the input data to be parsed.
314 * @param[in] options Parser options, see @ref parseroptions.
315 * @param[in] ... Additional argument must be supplied when #LYD_OPT_RPCREPLY value is specified in \p options. The
316 * argument is supposed to provide pointer to the RPC schema node for the reply's request
317 * (const struct ::lys_node* rpc).
318 * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure,
319 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
320 * #ly_errno contains appropriate error code (see #LY_ERR).
321 */
322struct lyd_node *lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, ...);
323
324/**
325 * @brief Read data from the given file path.
326 *
327 * In case of LY_XML format, the file content is parsed completely. It means that when it contains
328 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
329 * returned data node is a root of the first tree with other trees connected via the next pointer.
330 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
331 *
332 * @param[in] ctx Context to connect with the data tree being built here.
333 * @param[in] path Path to the file containing the data tree in the specified format.
334 * @param[in] format Format of the input data to be parsed.
335 * @param[in] options Parser options, see @ref parseroptions.
336 * @param[in] ... Additional argument must be supplied when #LYD_OPT_RPCREPLY value is specified in \p options. The
337 * argument is supposed to provide pointer to the RPC schema node for the reply's request
338 * (const struct ::lys_node* rpc).
339 * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure,
340 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
341 * #ly_errno contains appropriate error code (see #LY_ERR).
342 */
343struct lyd_node *lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options, ...);
344
345/**
346 * @brief Parse (and validate according to appropriate schema from the given context) XML tree.
347 *
348 * The output data tree is parsed from the given XML tree previously parsed by one of the
349 * lyxml_read* functions.
350 *
Radek Krejci722b0072016-02-01 17:09:45 +0100351 * 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 +0100352 * or the provided root is a root element of a subtree), all the sibling nodes (previous as well as
353 * following) are processed as well. The returned data node is a root of the first tree with other
354 * trees connected via the next pointer. This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
355 *
356 * When the function is used with #LYD_OPT_DESTRUCT, all the successfully parsed data including the
357 * XML \p root and all its siblings (if #LYD_OPT_NOSIBLINGS is not used) are freed. Only with
358 * #LYD_OPT_DESTRUCT option the \p root pointer is changed - if all the data are parsed, it is set
359 * to NULL, otherwise it will hold the XML tree without the successfully parsed elements.
360 *
361 * The context must be the same as the context used to parse XML tree by lyxml_read* function.
362 *
363 * @param[in] ctx Context to connect with the data tree being built here.
364 * @param[in,out] root XML tree to parse (convert) to data tree. By default, parser do not change the XML tree. However,
365 * when #LYD_OPT_DESTRUCT is specified in \p options, parser frees all successfully parsed data.
366 * @param[in] options Parser options, see @ref parseroptions.
367 * @param[in] ... Additional argument must be supplied when #LYD_OPT_RPCREPLY value is specified in \p options. The
368 * argument is supposed to provide pointer to the RPC schema node for the reply's request
369 * (const struct ::lys_node* rpc).
370 * @return Pointer to the built data tree or NULL in case of empty \p root. To free the returned structure,
371 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
372 * #ly_errno contains appropriate error code (see #LY_ERR).
373 */
374struct lyd_node *lyd_parse_xml(struct ly_ctx *ctx, struct lyxml_elem **root, int options,...);
375
376/**
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200377 * @brief Create a new container node in a data tree.
378 *
379 * @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 +0200380 * @param[in] module Module with the node being created.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200381 * @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 +0200382 * #LYS_NOTIF, or #LYS_RPC.
Michal Vasko1dca6882015-10-22 14:29:42 +0200383 * @return New node, NULL on error.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200384 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100385struct lyd_node *lyd_new(struct lyd_node *parent, const struct lys_module *module, const char *name);
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200386
387/**
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200388 * @brief Create a new leaf or leaflist node in a data tree with a string value that is converted to
389 * the actual value.
390 *
391 * @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 +0200392 * @param[in] module Module with the node being created.
393 * @param[in] name Schema node name of the new data node.
Michal Vasko3e671b52015-10-23 16:23:15 +0200394 * @param[in] val_str String form of the value of the node being created. In case the type is #LY_TYPE_INST
395 * 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 +0200396 * @return New node, NULL on error.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200397 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100398struct lyd_node *lyd_new_leaf(struct lyd_node *parent, const struct lys_module *module, const char *name,
Michal Vasko3e671b52015-10-23 16:23:15 +0200399 const char *val_str);
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200400
401/**
Radek Krejcib9b4d002016-01-18 13:08:51 +0100402 * @brief Change value of a leaf node.
403 *
404 * Despite the prototype allows to provide a leaflist node as \p leaf parameter, only leafs are accepted.
405 *
406 * @param[in] leaf A leaf node to change.
407 * @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
408 * or #LY_TYPE_IDENT, JSON node-id format is expected (nodes are prefixed with module names, not XML namespaces).
409 * @return 0 on success, non-zero on error.
410 */
411int lyd_change_leaf(struct lyd_node_leaf_list *leaf, const char *val_str);
412
413/**
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200414 * @brief Create a new anyxml node in a data tree.
Michal Vasko2d162e12015-09-24 14:33:29 +0200415 *
Michal Vasko2d162e12015-09-24 14:33:29 +0200416 * @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 +0200417 * @param[in] module Module with the node being created.
418 * @param[in] name Schema node name of the new data node.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200419 * @param[in] val_xml Value of the node being created. Must be a well-formed XML.
Michal Vasko1dca6882015-10-22 14:29:42 +0200420 * @return New node, NULL on error.
Michal Vasko2d162e12015-09-24 14:33:29 +0200421 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100422struct lyd_node *lyd_new_anyxml(struct lyd_node *parent, const struct lys_module *module, const char *name,
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200423 const char *val_xml);
Michal Vasko2d162e12015-09-24 14:33:29 +0200424
425/**
Michal Vasko50c0a872016-01-13 14:34:11 +0100426 * @brief Create a new container node in a data tree, whose schema parent is #LYS_OUTPUT.
Michal Vasko0df122f2015-12-14 13:38:21 +0100427 *
Michal Vasko50c0a872016-01-13 14:34:11 +0100428 * @param[in] schema Schema node of the container.
Michal Vasko0df122f2015-12-14 13:38:21 +0100429 * @return New node, NULL on error.
430 */
Michal Vasko50c0a872016-01-13 14:34:11 +0100431struct lyd_node *lyd_output_new(const struct lys_node *schema);
432
433/**
434 * @brief Create a new leaf or leaflist node in a data tree, whose schema parent is #LYS_OUTPUT.
435 *
436 * @param[in] schema Schema node of the leaf.
437 * @param[in] val_str String form of the value of the node being created. In case the type is #LY_TYPE_INST
438 * or #LY_TYPE_IDENT, JSON node-id format is expected (nodes are prefixed with module names, not XML namespaces).
439 * @return New node, NULL on error.
440 */
441struct lyd_node *lyd_output_new_leaf(const struct lys_node *schema, const char *val_str);
442
443/**
444 * @brief Create a new anyxml node in a data tree, whose schema parent is #LYS_OUTPUT.
445 *
446 * @param[in] schema Schema node of the leaf.
447 * @param[in] val_xml Value of the node being created. Must be a well-formed XML.
448 * @return New node, NULL on error.
449 */
450struct lyd_node *lyd_output_new_anyxml(const struct lys_node *schema, const char *val_xml);
Michal Vasko0df122f2015-12-14 13:38:21 +0100451
452/**
Michal Vaskoc0797f82015-10-14 15:51:25 +0200453 * @brief Create a copy of the specified data tree \p node. Namespaces are copied as needed,
454 * schema references are kept the same.
Michal Vasko2d162e12015-09-24 14:33:29 +0200455 *
456 * @param[in] node Data tree node to be duplicated.
457 * @param[in] recursive 1 if all children are supposed to be also duplicated.
458 * @return Created copy of the provided data \p node.
459 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100460struct lyd_node *lyd_dup(const struct lyd_node *node, int recursive);
Michal Vasko2d162e12015-09-24 14:33:29 +0200461
462/**
463 * @brief Insert the \p node element as child to the \p parent element. The \p node is inserted as a last child of the
464 * \p parent.
465 *
466 * If the node is part of some other tree, it is automatically unlinked.
467 * If the node is the first node of a node list (with no parent), all
468 * the subsequent nodes are also inserted.
469 *
470 * @param[in] parent Parent node for the \p node being inserted.
471 * @param[in] node The node being inserted.
Michal Vasko24337392015-10-16 09:58:16 +0200472 * @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 +0200473 * in the data tree.
474 */
Michal Vasko24337392015-10-16 09:58:16 +0200475int lyd_insert(struct lyd_node *parent, struct lyd_node *node);
Michal Vasko2d162e12015-09-24 14:33:29 +0200476
477/**
Michal Vasko3f7dba12015-10-15 13:09:27 +0200478 * @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 +0100479 * siblings (just moving \p node position), skip validation.
Michal Vasko2d162e12015-09-24 14:33:29 +0200480 *
Michal Vasko2d162e12015-09-24 14:33:29 +0200481 * @param[in] sibling The data tree node before which the \p node will be inserted.
Radek Krejci20a5f292016-02-09 15:04:49 +0100482 * @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 +0200483 * @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 +0200484 * in the data tree.
485 */
Michal Vasko24337392015-10-16 09:58:16 +0200486int lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node);
Michal Vasko2d162e12015-09-24 14:33:29 +0200487
488/**
Radek Krejci20a5f292016-02-09 15:04:49 +0100489 * @brief Insert the \p node element after the \p sibling element. If \p node and \p siblings are already
490 * siblings (just moving \p node position), skip validation.
Michal Vasko2d162e12015-09-24 14:33:29 +0200491 *
Michal Vasko3f7dba12015-10-15 13:09:27 +0200492 * @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 +0100493 * are already siblings (just moving \p node position), skip validation.
Radek Krejci20a5f292016-02-09 15:04:49 +0100494 * @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 +0200495 * @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 +0200496 * in the data tree.
497 */
Michal Vasko24337392015-10-16 09:58:16 +0200498int lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node);
499
500/**
Michal Vasko105cef12016-02-04 12:06:26 +0100501 * @brief Search in the given data for instances of nodes matching the provided XPath expression.
502 *
503 * 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 +0100504 * The XPath expression is evaluated on data -> skip all non-data nodes (input, output, choice, case).
Michal Vasko105cef12016-02-04 12:06:26 +0100505 *
Michal Vasko7fdf9b32016-03-01 15:59:48 +0100506 * Expr examples:
507 * "/ietf-yang-library:modules-state/module[name = 'ietf-yang-library']/namespace"
508 * "/ietf-netconf:get-config/source"
509 *
510 * @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 +0100511 * any state nodes in its tree are not accessible!
512 * @param[in] expr XPath expression filtering the matching nodes.
513 * @return Set of found data nodes (use dset member of ::ly_set). If no nodes are matching \p expr or the result
514 * would be a number, a string, or a boolean, the returned set is empty. In case of an error, NULL is returned.
515 */
516struct ly_set *lyd_get_node(const struct lyd_node *data, const char *expr);
517
518/**
Radek Krejcic5b6b912016-01-18 16:35:35 +0100519 * @brief Search in the given data for instances of the provided schema node.
520 *
521 * The \p data is used to find the data root and function then searches in the whole tree and all sibling trees.
522 *
523 * @param[in] data A node in the data tree to search.
524 * @param[in] schema Schema node of the data nodes caller want to find.
Radek Krejci2342cf62016-01-29 16:48:23 +0100525 * @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 +0100526 * In case of error, NULL is returned.
527 */
Michal Vasko105cef12016-02-04 12:06:26 +0100528struct ly_set *lyd_get_node2(const struct lyd_node *data, const struct lys_node *schema);
Radek Krejcic5b6b912016-01-18 16:35:35 +0100529
530/**
Michal Vasko6a1ab6f2016-02-04 12:08:11 +0100531 * @brief Get all key nodes of a \p list instance.
532 *
533 * @param[in] list List node, whose keys will be searched for.
534 * @return Set of list keys (use dset member of ::ly_set). If no keys are found/defined, the returned set is empty.
535 * In case of error, NULL is returned.
536 */
537struct ly_set *lyd_get_list_keys(const struct lyd_node *list);
538
539/**
Michal Vasko24337392015-10-16 09:58:16 +0200540 * @brief Validate \p node data subtree.
541 *
Radek Krejci03b71f72016-03-16 11:10:09 +0100542 * @param[in, out] node Data tree to be validated. In case the options includes #LYD_OPT_AUTODEL, libyang can modify the
543 * provided tree including the root \p node.
Michal Vasko24337392015-10-16 09:58:16 +0200544 * @param[in] options Options for the inserting data to the target data tree options, see @ref parseroptions.
Radek Krejcidef50022016-02-01 16:38:32 +0100545 * @param[in] ... libyang context for the data (used only in case the \p node is NULL, so in case of checking empty data tree)
546 * @return 0 on success (if options include #LYD_OPT_FILTER, some nodes can be deleted as an
Radek Krejci15c733d2016-01-27 16:31:17 +0100547 * optimization, which can have a bad consequences when the \p node stores a subtree instead of a tree with
548 * a top-level node(s)), nonzero in case of an error.
Michal Vasko24337392015-10-16 09:58:16 +0200549 */
Radek Krejci03b71f72016-03-16 11:10:09 +0100550int lyd_validate(struct lyd_node **node, int options, ...);
Michal Vasko2d162e12015-09-24 14:33:29 +0200551
552/**
Michal Vasko55f60be2015-10-14 13:12:58 +0200553 * @brief Unlink the specified data subtree. All referenced namespaces are copied.
Michal Vasko2d162e12015-09-24 14:33:29 +0200554 *
555 * Note, that the node's connection with the schema tree is kept. Therefore, in case of
556 * reconnecting the node to a data tree using lyd_paste() it is necessary to paste it
557 * to the appropriate place in the data tree following the schema.
558 *
559 * @param[in] node Data tree node to be unlinked (together with all children).
560 * @return 0 for success, nonzero for error
561 */
562int lyd_unlink(struct lyd_node *node);
563
564/**
565 * @brief Free (and unlink) the specified data (sub)tree.
566 *
567 * @param[in] node Root of the (sub)tree to be freed.
568 */
569void lyd_free(struct lyd_node *node);
570
571/**
Radek Krejci81468402016-01-07 13:52:40 +0100572 * @brief Free (and unlink) the specified data (sub)tree and all its siblings (preceding as well as following).
573 *
574 * @param[in] node One of the siblings root element of the (sub)trees to be freed.
575 */
576void lyd_free_withsiblings(struct lyd_node *node);
577
578/**
Radek Krejci134610e2015-10-20 17:15:34 +0200579 * @brief Insert attribute into the data node.
580 *
581 * @param[in] parent Data node where to place the attribute
Radek Krejci5f9e8c92015-10-30 10:01:06 +0100582 * @param[in] name Attribute name including the prefix (prefix:name). Prefix must be the name of one of the
583 * schema in the \p parent's context.
Radek Krejci134610e2015-10-20 17:15:34 +0200584 * @param[in] value Attribute value
585 * @return pointer to the created attribute (which is already connected in \p parent) or NULL on error.
586 */
587struct lyd_attr *lyd_insert_attr(struct lyd_node *parent, const char *name, const char *value);
588
589/**
Radek Krejci88f29302015-10-30 15:42:33 +0100590 * @brief Destroy data attribute
591 *
592 * If the attribute to destroy is a member of a node attribute list, it is necessary to
593 * provide the node itself as \p parent to keep the list consistent.
594 *
595 * @param[in] ctx Context where the attribute was created (usually it is the context of the \p parent)
596 * @param[in] parent Parent node where the attribute is placed
597 * @param[in] attr Attribute to destroy
598 * @param[in] recursive Zero to destroy only the attribute, non-zero to destroy also all the subsequent attributes
599 * in the list.
600 */
601void lyd_free_attr(struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_attr *attr, int recursive);
602
603/**
Radek Krejcidef50022016-02-01 16:38:32 +0100604* @brief Print data tree in the specified format.
605*
606* Same as lyd_print(), but it allocates memory and store the data into it.
607* It is up to caller to free the returned string by free().
608*
609* @param[out] strp Pointer to store the resulting dump.
610* @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
611* node of the data tree to print the specific subtree.
612* @param[in] format Data output format.
613* @param[in] options [printer flags](@ref printerflags).
614* @return 0 on success, 1 on failure (#ly_errno is set).
615*/
616int lyd_print_mem(char **strp, const struct lyd_node *root, LYD_FORMAT format, int options);
Michal Vasko2d162e12015-09-24 14:33:29 +0200617
618/**
Radek Krejcidef50022016-02-01 16:38:32 +0100619 * @brief Print data tree in the specified format.
Michal Vasko2d162e12015-09-24 14:33:29 +0200620 *
Radek Krejcidef50022016-02-01 16:38:32 +0100621 * Same as lyd_print(), but output is written into the specified file descriptor.
622 *
623 * @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
624 * node of the data tree to print the specific subtree.
625 * @param[in] fd File descriptor where to print the data.
626 * @param[in] format Data output format.
627 * @param[in] options [printer flags](@ref printerflags).
628 * @return 0 on success, 1 on failure (#ly_errno is set).
Michal Vasko2d162e12015-09-24 14:33:29 +0200629 */
Radek Krejcidef50022016-02-01 16:38:32 +0100630int lyd_print_fd(int fd, const struct lyd_node *root, LYD_FORMAT format, int options);
631
632/**
633 * @brief Print data tree in the specified format.
634 *
635 * To write data into a file descriptor, use lyd_print_fd().
636 *
637 * @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
638 * node of the data tree to print the specific subtree.
639 * @param[in] f File stream where to print the data.
640 * @param[in] format Data output format.
641 * @param[in] options [printer flags](@ref printerflags).
642 * @return 0 on success, 1 on failure (#ly_errno is set).
643 */
644int lyd_print_file(FILE *f, const struct lyd_node *root, LYD_FORMAT format, int options);
645
646/**
647 * @brief Print data tree in the specified format.
648 *
649 * Same as lyd_print(), but output is written via provided callback.
650 *
651 * @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
652 * node of the data tree to print the specific subtree.
653 * @param[in] writeclb Callback function to write the data (see write(1)).
654 * @param[in] arg Optional caller-specific argument to be passed to the \p writeclb callback.
655 * @param[in] format Data output format.
656 * @param[in] options [printer flags](@ref printerflags).
657 * @return 0 on success, 1 on failure (#ly_errno is set).
658 */
659int lyd_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg,
660 const struct lyd_node *root, LYD_FORMAT format, int options);
Michal Vasko2d162e12015-09-24 14:33:29 +0200661
Michal Vasko2d162e12015-09-24 14:33:29 +0200662/**@} */
663
664#ifdef __cplusplus
665}
666#endif
667
668#endif /* LY_TREE_DATA_H_ */