blob: 84505802a46a3b751bc33ee5439c82d2baabe9dc [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
2 * @file tree_data_internal.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief internal functions for YANG schema trees.
5 *
Michal Vasko60ea6352020-06-29 13:39:39 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcie7b95092019-05-15 11:03:07 +02007 *
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 Krejci857189e2020-09-01 13:26:36 +020018#include "log.h"
Radek Krejciaca74032019-06-04 08:53:06 +020019#include "plugins_types.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020020#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020021
Michal Vasko52927e22020-03-16 17:26:14 +010022#include <stddef.h>
23
Michal Vasko004d3152020-06-11 19:59:22 +020024struct ly_path_predicate;
Michal Vasko8cc3f662022-03-29 11:25:51 +020025struct lyd_ctx;
Michal Vaskoa6669ba2020-08-06 16:14:26 +020026struct lysc_module;
Michal Vasko004d3152020-06-11 19:59:22 +020027
Radek Krejcif13b87b2020-12-01 22:02:17 +010028#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 Krejcie7b95092019-05-15 11:03:07 +020035/**
Michal Vaskod7c048c2021-05-18 16:12:55 +020036 * @brief Internal structure for remembering "used" instances of lists with duplicate instances allowed.
37 */
38struct 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 */
52LY_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 */
60void lyd_dup_inst_free(struct lyd_dup_inst *dup_inst);
61
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 Vasko59892dd2022-05-13 11:02:30 +0200128 * @brief Search in the given siblings (NOT recursively) for the first schema node data instance.
129 * Uses hashes - should be used whenever possible for best performance.
130 *
131 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
132 * @param[in] schema Target data node schema to find.
133 * @param[out] match Can be NULL, otherwise the found data node.
134 * @return LY_SUCCESS on success, @p match set.
135 * @return LY_ENOTFOUND if not found, @p match set to NULL.
136 * @return LY_ERR value if another error occurred.
137 */
138LY_ERR lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match);
139
140/**
Michal Vasko106f0862021-11-02 11:49:27 +0100141 * @brief Check whether a node to be deleted is the root node, move it if it is.
142 *
143 * @param[in] root Root sibling.
144 * @param[in] to_del Node to be deleted.
145 * @param[in] mod If set, it is expected @p tree should point to the first node of @p mod. Otherwise it will simply be
146 * the first top-level sibling.
147 */
148void lyd_del_move_root(struct lyd_node **root, const struct lyd_node *to_del, const struct lys_module *mod);
149
150/**
Michal Vasko8cc3f662022-03-29 11:25:51 +0200151 * @brief Try to get schema node for data with a parent based on an extension instance.
152 *
153 * @param[in] parent Parsed parent data node. Set if @p sparent is NULL.
154 * @param[in] sparent Schema parent node. Set if @p sparent is NULL.
155 * @param[in] prefix Element prefix, if any.
156 * @param[in] prefix_len Length of @p prefix.
157 * @param[in] format Format of @p prefix.
158 * @param[in] prefix_data Format-specific data.
159 * @param[in] name Element name.
160 * @param[in] name_len Length of @p name.
161 * @param[out] snode Found schema node, NULL if no suitable was found.
162 * @param[out] ext Extension instance that provided @p snode.
163 * @return LY_SUCCESS on success;
164 * @return LY_ENOT if no extension instance parsed the data;
165 * @return LY_ERR on error.
166 */
167LY_ERR ly_nested_ext_schema(const struct lyd_node *parent, const struct lysc_node *sparent, const char *prefix,
168 size_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data, const char *name, size_t name_len,
169 const struct lysc_node **snode, struct lysc_ext_instance **ext);
170
171/**
Michal Vasko106f0862021-11-02 11:49:27 +0100172 * @brief Free stored prefix data.
173 *
174 * @param[in] format Format of the prefixes.
175 * @param[in] prefix_data Format-specific data to free:
176 * LY_PREF_SCHEMA - const struct lysp_module * (module used for resolving prefixes from imports)
177 * LY_PREF_SCHEMA_RESOLVED - struct lyd_value_prefix * (sized array of pairs: prefix - module)
178 * LY_PREF_XML - const struct ly_set * (set with defined namespaces stored as ::lyxml_ns)
179 * LY_PREF_JSON - NULL
180 */
181void ly_free_prefix_data(LY_VALUE_FORMAT format, void *prefix_data);
182
183/**
184 * @brief Duplicate prefix data.
185 *
186 * @param[in] ctx libyang context.
187 * @param[in] format Format of the prefixes in the value.
188 * @param[in] prefix_data Prefix data to duplicate.
189 * @param[out] prefix_data_p Duplicated prefix data.
190 * @return LY_ERR value.
191 */
192LY_ERR ly_dup_prefix_data(const struct ly_ctx *ctx, LY_VALUE_FORMAT format, const void *prefix_data, void **prefix_data_p);
193
194/**
195 * @brief Store used prefixes in a string.
196 *
197 * If @p prefix_data_p are non-NULL, they are treated as valid according to the @p format_p and new possible
198 * prefixes are simply added. This way it is possible to store prefix data for several strings together.
199 *
200 * @param[in] ctx libyang context.
201 * @param[in] value Value to be parsed.
202 * @param[in] value_len Length of the @p value.
203 * @param[in] format Format of the prefixes in the value.
204 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
205 * @param[in,out] format_p Resulting format of the prefixes.
206 * @param[in,out] prefix_data_p Resulting prefix data for the value in format @p format_p.
207 * @return LY_ERR value.
208 */
209LY_ERR ly_store_prefix_data(const struct ly_ctx *ctx, const void *value, size_t value_len, LY_VALUE_FORMAT format,
210 const void *prefix_data, LY_VALUE_FORMAT *format_p, void **prefix_data_p);
211
212/**
213 * @brief Get string name of the format.
214 *
215 * @param[in] format Format whose name to get.
216 * @return Format string name.
217 */
218const char *ly_format2str(LY_VALUE_FORMAT format);
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200219
220/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100221 * @brief Create a term (leaf/leaf-list) node from a string value.
222 *
223 * Hash is calculated and new node flag is set.
Michal Vasko90932a92020-02-12 14:33:03 +0100224 *
225 * @param[in] schema Schema node of the new data node.
226 * @param[in] value String value to be parsed.
Michal Vaskof03ed032020-03-04 13:31:44 +0100227 * @param[in] value_len Length of @p value, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100228 * @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 +0100229 * @param[in] format Input format of @p value.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200230 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200231 * @param[in] hints [Value hints](@ref lydvalhints) from the parser regarding the value type.
232 * @param[out] incomplete Whether the value needs to be resolved.
Michal Vasko90932a92020-02-12 14:33:03 +0100233 * @param[out] node Created node.
234 * @return LY_SUCCESS on success.
235 * @return LY_EINCOMPLETE in case data tree is needed to finish the validation.
236 * @return LY_ERR value if an error occurred.
237 */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200238LY_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 +0200239 LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, ly_bool *incomplete, struct lyd_node **node);
Michal Vasko90932a92020-02-12 14:33:03 +0100240
241/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100242 * @brief Create a term (leaf/leaf-list) node from a parsed value by duplicating it.
243 *
244 * Hash is calculated and new node flag is set.
245 *
246 * @param[in] schema Schema node of the new data node.
247 * @param[in] val Parsed value to use.
248 * @param[out] node Created node.
249 * @return LY_SUCCESS on success.
250 * @return LY_ERR value if an error occurred.
251 */
252LY_ERR lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node);
253
254/**
255 * @brief Create an inner (container/list/RPC/action/notification) node.
256 *
257 * Hash is calculated and new node flag is set except
258 * for list with keys, when the hash is not calculated!
259 * Also, non-presence container has its default flag set.
Michal Vasko90932a92020-02-12 14:33:03 +0100260 *
261 * @param[in] schema Schema node of the new data node.
262 * @param[out] node Created node.
263 * @return LY_SUCCESS on success.
264 * @return LY_ERR value if an error occurred.
265 */
266LY_ERR lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node);
267
268/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100269 * @brief Create a list with all its keys (cannot be used for key-less list).
270 *
271 * Hash is calculated and new node flag is set.
Michal Vasko90932a92020-02-12 14:33:03 +0100272 *
273 * @param[in] schema Schema node of the new data node.
Michal Vasko004d3152020-06-11 19:59:22 +0200274 * @param[in] predicates Compiled key list predicates.
Michal Vasko90932a92020-02-12 14:33:03 +0100275 * @param[out] node Created node.
276 * @return LY_SUCCESS on success.
277 * @return LY_ERR value if an error occurred.
278 */
Michal Vasko004d3152020-06-11 19:59:22 +0200279LY_ERR lyd_create_list(const struct lysc_node *schema, const struct ly_path_predicate *predicates, struct lyd_node **node);
Michal Vasko90932a92020-02-12 14:33:03 +0100280
281/**
Michal Vasko59892dd2022-05-13 11:02:30 +0200282 * @brief Create a list with all its keys (cannot be used for key-less list).
283 *
284 * Hash is calculated and new node flag is set.
285 *
286 * @param[in] schema Schema node of the new data node.
287 * @param[in] keys Key list predicates.
288 * @param[in] keys_len Length of @p keys.
289 * @param[out] node Created node.
290 * @return LY_SUCCESS on success.
291 * @return LY_ERR value if an error occurred.
292 */
293LY_ERR lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node);
294
295/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100296 * @brief Create an anyxml/anydata node.
297 *
298 * Hash is calculated and flags are properly set based on @p is_valid.
Michal Vasko90932a92020-02-12 14:33:03 +0100299 *
300 * @param[in] schema Schema node of the new data node.
Michal Vasko366a4a12020-12-04 16:23:57 +0100301 * @param[in] value Value of the any node.
Michal Vasko90932a92020-02-12 14:33:03 +0100302 * @param[in] value_type Value type of the value.
Michal Vasko742a5b12022-02-24 16:07:27 +0100303 * @param[in] use_value Whether to use dynamic @p value or duplicate it.
Michal Vasko90932a92020-02-12 14:33:03 +0100304 * @param[out] node Created node.
305 * @return LY_SUCCESS on success.
306 * @return LY_ERR value if an error occurred.
307 */
Michal Vasko9b368d32020-02-14 13:53:31 +0100308LY_ERR lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type,
Michal Vasko366a4a12020-12-04 16:23:57 +0100309 ly_bool use_value, struct lyd_node **node);
Michal Vasko90932a92020-02-12 14:33:03 +0100310
311/**
Michal Vasko52927e22020-03-16 17:26:14 +0100312 * @brief Create an opaque node.
313 *
314 * @param[in] ctx libyang context.
315 * @param[in] name Element name.
316 * @param[in] name_len Length of @p name, must be set correctly.
Michal Vasko52927e22020-03-16 17:26:14 +0100317 * @param[in] prefix Element prefix.
318 * @param[in] pref_len Length of @p prefix, must be set correctly.
Radek Krejci1798aae2020-07-14 13:26:06 +0200319 * @param[in] module_key Mandatory key to reference module, can be namespace or name.
320 * @param[in] module_key_len Length of @p module_key, must be set correctly.
Michal Vasko501af032020-11-11 20:27:44 +0100321 * @param[in] value String value to be parsed.
322 * @param[in] value_len Length of @p value, must be set correctly.
323 * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed.
324 * @param[in] format Input format of @p value and @p ns.
Radek Krejciec9ad602021-01-04 10:46:30 +0100325 * @param[in] val_prefix_data Format-specific prefix data, param is spent (even in case the function fails):
326 * LY_PREF_SCHEMA - const struct lysp_module * (module used for resolving prefixes from imports)
327 * LY_PREF_SCHEMA_RESOLVED - struct lyd_value_prefix * (sized array of pairs: prefix - module)
328 * LY_PREF_XML - const struct ly_set * (set with defined namespaces stored as ::lyxml_ns)
329 * LY_PREF_JSON - NULL
Michal Vasko501af032020-11-11 20:27:44 +0100330 * @param[in] hints [Hints](@ref lydhints) from the parser regarding the node/value type.
Michal Vasko52927e22020-03-16 17:26:14 +0100331 * @param[out] node Created node.
332 * @return LY_SUCCESS on success.
333 * @return LY_ERR value if an error occurred.
334 */
Michal Vasko501af032020-11-11 20:27:44 +0100335LY_ERR lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t pref_len,
336 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 +0200337 LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints, struct lyd_node **node);
Michal Vasko52927e22020-03-16 17:26:14 +0100338
339/**
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200340 * @brief Check the existence and create any non-existing implicit siblings, recursively for the created nodes.
341 *
342 * @param[in] parent Parent of the potential default values, NULL for top-level siblings.
343 * @param[in,out] first First sibling.
344 * @param[in] sparent Schema parent of the siblings, NULL if schema of @p parent can be used.
345 * @param[in] mod Module of the default values, NULL for nested siblings.
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200346 * @param[in] node_when Optional set to add nodes with "when" conditions into.
Michal Vaskoc43c8ab2021-03-05 13:32:44 +0100347 * @param[in] node_types Optional set to add nodes with unresolved types into.
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200348 * @param[in] impl_opts Implicit options (@ref implicitoptions).
349 * @param[in,out] diff Validation diff.
350 * @return LY_ERR value.
351 */
352LY_ERR lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Michal Vaskoddd76592022-01-17 13:34:48 +0100353 const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_types, uint32_t impl_opts,
354 struct lyd_node **diff);
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200355
356/**
Michal Vaskob104f112020-07-17 09:54:54 +0200357 * @brief Find the next node, before which to insert the new node.
Michal Vasko90932a92020-02-12 14:33:03 +0100358 *
Michal Vaskob104f112020-07-17 09:54:54 +0200359 * @param[in] first_sibling First sibling of the nodes to consider.
360 * @param[in] new_node Node that will be inserted.
361 * @return Node to insert after.
362 * @return NULL if the new node should be first.
Michal Vasko90932a92020-02-12 14:33:03 +0100363 */
Michal Vaskob104f112020-07-17 09:54:54 +0200364struct 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 +0100365
366/**
Michal Vaskoc61dd062022-06-07 11:01:28 +0200367 * @brief Insert node after a sibling.
368 *
369 * Handles inserting into NP containers and key-less lists.
370 *
371 * @param[in] sibling Sibling to insert after.
372 * @param[in] node Node to insert.
373 */
374void lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node);
375
376/**
377 * @brief Insert node before a sibling.
378 *
379 * Handles inserting into NP containers and key-less lists.
380 *
381 * @param[in] sibling Sibling to insert before.
382 * @param[in] node Node to insert.
383 */
384void lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node);
385
386/**
Michal Vaskob104f112020-07-17 09:54:54 +0200387 * @brief Insert a node into parent/siblings. Order and hashes are fully handled.
Michal Vasko90932a92020-02-12 14:33:03 +0100388 *
Michal Vasko9b368d32020-02-14 13:53:31 +0100389 * @param[in] parent Parent to insert into, NULL for top-level sibling.
390 * @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 +0100391 * @param[in] node Individual node (without siblings) to insert.
Michal Vasko6ee6f432021-07-16 09:49:14 +0200392 * @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 +0100393 */
Michal Vasko6ee6f432021-07-16 09:49:14 +0200394void 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 +0100395
396/**
Michal Vaskoa5da3292020-08-12 13:10:50 +0200397 * @brief Insert a metadata (last) into a parent
398 *
399 * @param[in] parent Parent of the metadata.
400 * @param[in] meta Metadata (list) to be added into the @p parent.
Michal Vasko871a0252020-11-11 18:35:24 +0100401 * @param[in] clear_dflt Whether to clear dflt flag starting from @p parent, recursively all NP containers.
Michal Vaskoa5da3292020-08-12 13:10:50 +0200402 */
Michal Vasko871a0252020-11-11 18:35:24 +0100403void lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200404
405/**
Michal Vasko52927e22020-03-16 17:26:14 +0100406 * @brief Create and insert a metadata (last) into a parent.
Michal Vasko90932a92020-02-12 14:33:03 +0100407 *
Michal Vasko52927e22020-03-16 17:26:14 +0100408 * @param[in] parent Parent of the metadata, can be NULL.
Michal Vasko9f96a052020-03-10 09:41:45 +0100409 * @param[in,out] meta Metadata list to add at its end if @p parent is NULL, returned created attribute.
410 * @param[in] mod Metadata module (with the annotation definition).
Michal Vasko90932a92020-02-12 14:33:03 +0100411 * @param[in] name Attribute name.
Michal Vaskof03ed032020-03-04 13:31:44 +0100412 * @param[in] name_len Length of @p name, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100413 * @param[in] value String value to be parsed.
Michal Vaskof03ed032020-03-04 13:31:44 +0100414 * @param[in] value_len Length of @p value, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100415 * @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 +0100416 * @param[in] format Input format of @p value.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200417 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200418 * @param[in] hints [Value hints](@ref lydvalhints) from the parser regarding the value type.
Michal Vaskoddd76592022-01-17 13:34:48 +0100419 * @param[in] ctx_node Value context node, may be NULL for metadata.
Michal Vasko871a0252020-11-11 18:35:24 +0100420 * @param[in] clear_dflt Whether to clear dflt flag starting from @p parent, recursively all NP containers.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200421 * @param[out] incomplete Whether the value needs to be resolved.
Michal Vasko90932a92020-02-12 14:33:03 +0100422 * @return LY_SUCCESS on success.
423 * @return LY_EINCOMPLETE in case data tree is needed to finish the validation.
424 * @return LY_ERR value if an error occurred.
425 */
Michal Vasko9f96a052020-03-10 09:41:45 +0100426LY_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 +0200427 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 +0100428 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 +0200429
430/**
Michal Vaskoa5da3292020-08-12 13:10:50 +0200431 * @brief Insert an attribute (last) into a parent
Radek Krejci1798aae2020-07-14 13:26:06 +0200432 *
Michal Vaskoa5da3292020-08-12 13:10:50 +0200433 * @param[in] parent Parent of the attributes.
434 * @param[in] attr Attribute (list) to be added into the @p parent.
Radek Krejci1798aae2020-07-14 13:26:06 +0200435 */
Michal Vaskoa5da3292020-08-12 13:10:50 +0200436void lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr);
Michal Vasko90932a92020-02-12 14:33:03 +0100437
438/**
Michal Vasko52927e22020-03-16 17:26:14 +0100439 * @brief Create and insert a generic attribute (last) into a parent.
440 *
441 * @param[in] parent Parent of the attribute, can be NULL.
442 * @param[in,out] attr Attribute list to add at its end if @p parent is NULL, returned created attribute.
443 * @param[in] ctx libyang context.
444 * @param[in] name Attribute name.
445 * @param[in] name_len Length of @p name, must be set correctly.
Michal Vasko52927e22020-03-16 17:26:14 +0100446 * @param[in] prefix Attribute prefix.
447 * @param[in] prefix_len Attribute prefix length.
Radek Krejci1798aae2020-07-14 13:26:06 +0200448 * @param[in] module_key Mandatory key to reference module, can be namespace or name.
449 * @param[in] module_key_len Length of @p module_key, must be set correctly.
Michal Vasko501af032020-11-11 20:27:44 +0100450 * @param[in] value String value to be parsed.
451 * @param[in] value_len Length of @p value, must be set correctly.
452 * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed.
453 * @param[in] format Input format of @p value and @p ns.
454 * @param[in] val_prefix_data Format-specific prefix data, param is spent (even in case the function fails).
455 * @param[in] hints [Hints](@ref lydhints) from the parser regarding the node/value type.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200456 * @return LY_SUCCESS on success,
457 * @return LY_ERR value on error.
Michal Vasko52927e22020-03-16 17:26:14 +0100458 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200459LY_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 +0100460 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 +0200461 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 +0200462
463/**
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200464 * @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 +0200465 *
Michal Vasko8d544252020-03-02 10:19:52 +0100466 * @param[in] ctx libyang context.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200467 * @param[in,out] val Storage for the value.
468 * @param[in] type Type of the value.
Radek Krejcif9943642021-04-26 10:18:21 +0200469 * @param[in] value Value to be parsed, must not be NULL.
Michal Vaskof03ed032020-03-04 13:31:44 +0100470 * @param[in] value_len Length of the give @p value, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100471 * @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 +0200472 * @param[in] format Input format of @p value.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200473 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200474 * @param[in] hints [Value hints](@ref lydvalhints) from the parser.
475 * @param[in] ctx_node Context schema node.
476 * @param[out] incomplete Optional, set if the value also needs to be resolved.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200477 * @return LY_SUCCESS on success,
478 * @return LY_ERR value on error.
Radek Krejci38d85362019-09-05 16:26:38 +0200479 */
Radek Krejcif9943642021-04-26 10:18:21 +0200480LY_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 +0200481 size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100482 const struct lysc_node *ctx_node, ly_bool *incomplete);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200483
484/**
485 * @brief Validate previously incompletely stored value.
486 *
487 * @param[in] ctx libyang context.
488 * @param[in] type Schema type of the value (not the stored one, but the original one).
489 * @param[in,out] val Stored value to resolve.
490 * @param[in] ctx_node Context node for the resolution.
491 * @param[in] tree Data tree for the resolution.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200492 * @return LY_SUCCESS on success,
493 * @return LY_ERR value on error.
494 */
495LY_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 +0100496 const struct lyd_node *ctx_node, const struct lyd_node *tree);
Radek Krejci38d85362019-09-05 16:26:38 +0200497
Michal Vaskoaebbce02021-04-06 09:23:37 +0200498/**
499 * @brief Check type restrictions applicable to the particular leaf/leaf-list with the given string @p value coming
500 * from a schema.
501 *
502 * This function check just the type's restriction, if you want to check also the data tree context (e.g. in case of
503 * require-instance restriction), use ::lyd_value_validate().
504 *
505 * @param[in] ctx libyang context for logging (function does not log errors when @p ctx is NULL)
506 * @param[in] node Schema node for the @p value.
507 * @param[in] value String value to be checked, expected to be in JSON format.
508 * @param[in] value_len Length of the given @p value (mandatory).
509 * @param[in] format Value prefix format.
510 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
511 * @return LY_SUCCESS on success
512 * @return LY_ERR value if an error occurred.
513 */
514LY_ERR lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
Radek Krejci8df109d2021-04-23 12:19:08 +0200515 LY_VALUE_FORMAT format, void *prefix_data);
Michal Vaskof937cfe2020-08-03 16:07:12 +0200516
Radek Krejci38d85362019-09-05 16:26:38 +0200517/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200518 * @defgroup datahash Data nodes hash manipulation
519 * @ingroup datatree
Michal Vasko8081a812021-07-15 09:19:43 +0200520 * @{
Radek Krejcie7b95092019-05-15 11:03:07 +0200521 */
522
523/**
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200524 * @brief Generate hash for the node.
525 *
526 * @param[in] node Data node to (re)generate hash value.
527 * @return LY_ERR value.
528 */
529LY_ERR lyd_hash(struct lyd_node *node);
530
531/**
532 * @brief Insert hash of the node into the hash table of its parent.
533 *
Radek Krejci8678fa42020-08-18 16:07:28 +0200534 * @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 +0200535 * @return LY_ERR value.
536 */
537LY_ERR lyd_insert_hash(struct lyd_node *node);
538
539/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200540 * @brief Maintain node's parent's children hash table when unlinking the node.
541 *
542 * When completely freeing data tree, it is expected to free the parent's children hash table first, at once.
543 *
544 * @param[in] node The data node being unlinked from its parent.
545 */
546void lyd_unlink_hash(struct lyd_node *node);
547
548/** @} datahash */
549
Radek Krejci084289f2019-07-09 17:35:30 +0200550/**
Michal Vaskod59035b2020-07-08 12:00:06 +0200551 * @brief Append all list key predicates to path.
552 *
553 * @param[in] node Node with keys to print.
554 * @param[in,out] buffer Buffer to print to.
555 * @param[in,out] buflen Current buffer length.
556 * @param[in,out] bufused Current number of characters used in @p buffer.
557 * @param[in] is_static Whether buffer is static or can be reallocated.
558 * @return LY_ERR
559 */
Radek Krejci857189e2020-09-01 13:26:36 +0200560LY_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 +0200561
Michal Vasko413af592021-12-13 11:50:51 +0100562/**
563 * @brief Remove an object on the specific set index keeping the order of the other objects.
564 *
565 * @param[in] set Set from which a node will be removed.
566 * @param[in] index Index of the object to remove in the \p set.
567 * @param[in] destructor Optional function to free the objects being removed.
568 * @return LY_ERR return value.
569 */
570LY_ERR ly_set_rm_index_ordered(struct ly_set *set, uint32_t index, void (*destructor)(void *obj));
571
Radek Krejcie7b95092019-05-15 11:03:07 +0200572#endif /* LY_TREE_DATA_INTERNAL_H_ */