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 | * |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2020 CESNET, z.s.p.o. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 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 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 18 | #include "log.h" |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 19 | #include "plugins_types.h" |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 20 | #include "set.h" |
| 21 | #include "tree_data.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 23 | #include <stddef.h> |
| 24 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 25 | struct ly_path_predicate; |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 26 | struct lysc_module; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 27 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 28 | #define LY_XML_SUFFIX ".xml" |
| 29 | #define LY_XML_SUFFIX_LEN 4 |
| 30 | #define LY_JSON_SUFFIX ".json" |
| 31 | #define LY_JSON_SUFFIX_LEN 5 |
| 32 | #define LY_LYB_SUFFIX ".lyb" |
| 33 | #define LY_LYB_SUFFIX_LEN 4 |
| 34 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 35 | /** |
Michal Vasko | d7c048c | 2021-05-18 16:12:55 +0200 | [diff] [blame] | 36 | * @brief Internal structure for remembering "used" instances of lists with duplicate instances allowed. |
| 37 | */ |
| 38 | struct lyd_dup_inst { |
| 39 | struct ly_set *inst_set; |
| 40 | uint32_t used; |
| 41 | }; |
| 42 | |
| 43 | /** |
| 44 | * @brief Update a found inst using a duplicate instance cache. Needs to be called for every "used" |
| 45 | * (that should not be considered next time) instance. |
| 46 | * |
| 47 | * @param[in,out] inst Found instance, is updated so that the same instance is not returned twice. |
| 48 | * @param[in] siblings Siblings where @p inst was found. |
| 49 | * @param[in,out] dup_inst_cache Duplicate instance cache. |
| 50 | * @return LY_ERR value. |
| 51 | */ |
| 52 | LY_ERR lyd_dup_inst_next(struct lyd_node **inst, const struct lyd_node *siblings, |
| 53 | struct lyd_dup_inst **dup_inst_cache); |
| 54 | |
| 55 | /** |
| 56 | * @brief Free duplicate instance cache. |
| 57 | * |
| 58 | * @param[in] dup_inst Duplicate instance cache to free. |
| 59 | */ |
| 60 | void lyd_dup_inst_free(struct lyd_dup_inst *dup_inst); |
| 61 | |
| 62 | /** |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 63 | * @brief Check whether a node to be deleted is the root node, move it if it is. |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 64 | * |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 65 | * @param[in] root Root sibling. |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 66 | * @param[in] to_del Node to be deleted. |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 67 | * @param[in] mod If set, it is expected @p tree should point to the first node of @p mod. Otherwise it will simply be |
| 68 | * the first top-level sibling. |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 69 | */ |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 70 | void lyd_del_move_root(struct lyd_node **root, const struct lyd_node *to_del, const struct lys_module *mod); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 71 | |
| 72 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 73 | * @brief Get address of a node's child pointer if any. |
| 74 | * |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 75 | * @param[in] node Node to check. |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 76 | * @return Address of the node's child member, |
| 77 | * @return NULL if there is no child pointer. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 78 | */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 79 | struct lyd_node **lyd_node_child_p(struct lyd_node *node); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 80 | |
| 81 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 82 | * @brief Just like ::lys_getnext() but iterates over all data instances of the schema nodes. |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 83 | * |
| 84 | * @param[in] last Last returned data node. |
| 85 | * @param[in] sibling Data node sibling to search in. |
| 86 | * @param[in,out] slast Schema last node, set to NULL for first call and do not change afterwards. |
| 87 | * May not be set if the function is used only for any suitable node existence check (such as the existence |
| 88 | * of any choice case data). |
| 89 | * @param[in] parent Schema parent of the iterated children nodes. |
| 90 | * @param[in] module Schema module of the iterated top-level nodes. |
| 91 | * @return Next matching data node, |
| 92 | * @return NULL if last data node was already returned. |
| 93 | */ |
| 94 | struct lyd_node *lys_getnext_data(const struct lyd_node *last, const struct lyd_node *sibling, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 95 | const struct lysc_node **slast, const struct lysc_node *parent, |
| 96 | const struct lysc_module *module); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 97 | |
| 98 | /** |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 99 | * @brief Create a term (leaf/leaf-list) node from a string value. |
| 100 | * |
| 101 | * Hash is calculated and new node flag is set. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 102 | * |
| 103 | * @param[in] schema Schema node of the new data node. |
| 104 | * @param[in] value String value to be parsed. |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 105 | * @param[in] value_len Length of @p value, must be set correctly. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 106 | * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 107 | * @param[in] format Input format of @p value. |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 108 | * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix). |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 109 | * @param[in] hints [Value hints](@ref lydvalhints) from the parser regarding the value type. |
| 110 | * @param[out] incomplete Whether the value needs to be resolved. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 111 | * @param[out] node Created node. |
| 112 | * @return LY_SUCCESS on success. |
| 113 | * @return LY_EINCOMPLETE in case data tree is needed to finish the validation. |
| 114 | * @return LY_ERR value if an error occurred. |
| 115 | */ |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 116 | LY_ERR lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool *dynamic, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 117 | LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, ly_bool *incomplete, struct lyd_node **node); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 118 | |
| 119 | /** |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 120 | * @brief Create a term (leaf/leaf-list) node from a parsed value by duplicating it. |
| 121 | * |
| 122 | * Hash is calculated and new node flag is set. |
| 123 | * |
| 124 | * @param[in] schema Schema node of the new data node. |
| 125 | * @param[in] val Parsed value to use. |
| 126 | * @param[out] node Created node. |
| 127 | * @return LY_SUCCESS on success. |
| 128 | * @return LY_ERR value if an error occurred. |
| 129 | */ |
| 130 | LY_ERR lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node); |
| 131 | |
| 132 | /** |
| 133 | * @brief Create an inner (container/list/RPC/action/notification) node. |
| 134 | * |
| 135 | * Hash is calculated and new node flag is set except |
| 136 | * for list with keys, when the hash is not calculated! |
| 137 | * Also, non-presence container has its default flag set. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 138 | * |
| 139 | * @param[in] schema Schema node of the new data node. |
| 140 | * @param[out] node Created node. |
| 141 | * @return LY_SUCCESS on success. |
| 142 | * @return LY_ERR value if an error occurred. |
| 143 | */ |
| 144 | LY_ERR lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node); |
| 145 | |
| 146 | /** |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 147 | * @brief Create a list with all its keys (cannot be used for key-less list). |
| 148 | * |
| 149 | * Hash is calculated and new node flag is set. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 150 | * |
| 151 | * @param[in] schema Schema node of the new data node. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 152 | * @param[in] predicates Compiled key list predicates. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 153 | * @param[out] node Created node. |
| 154 | * @return LY_SUCCESS on success. |
| 155 | * @return LY_ERR value if an error occurred. |
| 156 | */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 157 | LY_ERR lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 158 | |
| 159 | /** |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 160 | * @brief Create an anyxml/anydata node. |
| 161 | * |
| 162 | * 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] | 163 | * |
| 164 | * @param[in] schema Schema node of the new data node. |
Michal Vasko | 366a4a1 | 2020-12-04 16:23:57 +0100 | [diff] [blame] | 165 | * @param[in] value Value of the any node. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 166 | * @param[in] value_type Value type of the value. |
Michal Vasko | 366a4a1 | 2020-12-04 16:23:57 +0100 | [diff] [blame] | 167 | * @param[in] use_value Whether to directly assign (eat) the value or duplicate it. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 168 | * @param[out] node Created node. |
| 169 | * @return LY_SUCCESS on success. |
| 170 | * @return LY_ERR value if an error occurred. |
| 171 | */ |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 172 | LY_ERR lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, |
Michal Vasko | 366a4a1 | 2020-12-04 16:23:57 +0100 | [diff] [blame] | 173 | ly_bool use_value, struct lyd_node **node); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 174 | |
| 175 | /** |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 176 | * @brief Create an opaque node. |
| 177 | * |
| 178 | * @param[in] ctx libyang context. |
| 179 | * @param[in] name Element name. |
| 180 | * @param[in] name_len Length of @p name, must be set correctly. |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 181 | * @param[in] prefix Element prefix. |
| 182 | * @param[in] pref_len Length of @p prefix, must be set correctly. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 183 | * @param[in] module_key Mandatory key to reference module, can be namespace or name. |
| 184 | * @param[in] module_key_len Length of @p module_key, must be set correctly. |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 185 | * @param[in] value String value to be parsed. |
| 186 | * @param[in] value_len Length of @p value, must be set correctly. |
| 187 | * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed. |
| 188 | * @param[in] format Input format of @p value and @p ns. |
Radek Krejci | ec9ad60 | 2021-01-04 10:46:30 +0100 | [diff] [blame] | 189 | * @param[in] val_prefix_data Format-specific prefix data, param is spent (even in case the function fails): |
| 190 | * LY_PREF_SCHEMA - const struct lysp_module * (module used for resolving prefixes from imports) |
| 191 | * LY_PREF_SCHEMA_RESOLVED - struct lyd_value_prefix * (sized array of pairs: prefix - module) |
| 192 | * LY_PREF_XML - const struct ly_set * (set with defined namespaces stored as ::lyxml_ns) |
| 193 | * LY_PREF_JSON - NULL |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 194 | * @param[in] hints [Hints](@ref lydhints) from the parser regarding the node/value type. |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 195 | * @param[out] node Created node. |
| 196 | * @return LY_SUCCESS on success. |
| 197 | * @return LY_ERR value if an error occurred. |
| 198 | */ |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 199 | LY_ERR lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t pref_len, |
| 200 | const char *module_key, size_t module_key_len, const char *value, size_t value_len, ly_bool *dynamic, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 201 | LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints, struct lyd_node **node); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 202 | |
| 203 | /** |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 204 | * @brief Check the existence and create any non-existing implicit siblings, recursively for the created nodes. |
| 205 | * |
| 206 | * @param[in] parent Parent of the potential default values, NULL for top-level siblings. |
| 207 | * @param[in,out] first First sibling. |
| 208 | * @param[in] sparent Schema parent of the siblings, NULL if schema of @p parent can be used. |
| 209 | * @param[in] mod Module of the default values, NULL for nested siblings. |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 210 | * @param[in] node_when Optional set to add nodes with "when" conditions into. |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 211 | * @param[in] node_exts Optional set to add nodes and extension instances having own validation plugin callback into it. |
Michal Vasko | c43c8ab | 2021-03-05 13:32:44 +0100 | [diff] [blame] | 212 | * @param[in] node_types Optional set to add nodes with unresolved types into. |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 213 | * @param[in] impl_opts Implicit options (@ref implicitoptions). |
| 214 | * @param[in,out] diff Validation diff. |
| 215 | * @return LY_ERR value. |
| 216 | */ |
| 217 | LY_ERR lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent, |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 218 | const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_exts, struct ly_set *node_types, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 219 | uint32_t impl_opts, struct lyd_node **diff); |
Michal Vasko | a6669ba | 2020-08-06 16:14:26 +0200 | [diff] [blame] | 220 | |
| 221 | /** |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 222 | * @brief Find the next node, before which to insert the new node. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 223 | * |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 224 | * @param[in] first_sibling First sibling of the nodes to consider. |
| 225 | * @param[in] new_node Node that will be inserted. |
| 226 | * @return Node to insert after. |
| 227 | * @return NULL if the new node should be first. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 228 | */ |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 229 | struct lyd_node *lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 230 | |
| 231 | /** |
Michal Vasko | b104f11 | 2020-07-17 09:54:54 +0200 | [diff] [blame] | 232 | * @brief Insert a node into parent/siblings. Order and hashes are fully handled. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 233 | * |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 234 | * @param[in] parent Parent to insert into, NULL for top-level sibling. |
| 235 | * @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] | 236 | * @param[in] node Individual node (without siblings) to insert. |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 237 | * @param[in] last If set, do not search for the correct anchor but always insert at the end. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 238 | */ |
Michal Vasko | 6ee6f43 | 2021-07-16 09:49:14 +0200 | [diff] [blame] | 239 | void lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node, ly_bool last); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 240 | |
| 241 | /** |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 242 | * @brief Insert a metadata (last) into a parent |
| 243 | * |
| 244 | * @param[in] parent Parent of the metadata. |
| 245 | * @param[in] meta Metadata (list) to be added into the @p parent. |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 246 | * @param[in] clear_dflt Whether to clear dflt flag starting from @p parent, recursively all NP containers. |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 247 | */ |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 248 | void lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt); |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 249 | |
| 250 | /** |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 251 | * @brief Create and insert a metadata (last) into a parent. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 252 | * |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 253 | * @param[in] parent Parent of the metadata, can be NULL. |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 254 | * @param[in,out] meta Metadata list to add at its end if @p parent is NULL, returned created attribute. |
| 255 | * @param[in] mod Metadata module (with the annotation definition). |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 256 | * @param[in] name Attribute name. |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 257 | * @param[in] name_len Length of @p name, must be set correctly. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 258 | * @param[in] value String value to be parsed. |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 259 | * @param[in] value_len Length of @p value, must be set correctly. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 260 | * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 261 | * @param[in] format Input format of @p value. |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 262 | * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix). |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 263 | * @param[in] hints [Value hints](@ref lydvalhints) from the parser regarding the value type. |
Michal Vasko | 871a025 | 2020-11-11 18:35:24 +0100 | [diff] [blame] | 264 | * @param[in] clear_dflt Whether to clear dflt flag starting from @p parent, recursively all NP containers. |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 265 | * @param[out] incomplete Whether the value needs to be resolved. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 266 | * @return LY_SUCCESS on success. |
| 267 | * @return LY_EINCOMPLETE in case data tree is needed to finish the validation. |
| 268 | * @return LY_ERR value if an error occurred. |
| 269 | */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 270 | LY_ERR lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 271 | size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, |
Radek Krejci | 84d7fd7 | 2021-07-14 18:32:21 +0200 | [diff] [blame] | 272 | void *prefix_data, uint32_t hints, ly_bool clear_dflt, ly_bool *incomplete); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 273 | |
| 274 | /** |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 275 | * @brief Insert an attribute (last) into a parent |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 276 | * |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 277 | * @param[in] parent Parent of the attributes. |
| 278 | * @param[in] attr Attribute (list) to be added into the @p parent. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 279 | */ |
Michal Vasko | a5da329 | 2020-08-12 13:10:50 +0200 | [diff] [blame] | 280 | void lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr); |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 281 | |
| 282 | /** |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 283 | * @brief Create and insert a generic attribute (last) into a parent. |
| 284 | * |
| 285 | * @param[in] parent Parent of the attribute, can be NULL. |
| 286 | * @param[in,out] attr Attribute list to add at its end if @p parent is NULL, returned created attribute. |
| 287 | * @param[in] ctx libyang context. |
| 288 | * @param[in] name Attribute name. |
| 289 | * @param[in] name_len Length of @p name, must be set correctly. |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 290 | * @param[in] prefix Attribute prefix. |
| 291 | * @param[in] prefix_len Attribute prefix length. |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 292 | * @param[in] module_key Mandatory key to reference module, can be namespace or name. |
| 293 | * @param[in] module_key_len Length of @p module_key, must be set correctly. |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 294 | * @param[in] value String value to be parsed. |
| 295 | * @param[in] value_len Length of @p value, must be set correctly. |
| 296 | * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed. |
| 297 | * @param[in] format Input format of @p value and @p ns. |
| 298 | * @param[in] val_prefix_data Format-specific prefix data, param is spent (even in case the function fails). |
| 299 | * @param[in] hints [Hints](@ref lydhints) from the parser regarding the node/value type. |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 300 | * @return LY_SUCCESS on success, |
| 301 | * @return LY_ERR value on error. |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 302 | */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 303 | LY_ERR lyd_create_attr(struct lyd_node *parent, struct lyd_attr **attr, const struct ly_ctx *ctx, const char *name, |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 304 | size_t name_len, const char *prefix, size_t prefix_len, const char *module_key, size_t module_key_len, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 305 | const char *value, size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 306 | |
| 307 | /** |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 308 | * @brief Store and canonize the given @p value into @p val according to the schema node type rules. |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 309 | * |
Michal Vasko | 8d54425 | 2020-03-02 10:19:52 +0100 | [diff] [blame] | 310 | * @param[in] ctx libyang context. |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 311 | * @param[in,out] val Storage for the value. |
| 312 | * @param[in] type Type of the value. |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 313 | * @param[in] value Value to be parsed, must not be NULL. |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 314 | * @param[in] value_len Length of the give @p value, must be set correctly. |
Michal Vasko | 90932a9 | 2020-02-12 14:33:03 +0100 | [diff] [blame] | 315 | * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed. |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 316 | * @param[in] format Input format of @p value. |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 317 | * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix). |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 318 | * @param[in] hints [Value hints](@ref lydvalhints) from the parser. |
| 319 | * @param[in] ctx_node Context schema node. |
| 320 | * @param[out] incomplete Optional, set if the value also needs to be resolved. |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 321 | * @return LY_SUCCESS on success, |
| 322 | * @return LY_ERR value on error. |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 323 | */ |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 324 | LY_ERR lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const void *value, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 325 | size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 326 | const struct lysc_node *ctx_node, ly_bool *incomplete); |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 327 | |
| 328 | /** |
| 329 | * @brief Validate previously incompletely stored value. |
| 330 | * |
| 331 | * @param[in] ctx libyang context. |
| 332 | * @param[in] type Schema type of the value (not the stored one, but the original one). |
| 333 | * @param[in,out] val Stored value to resolve. |
| 334 | * @param[in] ctx_node Context node for the resolution. |
| 335 | * @param[in] tree Data tree for the resolution. |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 336 | * @return LY_SUCCESS on success, |
| 337 | * @return LY_ERR value on error. |
| 338 | */ |
| 339 | LY_ERR lyd_value_validate_incomplete(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *val, |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 340 | const struct lyd_node *ctx_node, const struct lyd_node *tree); |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 341 | |
Michal Vasko | aebbce0 | 2021-04-06 09:23:37 +0200 | [diff] [blame] | 342 | /** |
| 343 | * @brief Check type restrictions applicable to the particular leaf/leaf-list with the given string @p value coming |
| 344 | * from a schema. |
| 345 | * |
| 346 | * This function check just the type's restriction, if you want to check also the data tree context (e.g. in case of |
| 347 | * require-instance restriction), use ::lyd_value_validate(). |
| 348 | * |
| 349 | * @param[in] ctx libyang context for logging (function does not log errors when @p ctx is NULL) |
| 350 | * @param[in] node Schema node for the @p value. |
| 351 | * @param[in] value String value to be checked, expected to be in JSON format. |
| 352 | * @param[in] value_len Length of the given @p value (mandatory). |
| 353 | * @param[in] format Value prefix format. |
| 354 | * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix). |
| 355 | * @return LY_SUCCESS on success |
| 356 | * @return LY_ERR value if an error occurred. |
| 357 | */ |
| 358 | LY_ERR lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 359 | LY_VALUE_FORMAT format, void *prefix_data); |
Michal Vasko | f937cfe | 2020-08-03 16:07:12 +0200 | [diff] [blame] | 360 | |
Radek Krejci | 38d8536 | 2019-09-05 16:26:38 +0200 | [diff] [blame] | 361 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 362 | * @defgroup datahash Data nodes hash manipulation |
| 363 | * @ingroup datatree |
Michal Vasko | 8081a81 | 2021-07-15 09:19:43 +0200 | [diff] [blame] | 364 | * @{ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 365 | */ |
| 366 | |
| 367 | /** |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 368 | * @brief Generate hash for the node. |
| 369 | * |
| 370 | * @param[in] node Data node to (re)generate hash value. |
| 371 | * @return LY_ERR value. |
| 372 | */ |
| 373 | LY_ERR lyd_hash(struct lyd_node *node); |
| 374 | |
| 375 | /** |
| 376 | * @brief Insert hash of the node into the hash table of its parent. |
| 377 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 378 | * @param[in] node Data node which hash will be inserted into the ::lyd_node_inner.children_ht hash table of its parent. |
Radek Krejci | 1f05b6a | 2019-07-18 16:15:06 +0200 | [diff] [blame] | 379 | * @return LY_ERR value. |
| 380 | */ |
| 381 | LY_ERR lyd_insert_hash(struct lyd_node *node); |
| 382 | |
| 383 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 384 | * @brief Maintain node's parent's children hash table when unlinking the node. |
| 385 | * |
| 386 | * When completely freeing data tree, it is expected to free the parent's children hash table first, at once. |
| 387 | * |
| 388 | * @param[in] node The data node being unlinked from its parent. |
| 389 | */ |
| 390 | void lyd_unlink_hash(struct lyd_node *node); |
| 391 | |
| 392 | /** @} datahash */ |
| 393 | |
Radek Krejci | 084289f | 2019-07-09 17:35:30 +0200 | [diff] [blame] | 394 | /** |
Michal Vasko | 598063b | 2021-07-19 11:39:05 +0200 | [diff] [blame] | 395 | * @brief Update node pointer to point to the first data node of a module, leave unchanged if there is none. |
| 396 | * |
| 397 | * @param[in,out] node Node pointer, may be updated. |
| 398 | * @param[in] mod Module whose data to search for. |
| 399 | */ |
| 400 | void lyd_first_module_sibling(struct lyd_node **node, const struct lys_module *mod); |
| 401 | |
| 402 | /** |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 403 | * @brief Iterate over implemented modules for functions that accept specific modules or the whole context. |
| 404 | * |
| 405 | * @param[in] tree Data tree. |
Michal Vasko | 26e8001 | 2020-07-08 10:55:46 +0200 | [diff] [blame] | 406 | * @param[in] module Selected module, NULL for all. |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 407 | * @param[in] ctx Context, NULL for selected modules. |
| 408 | * @param[in,out] i Iterator, set to 0 on first call. |
| 409 | * @param[out] first First sibling of the returned module. |
| 410 | * @return Next module. |
| 411 | * @return NULL if all modules were traversed. |
| 412 | */ |
Michal Vasko | 26e8001 | 2020-07-08 10:55:46 +0200 | [diff] [blame] | 413 | const struct lys_module *lyd_mod_next_module(struct lyd_node *tree, const struct lys_module *module, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 414 | const struct ly_ctx *ctx, uint32_t *i, struct lyd_node **first); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 415 | |
| 416 | /** |
| 417 | * @brief Iterate over modules for functions that want to traverse all the top-level data. |
| 418 | * |
| 419 | * @param[in,out] next Pointer to the next module data, set to first top-level sibling on first call. |
| 420 | * @param[out] first First sibling of the returned module. |
| 421 | * @return Next module. |
| 422 | * @return NULL if all modules were traversed. |
| 423 | */ |
| 424 | const struct lys_module *lyd_data_next_module(struct lyd_node **next, struct lyd_node **first); |
| 425 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 426 | /** |
| 427 | * @brief Check that a list has all its keys. |
| 428 | * |
| 429 | * @param[in] node List to check. |
Michal Vasko | 44685da | 2020-03-17 15:38:06 +0100 | [diff] [blame] | 430 | * @return LY_SUCCESS on success. |
| 431 | * @return LY_ENOT on a missing key. |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 432 | */ |
| 433 | LY_ERR lyd_parse_check_keys(struct lyd_node *node); |
| 434 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 435 | /** |
| 436 | * @brief Set data flags for a newly parsed node. |
| 437 | * |
| 438 | * @param[in] node Node to use. |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 439 | * @param[in,out] when_check Set of nodes with unresolved when. |
| 440 | * @param[in,out] exts_check Set of nodes and their extension instances if they have own validation callback. |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 441 | * @param[in,out] meta Node metadata, may be removed from. |
| 442 | * @param[in] options Parse options. |
| 443 | */ |
Radek Krejci | 4f2e3e5 | 2021-03-30 14:20:28 +0200 | [diff] [blame] | 444 | void lyd_parse_set_data_flags(struct lyd_node *node, struct ly_set *when_check, struct ly_set *exts_check, |
| 445 | struct lyd_meta **meta, uint32_t options); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 446 | |
| 447 | /** |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 448 | * @brief Append all list key predicates to path. |
| 449 | * |
| 450 | * @param[in] node Node with keys to print. |
| 451 | * @param[in,out] buffer Buffer to print to. |
| 452 | * @param[in,out] buflen Current buffer length. |
| 453 | * @param[in,out] bufused Current number of characters used in @p buffer. |
| 454 | * @param[in] is_static Whether buffer is static or can be reallocated. |
| 455 | * @return LY_ERR |
| 456 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 457 | LY_ERR lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static); |
Michal Vasko | d59035b | 2020-07-08 12:00:06 +0200 | [diff] [blame] | 458 | |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 459 | /** |
| 460 | * @brief Free stored prefix data. |
| 461 | * |
| 462 | * @param[in] format Format of the prefixes. |
Radek Krejci | ec9ad60 | 2021-01-04 10:46:30 +0100 | [diff] [blame] | 463 | * @param[in] prefix_data Format-specific data to free: |
| 464 | * LY_PREF_SCHEMA - const struct lysp_module * (module used for resolving prefixes from imports) |
| 465 | * LY_PREF_SCHEMA_RESOLVED - struct lyd_value_prefix * (sized array of pairs: prefix - module) |
| 466 | * LY_PREF_XML - const struct ly_set * (set with defined namespaces stored as ::lyxml_ns) |
| 467 | * LY_PREF_JSON - NULL |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 468 | */ |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 469 | void ly_free_prefix_data(LY_VALUE_FORMAT format, void *prefix_data); |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 470 | |
| 471 | /** |
| 472 | * @brief Duplicate prefix data. |
| 473 | * |
| 474 | * @param[in] ctx libyang context. |
| 475 | * @param[in] format Format of the prefixes in the value. |
| 476 | * @param[in] prefix_data Prefix data to duplicate. |
| 477 | * @param[out] prefix_data_p Duplicated prefix data. |
| 478 | * @return LY_ERR value. |
| 479 | */ |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 480 | LY_ERR ly_dup_prefix_data(const struct ly_ctx *ctx, LY_VALUE_FORMAT format, const void *prefix_data, void **prefix_data_p); |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 481 | |
| 482 | /** |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 483 | * @brief Store used prefixes in a string. |
| 484 | * |
| 485 | * If @p prefix_data_p are non-NULL, they are treated as valid according to the @p format_p and new possible |
| 486 | * prefixes are simply added. This way it is possible to store prefix data for several strings together. |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 487 | * |
| 488 | * @param[in] ctx libyang context. |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 489 | * @param[in] value Value to be parsed. |
| 490 | * @param[in] value_len Length of the @p value. |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 491 | * @param[in] format Format of the prefixes in the value. |
| 492 | * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix). |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 493 | * @param[in,out] format_p Resulting format of the prefixes. |
| 494 | * @param[in,out] prefix_data_p Resulting prefix data for the value in format @p format_p. |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 495 | * @return LY_ERR value. |
| 496 | */ |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 497 | LY_ERR ly_store_prefix_data(const struct ly_ctx *ctx, const void *value, size_t value_len, LY_VALUE_FORMAT format, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 498 | const void *prefix_data, LY_VALUE_FORMAT *format_p, void **prefix_data_p); |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 499 | |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 500 | /** |
| 501 | * @brief Get string name of the format. |
| 502 | * |
| 503 | * @param[in] format Format whose name to get. |
| 504 | * @return Format string name. |
| 505 | */ |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 506 | const char *ly_format2str(LY_VALUE_FORMAT format); |
Michal Vasko | 7ed1fcb | 2020-12-03 14:15:22 +0100 | [diff] [blame] | 507 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 508 | #endif /* LY_TREE_DATA_INTERNAL_H_ */ |