blob: 7911fe0d663a13dc43a6bc0c06db6cb93ee08e8c [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
Michal Vasko60ea6352020-06-29 13:39:39 +020018#include "lyb.h"
Radek Krejciaca74032019-06-04 08:53:06 +020019#include "plugins_types.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020020#include "set.h"
21#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 Vaskoa6669ba2020-08-06 16:14:26 +020026struct lysc_module;
Michal Vasko004d3152020-06-11 19:59:22 +020027
Radek Krejcie7b95092019-05-15 11:03:07 +020028/**
Michal Vasko60ea6352020-06-29 13:39:39 +020029 * @brief Internal data parser flags.
30 */
31#define LYD_INTOPT_RPC 0x01 /**< RPC/action invocation is being parsed */
32#define LYD_INTOPT_NOTIF 0x02 /**< notification is being parsed */
33#define LYD_INTOPT_REPLY 0x04 /**< RPC/action reply is being parsed */
34
35/**
36 * @brief Hash schema sibling to be used for LYB data.
37 *
38 * @param[in] sibling Sibling to hash.
39 * @param[in] collision_id Collision ID of the hash to generate.
40 * @return Generated hash.
41 */
42LYB_HASH lyb_hash(struct lysc_node *sibling, uint8_t collision_id);
43
44/**
45 * @brief Check whether a sibling module is in a module array.
46 *
47 * @param[in] sibling Sibling to check.
48 * @param[in] models Modules in a sized array.
49 * @return non-zero if the module was found,
50 * @return 0 if not found.
51 */
52int lyb_has_schema_model(const struct lysc_node *sibling, const struct lys_module **models);
53
54/**
Michal Vaskob1b5c262020-03-05 14:29:47 +010055 * @brief Check whether a node to be deleted is the first top-level sibling.
56 *
57 * @param[in] first First sibling.
58 * @param[in] to_del Node to be deleted.
59 */
60#define LYD_DEL_IS_ROOT(first, to_del) (((first) == (to_del)) && !(first)->parent && !(first)->prev->next)
61
62/**
Radek Krejcie7b95092019-05-15 11:03:07 +020063 * @brief Get address of a node's child pointer if any.
64 *
Radek Krejcie7b95092019-05-15 11:03:07 +020065 * @param[in] node Node to check.
Michal Vasko9b368d32020-02-14 13:53:31 +010066 * @return Address of the node's child member,
67 * @return NULL if there is no child pointer.
Radek Krejcie7b95092019-05-15 11:03:07 +020068 */
69struct lyd_node **lyd_node_children_p(struct lyd_node *node);
70
71/**
Michal Vaskoa6669ba2020-08-06 16:14:26 +020072 * @brief Just like lys_getnext() but iterates over all data instances of the schema nodes.
73 *
74 * @param[in] last Last returned data node.
75 * @param[in] sibling Data node sibling to search in.
76 * @param[in,out] slast Schema last node, set to NULL for first call and do not change afterwards.
77 * May not be set if the function is used only for any suitable node existence check (such as the existence
78 * of any choice case data).
79 * @param[in] parent Schema parent of the iterated children nodes.
80 * @param[in] module Schema module of the iterated top-level nodes.
81 * @return Next matching data node,
82 * @return NULL if last data node was already returned.
83 */
84struct lyd_node *lys_getnext_data(const struct lyd_node *last, const struct lyd_node *sibling,
85 const struct lysc_node **slast, const struct lysc_node *parent,
86 const struct lysc_module *module);
87
88/**
Michal Vasko9b368d32020-02-14 13:53:31 +010089 * @brief Create a term (leaf/leaf-list) node from a string value.
90 *
91 * Hash is calculated and new node flag is set.
Michal Vasko90932a92020-02-12 14:33:03 +010092 *
93 * @param[in] schema Schema node of the new data node.
94 * @param[in] value String value to be parsed.
Michal Vaskof03ed032020-03-04 13:31:44 +010095 * @param[in] value_len Length of @p value, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +010096 * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed.
Radek Krejci1798aae2020-07-14 13:26:06 +020097 * @param[in] value_hint [Hint options](@ref lydvalueparseopts) from the parser regarding the value type.
Michal Vasko90932a92020-02-12 14:33:03 +010098 * @param[in] get_prefix Parser-specific getter to resolve prefixes used in the @p value string.
99 * @param[in] prefix_data User data for @p get_prefix.
100 * @param[in] format Input format of @p value.
101 * @param[out] node Created node.
102 * @return LY_SUCCESS on success.
103 * @return LY_EINCOMPLETE in case data tree is needed to finish the validation.
104 * @return LY_ERR value if an error occurred.
105 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200106LY_ERR lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic, int value_hint,
107 ly_resolve_prefix_clb get_prefix, void *prefix_data, LYD_FORMAT format, struct lyd_node **node);
Michal Vasko90932a92020-02-12 14:33:03 +0100108
109/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100110 * @brief Create a term (leaf/leaf-list) node from a parsed value by duplicating it.
111 *
112 * Hash is calculated and new node flag is set.
113 *
114 * @param[in] schema Schema node of the new data node.
115 * @param[in] val Parsed value to use.
116 * @param[out] node Created node.
117 * @return LY_SUCCESS on success.
118 * @return LY_ERR value if an error occurred.
119 */
120LY_ERR lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node);
121
122/**
123 * @brief Create an inner (container/list/RPC/action/notification) node.
124 *
125 * Hash is calculated and new node flag is set except
126 * for list with keys, when the hash is not calculated!
127 * Also, non-presence container has its default flag set.
Michal Vasko90932a92020-02-12 14:33:03 +0100128 *
129 * @param[in] schema Schema node of the new data node.
130 * @param[out] node Created node.
131 * @return LY_SUCCESS on success.
132 * @return LY_ERR value if an error occurred.
133 */
134LY_ERR lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node);
135
136/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100137 * @brief Create a list with all its keys (cannot be used for key-less list).
138 *
139 * Hash is calculated and new node flag is set.
Michal Vasko90932a92020-02-12 14:33:03 +0100140 *
141 * @param[in] schema Schema node of the new data node.
Michal Vasko004d3152020-06-11 19:59:22 +0200142 * @param[in] predicates Compiled key list predicates.
Michal Vasko90932a92020-02-12 14:33:03 +0100143 * @param[out] node Created node.
144 * @return LY_SUCCESS on success.
145 * @return LY_ERR value if an error occurred.
146 */
Michal Vasko004d3152020-06-11 19:59:22 +0200147LY_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 +0100148
149/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100150 * @brief Create an anyxml/anydata node.
151 *
152 * Hash is calculated and flags are properly set based on @p is_valid.
Michal Vasko90932a92020-02-12 14:33:03 +0100153 *
154 * @param[in] schema Schema node of the new data node.
155 * @param[in] value Value of the any node, is directly assigned into the data node.
156 * @param[in] value_type Value type of the value.
157 * @param[out] node Created node.
158 * @return LY_SUCCESS on success.
159 * @return LY_ERR value if an error occurred.
160 */
Michal Vasko9b368d32020-02-14 13:53:31 +0100161LY_ERR lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type,
162 struct lyd_node **node);
Michal Vasko90932a92020-02-12 14:33:03 +0100163
164/**
Michal Vasko52927e22020-03-16 17:26:14 +0100165 * @brief Create an opaque node.
166 *
167 * @param[in] ctx libyang context.
168 * @param[in] name Element name.
169 * @param[in] name_len Length of @p name, must be set correctly.
170 * @param[in] value String value to be parsed.
171 * @param[in] value_len Length of @p value, must be set correctly.
172 * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed.
Radek Krejci1798aae2020-07-14 13:26:06 +0200173 * @param[in] value_hint [Value hint](@ref lydvalueparseopts) from the parser regarding the value type.
Michal Vasko52927e22020-03-16 17:26:14 +0100174 * @param[in] format Input format of @p value and @p ns.
175 * @param[in] val_prefs Possible value prefixes, array is spent.
176 * @param[in] prefix Element prefix.
177 * @param[in] pref_len Length of @p prefix, must be set correctly.
Radek Krejci1798aae2020-07-14 13:26:06 +0200178 * @param[in] module_key Mandatory key to reference module, can be namespace or name.
179 * @param[in] module_key_len Length of @p module_key, must be set correctly.
Michal Vasko52927e22020-03-16 17:26:14 +0100180 * @param[out] node Created node.
181 * @return LY_SUCCESS on success.
182 * @return LY_ERR value if an error occurred.
183 */
184LY_ERR lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *value, size_t value_len,
Radek Krejci1798aae2020-07-14 13:26:06 +0200185 int *dynamic, int value_hint, LYD_FORMAT format, struct ly_prefix *val_prefs, const char *prefix, size_t pref_len,
186 const char *module_key, size_t module_key_len, struct lyd_node **node);
Michal Vasko52927e22020-03-16 17:26:14 +0100187
188/**
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200189 * @brief Check the existence and create any non-existing implicit siblings, recursively for the created nodes.
190 *
191 * @param[in] parent Parent of the potential default values, NULL for top-level siblings.
192 * @param[in,out] first First sibling.
193 * @param[in] sparent Schema parent of the siblings, NULL if schema of @p parent can be used.
194 * @param[in] mod Module of the default values, NULL for nested siblings.
195 * @param[in] node_types Optional set to add nodes with unresolved types into.
196 * @param[in] node_when Optional set to add nodes with "when" conditions into.
197 * @param[in] impl_opts Implicit options (@ref implicitoptions).
198 * @param[in,out] diff Validation diff.
199 * @return LY_ERR value.
200 */
201LY_ERR lyd_new_implicit_r(struct lyd_node *parent, struct lyd_node **first, const struct lysc_node *sparent,
202 const struct lys_module *mod, struct ly_set *node_types, struct ly_set *node_when,
203 int impl_opts, struct lyd_node **diff);
204
205/**
Michal Vaskob104f112020-07-17 09:54:54 +0200206 * @brief Find the next node, before which to insert the new node.
Michal Vasko90932a92020-02-12 14:33:03 +0100207 *
Michal Vaskob104f112020-07-17 09:54:54 +0200208 * @param[in] first_sibling First sibling of the nodes to consider.
209 * @param[in] new_node Node that will be inserted.
210 * @return Node to insert after.
211 * @return NULL if the new node should be first.
Michal Vasko90932a92020-02-12 14:33:03 +0100212 */
Michal Vaskob104f112020-07-17 09:54:54 +0200213struct 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 +0100214
215/**
Michal Vaskob104f112020-07-17 09:54:54 +0200216 * @brief Insert a node into parent/siblings. Order and hashes are fully handled.
Michal Vasko90932a92020-02-12 14:33:03 +0100217 *
Michal Vasko9b368d32020-02-14 13:53:31 +0100218 * @param[in] parent Parent to insert into, NULL for top-level sibling.
219 * @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 +0100220 * @param[in] node Individual node (without siblings) to insert.
221 */
Michal Vasko9b368d32020-02-14 13:53:31 +0100222void lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node);
Michal Vasko90932a92020-02-12 14:33:03 +0100223
224/**
Michal Vasko52927e22020-03-16 17:26:14 +0100225 * @brief Create and insert a metadata (last) into a parent.
Michal Vasko90932a92020-02-12 14:33:03 +0100226 *
Michal Vasko52927e22020-03-16 17:26:14 +0100227 * @param[in] parent Parent of the metadata, can be NULL.
Michal Vasko9f96a052020-03-10 09:41:45 +0100228 * @param[in,out] meta Metadata list to add at its end if @p parent is NULL, returned created attribute.
229 * @param[in] mod Metadata module (with the annotation definition).
Michal Vasko90932a92020-02-12 14:33:03 +0100230 * @param[in] name Attribute name.
Michal Vaskof03ed032020-03-04 13:31:44 +0100231 * @param[in] name_len Length of @p name, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100232 * @param[in] value String value to be parsed.
Michal Vaskof03ed032020-03-04 13:31:44 +0100233 * @param[in] value_len Length of @p value, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100234 * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed.
Radek Krejci1798aae2020-07-14 13:26:06 +0200235 * @param[in] value_hint [Value hint](@ref lydvalueparseopts) from the parser regarding the value type.
Michal Vasko52927e22020-03-16 17:26:14 +0100236 * @param[in] resolve_prefix Parser-specific getter to resolve prefixes used in the @p value string.
Michal Vasko90932a92020-02-12 14:33:03 +0100237 * @param[in] prefix_data User data for @p get_prefix.
238 * @param[in] format Input format of @p value.
Michal Vasko8d544252020-03-02 10:19:52 +0100239 * @param[in] ctx_snode Context node for value resolution in schema.
Michal Vasko90932a92020-02-12 14:33:03 +0100240 * @return LY_SUCCESS on success.
241 * @return LY_EINCOMPLETE in case data tree is needed to finish the validation.
242 * @return LY_ERR value if an error occurred.
243 */
Michal Vasko9f96a052020-03-10 09:41:45 +0100244LY_ERR lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name,
Radek Krejci1798aae2020-07-14 13:26:06 +0200245 size_t name_len, const char *value, size_t value_len, int *dynamic, int value_hint,
246 ly_resolve_prefix_clb resolve_prefix, void *prefix_data, LYD_FORMAT format, const struct lysc_node *ctx_snode);
247
248/**
249 * @brief Insert a metadata (last) into a parent
250 *
251 * @param[in] parent Parent of the metadata.
252 * @param[in] meta Metadata (list) to be added into the @p parent.
253 * @return LY_SUCCESS on success.
254 */
255LY_ERR lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta);
Michal Vasko90932a92020-02-12 14:33:03 +0100256
257/**
Michal Vasko52927e22020-03-16 17:26:14 +0100258 * @brief Create and insert a generic attribute (last) into a parent.
259 *
260 * @param[in] parent Parent of the attribute, can be NULL.
261 * @param[in,out] attr Attribute list to add at its end if @p parent is NULL, returned created attribute.
262 * @param[in] ctx libyang context.
263 * @param[in] name Attribute name.
264 * @param[in] name_len Length of @p name, must be set correctly.
265 * @param[in] value String value to be parsed.
266 * @param[in] value_len Length of @p value, must be set correctly.
267 * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed.
Radek Krejci1798aae2020-07-14 13:26:06 +0200268 * @param[in] value_hint [Value hint](@ref lydvalueparseopts) from the parser regarding the value type.
Michal Vasko52927e22020-03-16 17:26:14 +0100269 * @param[in] format Input format of @p value and @p ns.
270 * @param[in] val_prefs Possible value prefixes, array is spent.
271 * @param[in] prefix Attribute prefix.
272 * @param[in] prefix_len Attribute prefix length.
Radek Krejci1798aae2020-07-14 13:26:06 +0200273 * @param[in] module_key Mandatory key to reference module, can be namespace or name.
274 * @param[in] module_key_len Length of @p module_key, must be set correctly.
Michal Vasko52927e22020-03-16 17:26:14 +0100275 * @return LY_SUCCESS on success.
276 * @return LY_ERR value if an error occurred.
277 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200278LY_ERR lyd_create_attr(struct lyd_node *parent, struct lyd_attr **attr, const struct ly_ctx *ctx, const char *name,
279 size_t name_len, const char *value, size_t value_len, int *dynamic, int value_hint, LYD_FORMAT format,
280 struct ly_prefix *val_prefs, const char *prefix, size_t prefix_len, const char *module_key, size_t module_key_len);
281
282/**
283 * @defgroup lydvalueparseopts Hint options for type plugin callbacks from the data parsers.
284 *
285 * Options applicable to ly_value_parse()
286 * @{
287 */
288#define LYD_VALUE_PARSE_ISSTRING LY_TYPE_OPTS_ISSTRING /**< The input value is supposed to be a string. */
289#define LYD_VALUE_PARSE_ISNUMBER LY_TYPE_OPTS_ISNUMBER /**< The input value is supposed to be a number. */
290#define LYD_VALUE_PARSE_ISBOOLEAN LY_TYPE_OPTS_ISBOOLEAN /**< The input value is supposed to be a boolean. */
291#define LYD_VALUE_PARSE_ISEMPTY LY_TYPE_OPTS_ISEMPTY /**< The input value is supposed to be empty type. */
292
293/** @} lydvalueparseopts */
Michal Vasko52927e22020-03-16 17:26:14 +0100294
295/**
Radek Krejci5819f7c2019-05-31 14:53:29 +0200296 * @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 +0200297 *
Radek Krejci38d85362019-09-05 16:26:38 +0200298 * @param[in] node Data node for the @p value.
Radek Krejci084289f2019-07-09 17:35:30 +0200299 * @param[in] value String value to be parsed, must not be NULL.
Michal Vaskof03ed032020-03-04 13:31:44 +0100300 * @param[in] value_len Length of the give @p value, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100301 * @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 +0200302 * @param[in] second Flag for the second call after returning LY_EINCOMPLETE
Radek Krejci1798aae2020-07-14 13:26:06 +0200303 * @param[in] value_hint [Value hint](@ref lydvalueparseopts) from the parser.
Radek Krejci084289f2019-07-09 17:35:30 +0200304 * @param[in] get_prefix Parser-specific getter to resolve prefixes used in the @p value string.
Radek Krejciaca74032019-06-04 08:53:06 +0200305 * @param[in] parser Parser's data for @p get_prefix
Michal Vasko90932a92020-02-12 14:33:03 +0100306 * @param[in] format Input format of @p value.
Michal Vaskof03ed032020-03-04 13:31:44 +0100307 * @param[in] tree Data tree (e.g. when validating RPC/Notification) where the required
Radek Krejcie553e6d2019-06-07 15:33:18 +0200308 * data instance (leafref target, instance-identifier) can be placed. NULL in case the data tree are not yet complete,
309 * then LY_EINCOMPLETE can be returned.
310 * @return LY_SUCCESS on success
311 * @return LY_EINCOMPLETE in case the @p trees is not provided and it was needed to finish the validation.
312 * @return LY_ERR value if an error occurred.
Radek Krejcie7b95092019-05-15 11:03:07 +0200313 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200314LY_ERR lyd_value_parse(struct lyd_node_term *node, const char *value, size_t value_len, int *dynamic, int second, int value_hint,
315 ly_resolve_prefix_clb get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node *tree);
Radek Krejcie7b95092019-05-15 11:03:07 +0200316
Michal Vasko004d3152020-06-11 19:59:22 +0200317/* similar to lyd_value_parse except can be used just to store the value, hence does also not support a second call */
318LY_ERR lyd_value_store(struct lyd_value *val, const struct lysc_node *schema, const char *value, size_t value_len,
Radek Krejci1798aae2020-07-14 13:26:06 +0200319 int *dynamic, ly_resolve_prefix_clb get_prefix, void *parser, LYD_FORMAT format);
Michal Vasko004d3152020-06-11 19:59:22 +0200320
Radek Krejcie7b95092019-05-15 11:03:07 +0200321/**
Michal Vasko9f96a052020-03-10 09:41:45 +0100322 * @brief Validate, canonize and store the given @p value into the metadata according to the annotation type's rules.
Radek Krejci38d85362019-09-05 16:26:38 +0200323 *
Michal Vasko8d544252020-03-02 10:19:52 +0100324 * @param[in] ctx libyang context.
Michal Vasko9f96a052020-03-10 09:41:45 +0100325 * @param[in] meta Metadata for the @p value.
Radek Krejci38d85362019-09-05 16:26:38 +0200326 * @param[in] value String value to be parsed, must not be NULL.
Michal Vaskof03ed032020-03-04 13:31:44 +0100327 * @param[in] value_len Length of the give @p value, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100328 * @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 +0200329 * @param[in] second Flag for the second call after returning LY_EINCOMPLETE
Radek Krejci1798aae2020-07-14 13:26:06 +0200330 * @param[in] value_hint [Value hint](@ref lydvalueparseopts) from the parser.
Radek Krejci38d85362019-09-05 16:26:38 +0200331 * @param[in] get_prefix Parser-specific getter to resolve prefixes used in the @p value string.
332 * @param[in] parser Parser's data for @p get_prefix
333 * @param[in] format Input format of the data.
Michal Vasko8d544252020-03-02 10:19:52 +0100334 * @param[in] ctx_snode Context node for value resolution in schema.
Michal Vaskof03ed032020-03-04 13:31:44 +0100335 * @param[in] tree Data tree (e.g. when validating RPC/Notification) where the required
Radek Krejci38d85362019-09-05 16:26:38 +0200336 * data instance (leafref target, instance-identifier) can be placed. NULL in case the data tree are not yet complete,
337 * then LY_EINCOMPLETE can be returned.
338 * @return LY_SUCCESS on success
339 * @return LY_EINCOMPLETE in case the @p trees is not provided and it was needed to finish the validation.
340 * @return LY_ERR value if an error occurred.
341 */
Michal Vasko41586352020-07-13 13:54:25 +0200342LY_ERR lyd_value_parse_meta(const struct ly_ctx *ctx, struct lyd_meta *meta, const char *value, size_t value_len,
Radek Krejci1798aae2020-07-14 13:26:06 +0200343 int *dynamic, int second, int value_hint, ly_resolve_prefix_clb get_prefix, void *parser,
344 LYD_FORMAT format, const struct lysc_node *ctx_snode, const struct lyd_node *tree);
Radek Krejci38d85362019-09-05 16:26:38 +0200345
Michal Vaskof937cfe2020-08-03 16:07:12 +0200346/* generic function lys_value_validate */
347LY_ERR _lys_value_validate(const struct ly_ctx *ctx, const struct lysc_node *node, const char *value, size_t value_len,
348 ly_clb_resolve_prefix resolve_prefix, void *prefix_data, LYD_FORMAT format);
349
Radek Krejci38d85362019-09-05 16:26:38 +0200350/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200351 * @brief Parse XML string as YANG data tree.
352 *
353 * @param[in] ctx libyang context
Michal Vasko63f3d842020-07-08 10:10:14 +0200354 * @param[in] in Input structure.
Radek Krejci7931b192020-06-25 17:05:03 +0200355 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
356 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
Radek Krejci1798aae2020-07-14 13:26:06 +0200357 * @param[out] tree_p Parsed data tree. Note that NULL can be a valid result.
358 * @param[out] lydctx_p Data parser context to finish validation.
Michal Vasko9f96a052020-03-10 09:41:45 +0100359 * @return LY_ERR value.
Radek Krejcie7b95092019-05-15 11:03:07 +0200360 */
Michal Vasko63f3d842020-07-08 10:10:14 +0200361LY_ERR lyd_parse_xml_data(const struct ly_ctx *ctx, struct ly_in *in, int parse_options, int validate_options,
Radek Krejci1798aae2020-07-14 13:26:06 +0200362 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p);
Michal Vasko9f96a052020-03-10 09:41:45 +0100363
364/**
365 * @brief Parse XML string as YANG RPC/action invocation.
366 *
Radek Krejci7931b192020-06-25 17:05:03 +0200367 * Optional \<rpc\> envelope element is accepted if present. It is [checked](https://tools.ietf.org/html/rfc6241#section-4.1) and all
368 * 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
369 * 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 +0100370 *
371 * @param[in] ctx libyang context.
Michal Vasko63f3d842020-07-08 10:10:14 +0200372 * @param[in] in Input structure.
Radek Krejci1798aae2020-07-14 13:26:06 +0200373 * @param[out] tree_p Parsed full RPC/action tree.
374 * @param[out] op_p Optional pointer to the actual operation. Useful mainly for action.
Michal Vasko9f96a052020-03-10 09:41:45 +0100375 * @return LY_ERR value.
376 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200377LY_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 +0200378
379/**
Michal Vaskoa8edff02020-03-27 14:47:01 +0100380 * @brief Parse XML string as YANG notification.
381 *
382 * Optional \<notification\> envelope element, if present, is [checked](https://tools.ietf.org/html/rfc5277#page-25)
383 * and parsed. Specifically, its namespace and the child \<eventTime\> element and its value.
384 *
385 * @param[in] ctx libyang context.
Michal Vasko63f3d842020-07-08 10:10:14 +0200386 * @param[in] in Input structure.
Radek Krejci1798aae2020-07-14 13:26:06 +0200387 * @param[out] tree_p Parsed full notification tree.
388 * @param[out] op_p Optional pointer to the actual notification. Useful mainly for nested notifications.
Michal Vaskoa8edff02020-03-27 14:47:01 +0100389 * @return LY_ERR value.
390 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200391LY_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 +0100392
393/**
Michal Vasko1ce933a2020-03-30 12:38:22 +0200394 * @brief Parse XML string as YANG RPC/action reply.
395 *
396 * Optional \<rpc-reply\> envelope element, if present, is [checked](https://tools.ietf.org/html/rfc6241#section-4.2)
397 * and all its XML attributes parsed.
398 *
399 * @param[in] request Data tree of the RPC/action request.
Michal Vasko63f3d842020-07-08 10:10:14 +0200400 * @param[in] in Input structure.
Radek Krejci1798aae2020-07-14 13:26:06 +0200401 * @param[out] tree_p Parsed full reply tree. It always includes duplicated operation and parents of the @p request.
402 * @param[out] op_p Optional pointer to the reply operation. Useful mainly for action.
Michal Vasko1ce933a2020-03-30 12:38:22 +0200403 * @return LY_ERR value.
404 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200405LY_ERR lyd_parse_xml_reply(const struct lyd_node *request, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p);
406
407/**
408 * @brief Parse JSON string as YANG data tree.
409 *
410 * @param[in] ctx libyang context
411 * @param[in] in Input structure.
412 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
413 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
414 * @param[out] tree_p Parsed data tree. Note that NULL can be a valid result.
415 * @param[out] lydctx_p Data parser context to finish validation.
416 * @return LY_ERR value.
417 */
418LY_ERR lyd_parse_json_data(const struct ly_ctx *ctx, struct ly_in *in, int parse_options, int validate_options,
419 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p);
420
421/**
422 * @brief Parse JSON string as YANG notification.
423 *
424 * Optional top-level "notification" envelope object, if present, is [checked](https://tools.ietf.org/html/rfc5277#page-25)
425 * and parsed. Specifically the child "eventTime" member and its value.
426 *
427 * @param[in] ctx libyang context.
428 * @param[in] in Input structure.
429 * @param[out] tree_p Parsed full notification tree.
430 * @param[out] ntf_p Optional pointer to the actual notification. Useful mainly for nested notifications.
431 * @return LY_ERR value.
432 */
433LY_ERR lyd_parse_json_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **ntf_p);
434
435/**
436 * @brief Parse JSON string as YANG RPC/action invocation.
437 *
438 * Optional top-level "rpc" envelope object, if present is is [checked](https://tools.ietf.org/html/rfc6241#section-4.1) and the parser
439 * goes inside for the content, which is an RPC data or "action" envelope objects. The "action" envelope object is
440 * 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.
441 *
442 * @param[in] ctx libyang context.
443 * @param[in] in Input structure.
444 * @param[out] tree_p Parsed full RPC/action tree.
445 * @param[out] op_p Optional pointer to the actual operation. Useful mainly for action.
446 * @return LY_ERR value.
447 */
448LY_ERR lyd_parse_json_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p);
449
450/**
451 * @brief Parse JSON string as YANG RPC/action reply.
452 *
453 * Optional "rpc-reply" envelope object, if present, is [checked](https://tools.ietf.org/html/rfc6241#section-4.2).
454 *
455 * @param[in] request Data tree of the RPC/action request.
456 * @param[in] in Input structure.
457 * @param[out] tree_p Parsed full reply tree. It always includes duplicated operation and parents of the @p request.
458 * @param[out] op_p Optional pointer to the reply operation. Useful mainly for action.
459 * @return LY_ERR value.
460 */
461LY_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 +0200462
463/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200464 * @brief Parse binary data as YANG data tree.
465 *
466 * @param[in] ctx libyang context
Michal Vasko63f3d842020-07-08 10:10:14 +0200467 * @param[in] in Input structure.
Radek Krejci7931b192020-06-25 17:05:03 +0200468 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
469 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
Radek Krejci1798aae2020-07-14 13:26:06 +0200470 * @param[out] tree_p Parsed data tree. Note that NULL can be a valid result.
471 * @param[out] lydctx_p Data parser context to finish validation.
Michal Vasko60ea6352020-06-29 13:39:39 +0200472 * @return LY_ERR value.
473 */
Michal Vasko63f3d842020-07-08 10:10:14 +0200474LY_ERR lyd_parse_lyb_data(const struct ly_ctx *ctx, struct ly_in *in, int parse_options, int validate_options,
Radek Krejci1798aae2020-07-14 13:26:06 +0200475 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p);
Michal Vasko60ea6352020-06-29 13:39:39 +0200476
477/**
478 * @brief Parse binary data as YANG RPC/action invocation.
479 *
480 * @param[in] ctx libyang context.
Michal Vasko63f3d842020-07-08 10:10:14 +0200481 * @param[in] in Input structure.
Radek Krejci1798aae2020-07-14 13:26:06 +0200482 * @param[out] tree_p Parsed full RPC/action tree.
483 * @param[out] op_p Optional pointer to the actual operation. Useful mainly for action.
Michal Vasko60ea6352020-06-29 13:39:39 +0200484 * @return LY_ERR value.
485 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200486LY_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 +0200487
488/**
489 * @brief Parse binary data as YANG notification.
490 *
491 * @param[in] ctx libyang context.
Michal Vasko63f3d842020-07-08 10:10:14 +0200492 * @param[in] in Input structure.
Radek Krejci1798aae2020-07-14 13:26:06 +0200493 * @param[out] tree_p Parsed full notification tree.
494 * @param[out] ntf_p Optional pointer to the actual notification. Useful mainly for nested notifications.
Michal Vasko60ea6352020-06-29 13:39:39 +0200495 * @return LY_ERR value.
496 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200497LY_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 +0200498
499/**
500 * @brief Parse binary data as YANG RPC/action reply.
501 *
502 * @param[in] request Data tree of the RPC/action request.
Michal Vasko63f3d842020-07-08 10:10:14 +0200503 * @param[in] in Input structure.
Radek Krejci1798aae2020-07-14 13:26:06 +0200504 * @param[out] tree_p Parsed full reply tree. It always includes duplicated operation and parents of the @p request.
505 * @param[out] op_p Optional pointer to the reply operation. Useful mainly for action.
Michal Vasko60ea6352020-06-29 13:39:39 +0200506 * @return LY_ERR value.
507 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200508LY_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 +0200509
510/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200511 * @defgroup datahash Data nodes hash manipulation
512 * @ingroup datatree
513 */
514
515/**
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200516 * @brief Generate hash for the node.
517 *
518 * @param[in] node Data node to (re)generate hash value.
519 * @return LY_ERR value.
520 */
521LY_ERR lyd_hash(struct lyd_node *node);
522
523/**
524 * @brief Insert hash of the node into the hash table of its parent.
525 *
526 * @param[in] node Data node which hash will be inserted into the lyd_node_inner::children_hash hash table of its parent.
527 * @return LY_ERR value.
528 */
529LY_ERR lyd_insert_hash(struct lyd_node *node);
530
531/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200532 * @brief Maintain node's parent's children hash table when unlinking the node.
533 *
534 * When completely freeing data tree, it is expected to free the parent's children hash table first, at once.
535 *
536 * @param[in] node The data node being unlinked from its parent.
537 */
538void lyd_unlink_hash(struct lyd_node *node);
539
540/** @} datahash */
541
Radek Krejci084289f2019-07-09 17:35:30 +0200542/**
Michal Vaskob1b5c262020-03-05 14:29:47 +0100543 * @brief Iterate over implemented modules for functions that accept specific modules or the whole context.
544 *
545 * @param[in] tree Data tree.
Michal Vasko26e80012020-07-08 10:55:46 +0200546 * @param[in] module Selected module, NULL for all.
Michal Vaskob1b5c262020-03-05 14:29:47 +0100547 * @param[in] ctx Context, NULL for selected modules.
548 * @param[in,out] i Iterator, set to 0 on first call.
549 * @param[out] first First sibling of the returned module.
550 * @return Next module.
551 * @return NULL if all modules were traversed.
552 */
Michal Vasko26e80012020-07-08 10:55:46 +0200553const struct lys_module *lyd_mod_next_module(struct lyd_node *tree, const struct lys_module *module,
Michal Vaskob1b5c262020-03-05 14:29:47 +0100554 const struct ly_ctx *ctx, uint32_t *i, struct lyd_node **first);
555
556/**
557 * @brief Iterate over modules for functions that want to traverse all the top-level data.
558 *
559 * @param[in,out] next Pointer to the next module data, set to first top-level sibling on first call.
560 * @param[out] first First sibling of the returned module.
561 * @return Next module.
562 * @return NULL if all modules were traversed.
563 */
564const struct lys_module *lyd_data_next_module(struct lyd_node **next, struct lyd_node **first);
565
Michal Vasko9f96a052020-03-10 09:41:45 +0100566/**
567 * @brief Check that a list has all its keys.
568 *
569 * @param[in] node List to check.
Michal Vasko44685da2020-03-17 15:38:06 +0100570 * @return LY_SUCCESS on success.
571 * @return LY_ENOT on a missing key.
Michal Vasko9f96a052020-03-10 09:41:45 +0100572 */
573LY_ERR lyd_parse_check_keys(struct lyd_node *node);
574
Michal Vasko60ea6352020-06-29 13:39:39 +0200575/**
576 * @brief Set data flags for a newly parsed node.
577 *
578 * @param[in] node Node to use.
579 * @param[in] when_check Set of nodes with unresolved when.
580 * @param[in,out] meta Node metadata, may be removed from.
581 * @param[in] options Parse options.
582 */
583void lyd_parse_set_data_flags(struct lyd_node *node, struct ly_set *when_check, struct lyd_meta **meta, int options);
584
585/**
586 * @brief Free value prefixes.
587 *
588 * @param[in] ctx libyang context.
589 * @param[in] val_prefis Value prefixes to free, sized array (@ref sizedarrays).
590 */
591void ly_free_val_prefs(const struct ly_ctx *ctx, struct ly_prefix *val_prefs);
592
Michal Vaskod59035b2020-07-08 12:00:06 +0200593/**
594 * @brief Append all list key predicates to path.
595 *
596 * @param[in] node Node with keys to print.
597 * @param[in,out] buffer Buffer to print to.
598 * @param[in,out] buflen Current buffer length.
599 * @param[in,out] bufused Current number of characters used in @p buffer.
600 * @param[in] is_static Whether buffer is static or can be reallocated.
601 * @return LY_ERR
602 */
603LY_ERR lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static);
604
Radek Krejcie7b95092019-05-15 11:03:07 +0200605#endif /* LY_TREE_DATA_INTERNAL_H_ */