blob: 1f3a0861e59313ceb1fe15bace694791a1afcd5f [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 Vasko0df122f2015-12-14 13:38:21 +0100215 * @brief Create a new output node in a data tree.
216 *
217 * @param[in] rpc_schema Schema node of the RPC of the output.
218 * @return New node, NULL on error.
219 */
220struct lyd_node *lyd_new_output(const struct lys_node *rpc_schema);
221
222/**
Michal Vaskoc0797f82015-10-14 15:51:25 +0200223 * @brief Create a copy of the specified data tree \p node. Namespaces are copied as needed,
224 * schema references are kept the same.
Michal Vasko2d162e12015-09-24 14:33:29 +0200225 *
226 * @param[in] node Data tree node to be duplicated.
227 * @param[in] recursive 1 if all children are supposed to be also duplicated.
228 * @return Created copy of the provided data \p node.
229 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100230struct lyd_node *lyd_dup(const struct lyd_node *node, int recursive);
Michal Vasko2d162e12015-09-24 14:33:29 +0200231
232/**
233 * @brief Insert the \p node element as child to the \p parent element. The \p node is inserted as a last child of the
234 * \p parent.
235 *
236 * If the node is part of some other tree, it is automatically unlinked.
237 * If the node is the first node of a node list (with no parent), all
238 * the subsequent nodes are also inserted.
239 *
240 * @param[in] parent Parent node for the \p node being inserted.
241 * @param[in] node The node being inserted.
Michal Vasko24337392015-10-16 09:58:16 +0200242 * @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 +0200243 * in the data tree.
244 */
Michal Vasko24337392015-10-16 09:58:16 +0200245int lyd_insert(struct lyd_node *parent, struct lyd_node *node);
Michal Vasko2d162e12015-09-24 14:33:29 +0200246
247/**
Michal Vasko3f7dba12015-10-15 13:09:27 +0200248 * @brief Insert the \p node element after the \p sibling element. If \p node and \p siblings are already
249 * siblings (just moving \p node position), skip validation (\p options are ignored).
Michal Vasko2d162e12015-09-24 14:33:29 +0200250 *
Michal Vasko2d162e12015-09-24 14:33:29 +0200251 * @param[in] sibling The data tree node before which the \p node will be inserted.
252 * @param[in] node The data tree node to be inserted.
Michal Vasko24337392015-10-16 09:58:16 +0200253 * @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 +0200254 * in the data tree.
255 */
Michal Vasko24337392015-10-16 09:58:16 +0200256int lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node);
Michal Vasko2d162e12015-09-24 14:33:29 +0200257
258/**
259 * @brief Insert the \p node element after the \p sibling element.
260 *
Michal Vasko3f7dba12015-10-15 13:09:27 +0200261 * @param[in] sibling The data tree node before which the \p node will be inserted. If \p node and \p siblings
262 * are already siblings (just moving \p node position), skip validation (\p options are ignored).
Michal Vasko2d162e12015-09-24 14:33:29 +0200263 * @param[in] node The data tree node to be inserted.
Michal Vasko24337392015-10-16 09:58:16 +0200264 * @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 +0200265 * in the data tree.
266 */
Michal Vasko24337392015-10-16 09:58:16 +0200267int lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node);
268
269/**
270 * @brief Validate \p node data subtree.
271 *
272 * @param[in] node Data subtree to be validated.
273 * @param[in] options Options for the inserting data to the target data tree options, see @ref parseroptions.
274 * @return 0 on success (if options include #LYD_OPT_FILTER, some nodes could still have been deleted as an optimization),
275 * nonzero in case of an error.
276 */
277int lyd_validate(struct lyd_node *node, int options);
Michal Vasko2d162e12015-09-24 14:33:29 +0200278
279/**
Michal Vasko55f60be2015-10-14 13:12:58 +0200280 * @brief Unlink the specified data subtree. All referenced namespaces are copied.
Michal Vasko2d162e12015-09-24 14:33:29 +0200281 *
282 * Note, that the node's connection with the schema tree is kept. Therefore, in case of
283 * reconnecting the node to a data tree using lyd_paste() it is necessary to paste it
284 * to the appropriate place in the data tree following the schema.
285 *
286 * @param[in] node Data tree node to be unlinked (together with all children).
287 * @return 0 for success, nonzero for error
288 */
289int lyd_unlink(struct lyd_node *node);
290
291/**
292 * @brief Free (and unlink) the specified data (sub)tree.
293 *
294 * @param[in] node Root of the (sub)tree to be freed.
295 */
296void lyd_free(struct lyd_node *node);
297
298/**
Radek Krejci81468402016-01-07 13:52:40 +0100299 * @brief Free (and unlink) the specified data (sub)tree and all its siblings (preceding as well as following).
300 *
301 * @param[in] node One of the siblings root element of the (sub)trees to be freed.
302 */
303void lyd_free_withsiblings(struct lyd_node *node);
304
305/**
Radek Krejci134610e2015-10-20 17:15:34 +0200306 * @brief Insert attribute into the data node.
307 *
308 * @param[in] parent Data node where to place the attribute
Radek Krejci5f9e8c92015-10-30 10:01:06 +0100309 * @param[in] name Attribute name including the prefix (prefix:name). Prefix must be the name of one of the
310 * schema in the \p parent's context.
Radek Krejci134610e2015-10-20 17:15:34 +0200311 * @param[in] value Attribute value
312 * @return pointer to the created attribute (which is already connected in \p parent) or NULL on error.
313 */
314struct lyd_attr *lyd_insert_attr(struct lyd_node *parent, const char *name, const char *value);
315
316/**
Radek Krejci88f29302015-10-30 15:42:33 +0100317 * @brief Destroy data attribute
318 *
319 * If the attribute to destroy is a member of a node attribute list, it is necessary to
320 * provide the node itself as \p parent to keep the list consistent.
321 *
322 * @param[in] ctx Context where the attribute was created (usually it is the context of the \p parent)
323 * @param[in] parent Parent node where the attribute is placed
324 * @param[in] attr Attribute to destroy
325 * @param[in] recursive Zero to destroy only the attribute, non-zero to destroy also all the subsequent attributes
326 * in the list.
327 */
328void lyd_free_attr(struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_attr *attr, int recursive);
329
330/**
Michal Vasko2d162e12015-09-24 14:33:29 +0200331 * @brief Opaque internal structure, do not access it from outside.
332 */
333struct lyxml_elem;
334
335/**
336 * @brief Serialize anyxml content for further processing.
337 *
Michal Vasko2d162e12015-09-24 14:33:29 +0200338 * @param[in] anyxml Anyxml content from ::lyd_node_anyxml#value to serialize ax XML string
Michal Vaskoff4c2832015-10-15 13:30:50 +0200339 * @return Serialized content of the anyxml or NULL in case of error. Need to be freed after
340 * done using.
Michal Vasko2d162e12015-09-24 14:33:29 +0200341 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100342char *lyxml_serialize(const struct lyxml_elem *anyxml);
Michal Vasko2d162e12015-09-24 14:33:29 +0200343
344/**
345 * @brief Structure to hold a set of (not necessary somehow connected) ::lyd_node objects.
346 *
347 * To free the structure, use lyd_set_free() function, to manipulate with the structure, use other
348 * lyd_set_* functions.
349 */
350struct lyd_set {
351 unsigned int size; /**< allocated size of the set array */
352 unsigned int number; /**< number of elements in (used size of) the set array */
353 struct lyd_node **set; /**< array of pointers to a ::lyd_node objects */
354};
355
356/**
357 * @brief Create and initiate new ::lyd_set structure.
358 *
359 * @return Created ::lyd_set structure or NULL in case of error.
360 */
361struct lyd_set *lyd_set_new(void);
362
363/**
364 * @brief Add a ::lyd_node object into the set
365 *
366 * @param[in] set Set where the \p node will be added.
367 * @param[in] node The ::lyd_node object to be added into the \p set;
368 * @return 0 on success
369 */
370int lyd_set_add(struct lyd_set *set, struct lyd_node *node);
371
372/**
373 * @brief Free the ::lyd_set data. Frees only the set structure content, not the referred data.
374 *
375 * @param[in] set The set to be freed.
376 */
377void lyd_set_free(struct lyd_set *set);
378
379/**@} */
380
381#ifdef __cplusplus
382}
383#endif
384
385#endif /* LY_TREE_DATA_H_ */