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 */ \ |
Radek Krejci | dae0ee8 | 2020-05-06 16:53:24 +0200 | [diff] [blame] | 91 | (NEXT) = lyd_node_children((struct lyd_node*)ELEM); \ |
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 */ |
Radek Krejci | 355bf4f | 2019-07-16 17:14:16 +0200 | [diff] [blame] | 125 | #if 0 |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 126 | LYD_LYB, /**< LYB format of the instance data */ |
| 127 | #endif |
| 128 | } LYD_FORMAT; |
| 129 | |
| 130 | /** |
Radek Krejci | 59583bb | 2019-09-11 12:57:55 +0200 | [diff] [blame] | 131 | * @brief List of possible value types stored in ::lyd_node_any. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 132 | */ |
| 133 | typedef enum { |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 134 | 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] | 135 | is directly connected into the anydata node without duplication, caller is supposed to not manipulate |
| 136 | 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] | 137 | 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] | 138 | as string). XML sensitive characters (such as & or \>) are automatically escaped when the anydata |
| 139 | is printed in XML format. */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 140 | LYD_ANYDATA_XML, /**< Value is a string containing the serialized XML data. */ |
| 141 | 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] | 142 | #if 0 /* TODO LYB format */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 143 | 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] | 144 | #endif |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 145 | } LYD_ANYDATA_VALUETYPE; |
| 146 | |
| 147 | /** @} */ |
| 148 | |
| 149 | /** |
| 150 | * @brief YANG data representation |
| 151 | */ |
| 152 | struct lyd_value { |
Radek Krejci | 950f6a5 | 2019-09-12 17:15:32 +0200 | [diff] [blame] | 153 | const char *original; /**< Original string representation of the value. It is never NULL, but (canonical) string representation |
| 154 | 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] | 155 | union { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 156 | int8_t boolean; /**< 0 as false, 1 as true */ |
| 157 | int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */ |
Radek Krejci | 8dc4f2d | 2019-07-16 12:24:00 +0200 | [diff] [blame] | 158 | int8_t int8; /**< 8-bit signed integer */ |
| 159 | int16_t int16; /**< 16-bit signed integer */ |
| 160 | int32_t int32; /**< 32-bit signed integer */ |
| 161 | int64_t int64; /**< 64-bit signed integer */ |
| 162 | uint8_t uint8; /**< 8-bit unsigned integer */ |
| 163 | uint16_t uint16; /**< 16-bit signed integer */ |
| 164 | uint32_t uint32; /**< 32-bit signed integer */ |
| 165 | uint64_t uint64; /**< 64-bit signed integer */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 166 | 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] | 167 | 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] | 168 | 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] | 169 | |
| 170 | struct lyd_value_subvalue { |
| 171 | struct lyd_value_prefix { |
| 172 | const char *prefix; /**< prefix string used in the canonized string to identify the mod of the YANG schema */ |
| 173 | const struct lys_module *mod; /**< YANG schema module identified by the prefix string */ |
| 174 | } *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] | 175 | struct lyd_value *value; /**< representation of the value according to the selected union's subtype (stored as lyd_value::realpath |
| 176 | here, in subvalue structure */ |
| 177 | } *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] | 178 | 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] | 179 | one of the union's type. The lyd_value_subvalue:prefixes provides (possible) mappings from prefixes |
| 180 | in original value to YANG modules. These prefixes are necessary to parse original value to the union's |
| 181 | subtypes. */ |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 182 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 183 | struct ly_path *target; /**< Instance-identifier's target path. */ |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 184 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 185 | void *ptr; /**< generic data type structure used to store the data */ |
Radek Krejci | 8dc4f2d | 2019-07-16 12:24:00 +0200 | [diff] [blame] | 186 | }; /**< The union is just a list of shorthands to possible values stored by a type's plugin. libyang itself uses the lyd_value::realtype |
| 187 | plugin's callbacks to work with the data. */ |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 188 | |
Radek Krejci | 950f6a5 | 2019-09-12 17:15:32 +0200 | [diff] [blame] | 189 | 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] | 190 | in the schema node of the data node since the type's store plugin can use other types/plugins for |
| 191 | storing data. Speaking about built-in types, this is the case of leafref which stores data as its |
| 192 | target type. In contrast, union type also use its subtype's callbacks, but inside an internal data |
| 193 | lyd_value::subvalue structure, so here is the pointer to the union type. |
| 194 | In general, this type is used to get free callback for this lyd_value structure, so it must reflect |
| 195 | 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] | 196 | void *canonical_cache; /**< Generic cache for type plugins to store data necessary to print canonical value. It can be the canonical |
| 197 | value itself or anything else useful to print the canonical form of the value. Plugin is responsible for |
| 198 | freeing the cache in its free callback. */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 202 | * @brief Metadata structure. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 203 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 204 | * 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] | 205 | * annotations as specified in RFC 7952. The only exception is the filter type (in NETCONF get operations) |
| 206 | * and edit-config's operation attributes. In XML, they are represented as standard XML attributes. In JSON, |
| 207 | * they are represented as JSON elements starting with the '@' character (for more information, see the |
| 208 | * YANG metadata RFC. |
| 209 | * |
| 210 | */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 211 | struct lyd_meta { |
| 212 | struct lyd_node *parent; /**< data node where the metadata is placed */ |
| 213 | struct lyd_meta *next; /**< pointer to the next metadata of the same element */ |
| 214 | struct lysc_ext_instance *annotation; /**< pointer to the annotation's definition */ |
| 215 | const char *name; /**< metadata name */ |
| 216 | struct lyd_value value; /**< metadata value representation */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 217 | }; |
| 218 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 219 | /** |
| 220 | * @brief Generic prefix and namespace mapping, meaning depends on the format. |
| 221 | */ |
| 222 | struct ly_prefix { |
| 223 | const char *pref; |
| 224 | const char *ns; |
| 225 | }; |
| 226 | |
| 227 | /** |
| 228 | * @brief Generic attribute structure. |
| 229 | */ |
| 230 | struct ly_attr { |
| 231 | struct lyd_node_opaq *parent; /**< data node where the attribute is placed */ |
| 232 | struct ly_attr *next; |
| 233 | struct ly_prefix *val_prefs; /**< list of prefixes in the value ([sized array](@ref sizedarrays)) */ |
| 234 | const char *name; |
| 235 | const char *value; |
| 236 | |
| 237 | LYD_FORMAT format; |
| 238 | struct ly_prefix prefix; /**< name prefix, it is stored because they are a real pain to generate properly */ |
| 239 | |
| 240 | }; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 241 | |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 242 | #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] | 243 | #define LYD_NODE_TERM (LYS_LEAF|LYS_LEAFLIST) /**< Schema nodetype mask for lyd_node_term */ |
| 244 | #define LYD_NODE_ANY (LYS_ANYDATA) /**< Schema nodetype mask for lyd_node_any */ |
| 245 | |
| 246 | /** |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 247 | * @defgroup dnodeflags Data node flags |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 248 | * @ingroup datatree |
| 249 | * @{ |
| 250 | * |
| 251 | * Various flags of data nodes. |
| 252 | * |
| 253 | * 1 - container 5 - anydata/anyxml |
| 254 | * 2 - list 6 - rpc/action |
| 255 | * 3 - leaf 7 - notification |
| 256 | * 4 - leaflist |
| 257 | * |
| 258 | * bit name 1 2 3 4 5 6 7 |
| 259 | * ---------------------+-+-+-+-+-+-+-+ |
| 260 | * 1 LYD_DEFAULT |x| |x|x| | | | |
| 261 | * +-+-+-+-+-+-+-+ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 262 | * 2 LYD_WHEN_TRUE |x|x|x|x|x| | | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 263 | * +-+-+-+-+-+-+-+ |
| 264 | * 3 LYD_NEW |x|x|x|x|x|x|x| |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 265 | * ---------------------+-+-+-+-+-+-+-+ |
| 266 | * |
| 267 | */ |
| 268 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 269 | #define LYD_DEFAULT 0x01 /**< default (implicit) node */ |
| 270 | #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] | 271 | #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] | 272 | |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 273 | /** @} */ |
| 274 | |
| 275 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 276 | * @brief Callback provided by the data/schema parsers to type plugins to resolve (format-specific) mapping between prefixes used |
| 277 | * in the value strings to the YANG schemas. |
| 278 | * |
| 279 | * Reverse function to ly_clb_get_prefix. |
| 280 | * |
| 281 | * XML uses XML namespaces, JSON uses schema names as prefixes, YIN/YANG uses prefixes of the imports. |
| 282 | * |
| 283 | * @param[in] ctx libyang context to find the schema. |
| 284 | * @param[in] prefix Prefix found in the value string |
| 285 | * @param[in] prefix_len Length of the @p prefix. |
| 286 | * @param[in] private Internal data needed by the callback. |
| 287 | * @return Pointer to the YANG schema identified by the provided prefix or NULL if no mapping found. |
| 288 | */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 289 | typedef const struct lys_module *(*ly_clb_resolve_prefix)(const struct ly_ctx *ctx, const char *prefix, size_t prefix_len, |
| 290 | void *private); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 291 | |
| 292 | /** |
| 293 | * @brief Callback provided by the data/schema printers to type plugins to resolve (format-specific) mapping between YANG module of a data object |
| 294 | * to prefixes used in the value strings. |
| 295 | * |
| 296 | * Reverse function to ly_clb_resolve_prefix. |
| 297 | * |
| 298 | * XML uses XML namespaces, JSON uses schema names as prefixes, YIN/YANG uses prefixes of the imports. |
| 299 | * |
| 300 | * @param[in] mod YANG module of the object. |
| 301 | * @param[in] private Internal data needed by the callback. |
| 302 | * @return String representing prefix for the object of the given YANG module @p mod. |
| 303 | */ |
| 304 | typedef const char *(*ly_clb_get_prefix)(const struct lys_module *mod, void *private); |
| 305 | |
| 306 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 307 | * @brief Generic structure for a data node. |
| 308 | */ |
| 309 | struct lyd_node { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 310 | uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or |
| 311 | hashes of all nodes of subtree in case of keyless list). Note that while hash can be |
| 312 | used to get know that nodes are not equal, it cannot be used to decide that the |
| 313 | nodes are equal due to possible collisions. */ |
| 314 | uint32_t flags; /**< [data node flags](@ref dnodeflags) */ |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 315 | const struct lysc_node *schema; /**< pointer to the schema definition of this node, note that the target can be not just |
| 316 | ::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] | 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. */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 323 | 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] | 324 | |
| 325 | #ifdef LY_ENABLED_LYD_PRIV |
| 326 | void *priv; /**< private user data, not used by libyang */ |
| 327 | #endif |
| 328 | }; |
| 329 | |
| 330 | /** |
Radek Krejci | f3b6fec | 2019-07-24 15:53:11 +0200 | [diff] [blame] | 331 | * @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] | 332 | */ |
| 333 | struct lyd_node_inner { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 334 | uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or |
| 335 | hashes of all nodes of subtree in case of keyless list). Note that while hash can be |
| 336 | used to get know that nodes are not equal, it cannot be used to decide that the |
| 337 | nodes are equal due to possible collisions. */ |
| 338 | uint32_t flags; /**< [data node flags](@ref dnodeflags) */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 339 | const struct lysc_node *schema; /**< pointer to the schema definition of this node */ |
| 340 | struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 341 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 342 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 343 | never NULL. If there is no sibling node, pointer points to the node |
| 344 | itself. In case of the first node, this pointer points to the last |
| 345 | node in the list. */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 346 | 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] | 347 | |
| 348 | #ifdef LY_ENABLED_LYD_PRIV |
| 349 | void *priv; /**< private user data, not used by libyang */ |
| 350 | #endif |
| 351 | |
| 352 | struct lyd_node *child; /**< pointer to the first child node. */ |
| 353 | 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] | 354 | #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] | 355 | }; |
| 356 | |
| 357 | /** |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 358 | * @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] | 359 | */ |
| 360 | struct lyd_node_term { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 361 | uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or |
| 362 | hashes of all nodes of subtree in case of keyless list). Note that while hash can be |
| 363 | used to get know that nodes are not equal, it cannot be used to decide that the |
| 364 | nodes are equal due to possible collisions. */ |
| 365 | uint32_t flags; /**< [data node flags](@ref dnodeflags) */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 366 | const struct lysc_node *schema; /**< pointer to the schema definition of this node */ |
| 367 | struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 368 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 369 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 370 | never NULL. If there is no sibling node, pointer points to the node |
| 371 | itself. In case of the first node, this pointer points to the last |
| 372 | node in the list. */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 373 | 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] | 374 | |
| 375 | #ifdef LY_ENABLED_LYD_PRIV |
| 376 | void *priv; /**< private user data, not used by libyang */ |
| 377 | #endif |
| 378 | |
| 379 | struct lyd_value value; /**< node's value representation */ |
| 380 | }; |
| 381 | |
| 382 | /** |
| 383 | * @brief Data node structure for the anydata data tree nodes - anydatas and anyxmls. |
| 384 | */ |
| 385 | struct lyd_node_any { |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 386 | uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list or |
| 387 | hashes of all nodes of subtree in case of keyless list). Note that while hash can be |
| 388 | used to get know that nodes are not equal, it cannot be used to decide that the |
| 389 | nodes are equal due to possible collisions. */ |
| 390 | uint32_t flags; /**< [data node flags](@ref dnodeflags) */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 391 | const struct lysc_node *schema; /**< pointer to the schema definition of this node */ |
| 392 | struct lyd_node_inner *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 393 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 394 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 395 | never NULL. If there is no sibling node, pointer points to the node |
| 396 | itself. In case of the first node, this pointer points to the last |
| 397 | node in the list. */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 398 | 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] | 399 | |
| 400 | #ifdef LY_ENABLED_LYD_PRIV |
| 401 | void *priv; /**< private user data, not used by libyang */ |
| 402 | #endif |
| 403 | |
Radek Krejci | ee4cab2 | 2019-07-17 17:07:47 +0200 | [diff] [blame] | 404 | union { |
| 405 | struct lyd_node *tree; /**< data tree */ |
| 406 | const char *str; /**< Generic string data */ |
| 407 | const char *xml; /**< Serialized XML data */ |
| 408 | const char *json; /**< I-JSON encoded string */ |
| 409 | char *mem; /**< LYD_ANYDATA_LYB memory chunk */ |
| 410 | } value; /**< pointer to the stored value representation of the anydata/anyxml node */ |
| 411 | 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] | 412 | }; |
| 413 | |
| 414 | /** |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 415 | * @brief Data node structure for unparsed (opaque) nodes. |
| 416 | */ |
| 417 | struct lyd_node_opaq { |
| 418 | uint32_t hash; /**< always 0 */ |
| 419 | uint32_t flags; /**< always 0 */ |
| 420 | const struct lysc_node *schema; /**< always NULL */ |
| 421 | struct lyd_node *parent; /**< pointer to the parent node (NULL in case of root node) */ |
| 422 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 423 | struct lyd_node *prev; /**< pointer to the previous sibling node (last sibling if there is none) */ |
| 424 | struct ly_attr *attr; |
| 425 | |
| 426 | #ifdef LY_ENABLED_LYD_PRIV |
| 427 | void *priv; /**< private user data, not used by libyang */ |
| 428 | #endif |
| 429 | |
| 430 | struct lyd_node *child; /**< pointer to the child node (NULL if there are none) */ |
| 431 | const char *name; |
| 432 | LYD_FORMAT format; |
| 433 | struct ly_prefix prefix; /**< name prefix */ |
| 434 | struct ly_prefix *val_prefs; /**< list of prefixes in the value ([sized array](@ref sizedarrays)) */ |
| 435 | const char *value; /**< original value */ |
| 436 | const struct ly_ctx *ctx; /**< libyang context */ |
| 437 | }; |
| 438 | |
| 439 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 440 | * @defgroup dataparseroptions Data parser options |
| 441 | * @ingroup datatree |
| 442 | * |
| 443 | * Various options to change the data tree parsers behavior. |
| 444 | * |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 445 | * Default parser behavior: |
Michal Vasko | e75ecfd | 2020-03-06 14:12:28 +0100 | [diff] [blame] | 446 | * - complete input file is always parsed. In case of XML, even not well-formed XML document (multiple top-level |
| 447 | * elements) is parsed in its entirety, |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 448 | * - parser silently ignores data without matching schema node definition, |
| 449 | * - list instances are checked whether they have all the keys, error is raised if not. |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 450 | * |
| 451 | * Default parser validation behavior: |
| 452 | * - the provided data are expected to provide complete datastore content (both the configuration and state data) |
| 453 | * and performs data validation according to all YANG rules, specifics follow, |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 454 | * - list instances are expected to have all the keys (it is not checked), |
Michal Vasko | e75ecfd | 2020-03-06 14:12:28 +0100 | [diff] [blame] | 455 | * - instantiated (status) obsolete data print a warning, |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 456 | * - all types are fully resolved (leafref/instance-identifier targets, unions) and must be valid (lists have |
| 457 | * all the keys, leaf(-lists) correct values), |
| 458 | * - when statements on existing nodes are evaluated, if not satisfied, a validation error is raised, |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 459 | * - if-feature statements are evaluated, |
| 460 | * - invalid multiple data instances/data from several cases cause a validation error, |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 461 | * - default values are added. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 462 | * @{ |
| 463 | */ |
| 464 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 465 | #define LYD_OPT_PARSE_ONLY 0x0001 /**< Data will be only parsed and no validation will be performed. When statements |
| 466 | are kept unevaluated, union types may not be fully resolved, if-feature |
| 467 | statements are not checked, and default values are not added (only the ones |
| 468 | parsed are present). */ |
| 469 | #define LYD_OPT_TRUSTED 0x0002 /**< Data are considered trusted so they will be parsed as validated. If the parsed |
| 470 | data are not valid, using this flag may lead to some unexpected behavior! |
| 471 | This flag can be used only with #LYD_OPT_PARSE_ONLY. */ |
| 472 | #define LYD_OPT_STRICT 0x0004 /**< Instead of silently ignoring data without schema definition raise an error. |
| 473 | Do not combine with #LYD_OPT_OPAQ. */ |
| 474 | #define LYD_OPT_OPAQ 0x0008 /**< Instead of silently ignoring data without definition, parse them into |
| 475 | an opaq node. Do not combine with #LYD_OPT_STRICT. */ |
| 476 | #define LYD_OPT_NO_STATE 0x0010 /**< Forbid state data in the parsed data. */ |
| 477 | |
| 478 | #define LYD_OPT_MASK 0xFFFF /**< Mask for all the parser options. */ |
Michal Vasko | 5b37a35 | 2020-03-06 13:38:33 +0100 | [diff] [blame] | 479 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 480 | /** @} dataparseroptions */ |
| 481 | |
| 482 | /** |
| 483 | * @defgroup datavalidationoptions Data validation options |
| 484 | * @ingroup datatree |
| 485 | * |
| 486 | * Various options to change data validation behaviour, both for the parser and separate validation. |
| 487 | * |
| 488 | * Default separate validation behavior: |
| 489 | * - the provided data are expected to provide complete datastore content (both the configuration and state data) |
| 490 | * and performs data validation according to all YANG rules, specifics follow, |
Michal Vasko | e75ecfd | 2020-03-06 14:12:28 +0100 | [diff] [blame] | 491 | * - instantiated (status) obsolete data print a warning, |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 492 | * - all types are fully resolved (leafref/instance-identifier targets, unions) and must be valid (lists have |
| 493 | * all the keys, leaf(-lists) correct values), |
| 494 | * - when statements on existing nodes are evaluated. Depending on the previous when state (from previous validation |
| 495 | * or parsing), the node is silently auto-deleted if the state changed from true to false, otherwise a validation error |
| 496 | * is raised if it evaluates to false, |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 497 | * - if-feature statements are evaluated, |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 498 | * - data from several cases behave based on their previous state (from previous validation or parsing). If there existed |
| 499 | * already a case and another one was added, the previous one is silently auto-deleted. Otherwise (if data from 2 or |
| 500 | * more cases were created) a validation error is raised, |
| 501 | * - default values are added. |
| 502 | * |
| 503 | * @{ |
| 504 | */ |
| 505 | |
| 506 | #define LYD_VALOPT_NO_STATE 0x00010000 /**< Consider state data not allowed and raise an error if they are found. */ |
| 507 | #define LYD_VALOPT_DATA_ONLY 0x00020000 /**< Validate only modules whose data actually exist. */ |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 508 | #define LYD_VALOPT_INPUT 0x00040000 /**< Validate RPC/action request (input parameters). */ |
| 509 | #define LYD_VALOPT_OUTPUT 0x00080000 /**< Validate RPC/action reply (output parameters). */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 510 | |
Michal Vasko | 5b37a35 | 2020-03-06 13:38:33 +0100 | [diff] [blame] | 511 | #define LYD_VALOPT_MASK 0xFFFF0000 /**< Mask for all the validation options. */ |
| 512 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 513 | /** @} datavalidationoptions */ |
| 514 | |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 515 | //#define LYD_OPT_VAL_DIFF 0x40000 /**< Flag only for validation, store all the data node changes performed by the validation |
| 516 | // in a diff structure. */ |
| 517 | //#define LYD_OPT_DATA_TEMPLATE 0x1000000 /**< Data represents YANG data template. */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 518 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 519 | /** |
| 520 | * @brief Get the node's children list if any. |
| 521 | * |
| 522 | * Decides the node's type and in case it has a children list, returns it. |
| 523 | * @param[in] node Node to check. |
| 524 | * @return Pointer to the first child node (if any) of the \p node. |
| 525 | */ |
Radek Krejci | dae0ee8 | 2020-05-06 16:53:24 +0200 | [diff] [blame] | 526 | struct lyd_node *lyd_node_children(const struct lyd_node *node); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 527 | |
| 528 | /** |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 529 | * @brief Get the owner module of the data node. It is the module of the top-level schema node. Generally, |
| 530 | * in case of augments it is the target module, recursively, otherwise it is the module where the data node is defined. |
| 531 | * |
| 532 | * @param[in] node Data node to examine. |
| 533 | * @return Module owner of the node. |
| 534 | */ |
| 535 | const struct lys_module *lyd_owner_module(const struct lyd_node *node); |
| 536 | |
| 537 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 538 | * @brief Parse (and validate) data from memory. |
| 539 | * |
| 540 | * In case of LY_XML format, the data string is parsed completely. It means that when it contains |
| 541 | * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The |
| 542 | * returned data node is a root of the first tree with other trees connected via the next pointer. |
| 543 | * This behavior can be changed by #LYD_OPT_NOSIBLINGS option. |
| 544 | * |
| 545 | * @param[in] ctx Context to connect with the data tree being built here. |
| 546 | * @param[in] data Serialized data in the specified format. |
| 547 | * @param[in] format Format of the input data to be parsed. |
Michal Vasko | 5b37a35 | 2020-03-06 13:38:33 +0100 | [diff] [blame] | 548 | * @param[in] options Parser and validation options, see @ref parseroptions. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 549 | * @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] | 550 | * use lyd_free_all(). |
| 551 | * @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] | 552 | */ |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 553 | 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] | 554 | |
| 555 | /** |
| 556 | * @brief Read (and validate) data from the given file descriptor. |
| 557 | * |
| 558 | * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc. |
| 559 | * |
| 560 | * In case of LY_XML format, the file content is parsed completely. It means that when it contains |
| 561 | * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The |
| 562 | * returned data node is a root of the first tree with other trees connected via the next pointer. |
| 563 | * This behavior can be changed by #LYD_OPT_NOSIBLINGS option. |
| 564 | * |
| 565 | * @param[in] ctx Context to connect with the data tree being built here. |
| 566 | * @param[in] fd The standard file descriptor of the file containing the data tree in the specified format. |
| 567 | * @param[in] format Format of the input data to be parsed. |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 568 | * @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] | 569 | * @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] | 570 | * use lyd_free_all(). |
| 571 | * @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] | 572 | */ |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 573 | 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] | 574 | |
| 575 | /** |
| 576 | * @brief Read (and validate) data from the given file path. |
| 577 | * |
| 578 | * In case of LY_XML format, the file content is parsed completely. It means that when it contains |
| 579 | * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The |
| 580 | * returned data node is a root of the first tree with other trees connected via the next pointer. |
| 581 | * This behavior can be changed by #LYD_OPT_NOSIBLINGS option. |
| 582 | * |
| 583 | * @param[in] ctx Context to connect with the data tree being built here. |
| 584 | * @param[in] path Path to the file containing the data tree in the specified format. |
| 585 | * @param[in] format Format of the input data to be parsed. |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 586 | * @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] | 587 | * @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] | 588 | * use lyd_free_all(). |
| 589 | * @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] | 590 | */ |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 591 | 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] | 592 | |
| 593 | /** |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 594 | * @brief Fully validate a data tree. |
| 595 | * |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 596 | * @param[in,out] tree Data tree to recursively validate. May be changed by validation. |
| 597 | * @param[in] ctx libyang context. Can be NULL if @p tree is set. |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 598 | * @param[in] val_opts Validation options (@ref datavalidationoptions). |
| 599 | * @return LY_SUCCESS on success. |
| 600 | * @return LY_ERR error on error. |
| 601 | */ |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 602 | LY_ERR lyd_validate(struct lyd_node **tree, const struct ly_ctx *ctx, int val_opts); |
| 603 | |
| 604 | /** |
| 605 | * @brief Fully validate a data tree. |
| 606 | * |
| 607 | * @param[in,out] tree Data tree to recursively validate. May be changed by validation. |
| 608 | * @param[in] modules Array of modules to validate. |
| 609 | * @param[in] mod_count Number of @p modules. |
| 610 | * @param[in] val_opts Validation options (@ref datavalidationoptions). |
| 611 | * @return LY_SUCCESS on success. |
| 612 | * @return LY_ERR error on error. |
| 613 | */ |
| 614 | LY_ERR lyd_validate_modules(struct lyd_node **tree, const struct lys_module **modules, int mod_count, int val_opts); |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 615 | |
| 616 | /** |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 617 | * @brief Validate an RPC/action, notification, or RPC/action reply. |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 618 | * |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 619 | * @param[in,out] op_tree Operation tree with any parents. It can point to the operation itself or any of |
| 620 | * its parents, only the operation subtree is actually validated. |
| 621 | * @param[in] tree Tree to be used for validating references from the operation subtree. |
| 622 | * @param[in] val_opts Specific validation option (@ref datavalidationoptions): |
| 623 | * 0 - no validation option for validation notifications, |
| 624 | * ::LYD_VALOPT_INPUT - for validating RPC/action request (input), |
| 625 | * ::LYD_VALOPT_OUTPUT - for validatin RPC/action reply (output). |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 626 | * @return LY_SUCCESS on success. |
| 627 | * @return LY_ERR error on error. |
| 628 | */ |
Michal Vasko | cb7526d | 2020-03-30 15:08:26 +0200 | [diff] [blame] | 629 | LY_ERR lyd_validate_op(struct lyd_node *op_tree, const struct lyd_node *tree, int val_opts); |
Michal Vasko | fea12c6 | 2020-03-30 11:00:15 +0200 | [diff] [blame] | 630 | |
| 631 | /** |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 632 | * @brief Create a new inner node in a data tree. |
| 633 | * |
| 634 | * @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] | 635 | * @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] | 636 | * @param[in] name Schema node name of the new data node. The node can be #LYS_CONTAINER, #LYS_NOTIF, #LYS_RPC, or #LYS_ACTION. |
| 637 | * @return New created node. |
| 638 | * @return NULL on error. |
| 639 | */ |
| 640 | struct lyd_node *lyd_new_inner(struct lyd_node *parent, const struct lys_module *module, const char *name); |
| 641 | |
| 642 | /** |
| 643 | * @brief Create a new list node in a data tree. |
| 644 | * |
| 645 | * @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] | 646 | * @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] | 647 | * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST. |
| 648 | * @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] | 649 | * or identityref value, the JSON format is expected (module names instead of prefixes). No keys are expected for |
| 650 | * key-less lists. |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 651 | * @return New created node. |
| 652 | * @return NULL on error. |
| 653 | */ |
| 654 | struct lyd_node *lyd_new_list(struct lyd_node *parent, const struct lys_module *module, const char *name, ...); |
| 655 | |
| 656 | /** |
| 657 | * @brief Create a new list node in a data tree. |
| 658 | * |
| 659 | * @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] | 660 | * @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] | 661 | * @param[in] name Schema node name of the new data node. The node must be #LYS_LIST. |
| 662 | * @param[in] keys All key values predicate in the form of "[key1='val1'][key2='val2']...", they do not have to be ordered. |
| 663 | * 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] | 664 | * Use NULL or string of length 0 in case of key-less list. |
Michal Vasko | 013a818 | 2020-03-03 10:46:53 +0100 | [diff] [blame] | 665 | * @return New created node. |
| 666 | * @return NULL on error. |
| 667 | */ |
| 668 | struct lyd_node *lyd_new_list2(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *keys); |
| 669 | |
| 670 | /** |
| 671 | * @brief Create a new term node in a data tree. |
| 672 | * |
| 673 | * @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] | 674 | * @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] | 675 | * @param[in] name Schema node name of the new data node. The node can be #LYS_LEAF or #LYS_LEAFLIST. |
| 676 | * @param[in] val_str String form of the value of the node being created. In case of an instance-identifier or identityref |
| 677 | * value, the JSON format is expected (module names instead of prefixes). |
| 678 | * @return New created node. |
| 679 | * @return NULL on error. |
| 680 | */ |
| 681 | struct lyd_node *lyd_new_term(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str); |
| 682 | |
| 683 | /** |
| 684 | * @brief Create a new any node in a data tree. |
| 685 | * |
| 686 | * @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] | 687 | * @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] | 688 | * @param[in] name Schema node name of the new data node. The node can be #LYS_ANYDATA or #LYS_ANYXML. |
| 689 | * @param[in] value Value to be directly assigned to the node. Expected type is determined by @p value_type. |
| 690 | * @param[in] value_type Type of the provided value in @p value. |
| 691 | * @return New created node. |
| 692 | * @return NULL on error. |
| 693 | */ |
| 694 | struct lyd_node *lyd_new_any(struct lyd_node *parent, const struct lys_module *module, const char *name, |
| 695 | const void *value, LYD_ANYDATA_VALUETYPE value_type); |
| 696 | |
| 697 | /** |
Michal Vasko | d86997b | 2020-05-26 15:19:54 +0200 | [diff] [blame] | 698 | * @brief Create new metadata for a data node. |
| 699 | * |
| 700 | * @param[in] parent Parent node for the metadata being created. |
| 701 | * @param[in] module Module of the metdata being created. If NULL, @p name must include module name as the prefix. |
| 702 | * @param[in] name Annotation name of the new metadata. It can include the annotation module as the prefix. |
| 703 | * If the prefix is specified it is always used but if not specified, @p module must be set. |
| 704 | * @param[in] val_str String form of the value of the metadata being created. In case of an instance-identifier or identityref |
| 705 | * value, the JSON format is expected (module names instead of prefixes). |
| 706 | * @return New created metadata in the @p parent. |
| 707 | * @return NULL on error. |
| 708 | */ |
| 709 | struct lyd_meta *lyd_new_meta(struct lyd_node *parent, const struct lys_module *module, const char *name, |
| 710 | const char *val_str); |
| 711 | |
| 712 | /** |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 713 | * @brief Insert a child into a parent. It is inserted as the last child. |
| 714 | * |
| 715 | * - if the node is part of some other tree, it is automatically unlinked. |
| 716 | * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted. |
| 717 | * |
| 718 | * @param[in] parent Parent node to insert into. |
| 719 | * @param[in] node Node to insert. |
| 720 | * @return LY_SUCCESS on success. |
| 721 | * @return LY_ERR error on error. |
| 722 | */ |
| 723 | LY_ERR lyd_insert(struct lyd_node *parent, struct lyd_node *node); |
| 724 | |
| 725 | /** |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 726 | * @brief Insert a node into siblings. It is inserted as the last sibling. |
| 727 | * |
| 728 | * - if the node is part of some other tree, it is automatically unlinked. |
| 729 | * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted. |
| 730 | * |
| 731 | * @param[in] sibling Siblings to insert into. |
| 732 | * @param[in] node Node to insert. |
| 733 | * @return LY_SUCCESS on success. |
| 734 | * @return LY_ERR error on error. |
| 735 | */ |
| 736 | LY_ERR lyd_insert_sibling(struct lyd_node *sibling, struct lyd_node *node); |
| 737 | |
| 738 | /** |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 739 | * @brief Insert a node before another node that is its schema sibling. |
| 740 | * |
| 741 | * - if the node is part of some other tree, it is automatically unlinked. |
| 742 | * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted. |
| 743 | * |
| 744 | * @param[in] sibling Sibling node to insert before. |
| 745 | * @param[in] node Node to insert. |
| 746 | * @return LY_SUCCESS on success. |
| 747 | * @return LY_ERR error on error. |
| 748 | */ |
| 749 | LY_ERR lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node); |
| 750 | |
| 751 | /** |
| 752 | * @brief Insert a node after another node that is its schema sibling. |
| 753 | * |
| 754 | * - if the node is part of some other tree, it is automatically unlinked. |
| 755 | * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted. |
| 756 | * |
| 757 | * @param[in] sibling Sibling node to insert after. |
| 758 | * @param[in] node Node to insert. |
| 759 | * @return LY_SUCCESS on success. |
| 760 | * @return LY_ERR error on error. |
| 761 | */ |
| 762 | LY_ERR lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node); |
| 763 | |
| 764 | /** |
| 765 | * @brief Unlink the specified data subtree. |
| 766 | * |
| 767 | * @param[in] node Data tree node to be unlinked (together with all the children). |
| 768 | */ |
| 769 | void lyd_unlink_tree(struct lyd_node *node); |
| 770 | |
| 771 | /** |
Radek Krejci | b0849a2 | 2019-07-25 12:31:04 +0200 | [diff] [blame] | 772 | * @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] | 773 | * |
| 774 | * @param[in] node Any of the nodes inside the tree. |
| 775 | */ |
| 776 | void lyd_free_all(struct lyd_node *node); |
| 777 | |
| 778 | /** |
Radek Krejci | b0849a2 | 2019-07-25 12:31:04 +0200 | [diff] [blame] | 779 | * @brief Free all the sibling nodes. |
| 780 | * |
| 781 | * @param[in] node Any of the sibling nodes to free. |
| 782 | */ |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 783 | void lyd_free_siblings(struct lyd_node *node); |
Radek Krejci | b0849a2 | 2019-07-25 12:31:04 +0200 | [diff] [blame] | 784 | |
| 785 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 786 | * @brief Free (and unlink) the specified data (sub)tree. |
| 787 | * |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 788 | * @param[in] node Root of the (sub)tree to be freed. |
| 789 | */ |
| 790 | void lyd_free_tree(struct lyd_node *node); |
| 791 | |
| 792 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 793 | * @brief Destroy metadata. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 794 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 795 | * @param[in] ctx Context where the metadata was created. |
| 796 | * @param[in] meta Metadata to destroy |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 797 | * @param[in] recursive Zero to destroy only the single metadata (the metadata list is corrected), |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 798 | * non-zero to destroy also all the subsequent metadata in the list. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 799 | */ |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 800 | void lyd_free_meta(const struct ly_ctx *ctx, struct lyd_meta *meta, int recursive); |
| 801 | |
| 802 | /** |
| 803 | * @brief Destroy attributes. |
| 804 | * |
| 805 | * @param[in] ctx Context where the attributes were created. |
| 806 | * @param[in] attr Attributes to destroy. |
| 807 | * @param[in] recursive Zero to destroy only the single attribute (the attribute list is corrected), |
| 808 | * non-zero to destroy also all the subsequent attributes in the list. |
| 809 | */ |
| 810 | void ly_free_attr(const struct ly_ctx *ctx, struct ly_attr *attr, int recursive); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 811 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 812 | /** |
| 813 | * @brief Check type restrictions applicable to the particular leaf/leaf-list with the given string @p value. |
| 814 | * |
| 815 | * The given node is not modified in any way - it is just checked if the @p value can be set to the node. |
| 816 | * |
| 817 | * If there is no data node instance and you are fine with checking just the type's restrictions without the |
| 818 | * data tree context (e.g. for the case of require-instance restriction), use lys_value_validate(). |
| 819 | * |
| 820 | * @param[in] ctx libyang context for logging (function does not log errors when @p ctx is NULL) |
| 821 | * @param[in] node Data node for the @p value. |
| 822 | * @param[in] value String value to be checked. |
| 823 | * @param[in] value_len Length of the given @p value (mandatory). |
| 824 | * @param[in] get_prefix Callback function to resolve prefixes used in the @p value string. |
| 825 | * @param[in] get_prefix_data Private data for the @p get_prefix callback. |
| 826 | * @param[in] format Input format of the data. |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 827 | * @param[in] tree Data tree (e.g. when validating RPC/Notification) where the required data instance (leafref target, |
| 828 | * instance-identifier) can be placed. NULL in case the data tree is not yet complete, |
| 829 | * then LY_EINCOMPLETE can be returned. |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 830 | * @return LY_SUCCESS on success |
| 831 | * @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). |
| 832 | * @return LY_ERR value if an error occurred. |
| 833 | */ |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 834 | 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 | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 835 | ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format, const struct lyd_node *tree); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 836 | |
| 837 | /** |
| 838 | * @brief Compare the node's value with the given string value. The string value is first validated according to the node's type. |
| 839 | * |
| 840 | * @param[in] node Data node to compare. |
| 841 | * @param[in] value String value to be compared. It does not need to be in a canonical form - as part of the process, |
| 842 | * it is validated and canonized if possible. |
| 843 | * @param[in] value_len Length of the given @p value (mandatory). |
| 844 | * @param[in] get_prefix Callback function to resolve prefixes used in the @p value string. |
| 845 | * @param[in] get_prefix_data Private data for the @p get_prefix callback. |
| 846 | * @param[in] format Input format of the data. |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 847 | * @param[in] tree Data tree (e.g. when validating RPC/Notification) where the required data instance (leafref target, |
| 848 | * instance-identifier) can be placed. NULL in case the data tree is not yet complete, |
| 849 | * then LY_EINCOMPLETE can be returned. |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 850 | * @return LY_SUCCESS on success |
| 851 | * @return LY_EINCOMPLETE in case of success when the @p trees is not provided and it was needed to finish the validation of |
| 852 | * the given string @p value (e.g. due to require-instance). |
| 853 | * @return LY_ERR value if an error occurred. |
| 854 | */ |
| 855 | LY_ERR lyd_value_compare(const struct lyd_node_term *node, const char *value, size_t value_len, |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 856 | ly_clb_resolve_prefix get_prefix, void *get_prefix_data, LYD_FORMAT format, const struct lyd_node *tree); |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 857 | |
Radek Krejci | 576b23f | 2019-07-12 14:06:32 +0200 | [diff] [blame] | 858 | /** |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 859 | * @defgroup datacompareoptions Data compare options |
| 860 | * @ingroup datatree |
| 861 | * |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 862 | * Various options to change the lyd_compare() behavior. |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 863 | */ |
| 864 | #define LYD_COMPARE_FULL_RECURSION 0x01 /* lists and containers are the same only in case all they children |
| 865 | (subtree, so direct as well as indirect children) are the same. By default, |
| 866 | containers are the same in case of the same schema node and lists are the same |
| 867 | in case of equal keys (keyless lists do the full recursion comparison all the time). */ |
| 868 | #define LYD_COMPARE_DEFAULTS 0x02 /* By default, implicit and explicit default nodes are considered to be equal. This flag |
| 869 | changes this behavior and implicit (automatically created default node) and explicit |
| 870 | (explicitly created node with the default value) default nodes are considered different. */ |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 871 | /**@} datacompareoptions */ |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 872 | |
| 873 | /** |
| 874 | * @brief Compare 2 data nodes if they are equivalent. |
| 875 | * |
| 876 | * @param[in] node1 The first node to compare. |
| 877 | * @param[in] node2 The second node to compare. |
Radek Krejci | c5ad965 | 2019-09-11 11:31:51 +0200 | [diff] [blame] | 878 | * @param[in] options Various @ref datacompareoptions. |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 879 | * @return LY_SUCCESS if the nodes are equivalent. |
| 880 | * @return LY_ENOT if the nodes are not equivalent. |
| 881 | */ |
| 882 | LY_ERR lyd_compare(const struct lyd_node *node1, const struct lyd_node *node2, int options); |
| 883 | |
| 884 | /** |
Radek Krejci | 22ebdba | 2019-07-25 13:59:43 +0200 | [diff] [blame] | 885 | * @defgroup dupoptions Data duplication options |
| 886 | * @ingroup datatree |
| 887 | * |
| 888 | * Various options to change lyd_dup() behavior. |
| 889 | * |
| 890 | * Default behavior: |
| 891 | * - only the specified node is duplicated without siblings, parents, or children. |
| 892 | * - all the attributes of the duplicated nodes are also duplicated. |
| 893 | * @{ |
| 894 | */ |
| 895 | |
| 896 | #define LYD_DUP_RECURSIVE 0x01 /**< Duplicate not just the node but also all the children. Note that |
| 897 | list's keys are always duplicated. */ |
| 898 | #define LYD_DUP_NO_ATTR 0x02 /**< Do not duplicate attributes of any node. */ |
| 899 | #define LYD_DUP_WITH_PARENTS 0x04 /**< If a nested node is being duplicated, duplicate also all the parents. |
| 900 | Keys are also duplicated for lists. Return value does not change! */ |
| 901 | #define LYD_DUP_WITH_SIBLINGS 0x08 /**< Duplicate also all the sibling of the given node. */ |
| 902 | #define LYD_DUP_WITH_WHEN 0x10 /**< Also copy any when evaluation state flags. This is useful in case the copied |
| 903 | nodes are actually still part of the same datastore meaning no dependency data |
| 904 | could have changed. Otherwise nothing is assumed about the copied node when |
| 905 | state and it is evaluated from scratch during validation. */ |
| 906 | |
| 907 | /** @} dupoptions */ |
| 908 | |
| 909 | /** |
| 910 | * @brief Create a copy of the specified data tree \p node. Schema references are kept the same. |
| 911 | * |
| 912 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
| 913 | * |
| 914 | * @param[in] node Data tree node to be duplicated. |
| 915 | * @param[in] parent Optional parent node where to connect the duplicated node(s). |
| 916 | * If set in combination with LYD_DUP_WITH_PARENTS, the parents chain is duplicated until it comes to and connect with the @p parent |
| 917 | * (if the parents chain does not match at some node the schema node of the provided @p parent, duplication fails). |
| 918 | * @param[in] options Bitmask of options flags, see @ref dupoptions. |
| 919 | * @return Created copy of the provided data \p node (the first of the duplicated siblings when LYD_DUP_WITH_SIBLINGS used). |
| 920 | * Note that in case the parents chain is duplicated for the duplicated node(s) (when LYD_DUP_WITH_PARENTS used), the first duplicated node |
| 921 | * is still returned, not a pointer to the duplicated parents. |
| 922 | */ |
| 923 | struct lyd_node *lyd_dup(const struct lyd_node *node, struct lyd_node_inner *parent, int options); |
| 924 | |
| 925 | /** |
Radek Krejci | 576b23f | 2019-07-12 14:06:32 +0200 | [diff] [blame] | 926 | * @brief Resolve instance-identifier defined by lyd_value_path structure. |
| 927 | * |
| 928 | * @param[in] path Path structure specifying the instance-identifier target. |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 929 | * @param[in] tree Data tree to be searched. |
| 930 | * @return Target node of the instance-identifier present in the given data @p tree. |
Radek Krejci | 576b23f | 2019-07-12 14:06:32 +0200 | [diff] [blame] | 931 | */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 932 | 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] | 933 | |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 934 | /** |
| 935 | * @brief Get string value of a term data \p node. |
| 936 | * |
| 937 | * @param[in] node Data tree node with the value. |
| 938 | * @param[out] dynamic Whether the string value was dynmically allocated. |
| 939 | * @return String value of @p node, if @p dynamic, needs to be freed. |
| 940 | */ |
| 941 | const char *lyd_value2str(const struct lyd_node_term *node, int *dynamic); |
| 942 | |
| 943 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 944 | * @brief Get string value of a metadata \p meta. |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 945 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 946 | * @param[in] meta Metadata with the value. |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 947 | * @param[out] dynamic Whether the string value was dynmically allocated. |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 948 | * @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] | 949 | */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 950 | const char *lyd_meta2str(const struct lyd_meta *meta, int *dynamic); |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 951 | |
| 952 | /** |
| 953 | * @brief Types of the different data paths. |
| 954 | */ |
| 955 | typedef enum { |
Michal Vasko | 1465471 | 2020-02-06 08:35:21 +0100 | [diff] [blame] | 956 | LYD_PATH_LOG, /**< Descriptive path format used in log messages */ |
Michal Vasko | 5ec7cda | 2019-09-11 13:43:08 +0200 | [diff] [blame] | 957 | } LYD_PATH_TYPE; |
| 958 | |
| 959 | /** |
| 960 | * @brief Generate path of the given node in the requested format. |
| 961 | * |
| 962 | * @param[in] node Schema path of this node will be generated. |
| 963 | * @param[in] pathtype Format of the path to generate. |
| 964 | * @param[in,out] buffer Prepared buffer of the @p buflen length to store the generated path. |
| 965 | * If NULL, memory for the complete path is allocated. |
| 966 | * @param[in] buflen Size of the provided @p buffer. |
| 967 | * @return NULL in case of memory allocation error, path of the node otherwise. |
| 968 | * In case the @p buffer is NULL, the returned string is dynamically allocated and caller is responsible to free it. |
| 969 | */ |
| 970 | char *lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen); |
| 971 | |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 972 | /** |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 973 | * @brief Find the node, in the list, satisfying the given restrictions. |
| 974 | * Does **not** use hashes - should not be used unless necessary for best performance. |
| 975 | * |
| 976 | * @param[in] first Starting sibling node for search, only succeeding ones are searched. |
| 977 | * @param[in] module Module of the node to find. |
| 978 | * @param[in] name Name of the node to find. |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 979 | * @param[in] name_len Optional length of @p name in case it is not 0-terminated string. |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 980 | * @param[in] key_or_value Expected value depends on the type of @p name node: |
| 981 | * LYS_CONTAINER: |
| 982 | * LYS_ANYXML: |
| 983 | * LYS_ANYDATA: |
| 984 | * LYS_NOTIF: |
| 985 | * LYS_RPC: |
| 986 | * LYS_ACTION: |
| 987 | * NULL should be always set, will be ignored. |
| 988 | * LYS_LEAF: |
| 989 | * LYS_LEAFLIST: |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 990 | * Optional restriction on the specific leaf(-list) value. |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 991 | * LYS_LIST: |
| 992 | * Optional keys values of the matching list instances in the form of "[key1='val1'][key2='val2']...". |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 993 | * The keys do not have to be ordered and not all keys need to be specified. |
| 994 | * |
| 995 | * Note that any explicit values (leaf, leaf-list or list key values) will be canonized first |
| 996 | * before comparison. But values that do not have a canonical value are expected to be in the |
| 997 | * JSON format! |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 998 | * @param[in] val_len Optional length of @p key_or_value in case it is not 0-terminated string. |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 999 | * @param[out] match Found data node. |
| 1000 | * @return LY_SUCCESS on success, @p match set. |
| 1001 | * @return LY_ENOTFOUND if not found, @p match set to NULL. |
| 1002 | * @return LY_ERR value if another error occurred. |
| 1003 | */ |
| 1004 | LY_ERR lyd_find_sibling_next(const struct lyd_node *first, const struct lys_module *module, const char *name, |
| 1005 | size_t name_len, const char *key_or_value, size_t val_len, struct lyd_node **match); |
| 1006 | |
| 1007 | /** |
| 1008 | * @brief Search in the given siblings (NOT recursively) for the first target instance. |
| 1009 | * Uses hashes - should be used whenever possible for best performance. |
| 1010 | * |
| 1011 | * @param[in] siblings Siblings to search in including preceding and succeeding nodes. |
| 1012 | * @param[in] target Target node to find. |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 1013 | * @param[out] match Can be NULL, otherwise the found data node. |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 1014 | * @return LY_SUCCESS on success, @p match set. |
| 1015 | * @return LY_ENOTFOUND if not found, @p match set to NULL. |
| 1016 | * @return LY_ERR value if another error occurred. |
| 1017 | */ |
| 1018 | LY_ERR lyd_find_sibling_first(const struct lyd_node *siblings, const struct lyd_node *target, struct lyd_node **match); |
| 1019 | |
| 1020 | /** |
| 1021 | * @brief Search in the given siblings for all target instances. |
| 1022 | * Uses hashes - should be used whenever possible for best performance. |
| 1023 | * |
| 1024 | * @param[in] siblings Siblings to search in including preceding and succeeding nodes. |
| 1025 | * @param[in] target Target node to find. Key-less lists are compared based on |
| 1026 | * all its descendants (both direct and indirect). |
| 1027 | * @param[out] set Found nodes in a set in case of success. |
| 1028 | * @return LY_SUCCESS on success. |
| 1029 | * @return LY_ENOTFOUND if no matching siblings found. |
| 1030 | * @return LY_ERR value if another error occurred. |
| 1031 | */ |
| 1032 | LY_ERR lyd_find_sibling_set(const struct lyd_node *siblings, const struct lyd_node *target, struct ly_set **set); |
| 1033 | |
| 1034 | /** |
| 1035 | * @brief Search in the given siblings for the first schema instance. |
| 1036 | * Uses hashes - should be used whenever possible for best performance. |
| 1037 | * |
| 1038 | * @param[in] siblings Siblings to search in including preceding and succeeding nodes. |
| 1039 | * @param[in] schema Schema node of the data node to find. |
| 1040 | * @param[in] key_or_value Expected value depends on the type of \p schema: |
| 1041 | * LYS_CONTAINER: |
| 1042 | * LYS_LEAF: |
| 1043 | * LYS_ANYXML: |
| 1044 | * LYS_ANYDATA: |
| 1045 | * LYS_NOTIF: |
| 1046 | * LYS_RPC: |
| 1047 | * LYS_ACTION: |
| 1048 | * NULL should be always set, will be ignored. |
| 1049 | * LYS_LEAFLIST: |
| 1050 | * Searched instance value. |
| 1051 | * LYS_LIST: |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 1052 | * Searched instance key values in the form of "[key1='val1'][key2='val2']...". |
| 1053 | * The keys do not have to be ordered but all of them must be set. |
| 1054 | * |
| 1055 | * Note that any explicit values (leaf-list or list key values) will be canonized first |
| 1056 | * before comparison. But values that do not have a canonical value are expected to be in the |
| 1057 | * JSON format! |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 1058 | * @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] | 1059 | * @param[out] match Can be NULL, otherwise the found data node. |
Michal Vasko | e444f75 | 2020-02-10 12:20:06 +0100 | [diff] [blame] | 1060 | * @return LY_SUCCESS on success, @p match set. |
| 1061 | * @return LY_ENOTFOUND if not found, @p match set to NULL. |
| 1062 | * @return LY_EINVAL if @p schema is a key-less list. |
| 1063 | * @return LY_ERR value if another error occurred. |
| 1064 | */ |
| 1065 | LY_ERR lyd_find_sibling_val(const struct lyd_node *siblings, const struct lysc_node *schema, const char *key_or_value, |
| 1066 | size_t val_len, struct lyd_node **match); |
| 1067 | |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 1068 | /** |
| 1069 | * @brief Search in the given data for instances of nodes matching the provided XPath. |
| 1070 | * |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 1071 | * The expected format of the expression is JSON (::LYD_JSON) meaning the first node in every path |
| 1072 | * must have its module name as prefix or be the special `*` value for all the nodes. |
| 1073 | * |
Michal Vasko | 19a3060 | 2020-05-25 10:40:19 +0200 | [diff] [blame] | 1074 | * If a list instance is being selected with all its key values specified (but not necessarily ordered) |
| 1075 | * in the form `list[key1='val1'][key2='val2'][key3='val3']` or a leaf-list instance in the form |
| 1076 | * `leaf-list[.='val']`, these instances are found using hashes with constant (*O(1)*) complexity |
| 1077 | * (unless they are defined in top-level). Other predicates can still follow the aforementioned ones. |
| 1078 | * |
Michal Vasko | ccc0234 | 2020-05-21 10:09:21 +0200 | [diff] [blame] | 1079 | * @param[in] ctx_node XPath context node. |
| 1080 | * @param[in] xpath Data XPath expression filtering the matching nodes. ::LYD_JSON format is expected. |
| 1081 | * @param[out] set Set of found data nodes. In case the result is a number, a string, or a boolean, |
| 1082 | * the returned set is empty. |
| 1083 | * @return LY_SUCCESS on success, @p set is returned. |
| 1084 | * @return LY_ERR value if an error occurred. |
| 1085 | */ |
| 1086 | LY_ERR lyd_find_xpath(const struct lyd_node *ctx_node, const char *xpath, struct ly_set **set); |
| 1087 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1088 | #ifdef __cplusplus |
| 1089 | } |
| 1090 | #endif |
| 1091 | |
| 1092 | #endif /* LY_TREE_DATA_H_ */ |