blob: c7449ce27cd3145e24112e92523a89141c2f14ee [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
2 * @file tree_data_internal.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vaskodbf3e652022-10-21 08:46:25 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
Radek Krejcie7b95092019-05-15 11:03:07 +02005 * @brief internal functions for YANG schema trees.
6 *
Michal Vasko271d2e32023-01-31 15:43:19 +01007 * Copyright (c) 2015 - 2023 CESNET, z.s.p.o.
Radek Krejcie7b95092019-05-15 11:03:07 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
16#ifndef LY_TREE_DATA_INTERNAL_H_
17#define LY_TREE_DATA_INTERNAL_H_
18
Radek Krejci857189e2020-09-01 13:26:36 +020019#include "log.h"
Radek Krejciaca74032019-06-04 08:53:06 +020020#include "plugins_types.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020021#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020022
Michal Vasko52927e22020-03-16 17:26:14 +010023#include <stddef.h>
24
Michal Vasko004d3152020-06-11 19:59:22 +020025struct ly_path_predicate;
Michal Vasko8cc3f662022-03-29 11:25:51 +020026struct lyd_ctx;
Michal Vaskoa6669ba2020-08-06 16:14:26 +020027struct lysc_module;
Michal Vasko004d3152020-06-11 19:59:22 +020028
Radek Krejcif13b87b2020-12-01 22:02:17 +010029#define LY_XML_SUFFIX ".xml"
30#define LY_XML_SUFFIX_LEN 4
31#define LY_JSON_SUFFIX ".json"
32#define LY_JSON_SUFFIX_LEN 5
33#define LY_LYB_SUFFIX ".lyb"
34#define LY_LYB_SUFFIX_LEN 4
35
Radek Krejcie7b95092019-05-15 11:03:07 +020036/**
Michal Vasko271d2e32023-01-31 15:43:19 +010037 * @brief Internal item structure for remembering "used" instances of duplicate node instances.
Michal Vaskod7c048c2021-05-18 16:12:55 +020038 */
39struct lyd_dup_inst {
Michal Vasko271d2e32023-01-31 15:43:19 +010040 struct ly_set *set;
Michal Vaskod7c048c2021-05-18 16:12:55 +020041 uint32_t used;
42};
43
44/**
Michal Vasko271d2e32023-01-31 15:43:19 +010045 * @brief Update a found inst using a duplicate instance cache hash table. Needs to be called for every "used"
Michal Vaskod7c048c2021-05-18 16:12:55 +020046 * (that should not be considered next time) instance.
47 *
48 * @param[in,out] inst Found instance, is updated so that the same instance is not returned twice.
49 * @param[in] siblings Siblings where @p inst was found.
Michal Vasko271d2e32023-01-31 15:43:19 +010050 * @param[in] dup_inst_ht Duplicate instance cache hash table.
Michal Vaskod7c048c2021-05-18 16:12:55 +020051 * @return LY_ERR value.
52 */
Michal Vasko8efac242023-03-30 08:24:56 +020053LY_ERR lyd_dup_inst_next(struct lyd_node **inst, const struct lyd_node *siblings, struct ly_ht **dup_inst_ht);
Michal Vaskod7c048c2021-05-18 16:12:55 +020054
55/**
56 * @brief Free duplicate instance cache.
57 *
Michal Vasko271d2e32023-01-31 15:43:19 +010058 * @param[in] dup_inst Duplicate instance cache hash table to free.
Michal Vaskod7c048c2021-05-18 16:12:55 +020059 */
Michal Vasko8efac242023-03-30 08:24:56 +020060void lyd_dup_inst_free(struct ly_ht *dup_inst_ht);
Michal Vaskod7c048c2021-05-18 16:12:55 +020061
62/**
Radek Krejci8678fa42020-08-18 16:07:28 +020063 * @brief Just like ::lys_getnext() but iterates over all data instances of the schema nodes.
Michal Vaskoa6669ba2020-08-06 16:14:26 +020064 *
65 * @param[in] last Last returned data node.
66 * @param[in] sibling Data node sibling to search in.
67 * @param[in,out] slast Schema last node, set to NULL for first call and do not change afterwards.
68 * May not be set if the function is used only for any suitable node existence check (such as the existence
69 * of any choice case data).
70 * @param[in] parent Schema parent of the iterated children nodes.
71 * @param[in] module Schema module of the iterated top-level nodes.
72 * @return Next matching data node,
73 * @return NULL if last data node was already returned.
74 */
75struct lyd_node *lys_getnext_data(const struct lyd_node *last, const struct lyd_node *sibling,
Michal Vasko106f0862021-11-02 11:49:27 +010076 const struct lysc_node **slast, const struct lysc_node *parent, const struct lysc_module *module);
77
78/**
79 * @brief Get address of a node's child pointer if any.
80 *
81 * @param[in] node Node to check.
82 * @return Address of the node's child member,
83 * @return NULL if there is no child pointer.
84 */
85struct lyd_node **lyd_node_child_p(struct lyd_node *node);
86
87/**
88 * @brief Update node pointer to point to the first data node of a module, leave unchanged if there is none.
89 *
90 * @param[in,out] node Node pointer, may be updated.
91 * @param[in] mod Module whose data to search for.
92 */
93void lyd_first_module_sibling(struct lyd_node **node, const struct lys_module *mod);
94
95/**
96 * @brief Iterate over implemented modules for functions that accept specific modules or the whole context.
97 *
98 * @param[in] tree Data tree.
99 * @param[in] module Selected module, NULL for all.
100 * @param[in] ctx Context, NULL for selected modules.
101 * @param[in,out] i Iterator, set to 0 on first call.
102 * @param[out] first First sibling of the returned module.
103 * @return Next module.
104 * @return NULL if all modules were traversed.
105 */
106const struct lys_module *lyd_mod_next_module(struct lyd_node *tree, const struct lys_module *module,
107 const struct ly_ctx *ctx, uint32_t *i, struct lyd_node **first);
108
109/**
110 * @brief Iterate over modules for functions that want to traverse all the top-level data.
111 *
112 * @param[in,out] next Pointer to the next module data, set to first top-level sibling on first call.
113 * @param[out] first First sibling of the returned module.
114 * @return Next module.
115 * @return NULL if all modules were traversed.
116 */
117const struct lys_module *lyd_data_next_module(struct lyd_node **next, struct lyd_node **first);
118
119/**
Michal Vasko106f0862021-11-02 11:49:27 +0100120 * @brief Get schema node of a data node. Useful especially for opaque nodes.
121 *
122 * @param[in] node Data node to use.
123 * @return Schema node represented by data @p node, NULL if there is none.
124 */
125const struct lysc_node *lyd_node_schema(const struct lyd_node *node);
126
127/**
Michal Vasko4754d4a2022-12-01 10:11:21 +0100128 * @brief Set dflt flag for a NP container if applicable, recursively for parents.
129 *
130 * @param[in] node Node whose criteria for the dflt flag has changed.
131 */
132void lyd_cont_set_dflt(struct lyd_node *node);
133
134/**
Michal Vasko59892dd2022-05-13 11:02:30 +0200135 * @brief Search in the given siblings (NOT recursively) for the first schema node data instance.
136 * Uses hashes - should be used whenever possible for best performance.
137 *
138 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
139 * @param[in] schema Target data node schema to find.
140 * @param[out] match Can be NULL, otherwise the found data node.
141 * @return LY_SUCCESS on success, @p match set.
142 * @return LY_ENOTFOUND if not found, @p match set to NULL.
143 * @return LY_ERR value if another error occurred.
144 */
145LY_ERR lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match);
146
147/**
Michal Vasko106f0862021-11-02 11:49:27 +0100148 * @brief Check whether a node to be deleted is the root node, move it if it is.
149 *
150 * @param[in] root Root sibling.
151 * @param[in] to_del Node to be deleted.
152 * @param[in] mod If set, it is expected @p tree should point to the first node of @p mod. Otherwise it will simply be
153 * the first top-level sibling.
154 */
155void lyd_del_move_root(struct lyd_node **root, const struct lyd_node *to_del, const struct lys_module *mod);
156
157/**
Michal Vasko8cc3f662022-03-29 11:25:51 +0200158 * @brief Try to get schema node for data with a parent based on an extension instance.
159 *
160 * @param[in] parent Parsed parent data node. Set if @p sparent is NULL.
161 * @param[in] sparent Schema parent node. Set if @p sparent is NULL.
162 * @param[in] prefix Element prefix, if any.
163 * @param[in] prefix_len Length of @p prefix.
164 * @param[in] format Format of @p prefix.
165 * @param[in] prefix_data Format-specific data.
166 * @param[in] name Element name.
167 * @param[in] name_len Length of @p name.
168 * @param[out] snode Found schema node, NULL if no suitable was found.
Michal Vasko66330fc2022-11-21 15:52:24 +0100169 * @param[out] ext Optional extension instance that provided @p snode.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200170 * @return LY_SUCCESS on success;
171 * @return LY_ENOT if no extension instance parsed the data;
172 * @return LY_ERR on error.
173 */
174LY_ERR ly_nested_ext_schema(const struct lyd_node *parent, const struct lysc_node *sparent, const char *prefix,
175 size_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data, const char *name, size_t name_len,
176 const struct lysc_node **snode, struct lysc_ext_instance **ext);
177
178/**
Michal Vasko106f0862021-11-02 11:49:27 +0100179 * @brief Free stored prefix data.
180 *
181 * @param[in] format Format of the prefixes.
182 * @param[in] prefix_data Format-specific data to free:
183 * LY_PREF_SCHEMA - const struct lysp_module * (module used for resolving prefixes from imports)
184 * LY_PREF_SCHEMA_RESOLVED - struct lyd_value_prefix * (sized array of pairs: prefix - module)
185 * LY_PREF_XML - const struct ly_set * (set with defined namespaces stored as ::lyxml_ns)
186 * LY_PREF_JSON - NULL
187 */
188void ly_free_prefix_data(LY_VALUE_FORMAT format, void *prefix_data);
189
190/**
191 * @brief Duplicate prefix data.
192 *
193 * @param[in] ctx libyang context.
194 * @param[in] format Format of the prefixes in the value.
195 * @param[in] prefix_data Prefix data to duplicate.
196 * @param[out] prefix_data_p Duplicated prefix data.
197 * @return LY_ERR value.
198 */
199LY_ERR ly_dup_prefix_data(const struct ly_ctx *ctx, LY_VALUE_FORMAT format, const void *prefix_data, void **prefix_data_p);
200
201/**
202 * @brief Store used prefixes in a string.
203 *
204 * If @p prefix_data_p are non-NULL, they are treated as valid according to the @p format_p and new possible
205 * prefixes are simply added. This way it is possible to store prefix data for several strings together.
206 *
207 * @param[in] ctx libyang context.
208 * @param[in] value Value to be parsed.
209 * @param[in] value_len Length of the @p value.
210 * @param[in] format Format of the prefixes in the value.
211 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
212 * @param[in,out] format_p Resulting format of the prefixes.
213 * @param[in,out] prefix_data_p Resulting prefix data for the value in format @p format_p.
214 * @return LY_ERR value.
215 */
216LY_ERR ly_store_prefix_data(const struct ly_ctx *ctx, const void *value, size_t value_len, LY_VALUE_FORMAT format,
217 const void *prefix_data, LY_VALUE_FORMAT *format_p, void **prefix_data_p);
218
219/**
220 * @brief Get string name of the format.
221 *
222 * @param[in] format Format whose name to get.
223 * @return Format string name.
224 */
225const char *ly_format2str(LY_VALUE_FORMAT format);
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200226
227/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100228 * @brief Create a term (leaf/leaf-list) node from a string value.
229 *
230 * Hash is calculated and new node flag is set.
Michal Vasko90932a92020-02-12 14:33:03 +0100231 *
232 * @param[in] schema Schema node of the new data node.
233 * @param[in] value String value to be parsed.
Michal Vaskof03ed032020-03-04 13:31:44 +0100234 * @param[in] value_len Length of @p value, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100235 * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed.
Michal Vasko90932a92020-02-12 14:33:03 +0100236 * @param[in] format Input format of @p value.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200237 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200238 * @param[in] hints [Value hints](@ref lydvalhints) from the parser regarding the value type.
239 * @param[out] incomplete Whether the value needs to be resolved.
Michal Vasko90932a92020-02-12 14:33:03 +0100240 * @param[out] node Created node.
241 * @return LY_SUCCESS on success.
242 * @return LY_EINCOMPLETE in case data tree is needed to finish the validation.
243 * @return LY_ERR value if an error occurred.
244 */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200245LY_ERR lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool *dynamic,
Radek Krejci8df109d2021-04-23 12:19:08 +0200246 LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, ly_bool *incomplete, struct lyd_node **node);
Michal Vasko90932a92020-02-12 14:33:03 +0100247
248/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100249 * @brief Create a term (leaf/leaf-list) node from a parsed value by duplicating it.
250 *
251 * Hash is calculated and new node flag is set.
252 *
253 * @param[in] schema Schema node of the new data node.
254 * @param[in] val Parsed value to use.
255 * @param[out] node Created node.
256 * @return LY_SUCCESS on success.
257 * @return LY_ERR value if an error occurred.
258 */
259LY_ERR lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node);
260
261/**
262 * @brief Create an inner (container/list/RPC/action/notification) node.
263 *
264 * Hash is calculated and new node flag is set except
265 * for list with keys, when the hash is not calculated!
266 * Also, non-presence container has its default flag set.
Michal Vasko90932a92020-02-12 14:33:03 +0100267 *
268 * @param[in] schema Schema node of the new data node.
269 * @param[out] node Created node.
270 * @return LY_SUCCESS on success.
271 * @return LY_ERR value if an error occurred.
272 */
273LY_ERR lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node);
274
275/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100276 * @brief Create a list with all its keys (cannot be used for key-less list).
277 *
278 * Hash is calculated and new node flag is set.
Michal Vasko90932a92020-02-12 14:33:03 +0100279 *
280 * @param[in] schema Schema node of the new data node.
Michal Vasko004d3152020-06-11 19:59:22 +0200281 * @param[in] predicates Compiled key list predicates.
Michal Vasko90189962023-02-28 12:10:34 +0100282 * @param[in] vars Array of defined variables to use in predicates, may be NULL.
Michal Vasko90932a92020-02-12 14:33:03 +0100283 * @param[out] node Created node.
284 * @return LY_SUCCESS on success.
285 * @return LY_ERR value if an error occurred.
286 */
Michal Vasko90189962023-02-28 12:10:34 +0100287LY_ERR lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates,
288 const struct lyxp_var *vars, struct lyd_node **node);
Michal Vasko90932a92020-02-12 14:33:03 +0100289
290/**
Michal Vasko59892dd2022-05-13 11:02:30 +0200291 * @brief Create a list with all its keys (cannot be used for key-less list).
292 *
293 * Hash is calculated and new node flag is set.
294 *
295 * @param[in] schema Schema node of the new data node.
296 * @param[in] keys Key list predicates.
297 * @param[in] keys_len Length of @p keys.
298 * @param[out] node Created node.
299 * @return LY_SUCCESS on success.
300 * @return LY_ERR value if an error occurred.
301 */
302LY_ERR lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node);
303
304/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100305 * @brief Create an anyxml/anydata node.
306 *
307 * Hash is calculated and flags are properly set based on @p is_valid.
Michal Vasko90932a92020-02-12 14:33:03 +0100308 *
309 * @param[in] schema Schema node of the new data node.
Michal Vasko366a4a12020-12-04 16:23:57 +0100310 * @param[in] value Value of the any node.
Michal Vasko90932a92020-02-12 14:33:03 +0100311 * @param[in] value_type Value type of the value.
Michal Vasko742a5b12022-02-24 16:07:27 +0100312 * @param[in] use_value Whether to use dynamic @p value or duplicate it.
Michal Vasko90932a92020-02-12 14:33:03 +0100313 * @param[out] node Created node.
314 * @return LY_SUCCESS on success.
315 * @return LY_ERR value if an error occurred.
316 */
Michal Vasko9b368d32020-02-14 13:53:31 +0100317LY_ERR lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type,
Michal Vasko366a4a12020-12-04 16:23:57 +0100318 ly_bool use_value, struct lyd_node **node);
Michal Vasko90932a92020-02-12 14:33:03 +0100319
320/**
Michal Vasko52927e22020-03-16 17:26:14 +0100321 * @brief Create an opaque node.
322 *
323 * @param[in] ctx libyang context.
324 * @param[in] name Element name.
325 * @param[in] name_len Length of @p name, must be set correctly.
Michal Vasko52927e22020-03-16 17:26:14 +0100326 * @param[in] prefix Element prefix.
327 * @param[in] pref_len Length of @p prefix, must be set correctly.
Radek Krejci1798aae2020-07-14 13:26:06 +0200328 * @param[in] module_key Mandatory key to reference module, can be namespace or name.
329 * @param[in] module_key_len Length of @p module_key, must be set correctly.
Michal Vasko501af032020-11-11 20:27:44 +0100330 * @param[in] value String value to be parsed.
331 * @param[in] value_len Length of @p value, must be set correctly.
332 * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed.
333 * @param[in] format Input format of @p value and @p ns.
Radek Krejciec9ad602021-01-04 10:46:30 +0100334 * @param[in] val_prefix_data Format-specific prefix data, param is spent (even in case the function fails):
335 * LY_PREF_SCHEMA - const struct lysp_module * (module used for resolving prefixes from imports)
336 * LY_PREF_SCHEMA_RESOLVED - struct lyd_value_prefix * (sized array of pairs: prefix - module)
337 * LY_PREF_XML - const struct ly_set * (set with defined namespaces stored as ::lyxml_ns)
338 * LY_PREF_JSON - NULL
Michal Vasko501af032020-11-11 20:27:44 +0100339 * @param[in] hints [Hints](@ref lydhints) from the parser regarding the node/value type.
Michal Vasko52927e22020-03-16 17:26:14 +0100340 * @param[out] node Created node.
341 * @return LY_SUCCESS on success.
342 * @return LY_ERR value if an error occurred.
343 */
Michal Vasko501af032020-11-11 20:27:44 +0100344LY_ERR lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t pref_len,
345 const char *module_key, size_t module_key_len, const char *value, size_t value_len, ly_bool *dynamic,
Radek Krejci8df109d2021-04-23 12:19:08 +0200346 LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints, struct lyd_node **node);
Michal Vasko52927e22020-03-16 17:26:14 +0100347
348/**
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200349 * @brief Check the existence and create any non-existing implicit siblings, recursively for the created nodes.
350 *
351 * @param[in] parent Parent of the potential default values, NULL for top-level siblings.
352 * @param[in,out] first First sibling.
353 * @param[in] sparent Schema parent of the siblings, NULL if schema of @p parent can be used.
354 * @param[in] mod Module of the default values, NULL for nested siblings.
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200355 * @param[in] node_when Optional set to add nodes with "when" conditions into.
Michal Vaskoc43c8ab2021-03-05 13:32:44 +0100356 * @param[in] node_types Optional set to add nodes with unresolved types into.
Michal Vaskofcbd78f2022-08-26 08:34:15 +0200357 * @param[in] ext_node Optional set to add nodes with extension instance node callbacks into.
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200358 * @param[in] impl_opts Implicit options (@ref implicitoptions).
359 * @param[in,out] diff Validation diff.
360 * @return LY_ERR value.
361 */
362LY_ERR lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Michal Vaskofcbd78f2022-08-26 08:34:15 +0200363 const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_types, struct ly_set *ext_node,
364 uint32_t impl_opts, struct lyd_node **diff);
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200365
366/**
Michal Vaskob104f112020-07-17 09:54:54 +0200367 * @brief Find the next node, before which to insert the new node.
Michal Vasko90932a92020-02-12 14:33:03 +0100368 *
Michal Vaskob104f112020-07-17 09:54:54 +0200369 * @param[in] first_sibling First sibling of the nodes to consider.
370 * @param[in] new_node Node that will be inserted.
371 * @return Node to insert after.
372 * @return NULL if the new node should be first.
Michal Vasko90932a92020-02-12 14:33:03 +0100373 */
Michal Vaskob104f112020-07-17 09:54:54 +0200374struct lyd_node *lyd_insert_get_next_anchor(const struct lyd_node *first_sibling, const struct lyd_node *new_node);
Michal Vasko90932a92020-02-12 14:33:03 +0100375
376/**
Michal Vaskoc61dd062022-06-07 11:01:28 +0200377 * @brief Insert node after a sibling.
378 *
379 * Handles inserting into NP containers and key-less lists.
380 *
381 * @param[in] sibling Sibling to insert after.
382 * @param[in] node Node to insert.
383 */
384void lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node);
385
386/**
387 * @brief Insert node before a sibling.
388 *
389 * Handles inserting into NP containers and key-less lists.
390 *
391 * @param[in] sibling Sibling to insert before.
392 * @param[in] node Node to insert.
393 */
394void lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node);
395
396/**
Michal Vaskob104f112020-07-17 09:54:54 +0200397 * @brief Insert a node into parent/siblings. Order and hashes are fully handled.
Michal Vasko90932a92020-02-12 14:33:03 +0100398 *
Michal Vasko9b368d32020-02-14 13:53:31 +0100399 * @param[in] parent Parent to insert into, NULL for top-level sibling.
400 * @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 Vasko90932a92020-02-12 14:33:03 +0100401 * @param[in] node Individual node (without siblings) to insert.
Michal Vasko6ee6f432021-07-16 09:49:14 +0200402 * @param[in] last If set, do not search for the correct anchor but always insert at the end.
Michal Vasko90932a92020-02-12 14:33:03 +0100403 */
Michal Vasko6ee6f432021-07-16 09:49:14 +0200404void lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node, ly_bool last);
Michal Vasko90932a92020-02-12 14:33:03 +0100405
406/**
Michal Vaskoa5da3292020-08-12 13:10:50 +0200407 * @brief Insert a metadata (last) into a parent
408 *
409 * @param[in] parent Parent of the metadata.
410 * @param[in] meta Metadata (list) to be added into the @p parent.
Michal Vasko871a0252020-11-11 18:35:24 +0100411 * @param[in] clear_dflt Whether to clear dflt flag starting from @p parent, recursively all NP containers.
Michal Vaskoa5da3292020-08-12 13:10:50 +0200412 */
Michal Vasko871a0252020-11-11 18:35:24 +0100413void lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200414
415/**
Michal Vasko52927e22020-03-16 17:26:14 +0100416 * @brief Create and insert a metadata (last) into a parent.
Michal Vasko90932a92020-02-12 14:33:03 +0100417 *
Michal Vasko52927e22020-03-16 17:26:14 +0100418 * @param[in] parent Parent of the metadata, can be NULL.
Michal Vasko9f96a052020-03-10 09:41:45 +0100419 * @param[in,out] meta Metadata list to add at its end if @p parent is NULL, returned created attribute.
420 * @param[in] mod Metadata module (with the annotation definition).
Michal Vasko90932a92020-02-12 14:33:03 +0100421 * @param[in] name Attribute name.
Michal Vaskof03ed032020-03-04 13:31:44 +0100422 * @param[in] name_len Length of @p name, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100423 * @param[in] value String value to be parsed.
Michal Vaskof03ed032020-03-04 13:31:44 +0100424 * @param[in] value_len Length of @p value, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100425 * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed.
Michal Vasko90932a92020-02-12 14:33:03 +0100426 * @param[in] format Input format of @p value.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200427 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200428 * @param[in] hints [Value hints](@ref lydvalhints) from the parser regarding the value type.
Michal Vaskoddd76592022-01-17 13:34:48 +0100429 * @param[in] ctx_node Value context node, may be NULL for metadata.
Michal Vasko871a0252020-11-11 18:35:24 +0100430 * @param[in] clear_dflt Whether to clear dflt flag starting from @p parent, recursively all NP containers.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200431 * @param[out] incomplete Whether the value needs to be resolved.
Michal Vasko90932a92020-02-12 14:33:03 +0100432 * @return LY_SUCCESS on success.
433 * @return LY_EINCOMPLETE in case data tree is needed to finish the validation.
434 * @return LY_ERR value if an error occurred.
435 */
Michal Vasko9f96a052020-03-10 09:41:45 +0100436LY_ERR lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name,
Radek Krejci8df109d2021-04-23 12:19:08 +0200437 size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format,
Michal Vaskoddd76592022-01-17 13:34:48 +0100438 void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node, ly_bool clear_dflt, ly_bool *incomplete);
Radek Krejci1798aae2020-07-14 13:26:06 +0200439
440/**
Michal Vaskoa5da3292020-08-12 13:10:50 +0200441 * @brief Insert an attribute (last) into a parent
Radek Krejci1798aae2020-07-14 13:26:06 +0200442 *
Michal Vaskoa5da3292020-08-12 13:10:50 +0200443 * @param[in] parent Parent of the attributes.
444 * @param[in] attr Attribute (list) to be added into the @p parent.
Radek Krejci1798aae2020-07-14 13:26:06 +0200445 */
Michal Vaskoa5da3292020-08-12 13:10:50 +0200446void lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr);
Michal Vasko90932a92020-02-12 14:33:03 +0100447
448/**
Michal Vasko52927e22020-03-16 17:26:14 +0100449 * @brief Create and insert a generic attribute (last) into a parent.
450 *
451 * @param[in] parent Parent of the attribute, can be NULL.
452 * @param[in,out] attr Attribute list to add at its end if @p parent is NULL, returned created attribute.
453 * @param[in] ctx libyang context.
454 * @param[in] name Attribute name.
455 * @param[in] name_len Length of @p name, must be set correctly.
Michal Vasko52927e22020-03-16 17:26:14 +0100456 * @param[in] prefix Attribute prefix.
457 * @param[in] prefix_len Attribute prefix length.
Radek Krejci1798aae2020-07-14 13:26:06 +0200458 * @param[in] module_key Mandatory key to reference module, can be namespace or name.
459 * @param[in] module_key_len Length of @p module_key, must be set correctly.
Michal Vasko501af032020-11-11 20:27:44 +0100460 * @param[in] value String value to be parsed.
461 * @param[in] value_len Length of @p value, must be set correctly.
462 * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed.
463 * @param[in] format Input format of @p value and @p ns.
464 * @param[in] val_prefix_data Format-specific prefix data, param is spent (even in case the function fails).
465 * @param[in] hints [Hints](@ref lydhints) from the parser regarding the node/value type.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200466 * @return LY_SUCCESS on success,
467 * @return LY_ERR value on error.
Michal Vasko52927e22020-03-16 17:26:14 +0100468 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200469LY_ERR lyd_create_attr(struct lyd_node *parent, struct lyd_attr **attr, const struct ly_ctx *ctx, const char *name,
Michal Vasko501af032020-11-11 20:27:44 +0100470 size_t name_len, const char *prefix, size_t prefix_len, const char *module_key, size_t module_key_len,
Radek Krejci8df109d2021-04-23 12:19:08 +0200471 const char *value, size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints);
Radek Krejci1798aae2020-07-14 13:26:06 +0200472
473/**
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200474 * @brief Store and canonize the given @p value into @p val according to the schema node type rules.
Radek Krejci38d85362019-09-05 16:26:38 +0200475 *
Michal Vasko8d544252020-03-02 10:19:52 +0100476 * @param[in] ctx libyang context.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200477 * @param[in,out] val Storage for the value.
478 * @param[in] type Type of the value.
Radek Krejcif9943642021-04-26 10:18:21 +0200479 * @param[in] value Value to be parsed, must not be NULL.
Michal Vaskof03ed032020-03-04 13:31:44 +0100480 * @param[in] value_len Length of the give @p value, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100481 * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200482 * @param[in] format Input format of @p value.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200483 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200484 * @param[in] hints [Value hints](@ref lydvalhints) from the parser.
485 * @param[in] ctx_node Context schema node.
486 * @param[out] incomplete Optional, set if the value also needs to be resolved.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200487 * @return LY_SUCCESS on success,
488 * @return LY_ERR value on error.
Radek Krejci38d85362019-09-05 16:26:38 +0200489 */
Radek Krejcif9943642021-04-26 10:18:21 +0200490LY_ERR lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const void *value,
Radek Krejci8df109d2021-04-23 12:19:08 +0200491 size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100492 const struct lysc_node *ctx_node, ly_bool *incomplete);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200493
494/**
495 * @brief Validate previously incompletely stored value.
496 *
497 * @param[in] ctx libyang context.
498 * @param[in] type Schema type of the value (not the stored one, but the original one).
499 * @param[in,out] val Stored value to resolve.
500 * @param[in] ctx_node Context node for the resolution.
501 * @param[in] tree Data tree for the resolution.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200502 * @return LY_SUCCESS on success,
503 * @return LY_ERR value on error.
504 */
505LY_ERR lyd_value_validate_incomplete(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *val,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100506 const struct lyd_node *ctx_node, const struct lyd_node *tree);
Radek Krejci38d85362019-09-05 16:26:38 +0200507
Michal Vaskoaebbce02021-04-06 09:23:37 +0200508/**
Michal Vasko583b4642023-05-25 10:39:34 +0200509 * @brief Check type restrictions applicable to the particular leaf/leaf-list with the given string @p value.
Michal Vaskoaebbce02021-04-06 09:23:37 +0200510 *
511 * This function check just the type's restriction, if you want to check also the data tree context (e.g. in case of
512 * require-instance restriction), use ::lyd_value_validate().
513 *
514 * @param[in] ctx libyang context for logging (function does not log errors when @p ctx is NULL)
515 * @param[in] node Schema node for the @p value.
516 * @param[in] value String value to be checked, expected to be in JSON format.
517 * @param[in] value_len Length of the given @p value (mandatory).
518 * @param[in] format Value prefix format.
519 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vasko583b4642023-05-25 10:39:34 +0200520 * @param[in] hints Value encoding hints.
Michal Vaskoaebbce02021-04-06 09:23:37 +0200521 * @return LY_SUCCESS on success
522 * @return LY_ERR value if an error occurred.
523 */
Michal Vasko583b4642023-05-25 10:39:34 +0200524LY_ERR ly_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
525 LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints);
Michal Vaskof937cfe2020-08-03 16:07:12 +0200526
Radek Krejci38d85362019-09-05 16:26:38 +0200527/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200528 * @defgroup datahash Data nodes hash manipulation
529 * @ingroup datatree
Michal Vasko8081a812021-07-15 09:19:43 +0200530 * @{
Radek Krejcie7b95092019-05-15 11:03:07 +0200531 */
532
533/**
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200534 * @brief Generate hash for the node.
535 *
536 * @param[in] node Data node to (re)generate hash value.
537 * @return LY_ERR value.
538 */
539LY_ERR lyd_hash(struct lyd_node *node);
540
541/**
542 * @brief Insert hash of the node into the hash table of its parent.
543 *
Radek Krejci8678fa42020-08-18 16:07:28 +0200544 * @param[in] node Data node which hash will be inserted into the ::lyd_node_inner.children_ht hash table of its parent.
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200545 * @return LY_ERR value.
546 */
547LY_ERR lyd_insert_hash(struct lyd_node *node);
548
549/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200550 * @brief Maintain node's parent's children hash table when unlinking the node.
551 *
552 * When completely freeing data tree, it is expected to free the parent's children hash table first, at once.
553 *
554 * @param[in] node The data node being unlinked from its parent.
555 */
556void lyd_unlink_hash(struct lyd_node *node);
557
558/** @} datahash */
559
Radek Krejci084289f2019-07-09 17:35:30 +0200560/**
Michal Vaskod59035b2020-07-08 12:00:06 +0200561 * @brief Append all list key predicates to path.
562 *
563 * @param[in] node Node with keys to print.
564 * @param[in,out] buffer Buffer to print to.
565 * @param[in,out] buflen Current buffer length.
566 * @param[in,out] bufused Current number of characters used in @p buffer.
567 * @param[in] is_static Whether buffer is static or can be reallocated.
Michal Vaskodbf3e652022-10-21 08:46:25 +0200568 * @return LY_ERR value.
Michal Vaskod59035b2020-07-08 12:00:06 +0200569 */
Radek Krejci857189e2020-09-01 13:26:36 +0200570LY_ERR lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static);
Michal Vaskod59035b2020-07-08 12:00:06 +0200571
Michal Vasko413af592021-12-13 11:50:51 +0100572/**
Michal Vaskodbf3e652022-10-21 08:46:25 +0200573 * @brief Generate a path similar to ::lyd_path() except read the parents from a set.
574 *
575 * @param[in] dnodes Set with the data nodes, from parent to the last descendant.
576 * @param[in] pathtype Type of data path to generate.
577 * @return Generated data path.
578 */
579char *lyd_path_set(const struct ly_set *dnodes, LYD_PATH_TYPE pathtype);
580
581/**
Michal Vasko413af592021-12-13 11:50:51 +0100582 * @brief Remove an object on the specific set index keeping the order of the other objects.
583 *
584 * @param[in] set Set from which a node will be removed.
585 * @param[in] index Index of the object to remove in the \p set.
586 * @param[in] destructor Optional function to free the objects being removed.
Michal Vaskodbf3e652022-10-21 08:46:25 +0200587 * @return LY_ERR value.
Michal Vasko413af592021-12-13 11:50:51 +0100588 */
589LY_ERR ly_set_rm_index_ordered(struct ly_set *set, uint32_t index, void (*destructor)(void *obj));
590
Radek Krejcie7b95092019-05-15 11:03:07 +0200591#endif /* LY_TREE_DATA_INTERNAL_H_ */