blob: d1f91b541b96caa4f6c1894b5c074451f63c8950 [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"
Michal Vasko60ea6352020-06-29 13:39:39 +020019#include "lyb.h"
Radek Krejciaca74032019-06-04 08:53:06 +020020#include "plugins_types.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020021#include "set.h"
22#include "tree_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020023
Michal Vasko52927e22020-03-16 17:26:14 +010024#include <stddef.h>
25
Michal Vasko004d3152020-06-11 19:59:22 +020026struct ly_path_predicate;
Michal Vaskoa6669ba2020-08-06 16:14:26 +020027struct lysc_module;
Michal Vasko004d3152020-06-11 19:59:22 +020028
Radek Krejcie7b95092019-05-15 11:03:07 +020029/**
Michal Vasko60ea6352020-06-29 13:39:39 +020030 * @brief Internal data parser flags.
31 */
32#define LYD_INTOPT_RPC 0x01 /**< RPC/action invocation is being parsed */
33#define LYD_INTOPT_NOTIF 0x02 /**< notification is being parsed */
34#define LYD_INTOPT_REPLY 0x04 /**< RPC/action reply is being parsed */
35
36/**
37 * @brief Hash schema sibling to be used for LYB data.
38 *
39 * @param[in] sibling Sibling to hash.
40 * @param[in] collision_id Collision ID of the hash to generate.
41 * @return Generated hash.
42 */
43LYB_HASH lyb_hash(struct lysc_node *sibling, uint8_t collision_id);
44
45/**
Radek Krejci857189e2020-09-01 13:26:36 +020046 * @brief Check whether a sibling's module is in a module array.
Michal Vasko60ea6352020-06-29 13:39:39 +020047 *
48 * @param[in] sibling Sibling to check.
49 * @param[in] models Modules in a sized array.
50 * @return non-zero if the module was found,
Radek Krejci857189e2020-09-01 13:26:36 +020051 * @return Boolean value whether @p sibling's module found in the given @p models array.
Michal Vasko60ea6352020-06-29 13:39:39 +020052 */
Radek Krejci857189e2020-09-01 13:26:36 +020053ly_bool lyb_has_schema_model(const struct lysc_node *sibling, const struct lys_module **models);
Michal Vasko60ea6352020-06-29 13:39:39 +020054
55/**
Michal Vaskob1b5c262020-03-05 14:29:47 +010056 * @brief Check whether a node to be deleted is the first top-level sibling.
57 *
58 * @param[in] first First sibling.
59 * @param[in] to_del Node to be deleted.
60 */
61#define LYD_DEL_IS_ROOT(first, to_del) (((first) == (to_del)) && !(first)->parent && !(first)->prev->next)
62
63/**
Radek Krejcie7b95092019-05-15 11:03:07 +020064 * @brief Get address of a node's child pointer if any.
65 *
Radek Krejcie7b95092019-05-15 11:03:07 +020066 * @param[in] node Node to check.
Michal Vasko9b368d32020-02-14 13:53:31 +010067 * @return Address of the node's child member,
68 * @return NULL if there is no child pointer.
Radek Krejcie7b95092019-05-15 11:03:07 +020069 */
70struct lyd_node **lyd_node_children_p(struct lyd_node *node);
71
72/**
Michal Vaskoa6669ba2020-08-06 16:14:26 +020073 * @brief Just like lys_getnext() but iterates over all data instances of the schema nodes.
74 *
75 * @param[in] last Last returned data node.
76 * @param[in] sibling Data node sibling to search in.
77 * @param[in,out] slast Schema last node, set to NULL for first call and do not change afterwards.
78 * May not be set if the function is used only for any suitable node existence check (such as the existence
79 * of any choice case data).
80 * @param[in] parent Schema parent of the iterated children nodes.
81 * @param[in] module Schema module of the iterated top-level nodes.
82 * @return Next matching data node,
83 * @return NULL if last data node was already returned.
84 */
85struct lyd_node *lys_getnext_data(const struct lyd_node *last, const struct lyd_node *sibling,
Radek Krejci0f969882020-08-21 16:56:47 +020086 const struct lysc_node **slast, const struct lysc_node *parent,
87 const struct lysc_module *module);
Michal Vaskoa6669ba2020-08-06 16:14:26 +020088
89/**
Michal Vasko9b368d32020-02-14 13:53:31 +010090 * @brief Create a term (leaf/leaf-list) node from a string value.
91 *
92 * Hash is calculated and new node flag is set.
Michal Vasko90932a92020-02-12 14:33:03 +010093 *
94 * @param[in] schema Schema node of the new data node.
95 * @param[in] value String value to be parsed.
Michal Vaskof03ed032020-03-04 13:31:44 +010096 * @param[in] value_len Length of @p value, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +010097 * @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 +010098 * @param[in] format Input format of @p value.
Michal Vaskoc8a230d2020-08-14 12:17:10 +020099 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200100 * @param[in] hints [Value hints](@ref lydvalhints) from the parser regarding the value type.
101 * @param[out] incomplete Whether the value needs to be resolved.
Michal Vasko90932a92020-02-12 14:33:03 +0100102 * @param[out] node Created node.
103 * @return LY_SUCCESS on success.
104 * @return LY_EINCOMPLETE in case data tree is needed to finish the validation.
105 * @return LY_ERR value if an error occurred.
106 */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200107LY_ERR lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, ly_bool *dynamic,
108 LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints, ly_bool *incomplete, struct lyd_node **node);
Michal Vasko90932a92020-02-12 14:33:03 +0100109
110/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100111 * @brief Create a term (leaf/leaf-list) node from a parsed value by duplicating it.
112 *
113 * Hash is calculated and new node flag is set.
114 *
115 * @param[in] schema Schema node of the new data node.
116 * @param[in] val Parsed value to use.
117 * @param[out] node Created node.
118 * @return LY_SUCCESS on success.
119 * @return LY_ERR value if an error occurred.
120 */
121LY_ERR lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node);
122
123/**
124 * @brief Create an inner (container/list/RPC/action/notification) node.
125 *
126 * Hash is calculated and new node flag is set except
127 * for list with keys, when the hash is not calculated!
128 * Also, non-presence container has its default flag set.
Michal Vasko90932a92020-02-12 14:33:03 +0100129 *
130 * @param[in] schema Schema node of the new data node.
131 * @param[out] node Created node.
132 * @return LY_SUCCESS on success.
133 * @return LY_ERR value if an error occurred.
134 */
135LY_ERR lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node);
136
137/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100138 * @brief Create a list with all its keys (cannot be used for key-less list).
139 *
140 * Hash is calculated and new node flag is set.
Michal Vasko90932a92020-02-12 14:33:03 +0100141 *
142 * @param[in] schema Schema node of the new data node.
Michal Vasko004d3152020-06-11 19:59:22 +0200143 * @param[in] predicates Compiled key list predicates.
Michal Vasko90932a92020-02-12 14:33:03 +0100144 * @param[out] node Created node.
145 * @return LY_SUCCESS on success.
146 * @return LY_ERR value if an error occurred.
147 */
Michal Vasko004d3152020-06-11 19:59:22 +0200148LY_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 +0100149
150/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100151 * @brief Create an anyxml/anydata node.
152 *
153 * Hash is calculated and flags are properly set based on @p is_valid.
Michal Vasko90932a92020-02-12 14:33:03 +0100154 *
155 * @param[in] schema Schema node of the new data node.
156 * @param[in] value Value of the any node, is directly assigned into the data node.
157 * @param[in] value_type Value type of the value.
158 * @param[out] node Created node.
159 * @return LY_SUCCESS on success.
160 * @return LY_ERR value if an error occurred.
161 */
Michal Vasko9b368d32020-02-14 13:53:31 +0100162LY_ERR lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type,
Radek Krejci0f969882020-08-21 16:56:47 +0200163 struct lyd_node **node);
Michal Vasko90932a92020-02-12 14:33:03 +0100164
165/**
Michal Vasko52927e22020-03-16 17:26:14 +0100166 * @brief Create an opaque node.
167 *
168 * @param[in] ctx libyang context.
169 * @param[in] name Element name.
170 * @param[in] name_len Length of @p name, must be set correctly.
171 * @param[in] value String value to be parsed.
172 * @param[in] value_len Length of @p value, must be set correctly.
173 * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed.
174 * @param[in] format Input format of @p value and @p ns.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200175 * @param[in] hints [Hints](@ref lydhints) from the parser regarding the node/value type.
Radek Krejcid46e46a2020-09-15 14:22:42 +0200176 * @param[in] val_prefs Possible value prefixes, array is spent (even in case the function fails).
Michal Vasko52927e22020-03-16 17:26:14 +0100177 * @param[in] prefix Element prefix.
178 * @param[in] pref_len Length of @p prefix, must be set correctly.
Radek Krejci1798aae2020-07-14 13:26:06 +0200179 * @param[in] module_key Mandatory key to reference module, can be namespace or name.
180 * @param[in] module_key_len Length of @p module_key, must be set correctly.
Michal Vasko52927e22020-03-16 17:26:14 +0100181 * @param[out] node Created node.
182 * @return LY_SUCCESS on success.
183 * @return LY_ERR value if an error occurred.
184 */
185LY_ERR lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *value, size_t value_len,
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200186 ly_bool *dynamic, LYD_FORMAT format, uint32_t hints, struct ly_prefix *val_prefs, const char *prefix,
Radek Krejci0f969882020-08-21 16:56:47 +0200187 size_t pref_len, const char *module_key, size_t module_key_len, struct lyd_node **node);
Michal Vasko52927e22020-03-16 17:26:14 +0100188
189/**
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200190 * @brief Check the existence and create any non-existing implicit siblings, recursively for the created nodes.
191 *
192 * @param[in] parent Parent of the potential default values, NULL for top-level siblings.
193 * @param[in,out] first First sibling.
194 * @param[in] sparent Schema parent of the siblings, NULL if schema of @p parent can be used.
195 * @param[in] mod Module of the default values, NULL for nested siblings.
196 * @param[in] node_types Optional set to add nodes with unresolved types into.
197 * @param[in] node_when Optional set to add nodes with "when" conditions into.
198 * @param[in] impl_opts Implicit options (@ref implicitoptions).
199 * @param[in,out] diff Validation diff.
200 * @return LY_ERR value.
201 */
202LY_ERR lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
Radek Krejci0f969882020-08-21 16:56:47 +0200203 const struct lys_module *mod, struct ly_set *node_types, struct ly_set *node_when,
Radek Krejci1deb5be2020-08-26 16:43:36 +0200204 uint32_t impl_opts, struct lyd_node **diff);
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200205
206/**
Michal Vaskob104f112020-07-17 09:54:54 +0200207 * @brief Find the next node, before which to insert the new node.
Michal Vasko90932a92020-02-12 14:33:03 +0100208 *
Michal Vaskob104f112020-07-17 09:54:54 +0200209 * @param[in] first_sibling First sibling of the nodes to consider.
210 * @param[in] new_node Node that will be inserted.
211 * @return Node to insert after.
212 * @return NULL if the new node should be first.
Michal Vasko90932a92020-02-12 14:33:03 +0100213 */
Michal Vaskob104f112020-07-17 09:54:54 +0200214struct 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 +0100215
216/**
Michal Vaskob104f112020-07-17 09:54:54 +0200217 * @brief Insert a node into parent/siblings. Order and hashes are fully handled.
Michal Vasko90932a92020-02-12 14:33:03 +0100218 *
Michal Vasko9b368d32020-02-14 13:53:31 +0100219 * @param[in] parent Parent to insert into, NULL for top-level sibling.
220 * @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 +0100221 * @param[in] node Individual node (without siblings) to insert.
222 */
Michal Vasko9b368d32020-02-14 13:53:31 +0100223void lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node);
Michal Vasko90932a92020-02-12 14:33:03 +0100224
225/**
Michal Vaskoa5da3292020-08-12 13:10:50 +0200226 * @brief Insert a metadata (last) into a parent
227 *
228 * @param[in] parent Parent of the metadata.
229 * @param[in] meta Metadata (list) to be added into the @p parent.
230 */
231void lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta);
232
233/**
Michal Vasko52927e22020-03-16 17:26:14 +0100234 * @brief Create and insert a metadata (last) into a parent.
Michal Vasko90932a92020-02-12 14:33:03 +0100235 *
Michal Vasko52927e22020-03-16 17:26:14 +0100236 * @param[in] parent Parent of the metadata, can be NULL.
Michal Vasko9f96a052020-03-10 09:41:45 +0100237 * @param[in,out] meta Metadata list to add at its end if @p parent is NULL, returned created attribute.
238 * @param[in] mod Metadata module (with the annotation definition).
Michal Vasko90932a92020-02-12 14:33:03 +0100239 * @param[in] name Attribute name.
Michal Vaskof03ed032020-03-04 13:31:44 +0100240 * @param[in] name_len Length of @p name, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100241 * @param[in] value String value to be parsed.
Michal Vaskof03ed032020-03-04 13:31:44 +0100242 * @param[in] value_len Length of @p value, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100243 * @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 +0100244 * @param[in] format Input format of @p value.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200245 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200246 * @param[in] hints [Value hints](@ref lydvalhints) from the parser regarding the value type.
247 * @param[out] incomplete Whether the value needs to be resolved.
Michal Vasko90932a92020-02-12 14:33:03 +0100248 * @return LY_SUCCESS on success.
249 * @return LY_EINCOMPLETE in case data tree is needed to finish the validation.
250 * @return LY_ERR value if an error occurred.
251 */
Michal Vasko9f96a052020-03-10 09:41:45 +0100252LY_ERR lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name,
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200253 size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format,
254 void *prefix_data, uint32_t hints, ly_bool *incomplete);
Radek Krejci1798aae2020-07-14 13:26:06 +0200255
256/**
Michal Vaskoa5da3292020-08-12 13:10:50 +0200257 * @brief Insert an attribute (last) into a parent
Radek Krejci1798aae2020-07-14 13:26:06 +0200258 *
Michal Vaskoa5da3292020-08-12 13:10:50 +0200259 * @param[in] parent Parent of the attributes.
260 * @param[in] attr Attribute (list) to be added into the @p parent.
Radek Krejci1798aae2020-07-14 13:26:06 +0200261 */
Michal Vaskoa5da3292020-08-12 13:10:50 +0200262void lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr);
Michal Vasko90932a92020-02-12 14:33:03 +0100263
264/**
Michal Vasko52927e22020-03-16 17:26:14 +0100265 * @brief Create and insert a generic attribute (last) into a parent.
266 *
267 * @param[in] parent Parent of the attribute, can be NULL.
268 * @param[in,out] attr Attribute list to add at its end if @p parent is NULL, returned created attribute.
269 * @param[in] ctx libyang context.
270 * @param[in] name Attribute name.
271 * @param[in] name_len Length of @p name, must be set correctly.
272 * @param[in] value String value to be parsed.
273 * @param[in] value_len Length of @p value, must be set correctly.
274 * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed.
275 * @param[in] format Input format of @p value and @p ns.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200276 * @param[in] hints [Hints](@ref lydhints) from the parser regarding the node/value type.
Radek Krejcid46e46a2020-09-15 14:22:42 +0200277 * @param[in] val_prefs Possible value prefixes, array is spent (even in case the function fails).
Michal Vasko52927e22020-03-16 17:26:14 +0100278 * @param[in] prefix Attribute prefix.
279 * @param[in] prefix_len Attribute prefix length.
Radek Krejci1798aae2020-07-14 13:26:06 +0200280 * @param[in] module_key Mandatory key to reference module, can be namespace or name.
281 * @param[in] module_key_len Length of @p module_key, must be set correctly.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200282 * @return LY_SUCCESS on success,
283 * @return LY_ERR value on error.
Michal Vasko52927e22020-03-16 17:26:14 +0100284 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200285LY_ERR lyd_create_attr(struct lyd_node *parent, struct lyd_attr **attr, const struct ly_ctx *ctx, const char *name,
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200286 size_t name_len, const char *value, size_t value_len, ly_bool *dynamic, LYD_FORMAT format, uint32_t hints,
Radek Krejci0f969882020-08-21 16:56:47 +0200287 struct ly_prefix *val_prefs, const char *prefix, size_t prefix_len, const char *module_key, size_t module_key_len);
Radek Krejci1798aae2020-07-14 13:26:06 +0200288
289/**
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200290 * @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 +0200291 *
Michal Vasko8d544252020-03-02 10:19:52 +0100292 * @param[in] ctx libyang context.
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200293 * @param[in,out] val Storage for the value.
294 * @param[in] type Type of the value.
Radek Krejci38d85362019-09-05 16:26:38 +0200295 * @param[in] value String value to be parsed, must not be NULL.
Michal Vaskof03ed032020-03-04 13:31:44 +0100296 * @param[in] value_len Length of the give @p value, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100297 * @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 +0200298 * @param[in] format Input format of @p value.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200299 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200300 * @param[in] hints [Value hints](@ref lydvalhints) from the parser.
301 * @param[in] ctx_node Context schema node.
302 * @param[out] incomplete Optional, set if the value also needs to be resolved.
303 * @param[in] log_elem_type Elem type for logging.
304 * @param[in] log_elem Elem for logging.
305 * @return LY_SUCCESS on success,
306 * @return LY_ERR value on error.
Radek Krejci38d85362019-09-05 16:26:38 +0200307 */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200308LY_ERR lyd_value_store(const struct ly_ctx *ctx, struct lyd_value *val, const struct lysc_type *type, const char *value,
309 size_t value_len, ly_bool *dynamic, LY_PREFIX_FORMAT format, void *prefix_data, uint32_t hints,
310 const struct lysc_node *ctx_node, ly_bool *incomplete, enum LY_VLOG_ELEM log_elem_type, const void *log_elem);
311
312/**
313 * @brief Validate previously incompletely stored value.
314 *
315 * @param[in] ctx libyang context.
316 * @param[in] type Schema type of the value (not the stored one, but the original one).
317 * @param[in,out] val Stored value to resolve.
318 * @param[in] ctx_node Context node for the resolution.
319 * @param[in] tree Data tree for the resolution.
320 * @param[in] log_elem_type Elem type for logging.
321 * @param[in] log_elem Elem for logging.
322 * @return LY_SUCCESS on success,
323 * @return LY_ERR value on error.
324 */
325LY_ERR lyd_value_validate_incomplete(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *val,
326 const struct lyd_node *ctx_node, const struct lyd_node *tree, enum LY_VLOG_ELEM log_elem_type, const void *log_elem);
Radek Krejci38d85362019-09-05 16:26:38 +0200327
Michal Vaskof937cfe2020-08-03 16:07:12 +0200328/* generic function lys_value_validate */
329LY_ERR _lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200330 LY_PREFIX_FORMAT format, void *prefix_data);
Michal Vaskof937cfe2020-08-03 16:07:12 +0200331
Radek Krejci38d85362019-09-05 16:26:38 +0200332/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200333 * @brief Parse XML string as YANG data tree.
334 *
335 * @param[in] ctx libyang context
Michal Vasko63f3d842020-07-08 10:10:14 +0200336 * @param[in] in Input structure.
Radek Krejci7931b192020-06-25 17:05:03 +0200337 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
338 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
Radek Krejci1798aae2020-07-14 13:26:06 +0200339 * @param[out] tree_p Parsed data tree. Note that NULL can be a valid result.
340 * @param[out] lydctx_p Data parser context to finish validation.
Michal Vasko9f96a052020-03-10 09:41:45 +0100341 * @return LY_ERR value.
Radek Krejcie7b95092019-05-15 11:03:07 +0200342 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200343LY_ERR lyd_parse_xml_data(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_options, uint32_t validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +0200344 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p);
Michal Vasko9f96a052020-03-10 09:41:45 +0100345
346/**
347 * @brief Parse XML string as YANG RPC/action invocation.
348 *
Radek Krejci7931b192020-06-25 17:05:03 +0200349 * Optional \<rpc\> envelope element is accepted if present. It is [checked](https://tools.ietf.org/html/rfc6241#section-4.1) and all
350 * its XML attributes are parsed. As a content of the enveloper, an RPC data or \<action\> envelope element is expected. The \<action\> envelope element is
351 * also [checked](https://tools.ietf.org/html/rfc7950#section-7.15.2) and then an action data is expected as a content of this envelope.
Michal Vasko9f96a052020-03-10 09:41:45 +0100352 *
353 * @param[in] ctx libyang context.
Michal Vasko63f3d842020-07-08 10:10:14 +0200354 * @param[in] in Input structure.
Radek Krejci1798aae2020-07-14 13:26:06 +0200355 * @param[out] tree_p Parsed full RPC/action tree.
356 * @param[out] op_p Optional pointer to the actual operation. Useful mainly for action.
Michal Vasko9f96a052020-03-10 09:41:45 +0100357 * @return LY_ERR value.
358 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200359LY_ERR lyd_parse_xml_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p);
Radek Krejcie7b95092019-05-15 11:03:07 +0200360
361/**
Michal Vaskoa8edff02020-03-27 14:47:01 +0100362 * @brief Parse XML string as YANG notification.
363 *
364 * Optional \<notification\> envelope element, if present, is [checked](https://tools.ietf.org/html/rfc5277#page-25)
365 * and parsed. Specifically, its namespace and the child \<eventTime\> element and its value.
366 *
367 * @param[in] ctx libyang context.
Michal Vasko63f3d842020-07-08 10:10:14 +0200368 * @param[in] in Input structure.
Radek Krejci1798aae2020-07-14 13:26:06 +0200369 * @param[out] tree_p Parsed full notification tree.
370 * @param[out] op_p Optional pointer to the actual notification. Useful mainly for nested notifications.
Michal Vaskoa8edff02020-03-27 14:47:01 +0100371 * @return LY_ERR value.
372 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200373LY_ERR lyd_parse_xml_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **ntf_p);
Michal Vaskoa8edff02020-03-27 14:47:01 +0100374
375/**
Michal Vasko1ce933a2020-03-30 12:38:22 +0200376 * @brief Parse XML string as YANG RPC/action reply.
377 *
378 * Optional \<rpc-reply\> envelope element, if present, is [checked](https://tools.ietf.org/html/rfc6241#section-4.2)
379 * and all its XML attributes parsed.
380 *
381 * @param[in] request Data tree of the RPC/action request.
Michal Vasko63f3d842020-07-08 10:10:14 +0200382 * @param[in] in Input structure.
Radek Krejci1798aae2020-07-14 13:26:06 +0200383 * @param[out] tree_p Parsed full reply tree. It always includes duplicated operation and parents of the @p request.
384 * @param[out] op_p Optional pointer to the reply operation. Useful mainly for action.
Michal Vasko1ce933a2020-03-30 12:38:22 +0200385 * @return LY_ERR value.
386 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200387LY_ERR lyd_parse_xml_reply(const struct lyd_node *request, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p);
388
389/**
390 * @brief Parse JSON string as YANG data tree.
391 *
392 * @param[in] ctx libyang context
393 * @param[in] in Input structure.
394 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
395 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
396 * @param[out] tree_p Parsed data tree. Note that NULL can be a valid result.
397 * @param[out] lydctx_p Data parser context to finish validation.
398 * @return LY_ERR value.
399 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200400LY_ERR lyd_parse_json_data(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_options, uint32_t validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +0200401 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p);
Radek Krejci1798aae2020-07-14 13:26:06 +0200402
403/**
404 * @brief Parse JSON string as YANG notification.
405 *
406 * Optional top-level "notification" envelope object, if present, is [checked](https://tools.ietf.org/html/rfc5277#page-25)
407 * and parsed. Specifically the child "eventTime" member and its value.
408 *
409 * @param[in] ctx libyang context.
410 * @param[in] in Input structure.
411 * @param[out] tree_p Parsed full notification tree.
412 * @param[out] ntf_p Optional pointer to the actual notification. Useful mainly for nested notifications.
413 * @return LY_ERR value.
414 */
415LY_ERR lyd_parse_json_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **ntf_p);
416
417/**
418 * @brief Parse JSON string as YANG RPC/action invocation.
419 *
420 * Optional top-level "rpc" envelope object, if present is is [checked](https://tools.ietf.org/html/rfc6241#section-4.1) and the parser
421 * goes inside for the content, which is an RPC data or "action" envelope objects. The "action" envelope object is
422 * also [checked](https://tools.ietf.org/html/rfc7950#section-7.15.2) and then an action data is expected as a content of this envelope.
423 *
424 * @param[in] ctx libyang context.
425 * @param[in] in Input structure.
426 * @param[out] tree_p Parsed full RPC/action tree.
427 * @param[out] op_p Optional pointer to the actual operation. Useful mainly for action.
428 * @return LY_ERR value.
429 */
430LY_ERR lyd_parse_json_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p);
431
432/**
433 * @brief Parse JSON string as YANG RPC/action reply.
434 *
435 * Optional "rpc-reply" envelope object, if present, is [checked](https://tools.ietf.org/html/rfc6241#section-4.2).
436 *
437 * @param[in] request Data tree of the RPC/action request.
438 * @param[in] in Input structure.
439 * @param[out] tree_p Parsed full reply tree. It always includes duplicated operation and parents of the @p request.
440 * @param[out] op_p Optional pointer to the reply operation. Useful mainly for action.
441 * @return LY_ERR value.
442 */
443LY_ERR lyd_parse_json_reply(const struct lyd_node *request, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p);
Michal Vasko1ce933a2020-03-30 12:38:22 +0200444
445/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200446 * @brief Parse binary data as YANG data tree.
447 *
448 * @param[in] ctx libyang context
Michal Vasko63f3d842020-07-08 10:10:14 +0200449 * @param[in] in Input structure.
Radek Krejci7931b192020-06-25 17:05:03 +0200450 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
451 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
Radek Krejci1798aae2020-07-14 13:26:06 +0200452 * @param[out] tree_p Parsed data tree. Note that NULL can be a valid result.
453 * @param[out] lydctx_p Data parser context to finish validation.
Michal Vasko60ea6352020-06-29 13:39:39 +0200454 * @return LY_ERR value.
455 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200456LY_ERR lyd_parse_lyb_data(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_options, uint32_t validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +0200457 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p);
Michal Vasko60ea6352020-06-29 13:39:39 +0200458
459/**
460 * @brief Parse binary data as YANG RPC/action invocation.
461 *
462 * @param[in] ctx libyang context.
Michal Vasko63f3d842020-07-08 10:10:14 +0200463 * @param[in] in Input structure.
Radek Krejci1798aae2020-07-14 13:26:06 +0200464 * @param[out] tree_p Parsed full RPC/action tree.
465 * @param[out] op_p Optional pointer to the actual operation. Useful mainly for action.
Michal Vasko60ea6352020-06-29 13:39:39 +0200466 * @return LY_ERR value.
467 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200468LY_ERR lyd_parse_lyb_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p);
Michal Vasko60ea6352020-06-29 13:39:39 +0200469
470/**
471 * @brief Parse binary data as YANG notification.
472 *
473 * @param[in] ctx libyang context.
Michal Vasko63f3d842020-07-08 10:10:14 +0200474 * @param[in] in Input structure.
Radek Krejci1798aae2020-07-14 13:26:06 +0200475 * @param[out] tree_p Parsed full notification tree.
476 * @param[out] ntf_p Optional pointer to the actual notification. Useful mainly for nested notifications.
Michal Vasko60ea6352020-06-29 13:39:39 +0200477 * @return LY_ERR value.
478 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200479LY_ERR lyd_parse_lyb_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **ntf_p);
Michal Vasko60ea6352020-06-29 13:39:39 +0200480
481/**
482 * @brief Parse binary data as YANG RPC/action reply.
483 *
484 * @param[in] request Data tree of the RPC/action request.
Michal Vasko63f3d842020-07-08 10:10:14 +0200485 * @param[in] in Input structure.
Radek Krejci1798aae2020-07-14 13:26:06 +0200486 * @param[out] tree_p Parsed full reply tree. It always includes duplicated operation and parents of the @p request.
487 * @param[out] op_p Optional pointer to the reply operation. Useful mainly for action.
Michal Vasko60ea6352020-06-29 13:39:39 +0200488 * @return LY_ERR value.
489 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200490LY_ERR lyd_parse_lyb_reply(const struct lyd_node *request, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p);
Michal Vasko60ea6352020-06-29 13:39:39 +0200491
492/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200493 * @defgroup datahash Data nodes hash manipulation
494 * @ingroup datatree
495 */
496
497/**
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200498 * @brief Generate hash for the node.
499 *
500 * @param[in] node Data node to (re)generate hash value.
501 * @return LY_ERR value.
502 */
503LY_ERR lyd_hash(struct lyd_node *node);
504
505/**
506 * @brief Insert hash of the node into the hash table of its parent.
507 *
508 * @param[in] node Data node which hash will be inserted into the lyd_node_inner::children_hash hash table of its parent.
509 * @return LY_ERR value.
510 */
511LY_ERR lyd_insert_hash(struct lyd_node *node);
512
513/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200514 * @brief Maintain node's parent's children hash table when unlinking the node.
515 *
516 * When completely freeing data tree, it is expected to free the parent's children hash table first, at once.
517 *
518 * @param[in] node The data node being unlinked from its parent.
519 */
520void lyd_unlink_hash(struct lyd_node *node);
521
522/** @} datahash */
523
Radek Krejci084289f2019-07-09 17:35:30 +0200524/**
Michal Vaskob1b5c262020-03-05 14:29:47 +0100525 * @brief Iterate over implemented modules for functions that accept specific modules or the whole context.
526 *
527 * @param[in] tree Data tree.
Michal Vasko26e80012020-07-08 10:55:46 +0200528 * @param[in] module Selected module, NULL for all.
Michal Vaskob1b5c262020-03-05 14:29:47 +0100529 * @param[in] ctx Context, NULL for selected modules.
530 * @param[in,out] i Iterator, set to 0 on first call.
531 * @param[out] first First sibling of the returned module.
532 * @return Next module.
533 * @return NULL if all modules were traversed.
534 */
Michal Vasko26e80012020-07-08 10:55:46 +0200535const struct lys_module *lyd_mod_next_module(struct lyd_node *tree, const struct lys_module *module,
Radek Krejci0f969882020-08-21 16:56:47 +0200536 const struct ly_ctx *ctx, uint32_t *i, struct lyd_node **first);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100537
538/**
539 * @brief Iterate over modules for functions that want to traverse all the top-level data.
540 *
541 * @param[in,out] next Pointer to the next module data, set to first top-level sibling on first call.
542 * @param[out] first First sibling of the returned module.
543 * @return Next module.
544 * @return NULL if all modules were traversed.
545 */
546const struct lys_module *lyd_data_next_module(struct lyd_node **next, struct lyd_node **first);
547
Michal Vasko9f96a052020-03-10 09:41:45 +0100548/**
549 * @brief Check that a list has all its keys.
550 *
551 * @param[in] node List to check.
Michal Vasko44685da2020-03-17 15:38:06 +0100552 * @return LY_SUCCESS on success.
553 * @return LY_ENOT on a missing key.
Michal Vasko9f96a052020-03-10 09:41:45 +0100554 */
555LY_ERR lyd_parse_check_keys(struct lyd_node *node);
556
Michal Vasko60ea6352020-06-29 13:39:39 +0200557/**
558 * @brief Set data flags for a newly parsed node.
559 *
560 * @param[in] node Node to use.
561 * @param[in] when_check Set of nodes with unresolved when.
562 * @param[in,out] meta Node metadata, may be removed from.
563 * @param[in] options Parse options.
564 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200565void lyd_parse_set_data_flags(struct lyd_node *node, struct ly_set *when_check, struct lyd_meta **meta, uint32_t options);
Michal Vasko60ea6352020-06-29 13:39:39 +0200566
567/**
568 * @brief Free value prefixes.
569 *
570 * @param[in] ctx libyang context.
571 * @param[in] val_prefis Value prefixes to free, sized array (@ref sizedarrays).
572 */
573void ly_free_val_prefs(const struct ly_ctx *ctx, struct ly_prefix *val_prefs);
574
Michal Vaskod59035b2020-07-08 12:00:06 +0200575/**
576 * @brief Append all list key predicates to path.
577 *
578 * @param[in] node Node with keys to print.
579 * @param[in,out] buffer Buffer to print to.
580 * @param[in,out] buflen Current buffer length.
581 * @param[in,out] bufused Current number of characters used in @p buffer.
582 * @param[in] is_static Whether buffer is static or can be reallocated.
583 * @return LY_ERR
584 */
Radek Krejci857189e2020-09-01 13:26:36 +0200585LY_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 +0200586
Radek Krejcie7b95092019-05-15 11:03:07 +0200587#endif /* LY_TREE_DATA_INTERNAL_H_ */