blob: 3e6b1de6896518b0df03d21ea84b923043486286 [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 Vasko90932a92020-02-12 14:33:03 +0100142 * @return LY_SUCCESS on success.
143 * @return LY_EINCOMPLETE in case data tree is needed to finish the validation.
144 * @return LY_ERR value if an error occurred.
145 */
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100146LY_ERR lyd_create_attr(struct lyd_node *parent, struct lyd_attr **attr, const struct lys_module *mod, const char *name,
147 size_t name_len, const char *value, size_t value_len, int *dynamic,
148 ly_clb_resolve_prefix get_prefix, void *prefix_data, LYD_FORMAT format);
Michal Vasko90932a92020-02-12 14:33:03 +0100149
150/**
Radek Krejci5819f7c2019-05-31 14:53:29 +0200151 * @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 +0200152 *
Radek Krejci38d85362019-09-05 16:26:38 +0200153 * @param[in] node Data node for the @p value.
Radek Krejci084289f2019-07-09 17:35:30 +0200154 * @param[in] value String value to be parsed, must not be NULL.
Radek Krejci5819f7c2019-05-31 14:53:29 +0200155 * @param[in] value_len Length of the give @p value (mandatory).
Michal Vasko90932a92020-02-12 14:33:03 +0100156 * @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 +0200157 * @param[in] second Flag for the second call after returning LY_EINCOMPLETE
Radek Krejci084289f2019-07-09 17:35:30 +0200158 * @param[in] get_prefix Parser-specific getter to resolve prefixes used in the @p value string.
Radek Krejciaca74032019-06-04 08:53:06 +0200159 * @param[in] parser Parser's data for @p get_prefix
Michal Vasko90932a92020-02-12 14:33:03 +0100160 * @param[in] format Input format of @p value.
Radek Krejcie553e6d2019-06-07 15:33:18 +0200161 * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees (e.g. when validating RPC/Notification) where the required
162 * data instance (leafref target, instance-identifier) can be placed. NULL in case the data tree are not yet complete,
163 * then LY_EINCOMPLETE can be returned.
164 * @return LY_SUCCESS on success
165 * @return LY_EINCOMPLETE in case the @p trees is not provided and it was needed to finish the validation.
166 * @return LY_ERR value if an error occurred.
Radek Krejcie7b95092019-05-15 11:03:07 +0200167 */
Michal Vasko90932a92020-02-12 14:33:03 +0100168LY_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 +0200169 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node **trees);
Radek Krejcie7b95092019-05-15 11:03:07 +0200170
171/**
Radek Krejci38d85362019-09-05 16:26:38 +0200172 * @brief Validate, canonize and store the given @p value into the attribute according to the metadata annotation type's rules.
173 *
174 * @param[in] attr Data attribute for the @p value.
175 * @param[in] value String value to be parsed, must not be NULL.
176 * @param[in] value_len Length of the give @p value (mandatory).
Michal Vasko90932a92020-02-12 14:33:03 +0100177 * @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 +0200178 * @param[in] second Flag for the second call after returning LY_EINCOMPLETE
179 * @param[in] get_prefix Parser-specific getter to resolve prefixes used in the @p value string.
180 * @param[in] parser Parser's data for @p get_prefix
181 * @param[in] format Input format of the data.
182 * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees (e.g. when validating RPC/Notification) where the required
183 * data instance (leafref target, instance-identifier) can be placed. NULL in case the data tree are not yet complete,
184 * then LY_EINCOMPLETE can be returned.
185 * @return LY_SUCCESS on success
186 * @return LY_EINCOMPLETE in case the @p trees is not provided and it was needed to finish the validation.
187 * @return LY_ERR value if an error occurred.
188 */
Michal Vasko90932a92020-02-12 14:33:03 +0100189LY_ERR lyd_value_parse_attr(struct lyd_attr *attr, const char *value, size_t value_len, int *dynamic, int second,
Radek Krejci38d85362019-09-05 16:26:38 +0200190 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node **trees);
191
192/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200193 * @brief Parse XML string as YANG data tree.
194 *
195 * @param[in] ctx libyang context
196 * @param[in] data Pointer to the XML string representation of the YANG data to parse.
197 * @param[in] options @ref dataparseroptions
198 * @param[out] result Resulting list of the parsed data trees. Note that NULL can be a valid result.
199 * @reutn LY_ERR value.
200 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100201LY_ERR lyd_parse_xml(struct ly_ctx *ctx, const char *data, int options, struct lyd_node **result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200202
203/**
204 * @defgroup datahash Data nodes hash manipulation
205 * @ingroup datatree
206 */
207
208/**
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200209 * @brief Generate hash for the node.
210 *
211 * @param[in] node Data node to (re)generate hash value.
212 * @return LY_ERR value.
213 */
214LY_ERR lyd_hash(struct lyd_node *node);
215
216/**
217 * @brief Insert hash of the node into the hash table of its parent.
218 *
219 * @param[in] node Data node which hash will be inserted into the lyd_node_inner::children_hash hash table of its parent.
220 * @return LY_ERR value.
221 */
222LY_ERR lyd_insert_hash(struct lyd_node *node);
223
224/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200225 * @brief Maintain node's parent's children hash table when unlinking the node.
226 *
227 * When completely freeing data tree, it is expected to free the parent's children hash table first, at once.
228 *
229 * @param[in] node The data node being unlinked from its parent.
230 */
231void lyd_unlink_hash(struct lyd_node *node);
232
233/** @} datahash */
234
Radek Krejci084289f2019-07-09 17:35:30 +0200235/**
236 * @brief Free path (target) structure of the lyd_value.
237 *
238 * @param[in] ctx libyang context.
239 * @param[in] path The structure ([sized array](@ref sizedarrays)) to free.
240 */
241void lyd_value_free_path(struct ly_ctx *ctx, struct lyd_value_path *path);
242
Michal Vasko9b368d32020-02-14 13:53:31 +0100243/**
244 * @brief Find the node, in the list, satisfying the given restrictions.
245 * Does **not** use hashes - should not be used unless necessary for best performance.
246 *
247 * @param[in] first Starting sibling node for search, only succeeding ones are searched.
248 * @param[in] schema Schema node of the data node to find.
249 * @param[in] key_or_value Expected value depends on the type of @p schema:
250 * LYS_CONTAINER:
251 * LYS_ANYXML:
252 * LYS_ANYDATA:
253 * LYS_NOTIF:
254 * LYS_RPC:
255 * LYS_ACTION:
256 * NULL should be always set, will be ignored.
257 * LYS_LEAF:
258 * LYS_LEAFLIST:
259 * Optional restriction on the specific leaf(-list) value.
260 * LYS_LIST:
261 * Optional keys values of the matching list instances in the form of "[key1='val1'][key2='val2']...".
262 * The keys do not have to be ordered and not all keys need to be specified.
263 *
264 * Note that any explicit values (leaf, leaf-list or list key values) will be canonized first
265 * before comparison. But values that do not have a canonical value are expected to be in the
266 * JSON format!
267 * @param[in] val_len Optional length of the @p key_or_value argument in case it is not NULL-terminated string.
268 * @param[out] match Can be NULL, otherwise the found data node.
269 * @return LY_SUCCESS on success, @p match set.
270 * @return LY_ENOTFOUND if not found, @p match set to NULL.
271 * @return LY_ERR value if another error occurred.
272 */
273LY_ERR lyd_find_sibling_next2(const struct lyd_node *first, const struct lysc_node *schema, const char *key_or_value,
274 size_t val_len, struct lyd_node **match);
275
276/**
277 * @brief Get the module, whose data this top-level node belongs to. Useful for augments, when the augmented
278 * module is the data owner. Handles top-level choice augments.
279 *
280 * @param[in] node Data node to examine.
281 * @return Module owner of the node.
282 */
283const struct lys_module *lyd_top_node_module(const struct lyd_node *node);
284
Radek Krejcie7b95092019-05-15 11:03:07 +0200285#endif /* LY_TREE_DATA_INTERNAL_H_ */