blob: 5816def5b1caa24933325bd07154cb88c104b8f6 [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 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of the Company nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 */
21
22#ifndef LY_TREE_DATA_H_
23#define LY_TREE_DATA_H_
24
25#include <stddef.h>
26#include <stdint.h>
27
Mislav Novakovice251a652015-09-29 08:40:12 +020028#include "tree_schema.h"
Radek Krejcidef50022016-02-01 16:38:32 +010029#include "xml.h"
Mislav Novakovice251a652015-09-29 08:40:12 +020030
Michal Vasko2d162e12015-09-24 14:33:29 +020031#ifdef __cplusplus
32extern "C" {
33#endif
34
35/**
Radek Krejcidef50022016-02-01 16:38:32 +010036 * @defgroup datatree Data Tree
Michal Vasko2d162e12015-09-24 14:33:29 +020037 * @{
Radek Krejcidef50022016-02-01 16:38:32 +010038 *
39 * Data structures and functions to manipulate and access instance data tree.
Michal Vasko2d162e12015-09-24 14:33:29 +020040 */
41
42/**
Radek Krejcidef50022016-02-01 16:38:32 +010043 * @brief Data input/output formats supported by libyang [parser](@ref howtodataparsers) and
44 * [printer](@ref howtodataprinters) functions.
Michal Vasko2d162e12015-09-24 14:33:29 +020045 */
46typedef enum {
47 LYD_UNKNOWN, /**< unknown format, used as return value in case of error */
48 LYD_XML, /**< XML format of the instance data */
Radek Krejci452fb952015-10-02 16:07:46 +020049 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 +020050 LYD_JSON, /**< JSON format of the instance data */
51} LYD_FORMAT;
52
53/**
Michal Vasko2d162e12015-09-24 14:33:29 +020054 * @brief Attribute structure.
55 *
Radek Krejci5f9e8c92015-10-30 10:01:06 +010056 * The structure provides information about attributes of a data element. Such attributes partially
57 * maps to annotations from draft-ietf-netmod-yang-metadata. In XML, they are represented as standard
58 * XML attrbutes. In JSON, they are represented as JSON elements starting with the '@' character
59 * (for more information, see the yang metadata draft.
60 *
Michal Vasko2d162e12015-09-24 14:33:29 +020061 */
62struct lyd_attr {
Radek Krejci5f9e8c92015-10-30 10:01:06 +010063 struct lyd_attr *next; /**< pointer to the next attribute of the same element */
64 struct lys_module *module; /**< pointer to the attribute's module.
65 TODO when annotations will be supported, point to the annotation definition
66 and validate that the attribute is really defined there. Currently, we just
67 believe that it is defined in the module it says */
Michal Vasko2d162e12015-09-24 14:33:29 +020068 const char *name; /**< attribute name */
69 const char *value; /**< attribute value */
70};
71
72/**
73 * @brief node's value representation
74 */
75typedef union lyd_value_u {
76 const char *binary; /**< base64 encoded, NULL terminated string */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +020077 struct lys_type_bit **bit; /**< bitmap of pointers to the schema definition of the bit value that are set,
78 its size is always the number of defined bits in the schema */
Radek Krejci489773c2015-12-17 13:20:03 +010079 int8_t bln; /**< 0 as false, 1 as true */
Michal Vasko2d162e12015-09-24 14:33:29 +020080 int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */
81 struct lys_type_enum *enm; /**< pointer to the schema definition of the enumeration value */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +020082 struct lys_ident *ident; /**< pointer to the schema definition of the identityref value */
Michal Vasko2d162e12015-09-24 14:33:29 +020083 struct lyd_node *instance; /**< instance-identifier, pointer to the referenced data tree node */
84 int8_t int8; /**< 8-bit signed integer */
85 int16_t int16; /**< 16-bit signed integer */
86 int32_t int32; /**< 32-bit signed integer */
87 int64_t int64; /**< 64-bit signed integer */
88 struct lyd_node *leafref; /**< pointer to the referenced leaf/leaflist instance in data tree */
89 const char *string; /**< string */
90 uint8_t uint8; /**< 8-bit unsigned integer */
91 uint16_t uint16; /**< 16-bit signed integer */
92 uint32_t uint32; /**< 32-bit signed integer */
93 uint64_t uint64; /**< 64-bit signed integer */
94} lyd_val;
95
96/**
Radek Krejcica7efb72016-01-18 13:06:01 +010097 * @defgroup validityflags Validity flags
98 * @ingroup datatree
99 *
100 * Validity flags for data nodes.
101 *
102 * @{
103 */
104#define LYD_VAL_OK 0x00 /**< node is successfully validated including whole subtree */
105#define LYD_VAL_UNIQUE 0x01 /**< Unique value(s) changed, applicable only to ::lys_node_list data nodes */
106#define LYD_VAL_NOT 0xff /**< node was not validated yet */
107/**
108 * @}
109 */
110
111/**
Michal Vasko2d162e12015-09-24 14:33:29 +0200112 * @brief Generic structure for a data node, directly applicable to the data nodes defined as #LYS_CONTAINER, #LYS_LIST
113 * and #LYS_CHOICE.
114 *
115 * Completely fits to containers and choices and is compatible (can be used interchangeably except the #child member)
116 * with all other lyd_node_* structures. All data nodes are provides as ::lyd_node structure by default.
117 * 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 +0100118 * ::lyd_node_leaf_list or ::lyd_node_anyxml structures. This structure fits only to #LYS_CONTAINER, #LYS_LIST and
119 * #LYS_CHOICE values.
Michal Vasko2d162e12015-09-24 14:33:29 +0200120 *
121 * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro.
122 */
123struct lyd_node {
124 struct lys_node *schema; /**< pointer to the schema definition of this node */
Radek Krejcica7efb72016-01-18 13:06:01 +0100125 uint8_t validity; /**< [validity flags](@ref validityflags) */
Michal Vasko2d162e12015-09-24 14:33:29 +0200126
127 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
128 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
129 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
130 never NULL. If there is no sibling node, pointer points to the node
131 itself. In case of the first node, this pointer points to the last
132 node in the list. */
133 struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
134 struct lyd_node *child; /**< pointer to the first child node \note Since other lyd_node_*
Radek Krejciee360892015-10-06 11:23:14 +0200135 structures represent end nodes, this member
Michal Vasko2d162e12015-09-24 14:33:29 +0200136 is replaced in those structures. Therefore, be careful with accessing
137 this member without having information about the node type from the schema's
138 ::lys_node#nodetype member. */
139};
140
141/**
Michal Vasko4c183312015-09-25 10:41:47 +0200142 * @brief Structure for data nodes defined as #LYS_LEAF or #LYS_LEAFLIST.
Michal Vasko2d162e12015-09-24 14:33:29 +0200143 *
Michal Vasko4c183312015-09-25 10:41:47 +0200144 * Extension for ::lyd_node structure. It replaces the ::lyd_node#child member by
145 * three new members (#value, #value_str and #value_type) to provide
146 * information about the value. The first five members (#schema, #attr, #next,
Michal Vasko2d162e12015-09-24 14:33:29 +0200147 * #prev and #parent) are compatible with the ::lyd_node's members.
148 *
149 * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro.
150 */
Michal Vasko4c183312015-09-25 10:41:47 +0200151struct lyd_node_leaf_list {
Michal Vasko2d162e12015-09-24 14:33:29 +0200152 struct lys_node *schema; /**< pointer to the schema definition of this node which is ::lys_node_leaflist
153 structure */
Radek Krejcica7efb72016-01-18 13:06:01 +0100154 uint8_t validity; /**< [validity flags](@ref validityflags) */
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
172/**
173 * @brief Structure for data nodes defined as #LYS_ANYXML.
174 *
175 * Extension for ::lyd_node structure - replaces the ::lyd_node#child member by new #value member. The first five
176 * members (#schema, #attr, #next, #prev and #parent) are compatible with the ::lyd_node's members.
177 *
178 * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro.
179 */
180struct lyd_node_anyxml {
181 struct lys_node *schema; /**< pointer to the schema definition of this node which is ::lys_node_anyxml
182 structure */
Radek Krejcica7efb72016-01-18 13:06:01 +0100183 uint8_t validity; /**< [validity flags](@ref validityflags) */
Michal Vasko2d162e12015-09-24 14:33:29 +0200184
185 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
186 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
187 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
188 never NULL. If there is no sibling node, pointer points to the node
189 itself. In case of the first node, this pointer points to the last
190 node in the list. */
191 struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
192
193 /* struct lyd_node *child; should be here, but is not */
194
195 /* anyxml's specific members */
196 struct lyxml_elem *value; /**< anyxml name is the root element of value! */
197};
198
199/**
Radek Krejcidef50022016-02-01 16:38:32 +0100200 * @defgroup parseroptions Data parser options
201 * @ingroup datatree
202 *
203 * Various options to change the data tree parsers behavior.
204 *
205 * Default behavior:
206 * - in case of XML, parser reads all data from its input (file, memory, XML tree) including the case of not well-formed
207 * XML document (multiple top-level elements) and if there is an unknown element, it is skipped including its subtree
208 * (see the next point). This can be changed by the #LYD_OPT_NOSIBLINGS option which make parser to read only a single
209 * tree (with a single root element) from its input.
210 * - parser silently ignores the data without a matching node in schema trees. If the caller want to stop
211 * parsing in case of presence of unknown data, the #LYD_OPT_STRICT can be used. The strict mode is useful for
212 * NETCONF servers, since NETCONF clients should always send data according to the capabilities announced by the server.
213 * On the other hand, the default non-strict mode is useful for clients receiving data from NETCONF server since
214 * clients are not required to understand everything the server does. Of course, the optimal strategy for clients is
215 * to use filtering to get only the required data. Having an unknown element of the known namespace is always an error.
216 * The behavior can be changed by #LYD_OPT_STRICT option.
217 * - using obsolete statements (status set to obsolete) just generates a warning, but the processing continues. The
218 * behavior can be changed by #LYD_OPT_OBSOLETE option.
219 * - parser expects that the provided data provides complete datastore content (both the configuration and state data)
220 * and performs data validation according to all YANG rules. This can be a problem in case of representing NETCONF's
221 * subtree filter data, edit-config's data or other type of data set - such data do not represent a complete data set
222 * and some of the validation rules can fail. Therefore there are other options (within lower 8 bits) to make parser
223 * to accept such a data.
224 * @{
225 */
226
227#define LYD_OPT_DATA 0x00 /**< Default type of data - complete datastore content with configuration as well as
228 state data. */
229#define LYD_OPT_CONFIG 0x01 /**< A configuration datastore - complete datastore without state data.
230 Validation modifications:
231 - status data are not allowed */
232#define LYD_OPT_GET 0x02 /**< Data content from a NETCONF reply message to the NETCONF \<get\> operation.
233 Validation modifications:
234 - mandatory nodes can be omitted
235 - leafrefs and instance-identifier are not resolved
236 - list's keys/unique nodes are not required (so duplication is not checked) */
237#define LYD_OPT_GETCONFIG 0x04 /**< Data content from a NETCONF reply message to the NETCONF \<get-config\> operation
238 Validation modifications:
239 - mandatory nodes can be omitted
240 - leafrefs and instance-identifier are not resolved
241 - list's keys/unique nodes are not required (so duplication is not checked)
242 - status data are not allowed */
243#define LYD_OPT_EDIT 0x08 /**< Content of the NETCONF \<edit-config\>'s config element.
244 Validation modifications:
245 - mandatory nodes can be omitted
246 - leafrefs and instance-identifier are not resolved
247 - status data are not allowed */
248#define LYD_OPT_RPC 0x10 /**< Data represents RPC's input parameters. */
249#define LYD_OPT_RPCREPLY 0x20 /**< Data represents RPC's output parameters (maps to NETCONF <rpc-reply> data). */
250#define LYD_OPT_NOTIF 0x40 /**< Data represents an event notification data. */
251#define LYD_OPT_FILTER 0x80 /**< Data represents NETCONF subtree filter. Validation modifications:
252 - leafs/leaf-lists with no data are allowed (even not allowed e.g. by length restriction)
253 - multiple instances of container/leaf/.. are allowed
254 - list's keys/unique nodes are not required
255 - mandatory nodes can be omitted
256 - leafrefs and instance-identifier are not resolved
257 - data from different choice's branches are allowed */
258#define LYD_OPT_TYPEMASK 0xff /**< Mask to filter data type options. Always only a single data type option (only
259 single bit from the lower 8 bits) can be set. */
260
261#define LYD_OPT_STRICT 0x0100 /**< Instead of silent ignoring data without schema definition, raise an error. */
262#define LYD_OPT_DESTRUCT 0x0200 /**< Free the provided XML tree during parsing the data. With this option, the
263 provided XML tree is affected and all succesfully parsed data are freed.
264 This option is applicable only to lyd_parse_xml() function. */
265#define LYD_OPT_OBSOLETE 0x0400 /**< Raise an error when an obsolete statement (status set to obsolete) is used. */
266#define LYD_OPT_NOSIBLINGS 0x0800 /**< Parse only a single XML tree from the input. This option applies only to
267 XML input data. */
268
269/**@} parseroptions */
270
271/**
272 * @brief Parse (and validate according to appropriate schema from the given context) data.
273 *
274 * In case of LY_XML format, the data string is parsed completely. It means that when it contains
275 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
276 * returned data node is a root of the first tree with other trees connected via the next pointer.
277 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
278 *
279 * @param[in] ctx Context to connect with the data tree being built here.
280 * @param[in] data Serialized data in the specified format.
281 * @param[in] format Format of the input data to be parsed.
282 * @param[in] options Parser options, see @ref parseroptions.
283 * @param[in] ... Additional argument must be supplied when #LYD_OPT_RPCREPLY value is specified in \p options. The
284 * argument is supposed to provide pointer to the RPC schema node for the reply's request
285 * (const struct ::lys_node* rpc).
286 * @return Pointer to the built data tree or NULL in case of empty \p data. To free the returned structure,
287 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
288 * #ly_errno contains appropriate error code (see #LY_ERR).
289 */
290struct lyd_node *lyd_parse_data(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, ...);
291
292/**
293 * @brief Read data from the given file descriptor.
294 *
295 * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc.
296 *
297 * In case of LY_XML format, the file content is parsed completely. It means that when it contains
298 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
299 * returned data node is a root of the first tree with other trees connected via the next pointer.
300 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
301 *
302 * @param[in] ctx Context to connect with the data tree being built here.
303 * @param[in] fd The standard file descriptor of the file containing the data tree in the specified format.
304 * @param[in] format Format of the input data to be parsed.
305 * @param[in] options Parser options, see @ref parseroptions.
306 * @param[in] ... Additional argument must be supplied when #LYD_OPT_RPCREPLY value is specified in \p options. The
307 * argument is supposed to provide pointer to the RPC schema node for the reply's request
308 * (const struct ::lys_node* rpc).
309 * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure,
310 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
311 * #ly_errno contains appropriate error code (see #LY_ERR).
312 */
313struct lyd_node *lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, ...);
314
315/**
316 * @brief Read data from the given file path.
317 *
318 * In case of LY_XML format, the file content is parsed completely. It means that when it contains
319 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
320 * returned data node is a root of the first tree with other trees connected via the next pointer.
321 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
322 *
323 * @param[in] ctx Context to connect with the data tree being built here.
324 * @param[in] path Path to the file containing the data tree in the specified format.
325 * @param[in] format Format of the input data to be parsed.
326 * @param[in] options Parser options, see @ref parseroptions.
327 * @param[in] ... Additional argument must be supplied when #LYD_OPT_RPCREPLY value is specified in \p options. The
328 * argument is supposed to provide pointer to the RPC schema node for the reply's request
329 * (const struct ::lys_node* rpc).
330 * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure,
331 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
332 * #ly_errno contains appropriate error code (see #LY_ERR).
333 */
334struct lyd_node *lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options, ...);
335
336/**
337 * @brief Parse (and validate according to appropriate schema from the given context) XML tree.
338 *
339 * The output data tree is parsed from the given XML tree previously parsed by one of the
340 * lyxml_read* functions.
341 *
342 * If there are some sibling elements of the \p root (data were read with #LYXML_READ_MULTIROOT option
343 * or the provided root is a root element of a subtree), all the sibling nodes (previous as well as
344 * following) are processed as well. The returned data node is a root of the first tree with other
345 * trees connected via the next pointer. This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
346 *
347 * When the function is used with #LYD_OPT_DESTRUCT, all the successfully parsed data including the
348 * XML \p root and all its siblings (if #LYD_OPT_NOSIBLINGS is not used) are freed. Only with
349 * #LYD_OPT_DESTRUCT option the \p root pointer is changed - if all the data are parsed, it is set
350 * to NULL, otherwise it will hold the XML tree without the successfully parsed elements.
351 *
352 * The context must be the same as the context used to parse XML tree by lyxml_read* function.
353 *
354 * @param[in] ctx Context to connect with the data tree being built here.
355 * @param[in,out] root XML tree to parse (convert) to data tree. By default, parser do not change the XML tree. However,
356 * when #LYD_OPT_DESTRUCT is specified in \p options, parser frees all successfully parsed data.
357 * @param[in] options Parser options, see @ref parseroptions.
358 * @param[in] ... Additional argument must be supplied when #LYD_OPT_RPCREPLY value is specified in \p options. The
359 * argument is supposed to provide pointer to the RPC schema node for the reply's request
360 * (const struct ::lys_node* rpc).
361 * @return Pointer to the built data tree or NULL in case of empty \p root. To free the returned structure,
362 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
363 * #ly_errno contains appropriate error code (see #LY_ERR).
364 */
365struct lyd_node *lyd_parse_xml(struct ly_ctx *ctx, struct lyxml_elem **root, int options,...);
366
367/**
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200368 * @brief Create a new container node in a data tree.
369 *
370 * @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 +0200371 * @param[in] module Module with the node being created.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200372 * @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 +0200373 * #LYS_NOTIF, or #LYS_RPC.
Michal Vasko1dca6882015-10-22 14:29:42 +0200374 * @return New node, NULL on error.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200375 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100376struct lyd_node *lyd_new(struct lyd_node *parent, const struct lys_module *module, const char *name);
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200377
378/**
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200379 * @brief Create a new leaf or leaflist node in a data tree with a string value that is converted to
380 * the actual value.
381 *
382 * @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 +0200383 * @param[in] module Module with the node being created.
384 * @param[in] name Schema node name of the new data node.
Michal Vasko3e671b52015-10-23 16:23:15 +0200385 * @param[in] val_str String form of the value of the node being created. In case the type is #LY_TYPE_INST
386 * 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 +0200387 * @return New node, NULL on error.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200388 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100389struct lyd_node *lyd_new_leaf(struct lyd_node *parent, const struct lys_module *module, const char *name,
Michal Vasko3e671b52015-10-23 16:23:15 +0200390 const char *val_str);
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200391
392/**
Radek Krejcib9b4d002016-01-18 13:08:51 +0100393 * @brief Change value of a leaf node.
394 *
395 * Despite the prototype allows to provide a leaflist node as \p leaf parameter, only leafs are accepted.
396 *
397 * @param[in] leaf A leaf node to change.
398 * @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
399 * or #LY_TYPE_IDENT, JSON node-id format is expected (nodes are prefixed with module names, not XML namespaces).
400 * @return 0 on success, non-zero on error.
401 */
402int lyd_change_leaf(struct lyd_node_leaf_list *leaf, const char *val_str);
403
404/**
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200405 * @brief Create a new anyxml node in a data tree.
Michal Vasko2d162e12015-09-24 14:33:29 +0200406 *
Michal Vasko2d162e12015-09-24 14:33:29 +0200407 * @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.
409 * @param[in] name Schema node name of the new data node.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200410 * @param[in] val_xml Value of the node being created. Must be a well-formed XML.
Michal Vasko1dca6882015-10-22 14:29:42 +0200411 * @return New node, NULL on error.
Michal Vasko2d162e12015-09-24 14:33:29 +0200412 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100413struct lyd_node *lyd_new_anyxml(struct lyd_node *parent, const struct lys_module *module, const char *name,
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200414 const char *val_xml);
Michal Vasko2d162e12015-09-24 14:33:29 +0200415
416/**
Michal Vasko50c0a872016-01-13 14:34:11 +0100417 * @brief Create a new container node in a data tree, whose schema parent is #LYS_OUTPUT.
Michal Vasko0df122f2015-12-14 13:38:21 +0100418 *
Michal Vasko50c0a872016-01-13 14:34:11 +0100419 * @param[in] schema Schema node of the container.
Michal Vasko0df122f2015-12-14 13:38:21 +0100420 * @return New node, NULL on error.
421 */
Michal Vasko50c0a872016-01-13 14:34:11 +0100422struct lyd_node *lyd_output_new(const struct lys_node *schema);
423
424/**
425 * @brief Create a new leaf or leaflist node in a data tree, whose schema parent is #LYS_OUTPUT.
426 *
427 * @param[in] schema Schema node of the leaf.
428 * @param[in] val_str String form of the value of the node being created. In case the type is #LY_TYPE_INST
429 * or #LY_TYPE_IDENT, JSON node-id format is expected (nodes are prefixed with module names, not XML namespaces).
430 * @return New node, NULL on error.
431 */
432struct lyd_node *lyd_output_new_leaf(const struct lys_node *schema, const char *val_str);
433
434/**
435 * @brief Create a new anyxml node in a data tree, whose schema parent is #LYS_OUTPUT.
436 *
437 * @param[in] schema Schema node of the leaf.
438 * @param[in] val_xml Value of the node being created. Must be a well-formed XML.
439 * @return New node, NULL on error.
440 */
441struct lyd_node *lyd_output_new_anyxml(const struct lys_node *schema, const char *val_xml);
Michal Vasko0df122f2015-12-14 13:38:21 +0100442
443/**
Michal Vaskoc0797f82015-10-14 15:51:25 +0200444 * @brief Create a copy of the specified data tree \p node. Namespaces are copied as needed,
445 * schema references are kept the same.
Michal Vasko2d162e12015-09-24 14:33:29 +0200446 *
447 * @param[in] node Data tree node to be duplicated.
448 * @param[in] recursive 1 if all children are supposed to be also duplicated.
449 * @return Created copy of the provided data \p node.
450 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100451struct lyd_node *lyd_dup(const struct lyd_node *node, int recursive);
Michal Vasko2d162e12015-09-24 14:33:29 +0200452
453/**
454 * @brief Insert the \p node element as child to the \p parent element. The \p node is inserted as a last child of the
455 * \p parent.
456 *
457 * If the node is part of some other tree, it is automatically unlinked.
458 * If the node is the first node of a node list (with no parent), all
459 * the subsequent nodes are also inserted.
460 *
461 * @param[in] parent Parent node for the \p node being inserted.
462 * @param[in] node The node being inserted.
Michal Vasko24337392015-10-16 09:58:16 +0200463 * @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 +0200464 * in the data tree.
465 */
Michal Vasko24337392015-10-16 09:58:16 +0200466int lyd_insert(struct lyd_node *parent, struct lyd_node *node);
Michal Vasko2d162e12015-09-24 14:33:29 +0200467
468/**
Michal Vasko3f7dba12015-10-15 13:09:27 +0200469 * @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 +0100470 * siblings (just moving \p node position), skip validation.
Michal Vasko2d162e12015-09-24 14:33:29 +0200471 *
Michal Vasko2d162e12015-09-24 14:33:29 +0200472 * @param[in] sibling The data tree node before which the \p node will be inserted.
473 * @param[in] node The data tree node to be inserted.
Michal Vasko24337392015-10-16 09:58:16 +0200474 * @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 +0200475 * in the data tree.
476 */
Michal Vasko24337392015-10-16 09:58:16 +0200477int lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node);
Michal Vasko2d162e12015-09-24 14:33:29 +0200478
479/**
480 * @brief Insert the \p node element after the \p sibling element.
481 *
Michal Vasko3f7dba12015-10-15 13:09:27 +0200482 * @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 +0100483 * are already siblings (just moving \p node position), skip validation.
Michal Vasko2d162e12015-09-24 14:33:29 +0200484 * @param[in] node The data tree node to be inserted.
Michal Vasko24337392015-10-16 09:58:16 +0200485 * @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 +0200486 * in the data tree.
487 */
Michal Vasko24337392015-10-16 09:58:16 +0200488int lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node);
489
490/**
Radek Krejcic5b6b912016-01-18 16:35:35 +0100491 * @brief Search in the given data for instances of the provided schema node.
492 *
493 * The \p data is used to find the data root and function then searches in the whole tree and all sibling trees.
494 *
495 * @param[in] data A node in the data tree to search.
496 * @param[in] schema Schema node of the data nodes caller want to find.
Radek Krejci2342cf62016-01-29 16:48:23 +0100497 * @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 +0100498 * In case of error, NULL is returned.
499 */
Radek Krejcidc154432016-01-21 11:10:59 +0100500struct ly_set *lyd_get_node(const struct lyd_node *data, const struct lys_node *schema);
Radek Krejcic5b6b912016-01-18 16:35:35 +0100501
502/**
Michal Vasko24337392015-10-16 09:58:16 +0200503 * @brief Validate \p node data subtree.
504 *
Radek Krejci15c733d2016-01-27 16:31:17 +0100505 * @param[in] node Data tree to be validated.
Michal Vasko24337392015-10-16 09:58:16 +0200506 * @param[in] options Options for the inserting data to the target data tree options, see @ref parseroptions.
Radek Krejcidef50022016-02-01 16:38:32 +0100507 * @param[in] ... libyang context for the data (used only in case the \p node is NULL, so in case of checking empty data tree)
508 * @return 0 on success (if options include #LYD_OPT_FILTER, some nodes can be deleted as an
Radek Krejci15c733d2016-01-27 16:31:17 +0100509 * optimization, which can have a bad consequences when the \p node stores a subtree instead of a tree with
510 * a top-level node(s)), nonzero in case of an error.
Michal Vasko24337392015-10-16 09:58:16 +0200511 */
Radek Krejci2342cf62016-01-29 16:48:23 +0100512int lyd_validate(struct lyd_node *node, int options, ...);
Michal Vasko2d162e12015-09-24 14:33:29 +0200513
514/**
Michal Vasko55f60be2015-10-14 13:12:58 +0200515 * @brief Unlink the specified data subtree. All referenced namespaces are copied.
Michal Vasko2d162e12015-09-24 14:33:29 +0200516 *
517 * Note, that the node's connection with the schema tree is kept. Therefore, in case of
518 * reconnecting the node to a data tree using lyd_paste() it is necessary to paste it
519 * to the appropriate place in the data tree following the schema.
520 *
521 * @param[in] node Data tree node to be unlinked (together with all children).
522 * @return 0 for success, nonzero for error
523 */
524int lyd_unlink(struct lyd_node *node);
525
526/**
527 * @brief Free (and unlink) the specified data (sub)tree.
528 *
529 * @param[in] node Root of the (sub)tree to be freed.
530 */
531void lyd_free(struct lyd_node *node);
532
533/**
Radek Krejci81468402016-01-07 13:52:40 +0100534 * @brief Free (and unlink) the specified data (sub)tree and all its siblings (preceding as well as following).
535 *
536 * @param[in] node One of the siblings root element of the (sub)trees to be freed.
537 */
538void lyd_free_withsiblings(struct lyd_node *node);
539
540/**
Radek Krejci134610e2015-10-20 17:15:34 +0200541 * @brief Insert attribute into the data node.
542 *
543 * @param[in] parent Data node where to place the attribute
Radek Krejci5f9e8c92015-10-30 10:01:06 +0100544 * @param[in] name Attribute name including the prefix (prefix:name). Prefix must be the name of one of the
545 * schema in the \p parent's context.
Radek Krejci134610e2015-10-20 17:15:34 +0200546 * @param[in] value Attribute value
547 * @return pointer to the created attribute (which is already connected in \p parent) or NULL on error.
548 */
549struct lyd_attr *lyd_insert_attr(struct lyd_node *parent, const char *name, const char *value);
550
551/**
Radek Krejci88f29302015-10-30 15:42:33 +0100552 * @brief Destroy data attribute
553 *
554 * If the attribute to destroy is a member of a node attribute list, it is necessary to
555 * provide the node itself as \p parent to keep the list consistent.
556 *
557 * @param[in] ctx Context where the attribute was created (usually it is the context of the \p parent)
558 * @param[in] parent Parent node where the attribute is placed
559 * @param[in] attr Attribute to destroy
560 * @param[in] recursive Zero to destroy only the attribute, non-zero to destroy also all the subsequent attributes
561 * in the list.
562 */
563void lyd_free_attr(struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_attr *attr, int recursive);
564
565/**
Radek Krejcidef50022016-02-01 16:38:32 +0100566* @brief Print data tree in the specified format.
567*
568* Same as lyd_print(), but it allocates memory and store the data into it.
569* It is up to caller to free the returned string by free().
570*
571* @param[out] strp Pointer to store the resulting dump.
572* @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
573* node of the data tree to print the specific subtree.
574* @param[in] format Data output format.
575* @param[in] options [printer flags](@ref printerflags).
576* @return 0 on success, 1 on failure (#ly_errno is set).
577*/
578int lyd_print_mem(char **strp, const struct lyd_node *root, LYD_FORMAT format, int options);
Michal Vasko2d162e12015-09-24 14:33:29 +0200579
580/**
Radek Krejcidef50022016-02-01 16:38:32 +0100581 * @brief Print data tree in the specified format.
Michal Vasko2d162e12015-09-24 14:33:29 +0200582 *
Radek Krejcidef50022016-02-01 16:38:32 +0100583 * Same as lyd_print(), but output is written into the specified file descriptor.
584 *
585 * @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
586 * node of the data tree to print the specific subtree.
587 * @param[in] fd File descriptor where to print the data.
588 * @param[in] format Data output format.
589 * @param[in] options [printer flags](@ref printerflags).
590 * @return 0 on success, 1 on failure (#ly_errno is set).
Michal Vasko2d162e12015-09-24 14:33:29 +0200591 */
Radek Krejcidef50022016-02-01 16:38:32 +0100592int lyd_print_fd(int fd, const struct lyd_node *root, LYD_FORMAT format, int options);
593
594/**
595 * @brief Print data tree in the specified format.
596 *
597 * To write data into a file descriptor, use lyd_print_fd().
598 *
599 * @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
600 * node of the data tree to print the specific subtree.
601 * @param[in] f File stream where to print the data.
602 * @param[in] format Data output format.
603 * @param[in] options [printer flags](@ref printerflags).
604 * @return 0 on success, 1 on failure (#ly_errno is set).
605 */
606int lyd_print_file(FILE *f, const struct lyd_node *root, LYD_FORMAT format, int options);
607
608/**
609 * @brief Print data tree in the specified format.
610 *
611 * Same as lyd_print(), but output is written via provided callback.
612 *
613 * @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
614 * node of the data tree to print the specific subtree.
615 * @param[in] writeclb Callback function to write the data (see write(1)).
616 * @param[in] arg Optional caller-specific argument to be passed to the \p writeclb callback.
617 * @param[in] format Data output format.
618 * @param[in] options [printer flags](@ref printerflags).
619 * @return 0 on success, 1 on failure (#ly_errno is set).
620 */
621int lyd_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg,
622 const struct lyd_node *root, LYD_FORMAT format, int options);
Michal Vasko2d162e12015-09-24 14:33:29 +0200623
Michal Vasko2d162e12015-09-24 14:33:29 +0200624/**@} */
625
626#ifdef __cplusplus
627}
628#endif
629
630#endif /* LY_TREE_DATA_H_ */