Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame^] | 1 | /** |
| 2 | * @file tree_data.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief libyang representation of YANG data trees. |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2019 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * 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 |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | #ifndef LY_TREE_DATA_H_ |
| 16 | #define LY_TREE_DATA_H_ |
| 17 | |
| 18 | #include <stddef.h> |
| 19 | #include <stdint.h> |
| 20 | |
| 21 | #include "log.h" |
| 22 | #include "tree.h" |
| 23 | #include "tree_schema.h" |
| 24 | |
| 25 | struct ly_ctx; |
| 26 | |
| 27 | #ifdef __cplusplus |
| 28 | extern "C" { |
| 29 | #endif |
| 30 | |
| 31 | /** |
| 32 | * @defgroup datatree Data Tree |
| 33 | * @{ |
| 34 | * |
| 35 | * Data structures and functions to manipulate and access instance data tree. |
| 36 | */ |
| 37 | |
| 38 | /** |
| 39 | * @brief Macro to iterate via all elements in a data tree. This is the opening part |
| 40 | * to the #LYD_TREE_DFS_END - they always have to be used together. |
| 41 | * |
| 42 | * The function follows deep-first search algorithm: |
| 43 | * <pre> |
| 44 | * 1 |
| 45 | * / \ |
| 46 | * 2 4 |
| 47 | * / / \ |
| 48 | * 3 5 6 |
| 49 | * </pre> |
| 50 | * |
| 51 | * Use the same parameters for #LY_TREE_DFS_BEGIN and #LY_TREE_DFS_END. While |
| 52 | * START can be any of the lyd_node* types, NEXT and ELEM variables are expected |
| 53 | * to be pointers to a generic struct lyd_node. |
| 54 | * |
| 55 | * Since the next node is selected as part of #LYD_TREE_DFS_END, do not use |
| 56 | * continue statement between the #LYD_TREE_DFS_BEGIN and #LYD_TREE_DFS_END. |
| 57 | * |
| 58 | * Use with opening curly bracket '{' after the macro. |
| 59 | * |
| 60 | * @param START Pointer to the starting element processed first. |
| 61 | * @param NEXT Temporary storage, do not use. |
| 62 | * @param ELEM Iterator intended for use in the block. |
| 63 | */ |
| 64 | #define LYD_TREE_DFS_BEGIN(START, NEXT, ELEM) \ |
| 65 | for ((ELEM) = (NEXT) = (START); \ |
| 66 | (ELEM); \ |
| 67 | (ELEM) = (NEXT)) |
| 68 | |
| 69 | /** |
| 70 | * @brief Macro to iterate via all elements in a tree. This is the closing part |
| 71 | * to the #LYD_TREE_DFS_BEGIN - they always have to be used together. |
| 72 | * |
| 73 | * Use the same parameters for #LYD_TREE_DFS_BEGIN and #LYD_TREE_DFS_END. While |
| 74 | * START can be any of the lyd_node* types, NEXT and ELEM variables are expected |
| 75 | * to be pointers to a generic struct lyd_node. |
| 76 | * |
| 77 | * Use with closing curly bracket '}' after the macro. |
| 78 | * |
| 79 | * @param START Pointer to the starting element processed first. |
| 80 | * @param NEXT Temporary storage, do not use. |
| 81 | * @param ELEM Iterator intended for use in the block. |
| 82 | */ |
| 83 | |
| 84 | #define LYD_TREE_DFS_END(START, NEXT, ELEM) \ |
| 85 | /* select element for the next run - children first */ \ |
| 86 | (NEXT) = (struct lyd_node*)lyd_node_children((struct lyd_node*)ELEM); \ |
| 87 | if (!(NEXT)) { \ |
| 88 | /* no children */ \ |
| 89 | if ((ELEM) == (struct lyd_node*)(START)) { \ |
| 90 | /* we are done, (START) has no children */ \ |
| 91 | break; \ |
| 92 | } \ |
| 93 | /* try siblings */ \ |
| 94 | (NEXT) = (ELEM)->next; \ |
| 95 | } \ |
| 96 | while (!(NEXT)) { \ |
| 97 | /* parent is already processed, go to its sibling */ \ |
| 98 | (ELEM) = (struct lyd_node*)(ELEM)->parent; \ |
| 99 | /* no siblings, go back through parents */ \ |
| 100 | if ((ELEM)->parent == (START)->parent) { \ |
| 101 | /* we are done, no next element to process */ \ |
| 102 | break; \ |
| 103 | } \ |
| 104 | (NEXT) = (ELEM)->next; \ |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @brief Data input/output formats supported by libyang [parser](@ref howtodataparsers) and |
| 109 | * [printer](@ref howtodataprinters) functions. |
| 110 | */ |
| 111 | typedef enum { |
| 112 | LYD_UNKNOWN = 0, /**< unknown format, used as return value in case of error */ |
| 113 | LYD_XML, /**< XML format of the instance data */ |
| 114 | #if 0 |
| 115 | LYD_JSON, /**< JSON format of the instance data */ |
| 116 | LYD_LYB, /**< LYB format of the instance data */ |
| 117 | #endif |
| 118 | } LYD_FORMAT; |
| 119 | |
| 120 | /** |
| 121 | * @brief List of possible value types stored in ::lyd_node_anydata. |
| 122 | */ |
| 123 | typedef enum { |
| 124 | LYD_ANYDATA_CONSTSTRING = 0x00, /**< value is constant string (const char *) which is internally duplicated for |
| 125 | storing in the anydata structure; XML sensitive characters (such as & or \>) |
| 126 | are automatically escaped when the anydata is printed in XML format. */ |
| 127 | LYD_ANYDATA_STRING = 0x01, /**< value is dynamically allocated string (char*), so the data are used directly |
| 128 | without duplication and caller is supposed to not manipulate with the data |
| 129 | after a successful call (including calling free() on the provided data); XML |
| 130 | sensitive characters (such as & or \>) are automatically escaped when the |
| 131 | anydata is printed in XML format */ |
| 132 | LYD_ANYDATA_JSON = 0x02, /**< value is string containing the data modeled by YANG and encoded as I-JSON. The |
| 133 | string is handled as constant string. In case of using the value as input |
| 134 | parameter, the #LYD_ANYDATA_JSOND can be used for dynamically allocated |
| 135 | string. */ |
| 136 | LYD_ANYDATA_JSOND = 0x03, /**< In case of using value as input parameter, this enumeration is supposed to be |
| 137 | used for dynamically allocated strings (it is actually combination of |
| 138 | #LYD_ANYDATA_JSON and #LYD_ANYDATA_STRING (and it can be also specified as |
| 139 | ORed value of the mentioned values. */ |
| 140 | LYD_ANYDATA_SXML = 0x04, /**< value is string containing the serialized XML data. The string is handled as |
| 141 | constant string. In case of using the value as input parameter, the |
| 142 | #LYD_ANYDATA_SXMLD can be used for dynamically allocated string. */ |
| 143 | LYD_ANYDATA_SXMLD = 0x05, /**< In case of using serialized XML value as input parameter, this enumeration is |
| 144 | supposed to be used for dynamically allocated strings (it is actually |
| 145 | combination of #LYD_ANYDATA_SXML and #LYD_ANYDATA_STRING (and it can be also |
| 146 | specified as ORed value of the mentioned values). */ |
| 147 | LYD_ANYDATA_XML = 0x08, /**< value is struct lyxml_elem*, the structure is directly connected into the |
| 148 | anydata node without duplication, caller is supposed to not manipulate with the |
| 149 | data after a successful call (including calling lyxml_free() on the provided |
| 150 | data) */ |
| 151 | LYD_ANYDATA_DATATREE = 0x10, /**< value is struct lyd_node* (first sibling), the structure is directly connected |
| 152 | into the anydata node without duplication, caller is supposed to not manipulate |
| 153 | with the data after a successful call (including calling lyd_free() on the |
| 154 | provided data) */ |
| 155 | LYD_ANYDATA_LYB = 0x20, /**< value is a memory with serialized data tree in LYB format. The data are handled |
| 156 | as a constant string. In case of using the value as input parameter, |
| 157 | the #LYD_ANYDATA_LYBD can be used for dynamically allocated string. */ |
| 158 | LYD_ANYDATA_LYBD = 0x21, /**< In case of using LYB value as input parameter, this enumeration is |
| 159 | supposed to be used for dynamically allocated strings (it is actually |
| 160 | combination of #LYD_ANYDATA_LYB and #LYD_ANYDATA_STRING (and it can be also |
| 161 | specified as ORed value of the mentioned values). */ |
| 162 | } LYD_ANYDATA_VALUETYPE; |
| 163 | |
| 164 | /** @} */ |
| 165 | |
| 166 | /** |
| 167 | * @brief YANG data representation |
| 168 | */ |
| 169 | struct lyd_value { |
| 170 | const char *canonized; /**< string representation of value (for comparison, printing,...), canonized according to the |
| 171 | rules implemented in the type's canonization callback (if any). */ |
| 172 | union { |
| 173 | const char *string; /**< original, non-canonized string value. Useful for example for unions where the type (and therefore |
| 174 | the cannonization rules) can change by changing value (e.g. leafref target) somewhere else. */ |
| 175 | int8_t boolean; /**< 0 as false, 1 as true */ |
| 176 | int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */ |
| 177 | struct lysc_type_bitenum_item *enum_item; /**< pointer to the definition of the enumeration value */ |
| 178 | struct lysc_ident *ident; /**< pointer to the schema definition of the identityref value */ |
| 179 | int8_t int8; /**< 8-bit signed integer */ |
| 180 | int16_t int16; /**< 16-bit signed integer */ |
| 181 | int32_t int32; /**< 32-bit signed integer */ |
| 182 | int64_t int64; /**< 64-bit signed integer */ |
| 183 | uint8_t uint8; /**< 8-bit unsigned integer */ |
| 184 | uint16_t uint16; /**< 16-bit signed integer */ |
| 185 | uint32_t uint32; /**< 32-bit signed integer */ |
| 186 | uint64_t uint64; /**< 64-bit signed integer */ |
| 187 | void *ptr; /**< generic data type structure used to store the data */ |
| 188 | }; /**< The union is just a list of shorthands to possible values stored by a type's plugin. libyang works only with the canonized string, |
| 189 | this specific data type storage is just to simplify use of the values by the libyang users. */ |
| 190 | LY_DATA_TYPE type; /**< type of the value in the node, mainly for union to avoid repeating of type detection */ |
| 191 | }; |
| 192 | |
| 193 | /** |
| 194 | * @brief Attribute structure. |
| 195 | * |
| 196 | * The structure provides information about attributes of a data element. Such attributes must map to |
| 197 | * annotations as specified in RFC 7952. The only exception is the filter type (in NETCONF get operations) |
| 198 | * and edit-config's operation attributes. In XML, they are represented as standard XML attributes. In JSON, |
| 199 | * they are represented as JSON elements starting with the '@' character (for more information, see the |
| 200 | * YANG metadata RFC. |
| 201 | * |
| 202 | */ |
| 203 | struct lyd_attr { |
| 204 | struct lyd_node *parent; /**< data node where the attribute is placed */ |
| 205 | struct lyd_attr *next; /**< pointer to the next attribute of the same element */ |
| 206 | void *annotation; /**< TODO pointer to the attribute/annotation's definition */ |
| 207 | const char *name; /**< attribute name */ |
| 208 | struct lyd_value value; /**< attribute's value representation */ |
| 209 | }; |
| 210 | |
| 211 | |
| 212 | #define LYD_NODE_INNER (LYS_CONTAINER|LYS_LIST) /**< Schema nodetype mask for lyd_node_inner */ |
| 213 | #define LYD_NODE_TERM (LYS_LEAF|LYS_LEAFLIST) /**< Schema nodetype mask for lyd_node_term */ |
| 214 | #define LYD_NODE_ANY (LYS_ANYDATA) /**< Schema nodetype mask for lyd_node_any */ |
| 215 | |
| 216 | /** |
| 217 | * @brief Generic structure for a data node. |
| 218 | */ |
| 219 | struct lyd_node { |
| 220 | uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list) */ |
| 221 | const struct lysc_node *schema; /**< pointer to the schema definition of this node */ |
| 222 | struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 223 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 224 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 225 | never NULL. If there is no sibling node, pointer points to the node |
| 226 | itself. In case of the first node, this pointer points to the last |
| 227 | node in the list. */ |
| 228 | struct lyd_attr *attr; /**< pointer to the list of attributes of this node */ |
| 229 | |
| 230 | #ifdef LY_ENABLED_LYD_PRIV |
| 231 | void *priv; /**< private user data, not used by libyang */ |
| 232 | #endif |
| 233 | }; |
| 234 | |
| 235 | /** |
| 236 | * @brief Data node structure for the inner data tree nodes - containers and lists. |
| 237 | */ |
| 238 | struct lyd_node_inner { |
| 239 | uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list) */ |
| 240 | const struct lysc_node *schema; /**< pointer to the schema definition of this node */ |
| 241 | struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 242 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 243 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 244 | never NULL. If there is no sibling node, pointer points to the node |
| 245 | itself. In case of the first node, this pointer points to the last |
| 246 | node in the list. */ |
| 247 | struct lyd_attr *attr; /**< pointer to the list of attributes of this node */ |
| 248 | |
| 249 | #ifdef LY_ENABLED_LYD_PRIV |
| 250 | void *priv; /**< private user data, not used by libyang */ |
| 251 | #endif |
| 252 | |
| 253 | struct lyd_node *child; /**< pointer to the first child node. */ |
| 254 | struct hash_table *children_ht; /**< hash table with all the direct children (except keys for a list, lists without keys) */ |
| 255 | }; |
| 256 | |
| 257 | /** |
| 258 | * @brief Data node structure for the terminal data tree nodes - leafs and leaf-lists. |
| 259 | */ |
| 260 | struct lyd_node_term { |
| 261 | uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list) */ |
| 262 | const struct lysc_node *schema; /**< pointer to the schema definition of this node */ |
| 263 | struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 264 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 265 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 266 | never NULL. If there is no sibling node, pointer points to the node |
| 267 | itself. In case of the first node, this pointer points to the last |
| 268 | node in the list. */ |
| 269 | struct lyd_attr *attr; /**< pointer to the list of attributes of this node */ |
| 270 | |
| 271 | #ifdef LY_ENABLED_LYD_PRIV |
| 272 | void *priv; /**< private user data, not used by libyang */ |
| 273 | #endif |
| 274 | |
| 275 | struct lyd_value value; /**< node's value representation */ |
| 276 | }; |
| 277 | |
| 278 | /** |
| 279 | * @brief Data node structure for the anydata data tree nodes - anydatas and anyxmls. |
| 280 | */ |
| 281 | struct lyd_node_any { |
| 282 | uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list) */ |
| 283 | const struct lysc_node *schema; /**< pointer to the schema definition of this node */ |
| 284 | struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 285 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 286 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 287 | never NULL. If there is no sibling node, pointer points to the node |
| 288 | itself. In case of the first node, this pointer points to the last |
| 289 | node in the list. */ |
| 290 | struct lyd_attr *attr; /**< pointer to the list of attributes of this node */ |
| 291 | |
| 292 | #ifdef LY_ENABLED_LYD_PRIV |
| 293 | void *priv; /**< private user data, not used by libyang */ |
| 294 | #endif |
| 295 | |
| 296 | /* TODO - anydata representation */ |
| 297 | }; |
| 298 | |
| 299 | /** |
| 300 | * @defgroup dataparseroptions Data parser options |
| 301 | * @ingroup datatree |
| 302 | * |
| 303 | * Various options to change the data tree parsers behavior. |
| 304 | * |
| 305 | * Default behavior: |
| 306 | * - in case of XML, parser reads all data from its input (file, memory, XML tree) including the case of not well-formed |
| 307 | * XML document (multiple top-level elements) and if there is an unknown element, it is skipped including its subtree |
| 308 | * (see the next point). This can be changed by the #LYD_OPT_NOSIBLINGS option which make parser to read only a single |
| 309 | * tree (with a single root element) from its input. |
| 310 | * - parser silently ignores the data without a matching node in schema trees. If the caller want to stop |
| 311 | * parsing in case of presence of unknown data, the #LYD_OPT_STRICT can be used. The strict mode is useful for |
| 312 | * NETCONF servers, since NETCONF clients should always send data according to the capabilities announced by the server. |
| 313 | * On the other hand, the default non-strict mode is useful for clients receiving data from NETCONF server since |
| 314 | * clients are not required to understand everything the server does. Of course, the optimal strategy for clients is |
| 315 | * to use filtering to get only the required data. Having an unknown element of the known namespace is always an error. |
| 316 | * The behavior can be changed by #LYD_OPT_STRICT option. |
| 317 | * - using obsolete statements (status set to obsolete) just generates a warning, but the processing continues. The |
| 318 | * behavior can be changed by #LYD_OPT_OBSOLETE option. |
| 319 | * - parser expects that the provided data provides complete datastore content (both the configuration and state data) |
| 320 | * and performs data validation according to all YANG rules. This can be a problem in case of representing NETCONF's |
| 321 | * subtree filter data, edit-config's data or other type of data set - such data do not represent a complete data set |
| 322 | * and some of the validation rules can fail. Therefore there are other options (within lower 8 bits) to make parser |
| 323 | * to accept such a data. |
| 324 | * - when parser evaluates when-stmt condition to false, a validation error is raised. If the |
| 325 | * #LYD_OPT_WHENAUTODEL is used, the invalid node is silently removed instead of an error. The option (and also this default |
| 326 | * behavior) takes effect only in case of #LYD_OPT_DATA or #LYD_OPT_CONFIG type of data. |
| 327 | * @{ |
| 328 | */ |
| 329 | |
| 330 | #define LYD_OPT_DATA 0x00 /**< Default type of data - complete datastore content with configuration as well as |
| 331 | state data. To handle possibly missing (but by default required) ietf-yang-library |
| 332 | data, use #LYD_OPT_DATA_NO_YANGLIB or #LYD_OPT_DATA_ADD_YANGLIB options. */ |
| 333 | #define LYD_OPT_CONFIG 0x01 /**< A configuration datastore - complete datastore without state data. |
| 334 | Validation modifications: |
| 335 | - status data are not allowed */ |
| 336 | #define LYD_OPT_GET 0x02 /**< Data content from a NETCONF reply message to the NETCONF \<get\> operation. |
| 337 | Validation modifications: |
| 338 | - mandatory nodes can be omitted |
| 339 | - leafrefs and instance-identifier resolution is allowed to fail |
| 340 | - list's keys/unique nodes are not required (so duplication is not checked) |
| 341 | - must and when evaluation skipped */ |
| 342 | #define LYD_OPT_GETCONFIG 0x04 /**< Data content from a NETCONF reply message to the NETCONF \<get-config\> operation |
| 343 | Validation modifications: |
| 344 | - mandatory nodes can be omitted |
| 345 | - leafrefs and instance-identifier resolution is allowed to fail |
| 346 | - list's keys/unique nodes are not required (so duplication is not checked) |
| 347 | - must and when evaluation skipped |
| 348 | - status data are not allowed */ |
| 349 | #define LYD_OPT_EDIT 0x08 /**< Content of the NETCONF \<edit-config\>'s config element. |
| 350 | Validation modifications: |
| 351 | - mandatory nodes can be omitted |
| 352 | - leafrefs and instance-identifier resolution is allowed to fail |
| 353 | - must and when evaluation skipped |
| 354 | - status data are not allowed */ |
| 355 | #define LYD_OPT_RPC 0x10 /**< Data represents RPC or action input parameters. */ |
| 356 | #define LYD_OPT_RPCREPLY 0x20 /**< Data represents RPC or action output parameters (maps to NETCONF <rpc-reply> data). */ |
| 357 | #define LYD_OPT_NOTIF 0x40 /**< Data represents an event notification data. */ |
| 358 | #define LYD_OPT_NOTIF_FILTER 0x80 /**< Data represents a filtered event notification data. |
| 359 | Validation modification: |
| 360 | - the only requirement is that the data tree matches the schema tree */ |
| 361 | #define LYD_OPT_TYPEMASK 0x10000ff /**< Mask to filter data type options. Always only a single data type option (only |
| 362 | single bit from the lower 8 bits) can be set. */ |
| 363 | |
| 364 | /* 0x100 reserved, used internally */ |
| 365 | #define LYD_OPT_STRICT 0x0200 /**< Instead of silent ignoring data without schema definition, raise an error. */ |
| 366 | #define LYD_OPT_DESTRUCT 0x0400 /**< Free the provided XML tree during parsing the data. With this option, the |
| 367 | provided XML tree is affected and all successfully parsed data are freed. |
| 368 | This option is applicable only to lyd_parse_xml() function. */ |
| 369 | #define LYD_OPT_OBSOLETE 0x0800 /**< Raise an error when an obsolete statement (status set to obsolete) is used. */ |
| 370 | #define LYD_OPT_NOSIBLINGS 0x1000 /**< Parse only a single XML tree from the input. This option applies only to |
| 371 | XML input data. */ |
| 372 | #define LYD_OPT_TRUSTED 0x2000 /**< Data comes from a trusted source and it is not needed to validate them. Data |
| 373 | are connected with the schema, but the most validation checks (mandatory nodes, |
| 374 | list instance uniqueness, etc.) are not performed. This option does not make |
| 375 | sense for lyd_validate() so it is ignored by this function. */ |
| 376 | #define LYD_OPT_WHENAUTODEL 0x4000 /**< Automatically delete subtrees with false when-stmt condition. The flag is |
| 377 | applicable only in combination with #LYD_OPT_DATA and #LYD_OPT_CONFIG flags. |
| 378 | If used, libyang will not generate a validation error. */ |
| 379 | #define LYD_OPT_NOEXTDEPS 0x8000 /**< Allow external dependencies (external leafrefs, instance-identifiers, must, |
| 380 | and when) to not be resolved/satisfied during validation. */ |
| 381 | #define LYD_OPT_DATA_NO_YANGLIB 0x10000 /**< Ignore (possibly) missing ietf-yang-library data. Applicable only with #LYD_OPT_DATA. */ |
| 382 | #define LYD_OPT_DATA_ADD_YANGLIB 0x20000 /**< Add missing ietf-yang-library data into the validated data tree. Applicable |
| 383 | only with #LYD_OPT_DATA. If some ietf-yang-library data are present, they are |
| 384 | preserved and option is ignored. */ |
| 385 | #define LYD_OPT_VAL_DIFF 0x40000 /**< Flag only for validation, store all the data node changes performed by the validation |
| 386 | in a diff structure. */ |
| 387 | #define LYD_OPT_DATA_TEMPLATE 0x1000000 /**< Data represents YANG data template. */ |
| 388 | |
| 389 | /**@} dataparseroptions */ |
| 390 | |
| 391 | /** |
| 392 | * @brief Get the node's children list if any. |
| 393 | * |
| 394 | * Decides the node's type and in case it has a children list, returns it. |
| 395 | * @param[in] node Node to check. |
| 396 | * @return Pointer to the first child node (if any) of the \p node. |
| 397 | */ |
| 398 | const struct lyd_node *lyd_node_children(const struct lyd_node *node); |
| 399 | |
| 400 | /** |
| 401 | * @brief Find the node, in the list, satisfying the given restrictions. |
| 402 | * |
| 403 | * @param[in] first Starting child node for search. |
| 404 | * @param[in] module Module of the node to find (mandatory argument). |
| 405 | * @param[in] name Name of the node to find (mandatory argument). |
| 406 | * @param[in] name_len Optional length of the @p name argument in case it is not NULL-terminated string. |
| 407 | * @param[in] nodetype Optional mask for the nodetype of the node to find, 0 is understood as all nodetypes. |
| 408 | * @param[in] value Optional restriction for lyd_node_term nodes to select node with the specific value. |
| 409 | * @param[in] value_len Optional length of the @p value argument in case it is not NULL-terminated string. |
| 410 | * @return The sibling node of the @p first (or itself), satisfying the given restrictions. |
| 411 | * @return NULL in case there is no node satisfying the restrictions. |
| 412 | */ |
| 413 | const struct lyd_node *lyd_search(const struct lyd_node *first, const struct lys_module *module, |
| 414 | const char *name, size_t name_len, uint16_t nodetype, const char *value, size_t value_len); |
| 415 | |
| 416 | /** |
| 417 | * @brief Parse (and validate) data from memory. |
| 418 | * |
| 419 | * In case of LY_XML format, the data string is parsed completely. It means that when it contains |
| 420 | * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The |
| 421 | * returned data node is a root of the first tree with other trees connected via the next pointer. |
| 422 | * This behavior can be changed by #LYD_OPT_NOSIBLINGS option. |
| 423 | * |
| 424 | * @param[in] ctx Context to connect with the data tree being built here. |
| 425 | * @param[in] data Serialized data in the specified format. |
| 426 | * @param[in] format Format of the input data to be parsed. |
| 427 | * @param[in] options Parser options, see @ref parseroptions. \p format LYD_LYB uses #LYD_OPT_TRUSTED implicitly. |
| 428 | * @param[in] ... Variable arguments depend on \p options. If they include: |
| 429 | * - #LYD_OPT_DATA: |
| 430 | * - #LYD_OPT_CONFIG: |
| 431 | * - #LYD_OPT_GET: |
| 432 | * - #LYD_OPT_GETCONFIG: |
| 433 | * - #LYD_OPT_EDIT: |
| 434 | * - no variable arguments expected. |
| 435 | * - #LYD_OPT_RPC: |
| 436 | * - #LYD_OPT_NOTIF: |
| 437 | * - struct lyd_node *data_tree - additional data tree that will be used |
| 438 | * when checking any "when" or "must" conditions in the parsed tree that require |
| 439 | * some nodes outside their subtree. It must be a list of top-level elements! |
| 440 | * - #LYD_OPT_RPCREPLY: |
| 441 | * - const struct ::lyd_node *rpc_act - pointer to the whole RPC or (top-level) action operation |
| 442 | * data tree (the request) of the reply. |
| 443 | * - const struct ::lyd_node *data_tree - additional data tree that will be used |
| 444 | * when checking any "when" or "must" conditions in the parsed tree that require |
| 445 | * some nodes outside their subtree. It must be a list of top-level elements! |
| 446 | * @return Pointer to the built data tree or NULL in case of empty \p data. To free the returned structure, |
| 447 | * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error, |
| 448 | * #ly_errno contains appropriate error code (see #LY_ERR). |
| 449 | */ |
| 450 | struct lyd_node *lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, ...); |
| 451 | |
| 452 | /** |
| 453 | * @brief Read (and validate) data from the given file descriptor. |
| 454 | * |
| 455 | * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc. |
| 456 | * |
| 457 | * In case of LY_XML format, the file content is parsed completely. It means that when it contains |
| 458 | * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The |
| 459 | * returned data node is a root of the first tree with other trees connected via the next pointer. |
| 460 | * This behavior can be changed by #LYD_OPT_NOSIBLINGS option. |
| 461 | * |
| 462 | * @param[in] ctx Context to connect with the data tree being built here. |
| 463 | * @param[in] fd The standard file descriptor of the file containing the data tree in the specified format. |
| 464 | * @param[in] format Format of the input data to be parsed. |
| 465 | * @param[in] options Parser options, see @ref parseroptions. \p format LYD_LYB uses #LYD_OPT_TRUSTED implicitly. |
| 466 | * @param[in] ... Variable arguments depend on \p options. If they include: |
| 467 | * - #LYD_OPT_DATA: |
| 468 | * - #LYD_OPT_CONFIG: |
| 469 | * - #LYD_OPT_GET: |
| 470 | * - #LYD_OPT_GETCONFIG: |
| 471 | * - #LYD_OPT_EDIT: |
| 472 | * - no variable arguments expected. |
| 473 | * - #LYD_OPT_RPC: |
| 474 | * - #LYD_OPT_NOTIF: |
| 475 | * - struct lyd_node *data_tree - additional data tree that will be used |
| 476 | * when checking any "when" or "must" conditions in the parsed tree that require |
| 477 | * some nodes outside their subtree. It must be a list of top-level elements! |
| 478 | * - #LYD_OPT_RPCREPLY: |
| 479 | * - const struct ::lyd_node *rpc_act - pointer to the whole RPC or action operation data |
| 480 | * tree (the request) of the reply. |
| 481 | * - const struct ::lyd_node *data_tree - additional data tree that will be used |
| 482 | * when checking any "when" or "must" conditions in the parsed tree that require |
| 483 | * some nodes outside their subtree. It must be a list of top-level elements! |
| 484 | * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure, |
| 485 | * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error, |
| 486 | * #ly_errno contains appropriate error code (see #LY_ERR). |
| 487 | */ |
| 488 | struct lyd_node *lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, ...); |
| 489 | |
| 490 | /** |
| 491 | * @brief Read (and validate) data from the given file path. |
| 492 | * |
| 493 | * In case of LY_XML format, the file content is parsed completely. It means that when it contains |
| 494 | * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The |
| 495 | * returned data node is a root of the first tree with other trees connected via the next pointer. |
| 496 | * This behavior can be changed by #LYD_OPT_NOSIBLINGS option. |
| 497 | * |
| 498 | * @param[in] ctx Context to connect with the data tree being built here. |
| 499 | * @param[in] path Path to the file containing the data tree in the specified format. |
| 500 | * @param[in] format Format of the input data to be parsed. |
| 501 | * @param[in] options Parser options, see @ref parseroptions. \p format LYD_LYB uses #LYD_OPT_TRUSTED implicitly. |
| 502 | * @param[in] ... Variable arguments depend on \p options. If they include: |
| 503 | * - #LYD_OPT_DATA: |
| 504 | * - #LYD_OPT_CONFIG: |
| 505 | * - #LYD_OPT_GET: |
| 506 | * - #LYD_OPT_GETCONFIG: |
| 507 | * - #LYD_OPT_EDIT: |
| 508 | * - no variable arguments expected. |
| 509 | * - #LYD_OPT_RPC: |
| 510 | * - #LYD_OPT_NOTIF: |
| 511 | * - struct lyd_node *data_tree - additional data tree that will be used |
| 512 | * when checking any "when" or "must" conditions in the parsed tree that require |
| 513 | * some nodes outside their subtree. It must be a list of top-level elements! |
| 514 | * - #LYD_OPT_RPCREPLY: |
| 515 | * - const struct ::lyd_node *rpc_act - pointer to the whole RPC or action operation data |
| 516 | * tree (the request) of the reply. |
| 517 | * - const struct ::lyd_node *data_tree - additional data tree that will be used |
| 518 | * when checking any "when" or "must" conditions in the parsed tree that require |
| 519 | * some nodes outside their subtree. It must be a list of top-level elements! |
| 520 | * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure, |
| 521 | * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error, |
| 522 | * #ly_errno contains appropriate error code (see #LY_ERR). |
| 523 | */ |
| 524 | struct lyd_node *lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options, ...); |
| 525 | |
| 526 | /** |
| 527 | * @brief Free all the nodes in the data tree. |
| 528 | * |
| 529 | * @param[in] node Any of the nodes inside the tree. |
| 530 | */ |
| 531 | void lyd_free_all(struct lyd_node *node); |
| 532 | |
| 533 | /** |
| 534 | * @brief Free (and unlink) the specified data (sub)tree. |
| 535 | * |
| 536 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
| 537 | * |
| 538 | * @param[in] node Root of the (sub)tree to be freed. |
| 539 | */ |
| 540 | void lyd_free_tree(struct lyd_node *node); |
| 541 | |
| 542 | /** |
| 543 | * @brief Unlink the specified data subtree. All referenced namespaces are copied. |
| 544 | * |
| 545 | * Note, that the node's connection with the schema tree is kept. Therefore, in case of |
| 546 | * reconnecting the node to a data tree using lyd_paste() it is necessary to paste it |
| 547 | * to the appropriate place in the data tree following the schema. |
| 548 | * |
| 549 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
| 550 | * |
| 551 | * @param[in] node Data tree node to be unlinked (together with all children). |
| 552 | * @return LY_SUCCESS for success |
| 553 | * @return LY_E* values in case of error |
| 554 | */ |
| 555 | LY_ERR lyd_unlink_tree(struct lyd_node *node); |
| 556 | |
| 557 | /** |
| 558 | * @brief Destroy data attribute. |
| 559 | * |
| 560 | * @param[in] ctx Context where the attribute was created. |
| 561 | * @param[in] attr Attribute to destroy |
| 562 | * @param[in] recursive Zero to destroy only the attribute (the attribute list is corrected), |
| 563 | * non-zero to destroy also all the subsequent attributes in the list. |
| 564 | */ |
| 565 | void lyd_free_attr(struct ly_ctx *ctx, struct lyd_attr *attr, int recursive); |
| 566 | |
| 567 | #ifdef __cplusplus |
| 568 | } |
| 569 | #endif |
| 570 | |
| 571 | #endif /* LY_TREE_DATA_H_ */ |