blob: 7cd5d34c8ed0a32db98bb07ad23aa6809d663933 [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/**
22 * @brief Get address of a node's child pointer if any.
23 *
Radek Krejcie7b95092019-05-15 11:03:07 +020024 * @param[in] node Node to check.
Michal Vasko9b368d32020-02-14 13:53:31 +010025 * @return Address of the node's child member,
26 * @return NULL if there is no child pointer.
Radek Krejcie7b95092019-05-15 11:03:07 +020027 */
28struct lyd_node **lyd_node_children_p(struct lyd_node *node);
29
30/**
Michal Vasko9b368d32020-02-14 13:53:31 +010031 * @brief Create a term (leaf/leaf-list) node from a string value.
32 *
33 * Hash is calculated and new node flag is set.
Michal Vasko90932a92020-02-12 14:33:03 +010034 *
35 * @param[in] schema Schema node of the new data node.
36 * @param[in] value String value to be parsed.
37 * @param[in] value_len Length of @p value.
38 * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed.
39 * @param[in] get_prefix Parser-specific getter to resolve prefixes used in the @p value string.
40 * @param[in] prefix_data User data for @p get_prefix.
41 * @param[in] format Input format of @p value.
42 * @param[out] node Created node.
43 * @return LY_SUCCESS on success.
44 * @return LY_EINCOMPLETE in case data tree is needed to finish the validation.
45 * @return LY_ERR value if an error occurred.
46 */
47LY_ERR lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
48 ly_clb_resolve_prefix get_prefix, void *prefix_data, LYD_FORMAT format, struct lyd_node **node);
49
50/**
Michal Vasko9b368d32020-02-14 13:53:31 +010051 * @brief Create a term (leaf/leaf-list) node from a parsed value by duplicating it.
52 *
53 * Hash is calculated and new node flag is set.
54 *
55 * @param[in] schema Schema node of the new data node.
56 * @param[in] val Parsed value to use.
57 * @param[out] node Created node.
58 * @return LY_SUCCESS on success.
59 * @return LY_ERR value if an error occurred.
60 */
61LY_ERR lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node);
62
63/**
64 * @brief Create an inner (container/list/RPC/action/notification) node.
65 *
66 * Hash is calculated and new node flag is set except
67 * for list with keys, when the hash is not calculated!
68 * Also, non-presence container has its default flag set.
Michal Vasko90932a92020-02-12 14:33:03 +010069 *
70 * @param[in] schema Schema node of the new data node.
71 * @param[out] node Created node.
72 * @return LY_SUCCESS on success.
73 * @return LY_ERR value if an error occurred.
74 */
75LY_ERR lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node);
76
77/**
Michal Vasko9b368d32020-02-14 13:53:31 +010078 * @brief Create a list with all its keys (cannot be used for key-less list).
79 *
80 * Hash is calculated and new node flag is set.
Michal Vasko90932a92020-02-12 14:33:03 +010081 *
82 * @param[in] schema Schema node of the new data node.
83 * @param[in] keys_str List instance key values in the form of "[key1='val1'][key2='val2']...".
84 * The keys do not have to be ordered but all of them must be set.
85 * @param[in] keys_len Length of @p keys_str.
86 * @param[out] node Created node.
87 * @return LY_SUCCESS on success.
88 * @return LY_ERR value if an error occurred.
89 */
90LY_ERR lyd_create_list(const struct lysc_node *schema, const char *keys_str, size_t keys_len, struct lyd_node **node);
91
92/**
Michal Vasko9b368d32020-02-14 13:53:31 +010093 * @brief Create an anyxml/anydata node.
94 *
95 * Hash is calculated and flags are properly set based on @p is_valid.
Michal Vasko90932a92020-02-12 14:33:03 +010096 *
97 * @param[in] schema Schema node of the new data node.
98 * @param[in] value Value of the any node, is directly assigned into the data node.
99 * @param[in] value_type Value type of the value.
100 * @param[out] node Created node.
101 * @return LY_SUCCESS on success.
102 * @return LY_ERR value if an error occurred.
103 */
Michal Vasko9b368d32020-02-14 13:53:31 +0100104LY_ERR lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type,
105 struct lyd_node **node);
Michal Vasko90932a92020-02-12 14:33:03 +0100106
107/**
108 * @brief Find the key after which to insert the new key.
109 *
110 * @param[in] first_sibling List first sibling.
111 * @param[in] new_key Key that will be inserted.
112 * @return Key to insert after.
113 * @return NULL if the new key should be first.
114 */
115struct lyd_node *lyd_get_prev_key_anchor(const struct lyd_node *first_sibling, const struct lysc_node *new_key);
116
117/**
118 * @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 +0100119 * is found and inserted into. Also, in case we are inserting into top-level siblings, insert it as
120 * 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 +0100121 *
Michal Vasko9b368d32020-02-14 13:53:31 +0100122 * @param[in] parent Parent to insert into, NULL for top-level sibling.
123 * @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 +0100124 * @param[in] node Individual node (without siblings) to insert.
125 */
Michal Vasko9b368d32020-02-14 13:53:31 +0100126void lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node);
Michal Vasko90932a92020-02-12 14:33:03 +0100127
128/**
129 * @brief Create and insert an attribute (last) into a parent.
130 *
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100131 * @param[in] parent Parent of the attribute, can be NULL.
132 * @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 +0100133 * @param[in] mod Attribute module (with the annotation definition).
134 * @param[in] name Attribute name.
135 * @param[in] name_len Length of @p name.
136 * @param[in] value String value to be parsed.
137 * @param[in] value_len Length of @p value.
138 * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed.
139 * @param[in] get_prefix Parser-specific getter to resolve prefixes used in the @p value string.
140 * @param[in] prefix_data User data for @p get_prefix.
141 * @param[in] format Input format of @p value.
Michal Vasko8d544252020-03-02 10:19:52 +0100142 * @param[in] ctx_snode Context node for value resolution in schema.
Michal Vasko90932a92020-02-12 14:33:03 +0100143 * @return LY_SUCCESS on success.
144 * @return LY_EINCOMPLETE in case data tree is needed to finish the validation.
145 * @return LY_ERR value if an error occurred.
146 */
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100147LY_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 +0100148 size_t name_len, const char *value, size_t value_len, int *dynamic, ly_clb_resolve_prefix get_prefix,
149 void *prefix_data, LYD_FORMAT format, const struct lysc_node *ctx_snode);
Michal Vasko90932a92020-02-12 14:33:03 +0100150
151/**
Radek Krejci5819f7c2019-05-31 14:53:29 +0200152 * @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 +0200153 *
Radek Krejci38d85362019-09-05 16:26:38 +0200154 * @param[in] node Data node for the @p value.
Radek Krejci084289f2019-07-09 17:35:30 +0200155 * @param[in] value String value to be parsed, must not be NULL.
Radek Krejci5819f7c2019-05-31 14:53:29 +0200156 * @param[in] value_len Length of the give @p value (mandatory).
Michal Vasko90932a92020-02-12 14:33:03 +0100157 * @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 +0200158 * @param[in] second Flag for the second call after returning LY_EINCOMPLETE
Radek Krejci084289f2019-07-09 17:35:30 +0200159 * @param[in] get_prefix Parser-specific getter to resolve prefixes used in the @p value string.
Radek Krejciaca74032019-06-04 08:53:06 +0200160 * @param[in] parser Parser's data for @p get_prefix
Michal Vasko90932a92020-02-12 14:33:03 +0100161 * @param[in] format Input format of @p value.
Radek Krejcie553e6d2019-06-07 15:33:18 +0200162 * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees (e.g. when validating RPC/Notification) where the required
163 * data instance (leafref target, instance-identifier) can be placed. NULL in case the data tree are not yet complete,
164 * then LY_EINCOMPLETE can be returned.
165 * @return LY_SUCCESS on success
166 * @return LY_EINCOMPLETE in case the @p trees is not provided and it was needed to finish the validation.
167 * @return LY_ERR value if an error occurred.
Radek Krejcie7b95092019-05-15 11:03:07 +0200168 */
Michal Vasko90932a92020-02-12 14:33:03 +0100169LY_ERR lyd_value_parse(struct lyd_node_term *node, const char *value, size_t value_len, int *dynamic, int second,
Radek Krejci576b23f2019-07-12 14:06:32 +0200170 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node **trees);
Radek Krejcie7b95092019-05-15 11:03:07 +0200171
172/**
Radek Krejci38d85362019-09-05 16:26:38 +0200173 * @brief Validate, canonize and store the given @p value into the attribute according to the metadata annotation type's rules.
174 *
Michal Vasko8d544252020-03-02 10:19:52 +0100175 * @param[in] ctx libyang context.
Radek Krejci38d85362019-09-05 16:26:38 +0200176 * @param[in] attr Data attribute for the @p value.
177 * @param[in] value String value to be parsed, must not be NULL.
178 * @param[in] value_len Length of the give @p value (mandatory).
Michal Vasko90932a92020-02-12 14:33:03 +0100179 * @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 +0200180 * @param[in] second Flag for the second call after returning LY_EINCOMPLETE
181 * @param[in] get_prefix Parser-specific getter to resolve prefixes used in the @p value string.
182 * @param[in] parser Parser's data for @p get_prefix
183 * @param[in] format Input format of the data.
Michal Vasko8d544252020-03-02 10:19:52 +0100184 * @param[in] ctx_snode Context node for value resolution in schema.
Radek Krejci38d85362019-09-05 16:26:38 +0200185 * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees (e.g. when validating RPC/Notification) where the required
186 * data instance (leafref target, instance-identifier) can be placed. NULL in case the data tree are not yet complete,
187 * then LY_EINCOMPLETE can be returned.
188 * @return LY_SUCCESS on success
189 * @return LY_EINCOMPLETE in case the @p trees is not provided and it was needed to finish the validation.
190 * @return LY_ERR value if an error occurred.
191 */
Michal Vasko8d544252020-03-02 10:19:52 +0100192LY_ERR lyd_value_parse_attr(struct ly_ctx *ctx, struct lyd_attr *attr, const char *value, size_t value_len, int *dynamic,
193 int second, ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format,
194 const struct lysc_node *ctx_snode, const struct lyd_node **trees);
Radek Krejci38d85362019-09-05 16:26:38 +0200195
196/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200197 * @brief Parse XML string as YANG data tree.
198 *
199 * @param[in] ctx libyang context
200 * @param[in] data Pointer to the XML string representation of the YANG data to parse.
201 * @param[in] options @ref dataparseroptions
202 * @param[out] result Resulting list of the parsed data trees. Note that NULL can be a valid result.
203 * @reutn LY_ERR value.
204 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100205LY_ERR lyd_parse_xml(struct ly_ctx *ctx, const char *data, int options, struct lyd_node **result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200206
207/**
208 * @defgroup datahash Data nodes hash manipulation
209 * @ingroup datatree
210 */
211
212/**
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200213 * @brief Generate hash for the node.
214 *
215 * @param[in] node Data node to (re)generate hash value.
216 * @return LY_ERR value.
217 */
218LY_ERR lyd_hash(struct lyd_node *node);
219
220/**
221 * @brief Insert hash of the node into the hash table of its parent.
222 *
223 * @param[in] node Data node which hash will be inserted into the lyd_node_inner::children_hash hash table of its parent.
224 * @return LY_ERR value.
225 */
226LY_ERR lyd_insert_hash(struct lyd_node *node);
227
228/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200229 * @brief Maintain node's parent's children hash table when unlinking the node.
230 *
231 * When completely freeing data tree, it is expected to free the parent's children hash table first, at once.
232 *
233 * @param[in] node The data node being unlinked from its parent.
234 */
235void lyd_unlink_hash(struct lyd_node *node);
236
237/** @} datahash */
238
Radek Krejci084289f2019-07-09 17:35:30 +0200239/**
240 * @brief Free path (target) structure of the lyd_value.
241 *
242 * @param[in] ctx libyang context.
243 * @param[in] path The structure ([sized array](@ref sizedarrays)) to free.
244 */
245void lyd_value_free_path(struct ly_ctx *ctx, struct lyd_value_path *path);
246
Michal Vasko9b368d32020-02-14 13:53:31 +0100247/**
248 * @brief Find the node, in the list, satisfying the given restrictions.
249 * Does **not** use hashes - should not be used unless necessary for best performance.
250 *
251 * @param[in] first Starting sibling node for search, only succeeding ones are searched.
252 * @param[in] schema Schema node of the data node to find.
253 * @param[in] key_or_value Expected value depends on the type of @p schema:
254 * LYS_CONTAINER:
255 * LYS_ANYXML:
256 * LYS_ANYDATA:
257 * LYS_NOTIF:
258 * LYS_RPC:
259 * LYS_ACTION:
260 * NULL should be always set, will be ignored.
261 * LYS_LEAF:
262 * LYS_LEAFLIST:
263 * Optional restriction on the specific leaf(-list) value.
264 * LYS_LIST:
265 * Optional keys values of the matching list instances in the form of "[key1='val1'][key2='val2']...".
266 * The keys do not have to be ordered and not all keys need to be specified.
267 *
268 * Note that any explicit values (leaf, leaf-list or list key values) will be canonized first
269 * before comparison. But values that do not have a canonical value are expected to be in the
270 * JSON format!
271 * @param[in] val_len Optional length of the @p key_or_value argument in case it is not NULL-terminated string.
272 * @param[out] match Can be NULL, otherwise the found data node.
273 * @return LY_SUCCESS on success, @p match set.
274 * @return LY_ENOTFOUND if not found, @p match set to NULL.
275 * @return LY_ERR value if another error occurred.
276 */
277LY_ERR lyd_find_sibling_next2(const struct lyd_node *first, const struct lysc_node *schema, const char *key_or_value,
278 size_t val_len, struct lyd_node **match);
279
280/**
281 * @brief Get the module, whose data this top-level node belongs to. Useful for augments, when the augmented
282 * module is the data owner. Handles top-level choice augments.
283 *
284 * @param[in] node Data node to examine.
285 * @return Module owner of the node.
286 */
287const struct lys_module *lyd_top_node_module(const struct lyd_node *node);
288
Radek Krejcie7b95092019-05-15 11:03:07 +0200289#endif /* LY_TREE_DATA_INTERNAL_H_ */