blob: 8825d0f8cec5905fd5763d5ed2756274a4046678 [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 *
6 * Copyright (c) 2015 - 2019 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#ifndef LY_TREE_DATA_INTERNAL_H_
16#define LY_TREE_DATA_INTERNAL_H_
17
18#include "tree_data.h"
Radek Krejciaca74032019-06-04 08:53:06 +020019#include "plugins_types.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020020
21/**
Michal Vaskob1b5c262020-03-05 14:29:47 +010022 * @brief Check whether a node to be deleted is the first top-level sibling.
23 *
24 * @param[in] first First sibling.
25 * @param[in] to_del Node to be deleted.
26 */
27#define LYD_DEL_IS_ROOT(first, to_del) (((first) == (to_del)) && !(first)->parent && !(first)->prev->next)
28
29/**
Radek Krejcie7b95092019-05-15 11:03:07 +020030 * @brief Get address of a node's child pointer if any.
31 *
Radek Krejcie7b95092019-05-15 11:03:07 +020032 * @param[in] node Node to check.
Michal Vasko9b368d32020-02-14 13:53:31 +010033 * @return Address of the node's child member,
34 * @return NULL if there is no child pointer.
Radek Krejcie7b95092019-05-15 11:03:07 +020035 */
36struct lyd_node **lyd_node_children_p(struct lyd_node *node);
37
38/**
Michal Vasko9b368d32020-02-14 13:53:31 +010039 * @brief Create a term (leaf/leaf-list) node from a string value.
40 *
41 * Hash is calculated and new node flag is set.
Michal Vasko90932a92020-02-12 14:33:03 +010042 *
43 * @param[in] schema Schema node of the new data node.
44 * @param[in] value String value to be parsed.
Michal Vaskof03ed032020-03-04 13:31:44 +010045 * @param[in] value_len Length of @p value, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +010046 * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed.
47 * @param[in] get_prefix Parser-specific getter to resolve prefixes used in the @p value string.
48 * @param[in] prefix_data User data for @p get_prefix.
49 * @param[in] format Input format of @p value.
50 * @param[out] node Created node.
51 * @return LY_SUCCESS on success.
52 * @return LY_EINCOMPLETE in case data tree is needed to finish the validation.
53 * @return LY_ERR value if an error occurred.
54 */
55LY_ERR lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
56 ly_clb_resolve_prefix get_prefix, void *prefix_data, LYD_FORMAT format, struct lyd_node **node);
57
58/**
Michal Vasko9b368d32020-02-14 13:53:31 +010059 * @brief Create a term (leaf/leaf-list) node from a parsed value by duplicating it.
60 *
61 * Hash is calculated and new node flag is set.
62 *
63 * @param[in] schema Schema node of the new data node.
64 * @param[in] val Parsed value to use.
65 * @param[out] node Created node.
66 * @return LY_SUCCESS on success.
67 * @return LY_ERR value if an error occurred.
68 */
69LY_ERR lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node);
70
71/**
72 * @brief Create an inner (container/list/RPC/action/notification) node.
73 *
74 * Hash is calculated and new node flag is set except
75 * for list with keys, when the hash is not calculated!
76 * Also, non-presence container has its default flag set.
Michal Vasko90932a92020-02-12 14:33:03 +010077 *
78 * @param[in] schema Schema node of the new data node.
79 * @param[out] node Created node.
80 * @return LY_SUCCESS on success.
81 * @return LY_ERR value if an error occurred.
82 */
83LY_ERR lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node);
84
85/**
Michal Vasko9b368d32020-02-14 13:53:31 +010086 * @brief Create a list with all its keys (cannot be used for key-less list).
87 *
88 * Hash is calculated and new node flag is set.
Michal Vasko90932a92020-02-12 14:33:03 +010089 *
90 * @param[in] schema Schema node of the new data node.
91 * @param[in] keys_str List instance key values in the form of "[key1='val1'][key2='val2']...".
92 * The keys do not have to be ordered but all of them must be set.
Michal Vaskof03ed032020-03-04 13:31:44 +010093 * @param[in] keys_len Length of @p keys_str, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +010094 * @param[out] node Created node.
95 * @return LY_SUCCESS on success.
96 * @return LY_ERR value if an error occurred.
97 */
98LY_ERR lyd_create_list(const struct lysc_node *schema, const char *keys_str, size_t keys_len, struct lyd_node **node);
99
100/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100101 * @brief Create an anyxml/anydata node.
102 *
103 * Hash is calculated and flags are properly set based on @p is_valid.
Michal Vasko90932a92020-02-12 14:33:03 +0100104 *
105 * @param[in] schema Schema node of the new data node.
106 * @param[in] value Value of the any node, is directly assigned into the data node.
107 * @param[in] value_type Value type of the value.
108 * @param[out] node Created node.
109 * @return LY_SUCCESS on success.
110 * @return LY_ERR value if an error occurred.
111 */
Michal Vasko9b368d32020-02-14 13:53:31 +0100112LY_ERR lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type,
113 struct lyd_node **node);
Michal Vasko90932a92020-02-12 14:33:03 +0100114
115/**
116 * @brief Find the key after which to insert the new key.
117 *
118 * @param[in] first_sibling List first sibling.
119 * @param[in] new_key Key that will be inserted.
120 * @return Key to insert after.
121 * @return NULL if the new key should be first.
122 */
123struct lyd_node *lyd_get_prev_key_anchor(const struct lyd_node *first_sibling, const struct lysc_node *new_key);
124
125/**
126 * @brief Insert a node into parent/siblings. In case a key is being inserted into a list, the correct position
Michal Vasko9b368d32020-02-14 13:53:31 +0100127 * is found and inserted into. Also, in case we are inserting into top-level siblings, insert it as
128 * the last sibling of all the module data siblings. Otherwise it is inserted at the very last place.
Michal Vasko90932a92020-02-12 14:33:03 +0100129 *
Michal Vasko9b368d32020-02-14 13:53:31 +0100130 * @param[in] parent Parent to insert into, NULL for top-level sibling.
131 * @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 +0100132 * @param[in] node Individual node (without siblings) to insert.
133 */
Michal Vasko9b368d32020-02-14 13:53:31 +0100134void lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node);
Michal Vasko90932a92020-02-12 14:33:03 +0100135
136/**
137 * @brief Create and insert an attribute (last) into a parent.
138 *
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100139 * @param[in] parent Parent of the attribute, can be NULL.
140 * @param[in,out] attr Attribute list to add at its end if @p parent is NULL, returned created attribute.
Michal Vasko90932a92020-02-12 14:33:03 +0100141 * @param[in] mod Attribute module (with the annotation definition).
142 * @param[in] name Attribute name.
Michal Vaskof03ed032020-03-04 13:31:44 +0100143 * @param[in] name_len Length of @p name, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100144 * @param[in] value String value to be parsed.
Michal Vaskof03ed032020-03-04 13:31:44 +0100145 * @param[in] value_len Length of @p value, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100146 * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed.
147 * @param[in] get_prefix Parser-specific getter to resolve prefixes used in the @p value string.
148 * @param[in] prefix_data User data for @p get_prefix.
149 * @param[in] format Input format of @p value.
Michal Vasko8d544252020-03-02 10:19:52 +0100150 * @param[in] ctx_snode Context node for value resolution in schema.
Michal Vasko90932a92020-02-12 14:33:03 +0100151 * @return LY_SUCCESS on success.
152 * @return LY_EINCOMPLETE in case data tree is needed to finish the validation.
153 * @return LY_ERR value if an error occurred.
154 */
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100155LY_ERR lyd_create_attr(struct lyd_node *parent, struct lyd_attr **attr, const struct lys_module *mod, const char *name,
Michal Vasko8d544252020-03-02 10:19:52 +0100156 size_t name_len, const char *value, size_t value_len, int *dynamic, ly_clb_resolve_prefix get_prefix,
157 void *prefix_data, LYD_FORMAT format, const struct lysc_node *ctx_snode);
Michal Vasko90932a92020-02-12 14:33:03 +0100158
159/**
Radek Krejci5819f7c2019-05-31 14:53:29 +0200160 * @brief Validate, canonize and store the given @p value into the node according to the node's type's rules.
Radek Krejcie7b95092019-05-15 11:03:07 +0200161 *
Radek Krejci38d85362019-09-05 16:26:38 +0200162 * @param[in] node Data node for the @p value.
Radek Krejci084289f2019-07-09 17:35:30 +0200163 * @param[in] value String value to be parsed, must not be NULL.
Michal Vaskof03ed032020-03-04 13:31:44 +0100164 * @param[in] value_len Length of the give @p value, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100165 * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed.
Radek Krejci3c9758d2019-07-11 16:49:10 +0200166 * @param[in] second Flag for the second call after returning LY_EINCOMPLETE
Radek Krejci084289f2019-07-09 17:35:30 +0200167 * @param[in] get_prefix Parser-specific getter to resolve prefixes used in the @p value string.
Radek Krejciaca74032019-06-04 08:53:06 +0200168 * @param[in] parser Parser's data for @p get_prefix
Michal Vasko90932a92020-02-12 14:33:03 +0100169 * @param[in] format Input format of @p value.
Michal Vaskof03ed032020-03-04 13:31:44 +0100170 * @param[in] tree Data tree (e.g. when validating RPC/Notification) where the required
Radek Krejcie553e6d2019-06-07 15:33:18 +0200171 * data instance (leafref target, instance-identifier) can be placed. NULL in case the data tree are not yet complete,
172 * then LY_EINCOMPLETE can be returned.
173 * @return LY_SUCCESS on success
174 * @return LY_EINCOMPLETE in case the @p trees is not provided and it was needed to finish the validation.
175 * @return LY_ERR value if an error occurred.
Radek Krejcie7b95092019-05-15 11:03:07 +0200176 */
Michal Vasko90932a92020-02-12 14:33:03 +0100177LY_ERR lyd_value_parse(struct lyd_node_term *node, const char *value, size_t value_len, int *dynamic, int second,
Michal Vaskof03ed032020-03-04 13:31:44 +0100178 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node *tree);
Radek Krejcie7b95092019-05-15 11:03:07 +0200179
180/**
Radek Krejci38d85362019-09-05 16:26:38 +0200181 * @brief Validate, canonize and store the given @p value into the attribute according to the metadata annotation type's rules.
182 *
Michal Vasko8d544252020-03-02 10:19:52 +0100183 * @param[in] ctx libyang context.
Radek Krejci38d85362019-09-05 16:26:38 +0200184 * @param[in] attr Data attribute for the @p value.
185 * @param[in] value String value to be parsed, must not be NULL.
Michal Vaskof03ed032020-03-04 13:31:44 +0100186 * @param[in] value_len Length of the give @p value, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100187 * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed.
Radek Krejci38d85362019-09-05 16:26:38 +0200188 * @param[in] second Flag for the second call after returning LY_EINCOMPLETE
189 * @param[in] get_prefix Parser-specific getter to resolve prefixes used in the @p value string.
190 * @param[in] parser Parser's data for @p get_prefix
191 * @param[in] format Input format of the data.
Michal Vasko8d544252020-03-02 10:19:52 +0100192 * @param[in] ctx_snode Context node for value resolution in schema.
Michal Vaskof03ed032020-03-04 13:31:44 +0100193 * @param[in] tree Data tree (e.g. when validating RPC/Notification) where the required
Radek Krejci38d85362019-09-05 16:26:38 +0200194 * data instance (leafref target, instance-identifier) can be placed. NULL in case the data tree are not yet complete,
195 * then LY_EINCOMPLETE can be returned.
196 * @return LY_SUCCESS on success
197 * @return LY_EINCOMPLETE in case the @p trees is not provided and it was needed to finish the validation.
198 * @return LY_ERR value if an error occurred.
199 */
Michal Vasko8d544252020-03-02 10:19:52 +0100200LY_ERR lyd_value_parse_attr(struct ly_ctx *ctx, struct lyd_attr *attr, const char *value, size_t value_len, int *dynamic,
201 int second, ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format,
Michal Vaskof03ed032020-03-04 13:31:44 +0100202 const struct lysc_node *ctx_snode, const struct lyd_node *tree);
Radek Krejci38d85362019-09-05 16:26:38 +0200203
204/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200205 * @brief Parse XML string as YANG data tree.
206 *
207 * @param[in] ctx libyang context
208 * @param[in] data Pointer to the XML string representation of the YANG data to parse.
209 * @param[in] options @ref dataparseroptions
Michal Vaskob1b5c262020-03-05 14:29:47 +0100210 * @param[out] tree Parsed data tree. Note that NULL can be a valid result.
Radek Krejcie7b95092019-05-15 11:03:07 +0200211 * @reutn LY_ERR value.
212 */
Michal Vaskob1b5c262020-03-05 14:29:47 +0100213LY_ERR lyd_parse_xml(struct ly_ctx *ctx, const char *data, int options, struct lyd_node **tree);
Radek Krejcie7b95092019-05-15 11:03:07 +0200214
215/**
216 * @defgroup datahash Data nodes hash manipulation
217 * @ingroup datatree
218 */
219
220/**
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200221 * @brief Generate hash for the node.
222 *
223 * @param[in] node Data node to (re)generate hash value.
224 * @return LY_ERR value.
225 */
226LY_ERR lyd_hash(struct lyd_node *node);
227
228/**
229 * @brief Insert hash of the node into the hash table of its parent.
230 *
231 * @param[in] node Data node which hash will be inserted into the lyd_node_inner::children_hash hash table of its parent.
232 * @return LY_ERR value.
233 */
234LY_ERR lyd_insert_hash(struct lyd_node *node);
235
236/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200237 * @brief Maintain node's parent's children hash table when unlinking the node.
238 *
239 * When completely freeing data tree, it is expected to free the parent's children hash table first, at once.
240 *
241 * @param[in] node The data node being unlinked from its parent.
242 */
243void lyd_unlink_hash(struct lyd_node *node);
244
245/** @} datahash */
246
Radek Krejci084289f2019-07-09 17:35:30 +0200247/**
248 * @brief Free path (target) structure of the lyd_value.
249 *
250 * @param[in] ctx libyang context.
251 * @param[in] path The structure ([sized array](@ref sizedarrays)) to free.
252 */
253void lyd_value_free_path(struct ly_ctx *ctx, struct lyd_value_path *path);
254
Michal Vasko9b368d32020-02-14 13:53:31 +0100255/**
256 * @brief Find the node, in the list, satisfying the given restrictions.
257 * Does **not** use hashes - should not be used unless necessary for best performance.
258 *
259 * @param[in] first Starting sibling node for search, only succeeding ones are searched.
260 * @param[in] schema Schema node of the data node to find.
261 * @param[in] key_or_value Expected value depends on the type of @p schema:
262 * LYS_CONTAINER:
263 * LYS_ANYXML:
264 * LYS_ANYDATA:
265 * LYS_NOTIF:
266 * LYS_RPC:
267 * LYS_ACTION:
268 * NULL should be always set, will be ignored.
269 * LYS_LEAF:
270 * LYS_LEAFLIST:
271 * Optional restriction on the specific leaf(-list) value.
272 * LYS_LIST:
273 * Optional keys values of the matching list instances in the form of "[key1='val1'][key2='val2']...".
274 * The keys do not have to be ordered and not all keys need to be specified.
275 *
276 * Note that any explicit values (leaf, leaf-list or list key values) will be canonized first
277 * before comparison. But values that do not have a canonical value are expected to be in the
278 * JSON format!
279 * @param[in] val_len Optional length of the @p key_or_value argument in case it is not NULL-terminated string.
280 * @param[out] match Can be NULL, otherwise the found data node.
281 * @return LY_SUCCESS on success, @p match set.
282 * @return LY_ENOTFOUND if not found, @p match set to NULL.
283 * @return LY_ERR value if another error occurred.
284 */
285LY_ERR lyd_find_sibling_next2(const struct lyd_node *first, const struct lysc_node *schema, const char *key_or_value,
286 size_t val_len, struct lyd_node **match);
287
288/**
289 * @brief Get the module, whose data this top-level node belongs to. Useful for augments, when the augmented
290 * module is the data owner. Handles top-level choice augments.
291 *
292 * @param[in] node Data node to examine.
293 * @return Module owner of the node.
294 */
295const struct lys_module *lyd_top_node_module(const struct lyd_node *node);
296
Michal Vaskob1b5c262020-03-05 14:29:47 +0100297/**
298 * @brief Iterate over implemented modules for functions that accept specific modules or the whole context.
299 *
300 * @param[in] tree Data tree.
301 * @param[in] modules Selected modules, NULL for all.
302 * @param[in] mod_count Count of @p modules.
303 * @param[in] ctx Context, NULL for selected modules.
304 * @param[in,out] i Iterator, set to 0 on first call.
305 * @param[out] first First sibling of the returned module.
306 * @return Next module.
307 * @return NULL if all modules were traversed.
308 */
309const struct lys_module *lyd_mod_next_module(struct lyd_node *tree, const struct lys_module **modules, int mod_count,
310 const struct ly_ctx *ctx, uint32_t *i, struct lyd_node **first);
311
312/**
313 * @brief Iterate over modules for functions that want to traverse all the top-level data.
314 *
315 * @param[in,out] next Pointer to the next module data, set to first top-level sibling on first call.
316 * @param[out] first First sibling of the returned module.
317 * @return Next module.
318 * @return NULL if all modules were traversed.
319 */
320const struct lys_module *lyd_data_next_module(struct lyd_node **next, struct lyd_node **first);
321
Radek Krejcie7b95092019-05-15 11:03:07 +0200322#endif /* LY_TREE_DATA_INTERNAL_H_ */