blob: e2861e237b3314645356d1ce44670885b7ed2f32 [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/**
Radek Krejci45826012016-08-24 15:07:57 +020046 * @brief List of possible value types stored in ::lyd_node_anydata.
47 */
48typedef enum {
Radek Krejci83bf1402016-09-27 15:05:20 +020049 LYD_ANYDATA_CONSTSTRING = 0x00, /**< value is constant string (const char *) which is internally duplicated for
50 storing in the anydata structure; XML sensitive characters (such as & or \>)
51 are automatically escaped when the anydata is printed in XML format */
52 LYD_ANYDATA_STRING = 0x01, /**< value is dynamically allocated string (char*), so the data are used directly
53 without duplication and caller is supposed to not manipulate with the data
54 after a successful call (including calling free() on the provided data); XML
55 sensitive characters (such as & or \>) are automatically escaped when the
56 anydata is printed in XML format */
57 LYD_ANYDATA_JSON = 0x02, /**< value is string containing the data modeled by YANG and encoded as I-JSON. The
58 string is handled as constant string. In case of using the value as input
59 parameter, the #LYD_ANYDATA_JSOND can be used for dynamically allocated
60 string. */
61 LYD_ANYDATA_JSOND = 0x03, /**< In case of using value as input parameter, this enumeration is supposed to be
62 used for dynamically allocated strings (it is actually combination of
63 #LYD_ANYDATA_JSON and #LYD_ANYDATA_STRING (and it can be also specified as
64 ORed value of the mentioned values. */
65 LYD_ANYDATA_SXML = 0x04, /**< value is string containing the serialized XML data. The string is handled as
66 constant string. In case of using the value as input parameter, the
67 #LYD_ANYDATA_SXMLD can be used for dynamically allocated string. */
68 LYD_ANYDATA_SXMLD = 0x05, /**< In case of using serialized XML value as input parameter, this enumeration is
69 supposed to be used for dynamically allocated strings (it is actually
70 combination of #LYD_ANYDATA_SXML and #LYD_ANYDATA_STRING (and it can be also
71 specified as ORed value of the mentioned values). */
72 LYD_ANYDATA_XML = 0x08, /**< value is struct lyxml_elem*, the structure is directly connected into the
73 anydata node without duplication, caller is supposed to not manipulate with the
74 data after a successful call (including calling lyxml_free() on the provided
75 data) */
76 LYD_ANYDATA_DATATREE = 0x10, /**< value is struct lyd_node* (first sibling), the structure is directly connected
77 into the anydata node without duplication, caller is supposed to not manipulate
78 with the data after a successful call (including calling lyd_free() on the
79 provided data) */
Radek Krejci45826012016-08-24 15:07:57 +020080} LYD_ANYDATA_VALUETYPE;
81
82/**
Michal Vasko2d162e12015-09-24 14:33:29 +020083 * @brief Attribute structure.
84 *
Radek Krejci5f9e8c92015-10-30 10:01:06 +010085 * The structure provides information about attributes of a data element. Such attributes partially
86 * maps to annotations from draft-ietf-netmod-yang-metadata. In XML, they are represented as standard
87 * XML attrbutes. In JSON, they are represented as JSON elements starting with the '@' character
88 * (for more information, see the yang metadata draft.
89 *
Michal Vasko2d162e12015-09-24 14:33:29 +020090 */
91struct lyd_attr {
Radek Krejci5f9e8c92015-10-30 10:01:06 +010092 struct lyd_attr *next; /**< pointer to the next attribute of the same element */
93 struct lys_module *module; /**< pointer to the attribute's module.
94 TODO when annotations will be supported, point to the annotation definition
95 and validate that the attribute is really defined there. Currently, we just
96 believe that it is defined in the module it says */
Michal Vasko2d162e12015-09-24 14:33:29 +020097 const char *name; /**< attribute name */
98 const char *value; /**< attribute value */
99};
100
101/**
102 * @brief node's value representation
103 */
104typedef union lyd_value_u {
105 const char *binary; /**< base64 encoded, NULL terminated string */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200106 struct lys_type_bit **bit; /**< bitmap of pointers to the schema definition of the bit value that are set,
107 its size is always the number of defined bits in the schema */
Radek Krejci489773c2015-12-17 13:20:03 +0100108 int8_t bln; /**< 0 as false, 1 as true */
Michal Vasko2d162e12015-09-24 14:33:29 +0200109 int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */
110 struct lys_type_enum *enm; /**< pointer to the schema definition of the enumeration value */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200111 struct lys_ident *ident; /**< pointer to the schema definition of the identityref value */
Radek Krejci40f17b92016-02-03 14:30:43 +0100112 struct lyd_node *instance; /**< pointer to the instance-identifier target, note that if the tree was modified,
113 the target (address) can be invalid - the pointer is correctly checked and updated
114 by lyd_validate() */
Michal Vasko2d162e12015-09-24 14:33:29 +0200115 int8_t int8; /**< 8-bit signed integer */
116 int16_t int16; /**< 16-bit signed integer */
117 int32_t int32; /**< 32-bit signed integer */
118 int64_t int64; /**< 64-bit signed integer */
119 struct lyd_node *leafref; /**< pointer to the referenced leaf/leaflist instance in data tree */
120 const char *string; /**< string */
121 uint8_t uint8; /**< 8-bit unsigned integer */
122 uint16_t uint16; /**< 16-bit signed integer */
123 uint32_t uint32; /**< 32-bit signed integer */
124 uint64_t uint64; /**< 64-bit signed integer */
125} lyd_val;
126
127/**
Radek Krejcica7efb72016-01-18 13:06:01 +0100128 * @defgroup validityflags Validity flags
129 * @ingroup datatree
130 *
131 * Validity flags for data nodes.
132 *
133 * @{
134 */
135#define LYD_VAL_OK 0x00 /**< node is successfully validated including whole subtree */
136#define LYD_VAL_UNIQUE 0x01 /**< Unique value(s) changed, applicable only to ::lys_node_list data nodes */
Radek Krejcid788a522016-07-25 14:57:38 +0200137#define LYD_VAL_MAND 0x02 /**< Some child added/removed and it is needed to perform check for mandatory
138 node or min/max constraints of direct list/leaflist children, applicable only
139 to ::lys_node_list and ::lys_node_container data nodes */
Radek Krejci991a3962016-05-05 15:00:14 +0200140#define LYD_VAL_NOT 0x07 /**< node was not validated yet */
141#define LYD_VAL_INUSE 0x08 /**< Internal flag for note about various processing on data, should be used only
142 internally and removed before the libyang returns to the caller */
Radek Krejcica7efb72016-01-18 13:06:01 +0100143/**
144 * @}
145 */
146
147/**
Michal Vasko2d162e12015-09-24 14:33:29 +0200148 * @brief Generic structure for a data node, directly applicable to the data nodes defined as #LYS_CONTAINER, #LYS_LIST
149 * and #LYS_CHOICE.
150 *
151 * Completely fits to containers and choices and is compatible (can be used interchangeably except the #child member)
152 * with all other lyd_node_* structures. All data nodes are provides as ::lyd_node structure by default.
153 * According to the schema's ::lys_node#nodetype member, the specific object is supposed to be cast to
Radek Krejcibf2abff2016-08-23 15:51:52 +0200154 * ::lyd_node_leaf_list or ::lyd_node_anydata structures. This structure fits only to #LYS_CONTAINER, #LYS_LIST and
Radek Krejcica7efb72016-01-18 13:06:01 +0100155 * #LYS_CHOICE values.
Michal Vasko2d162e12015-09-24 14:33:29 +0200156 *
157 * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro.
158 */
159struct lyd_node {
160 struct lys_node *schema; /**< pointer to the schema definition of this node */
Radek Krejci1eefeb32016-04-15 16:01:46 +0200161 uint8_t validity:4; /**< [validity flags](@ref validityflags) */
Radek Krejci082c84f2016-10-17 16:33:06 +0200162 uint8_t dflt:1; /**< flag for default node */
Radek Krejci0b7704f2016-03-18 12:16:14 +0100163 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 +0100164 do not use this value! */
Michal Vasko2d162e12015-09-24 14:33:29 +0200165
166 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
167 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
168 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
169 never NULL. If there is no sibling node, pointer points to the node
170 itself. In case of the first node, this pointer points to the last
171 node in the list. */
172 struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
173 struct lyd_node *child; /**< pointer to the first child node \note Since other lyd_node_*
Radek Krejciee360892015-10-06 11:23:14 +0200174 structures represent end nodes, this member
Michal Vasko2d162e12015-09-24 14:33:29 +0200175 is replaced in those structures. Therefore, be careful with accessing
176 this member without having information about the node type from the schema's
177 ::lys_node#nodetype member. */
178};
179
180/**
Michal Vasko4c183312015-09-25 10:41:47 +0200181 * @brief Structure for data nodes defined as #LYS_LEAF or #LYS_LEAFLIST.
Michal Vasko2d162e12015-09-24 14:33:29 +0200182 *
Michal Vasko4c183312015-09-25 10:41:47 +0200183 * Extension for ::lyd_node structure. It replaces the ::lyd_node#child member by
184 * three new members (#value, #value_str and #value_type) to provide
185 * information about the value. The first five members (#schema, #attr, #next,
Michal Vasko2d162e12015-09-24 14:33:29 +0200186 * #prev and #parent) are compatible with the ::lyd_node's members.
187 *
188 * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro.
189 */
Michal Vasko4c183312015-09-25 10:41:47 +0200190struct lyd_node_leaf_list {
Michal Vasko2d162e12015-09-24 14:33:29 +0200191 struct lys_node *schema; /**< pointer to the schema definition of this node which is ::lys_node_leaflist
192 structure */
Radek Krejci1eefeb32016-04-15 16:01:46 +0200193 uint8_t validity:4; /**< [validity flags](@ref validityflags) */
Radek Krejci082c84f2016-10-17 16:33:06 +0200194 uint8_t dflt:1; /**< flag for default node */
Radek Krejci0b7704f2016-03-18 12:16:14 +0100195 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 +0100196 do not use this value! */
Michal Vasko2d162e12015-09-24 14:33:29 +0200197
198 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
199 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
200 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
201 never NULL. If there is no sibling node, pointer points to the node
202 itself. In case of the first node, this pointer points to the last
203 node in the list. */
204 struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
205
206 /* struct lyd_node *child; should be here, but is not */
207
208 /* leaflist's specific members */
Michal Vasko6a027702016-06-30 10:32:14 +0200209 const char *value_str; /**< string representation of value (for comparison, printing,...), always corresponds to value_type */
210 lyd_val value; /**< node's value representation, always corresponds to schema->type.base */
211 LY_DATA_TYPE value_type; /**< type of the value in the node, mainly for union to avoid repeating of type detection,
212 if (schema->type.base == LY_TYPE_LEAFREF), then value_type may be
213 (LY_TYPE_LEAFREF_UNRES | leafref target value_type) and (value.leafref == NULL) */
Michal Vasko2d162e12015-09-24 14:33:29 +0200214};
215
216/**
Radek Krejcibf2abff2016-08-23 15:51:52 +0200217 * @brief Structure for data nodes defined as #LYS_ANYDATA or #LYS_ANYXML.
Michal Vasko2d162e12015-09-24 14:33:29 +0200218 *
219 * Extension for ::lyd_node structure - replaces the ::lyd_node#child member by new #value member. The first five
220 * members (#schema, #attr, #next, #prev and #parent) are compatible with the ::lyd_node's members.
221 *
222 * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro.
223 */
Radek Krejcibf2abff2016-08-23 15:51:52 +0200224struct lyd_node_anydata {
225 struct lys_node *schema; /**< pointer to the schema definition of this node which is ::lys_node_anydata
Michal Vasko2d162e12015-09-24 14:33:29 +0200226 structure */
Radek Krejci1eefeb32016-04-15 16:01:46 +0200227 uint8_t validity:4; /**< [validity flags](@ref validityflags) */
Radek Krejci082c84f2016-10-17 16:33:06 +0200228 uint8_t dflt:1; /**< flag for default node */
Radek Krejci0b7704f2016-03-18 12:16:14 +0100229 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 +0100230 do not use this value! */
Michal Vasko2d162e12015-09-24 14:33:29 +0200231
232 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
233 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
234 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
235 never NULL. If there is no sibling node, pointer points to the node
236 itself. In case of the first node, this pointer points to the last
237 node in the list. */
238 struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
239
240 /* struct lyd_node *child; should be here, but is not */
241
242 /* anyxml's specific members */
Radek Krejci45826012016-08-24 15:07:57 +0200243 LYD_ANYDATA_VALUETYPE value_type;/**< type of the stored anydata value */
244 union {
245 const char *str; /**< string value, in case of printing as XML, characters like '<' or '&' are escaped */
246 struct lyxml_elem *xml; /**< xml tree */
247 struct lyd_node *tree; /**< libyang data tree, does not change the root's parent, so it is not possible
248 to get from the data tree into the anydata/anyxml */
249 } value;
Michal Vasko2d162e12015-09-24 14:33:29 +0200250};
251
252/**
Radek Krejci991a3962016-05-05 15:00:14 +0200253 * @brief list of possible types of differencies in #lyd_difflist
254 */
255typedef enum {
Radek Krejci9e6f0b82016-05-13 17:33:16 +0200256 LYD_DIFF_END = 0, /**< end of the differences list */
Radek Krejci9e6f0b82016-05-13 17:33:16 +0200257 LYD_DIFF_DELETED, /**< deleted node
258 - Node is present in the first tree, but not in the second tree.
259 - To make both trees the same the node in lyd_difflist::first can be deleted from the
260 first tree. The pointer at the same index in the lyd_difflist::second array is
261 NULL */
262 LYD_DIFF_CHANGED, /**< value of a leaf or anyxml is changed, the lyd_difflist::first and lyd_difflist::second
263 points to the leaf/anyxml instances in the first and the second tree respectively. */
Radek Krejci22d2ca92016-05-17 16:23:51 +0200264 LYD_DIFF_MOVEDAFTER1, /**< user-ordered (leaf-)list item was moved.
265 - To make both trees the same, all #LYD_DIFF_MOVEDAFTER1 transactions must be applied
Radek Krejci9e6f0b82016-05-13 17:33:16 +0200266 to the first tree in the strict order they appear in the difflist. The
267 lyd_difflist::first points to the first tree node being moved and the
268 lyd_difflist::second points to the first tree node after which the first node is
269 supposed to be moved. If the second pointer is NULL, the node is being moved into
270 the beginning as the first node of the (leaf-)list instances. */
Radek Krejci22d2ca92016-05-17 16:23:51 +0200271 LYD_DIFF_CREATED, /**< newly created node
272 - Node is present in the second tree, but not in the first tree.
273 - To make both trees the same the node in lyd_difflist::second is supposed to be
274 inserted (copied via lyd_dup()) into the node (as a child) at the same index in the
275 lyd_difflist::first array (where is its parent). If the lyd_difflist::first at the
276 index is NULL, the missing node is top-level. */
277 LYD_DIFF_MOVEDAFTER2 /**< similar to LYD_DIFF_MOVEDAFTER1, but this time the moved item is in the second tree.
278 This type is always used in combination with (as a successor of) #LYD_DIFF_CREATED
279 as an instruction to move the newly created node to a specific position. Note, that
280 due to applicability to the second tree, the meaning of lyd_difflist:first and
281 lyd_difflist:second is inverse in comparison to #LYD_DIFF_MOVEDAFTER1. The
282 lyd_difflist::second points to the (previously) created node in the second tree and
283 the lyd_difflist::first points to the predecessor node in the second tree. If the
284 predecessor is NULL, the node is supposed to bes the first sibling. */
Radek Krejci991a3962016-05-05 15:00:14 +0200285} LYD_DIFFTYPE;
286
287/**
288 * @brief Structure for the result of lyd_diff(), describing differences between two data trees.
289 */
290struct lyd_difflist {
291 LYD_DIFFTYPE *type; /**< array of the differences types, terminated by #LYD_DIFF_END value. */
292 struct lyd_node **first; /**< array of nodes in the first tree for the specific type of difference, see the
293 description of #LYD_DIFFTYPE values for more information. */
294 struct lyd_node **second;/**< array of nodes in the second tree for the specific type of difference, see the
295 description of #LYD_DIFFTYPE values for more information. */
296};
297
298/**
299 * @brief Free the result of lyd_diff(). It frees the structure of the lyd_diff() result, not the referenced nodes.
300 *
301 * @param[in] diff The lyd_diff() result to free.
302 */
303void lyd_free_diff(struct lyd_difflist *diff);
304
305/**
306 * @brief Compare two data trees and provide list of differences.
307 *
308 * Note, that the \p first and the \p second must have the same schema parent (or they must be top-level elements).
309 * In case of using #LYD_OPT_NOSIBLINGS, they both must be instances of the same schema node.
310 *
Radek Krejci913100d2016-05-09 17:23:51 +0200311 * Order of the resulting set follows these rules:
Radek Krejci22d2ca92016-05-17 16:23:51 +0200312 * - To change the first tree into the second tree, the resulting transactions are supposed to be applied in the order
313 * they appear in the result. First, the changed (#LYD_DIFF_CHANGED) nodes are described followed by the deleted
314 * (#LYD_DIFF_DELETED) nodes. Then, the moving of the user-ordered nodes present in both trees (#LYD_DIFF_MOVEDAFTER1)
315 * follows and the last transactions in the results are the newly created (#LYD_DIFF_CREATED) nodes. These nodes are
316 * supposed to be added as the last siblings, but in some case they can need additional move. In such a case, the
317 * #LYD_DIFF_MOVEDAFTER2 transactions can appear.
318 * - The order of the changed (#LYD_DIFF_CHANGED) and created (#LYD_DIFF_CREATED) follows the nodes order in the
319 * second tree - the current siblings are processed first and then the children are processed. Note, that this is
320 * actually not the BFS:
Radek Krejci9e47ddf2016-05-18 15:01:09 +0200321 *
Radek Krejci913100d2016-05-09 17:23:51 +0200322 * 1 2
323 * / \ / \
324 * 3 4 7 8
325 * / \
326 * 5 6
Radek Krejci9e47ddf2016-05-18 15:01:09 +0200327 *
Radek Krejci22d2ca92016-05-17 16:23:51 +0200328 * - The order of the deleted (#LYD_DIFF_DELETED) nodes is the DFS:
Radek Krejci9e47ddf2016-05-18 15:01:09 +0200329 *
330 * 1 6
331 * / \ / \
332 * 2 5 7 8
333 * / \
334 * 3 4
Radek Krejci913100d2016-05-09 17:23:51 +0200335 *
336 * To change the first tree into the second one, it is necessary to follow the order of transactions described in
337 * the result. Note, that it is not possible just to use the transactions in the reverse order to transform the
338 * second tree into the first one. The transactions can be generalized (to be used on a different instance of the
339 * first tree) using lyd_path() to get identifiers for the nodes used in the transactions.
340 *
Radek Krejci9a6a5dd2016-05-05 15:56:24 +0200341 * @param[in] first The first (sub)tree to compare. Without #LYD_OPT_NOSIBLINGS option, all siblings are
Radek Krejci4c3bc112016-05-19 15:09:03 +0200342 * taken into comparison. If NULL, all the \p second nodes are supposed to be top level and they will
343 * be marked as #LYD_DIFF_CREATED.
Radek Krejci9a6a5dd2016-05-05 15:56:24 +0200344 * @param[in] second The second (sub)tree to compare. Without #LYD_OPT_NOSIBLINGS option, all siblings are
Radek Krejci4c3bc112016-05-19 15:09:03 +0200345 * taken into comparison. If NULL, all the \p first nodes will be marked as #LYD_DIFF_DELETED.
Radek Krejci99d737f2016-09-06 11:19:52 +0200346 * @param[in] options The @ref diffoptions are accepted.
Radek Krejci991a3962016-05-05 15:00:14 +0200347 * @return NULL on error, the list of differences on success. In case the trees are the same, the first item in the
Radek Krejci9a6a5dd2016-05-05 15:56:24 +0200348 * lyd_difflist::type array is #LYD_DIFF_END. The returned structure is supposed to be freed by lyd_free_diff().
Radek Krejci991a3962016-05-05 15:00:14 +0200349 */
350struct lyd_difflist *lyd_diff(struct lyd_node *first, struct lyd_node *second, int options);
351
352/**
Radek Krejci99d737f2016-09-06 11:19:52 +0200353 * @defgroup diffoptions Diff options
354 * @ingroup datatree
355 *
356 * @{
357 */
358/* LYD_DIFFOPT_NOSIBLINGS value is the same as LYD_OPT_NOSIBLINGS due to backward compatibility. The LYD_OPT_NOSIBLINGS
359 * was used previously as an option for lyd_diff(). */
360#define LYD_DIFFOPT_NOSIBLINGS 0x0800 /**< The both trees to diff have to instantiate the same schema node so only the
361 single subtree is compared. */
362#define LYD_DIFFOPT_WITHDEFAULTS 0x0001 /**< Take default nodes with their values into account and handle them as part
363 of both trees. In this case, a node with defined default value cannot be
364 deleted, because when it is removed from a tree, it is implicitly replaced
365 by the default node, so the node is not #LYD_DIFF_DELETED, but
366 #LYD_DIFF_CHANGED. Note that in this case, applying the resulting
367 transactions on the first tree does not result to the exact second tree,
368 because instead of having implicit default nodes you are going to have
369 explicit default nodes. */
370/**@} diffoptions */
371
372/**
Radek Krejci6d538282016-05-05 14:24:12 +0200373 * @brief Build path (usable as XPath) of the data node.
374 * @param[in] node Data node to be processed. Note that the node should be from a complete data tree, having a subtree
375 * (after using lyd_unlink()) can cause generating invalid paths.
376 * @return NULL on error, on success the buffer for the resulting path is allocated and caller is supposed to free it
377 * with free().
378 */
379char *lyd_path(struct lyd_node *node);
380
381/**
Radek Krejcidef50022016-02-01 16:38:32 +0100382 * @defgroup parseroptions Data parser options
383 * @ingroup datatree
384 *
385 * Various options to change the data tree parsers behavior.
386 *
387 * Default behavior:
388 * - in case of XML, parser reads all data from its input (file, memory, XML tree) including the case of not well-formed
389 * XML document (multiple top-level elements) and if there is an unknown element, it is skipped including its subtree
390 * (see the next point). This can be changed by the #LYD_OPT_NOSIBLINGS option which make parser to read only a single
391 * tree (with a single root element) from its input.
392 * - parser silently ignores the data without a matching node in schema trees. If the caller want to stop
393 * parsing in case of presence of unknown data, the #LYD_OPT_STRICT can be used. The strict mode is useful for
394 * NETCONF servers, since NETCONF clients should always send data according to the capabilities announced by the server.
395 * On the other hand, the default non-strict mode is useful for clients receiving data from NETCONF server since
396 * clients are not required to understand everything the server does. Of course, the optimal strategy for clients is
397 * to use filtering to get only the required data. Having an unknown element of the known namespace is always an error.
398 * The behavior can be changed by #LYD_OPT_STRICT option.
399 * - using obsolete statements (status set to obsolete) just generates a warning, but the processing continues. The
400 * behavior can be changed by #LYD_OPT_OBSOLETE option.
401 * - parser expects that the provided data provides complete datastore content (both the configuration and state data)
402 * and performs data validation according to all YANG rules. This can be a problem in case of representing NETCONF's
403 * subtree filter data, edit-config's data or other type of data set - such data do not represent a complete data set
404 * and some of the validation rules can fail. Therefore there are other options (within lower 8 bits) to make parser
405 * to accept such a data.
Radek Krejcif3c218d2016-03-24 12:40:08 +0100406 * - when parser evaluates when-stmt condition to false, the constrained subtree is automatically removed. If the
407 * #LYD_OPT_NOAUTODEL is used, error is raised instead of silent auto delete. The option (and also this default
408 * behavior) takes effect only in case of #LYD_OPT_DATA or #LYD_OPT_CONFIG type of data.
Radek Krejcidef50022016-02-01 16:38:32 +0100409 * @{
410 */
411
412#define LYD_OPT_DATA 0x00 /**< Default type of data - complete datastore content with configuration as well as
413 state data. */
414#define LYD_OPT_CONFIG 0x01 /**< A configuration datastore - complete datastore without state data.
415 Validation modifications:
416 - status data are not allowed */
417#define LYD_OPT_GET 0x02 /**< Data content from a NETCONF reply message to the NETCONF \<get\> operation.
418 Validation modifications:
419 - mandatory nodes can be omitted
420 - leafrefs and instance-identifier are not resolved
421 - list's keys/unique nodes are not required (so duplication is not checked) */
422#define LYD_OPT_GETCONFIG 0x04 /**< Data content from a NETCONF reply message to the NETCONF \<get-config\> operation
423 Validation modifications:
424 - mandatory nodes can be omitted
425 - leafrefs and instance-identifier are not resolved
426 - list's keys/unique nodes are not required (so duplication is not checked)
427 - status data are not allowed */
428#define LYD_OPT_EDIT 0x08 /**< Content of the NETCONF \<edit-config\>'s config element.
429 Validation modifications:
430 - mandatory nodes can be omitted
431 - leafrefs and instance-identifier are not resolved
432 - status data are not allowed */
433#define LYD_OPT_RPC 0x10 /**< Data represents RPC's input parameters. */
434#define LYD_OPT_RPCREPLY 0x20 /**< Data represents RPC's output parameters (maps to NETCONF <rpc-reply> data). */
435#define LYD_OPT_NOTIF 0x40 /**< Data represents an event notification data. */
Michal Vaskob1b19442016-07-13 12:26:01 +0200436/* 0x80 reserved, formerly LYD_OPT_FILTER, now used internally */
Radek Krejcidef50022016-02-01 16:38:32 +0100437#define LYD_OPT_TYPEMASK 0xff /**< Mask to filter data type options. Always only a single data type option (only
438 single bit from the lower 8 bits) can be set. */
439
440#define LYD_OPT_STRICT 0x0100 /**< Instead of silent ignoring data without schema definition, raise an error. */
441#define LYD_OPT_DESTRUCT 0x0200 /**< Free the provided XML tree during parsing the data. With this option, the
442 provided XML tree is affected and all succesfully parsed data are freed.
443 This option is applicable only to lyd_parse_xml() function. */
444#define LYD_OPT_OBSOLETE 0x0400 /**< Raise an error when an obsolete statement (status set to obsolete) is used. */
445#define LYD_OPT_NOSIBLINGS 0x0800 /**< Parse only a single XML tree from the input. This option applies only to
446 XML input data. */
Radek Krejci93fab982016-02-03 15:58:19 +0100447#define LYD_OPT_TRUSTED 0x1000 /**< Data comes from a trusted source and it is not needed to validate them. Data
448 are connected with the schema, but the most validation checks (mandatory nodes,
449 list instance uniqueness, etc.) are not performed. This option does not make
450 sense for lyd_validate() so it is ignored by this function. */
Radek Krejci03b71f72016-03-16 11:10:09 +0100451#define LYD_OPT_NOAUTODEL 0x2000 /**< Avoid automatic delete of subtrees with false when-stmt condition. The flag is
452 applicable only in combination with LYD_OPT_DATA and LYD_OPT_CONFIG flags.
453 If used, libyang generates validation error instead of silently removing the
454 constrained subtree. */
Radek Krejcidef50022016-02-01 16:38:32 +0100455
456/**@} parseroptions */
457
458/**
459 * @brief Parse (and validate according to appropriate schema from the given context) data.
460 *
461 * In case of LY_XML format, the data string is parsed completely. It means that when it contains
462 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
463 * returned data node is a root of the first tree with other trees connected via the next pointer.
464 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
465 *
466 * @param[in] ctx Context to connect with the data tree being built here.
467 * @param[in] data Serialized data in the specified format.
468 * @param[in] format Format of the input data to be parsed.
469 * @param[in] options Parser options, see @ref parseroptions.
Michal Vasko6b44d712016-09-12 16:25:46 +0200470 * @param[in] ... Variable arguments depend on \p options. If they include:
471 * - #LYD_OPT_DATA:
472 * - #LYD_OPT_CONFIG:
473 * - #LYD_OPT_GET:
474 * - #LYD_OPT_GETCONFIG:
475 * - #LYD_OPT_EDIT:
476 * - no variable arguments expected.
477 * - #LYD_OPT_RPC:
478 * - #LYD_OPT_NOTIF:
479 * - struct lyd_node *data_tree - additional data tree that will be used
480 * when checking any "when" or "must" conditions in the parsed tree that require
481 * some nodes outside their subtree. It must be a list of top-level elements!
482 * - #LYD_OPT_RPCREPLY:
Michal Vaskod55f1092016-10-24 11:21:08 +0200483 * - const struct ::lyd_node *rpc_act - pointer to the whole RPC or action operation data
484 * tree (the request) of the reply.
Michal Vasko945b96b2016-10-18 11:49:12 +0200485 * - const struct ::lyd_node *data_tree - additional data tree that will be used
Michal Vasko6b44d712016-09-12 16:25:46 +0200486 * when checking any "when" or "must" conditions in the parsed tree that require
487 * some nodes outside their subtree. It must be a list of top-level elements!
Radek Krejcidef50022016-02-01 16:38:32 +0100488 * @return Pointer to the built data tree or NULL in case of empty \p data. To free the returned structure,
489 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
490 * #ly_errno contains appropriate error code (see #LY_ERR).
491 */
Radek Krejci722b0072016-02-01 17:09:45 +0100492struct lyd_node *lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, ...);
Radek Krejcidef50022016-02-01 16:38:32 +0100493
494/**
495 * @brief Read data from the given file descriptor.
496 *
497 * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc.
498 *
499 * In case of LY_XML format, the file content is parsed completely. It means that when it contains
500 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
501 * returned data node is a root of the first tree with other trees connected via the next pointer.
502 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
503 *
504 * @param[in] ctx Context to connect with the data tree being built here.
505 * @param[in] fd The standard file descriptor of the file containing the data tree in the specified format.
506 * @param[in] format Format of the input data to be parsed.
507 * @param[in] options Parser options, see @ref parseroptions.
Michal Vasko6b44d712016-09-12 16:25:46 +0200508 * @param[in] ... Variable arguments depend on \p options. If they include:
509 * - #LYD_OPT_DATA:
510 * - #LYD_OPT_CONFIG:
511 * - #LYD_OPT_GET:
512 * - #LYD_OPT_GETCONFIG:
513 * - #LYD_OPT_EDIT:
514 * - no variable arguments expected.
515 * - #LYD_OPT_RPC:
516 * - #LYD_OPT_NOTIF:
517 * - struct lyd_node *data_tree - additional data tree that will be used
518 * when checking any "when" or "must" conditions in the parsed tree that require
519 * some nodes outside their subtree. It must be a list of top-level elements!
520 * - #LYD_OPT_RPCREPLY:
Michal Vaskod55f1092016-10-24 11:21:08 +0200521 * - const struct ::lyd_node *rpc_act - pointer to the whole RPC or action operation data
522 * tree (the request) of the reply.
Michal Vasko945b96b2016-10-18 11:49:12 +0200523 * - const struct ::lyd_node *data_tree - additional data tree that will be used
Michal Vasko6b44d712016-09-12 16:25:46 +0200524 * when checking any "when" or "must" conditions in the parsed tree that require
525 * some nodes outside their subtree. It must be a list of top-level elements!
Radek Krejcidef50022016-02-01 16:38:32 +0100526 * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure,
527 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
528 * #ly_errno contains appropriate error code (see #LY_ERR).
529 */
530struct lyd_node *lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, ...);
531
532/**
533 * @brief Read data from the given file path.
534 *
535 * In case of LY_XML format, the file content is parsed completely. It means that when it contains
536 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
537 * returned data node is a root of the first tree with other trees connected via the next pointer.
538 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
539 *
540 * @param[in] ctx Context to connect with the data tree being built here.
541 * @param[in] path Path to the file containing the data tree in the specified format.
542 * @param[in] format Format of the input data to be parsed.
543 * @param[in] options Parser options, see @ref parseroptions.
Michal Vasko6b44d712016-09-12 16:25:46 +0200544 * @param[in] ... Variable arguments depend on \p options. If they include:
545 * - #LYD_OPT_DATA:
546 * - #LYD_OPT_CONFIG:
547 * - #LYD_OPT_GET:
548 * - #LYD_OPT_GETCONFIG:
549 * - #LYD_OPT_EDIT:
550 * - no variable arguments expected.
551 * - #LYD_OPT_RPC:
552 * - #LYD_OPT_NOTIF:
553 * - struct lyd_node *data_tree - additional data tree that will be used
554 * when checking any "when" or "must" conditions in the parsed tree that require
555 * some nodes outside their subtree. It must be a list of top-level elements!
556 * - #LYD_OPT_RPCREPLY:
Michal Vaskod55f1092016-10-24 11:21:08 +0200557 * - const struct ::lyd_node *rpc_act - pointer to the whole RPC or action operation data
558 * tree (the request) of the reply.
Michal Vasko945b96b2016-10-18 11:49:12 +0200559 * - const struct ::lyd_node *data_tree - additional data tree that will be used
Michal Vasko6b44d712016-09-12 16:25:46 +0200560 * when checking any "when" or "must" conditions in the parsed tree that require
561 * some nodes outside their subtree. It must be a list of top-level elements!
Radek Krejcidef50022016-02-01 16:38:32 +0100562 * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure,
563 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
564 * #ly_errno contains appropriate error code (see #LY_ERR).
565 */
566struct lyd_node *lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options, ...);
567
568/**
569 * @brief Parse (and validate according to appropriate schema from the given context) XML tree.
570 *
571 * The output data tree is parsed from the given XML tree previously parsed by one of the
572 * lyxml_read* functions.
573 *
Radek Krejci722b0072016-02-01 17:09:45 +0100574 * 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 +0100575 * or the provided root is a root element of a subtree), all the sibling nodes (previous as well as
576 * following) are processed as well. The returned data node is a root of the first tree with other
577 * trees connected via the next pointer. This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
578 *
579 * When the function is used with #LYD_OPT_DESTRUCT, all the successfully parsed data including the
580 * XML \p root and all its siblings (if #LYD_OPT_NOSIBLINGS is not used) are freed. Only with
581 * #LYD_OPT_DESTRUCT option the \p root pointer is changed - if all the data are parsed, it is set
582 * to NULL, otherwise it will hold the XML tree without the successfully parsed elements.
583 *
584 * The context must be the same as the context used to parse XML tree by lyxml_read* function.
585 *
586 * @param[in] ctx Context to connect with the data tree being built here.
587 * @param[in,out] root XML tree to parse (convert) to data tree. By default, parser do not change the XML tree. However,
588 * when #LYD_OPT_DESTRUCT is specified in \p options, parser frees all successfully parsed data.
589 * @param[in] options Parser options, see @ref parseroptions.
Michal Vasko6b44d712016-09-12 16:25:46 +0200590 * @param[in] ... Variable arguments depend on \p options. If they include:
591 * - #LYD_OPT_DATA:
592 * - #LYD_OPT_CONFIG:
593 * - #LYD_OPT_GET:
594 * - #LYD_OPT_GETCONFIG:
595 * - #LYD_OPT_EDIT:
596 * - no variable arguments expected.
597 * - #LYD_OPT_RPC:
598 * - #LYD_OPT_NOTIF:
599 * - struct lyd_node *data_tree - additional data tree that will be used
600 * when checking any "when" or "must" conditions in the parsed tree that require
601 * some nodes outside their subtree. It must be a list of top-level elements!
602 * - #LYD_OPT_RPCREPLY:
Michal Vaskod55f1092016-10-24 11:21:08 +0200603 * - const struct ::lyd_node *rpc_act - pointer to the whole RPC or action operation data
604 * tree (the request) of the reply.
Michal Vasko945b96b2016-10-18 11:49:12 +0200605 * - const struct ::lyd_node *data_tree - additional data tree that will be used
Michal Vasko6b44d712016-09-12 16:25:46 +0200606 * when checking any "when" or "must" conditions in the parsed tree that require
607 * some nodes outside their subtree. It must be a list of top-level elements!
Radek Krejcidef50022016-02-01 16:38:32 +0100608 * @return Pointer to the built data tree or NULL in case of empty \p root. To free the returned structure,
609 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
610 * #ly_errno contains appropriate error code (see #LY_ERR).
611 */
612struct lyd_node *lyd_parse_xml(struct ly_ctx *ctx, struct lyxml_elem **root, int options,...);
613
614/**
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200615 * @brief Create a new container node in a data tree.
616 *
617 * @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 +0200618 * @param[in] module Module with the node being created.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200619 * @param[in] name Schema node name of the new data node. The node can be #LYS_CONTAINER, #LYS_LIST,
Michal Vasko945b96b2016-10-18 11:49:12 +0200620 * #LYS_NOTIF, #LYS_RPC, or #LYS_ACTION.
Michal Vasko1dca6882015-10-22 14:29:42 +0200621 * @return New node, NULL on error.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200622 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100623struct lyd_node *lyd_new(struct lyd_node *parent, const struct lys_module *module, const char *name);
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200624
625/**
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200626 * @brief Create a new leaf or leaflist node in a data tree with a string value that is converted to
627 * the actual value.
628 *
629 * @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 +0200630 * @param[in] module Module with the node being created.
631 * @param[in] name Schema node name of the new data node.
Michal Vasko3e671b52015-10-23 16:23:15 +0200632 * @param[in] val_str String form of the value of the node being created. In case the type is #LY_TYPE_INST
633 * 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 +0200634 * @return New node, NULL on error.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200635 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100636struct lyd_node *lyd_new_leaf(struct lyd_node *parent, const struct lys_module *module, const char *name,
Michal Vasko3e671b52015-10-23 16:23:15 +0200637 const char *val_str);
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200638
639/**
Radek Krejcib9b4d002016-01-18 13:08:51 +0100640 * @brief Change value of a leaf node.
641 *
642 * Despite the prototype allows to provide a leaflist node as \p leaf parameter, only leafs are accepted.
Michal Vasko3a55a8a2016-04-13 14:19:53 +0200643 * Also, changing the value of a list key is prohibited.
Radek Krejcib9b4d002016-01-18 13:08:51 +0100644 *
Radek Krejci0562dbc2016-04-18 14:18:26 +0200645 * As for the other data tree manipulation functions, the change is not fully validated to allow multiple changes
646 * in the data tree. Therefore, when all changes on the data tree are done, caller is supposed to call lyd_validate()
647 * to check that the result is valid data tree. Specifically, if a leafref leaf is changed, it is not checked that
648 * the (leafref) value is correct.
649 *
Radek Krejcib9b4d002016-01-18 13:08:51 +0100650 * @param[in] leaf A leaf node to change.
651 * @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
652 * or #LY_TYPE_IDENT, JSON node-id format is expected (nodes are prefixed with module names, not XML namespaces).
653 * @return 0 on success, non-zero on error.
654 */
655int lyd_change_leaf(struct lyd_node_leaf_list *leaf, const char *val_str);
656
657/**
Radek Krejci45826012016-08-24 15:07:57 +0200658 * @brief Create a new anydata or anyxml node in a data tree.
659 *
660 * This function is supposed to be a replacement for the lyd_new_anyxml_str() and lyd_new_anyxml_xml().
Michal Vasko2d162e12015-09-24 14:33:29 +0200661 *
Michal Vasko2d162e12015-09-24 14:33:29 +0200662 * @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 +0200663 * @param[in] module Module with the node being created.
Radek Krejci45826012016-08-24 15:07:57 +0200664 * @param[in] name Schema node name of the new data node. The schema node determines if the anydata or anyxml node
665 * is created.
666 * @param[in] value Pointer to the value data to be stored in the anydata/anyxml node. The type of the data is
667 * determined according to the \p value_type parameter.
668 * @param[in] value_type Type of the provided data \p value.
Michal Vasko1dca6882015-10-22 14:29:42 +0200669 * @return New node, NULL on error.
Michal Vasko2d162e12015-09-24 14:33:29 +0200670 */
Radek Krejci45826012016-08-24 15:07:57 +0200671struct lyd_node *lyd_new_anydata(struct lyd_node *parent, const struct lys_module *module, const char *name,
672 void *value, LYD_ANYDATA_VALUETYPE value_type);
Michal Vasko2d162e12015-09-24 14:33:29 +0200673
674/**
Michal Vasko945b96b2016-10-18 11:49:12 +0200675 * @brief Create a new container node in a data tree. Ignore RPC/action input nodes and instead use RPC/action output ones.
Michal Vasko0df122f2015-12-14 13:38:21 +0100676 *
Michal Vasko98a5a742016-05-11 11:02:56 +0200677 * @param[in] parent Parent node for the node being created. NULL in case of creating top level element.
678 * @param[in] module Module with the node being created.
Michal Vasko945b96b2016-10-18 11:49:12 +0200679 * @param[in] name Schema node name of the new data node. The node should only be #LYS_CONTAINER or #LYS_LIST,
680 * but accepted are also #LYS_NOTIF, #LYS_RPC, or #LYS_ACTION.
Michal Vasko0df122f2015-12-14 13:38:21 +0100681 * @return New node, NULL on error.
682 */
Michal Vasko98a5a742016-05-11 11:02:56 +0200683struct lyd_node *lyd_new_output(struct lyd_node *parent, const struct lys_module *module, const char *name);
Michal Vasko50c0a872016-01-13 14:34:11 +0100684
685/**
Michal Vasko98a5a742016-05-11 11:02:56 +0200686 * @brief Create a new leaf or leaflist node in a data tree with a string value that is converted to
Michal Vasko945b96b2016-10-18 11:49:12 +0200687 * the actual value. Ignore RPC/action input nodes and instead use RPC/action output ones.
Michal Vasko50c0a872016-01-13 14:34:11 +0100688 *
Michal Vasko98a5a742016-05-11 11:02:56 +0200689 * @param[in] parent Parent node for the node being created. NULL in case of creating top level element.
690 * @param[in] module Module with the node being created.
691 * @param[in] name Schema node name of the new data node.
Michal Vasko50c0a872016-01-13 14:34:11 +0100692 * @param[in] val_str String form of the value of the node being created. In case the type is #LY_TYPE_INST
693 * or #LY_TYPE_IDENT, JSON node-id format is expected (nodes are prefixed with module names, not XML namespaces).
694 * @return New node, NULL on error.
695 */
Michal Vasko98a5a742016-05-11 11:02:56 +0200696struct lyd_node *lyd_new_output_leaf(struct lyd_node *parent, const struct lys_module *module, const char *name,
697 const char *val_str);
Michal Vasko50c0a872016-01-13 14:34:11 +0100698
699/**
Michal Vasko945b96b2016-10-18 11:49:12 +0200700 * @brief Create a new anydata or anyxml node in a data tree. Ignore RPC/action input nodes and instead use
701 * RPC/action output ones.
Michal Vasko50c0a872016-01-13 14:34:11 +0100702 *
Michal Vasko98a5a742016-05-11 11:02:56 +0200703 * @param[in] parent Parent node for the node being created. NULL in case of creating top level element.
704 * @param[in] module Module with the node being created.
Radek Krejci45826012016-08-24 15:07:57 +0200705 * @param[in] name Schema node name of the new data node. The schema node determines if the anydata or anyxml node
706 * is created.
707 * @param[in] value Pointer to the value data to be stored in the anydata/anyxml node. The type of the data is
708 * determined according to the \p value_type parameter. Data are supposed to be dynamically allocated.
709 * Since it is directly attached into the created data node, caller is supposed to not manipulate with
710 * the data after a successful call (including calling free() on the provided data).
711 * @param[in] value_type Type of the provided data \p value.
Michal Vasko50c0a872016-01-13 14:34:11 +0100712 * @return New node, NULL on error.
713 */
Radek Krejci45826012016-08-24 15:07:57 +0200714struct lyd_node *lyd_new_output_anydata(struct lyd_node *parent, const struct lys_module *module, const char *name,
715 void *value, LYD_ANYDATA_VALUETYPE value_type);
Michal Vasko0df122f2015-12-14 13:38:21 +0100716
717/**
Michal Vaskof5299282016-03-16 13:32:02 +0100718 * @defgroup pathoptions Data path creation options
719 * @ingroup datatree
720 *
721 * Various options to change lyd_new_path() behavior.
722 *
723 * Default behavior:
Michal Vaskof5299282016-03-16 13:32:02 +0100724 * - if the target node already exists, an error is returned.
Michal Vasko9db078d2016-03-23 11:08:51 +0100725 * - the whole path to the target node is created (with any missing parents) if necessary.
Michal Vasko2411b942016-03-23 13:50:03 +0100726 * - RPC output schema children are completely ignored in all modules. Input is searched and nodes created normally.
Michal Vaskof5299282016-03-16 13:32:02 +0100727 * @{
728 */
729
Michal Vasko72d35102016-03-31 10:03:38 +0200730#define LYD_PATH_OPT_UPDATE 0x01 /**< If the target node exists and is a leaf, it is updated with the new value and returned.
731 If the target node exists and is not a leaf, NULL is returned and no error set. */
Michal Vasko9db078d2016-03-23 11:08:51 +0100732#define LYD_PATH_OPT_NOPARENT 0x02 /**< If any parents of the target node exist, return an error. */
Michal Vasko945b96b2016-10-18 11:49:12 +0200733#define LYD_PATH_OPT_OUTPUT 0x04 /**< Changes the behavior to ignoring RPC/action input schema nodes and using only output ones. */
Michal Vaskof5299282016-03-16 13:32:02 +0100734
735/** @} pathoptions */
736
737/**
738 * @brief Create a new data node based on a simple XPath.
739 *
Michal Vasko8d18ef52016-04-06 12:21:46 +0200740 * The new node is normally inserted at the end, either as the last child of a parent or as the last sibling
741 * if working with top-level elements. However, when manipulating RPC input or output, schema ordering is
Michal Vasko98a5a742016-05-11 11:02:56 +0200742 * required and always guaranteed.
Michal Vasko58f74f12016-03-24 13:26:06 +0100743 *
Michal Vasko8c419642016-04-13 14:22:01 +0200744 * If \p path points to a list key and the list does not exist, the key value from the predicate is used
745 * and \p value is ignored.
746 *
Michal Vasko945b96b2016-10-18 11:49:12 +0200747 * @param[in] data_tree Existing data tree to add to/modify. If creating RPCs/actions, there should only be one
748 * RPC/action and either input or output, not both. Can be NULL.
Michal Vaskof5299282016-03-16 13:32:02 +0100749 * @param[in] ctx Context to use. Mandatory if \p data_tree is NULL.
Michal Vasko9db078d2016-03-23 11:08:51 +0100750 * @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 +0100751 * 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 +0200752 * with its value as well, leaves and leaf-lists can have predicates too that have preference over \p value,
753 * see @ref howtoxpath.
Radek Krejci45826012016-08-24 15:07:57 +0200754 * @param[in] value Value of the new leaf/lealf-list (const char*). If creating anydata or anyxml, the following
755 * \p value_type parameter is required to be specified correctly. If creating nodes of other types, the
756 * parameter is ignored.
757 * @param[in] value_type Type of the provided \p value parameter in case of creating anydata or anyxml node.
Michal Vaskof5299282016-03-16 13:32:02 +0100758 * @param[in] options Bitmask of options flags, see @ref pathoptions.
Michal Vasko8c419642016-04-13 14:22:01 +0200759 * @return First created (or updated with #LYD_PATH_OPT_UPDATE) node,
Michal Vasko17bb4902016-04-05 15:20:51 +0200760 * 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 +0200761 * NULL and ly_errno is set on error.
Michal Vaskof5299282016-03-16 13:32:02 +0100762 */
Radek Krejci45826012016-08-24 15:07:57 +0200763struct lyd_node *lyd_new_path(struct lyd_node *data_tree, struct ly_ctx *ctx, const char *path, void *value,
764 LYD_ANYDATA_VALUETYPE value_type, int options);
Michal Vaskof5299282016-03-16 13:32:02 +0100765
766/**
Michal Vaskoc0797f82015-10-14 15:51:25 +0200767 * @brief Create a copy of the specified data tree \p node. Namespaces are copied as needed,
768 * schema references are kept the same.
Michal Vasko2d162e12015-09-24 14:33:29 +0200769 *
770 * @param[in] node Data tree node to be duplicated.
771 * @param[in] recursive 1 if all children are supposed to be also duplicated.
772 * @return Created copy of the provided data \p node.
773 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100774struct lyd_node *lyd_dup(const struct lyd_node *node, int recursive);
Michal Vasko2d162e12015-09-24 14:33:29 +0200775
776/**
Michal Vasko45fb2822016-04-18 13:32:17 +0200777 * @brief Merge a (sub)tree into a data tree. Missing nodes are merged, leaf values updated.
778 * If \p target and \p source do not share the top-level schema node, even if they
779 * are from different modules, \p source parents up to top-level node will be created and
780 * linked to the \p target (but only containers can be created this way, lists need keys,
781 * so if lists are missing, an error will be returned).
782 *
783 * In short, this function will always try to return a fully valid data tree and will fail
Michal Vaskocf6dc7e2016-04-18 16:00:37 +0200784 * if it is not possible. Also, in some less common cases, despite both trees \p target and
785 * \p source are valid, the resulting tree may be invalid and this function will succeed.
786 * If you know there are such possibilities in your data trees or you are not sure, always
787 * validate the resulting merged \p target tree.
Michal Vasko45fb2822016-04-18 13:32:17 +0200788 *
Michal Vaskocf6dc7e2016-04-18 16:00:37 +0200789 * @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 +0200790 * @param[in] source Data tree to merge \p target with. Must be valid (at least as a subtree).
791 * @param[in] options Bitmask of 2 option flags:
Michal Vasko0300b532016-09-14 12:16:02 +0200792 * - #LYD_OPT_DESTRUCT - spend \p source in the function, otherwise \p source is left untouched,
793 * - #LYD_OPT_NOSIBLINGS - merge only the \p source subtree (ignore siblings), otherwise merge
794 * \p source and all its succeeding siblings (preceeding ones are still ignored!),
795 * - #LYD_OPT_EXPLICIT - when merging an explicitly set node and a default node, always put
796 * the explicit node into \p target, otherwise the node which is in \p source is used.
Michal Vasko45fb2822016-04-18 13:32:17 +0200797 * @return 0 on success, nonzero in case of an error.
798 */
799int lyd_merge(struct lyd_node *target, const struct lyd_node *source, int options);
800
Michal Vasko0300b532016-09-14 12:16:02 +0200801#define LYD_OPT_EXPLICIT 0x0100
802
Michal Vasko45fb2822016-04-18 13:32:17 +0200803/**
Michal Vasko2d162e12015-09-24 14:33:29 +0200804 * @brief Insert the \p node element as child to the \p parent element. The \p node is inserted as a last child of the
805 * \p parent.
806 *
Michal Vaskob6c51f12016-09-14 12:15:11 +0200807 * - if the node is part of some other tree, it is automatically unlinked.
808 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
809 * - if the key of a list is being inserted, it is placed into a correct position instead of being placed as the last
Radek Krejcia1c33bf2016-09-07 12:38:49 +0200810 * element.
Michal Vaskob6c51f12016-09-14 12:15:11 +0200811 * - if the target tree includes the default instance of the node being inserted, the default node is silently replaced
Michal Vasko3c126822016-09-22 13:48:42 +0200812 * by the new node.
813 * - if a default node is being inserted and the target tree already contains non-default instance, the existing
814 * instance is silently replaced. If it contains the exact same default node, it is replaced as well.
Michal Vaskob6c51f12016-09-14 12:15:11 +0200815 * - if a non-default node is being inserted and there is already its non-default instance in the target tree, the new
Radek Krejcifd0bcf02016-09-09 13:28:34 +0200816 * node is inserted and it is up to the caller to solve the presence of multiple instances afterwards.
817 *
818 * Note that this function differs from lyd_insert_before() and lyd_insert_after() because the position of the
819 * node being inserted is determined automatically according to the rules described above. In contrast to
820 * lyd_insert_parent(), lyd_insert() can not be used for top-level elements since the \p parent parameter must not be
Michal Vasko3c126822016-09-22 13:48:42 +0200821 * NULL. If inserting something larger and not fitting the mentioned use-cases (or simply if unsure), you can always
822 * use lyd_merge(), it should be able to handle any situation.
Michal Vasko2d162e12015-09-24 14:33:29 +0200823 *
824 * @param[in] parent Parent node for the \p node being inserted.
825 * @param[in] node The node being inserted.
Michal Vasko24337392015-10-16 09:58:16 +0200826 * @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 +0200827 * in the data tree.
828 */
Michal Vasko24337392015-10-16 09:58:16 +0200829int lyd_insert(struct lyd_node *parent, struct lyd_node *node);
Michal Vasko2d162e12015-09-24 14:33:29 +0200830
831/**
Radek Krejcifd0bcf02016-09-09 13:28:34 +0200832 * @brief Insert the \p node element as a last sibling of the specified \p sibling element.
833 *
Michal Vaskob6c51f12016-09-14 12:15:11 +0200834 * - if the node is part of some other tree, it is automatically unlinked.
835 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
836 * - if the key of a list is being inserted, it is placed into a correct position instead of being placed as the last
Radek Krejcifd0bcf02016-09-09 13:28:34 +0200837 * element.
Michal Vaskob6c51f12016-09-14 12:15:11 +0200838 * - if the target tree includes the default instance of the node being inserted, the default node is silently replaced
Michal Vasko3c126822016-09-22 13:48:42 +0200839 * by the new node.
840 * - if a default node is being inserted and the target tree already contains non-default instance, the existing
841 * instance is silently replaced. If it contains the exact same default node, it is replaced as well.
Michal Vaskob6c51f12016-09-14 12:15:11 +0200842 * - if a non-default node is being inserted and there is already its non-default instance in the target tree, the new
843 * node is inserted and it is up to the caller to solve the presence of multiple instances afterwards.
Radek Krejcifd0bcf02016-09-09 13:28:34 +0200844 *
845 * Note that this function differs from lyd_insert_before() and lyd_insert_after() because the position of the
846 * node being inserted is determined automatically as in the case of lyd_insert(). In contrast to lyd_insert(),
Michal Vasko3c126822016-09-22 13:48:42 +0200847 * lyd_insert_sibling() can be used to insert top-level elements. If inserting something larger and not fitting
848 * the mentioned use-cases (or simply if unsure), you can always use lyd_merge(), it should be able to handle
849 * any situation.
Radek Krejcifd0bcf02016-09-09 13:28:34 +0200850 *
851 * @param[in,out] sibling Sibling node as a reference where to insert the \p node. When function succeeds, the sibling
852 * is always set to point to the first sibling node. Note that in some cases described above, the provided sibling
853 * node could be removed from the tree.
854 * @param[in] node The node being inserted.
855 * @return 0 on success, nonzero in case of error, e.g. when the node is being inserted to an inappropriate place
856 * in the data tree.
857 */
858int lyd_insert_sibling(struct lyd_node **sibling, struct lyd_node *node);
859
860/**
Michal Vasko3f7dba12015-10-15 13:09:27 +0200861 * @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 +0100862 * siblings (just moving \p node position), skip validation.
Michal Vasko2d162e12015-09-24 14:33:29 +0200863 *
Michal Vaskob6c51f12016-09-14 12:15:11 +0200864 * - if the target tree includes the default instance of the node being inserted, the default node is silently removed.
Michal Vasko3c126822016-09-22 13:48:42 +0200865 * - if a default node is being inserted and the target tree already contains non-default instance, the existing
866 * instance is removed. If it contains the exact same default node, it is removed as well.
Michal Vaskob6c51f12016-09-14 12:15:11 +0200867 * - if a non-default node is being inserted and there is already its non-default instance in the target tree, the new
868 * node is inserted and it is up to the caller to solve the presence of multiple instances afterwards.
869 *
Michal Vasko2d162e12015-09-24 14:33:29 +0200870 * @param[in] sibling The data tree node before which the \p node will be inserted.
Radek Krejci20a5f292016-02-09 15:04:49 +0100871 * @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 +0200872 * @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 +0200873 * in the data tree.
874 */
Michal Vasko24337392015-10-16 09:58:16 +0200875int lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node);
Michal Vasko2d162e12015-09-24 14:33:29 +0200876
877/**
Radek Krejci20a5f292016-02-09 15:04:49 +0100878 * @brief Insert the \p node element after the \p sibling element. If \p node and \p siblings are already
879 * siblings (just moving \p node position), skip validation.
Michal Vasko2d162e12015-09-24 14:33:29 +0200880 *
Michal Vaskob6c51f12016-09-14 12:15:11 +0200881 * - if the target tree includes the default instance of the node being inserted, the default node is silently removed.
Michal Vasko3c126822016-09-22 13:48:42 +0200882 * - if a default node is being inserted and the target tree already contains non-default instance, the existing
883 * instance is removed. If it contains the exact same default node, it is removed as well.
Michal Vaskob6c51f12016-09-14 12:15:11 +0200884 * - if a non-default node is being inserted and there is already its non-default instance in the target tree, the new
885 * node is inserted and it is up to the caller to solve the presence of multiple instances afterwards.
886 *
Michal Vasko3f7dba12015-10-15 13:09:27 +0200887 * @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 +0100888 * are already siblings (just moving \p node position), skip validation.
Radek Krejci20a5f292016-02-09 15:04:49 +0100889 * @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 +0200890 * @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 +0200891 * in the data tree.
892 */
Michal Vasko24337392015-10-16 09:58:16 +0200893int lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node);
894
895/**
Radek Krejcic090d992016-09-07 16:26:03 +0200896 * @brief Insert the \p new element instead of the \p old element.
897 *
898 * If the \p new is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
899 * If the \p new is NULL and \p destroy is true, it works like lyd_free(old).
900 *
Radek Krejci36525672016-09-27 09:38:11 +0200901 * @param[in] orig The specific node in the original tree supposed to be replaced.
902 * @param[in] repl The new (list of) node(s) to be inserted instead of \p old
Radek Krejcic090d992016-09-07 16:26:03 +0200903 * @param[in] destroy Flag for freeing the \p old.
904 * @return 0 on success, nonzero in case of error.
905 */
Radek Krejci36525672016-09-27 09:38:11 +0200906int lyd_replace(struct lyd_node *orig, struct lyd_node *repl, int destroy);
Radek Krejcic090d992016-09-07 16:26:03 +0200907
908/**
Michal Vasko2411b942016-03-23 13:50:03 +0100909 * @brief Order siblings according to the schema node ordering.
910 *
Michal Vasko58f74f12016-03-24 13:26:06 +0100911 * If the siblings include data nodes from other modules, they are
912 * sorted based on the module order in the context.
913 *
914 * @param[in] sibling Node, whose siblings will be sorted.
915 * @param[in] recursive Whether sort all siblings of siblings, recursively.
916 * @return 0 on success, nonzero in case of an error.
Michal Vasko2411b942016-03-23 13:50:03 +0100917 */
Michal Vasko58f74f12016-03-24 13:26:06 +0100918int lyd_schema_sort(struct lyd_node *sibling, int recursive);
Michal Vasko2411b942016-03-23 13:50:03 +0100919
920/**
Michal Vasko105cef12016-02-04 12:06:26 +0100921 * @brief Search in the given data for instances of nodes matching the provided XPath expression.
922 *
Michal Vasko7fdf9b32016-03-01 15:59:48 +0100923 * The XPath expression is evaluated on data -> skip all non-data nodes (input, output, choice, case).
Michal Vasko105cef12016-02-04 12:06:26 +0100924 *
Michal Vasko7fdf9b32016-03-01 15:59:48 +0100925 * Expr examples:
926 * "/ietf-yang-library:modules-state/module[name = 'ietf-yang-library']/namespace"
927 * "/ietf-netconf:get-config/source"
928 *
Michal Vasko46a4bf92016-09-08 08:23:49 +0200929 * @param[in] data Node in the data tree considered the context node if \p expr is relative,
930 * otherwise any node.
Michal Vasko105cef12016-02-04 12:06:26 +0100931 * @param[in] expr XPath expression filtering the matching nodes.
Michal Vasko46a4bf92016-09-08 08:23:49 +0200932 * @return Set of found data nodes. If no nodes are matching \p expr or the result
Michal Vasko105cef12016-02-04 12:06:26 +0100933 * would be a number, a string, or a boolean, the returned set is empty. In case of an error, NULL is returned.
934 */
Michal Vaskof06fb5b2016-09-08 10:05:56 +0200935struct ly_set *lyd_find_xpath(const struct lyd_node *data, const char *expr);
Michal Vasko105cef12016-02-04 12:06:26 +0100936
937/**
Radek Krejcic5b6b912016-01-18 16:35:35 +0100938 * @brief Search in the given data for instances of the provided schema node.
939 *
940 * The \p data is used to find the data root and function then searches in the whole tree and all sibling trees.
941 *
942 * @param[in] data A node in the data tree to search.
943 * @param[in] schema Schema node of the data nodes caller want to find.
Michal Vasko46a4bf92016-09-08 08:23:49 +0200944 * @return Set of found data nodes. If no data node is found, the returned set is empty.
Radek Krejcic5b6b912016-01-18 16:35:35 +0100945 * In case of error, NULL is returned.
946 */
Michal Vaskof06fb5b2016-09-08 10:05:56 +0200947struct ly_set *lyd_find_instance(const struct lyd_node *data, const struct lys_node *schema);
Radek Krejcic5b6b912016-01-18 16:35:35 +0100948
949/**
Radek Krejcid788a522016-07-25 14:57:38 +0200950 * @brief Get the first sibling of the given node.
951 *
952 * @param[in] node Node which first sibling is going to be the result.
953 * @return The first sibling of the given node or the node itself if it is the first child of the parent.
954 */
955struct lyd_node *lyd_first_sibling(struct lyd_node *node);
956
957/**
Radek Krejcibe90a902016-06-14 16:11:51 +0200958 * @brief Resolve the leafref.
959 *
960 * This function is considered to be a part of a low level API and it should be used deliberately.
961 *
962 * @param[in] leafref The leafref node to resolve.
963 * @return
964 * - EXIT_SUCCESS on success,
965 * - EXIT_FAILURE when target does not exist,
966 * - -1 on error.
967 */
968int lyd_validate_leafref(struct lyd_node_leaf_list *leafref);
969
970/**
Michal Vasko24337392015-10-16 09:58:16 +0200971 * @brief Validate \p node data subtree.
972 *
Michal Vaskodedea832016-04-19 11:24:45 +0200973 * @param[in,out] node Data tree to be validated. In case the \p options does not includes #LYD_OPT_NOAUTODEL, libyang
Michal Vaskob2f40be2016-09-08 16:03:48 +0200974 * can modify the provided tree including the root \p node.
Michal Vasko24337392015-10-16 09:58:16 +0200975 * @param[in] options Options for the inserting data to the target data tree options, see @ref parseroptions.
Michal Vaskocdb90172016-09-13 09:34:36 +0200976 * @param[in] var_arg Variable argument depends on \p options. If they include:
Michal Vasko6b44d712016-09-12 16:25:46 +0200977 * - #LYD_OPT_DATA:
978 * - #LYD_OPT_CONFIG:
979 * - #LYD_OPT_GET:
980 * - #LYD_OPT_GETCONFIG:
981 * - #LYD_OPT_EDIT:
Michal Vaskocdb90172016-09-13 09:34:36 +0200982 * - struct ly_ctx *ctx - context to use when \p node is NULL (for checking an empty tree),
983 * otherwise can be NULL.
Michal Vasko6b44d712016-09-12 16:25:46 +0200984 * - #LYD_OPT_RPC:
985 * - #LYD_OPT_RPCREPLY:
986 * - #LYD_OPT_NOTIF:
987 * - struct ::lyd_node *data_tree - additional data tree that will be used when checking
Michal Vaskocdb90172016-09-13 09:34:36 +0200988 * any "when" or "must" conditions in the \p node tree
989 * that require some nodes outside their subtree. If set,
990 * it must be a list of top-level elements!
Radek Krejci92ece002016-04-04 15:45:05 +0200991 * @return 0 on success, nonzero in case of an error.
Michal Vasko24337392015-10-16 09:58:16 +0200992 */
Michal Vaskocdb90172016-09-13 09:34:36 +0200993int lyd_validate(struct lyd_node **node, int options, void *var_arg);
Michal Vasko2d162e12015-09-24 14:33:29 +0200994
995/**
Radek Krejci46180b52016-08-31 16:01:32 +0200996 * @brief Get know if the node contain (despite implicit or explicit) default value.
Radek Krejci7b4309c2016-03-23 10:30:29 +0100997 *
Radek Krejci46180b52016-08-31 16:01:32 +0200998 * @param[in] node The leaf or leaf-list to check. Note, that leaf-list is marked as default only when the complete
999 * and only the default set is present (node's siblings are also checked).
1000 * @return 1 if the node contains the default value, 0 otherwise.
Radek Krejci7b4309c2016-03-23 10:30:29 +01001001 */
Radek Krejci46180b52016-08-31 16:01:32 +02001002int lyd_wd_default(struct lyd_node_leaf_list *node);
Radek Krejci6b8f6ac2016-03-23 12:33:04 +01001003
1004/**
Michal Vasko55f60be2015-10-14 13:12:58 +02001005 * @brief Unlink the specified data subtree. All referenced namespaces are copied.
Michal Vasko2d162e12015-09-24 14:33:29 +02001006 *
1007 * Note, that the node's connection with the schema tree is kept. Therefore, in case of
1008 * reconnecting the node to a data tree using lyd_paste() it is necessary to paste it
1009 * to the appropriate place in the data tree following the schema.
1010 *
1011 * @param[in] node Data tree node to be unlinked (together with all children).
1012 * @return 0 for success, nonzero for error
1013 */
1014int lyd_unlink(struct lyd_node *node);
1015
1016/**
Radek Krejcifd0bcf02016-09-09 13:28:34 +02001017 * @brief Free (and unlink) the specified data subtree. Use carefully, since libyang silently creates default nodes,
1018 * it is always better to use lyd_free_withsiblings() to free the complete data tree.
Michal Vasko2d162e12015-09-24 14:33:29 +02001019 *
1020 * @param[in] node Root of the (sub)tree to be freed.
1021 */
1022void lyd_free(struct lyd_node *node);
1023
1024/**
Radek Krejcifd0bcf02016-09-09 13:28:34 +02001025 * @brief Free (and unlink) the specified data tree and all its siblings (preceding as well as following).
Radek Krejci81468402016-01-07 13:52:40 +01001026 *
1027 * @param[in] node One of the siblings root element of the (sub)trees to be freed.
1028 */
1029void lyd_free_withsiblings(struct lyd_node *node);
1030
1031/**
Radek Krejci134610e2015-10-20 17:15:34 +02001032 * @brief Insert attribute into the data node.
1033 *
1034 * @param[in] parent Data node where to place the attribute
Radek Krejci70ecd722016-03-21 09:04:00 +01001035 * @param[in] mod An alternative way to specify attribute's module (namespace) used in case the \p name does
1036 * not include prefix. If neither prefix in the \p name nor mod is specified, the attribute's
1037 * module is inherited from the \p parent node. It is not allowed to have attributes with no
1038 * module (namespace).
1039 * @param[in] name Attribute name. The string can include the attribute's module (namespace) as the name's
1040 * prefix (prefix:name). Prefix must be the name of one of the schema in the \p parent's context.
1041 * If the prefix is not specified, the \p mod parameter is used. If neither of these parameters is
1042 * usable, attribute inherits module (namespace) from the \p parent node. It is not allowed to
1043 * have attributes with no module (namespace).
Radek Krejci134610e2015-10-20 17:15:34 +02001044 * @param[in] value Attribute value
1045 * @return pointer to the created attribute (which is already connected in \p parent) or NULL on error.
1046 */
Radek Krejci70ecd722016-03-21 09:04:00 +01001047struct lyd_attr *lyd_insert_attr(struct lyd_node *parent, const struct lys_module *mod, const char *name,
1048 const char *value);
Radek Krejci134610e2015-10-20 17:15:34 +02001049
1050/**
Radek Krejci88f29302015-10-30 15:42:33 +01001051 * @brief Destroy data attribute
1052 *
1053 * If the attribute to destroy is a member of a node attribute list, it is necessary to
1054 * provide the node itself as \p parent to keep the list consistent.
1055 *
1056 * @param[in] ctx Context where the attribute was created (usually it is the context of the \p parent)
1057 * @param[in] parent Parent node where the attribute is placed
1058 * @param[in] attr Attribute to destroy
1059 * @param[in] recursive Zero to destroy only the attribute, non-zero to destroy also all the subsequent attributes
1060 * in the list.
1061 */
1062void lyd_free_attr(struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_attr *attr, int recursive);
1063
1064/**
Radek Krejci6910a032016-04-13 10:06:21 +02001065 * @brief Return main module of the data tree node.
1066 *
1067 * In case of regular YANG module, it returns ::lys_node#module pointer,
1068 * but in case of submodule, it returns pointer to the main module.
1069 *
1070 * @param[in] node Data tree node to be examined
1071 * @return pointer to the main module (schema structure), NULL in case of error.
1072 */
1073struct lys_module *lyd_node_module(const struct lyd_node *node);
1074
1075/**
Radek Krejcidef50022016-02-01 16:38:32 +01001076* @brief Print data tree in the specified format.
1077*
1078* Same as lyd_print(), but it allocates memory and store the data into it.
1079* It is up to caller to free the returned string by free().
1080*
1081* @param[out] strp Pointer to store the resulting dump.
1082* @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
1083* node of the data tree to print the specific subtree.
1084* @param[in] format Data output format.
1085* @param[in] options [printer flags](@ref printerflags).
1086* @return 0 on success, 1 on failure (#ly_errno is set).
1087*/
1088int lyd_print_mem(char **strp, const struct lyd_node *root, LYD_FORMAT format, int options);
Michal Vasko2d162e12015-09-24 14:33:29 +02001089
1090/**
Radek Krejcidef50022016-02-01 16:38:32 +01001091 * @brief Print data tree in the specified format.
Michal Vasko2d162e12015-09-24 14:33:29 +02001092 *
Radek Krejcidef50022016-02-01 16:38:32 +01001093 * Same as lyd_print(), but output is written into the specified file descriptor.
1094 *
1095 * @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
1096 * node of the data tree to print the specific subtree.
1097 * @param[in] fd File descriptor where to print the data.
1098 * @param[in] format Data output format.
1099 * @param[in] options [printer flags](@ref printerflags).
1100 * @return 0 on success, 1 on failure (#ly_errno is set).
Michal Vasko2d162e12015-09-24 14:33:29 +02001101 */
Radek Krejcidef50022016-02-01 16:38:32 +01001102int lyd_print_fd(int fd, const struct lyd_node *root, LYD_FORMAT format, int options);
1103
1104/**
1105 * @brief Print data tree in the specified format.
1106 *
1107 * To write data into a file descriptor, use lyd_print_fd().
1108 *
1109 * @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
1110 * node of the data tree to print the specific subtree.
1111 * @param[in] f File stream where to print the data.
1112 * @param[in] format Data output format.
1113 * @param[in] options [printer flags](@ref printerflags).
1114 * @return 0 on success, 1 on failure (#ly_errno is set).
1115 */
1116int lyd_print_file(FILE *f, const struct lyd_node *root, LYD_FORMAT format, int options);
1117
1118/**
1119 * @brief Print data tree in the specified format.
1120 *
1121 * Same as lyd_print(), but output is written via provided callback.
1122 *
1123 * @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
1124 * node of the data tree to print the specific subtree.
1125 * @param[in] writeclb Callback function to write the data (see write(1)).
1126 * @param[in] arg Optional caller-specific argument to be passed to the \p writeclb callback.
1127 * @param[in] format Data output format.
1128 * @param[in] options [printer flags](@ref printerflags).
1129 * @return 0 on success, 1 on failure (#ly_errno is set).
1130 */
1131int lyd_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg,
1132 const struct lyd_node *root, LYD_FORMAT format, int options);
Michal Vasko2d162e12015-09-24 14:33:29 +02001133
Michal Vasko4d1f0482016-09-19 14:35:06 +02001134/**
1135 * @brief Get the double value of a decimal64 leaf/leaf-list.
1136 *
1137 * YANG decimal64 type enables higher precision numbers than IEEE 754 double-precision
1138 * format, so this conversion does not have to be lossless.
1139 *
1140 * @param[in] node Leaf/leaf-list of type decimal64.
1141 * @return Closest double equivalent to the decimal64 value.
1142 */
1143double lyd_dec64_to_double(const struct lyd_node *node);
1144
Radek Krejci9b6aad22016-09-20 15:55:51 +02001145/**
Radek Krejci9ad23f42016-10-31 15:46:52 +01001146 * @brief Get the real data type definition of the leaf/leaf-list node.
Radek Krejci9b6aad22016-09-20 15:55:51 +02001147 *
1148 * Usually the data type can be obtained directly from the value_type member of the leaf/leaf-list.
Radek Krejci9ad23f42016-10-31 15:46:52 +01001149 * However, in case the node is unresolved leafref or the complete definition of the type is needed, it can be quite
1150 * complicated to get the correct data type, so this function can be used. The real type describes the value
1151 * representation so it is not #LY_TYPE_UNION neither #LY_TYPE_LEAFREF.
Radek Krejci9b6aad22016-09-20 15:55:51 +02001152 *
1153 * @param[in] leaf The leaf/leaf-list node to be examined.
Radek Krejci1899d6a2016-11-03 13:48:07 +01001154 * @param[in] resolve Flag to store the leaf's value as its value data, in this case the value is resolved in the data
1155 * tree. Otherwise the data tree is not checked, so e.g. leafref type can be returned even if the value
1156 * does not match any target value in the data tree.
Radek Krejci9ad23f42016-10-31 15:46:52 +01001157 * @return Pointer to the type definition of the \p leaf.
Radek Krejci9b6aad22016-09-20 15:55:51 +02001158 */
Radek Krejci1899d6a2016-11-03 13:48:07 +01001159const struct lys_type *lyd_leaf_type(struct lyd_node_leaf_list *leaf, int resolve);
Radek Krejci9b6aad22016-09-20 15:55:51 +02001160
Michal Vasko2d162e12015-09-24 14:33:29 +02001161/**@} */
1162
1163#ifdef __cplusplus
1164}
1165#endif
1166
1167#endif /* LY_TREE_DATA_H_ */