Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_data_internal.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief internal functions for YANG schema 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_INTERNAL_H_ |
| 16 | #define LY_TREE_DATA_INTERNAL_H_ |
| 17 | |
| 18 | #include "tree_data.h" |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 19 | #include "plugins_types.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * @brief Get address of a node's child pointer if any. |
| 23 | * |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 24 | * @param[in] node Node to check. |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame^] | 25 | * @return Address of the node's child member, |
| 26 | * @return NULL if there is no child pointer. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 27 | */ |
| 28 | struct lyd_node **lyd_node_children_p(struct lyd_node *node); |
| 29 | |
| 30 | /** |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame^] | 31 | * @brief Create a term (leaf/leaf-list) node from a string value. |
| 32 | * |
| 33 | * Hash is calculated and new node flag is set. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 34 | * |
| 35 | * @param[in] schema Schema node of the new data node. |
| 36 | * @param[in] value String value to be parsed. |
| 37 | * @param[in] value_len Length of @p value. |
| 38 | * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed. |
| 39 | * @param[in] get_prefix Parser-specific getter to resolve prefixes used in the @p value string. |
| 40 | * @param[in] prefix_data User data for @p get_prefix. |
| 41 | * @param[in] format Input format of @p value. |
| 42 | * @param[out] node Created node. |
| 43 | * @return LY_SUCCESS on success. |
| 44 | * @return LY_EINCOMPLETE in case data tree is needed to finish the validation. |
| 45 | * @return LY_ERR value if an error occurred. |
| 46 | */ |
| 47 | LY_ERR lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic, |
| 48 | ly_clb_resolve_prefix get_prefix, void *prefix_data, LYD_FORMAT format, struct lyd_node **node); |
| 49 | |
| 50 | /** |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame^] | 51 | * @brief Create a term (leaf/leaf-list) node from a parsed value by duplicating it. |
| 52 | * |
| 53 | * Hash is calculated and new node flag is set. |
| 54 | * |
| 55 | * @param[in] schema Schema node of the new data node. |
| 56 | * @param[in] val Parsed value to use. |
| 57 | * @param[out] node Created node. |
| 58 | * @return LY_SUCCESS on success. |
| 59 | * @return LY_ERR value if an error occurred. |
| 60 | */ |
| 61 | LY_ERR lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node); |
| 62 | |
| 63 | /** |
| 64 | * @brief Create an inner (container/list/RPC/action/notification) node. |
| 65 | * |
| 66 | * Hash is calculated and new node flag is set except |
| 67 | * for list with keys, when the hash is not calculated! |
| 68 | * Also, non-presence container has its default flag set. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 69 | * |
| 70 | * @param[in] schema Schema node of the new data node. |
| 71 | * @param[out] node Created node. |
| 72 | * @return LY_SUCCESS on success. |
| 73 | * @return LY_ERR value if an error occurred. |
| 74 | */ |
| 75 | LY_ERR lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node); |
| 76 | |
| 77 | /** |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame^] | 78 | * @brief Create a list with all its keys (cannot be used for key-less list). |
| 79 | * |
| 80 | * Hash is calculated and new node flag is set. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 81 | * |
| 82 | * @param[in] schema Schema node of the new data node. |
| 83 | * @param[in] keys_str List instance key values in the form of "[key1='val1'][key2='val2']...". |
| 84 | * The keys do not have to be ordered but all of them must be set. |
| 85 | * @param[in] keys_len Length of @p keys_str. |
| 86 | * @param[out] node Created node. |
| 87 | * @return LY_SUCCESS on success. |
| 88 | * @return LY_ERR value if an error occurred. |
| 89 | */ |
| 90 | LY_ERR lyd_create_list(const struct lysc_node *schema, const char *keys_str, size_t keys_len, struct lyd_node **node); |
| 91 | |
| 92 | /** |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame^] | 93 | * @brief Create an anyxml/anydata node. |
| 94 | * |
| 95 | * Hash is calculated and flags are properly set based on @p is_valid. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 96 | * |
| 97 | * @param[in] schema Schema node of the new data node. |
| 98 | * @param[in] value Value of the any node, is directly assigned into the data node. |
| 99 | * @param[in] value_type Value type of the value. |
| 100 | * @param[out] node Created node. |
| 101 | * @return LY_SUCCESS on success. |
| 102 | * @return LY_ERR value if an error occurred. |
| 103 | */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame^] | 104 | LY_ERR lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, |
| 105 | struct lyd_node **node); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 106 | |
| 107 | /** |
| 108 | * @brief Find the key after which to insert the new key. |
| 109 | * |
| 110 | * @param[in] first_sibling List first sibling. |
| 111 | * @param[in] new_key Key that will be inserted. |
| 112 | * @return Key to insert after. |
| 113 | * @return NULL if the new key should be first. |
| 114 | */ |
| 115 | struct lyd_node *lyd_get_prev_key_anchor(const struct lyd_node *first_sibling, const struct lysc_node *new_key); |
| 116 | |
| 117 | /** |
| 118 | * @brief Insert a node into parent/siblings. In case a key is being inserted into a list, the correct position |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame^] | 119 | * is found and inserted into. Also, in case we are inserting into top-level siblings, insert it as |
| 120 | * the last sibling of all the module data siblings. Otherwise it is inserted at the very last place. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 121 | * |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame^] | 122 | * @param[in] parent Parent to insert into, NULL for top-level sibling. |
| 123 | * @param[in,out] first_sibling First sibling, NULL if no top-level sibling exist yet. Can be also NULL if @p parent is set. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 124 | * @param[in] node Individual node (without siblings) to insert. |
| 125 | */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame^] | 126 | void lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 127 | |
| 128 | /** |
| 129 | * @brief Create and insert an attribute (last) into a parent. |
| 130 | * |
| 131 | * @param[in] parent Parent of the attribute. |
| 132 | * @param[in] mod Attribute module (with the annotation definition). |
| 133 | * @param[in] name Attribute name. |
| 134 | * @param[in] name_len Length of @p name. |
| 135 | * @param[in] value String value to be parsed. |
| 136 | * @param[in] value_len Length of @p value. |
| 137 | * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed. |
| 138 | * @param[in] get_prefix Parser-specific getter to resolve prefixes used in the @p value string. |
| 139 | * @param[in] prefix_data User data for @p get_prefix. |
| 140 | * @param[in] format Input format of @p value. |
| 141 | * @param[out] attr CReated attribute. |
| 142 | * @return LY_SUCCESS on success. |
| 143 | * @return LY_EINCOMPLETE in case data tree is needed to finish the validation. |
| 144 | * @return LY_ERR value if an error occurred. |
| 145 | */ |
| 146 | LY_ERR lyd_create_attr(struct lyd_node *parent, const struct lys_module *mod, const char *name, size_t name_len, |
| 147 | const char *value, size_t value_len, int *dynamic, ly_clb_resolve_prefix get_prefix, void *prefix_data, |
| 148 | LYD_FORMAT format, struct lyd_attr **attr); |
| 149 | |
| 150 | /** |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 151 | * @brief Validate, canonize and store the given @p value into the node according to the node's type's rules. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 152 | * |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 153 | * @param[in] node Data node for the @p value. |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 154 | * @param[in] value String value to be parsed, must not be NULL. |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 155 | * @param[in] value_len Length of the give @p value (mandatory). |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 156 | * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed. |
Radek Krejci | 3c9758d | 2019-07-11 16:49:10 +0200 | [diff] [blame] | 157 | * @param[in] second Flag for the second call after returning LY_EINCOMPLETE |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 158 | * @param[in] get_prefix Parser-specific getter to resolve prefixes used in the @p value string. |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 159 | * @param[in] parser Parser's data for @p get_prefix |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 160 | * @param[in] format Input format of @p value. |
Radek Krejci | e553e6d | 2019-06-07 15:33:18 +0200 | [diff] [blame] | 161 | * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees (e.g. when validating RPC/Notification) where the required |
| 162 | * data instance (leafref target, instance-identifier) can be placed. NULL in case the data tree are not yet complete, |
| 163 | * then LY_EINCOMPLETE can be returned. |
| 164 | * @return LY_SUCCESS on success |
| 165 | * @return LY_EINCOMPLETE in case the @p trees is not provided and it was needed to finish the validation. |
| 166 | * @return LY_ERR value if an error occurred. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 167 | */ |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 168 | LY_ERR lyd_value_parse(struct lyd_node_term *node, const char *value, size_t value_len, int *dynamic, int second, |
Radek Krejci | 576b23f | 2019-07-12 14:06:32 +0200 | [diff] [blame] | 169 | ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node **trees); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 170 | |
| 171 | /** |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 172 | * @brief Validate, canonize and store the given @p value into the attribute according to the metadata annotation type's rules. |
| 173 | * |
| 174 | * @param[in] attr Data attribute for the @p value. |
| 175 | * @param[in] value String value to be parsed, must not be NULL. |
| 176 | * @param[in] value_len Length of the give @p value (mandatory). |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 177 | * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed. |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 178 | * @param[in] second Flag for the second call after returning LY_EINCOMPLETE |
| 179 | * @param[in] get_prefix Parser-specific getter to resolve prefixes used in the @p value string. |
| 180 | * @param[in] parser Parser's data for @p get_prefix |
| 181 | * @param[in] format Input format of the data. |
| 182 | * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees (e.g. when validating RPC/Notification) where the required |
| 183 | * data instance (leafref target, instance-identifier) can be placed. NULL in case the data tree are not yet complete, |
| 184 | * then LY_EINCOMPLETE can be returned. |
| 185 | * @return LY_SUCCESS on success |
| 186 | * @return LY_EINCOMPLETE in case the @p trees is not provided and it was needed to finish the validation. |
| 187 | * @return LY_ERR value if an error occurred. |
| 188 | */ |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 189 | LY_ERR lyd_value_parse_attr(struct lyd_attr *attr, const char *value, size_t value_len, int *dynamic, int second, |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 190 | ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node **trees); |
| 191 | |
| 192 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 193 | * @brief Parse XML string as YANG data tree. |
| 194 | * |
| 195 | * @param[in] ctx libyang context |
| 196 | * @param[in] data Pointer to the XML string representation of the YANG data to parse. |
| 197 | * @param[in] options @ref dataparseroptions |
| 198 | * @param[out] result Resulting list of the parsed data trees. Note that NULL can be a valid result. |
| 199 | * @reutn LY_ERR value. |
| 200 | */ |
Michal Vasko | a388136 | 2020-01-21 15:57:35 +0100 | [diff] [blame] | 201 | LY_ERR lyd_parse_xml(struct ly_ctx *ctx, const char *data, int options, struct lyd_node **result); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 202 | |
| 203 | /** |
| 204 | * @defgroup datahash Data nodes hash manipulation |
| 205 | * @ingroup datatree |
| 206 | */ |
| 207 | |
| 208 | /** |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 209 | * @brief Generate hash for the node. |
| 210 | * |
| 211 | * @param[in] node Data node to (re)generate hash value. |
| 212 | * @return LY_ERR value. |
| 213 | */ |
| 214 | LY_ERR lyd_hash(struct lyd_node *node); |
| 215 | |
| 216 | /** |
| 217 | * @brief Insert hash of the node into the hash table of its parent. |
| 218 | * |
| 219 | * @param[in] node Data node which hash will be inserted into the lyd_node_inner::children_hash hash table of its parent. |
| 220 | * @return LY_ERR value. |
| 221 | */ |
| 222 | LY_ERR lyd_insert_hash(struct lyd_node *node); |
| 223 | |
| 224 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 225 | * @brief Maintain node's parent's children hash table when unlinking the node. |
| 226 | * |
| 227 | * When completely freeing data tree, it is expected to free the parent's children hash table first, at once. |
| 228 | * |
| 229 | * @param[in] node The data node being unlinked from its parent. |
| 230 | */ |
| 231 | void lyd_unlink_hash(struct lyd_node *node); |
| 232 | |
| 233 | /** @} datahash */ |
| 234 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 235 | /** |
| 236 | * @brief Free path (target) structure of the lyd_value. |
| 237 | * |
| 238 | * @param[in] ctx libyang context. |
| 239 | * @param[in] path The structure ([sized array](@ref sizedarrays)) to free. |
| 240 | */ |
| 241 | void lyd_value_free_path(struct ly_ctx *ctx, struct lyd_value_path *path); |
| 242 | |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame^] | 243 | /** |
| 244 | * @brief Find the node, in the list, satisfying the given restrictions. |
| 245 | * Does **not** use hashes - should not be used unless necessary for best performance. |
| 246 | * |
| 247 | * @param[in] first Starting sibling node for search, only succeeding ones are searched. |
| 248 | * @param[in] schema Schema node of the data node to find. |
| 249 | * @param[in] key_or_value Expected value depends on the type of @p schema: |
| 250 | * LYS_CONTAINER: |
| 251 | * LYS_ANYXML: |
| 252 | * LYS_ANYDATA: |
| 253 | * LYS_NOTIF: |
| 254 | * LYS_RPC: |
| 255 | * LYS_ACTION: |
| 256 | * NULL should be always set, will be ignored. |
| 257 | * LYS_LEAF: |
| 258 | * LYS_LEAFLIST: |
| 259 | * Optional restriction on the specific leaf(-list) value. |
| 260 | * LYS_LIST: |
| 261 | * Optional keys values of the matching list instances in the form of "[key1='val1'][key2='val2']...". |
| 262 | * The keys do not have to be ordered and not all keys need to be specified. |
| 263 | * |
| 264 | * Note that any explicit values (leaf, leaf-list or list key values) will be canonized first |
| 265 | * before comparison. But values that do not have a canonical value are expected to be in the |
| 266 | * JSON format! |
| 267 | * @param[in] val_len Optional length of the @p key_or_value argument in case it is not NULL-terminated string. |
| 268 | * @param[out] match Can be NULL, otherwise the found data node. |
| 269 | * @return LY_SUCCESS on success, @p match set. |
| 270 | * @return LY_ENOTFOUND if not found, @p match set to NULL. |
| 271 | * @return LY_ERR value if another error occurred. |
| 272 | */ |
| 273 | LY_ERR lyd_find_sibling_next2(const struct lyd_node *first, const struct lysc_node *schema, const char *key_or_value, |
| 274 | size_t val_len, struct lyd_node **match); |
| 275 | |
| 276 | /** |
| 277 | * @brief Get the module, whose data this top-level node belongs to. Useful for augments, when the augmented |
| 278 | * module is the data owner. Handles top-level choice augments. |
| 279 | * |
| 280 | * @param[in] node Data node to examine. |
| 281 | * @return Module owner of the node. |
| 282 | */ |
| 283 | const struct lys_module *lyd_top_node_module(const struct lyd_node *node); |
| 284 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 285 | #endif /* LY_TREE_DATA_INTERNAL_H_ */ |