blob: 2bfa66de7cd2d90a0a864e00076739ae40aaa406 [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 Vasko4754d4a2022-12-01 10:11:21 +0100120 * @brief Set dflt flag for a NP container if applicable, recursively for parents.
121 *
122 * @param[in] node Node whose criteria for the dflt flag has changed.
123 */
124void lyd_cont_set_dflt(struct lyd_node *node);
125
126/**
Michal Vasko59892dd2022-05-13 11:02:30 +0200127 * @brief Search in the given siblings (NOT recursively) for the first schema node data instance.
128 * Uses hashes - should be used whenever possible for best performance.
129 *
130 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
131 * @param[in] schema Target data node schema to find.
132 * @param[out] match Can be NULL, otherwise the found data node.
133 * @return LY_SUCCESS on success, @p match set.
134 * @return LY_ENOTFOUND if not found, @p match set to NULL.
135 * @return LY_ERR value if another error occurred.
136 */
137LY_ERR lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match);
138
139/**
Michal Vasko106f0862021-11-02 11:49:27 +0100140 * @brief Check whether a node to be deleted is the root node, move it if it is.
141 *
142 * @param[in] root Root sibling.
143 * @param[in] to_del Node to be deleted.
144 * @param[in] mod If set, it is expected @p tree should point to the first node of @p mod. Otherwise it will simply be
145 * the first top-level sibling.
146 */
147void lyd_del_move_root(struct lyd_node **root, const struct lyd_node *to_del, const struct lys_module *mod);
148
149/**
Michal Vasko8cc3f662022-03-29 11:25:51 +0200150 * @brief Try to get schema node for data with a parent based on an extension instance.
151 *
152 * @param[in] parent Parsed parent data node. Set if @p sparent is NULL.
153 * @param[in] sparent Schema parent node. Set if @p sparent is NULL.
154 * @param[in] prefix Element prefix, if any.
155 * @param[in] prefix_len Length of @p prefix.
156 * @param[in] format Format of @p prefix.
157 * @param[in] prefix_data Format-specific data.
158 * @param[in] name Element name.
159 * @param[in] name_len Length of @p name.
160 * @param[out] snode Found schema node, NULL if no suitable was found.
Michal Vasko66330fc2022-11-21 15:52:24 +0100161 * @param[out] ext Optional extension instance that provided @p snode.
Michal Vasko8cc3f662022-03-29 11:25:51 +0200162 * @return LY_SUCCESS on success;
163 * @return LY_ENOT if no extension instance parsed the data;
164 * @return LY_ERR on error.
165 */
166LY_ERR ly_nested_ext_schema(const struct lyd_node *parent, const struct lysc_node *sparent, const char *prefix,
167 size_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data, const char *name, size_t name_len,
168 const struct lysc_node **snode, struct lysc_ext_instance **ext);
169
170/**
Michal Vasko106f0862021-11-02 11:49:27 +0100171 * @brief Free stored prefix data.
172 *
173 * @param[in] format Format of the prefixes.
174 * @param[in] prefix_data Format-specific data to free:
175 * LY_PREF_SCHEMA - const struct lysp_module * (module used for resolving prefixes from imports)
176 * LY_PREF_SCHEMA_RESOLVED - struct lyd_value_prefix * (sized array of pairs: prefix - module)
177 * LY_PREF_XML - const struct ly_set * (set with defined namespaces stored as ::lyxml_ns)
178 * LY_PREF_JSON - NULL
179 */
180void ly_free_prefix_data(LY_VALUE_FORMAT format, void *prefix_data);
181
182/**
183 * @brief Duplicate prefix data.
184 *
185 * @param[in] ctx libyang context.
186 * @param[in] format Format of the prefixes in the value.
187 * @param[in] prefix_data Prefix data to duplicate.
188 * @param[out] prefix_data_p Duplicated prefix data.
189 * @return LY_ERR value.
190 */
191LY_ERR ly_dup_prefix_data(const struct ly_ctx *ctx, LY_VALUE_FORMAT format, const void *prefix_data, void **prefix_data_p);
192
193/**
194 * @brief Store used prefixes in a string.
195 *
196 * If @p prefix_data_p are non-NULL, they are treated as valid according to the @p format_p and new possible
197 * prefixes are simply added. This way it is possible to store prefix data for several strings together.
198 *
199 * @param[in] ctx libyang context.
200 * @param[in] value Value to be parsed.
201 * @param[in] value_len Length of the @p value.
202 * @param[in] format Format of the prefixes in the value.
203 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
204 * @param[in,out] format_p Resulting format of the prefixes.
205 * @param[in,out] prefix_data_p Resulting prefix data for the value in format @p format_p.
206 * @return LY_ERR value.
207 */
208LY_ERR ly_store_prefix_data(const struct ly_ctx *ctx, const void *value, size_t value_len, LY_VALUE_FORMAT format,
209 const void *prefix_data, LY_VALUE_FORMAT *format_p, void **prefix_data_p);
210
211/**
212 * @brief Get string name of the format.
213 *
214 * @param[in] format Format whose name to get.
215 * @return Format string name.
216 */
217const char *ly_format2str(LY_VALUE_FORMAT format);
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200218
219/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100220 * @brief Create a term (leaf/leaf-list) node from a string value.
221 *
222 * Hash is calculated and new node flag is set.
Michal Vasko90932a92020-02-12 14:33:03 +0100223 *
224 * @param[in] schema Schema node of the new data node.
225 * @param[in] value String value to be parsed.
Michal Vaskof03ed032020-03-04 13:31:44 +0100226 * @param[in] value_len Length of @p value, must be set correctly.
Michal Vasko989cdb42023-10-06 15:32:37 +0200227 * @param[in] is_utf8 Whether @p value is a valid UTF-8 string, if applicable.
stewegd4cde642024-02-21 08:34:16 +0100228 * @param[in] store_only Whether to perform storing operation only.
Michal Vasko90932a92020-02-12 14:33:03 +0100229 * @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 +0100230 * @param[in] format Input format of @p value.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200231 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200232 * @param[in] hints [Value hints](@ref lydvalhints) from the parser regarding the value type.
233 * @param[out] incomplete Whether the value needs to be resolved.
Michal Vasko90932a92020-02-12 14:33:03 +0100234 * @param[out] node Created node.
235 * @return LY_SUCCESS on success.
236 * @return LY_EINCOMPLETE in case data tree is needed to finish the validation.
237 * @return LY_ERR value if an error occurred.
238 */
Michal Vasko989cdb42023-10-06 15:32:37 +0200239LY_ERR lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool is_utf8,
stewegd4cde642024-02-21 08:34:16 +0100240 ly_bool store_only, ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints,
241 ly_bool *incomplete, struct lyd_node **node);
Michal Vasko90932a92020-02-12 14:33:03 +0100242
243/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100244 * @brief Create a term (leaf/leaf-list) node from a parsed value by duplicating it.
245 *
246 * Hash is calculated and new node flag is set.
247 *
248 * @param[in] schema Schema node of the new data node.
249 * @param[in] val Parsed value to use.
250 * @param[out] node Created node.
251 * @return LY_SUCCESS on success.
252 * @return LY_ERR value if an error occurred.
253 */
254LY_ERR lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node);
255
256/**
257 * @brief Create an inner (container/list/RPC/action/notification) node.
258 *
259 * Hash is calculated and new node flag is set except
260 * for list with keys, when the hash is not calculated!
261 * Also, non-presence container has its default flag set.
Michal Vasko90932a92020-02-12 14:33:03 +0100262 *
263 * @param[in] schema Schema node of the new data node.
264 * @param[out] node Created node.
265 * @return LY_SUCCESS on success.
266 * @return LY_ERR value if an error occurred.
267 */
268LY_ERR lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node);
269
270/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100271 * @brief Create a list with all its keys (cannot be used for key-less list).
272 *
273 * Hash is calculated and new node flag is set.
Michal Vasko90932a92020-02-12 14:33:03 +0100274 *
275 * @param[in] schema Schema node of the new data node.
Michal Vasko004d3152020-06-11 19:59:22 +0200276 * @param[in] predicates Compiled key list predicates.
Michal Vasko90189962023-02-28 12:10:34 +0100277 * @param[in] vars Array of defined variables to use in predicates, may be NULL.
stewegd4cde642024-02-21 08:34:16 +0100278 * @param[in] store_only Whether to perform storing operation only.
Michal Vasko90932a92020-02-12 14:33:03 +0100279 * @param[out] node Created node.
280 * @return LY_SUCCESS on success.
281 * @return LY_ERR value if an error occurred.
282 */
Michal Vasko90189962023-02-28 12:10:34 +0100283LY_ERR lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates,
stewegd4cde642024-02-21 08:34:16 +0100284 const struct lyxp_var *vars, ly_bool store_only, struct lyd_node **node);
Michal Vasko90932a92020-02-12 14:33:03 +0100285
286/**
Michal Vasko59892dd2022-05-13 11:02:30 +0200287 * @brief Create a list with all its keys (cannot be used for key-less list).
288 *
289 * Hash is calculated and new node flag is set.
290 *
291 * @param[in] schema Schema node of the new data node.
292 * @param[in] keys Key list predicates.
293 * @param[in] keys_len Length of @p keys.
stewegd4cde642024-02-21 08:34:16 +0100294 * @param[in] store_only Whether to perform storing operation only.
Michal Vasko59892dd2022-05-13 11:02:30 +0200295 * @param[out] node Created node.
296 * @return LY_SUCCESS on success.
297 * @return LY_ERR value if an error occurred.
298 */
stewegd4cde642024-02-21 08:34:16 +0100299LY_ERR lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, ly_bool store_only,
300 struct lyd_node **node);
Michal Vasko59892dd2022-05-13 11:02:30 +0200301
302/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100303 * @brief Create an anyxml/anydata node.
304 *
305 * Hash is calculated and flags are properly set based on @p is_valid.
Michal Vasko90932a92020-02-12 14:33:03 +0100306 *
307 * @param[in] schema Schema node of the new data node.
Michal Vasko366a4a12020-12-04 16:23:57 +0100308 * @param[in] value Value of the any node.
Michal Vasko90932a92020-02-12 14:33:03 +0100309 * @param[in] value_type Value type of the value.
Michal Vasko742a5b12022-02-24 16:07:27 +0100310 * @param[in] use_value Whether to use dynamic @p value or duplicate it.
Michal Vasko90932a92020-02-12 14:33:03 +0100311 * @param[out] node Created node.
312 * @return LY_SUCCESS on success.
313 * @return LY_ERR value if an error occurred.
314 */
Michal Vasko9b368d32020-02-14 13:53:31 +0100315LY_ERR lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type,
Michal Vasko366a4a12020-12-04 16:23:57 +0100316 ly_bool use_value, struct lyd_node **node);
Michal Vasko90932a92020-02-12 14:33:03 +0100317
318/**
Michal Vasko52927e22020-03-16 17:26:14 +0100319 * @brief Create an opaque node.
320 *
321 * @param[in] ctx libyang context.
322 * @param[in] name Element name.
323 * @param[in] name_len Length of @p name, must be set correctly.
Michal Vasko52927e22020-03-16 17:26:14 +0100324 * @param[in] prefix Element prefix.
325 * @param[in] pref_len Length of @p prefix, must be set correctly.
Radek Krejci1798aae2020-07-14 13:26:06 +0200326 * @param[in] module_key Mandatory key to reference module, can be namespace or name.
327 * @param[in] module_key_len Length of @p module_key, must be set correctly.
Michal Vasko501af032020-11-11 20:27:44 +0100328 * @param[in] value String value to be parsed.
329 * @param[in] value_len Length of @p value, must be set correctly.
330 * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed.
331 * @param[in] format Input format of @p value and @p ns.
Radek Krejciec9ad602021-01-04 10:46:30 +0100332 * @param[in] val_prefix_data Format-specific prefix data, param is spent (even in case the function fails):
333 * LY_PREF_SCHEMA - const struct lysp_module * (module used for resolving prefixes from imports)
334 * LY_PREF_SCHEMA_RESOLVED - struct lyd_value_prefix * (sized array of pairs: prefix - module)
335 * LY_PREF_XML - const struct ly_set * (set with defined namespaces stored as ::lyxml_ns)
336 * LY_PREF_JSON - NULL
Michal Vasko501af032020-11-11 20:27:44 +0100337 * @param[in] hints [Hints](@ref lydhints) from the parser regarding the node/value type.
Michal Vasko52927e22020-03-16 17:26:14 +0100338 * @param[out] node Created node.
339 * @return LY_SUCCESS on success.
340 * @return LY_ERR value if an error occurred.
341 */
Michal Vasko501af032020-11-11 20:27:44 +0100342LY_ERR lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t pref_len,
343 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 +0200344 LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints, struct lyd_node **node);
Michal Vasko52927e22020-03-16 17:26:14 +0100345
346/**
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200347 * @brief Check the existence and create any non-existing implicit siblings, recursively for the created nodes.
348 *
349 * @param[in] parent Parent of the potential default values, NULL for top-level siblings.
350 * @param[in,out] first First sibling.
351 * @param[in] sparent Schema parent of the siblings, NULL if schema of @p parent can be used.
352 * @param[in] mod Module of the default values, NULL for nested siblings.
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200353 * @param[in] node_when Optional set to add nodes with "when" conditions into.
Michal Vaskoc43c8ab2021-03-05 13:32:44 +0100354 * @param[in] node_types Optional set to add nodes with unresolved types into.
Michal Vaskofcbd78f2022-08-26 08:34:15 +0200355 * @param[in] ext_node Optional set to add nodes with extension instance node callbacks into.
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200356 * @param[in] impl_opts Implicit options (@ref implicitoptions).
357 * @param[in,out] diff Validation diff.
358 * @return LY_ERR value.
359 */
360LY_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 +0200361 const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_types, struct ly_set *ext_node,
362 uint32_t impl_opts, struct lyd_node **diff);
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200363
364/**
Michal Vaskob104f112020-07-17 09:54:54 +0200365 * @brief Find the next node, before which to insert the new node.
Michal Vasko90932a92020-02-12 14:33:03 +0100366 *
Michal Vaskob104f112020-07-17 09:54:54 +0200367 * @param[in] first_sibling First sibling of the nodes to consider.
368 * @param[in] new_node Node that will be inserted.
369 * @return Node to insert after.
370 * @return NULL if the new node should be first.
Michal Vasko90932a92020-02-12 14:33:03 +0100371 */
Michal Vaskob104f112020-07-17 09:54:54 +0200372struct 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 +0100373
374/**
Michal Vaskoc61dd062022-06-07 11:01:28 +0200375 * @brief Insert node after a sibling.
376 *
377 * Handles inserting into NP containers and key-less lists.
378 *
aPiecek743184b2024-02-01 13:25:56 +0100379 * @param[in,out] first_sibling Optional, useful for optimization. The function operates with the first sibling
380 * only if the node is inserted last. It is optimal when the first sibling is set. If it is set to NULL or
381 * if it points to a NULL pointer, then the function will find the first sibling itself.
Michal Vaskoc61dd062022-06-07 11:01:28 +0200382 * @param[in] sibling Sibling to insert after.
383 * @param[in] node Node to insert.
384 */
aPiecek743184b2024-02-01 13:25:56 +0100385void lyd_insert_after_node(struct lyd_node **first_sibling, struct lyd_node *sibling, struct lyd_node *node);
Michal Vaskoc61dd062022-06-07 11:01:28 +0200386
387/**
388 * @brief Insert node before a sibling.
389 *
390 * Handles inserting into NP containers and key-less lists.
391 *
392 * @param[in] sibling Sibling to insert before.
393 * @param[in] node Node to insert.
394 */
395void lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node);
396
397/**
aPiecek1462ab12024-02-07 09:13:29 +0100398 * @defgroup insertorder Data insert order.
399 *
400 * Various options for optimal node insertion.
401 * Flags that cause the order of nodes not to be checked are adapted to fast insertion but can cause
402 * nodes for (leaf-)lists with LYS_ORDBY_SYSTEM flag set to be out of order, which is an undesirable state,
403 * so these flags must be set carefully. In such exceptional cases, (leaf-)list instances may remain unsorted,
404 * in which case inserting a new node causes sorting to be invoked.
405 * @{
406 */
407
408#define LYD_INSERT_NODE_DEFAULT 0x00 /**< Default behavior. Node is inserted to preserve order. */
409#define LYD_INSERT_NODE_LAST 0x01 /**< Node inserted as last sibling. Node ordering is checked only
410 in Debug build, to detect misuse of the LYD_PARSE_ORDERED flag. */
411#define LYD_INSERT_NODE_LAST_BY_SCHEMA 0x02 /**< The node is inserted according to the schema as a last instance.
412 Node order not checked. */
413
414/** @} insertorder */
415
416/**
Michal Vaskob104f112020-07-17 09:54:54 +0200417 * @brief Insert a node into parent/siblings. Order and hashes are fully handled.
Michal Vasko90932a92020-02-12 14:33:03 +0100418 *
Michal Vasko9b368d32020-02-14 13:53:31 +0100419 * @param[in] parent Parent to insert into, NULL for top-level sibling.
420 * @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 +0100421 * @param[in] node Individual node (without siblings) to insert.
aPiecek1462ab12024-02-07 09:13:29 +0100422 * @param[in] order Options for inserting (sorting) the node (@ref insertorder).
Michal Vasko90932a92020-02-12 14:33:03 +0100423 */
aPiecek1462ab12024-02-07 09:13:29 +0100424void lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node,
425 uint32_t order);
Michal Vasko90932a92020-02-12 14:33:03 +0100426
427/**
aPieceke6955f42024-01-19 14:01:54 +0100428 * @brief Insert a node into parent/siblings, either before the 'anchor' or as the last sibling.
429 *
430 * @param[in] parent Parent to insert into, NULL for top-level sibling.
aPiecek743184b2024-02-01 13:25:56 +0100431 * @param[in,out] first_sibling First sibling, pointing to NULL pointer if no top-level sibling exist yet
432 * or if @p parent is set.
aPieceke6955f42024-01-19 14:01:54 +0100433 * @param[in] node Individual node (without siblings) to insert.
434 */
aPiecek743184b2024-02-01 13:25:56 +0100435void lyd_insert_node_ordby_schema(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node);
aPieceke6955f42024-01-19 14:01:54 +0100436
437/**
Michal Vasko2e784f82024-01-11 09:51:22 +0100438 * @brief Unlink the specified data subtree.
439 *
440 * @param[in] node Data tree node to be unlinked (together with all the children).
441 */
442void lyd_unlink(struct lyd_node *node);
443
444/**
Michal Vaskoa5da3292020-08-12 13:10:50 +0200445 * @brief Insert a metadata (last) into a parent
446 *
447 * @param[in] parent Parent of the metadata.
448 * @param[in] meta Metadata (list) to be added into the @p parent.
Michal Vasko871a0252020-11-11 18:35:24 +0100449 * @param[in] clear_dflt Whether to clear dflt flag starting from @p parent, recursively all NP containers.
Michal Vaskoa5da3292020-08-12 13:10:50 +0200450 */
Michal Vasko871a0252020-11-11 18:35:24 +0100451void lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200452
453/**
aPiecek0e92afc2023-11-08 10:48:02 +0100454 * @brief Unlink a single metadata instance.
455 *
456 * @param[in] meta Metadata to unlink.
457 */
458void lyd_unlink_meta_single(struct lyd_meta *meta);
459
460/**
Michal Vasko52927e22020-03-16 17:26:14 +0100461 * @brief Create and insert a metadata (last) into a parent.
Michal Vasko90932a92020-02-12 14:33:03 +0100462 *
Michal Vasko52927e22020-03-16 17:26:14 +0100463 * @param[in] parent Parent of the metadata, can be NULL.
Michal Vasko9f96a052020-03-10 09:41:45 +0100464 * @param[in,out] meta Metadata list to add at its end if @p parent is NULL, returned created attribute.
465 * @param[in] mod Metadata module (with the annotation definition).
Michal Vasko90932a92020-02-12 14:33:03 +0100466 * @param[in] name Attribute name.
Michal Vaskof03ed032020-03-04 13:31:44 +0100467 * @param[in] name_len Length of @p name, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100468 * @param[in] value String value to be parsed.
Michal Vaskof03ed032020-03-04 13:31:44 +0100469 * @param[in] value_len Length of @p value, must be set correctly.
Michal Vasko989cdb42023-10-06 15:32:37 +0200470 * @param[in] is_utf8 Whether @p value is a valid UTF-8 string, if applicable.
stewegd4cde642024-02-21 08:34:16 +0100471 * @param[in] store_only Whether to perform storing operation only.
Michal Vasko90932a92020-02-12 14:33:03 +0100472 * @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 +0100473 * @param[in] format Input format of @p value.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200474 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200475 * @param[in] hints [Value hints](@ref lydvalhints) from the parser regarding the value type.
Michal Vaskoddd76592022-01-17 13:34:48 +0100476 * @param[in] ctx_node Value context node, may be NULL for metadata.
Michal Vasko871a0252020-11-11 18:35:24 +0100477 * @param[in] clear_dflt Whether to clear dflt flag starting from @p parent, recursively all NP containers.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200478 * @param[out] incomplete Whether the value needs to be resolved.
Michal Vasko90932a92020-02-12 14:33:03 +0100479 * @return LY_SUCCESS on success.
480 * @return LY_EINCOMPLETE in case data tree is needed to finish the validation.
481 * @return LY_ERR value if an error occurred.
482 */
Michal Vasko9f96a052020-03-10 09:41:45 +0100483LY_ERR lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name,
stewegd4cde642024-02-21 08:34:16 +0100484 size_t name_len, const char *value, size_t value_len, ly_bool is_utf8, ly_bool store_only, ly_bool *dynamic,
485 LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node, ly_bool clear_dflt,
486 ly_bool *incomplete);
Radek Krejci1798aae2020-07-14 13:26:06 +0200487
488/**
aPiecek41680342023-11-08 10:19:44 +0100489 * @brief Create a copy of the metadata.
490 *
491 * @param[in] parent_ctx Target context for duplicated nodes.
492 * @param[in] meta Metadata to copy.
493 * @param[in] parent Node where to append the new metadata.
494 * @param[out] dup Optional created metadata copy.
495 * @return LY_ERR value.
496 */
497LY_ERR lyd_dup_meta_single_to_ctx(const struct ly_ctx *parent_ctx, const struct lyd_meta *meta, struct lyd_node *parent,
498 struct lyd_meta **dup);
499
500/**
Michal Vaskoa5da3292020-08-12 13:10:50 +0200501 * @brief Insert an attribute (last) into a parent
Radek Krejci1798aae2020-07-14 13:26:06 +0200502 *
Michal Vaskoa5da3292020-08-12 13:10:50 +0200503 * @param[in] parent Parent of the attributes.
504 * @param[in] attr Attribute (list) to be added into the @p parent.
Radek Krejci1798aae2020-07-14 13:26:06 +0200505 */
Michal Vaskoa5da3292020-08-12 13:10:50 +0200506void lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr);
Michal Vasko90932a92020-02-12 14:33:03 +0100507
508/**
Michal Vasko52927e22020-03-16 17:26:14 +0100509 * @brief Create and insert a generic attribute (last) into a parent.
510 *
511 * @param[in] parent Parent of the attribute, can be NULL.
512 * @param[in,out] attr Attribute list to add at its end if @p parent is NULL, returned created attribute.
513 * @param[in] ctx libyang context.
514 * @param[in] name Attribute name.
515 * @param[in] name_len Length of @p name, must be set correctly.
Michal Vasko52927e22020-03-16 17:26:14 +0100516 * @param[in] prefix Attribute prefix.
517 * @param[in] prefix_len Attribute prefix length.
Radek Krejci1798aae2020-07-14 13:26:06 +0200518 * @param[in] module_key Mandatory key to reference module, can be namespace or name.
519 * @param[in] module_key_len Length of @p module_key, must be set correctly.
Michal Vasko501af032020-11-11 20:27:44 +0100520 * @param[in] value String value to be parsed.
521 * @param[in] value_len Length of @p value, must be set correctly.
522 * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed.
523 * @param[in] format Input format of @p value and @p ns.
524 * @param[in] val_prefix_data Format-specific prefix data, param is spent (even in case the function fails).
525 * @param[in] hints [Hints](@ref lydhints) from the parser regarding the node/value type.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200526 * @return LY_SUCCESS on success,
527 * @return LY_ERR value on error.
Michal Vasko52927e22020-03-16 17:26:14 +0100528 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200529LY_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 +0100530 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 +0200531 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 +0200532
533/**
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200534 * @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 +0200535 *
Michal Vasko8d544252020-03-02 10:19:52 +0100536 * @param[in] ctx libyang context.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200537 * @param[in,out] val Storage for the value.
538 * @param[in] type Type of the value.
Radek Krejcif9943642021-04-26 10:18:21 +0200539 * @param[in] value Value to be parsed, must not be NULL.
Michal Vaskof03ed032020-03-04 13:31:44 +0100540 * @param[in] value_len Length of the give @p value, must be set correctly.
Michal Vasko989cdb42023-10-06 15:32:37 +0200541 * @param[in] is_utf8 Whether @p value is a valid UTF-8 string, if applicable.
stewegd4cde642024-02-21 08:34:16 +0100542 * @param[in] store_only Whether to perform storing operation only.
Michal Vasko90932a92020-02-12 14:33:03 +0100543 * @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 +0200544 * @param[in] format Input format of @p value.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200545 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200546 * @param[in] hints [Value hints](@ref lydvalhints) from the parser.
547 * @param[in] ctx_node Context schema node.
548 * @param[out] incomplete Optional, set if the value also needs to be resolved.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200549 * @return LY_SUCCESS on success,
550 * @return LY_ERR value on error.
Radek Krejci38d85362019-09-05 16:26:38 +0200551 */
Radek Krejcif9943642021-04-26 10:18:21 +0200552LY_ERR lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const void *value,
stewegd4cde642024-02-21 08:34:16 +0100553 size_t value_len, ly_bool is_utf8, ly_bool store_only, ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data,
554 uint32_t hints, const struct lysc_node *ctx_node, ly_bool *incomplete);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200555
556/**
557 * @brief Validate previously incompletely stored value.
558 *
559 * @param[in] ctx libyang context.
560 * @param[in] type Schema type of the value (not the stored one, but the original one).
561 * @param[in,out] val Stored value to resolve.
562 * @param[in] ctx_node Context node for the resolution.
563 * @param[in] tree Data tree for the resolution.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200564 * @return LY_SUCCESS on success,
565 * @return LY_ERR value on error.
566 */
567LY_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 +0100568 const struct lyd_node *ctx_node, const struct lyd_node *tree);
Radek Krejci38d85362019-09-05 16:26:38 +0200569
Michal Vaskoaebbce02021-04-06 09:23:37 +0200570/**
Michal Vasko583b4642023-05-25 10:39:34 +0200571 * @brief Check type restrictions applicable to the particular leaf/leaf-list with the given string @p value.
Michal Vaskoaebbce02021-04-06 09:23:37 +0200572 *
573 * This function check just the type's restriction, if you want to check also the data tree context (e.g. in case of
574 * require-instance restriction), use ::lyd_value_validate().
575 *
576 * @param[in] ctx libyang context for logging (function does not log errors when @p ctx is NULL)
577 * @param[in] node Schema node for the @p value.
578 * @param[in] value String value to be checked, expected to be in JSON format.
579 * @param[in] value_len Length of the given @p value (mandatory).
580 * @param[in] format Value prefix format.
581 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vasko583b4642023-05-25 10:39:34 +0200582 * @param[in] hints Value encoding hints.
Michal Vaskoaebbce02021-04-06 09:23:37 +0200583 * @return LY_SUCCESS on success
584 * @return LY_ERR value if an error occurred.
585 */
Michal Vasko583b4642023-05-25 10:39:34 +0200586LY_ERR ly_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
587 LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints);
Michal Vaskof937cfe2020-08-03 16:07:12 +0200588
Radek Krejci38d85362019-09-05 16:26:38 +0200589/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200590 * @defgroup datahash Data nodes hash manipulation
591 * @ingroup datatree
Michal Vasko8081a812021-07-15 09:19:43 +0200592 * @{
Radek Krejcie7b95092019-05-15 11:03:07 +0200593 */
594
595/**
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200596 * @brief Generate hash for the node.
597 *
598 * @param[in] node Data node to (re)generate hash value.
599 * @return LY_ERR value.
600 */
601LY_ERR lyd_hash(struct lyd_node *node);
602
603/**
604 * @brief Insert hash of the node into the hash table of its parent.
605 *
Radek Krejci8678fa42020-08-18 16:07:28 +0200606 * @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 +0200607 * @return LY_ERR value.
608 */
609LY_ERR lyd_insert_hash(struct lyd_node *node);
610
611/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200612 * @brief Maintain node's parent's children hash table when unlinking the node.
613 *
614 * When completely freeing data tree, it is expected to free the parent's children hash table first, at once.
615 *
616 * @param[in] node The data node being unlinked from its parent.
617 */
618void lyd_unlink_hash(struct lyd_node *node);
619
620/** @} datahash */
621
Radek Krejci084289f2019-07-09 17:35:30 +0200622/**
Michal Vaskod59035b2020-07-08 12:00:06 +0200623 * @brief Append all list key predicates to path.
624 *
625 * @param[in] node Node with keys to print.
626 * @param[in,out] buffer Buffer to print to.
627 * @param[in,out] buflen Current buffer length.
628 * @param[in,out] bufused Current number of characters used in @p buffer.
629 * @param[in] is_static Whether buffer is static or can be reallocated.
Michal Vaskodbf3e652022-10-21 08:46:25 +0200630 * @return LY_ERR value.
Michal Vaskod59035b2020-07-08 12:00:06 +0200631 */
Radek Krejci857189e2020-09-01 13:26:36 +0200632LY_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 +0200633
Michal Vasko413af592021-12-13 11:50:51 +0100634/**
Michal Vaskodbf3e652022-10-21 08:46:25 +0200635 * @brief Generate a path similar to ::lyd_path() except read the parents from a set.
636 *
637 * @param[in] dnodes Set with the data nodes, from parent to the last descendant.
638 * @param[in] pathtype Type of data path to generate.
639 * @return Generated data path.
640 */
641char *lyd_path_set(const struct ly_set *dnodes, LYD_PATH_TYPE pathtype);
642
643/**
Michal Vasko413af592021-12-13 11:50:51 +0100644 * @brief Remove an object on the specific set index keeping the order of the other objects.
645 *
646 * @param[in] set Set from which a node will be removed.
647 * @param[in] index Index of the object to remove in the \p set.
648 * @param[in] destructor Optional function to free the objects being removed.
Michal Vaskodbf3e652022-10-21 08:46:25 +0200649 * @return LY_ERR value.
Michal Vasko413af592021-12-13 11:50:51 +0100650 */
651LY_ERR ly_set_rm_index_ordered(struct ly_set *set, uint32_t index, void (*destructor)(void *obj));
652
stewegf9041a22024-01-18 13:29:12 +0100653/**
654 * @brief Frees data within leafref links record
655 *
656 * @param[in] rec The leafref links record
657 */
658void lyd_free_leafref_links_rec(struct lyd_leafref_links_rec *rec);
659
660/**
661 * @brief Frees all leafref nodes and target node of given data node
662 *
663 * @param[in] node The data node, which leafref nodes and/or target node should be cleared.
664 */
665void lyd_free_leafref_nodes(const struct lyd_node_term *node);
666
667/**
668 * @brief Gets or creates the leafref links record.
669 *
670 * @param[in] node The term data node.
671 * @param[out] record The leafref links record.
672 * @param[in] create Whether to create record if not exists.
673 * @return LY_SUCCESS on success.
674 * @return LY_ERR value on error.
675 */
676LY_ERR lyd_get_or_create_leafref_links_record(const struct lyd_node_term *node, struct lyd_leafref_links_rec **record, ly_bool create);
677
678/**
steweg67388952024-01-25 12:14:50 +0100679 * @brief Adds links between leafref and data node.
stewegf9041a22024-01-18 13:29:12 +0100680 *
681 * If the links were already added, it will not be added again.
682 * This API requires usage of LY_CTX_LEAFREF_LINKING context flag.
683 *
684 * @param[in] node Data node to which, the leafref is pointing to.
685 * @param[in] leafref_node The leafref, which points to given node.
686 * @return LY_SUCCESS on success.
687 * @return LY_ERR value on error.
688 */
689LY_ERR lyd_link_leafref_node(const struct lyd_node_term *node, const struct lyd_node_term *leafref_node);
690
691/**
steweg67388952024-01-25 12:14:50 +0100692 * @brief Removes links between leafref and data node.
stewegf9041a22024-01-18 13:29:12 +0100693 *
694 * If the links were never added, it will be silently ignored.
695 * This API requires usage of LY_CTX_LEAFREF_LINKING context flag.
696 *
697 * @param[in] node Data node to which, the leafref is pointing to.
698 * @param[in] leafref_node The leafref, which points to given node.
699 * @return LY_SUCCESS on success.
700 * @return LY_ERR value on error.
701 */
702LY_ERR lyd_unlink_leafref_node(const struct lyd_node_term *node, const struct lyd_node_term *leafref_node);
703
aPiecekcada0d72024-01-11 15:04:12 +0100704/**
705 * @brief Unlink the specified data subtree.
706 *
707 * The lyds_unlink() is NOT called in this function.
708 *
aPiecek743184b2024-02-01 13:25:56 +0100709 * @param[in,out] first_sibling Optional, performs an update if @p node is first or @p sibling is last.
aPiecekcada0d72024-01-11 15:04:12 +0100710 * @param[in] node Data tree node to be unlinked (together with all the children).
711 */
aPiecek743184b2024-02-01 13:25:56 +0100712void lyd_unlink_ignore_lyds(struct lyd_node **first_sibling, struct lyd_node *node);
aPiecekcada0d72024-01-11 15:04:12 +0100713
Radek Krejcie7b95092019-05-15 11:03:07 +0200714#endif /* LY_TREE_DATA_INTERNAL_H_ */