blob: 09af0ddfef135f47eb88ebbc2762c35d0ce4a90c [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
Michal Vaskofcd974b2017-08-22 10:17:49 +020021#include "libyang.h"
Mislav Novakovice251a652015-09-29 08:40:12 +020022#include "tree_schema.h"
Radek Krejcidef50022016-02-01 16:38:32 +010023#include "xml.h"
Mislav Novakovice251a652015-09-29 08:40:12 +020024
Michal Vasko2d162e12015-09-24 14:33:29 +020025#ifdef __cplusplus
26extern "C" {
27#endif
28
29/**
Radek Krejcidef50022016-02-01 16:38:32 +010030 * @defgroup datatree Data Tree
Michal Vasko2d162e12015-09-24 14:33:29 +020031 * @{
Radek Krejcidef50022016-02-01 16:38:32 +010032 *
33 * Data structures and functions to manipulate and access instance data tree.
Michal Vasko2d162e12015-09-24 14:33:29 +020034 */
35
36/**
Radek Krejcidef50022016-02-01 16:38:32 +010037 * @brief Data input/output formats supported by libyang [parser](@ref howtodataparsers) and
38 * [printer](@ref howtodataprinters) functions.
Michal Vasko2d162e12015-09-24 14:33:29 +020039 */
40typedef enum {
41 LYD_UNKNOWN, /**< unknown format, used as return value in case of error */
42 LYD_XML, /**< XML format of the instance data */
43 LYD_JSON, /**< JSON format of the instance data */
Michal Vasko1e82a3b2018-07-03 12:16:58 +020044 LYD_LYB, /**< LYB format of the instance data */
Michal Vasko2d162e12015-09-24 14:33:29 +020045} LYD_FORMAT;
46
47/**
Radek Krejci45826012016-08-24 15:07:57 +020048 * @brief List of possible value types stored in ::lyd_node_anydata.
49 */
50typedef enum {
Radek Krejci83bf1402016-09-27 15:05:20 +020051 LYD_ANYDATA_CONSTSTRING = 0x00, /**< value is constant string (const char *) which is internally duplicated for
52 storing in the anydata structure; XML sensitive characters (such as & or \>)
Radek Krejcie534c132016-11-23 13:32:31 +010053 are automatically escaped when the anydata is printed in XML format. */
Radek Krejci83bf1402016-09-27 15:05:20 +020054 LYD_ANYDATA_STRING = 0x01, /**< value is dynamically allocated string (char*), so the data are used directly
55 without duplication and caller is supposed to not manipulate with the data
56 after a successful call (including calling free() on the provided data); XML
57 sensitive characters (such as & or \>) are automatically escaped when the
58 anydata is printed in XML format */
59 LYD_ANYDATA_JSON = 0x02, /**< value is string containing the data modeled by YANG and encoded as I-JSON. The
60 string is handled as constant string. In case of using the value as input
61 parameter, the #LYD_ANYDATA_JSOND can be used for dynamically allocated
62 string. */
63 LYD_ANYDATA_JSOND = 0x03, /**< In case of using value as input parameter, this enumeration is supposed to be
64 used for dynamically allocated strings (it is actually combination of
65 #LYD_ANYDATA_JSON and #LYD_ANYDATA_STRING (and it can be also specified as
66 ORed value of the mentioned values. */
67 LYD_ANYDATA_SXML = 0x04, /**< value is string containing the serialized XML data. The string is handled as
68 constant string. In case of using the value as input parameter, the
69 #LYD_ANYDATA_SXMLD can be used for dynamically allocated string. */
70 LYD_ANYDATA_SXMLD = 0x05, /**< In case of using serialized XML value as input parameter, this enumeration is
71 supposed to be used for dynamically allocated strings (it is actually
72 combination of #LYD_ANYDATA_SXML and #LYD_ANYDATA_STRING (and it can be also
73 specified as ORed value of the mentioned values). */
74 LYD_ANYDATA_XML = 0x08, /**< value is struct lyxml_elem*, the structure is directly connected into the
75 anydata node without duplication, caller is supposed to not manipulate with the
76 data after a successful call (including calling lyxml_free() on the provided
77 data) */
78 LYD_ANYDATA_DATATREE = 0x10, /**< value is struct lyd_node* (first sibling), the structure is directly connected
79 into the anydata node without duplication, caller is supposed to not manipulate
80 with the data after a successful call (including calling lyd_free() on the
81 provided data) */
Michal Vaskodcaf7222018-08-08 16:27:00 +020082 LYD_ANYDATA_LYB = 0x20, /**< value is a memory with serialized data tree in LYB format. The data are handled
83 as a constant string. In case of using the value as input parameter,
84 the #LYD_ANYDATA_LYBD can be used for dynamically allocated string. */
85 LYD_ANYDATA_LYBD = 0x21, /**< In case of using LYB value as input parameter, this enumeration is
86 supposed to be used for dynamically allocated strings (it is actually
87 combination of #LYD_ANYDATA_LYB and #LYD_ANYDATA_STRING (and it can be also
88 specified as ORed value of the mentioned values). */
Radek Krejci45826012016-08-24 15:07:57 +020089} LYD_ANYDATA_VALUETYPE;
90
91/**
Michal Vasko2d162e12015-09-24 14:33:29 +020092 * @brief node's value representation
93 */
94typedef union lyd_value_u {
95 const char *binary; /**< base64 encoded, NULL terminated string */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +020096 struct lys_type_bit **bit; /**< bitmap of pointers to the schema definition of the bit value that are set,
97 its size is always the number of defined bits in the schema */
Radek Krejci489773c2015-12-17 13:20:03 +010098 int8_t bln; /**< 0 as false, 1 as true */
Michal Vasko2d162e12015-09-24 14:33:29 +020099 int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */
100 struct lys_type_enum *enm; /**< pointer to the schema definition of the enumeration value */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200101 struct lys_ident *ident; /**< pointer to the schema definition of the identityref value */
Radek Krejci40f17b92016-02-03 14:30:43 +0100102 struct lyd_node *instance; /**< pointer to the instance-identifier target, note that if the tree was modified,
103 the target (address) can be invalid - the pointer is correctly checked and updated
104 by lyd_validate() */
Michal Vasko2d162e12015-09-24 14:33:29 +0200105 int8_t int8; /**< 8-bit signed integer */
106 int16_t int16; /**< 16-bit signed integer */
107 int32_t int32; /**< 32-bit signed integer */
108 int64_t int64; /**< 64-bit signed integer */
109 struct lyd_node *leafref; /**< pointer to the referenced leaf/leaflist instance in data tree */
110 const char *string; /**< string */
111 uint8_t uint8; /**< 8-bit unsigned integer */
112 uint16_t uint16; /**< 16-bit signed integer */
113 uint32_t uint32; /**< 32-bit signed integer */
114 uint64_t uint64; /**< 64-bit signed integer */
Michal Vaskoc6cd3f02018-03-02 14:07:42 +0100115 void *ptr; /**< arbitrary data stored using a type plugin */
Michal Vasko2d162e12015-09-24 14:33:29 +0200116} lyd_val;
117
118/**
Radek Krejcia571d942017-02-24 09:26:49 +0100119 * @brief Attribute structure.
120 *
121 * The structure provides information about attributes of a data element. Such attributes must map to
122 * annotations as specified in RFC 7952. The only exception is the filter type (in NETCONF get operations)
123 * and edit-config's operation attributes. In XML, they are represented as standard XML attrbutes. In JSON,
124 * they are represented as JSON elements starting with the '@' character (for more information, see the
125 * YANG metadata RFC.
126 *
127 */
128struct lyd_attr {
129 struct lyd_node *parent; /**< data node where the attribute is placed */
130 struct lyd_attr *next; /**< pointer to the next attribute of the same element */
131 struct lys_ext_instance_complex *annotation; /**< pointer to the attribute/annotation's definition */
132 const char *name; /**< attribute name */
133 const char *value_str; /**< string representation of value (for comparison, printing,...), always corresponds to value_type */
134 lyd_val value; /**< node's value representation, always corresponds to schema->type.base */
Michal Vasko70bf8e52018-03-26 11:32:33 +0200135 LY_DATA_TYPE _PACKED value_type; /**< type of the value in the node, mainly for union to avoid repeating of type detection */
Michal Vasko101658e2018-06-05 15:05:54 +0200136 uint8_t value_flags; /**< value type flags */
Radek Krejcia571d942017-02-24 09:26:49 +0100137};
138
139/**
Radek Krejcica7efb72016-01-18 13:06:01 +0100140 * @defgroup validityflags Validity flags
141 * @ingroup datatree
142 *
143 * Validity flags for data nodes.
144 *
145 * @{
146 */
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100147#define LYD_VAL_OK 0x00 /**< Node is successfully validated including whole subtree */
Michal Vasko185b5272018-09-13 14:26:12 +0200148#define LYD_VAL_DUP 0x01 /**< Instance duplication must be checked again, applicable only to ::lys_node_list and
149 ::lys_node_leaf_list data nodes */
150#define LYD_VAL_UNIQUE 0x02 /**< Unique value(s) changed, applicable only to ::lys_node_list data nodes */
151#define LYD_VAL_MAND 0x04 /**< Some child added/removed and it is needed to perform check for mandatory
Radek Krejcid788a522016-07-25 14:57:38 +0200152 node or min/max constraints of direct list/leaflist children, applicable only
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100153 to ::lys_node_list and ::lys_node_container data nodes, but if on any other node
154 except ::lys_node_leaflist, it means checking that data node for duplicities.
155 Additionally, it can be set on truly any node type and then status references
156 are checked for this node if flag #LYD_OPT_OBSOLETE is used. */
Michal Vasko185b5272018-09-13 14:26:12 +0200157#define LYD_VAL_LEAFREF 0x08 /**< Node is a leafref, which needs to be resolved (it is invalid, new possible
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100158 resolvent, or something similar) */
159#define LYD_VAL_INUSE 0x80 /**< Internal flag for note about various processing on data, should be used only
160 internally and removed before libyang returns the node to the caller */
Radek Krejcica7efb72016-01-18 13:06:01 +0100161/**
162 * @}
163 */
164
165/**
Michal Vasko2d162e12015-09-24 14:33:29 +0200166 * @brief Generic structure for a data node, directly applicable to the data nodes defined as #LYS_CONTAINER, #LYS_LIST
167 * and #LYS_CHOICE.
168 *
169 * Completely fits to containers and choices and is compatible (can be used interchangeably except the #child member)
170 * with all other lyd_node_* structures. All data nodes are provides as ::lyd_node structure by default.
171 * 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 +0200172 * ::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 +0100173 * #LYS_CHOICE values.
Michal Vasko2d162e12015-09-24 14:33:29 +0200174 *
Michal Vaskoc30c95a2018-08-01 14:35:54 +0200175 * To traverse all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro. To traverse
176 * the whole subtree, use #LY_TREE_DFS_BEGIN macro.
Michal Vasko2d162e12015-09-24 14:33:29 +0200177 */
178struct lyd_node {
179 struct lys_node *schema; /**< pointer to the schema definition of this node */
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100180 uint8_t validity; /**< [validity flags](@ref validityflags) */
Michal Vaskoe77dc992017-01-18 12:09:42 +0100181 uint8_t dflt:1; /**< flag for implicit default node */
Radek Krejci0b7704f2016-03-18 12:16:14 +0100182 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 +0100183 do not use this value! */
Michal Vasko2d162e12015-09-24 14:33:29 +0200184
185 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
186 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
187 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
188 never NULL. If there is no sibling node, pointer points to the node
189 itself. In case of the first node, this pointer points to the last
190 node in the list. */
191 struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
Michal Vasko24affa02018-04-03 09:06:06 +0200192
Michal Vaskod025ee32018-06-28 10:04:19 +0200193#ifdef LY_ENABLED_LYD_PRIV
194 void *priv; /**< private user data, not used by libyang */
195#endif
196
Michal Vasko24affa02018-04-03 09:06:06 +0200197#ifdef LY_ENABLED_CACHE
198 uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list) */
199 struct hash_table *ht; /**< hash table with all the direct children (except keys for a list, lists without keys) */
200#endif
201
Michal Vasko2d162e12015-09-24 14:33:29 +0200202 struct lyd_node *child; /**< pointer to the first child node \note Since other lyd_node_*
Radek Krejciee360892015-10-06 11:23:14 +0200203 structures represent end nodes, this member
Michal Vasko2d162e12015-09-24 14:33:29 +0200204 is replaced in those structures. Therefore, be careful with accessing
205 this member without having information about the node type from the schema's
206 ::lys_node#nodetype member. */
207};
208
209/**
Michal Vasko4c183312015-09-25 10:41:47 +0200210 * @brief Structure for data nodes defined as #LYS_LEAF or #LYS_LEAFLIST.
Michal Vasko2d162e12015-09-24 14:33:29 +0200211 *
Michal Vasko4c183312015-09-25 10:41:47 +0200212 * Extension for ::lyd_node structure. It replaces the ::lyd_node#child member by
213 * three new members (#value, #value_str and #value_type) to provide
214 * information about the value. The first five members (#schema, #attr, #next,
Michal Vasko2d162e12015-09-24 14:33:29 +0200215 * #prev and #parent) are compatible with the ::lyd_node's members.
216 *
217 * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro.
218 */
Michal Vasko4c183312015-09-25 10:41:47 +0200219struct lyd_node_leaf_list {
Michal Vasko2d162e12015-09-24 14:33:29 +0200220 struct lys_node *schema; /**< pointer to the schema definition of this node which is ::lys_node_leaflist
221 structure */
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100222 uint8_t validity; /**< [validity flags](@ref validityflags) */
Michal Vaskoe77dc992017-01-18 12:09:42 +0100223 uint8_t dflt:1; /**< flag for implicit default node */
Radek Krejci0b7704f2016-03-18 12:16:14 +0100224 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 +0100225 do not use this value! */
Michal Vasko2d162e12015-09-24 14:33:29 +0200226
227 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
228 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
229 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
230 never NULL. If there is no sibling node, pointer points to the node
231 itself. In case of the first node, this pointer points to the last
232 node in the list. */
233 struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
234
Michal Vaskod025ee32018-06-28 10:04:19 +0200235#ifdef LY_ENABLED_LYD_PRIV
236 void *priv; /**< private user data, not used by libyang */
237#endif
238
Michal Vasko24affa02018-04-03 09:06:06 +0200239#ifdef LY_ENABLED_CACHE
240 uint32_t hash; /**< hash of this particular node (module name + schema name + string value if leaf-list) */
241#endif
242
Michal Vasko2d162e12015-09-24 14:33:29 +0200243 /* struct lyd_node *child; should be here, but is not */
244
245 /* leaflist's specific members */
Michal Vasko6a027702016-06-30 10:32:14 +0200246 const char *value_str; /**< string representation of value (for comparison, printing,...), always corresponds to value_type */
247 lyd_val value; /**< node's value representation, always corresponds to schema->type.base */
Michal Vasko70bf8e52018-03-26 11:32:33 +0200248 LY_DATA_TYPE _PACKED value_type; /**< type of the value in the node, mainly for union to avoid repeating of type detection */
Michal Vasko101658e2018-06-05 15:05:54 +0200249 uint8_t value_flags; /**< value type flags */
Michal Vasko2d162e12015-09-24 14:33:29 +0200250};
251
252/**
Michal Vasko101658e2018-06-05 15:05:54 +0200253 * @brief Flags for values
254 */
255#define LY_VALUE_UNRES 0x01 /**< flag for unresolved leafref or instance-identifier,
256 leafref - value union is filled as if being the target node's type,
257 instance-identifier - value union should not be accessed */
258#define LY_VALUE_USER 0x02 /**< flag for a user type stored value */
259/* 0x80 is reserveed for internal use */
260
261/**
Radek Krejcibf2abff2016-08-23 15:51:52 +0200262 * @brief Structure for data nodes defined as #LYS_ANYDATA or #LYS_ANYXML.
Michal Vasko2d162e12015-09-24 14:33:29 +0200263 *
264 * Extension for ::lyd_node structure - replaces the ::lyd_node#child member by new #value member. The first five
265 * members (#schema, #attr, #next, #prev and #parent) are compatible with the ::lyd_node's members.
266 *
267 * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro.
268 */
Radek Krejcibf2abff2016-08-23 15:51:52 +0200269struct lyd_node_anydata {
270 struct lys_node *schema; /**< pointer to the schema definition of this node which is ::lys_node_anydata
Michal Vasko2d162e12015-09-24 14:33:29 +0200271 structure */
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100272 uint8_t validity; /**< [validity flags](@ref validityflags) */
Michal Vaskoe77dc992017-01-18 12:09:42 +0100273 uint8_t dflt:1; /**< flag for implicit default node */
Radek Krejci0b7704f2016-03-18 12:16:14 +0100274 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 +0100275 do not use this value! */
Michal Vasko2d162e12015-09-24 14:33:29 +0200276
277 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
278 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
279 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
280 never NULL. If there is no sibling node, pointer points to the node
281 itself. In case of the first node, this pointer points to the last
282 node in the list. */
283 struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
284
Michal Vaskod025ee32018-06-28 10:04:19 +0200285#ifdef LY_ENABLED_LYD_PRIV
286 void *priv; /**< private user data, not used by libyang */
287#endif
288
Michal Vasko24affa02018-04-03 09:06:06 +0200289#ifdef LY_ENABLED_CACHE
290 uint32_t hash; /**< hash of this particular node (module name + schema name) */
291#endif
292
Michal Vasko2d162e12015-09-24 14:33:29 +0200293 /* struct lyd_node *child; should be here, but is not */
294
295 /* anyxml's specific members */
Radek Krejci45826012016-08-24 15:07:57 +0200296 LYD_ANYDATA_VALUETYPE value_type;/**< type of the stored anydata value */
297 union {
298 const char *str; /**< string value, in case of printing as XML, characters like '<' or '&' are escaped */
Michal Vaskodcaf7222018-08-08 16:27:00 +0200299 char *mem; /**< raw memory (used for LYB format) */
Radek Krejci45826012016-08-24 15:07:57 +0200300 struct lyxml_elem *xml; /**< xml tree */
301 struct lyd_node *tree; /**< libyang data tree, does not change the root's parent, so it is not possible
302 to get from the data tree into the anydata/anyxml */
303 } value;
Michal Vasko2d162e12015-09-24 14:33:29 +0200304};
305
306/**
Radek Krejci991a3962016-05-05 15:00:14 +0200307 * @brief list of possible types of differencies in #lyd_difflist
308 */
309typedef enum {
Radek Krejci9e6f0b82016-05-13 17:33:16 +0200310 LYD_DIFF_END = 0, /**< end of the differences list */
Radek Krejci9e6f0b82016-05-13 17:33:16 +0200311 LYD_DIFF_DELETED, /**< deleted node
312 - Node is present in the first tree, but not in the second tree.
313 - To make both trees the same the node in lyd_difflist::first can be deleted from the
314 first tree. The pointer at the same index in the lyd_difflist::second array is
Michal Vasko6407fca2018-04-24 09:44:11 +0200315 NULL.
316 - If the deleted node has some children, these do not appear in the resulting diff
317 separately. In other words, a deleted node is considered deleted with all
318 its children. */
Radek Krejci9e6f0b82016-05-13 17:33:16 +0200319 LYD_DIFF_CHANGED, /**< value of a leaf or anyxml is changed, the lyd_difflist::first and lyd_difflist::second
320 points to the leaf/anyxml instances in the first and the second tree respectively. */
Radek Krejci22d2ca92016-05-17 16:23:51 +0200321 LYD_DIFF_MOVEDAFTER1, /**< user-ordered (leaf-)list item was moved.
322 - To make both trees the same, all #LYD_DIFF_MOVEDAFTER1 transactions must be applied
Radek Krejci9e6f0b82016-05-13 17:33:16 +0200323 to the first tree in the strict order they appear in the difflist. The
324 lyd_difflist::first points to the first tree node being moved and the
325 lyd_difflist::second points to the first tree node after which the first node is
326 supposed to be moved. If the second pointer is NULL, the node is being moved into
327 the beginning as the first node of the (leaf-)list instances. */
Radek Krejci22d2ca92016-05-17 16:23:51 +0200328 LYD_DIFF_CREATED, /**< newly created node
329 - Node is present in the second tree, but not in the first tree.
330 - To make both trees the same the node in lyd_difflist::second is supposed to be
331 inserted (copied via lyd_dup()) into the node (as a child) at the same index in the
332 lyd_difflist::first array (where is its parent). If the lyd_difflist::first at the
Michal Vasko6407fca2018-04-24 09:44:11 +0200333 index is NULL, the missing node is top-level.
334 - If the created node has some children, these do not appear in the resulting diff
335 separately. In other words, a created node is considered created with all
336 its children. */
Radek Krejci22d2ca92016-05-17 16:23:51 +0200337 LYD_DIFF_MOVEDAFTER2 /**< similar to LYD_DIFF_MOVEDAFTER1, but this time the moved item is in the second tree.
338 This type is always used in combination with (as a successor of) #LYD_DIFF_CREATED
339 as an instruction to move the newly created node to a specific position. Note, that
340 due to applicability to the second tree, the meaning of lyd_difflist:first and
341 lyd_difflist:second is inverse in comparison to #LYD_DIFF_MOVEDAFTER1. The
342 lyd_difflist::second points to the (previously) created node in the second tree and
343 the lyd_difflist::first points to the predecessor node in the second tree. If the
344 predecessor is NULL, the node is supposed to bes the first sibling. */
Radek Krejci991a3962016-05-05 15:00:14 +0200345} LYD_DIFFTYPE;
346
347/**
348 * @brief Structure for the result of lyd_diff(), describing differences between two data trees.
349 */
350struct lyd_difflist {
351 LYD_DIFFTYPE *type; /**< array of the differences types, terminated by #LYD_DIFF_END value. */
352 struct lyd_node **first; /**< array of nodes in the first tree for the specific type of difference, see the
353 description of #LYD_DIFFTYPE values for more information. */
354 struct lyd_node **second;/**< array of nodes in the second tree for the specific type of difference, see the
355 description of #LYD_DIFFTYPE values for more information. */
356};
357
358/**
359 * @brief Free the result of lyd_diff(). It frees the structure of the lyd_diff() result, not the referenced nodes.
360 *
361 * @param[in] diff The lyd_diff() result to free.
362 */
363void lyd_free_diff(struct lyd_difflist *diff);
364
365/**
366 * @brief Compare two data trees and provide list of differences.
367 *
368 * Note, that the \p first and the \p second must have the same schema parent (or they must be top-level elements).
369 * In case of using #LYD_OPT_NOSIBLINGS, they both must be instances of the same schema node.
370 *
Radek Krejci913100d2016-05-09 17:23:51 +0200371 * Order of the resulting set follows these rules:
Radek Krejci22d2ca92016-05-17 16:23:51 +0200372 * - To change the first tree into the second tree, the resulting transactions are supposed to be applied in the order
373 * they appear in the result. First, the changed (#LYD_DIFF_CHANGED) nodes are described followed by the deleted
374 * (#LYD_DIFF_DELETED) nodes. Then, the moving of the user-ordered nodes present in both trees (#LYD_DIFF_MOVEDAFTER1)
375 * follows and the last transactions in the results are the newly created (#LYD_DIFF_CREATED) nodes. These nodes are
376 * supposed to be added as the last siblings, but in some case they can need additional move. In such a case, the
377 * #LYD_DIFF_MOVEDAFTER2 transactions can appear.
378 * - The order of the changed (#LYD_DIFF_CHANGED) and created (#LYD_DIFF_CREATED) follows the nodes order in the
379 * second tree - the current siblings are processed first and then the children are processed. Note, that this is
380 * actually not the BFS:
Radek Krejci9e47ddf2016-05-18 15:01:09 +0200381 *
Radek Krejci913100d2016-05-09 17:23:51 +0200382 * 1 2
383 * / \ / \
384 * 3 4 7 8
385 * / \
386 * 5 6
Radek Krejci9e47ddf2016-05-18 15:01:09 +0200387 *
Radek Krejci22d2ca92016-05-17 16:23:51 +0200388 * - The order of the deleted (#LYD_DIFF_DELETED) nodes is the DFS:
Radek Krejci9e47ddf2016-05-18 15:01:09 +0200389 *
390 * 1 6
391 * / \ / \
392 * 2 5 7 8
393 * / \
394 * 3 4
Radek Krejci913100d2016-05-09 17:23:51 +0200395 *
396 * To change the first tree into the second one, it is necessary to follow the order of transactions described in
397 * the result. Note, that it is not possible just to use the transactions in the reverse order to transform the
398 * second tree into the first one. The transactions can be generalized (to be used on a different instance of the
399 * first tree) using lyd_path() to get identifiers for the nodes used in the transactions.
400 *
Radek Krejci9a6a5dd2016-05-05 15:56:24 +0200401 * @param[in] first The first (sub)tree to compare. Without #LYD_OPT_NOSIBLINGS option, all siblings are
Radek Krejci4c3bc112016-05-19 15:09:03 +0200402 * taken into comparison. If NULL, all the \p second nodes are supposed to be top level and they will
403 * be marked as #LYD_DIFF_CREATED.
Radek Krejci9a6a5dd2016-05-05 15:56:24 +0200404 * @param[in] second The second (sub)tree to compare. Without #LYD_OPT_NOSIBLINGS option, all siblings are
Radek Krejci4c3bc112016-05-19 15:09:03 +0200405 * taken into comparison. If NULL, all the \p first nodes will be marked as #LYD_DIFF_DELETED.
Radek Krejci99d737f2016-09-06 11:19:52 +0200406 * @param[in] options The @ref diffoptions are accepted.
Radek Krejci991a3962016-05-05 15:00:14 +0200407 * @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 +0200408 * 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 +0200409 */
410struct lyd_difflist *lyd_diff(struct lyd_node *first, struct lyd_node *second, int options);
411
412/**
Radek Krejci99d737f2016-09-06 11:19:52 +0200413 * @defgroup diffoptions Diff options
414 * @ingroup datatree
415 *
416 * @{
417 */
418/* LYD_DIFFOPT_NOSIBLINGS value is the same as LYD_OPT_NOSIBLINGS due to backward compatibility. The LYD_OPT_NOSIBLINGS
419 * was used previously as an option for lyd_diff(). */
420#define LYD_DIFFOPT_NOSIBLINGS 0x0800 /**< The both trees to diff have to instantiate the same schema node so only the
421 single subtree is compared. */
422#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 +0100423 of both trees. Summary of the modified behavior:
424 - deleted node is replaced with implicit default node - #LYD_DIFF_CHANGED instead delete
425 - created node replaces an implicit default node - #LYD_DIFF_CHANGED instead create
426 - in both cases even if the values match - #LYD_DIFF_CHANGED is still returned, because dlft flag was changed
427 Note that in this case, applying the resulting
Radek Krejci99d737f2016-09-06 11:19:52 +0200428 transactions on the first tree does not result to the exact second tree,
429 because instead of having implicit default nodes you are going to have
430 explicit default nodes. */
431/**@} diffoptions */
432
433/**
Michal Vasko50576712017-07-28 12:28:33 +0200434 * @brief Build data path (usable as path, see @ref howtoxpath) of the data node.
Radek Krejci6d538282016-05-05 14:24:12 +0200435 * @param[in] node Data node to be processed. Note that the node should be from a complete data tree, having a subtree
436 * (after using lyd_unlink()) can cause generating invalid paths.
437 * @return NULL on error, on success the buffer for the resulting path is allocated and caller is supposed to free it
438 * with free().
439 */
Michal Vasko5efa25c2017-01-10 11:34:30 +0100440char *lyd_path(const struct lyd_node *node);
441
442/**
Radek Krejcidef50022016-02-01 16:38:32 +0100443 * @defgroup parseroptions Data parser options
444 * @ingroup datatree
445 *
446 * Various options to change the data tree parsers behavior.
447 *
448 * Default behavior:
449 * - in case of XML, parser reads all data from its input (file, memory, XML tree) including the case of not well-formed
450 * XML document (multiple top-level elements) and if there is an unknown element, it is skipped including its subtree
451 * (see the next point). This can be changed by the #LYD_OPT_NOSIBLINGS option which make parser to read only a single
452 * tree (with a single root element) from its input.
453 * - parser silently ignores the data without a matching node in schema trees. If the caller want to stop
454 * parsing in case of presence of unknown data, the #LYD_OPT_STRICT can be used. The strict mode is useful for
455 * NETCONF servers, since NETCONF clients should always send data according to the capabilities announced by the server.
456 * On the other hand, the default non-strict mode is useful for clients receiving data from NETCONF server since
457 * clients are not required to understand everything the server does. Of course, the optimal strategy for clients is
458 * to use filtering to get only the required data. Having an unknown element of the known namespace is always an error.
459 * The behavior can be changed by #LYD_OPT_STRICT option.
460 * - using obsolete statements (status set to obsolete) just generates a warning, but the processing continues. The
461 * behavior can be changed by #LYD_OPT_OBSOLETE option.
462 * - parser expects that the provided data provides complete datastore content (both the configuration and state data)
463 * and performs data validation according to all YANG rules. This can be a problem in case of representing NETCONF's
464 * subtree filter data, edit-config's data or other type of data set - such data do not represent a complete data set
465 * and some of the validation rules can fail. Therefore there are other options (within lower 8 bits) to make parser
466 * to accept such a data.
Radek Krejcif3c218d2016-03-24 12:40:08 +0100467 * - when parser evaluates when-stmt condition to false, the constrained subtree is automatically removed. If the
468 * #LYD_OPT_NOAUTODEL is used, error is raised instead of silent auto delete. The option (and also this default
469 * behavior) takes effect only in case of #LYD_OPT_DATA or #LYD_OPT_CONFIG type of data.
Radek Krejcidef50022016-02-01 16:38:32 +0100470 * @{
471 */
472
473#define LYD_OPT_DATA 0x00 /**< Default type of data - complete datastore content with configuration as well as
Radek Krejci06f8bb92017-08-02 15:36:25 +0200474 state data. To handle possibly missing (but by default required) ietf-yang-library
475 data, use #LYD_OPT_DATA_NO_YANGLIB or #LYD_OPT_DATA_ADD_YANGLIB options. */
Radek Krejcidef50022016-02-01 16:38:32 +0100476#define LYD_OPT_CONFIG 0x01 /**< A configuration datastore - complete datastore without state data.
477 Validation modifications:
478 - status data are not allowed */
479#define LYD_OPT_GET 0x02 /**< Data content from a NETCONF reply message to the NETCONF \<get\> operation.
480 Validation modifications:
481 - mandatory nodes can be omitted
Michal Vasko62671b92017-01-02 13:08:57 +0100482 - leafrefs and instance-identifier resolution is allowed to fail
Michal Vaskoebf7df22017-03-28 16:08:07 +0200483 - list's keys/unique nodes are not required (so duplication is not checked)
484 - must and when evaluation skipped */
Radek Krejcidef50022016-02-01 16:38:32 +0100485#define LYD_OPT_GETCONFIG 0x04 /**< Data content from a NETCONF reply message to the NETCONF \<get-config\> operation
486 Validation modifications:
487 - mandatory nodes can be omitted
Michal Vasko62671b92017-01-02 13:08:57 +0100488 - leafrefs and instance-identifier resolution is allowed to fail
Radek Krejcidef50022016-02-01 16:38:32 +0100489 - list's keys/unique nodes are not required (so duplication is not checked)
Michal Vaskoebf7df22017-03-28 16:08:07 +0200490 - must and when evaluation skipped
Radek Krejcidef50022016-02-01 16:38:32 +0100491 - status data are not allowed */
492#define LYD_OPT_EDIT 0x08 /**< Content of the NETCONF \<edit-config\>'s config element.
493 Validation modifications:
494 - mandatory nodes can be omitted
Michal Vasko62671b92017-01-02 13:08:57 +0100495 - leafrefs and instance-identifier resolution is allowed to fail
Michal Vaskoebf7df22017-03-28 16:08:07 +0200496 - must and when evaluation skipped
Radek Krejcidef50022016-02-01 16:38:32 +0100497 - status data are not allowed */
Michal Vasko75250262017-02-09 15:36:13 +0100498#define LYD_OPT_RPC 0x10 /**< Data represents RPC or action input parameters. */
499#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 +0100500#define LYD_OPT_NOTIF 0x40 /**< Data represents an event notification data. */
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100501#define LYD_OPT_NOTIF_FILTER 0x80 /**< Data represents a filtered event notification data.
502 Validation modification:
503 - the only requirement is that the data tree matches the schema tree */
PavolVican832f5432018-02-21 00:54:45 +0100504#define LYD_OPT_TYPEMASK 0x10000ff /**< Mask to filter data type options. Always only a single data type option (only
505 single bit from the lower 8 bits) can be set. */
Radek Krejcidef50022016-02-01 16:38:32 +0100506
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100507/* 0x100 reserved, used internally */
508#define LYD_OPT_STRICT 0x0200 /**< Instead of silent ignoring data without schema definition, raise an error. */
509#define LYD_OPT_DESTRUCT 0x0400 /**< Free the provided XML tree during parsing the data. With this option, the
Radek Krejci06f8bb92017-08-02 15:36:25 +0200510 provided XML tree is affected and all successfully parsed data are freed.
Radek Krejcidef50022016-02-01 16:38:32 +0100511 This option is applicable only to lyd_parse_xml() function. */
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100512#define LYD_OPT_OBSOLETE 0x0800 /**< Raise an error when an obsolete statement (status set to obsolete) is used. */
513#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 +0100514 XML input data. */
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100515#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 +0100516 are connected with the schema, but the most validation checks (mandatory nodes,
Michal Vaskod7f9bda2018-03-16 12:33:35 +0100517 list instance uniqueness, etc.) are not performed. This option does not make
518 sense for lyd_validate() so it is ignored by this function. */
Michal Vaskoe3886bb2017-01-02 11:33:28 +0100519#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 +0100520 applicable only in combination with #LYD_OPT_DATA and #LYD_OPT_CONFIG flags.
Radek Krejci03b71f72016-03-16 11:10:09 +0100521 If used, libyang generates validation error instead of silently removing the
522 constrained subtree. */
Michal Vasko3cfa3182017-01-17 10:00:58 +0100523#define LYD_OPT_NOEXTDEPS 0x8000 /**< Allow external dependencies (external leafrefs, instance-identifiers, must,
Michal Vaskof6aa8612017-03-02 10:52:44 +0100524 and when) to not be resolved/satisfied during validation. */
Radek Krejci06f8bb92017-08-02 15:36:25 +0200525#define LYD_OPT_DATA_NO_YANGLIB 0x10000 /**< Ignore (possibly) missing ietf-yang-library data. Applicable only with #LYD_OPT_DATA. */
526#define LYD_OPT_DATA_ADD_YANGLIB 0x20000 /**< Add missing ietf-yang-library data into the validated data tree. Applicable
527 only with #LYD_OPT_DATA. If some ietf-yang-library data are present, they are
528 preserved and option is ignored. */
PavolVican832f5432018-02-21 00:54:45 +0100529#define LYD_OPT_DATA_TEMPLATE 0x1000000 /**< Data represents YANG data template. */
Radek Krejcidef50022016-02-01 16:38:32 +0100530
531/**@} parseroptions */
532
533/**
Michal Vasko299f9832017-01-06 13:29:22 +0100534 * @brief Parse (and validate) data from memory.
Radek Krejcidef50022016-02-01 16:38:32 +0100535 *
536 * In case of LY_XML format, the data string is parsed completely. It means that when it contains
537 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
538 * returned data node is a root of the first tree with other trees connected via the next pointer.
539 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
540 *
541 * @param[in] ctx Context to connect with the data tree being built here.
542 * @param[in] data Serialized data in the specified format.
543 * @param[in] format Format of the input data to be parsed.
Michal Vasko228431e2018-07-10 15:47:11 +0200544 * @param[in] options Parser options, see @ref parseroptions. \p format LYD_LYB uses #LYD_OPT_TRUSTED implicitly.
Michal Vasko6b44d712016-09-12 16:25:46 +0200545 * @param[in] ... Variable arguments depend on \p options. If they include:
546 * - #LYD_OPT_DATA:
547 * - #LYD_OPT_CONFIG:
548 * - #LYD_OPT_GET:
549 * - #LYD_OPT_GETCONFIG:
550 * - #LYD_OPT_EDIT:
551 * - no variable arguments expected.
552 * - #LYD_OPT_RPC:
553 * - #LYD_OPT_NOTIF:
554 * - struct lyd_node *data_tree - additional data tree that will be used
555 * when checking any "when" or "must" conditions in the parsed tree that require
556 * some nodes outside their subtree. It must be a list of top-level elements!
557 * - #LYD_OPT_RPCREPLY:
Michal Vaskod55f1092016-10-24 11:21:08 +0200558 * - const struct ::lyd_node *rpc_act - pointer to the whole RPC or action operation data
559 * tree (the request) of the reply.
Michal Vasko945b96b2016-10-18 11:49:12 +0200560 * - const struct ::lyd_node *data_tree - additional data tree that will be used
Michal Vasko6b44d712016-09-12 16:25:46 +0200561 * when checking any "when" or "must" conditions in the parsed tree that require
562 * some nodes outside their subtree. It must be a list of top-level elements!
Radek Krejcidef50022016-02-01 16:38:32 +0100563 * @return Pointer to the built data tree or NULL in case of empty \p data. To free the returned structure,
564 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
565 * #ly_errno contains appropriate error code (see #LY_ERR).
566 */
Radek Krejci722b0072016-02-01 17:09:45 +0100567struct lyd_node *lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, ...);
Radek Krejcidef50022016-02-01 16:38:32 +0100568
569/**
Michal Vasko299f9832017-01-06 13:29:22 +0100570 * @brief Read (and validate) data from the given file descriptor.
Radek Krejcidef50022016-02-01 16:38:32 +0100571 *
572 * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc.
573 *
574 * In case of LY_XML format, the file content is parsed completely. It means that when it contains
575 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
576 * returned data node is a root of the first tree with other trees connected via the next pointer.
577 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
578 *
579 * @param[in] ctx Context to connect with the data tree being built here.
580 * @param[in] fd The standard file descriptor of the file containing the data tree in the specified format.
581 * @param[in] format Format of the input data to be parsed.
Michal Vasko228431e2018-07-10 15:47:11 +0200582 * @param[in] options Parser options, see @ref parseroptions. \p format LYD_LYB uses #LYD_OPT_TRUSTED implicitly.
Michal Vasko6b44d712016-09-12 16:25:46 +0200583 * @param[in] ... Variable arguments depend on \p options. If they include:
584 * - #LYD_OPT_DATA:
585 * - #LYD_OPT_CONFIG:
586 * - #LYD_OPT_GET:
587 * - #LYD_OPT_GETCONFIG:
588 * - #LYD_OPT_EDIT:
589 * - no variable arguments expected.
590 * - #LYD_OPT_RPC:
591 * - #LYD_OPT_NOTIF:
592 * - struct lyd_node *data_tree - additional data tree that will be used
593 * when checking any "when" or "must" conditions in the parsed tree that require
594 * some nodes outside their subtree. It must be a list of top-level elements!
595 * - #LYD_OPT_RPCREPLY:
Michal Vaskod55f1092016-10-24 11:21:08 +0200596 * - const struct ::lyd_node *rpc_act - pointer to the whole RPC or action operation data
597 * tree (the request) of the reply.
Michal Vasko945b96b2016-10-18 11:49:12 +0200598 * - const struct ::lyd_node *data_tree - additional data tree that will be used
Michal Vasko6b44d712016-09-12 16:25:46 +0200599 * when checking any "when" or "must" conditions in the parsed tree that require
600 * some nodes outside their subtree. It must be a list of top-level elements!
Radek Krejcidef50022016-02-01 16:38:32 +0100601 * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure,
602 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
603 * #ly_errno contains appropriate error code (see #LY_ERR).
604 */
605struct lyd_node *lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, ...);
606
607/**
Michal Vasko299f9832017-01-06 13:29:22 +0100608 * @brief Read (and validate) data from the given file path.
Radek Krejcidef50022016-02-01 16:38:32 +0100609 *
610 * In case of LY_XML format, the file content is parsed completely. It means that when it contains
611 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
612 * returned data node is a root of the first tree with other trees connected via the next pointer.
613 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
614 *
615 * @param[in] ctx Context to connect with the data tree being built here.
616 * @param[in] path Path to the file containing the data tree in the specified format.
617 * @param[in] format Format of the input data to be parsed.
Michal Vasko228431e2018-07-10 15:47:11 +0200618 * @param[in] options Parser options, see @ref parseroptions. \p format LYD_LYB uses #LYD_OPT_TRUSTED implicitly.
Michal Vasko6b44d712016-09-12 16:25:46 +0200619 * @param[in] ... Variable arguments depend on \p options. If they include:
620 * - #LYD_OPT_DATA:
621 * - #LYD_OPT_CONFIG:
622 * - #LYD_OPT_GET:
623 * - #LYD_OPT_GETCONFIG:
624 * - #LYD_OPT_EDIT:
625 * - no variable arguments expected.
626 * - #LYD_OPT_RPC:
627 * - #LYD_OPT_NOTIF:
628 * - struct lyd_node *data_tree - additional data tree that will be used
629 * when checking any "when" or "must" conditions in the parsed tree that require
630 * some nodes outside their subtree. It must be a list of top-level elements!
631 * - #LYD_OPT_RPCREPLY:
Michal Vaskod55f1092016-10-24 11:21:08 +0200632 * - const struct ::lyd_node *rpc_act - pointer to the whole RPC or action operation data
633 * tree (the request) of the reply.
Michal Vasko945b96b2016-10-18 11:49:12 +0200634 * - const struct ::lyd_node *data_tree - additional data tree that will be used
Michal Vasko6b44d712016-09-12 16:25:46 +0200635 * when checking any "when" or "must" conditions in the parsed tree that require
636 * some nodes outside their subtree. It must be a list of top-level elements!
Radek Krejcidef50022016-02-01 16:38:32 +0100637 * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure,
638 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
639 * #ly_errno contains appropriate error code (see #LY_ERR).
640 */
641struct lyd_node *lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options, ...);
642
643/**
Michal Vasko299f9832017-01-06 13:29:22 +0100644 * @brief Parse (and validate) XML tree.
Radek Krejcidef50022016-02-01 16:38:32 +0100645 *
646 * The output data tree is parsed from the given XML tree previously parsed by one of the
647 * lyxml_read* functions.
648 *
Radek Krejci722b0072016-02-01 17:09:45 +0100649 * 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 +0100650 * or the provided root is a root element of a subtree), all the sibling nodes (previous as well as
651 * following) are processed as well. The returned data node is a root of the first tree with other
652 * trees connected via the next pointer. This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
653 *
654 * When the function is used with #LYD_OPT_DESTRUCT, all the successfully parsed data including the
655 * XML \p root and all its siblings (if #LYD_OPT_NOSIBLINGS is not used) are freed. Only with
656 * #LYD_OPT_DESTRUCT option the \p root pointer is changed - if all the data are parsed, it is set
657 * to NULL, otherwise it will hold the XML tree without the successfully parsed elements.
658 *
659 * The context must be the same as the context used to parse XML tree by lyxml_read* function.
660 *
661 * @param[in] ctx Context to connect with the data tree being built here.
662 * @param[in,out] root XML tree to parse (convert) to data tree. By default, parser do not change the XML tree. However,
663 * when #LYD_OPT_DESTRUCT is specified in \p options, parser frees all successfully parsed data.
664 * @param[in] options Parser options, see @ref parseroptions.
Michal Vasko6b44d712016-09-12 16:25:46 +0200665 * @param[in] ... Variable arguments depend on \p options. If they include:
666 * - #LYD_OPT_DATA:
667 * - #LYD_OPT_CONFIG:
668 * - #LYD_OPT_GET:
669 * - #LYD_OPT_GETCONFIG:
670 * - #LYD_OPT_EDIT:
671 * - no variable arguments expected.
672 * - #LYD_OPT_RPC:
673 * - #LYD_OPT_NOTIF:
674 * - struct lyd_node *data_tree - additional data tree that will be used
675 * when checking any "when" or "must" conditions in the parsed tree that require
676 * some nodes outside their subtree. It must be a list of top-level elements!
677 * - #LYD_OPT_RPCREPLY:
Michal Vaskod55f1092016-10-24 11:21:08 +0200678 * - const struct ::lyd_node *rpc_act - pointer to the whole RPC or action operation data
679 * tree (the request) of the reply.
Michal Vasko945b96b2016-10-18 11:49:12 +0200680 * - const struct ::lyd_node *data_tree - additional data tree that will be used
Michal Vasko6b44d712016-09-12 16:25:46 +0200681 * when checking any "when" or "must" conditions in the parsed tree that require
682 * some nodes outside their subtree. It must be a list of top-level elements!
Radek Krejcidef50022016-02-01 16:38:32 +0100683 * @return Pointer to the built data tree or NULL in case of empty \p root. To free the returned structure,
684 * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error,
685 * #ly_errno contains appropriate error code (see #LY_ERR).
686 */
687struct lyd_node *lyd_parse_xml(struct ly_ctx *ctx, struct lyxml_elem **root, int options,...);
688
689/**
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200690 * @brief Create a new container node in a data tree.
691 *
Michal Vasko299f9832017-01-06 13:29:22 +0100692 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
693 *
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200694 * @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 +0200695 * @param[in] module Module with the node being created.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200696 * @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 +0200697 * #LYS_NOTIF, #LYS_RPC, or #LYS_ACTION.
Michal Vasko1dca6882015-10-22 14:29:42 +0200698 * @return New node, NULL on error.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200699 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100700struct lyd_node *lyd_new(struct lyd_node *parent, const struct lys_module *module, const char *name);
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200701
702/**
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200703 * @brief Create a new leaf or leaflist node in a data tree with a string value that is converted to
704 * the actual value.
705 *
Michal Vasko299f9832017-01-06 13:29:22 +0100706 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
707 *
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200708 * @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 +0200709 * @param[in] module Module with the node being created.
710 * @param[in] name Schema node name of the new data node.
Michal Vasko3e671b52015-10-23 16:23:15 +0200711 * @param[in] val_str String form of the value of the node being created. In case the type is #LY_TYPE_INST
712 * 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 +0200713 * @return New node, NULL on error.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200714 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100715struct lyd_node *lyd_new_leaf(struct lyd_node *parent, const struct lys_module *module, const char *name,
Michal Vasko3e671b52015-10-23 16:23:15 +0200716 const char *val_str);
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200717
718/**
Radek Krejcib9b4d002016-01-18 13:08:51 +0100719 * @brief Change value of a leaf node.
720 *
Michal Vasko299f9832017-01-06 13:29:22 +0100721 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
722 *
Radek Krejcib9b4d002016-01-18 13:08:51 +0100723 * Despite the prototype allows to provide a leaflist node as \p leaf parameter, only leafs are accepted.
Michal Vasko2da8e042018-05-25 11:10:13 +0200724 * Also, the leaf will never be default after calling this function successfully.
Radek Krejcib9b4d002016-01-18 13:08:51 +0100725 *
726 * @param[in] leaf A leaf node to change.
727 * @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
728 * or #LY_TYPE_IDENT, JSON node-id format is expected (nodes are prefixed with module names, not XML namespaces).
Michal Vasko3c0eb752018-02-08 16:09:19 +0100729 * @return 0 if the leaf was changed successfully (either its value changed or at least its default flag was cleared),
730 * <0 on error,
731 * 1 if the (canonical) value matched the original one and no value neither default flag change occured.
Radek Krejcib9b4d002016-01-18 13:08:51 +0100732 */
733int lyd_change_leaf(struct lyd_node_leaf_list *leaf, const char *val_str);
734
735/**
Radek Krejci45826012016-08-24 15:07:57 +0200736 * @brief Create a new anydata or anyxml node in a data tree.
737 *
Michal Vasko299f9832017-01-06 13:29:22 +0100738 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
739 *
Radek Krejci45826012016-08-24 15:07:57 +0200740 * 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 +0200741 *
Michal Vasko2d162e12015-09-24 14:33:29 +0200742 * @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 +0200743 * @param[in] module Module with the node being created.
Radek Krejci45826012016-08-24 15:07:57 +0200744 * @param[in] name Schema node name of the new data node. The schema node determines if the anydata or anyxml node
745 * is created.
746 * @param[in] value Pointer to the value data to be stored in the anydata/anyxml node. The type of the data is
747 * determined according to the \p value_type parameter.
748 * @param[in] value_type Type of the provided data \p value.
Michal Vasko1dca6882015-10-22 14:29:42 +0200749 * @return New node, NULL on error.
Michal Vasko2d162e12015-09-24 14:33:29 +0200750 */
Radek Krejci45826012016-08-24 15:07:57 +0200751struct lyd_node *lyd_new_anydata(struct lyd_node *parent, const struct lys_module *module, const char *name,
752 void *value, LYD_ANYDATA_VALUETYPE value_type);
Michal Vasko2d162e12015-09-24 14:33:29 +0200753
754/**
Michal Vasko945b96b2016-10-18 11:49:12 +0200755 * @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 +0100756 *
Michal Vasko299f9832017-01-06 13:29:22 +0100757 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
758 *
Michal Vasko98a5a742016-05-11 11:02:56 +0200759 * @param[in] parent Parent node for the node being created. NULL in case of creating top level element.
760 * @param[in] module Module with the node being created.
Michal Vasko945b96b2016-10-18 11:49:12 +0200761 * @param[in] name Schema node name of the new data node. The node should only be #LYS_CONTAINER or #LYS_LIST,
762 * but accepted are also #LYS_NOTIF, #LYS_RPC, or #LYS_ACTION.
Michal Vasko0df122f2015-12-14 13:38:21 +0100763 * @return New node, NULL on error.
764 */
Michal Vasko98a5a742016-05-11 11:02:56 +0200765struct lyd_node *lyd_new_output(struct lyd_node *parent, const struct lys_module *module, const char *name);
Michal Vasko50c0a872016-01-13 14:34:11 +0100766
767/**
Michal Vasko98a5a742016-05-11 11:02:56 +0200768 * @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 +0200769 * the actual value. Ignore RPC/action input nodes and instead use RPC/action output ones.
Michal Vasko50c0a872016-01-13 14:34:11 +0100770 *
Michal Vasko299f9832017-01-06 13:29:22 +0100771 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
772 *
Michal Vasko98a5a742016-05-11 11:02:56 +0200773 * @param[in] parent Parent node for the node being created. NULL in case of creating top level element.
774 * @param[in] module Module with the node being created.
775 * @param[in] name Schema node name of the new data node.
Michal Vasko50c0a872016-01-13 14:34:11 +0100776 * @param[in] val_str String form of the value of the node being created. In case the type is #LY_TYPE_INST
777 * or #LY_TYPE_IDENT, JSON node-id format is expected (nodes are prefixed with module names, not XML namespaces).
778 * @return New node, NULL on error.
779 */
Michal Vasko98a5a742016-05-11 11:02:56 +0200780struct lyd_node *lyd_new_output_leaf(struct lyd_node *parent, const struct lys_module *module, const char *name,
781 const char *val_str);
Michal Vasko50c0a872016-01-13 14:34:11 +0100782
783/**
Michal Vasko945b96b2016-10-18 11:49:12 +0200784 * @brief Create a new anydata or anyxml node in a data tree. Ignore RPC/action input nodes and instead use
785 * RPC/action output ones.
Michal Vasko50c0a872016-01-13 14:34:11 +0100786 *
Michal Vasko299f9832017-01-06 13:29:22 +0100787 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
788 *
Michal Vasko98a5a742016-05-11 11:02:56 +0200789 * @param[in] parent Parent node for the node being created. NULL in case of creating top level element.
790 * @param[in] module Module with the node being created.
Radek Krejci45826012016-08-24 15:07:57 +0200791 * @param[in] name Schema node name of the new data node. The schema node determines if the anydata or anyxml node
792 * is created.
793 * @param[in] value Pointer to the value data to be stored in the anydata/anyxml node. The type of the data is
794 * determined according to the \p value_type parameter. Data are supposed to be dynamically allocated.
795 * Since it is directly attached into the created data node, caller is supposed to not manipulate with
796 * the data after a successful call (including calling free() on the provided data).
797 * @param[in] value_type Type of the provided data \p value.
Michal Vasko50c0a872016-01-13 14:34:11 +0100798 * @return New node, NULL on error.
799 */
Radek Krejci45826012016-08-24 15:07:57 +0200800struct lyd_node *lyd_new_output_anydata(struct lyd_node *parent, const struct lys_module *module, const char *name,
801 void *value, LYD_ANYDATA_VALUETYPE value_type);
Michal Vasko0df122f2015-12-14 13:38:21 +0100802
803/**
PavolVican832f5432018-02-21 00:54:45 +0100804 * @brief Create a new yang-data template in a data tree. It creates container, which name is in third parameter.
805 *
806 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
807 *
808 * @param[in] module Module with the node being created.
809 * @param[in] name_template Yang-data template name. This name is used for searching of yang-data instance.
810 * @param[in] name Schema node name of the new data node. This node is container.
811 * @return New node, NULL on error.
812 */
813struct lyd_node *lyd_new_yangdata(const struct lys_module *module, const char *name_template, const char *name);
814
815/**
Michal Vaskof5299282016-03-16 13:32:02 +0100816 * @defgroup pathoptions Data path creation options
817 * @ingroup datatree
818 *
819 * Various options to change lyd_new_path() behavior.
820 *
821 * Default behavior:
Michal Vasko3c0eb752018-02-08 16:09:19 +0100822 * - if the target node already exists (and is not default), an error is returned.
Michal Vasko9db078d2016-03-23 11:08:51 +0100823 * - the whole path to the target node is created (with any missing parents) if necessary.
Michal Vasko2411b942016-03-23 13:50:03 +0100824 * - RPC output schema children are completely ignored in all modules. Input is searched and nodes created normally.
Michal Vaskof5299282016-03-16 13:32:02 +0100825 * @{
826 */
827
Michal Vasko3c0eb752018-02-08 16:09:19 +0100828#define LYD_PATH_OPT_UPDATE 0x01 /**< If the target node exists, is a leaf, and it is updated with a new value or its
829 default flag is changed, it is returned. If the target node exists and is not
830 a leaf or generally no change occurs in the \p data_tree, NULL is returned and no error set. */
Michal Vasko1fabbef2018-04-03 09:13:02 +0200831#define LYD_PATH_OPT_NOPARENT 0x02 /**< If any parents of the target node do not exist, return an error instead of implicitly creating them. */
Michal Vasko945b96b2016-10-18 11:49:12 +0200832#define LYD_PATH_OPT_OUTPUT 0x04 /**< Changes the behavior to ignoring RPC/action input schema nodes and using only output ones. */
Michal Vasko6b293d12017-10-31 10:03:22 +0100833#define LYD_PATH_OPT_DFLT 0x08 /**< The created node (nodes, if also creating the parents) is a default one. If working with data tree of type #LYD_OPT_DATA, #LYD_OPT_CONFIG, #LYD_OPT_RPC, #LYD_OPT_RPCREPLY, or #LYD_OPT_NOTIF, this flag is never needed and therefore should not be used. However, if the tree is #LYD_OPT_GET, #LYD_OPT_GETCONFIG, or #LYD_OPT_EDIT, the default nodes are not created during validation and using this flag one can set them (see @ref howtodatawd). */
Michal Vaskof5299282016-03-16 13:32:02 +0100834
835/** @} pathoptions */
836
837/**
838 * @brief Create a new data node based on a simple XPath.
839 *
Michal Vasko299f9832017-01-06 13:29:22 +0100840 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
841 *
Michal Vasko8d18ef52016-04-06 12:21:46 +0200842 * The new node is normally inserted at the end, either as the last child of a parent or as the last sibling
843 * if working with top-level elements. However, when manipulating RPC input or output, schema ordering is
Michal Vasko98a5a742016-05-11 11:02:56 +0200844 * required and always guaranteed.
Michal Vasko58f74f12016-03-24 13:26:06 +0100845 *
Michal Vasko8c419642016-04-13 14:22:01 +0200846 * If \p path points to a list key and the list does not exist, the key value from the predicate is used
847 * and \p value is ignored.
848 *
Michal Vasko7800b242018-04-03 11:15:05 +0200849 * @param[in] data_tree Existing data tree to add to/modify (including siblings). If creating RPCs/actions, there
850 * should only be one RPC/action and either input or output, not both. Can be NULL.
Michal Vaskof5299282016-03-16 13:32:02 +0100851 * @param[in] ctx Context to use. Mandatory if \p data_tree is NULL.
Michal Vasko50576712017-07-28 12:28:33 +0200852 * @param[in] path Simple data path (see @ref howtoxpath). List nodes can have predicates, one for each list key
853 * in the correct order and with its value as well or using specific instance position, leaves and leaf-lists
Michal Vasko310bc582018-05-22 10:47:59 +0200854 * can have predicates too that have preference over \p value. When specifying an identityref value in a predicate,
855 * you MUST use the module name as the value prefix!
Radek Krejci45826012016-08-24 15:07:57 +0200856 * @param[in] value Value of the new leaf/lealf-list (const char*). If creating anydata or anyxml, the following
Michal Vasko50576712017-07-28 12:28:33 +0200857 * \p value_type parameter is required to be specified correctly. If creating nodes of other types, the
858 * parameter is ignored.
Radek Krejci45826012016-08-24 15:07:57 +0200859 * @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 +0100860 * @param[in] options Bitmask of options flags, see @ref pathoptions.
Michal Vasko8c419642016-04-13 14:22:01 +0200861 * @return First created (or updated with #LYD_PATH_OPT_UPDATE) node,
Michal Vasko17bb4902016-04-05 15:20:51 +0200862 * 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 +0200863 * NULL and ly_errno is set on error.
Michal Vaskof5299282016-03-16 13:32:02 +0100864 */
Radek Krejci45826012016-08-24 15:07:57 +0200865struct lyd_node *lyd_new_path(struct lyd_node *data_tree, struct ly_ctx *ctx, const char *path, void *value,
866 LYD_ANYDATA_VALUETYPE value_type, int options);
Michal Vaskof5299282016-03-16 13:32:02 +0100867
868/**
Michal Vaskoae5a53e2017-01-05 10:33:41 +0100869 * @brief Learn the relative instance position of a list or leaf-list within other instances of the
870 * same schema node.
871 *
872 * @param[in] node List or leaf-list to get the position of.
873 * @return 0 on error or positive integer of the instance position.
874 */
875unsigned int lyd_list_pos(const struct lyd_node *node);
876
877/**
Michal Vasko39dc8992018-04-03 11:32:00 +0200878 * @brief Create a copy of the specified data tree \p node. Schema references are kept the same. Use carefully,
879 * since libyang silently creates default nodes, it is always better to use lyd_dup_withsiblings() to duplicate
880 * the complete data tree.
Michal Vasko2d162e12015-09-24 14:33:29 +0200881 *
Michal Vasko299f9832017-01-06 13:29:22 +0100882 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
Michal Vasko2f95fe62016-12-01 09:36:08 +0100883 *
Michal Vasko2d162e12015-09-24 14:33:29 +0200884 * @param[in] node Data tree node to be duplicated.
885 * @param[in] recursive 1 if all children are supposed to be also duplicated.
886 * @return Created copy of the provided data \p node.
887 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100888struct lyd_node *lyd_dup(const struct lyd_node *node, int recursive);
Michal Vasko2d162e12015-09-24 14:33:29 +0200889
890/**
Michal Vasko39dc8992018-04-03 11:32:00 +0200891 * @brief Create a copy of the specified data tree and all its siblings (preceding as well as following).
892 * Schema references are kept the same.
893 *
894 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
895 *
896 * @param[in] node Data tree sibling node to be duplicated.
897 * @param[in] recursive 1 if all children of all the siblings are supposed to be also duplicated.
898 * @return Created copy of the provided data \p node and all of its siblings.
899 */
900struct lyd_node *lyd_dup_withsiblings(const struct lyd_node *node, int recursive);
901
902/**
Radek Krejcia17c85c2017-01-06 12:22:34 +0100903 * @brief Create a copy of the specified data tree \p node in the different context. All the
904 * schema references and strings are re-mapped into the specified context.
905 *
906 * If the target context does not contain the schemas used in the source data tree, error
907 * is raised and the new data tree is not created.
908 *
909 * @param[in] node Data tree node to be duplicated.
910 * @param[in] recursive 1 if all children are supposed to be also duplicated.
911 * @param[in] ctx Target context for the duplicated data.
912 * @return Created copy of the provided data \p node.
913 */
914struct lyd_node *lyd_dup_to_ctx(const struct lyd_node *node, int recursive, struct ly_ctx *ctx);
915
916/**
Michal Vasko299f9832017-01-06 13:29:22 +0100917 * @brief Merge a (sub)tree into a data tree.
918 *
919 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
920 *
921 * Missing nodes are merged, leaf values updated.
Radek Krejci8f4eba52017-01-06 15:32:41 +0100922 *
Michal Vasko45fb2822016-04-18 13:32:17 +0200923 * If \p target and \p source do not share the top-level schema node, even if they
924 * are from different modules, \p source parents up to top-level node will be created and
925 * linked to the \p target (but only containers can be created this way, lists need keys,
926 * so if lists are missing, an error will be returned).
927 *
Radek Krejci2ffe9932017-01-06 16:29:47 +0100928 * If the source data tree is in a different context, the resulting data are placed into the context
929 * of the target tree.
Michal Vasko45fb2822016-04-18 13:32:17 +0200930 *
Michal Vaskocf6dc7e2016-04-18 16:00:37 +0200931 * @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 +0200932 * @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 +0100933 * @param[in] options Bitmask of the following option flags:
Michal Vasko0300b532016-09-14 12:16:02 +0200934 * - #LYD_OPT_DESTRUCT - spend \p source in the function, otherwise \p source is left untouched,
935 * - #LYD_OPT_NOSIBLINGS - merge only the \p source subtree (ignore siblings), otherwise merge
936 * \p source and all its succeeding siblings (preceeding ones are still ignored!),
937 * - #LYD_OPT_EXPLICIT - when merging an explicitly set node and a default node, always put
938 * the explicit node into \p target, otherwise the node which is in \p source is used.
Michal Vasko45fb2822016-04-18 13:32:17 +0200939 * @return 0 on success, nonzero in case of an error.
940 */
941int lyd_merge(struct lyd_node *target, const struct lyd_node *source, int options);
942
Radek Krejci2ffe9932017-01-06 16:29:47 +0100943/**
944 * @brief Same as lyd_merge(), but moves the resulting data into the specified context.
945 *
946 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
947 *
948 * @param[in] trg Top-level (or an RPC output child) data tree to merge to. Must be valid. If its context
949 * 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 +0100950 * tree in the required context is returned on success. To keep the \p trg tree, convert it to the
951 * 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 +0100952 * @param[in] src Data tree to merge \p target with. Must be valid (at least as a subtree).
953 * @param[in] options Bitmask of the following option flags:
954 * - #LYD_OPT_DESTRUCT - spend \p source in the function, otherwise \p source is left untouched,
955 * - #LYD_OPT_NOSIBLINGS - merge only the \p source subtree (ignore siblings), otherwise merge
956 * \p source and all its succeeding siblings (preceeding ones are still ignored!),
957 * - #LYD_OPT_EXPLICIT - when merging an explicitly set node and a default node, always put
958 * the explicit node into \p target, otherwise the node which is in \p source is used.
959 * @param[in] ctx Target context in which the result will be created. Note that the successful merge requires to have
960 * all the used modules in the source and target data trees loaded in the target context.
961 * @return 0 on success, nonzero in case of an error.
962 */
963int lyd_merge_to_ctx(struct lyd_node **trg, const struct lyd_node *src, int options, struct ly_ctx *ctx);
964
Michal Vasko0300b532016-09-14 12:16:02 +0200965#define LYD_OPT_EXPLICIT 0x0100
966
Michal Vasko45fb2822016-04-18 13:32:17 +0200967/**
Michal Vasko2d162e12015-09-24 14:33:29 +0200968 * @brief Insert the \p node element as child to the \p parent element. The \p node is inserted as a last child of the
969 * \p parent.
970 *
Michal Vasko299f9832017-01-06 13:29:22 +0100971 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
972 *
Michal Vaskob6c51f12016-09-14 12:15:11 +0200973 * - if the node is part of some other tree, it is automatically unlinked.
974 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
975 * - 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 +0200976 * element.
Michal Vaskob6c51f12016-09-14 12:15:11 +0200977 * - 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 +0200978 * by the new node.
979 * - if a default node is being inserted and the target tree already contains non-default instance, the existing
980 * instance is silently replaced. If it contains the exact same default node, it is replaced as well.
Michal Vaskob6c51f12016-09-14 12:15:11 +0200981 * - 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 +0200982 * node is inserted and it is up to the caller to solve the presence of multiple instances afterwards.
983 *
984 * Note that this function differs from lyd_insert_before() and lyd_insert_after() because the position of the
985 * node being inserted is determined automatically according to the rules described above. In contrast to
986 * 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 +0200987 * NULL. If inserting something larger and not fitting the mentioned use-cases (or simply if unsure), you can always
988 * use lyd_merge(), it should be able to handle any situation.
Michal Vasko2d162e12015-09-24 14:33:29 +0200989 *
990 * @param[in] parent Parent node for the \p node being inserted.
991 * @param[in] node The node being inserted.
Michal Vasko24337392015-10-16 09:58:16 +0200992 * @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 +0200993 * in the data tree.
994 */
Michal Vasko24337392015-10-16 09:58:16 +0200995int lyd_insert(struct lyd_node *parent, struct lyd_node *node);
Michal Vasko2d162e12015-09-24 14:33:29 +0200996
997/**
Radek Krejcifd0bcf02016-09-09 13:28:34 +0200998 * @brief Insert the \p node element as a last sibling of the specified \p sibling element.
999 *
Michal Vasko299f9832017-01-06 13:29:22 +01001000 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
1001 *
Michal Vaskob6c51f12016-09-14 12:15:11 +02001002 * - if the node is part of some other tree, it is automatically unlinked.
1003 * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted.
1004 * - 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 +02001005 * element.
Michal Vaskob6c51f12016-09-14 12:15:11 +02001006 * - 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 +02001007 * by the new node.
1008 * - if a default node is being inserted and the target tree already contains non-default instance, the existing
1009 * instance is silently replaced. If it contains the exact same default node, it is replaced as well.
Michal Vaskob6c51f12016-09-14 12:15:11 +02001010 * - if a non-default node is being inserted and there is already its non-default instance in the target tree, the new
1011 * 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 +02001012 *
1013 * Note that this function differs from lyd_insert_before() and lyd_insert_after() because the position of the
1014 * 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 +02001015 * lyd_insert_sibling() can be used to insert top-level elements. If inserting something larger and not fitting
1016 * the mentioned use-cases (or simply if unsure), you can always use lyd_merge(), it should be able to handle
1017 * any situation.
Radek Krejcifd0bcf02016-09-09 13:28:34 +02001018 *
1019 * @param[in,out] sibling Sibling node as a reference where to insert the \p node. When function succeeds, the sibling
1020 * is always set to point to the first sibling node. Note that in some cases described above, the provided sibling
1021 * node could be removed from the tree.
1022 * @param[in] node The node being inserted.
1023 * @return 0 on success, nonzero in case of error, e.g. when the node is being inserted to an inappropriate place
1024 * in the data tree.
1025 */
1026int lyd_insert_sibling(struct lyd_node **sibling, struct lyd_node *node);
1027
1028/**
Michal Vasko3f7dba12015-10-15 13:09:27 +02001029 * @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 +01001030 * siblings (just moving \p node position).
1031 *
1032 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
Michal Vasko2d162e12015-09-24 14:33:29 +02001033 *
Michal Vaskob6c51f12016-09-14 12:15:11 +02001034 * - 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 +02001035 * - if a default node is being inserted and the target tree already contains non-default instance, the existing
1036 * instance is removed. If it contains the exact same default node, it is removed as well.
Michal Vaskob6c51f12016-09-14 12:15:11 +02001037 * - if a non-default node is being inserted and there is already its non-default instance in the target tree, the new
1038 * node is inserted and it is up to the caller to solve the presence of multiple instances afterwards.
1039 *
Michal Vasko2d162e12015-09-24 14:33:29 +02001040 * @param[in] sibling The data tree node before which the \p node will be inserted.
Radek Krejci20a5f292016-02-09 15:04:49 +01001041 * @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 +02001042 * @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 +02001043 * in the data tree.
1044 */
Michal Vasko24337392015-10-16 09:58:16 +02001045int lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node);
Michal Vasko2d162e12015-09-24 14:33:29 +02001046
1047/**
Radek Krejci20a5f292016-02-09 15:04:49 +01001048 * @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 +01001049 * siblings (just moving \p node position).
1050 *
1051 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
Michal Vasko2d162e12015-09-24 14:33:29 +02001052 *
Michal Vaskob6c51f12016-09-14 12:15:11 +02001053 * - 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 +02001054 * - if a default node is being inserted and the target tree already contains non-default instance, the existing
1055 * instance is removed. If it contains the exact same default node, it is removed as well.
Michal Vaskob6c51f12016-09-14 12:15:11 +02001056 * - if a non-default node is being inserted and there is already its non-default instance in the target tree, the new
1057 * node is inserted and it is up to the caller to solve the presence of multiple instances afterwards.
1058 *
Michal Vasko3f7dba12015-10-15 13:09:27 +02001059 * @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 +01001060 * are already siblings (just moving \p node position), skip validation.
Radek Krejci20a5f292016-02-09 15:04:49 +01001061 * @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 +02001062 * @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 +02001063 * in the data tree.
1064 */
Michal Vasko24337392015-10-16 09:58:16 +02001065int lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node);
1066
1067/**
Michal Vasko2411b942016-03-23 13:50:03 +01001068 * @brief Order siblings according to the schema node ordering.
1069 *
Michal Vasko299f9832017-01-06 13:29:22 +01001070 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
1071 *
Michal Vasko58f74f12016-03-24 13:26:06 +01001072 * If the siblings include data nodes from other modules, they are
1073 * sorted based on the module order in the context.
1074 *
1075 * @param[in] sibling Node, whose siblings will be sorted.
1076 * @param[in] recursive Whether sort all siblings of siblings, recursively.
1077 * @return 0 on success, nonzero in case of an error.
Michal Vasko2411b942016-03-23 13:50:03 +01001078 */
Michal Vasko58f74f12016-03-24 13:26:06 +01001079int lyd_schema_sort(struct lyd_node *sibling, int recursive);
Michal Vasko2411b942016-03-23 13:50:03 +01001080
1081/**
Michal Vasko50576712017-07-28 12:28:33 +02001082 * @brief Search in the given data for instances of nodes matching the provided path.
Michal Vasko105cef12016-02-04 12:06:26 +01001083 *
Michal Vasko50576712017-07-28 12:28:33 +02001084 * Learn more about the path format on page @ref howtoxpath.
Michal Vasko105cef12016-02-04 12:06:26 +01001085 *
Michal Vasko50576712017-07-28 12:28:33 +02001086 * @param[in] ctx_node Path context node.
1087 * @param[in] path Data path expression filtering the matching nodes.
1088 * @return Set of found data nodes. If no nodes are matching \p path or the result
Michal Vasko105cef12016-02-04 12:06:26 +01001089 * would be a number, a string, or a boolean, the returned set is empty. In case of an error, NULL is returned.
1090 */
Michal Vasko50576712017-07-28 12:28:33 +02001091struct ly_set *lyd_find_path(const struct lyd_node *ctx_node, const char *path);
Michal Vasko105cef12016-02-04 12:06:26 +01001092
1093/**
Radek Krejcic5b6b912016-01-18 16:35:35 +01001094 * @brief Search in the given data for instances of the provided schema node.
1095 *
1096 * The \p data is used to find the data root and function then searches in the whole tree and all sibling trees.
1097 *
1098 * @param[in] data A node in the data tree to search.
1099 * @param[in] schema Schema node of the data nodes caller want to find.
Michal Vasko46a4bf92016-09-08 08:23:49 +02001100 * @return Set of found data nodes. If no data node is found, the returned set is empty.
Radek Krejcic5b6b912016-01-18 16:35:35 +01001101 * In case of error, NULL is returned.
1102 */
Michal Vaskof06fb5b2016-09-08 10:05:56 +02001103struct ly_set *lyd_find_instance(const struct lyd_node *data, const struct lys_node *schema);
Radek Krejcic5b6b912016-01-18 16:35:35 +01001104
1105/**
Radek Krejcid788a522016-07-25 14:57:38 +02001106 * @brief Get the first sibling of the given node.
1107 *
1108 * @param[in] node Node which first sibling is going to be the result.
1109 * @return The first sibling of the given node or the node itself if it is the first child of the parent.
1110 */
1111struct lyd_node *lyd_first_sibling(struct lyd_node *node);
1112
1113/**
Michal Vasko24337392015-10-16 09:58:16 +02001114 * @brief Validate \p node data subtree.
1115 *
Michal Vaskodedea832016-04-19 11:24:45 +02001116 * @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 +02001117 * can modify the provided tree including the root \p node.
Michal Vasko24337392015-10-16 09:58:16 +02001118 * @param[in] options Options for the inserting data to the target data tree options, see @ref parseroptions.
Michal Vaskocdb90172016-09-13 09:34:36 +02001119 * @param[in] var_arg Variable argument depends on \p options. If they include:
Michal Vasko6b44d712016-09-12 16:25:46 +02001120 * - #LYD_OPT_DATA:
1121 * - #LYD_OPT_CONFIG:
1122 * - #LYD_OPT_GET:
1123 * - #LYD_OPT_GETCONFIG:
1124 * - #LYD_OPT_EDIT:
Michal Vaskocdb90172016-09-13 09:34:36 +02001125 * - struct ly_ctx *ctx - context to use when \p node is NULL (for checking an empty tree),
1126 * otherwise can be NULL.
Michal Vasko6b44d712016-09-12 16:25:46 +02001127 * - #LYD_OPT_RPC:
1128 * - #LYD_OPT_RPCREPLY:
1129 * - #LYD_OPT_NOTIF:
1130 * - struct ::lyd_node *data_tree - additional data tree that will be used when checking
Michal Vaskocdb90172016-09-13 09:34:36 +02001131 * any "when" or "must" conditions in the \p node tree
1132 * that require some nodes outside their subtree. If set,
1133 * it must be a list of top-level elements!
Radek Krejci92ece002016-04-04 15:45:05 +02001134 * @return 0 on success, nonzero in case of an error.
Michal Vasko24337392015-10-16 09:58:16 +02001135 */
Michal Vaskocdb90172016-09-13 09:34:36 +02001136int lyd_validate(struct lyd_node **node, int options, void *var_arg);
Michal Vasko2d162e12015-09-24 14:33:29 +02001137
1138/**
Radek Krejcif6fac5e2017-05-18 15:14:18 +02001139 * @brief Check restrictions applicable to the particular leaf/leaf-list on the given string value.
1140 *
1141 * Validates the value only using the types' restrictions. Do not check the rest of restrictions dependent on the
1142 * data tree (must, when statements or uniqueness of the leaf-list item).
1143 *
Radek Krejcie3bd2f32017-08-07 13:52:28 +02001144 * The format of the data must follow rules for the lexical representation of the specific YANG type. Note
1145 * that if there are some extensions of the lexical representation for the YANG module (default value), they are
1146 * not supported by this function - it strictly follows rules for the lexical representations in data trees.
1147 *
Radek Krejcif6fac5e2017-05-18 15:14:18 +02001148 * @param[in] node Schema node of the leaf or leaf-list eventually holding the \p value.
1149 * @param[in] value Value to be checked (NULL is checked as empty string).
1150 * @return EXIT_SUCCESS if the \p value conforms to the restrictions, EXIT_FAILURE otherwise.
1151 */
1152int lyd_validate_value(struct lys_node *node, const char *value);
1153
1154/**
Radek Krejci46180b52016-08-31 16:01:32 +02001155 * @brief Get know if the node contain (despite implicit or explicit) default value.
Radek Krejci7b4309c2016-03-23 10:30:29 +01001156 *
Radek Krejci46180b52016-08-31 16:01:32 +02001157 * @param[in] node The leaf or leaf-list to check. Note, that leaf-list is marked as default only when the complete
1158 * and only the default set is present (node's siblings are also checked).
1159 * @return 1 if the node contains the default value, 0 otherwise.
Radek Krejci7b4309c2016-03-23 10:30:29 +01001160 */
Radek Krejci46180b52016-08-31 16:01:32 +02001161int lyd_wd_default(struct lyd_node_leaf_list *node);
Radek Krejci6b8f6ac2016-03-23 12:33:04 +01001162
1163/**
Michal Vasko55f60be2015-10-14 13:12:58 +02001164 * @brief Unlink the specified data subtree. All referenced namespaces are copied.
Michal Vasko2d162e12015-09-24 14:33:29 +02001165 *
Michal Vasko299f9832017-01-06 13:29:22 +01001166 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
1167 *
Michal Vasko2d162e12015-09-24 14:33:29 +02001168 * Note, that the node's connection with the schema tree is kept. Therefore, in case of
1169 * reconnecting the node to a data tree using lyd_paste() it is necessary to paste it
1170 * to the appropriate place in the data tree following the schema.
1171 *
1172 * @param[in] node Data tree node to be unlinked (together with all children).
1173 * @return 0 for success, nonzero for error
1174 */
1175int lyd_unlink(struct lyd_node *node);
1176
1177/**
Radek Krejcifd0bcf02016-09-09 13:28:34 +02001178 * @brief Free (and unlink) the specified data subtree. Use carefully, since libyang silently creates default nodes,
1179 * it is always better to use lyd_free_withsiblings() to free the complete data tree.
Michal Vasko2d162e12015-09-24 14:33:29 +02001180 *
Michal Vasko299f9832017-01-06 13:29:22 +01001181 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
1182 *
Michal Vasko2d162e12015-09-24 14:33:29 +02001183 * @param[in] node Root of the (sub)tree to be freed.
1184 */
1185void lyd_free(struct lyd_node *node);
1186
1187/**
Radek Krejcifd0bcf02016-09-09 13:28:34 +02001188 * @brief Free (and unlink) the specified data tree and all its siblings (preceding as well as following).
Radek Krejci81468402016-01-07 13:52:40 +01001189 *
Michal Vasko299f9832017-01-06 13:29:22 +01001190 * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators).
1191 *
Radek Krejci81468402016-01-07 13:52:40 +01001192 * @param[in] node One of the siblings root element of the (sub)trees to be freed.
1193 */
1194void lyd_free_withsiblings(struct lyd_node *node);
1195
1196/**
Radek Krejci134610e2015-10-20 17:15:34 +02001197 * @brief Insert attribute into the data node.
1198 *
1199 * @param[in] parent Data node where to place the attribute
Radek Krejci70ecd722016-03-21 09:04:00 +01001200 * @param[in] mod An alternative way to specify attribute's module (namespace) used in case the \p name does
1201 * not include prefix. If neither prefix in the \p name nor mod is specified, the attribute's
1202 * module is inherited from the \p parent node. It is not allowed to have attributes with no
1203 * module (namespace).
1204 * @param[in] name Attribute name. The string can include the attribute's module (namespace) as the name's
1205 * prefix (prefix:name). Prefix must be the name of one of the schema in the \p parent's context.
1206 * If the prefix is not specified, the \p mod parameter is used. If neither of these parameters is
1207 * usable, attribute inherits module (namespace) from the \p parent node. It is not allowed to
1208 * have attributes with no module (namespace).
Radek Krejci134610e2015-10-20 17:15:34 +02001209 * @param[in] value Attribute value
1210 * @return pointer to the created attribute (which is already connected in \p parent) or NULL on error.
1211 */
Radek Krejci70ecd722016-03-21 09:04:00 +01001212struct lyd_attr *lyd_insert_attr(struct lyd_node *parent, const struct lys_module *mod, const char *name,
1213 const char *value);
Radek Krejci134610e2015-10-20 17:15:34 +02001214
1215/**
Radek Krejci88f29302015-10-30 15:42:33 +01001216 * @brief Destroy data attribute
1217 *
1218 * If the attribute to destroy is a member of a node attribute list, it is necessary to
1219 * provide the node itself as \p parent to keep the list consistent.
1220 *
1221 * @param[in] ctx Context where the attribute was created (usually it is the context of the \p parent)
1222 * @param[in] parent Parent node where the attribute is placed
1223 * @param[in] attr Attribute to destroy
1224 * @param[in] recursive Zero to destroy only the attribute, non-zero to destroy also all the subsequent attributes
1225 * in the list.
1226 */
1227void lyd_free_attr(struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_attr *attr, int recursive);
1228
1229/**
Radek Krejci6910a032016-04-13 10:06:21 +02001230 * @brief Return main module of the data tree node.
1231 *
1232 * In case of regular YANG module, it returns ::lys_node#module pointer,
1233 * but in case of submodule, it returns pointer to the main module.
1234 *
1235 * @param[in] node Data tree node to be examined
1236 * @return pointer to the main module (schema structure), NULL in case of error.
1237 */
1238struct lys_module *lyd_node_module(const struct lyd_node *node);
1239
1240/**
Michal Vasko0a8ab412017-01-09 11:10:08 +01001241 * @brief Get the type structure of a leaf.
1242 *
1243 * In case of a union, the correct specific type is found.
1244 * 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 +01001245 *
1246 * @param[in] leaf Leaf to examine.
Michal Vasko0a8ab412017-01-09 11:10:08 +01001247 * @return Found type, NULL on error.
Michal Vaskoe3886bb2017-01-02 11:33:28 +01001248 */
1249const struct lys_type *lyd_leaf_type(const struct lyd_node_leaf_list *leaf);
1250
1251/**
Radek Krejcidef50022016-02-01 16:38:32 +01001252* @brief Print data tree in the specified format.
1253*
Radek Krejcidef50022016-02-01 16:38:32 +01001254* @param[out] strp Pointer to store the resulting dump.
1255* @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
1256* node of the data tree to print the specific subtree.
1257* @param[in] format Data output format.
Michal Vasko228431e2018-07-10 15:47:11 +02001258* @param[in] options [printer flags](@ref printerflags). \p format LYD_LYB accepts only #LYP_WITHSIBLINGS option.
Radek Krejcidef50022016-02-01 16:38:32 +01001259* @return 0 on success, 1 on failure (#ly_errno is set).
1260*/
1261int lyd_print_mem(char **strp, const struct lyd_node *root, LYD_FORMAT format, int options);
Michal Vasko2d162e12015-09-24 14:33:29 +02001262
1263/**
Radek Krejcidef50022016-02-01 16:38:32 +01001264 * @brief Print data tree in the specified format.
Michal Vasko2d162e12015-09-24 14:33:29 +02001265 *
Radek Krejcidef50022016-02-01 16:38:32 +01001266 * @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
1267 * node of the data tree to print the specific subtree.
1268 * @param[in] fd File descriptor where to print the data.
1269 * @param[in] format Data output format.
Michal Vasko228431e2018-07-10 15:47:11 +02001270 * @param[in] options [printer flags](@ref printerflags). \p format LYD_LYB accepts only #LYP_WITHSIBLINGS option.
Radek Krejcidef50022016-02-01 16:38:32 +01001271 * @return 0 on success, 1 on failure (#ly_errno is set).
Michal Vasko2d162e12015-09-24 14:33:29 +02001272 */
Radek Krejcidef50022016-02-01 16:38:32 +01001273int lyd_print_fd(int fd, const struct lyd_node *root, LYD_FORMAT format, int options);
1274
1275/**
1276 * @brief Print data tree in the specified format.
1277 *
Radek Krejcidef50022016-02-01 16:38:32 +01001278 * @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
1279 * node of the data tree to print the specific subtree.
1280 * @param[in] f File stream where to print the data.
1281 * @param[in] format Data output format.
Michal Vasko228431e2018-07-10 15:47:11 +02001282 * @param[in] options [printer flags](@ref printerflags). \p format LYD_LYB accepts only #LYP_WITHSIBLINGS option.
Radek Krejcidef50022016-02-01 16:38:32 +01001283 * @return 0 on success, 1 on failure (#ly_errno is set).
1284 */
1285int lyd_print_file(FILE *f, const struct lyd_node *root, LYD_FORMAT format, int options);
1286
1287/**
1288 * @brief Print data tree in the specified format.
1289 *
Radek Krejcidef50022016-02-01 16:38:32 +01001290 * @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
1291 * node of the data tree to print the specific subtree.
1292 * @param[in] writeclb Callback function to write the data (see write(1)).
1293 * @param[in] arg Optional caller-specific argument to be passed to the \p writeclb callback.
1294 * @param[in] format Data output format.
Michal Vasko228431e2018-07-10 15:47:11 +02001295 * @param[in] options [printer flags](@ref printerflags). \p format LYD_LYB accepts only #LYP_WITHSIBLINGS option.
Radek Krejcidef50022016-02-01 16:38:32 +01001296 * @return 0 on success, 1 on failure (#ly_errno is set).
1297 */
1298int lyd_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg,
1299 const struct lyd_node *root, LYD_FORMAT format, int options);
Michal Vasko2d162e12015-09-24 14:33:29 +02001300
Michal Vasko4d1f0482016-09-19 14:35:06 +02001301/**
1302 * @brief Get the double value of a decimal64 leaf/leaf-list.
1303 *
1304 * YANG decimal64 type enables higher precision numbers than IEEE 754 double-precision
1305 * format, so this conversion does not have to be lossless.
1306 *
1307 * @param[in] node Leaf/leaf-list of type decimal64.
1308 * @return Closest double equivalent to the decimal64 value.
1309 */
1310double lyd_dec64_to_double(const struct lyd_node *node);
1311
Michal Vasko196c6102018-08-08 16:27:42 +02001312/**
1313 * @brief Get the length of a printed LYB data tree.
1314 *
1315 * @param[in] data LYB data.
1316 * @return \p data length or -1 on error.
1317 */
1318int lyd_lyb_data_length(const char *data);
1319
Michal Vaskod025ee32018-06-28 10:04:19 +02001320#ifdef LY_ENABLED_LYD_PRIV
1321
1322/**
1323 * @brief Set a schema private pointer to a user pointer.
1324 *
1325 * @param[in] node Data node, whose private field will be assigned.
1326 * @param[in] priv Arbitrary user-specified pointer.
1327 * @return Previous private object of the \p node (NULL if this is the first call on the \p node). Note, that
1328 * the caller is in this case responsible (if it is necessary) for freeing the replaced private object. In case
1329 * of invalid (NULL) \p node, NULL is returned and #ly_errno is set to #LY_EINVAL.
1330 */
1331void *lyd_set_private(const struct lyd_node *node, void *priv);
1332
1333#endif
1334
Michal Vasko2d162e12015-09-24 14:33:29 +02001335/**@} */
1336
1337#ifdef __cplusplus
1338}
1339#endif
1340
1341#endif /* LY_TREE_DATA_H_ */