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 | * |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 51 | * Use the same parameters for #LYD_TREE_DFS_BEGIN and #LYD_TREE_DFS_END. While |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 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 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 108 | * @brief Macro to get context from a data tree node. |
| 109 | */ |
| 110 | #define LYD_NODE_CTX(node) ((node)->schema->module->ctx) |
| 111 | |
| 112 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 113 | * @brief Data input/output formats supported by libyang [parser](@ref howtodataparsers) and |
| 114 | * [printer](@ref howtodataprinters) functions. |
| 115 | */ |
| 116 | typedef enum { |
| 117 | LYD_UNKNOWN = 0, /**< unknown format, used as return value in case of error */ |
| 118 | LYD_XML, /**< XML format of the instance data */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 119 | LYD_JSON, /**< JSON format of the instance data */ |
Radek Krejci | 355bf4f | 2019-07-16 17:14:16 +0200 | [diff] [blame] | 120 | #if 0 |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 121 | LYD_LYB, /**< LYB format of the instance data */ |
| 122 | #endif |
| 123 | } LYD_FORMAT; |
| 124 | |
| 125 | /** |
Radek Krejci | 59583bb | 2019-09-11 12:57:55 +0200 | [diff] [blame] | 126 | * @brief List of possible value types stored in ::lyd_node_any. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 127 | */ |
| 128 | typedef enum { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 129 | LYD_ANYDATA_DATATREE, /**< Value is a pointer to lyd_node structure (first sibling). When provided as input parameter, the pointer |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 130 | is directly connected into the anydata node without duplication, caller is supposed to not manipulate |
| 131 | with the data after a successful call (including calling lyd_free() on the provided data) */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 132 | LYD_ANYDATA_STRING, /**< Value is a generic string without any knowledge about its format (e.g. anyxml value in JSON encoded |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 133 | as string). XML sensitive characters (such as & or \>) are automatically escaped when the anydata |
| 134 | is printed in XML format. */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 135 | LYD_ANYDATA_XML, /**< Value is a string containing the serialized XML data. */ |
| 136 | LYD_ANYDATA_JSON, /**< Value is a string containing the data modeled by YANG and encoded as I-JSON. */ |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 137 | #if 0 /* TODO LYB format */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 138 | LYD_ANYDATA_LYB, /**< Value is a memory chunk with the serialized data tree in LYB format. */ |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 139 | #endif |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 140 | } LYD_ANYDATA_VALUETYPE; |
| 141 | |
| 142 | /** @} */ |
| 143 | |
| 144 | /** |
| 145 | * @brief YANG data representation |
| 146 | */ |
| 147 | struct lyd_value { |
Radek Krejci | 950f6a5 | 2019-09-12 17:15:32 +0200 | [diff] [blame] | 148 | const char *original; /**< Original string representation of the value. It is never NULL, but (canonical) string representation |
| 149 | of the value should be always obtained via the type's printer callback (lyd_value::realtype::plugin::print). */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 150 | union { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 151 | int8_t boolean; /**< 0 as false, 1 as true */ |
| 152 | int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */ |
Radek Krejci | 8dc4f2d | 2019-07-16 12:24:00 +0200 | [diff] [blame] | 153 | int8_t int8; /**< 8-bit signed integer */ |
| 154 | int16_t int16; /**< 16-bit signed integer */ |
| 155 | int32_t int32; /**< 32-bit signed integer */ |
| 156 | int64_t int64; /**< 64-bit signed integer */ |
| 157 | uint8_t uint8; /**< 8-bit unsigned integer */ |
| 158 | uint16_t uint16; /**< 16-bit signed integer */ |
| 159 | uint32_t uint32; /**< 32-bit signed integer */ |
| 160 | uint64_t uint64; /**< 64-bit signed integer */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 161 | struct lysc_type_bitenum_item *enum_item; /**< pointer to the definition of the enumeration value */ |
Radek Krejci | 849a62a | 2019-05-22 15:29:05 +0200 | [diff] [blame] | 162 | struct lysc_type_bitenum_item **bits_items; /**< list of set pointers to the specification of the set bits ([sized array](@ref sizedarrays)) */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 163 | struct lysc_ident *ident; /**< pointer to the schema definition of the identityref value */ |
Radek Krejci | efbb392 | 2019-07-15 12:58:00 +0200 | [diff] [blame] | 164 | |
| 165 | struct lyd_value_subvalue { |
| 166 | struct lyd_value_prefix { |
| 167 | const char *prefix; /**< prefix string used in the canonized string to identify the mod of the YANG schema */ |
| 168 | const struct lys_module *mod; /**< YANG schema module identified by the prefix string */ |
| 169 | } *prefixes; /**< list of mappings between prefix in canonized value to a YANG schema ([sized array](@ref sizedarrays)) */ |
Radek Krejci | 8dc4f2d | 2019-07-16 12:24:00 +0200 | [diff] [blame] | 170 | struct lyd_value *value; /**< representation of the value according to the selected union's subtype (stored as lyd_value::realpath |
| 171 | here, in subvalue structure */ |
| 172 | } *subvalue; /**< data to represent data with multiple types (union). Original value is stored in the main |
| 173 | lyd_value:canonized while the lyd_value_subvalue::value contains representation according to the |
| 174 | one of the union's type. The lyd_value_subvalue:prefixes provides (possible) mappings from prefixes |
| 175 | in original value to YANG modules. These prefixes are necessary to parse original value to the union's |
| 176 | subtypes. */ |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 177 | |
| 178 | struct lyd_value_path { |
Radek Krejci | 8dc4f2d | 2019-07-16 12:24:00 +0200 | [diff] [blame] | 179 | const struct lysc_node *node; /**< Schema node representing the path segment */ |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 180 | struct lyd_value_path_predicate { |
| 181 | union { |
| 182 | struct { |
Radek Krejci | 8dc4f2d | 2019-07-16 12:24:00 +0200 | [diff] [blame] | 183 | const struct lysc_node *key; /**< key node of the predicate, in case of the leaf-list-predicate, it is the leaf-list node itself */ |
| 184 | struct lyd_value *value; /**< value representation according to the key's type */ |
| 185 | }; /**< key-value pair for leaf-list-predicate and key-predicate (type 1 and 2) */ |
| 186 | uint64_t position; /**< position value for the position-predicate (type 0) */ |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 187 | }; |
Radek Krejci | 8dc4f2d | 2019-07-16 12:24:00 +0200 | [diff] [blame] | 188 | uint8_t type; /**< Predicate types (see YANG ABNF): 0 - position, 1 - key-predicate, 2 - leaf-list-predicate */ |
| 189 | } *predicates; /**< [Sized array](@ref sizedarrays) of the path segment's predicates */ |
| 190 | } *target; /**< [Sized array](@ref sizedarrays) of (instance-identifier's) path segments. */ |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 191 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 192 | void *ptr; /**< generic data type structure used to store the data */ |
Radek Krejci | 8dc4f2d | 2019-07-16 12:24:00 +0200 | [diff] [blame] | 193 | }; /**< The union is just a list of shorthands to possible values stored by a type's plugin. libyang itself uses the lyd_value::realtype |
| 194 | plugin's callbacks to work with the data. */ |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 195 | |
Radek Krejci | 950f6a5 | 2019-09-12 17:15:32 +0200 | [diff] [blame] | 196 | struct lysc_type *realtype; /**< pointer to the real type of the data stored in the value structure. This type can differ from the type |
Radek Krejci | 62903c3 | 2019-07-15 14:42:05 +0200 | [diff] [blame] | 197 | in the schema node of the data node since the type's store plugin can use other types/plugins for |
| 198 | storing data. Speaking about built-in types, this is the case of leafref which stores data as its |
| 199 | target type. In contrast, union type also use its subtype's callbacks, but inside an internal data |
| 200 | lyd_value::subvalue structure, so here is the pointer to the union type. |
| 201 | In general, this type is used to get free callback for this lyd_value structure, so it must reflect |
| 202 | the type used to store data directly in the same lyd_value instance. */ |
Radek Krejci | 950f6a5 | 2019-09-12 17:15:32 +0200 | [diff] [blame] | 203 | void *canonical_cache; /**< Generic cache for type plugins to store data necessary to print canonical value. It can be the canonical |
| 204 | value itself or anything else useful to print the canonical form of the value. Plugin is responsible for |
| 205 | freeing the cache in its free callback. */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 206 | }; |
| 207 | |
| 208 | /** |
| 209 | * @brief Attribute structure. |
| 210 | * |
| 211 | * The structure provides information about attributes of a data element. Such attributes must map to |
| 212 | * annotations as specified in RFC 7952. The only exception is the filter type (in NETCONF get operations) |
| 213 | * and edit-config's operation attributes. In XML, they are represented as standard XML attributes. In JSON, |
| 214 | * they are represented as JSON elements starting with the '@' character (for more information, see the |
| 215 | * YANG metadata RFC. |
| 216 | * |
| 217 | */ |
| 218 | struct lyd_attr { |
| 219 | struct lyd_node *parent; /**< data node where the attribute is placed */ |
| 220 | struct lyd_attr *next; /**< pointer to the next attribute of the same element */ |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 221 | struct lysc_ext_instance *annotation; /**< pointer to the attribute/annotation's definition */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 222 | const char *name; /**< attribute name */ |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 223 | struct lyd_value value; /**< attribute's value representation */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 224 | }; |
| 225 | |
| 226 | |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 227 | #define LYD_NODE_INNER (LYS_CONTAINER|LYS_LIST|LYS_ACTION|LYS_NOTIF) /**< Schema nodetype mask for lyd_node_inner */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 228 | #define LYD_NODE_TERM (LYS_LEAF|LYS_LEAFLIST) /**< Schema nodetype mask for lyd_node_term */ |
| 229 | #define LYD_NODE_ANY (LYS_ANYDATA) /**< Schema nodetype mask for lyd_node_any */ |
| 230 | |
| 231 | /** |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 232 | * @defgroup dnodeflags Data nodes flags |
| 233 | * @ingroup datatree |
| 234 | * @{ |
| 235 | * |
| 236 | * Various flags of data nodes. |
| 237 | * |
| 238 | * 1 - container 5 - anydata/anyxml |
| 239 | * 2 - list 6 - rpc/action |
| 240 | * 3 - leaf 7 - notification |
| 241 | * 4 - leaflist |
| 242 | * |
| 243 | * bit name 1 2 3 4 5 6 7 |
| 244 | * ---------------------+-+-+-+-+-+-+-+ |
| 245 | * 1 LYD_DEFAULT |x| |x|x| | | | |
| 246 | * +-+-+-+-+-+-+-+ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 247 | * 2 LYD_WHEN_TRUE |x|x|x|x|x| | | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 248 | * ---------------------+-+-+-+-+-+-+-+ |
| 249 | * |
| 250 | */ |
| 251 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 252 | #define LYD_DEFAULT 0x01 /**< default (implicit) node */ |
| 253 | #define LYD_WHEN_TRUE 0x02 /**< all when conditions of this node were evaluated to true */ |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 254 | /** @} */ |
| 255 | |
| 256 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 257 | * @brief Generic structure for a data node. |
| 258 | */ |
| 259 | struct lyd_node { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 260 | uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or |
| 261 | hashes of all nodes of subtree in case of keyless list). Note that while hash can be |
| 262 | used to get know that nodes are not equal, it cannot be used to decide that the |
| 263 | nodes are equal due to possible collisions. */ |
| 264 | uint32_t flags; /**< [data node flags](@ref dnodeflags) */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 265 | const struct lysc_node *schema; /**< pointer to the schema definition of this node, note that the target can be not just |
| 266 | ::struct lysc_node but ::struct lysc_action or ::struct lysc_notif as well */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 267 | struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 268 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 269 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 270 | never NULL. If there is no sibling node, pointer points to the node |
| 271 | itself. In case of the first node, this pointer points to the last |
| 272 | node in the list. */ |
| 273 | struct lyd_attr *attr; /**< pointer to the list of attributes of this node */ |
| 274 | |
| 275 | #ifdef LY_ENABLED_LYD_PRIV |
| 276 | void *priv; /**< private user data, not used by libyang */ |
| 277 | #endif |
| 278 | }; |
| 279 | |
| 280 | /** |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 281 | * @brief Data node structure for the inner data tree nodes - containers, lists, RPCs, actions and Notifications. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 282 | */ |
| 283 | struct lyd_node_inner { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 284 | uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or |
| 285 | hashes of all nodes of subtree in case of keyless list). Note that while hash can be |
| 286 | used to get know that nodes are not equal, it cannot be used to decide that the |
| 287 | nodes are equal due to possible collisions. */ |
| 288 | uint32_t flags; /**< [data node flags](@ref dnodeflags) */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 289 | const struct lysc_node *schema; /**< pointer to the schema definition of this node */ |
| 290 | struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 291 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 292 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 293 | never NULL. If there is no sibling node, pointer points to the node |
| 294 | itself. In case of the first node, this pointer points to the last |
| 295 | node in the list. */ |
| 296 | struct lyd_attr *attr; /**< pointer to the list of attributes of this node */ |
| 297 | |
| 298 | #ifdef LY_ENABLED_LYD_PRIV |
| 299 | void *priv; /**< private user data, not used by libyang */ |
| 300 | #endif |
| 301 | |
| 302 | struct lyd_node *child; /**< pointer to the first child node. */ |
| 303 | struct hash_table *children_ht; /**< hash table with all the direct children (except keys for a list, lists without keys) */ |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 304 | #define LYD_HT_MIN_ITEMS 4 /**< minimal number of children to create lyd_node_inner::children_ht hash table. */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 305 | }; |
| 306 | |
| 307 | /** |
| 308 | * @brief Data node structure for the terminal data tree nodes - leafs and leaf-lists. |
| 309 | */ |
| 310 | struct lyd_node_term { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 311 | uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or |
| 312 | hashes of all nodes of subtree in case of keyless list). Note that while hash can be |
| 313 | used to get know that nodes are not equal, it cannot be used to decide that the |
| 314 | nodes are equal due to possible collisions. */ |
| 315 | uint32_t flags; /**< [data node flags](@ref dnodeflags) */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 316 | const struct lysc_node *schema; /**< pointer to the schema definition of this node */ |
| 317 | struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 318 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 319 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 320 | never NULL. If there is no sibling node, pointer points to the node |
| 321 | itself. In case of the first node, this pointer points to the last |
| 322 | node in the list. */ |
| 323 | struct lyd_attr *attr; /**< pointer to the list of attributes of this node */ |
| 324 | |
| 325 | #ifdef LY_ENABLED_LYD_PRIV |
| 326 | void *priv; /**< private user data, not used by libyang */ |
| 327 | #endif |
| 328 | |
| 329 | struct lyd_value value; /**< node's value representation */ |
| 330 | }; |
| 331 | |
| 332 | /** |
| 333 | * @brief Data node structure for the anydata data tree nodes - anydatas and anyxmls. |
| 334 | */ |
| 335 | struct lyd_node_any { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 336 | uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or |
| 337 | hashes of all nodes of subtree in case of keyless list). Note that while hash can be |
| 338 | used to get know that nodes are not equal, it cannot be used to decide that the |
| 339 | nodes are equal due to possible collisions. */ |
| 340 | uint32_t flags; /**< [data node flags](@ref dnodeflags) */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 341 | const struct lysc_node *schema; /**< pointer to the schema definition of this node */ |
| 342 | struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 343 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 344 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 345 | never NULL. If there is no sibling node, pointer points to the node |
| 346 | itself. In case of the first node, this pointer points to the last |
| 347 | node in the list. */ |
| 348 | struct lyd_attr *attr; /**< pointer to the list of attributes of this node */ |
| 349 | |
| 350 | #ifdef LY_ENABLED_LYD_PRIV |
| 351 | void *priv; /**< private user data, not used by libyang */ |
| 352 | #endif |
| 353 | |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 354 | union { |
| 355 | struct lyd_node *tree; /**< data tree */ |
| 356 | const char *str; /**< Generic string data */ |
| 357 | const char *xml; /**< Serialized XML data */ |
| 358 | const char *json; /**< I-JSON encoded string */ |
| 359 | char *mem; /**< LYD_ANYDATA_LYB memory chunk */ |
| 360 | } value; /**< pointer to the stored value representation of the anydata/anyxml node */ |
| 361 | LYD_ANYDATA_VALUETYPE value_type;/**< type of the data stored as lyd_node_any::value */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 362 | }; |
| 363 | |
| 364 | /** |
| 365 | * @defgroup dataparseroptions Data parser options |
| 366 | * @ingroup datatree |
| 367 | * |
| 368 | * Various options to change the data tree parsers behavior. |
| 369 | * |
| 370 | * Default behavior: |
| 371 | * - in case of XML, parser reads all data from its input (file, memory, XML tree) including the case of not well-formed |
| 372 | * XML document (multiple top-level elements) and if there is an unknown element, it is skipped including its subtree |
| 373 | * (see the next point). This can be changed by the #LYD_OPT_NOSIBLINGS option which make parser to read only a single |
| 374 | * tree (with a single root element) from its input. |
| 375 | * - parser silently ignores the data without a matching node in schema trees. If the caller want to stop |
| 376 | * parsing in case of presence of unknown data, the #LYD_OPT_STRICT can be used. The strict mode is useful for |
| 377 | * NETCONF servers, since NETCONF clients should always send data according to the capabilities announced by the server. |
| 378 | * On the other hand, the default non-strict mode is useful for clients receiving data from NETCONF server since |
| 379 | * clients are not required to understand everything the server does. Of course, the optimal strategy for clients is |
| 380 | * to use filtering to get only the required data. Having an unknown element of the known namespace is always an error. |
| 381 | * The behavior can be changed by #LYD_OPT_STRICT option. |
| 382 | * - using obsolete statements (status set to obsolete) just generates a warning, but the processing continues. The |
| 383 | * behavior can be changed by #LYD_OPT_OBSOLETE option. |
| 384 | * - parser expects that the provided data provides complete datastore content (both the configuration and state data) |
| 385 | * and performs data validation according to all YANG rules. This can be a problem in case of representing NETCONF's |
| 386 | * subtree filter data, edit-config's data or other type of data set - such data do not represent a complete data set |
| 387 | * and some of the validation rules can fail. Therefore there are other options (within lower 8 bits) to make parser |
| 388 | * to accept such a data. |
| 389 | * - when parser evaluates when-stmt condition to false, a validation error is raised. If the |
| 390 | * #LYD_OPT_WHENAUTODEL is used, the invalid node is silently removed instead of an error. The option (and also this default |
| 391 | * behavior) takes effect only in case of #LYD_OPT_DATA or #LYD_OPT_CONFIG type of data. |
| 392 | * @{ |
| 393 | */ |
| 394 | |
| 395 | #define LYD_OPT_DATA 0x00 /**< Default type of data - complete datastore content with configuration as well as |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame^] | 396 | state data. */ |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 397 | #define LYD_OPT_CONFIG LYD_OPT_NO_STATE /**< A configuration datastore - complete datastore without state data. */ |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame^] | 398 | #define LYD_OPT_GET LYD_OPT_PARSE_ONLY /**< Data content from a NETCONF reply message to the NETCONF |
| 399 | \<get\> operation. */ |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 400 | #define LYD_OPT_GETCONFIG LYD_OPT_PARSE_ONLY | LYD_OPT_NO_STATE /**< Data content from a NETCONF reply message to |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame^] | 401 | the NETCONF \<get-config\> operation. */ |
| 402 | #define LYD_OPT_EDIT LYD_OPT_PARSE_ONLY | LYD_OPT_NO_STATE | LYD_OPT_EMPTY_INST /**< Content of |
| 403 | the NETCONF \<edit-config\>'s config element. */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 404 | |
Michal Vasko | acd83e7 | 2020-02-04 14:12:01 +0100 | [diff] [blame] | 405 | #define LYD_OPT_STRICT 0x0001 /**< Instead of silent ignoring data without schema definition raise an error. */ |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 406 | #define LYD_OPT_PARSE_ONLY 0x0002 /**< Data will be only parsed and no (only required) validation will be performed. */ |
| 407 | #define LYD_OPT_NO_STATE 0x0004 /**< Consider state data not allowed and raise an error if they are found. */ |
| 408 | #define LYD_OPT_EMPTY_INST 0x0008 /**< Allow leaf/leaf-list instances without values and lists without keys. */ |
| 409 | #define LYD_OPT_VAL_DATA_ONLY 0x0010 /**< Validate only modules whose data actually exist. */ |
| 410 | //#define LYD_OPT_DESTRUCT 0x0400 /**< Free the provided XML tree during parsing the data. With this option, the |
| 411 | // provided XML tree is affected and all successfully parsed data are freed. |
| 412 | // This option is applicable only to lyd_parse_xml() function. */ |
| 413 | //#define LYD_OPT_OBSOLETE 0x0800 /**< Raise an error when an obsolete statement (status set to obsolete) is used. */ |
| 414 | //#define LYD_OPT_NOSIBLINGS 0x1000 /**< Parse only a single XML tree from the input. This option applies only to |
| 415 | // XML input data. */ |
| 416 | //#define LYD_OPT_VAL_DIFF 0x40000 /**< Flag only for validation, store all the data node changes performed by the validation |
| 417 | // in a diff structure. */ |
| 418 | //#define LYD_OPT_DATA_TEMPLATE 0x1000000 /**< Data represents YANG data template. */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 419 | |
| 420 | /**@} dataparseroptions */ |
| 421 | |
| 422 | /** |
| 423 | * @brief Get the node's children list if any. |
| 424 | * |
| 425 | * Decides the node's type and in case it has a children list, returns it. |
| 426 | * @param[in] node Node to check. |
| 427 | * @return Pointer to the first child node (if any) of the \p node. |
| 428 | */ |
| 429 | const struct lyd_node *lyd_node_children(const struct lyd_node *node); |
| 430 | |
| 431 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 432 | * @brief Parse (and validate) data from memory. |
| 433 | * |
| 434 | * In case of LY_XML format, the data string is parsed completely. It means that when it contains |
| 435 | * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The |
| 436 | * returned data node is a root of the first tree with other trees connected via the next pointer. |
| 437 | * This behavior can be changed by #LYD_OPT_NOSIBLINGS option. |
| 438 | * |
| 439 | * @param[in] ctx Context to connect with the data tree being built here. |
| 440 | * @param[in] data Serialized data in the specified format. |
| 441 | * @param[in] format Format of the input data to be parsed. |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 442 | * @param[in] options Parser options, see @ref parseroptions. \p format LYD_LYB uses #LYD_OPT_PARSE_ONLY implicitly. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 443 | * @return Pointer to the built data tree or NULL in case of empty \p data. To free the returned structure, |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 444 | * use lyd_free_all(). |
| 445 | * @return NULL in case of error. The error information can be then obtained using ly_err* functions. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 446 | */ |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 447 | struct lyd_node *lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 448 | |
| 449 | /** |
| 450 | * @brief Read (and validate) data from the given file descriptor. |
| 451 | * |
| 452 | * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc. |
| 453 | * |
| 454 | * In case of LY_XML format, the file content is parsed completely. It means that when it contains |
| 455 | * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The |
| 456 | * returned data node is a root of the first tree with other trees connected via the next pointer. |
| 457 | * This behavior can be changed by #LYD_OPT_NOSIBLINGS option. |
| 458 | * |
| 459 | * @param[in] ctx Context to connect with the data tree being built here. |
| 460 | * @param[in] fd The standard file descriptor of the file containing the data tree in the specified format. |
| 461 | * @param[in] format Format of the input data to be parsed. |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 462 | * @param[in] options Parser options, see @ref parseroptions. \p format LYD_LYB uses #LYD_OPT_PARSE_ONLY implicitly. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 463 | * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure, |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 464 | * use lyd_free_all(). |
| 465 | * @return NULL in case of error. The error information can be then obtained using ly_err* functions. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 466 | */ |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 467 | struct lyd_node *lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 468 | |
| 469 | /** |
| 470 | * @brief Read (and validate) data from the given file path. |
| 471 | * |
| 472 | * In case of LY_XML format, the file content is parsed completely. It means that when it contains |
| 473 | * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The |
| 474 | * returned data node is a root of the first tree with other trees connected via the next pointer. |
| 475 | * This behavior can be changed by #LYD_OPT_NOSIBLINGS option. |
| 476 | * |
| 477 | * @param[in] ctx Context to connect with the data tree being built here. |
| 478 | * @param[in] path Path to the file containing the data tree in the specified format. |
| 479 | * @param[in] format Format of the input data to be parsed. |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 480 | * @param[in] options Parser options, see @ref parseroptions. \p format LYD_LYB uses #LYD_OPT_PARSE_ONLY implicitly. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 481 | * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure, |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 482 | * use lyd_free_all(). |
| 483 | * @return NULL in case of error. The error information can be then obtained using ly_err* functions. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 484 | */ |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 485 | struct lyd_node *lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 486 | |
| 487 | /** |
Radek Krejci | b0849a2 | 2019-07-25 12:31:04 +0200 | [diff] [blame] | 488 | * @brief Free all the nodes (even parents of the node) in the data tree. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 489 | * |
| 490 | * @param[in] node Any of the nodes inside the tree. |
| 491 | */ |
| 492 | void lyd_free_all(struct lyd_node *node); |
| 493 | |
| 494 | /** |
Radek Krejci | b0849a2 | 2019-07-25 12:31:04 +0200 | [diff] [blame] | 495 | * @brief Free all the sibling nodes. |
| 496 | * |
| 497 | * @param[in] node Any of the sibling nodes to free. |
| 498 | */ |
| 499 | void lyd_free_withsiblings(struct lyd_node *node); |
| 500 | |
| 501 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 502 | * @brief Free (and unlink) the specified data (sub)tree. |
| 503 | * |
| 504 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
| 505 | * |
| 506 | * @param[in] node Root of the (sub)tree to be freed. |
| 507 | */ |
| 508 | void lyd_free_tree(struct lyd_node *node); |
| 509 | |
| 510 | /** |
| 511 | * @brief Unlink the specified data subtree. All referenced namespaces are copied. |
| 512 | * |
| 513 | * Note, that the node's connection with the schema tree is kept. Therefore, in case of |
| 514 | * reconnecting the node to a data tree using lyd_paste() it is necessary to paste it |
| 515 | * to the appropriate place in the data tree following the schema. |
| 516 | * |
| 517 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
| 518 | * |
| 519 | * @param[in] node Data tree node to be unlinked (together with all children). |
| 520 | * @return LY_SUCCESS for success |
| 521 | * @return LY_E* values in case of error |
| 522 | */ |
| 523 | LY_ERR lyd_unlink_tree(struct lyd_node *node); |
| 524 | |
| 525 | /** |
| 526 | * @brief Destroy data attribute. |
| 527 | * |
| 528 | * @param[in] ctx Context where the attribute was created. |
| 529 | * @param[in] attr Attribute to destroy |
| 530 | * @param[in] recursive Zero to destroy only the attribute (the attribute list is corrected), |
| 531 | * non-zero to destroy also all the subsequent attributes in the list. |
| 532 | */ |
| 533 | void lyd_free_attr(struct ly_ctx *ctx, struct lyd_attr *attr, int recursive); |
| 534 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 535 | /** |
Radek Krejci | 576b23f | 2019-07-12 14:06:32 +0200 | [diff] [blame] | 536 | * @brief Prepare ([sized array](@ref sizedarrays)) of data trees required by various (mostly validation) functions. |
| 537 | * |
| 538 | * @param[in] count Number of trees to include (including the mandatory @p tree). |
| 539 | * @param[in] tree First (and mandatory) tree to be included into the resulting ([sized array](@ref sizedarrays)). |
| 540 | * @return NULL in case of memory allocation failure or invalid argument, prepared ([sized array](@ref sizedarrays)) otherwise. |
| 541 | */ |
| 542 | const struct lyd_node **lyd_trees_new(size_t count, const struct lyd_node *tree, ...); |
| 543 | |
| 544 | /** |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 545 | * @brief Add tree into the ([sized array](@ref sizedarrays)) of data trees created by lyd_trees_new(), |
| 546 | * |
| 547 | * @param[in] trees Existing [sized array](@ref sizedarrays)) of data trees to be extended. |
| 548 | * @param[in] tree Data tree to be included into the provided @p trees ([sized array](@ref sizedarrays)). |
| 549 | * @return NULL in case of memory allocation failure or invalid argument, extended @p trees ([sized array](@ref sizedarrays)) otherwise. |
| 550 | */ |
| 551 | const struct lyd_node **lyd_trees_add(const struct lyd_node **trees, const struct lyd_node *tree); |
| 552 | |
| 553 | /** |
Radek Krejci | 576b23f | 2019-07-12 14:06:32 +0200 | [diff] [blame] | 554 | * @brief Free the trees ([sized array](@ref sizedarrays)). |
| 555 | * |
| 556 | * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees. |
| 557 | * @param[in] free_data Flag to free also the particular trees in the @p trees ([sized array](@ref sizedarrays)). |
| 558 | * If set to zero, only the trees envelope is freed and data are untouched. |
| 559 | */ |
| 560 | void lyd_trees_free(const struct lyd_node **trees, int free_data); |
| 561 | |
| 562 | /** |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 563 | * @brief Check type restrictions applicable to the particular leaf/leaf-list with the given string @p value. |
| 564 | * |
| 565 | * The given node is not modified in any way - it is just checked if the @p value can be set to the node. |
| 566 | * |
| 567 | * If there is no data node instance and you are fine with checking just the type's restrictions without the |
| 568 | * data tree context (e.g. for the case of require-instance restriction), use lys_value_validate(). |
| 569 | * |
| 570 | * @param[in] ctx libyang context for logging (function does not log errors when @p ctx is NULL) |
| 571 | * @param[in] node Data node for the @p value. |
| 572 | * @param[in] value String value to be checked. |
| 573 | * @param[in] value_len Length of the given @p value (mandatory). |
| 574 | * @param[in] get_prefix Callback function to resolve prefixes used in the @p value string. |
| 575 | * @param[in] get_prefix_data Private data for the @p get_prefix callback. |
| 576 | * @param[in] format Input format of the data. |
| 577 | * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees (e.g. when validating RPC/Notification) where the required |
| 578 | * data instance (leafref target, instance-identifier) can be placed. NULL in case the data tree are not yet complete, |
Radek Krejci | 576b23f | 2019-07-12 14:06:32 +0200 | [diff] [blame] | 579 | * then LY_EINCOMPLETE can be returned. To simply prepare this structure, use lyd_trees_new(). |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 580 | * @return LY_SUCCESS on success |
| 581 | * @return LY_EINCOMPLETE in case the @p trees is not provided and it was needed to finish the validation (e.g. due to require-instance). |
| 582 | * @return LY_ERR value if an error occurred. |
| 583 | */ |
| 584 | LY_ERR lyd_value_validate(struct ly_ctx *ctx, const struct lyd_node_term *node, const char *value, size_t value_len, |
Radek Krejci | 576b23f | 2019-07-12 14:06:32 +0200 | [diff] [blame] | 585 | ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format, const struct lyd_node **trees); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 586 | |
| 587 | /** |
| 588 | * @brief Compare the node's value with the given string value. The string value is first validated according to the node's type. |
| 589 | * |
| 590 | * @param[in] node Data node to compare. |
| 591 | * @param[in] value String value to be compared. It does not need to be in a canonical form - as part of the process, |
| 592 | * it is validated and canonized if possible. |
| 593 | * @param[in] value_len Length of the given @p value (mandatory). |
| 594 | * @param[in] get_prefix Callback function to resolve prefixes used in the @p value string. |
| 595 | * @param[in] get_prefix_data Private data for the @p get_prefix callback. |
| 596 | * @param[in] format Input format of the data. |
| 597 | * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees (e.g. when validating RPC/Notification) where the required |
| 598 | * data instance (leafref target, instance-identifier) can be placed. NULL in case the data tree are not yet complete, |
Radek Krejci | 576b23f | 2019-07-12 14:06:32 +0200 | [diff] [blame] | 599 | * then LY_EINCOMPLETE can be returned in case the validation was not completed, but values matches. To simply prepare |
| 600 | * this structure, use lyd_trees_new(). To simply prepare this structure, use lyd_trees_new(). |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 601 | * @return LY_SUCCESS on success |
| 602 | * @return LY_EINCOMPLETE in case of success when the @p trees is not provided and it was needed to finish the validation of |
| 603 | * the given string @p value (e.g. due to require-instance). |
| 604 | * @return LY_ERR value if an error occurred. |
| 605 | */ |
| 606 | LY_ERR lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len, |
Radek Krejci | 576b23f | 2019-07-12 14:06:32 +0200 | [diff] [blame] | 607 | ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format, const struct lyd_node **trees); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 608 | |
Radek Krejci | 576b23f | 2019-07-12 14:06:32 +0200 | [diff] [blame] | 609 | /** |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 610 | * @defgroup datacompareoptions Data compare options |
| 611 | * @ingroup datatree |
| 612 | * |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 613 | * Various options to change the lyd_compare() behavior. |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 614 | */ |
| 615 | #define LYD_COMPARE_FULL_RECURSION 0x01 /* lists and containers are the same only in case all they children |
| 616 | (subtree, so direct as well as indirect children) are the same. By default, |
| 617 | containers are the same in case of the same schema node and lists are the same |
| 618 | in case of equal keys (keyless lists do the full recursion comparison all the time). */ |
| 619 | #define LYD_COMPARE_DEFAULTS 0x02 /* By default, implicit and explicit default nodes are considered to be equal. This flag |
| 620 | changes this behavior and implicit (automatically created default node) and explicit |
| 621 | (explicitly created node with the default value) default nodes are considered different. */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 622 | /**@} datacompareoptions */ |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 623 | |
| 624 | /** |
| 625 | * @brief Compare 2 data nodes if they are equivalent. |
| 626 | * |
| 627 | * @param[in] node1 The first node to compare. |
| 628 | * @param[in] node2 The second node to compare. |
Radek Krejci | c5ad965 | 2019-09-11 11:31:51 +0200 | [diff] [blame] | 629 | * @param[in] options Various @ref datacompareoptions. |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 630 | * @return LY_SUCCESS if the nodes are equivalent. |
| 631 | * @return LY_ENOT if the nodes are not equivalent. |
| 632 | */ |
| 633 | LY_ERR lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options); |
| 634 | |
| 635 | /** |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 636 | * @defgroup dupoptions Data duplication options |
| 637 | * @ingroup datatree |
| 638 | * |
| 639 | * Various options to change lyd_dup() behavior. |
| 640 | * |
| 641 | * Default behavior: |
| 642 | * - only the specified node is duplicated without siblings, parents, or children. |
| 643 | * - all the attributes of the duplicated nodes are also duplicated. |
| 644 | * @{ |
| 645 | */ |
| 646 | |
| 647 | #define LYD_DUP_RECURSIVE 0x01 /**< Duplicate not just the node but also all the children. Note that |
| 648 | list's keys are always duplicated. */ |
| 649 | #define LYD_DUP_NO_ATTR 0x02 /**< Do not duplicate attributes of any node. */ |
| 650 | #define LYD_DUP_WITH_PARENTS 0x04 /**< If a nested node is being duplicated, duplicate also all the parents. |
| 651 | Keys are also duplicated for lists. Return value does not change! */ |
| 652 | #define LYD_DUP_WITH_SIBLINGS 0x08 /**< Duplicate also all the sibling of the given node. */ |
| 653 | #define LYD_DUP_WITH_WHEN 0x10 /**< Also copy any when evaluation state flags. This is useful in case the copied |
| 654 | nodes are actually still part of the same datastore meaning no dependency data |
| 655 | could have changed. Otherwise nothing is assumed about the copied node when |
| 656 | state and it is evaluated from scratch during validation. */ |
| 657 | |
| 658 | /** @} dupoptions */ |
| 659 | |
| 660 | /** |
| 661 | * @brief Create a copy of the specified data tree \p node. Schema references are kept the same. |
| 662 | * |
| 663 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
| 664 | * |
| 665 | * @param[in] node Data tree node to be duplicated. |
| 666 | * @param[in] parent Optional parent node where to connect the duplicated node(s). |
| 667 | * If set in combination with LYD_DUP_WITH_PARENTS, the parents chain is duplicated until it comes to and connect with the @p parent |
| 668 | * (if the parents chain does not match at some node the schema node of the provided @p parent, duplication fails). |
| 669 | * @param[in] options Bitmask of options flags, see @ref dupoptions. |
| 670 | * @return Created copy of the provided data \p node (the first of the duplicated siblings when LYD_DUP_WITH_SIBLINGS used). |
| 671 | * Note that in case the parents chain is duplicated for the duplicated node(s) (when LYD_DUP_WITH_PARENTS used), the first duplicated node |
| 672 | * is still returned, not a pointer to the duplicated parents. |
| 673 | */ |
| 674 | struct lyd_node *lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options); |
| 675 | |
| 676 | /** |
Radek Krejci | 576b23f | 2019-07-12 14:06:32 +0200 | [diff] [blame] | 677 | * @brief Resolve instance-identifier defined by lyd_value_path structure. |
| 678 | * |
| 679 | * @param[in] path Path structure specifying the instance-identifier target. |
| 680 | * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees to be searched. |
| 681 | * To simply prepare this structure, use lyd_trees_new(). |
| 682 | * @return Target node of the instance-identifier present in the given data @p trees. |
| 683 | */ |
| 684 | const struct lyd_node_term *lyd_target(struct lyd_value_path *path, const struct lyd_node **trees); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 685 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 686 | /** |
| 687 | * @brief Get string value of a term data \p node. |
| 688 | * |
| 689 | * @param[in] node Data tree node with the value. |
| 690 | * @param[out] dynamic Whether the string value was dynmically allocated. |
| 691 | * @return String value of @p node, if @p dynamic, needs to be freed. |
| 692 | */ |
| 693 | const char *lyd_value2str(const struct lyd_node_term *node, int *dynamic); |
| 694 | |
| 695 | /** |
| 696 | * @brief Get string value of an attribute \p attr. |
| 697 | * |
| 698 | * @param[in] attr Attribute with the value. |
| 699 | * @param[out] dynamic Whether the string value was dynmically allocated. |
| 700 | * @return String value of @p attr, if @p dynamic, needs to be freed. |
| 701 | */ |
| 702 | const char *lyd_attr2str(const struct lyd_attr *attr, int *dynamic); |
| 703 | |
| 704 | /** |
| 705 | * @brief Types of the different data paths. |
| 706 | */ |
| 707 | typedef enum { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 708 | LYD_PATH_LOG, /**< Descriptive path format used in log messages */ |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 709 | } LYD_PATH_TYPE; |
| 710 | |
| 711 | /** |
| 712 | * @brief Generate path of the given node in the requested format. |
| 713 | * |
| 714 | * @param[in] node Schema path of this node will be generated. |
| 715 | * @param[in] pathtype Format of the path to generate. |
| 716 | * @param[in,out] buffer Prepared buffer of the @p buflen length to store the generated path. |
| 717 | * If NULL, memory for the complete path is allocated. |
| 718 | * @param[in] buflen Size of the provided @p buffer. |
| 719 | * @return NULL in case of memory allocation error, path of the node otherwise. |
| 720 | * In case the @p buffer is NULL, the returned string is dynamically allocated and caller is responsible to free it. |
| 721 | */ |
| 722 | char *lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen); |
| 723 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame^] | 724 | /** |
| 725 | * @brief Search in the given siblings for instances of the provided schema node, recursively. |
| 726 | * Does **not** use hashes - should not be used unless necessary for best performance. |
| 727 | * |
| 728 | * If \p sibling is the first top-level sibling, the whole data tree is effectively searched. |
| 729 | * |
| 730 | * @param[in] sibling Starting data sibling for the search. |
| 731 | * @param[in] schema Schema node of the data nodes caller wants to find. |
| 732 | * @return Set of found data nodes. If no data node is found, the returned set is empty. |
| 733 | * @return NULL in case of error. |
| 734 | */ |
| 735 | struct ly_set *lyd_find_instance(const struct lyd_node *sibling, const struct lysc_node *schema); |
| 736 | |
| 737 | /** |
| 738 | * @brief Find the node, in the list, satisfying the given restrictions. |
| 739 | * Does **not** use hashes - should not be used unless necessary for best performance. |
| 740 | * |
| 741 | * @param[in] first Starting sibling node for search, only succeeding ones are searched. |
| 742 | * @param[in] module Module of the node to find. |
| 743 | * @param[in] name Name of the node to find. |
| 744 | * @param[in] name_len Optional length of the @p name argument in case it is not NULL-terminated string. |
| 745 | * @param[in] key_or_value Expected value depends on the type of @p name node: |
| 746 | * LYS_CONTAINER: |
| 747 | * LYS_ANYXML: |
| 748 | * LYS_ANYDATA: |
| 749 | * LYS_NOTIF: |
| 750 | * LYS_RPC: |
| 751 | * LYS_ACTION: |
| 752 | * NULL should be always set, will be ignored. |
| 753 | * LYS_LEAF: |
| 754 | * LYS_LEAFLIST: |
| 755 | * Optional restriction on the specific leaf(-list) value. Can be set only if the node |
| 756 | * has a canonical value! |
| 757 | * LYS_LIST: |
| 758 | * Optional keys values of the matching list instances in the form of "[key1='val1'][key2='val2']...". |
| 759 | * The keys do not have to be ordered and not all keys need to be specified, but they must have |
| 760 | * a canonical value! |
| 761 | * @param[in] val_len Optional length of the @p key_or_value argument in case it is not NULL-terminated string. |
| 762 | * @param[out] match Found data node. |
| 763 | * @return LY_SUCCESS on success, @p match set. |
| 764 | * @return LY_ENOTFOUND if not found, @p match set to NULL. |
| 765 | * @return LY_ERR value if another error occurred. |
| 766 | */ |
| 767 | LY_ERR lyd_find_sibling_next(const struct lyd_node *first, const struct lys_module *module, const char *name, |
| 768 | size_t name_len, const char *key_or_value, size_t val_len, struct lyd_node **match); |
| 769 | |
| 770 | /** |
| 771 | * @brief Search in the given siblings (NOT recursively) for the first target instance. |
| 772 | * Uses hashes - should be used whenever possible for best performance. |
| 773 | * |
| 774 | * @param[in] siblings Siblings to search in including preceding and succeeding nodes. |
| 775 | * @param[in] target Target node to find. |
| 776 | * @param[out] match Found data node. |
| 777 | * @return LY_SUCCESS on success, @p match set. |
| 778 | * @return LY_ENOTFOUND if not found, @p match set to NULL. |
| 779 | * @return LY_ERR value if another error occurred. |
| 780 | */ |
| 781 | LY_ERR lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match); |
| 782 | |
| 783 | /** |
| 784 | * @brief Search in the given siblings for all target instances. |
| 785 | * Uses hashes - should be used whenever possible for best performance. |
| 786 | * |
| 787 | * @param[in] siblings Siblings to search in including preceding and succeeding nodes. |
| 788 | * @param[in] target Target node to find. Key-less lists are compared based on |
| 789 | * all its descendants (both direct and indirect). |
| 790 | * @param[out] set Found nodes in a set in case of success. |
| 791 | * @return LY_SUCCESS on success. |
| 792 | * @return LY_ENOTFOUND if no matching siblings found. |
| 793 | * @return LY_ERR value if another error occurred. |
| 794 | */ |
| 795 | LY_ERR lyd_find_sibling_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set); |
| 796 | |
| 797 | /** |
| 798 | * @brief Search in the given siblings for the first schema instance. |
| 799 | * Uses hashes - should be used whenever possible for best performance. |
| 800 | * |
| 801 | * @param[in] siblings Siblings to search in including preceding and succeeding nodes. |
| 802 | * @param[in] schema Schema node of the data node to find. |
| 803 | * @param[in] key_or_value Expected value depends on the type of \p schema: |
| 804 | * LYS_CONTAINER: |
| 805 | * LYS_LEAF: |
| 806 | * LYS_ANYXML: |
| 807 | * LYS_ANYDATA: |
| 808 | * LYS_NOTIF: |
| 809 | * LYS_RPC: |
| 810 | * LYS_ACTION: |
| 811 | * NULL should be always set, will be ignored. |
| 812 | * LYS_LEAFLIST: |
| 813 | * Searched instance value. |
| 814 | * LYS_LIST: |
| 815 | * Searched instance all key values in the form of "[key1='val1'][key2='val2']...". |
| 816 | * The keys do not have to be ordered. |
| 817 | * @param[out] match Found data node. |
| 818 | * @return LY_SUCCESS on success, @p match set. |
| 819 | * @return LY_ENOTFOUND if not found, @p match set to NULL. |
| 820 | * @return LY_EINVAL if @p schema is a key-less list. |
| 821 | * @return LY_ERR value if another error occurred. |
| 822 | */ |
| 823 | LY_ERR lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value, |
| 824 | size_t val_len, struct lyd_node **match); |
| 825 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 826 | #ifdef __cplusplus |
| 827 | } |
| 828 | #endif |
| 829 | |
| 830 | #endif /* LY_TREE_DATA_H_ */ |