Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame^] | 1 | /** |
| 2 | * @file tree.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief libyang representation of data model and data trees. |
| 5 | * |
| 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 | |
| 28 | #ifdef __cplusplus |
| 29 | extern "C" { |
| 30 | #endif |
| 31 | |
| 32 | /** |
| 33 | * @addtogroup datatree |
| 34 | * @{ |
| 35 | */ |
| 36 | |
| 37 | /** |
| 38 | * @brief Data input/output formats supported by libyang [parser](@ref parsers) and [printer](@ref printers) functions. |
| 39 | */ |
| 40 | typedef 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 */ |
| 44 | } LYD_FORMAT; |
| 45 | |
| 46 | /** |
| 47 | * @brief Data attribute's type to distinguish between a standard (XML) attribute and namespace definition |
| 48 | */ |
| 49 | typedef enum lyd_attr_type { |
| 50 | LYD_ATTR_STD = 1, /**< standard attribute, see ::lyd_attr structure */ |
| 51 | LYD_ATTR_NS = 2 /**< namespace definition, see ::lyd_ns structure */ |
| 52 | } LYD_ATTR_TYPE; |
| 53 | |
| 54 | /** |
| 55 | * @brief Namespace definition structure. |
| 56 | * |
| 57 | * Usually, the object is provided as ::lyd_attr structure. The structure is compatible with |
| 58 | * ::lyd_attr within the first two members (#type and #next) to allow passing through and type |
| 59 | * detection interchangeably. When the type member is set to #LYD_ATTR_NS, the ::lyd_attr |
| 60 | * structure should be cast to ::lyd_ns to access the rest of members. |
| 61 | */ |
| 62 | struct lyd_ns { |
| 63 | LYD_ATTR_TYPE type; /**< always #LYD_ATTR_NS, compatible with ::lyd_attr */ |
| 64 | struct lyd_attr *next; /**< pointer to the next attribute or namespace definition of an element, |
| 65 | compatible with ::lyd_attr */ |
| 66 | struct lyd_node *parent; /**< pointer to the element where the namespace definition is placed */ |
| 67 | const char *prefix; /**< namespace prefix value */ |
| 68 | const char *value; /**< namespace value */ |
| 69 | }; |
| 70 | |
| 71 | /** |
| 72 | * @brief Attribute structure. |
| 73 | * |
| 74 | * The structure provides information about attributes of a data element and covers not only |
| 75 | * attributes but also namespace definitions. Therefore, the first two members (#type and #next) |
| 76 | * can be safely accessed to pass through the attributes list and type detection. When the #type |
| 77 | * member has #LYD_ATTR_STD value, the rest of the members can be used. Otherwise, the object |
| 78 | * should be cast to the appropriate structure according to #LYD_ATTR_TYPE enumeration. |
| 79 | */ |
| 80 | struct lyd_attr { |
| 81 | LYD_ATTR_TYPE type; /**< type of the attribute, to access the last three members, the value |
| 82 | must be ::LYD_ATTR_STD */ |
| 83 | struct lyd_attr *next; /**< pointer to the next attribute or namespace definition of an element */ |
| 84 | struct lyd_ns *ns; /**< pointer to the definition of the namespace of the attribute */ |
| 85 | const char *name; /**< attribute name */ |
| 86 | const char *value; /**< attribute value */ |
| 87 | }; |
| 88 | |
| 89 | /** |
| 90 | * @brief node's value representation |
| 91 | */ |
| 92 | typedef union lyd_value_u { |
| 93 | const char *binary; /**< base64 encoded, NULL terminated string */ |
| 94 | struct lys_type_bit **bit; /**< array of pointers to the schema definition of the bit value that are set */ |
| 95 | int8_t bool; /**< 0 as false, 1 as true */ |
| 96 | int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */ |
| 97 | struct lys_type_enum *enm; /**< pointer to the schema definition of the enumeration value */ |
| 98 | struct lys_ident *ident; /**< pointer to the schema definition of the identityref value */ |
| 99 | struct lyd_node *instance; /**< instance-identifier, pointer to the referenced data tree node */ |
| 100 | int8_t int8; /**< 8-bit signed integer */ |
| 101 | int16_t int16; /**< 16-bit signed integer */ |
| 102 | int32_t int32; /**< 32-bit signed integer */ |
| 103 | int64_t int64; /**< 64-bit signed integer */ |
| 104 | struct lyd_node *leafref; /**< pointer to the referenced leaf/leaflist instance in data tree */ |
| 105 | const char *string; /**< string */ |
| 106 | uint8_t uint8; /**< 8-bit unsigned integer */ |
| 107 | uint16_t uint16; /**< 16-bit signed integer */ |
| 108 | uint32_t uint32; /**< 32-bit signed integer */ |
| 109 | uint64_t uint64; /**< 64-bit signed integer */ |
| 110 | } lyd_val; |
| 111 | |
| 112 | /** |
| 113 | * @brief Generic structure for a data node, directly applicable to the data nodes defined as #LYS_CONTAINER, #LYS_LIST |
| 114 | * and #LYS_CHOICE. |
| 115 | * |
| 116 | * Completely fits to containers and choices and is compatible (can be used interchangeably except the #child member) |
| 117 | * with all other lyd_node_* structures. All data nodes are provides as ::lyd_node structure by default. |
| 118 | * According to the schema's ::lys_node#nodetype member, the specific object is supposed to be cast to |
| 119 | * ::lyd_node_list, ::lyd_node_leaf, ::lyd_node_leaflist or ::lyd_node_anyxml structures. This structure fits only to |
| 120 | * #LYS_CONTAINER and #LYS_CHOICE values. |
| 121 | * |
| 122 | * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro. |
| 123 | */ |
| 124 | struct lyd_node { |
| 125 | struct lys_node *schema; /**< pointer to the schema definition of this node */ |
| 126 | |
| 127 | struct lyd_attr *attr; /**< pointer to the list of attributes of this node */ |
| 128 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 129 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 130 | never NULL. If there is no sibling node, pointer points to the node |
| 131 | itself. In case of the first node, this pointer points to the last |
| 132 | node in the list. */ |
| 133 | struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 134 | struct lyd_node *child; /**< pointer to the first child node \note Since other lyd_node_* |
| 135 | structures (except ::lyd_node_list) represent end nodes, this member |
| 136 | is replaced in those structures. Therefore, be careful with accessing |
| 137 | this member without having information about the node type from the schema's |
| 138 | ::lys_node#nodetype member. */ |
| 139 | }; |
| 140 | |
| 141 | /** |
| 142 | * @brief Structure for data nodes defined as #LYS_LEAF. |
| 143 | * |
| 144 | * Extension for ::lyd_node structure - replaces the ::lyd_node#child member by three new members (#value, #value_str |
| 145 | * and #value_type) to provide information about the leaf's value. The first five members (#schema, #attr, #next, |
| 146 | * #prev and #parent) are compatible with the ::lyd_node's members. |
| 147 | * |
| 148 | * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro. |
| 149 | */ |
| 150 | struct lyd_node_leaf { |
| 151 | struct lys_node *schema; /**< pointer to the schema definition of this node which is ::lys_node_leaf |
| 152 | structure */ |
| 153 | |
| 154 | struct lyd_attr *attr; /**< pointer to the list of attributes of this node */ |
| 155 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 156 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 157 | never NULL. If there is no sibling node, pointer points to the node |
| 158 | itself. In case of the first node, this pointer points to the last |
| 159 | node in the list. */ |
| 160 | struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 161 | |
| 162 | /* struct lyd_node *child; should be here, but it is not! */ |
| 163 | |
| 164 | /* leaf's specific members */ |
| 165 | lyd_val value; /**< node's value representation */ |
| 166 | const char *value_str; /**< string representation of value (for comparison, printing,...) */ |
| 167 | LY_DATA_TYPE value_type; /**< type of the value in the node, mainly for union to avoid repeating of type detection */ |
| 168 | }; |
| 169 | |
| 170 | /** |
| 171 | * @brief Structure for data nodes defined as #LYS_LEAF. |
| 172 | * |
| 173 | * Extension for ::lyd_node structure. It combines ::lyd_node_leaf and :lyd_node_list by replacing the |
| 174 | * ::lyd_node#child member by five new members (#value, #value_str and #value_type) to provide |
| 175 | * information about the value and other leaf-list's instances. The first five members (#schema, #attr, #next, |
| 176 | * #prev and #parent) are compatible with the ::lyd_node's members. |
| 177 | * |
| 178 | * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro. |
| 179 | */ |
| 180 | struct lyd_node_leaflist { |
| 181 | struct lys_node *schema; /**< pointer to the schema definition of this node which is ::lys_node_leaflist |
| 182 | structure */ |
| 183 | |
| 184 | struct lyd_attr *attr; /**< pointer to the list of attributes of this node */ |
| 185 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 186 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 187 | never NULL. If there is no sibling node, pointer points to the node |
| 188 | itself. In case of the first node, this pointer points to the last |
| 189 | node in the list. */ |
| 190 | struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 191 | |
| 192 | /* struct lyd_node *child; should be here, but is not */ |
| 193 | |
| 194 | /* leaflist's specific members */ |
| 195 | lyd_val value; /**< node's value representation */ |
| 196 | const char *value_str; /**< string representation of value (for comparison, printing,...) */ |
| 197 | LY_DATA_TYPE value_type; /**< type of the value in the node, mainly for union to avoid repeating of type detection */ |
| 198 | }; |
| 199 | |
| 200 | /** |
| 201 | * @brief Structure for data nodes defined as #LYS_ANYXML. |
| 202 | * |
| 203 | * Extension for ::lyd_node structure - replaces the ::lyd_node#child member by new #value member. The first five |
| 204 | * members (#schema, #attr, #next, #prev and #parent) are compatible with the ::lyd_node's members. |
| 205 | * |
| 206 | * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro. |
| 207 | */ |
| 208 | struct lyd_node_anyxml { |
| 209 | struct lys_node *schema; /**< pointer to the schema definition of this node which is ::lys_node_anyxml |
| 210 | structure */ |
| 211 | |
| 212 | struct lyd_attr *attr; /**< pointer to the list of attributes of this node */ |
| 213 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 214 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 215 | never NULL. If there is no sibling node, pointer points to the node |
| 216 | itself. In case of the first node, this pointer points to the last |
| 217 | node in the list. */ |
| 218 | struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 219 | |
| 220 | /* struct lyd_node *child; should be here, but is not */ |
| 221 | |
| 222 | /* anyxml's specific members */ |
| 223 | struct lyxml_elem *value; /**< anyxml name is the root element of value! */ |
| 224 | }; |
| 225 | |
| 226 | /** |
| 227 | * @brief Create a new node in a data tree. |
| 228 | * |
| 229 | * TODO not implemented |
| 230 | * |
| 231 | * @param[in] parent Parent node for the node being created. NULL in case of creating top level element. |
| 232 | * @param[in] module Module of the node being created. Can be NULL in case the new node belongs to the same |
| 233 | * module as its parent. Therefore, the module parameter must be specified for top level and augmenting elements. |
| 234 | * @param[in] name Name of the node being created. |
| 235 | * @param[in] type Type of the value provided in the \p value parameter. Accepted only in case of creating |
| 236 | * #LYS_LEAF or #LYS_LEAFLIST. |
| 237 | * @param[in] value Value of the node being created. Accepted only in case of creating #LYS_LEAF or #LYS_LEAFLIST. |
| 238 | */ |
| 239 | struct lyd_node *lyd_new(struct lyd_node *parent, struct lys_module *module, const char *name, LY_DATA_TYPE type, |
| 240 | lyd_val *value); |
| 241 | |
| 242 | /** |
| 243 | * @brief Create a copy of the specified data tree \p node |
| 244 | * |
| 245 | * TODO not implemented |
| 246 | * |
| 247 | * @param[in] node Data tree node to be duplicated. |
| 248 | * @param[in] recursive 1 if all children are supposed to be also duplicated. |
| 249 | * @return Created copy of the provided data \p node. |
| 250 | */ |
| 251 | struct lyd_node *lyd_dup(struct lyd_node *node, int recursive); |
| 252 | |
| 253 | /** |
| 254 | * @brief Insert the \p node element as child to the \p parent element. The \p node is inserted as a last child of the |
| 255 | * \p parent. |
| 256 | * |
| 257 | * If the node is part of some other tree, it is automatically unlinked. |
| 258 | * If the node is the first node of a node list (with no parent), all |
| 259 | * the subsequent nodes are also inserted. |
| 260 | * |
| 261 | * @param[in] parent Parent node for the \p node being inserted. |
| 262 | * @param[in] node The node being inserted. |
| 263 | * @param[in] options Options for the inserting data to the target data tree options, see @ref parseroptions. |
| 264 | * @return 0 fo success, nonzero in case of error, e.g. when the node is being inserted to an inappropriate place |
| 265 | * in the data tree. |
| 266 | */ |
| 267 | int lyd_insert(struct lyd_node *parent, struct lyd_node *node, int options); |
| 268 | |
| 269 | /** |
| 270 | * @brief Insert the \p node element after the \p sibling element. |
| 271 | * |
| 272 | * TODO not implemented |
| 273 | * |
| 274 | * @param[in] sibling The data tree node before which the \p node will be inserted. |
| 275 | * @param[in] node The data tree node to be inserted. |
| 276 | * @return 0 fo success, nonzero in case of error, e.g. when the node is being inserted to an inappropriate place |
| 277 | * in the data tree. |
| 278 | */ |
| 279 | int lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node); |
| 280 | |
| 281 | /** |
| 282 | * @brief Insert the \p node element after the \p sibling element. |
| 283 | * |
| 284 | * @param[in] sibling The data tree node before which the \p node will be inserted. |
| 285 | * @param[in] node The data tree node to be inserted. |
| 286 | * @param[in] options Options for the inserting data to the target data tree options, see @ref parseroptions. |
| 287 | * @return 0 fo success, nonzero in case of error, e.g. when the node is being inserted to an inappropriate place |
| 288 | * in the data tree. |
| 289 | */ |
| 290 | int lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node, int options); |
| 291 | |
| 292 | /** |
| 293 | * @brief Move the data tree \p node before the specified \p sibling node |
| 294 | * |
| 295 | * Both the data nodes must be in the same children list, i.e. they have the same parent. |
| 296 | * |
| 297 | * TODO not implemented |
| 298 | * |
| 299 | * @param[in] sibling The data tree node before which the \p node will be moved. |
| 300 | * @param[in] node The data tree node to be moved. |
| 301 | * @return 0 for success, nonzero in case of error |
| 302 | */ |
| 303 | int lyd_move_before(struct lyd_node *sibling, struct lyd_node *node); |
| 304 | |
| 305 | /** |
| 306 | * @brief Move the data tree \p node after the specified \p sibling node |
| 307 | * |
| 308 | * Both the data nodes must be in the same children list, i.e. they have the same parent. |
| 309 | * |
| 310 | * TODO not implemented |
| 311 | * |
| 312 | * @param[in] sibling The data tree node after which the \p node will be moved. |
| 313 | * @param[in] node The data tree node to be moved. |
| 314 | * @return 0 for success, nonzero in case of error |
| 315 | */ |
| 316 | int lyd_move_after(struct lyd_node *sibling, struct lyd_node *node); |
| 317 | |
| 318 | /** |
| 319 | * @brief Unlink the specified data subtree. |
| 320 | * |
| 321 | * Note, that the node's connection with the schema tree is kept. Therefore, in case of |
| 322 | * reconnecting the node to a data tree using lyd_paste() it is necessary to paste it |
| 323 | * to the appropriate place in the data tree following the schema. |
| 324 | * |
| 325 | * @param[in] node Data tree node to be unlinked (together with all children). |
| 326 | * @return 0 for success, nonzero for error |
| 327 | */ |
| 328 | int lyd_unlink(struct lyd_node *node); |
| 329 | |
| 330 | /** |
| 331 | * @brief Free (and unlink) the specified data (sub)tree. |
| 332 | * |
| 333 | * @param[in] node Root of the (sub)tree to be freed. |
| 334 | */ |
| 335 | void lyd_free(struct lyd_node *node); |
| 336 | |
| 337 | /** |
| 338 | * @brief Opaque internal structure, do not access it from outside. |
| 339 | */ |
| 340 | struct lyxml_elem; |
| 341 | |
| 342 | /** |
| 343 | * @brief Serialize anyxml content for further processing. |
| 344 | * |
| 345 | * TODO not implemented |
| 346 | * |
| 347 | * @param[in] anyxml Anyxml content from ::lyd_node_anyxml#value to serialize ax XML string |
| 348 | * @return Serialized content of the anyxml or NULL in case of error |
| 349 | */ |
| 350 | char *lyxml_serialize(struct lyxml_elem *anyxml); |
| 351 | |
| 352 | /** |
| 353 | * @brief Structure to hold a set of (not necessary somehow connected) ::lyd_node objects. |
| 354 | * |
| 355 | * To free the structure, use lyd_set_free() function, to manipulate with the structure, use other |
| 356 | * lyd_set_* functions. |
| 357 | */ |
| 358 | struct lyd_set { |
| 359 | unsigned int size; /**< allocated size of the set array */ |
| 360 | unsigned int number; /**< number of elements in (used size of) the set array */ |
| 361 | struct lyd_node **set; /**< array of pointers to a ::lyd_node objects */ |
| 362 | }; |
| 363 | |
| 364 | /** |
| 365 | * @brief Create and initiate new ::lyd_set structure. |
| 366 | * |
| 367 | * @return Created ::lyd_set structure or NULL in case of error. |
| 368 | */ |
| 369 | struct lyd_set *lyd_set_new(void); |
| 370 | |
| 371 | /** |
| 372 | * @brief Add a ::lyd_node object into the set |
| 373 | * |
| 374 | * @param[in] set Set where the \p node will be added. |
| 375 | * @param[in] node The ::lyd_node object to be added into the \p set; |
| 376 | * @return 0 on success |
| 377 | */ |
| 378 | int lyd_set_add(struct lyd_set *set, struct lyd_node *node); |
| 379 | |
| 380 | /** |
| 381 | * @brief Free the ::lyd_set data. Frees only the set structure content, not the referred data. |
| 382 | * |
| 383 | * @param[in] set The set to be freed. |
| 384 | */ |
| 385 | void lyd_set_free(struct lyd_set *set); |
| 386 | |
| 387 | /**@} */ |
| 388 | |
| 389 | #ifdef __cplusplus |
| 390 | } |
| 391 | #endif |
| 392 | |
| 393 | #endif /* LY_TREE_DATA_H_ */ |