blob: d3a870058878be09661a2d30ab44800edd80fab9 [file] [log] [blame]
Michal Vasko2d162e12015-09-24 14:33:29 +02001/**
Radek Krejciaa429e42015-10-09 15:52:37 +02002 * @file tree_data.h
Michal Vasko2d162e12015-09-24 14:33:29 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Radek Krejciaa429e42015-10-09 15:52:37 +02004 * @brief libyang representation of data trees.
Michal Vasko2d162e12015-09-24 14:33:29 +02005 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of the Company nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 */
21
22#ifndef LY_TREE_DATA_H_
23#define LY_TREE_DATA_H_
24
25#include <stddef.h>
26#include <stdint.h>
27
Mislav Novakovice251a652015-09-29 08:40:12 +020028#include "tree_schema.h"
29
Michal Vasko2d162e12015-09-24 14:33:29 +020030#ifdef __cplusplus
31extern "C" {
32#endif
33
34/**
35 * @addtogroup datatree
36 * @{
37 */
38
39/**
40 * @brief Data input/output formats supported by libyang [parser](@ref parsers) and [printer](@ref printers) functions.
41 */
42typedef enum {
43 LYD_UNKNOWN, /**< unknown format, used as return value in case of error */
44 LYD_XML, /**< XML format of the instance data */
Radek Krejci452fb952015-10-02 16:07:46 +020045 LYD_XML_FORMAT, /**< For input data, it is interchangeable with #LYD_XML, for output it formats XML with indentantion */
Michal Vasko2d162e12015-09-24 14:33:29 +020046 LYD_JSON, /**< JSON format of the instance data */
47} LYD_FORMAT;
48
49/**
Michal Vasko2d162e12015-09-24 14:33:29 +020050 * @brief Attribute structure.
51 *
Radek Krejci5f9e8c92015-10-30 10:01:06 +010052 * The structure provides information about attributes of a data element. Such attributes partially
53 * maps to annotations from draft-ietf-netmod-yang-metadata. In XML, they are represented as standard
54 * XML attrbutes. In JSON, they are represented as JSON elements starting with the '@' character
55 * (for more information, see the yang metadata draft.
56 *
Michal Vasko2d162e12015-09-24 14:33:29 +020057 */
58struct lyd_attr {
Radek Krejci5f9e8c92015-10-30 10:01:06 +010059 struct lyd_attr *next; /**< pointer to the next attribute of the same element */
60 struct lys_module *module; /**< pointer to the attribute's module.
61 TODO when annotations will be supported, point to the annotation definition
62 and validate that the attribute is really defined there. Currently, we just
63 believe that it is defined in the module it says */
Michal Vasko2d162e12015-09-24 14:33:29 +020064 const char *name; /**< attribute name */
65 const char *value; /**< attribute value */
66};
67
68/**
69 * @brief node's value representation
70 */
71typedef union lyd_value_u {
72 const char *binary; /**< base64 encoded, NULL terminated string */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +020073 struct lys_type_bit **bit; /**< bitmap of pointers to the schema definition of the bit value that are set,
74 its size is always the number of defined bits in the schema */
Radek Krejci489773c2015-12-17 13:20:03 +010075 int8_t bln; /**< 0 as false, 1 as true */
Michal Vasko2d162e12015-09-24 14:33:29 +020076 int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */
77 struct lys_type_enum *enm; /**< pointer to the schema definition of the enumeration value */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +020078 struct lys_ident *ident; /**< pointer to the schema definition of the identityref value */
Michal Vasko2d162e12015-09-24 14:33:29 +020079 struct lyd_node *instance; /**< instance-identifier, pointer to the referenced data tree node */
80 int8_t int8; /**< 8-bit signed integer */
81 int16_t int16; /**< 16-bit signed integer */
82 int32_t int32; /**< 32-bit signed integer */
83 int64_t int64; /**< 64-bit signed integer */
84 struct lyd_node *leafref; /**< pointer to the referenced leaf/leaflist instance in data tree */
85 const char *string; /**< string */
86 uint8_t uint8; /**< 8-bit unsigned integer */
87 uint16_t uint16; /**< 16-bit signed integer */
88 uint32_t uint32; /**< 32-bit signed integer */
89 uint64_t uint64; /**< 64-bit signed integer */
90} lyd_val;
91
92/**
93 * @brief Generic structure for a data node, directly applicable to the data nodes defined as #LYS_CONTAINER, #LYS_LIST
94 * and #LYS_CHOICE.
95 *
96 * Completely fits to containers and choices and is compatible (can be used interchangeably except the #child member)
97 * with all other lyd_node_* structures. All data nodes are provides as ::lyd_node structure by default.
98 * According to the schema's ::lys_node#nodetype member, the specific object is supposed to be cast to
Radek Krejciee360892015-10-06 11:23:14 +020099 * ::lyd_node_leaf_list or ::lyd_node_anyxml structures. This structure fits only to
Michal Vasko2d162e12015-09-24 14:33:29 +0200100 * #LYS_CONTAINER and #LYS_CHOICE values.
101 *
102 * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro.
103 */
104struct lyd_node {
105 struct lys_node *schema; /**< pointer to the schema definition of this node */
106
107 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
108 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
109 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
110 never NULL. If there is no sibling node, pointer points to the node
111 itself. In case of the first node, this pointer points to the last
112 node in the list. */
113 struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
114 struct lyd_node *child; /**< pointer to the first child node \note Since other lyd_node_*
Radek Krejciee360892015-10-06 11:23:14 +0200115 structures represent end nodes, this member
Michal Vasko2d162e12015-09-24 14:33:29 +0200116 is replaced in those structures. Therefore, be careful with accessing
117 this member without having information about the node type from the schema's
118 ::lys_node#nodetype member. */
119};
120
121/**
Michal Vasko4c183312015-09-25 10:41:47 +0200122 * @brief Structure for data nodes defined as #LYS_LEAF or #LYS_LEAFLIST.
Michal Vasko2d162e12015-09-24 14:33:29 +0200123 *
Michal Vasko4c183312015-09-25 10:41:47 +0200124 * Extension for ::lyd_node structure. It replaces the ::lyd_node#child member by
125 * three new members (#value, #value_str and #value_type) to provide
126 * information about the value. The first five members (#schema, #attr, #next,
Michal Vasko2d162e12015-09-24 14:33:29 +0200127 * #prev and #parent) are compatible with the ::lyd_node's members.
128 *
129 * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro.
130 */
Michal Vasko4c183312015-09-25 10:41:47 +0200131struct lyd_node_leaf_list {
Michal Vasko2d162e12015-09-24 14:33:29 +0200132 struct lys_node *schema; /**< pointer to the schema definition of this node which is ::lys_node_leaflist
133 structure */
134
135 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
136 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
137 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
138 never NULL. If there is no sibling node, pointer points to the node
139 itself. In case of the first node, this pointer points to the last
140 node in the list. */
141 struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
142
143 /* struct lyd_node *child; should be here, but is not */
144
145 /* leaflist's specific members */
Michal Vasko2d162e12015-09-24 14:33:29 +0200146 const char *value_str; /**< string representation of value (for comparison, printing,...) */
Radek Krejci23238922015-10-27 17:13:34 +0100147 lyd_val value; /**< node's value representation */
Michal Vasko2d162e12015-09-24 14:33:29 +0200148 LY_DATA_TYPE value_type; /**< type of the value in the node, mainly for union to avoid repeating of type detection */
149};
150
151/**
152 * @brief Structure for data nodes defined as #LYS_ANYXML.
153 *
154 * Extension for ::lyd_node structure - replaces the ::lyd_node#child member by new #value member. The first five
155 * members (#schema, #attr, #next, #prev and #parent) are compatible with the ::lyd_node's members.
156 *
157 * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro.
158 */
159struct lyd_node_anyxml {
160 struct lys_node *schema; /**< pointer to the schema definition of this node which is ::lys_node_anyxml
161 structure */
162
163 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
164 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
165 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
166 never NULL. If there is no sibling node, pointer points to the node
167 itself. In case of the first node, this pointer points to the last
168 node in the list. */
169 struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
170
171 /* struct lyd_node *child; should be here, but is not */
172
173 /* anyxml's specific members */
174 struct lyxml_elem *value; /**< anyxml name is the root element of value! */
175};
176
177/**
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200178 * @brief Create a new container node in a data tree.
179 *
180 * @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 +0200181 * @param[in] module Module with the node being created.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200182 * @param[in] name Schema node name of the new data node. The node can be #LYS_CONTAINER, #LYS_LIST,
Michal Vaskoa45cf2b2015-10-23 09:45:36 +0200183 * #LYS_NOTIF, or #LYS_RPC.
Michal Vasko1dca6882015-10-22 14:29:42 +0200184 * @return New node, NULL on error.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200185 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100186struct lyd_node *lyd_new(struct lyd_node *parent, const struct lys_module *module, const char *name);
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200187
188/**
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200189 * @brief Create a new leaf or leaflist node in a data tree with a string value that is converted to
190 * the actual value.
191 *
192 * @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 +0200193 * @param[in] module Module with the node being created.
194 * @param[in] name Schema node name of the new data node.
Michal Vasko3e671b52015-10-23 16:23:15 +0200195 * @param[in] val_str String form of the value of the node being created. In case the type is #LY_TYPE_INST
196 * 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 +0200197 * @return New node, NULL on error.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200198 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100199struct lyd_node *lyd_new_leaf(struct lyd_node *parent, const struct lys_module *module, const char *name,
Michal Vasko3e671b52015-10-23 16:23:15 +0200200 const char *val_str);
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200201
202/**
203 * @brief Create a new anyxml node in a data tree.
Michal Vasko2d162e12015-09-24 14:33:29 +0200204 *
Michal Vasko2d162e12015-09-24 14:33:29 +0200205 * @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 +0200206 * @param[in] module Module with the node being created.
207 * @param[in] name Schema node name of the new data node.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200208 * @param[in] val_xml Value of the node being created. Must be a well-formed XML.
Michal Vasko1dca6882015-10-22 14:29:42 +0200209 * @return New node, NULL on error.
Michal Vasko2d162e12015-09-24 14:33:29 +0200210 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100211struct lyd_node *lyd_new_anyxml(struct lyd_node *parent, const struct lys_module *module, const char *name,
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200212 const char *val_xml);
Michal Vasko2d162e12015-09-24 14:33:29 +0200213
214/**
Michal Vasko50c0a872016-01-13 14:34:11 +0100215 * @brief Create a new container node in a data tree, whose schema parent is #LYS_OUTPUT.
Michal Vasko0df122f2015-12-14 13:38:21 +0100216 *
Michal Vasko50c0a872016-01-13 14:34:11 +0100217 * @param[in] schema Schema node of the container.
Michal Vasko0df122f2015-12-14 13:38:21 +0100218 * @return New node, NULL on error.
219 */
Michal Vasko50c0a872016-01-13 14:34:11 +0100220struct lyd_node *lyd_output_new(const struct lys_node *schema);
221
222/**
223 * @brief Create a new leaf or leaflist node in a data tree, whose schema parent is #LYS_OUTPUT.
224 *
225 * @param[in] schema Schema node of the leaf.
226 * @param[in] val_str String form of the value of the node being created. In case the type is #LY_TYPE_INST
227 * or #LY_TYPE_IDENT, JSON node-id format is expected (nodes are prefixed with module names, not XML namespaces).
228 * @return New node, NULL on error.
229 */
230struct lyd_node *lyd_output_new_leaf(const struct lys_node *schema, const char *val_str);
231
232/**
233 * @brief Create a new anyxml node in a data tree, whose schema parent is #LYS_OUTPUT.
234 *
235 * @param[in] schema Schema node of the leaf.
236 * @param[in] val_xml Value of the node being created. Must be a well-formed XML.
237 * @return New node, NULL on error.
238 */
239struct lyd_node *lyd_output_new_anyxml(const struct lys_node *schema, const char *val_xml);
Michal Vasko0df122f2015-12-14 13:38:21 +0100240
241/**
Michal Vaskoc0797f82015-10-14 15:51:25 +0200242 * @brief Create a copy of the specified data tree \p node. Namespaces are copied as needed,
243 * schema references are kept the same.
Michal Vasko2d162e12015-09-24 14:33:29 +0200244 *
245 * @param[in] node Data tree node to be duplicated.
246 * @param[in] recursive 1 if all children are supposed to be also duplicated.
247 * @return Created copy of the provided data \p node.
248 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100249struct lyd_node *lyd_dup(const struct lyd_node *node, int recursive);
Michal Vasko2d162e12015-09-24 14:33:29 +0200250
251/**
252 * @brief Insert the \p node element as child to the \p parent element. The \p node is inserted as a last child of the
253 * \p parent.
254 *
255 * If the node is part of some other tree, it is automatically unlinked.
256 * If the node is the first node of a node list (with no parent), all
257 * the subsequent nodes are also inserted.
258 *
259 * @param[in] parent Parent node for the \p node being inserted.
260 * @param[in] node The node being inserted.
Michal Vasko24337392015-10-16 09:58:16 +0200261 * @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 +0200262 * in the data tree.
263 */
Michal Vasko24337392015-10-16 09:58:16 +0200264int lyd_insert(struct lyd_node *parent, struct lyd_node *node);
Michal Vasko2d162e12015-09-24 14:33:29 +0200265
266/**
Michal Vasko3f7dba12015-10-15 13:09:27 +0200267 * @brief Insert the \p node element after the \p sibling element. If \p node and \p siblings are already
268 * siblings (just moving \p node position), skip validation (\p options are ignored).
Michal Vasko2d162e12015-09-24 14:33:29 +0200269 *
Michal Vasko2d162e12015-09-24 14:33:29 +0200270 * @param[in] sibling The data tree node before which the \p node will be inserted.
271 * @param[in] node The data tree node to be inserted.
Michal Vasko24337392015-10-16 09:58:16 +0200272 * @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 +0200273 * in the data tree.
274 */
Michal Vasko24337392015-10-16 09:58:16 +0200275int lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node);
Michal Vasko2d162e12015-09-24 14:33:29 +0200276
277/**
278 * @brief Insert the \p node element after the \p sibling element.
279 *
Michal Vasko3f7dba12015-10-15 13:09:27 +0200280 * @param[in] sibling The data tree node before which the \p node will be inserted. If \p node and \p siblings
281 * are already siblings (just moving \p node position), skip validation (\p options are ignored).
Michal Vasko2d162e12015-09-24 14:33:29 +0200282 * @param[in] node The data tree node to be inserted.
Michal Vasko24337392015-10-16 09:58:16 +0200283 * @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 +0200284 * in the data tree.
285 */
Michal Vasko24337392015-10-16 09:58:16 +0200286int lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node);
287
288/**
289 * @brief Validate \p node data subtree.
290 *
291 * @param[in] node Data subtree to be validated.
292 * @param[in] options Options for the inserting data to the target data tree options, see @ref parseroptions.
293 * @return 0 on success (if options include #LYD_OPT_FILTER, some nodes could still have been deleted as an optimization),
294 * nonzero in case of an error.
295 */
296int lyd_validate(struct lyd_node *node, int options);
Michal Vasko2d162e12015-09-24 14:33:29 +0200297
298/**
Michal Vasko55f60be2015-10-14 13:12:58 +0200299 * @brief Unlink the specified data subtree. All referenced namespaces are copied.
Michal Vasko2d162e12015-09-24 14:33:29 +0200300 *
301 * Note, that the node's connection with the schema tree is kept. Therefore, in case of
302 * reconnecting the node to a data tree using lyd_paste() it is necessary to paste it
303 * to the appropriate place in the data tree following the schema.
304 *
305 * @param[in] node Data tree node to be unlinked (together with all children).
306 * @return 0 for success, nonzero for error
307 */
308int lyd_unlink(struct lyd_node *node);
309
310/**
311 * @brief Free (and unlink) the specified data (sub)tree.
312 *
313 * @param[in] node Root of the (sub)tree to be freed.
314 */
315void lyd_free(struct lyd_node *node);
316
317/**
Radek Krejci81468402016-01-07 13:52:40 +0100318 * @brief Free (and unlink) the specified data (sub)tree and all its siblings (preceding as well as following).
319 *
320 * @param[in] node One of the siblings root element of the (sub)trees to be freed.
321 */
322void lyd_free_withsiblings(struct lyd_node *node);
323
324/**
Radek Krejci134610e2015-10-20 17:15:34 +0200325 * @brief Insert attribute into the data node.
326 *
327 * @param[in] parent Data node where to place the attribute
Radek Krejci5f9e8c92015-10-30 10:01:06 +0100328 * @param[in] name Attribute name including the prefix (prefix:name). Prefix must be the name of one of the
329 * schema in the \p parent's context.
Radek Krejci134610e2015-10-20 17:15:34 +0200330 * @param[in] value Attribute value
331 * @return pointer to the created attribute (which is already connected in \p parent) or NULL on error.
332 */
333struct lyd_attr *lyd_insert_attr(struct lyd_node *parent, const char *name, const char *value);
334
335/**
Radek Krejci88f29302015-10-30 15:42:33 +0100336 * @brief Destroy data attribute
337 *
338 * If the attribute to destroy is a member of a node attribute list, it is necessary to
339 * provide the node itself as \p parent to keep the list consistent.
340 *
341 * @param[in] ctx Context where the attribute was created (usually it is the context of the \p parent)
342 * @param[in] parent Parent node where the attribute is placed
343 * @param[in] attr Attribute to destroy
344 * @param[in] recursive Zero to destroy only the attribute, non-zero to destroy also all the subsequent attributes
345 * in the list.
346 */
347void lyd_free_attr(struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_attr *attr, int recursive);
348
349/**
Michal Vasko2d162e12015-09-24 14:33:29 +0200350 * @brief Opaque internal structure, do not access it from outside.
351 */
352struct lyxml_elem;
353
354/**
355 * @brief Serialize anyxml content for further processing.
356 *
Michal Vasko2d162e12015-09-24 14:33:29 +0200357 * @param[in] anyxml Anyxml content from ::lyd_node_anyxml#value to serialize ax XML string
Michal Vaskoff4c2832015-10-15 13:30:50 +0200358 * @return Serialized content of the anyxml or NULL in case of error. Need to be freed after
359 * done using.
Michal Vasko2d162e12015-09-24 14:33:29 +0200360 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100361char *lyxml_serialize(const struct lyxml_elem *anyxml);
Michal Vasko2d162e12015-09-24 14:33:29 +0200362
363/**
364 * @brief Structure to hold a set of (not necessary somehow connected) ::lyd_node objects.
365 *
366 * To free the structure, use lyd_set_free() function, to manipulate with the structure, use other
367 * lyd_set_* functions.
368 */
369struct lyd_set {
370 unsigned int size; /**< allocated size of the set array */
371 unsigned int number; /**< number of elements in (used size of) the set array */
372 struct lyd_node **set; /**< array of pointers to a ::lyd_node objects */
373};
374
375/**
376 * @brief Create and initiate new ::lyd_set structure.
377 *
378 * @return Created ::lyd_set structure or NULL in case of error.
379 */
380struct lyd_set *lyd_set_new(void);
381
382/**
383 * @brief Add a ::lyd_node object into the set
384 *
385 * @param[in] set Set where the \p node will be added.
386 * @param[in] node The ::lyd_node object to be added into the \p set;
387 * @return 0 on success
388 */
389int lyd_set_add(struct lyd_set *set, struct lyd_node *node);
390
391/**
392 * @brief Free the ::lyd_set data. Frees only the set structure content, not the referred data.
393 *
394 * @param[in] set The set to be freed.
395 */
396void lyd_set_free(struct lyd_set *set);
397
398/**@} */
399
400#ifdef __cplusplus
401}
402#endif
403
404#endif /* LY_TREE_DATA_H_ */