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" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 23 | #ifdef __cplusplus |
| 24 | extern "C" { |
| 25 | #endif |
| 26 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 27 | struct ly_ctx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 28 | struct ly_path; |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 29 | struct ly_set; |
| 30 | struct lyd_node; |
| 31 | struct lyd_node_opaq; |
| 32 | struct lys_module; |
| 33 | struct lysc_node; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 34 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 35 | /** |
| 36 | * @defgroup datatree Data Tree |
Radek Krejci | 2ff0d57 | 2020-05-21 15:27:28 +0200 | [diff] [blame] | 37 | * @ingroup trees |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 38 | * @{ |
| 39 | * |
| 40 | * Data structures and functions to manipulate and access instance data tree. |
| 41 | */ |
| 42 | |
| 43 | /** |
| 44 | * @brief Macro to iterate via all elements in a data tree. This is the opening part |
| 45 | * to the #LYD_TREE_DFS_END - they always have to be used together. |
| 46 | * |
| 47 | * The function follows deep-first search algorithm: |
| 48 | * <pre> |
| 49 | * 1 |
| 50 | * / \ |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 51 | * 2 4 |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 52 | * / / \ |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 53 | * 3 5 6 |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 54 | * </pre> |
| 55 | * |
Radek Krejci | 0935f41 | 2019-08-20 16:15:18 +0200 | [diff] [blame] | 56 | * 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] | 57 | * START can be any of the lyd_node* types, NEXT and ELEM variables are expected |
| 58 | * to be pointers to a generic struct lyd_node. |
| 59 | * |
| 60 | * Since the next node is selected as part of #LYD_TREE_DFS_END, do not use |
| 61 | * continue statement between the #LYD_TREE_DFS_BEGIN and #LYD_TREE_DFS_END. |
| 62 | * |
| 63 | * Use with opening curly bracket '{' after the macro. |
| 64 | * |
| 65 | * @param START Pointer to the starting element processed first. |
| 66 | * @param NEXT Temporary storage, do not use. |
| 67 | * @param ELEM Iterator intended for use in the block. |
| 68 | */ |
| 69 | #define LYD_TREE_DFS_BEGIN(START, NEXT, ELEM) \ |
| 70 | for ((ELEM) = (NEXT) = (START); \ |
| 71 | (ELEM); \ |
| 72 | (ELEM) = (NEXT)) |
| 73 | |
| 74 | /** |
| 75 | * @brief Macro to iterate via all elements in a tree. This is the closing part |
| 76 | * to the #LYD_TREE_DFS_BEGIN - they always have to be used together. |
| 77 | * |
| 78 | * Use the same parameters for #LYD_TREE_DFS_BEGIN and #LYD_TREE_DFS_END. While |
| 79 | * START can be any of the lyd_node* types, NEXT and ELEM variables are expected |
| 80 | * to be pointers to a generic struct lyd_node. |
| 81 | * |
| 82 | * Use with closing curly bracket '}' after the macro. |
| 83 | * |
| 84 | * @param START Pointer to the starting element processed first. |
| 85 | * @param NEXT Temporary storage, do not use. |
| 86 | * @param ELEM Iterator intended for use in the block. |
| 87 | */ |
| 88 | |
| 89 | #define LYD_TREE_DFS_END(START, NEXT, ELEM) \ |
| 90 | /* select element for the next run - children first */ \ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 91 | (NEXT) = lyd_node_children((struct lyd_node*)ELEM, 0); \ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 92 | if (!(NEXT)) { \ |
| 93 | /* no children */ \ |
| 94 | if ((ELEM) == (struct lyd_node*)(START)) { \ |
| 95 | /* we are done, (START) has no children */ \ |
| 96 | break; \ |
| 97 | } \ |
| 98 | /* try siblings */ \ |
| 99 | (NEXT) = (ELEM)->next; \ |
| 100 | } \ |
| 101 | while (!(NEXT)) { \ |
| 102 | /* parent is already processed, go to its sibling */ \ |
| 103 | (ELEM) = (struct lyd_node*)(ELEM)->parent; \ |
| 104 | /* no siblings, go back through parents */ \ |
| 105 | if ((ELEM)->parent == (START)->parent) { \ |
| 106 | /* we are done, no next element to process */ \ |
| 107 | break; \ |
| 108 | } \ |
| 109 | (NEXT) = (ELEM)->next; \ |
| 110 | } |
| 111 | |
| 112 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 113 | * @brief Macro to get context from a data tree node. |
| 114 | */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 115 | #define LYD_NODE_CTX(node) ((node)->schema ? (node)->schema->module->ctx : ((struct lyd_node_opaq *)(node))->ctx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 116 | |
| 117 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 118 | * @brief Data input/output formats supported by libyang [parser](@ref howtodataparsers) and |
Radek Krejci | d91d4da | 2020-05-18 11:28:02 +0200 | [diff] [blame] | 119 | * [printer](@ref howtodataprinters) functions. Also used for value prefix format (TODO link to prefix formats descriptions). |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 120 | */ |
| 121 | typedef enum { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 122 | LYD_SCHEMA = 0, /**< invalid instance data format, value prefixes map to YANG import prefixes */ |
| 123 | LYD_XML, /**< XML instance data format, value prefixes map to XML namespace prefixes */ |
| 124 | LYD_JSON, /**< JSON instance data format, value prefixes map to module names */ |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 125 | LYD_LYB, /**< LYB instance data format, invalid value prefix format (same as LYD_JSON) */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 126 | } LYD_FORMAT; |
| 127 | |
| 128 | /** |
Radek Krejci | 59583bb | 2019-09-11 12:57:55 +0200 | [diff] [blame] | 129 | * @brief List of possible value types stored in ::lyd_node_any. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 130 | */ |
| 131 | typedef enum { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 132 | 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] | 133 | is directly connected into the anydata node without duplication, caller is supposed to not manipulate |
| 134 | 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] | 135 | 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] | 136 | as string). XML sensitive characters (such as & or \>) are automatically escaped when the anydata |
| 137 | is printed in XML format. */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 138 | LYD_ANYDATA_XML, /**< Value is a string containing the serialized XML data. */ |
| 139 | LYD_ANYDATA_JSON, /**< Value is a string containing the data modeled by YANG and encoded as I-JSON. */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 140 | LYD_ANYDATA_LYB, /**< Value is a memory chunk with the serialized data tree in LYB format. */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 141 | } LYD_ANYDATA_VALUETYPE; |
| 142 | |
| 143 | /** @} */ |
| 144 | |
| 145 | /** |
| 146 | * @brief YANG data representation |
| 147 | */ |
| 148 | struct lyd_value { |
Radek Krejci | 950f6a5 | 2019-09-12 17:15:32 +0200 | [diff] [blame] | 149 | const char *original; /**< Original string representation of the value. It is never NULL, but (canonical) string representation |
| 150 | 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] | 151 | union { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 152 | int8_t boolean; /**< 0 as false, 1 as true */ |
| 153 | int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */ |
Radek Krejci | 8dc4f2d | 2019-07-16 12:24:00 +0200 | [diff] [blame] | 154 | int8_t int8; /**< 8-bit signed integer */ |
| 155 | int16_t int16; /**< 16-bit signed integer */ |
| 156 | int32_t int32; /**< 32-bit signed integer */ |
| 157 | int64_t int64; /**< 64-bit signed integer */ |
| 158 | uint8_t uint8; /**< 8-bit unsigned integer */ |
Michal Vasko | 7c8439f | 2020-08-05 13:25:19 +0200 | [diff] [blame] | 159 | uint16_t uint16; /**< 16-bit unsigned integer */ |
| 160 | uint32_t uint32; /**< 32-bit unsigned integer */ |
| 161 | uint64_t uint64; /**< 64-bit unsigned integer */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 162 | 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] | 163 | 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] | 164 | 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] | 165 | |
| 166 | struct lyd_value_subvalue { |
| 167 | struct lyd_value_prefix { |
| 168 | const char *prefix; /**< prefix string used in the canonized string to identify the mod of the YANG schema */ |
| 169 | const struct lys_module *mod; /**< YANG schema module identified by the prefix string */ |
| 170 | } *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] | 171 | struct lyd_value *value; /**< representation of the value according to the selected union's subtype (stored as lyd_value::realpath |
| 172 | here, in subvalue structure */ |
| 173 | } *subvalue; /**< data to represent data with multiple types (union). Original value is stored in the main |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 174 | lyd_value:canonical_cache while the lyd_value_subvalue::value contains representation according to the |
Radek Krejci | 8dc4f2d | 2019-07-16 12:24:00 +0200 | [diff] [blame] | 175 | one of the union's type. The lyd_value_subvalue:prefixes provides (possible) mappings from prefixes |
| 176 | in original value to YANG modules. These prefixes are necessary to parse original value to the union's |
| 177 | subtypes. */ |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 178 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 179 | struct ly_path *target; /**< Instance-identifier's target path. */ |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 180 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 181 | void *ptr; /**< generic data type structure used to store the data */ |
Radek Krejci | 8dc4f2d | 2019-07-16 12:24:00 +0200 | [diff] [blame] | 182 | }; /**< The union is just a list of shorthands to possible values stored by a type's plugin. libyang itself uses the lyd_value::realtype |
| 183 | plugin's callbacks to work with the data. */ |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 184 | |
Radek Krejci | 950f6a5 | 2019-09-12 17:15:32 +0200 | [diff] [blame] | 185 | 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] | 186 | in the schema node of the data node since the type's store plugin can use other types/plugins for |
| 187 | storing data. Speaking about built-in types, this is the case of leafref which stores data as its |
| 188 | target type. In contrast, union type also use its subtype's callbacks, but inside an internal data |
| 189 | lyd_value::subvalue structure, so here is the pointer to the union type. |
| 190 | In general, this type is used to get free callback for this lyd_value structure, so it must reflect |
| 191 | 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] | 192 | void *canonical_cache; /**< Generic cache for type plugins to store data necessary to print canonical value. It can be the canonical |
| 193 | value itself or anything else useful to print the canonical form of the value. Plugin is responsible for |
| 194 | freeing the cache in its free callback. */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 198 | * @brief Metadata structure. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 199 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 200 | * The structure provides information about metadata of a data element. Such attributes must map to |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 201 | * annotations as specified in RFC 7952. The only exception is the filter type (in NETCONF get operations) |
| 202 | * and edit-config's operation attributes. In XML, they are represented as standard XML attributes. In JSON, |
| 203 | * they are represented as JSON elements starting with the '@' character (for more information, see the |
| 204 | * YANG metadata RFC. |
| 205 | * |
| 206 | */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 207 | struct lyd_meta { |
| 208 | struct lyd_node *parent; /**< data node where the metadata is placed */ |
| 209 | struct lyd_meta *next; /**< pointer to the next metadata of the same element */ |
| 210 | struct lysc_ext_instance *annotation; /**< pointer to the annotation's definition */ |
| 211 | const char *name; /**< metadata name */ |
| 212 | struct lyd_value value; /**< metadata value representation */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 213 | }; |
| 214 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 215 | /** |
| 216 | * @brief Generic prefix and namespace mapping, meaning depends on the format. |
| 217 | */ |
| 218 | struct ly_prefix { |
| 219 | const char *pref; |
| 220 | const char *ns; |
| 221 | }; |
| 222 | |
| 223 | /** |
| 224 | * @brief Generic attribute structure. |
| 225 | */ |
| 226 | struct ly_attr { |
| 227 | struct lyd_node_opaq *parent; /**< data node where the attribute is placed */ |
| 228 | struct ly_attr *next; |
| 229 | struct ly_prefix *val_prefs; /**< list of prefixes in the value ([sized array](@ref sizedarrays)) */ |
| 230 | const char *name; |
| 231 | const char *value; |
| 232 | |
| 233 | LYD_FORMAT format; |
| 234 | struct ly_prefix prefix; /**< name prefix, it is stored because they are a real pain to generate properly */ |
| 235 | |
| 236 | }; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 237 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 238 | #define LYD_NODE_INNER (LYS_CONTAINER|LYS_LIST|LYS_RPC|LYS_ACTION|LYS_NOTIF) /**< Schema nodetype mask for lyd_node_inner */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 239 | #define LYD_NODE_TERM (LYS_LEAF|LYS_LEAFLIST) /**< Schema nodetype mask for lyd_node_term */ |
| 240 | #define LYD_NODE_ANY (LYS_ANYDATA) /**< Schema nodetype mask for lyd_node_any */ |
| 241 | |
| 242 | /** |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 243 | * @defgroup dnodeflags Data node flags |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 244 | * @ingroup datatree |
| 245 | * @{ |
| 246 | * |
| 247 | * Various flags of data nodes. |
| 248 | * |
| 249 | * 1 - container 5 - anydata/anyxml |
| 250 | * 2 - list 6 - rpc/action |
| 251 | * 3 - leaf 7 - notification |
| 252 | * 4 - leaflist |
| 253 | * |
| 254 | * bit name 1 2 3 4 5 6 7 |
| 255 | * ---------------------+-+-+-+-+-+-+-+ |
| 256 | * 1 LYD_DEFAULT |x| |x|x| | | | |
| 257 | * +-+-+-+-+-+-+-+ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 258 | * 2 LYD_WHEN_TRUE |x|x|x|x|x| | | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 259 | * +-+-+-+-+-+-+-+ |
| 260 | * 3 LYD_NEW |x|x|x|x|x|x|x| |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 261 | * ---------------------+-+-+-+-+-+-+-+ |
| 262 | * |
| 263 | */ |
| 264 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 265 | #define LYD_DEFAULT 0x01 /**< default (implicit) node */ |
| 266 | #define LYD_WHEN_TRUE 0x02 /**< all when conditions of this node were evaluated to true */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 267 | #define LYD_NEW 0x04 /**< node was created after the last validation, is needed for the next validation */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 268 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 269 | /** @} */ |
| 270 | |
| 271 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 272 | * @brief Generic structure for a data node. |
| 273 | */ |
| 274 | struct lyd_node { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 275 | uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or |
| 276 | hashes of all nodes of subtree in case of keyless list). Note that while hash can be |
| 277 | used to get know that nodes are not equal, it cannot be used to decide that the |
| 278 | nodes are equal due to possible collisions. */ |
| 279 | uint32_t flags; /**< [data node flags](@ref dnodeflags) */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 280 | const struct lysc_node *schema; /**< pointer to the schema definition of this node, note that the target can be not just |
| 281 | ::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] | 282 | struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 283 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 284 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 285 | never NULL. If there is no sibling node, pointer points to the node |
| 286 | itself. In case of the first node, this pointer points to the last |
| 287 | node in the list. */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 288 | struct lyd_meta *meta; /**< pointer to the list of metadata of this node */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 289 | |
| 290 | #ifdef LY_ENABLED_LYD_PRIV |
| 291 | void *priv; /**< private user data, not used by libyang */ |
| 292 | #endif |
| 293 | }; |
| 294 | |
| 295 | /** |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 296 | * @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] | 297 | */ |
| 298 | struct lyd_node_inner { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 299 | uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or |
| 300 | hashes of all nodes of subtree in case of keyless list). Note that while hash can be |
| 301 | used to get know that nodes are not equal, it cannot be used to decide that the |
| 302 | nodes are equal due to possible collisions. */ |
| 303 | uint32_t flags; /**< [data node flags](@ref dnodeflags) */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 304 | const struct lysc_node *schema; /**< pointer to the schema definition of this node */ |
| 305 | struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 306 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 307 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 308 | never NULL. If there is no sibling node, pointer points to the node |
| 309 | itself. In case of the first node, this pointer points to the last |
| 310 | node in the list. */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 311 | struct lyd_meta *meta; /**< pointer to the list of metadata of this node */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 312 | |
| 313 | #ifdef LY_ENABLED_LYD_PRIV |
| 314 | void *priv; /**< private user data, not used by libyang */ |
| 315 | #endif |
| 316 | |
| 317 | struct lyd_node *child; /**< pointer to the first child node. */ |
| 318 | 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] | 319 | #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] | 320 | }; |
| 321 | |
| 322 | /** |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 323 | * @brief Data node structure for the terminal data tree nodes - leaves and leaf-lists. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 324 | */ |
| 325 | struct lyd_node_term { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 326 | uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or |
| 327 | hashes of all nodes of subtree in case of keyless list). Note that while hash can be |
| 328 | used to get know that nodes are not equal, it cannot be used to decide that the |
| 329 | nodes are equal due to possible collisions. */ |
| 330 | uint32_t flags; /**< [data node flags](@ref dnodeflags) */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 331 | const struct lysc_node *schema; /**< pointer to the schema definition of this node */ |
| 332 | struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 333 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 334 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 335 | never NULL. If there is no sibling node, pointer points to the node |
| 336 | itself. In case of the first node, this pointer points to the last |
| 337 | node in the list. */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 338 | struct lyd_meta *meta; /**< pointer to the list of metadata of this node */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 339 | |
| 340 | #ifdef LY_ENABLED_LYD_PRIV |
| 341 | void *priv; /**< private user data, not used by libyang */ |
| 342 | #endif |
| 343 | |
| 344 | struct lyd_value value; /**< node's value representation */ |
| 345 | }; |
| 346 | |
| 347 | /** |
| 348 | * @brief Data node structure for the anydata data tree nodes - anydatas and anyxmls. |
| 349 | */ |
| 350 | struct lyd_node_any { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 351 | uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or |
| 352 | hashes of all nodes of subtree in case of keyless list). Note that while hash can be |
| 353 | used to get know that nodes are not equal, it cannot be used to decide that the |
| 354 | nodes are equal due to possible collisions. */ |
| 355 | uint32_t flags; /**< [data node flags](@ref dnodeflags) */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 356 | const struct lysc_node *schema; /**< pointer to the schema definition of this node */ |
| 357 | struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 358 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 359 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 360 | never NULL. If there is no sibling node, pointer points to the node |
| 361 | itself. In case of the first node, this pointer points to the last |
| 362 | node in the list. */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 363 | struct lyd_meta *meta; /**< pointer to the list of attributes of this node */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 364 | |
| 365 | #ifdef LY_ENABLED_LYD_PRIV |
| 366 | void *priv; /**< private user data, not used by libyang */ |
| 367 | #endif |
| 368 | |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 369 | union lyd_any_value { |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 370 | struct lyd_node *tree; /**< data tree */ |
| 371 | const char *str; /**< Generic string data */ |
| 372 | const char *xml; /**< Serialized XML data */ |
| 373 | const char *json; /**< I-JSON encoded string */ |
| 374 | char *mem; /**< LYD_ANYDATA_LYB memory chunk */ |
| 375 | } value; /**< pointer to the stored value representation of the anydata/anyxml node */ |
| 376 | 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] | 377 | }; |
| 378 | |
| 379 | /** |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 380 | * @brief Data node structure for unparsed (opaque) nodes. |
| 381 | */ |
| 382 | struct lyd_node_opaq { |
| 383 | uint32_t hash; /**< always 0 */ |
| 384 | uint32_t flags; /**< always 0 */ |
| 385 | const struct lysc_node *schema; /**< always NULL */ |
| 386 | struct lyd_node *parent; /**< pointer to the parent node (NULL in case of root node) */ |
| 387 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 388 | struct lyd_node *prev; /**< pointer to the previous sibling node (last sibling if there is none) */ |
| 389 | struct ly_attr *attr; |
| 390 | |
| 391 | #ifdef LY_ENABLED_LYD_PRIV |
| 392 | void *priv; /**< private user data, not used by libyang */ |
| 393 | #endif |
| 394 | |
| 395 | struct lyd_node *child; /**< pointer to the child node (NULL if there are none) */ |
| 396 | const char *name; |
| 397 | LYD_FORMAT format; |
| 398 | struct ly_prefix prefix; /**< name prefix */ |
| 399 | struct ly_prefix *val_prefs; /**< list of prefixes in the value ([sized array](@ref sizedarrays)) */ |
| 400 | const char *value; /**< original value */ |
| 401 | const struct ly_ctx *ctx; /**< libyang context */ |
| 402 | }; |
| 403 | |
| 404 | /** |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 405 | * @defgroup children_options Children traversal options. |
| 406 | * @ingroup datatree |
| 407 | */ |
| 408 | |
| 409 | #define LYD_CHILDREN_SKIP_KEYS 0x01 /**< If list children are returned, skip its keys. */ |
| 410 | |
| 411 | /** @} children_options */ |
| 412 | |
| 413 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 414 | * @brief Get the node's children list if any. |
| 415 | * |
| 416 | * Decides the node's type and in case it has a children list, returns it. |
| 417 | * @param[in] node Node to check. |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 418 | * @param[in] options Bitmask of options, see @ref |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 419 | * @return Pointer to the first child node (if any) of the \p node. |
| 420 | */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 421 | struct lyd_node *lyd_node_children(const struct lyd_node *node, int options); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 422 | |
| 423 | /** |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 424 | * @brief Get the owner module of the data node. It is the module of the top-level schema node. Generally, |
| 425 | * in case of augments it is the target module, recursively, otherwise it is the module where the data node is defined. |
| 426 | * |
| 427 | * @param[in] node Data node to examine. |
| 428 | * @return Module owner of the node. |
| 429 | */ |
| 430 | const struct lys_module *lyd_owner_module(const struct lyd_node *node); |
| 431 | |
| 432 | /** |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 433 | * @brief Learn the length of LYB data. |
| 434 | * |
| 435 | * @param[in] data LYB data to examine. |
| 436 | * @return Length of the LYB data chunk, |
| 437 | * @return -1 on error. |
| 438 | */ |
| 439 | int lyd_lyb_data_length(const char *data); |
| 440 | |
| 441 | /** |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 442 | * @brief Create a new inner node in the data tree. |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 443 | * |
| 444 | * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element. |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 445 | * @param[in] module Module of the node being created. If NULL, @p parent module will be used. |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 446 | * @param[in] name Schema node name of the new data node. The node can be #LYS_CONTAINER, #LYS_NOTIF, #LYS_RPC, or #LYS_ACTION. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 447 | * @param[out] node Optional created node. |
| 448 | * @return LY_ERR value. |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 449 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 450 | LY_ERR lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name, struct lyd_node **node); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 451 | |
| 452 | /** |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 453 | * @brief Create a new list node in the data tree. |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 454 | * |
| 455 | * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element. |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 456 | * @param[in] module Module of the node being created. If NULL, @p parent module will be used. |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 457 | * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 458 | * @param[out] node Optional created node. |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 459 | * @param[in] ... Ordered key values of the new list instance, all must be set. In case of an instance-identifier |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 460 | * or identityref value, the JSON format is expected (module names instead of prefixes). No keys are expected for |
| 461 | * key-less lists. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 462 | * @return LY_ERR value. |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 463 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 464 | LY_ERR lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, struct lyd_node **node, ...); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 465 | |
| 466 | /** |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 467 | * @brief Create a new list node in the data tree. |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 468 | * |
| 469 | * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element. |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 470 | * @param[in] module Module of the node being created. If NULL, @p parent module will be used. |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 471 | * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST. |
| 472 | * @param[in] keys All key values predicate in the form of "[key1='val1'][key2='val2']...", they do not have to be ordered. |
| 473 | * In case of an instance-identifier or identityref value, the JSON format is expected (module names instead of prefixes). |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 474 | * Use NULL or string of length 0 in case of key-less list. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 475 | * @param[out] node Optional created node. |
| 476 | * @return LY_ERR value. |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 477 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 478 | LY_ERR lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys, |
| 479 | struct lyd_node **node); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 480 | |
| 481 | /** |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 482 | * @brief Create a new term node in the data tree. |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 483 | * |
| 484 | * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element. |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 485 | * @param[in] module Module of the node being created. If NULL, @p parent module will be used. |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 486 | * @param[in] name Schema node name of the new data node. The node can be #LYS_LEAF or #LYS_LEAFLIST. |
| 487 | * @param[in] val_str String form of the value of the node being created. In case of an instance-identifier or identityref |
| 488 | * value, the JSON format is expected (module names instead of prefixes). |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 489 | * @param[out] node Optional created node. |
| 490 | * @return LY_ERR value. |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 491 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 492 | LY_ERR lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str, |
| 493 | struct lyd_node **node); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 494 | |
| 495 | /** |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 496 | * @brief Create a new any node in the data tree. |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 497 | * |
| 498 | * @param[in] parent Parent node for the node being created. NULL in case of creating a top level element. |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 499 | * @param[in] module Module of the node being created. If NULL, @p parent module will be used. |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 500 | * @param[in] name Schema node name of the new data node. The node can be #LYS_ANYDATA or #LYS_ANYXML. |
| 501 | * @param[in] value Value to be directly assigned to the node. Expected type is determined by @p value_type. |
| 502 | * @param[in] value_type Type of the provided value in @p value. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 503 | * @param[out] node Optional created node. |
| 504 | * @return LY_ERR value. |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 505 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 506 | LY_ERR lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, const void *value, |
| 507 | LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node); |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 508 | |
| 509 | /** |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 510 | * @brief Create new metadata for a data node. |
| 511 | * |
| 512 | * @param[in] parent Parent node for the metadata being created. |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 513 | * @param[in] module Module of the metadata being created. If NULL, @p name must include module name as the prefix. |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 514 | * @param[in] name Annotation name of the new metadata. It can include the annotation module as the prefix. |
| 515 | * If the prefix is specified it is always used but if not specified, @p module must be set. |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 516 | * @param[in] val_str String form of the value of the metadata. In case of an instance-identifier or identityref |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 517 | * value, the JSON format is expected (module names instead of prefixes). |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 518 | * @param[out] meta Optional created metadata. |
| 519 | * @return LY_ERR value. |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 520 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 521 | LY_ERR lyd_new_meta(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str, |
| 522 | struct lyd_meta **meta); |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 523 | |
| 524 | /** |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 525 | * @brief Create a new opaque node in the data tree. |
| 526 | * |
| 527 | * @param[in] parent Parent node for the node beaing created. NULL in case of creating a top level element. |
| 528 | * @param[in] ctx libyang context. If NULL, @p parent context will be used. |
| 529 | * @param[in] name Node name. |
| 530 | * @param[in] value Node value, may be NULL. |
| 531 | * @param[in] module_name Node module name. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 532 | * @param[out] node Optional created node. |
| 533 | * @return LY_ERR value. |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 534 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 535 | LY_ERR lyd_new_opaq(struct lyd_node *parent, const struct ly_ctx *ctx, const char *name, const char *value, |
| 536 | const char *module_name, struct lyd_node **node); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 537 | |
| 538 | /** |
| 539 | * @brief Create new attribute for an opaque data node. |
| 540 | * |
| 541 | * @param[in] parent Parent opaque node for the attribute being created. |
| 542 | * @param[in] module Module name of the attribute being created. There may be none. |
| 543 | * @param[in] name Attribute name. It can include the module name as the prefix. |
| 544 | * @param[in] val_str String value of the attribute. Is stored directly. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 545 | * @param[out] attr Optional created attribute. |
| 546 | * @return LY_ERR value. |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 547 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 548 | LY_ERR lyd_new_attr(struct lyd_node *parent, const char *module_name, const char *name, const char *val_str, |
| 549 | struct ly_attr **attr); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 550 | |
| 551 | /** |
| 552 | * @defgroup pathoptions Data path creation options |
| 553 | * @ingroup datatree |
| 554 | * |
| 555 | * Various options to change lyd_new_path*() behavior. |
| 556 | * |
| 557 | * Default behavior: |
| 558 | * - if the target node already exists (and is not default), an error is returned. |
| 559 | * - the whole path to the target node is created (with any missing parents) if necessary. |
| 560 | * - RPC output schema children are completely ignored in all modules. Input is searched and nodes created normally. |
| 561 | * @{ |
| 562 | */ |
| 563 | |
| 564 | #define LYD_NEWOPT_UPDATE 0x01 /**< If the target node exists, is a leaf, and it is updated with a new value or its |
| 565 | default flag is changed, it is returned. If the target node exists and is not |
| 566 | a leaf or generally no change occurs in the @p parent tree, NULL is returned and |
| 567 | no error set. */ |
| 568 | #define LYD_NEWOPT_OUTPUT 0x02 /**< Changes the behavior to ignoring RPC/action input schema nodes and using only |
| 569 | output ones. */ |
| 570 | #define LYD_NEWOPT_OPAQ 0x04 /**< Enables the creation of opaque nodes with some specific rules. If the __last node__ |
| 571 | in the path is not uniquely defined ((leaf-)list without a predicate) or has an |
| 572 | invalid value (leaf/leaf-list), it is created as opaque. */ |
| 573 | |
| 574 | /** @} pathoptions */ |
| 575 | |
| 576 | /** |
| 577 | * @brief Create a new node in the data tree based on a path. Cannot be used for anyxml/anydata nodes, |
| 578 | * for those use ::lyd_new_path_any. |
| 579 | * |
| 580 | * If @p path points to a list key and the list instance does not exist, the key value from the predicate is used |
| 581 | * and @p value is ignored. Also, if a leaf-list is being created and both a predicate is defined in @p path |
| 582 | * and @p value is set, the predicate is preferred. |
| 583 | * |
| 584 | * @param[in] parent Data parent to add to/modify, can be NULL. |
| 585 | * @param[in] ctx libyang context, must be set if @p parent is NULL. |
| 586 | * @param[in] path Path to create (TODO ref path). |
| 587 | * @param[in] value Value of the new leaf/leaf-list. For other node types, it is ignored. |
| 588 | * @param[in] options Bitmask of options, see @ref pathoptions. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 589 | * @param[out] node Optional first created node. |
| 590 | * @return LY_ERR value. |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 591 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 592 | LY_ERR lyd_new_path(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const char *value, |
| 593 | int options, struct lyd_node **node); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 594 | |
| 595 | /** |
| 596 | * @brief Create a new node in the data tree based on a path. All node types can be created. |
| 597 | * |
| 598 | * If @p path points to a list key and the list instance does not exist, the key value from the predicate is used |
| 599 | * and @p value is ignored. Also, if a leaf-list is being created and both a predicate is defined in @p path |
| 600 | * and @p value is set, the predicate is preferred. |
| 601 | * |
| 602 | * @param[in] parent Data parent to add to/modify, can be NULL. |
| 603 | * @param[in] ctx libyang context, must be set if @p parent is NULL. |
| 604 | * @param[in] path Path to create (TODO ref path). |
| 605 | * @param[in] value Value of the new leaf/leaf-list/anyxml/anydata. For other node types, it is ignored. |
| 606 | * @param[in] value_type Anyxml/anydata node @p value type. |
| 607 | * @param[in] options Bitmask of options, see @ref pathoptions. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 608 | * @param[out] node Optional first created node. |
| 609 | * @return LY_ERR value. |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 610 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 611 | LY_ERR lyd_new_path_any(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value, |
| 612 | LYD_ANYDATA_VALUETYPE value_type, int options, struct lyd_node **node); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 613 | |
| 614 | /** |
| 615 | * @brief Create a new node in the data tree based on a path. All node types can be created. |
| 616 | * |
| 617 | * If @p path points to a list key and the list instance does not exist, the key value from the predicate is used |
| 618 | * and @p value is ignored. Also, if a leaf-list is being created and both a predicate is defined in @p path |
| 619 | * and @p value is set, the predicate is preferred. |
| 620 | * |
| 621 | * @param[in] parent Data parent to add to/modify, can be NULL. |
| 622 | * @param[in] ctx libyang context, must be set if @p parent is NULL. |
| 623 | * @param[in] path Path to create (TODO ref path). |
| 624 | * @param[in] value Value of the new leaf/leaf-list/anyxml/anydata. For other node types, it is ignored. |
| 625 | * @param[in] value_type Anyxml/anydata node @p value type. |
| 626 | * @param[in] options Bitmask of options, see @ref pathoptions. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 627 | * @param[out] new_parent Optional first parent node created. If only one node was created, equals to @p new_node. |
| 628 | * @param[out] new_node Optional last node created. |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 629 | * @return LY_ERR value. |
| 630 | */ |
| 631 | LY_ERR lyd_new_path2(struct lyd_node *parent, const struct ly_ctx *ctx, const char *path, const void *value, |
| 632 | LYD_ANYDATA_VALUETYPE value_type, int options, struct lyd_node **new_parent, struct lyd_node **new_node); |
| 633 | |
| 634 | /** |
| 635 | * @brief Change the value of a term (leaf or leaf-list) node. |
| 636 | * |
| 637 | * Node changed this way is always considered explicitly set, meaning its default flag |
| 638 | * is always cleared. |
| 639 | * |
| 640 | * @param[in] term Term node to change. |
| 641 | * @param[in] val_str New value to set, any prefixes are expected in JSON format. |
| 642 | * @return LY_SUCCESS if value was changed, |
| 643 | * @return LY_EEXIST if value was the same and only the default flag was cleared, |
| 644 | * @return LY_ENOT if the values were equal and no change occured, |
| 645 | * @return LY_ERR value on other errors. |
| 646 | */ |
| 647 | LY_ERR lyd_change_term(struct lyd_node *term, const char *val_str); |
| 648 | |
| 649 | /** |
Michal Vasko | 4158635 | 2020-07-13 13:54:25 +0200 | [diff] [blame] | 650 | * @brief Change the value of a metadata instance. |
| 651 | * |
| 652 | * @param[in] ctx libyang context. |
| 653 | * @param[in] meta Metadata to change. |
| 654 | * @param[in] val_str New value to set, any prefixes are expected in JSON format. |
| 655 | * @return LY_SUCCESS if value was changed, |
| 656 | * @return LY_ENOT if the values were equal and no change occured, |
| 657 | * @return LY_ERR value on other errors. |
| 658 | */ |
| 659 | LY_ERR lyd_change_meta(struct lyd_meta *meta, const char *val_str); |
| 660 | |
| 661 | /** |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 662 | * @brief Insert a child into a parent. |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 663 | * |
| 664 | * - if the node is part of some other tree, it is automatically unlinked. |
| 665 | * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted. |
| 666 | * |
| 667 | * @param[in] parent Parent node to insert into. |
| 668 | * @param[in] node Node to insert. |
| 669 | * @return LY_SUCCESS on success. |
| 670 | * @return LY_ERR error on error. |
| 671 | */ |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 672 | LY_ERR lyd_insert_child(struct lyd_node *parent, struct lyd_node *node); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 673 | |
| 674 | /** |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 675 | * @brief Insert a node into siblings. |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 676 | * |
| 677 | * - if the node is part of some other tree, it is automatically unlinked. |
| 678 | * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted. |
| 679 | * |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 680 | * @param[in] sibling Siblings to insert into, can even be NULL. |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 681 | * @param[in] node Node to insert. |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 682 | * @param[out] first Optionally return the first sibling after insertion. Can be the address of @p sibling. |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 683 | * @return LY_SUCCESS on success. |
| 684 | * @return LY_ERR error on error. |
| 685 | */ |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 686 | LY_ERR lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node, struct lyd_node **first); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 687 | |
| 688 | /** |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 689 | * @brief Insert a node before another node, can be used only for user-ordered nodes. |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 690 | * |
| 691 | * - if the node is part of some other tree, it is automatically unlinked. |
| 692 | * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted. |
| 693 | * |
| 694 | * @param[in] sibling Sibling node to insert before. |
| 695 | * @param[in] node Node to insert. |
| 696 | * @return LY_SUCCESS on success. |
| 697 | * @return LY_ERR error on error. |
| 698 | */ |
| 699 | LY_ERR lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node); |
| 700 | |
| 701 | /** |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 702 | * @brief Insert a node after another node, can be used only for user-ordered nodes. |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 703 | * |
| 704 | * - if the node is part of some other tree, it is automatically unlinked. |
| 705 | * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted. |
| 706 | * |
| 707 | * @param[in] sibling Sibling node to insert after. |
| 708 | * @param[in] node Node to insert. |
| 709 | * @return LY_SUCCESS on success. |
| 710 | * @return LY_ERR error on error. |
| 711 | */ |
| 712 | LY_ERR lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node); |
| 713 | |
| 714 | /** |
| 715 | * @brief Unlink the specified data subtree. |
| 716 | * |
| 717 | * @param[in] node Data tree node to be unlinked (together with all the children). |
| 718 | */ |
| 719 | void lyd_unlink_tree(struct lyd_node *node); |
| 720 | |
| 721 | /** |
Radek Krejci | b0849a2 | 2019-07-25 12:31:04 +0200 | [diff] [blame] | 722 | * @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] | 723 | * |
| 724 | * @param[in] node Any of the nodes inside the tree. |
| 725 | */ |
| 726 | void lyd_free_all(struct lyd_node *node); |
| 727 | |
| 728 | /** |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 729 | * @brief Free all the sibling nodes (preceding as well as succeeding). |
Radek Krejci | b0849a2 | 2019-07-25 12:31:04 +0200 | [diff] [blame] | 730 | * |
| 731 | * @param[in] node Any of the sibling nodes to free. |
| 732 | */ |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 733 | void lyd_free_siblings(struct lyd_node *node); |
Radek Krejci | b0849a2 | 2019-07-25 12:31:04 +0200 | [diff] [blame] | 734 | |
| 735 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 736 | * @brief Free (and unlink) the specified data (sub)tree. |
| 737 | * |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 738 | * @param[in] node Root of the (sub)tree to be freed. |
| 739 | */ |
| 740 | void lyd_free_tree(struct lyd_node *node); |
| 741 | |
| 742 | /** |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 743 | * @brief Free a single metadata instance. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 744 | * |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 745 | * @param[in] meta Metadata to free. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 746 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 747 | void lyd_free_meta_single(struct lyd_meta *meta); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 748 | |
| 749 | /** |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 750 | * @brief Free the metadata instance with any following instances. |
| 751 | * |
| 752 | * @param[in] meta Metadata to free. |
| 753 | */ |
| 754 | void lyd_free_meta_siblings(struct lyd_meta *meta); |
| 755 | |
| 756 | /** |
| 757 | * @brief Free a single attribute. |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 758 | * |
| 759 | * @param[in] ctx Context where the attributes were created. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 760 | * @param[in] attr Attribute to free. |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 761 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 762 | void ly_free_attr_single(const struct ly_ctx *ctx, struct ly_attr *attr); |
| 763 | |
| 764 | /** |
| 765 | * @brief Free the attribute with any following attributes. |
| 766 | * |
| 767 | * @param[in] ctx Context where the attributes were created. |
| 768 | * @param[in] attr First attribute to free. |
| 769 | */ |
| 770 | void ly_free_attr_siblings(const struct ly_ctx *ctx, struct ly_attr *attr); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 771 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 772 | /** |
| 773 | * @brief Check type restrictions applicable to the particular leaf/leaf-list with the given string @p value. |
| 774 | * |
| 775 | * The given node is not modified in any way - it is just checked if the @p value can be set to the node. |
| 776 | * |
| 777 | * If there is no data node instance and you are fine with checking just the type's restrictions without the |
| 778 | * data tree context (e.g. for the case of require-instance restriction), use lys_value_validate(). |
| 779 | * |
| 780 | * @param[in] ctx libyang context for logging (function does not log errors when @p ctx is NULL) |
| 781 | * @param[in] node Data node for the @p value. |
Michal Vasko | f937cfe | 2020-08-03 16:07:12 +0200 | [diff] [blame] | 782 | * @param[in] value String value to be checked, it is expected to be in JSON format. |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 783 | * @param[in] value_len Length of the given @p value (mandatory). |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 784 | * @param[in] tree Data tree (e.g. when validating RPC/Notification) where the required data instance (leafref target, |
| 785 | * instance-identifier) can be placed. NULL in case the data tree is not yet complete, |
| 786 | * then LY_EINCOMPLETE can be returned. |
Michal Vasko | 3701af5 | 2020-08-03 14:29:38 +0200 | [diff] [blame] | 787 | * @param[out] realtype Optional real type of the value. |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 788 | * @return LY_SUCCESS on success |
| 789 | * @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). |
| 790 | * @return LY_ERR value if an error occurred. |
| 791 | */ |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 792 | LY_ERR lyd_value_validate(const struct ly_ctx *ctx, const struct lyd_node_term *node, const char *value, size_t value_len, |
Michal Vasko | 3701af5 | 2020-08-03 14:29:38 +0200 | [diff] [blame] | 793 | const struct lyd_node *tree, struct lysc_type **realtype); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 794 | |
| 795 | /** |
| 796 | * @brief Compare the node's value with the given string value. The string value is first validated according to the node's type. |
| 797 | * |
| 798 | * @param[in] node Data node to compare. |
| 799 | * @param[in] value String value to be compared. It does not need to be in a canonical form - as part of the process, |
Michal Vasko | f937cfe | 2020-08-03 16:07:12 +0200 | [diff] [blame] | 800 | * it is validated and canonized if possible. But it is expected to be in JSON format. |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 801 | * @param[in] value_len Length of the given @p value (mandatory). |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 802 | * @param[in] tree Data tree (e.g. when validating RPC/Notification) where the required data instance (leafref target, |
| 803 | * instance-identifier) can be placed. NULL in case the data tree is not yet complete, |
| 804 | * then LY_EINCOMPLETE can be returned. |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 805 | * @return LY_SUCCESS on success |
| 806 | * @return LY_EINCOMPLETE in case of success when the @p trees is not provided and it was needed to finish the validation of |
| 807 | * the given string @p value (e.g. due to require-instance). |
Michal Vasko | b3ddccb | 2020-07-09 15:43:05 +0200 | [diff] [blame] | 808 | * @return LY_ENOT if the values do not match. |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 809 | * @return LY_ERR value if an error occurred. |
| 810 | */ |
Michal Vasko | f937cfe | 2020-08-03 16:07:12 +0200 | [diff] [blame] | 811 | LY_ERR lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len, const struct lyd_node *tree); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 812 | |
Radek Krejci | 576b23f | 2019-07-12 14:06:32 +0200 | [diff] [blame] | 813 | /** |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 814 | * @defgroup datacompareoptions Data compare options |
| 815 | * @ingroup datatree |
| 816 | * |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 817 | * Various options to change the lyd_compare() behavior. |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 818 | */ |
| 819 | #define LYD_COMPARE_FULL_RECURSION 0x01 /* lists and containers are the same only in case all they children |
| 820 | (subtree, so direct as well as indirect children) are the same. By default, |
| 821 | containers are the same in case of the same schema node and lists are the same |
| 822 | in case of equal keys (keyless lists do the full recursion comparison all the time). */ |
| 823 | #define LYD_COMPARE_DEFAULTS 0x02 /* By default, implicit and explicit default nodes are considered to be equal. This flag |
| 824 | changes this behavior and implicit (automatically created default node) and explicit |
| 825 | (explicitly created node with the default value) default nodes are considered different. */ |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 826 | /** @} datacompareoptions */ |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 827 | |
| 828 | /** |
| 829 | * @brief Compare 2 data nodes if they are equivalent. |
| 830 | * |
| 831 | * @param[in] node1 The first node to compare. |
| 832 | * @param[in] node2 The second node to compare. |
Radek Krejci | c5ad965 | 2019-09-11 11:31:51 +0200 | [diff] [blame] | 833 | * @param[in] options Various @ref datacompareoptions. |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 834 | * @return LY_SUCCESS if the nodes are equivalent. |
| 835 | * @return LY_ENOT if the nodes are not equivalent. |
| 836 | */ |
Michal Vasko | 8f359bf | 2020-07-28 10:41:15 +0200 | [diff] [blame] | 837 | LY_ERR lyd_compare_single(const struct lyd_node *node1, const struct lyd_node *node2, int options); |
| 838 | |
| 839 | /** |
| 840 | * @brief Compare 2 lists of siblings if they are equivalent. |
| 841 | * |
| 842 | * @param[in] node1 The first sibling list to compare. |
| 843 | * @param[in] node2 The second sibling list to compare. |
| 844 | * @param[in] options Various @ref datacompareoptions. |
| 845 | * @return LY_SUCCESS if all the siblings are equivalent. |
| 846 | * @return LY_ENOT if the siblings are not equivalent. |
| 847 | */ |
| 848 | LY_ERR lyd_compare_siblings(const struct lyd_node *node1, const struct lyd_node *node2, int options); |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 849 | |
| 850 | /** |
Michal Vasko | 2172574 | 2020-06-29 11:49:25 +0200 | [diff] [blame] | 851 | * @brief Compare 2 metadata. |
| 852 | * |
| 853 | * @param[in] meta1 First metadata. |
| 854 | * @param[in] meta2 Second metadata. |
| 855 | * @return LY_SUCCESS if the metadata are equivalent. |
| 856 | * @return LY_ENOT if not. |
| 857 | */ |
| 858 | LY_ERR lyd_compare_meta(const struct lyd_meta *meta1, const struct lyd_meta *meta2); |
| 859 | |
| 860 | /** |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 861 | * @defgroup dupoptions Data duplication options |
| 862 | * @ingroup datatree |
| 863 | * |
| 864 | * Various options to change lyd_dup() behavior. |
| 865 | * |
| 866 | * Default behavior: |
| 867 | * - only the specified node is duplicated without siblings, parents, or children. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 868 | * - all the metadata of the duplicated nodes are also duplicated. |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 869 | * @{ |
| 870 | */ |
| 871 | |
| 872 | #define LYD_DUP_RECURSIVE 0x01 /**< Duplicate not just the node but also all the children. Note that |
| 873 | list's keys are always duplicated. */ |
Michal Vasko | a29a576 | 2020-06-23 13:28:49 +0200 | [diff] [blame] | 874 | #define LYD_DUP_NO_META 0x02 /**< Do not duplicate metadata of any node. */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 875 | #define LYD_DUP_WITH_PARENTS 0x04 /**< If a nested node is being duplicated, duplicate also all the parents. |
| 876 | Keys are also duplicated for lists. Return value does not change! */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 877 | #define LYD_DUP_WITH_FLAGS 0x08 /**< Also copy any data node flags. That will cause the duplicated data to preserve |
Michal Vasko | f6df0a0 | 2020-06-16 13:08:34 +0200 | [diff] [blame] | 878 | its validation/default node state. */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 879 | |
| 880 | /** @} dupoptions */ |
| 881 | |
| 882 | /** |
| 883 | * @brief Create a copy of the specified data tree \p node. Schema references are kept the same. |
| 884 | * |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 885 | * @param[in] node Data tree node to be duplicated. |
| 886 | * @param[in] parent Optional parent node where to connect the duplicated node(s). |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 887 | * If set in combination with LYD_DUP_WITH_PARENTS, the parents chain is duplicated until it comes to and connects with |
| 888 | * the @p parent. |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 889 | * @param[in] options Bitmask of options flags, see @ref dupoptions. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 890 | * @param[out] dup Optional created copy of the node. Note that in case the parents chain is duplicated for the duplicated |
| 891 | * node(s) (when LYD_DUP_WITH_PARENTS used), the first duplicated node is still returned. |
| 892 | * @return LY_ERR value. |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 893 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 894 | LY_ERR lyd_dup_single(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup); |
| 895 | |
| 896 | /** |
| 897 | * @brief Create a copy of the specified data tree \p node with any following siblings. Schema references are kept the same. |
| 898 | * |
| 899 | * @param[in] node Data tree node to be duplicated. |
| 900 | * @param[in] parent Optional parent node where to connect the duplicated node(s). |
| 901 | * If set in combination with LYD_DUP_WITH_PARENTS, the parents chain is duplicated until it comes to and connects with |
| 902 | * the @p parent. |
| 903 | * @param[in] options Bitmask of options flags, see @ref dupoptions. |
| 904 | * @param[out] dup Optional created copy of the node. Note that in case the parents chain is duplicated for the duplicated |
| 905 | * node(s) (when LYD_DUP_WITH_PARENTS used), the first duplicated node is still returned. |
| 906 | * @return LY_ERR value. |
| 907 | */ |
| 908 | LY_ERR lyd_dup_siblings(const struct lyd_node *node, struct lyd_node_inner *parent, int options, struct lyd_node **dup); |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 909 | |
| 910 | /** |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 911 | * @brief Create a copy of the metadata. |
| 912 | * |
| 913 | * @param[in] meta Metadata to copy. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 914 | * @param[in] parent Node where to append the new metadata. |
| 915 | * @param[out] dup Optional created metadata copy. |
| 916 | * @return LY_ERR value. |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 917 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 918 | LY_ERR lyd_dup_meta_single(const struct lyd_meta *meta, struct lyd_node *parent, struct lyd_meta **dup); |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 919 | |
| 920 | /** |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 921 | * @defgroup mergeoptions Data merge options. |
| 922 | * @ingroup datatree |
Radek Krejci | 576b23f | 2019-07-12 14:06:32 +0200 | [diff] [blame] | 923 | * |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 924 | * Various options to change lyd_merge() behavior. |
| 925 | * |
| 926 | * Default behavior: |
| 927 | * - source data tree is not modified in any way, |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 928 | * - any default nodes in the source are ignored if there are explicit nodes in the target. |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 929 | * @{ |
| 930 | */ |
| 931 | |
| 932 | #define LYD_MERGE_DESTRUCT 0x01 /**< Spend source data tree in the function, it cannot be used afterwards! */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 933 | #define LYD_MERGE_DEFAULTS 0x02 /**< Default nodes in the source tree replace even explicit nodes in the target. */ |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 934 | |
| 935 | /** @} mergeoptions */ |
| 936 | |
| 937 | /** |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 938 | * @brief Merge the source data subtree into the target data tree. Merge may not be complete until validation |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 939 | * is called on the resulting data tree (data from more cases may be present, default and non-default values). |
| 940 | * |
| 941 | * @param[in,out] target Target data tree to merge into, must be a top-level tree. |
| 942 | * @param[in] source Source data tree to merge, must be a top-level tree. |
| 943 | * @param[in] options Bitmask of option flags, see @ref mergeoptions. |
| 944 | * @return LY_SUCCESS on success, |
| 945 | * @return LY_ERR value on error. |
| 946 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 947 | LY_ERR lyd_merge_tree(struct lyd_node **target, const struct lyd_node *source, int options); |
| 948 | |
| 949 | /** |
| 950 | * @brief Merge the source data tree with any following siblings into the target data tree. Merge may not be |
| 951 | * complete until validation called on the resulting data tree (data from more cases may be present, default |
| 952 | * and non-default values). |
| 953 | * |
| 954 | * @param[in,out] target Target data tree to merge into, must be a top-level tree. |
| 955 | * @param[in] source Source data tree to merge, must be a top-level tree. |
| 956 | * @param[in] options Bitmask of option flags, see @ref mergeoptions. |
| 957 | * @return LY_SUCCESS on success, |
| 958 | * @return LY_ERR value on error. |
| 959 | */ |
| 960 | LY_ERR lyd_merge_siblings(struct lyd_node **target, const struct lyd_node *source, int options); |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 961 | |
| 962 | /** |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 963 | * @defgroup diffoptions Data diff options. |
| 964 | * @ingroup datatree |
| 965 | * |
| 966 | * Various options to change lyd_diff() behavior. |
| 967 | * |
| 968 | * Default behavior: |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 969 | * - any default nodes are treated as non-existent and ignored. |
| 970 | * @{ |
| 971 | */ |
| 972 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 973 | #define LYD_DIFF_DEFAULTS 0x01 /**< Default nodes in the trees are not ignored but treated similarly to explicit |
| 974 | nodes. Also, leaves and leaf-lists are added into diff even in case only their |
| 975 | default flag (state) was changed. */ |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 976 | |
| 977 | /** @} diffoptions */ |
| 978 | |
| 979 | /** |
| 980 | * @brief Learn the differences between 2 data trees. |
| 981 | * |
| 982 | * The resulting diff is represented as a data tree with specific metadata from the internal 'yang' |
| 983 | * module. Most importantly, every node has an effective 'operation' metadata. If there is none |
| 984 | * defined on the node, it inherits the operation from the nearest parent. Top-level nodes must |
| 985 | * always have the 'operation' metadata defined. Additional metadata ('orig-default', 'value', |
| 986 | * 'orig-value', 'key', 'orig-key') are used for storing more information about the value in the first |
| 987 | * or the second tree. |
| 988 | * |
| 989 | * The diff tree is completely independent on the @p first and @p second trees, meaning all |
| 990 | * the information about the change is stored in the diff and the trees are not needed. |
| 991 | * |
| 992 | * !! Caution !! |
| 993 | * The diff tree should never be validated because it may easily not be valid! For example, |
| 994 | * when data from one case branch are deleted and data from another branch created - data from both |
| 995 | * branches are then stored in the diff tree simultaneously. |
| 996 | * |
| 997 | * @param[in] first First data tree. |
| 998 | * @param[in] second Second data tree. |
| 999 | * @param[in] options Bitmask of options flags, see @ref diffoptions. |
| 1000 | * @param[out] diff Generated diff. |
| 1001 | * @return LY_SUCCESS on success, |
| 1002 | * @return LY_ERR on error. |
| 1003 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1004 | LY_ERR lyd_diff_tree(const struct lyd_node *first, const struct lyd_node *second, int options, struct lyd_node **diff); |
| 1005 | |
| 1006 | /** |
| 1007 | * @brief Learn the differences between 2 data trees including all the following siblings. |
| 1008 | * |
| 1009 | * @param[in] first First data tree. |
| 1010 | * @param[in] second Second data tree. |
| 1011 | * @param[in] options Bitmask of options flags, see @ref diffoptions. |
| 1012 | * @param[out] diff Generated diff. |
| 1013 | * @return LY_SUCCESS on success, |
| 1014 | * @return LY_ERR on error. |
| 1015 | */ |
| 1016 | LY_ERR lyd_diff_siblings(const struct lyd_node *first, const struct lyd_node *second, int options, struct lyd_node **diff); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 1017 | |
| 1018 | /** |
| 1019 | * @brief Callback for diff nodes. |
| 1020 | * |
| 1021 | * @param[in] diff_node Diff node. |
| 1022 | * @param[in] data_node Matching node in data. |
| 1023 | * @param[in] cb_data Arbitrary callback data. |
| 1024 | * @return LY_ERR value. |
| 1025 | */ |
| 1026 | typedef LY_ERR (*lyd_diff_cb)(const struct lyd_node *diff_node, struct lyd_node *data_node, void *cb_data); |
| 1027 | |
| 1028 | /** |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1029 | * @brief Apply the whole diff on a data tree but restrict the operation to one module. |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 1030 | * |
| 1031 | * @param[in,out] data Data to apply the diff on. |
| 1032 | * @param[in] diff Diff to apply. |
| 1033 | * @param[in] mod Module, whose diff/data only to consider, NULL for all modules. |
| 1034 | * @param[in] diff_cb Optional diff callback that will be called for every changed node. |
| 1035 | * @param[in] cb_data Arbitrary callback data. |
| 1036 | * @return LY_SUCCESS on success, |
| 1037 | * @return LY_ERR on error. |
| 1038 | */ |
| 1039 | LY_ERR lyd_diff_apply_module(struct lyd_node **data, const struct lyd_node *diff, const struct lys_module *mod, |
| 1040 | lyd_diff_cb diff_cb, void *cb_data); |
| 1041 | |
| 1042 | /** |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1043 | * @brief Apply the whole diff tree on a data tree. |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 1044 | * |
| 1045 | * @param[in,out] data Data to apply the diff on. |
| 1046 | * @param[in] diff Diff to apply. |
| 1047 | * @return LY_SUCCESS on success, |
| 1048 | * @return LY_ERR on error. |
| 1049 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1050 | LY_ERR lyd_diff_apply_all(struct lyd_node **data, const struct lyd_node *diff); |
Michal Vasko | e893ddd | 2020-06-23 13:35:20 +0200 | [diff] [blame] | 1051 | |
| 1052 | /** |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1053 | * @brief Merge 2 diffs into each other but restrict the operation to one module. |
| 1054 | * |
| 1055 | * The diffs must be possible to be merged, which is guaranteed only if the source diff was |
| 1056 | * created on data that had the target diff applied on them. In other words, this sequence is legal |
| 1057 | * |
| 1058 | * diff1 from data1 and data2 -> data11 from apply diff1 on data1 -> diff2 from data11 and data3 -> |
| 1059 | * -> data 33 frm apply diff2 on data1 |
| 1060 | * |
| 1061 | * and reusing these diffs |
| 1062 | * |
| 1063 | * diff11 from merge diff1 and diff2 -> data33 from apply diff11 on data1 |
| 1064 | * |
| 1065 | * @param[in] src_diff Source diff. |
| 1066 | * @param[in] mod Module, whose diff only to consider, NULL for all modules. |
| 1067 | * @param[in] diff_cb Optional diff callback that will be called for every changed node. |
| 1068 | * @param[in] cb_data Arbitrary callback data. |
| 1069 | * @param[in,out] diff Target diff to merge into. |
| 1070 | * @return LY_SUCCESS on success, |
| 1071 | * @return LY_ERR on error. |
| 1072 | */ |
| 1073 | LY_ERR lyd_diff_merge_module(const struct lyd_node *src_diff, const struct lys_module *mod, lyd_diff_cb diff_cb, |
| 1074 | void *cb_data, struct lyd_node **diff); |
| 1075 | |
| 1076 | /** |
| 1077 | * @brief Merge 2 diffs into each other. |
| 1078 | * |
| 1079 | * @param[in] src_diff Source diff. |
| 1080 | * @param[in,out] diff Target diff to merge into. |
| 1081 | * @return LY_SUCCESS on success, |
| 1082 | * @return LY_ERR on error. |
| 1083 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1084 | LY_ERR lyd_diff_merge_all(const struct lyd_node *src_diff, struct lyd_node **diff); |
Michal Vasko | e6323f6 | 2020-07-09 15:49:02 +0200 | [diff] [blame] | 1085 | |
| 1086 | /** |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1087 | * @brief Reverse a diff and make the opposite changes. Meaning change create to delete, delete to create, |
| 1088 | * or move from place A to B to move from B to A and so on. |
| 1089 | * |
| 1090 | * @param[in] src_diff Diff to reverse. |
| 1091 | * @param[out] diff Reversed diff. |
| 1092 | * @return LY_SUCCESS on success. |
| 1093 | * @return LY_ERR on error. |
| 1094 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1095 | LY_ERR lyd_diff_reverse_all(const struct lyd_node *src_diff, struct lyd_node **diff); |
Michal Vasko | 4231fb6 | 2020-07-13 13:54:47 +0200 | [diff] [blame] | 1096 | |
| 1097 | /** |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 1098 | * @brief Find the target in data of a compiled ly_path structure (instance-identifier). |
| 1099 | * |
| 1100 | * @param[in] path Compiled path structure. |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1101 | * @param[in] tree Data tree to be searched. |
Michal Vasko | 4490d31 | 2020-06-16 13:08:55 +0200 | [diff] [blame] | 1102 | * @return Found target node, |
| 1103 | * @return NULL if not found. |
Radek Krejci | 576b23f | 2019-07-12 14:06:32 +0200 | [diff] [blame] | 1104 | */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1105 | const struct lyd_node_term *lyd_target(const struct ly_path *path, const struct lyd_node *tree); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 1106 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 1107 | /** |
| 1108 | * @brief Get string value of a term data \p node. |
| 1109 | * |
| 1110 | * @param[in] node Data tree node with the value. |
| 1111 | * @param[out] dynamic Whether the string value was dynmically allocated. |
| 1112 | * @return String value of @p node, if @p dynamic, needs to be freed. |
| 1113 | */ |
| 1114 | const char *lyd_value2str(const struct lyd_node_term *node, int *dynamic); |
| 1115 | |
| 1116 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1117 | * @brief Get string value of a metadata \p meta. |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 1118 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1119 | * @param[in] meta Metadata with the value. |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 1120 | * @param[out] dynamic Whether the string value was dynmically allocated. |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1121 | * @return String value of @p meta, if @p dynamic, needs to be freed. |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 1122 | */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1123 | const char *lyd_meta2str(const struct lyd_meta *meta, int *dynamic); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 1124 | |
| 1125 | /** |
| 1126 | * @brief Types of the different data paths. |
| 1127 | */ |
| 1128 | typedef enum { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 1129 | LYD_PATH_LOG, /**< Descriptive path format used in log messages */ |
Michal Vasko | 790b2bc | 2020-08-03 13:35:06 +0200 | [diff] [blame] | 1130 | LYD_PATH_LOG_NO_LAST_PRED, /**< Similar to ::LYD_PATH_LOG except there is never a predicate on the last node */ |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 1131 | } LYD_PATH_TYPE; |
| 1132 | |
| 1133 | /** |
| 1134 | * @brief Generate path of the given node in the requested format. |
| 1135 | * |
| 1136 | * @param[in] node Schema path of this node will be generated. |
| 1137 | * @param[in] pathtype Format of the path to generate. |
| 1138 | * @param[in,out] buffer Prepared buffer of the @p buflen length to store the generated path. |
| 1139 | * If NULL, memory for the complete path is allocated. |
| 1140 | * @param[in] buflen Size of the provided @p buffer. |
| 1141 | * @return NULL in case of memory allocation error, path of the node otherwise. |
| 1142 | * In case the @p buffer is NULL, the returned string is dynamically allocated and caller is responsible to free it. |
| 1143 | */ |
| 1144 | char *lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen); |
| 1145 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 1146 | /** |
Michal Vasko | 25a3282 | 2020-07-09 15:48:22 +0200 | [diff] [blame] | 1147 | * @brief Find a specific metadata. |
| 1148 | * |
| 1149 | * @param[in] first First metadata to consider. |
| 1150 | * @param[in] module Module of the metadata definition, may be NULL if @p name includes a prefix. |
| 1151 | * @param[in] name Name of the metadata to find, may not include a prefix (module name) if @p module is set. |
| 1152 | * @return Found metadata, |
| 1153 | * @return NULL if not found. |
| 1154 | */ |
| 1155 | struct lyd_meta *lyd_find_meta(const struct lyd_meta *first, const struct lys_module *module, const char *name); |
| 1156 | |
| 1157 | /** |
Michal Vasko | da85903 | 2020-07-14 12:20:14 +0200 | [diff] [blame] | 1158 | * @brief Search in the given siblings (NOT recursively) for the first target instance with the same value. |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 1159 | * Uses hashes - should be used whenever possible for best performance. |
| 1160 | * |
| 1161 | * @param[in] siblings Siblings to search in including preceding and succeeding nodes. |
| 1162 | * @param[in] target Target node to find. |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 1163 | * @param[out] match Can be NULL, otherwise the found data node. |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 1164 | * @return LY_SUCCESS on success, @p match set. |
| 1165 | * @return LY_ENOTFOUND if not found, @p match set to NULL. |
| 1166 | * @return LY_ERR value if another error occurred. |
| 1167 | */ |
| 1168 | LY_ERR lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match); |
| 1169 | |
| 1170 | /** |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 1171 | * @brief Search in the given siblings for the first schema instance. |
| 1172 | * Uses hashes - should be used whenever possible for best performance. |
| 1173 | * |
| 1174 | * @param[in] siblings Siblings to search in including preceding and succeeding nodes. |
| 1175 | * @param[in] schema Schema node of the data node to find. |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1176 | * @param[in] key_or_value If it is NULL, the first schema node data instance is found. For nodes with many |
| 1177 | * instances, it can be set based on the type of @p schema: |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 1178 | * LYS_LEAFLIST: |
| 1179 | * Searched instance value. |
| 1180 | * LYS_LIST: |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1181 | * Searched instance key values in the form of "[key1='val1'][key2='val2']...". |
| 1182 | * The keys do not have to be ordered but all of them must be set. |
| 1183 | * |
| 1184 | * Note that any explicit values (leaf-list or list key values) will be canonized first |
| 1185 | * before comparison. But values that do not have a canonical value are expected to be in the |
| 1186 | * JSON format! |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1187 | * @param[in] val_len Optional length of @p key_or_value in case it is not 0-terminated. |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 1188 | * @param[out] match Can be NULL, otherwise the found data node. |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 1189 | * @return LY_SUCCESS on success, @p match set. |
| 1190 | * @return LY_ENOTFOUND if not found, @p match set to NULL. |
| 1191 | * @return LY_EINVAL if @p schema is a key-less list. |
| 1192 | * @return LY_ERR value if another error occurred. |
| 1193 | */ |
| 1194 | LY_ERR lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value, |
| 1195 | size_t val_len, struct lyd_node **match); |
| 1196 | |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 1197 | /** |
| 1198 | * @brief Search in the given data for instances of nodes matching the provided XPath. |
| 1199 | * |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 1200 | * The expected format of the expression is ::LYD_JSON, meaning the first node in every path |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 1201 | * must have its module name as prefix or be the special `*` value for all the nodes. |
| 1202 | * |
Michal Vasko | 19a3060 | 2020-05-25 10:40:19 +0200 | [diff] [blame] | 1203 | * If a list instance is being selected with all its key values specified (but not necessarily ordered) |
| 1204 | * in the form `list[key1='val1'][key2='val2'][key3='val3']` or a leaf-list instance in the form |
| 1205 | * `leaf-list[.='val']`, these instances are found using hashes with constant (*O(1)*) complexity |
| 1206 | * (unless they are defined in top-level). Other predicates can still follow the aforementioned ones. |
| 1207 | * |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 1208 | * @param[in] ctx_node XPath context node. |
| 1209 | * @param[in] xpath Data XPath expression filtering the matching nodes. ::LYD_JSON format is expected. |
| 1210 | * @param[out] set Set of found data nodes. In case the result is a number, a string, or a boolean, |
| 1211 | * the returned set is empty. |
| 1212 | * @return LY_SUCCESS on success, @p set is returned. |
| 1213 | * @return LY_ERR value if an error occurred. |
| 1214 | */ |
| 1215 | LY_ERR lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set); |
| 1216 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1217 | #ifdef __cplusplus |
| 1218 | } |
| 1219 | #endif |
| 1220 | |
| 1221 | #endif /* LY_TREE_DATA_H_ */ |