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