blob: 768fdce4ea930652d7da7dcf67ba9653431f6c4f [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/**
50 * @brief Data attribute's type to distinguish between a standard (XML) attribute and namespace definition
51 */
52typedef enum lyd_attr_type {
53 LYD_ATTR_STD = 1, /**< standard attribute, see ::lyd_attr structure */
54 LYD_ATTR_NS = 2 /**< namespace definition, see ::lyd_ns structure */
55} LYD_ATTR_TYPE;
56
57/**
58 * @brief Namespace definition structure.
59 *
60 * Usually, the object is provided as ::lyd_attr structure. The structure is compatible with
61 * ::lyd_attr within the first two members (#type and #next) to allow passing through and type
62 * detection interchangeably. When the type member is set to #LYD_ATTR_NS, the ::lyd_attr
63 * structure should be cast to ::lyd_ns to access the rest of members.
64 */
65struct lyd_ns {
66 LYD_ATTR_TYPE type; /**< always #LYD_ATTR_NS, compatible with ::lyd_attr */
67 struct lyd_attr *next; /**< pointer to the next attribute or namespace definition of an element,
68 compatible with ::lyd_attr */
69 struct lyd_node *parent; /**< pointer to the element where the namespace definition is placed */
70 const char *prefix; /**< namespace prefix value */
71 const char *value; /**< namespace value */
72};
73
74/**
75 * @brief Attribute structure.
76 *
77 * The structure provides information about attributes of a data element and covers not only
78 * attributes but also namespace definitions. Therefore, the first two members (#type and #next)
79 * can be safely accessed to pass through the attributes list and type detection. When the #type
80 * member has #LYD_ATTR_STD value, the rest of the members can be used. Otherwise, the object
81 * should be cast to the appropriate structure according to #LYD_ATTR_TYPE enumeration.
82 */
83struct lyd_attr {
84 LYD_ATTR_TYPE type; /**< type of the attribute, to access the last three members, the value
85 must be ::LYD_ATTR_STD */
86 struct lyd_attr *next; /**< pointer to the next attribute or namespace definition of an element */
87 struct lyd_ns *ns; /**< pointer to the definition of the namespace of the attribute */
88 const char *name; /**< attribute name */
89 const char *value; /**< attribute value */
90};
91
92/**
93 * @brief node's value representation
94 */
95typedef union lyd_value_u {
96 const char *binary; /**< base64 encoded, NULL terminated string */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +020097 struct lys_type_bit **bit; /**< bitmap of pointers to the schema definition of the bit value that are set,
98 its size is always the number of defined bits in the schema */
Michal Vasko2d162e12015-09-24 14:33:29 +020099 int8_t bool; /**< 0 as false, 1 as true */
100 int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */
101 struct lys_type_enum *enm; /**< pointer to the schema definition of the enumeration value */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200102 struct lys_ident *ident; /**< pointer to the schema definition of the identityref value */
Michal Vasko2d162e12015-09-24 14:33:29 +0200103 struct lyd_node *instance; /**< instance-identifier, pointer to the referenced data tree node */
104 int8_t int8; /**< 8-bit signed integer */
105 int16_t int16; /**< 16-bit signed integer */
106 int32_t int32; /**< 32-bit signed integer */
107 int64_t int64; /**< 64-bit signed integer */
108 struct lyd_node *leafref; /**< pointer to the referenced leaf/leaflist instance in data tree */
109 const char *string; /**< string */
110 uint8_t uint8; /**< 8-bit unsigned integer */
111 uint16_t uint16; /**< 16-bit signed integer */
112 uint32_t uint32; /**< 32-bit signed integer */
113 uint64_t uint64; /**< 64-bit signed integer */
114} lyd_val;
115
116/**
117 * @brief Generic structure for a data node, directly applicable to the data nodes defined as #LYS_CONTAINER, #LYS_LIST
118 * and #LYS_CHOICE.
119 *
120 * Completely fits to containers and choices and is compatible (can be used interchangeably except the #child member)
121 * with all other lyd_node_* structures. All data nodes are provides as ::lyd_node structure by default.
122 * 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 +0200123 * ::lyd_node_leaf_list or ::lyd_node_anyxml structures. This structure fits only to
Michal Vasko2d162e12015-09-24 14:33:29 +0200124 * #LYS_CONTAINER and #LYS_CHOICE values.
125 *
126 * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro.
127 */
128struct lyd_node {
129 struct lys_node *schema; /**< pointer to the schema definition of this node */
130
131 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
132 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
133 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
134 never NULL. If there is no sibling node, pointer points to the node
135 itself. In case of the first node, this pointer points to the last
136 node in the list. */
137 struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
138 struct lyd_node *child; /**< pointer to the first child node \note Since other lyd_node_*
Radek Krejciee360892015-10-06 11:23:14 +0200139 structures represent end nodes, this member
Michal Vasko2d162e12015-09-24 14:33:29 +0200140 is replaced in those structures. Therefore, be careful with accessing
141 this member without having information about the node type from the schema's
142 ::lys_node#nodetype member. */
143};
144
145/**
Michal Vasko4c183312015-09-25 10:41:47 +0200146 * @brief Structure for data nodes defined as #LYS_LEAF or #LYS_LEAFLIST.
Michal Vasko2d162e12015-09-24 14:33:29 +0200147 *
Michal Vasko4c183312015-09-25 10:41:47 +0200148 * Extension for ::lyd_node structure. It replaces the ::lyd_node#child member by
149 * three new members (#value, #value_str and #value_type) to provide
150 * information about the value. The first five members (#schema, #attr, #next,
Michal Vasko2d162e12015-09-24 14:33:29 +0200151 * #prev and #parent) are compatible with the ::lyd_node's members.
152 *
153 * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro.
154 */
Michal Vasko4c183312015-09-25 10:41:47 +0200155struct lyd_node_leaf_list {
Michal Vasko2d162e12015-09-24 14:33:29 +0200156 struct lys_node *schema; /**< pointer to the schema definition of this node which is ::lys_node_leaflist
157 structure */
158
159 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
160 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
161 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
162 never NULL. If there is no sibling node, pointer points to the node
163 itself. In case of the first node, this pointer points to the last
164 node in the list. */
165 struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
166
167 /* struct lyd_node *child; should be here, but is not */
168
169 /* leaflist's specific members */
170 lyd_val value; /**< node's value representation */
171 const char *value_str; /**< string representation of value (for comparison, printing,...) */
172 LY_DATA_TYPE value_type; /**< type of the value in the node, mainly for union to avoid repeating of type detection */
173};
174
175/**
176 * @brief Structure for data nodes defined as #LYS_ANYXML.
177 *
178 * Extension for ::lyd_node structure - replaces the ::lyd_node#child member by new #value member. The first five
179 * members (#schema, #attr, #next, #prev and #parent) are compatible with the ::lyd_node's members.
180 *
181 * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro.
182 */
183struct lyd_node_anyxml {
184 struct lys_node *schema; /**< pointer to the schema definition of this node which is ::lys_node_anyxml
185 structure */
186
187 struct lyd_attr *attr; /**< pointer to the list of attributes of this node */
188 struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */
189 struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is
190 never NULL. If there is no sibling node, pointer points to the node
191 itself. In case of the first node, this pointer points to the last
192 node in the list. */
193 struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */
194
195 /* struct lyd_node *child; should be here, but is not */
196
197 /* anyxml's specific members */
198 struct lyxml_elem *value; /**< anyxml name is the root element of value! */
199};
200
201/**
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200202 * @brief Create a new container node in a data tree.
203 *
204 * @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 +0200205 * @param[in] module Module with the node being created.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200206 * @param[in] name Schema node name of the new data node. The node can be #LYS_CONTAINER, #LYS_LIST,
207 * #LYS_INPUT, #LYS_OUTPUT, #LYS_NOTIF, or #LYS_RPC.
208 */
209struct lyd_node *lyd_new(struct lyd_node *parent, struct lys_module *module, const char *name);
210
211/**
212 * @brief Create a new leaf or leaflist node in a data tree with a specific value.
213 *
214 * @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 +0200215 * @param[in] module Module with the node being created.
216 * @param[in] name Schema node name of the new data node.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200217 * @param[in] type Type of the value provided in the \p value parameter. Cannot be #LY_TYPE_DER, #LY_TYPE_UNION,
218 * or #LY_TYPE_INST.
Michal Vasko81eef2c2015-10-02 15:37:25 +0200219 * @param[in] value Value of the node being created. Can be NULL only if \p type is #LY_TYPE_EMPTY.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200220 */
221struct lyd_node *lyd_new_leaf_val(struct lyd_node *parent, struct lys_module *module, const char *name,
Michal Vasko0e3c8722015-09-29 16:06:08 +0200222 LY_DATA_TYPE type, lyd_val value);
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200223
224/**
225 * @brief Create a new leaf or leaflist node in a data tree with a string value that is converted to
226 * the actual value.
227 *
228 * @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 +0200229 * @param[in] module Module with the node being created.
230 * @param[in] name Schema node name of the new data node.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200231 * @param[in] type Interpretation of the string provided in the \p val_str parameter. After appropriate
232 * conversion this will be the resulting type of the value in the node. Cannot be #LY_TYPE_DER or #LY_TYPE_UNION.
233 * @param[in] val_str String form of the value of the node being created. Can be NULL only if \p type is
234 * #LY_TYPE_EMPTY.
235 */
236struct lyd_node *lyd_new_leaf_str(struct lyd_node *parent, struct lys_module *module, const char *name,
237 LY_DATA_TYPE type, const char *val_str);
238
239/**
240 * @brief Create a new anyxml node in a data tree.
Michal Vasko2d162e12015-09-24 14:33:29 +0200241 *
Michal Vasko2d162e12015-09-24 14:33:29 +0200242 * @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 +0200243 * @param[in] module Module with the node being created.
244 * @param[in] name Schema node name of the new data node.
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200245 * @param[in] val_xml Value of the node being created. Must be a well-formed XML.
Michal Vasko2d162e12015-09-24 14:33:29 +0200246 */
Michal Vasko8ea2b7f2015-09-29 14:30:53 +0200247struct lyd_node *lyd_new_anyxml(struct lyd_node *parent, struct lys_module *module, const char *name,
248 const char *val_xml);
Michal Vasko2d162e12015-09-24 14:33:29 +0200249
250/**
Michal Vaskoc0797f82015-10-14 15:51:25 +0200251 * @brief Create a copy of the specified data tree \p node. Namespaces are copied as needed,
252 * schema references are kept the same.
Michal Vasko2d162e12015-09-24 14:33:29 +0200253 *
254 * @param[in] node Data tree node to be duplicated.
255 * @param[in] recursive 1 if all children are supposed to be also duplicated.
256 * @return Created copy of the provided data \p node.
257 */
258struct lyd_node *lyd_dup(struct lyd_node *node, int recursive);
259
260/**
261 * @brief Insert the \p node element as child to the \p parent element. The \p node is inserted as a last child of the
262 * \p parent.
263 *
264 * If the node is part of some other tree, it is automatically unlinked.
265 * If the node is the first node of a node list (with no parent), all
266 * the subsequent nodes are also inserted.
267 *
268 * @param[in] parent Parent node for the \p node being inserted.
269 * @param[in] node The node being inserted.
270 * @param[in] options Options for the inserting data to the target data tree options, see @ref parseroptions.
271 * @return 0 fo success, nonzero in case of error, e.g. when the node is being inserted to an inappropriate place
272 * in the data tree.
273 */
274int lyd_insert(struct lyd_node *parent, struct lyd_node *node, int options);
275
276/**
Michal Vasko3f7dba12015-10-15 13:09:27 +0200277 * @brief Insert the \p node element after the \p sibling element. If \p node and \p siblings are already
278 * siblings (just moving \p node position), skip validation (\p options are ignored).
Michal Vasko2d162e12015-09-24 14:33:29 +0200279 *
Michal Vasko2d162e12015-09-24 14:33:29 +0200280 * @param[in] sibling The data tree node before which the \p node will be inserted.
281 * @param[in] node The data tree node to be inserted.
Michal Vasko02592902015-10-15 12:14:40 +0200282 * @param[in] options Options for the inserting data to the target data tree options, see @ref parseroptions.
Michal Vasko2d162e12015-09-24 14:33:29 +0200283 * @return 0 fo success, nonzero in case of error, e.g. when the node is being inserted to an inappropriate place
284 * in the data tree.
285 */
Michal Vasko02592902015-10-15 12:14:40 +0200286int lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node, int options);
Michal Vasko2d162e12015-09-24 14:33:29 +0200287
288/**
289 * @brief Insert the \p node element after the \p sibling element.
290 *
Michal Vasko3f7dba12015-10-15 13:09:27 +0200291 * @param[in] sibling The data tree node before which the \p node will be inserted. If \p node and \p siblings
292 * are already siblings (just moving \p node position), skip validation (\p options are ignored).
293 *
Michal Vasko2d162e12015-09-24 14:33:29 +0200294 * @param[in] node The data tree node to be inserted.
295 * @param[in] options Options for the inserting data to the target data tree options, see @ref parseroptions.
296 * @return 0 fo success, nonzero in case of error, e.g. when the node is being inserted to an inappropriate place
297 * in the data tree.
298 */
299int lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node, int options);
300
301/**
Michal Vasko55f60be2015-10-14 13:12:58 +0200302 * @brief Unlink the specified data subtree. All referenced namespaces are copied.
Michal Vasko2d162e12015-09-24 14:33:29 +0200303 *
304 * Note, that the node's connection with the schema tree is kept. Therefore, in case of
305 * reconnecting the node to a data tree using lyd_paste() it is necessary to paste it
306 * to the appropriate place in the data tree following the schema.
307 *
308 * @param[in] node Data tree node to be unlinked (together with all children).
309 * @return 0 for success, nonzero for error
310 */
311int lyd_unlink(struct lyd_node *node);
312
313/**
314 * @brief Free (and unlink) the specified data (sub)tree.
315 *
316 * @param[in] node Root of the (sub)tree to be freed.
317 */
318void lyd_free(struct lyd_node *node);
319
320/**
321 * @brief Opaque internal structure, do not access it from outside.
322 */
323struct lyxml_elem;
324
325/**
326 * @brief Serialize anyxml content for further processing.
327 *
328 * TODO not implemented
329 *
330 * @param[in] anyxml Anyxml content from ::lyd_node_anyxml#value to serialize ax XML string
331 * @return Serialized content of the anyxml or NULL in case of error
332 */
333char *lyxml_serialize(struct lyxml_elem *anyxml);
334
335/**
336 * @brief Structure to hold a set of (not necessary somehow connected) ::lyd_node objects.
337 *
338 * To free the structure, use lyd_set_free() function, to manipulate with the structure, use other
339 * lyd_set_* functions.
340 */
341struct lyd_set {
342 unsigned int size; /**< allocated size of the set array */
343 unsigned int number; /**< number of elements in (used size of) the set array */
344 struct lyd_node **set; /**< array of pointers to a ::lyd_node objects */
345};
346
347/**
348 * @brief Create and initiate new ::lyd_set structure.
349 *
350 * @return Created ::lyd_set structure or NULL in case of error.
351 */
352struct lyd_set *lyd_set_new(void);
353
354/**
355 * @brief Add a ::lyd_node object into the set
356 *
357 * @param[in] set Set where the \p node will be added.
358 * @param[in] node The ::lyd_node object to be added into the \p set;
359 * @return 0 on success
360 */
361int lyd_set_add(struct lyd_set *set, struct lyd_node *node);
362
363/**
364 * @brief Free the ::lyd_set data. Frees only the set structure content, not the referred data.
365 *
366 * @param[in] set The set to be freed.
367 */
368void lyd_set_free(struct lyd_set *set);
369
370/**@} */
371
372#ifdef __cplusplus
373}
374#endif
375
376#endif /* LY_TREE_DATA_H_ */