blob: 95191083b8f5724cfb13f341f89dfd0ee82dee9b [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 \>)
Radek Krejcie534c132016-11-23 13:32:31 +010051 are automatically escaped when the anydata is printed in XML format. */
Radek Krejci83bf1402016-09-27 15:05:20 +020052 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 node's value representation
84 */
85typedef union lyd_value_u {
86 const char *binary; /**< base64 encoded, NULL terminated string */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +020087 struct lys_type_bit **bit; /**< bitmap of pointers to the schema definition of the bit value that are set,
88 its size is always the number of defined bits in the schema */
Radek Krejci489773c2015-12-17 13:20:03 +010089 int8_t bln; /**< 0 as false, 1 as true */
Michal Vasko2d162e12015-09-24 14:33:29 +020090 int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */
91 struct lys_type_enum *enm; /**< pointer to the schema definition of the enumeration value */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +020092 struct lys_ident *ident; /**< pointer to the schema definition of the identityref value */
Radek Krejci40f17b92016-02-03 14:30:43 +010093 struct lyd_node *instance; /**< pointer to the instance-identifier target, note that if the tree was modified,
94 the target (address) can be invalid - the pointer is correctly checked and updated
95 by lyd_validate() */
Michal Vasko2d162e12015-09-24 14:33:29 +020096 int8_t int8; /**< 8-bit signed integer */
97 int16_t int16; /**< 16-bit signed integer */
98 int32_t int32; /**< 32-bit signed integer */
99 int64_t int64; /**< 64-bit signed integer */
100 struct lyd_node *leafref; /**< pointer to the referenced leaf/leaflist instance in data tree */
101 const char *string; /**< string */
102 uint8_t uint8; /**< 8-bit unsigned integer */
103 uint16_t uint16; /**< 16-bit signed integer */
104 uint32_t uint32; /**< 32-bit signed integer */
105 uint64_t uint64; /**< 64-bit signed integer */
106} lyd_val;
107
108/**
Radek Krejcia571d942017-02-24 09:26:49 +0100109 * @brief Attribute structure.
110 *
111 * The structure provides information about attributes of a data element. Such attributes must map to
112 * annotations as specified in RFC 7952. The only exception is the filter type (in NETCONF get operations)
113 * and edit-config's operation attributes. In XML, they are represented as standard XML attrbutes. In JSON,
114 * they are represented as JSON elements starting with the '@' character (for more information, see the
115 * YANG metadata RFC.
116 *
117 */
118struct lyd_attr {
119 struct lyd_node *parent; /**< data node where the attribute is placed */
120 struct lyd_attr *next; /**< pointer to the next attribute of the same element */
121 struct lys_ext_instance_complex *annotation; /**< pointer to the attribute/annotation's definition */
122 const char *name; /**< attribute name */
123 const char *value_str; /**< string representation of value (for comparison, printing,...), always corresponds to value_type */
124 lyd_val value; /**< node's value representation, always corresponds to schema->type.base */
125 uint16_t value_type; /**< type of the value in the node, mainly for union to avoid repeating of type detection,
126 if (schema->type.base == LY_TYPE_LEAFREF), then value_type may be
127 (LY_TYPE_LEAFREF_UNRES | leafref target value_type) and (value.leafref == NULL) */
128};
129
130/**
Radek Krejcica7efb72016-01-18 13:06:01 +0100131 * @defgroup validityflags Validity flags
132 * @ingroup datatree
133 *
134 * Validity flags for data nodes.
135 *
136 * @{
137 */
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100138#define LYD_VAL_OK 0x00 /**< Node is successfully validated including whole subtree */
Radek Krejcica7efb72016-01-18 13:06:01 +0100139#define LYD_VAL_UNIQUE 0x01 /**< Unique value(s) changed, applicable only to ::lys_node_list data nodes */
Radek Krejcid788a522016-07-25 14:57:38 +0200140#define LYD_VAL_MAND 0x02 /**< Some child added/removed and it is needed to perform check for mandatory
141 node or min/max constraints of direct list/leaflist children, applicable only
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100142 to ::lys_node_list and ::lys_node_container data nodes, but if on any other node
143 except ::lys_node_leaflist, it means checking that data node for duplicities.
144 Additionally, it can be set on truly any node type and then status references
145 are checked for this node if flag #LYD_OPT_OBSOLETE is used. */
146#define LYD_VAL_LEAFREF 0x04 /**< Node is a leafref, which needs to be resolved (it is invalid, new possible
147 resolvent, or something similar) */
148#define LYD_VAL_INUSE 0x80 /**< Internal flag for note about various processing on data, should be used only
149 internally and removed before libyang returns the node to the caller */
Radek Krejcica7efb72016-01-18 13:06:01 +0100150/**
151 * @}
152 */
153
154/**
Michal Vasko2d162e12015-09-24 14:33:29 +0200155 * @brief Generic structure for a data node, directly applicable to the data nodes defined as #LYS_CONTAINER, #LYS_LIST
156 * and #LYS_CHOICE.
157 *
158 * Completely fits to containers and choices and is compatible (can be used interchangeably except the #child member)
159 * with all other lyd_node_* structures. All data nodes are provides as ::lyd_node structure by default.
160 * 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 +0200161 * ::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 +0100162 * #LYS_CHOICE values.
Michal Vasko2d162e12015-09-24 14:33:29 +0200163 *
164 * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro.
165 */
166struct lyd_node {
167 struct lys_node *schema; /**< pointer to the schema definition of this node */
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100168 uint8_t validity; /**< [validity flags](@ref validityflags) */
Michal Vaskoe77dc992017-01-18 12:09:42 +0100169 uint8_t dflt:1; /**< flag for implicit default node */
Radek Krejci0b7704f2016-03-18 12:16:14 +0100170 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 +0100171 do not use this value! */
Michal Vasko2d162e12015-09-24 14:33:29 +0200172
173 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
174 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
175 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
176 never NULL. If there is no sibling node, pointer points to the node
177 itself. In case of the first node, this pointer points to the last
178 node in the list. */
179 struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
180 struct lyd_node *child; /**< pointer to the first child node \note Since other lyd_node_*
Radek Krejciee360892015-10-06 11:23:14 +0200181 structures represent end nodes, this member
Michal Vasko2d162e12015-09-24 14:33:29 +0200182 is replaced in those structures. Therefore, be careful with accessing
183 this member without having information about the node type from the schema's
184 ::lys_node#nodetype member. */
185};
186
187/**
Michal Vasko4c183312015-09-25 10:41:47 +0200188 * @brief Structure for data nodes defined as #LYS_LEAF or #LYS_LEAFLIST.
Michal Vasko2d162e12015-09-24 14:33:29 +0200189 *
Michal Vasko4c183312015-09-25 10:41:47 +0200190 * Extension for ::lyd_node structure. It replaces the ::lyd_node#child member by
191 * three new members (#value, #value_str and #value_type) to provide
192 * information about the value. The first five members (#schema, #attr, #next,
Michal Vasko2d162e12015-09-24 14:33:29 +0200193 * #prev and #parent) are compatible with the ::lyd_node's members.
194 *
195 * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro.
196 */
Michal Vasko4c183312015-09-25 10:41:47 +0200197struct lyd_node_leaf_list {
Michal Vasko2d162e12015-09-24 14:33:29 +0200198 struct lys_node *schema; /**< pointer to the schema definition of this node which is ::lys_node_leaflist
199 structure */
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100200 uint8_t validity; /**< [validity flags](@ref validityflags) */
Michal Vaskoe77dc992017-01-18 12:09:42 +0100201 uint8_t dflt:1; /**< flag for implicit default node */
Radek Krejci0b7704f2016-03-18 12:16:14 +0100202 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 +0100203 do not use this value! */
Michal Vasko2d162e12015-09-24 14:33:29 +0200204
205 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
206 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
207 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
208 never NULL. If there is no sibling node, pointer points to the node
209 itself. In case of the first node, this pointer points to the last
210 node in the list. */
211 struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
212
213 /* struct lyd_node *child; should be here, but is not */
214
215 /* leaflist's specific members */
Michal Vasko6a027702016-06-30 10:32:14 +0200216 const char *value_str; /**< string representation of value (for comparison, printing,...), always corresponds to value_type */
217 lyd_val value; /**< node's value representation, always corresponds to schema->type.base */
Michal Vasko35e08a02017-01-05 13:03:13 +0100218 uint16_t value_type; /**< type of the value in the node, mainly for union to avoid repeating of type detection,
Michal Vasko6a027702016-06-30 10:32:14 +0200219 if (schema->type.base == LY_TYPE_LEAFREF), then value_type may be
220 (LY_TYPE_LEAFREF_UNRES | leafref target value_type) and (value.leafref == NULL) */
Michal Vasko2d162e12015-09-24 14:33:29 +0200221};
222
223/**
Radek Krejcibf2abff2016-08-23 15:51:52 +0200224 * @brief Structure for data nodes defined as #LYS_ANYDATA or #LYS_ANYXML.
Michal Vasko2d162e12015-09-24 14:33:29 +0200225 *
226 * Extension for ::lyd_node structure - replaces the ::lyd_node#child member by new #value member. The first five
227 * members (#schema, #attr, #next, #prev and #parent) are compatible with the ::lyd_node's members.
228 *
229 * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro.
230 */
Radek Krejcibf2abff2016-08-23 15:51:52 +0200231struct lyd_node_anydata {
232 struct lys_node *schema; /**< pointer to the schema definition of this node which is ::lys_node_anydata
Michal Vasko2d162e12015-09-24 14:33:29 +0200233 structure */
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100234 uint8_t validity; /**< [validity flags](@ref validityflags) */
Michal Vaskoe77dc992017-01-18 12:09:42 +0100235 uint8_t dflt:1; /**< flag for implicit default node */
Radek Krejci0b7704f2016-03-18 12:16:14 +0100236 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 +0100237 do not use this value! */
Michal Vasko2d162e12015-09-24 14:33:29 +0200238
239 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
240 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
241 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
242 never NULL. If there is no sibling node, pointer points to the node
243 itself. In case of the first node, this pointer points to the last
244 node in the list. */
245 struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
246
247 /* struct lyd_node *child; should be here, but is not */
248
249 /* anyxml's specific members */
Radek Krejci45826012016-08-24 15:07:57 +0200250 LYD_ANYDATA_VALUETYPE value_type;/**< type of the stored anydata value */
251 union {
252 const char *str; /**< string value, in case of printing as XML, characters like '<' or '&' are escaped */
253 struct lyxml_elem *xml; /**< xml tree */
254 struct lyd_node *tree; /**< libyang data tree, does not change the root's parent, so it is not possible
255 to get from the data tree into the anydata/anyxml */
256 } value;
Michal Vasko2d162e12015-09-24 14:33:29 +0200257};
258
259/**
Radek Krejci991a3962016-05-05 15:00:14 +0200260 * @brief list of possible types of differencies in #lyd_difflist
261 */
262typedef enum {
Radek Krejci9e6f0b82016-05-13 17:33:16 +0200263 LYD_DIFF_END = 0, /**< end of the differences list */
Radek Krejci9e6f0b82016-05-13 17:33:16 +0200264 LYD_DIFF_DELETED, /**< deleted node
265 - Node is present in the first tree, but not in the second tree.
266 - To make both trees the same the node in lyd_difflist::first can be deleted from the
267 first tree. The pointer at the same index in the lyd_difflist::second array is
268 NULL */
269 LYD_DIFF_CHANGED, /**< value of a leaf or anyxml is changed, the lyd_difflist::first and lyd_difflist::second
270 points to the leaf/anyxml instances in the first and the second tree respectively. */
Radek Krejci22d2ca92016-05-17 16:23:51 +0200271 LYD_DIFF_MOVEDAFTER1, /**< user-ordered (leaf-)list item was moved.
272 - To make both trees the same, all #LYD_DIFF_MOVEDAFTER1 transactions must be applied
Radek Krejci9e6f0b82016-05-13 17:33:16 +0200273 to the first tree in the strict order they appear in the difflist. The
274 lyd_difflist::first points to the first tree node being moved and the
275 lyd_difflist::second points to the first tree node after which the first node is
276 supposed to be moved. If the second pointer is NULL, the node is being moved into
277 the beginning as the first node of the (leaf-)list instances. */
Radek Krejci22d2ca92016-05-17 16:23:51 +0200278 LYD_DIFF_CREATED, /**< newly created node
279 - Node is present in the second tree, but not in the first tree.
280 - To make both trees the same the node in lyd_difflist::second is supposed to be
281 inserted (copied via lyd_dup()) into the node (as a child) at the same index in the
282 lyd_difflist::first array (where is its parent). If the lyd_difflist::first at the
283 index is NULL, the missing node is top-level. */
284 LYD_DIFF_MOVEDAFTER2 /**< similar to LYD_DIFF_MOVEDAFTER1, but this time the moved item is in the second tree.
285 This type is always used in combination with (as a successor of) #LYD_DIFF_CREATED
286 as an instruction to move the newly created node to a specific position. Note, that
287 due to applicability to the second tree, the meaning of lyd_difflist:first and
288 lyd_difflist:second is inverse in comparison to #LYD_DIFF_MOVEDAFTER1. The
289 lyd_difflist::second points to the (previously) created node in the second tree and
290 the lyd_difflist::first points to the predecessor node in the second tree. If the
291 predecessor is NULL, the node is supposed to bes the first sibling. */
Radek Krejci991a3962016-05-05 15:00:14 +0200292} LYD_DIFFTYPE;
293
294/**
295 * @brief Structure for the result of lyd_diff(), describing differences between two data trees.
296 */
297struct lyd_difflist {
298 LYD_DIFFTYPE *type; /**< array of the differences types, terminated by #LYD_DIFF_END value. */
299 struct lyd_node **first; /**< array of nodes in the first tree for the specific type of difference, see the
300 description of #LYD_DIFFTYPE values for more information. */
301 struct lyd_node **second;/**< array of nodes in the second tree for the specific type of difference, see the
302 description of #LYD_DIFFTYPE values for more information. */
303};
304
305/**
306 * @brief Free the result of lyd_diff(). It frees the structure of the lyd_diff() result, not the referenced nodes.
307 *
308 * @param[in] diff The lyd_diff() result to free.
309 */
310void lyd_free_diff(struct lyd_difflist *diff);
311
312/**
313 * @brief Compare two data trees and provide list of differences.
314 *
315 * Note, that the \p first and the \p second must have the same schema parent (or they must be top-level elements).
316 * In case of using #LYD_OPT_NOSIBLINGS, they both must be instances of the same schema node.
317 *
Radek Krejci913100d2016-05-09 17:23:51 +0200318 * Order of the resulting set follows these rules:
Radek Krejci22d2ca92016-05-17 16:23:51 +0200319 * - To change the first tree into the second tree, the resulting transactions are supposed to be applied in the order
320 * they appear in the result. First, the changed (#LYD_DIFF_CHANGED) nodes are described followed by the deleted
321 * (#LYD_DIFF_DELETED) nodes. Then, the moving of the user-ordered nodes present in both trees (#LYD_DIFF_MOVEDAFTER1)
322 * follows and the last transactions in the results are the newly created (#LYD_DIFF_CREATED) nodes. These nodes are
323 * supposed to be added as the last siblings, but in some case they can need additional move. In such a case, the
324 * #LYD_DIFF_MOVEDAFTER2 transactions can appear.
325 * - The order of the changed (#LYD_DIFF_CHANGED) and created (#LYD_DIFF_CREATED) follows the nodes order in the
326 * second tree - the current siblings are processed first and then the children are processed. Note, that this is
327 * actually not the BFS:
Radek Krejci9e47ddf2016-05-18 15:01:09 +0200328 *
Radek Krejci913100d2016-05-09 17:23:51 +0200329 * 1 2
330 * / \ / \
331 * 3 4 7 8
332 * / \
333 * 5 6
Radek Krejci9e47ddf2016-05-18 15:01:09 +0200334 *
Radek Krejci22d2ca92016-05-17 16:23:51 +0200335 * - The order of the deleted (#LYD_DIFF_DELETED) nodes is the DFS:
Radek Krejci9e47ddf2016-05-18 15:01:09 +0200336 *
337 * 1 6
338 * / \ / \
339 * 2 5 7 8
340 * / \
341 * 3 4
Radek Krejci913100d2016-05-09 17:23:51 +0200342 *
343 * To change the first tree into the second one, it is necessary to follow the order of transactions described in
344 * the result. Note, that it is not possible just to use the transactions in the reverse order to transform the
345 * second tree into the first one. The transactions can be generalized (to be used on a different instance of the
346 * first tree) using lyd_path() to get identifiers for the nodes used in the transactions.
347 *
Radek Krejci9a6a5dd2016-05-05 15:56:24 +0200348 * @param[in] first The first (sub)tree to compare. Without #LYD_OPT_NOSIBLINGS option, all siblings are
Radek Krejci4c3bc112016-05-19 15:09:03 +0200349 * taken into comparison. If NULL, all the \p second nodes are supposed to be top level and they will
350 * be marked as #LYD_DIFF_CREATED.
Radek Krejci9a6a5dd2016-05-05 15:56:24 +0200351 * @param[in] second The second (sub)tree to compare. Without #LYD_OPT_NOSIBLINGS option, all siblings are
Radek Krejci4c3bc112016-05-19 15:09:03 +0200352 * taken into comparison. If NULL, all the \p first nodes will be marked as #LYD_DIFF_DELETED.
Radek Krejci99d737f2016-09-06 11:19:52 +0200353 * @param[in] options The @ref diffoptions are accepted.
Radek Krejci991a3962016-05-05 15:00:14 +0200354 * @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 +0200355 * 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 +0200356 */
357struct lyd_difflist *lyd_diff(struct lyd_node *first, struct lyd_node *second, int options);
358
359/**
Radek Krejci99d737f2016-09-06 11:19:52 +0200360 * @defgroup diffoptions Diff options
361 * @ingroup datatree
362 *
363 * @{
364 */
365/* LYD_DIFFOPT_NOSIBLINGS value is the same as LYD_OPT_NOSIBLINGS due to backward compatibility. The LYD_OPT_NOSIBLINGS
366 * was used previously as an option for lyd_diff(). */
367#define LYD_DIFFOPT_NOSIBLINGS 0x0800 /**< The both trees to diff have to instantiate the same schema node so only the
368 single subtree is compared. */
369#define LYD_DIFFOPT_WITHDEFAULTS 0x0001 /**< Take default nodes with their values into account and handle them as part
Michal Vaskoe6ff4282017-02-07 15:13:36 +0100370 of both trees. Summary of the modified behavior:
371 - deleted node is replaced with implicit default node - #LYD_DIFF_CHANGED instead delete
372 - created node replaces an implicit default node - #LYD_DIFF_CHANGED instead create
373 - in both cases even if the values match - #LYD_DIFF_CHANGED is still returned, because dlft flag was changed
374 Note that in this case, applying the resulting
Radek Krejci99d737f2016-09-06 11:19:52 +0200375 transactions on the first tree does not result to the exact second tree,
376 because instead of having implicit default nodes you are going to have
377 explicit default nodes. */
378/**@} diffoptions */
379
380/**
Radek Krejci6d538282016-05-05 14:24:12 +0200381 * @brief Build path (usable as XPath) of the data node.
382 * @param[in] node Data node to be processed. Note that the node should be from a complete data tree, having a subtree
383 * (after using lyd_unlink()) can cause generating invalid paths.
384 * @return NULL on error, on success the buffer for the resulting path is allocated and caller is supposed to free it
385 * with free().
386 */
Michal Vasko5efa25c2017-01-10 11:34:30 +0100387char *lyd_path(const struct lyd_node *node);
388
389/**
390 * @brief Build path (usable as instance-identified) of the data node with all the nodes fully qualified (having their
391 * model as prefix).
392 * @param[in] node Data node to be processed. Note that the node should be from a complete data tree, having a subtree
393 * (after using lyd_unlink()) can cause generating invalid paths.
394 * @return NULL on error, on success the buffer for the resulting path is allocated and caller is supposed to free it
395 * with free().
396 */
397char *lyd_qualified_path(const struct lyd_node *node);
Radek Krejci6d538282016-05-05 14:24:12 +0200398
399/**
Radek Krejcidef50022016-02-01 16:38:32 +0100400 * @defgroup parseroptions Data parser options
401 * @ingroup datatree
402 *
403 * Various options to change the data tree parsers behavior.
404 *
405 * Default behavior:
406 * - in case of XML, parser reads all data from its input (file, memory, XML tree) including the case of not well-formed
407 * XML document (multiple top-level elements) and if there is an unknown element, it is skipped including its subtree
408 * (see the next point). This can be changed by the #LYD_OPT_NOSIBLINGS option which make parser to read only a single
409 * tree (with a single root element) from its input.
410 * - parser silently ignores the data without a matching node in schema trees. If the caller want to stop
411 * parsing in case of presence of unknown data, the #LYD_OPT_STRICT can be used. The strict mode is useful for
412 * NETCONF servers, since NETCONF clients should always send data according to the capabilities announced by the server.
413 * On the other hand, the default non-strict mode is useful for clients receiving data from NETCONF server since
414 * clients are not required to understand everything the server does. Of course, the optimal strategy for clients is
415 * to use filtering to get only the required data. Having an unknown element of the known namespace is always an error.
416 * The behavior can be changed by #LYD_OPT_STRICT option.
417 * - using obsolete statements (status set to obsolete) just generates a warning, but the processing continues. The
418 * behavior can be changed by #LYD_OPT_OBSOLETE option.
419 * - parser expects that the provided data provides complete datastore content (both the configuration and state data)
420 * and performs data validation according to all YANG rules. This can be a problem in case of representing NETCONF's
421 * subtree filter data, edit-config's data or other type of data set - such data do not represent a complete data set
422 * and some of the validation rules can fail. Therefore there are other options (within lower 8 bits) to make parser
423 * to accept such a data.
Radek Krejcif3c218d2016-03-24 12:40:08 +0100424 * - when parser evaluates when-stmt condition to false, the constrained subtree is automatically removed. If the
425 * #LYD_OPT_NOAUTODEL is used, error is raised instead of silent auto delete. The option (and also this default
426 * behavior) takes effect only in case of #LYD_OPT_DATA or #LYD_OPT_CONFIG type of data.
Radek Krejcidef50022016-02-01 16:38:32 +0100427 * @{
428 */
429
430#define LYD_OPT_DATA 0x00 /**< Default type of data - complete datastore content with configuration as well as
431 state data. */
432#define LYD_OPT_CONFIG 0x01 /**< A configuration datastore - complete datastore without state data.
433 Validation modifications:
434 - status data are not allowed */
435#define LYD_OPT_GET 0x02 /**< Data content from a NETCONF reply message to the NETCONF \<get\> operation.
436 Validation modifications:
437 - mandatory nodes can be omitted
Michal Vasko62671b92017-01-02 13:08:57 +0100438 - leafrefs and instance-identifier resolution is allowed to fail
Michal Vaskoebf7df22017-03-28 16:08:07 +0200439 - list's keys/unique nodes are not required (so duplication is not checked)
440 - must and when evaluation skipped */
Radek Krejcidef50022016-02-01 16:38:32 +0100441#define LYD_OPT_GETCONFIG 0x04 /**< Data content from a NETCONF reply message to the NETCONF \<get-config\> operation
442 Validation modifications:
443 - mandatory nodes can be omitted
Michal Vasko62671b92017-01-02 13:08:57 +0100444 - leafrefs and instance-identifier resolution is allowed to fail
Radek Krejcidef50022016-02-01 16:38:32 +0100445 - list's keys/unique nodes are not required (so duplication is not checked)
Michal Vaskoebf7df22017-03-28 16:08:07 +0200446 - must and when evaluation skipped
Radek Krejcidef50022016-02-01 16:38:32 +0100447 - status data are not allowed */
448#define LYD_OPT_EDIT 0x08 /**< Content of the NETCONF \<edit-config\>'s config element.
449 Validation modifications:
450 - mandatory nodes can be omitted
Michal Vasko62671b92017-01-02 13:08:57 +0100451 - leafrefs and instance-identifier resolution is allowed to fail
Michal Vaskoebf7df22017-03-28 16:08:07 +0200452 - must and when evaluation skipped
Radek Krejcidef50022016-02-01 16:38:32 +0100453 - status data are not allowed */
Michal Vasko75250262017-02-09 15:36:13 +0100454#define LYD_OPT_RPC 0x10 /**< Data represents RPC or action input parameters. */
455#define LYD_OPT_RPCREPLY 0x20 /**< Data represents RPC or action output parameters (maps to NETCONF <rpc-reply> data). */
Radek Krejcidef50022016-02-01 16:38:32 +0100456#define LYD_OPT_NOTIF 0x40 /**< Data represents an event notification data. */
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100457#define LYD_OPT_NOTIF_FILTER 0x80 /**< Data represents a filtered event notification data.
458 Validation modification:
459 - the only requirement is that the data tree matches the schema tree */
Radek Krejcidef50022016-02-01 16:38:32 +0100460#define LYD_OPT_TYPEMASK 0xff /**< Mask to filter data type options. Always only a single data type option (only
461 single bit from the lower 8 bits) can be set. */
462
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100463/* 0x100 reserved, used internally */
464#define LYD_OPT_STRICT 0x0200 /**< Instead of silent ignoring data without schema definition, raise an error. */
465#define LYD_OPT_DESTRUCT 0x0400 /**< Free the provided XML tree during parsing the data. With this option, the
Radek Krejcidef50022016-02-01 16:38:32 +0100466 provided XML tree is affected and all succesfully parsed data are freed.
467 This option is applicable only to lyd_parse_xml() function. */
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100468#define LYD_OPT_OBSOLETE 0x0800 /**< Raise an error when an obsolete statement (status set to obsolete) is used. */
469#define LYD_OPT_NOSIBLINGS 0x1000 /**< Parse only a single XML tree from the input. This option applies only to
Radek Krejcidef50022016-02-01 16:38:32 +0100470 XML input data. */
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100471#define LYD_OPT_TRUSTED 0x2000 /**< Data comes from a trusted source and it is not needed to validate them. Data
Radek Krejci93fab982016-02-03 15:58:19 +0100472 are connected with the schema, but the most validation checks (mandatory nodes,
473 list instance uniqueness, etc.) are not performed. This option does not make
474 sense for lyd_validate() so it is ignored by this function. */
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100475#define LYD_OPT_NOAUTODEL 0x4000 /**< Avoid automatic delete of subtrees with false when-stmt condition. The flag is
Michal Vasko75250262017-02-09 15:36:13 +0100476 applicable only in combination with #LYD_OPT_DATA and #LYD_OPT_CONFIG flags.
Radek Krejci03b71f72016-03-16 11:10:09 +0100477 If used, libyang generates validation error instead of silently removing the
478 constrained subtree. */
Michal Vasko3cfa3182017-01-17 10:00:58 +0100479#define LYD_OPT_NOEXTDEPS 0x8000 /**< Allow external dependencies (external leafrefs, instance-identifiers, must,
Michal Vaskof6aa8612017-03-02 10:52:44 +0100480 and when) to not be resolved/satisfied during validation. */
Radek Krejcidef50022016-02-01 16:38:32 +0100481
482/**@} parseroptions */
483
484/**
Michal Vasko299f9832017-01-06 13:29:22 +0100485 * @brief Parse (and validate) data from memory.
Radek Krejcidef50022016-02-01 16:38:32 +0100486 *
487 * In case of LY_XML format, the data string is parsed completely. It means that when it contains
488 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
489 * returned data node is a root of the first tree with other trees connected via the next pointer.
490 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
491 *
492 * @param[in] ctx Context to connect with the data tree being built here.
493 * @param[in] data Serialized data in the specified format.
494 * @param[in] format Format of the input data to be parsed.
495 * @param[in] options Parser options, see @ref parseroptions.
Michal Vasko6b44d712016-09-12 16:25:46 +0200496 * @param[in] ... Variable arguments depend on \p options. If they include:
497 * - #LYD_OPT_DATA:
498 * - #LYD_OPT_CONFIG:
499 * - #LYD_OPT_GET:
500 * - #LYD_OPT_GETCONFIG:
501 * - #LYD_OPT_EDIT:
502 * - no variable arguments expected.
503 * - #LYD_OPT_RPC:
504 * - #LYD_OPT_NOTIF:
505 * - struct lyd_node *data_tree - additional data tree that will be used
506 * when checking any "when" or "must" conditions in the parsed tree that require
507 * some nodes outside their subtree. It must be a list of top-level elements!
508 * - #LYD_OPT_RPCREPLY:
Michal Vaskod55f1092016-10-24 11:21:08 +0200509 * - const struct ::lyd_node *rpc_act - pointer to the whole RPC or action operation data
510 * tree (the request) of the reply.
Michal Vasko945b96b2016-10-18 11:49:12 +0200511 * - const struct ::lyd_node *data_tree - additional data tree that will be used
Michal Vasko6b44d712016-09-12 16:25:46 +0200512 * when checking any "when" or "must" conditions in the parsed tree that require
513 * some nodes outside their subtree. It must be a list of top-level elements!
Radek Krejcidef50022016-02-01 16:38:32 +0100514 * @return Pointer to the built data tree or NULL in case of empty \p data. To free the returned structure,
515 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
516 * #ly_errno contains appropriate error code (see #LY_ERR).
517 */
Radek Krejci722b0072016-02-01 17:09:45 +0100518struct lyd_node *lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, ...);
Radek Krejcidef50022016-02-01 16:38:32 +0100519
520/**
Michal Vasko299f9832017-01-06 13:29:22 +0100521 * @brief Read (and validate) data from the given file descriptor.
Radek Krejcidef50022016-02-01 16:38:32 +0100522 *
523 * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc.
524 *
525 * In case of LY_XML format, the file content is parsed completely. It means that when it contains
526 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
527 * returned data node is a root of the first tree with other trees connected via the next pointer.
528 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
529 *
530 * @param[in] ctx Context to connect with the data tree being built here.
531 * @param[in] fd The standard file descriptor of the file containing the data tree in the specified format.
532 * @param[in] format Format of the input data to be parsed.
533 * @param[in] options Parser options, see @ref parseroptions.
Michal Vasko6b44d712016-09-12 16:25:46 +0200534 * @param[in] ... Variable arguments depend on \p options. If they include:
535 * - #LYD_OPT_DATA:
536 * - #LYD_OPT_CONFIG:
537 * - #LYD_OPT_GET:
538 * - #LYD_OPT_GETCONFIG:
539 * - #LYD_OPT_EDIT:
540 * - no variable arguments expected.
541 * - #LYD_OPT_RPC:
542 * - #LYD_OPT_NOTIF:
543 * - struct lyd_node *data_tree - additional data tree that will be used
544 * when checking any "when" or "must" conditions in the parsed tree that require
545 * some nodes outside their subtree. It must be a list of top-level elements!
546 * - #LYD_OPT_RPCREPLY:
Michal Vaskod55f1092016-10-24 11:21:08 +0200547 * - const struct ::lyd_node *rpc_act - pointer to the whole RPC or action operation data
548 * tree (the request) of the reply.
Michal Vasko945b96b2016-10-18 11:49:12 +0200549 * - const struct ::lyd_node *data_tree - additional data tree that will be used
Michal Vasko6b44d712016-09-12 16:25:46 +0200550 * when checking any "when" or "must" conditions in the parsed tree that require
551 * some nodes outside their subtree. It must be a list of top-level elements!
Radek Krejcidef50022016-02-01 16:38:32 +0100552 * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure,
553 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
554 * #ly_errno contains appropriate error code (see #LY_ERR).
555 */
556struct lyd_node *lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, ...);
557
558/**
Michal Vasko299f9832017-01-06 13:29:22 +0100559 * @brief Read (and validate) data from the given file path.
Radek Krejcidef50022016-02-01 16:38:32 +0100560 *
561 * In case of LY_XML format, the file content is parsed completely. It means that when it contains
562 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
563 * returned data node is a root of the first tree with other trees connected via the next pointer.
564 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
565 *
566 * @param[in] ctx Context to connect with the data tree being built here.
567 * @param[in] path Path to the file containing the data tree in the specified format.
568 * @param[in] format Format of the input data to be parsed.
569 * @param[in] options Parser options, see @ref parseroptions.
Michal Vasko6b44d712016-09-12 16:25:46 +0200570 * @param[in] ... Variable arguments depend on \p options. If they include:
571 * - #LYD_OPT_DATA:
572 * - #LYD_OPT_CONFIG:
573 * - #LYD_OPT_GET:
574 * - #LYD_OPT_GETCONFIG:
575 * - #LYD_OPT_EDIT:
576 * - no variable arguments expected.
577 * - #LYD_OPT_RPC:
578 * - #LYD_OPT_NOTIF:
579 * - struct lyd_node *data_tree - additional data tree that will be used
580 * when checking any "when" or "must" conditions in the parsed tree that require
581 * some nodes outside their subtree. It must be a list of top-level elements!
582 * - #LYD_OPT_RPCREPLY:
Michal Vaskod55f1092016-10-24 11:21:08 +0200583 * - const struct ::lyd_node *rpc_act - pointer to the whole RPC or action operation data
584 * tree (the request) of the reply.
Michal Vasko945b96b2016-10-18 11:49:12 +0200585 * - const struct ::lyd_node *data_tree - additional data tree that will be used
Michal Vasko6b44d712016-09-12 16:25:46 +0200586 * when checking any "when" or "must" conditions in the parsed tree that require
587 * some nodes outside their subtree. It must be a list of top-level elements!
Radek Krejcidef50022016-02-01 16:38:32 +0100588 * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure,
589 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
590 * #ly_errno contains appropriate error code (see #LY_ERR).
591 */
592struct lyd_node *lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options, ...);
593
594/**
Michal Vasko299f9832017-01-06 13:29:22 +0100595 * @brief Parse (and validate) XML tree.
Radek Krejcidef50022016-02-01 16:38:32 +0100596 *
597 * The output data tree is parsed from the given XML tree previously parsed by one of the
598 * lyxml_read* functions.
599 *
Radek Krejci722b0072016-02-01 17:09:45 +0100600 * 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 +0100601 * or the provided root is a root element of a subtree), all the sibling nodes (previous as well as
602 * following) are processed as well. The returned data node is a root of the first tree with other
603 * trees connected via the next pointer. This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
604 *
605 * When the function is used with #LYD_OPT_DESTRUCT, all the successfully parsed data including the
606 * XML \p root and all its siblings (if #LYD_OPT_NOSIBLINGS is not used) are freed. Only with
607 * #LYD_OPT_DESTRUCT option the \p root pointer is changed - if all the data are parsed, it is set
608 * to NULL, otherwise it will hold the XML tree without the successfully parsed elements.
609 *
610 * The context must be the same as the context used to parse XML tree by lyxml_read* function.
611 *
612 * @param[in] ctx Context to connect with the data tree being built here.
613 * @param[in,out] root XML tree to parse (convert) to data tree. By default, parser do not change the XML tree. However,
614 * when #LYD_OPT_DESTRUCT is specified in \p options, parser frees all successfully parsed data.
615 * @param[in] options Parser options, see @ref parseroptions.
Michal Vasko6b44d712016-09-12 16:25:46 +0200616 * @param[in] ... Variable arguments depend on \p options. If they include:
617 * - #LYD_OPT_DATA:
618 * - #LYD_OPT_CONFIG:
619 * - #LYD_OPT_GET:
620 * - #LYD_OPT_GETCONFIG:
621 * - #LYD_OPT_EDIT:
622 * - no variable arguments expected.
623 * - #LYD_OPT_RPC:
624 * - #LYD_OPT_NOTIF:
625 * - struct lyd_node *data_tree - additional data tree that will be used
626 * when checking any "when" or "must" conditions in the parsed tree that require
627 * some nodes outside their subtree. It must be a list of top-level elements!
628 * - #LYD_OPT_RPCREPLY:
Michal Vaskod55f1092016-10-24 11:21:08 +0200629 * - const struct ::lyd_node *rpc_act - pointer to the whole RPC or action operation data
630 * tree (the request) of the reply.
Michal Vasko945b96b2016-10-18 11:49:12 +0200631 * - const struct ::lyd_node *data_tree - additional data tree that will be used
Michal Vasko6b44d712016-09-12 16:25:46 +0200632 * when checking any "when" or "must" conditions in the parsed tree that require
633 * some nodes outside their subtree. It must be a list of top-level elements!
Radek Krejcidef50022016-02-01 16:38:32 +0100634 * @return Pointer to the built data tree or NULL in case of empty \p root. To free the returned structure,
635 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
636 * #ly_errno contains appropriate error code (see #LY_ERR).
637 */
638struct lyd_node *lyd_parse_xml(struct ly_ctx *ctx, struct lyxml_elem **root, int options,...);
639
640/**
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200641 * @brief Create a new container node in a data tree.
642 *
Michal Vasko299f9832017-01-06 13:29:22 +0100643 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
644 *
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200645 * @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 +0200646 * @param[in] module Module with the node being created.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200647 * @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 +0200648 * #LYS_NOTIF, #LYS_RPC, or #LYS_ACTION.
Michal Vasko1dca6882015-10-22 14:29:42 +0200649 * @return New node, NULL on error.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200650 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100651struct lyd_node *lyd_new(struct lyd_node *parent, const struct lys_module *module, const char *name);
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200652
653/**
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200654 * @brief Create a new leaf or leaflist node in a data tree with a string value that is converted to
655 * the actual value.
656 *
Michal Vasko299f9832017-01-06 13:29:22 +0100657 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
658 *
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200659 * @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 +0200660 * @param[in] module Module with the node being created.
661 * @param[in] name Schema node name of the new data node.
Michal Vasko3e671b52015-10-23 16:23:15 +0200662 * @param[in] val_str String form of the value of the node being created. In case the type is #LY_TYPE_INST
663 * 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 +0200664 * @return New node, NULL on error.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200665 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100666struct lyd_node *lyd_new_leaf(struct lyd_node *parent, const struct lys_module *module, const char *name,
Michal Vasko3e671b52015-10-23 16:23:15 +0200667 const char *val_str);
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200668
669/**
Radek Krejcib9b4d002016-01-18 13:08:51 +0100670 * @brief Change value of a leaf node.
671 *
Michal Vasko299f9832017-01-06 13:29:22 +0100672 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
673 *
Radek Krejcib9b4d002016-01-18 13:08:51 +0100674 * Despite the prototype allows to provide a leaflist node as \p leaf parameter, only leafs are accepted.
Michal Vasko3a55a8a2016-04-13 14:19:53 +0200675 * Also, changing the value of a list key is prohibited.
Radek Krejcib9b4d002016-01-18 13:08:51 +0100676 *
677 * @param[in] leaf A leaf node to change.
678 * @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
679 * or #LY_TYPE_IDENT, JSON node-id format is expected (nodes are prefixed with module names, not XML namespaces).
Michal Vaskobb513032017-05-23 14:38:57 +0200680 * @return 0 on success, <0 on error, 1 if the (canonical) value matched the original one and no change occured.
Radek Krejcib9b4d002016-01-18 13:08:51 +0100681 */
682int lyd_change_leaf(struct lyd_node_leaf_list *leaf, const char *val_str);
683
684/**
Radek Krejci45826012016-08-24 15:07:57 +0200685 * @brief Create a new anydata or anyxml node in a data tree.
686 *
Michal Vasko299f9832017-01-06 13:29:22 +0100687 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
688 *
Radek Krejci45826012016-08-24 15:07:57 +0200689 * 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 +0200690 *
Michal Vasko2d162e12015-09-24 14:33:29 +0200691 * @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 +0200692 * @param[in] module Module with the node being created.
Radek Krejci45826012016-08-24 15:07:57 +0200693 * @param[in] name Schema node name of the new data node. The schema node determines if the anydata or anyxml node
694 * is created.
695 * @param[in] value Pointer to the value data to be stored in the anydata/anyxml node. The type of the data is
696 * determined according to the \p value_type parameter.
697 * @param[in] value_type Type of the provided data \p value.
Michal Vasko1dca6882015-10-22 14:29:42 +0200698 * @return New node, NULL on error.
Michal Vasko2d162e12015-09-24 14:33:29 +0200699 */
Radek Krejci45826012016-08-24 15:07:57 +0200700struct lyd_node *lyd_new_anydata(struct lyd_node *parent, const struct lys_module *module, const char *name,
701 void *value, LYD_ANYDATA_VALUETYPE value_type);
Michal Vasko2d162e12015-09-24 14:33:29 +0200702
703/**
Michal Vasko945b96b2016-10-18 11:49:12 +0200704 * @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 +0100705 *
Michal Vasko299f9832017-01-06 13:29:22 +0100706 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
707 *
Michal Vasko98a5a742016-05-11 11:02:56 +0200708 * @param[in] parent Parent node for the node being created. NULL in case of creating top level element.
709 * @param[in] module Module with the node being created.
Michal Vasko945b96b2016-10-18 11:49:12 +0200710 * @param[in] name Schema node name of the new data node. The node should only be #LYS_CONTAINER or #LYS_LIST,
711 * but accepted are also #LYS_NOTIF, #LYS_RPC, or #LYS_ACTION.
Michal Vasko0df122f2015-12-14 13:38:21 +0100712 * @return New node, NULL on error.
713 */
Michal Vasko98a5a742016-05-11 11:02:56 +0200714struct lyd_node *lyd_new_output(struct lyd_node *parent, const struct lys_module *module, const char *name);
Michal Vasko50c0a872016-01-13 14:34:11 +0100715
716/**
Michal Vasko98a5a742016-05-11 11:02:56 +0200717 * @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 +0200718 * the actual value. Ignore RPC/action input nodes and instead use RPC/action output ones.
Michal Vasko50c0a872016-01-13 14:34:11 +0100719 *
Michal Vasko299f9832017-01-06 13:29:22 +0100720 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
721 *
Michal Vasko98a5a742016-05-11 11:02:56 +0200722 * @param[in] parent Parent node for the node being created. NULL in case of creating top level element.
723 * @param[in] module Module with the node being created.
724 * @param[in] name Schema node name of the new data node.
Michal Vasko50c0a872016-01-13 14:34:11 +0100725 * @param[in] val_str String form of the value of the node being created. In case the type is #LY_TYPE_INST
726 * or #LY_TYPE_IDENT, JSON node-id format is expected (nodes are prefixed with module names, not XML namespaces).
727 * @return New node, NULL on error.
728 */
Michal Vasko98a5a742016-05-11 11:02:56 +0200729struct lyd_node *lyd_new_output_leaf(struct lyd_node *parent, const struct lys_module *module, const char *name,
730 const char *val_str);
Michal Vasko50c0a872016-01-13 14:34:11 +0100731
732/**
Michal Vasko945b96b2016-10-18 11:49:12 +0200733 * @brief Create a new anydata or anyxml node in a data tree. Ignore RPC/action input nodes and instead use
734 * RPC/action output ones.
Michal Vasko50c0a872016-01-13 14:34:11 +0100735 *
Michal Vasko299f9832017-01-06 13:29:22 +0100736 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
737 *
Michal Vasko98a5a742016-05-11 11:02:56 +0200738 * @param[in] parent Parent node for the node being created. NULL in case of creating top level element.
739 * @param[in] module Module with the node being created.
Radek Krejci45826012016-08-24 15:07:57 +0200740 * @param[in] name Schema node name of the new data node. The schema node determines if the anydata or anyxml node
741 * is created.
742 * @param[in] value Pointer to the value data to be stored in the anydata/anyxml node. The type of the data is
743 * determined according to the \p value_type parameter. Data are supposed to be dynamically allocated.
744 * Since it is directly attached into the created data node, caller is supposed to not manipulate with
745 * the data after a successful call (including calling free() on the provided data).
746 * @param[in] value_type Type of the provided data \p value.
Michal Vasko50c0a872016-01-13 14:34:11 +0100747 * @return New node, NULL on error.
748 */
Radek Krejci45826012016-08-24 15:07:57 +0200749struct lyd_node *lyd_new_output_anydata(struct lyd_node *parent, const struct lys_module *module, const char *name,
750 void *value, LYD_ANYDATA_VALUETYPE value_type);
Michal Vasko0df122f2015-12-14 13:38:21 +0100751
752/**
Michal Vaskof5299282016-03-16 13:32:02 +0100753 * @defgroup pathoptions Data path creation options
754 * @ingroup datatree
755 *
756 * Various options to change lyd_new_path() behavior.
757 *
758 * Default behavior:
Michal Vaskof5299282016-03-16 13:32:02 +0100759 * - if the target node already exists, an error is returned.
Michal Vasko9db078d2016-03-23 11:08:51 +0100760 * - the whole path to the target node is created (with any missing parents) if necessary.
Michal Vasko2411b942016-03-23 13:50:03 +0100761 * - RPC output schema children are completely ignored in all modules. Input is searched and nodes created normally.
Michal Vaskof5299282016-03-16 13:32:02 +0100762 * @{
763 */
764
Michal Vasko72d35102016-03-31 10:03:38 +0200765#define LYD_PATH_OPT_UPDATE 0x01 /**< If the target node exists and is a leaf, it is updated with the new value and returned.
766 If the target node exists and is not a leaf, NULL is returned and no error set. */
Michal Vasko9db078d2016-03-23 11:08:51 +0100767#define LYD_PATH_OPT_NOPARENT 0x02 /**< If any parents of the target node exist, return an error. */
Michal Vasko945b96b2016-10-18 11:49:12 +0200768#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 +0100769
770/** @} pathoptions */
771
772/**
773 * @brief Create a new data node based on a simple XPath.
774 *
Michal Vasko299f9832017-01-06 13:29:22 +0100775 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
776 *
Michal Vasko8d18ef52016-04-06 12:21:46 +0200777 * The new node is normally inserted at the end, either as the last child of a parent or as the last sibling
778 * if working with top-level elements. However, when manipulating RPC input or output, schema ordering is
Michal Vasko98a5a742016-05-11 11:02:56 +0200779 * required and always guaranteed.
Michal Vasko58f74f12016-03-24 13:26:06 +0100780 *
Michal Vasko8c419642016-04-13 14:22:01 +0200781 * If \p path points to a list key and the list does not exist, the key value from the predicate is used
782 * and \p value is ignored.
783 *
Michal Vasko945b96b2016-10-18 11:49:12 +0200784 * @param[in] data_tree Existing data tree to add to/modify. If creating RPCs/actions, there should only be one
785 * RPC/action and either input or output, not both. Can be NULL.
Michal Vaskof5299282016-03-16 13:32:02 +0100786 * @param[in] ctx Context to use. Mandatory if \p data_tree is NULL.
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100787 * @param[in] path Simple absolute data XPath of the new node. It can contain only simple node addressing with optional
Michal Vasko58c2aab2017-01-05 10:02:05 +0100788 * module names as prefixes. List nodes can have predicates, one for each list key in the correct order and
789 * with its value as well or using specific instance position, leaves and leaf-lists can have predicates too that
790 * have preference over \p value, see @ref howtoxpath.
Radek Krejci45826012016-08-24 15:07:57 +0200791 * @param[in] value Value of the new leaf/lealf-list (const char*). If creating anydata or anyxml, the following
792 * \p value_type parameter is required to be specified correctly. If creating nodes of other types, the
793 * parameter is ignored.
794 * @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 +0100795 * @param[in] options Bitmask of options flags, see @ref pathoptions.
Michal Vasko8c419642016-04-13 14:22:01 +0200796 * @return First created (or updated with #LYD_PATH_OPT_UPDATE) node,
Michal Vasko17bb4902016-04-05 15:20:51 +0200797 * 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 +0200798 * NULL and ly_errno is set on error.
Michal Vaskof5299282016-03-16 13:32:02 +0100799 */
Radek Krejci45826012016-08-24 15:07:57 +0200800struct lyd_node *lyd_new_path(struct lyd_node *data_tree, struct ly_ctx *ctx, const char *path, void *value,
801 LYD_ANYDATA_VALUETYPE value_type, int options);
Michal Vaskof5299282016-03-16 13:32:02 +0100802
803/**
Michal Vaskoae5a53e2017-01-05 10:33:41 +0100804 * @brief Learn the relative instance position of a list or leaf-list within other instances of the
805 * same schema node.
806 *
807 * @param[in] node List or leaf-list to get the position of.
808 * @return 0 on error or positive integer of the instance position.
809 */
810unsigned int lyd_list_pos(const struct lyd_node *node);
811
812/**
Radek Krejcia17c85c2017-01-06 12:22:34 +0100813 * @brief Create a copy of the specified data tree \p node. Schema references are kept the same.
Michal Vasko2d162e12015-09-24 14:33:29 +0200814 *
Michal Vasko299f9832017-01-06 13:29:22 +0100815 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
Michal Vasko2f95fe62016-12-01 09:36:08 +0100816 *
Michal Vasko2d162e12015-09-24 14:33:29 +0200817 * @param[in] node Data tree node to be duplicated.
818 * @param[in] recursive 1 if all children are supposed to be also duplicated.
819 * @return Created copy of the provided data \p node.
820 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100821struct lyd_node *lyd_dup(const struct lyd_node *node, int recursive);
Michal Vasko2d162e12015-09-24 14:33:29 +0200822
823/**
Radek Krejcia17c85c2017-01-06 12:22:34 +0100824 * @brief Create a copy of the specified data tree \p node in the different context. All the
825 * schema references and strings are re-mapped into the specified context.
826 *
827 * If the target context does not contain the schemas used in the source data tree, error
828 * is raised and the new data tree is not created.
829 *
830 * @param[in] node Data tree node to be duplicated.
831 * @param[in] recursive 1 if all children are supposed to be also duplicated.
832 * @param[in] ctx Target context for the duplicated data.
833 * @return Created copy of the provided data \p node.
834 */
835struct lyd_node *lyd_dup_to_ctx(const struct lyd_node *node, int recursive, struct ly_ctx *ctx);
836
837/**
Michal Vasko299f9832017-01-06 13:29:22 +0100838 * @brief Merge a (sub)tree into a data tree.
839 *
840 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
841 *
842 * Missing nodes are merged, leaf values updated.
Radek Krejci8f4eba52017-01-06 15:32:41 +0100843 *
Michal Vasko45fb2822016-04-18 13:32:17 +0200844 * If \p target and \p source do not share the top-level schema node, even if they
845 * are from different modules, \p source parents up to top-level node will be created and
846 * linked to the \p target (but only containers can be created this way, lists need keys,
847 * so if lists are missing, an error will be returned).
848 *
Radek Krejci2ffe9932017-01-06 16:29:47 +0100849 * If the source data tree is in a different context, the resulting data are placed into the context
850 * of the target tree.
Michal Vasko45fb2822016-04-18 13:32:17 +0200851 *
Michal Vaskocf6dc7e2016-04-18 16:00:37 +0200852 * @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 +0200853 * @param[in] source Data tree to merge \p target with. Must be valid (at least as a subtree).
Radek Krejci2ffe9932017-01-06 16:29:47 +0100854 * @param[in] options Bitmask of the following option flags:
Michal Vasko0300b532016-09-14 12:16:02 +0200855 * - #LYD_OPT_DESTRUCT - spend \p source in the function, otherwise \p source is left untouched,
856 * - #LYD_OPT_NOSIBLINGS - merge only the \p source subtree (ignore siblings), otherwise merge
857 * \p source and all its succeeding siblings (preceeding ones are still ignored!),
858 * - #LYD_OPT_EXPLICIT - when merging an explicitly set node and a default node, always put
859 * the explicit node into \p target, otherwise the node which is in \p source is used.
Michal Vasko45fb2822016-04-18 13:32:17 +0200860 * @return 0 on success, nonzero in case of an error.
861 */
862int lyd_merge(struct lyd_node *target, const struct lyd_node *source, int options);
863
Radek Krejci2ffe9932017-01-06 16:29:47 +0100864/**
865 * @brief Same as lyd_merge(), but moves the resulting data into the specified context.
866 *
867 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
868 *
869 * @param[in] trg Top-level (or an RPC output child) data tree to merge to. Must be valid. If its context
870 * differs from the specified \p ctx of the result, the provided data tree is freed and the new
Radek Krejciab80e3a2017-01-09 13:07:31 +0100871 * tree in the required context is returned on success. To keep the \p trg tree, convert it to the
872 * target context using lyd_dup_to_ctx() and then call lyd_merge() instead of lyd_merge_to_ctx().
Radek Krejci2ffe9932017-01-06 16:29:47 +0100873 * @param[in] src Data tree to merge \p target with. Must be valid (at least as a subtree).
874 * @param[in] options Bitmask of the following option flags:
875 * - #LYD_OPT_DESTRUCT - spend \p source in the function, otherwise \p source is left untouched,
876 * - #LYD_OPT_NOSIBLINGS - merge only the \p source subtree (ignore siblings), otherwise merge
877 * \p source and all its succeeding siblings (preceeding ones are still ignored!),
878 * - #LYD_OPT_EXPLICIT - when merging an explicitly set node and a default node, always put
879 * the explicit node into \p target, otherwise the node which is in \p source is used.
880 * @param[in] ctx Target context in which the result will be created. Note that the successful merge requires to have
881 * all the used modules in the source and target data trees loaded in the target context.
882 * @return 0 on success, nonzero in case of an error.
883 */
884int lyd_merge_to_ctx(struct lyd_node **trg, const struct lyd_node *src, int options, struct ly_ctx *ctx);
885
Michal Vasko0300b532016-09-14 12:16:02 +0200886#define LYD_OPT_EXPLICIT 0x0100
887
Michal Vasko45fb2822016-04-18 13:32:17 +0200888/**
Michal Vasko2d162e12015-09-24 14:33:29 +0200889 * @brief Insert the \p node element as child to the \p parent element. The \p node is inserted as a last child of the
890 * \p parent.
891 *
Michal Vasko299f9832017-01-06 13:29:22 +0100892 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
893 *
Michal Vaskob6c51f12016-09-14 12:15:11 +0200894 * - if the node is part of some other tree, it is automatically unlinked.
895 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
896 * - 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 +0200897 * element.
Michal Vaskob6c51f12016-09-14 12:15:11 +0200898 * - 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 +0200899 * by the new node.
900 * - if a default node is being inserted and the target tree already contains non-default instance, the existing
901 * instance is silently replaced. If it contains the exact same default node, it is replaced as well.
Michal Vaskob6c51f12016-09-14 12:15:11 +0200902 * - 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 +0200903 * node is inserted and it is up to the caller to solve the presence of multiple instances afterwards.
904 *
905 * Note that this function differs from lyd_insert_before() and lyd_insert_after() because the position of the
906 * node being inserted is determined automatically according to the rules described above. In contrast to
907 * 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 +0200908 * NULL. If inserting something larger and not fitting the mentioned use-cases (or simply if unsure), you can always
909 * use lyd_merge(), it should be able to handle any situation.
Michal Vasko2d162e12015-09-24 14:33:29 +0200910 *
911 * @param[in] parent Parent node for the \p node being inserted.
912 * @param[in] node The node being inserted.
Michal Vasko24337392015-10-16 09:58:16 +0200913 * @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 +0200914 * in the data tree.
915 */
Michal Vasko24337392015-10-16 09:58:16 +0200916int lyd_insert(struct lyd_node *parent, struct lyd_node *node);
Michal Vasko2d162e12015-09-24 14:33:29 +0200917
918/**
Radek Krejcifd0bcf02016-09-09 13:28:34 +0200919 * @brief Insert the \p node element as a last sibling of the specified \p sibling element.
920 *
Michal Vasko299f9832017-01-06 13:29:22 +0100921 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
922 *
Michal Vaskob6c51f12016-09-14 12:15:11 +0200923 * - if the node is part of some other tree, it is automatically unlinked.
924 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
925 * - 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 +0200926 * element.
Michal Vaskob6c51f12016-09-14 12:15:11 +0200927 * - 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 +0200928 * by the new node.
929 * - if a default node is being inserted and the target tree already contains non-default instance, the existing
930 * instance is silently replaced. If it contains the exact same default node, it is replaced as well.
Michal Vaskob6c51f12016-09-14 12:15:11 +0200931 * - if a non-default node is being inserted and there is already its non-default instance in the target tree, the new
932 * 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 +0200933 *
934 * Note that this function differs from lyd_insert_before() and lyd_insert_after() because the position of the
935 * 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 +0200936 * lyd_insert_sibling() can be used to insert top-level elements. If inserting something larger and not fitting
937 * the mentioned use-cases (or simply if unsure), you can always use lyd_merge(), it should be able to handle
938 * any situation.
Radek Krejcifd0bcf02016-09-09 13:28:34 +0200939 *
940 * @param[in,out] sibling Sibling node as a reference where to insert the \p node. When function succeeds, the sibling
941 * is always set to point to the first sibling node. Note that in some cases described above, the provided sibling
942 * node could be removed from the tree.
943 * @param[in] node The node being inserted.
944 * @return 0 on success, nonzero in case of error, e.g. when the node is being inserted to an inappropriate place
945 * in the data tree.
946 */
947int lyd_insert_sibling(struct lyd_node **sibling, struct lyd_node *node);
948
949/**
Michal Vasko3f7dba12015-10-15 13:09:27 +0200950 * @brief Insert the \p node element after the \p sibling element. If \p node and \p siblings are already
Michal Vasko299f9832017-01-06 13:29:22 +0100951 * siblings (just moving \p node position).
952 *
953 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
Michal Vasko2d162e12015-09-24 14:33:29 +0200954 *
Michal Vaskob6c51f12016-09-14 12:15:11 +0200955 * - 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 +0200956 * - if a default node is being inserted and the target tree already contains non-default instance, the existing
957 * instance is removed. If it contains the exact same default node, it is removed as well.
Michal Vaskob6c51f12016-09-14 12:15:11 +0200958 * - if a non-default node is being inserted and there is already its non-default instance in the target tree, the new
959 * node is inserted and it is up to the caller to solve the presence of multiple instances afterwards.
960 *
Michal Vasko2d162e12015-09-24 14:33:29 +0200961 * @param[in] sibling The data tree node before which the \p node will be inserted.
Radek Krejci20a5f292016-02-09 15:04:49 +0100962 * @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 +0200963 * @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 +0200964 * in the data tree.
965 */
Michal Vasko24337392015-10-16 09:58:16 +0200966int lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node);
Michal Vasko2d162e12015-09-24 14:33:29 +0200967
968/**
Radek Krejci20a5f292016-02-09 15:04:49 +0100969 * @brief Insert the \p node element after the \p sibling element. If \p node and \p siblings are already
Michal Vasko299f9832017-01-06 13:29:22 +0100970 * siblings (just moving \p node position).
971 *
972 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
Michal Vasko2d162e12015-09-24 14:33:29 +0200973 *
Michal Vaskob6c51f12016-09-14 12:15:11 +0200974 * - 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 +0200975 * - if a default node is being inserted and the target tree already contains non-default instance, the existing
976 * instance is removed. If it contains the exact same default node, it is removed as well.
Michal Vaskob6c51f12016-09-14 12:15:11 +0200977 * - if a non-default node is being inserted and there is already its non-default instance in the target tree, the new
978 * node is inserted and it is up to the caller to solve the presence of multiple instances afterwards.
979 *
Michal Vasko3f7dba12015-10-15 13:09:27 +0200980 * @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 +0100981 * are already siblings (just moving \p node position), skip validation.
Radek Krejci20a5f292016-02-09 15:04:49 +0100982 * @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 +0200983 * @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 +0200984 * in the data tree.
985 */
Michal Vasko24337392015-10-16 09:58:16 +0200986int lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node);
987
988/**
Michal Vasko2411b942016-03-23 13:50:03 +0100989 * @brief Order siblings according to the schema node ordering.
990 *
Michal Vasko299f9832017-01-06 13:29:22 +0100991 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
992 *
Michal Vasko58f74f12016-03-24 13:26:06 +0100993 * If the siblings include data nodes from other modules, they are
994 * sorted based on the module order in the context.
995 *
996 * @param[in] sibling Node, whose siblings will be sorted.
997 * @param[in] recursive Whether sort all siblings of siblings, recursively.
998 * @return 0 on success, nonzero in case of an error.
Michal Vasko2411b942016-03-23 13:50:03 +0100999 */
Michal Vasko58f74f12016-03-24 13:26:06 +01001000int lyd_schema_sort(struct lyd_node *sibling, int recursive);
Michal Vasko2411b942016-03-23 13:50:03 +01001001
1002/**
Michal Vasko105cef12016-02-04 12:06:26 +01001003 * @brief Search in the given data for instances of nodes matching the provided XPath expression.
1004 *
Michal Vasko7fdf9b32016-03-01 15:59:48 +01001005 * The XPath expression is evaluated on data -> skip all non-data nodes (input, output, choice, case).
Michal Vasko105cef12016-02-04 12:06:26 +01001006 *
Michal Vasko7fdf9b32016-03-01 15:59:48 +01001007 * Expr examples:
Michal Vaskof3557312017-07-03 14:02:48 +02001008 * "/modules-state/module[name = 'ietf-yang-library']/namespace" with context node "ietf-yang-library:modules-state"
1009 * "/ietf-netconf:get-config/ietf-netconf:source" with an arbitrary context node (all node names are prefixed)
Michal Vasko7fdf9b32016-03-01 15:59:48 +01001010 *
Michal Vaskof3557312017-07-03 14:02:48 +02001011 * @param[in] ctx_node Context node.
Michal Vasko105cef12016-02-04 12:06:26 +01001012 * @param[in] expr XPath expression filtering the matching nodes.
Michal Vasko46a4bf92016-09-08 08:23:49 +02001013 * @return Set of found data nodes. If no nodes are matching \p expr or the result
Michal Vasko105cef12016-02-04 12:06:26 +01001014 * would be a number, a string, or a boolean, the returned set is empty. In case of an error, NULL is returned.
1015 */
Michal Vaskof3557312017-07-03 14:02:48 +02001016struct ly_set *lyd_find_xpath(const struct lyd_node *ctx_node, const char *expr);
Michal Vasko105cef12016-02-04 12:06:26 +01001017
1018/**
Radek Krejcic5b6b912016-01-18 16:35:35 +01001019 * @brief Search in the given data for instances of the provided schema node.
1020 *
1021 * The \p data is used to find the data root and function then searches in the whole tree and all sibling trees.
1022 *
1023 * @param[in] data A node in the data tree to search.
1024 * @param[in] schema Schema node of the data nodes caller want to find.
Michal Vasko46a4bf92016-09-08 08:23:49 +02001025 * @return Set of found data nodes. If no data node is found, the returned set is empty.
Radek Krejcic5b6b912016-01-18 16:35:35 +01001026 * In case of error, NULL is returned.
1027 */
Michal Vaskof06fb5b2016-09-08 10:05:56 +02001028struct ly_set *lyd_find_instance(const struct lyd_node *data, const struct lys_node *schema);
Radek Krejcic5b6b912016-01-18 16:35:35 +01001029
1030/**
Radek Krejcid788a522016-07-25 14:57:38 +02001031 * @brief Get the first sibling of the given node.
1032 *
1033 * @param[in] node Node which first sibling is going to be the result.
1034 * @return The first sibling of the given node or the node itself if it is the first child of the parent.
1035 */
1036struct lyd_node *lyd_first_sibling(struct lyd_node *node);
1037
1038/**
Michal Vasko24337392015-10-16 09:58:16 +02001039 * @brief Validate \p node data subtree.
1040 *
Michal Vaskodedea832016-04-19 11:24:45 +02001041 * @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 +02001042 * can modify the provided tree including the root \p node.
Michal Vasko24337392015-10-16 09:58:16 +02001043 * @param[in] options Options for the inserting data to the target data tree options, see @ref parseroptions.
Michal Vaskocdb90172016-09-13 09:34:36 +02001044 * @param[in] var_arg Variable argument depends on \p options. If they include:
Michal Vasko6b44d712016-09-12 16:25:46 +02001045 * - #LYD_OPT_DATA:
1046 * - #LYD_OPT_CONFIG:
1047 * - #LYD_OPT_GET:
1048 * - #LYD_OPT_GETCONFIG:
1049 * - #LYD_OPT_EDIT:
Michal Vaskocdb90172016-09-13 09:34:36 +02001050 * - struct ly_ctx *ctx - context to use when \p node is NULL (for checking an empty tree),
1051 * otherwise can be NULL.
Michal Vasko6b44d712016-09-12 16:25:46 +02001052 * - #LYD_OPT_RPC:
1053 * - #LYD_OPT_RPCREPLY:
1054 * - #LYD_OPT_NOTIF:
1055 * - struct ::lyd_node *data_tree - additional data tree that will be used when checking
Michal Vaskocdb90172016-09-13 09:34:36 +02001056 * any "when" or "must" conditions in the \p node tree
1057 * that require some nodes outside their subtree. If set,
1058 * it must be a list of top-level elements!
Radek Krejci92ece002016-04-04 15:45:05 +02001059 * @return 0 on success, nonzero in case of an error.
Michal Vasko24337392015-10-16 09:58:16 +02001060 */
Michal Vaskocdb90172016-09-13 09:34:36 +02001061int lyd_validate(struct lyd_node **node, int options, void *var_arg);
Michal Vasko2d162e12015-09-24 14:33:29 +02001062
1063/**
Radek Krejcif6fac5e2017-05-18 15:14:18 +02001064 * @brief Check restrictions applicable to the particular leaf/leaf-list on the given string value.
1065 *
1066 * Validates the value only using the types' restrictions. Do not check the rest of restrictions dependent on the
1067 * data tree (must, when statements or uniqueness of the leaf-list item).
1068 *
1069 * @param[in] node Schema node of the leaf or leaf-list eventually holding the \p value.
1070 * @param[in] value Value to be checked (NULL is checked as empty string).
1071 * @return EXIT_SUCCESS if the \p value conforms to the restrictions, EXIT_FAILURE otherwise.
1072 */
1073int lyd_validate_value(struct lys_node *node, const char *value);
1074
1075/**
Radek Krejci46180b52016-08-31 16:01:32 +02001076 * @brief Get know if the node contain (despite implicit or explicit) default value.
Radek Krejci7b4309c2016-03-23 10:30:29 +01001077 *
Radek Krejci46180b52016-08-31 16:01:32 +02001078 * @param[in] node The leaf or leaf-list to check. Note, that leaf-list is marked as default only when the complete
1079 * and only the default set is present (node's siblings are also checked).
1080 * @return 1 if the node contains the default value, 0 otherwise.
Radek Krejci7b4309c2016-03-23 10:30:29 +01001081 */
Radek Krejci46180b52016-08-31 16:01:32 +02001082int lyd_wd_default(struct lyd_node_leaf_list *node);
Radek Krejci6b8f6ac2016-03-23 12:33:04 +01001083
1084/**
Michal Vasko55f60be2015-10-14 13:12:58 +02001085 * @brief Unlink the specified data subtree. All referenced namespaces are copied.
Michal Vasko2d162e12015-09-24 14:33:29 +02001086 *
Michal Vasko299f9832017-01-06 13:29:22 +01001087 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
1088 *
Michal Vasko2d162e12015-09-24 14:33:29 +02001089 * Note, that the node's connection with the schema tree is kept. Therefore, in case of
1090 * reconnecting the node to a data tree using lyd_paste() it is necessary to paste it
1091 * to the appropriate place in the data tree following the schema.
1092 *
1093 * @param[in] node Data tree node to be unlinked (together with all children).
1094 * @return 0 for success, nonzero for error
1095 */
1096int lyd_unlink(struct lyd_node *node);
1097
1098/**
Radek Krejcifd0bcf02016-09-09 13:28:34 +02001099 * @brief Free (and unlink) the specified data subtree. Use carefully, since libyang silently creates default nodes,
1100 * it is always better to use lyd_free_withsiblings() to free the complete data tree.
Michal Vasko2d162e12015-09-24 14:33:29 +02001101 *
Michal Vasko299f9832017-01-06 13:29:22 +01001102 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
1103 *
Michal Vasko2d162e12015-09-24 14:33:29 +02001104 * @param[in] node Root of the (sub)tree to be freed.
1105 */
1106void lyd_free(struct lyd_node *node);
1107
1108/**
Radek Krejcifd0bcf02016-09-09 13:28:34 +02001109 * @brief Free (and unlink) the specified data tree and all its siblings (preceding as well as following).
Radek Krejci81468402016-01-07 13:52:40 +01001110 *
Michal Vasko299f9832017-01-06 13:29:22 +01001111 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
1112 *
Radek Krejci81468402016-01-07 13:52:40 +01001113 * @param[in] node One of the siblings root element of the (sub)trees to be freed.
1114 */
1115void lyd_free_withsiblings(struct lyd_node *node);
1116
1117/**
Radek Krejci134610e2015-10-20 17:15:34 +02001118 * @brief Insert attribute into the data node.
1119 *
1120 * @param[in] parent Data node where to place the attribute
Radek Krejci70ecd722016-03-21 09:04:00 +01001121 * @param[in] mod An alternative way to specify attribute's module (namespace) used in case the \p name does
1122 * not include prefix. If neither prefix in the \p name nor mod is specified, the attribute's
1123 * module is inherited from the \p parent node. It is not allowed to have attributes with no
1124 * module (namespace).
1125 * @param[in] name Attribute name. The string can include the attribute's module (namespace) as the name's
1126 * prefix (prefix:name). Prefix must be the name of one of the schema in the \p parent's context.
1127 * If the prefix is not specified, the \p mod parameter is used. If neither of these parameters is
1128 * usable, attribute inherits module (namespace) from the \p parent node. It is not allowed to
1129 * have attributes with no module (namespace).
Radek Krejci134610e2015-10-20 17:15:34 +02001130 * @param[in] value Attribute value
1131 * @return pointer to the created attribute (which is already connected in \p parent) or NULL on error.
1132 */
Radek Krejci70ecd722016-03-21 09:04:00 +01001133struct lyd_attr *lyd_insert_attr(struct lyd_node *parent, const struct lys_module *mod, const char *name,
1134 const char *value);
Radek Krejci134610e2015-10-20 17:15:34 +02001135
1136/**
Radek Krejci88f29302015-10-30 15:42:33 +01001137 * @brief Destroy data attribute
1138 *
1139 * If the attribute to destroy is a member of a node attribute list, it is necessary to
1140 * provide the node itself as \p parent to keep the list consistent.
1141 *
1142 * @param[in] ctx Context where the attribute was created (usually it is the context of the \p parent)
1143 * @param[in] parent Parent node where the attribute is placed
1144 * @param[in] attr Attribute to destroy
1145 * @param[in] recursive Zero to destroy only the attribute, non-zero to destroy also all the subsequent attributes
1146 * in the list.
1147 */
1148void lyd_free_attr(struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_attr *attr, int recursive);
1149
1150/**
Radek Krejci6910a032016-04-13 10:06:21 +02001151 * @brief Return main module of the data tree node.
1152 *
1153 * In case of regular YANG module, it returns ::lys_node#module pointer,
1154 * but in case of submodule, it returns pointer to the main module.
1155 *
1156 * @param[in] node Data tree node to be examined
1157 * @return pointer to the main module (schema structure), NULL in case of error.
1158 */
1159struct lys_module *lyd_node_module(const struct lyd_node *node);
1160
1161/**
Michal Vasko0a8ab412017-01-09 11:10:08 +01001162 * @brief Get the type structure of a leaf.
1163 *
1164 * In case of a union, the correct specific type is found.
1165 * In case of a leafref, the final (if there is a chain of leafrefs) target's type is found.
Michal Vaskoe3886bb2017-01-02 11:33:28 +01001166 *
1167 * @param[in] leaf Leaf to examine.
Michal Vasko0a8ab412017-01-09 11:10:08 +01001168 * @return Found type, NULL on error.
Michal Vaskoe3886bb2017-01-02 11:33:28 +01001169 */
1170const struct lys_type *lyd_leaf_type(const struct lyd_node_leaf_list *leaf);
1171
1172/**
Radek Krejcidef50022016-02-01 16:38:32 +01001173* @brief Print data tree in the specified format.
1174*
1175* Same as lyd_print(), but it allocates memory and store the data into it.
1176* It is up to caller to free the returned string by free().
1177*
1178* @param[out] strp Pointer to store the resulting dump.
1179* @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
1180* node of the data tree to print the specific subtree.
1181* @param[in] format Data output format.
1182* @param[in] options [printer flags](@ref printerflags).
1183* @return 0 on success, 1 on failure (#ly_errno is set).
1184*/
1185int lyd_print_mem(char **strp, const struct lyd_node *root, LYD_FORMAT format, int options);
Michal Vasko2d162e12015-09-24 14:33:29 +02001186
1187/**
Radek Krejcidef50022016-02-01 16:38:32 +01001188 * @brief Print data tree in the specified format.
Michal Vasko2d162e12015-09-24 14:33:29 +02001189 *
Radek Krejcidef50022016-02-01 16:38:32 +01001190 * Same as lyd_print(), but output is written into the specified file descriptor.
1191 *
1192 * @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
1193 * node of the data tree to print the specific subtree.
1194 * @param[in] fd File descriptor where to print the data.
1195 * @param[in] format Data output format.
1196 * @param[in] options [printer flags](@ref printerflags).
1197 * @return 0 on success, 1 on failure (#ly_errno is set).
Michal Vasko2d162e12015-09-24 14:33:29 +02001198 */
Radek Krejcidef50022016-02-01 16:38:32 +01001199int lyd_print_fd(int fd, const struct lyd_node *root, LYD_FORMAT format, int options);
1200
1201/**
1202 * @brief Print data tree in the specified format.
1203 *
1204 * To write data into a file descriptor, use lyd_print_fd().
1205 *
1206 * @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
1207 * node of the data tree to print the specific subtree.
1208 * @param[in] f File stream where to print the data.
1209 * @param[in] format Data output format.
1210 * @param[in] options [printer flags](@ref printerflags).
1211 * @return 0 on success, 1 on failure (#ly_errno is set).
1212 */
1213int lyd_print_file(FILE *f, const struct lyd_node *root, LYD_FORMAT format, int options);
1214
1215/**
1216 * @brief Print data tree in the specified format.
1217 *
1218 * Same as lyd_print(), but output is written via provided callback.
1219 *
1220 * @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
1221 * node of the data tree to print the specific subtree.
1222 * @param[in] writeclb Callback function to write the data (see write(1)).
1223 * @param[in] arg Optional caller-specific argument to be passed to the \p writeclb callback.
1224 * @param[in] format Data output format.
1225 * @param[in] options [printer flags](@ref printerflags).
1226 * @return 0 on success, 1 on failure (#ly_errno is set).
1227 */
1228int lyd_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg,
1229 const struct lyd_node *root, LYD_FORMAT format, int options);
Michal Vasko2d162e12015-09-24 14:33:29 +02001230
Michal Vasko4d1f0482016-09-19 14:35:06 +02001231/**
1232 * @brief Get the double value of a decimal64 leaf/leaf-list.
1233 *
1234 * YANG decimal64 type enables higher precision numbers than IEEE 754 double-precision
1235 * format, so this conversion does not have to be lossless.
1236 *
1237 * @param[in] node Leaf/leaf-list of type decimal64.
1238 * @return Closest double equivalent to the decimal64 value.
1239 */
1240double lyd_dec64_to_double(const struct lyd_node *node);
1241
Michal Vasko2d162e12015-09-24 14:33:29 +02001242/**@} */
1243
1244#ifdef __cplusplus
1245}
1246#endif
1247
1248#endif /* LY_TREE_DATA_H_ */