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