blob: f7c05405dfbf20621932d038d3743365cf4320f3 [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,
Radek Krejci0f969882020-08-21 16:56:47 +020085 const struct lysc_node **slast, const struct lysc_node *parent,
86 const struct lysc_module *module);
Michal Vaskoa6669ba2020-08-06 16:14:26 +020087
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] 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 Vasko90932a92020-02-12 14:33:03 +0100100 * @param[out] node Created node.
101 * @return LY_SUCCESS on success.
102 * @return LY_EINCOMPLETE in case data tree is needed to finish the validation.
103 * @return LY_ERR value if an error occurred.
104 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200105LY_ERR lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic, int value_hint,
Radek Krejci0f969882020-08-21 16:56:47 +0200106 LY_PREFIX_FORMAT format, void *prefix_data, struct lyd_node **node);
Michal Vasko90932a92020-02-12 14:33:03 +0100107
108/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100109 * @brief Create a term (leaf/leaf-list) node from a parsed value by duplicating it.
110 *
111 * Hash is calculated and new node flag is set.
112 *
113 * @param[in] schema Schema node of the new data node.
114 * @param[in] val Parsed value to use.
115 * @param[out] node Created node.
116 * @return LY_SUCCESS on success.
117 * @return LY_ERR value if an error occurred.
118 */
119LY_ERR lyd_create_term2(const struct lysc_node *schema, const struct lyd_value *val, struct lyd_node **node);
120
121/**
122 * @brief Create an inner (container/list/RPC/action/notification) node.
123 *
124 * Hash is calculated and new node flag is set except
125 * for list with keys, when the hash is not calculated!
126 * Also, non-presence container has its default flag set.
Michal Vasko90932a92020-02-12 14:33:03 +0100127 *
128 * @param[in] schema Schema node of the new data node.
129 * @param[out] node Created node.
130 * @return LY_SUCCESS on success.
131 * @return LY_ERR value if an error occurred.
132 */
133LY_ERR lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node);
134
135/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100136 * @brief Create a list with all its keys (cannot be used for key-less list).
137 *
138 * Hash is calculated and new node flag is set.
Michal Vasko90932a92020-02-12 14:33:03 +0100139 *
140 * @param[in] schema Schema node of the new data node.
Michal Vasko004d3152020-06-11 19:59:22 +0200141 * @param[in] predicates Compiled key list predicates.
Michal Vasko90932a92020-02-12 14:33:03 +0100142 * @param[out] node Created node.
143 * @return LY_SUCCESS on success.
144 * @return LY_ERR value if an error occurred.
145 */
Michal Vasko004d3152020-06-11 19:59:22 +0200146LY_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 +0100147
148/**
Michal Vasko9b368d32020-02-14 13:53:31 +0100149 * @brief Create an anyxml/anydata node.
150 *
151 * Hash is calculated and flags are properly set based on @p is_valid.
Michal Vasko90932a92020-02-12 14:33:03 +0100152 *
153 * @param[in] schema Schema node of the new data node.
154 * @param[in] value Value of the any node, is directly assigned into the data node.
155 * @param[in] value_type Value type of the value.
156 * @param[out] node Created node.
157 * @return LY_SUCCESS on success.
158 * @return LY_ERR value if an error occurred.
159 */
Michal Vasko9b368d32020-02-14 13:53:31 +0100160LY_ERR lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type,
Radek Krejci0f969882020-08-21 16:56:47 +0200161 struct lyd_node **node);
Michal Vasko90932a92020-02-12 14:33:03 +0100162
163/**
Michal Vasko52927e22020-03-16 17:26:14 +0100164 * @brief Create an opaque node.
165 *
166 * @param[in] ctx libyang context.
167 * @param[in] name Element name.
168 * @param[in] name_len Length of @p name, must be set correctly.
169 * @param[in] value String value to be parsed.
170 * @param[in] value_len Length of @p value, must be set correctly.
171 * @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 +0200172 * @param[in] value_hint [Value hint](@ref lydvalueparseopts) from the parser regarding the value type.
Michal Vasko52927e22020-03-16 17:26:14 +0100173 * @param[in] format Input format of @p value and @p ns.
174 * @param[in] val_prefs Possible value prefixes, array is spent.
175 * @param[in] prefix Element prefix.
176 * @param[in] pref_len Length of @p prefix, must be set correctly.
Radek Krejci1798aae2020-07-14 13:26:06 +0200177 * @param[in] module_key Mandatory key to reference module, can be namespace or name.
178 * @param[in] module_key_len Length of @p module_key, must be set correctly.
Michal Vasko52927e22020-03-16 17:26:14 +0100179 * @param[out] node Created node.
180 * @return LY_SUCCESS on success.
181 * @return LY_ERR value if an error occurred.
182 */
183LY_ERR lyd_create_opaq(const struct ly_ctx *ctx, const char *name, size_t name_len, const char *value, size_t value_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200184 int *dynamic, int value_hint, LYD_FORMAT format, struct ly_prefix *val_prefs, const char *prefix,
185 size_t pref_len, const char *module_key, size_t module_key_len, struct lyd_node **node);
Michal Vasko52927e22020-03-16 17:26:14 +0100186
187/**
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200188 * @brief Check the existence and create any non-existing implicit siblings, recursively for the created nodes.
189 *
190 * @param[in] parent Parent of the potential default values, NULL for top-level siblings.
191 * @param[in,out] first First sibling.
192 * @param[in] sparent Schema parent of the siblings, NULL if schema of @p parent can be used.
193 * @param[in] mod Module of the default values, NULL for nested siblings.
194 * @param[in] node_types Optional set to add nodes with unresolved types into.
195 * @param[in] node_when Optional set to add nodes with "when" conditions into.
196 * @param[in] impl_opts Implicit options (@ref implicitoptions).
197 * @param[in,out] diff Validation diff.
198 * @return LY_ERR value.
199 */
200LY_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 +0200201 const struct lys_module *mod, struct ly_set *node_types, struct ly_set *node_when,
202 int impl_opts, struct lyd_node **diff);
Michal Vaskoa6669ba2020-08-06 16:14:26 +0200203
204/**
Michal Vaskob104f112020-07-17 09:54:54 +0200205 * @brief Find the next node, before which to insert the new node.
Michal Vasko90932a92020-02-12 14:33:03 +0100206 *
Michal Vaskob104f112020-07-17 09:54:54 +0200207 * @param[in] first_sibling First sibling of the nodes to consider.
208 * @param[in] new_node Node that will be inserted.
209 * @return Node to insert after.
210 * @return NULL if the new node should be first.
Michal Vasko90932a92020-02-12 14:33:03 +0100211 */
Michal Vaskob104f112020-07-17 09:54:54 +0200212struct 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 +0100213
214/**
Michal Vaskob104f112020-07-17 09:54:54 +0200215 * @brief Insert a node into parent/siblings. Order and hashes are fully handled.
Michal Vasko90932a92020-02-12 14:33:03 +0100216 *
Michal Vasko9b368d32020-02-14 13:53:31 +0100217 * @param[in] parent Parent to insert into, NULL for top-level sibling.
218 * @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 +0100219 * @param[in] node Individual node (without siblings) to insert.
220 */
Michal Vasko9b368d32020-02-14 13:53:31 +0100221void lyd_insert_node(struct lyd_node *parent, struct lyd_node **first_sibling, struct lyd_node *node);
Michal Vasko90932a92020-02-12 14:33:03 +0100222
223/**
Michal Vaskoa5da3292020-08-12 13:10:50 +0200224 * @brief Insert a metadata (last) into a parent
225 *
226 * @param[in] parent Parent of the metadata.
227 * @param[in] meta Metadata (list) to be added into the @p parent.
228 */
229void lyd_insert_meta(struct lyd_node *parent, struct lyd_meta *meta);
230
231/**
Michal Vasko52927e22020-03-16 17:26:14 +0100232 * @brief Create and insert a metadata (last) into a parent.
Michal Vasko90932a92020-02-12 14:33:03 +0100233 *
Michal Vasko52927e22020-03-16 17:26:14 +0100234 * @param[in] parent Parent of the metadata, can be NULL.
Michal Vasko9f96a052020-03-10 09:41:45 +0100235 * @param[in,out] meta Metadata list to add at its end if @p parent is NULL, returned created attribute.
236 * @param[in] mod Metadata module (with the annotation definition).
Michal Vasko90932a92020-02-12 14:33:03 +0100237 * @param[in] name Attribute name.
Michal Vaskof03ed032020-03-04 13:31:44 +0100238 * @param[in] name_len Length of @p name, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100239 * @param[in] value String value to be parsed.
Michal Vaskof03ed032020-03-04 13:31:44 +0100240 * @param[in] value_len Length of @p value, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100241 * @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 +0200242 * @param[in] value_hint [Value hint](@ref lydvalueparseopts) from the parser regarding the value type.
Michal Vasko90932a92020-02-12 14:33:03 +0100243 * @param[in] format Input format of @p value.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200244 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vasko8d544252020-03-02 10:19:52 +0100245 * @param[in] ctx_snode Context node for value resolution in schema.
Michal Vasko90932a92020-02-12 14:33:03 +0100246 * @return LY_SUCCESS on success.
247 * @return LY_EINCOMPLETE in case data tree is needed to finish the validation.
248 * @return LY_ERR value if an error occurred.
249 */
Michal Vasko9f96a052020-03-10 09:41:45 +0100250LY_ERR lyd_create_meta(struct lyd_node *parent, struct lyd_meta **meta, const struct lys_module *mod, const char *name,
Radek Krejci0f969882020-08-21 16:56:47 +0200251 size_t name_len, const char *value, size_t value_len, int *dynamic, int value_hint,
252 LY_PREFIX_FORMAT format, void *prefix_data, const struct lysc_node *ctx_snode);
Radek Krejci1798aae2020-07-14 13:26:06 +0200253
254/**
Michal Vaskoa5da3292020-08-12 13:10:50 +0200255 * @brief Insert an attribute (last) into a parent
Radek Krejci1798aae2020-07-14 13:26:06 +0200256 *
Michal Vaskoa5da3292020-08-12 13:10:50 +0200257 * @param[in] parent Parent of the attributes.
258 * @param[in] attr Attribute (list) to be added into the @p parent.
Radek Krejci1798aae2020-07-14 13:26:06 +0200259 */
Michal Vaskoa5da3292020-08-12 13:10:50 +0200260void lyd_insert_attr(struct lyd_node *parent, struct lyd_attr *attr);
Michal Vasko90932a92020-02-12 14:33:03 +0100261
262/**
Michal Vasko52927e22020-03-16 17:26:14 +0100263 * @brief Create and insert a generic attribute (last) into a parent.
264 *
265 * @param[in] parent Parent of the attribute, can be NULL.
266 * @param[in,out] attr Attribute list to add at its end if @p parent is NULL, returned created attribute.
267 * @param[in] ctx libyang context.
268 * @param[in] name Attribute name.
269 * @param[in] name_len Length of @p name, must be set correctly.
270 * @param[in] value String value to be parsed.
271 * @param[in] value_len Length of @p value, must be set correctly.
272 * @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 +0200273 * @param[in] value_hint [Value hint](@ref lydvalueparseopts) from the parser regarding the value type.
Michal Vasko52927e22020-03-16 17:26:14 +0100274 * @param[in] format Input format of @p value and @p ns.
275 * @param[in] val_prefs Possible value prefixes, array is spent.
276 * @param[in] prefix Attribute prefix.
277 * @param[in] prefix_len Attribute prefix length.
Radek Krejci1798aae2020-07-14 13:26:06 +0200278 * @param[in] module_key Mandatory key to reference module, can be namespace or name.
279 * @param[in] module_key_len Length of @p module_key, must be set correctly.
Michal Vasko52927e22020-03-16 17:26:14 +0100280 * @return LY_SUCCESS on success.
281 * @return LY_ERR value if an error occurred.
282 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200283LY_ERR lyd_create_attr(struct lyd_node *parent, struct lyd_attr **attr, const struct ly_ctx *ctx, const char *name,
Radek Krejci0f969882020-08-21 16:56:47 +0200284 size_t name_len, const char *value, size_t value_len, int *dynamic, int value_hint, LYD_FORMAT format,
285 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 +0200286
287/**
288 * @defgroup lydvalueparseopts Hint options for type plugin callbacks from the data parsers.
289 *
290 * Options applicable to ly_value_parse()
291 * @{
292 */
293#define LYD_VALUE_PARSE_ISSTRING LY_TYPE_OPTS_ISSTRING /**< The input value is supposed to be a string. */
294#define LYD_VALUE_PARSE_ISNUMBER LY_TYPE_OPTS_ISNUMBER /**< The input value is supposed to be a number. */
295#define LYD_VALUE_PARSE_ISBOOLEAN LY_TYPE_OPTS_ISBOOLEAN /**< The input value is supposed to be a boolean. */
296#define LYD_VALUE_PARSE_ISEMPTY LY_TYPE_OPTS_ISEMPTY /**< The input value is supposed to be empty type. */
297
298/** @} lydvalueparseopts */
Michal Vasko52927e22020-03-16 17:26:14 +0100299
300/**
Radek Krejci5819f7c2019-05-31 14:53:29 +0200301 * @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 +0200302 *
Radek Krejci38d85362019-09-05 16:26:38 +0200303 * @param[in] node Data node for the @p value.
Radek Krejci084289f2019-07-09 17:35:30 +0200304 * @param[in] value String value to be parsed, must not be NULL.
Michal Vaskof03ed032020-03-04 13:31:44 +0100305 * @param[in] value_len Length of the give @p value, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100306 * @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 +0200307 * @param[in] second Flag for the second call after returning LY_EINCOMPLETE
Radek Krejci1798aae2020-07-14 13:26:06 +0200308 * @param[in] value_hint [Value hint](@ref lydvalueparseopts) from the parser.
Michal Vasko90932a92020-02-12 14:33:03 +0100309 * @param[in] format Input format of @p value.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200310 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vaskof03ed032020-03-04 13:31:44 +0100311 * @param[in] tree Data tree (e.g. when validating RPC/Notification) where the required
Radek Krejcie553e6d2019-06-07 15:33:18 +0200312 * data instance (leafref target, instance-identifier) can be placed. NULL in case the data tree are not yet complete,
313 * then LY_EINCOMPLETE can be returned.
314 * @return LY_SUCCESS on success
315 * @return LY_EINCOMPLETE in case the @p trees is not provided and it was needed to finish the validation.
316 * @return LY_ERR value if an error occurred.
Radek Krejcie7b95092019-05-15 11:03:07 +0200317 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200318LY_ERR lyd_value_parse(struct lyd_node_term *node, const char *value, size_t value_len, int *dynamic, int second, int value_hint,
Radek Krejci0f969882020-08-21 16:56:47 +0200319 LY_PREFIX_FORMAT format, void *prefix_data, const struct lyd_node *tree);
Radek Krejcie7b95092019-05-15 11:03:07 +0200320
Michal Vasko004d3152020-06-11 19:59:22 +0200321/* similar to lyd_value_parse except can be used just to store the value, hence does also not support a second call */
322LY_ERR lyd_value_store(struct lyd_value *val, const struct lysc_node *schema, const char *value, size_t value_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200323 int *dynamic, LY_PREFIX_FORMAT format, void *prefix_data);
Michal Vasko004d3152020-06-11 19:59:22 +0200324
Radek Krejcie7b95092019-05-15 11:03:07 +0200325/**
Michal Vasko9f96a052020-03-10 09:41:45 +0100326 * @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 +0200327 *
Michal Vasko8d544252020-03-02 10:19:52 +0100328 * @param[in] ctx libyang context.
Michal Vasko9f96a052020-03-10 09:41:45 +0100329 * @param[in] meta Metadata for the @p value.
Radek Krejci38d85362019-09-05 16:26:38 +0200330 * @param[in] value String value to be parsed, must not be NULL.
Michal Vaskof03ed032020-03-04 13:31:44 +0100331 * @param[in] value_len Length of the give @p value, must be set correctly.
Michal Vasko90932a92020-02-12 14:33:03 +0100332 * @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 +0200333 * @param[in] second Flag for the second call after returning LY_EINCOMPLETE
Radek Krejci1798aae2020-07-14 13:26:06 +0200334 * @param[in] value_hint [Value hint](@ref lydvalueparseopts) from the parser.
Radek Krejci38d85362019-09-05 16:26:38 +0200335 * @param[in] format Input format of the data.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200336 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vasko8d544252020-03-02 10:19:52 +0100337 * @param[in] ctx_snode Context node for value resolution in schema.
Michal Vaskof03ed032020-03-04 13:31:44 +0100338 * @param[in] tree Data tree (e.g. when validating RPC/Notification) where the required
Radek Krejci38d85362019-09-05 16:26:38 +0200339 * data instance (leafref target, instance-identifier) can be placed. NULL in case the data tree are not yet complete,
340 * then LY_EINCOMPLETE can be returned.
341 * @return LY_SUCCESS on success
342 * @return LY_EINCOMPLETE in case the @p trees is not provided and it was needed to finish the validation.
343 * @return LY_ERR value if an error occurred.
344 */
Michal Vasko41586352020-07-13 13:54:25 +0200345LY_ERR lyd_value_parse_meta(const struct ly_ctx *ctx, struct lyd_meta *meta, const char *value, size_t value_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200346 int *dynamic, int second, int value_hint, LY_PREFIX_FORMAT format, void *prefix_data,
347 const struct lysc_node *ctx_snode, const struct lyd_node *tree);
Radek Krejci38d85362019-09-05 16:26:38 +0200348
Michal Vaskof937cfe2020-08-03 16:07:12 +0200349/* generic function lys_value_validate */
350LY_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 +0200351 LY_PREFIX_FORMAT format, void *prefix_data);
Michal Vaskof937cfe2020-08-03 16:07:12 +0200352
Radek Krejci38d85362019-09-05 16:26:38 +0200353/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200354 * @brief Parse XML string as YANG data tree.
355 *
356 * @param[in] ctx libyang context
Michal Vasko63f3d842020-07-08 10:10:14 +0200357 * @param[in] in Input structure.
Radek Krejci7931b192020-06-25 17:05:03 +0200358 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
359 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
Radek Krejci1798aae2020-07-14 13:26:06 +0200360 * @param[out] tree_p Parsed data tree. Note that NULL can be a valid result.
361 * @param[out] lydctx_p Data parser context to finish validation.
Michal Vasko9f96a052020-03-10 09:41:45 +0100362 * @return LY_ERR value.
Radek Krejcie7b95092019-05-15 11:03:07 +0200363 */
Michal Vasko63f3d842020-07-08 10:10:14 +0200364LY_ERR lyd_parse_xml_data(const struct ly_ctx *ctx, struct ly_in *in, int parse_options, int validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +0200365 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p);
Michal Vasko9f96a052020-03-10 09:41:45 +0100366
367/**
368 * @brief Parse XML string as YANG RPC/action invocation.
369 *
Radek Krejci7931b192020-06-25 17:05:03 +0200370 * Optional \<rpc\> envelope element is accepted if present. It is [checked](https://tools.ietf.org/html/rfc6241#section-4.1) and all
371 * 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
372 * 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 +0100373 *
374 * @param[in] ctx libyang context.
Michal Vasko63f3d842020-07-08 10:10:14 +0200375 * @param[in] in Input structure.
Radek Krejci1798aae2020-07-14 13:26:06 +0200376 * @param[out] tree_p Parsed full RPC/action tree.
377 * @param[out] op_p Optional pointer to the actual operation. Useful mainly for action.
Michal Vasko9f96a052020-03-10 09:41:45 +0100378 * @return LY_ERR value.
379 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200380LY_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 +0200381
382/**
Michal Vaskoa8edff02020-03-27 14:47:01 +0100383 * @brief Parse XML string as YANG notification.
384 *
385 * Optional \<notification\> envelope element, if present, is [checked](https://tools.ietf.org/html/rfc5277#page-25)
386 * and parsed. Specifically, its namespace and the child \<eventTime\> element and its value.
387 *
388 * @param[in] ctx libyang context.
Michal Vasko63f3d842020-07-08 10:10:14 +0200389 * @param[in] in Input structure.
Radek Krejci1798aae2020-07-14 13:26:06 +0200390 * @param[out] tree_p Parsed full notification tree.
391 * @param[out] op_p Optional pointer to the actual notification. Useful mainly for nested notifications.
Michal Vaskoa8edff02020-03-27 14:47:01 +0100392 * @return LY_ERR value.
393 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200394LY_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 +0100395
396/**
Michal Vasko1ce933a2020-03-30 12:38:22 +0200397 * @brief Parse XML string as YANG RPC/action reply.
398 *
399 * Optional \<rpc-reply\> envelope element, if present, is [checked](https://tools.ietf.org/html/rfc6241#section-4.2)
400 * and all its XML attributes parsed.
401 *
402 * @param[in] request Data tree of the RPC/action request.
Michal Vasko63f3d842020-07-08 10:10:14 +0200403 * @param[in] in Input structure.
Radek Krejci1798aae2020-07-14 13:26:06 +0200404 * @param[out] tree_p Parsed full reply tree. It always includes duplicated operation and parents of the @p request.
405 * @param[out] op_p Optional pointer to the reply operation. Useful mainly for action.
Michal Vasko1ce933a2020-03-30 12:38:22 +0200406 * @return LY_ERR value.
407 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200408LY_ERR lyd_parse_xml_reply(const struct lyd_node *request, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p);
409
410/**
411 * @brief Parse JSON string as YANG data tree.
412 *
413 * @param[in] ctx libyang context
414 * @param[in] in Input structure.
415 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
416 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
417 * @param[out] tree_p Parsed data tree. Note that NULL can be a valid result.
418 * @param[out] lydctx_p Data parser context to finish validation.
419 * @return LY_ERR value.
420 */
421LY_ERR lyd_parse_json_data(const struct ly_ctx *ctx, struct ly_in *in, int parse_options, int validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +0200422 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p);
Radek Krejci1798aae2020-07-14 13:26:06 +0200423
424/**
425 * @brief Parse JSON string as YANG notification.
426 *
427 * Optional top-level "notification" envelope object, if present, is [checked](https://tools.ietf.org/html/rfc5277#page-25)
428 * and parsed. Specifically the child "eventTime" member and its value.
429 *
430 * @param[in] ctx libyang context.
431 * @param[in] in Input structure.
432 * @param[out] tree_p Parsed full notification tree.
433 * @param[out] ntf_p Optional pointer to the actual notification. Useful mainly for nested notifications.
434 * @return LY_ERR value.
435 */
436LY_ERR lyd_parse_json_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **ntf_p);
437
438/**
439 * @brief Parse JSON string as YANG RPC/action invocation.
440 *
441 * Optional top-level "rpc" envelope object, if present is is [checked](https://tools.ietf.org/html/rfc6241#section-4.1) and the parser
442 * goes inside for the content, which is an RPC data or "action" envelope objects. The "action" envelope object is
443 * 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.
444 *
445 * @param[in] ctx libyang context.
446 * @param[in] in Input structure.
447 * @param[out] tree_p Parsed full RPC/action tree.
448 * @param[out] op_p Optional pointer to the actual operation. Useful mainly for action.
449 * @return LY_ERR value.
450 */
451LY_ERR lyd_parse_json_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree_p, struct lyd_node **op_p);
452
453/**
454 * @brief Parse JSON string as YANG RPC/action reply.
455 *
456 * Optional "rpc-reply" envelope object, if present, is [checked](https://tools.ietf.org/html/rfc6241#section-4.2).
457 *
458 * @param[in] request Data tree of the RPC/action request.
459 * @param[in] in Input structure.
460 * @param[out] tree_p Parsed full reply tree. It always includes duplicated operation and parents of the @p request.
461 * @param[out] op_p Optional pointer to the reply operation. Useful mainly for action.
462 * @return LY_ERR value.
463 */
464LY_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 +0200465
466/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200467 * @brief Parse binary data as YANG data tree.
468 *
469 * @param[in] ctx libyang context
Michal Vasko63f3d842020-07-08 10:10:14 +0200470 * @param[in] in Input structure.
Radek Krejci7931b192020-06-25 17:05:03 +0200471 * @param[in] parse_options Options for parser, see @ref dataparseroptions.
472 * @param[in] validate_options Options for the validation phase, see @ref datavalidationoptions.
Radek Krejci1798aae2020-07-14 13:26:06 +0200473 * @param[out] tree_p Parsed data tree. Note that NULL can be a valid result.
474 * @param[out] lydctx_p Data parser context to finish validation.
Michal Vasko60ea6352020-06-29 13:39:39 +0200475 * @return LY_ERR value.
476 */
Michal Vasko63f3d842020-07-08 10:10:14 +0200477LY_ERR lyd_parse_lyb_data(const struct ly_ctx *ctx, struct ly_in *in, int parse_options, int validate_options,
Radek Krejci0f969882020-08-21 16:56:47 +0200478 struct lyd_node **tree_p, struct lyd_ctx **lydctx_p);
Michal Vasko60ea6352020-06-29 13:39:39 +0200479
480/**
481 * @brief Parse binary data as YANG RPC/action invocation.
482 *
483 * @param[in] ctx libyang context.
Michal Vasko63f3d842020-07-08 10:10:14 +0200484 * @param[in] in Input structure.
Radek Krejci1798aae2020-07-14 13:26:06 +0200485 * @param[out] tree_p Parsed full RPC/action tree.
486 * @param[out] op_p Optional pointer to the actual operation. Useful mainly for action.
Michal Vasko60ea6352020-06-29 13:39:39 +0200487 * @return LY_ERR value.
488 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200489LY_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 +0200490
491/**
492 * @brief Parse binary data as YANG notification.
493 *
494 * @param[in] ctx libyang context.
Michal Vasko63f3d842020-07-08 10:10:14 +0200495 * @param[in] in Input structure.
Radek Krejci1798aae2020-07-14 13:26:06 +0200496 * @param[out] tree_p Parsed full notification tree.
497 * @param[out] ntf_p Optional pointer to the actual notification. Useful mainly for nested notifications.
Michal Vasko60ea6352020-06-29 13:39:39 +0200498 * @return LY_ERR value.
499 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200500LY_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 +0200501
502/**
503 * @brief Parse binary data as YANG RPC/action reply.
504 *
505 * @param[in] request Data tree of the RPC/action request.
Michal Vasko63f3d842020-07-08 10:10:14 +0200506 * @param[in] in Input structure.
Radek Krejci1798aae2020-07-14 13:26:06 +0200507 * @param[out] tree_p Parsed full reply tree. It always includes duplicated operation and parents of the @p request.
508 * @param[out] op_p Optional pointer to the reply operation. Useful mainly for action.
Michal Vasko60ea6352020-06-29 13:39:39 +0200509 * @return LY_ERR value.
510 */
Radek Krejci1798aae2020-07-14 13:26:06 +0200511LY_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 +0200512
513/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200514 * @defgroup datahash Data nodes hash manipulation
515 * @ingroup datatree
516 */
517
518/**
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200519 * @brief Generate hash for the node.
520 *
521 * @param[in] node Data node to (re)generate hash value.
522 * @return LY_ERR value.
523 */
524LY_ERR lyd_hash(struct lyd_node *node);
525
526/**
527 * @brief Insert hash of the node into the hash table of its parent.
528 *
529 * @param[in] node Data node which hash will be inserted into the lyd_node_inner::children_hash hash table of its parent.
530 * @return LY_ERR value.
531 */
532LY_ERR lyd_insert_hash(struct lyd_node *node);
533
534/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200535 * @brief Maintain node's parent's children hash table when unlinking the node.
536 *
537 * When completely freeing data tree, it is expected to free the parent's children hash table first, at once.
538 *
539 * @param[in] node The data node being unlinked from its parent.
540 */
541void lyd_unlink_hash(struct lyd_node *node);
542
543/** @} datahash */
544
Radek Krejci084289f2019-07-09 17:35:30 +0200545/**
Michal Vaskob1b5c262020-03-05 14:29:47 +0100546 * @brief Iterate over implemented modules for functions that accept specific modules or the whole context.
547 *
548 * @param[in] tree Data tree.
Michal Vasko26e80012020-07-08 10:55:46 +0200549 * @param[in] module Selected module, NULL for all.
Michal Vaskob1b5c262020-03-05 14:29:47 +0100550 * @param[in] ctx Context, NULL for selected modules.
551 * @param[in,out] i Iterator, set to 0 on first call.
552 * @param[out] first First sibling of the returned module.
553 * @return Next module.
554 * @return NULL if all modules were traversed.
555 */
Michal Vasko26e80012020-07-08 10:55:46 +0200556const struct lys_module *lyd_mod_next_module(struct lyd_node *tree, const struct lys_module *module,
Radek Krejci0f969882020-08-21 16:56:47 +0200557 const struct ly_ctx *ctx, uint32_t *i, struct lyd_node **first);
Michal Vaskob1b5c262020-03-05 14:29:47 +0100558
559/**
560 * @brief Iterate over modules for functions that want to traverse all the top-level data.
561 *
562 * @param[in,out] next Pointer to the next module data, set to first top-level sibling on first call.
563 * @param[out] first First sibling of the returned module.
564 * @return Next module.
565 * @return NULL if all modules were traversed.
566 */
567const struct lys_module *lyd_data_next_module(struct lyd_node **next, struct lyd_node **first);
568
Michal Vasko9f96a052020-03-10 09:41:45 +0100569/**
570 * @brief Check that a list has all its keys.
571 *
572 * @param[in] node List to check.
Michal Vasko44685da2020-03-17 15:38:06 +0100573 * @return LY_SUCCESS on success.
574 * @return LY_ENOT on a missing key.
Michal Vasko9f96a052020-03-10 09:41:45 +0100575 */
576LY_ERR lyd_parse_check_keys(struct lyd_node *node);
577
Michal Vasko60ea6352020-06-29 13:39:39 +0200578/**
579 * @brief Set data flags for a newly parsed node.
580 *
581 * @param[in] node Node to use.
582 * @param[in] when_check Set of nodes with unresolved when.
583 * @param[in,out] meta Node metadata, may be removed from.
584 * @param[in] options Parse options.
585 */
586void lyd_parse_set_data_flags(struct lyd_node *node, struct ly_set *when_check, struct lyd_meta **meta, int options);
587
588/**
589 * @brief Free value prefixes.
590 *
591 * @param[in] ctx libyang context.
592 * @param[in] val_prefis Value prefixes to free, sized array (@ref sizedarrays).
593 */
594void ly_free_val_prefs(const struct ly_ctx *ctx, struct ly_prefix *val_prefs);
595
Michal Vaskod59035b2020-07-08 12:00:06 +0200596/**
597 * @brief Append all list key predicates to path.
598 *
599 * @param[in] node Node with keys to print.
600 * @param[in,out] buffer Buffer to print to.
601 * @param[in,out] buflen Current buffer length.
602 * @param[in,out] bufused Current number of characters used in @p buffer.
603 * @param[in] is_static Whether buffer is static or can be reallocated.
604 * @return LY_ERR
605 */
606LY_ERR lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, int is_static);
607
Radek Krejcie7b95092019-05-15 11:03:07 +0200608#endif /* LY_TREE_DATA_INTERNAL_H_ */