blob: 373db412265cb3343be60a6ce136066dba44db6d [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 Vaskodbf3e652022-10-21 08:46:25 +02007 * Copyright (c) 2015 - 2022 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 Vaskod7c048c2021-05-18 16:12:55 +020037 * @brief Internal structure for remembering "used" instances of lists with duplicate instances allowed.
38 */
39struct lyd_dup_inst {
40 struct ly_set *inst_set;
41 uint32_t used;
42};
43
44/**
45 * @brief Update a found inst using a duplicate instance cache. Needs to be called for every "used"
46 * (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.
50 * @param[in,out] dup_inst_cache Duplicate instance cache.
51 * @return LY_ERR value.
52 */
53LY_ERR lyd_dup_inst_next(struct lyd_node **inst, const struct lyd_node *siblings,
54 struct lyd_dup_inst **dup_inst_cache);
55
56/**
57 * @brief Free duplicate instance cache.
58 *
59 * @param[in] dup_inst Duplicate instance cache to free.
60 */
61void lyd_dup_inst_free(struct lyd_dup_inst *dup_inst);
62
63/**
Radek Krejci8678fa42020-08-18 16:07:28 +020064 * @brief Just like ::lys_getnext() but iterates over all data instances of the schema nodes.
Michal Vaskoa6669ba2020-08-06 16:14:26 +020065 *
66 * @param[in] last Last returned data node.
67 * @param[in] sibling Data node sibling to search in.
68 * @param[in,out] slast Schema last node, set to NULL for first call and do not change afterwards.
69 * May not be set if the function is used only for any suitable node existence check (such as the existence
70 * of any choice case data).
71 * @param[in] parent Schema parent of the iterated children nodes.
72 * @param[in] module Schema module of the iterated top-level nodes.
73 * @return Next matching data node,
74 * @return NULL if last data node was already returned.
75 */
76struct lyd_node *lys_getnext_data(const struct lyd_node *last, const struct lyd_node *sibling,
Michal Vasko106f0862021-11-02 11:49:27 +010077 const struct lysc_node **slast, const struct lysc_node *parent, const struct lysc_module *module);
78
79/**
80 * @brief Get address of a node's child pointer if any.
81 *
82 * @param[in] node Node to check.
83 * @return Address of the node's child member,
84 * @return NULL if there is no child pointer.
85 */
86struct lyd_node **lyd_node_child_p(struct lyd_node *node);
87
88/**
89 * @brief Update node pointer to point to the first data node of a module, leave unchanged if there is none.
90 *
91 * @param[in,out] node Node pointer, may be updated.
92 * @param[in] mod Module whose data to search for.
93 */
94void lyd_first_module_sibling(struct lyd_node **node, const struct lys_module *mod);
95
96/**
97 * @brief Iterate over implemented modules for functions that accept specific modules or the whole context.
98 *
99 * @param[in] tree Data tree.
100 * @param[in] module Selected module, NULL for all.
101 * @param[in] ctx Context, NULL for selected modules.
102 * @param[in,out] i Iterator, set to 0 on first call.
103 * @param[out] first First sibling of the returned module.
104 * @return Next module.
105 * @return NULL if all modules were traversed.
106 */
107const struct lys_module *lyd_mod_next_module(struct lyd_node *tree, const struct lys_module *module,
108 const struct ly_ctx *ctx, uint32_t *i, struct lyd_node **first);
109
110/**
111 * @brief Iterate over modules for functions that want to traverse all the top-level data.
112 *
113 * @param[in,out] next Pointer to the next module data, set to first top-level sibling on first call.
114 * @param[out] first First sibling of the returned module.
115 * @return Next module.
116 * @return NULL if all modules were traversed.
117 */
118const struct lys_module *lyd_data_next_module(struct lyd_node **next, struct lyd_node **first);
119
120/**
Michal Vasko106f0862021-11-02 11:49:27 +0100121 * @brief Get schema node of a data node. Useful especially for opaque nodes.
122 *
123 * @param[in] node Data node to use.
124 * @return Schema node represented by data @p node, NULL if there is none.
125 */
126const struct lysc_node *lyd_node_schema(const struct lyd_node *node);
127
128/**
Michal Vasko59892dd2022-05-13 11:02:30 +0200129 * @brief Search in the given siblings (NOT recursively) for the first schema node data instance.
130 * Uses hashes - should be used whenever possible for best performance.
131 *
132 * @param[in] siblings Siblings to search in including preceding and succeeding nodes.
133 * @param[in] schema Target data node schema to find.
134 * @param[out] match Can be NULL, otherwise the found data node.
135 * @return LY_SUCCESS on success, @p match set.
136 * @return LY_ENOTFOUND if not found, @p match set to NULL.
137 * @return LY_ERR value if another error occurred.
138 */
139LY_ERR lyd_find_sibling_schema(const struct lyd_node *siblings, const struct lysc_node *schema, struct lyd_node **match);
140
141/**
Michal Vasko106f0862021-11-02 11:49:27 +0100142 * @brief Check whether a node to be deleted is the root node, move it if it is.
143 *
144 * @param[in] root Root sibling.
145 * @param[in] to_del Node to be deleted.
146 * @param[in] mod If set, it is expected @p tree should point to the first node of @p mod. Otherwise it will simply be
147 * the first top-level sibling.
148 */
149void lyd_del_move_root(struct lyd_node **root, const struct lyd_node *to_del, const struct lys_module *mod);
150
151/**
Michal Vasko8cc3f662022-03-29 11:25:51 +0200152 * @brief Try to get schema node for data with a parent based on an extension instance.
153 *
154 * @param[in] parent Parsed parent data node. Set if @p sparent is NULL.
155 * @param[in] sparent Schema parent node. Set if @p sparent is NULL.
156 * @param[in] prefix Element prefix, if any.
157 * @param[in] prefix_len Length of @p prefix.
158 * @param[in] format Format of @p prefix.
159 * @param[in] prefix_data Format-specific data.
160 * @param[in] name Element name.
161 * @param[in] name_len Length of @p name.
162 * @param[out] snode Found schema node, NULL if no suitable was found.
163 * @param[out] ext Extension instance that provided @p snode.
164 * @return LY_SUCCESS on success;
165 * @return LY_ENOT if no extension instance parsed the data;
166 * @return LY_ERR on error.
167 */
168LY_ERR ly_nested_ext_schema(const struct lyd_node *parent, const struct lysc_node *sparent, const char *prefix,
169 size_t prefix_len, LY_VALUE_FORMAT format, void *prefix_data, const char *name, size_t name_len,
170 const struct lysc_node **snode, struct lysc_ext_instance **ext);
171
172/**
Michal Vasko106f0862021-11-02 11:49:27 +0100173 * @brief Free stored prefix data.
174 *
175 * @param[in] format Format of the prefixes.
176 * @param[in] prefix_data Format-specific data to free:
177 * LY_PREF_SCHEMA - const struct lysp_module * (module used for resolving prefixes from imports)
178 * LY_PREF_SCHEMA_RESOLVED - struct lyd_value_prefix * (sized array of pairs: prefix - module)
179 * LY_PREF_XML - const struct ly_set * (set with defined namespaces stored as ::lyxml_ns)
180 * LY_PREF_JSON - NULL
181 */
182void ly_free_prefix_data(LY_VALUE_FORMAT format, void *prefix_data);
183
184/**
185 * @brief Duplicate prefix data.
186 *
187 * @param[in] ctx libyang context.
188 * @param[in] format Format of the prefixes in the value.
189 * @param[in] prefix_data Prefix data to duplicate.
190 * @param[out] prefix_data_p Duplicated prefix data.
191 * @return LY_ERR value.
192 */
193LY_ERR ly_dup_prefix_data(const struct ly_ctx *ctx, LY_VALUE_FORMAT format, const void *prefix_data, void **prefix_data_p);
194
195/**
196 * @brief Store used prefixes in a string.
197 *
198 * If @p prefix_data_p are non-NULL, they are treated as valid according to the @p format_p and new possible
199 * prefixes are simply added. This way it is possible to store prefix data for several strings together.
200 *
201 * @param[in] ctx libyang context.
202 * @param[in] value Value to be parsed.
203 * @param[in] value_len Length of the @p value.
204 * @param[in] format Format of the prefixes in the value.
205 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
206 * @param[in,out] format_p Resulting format of the prefixes.
207 * @param[in,out] prefix_data_p Resulting prefix data for the value in format @p format_p.
208 * @return LY_ERR value.
209 */
210LY_ERR ly_store_prefix_data(const struct ly_ctx *ctx, const void *value, size_t value_len, LY_VALUE_FORMAT format,
211 const void *prefix_data, LY_VALUE_FORMAT *format_p, void **prefix_data_p);
212
213/**
214 * @brief Get string name of the format.
215 *
216 * @param[in] format Format whose name to get.
217 * @return Format string name.
218 */
219const char *ly_format2str(LY_VALUE_FORMAT format);
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200220
221/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100222 * @brief Create a term (leaf/leaf-list) node from a string value.
223 *
224 * Hash is calculated and new node flag is set.
Michal Vasko90932a92020-02-12 14:33:03 +0100225 *
226 * @param[in] schema Schema node of the new data node.
227 * @param[in] value String value to be parsed.
Michal Vaskof03ed032020-03-04 13:31:44 +0100228 * @param[in] value_len Length of @p value, must be set correctly.
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 Vaskofeca4fb2020-10-05 08:58:40 +0200239LY_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 +0200240 LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, ly_bool *incomplete, struct lyd_node **node);
Michal Vasko90932a92020-02-12 14:33:03 +0100241
242/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100243 * @brief Create a term (leaf/leaf-list) node from a parsed value by duplicating it.
244 *
245 * Hash is calculated and new node flag is set.
246 *
247 * @param[in] schema Schema node of the new data node.
248 * @param[in] val Parsed value to use.
249 * @param[out] node Created node.
250 * @return LY_SUCCESS on success.
251 * @return LY_ERR value if an error occurred.
252 */
253LY_ERR lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node);
254
255/**
256 * @brief Create an inner (container/list/RPC/action/notification) node.
257 *
258 * Hash is calculated and new node flag is set except
259 * for list with keys, when the hash is not calculated!
260 * Also, non-presence container has its default flag set.
Michal Vasko90932a92020-02-12 14:33:03 +0100261 *
262 * @param[in] schema Schema node of the new data node.
263 * @param[out] node Created node.
264 * @return LY_SUCCESS on success.
265 * @return LY_ERR value if an error occurred.
266 */
267LY_ERR lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node);
268
269/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100270 * @brief Create a list with all its keys (cannot be used for key-less list).
271 *
272 * Hash is calculated and new node flag is set.
Michal Vasko90932a92020-02-12 14:33:03 +0100273 *
274 * @param[in] schema Schema node of the new data node.
Michal Vasko004d3152020-06-11 19:59:22 +0200275 * @param[in] predicates Compiled key list predicates.
Michal Vasko90932a92020-02-12 14:33:03 +0100276 * @param[out] node Created node.
277 * @return LY_SUCCESS on success.
278 * @return LY_ERR value if an error occurred.
279 */
Michal Vasko004d3152020-06-11 19:59:22 +0200280LY_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 +0100281
282/**
Michal Vasko59892dd2022-05-13 11:02:30 +0200283 * @brief Create a list with all its keys (cannot be used for key-less list).
284 *
285 * Hash is calculated and new node flag is set.
286 *
287 * @param[in] schema Schema node of the new data node.
288 * @param[in] keys Key list predicates.
289 * @param[in] keys_len Length of @p keys.
290 * @param[out] node Created node.
291 * @return LY_SUCCESS on success.
292 * @return LY_ERR value if an error occurred.
293 */
294LY_ERR lyd_create_list2(const struct lysc_node *schema, const char *keys, size_t keys_len, struct lyd_node **node);
295
296/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100297 * @brief Create an anyxml/anydata node.
298 *
299 * Hash is calculated and flags are properly set based on @p is_valid.
Michal Vasko90932a92020-02-12 14:33:03 +0100300 *
301 * @param[in] schema Schema node of the new data node.
Michal Vasko366a4a12020-12-04 16:23:57 +0100302 * @param[in] value Value of the any node.
Michal Vasko90932a92020-02-12 14:33:03 +0100303 * @param[in] value_type Value type of the value.
Michal Vasko742a5b12022-02-24 16:07:27 +0100304 * @param[in] use_value Whether to use dynamic @p value or duplicate it.
Michal Vasko90932a92020-02-12 14:33:03 +0100305 * @param[out] node Created node.
306 * @return LY_SUCCESS on success.
307 * @return LY_ERR value if an error occurred.
308 */
Michal Vasko9b368d32020-02-14 13:53:31 +0100309LY_ERR lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type,
Michal Vasko366a4a12020-12-04 16:23:57 +0100310 ly_bool use_value, struct lyd_node **node);
Michal Vasko90932a92020-02-12 14:33:03 +0100311
312/**
Michal Vasko52927e22020-03-16 17:26:14 +0100313 * @brief Create an opaque node.
314 *
315 * @param[in] ctx libyang context.
316 * @param[in] name Element name.
317 * @param[in] name_len Length of @p name, must be set correctly.
Michal Vasko52927e22020-03-16 17:26:14 +0100318 * @param[in] prefix Element prefix.
319 * @param[in] pref_len Length of @p prefix, must be set correctly.
Radek Krejci1798aae2020-07-14 13:26:06 +0200320 * @param[in] module_key Mandatory key to reference module, can be namespace or name.
321 * @param[in] module_key_len Length of @p module_key, must be set correctly.
Michal Vasko501af032020-11-11 20:27:44 +0100322 * @param[in] value String value to be parsed.
323 * @param[in] value_len Length of @p value, must be set correctly.
324 * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed.
325 * @param[in] format Input format of @p value and @p ns.
Radek Krejciec9ad602021-01-04 10:46:30 +0100326 * @param[in] val_prefix_data Format-specific prefix data, param is spent (even in case the function fails):
327 * LY_PREF_SCHEMA - const struct lysp_module * (module used for resolving prefixes from imports)
328 * LY_PREF_SCHEMA_RESOLVED - struct lyd_value_prefix * (sized array of pairs: prefix - module)
329 * LY_PREF_XML - const struct ly_set * (set with defined namespaces stored as ::lyxml_ns)
330 * LY_PREF_JSON - NULL
Michal Vasko501af032020-11-11 20:27:44 +0100331 * @param[in] hints [Hints](@ref lydhints) from the parser regarding the node/value type.
Michal Vasko52927e22020-03-16 17:26:14 +0100332 * @param[out] node Created node.
333 * @return LY_SUCCESS on success.
334 * @return LY_ERR value if an error occurred.
335 */
Michal Vasko501af032020-11-11 20:27:44 +0100336LY_ERR lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *prefix, size_t pref_len,
337 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 +0200338 LY_VALUE_FORMAT format, void *val_prefix_data, uint32_t hints, struct lyd_node **node);
Michal Vasko52927e22020-03-16 17:26:14 +0100339
340/**
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200341 * @brief Check the existence and create any non-existing implicit siblings, recursively for the created nodes.
342 *
343 * @param[in] parent Parent of the potential default values, NULL for top-level siblings.
344 * @param[in,out] first First sibling.
345 * @param[in] sparent Schema parent of the siblings, NULL if schema of @p parent can be used.
346 * @param[in] mod Module of the default values, NULL for nested siblings.
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200347 * @param[in] node_when Optional set to add nodes with "when" conditions into.
Michal Vaskoc43c8ab2021-03-05 13:32:44 +0100348 * @param[in] node_types Optional set to add nodes with unresolved types into.
Michal Vaskofcbd78f2022-08-26 08:34:15 +0200349 * @param[in] ext_node Optional set to add nodes with extension instance node callbacks into.
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200350 * @param[in] impl_opts Implicit options (@ref implicitoptions).
351 * @param[in,out] diff Validation diff.
352 * @return LY_ERR value.
353 */
354LY_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 +0200355 const struct lys_module *mod, struct ly_set *node_when, struct ly_set *node_types, struct ly_set *ext_node,
356 uint32_t impl_opts, struct lyd_node **diff);
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200357
358/**
Michal Vaskob104f112020-07-17 09:54:54 +0200359 * @brief Find the next node, before which to insert the new node.
Michal Vasko90932a92020-02-12 14:33:03 +0100360 *
Michal Vaskob104f112020-07-17 09:54:54 +0200361 * @param[in] first_sibling First sibling of the nodes to consider.
362 * @param[in] new_node Node that will be inserted.
363 * @return Node to insert after.
364 * @return NULL if the new node should be first.
Michal Vasko90932a92020-02-12 14:33:03 +0100365 */
Michal Vaskob104f112020-07-17 09:54:54 +0200366struct 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 +0100367
368/**
Michal Vaskoc61dd062022-06-07 11:01:28 +0200369 * @brief Insert node after a sibling.
370 *
371 * Handles inserting into NP containers and key-less lists.
372 *
373 * @param[in] sibling Sibling to insert after.
374 * @param[in] node Node to insert.
375 */
376void lyd_insert_after_node(struct lyd_node *sibling, struct lyd_node *node);
377
378/**
379 * @brief Insert node before a sibling.
380 *
381 * Handles inserting into NP containers and key-less lists.
382 *
383 * @param[in] sibling Sibling to insert before.
384 * @param[in] node Node to insert.
385 */
386void lyd_insert_before_node(struct lyd_node *sibling, struct lyd_node *node);
387
388/**
Michal Vaskob104f112020-07-17 09:54:54 +0200389 * @brief Insert a node into parent/siblings. Order and hashes are fully handled.
Michal Vasko90932a92020-02-12 14:33:03 +0100390 *
Michal Vasko9b368d32020-02-14 13:53:31 +0100391 * @param[in] parent Parent to insert into, NULL for top-level sibling.
392 * @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 +0100393 * @param[in] node Individual node (without siblings) to insert.
Michal Vasko6ee6f432021-07-16 09:49:14 +0200394 * @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 +0100395 */
Michal Vasko6ee6f432021-07-16 09:49:14 +0200396void 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 +0100397
398/**
Michal Vaskoa5da3292020-08-12 13:10:50 +0200399 * @brief Insert a metadata (last) into a parent
400 *
401 * @param[in] parent Parent of the metadata.
402 * @param[in] meta Metadata (list) to be added into the @p parent.
Michal Vasko871a0252020-11-11 18:35:24 +0100403 * @param[in] clear_dflt Whether to clear dflt flag starting from @p parent, recursively all NP containers.
Michal Vaskoa5da3292020-08-12 13:10:50 +0200404 */
Michal Vasko871a0252020-11-11 18:35:24 +0100405void lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta, ly_bool clear_dflt);
Michal Vaskoa5da3292020-08-12 13:10:50 +0200406
407/**
Michal Vasko52927e22020-03-16 17:26:14 +0100408 * @brief Create and insert a metadata (last) into a parent.
Michal Vasko90932a92020-02-12 14:33:03 +0100409 *
Michal Vasko52927e22020-03-16 17:26:14 +0100410 * @param[in] parent Parent of the metadata, can be NULL.
Michal Vasko9f96a052020-03-10 09:41:45 +0100411 * @param[in,out] meta Metadata list to add at its end if @p parent is NULL, returned created attribute.
412 * @param[in] mod Metadata module (with the annotation definition).
Michal Vasko90932a92020-02-12 14:33:03 +0100413 * @param[in] name Attribute name.
Michal Vaskof03ed032020-03-04 13:31:44 +0100414 * @param[in] name_len Length of @p name, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100415 * @param[in] value String value to be parsed.
Michal Vaskof03ed032020-03-04 13:31:44 +0100416 * @param[in] value_len Length of @p value, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100417 * @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 +0100418 * @param[in] format Input format of @p value.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200419 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200420 * @param[in] hints [Value hints](@ref lydvalhints) from the parser regarding the value type.
Michal Vaskoddd76592022-01-17 13:34:48 +0100421 * @param[in] ctx_node Value context node, may be NULL for metadata.
Michal Vasko871a0252020-11-11 18:35:24 +0100422 * @param[in] clear_dflt Whether to clear dflt flag starting from @p parent, recursively all NP containers.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200423 * @param[out] incomplete Whether the value needs to be resolved.
Michal Vasko90932a92020-02-12 14:33:03 +0100424 * @return LY_SUCCESS on success.
425 * @return LY_EINCOMPLETE in case data tree is needed to finish the validation.
426 * @return LY_ERR value if an error occurred.
427 */
Michal Vasko9f96a052020-03-10 09:41:45 +0100428LY_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 +0200429 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 +0100430 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 +0200431
432/**
Michal Vaskoa5da3292020-08-12 13:10:50 +0200433 * @brief Insert an attribute (last) into a parent
Radek Krejci1798aae2020-07-14 13:26:06 +0200434 *
Michal Vaskoa5da3292020-08-12 13:10:50 +0200435 * @param[in] parent Parent of the attributes.
436 * @param[in] attr Attribute (list) to be added into the @p parent.
Radek Krejci1798aae2020-07-14 13:26:06 +0200437 */
Michal Vaskoa5da3292020-08-12 13:10:50 +0200438void lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr);
Michal Vasko90932a92020-02-12 14:33:03 +0100439
440/**
Michal Vasko52927e22020-03-16 17:26:14 +0100441 * @brief Create and insert a generic attribute (last) into a parent.
442 *
443 * @param[in] parent Parent of the attribute, can be NULL.
444 * @param[in,out] attr Attribute list to add at its end if @p parent is NULL, returned created attribute.
445 * @param[in] ctx libyang context.
446 * @param[in] name Attribute name.
447 * @param[in] name_len Length of @p name, must be set correctly.
Michal Vasko52927e22020-03-16 17:26:14 +0100448 * @param[in] prefix Attribute prefix.
449 * @param[in] prefix_len Attribute prefix length.
Radek Krejci1798aae2020-07-14 13:26:06 +0200450 * @param[in] module_key Mandatory key to reference module, can be namespace or name.
451 * @param[in] module_key_len Length of @p module_key, must be set correctly.
Michal Vasko501af032020-11-11 20:27:44 +0100452 * @param[in] value String value to be parsed.
453 * @param[in] value_len Length of @p value, must be set correctly.
454 * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed.
455 * @param[in] format Input format of @p value and @p ns.
456 * @param[in] val_prefix_data Format-specific prefix data, param is spent (even in case the function fails).
457 * @param[in] hints [Hints](@ref lydhints) from the parser regarding the node/value type.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200458 * @return LY_SUCCESS on success,
459 * @return LY_ERR value on error.
Michal Vasko52927e22020-03-16 17:26:14 +0100460 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200461LY_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 +0100462 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 +0200463 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 +0200464
465/**
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200466 * @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 +0200467 *
Michal Vasko8d544252020-03-02 10:19:52 +0100468 * @param[in] ctx libyang context.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200469 * @param[in,out] val Storage for the value.
470 * @param[in] type Type of the value.
Radek Krejcif9943642021-04-26 10:18:21 +0200471 * @param[in] value Value to be parsed, must not be NULL.
Michal Vaskof03ed032020-03-04 13:31:44 +0100472 * @param[in] value_len Length of the give @p value, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100473 * @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 +0200474 * @param[in] format Input format of @p value.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200475 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200476 * @param[in] hints [Value hints](@ref lydvalhints) from the parser.
477 * @param[in] ctx_node Context schema node.
478 * @param[out] incomplete Optional, set if the value also needs to be resolved.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200479 * @return LY_SUCCESS on success,
480 * @return LY_ERR value on error.
Radek Krejci38d85362019-09-05 16:26:38 +0200481 */
Radek Krejcif9943642021-04-26 10:18:21 +0200482LY_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 +0200483 size_t value_len, ly_bool *dynamic, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints,
Radek Krejci2efc45b2020-12-22 16:25:44 +0100484 const struct lysc_node *ctx_node, ly_bool *incomplete);
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200485
486/**
487 * @brief Validate previously incompletely stored value.
488 *
489 * @param[in] ctx libyang context.
490 * @param[in] type Schema type of the value (not the stored one, but the original one).
491 * @param[in,out] val Stored value to resolve.
492 * @param[in] ctx_node Context node for the resolution.
493 * @param[in] tree Data tree for the resolution.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200494 * @return LY_SUCCESS on success,
495 * @return LY_ERR value on error.
496 */
497LY_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 +0100498 const struct lyd_node *ctx_node, const struct lyd_node *tree);
Radek Krejci38d85362019-09-05 16:26:38 +0200499
Michal Vaskoaebbce02021-04-06 09:23:37 +0200500/**
501 * @brief Check type restrictions applicable to the particular leaf/leaf-list with the given string @p value coming
502 * from a schema.
503 *
504 * This function check just the type's restriction, if you want to check also the data tree context (e.g. in case of
505 * require-instance restriction), use ::lyd_value_validate().
506 *
507 * @param[in] ctx libyang context for logging (function does not log errors when @p ctx is NULL)
508 * @param[in] node Schema node for the @p value.
509 * @param[in] value String value to be checked, expected to be in JSON format.
510 * @param[in] value_len Length of the given @p value (mandatory).
511 * @param[in] format Value prefix format.
512 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
513 * @return LY_SUCCESS on success
514 * @return LY_ERR value if an error occurred.
515 */
516LY_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 +0200517 LY_VALUE_FORMAT format, void *prefix_data);
Michal Vaskof937cfe2020-08-03 16:07:12 +0200518
Radek Krejci38d85362019-09-05 16:26:38 +0200519/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200520 * @defgroup datahash Data nodes hash manipulation
521 * @ingroup datatree
Michal Vasko8081a812021-07-15 09:19:43 +0200522 * @{
Radek Krejcie7b95092019-05-15 11:03:07 +0200523 */
524
525/**
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200526 * @brief Generate hash for the node.
527 *
528 * @param[in] node Data node to (re)generate hash value.
529 * @return LY_ERR value.
530 */
531LY_ERR lyd_hash(struct lyd_node *node);
532
533/**
534 * @brief Insert hash of the node into the hash table of its parent.
535 *
Radek Krejci8678fa42020-08-18 16:07:28 +0200536 * @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 +0200537 * @return LY_ERR value.
538 */
539LY_ERR lyd_insert_hash(struct lyd_node *node);
540
541/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200542 * @brief Maintain node's parent's children hash table when unlinking the node.
543 *
544 * When completely freeing data tree, it is expected to free the parent's children hash table first, at once.
545 *
546 * @param[in] node The data node being unlinked from its parent.
547 */
548void lyd_unlink_hash(struct lyd_node *node);
549
550/** @} datahash */
551
Radek Krejci084289f2019-07-09 17:35:30 +0200552/**
Michal Vaskod59035b2020-07-08 12:00:06 +0200553 * @brief Append all list key predicates to path.
554 *
555 * @param[in] node Node with keys to print.
556 * @param[in,out] buffer Buffer to print to.
557 * @param[in,out] buflen Current buffer length.
558 * @param[in,out] bufused Current number of characters used in @p buffer.
559 * @param[in] is_static Whether buffer is static or can be reallocated.
Michal Vaskodbf3e652022-10-21 08:46:25 +0200560 * @return LY_ERR value.
Michal Vaskod59035b2020-07-08 12:00:06 +0200561 */
Radek Krejci857189e2020-09-01 13:26:36 +0200562LY_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 +0200563
Michal Vasko413af592021-12-13 11:50:51 +0100564/**
Michal Vaskodbf3e652022-10-21 08:46:25 +0200565 * @brief Generate a path similar to ::lyd_path() except read the parents from a set.
566 *
567 * @param[in] dnodes Set with the data nodes, from parent to the last descendant.
568 * @param[in] pathtype Type of data path to generate.
569 * @return Generated data path.
570 */
571char *lyd_path_set(const struct ly_set *dnodes, LYD_PATH_TYPE pathtype);
572
573/**
Michal Vasko413af592021-12-13 11:50:51 +0100574 * @brief Remove an object on the specific set index keeping the order of the other objects.
575 *
576 * @param[in] set Set from which a node will be removed.
577 * @param[in] index Index of the object to remove in the \p set.
578 * @param[in] destructor Optional function to free the objects being removed.
Michal Vaskodbf3e652022-10-21 08:46:25 +0200579 * @return LY_ERR value.
Michal Vasko413af592021-12-13 11:50:51 +0100580 */
581LY_ERR ly_set_rm_index_ordered(struct ly_set *set, uint32_t index, void (*destructor)(void *obj));
582
Radek Krejcie7b95092019-05-15 11:03:07 +0200583#endif /* LY_TREE_DATA_INTERNAL_H_ */