blob: 9b24e107a91b83091c145cb644adbbd01b2e4acc [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
2 * @file tree_data_internal.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief internal functions for YANG schema trees.
5 *
6 * Copyright (c) 2015 - 2019 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#ifndef LY_TREE_DATA_INTERNAL_H_
16#define LY_TREE_DATA_INTERNAL_H_
17
18#include "tree_data.h"
Radek Krejciaca74032019-06-04 08:53:06 +020019#include "plugins_types.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020020
21/**
22 * @brief Get address of a node's child pointer if any.
23 *
24 * Decides the node's type and in case it has a children list, returns its address.
25 * @param[in] node Node to check.
26 * @return Address of the node's child member if any, NULL otherwise.
27 */
28struct lyd_node **lyd_node_children_p(struct lyd_node *node);
29
30/**
Michal Vasko90932a92020-02-12 14:33:03 +010031 * @brief Create a term (leaf/leaf-list) node. Hash is not calculated.
32 *
33 * @param[in] schema Schema node of the new data node.
34 * @param[in] value String value to be parsed.
35 * @param[in] value_len Length of @p value.
36 * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed.
37 * @param[in] get_prefix Parser-specific getter to resolve prefixes used in the @p value string.
38 * @param[in] prefix_data User data for @p get_prefix.
39 * @param[in] format Input format of @p value.
40 * @param[out] node Created node.
41 * @return LY_SUCCESS on success.
42 * @return LY_EINCOMPLETE in case data tree is needed to finish the validation.
43 * @return LY_ERR value if an error occurred.
44 */
45LY_ERR lyd_create_term(const struct lysc_node *schema, const char *value, size_t value_len, int *dynamic,
46 ly_clb_resolve_prefix get_prefix, void *prefix_data, LYD_FORMAT format, struct lyd_node **node);
47
48/**
49 * @brief Create an inner (container/list/RPC/action/notification) node. Hash is not calculated.
50 *
51 * @param[in] schema Schema node of the new data node.
52 * @param[out] node Created node.
53 * @return LY_SUCCESS on success.
54 * @return LY_ERR value if an error occurred.
55 */
56LY_ERR lyd_create_inner(const struct lysc_node *schema, struct lyd_node **node);
57
58/**
59 * @brief Create a list with all its keys (cannot be used for key-less list). Hash is not calculated.
60 *
61 * @param[in] schema Schema node of the new data node.
62 * @param[in] keys_str List instance key values in the form of "[key1='val1'][key2='val2']...".
63 * The keys do not have to be ordered but all of them must be set.
64 * @param[in] keys_len Length of @p keys_str.
65 * @param[out] node Created node.
66 * @return LY_SUCCESS on success.
67 * @return LY_ERR value if an error occurred.
68 */
69LY_ERR lyd_create_list(const struct lysc_node *schema, const char *keys_str, size_t keys_len, struct lyd_node **node);
70
71/**
72 * @brief Create an anyxml/anydata node. Hash is not calculated.
73 *
74 * @param[in] schema Schema node of the new data node.
75 * @param[in] value Value of the any node, is directly assigned into the data node.
76 * @param[in] value_type Value type of the value.
77 * @param[out] node Created node.
78 * @return LY_SUCCESS on success.
79 * @return LY_ERR value if an error occurred.
80 */
81LY_ERR lyd_create_any(const struct lysc_node *schema, const void *value, LYD_ANYDATA_VALUETYPE value_type, struct lyd_node **node);
82
83/**
84 * @brief Find the key after which to insert the new key.
85 *
86 * @param[in] first_sibling List first sibling.
87 * @param[in] new_key Key that will be inserted.
88 * @return Key to insert after.
89 * @return NULL if the new key should be first.
90 */
91struct lyd_node *lyd_get_prev_key_anchor(const struct lyd_node *first_sibling, const struct lysc_node *new_key);
92
93/**
94 * @brief Insert a node into parent/siblings. In case a key is being inserted into a list, the correct position
95 * is found and inserted into. Otherwise it is inserted at the last place.
96 *
97 * @param[in] parent Parent to insert into, NULL if @p first_sibling is set.
98 * @param[in] first_sibling First top-level sibling, NULL if @p parent is set.
99 * @param[in] node Individual node (without siblings) to insert.
100 */
101void lyd_insert_node(struct lyd_node *parent, struct lyd_node *first_sibling, struct lyd_node *node);
102
103/**
104 * @brief Create and insert an attribute (last) into a parent.
105 *
106 * @param[in] parent Parent of the attribute.
107 * @param[in] mod Attribute module (with the annotation definition).
108 * @param[in] name Attribute name.
109 * @param[in] name_len Length of @p name.
110 * @param[in] value String value to be parsed.
111 * @param[in] value_len Length of @p value.
112 * @param[in,out] dynamic Flag if @p value is dynamically allocated, is adjusted when @p value is consumed.
113 * @param[in] get_prefix Parser-specific getter to resolve prefixes used in the @p value string.
114 * @param[in] prefix_data User data for @p get_prefix.
115 * @param[in] format Input format of @p value.
116 * @param[out] attr CReated attribute.
117 * @return LY_SUCCESS on success.
118 * @return LY_EINCOMPLETE in case data tree is needed to finish the validation.
119 * @return LY_ERR value if an error occurred.
120 */
121LY_ERR lyd_create_attr(struct lyd_node *parent, const struct lys_module *mod, const char *name, size_t name_len,
122 const char *value, size_t value_len, int *dynamic, ly_clb_resolve_prefix get_prefix, void *prefix_data,
123 LYD_FORMAT format, struct lyd_attr **attr);
124
125/**
Radek Krejci5819f7c2019-05-31 14:53:29 +0200126 * @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 +0200127 *
Radek Krejci38d85362019-09-05 16:26:38 +0200128 * @param[in] node Data node for the @p value.
Radek Krejci084289f2019-07-09 17:35:30 +0200129 * @param[in] value String value to be parsed, must not be NULL.
Radek Krejci5819f7c2019-05-31 14:53:29 +0200130 * @param[in] value_len Length of the give @p value (mandatory).
Michal Vasko90932a92020-02-12 14:33:03 +0100131 * @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 +0200132 * @param[in] second Flag for the second call after returning LY_EINCOMPLETE
Radek Krejci084289f2019-07-09 17:35:30 +0200133 * @param[in] get_prefix Parser-specific getter to resolve prefixes used in the @p value string.
Radek Krejciaca74032019-06-04 08:53:06 +0200134 * @param[in] parser Parser's data for @p get_prefix
Michal Vasko90932a92020-02-12 14:33:03 +0100135 * @param[in] format Input format of @p value.
Radek Krejcie553e6d2019-06-07 15:33:18 +0200136 * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees (e.g. when validating RPC/Notification) where the required
137 * data instance (leafref target, instance-identifier) can be placed. NULL in case the data tree are not yet complete,
138 * then LY_EINCOMPLETE can be returned.
139 * @return LY_SUCCESS on success
140 * @return LY_EINCOMPLETE in case the @p trees is not provided and it was needed to finish the validation.
141 * @return LY_ERR value if an error occurred.
Radek Krejcie7b95092019-05-15 11:03:07 +0200142 */
Michal Vasko90932a92020-02-12 14:33:03 +0100143LY_ERR lyd_value_parse(struct lyd_node_term *node, const char *value, size_t value_len, int *dynamic, int second,
Radek Krejci576b23f2019-07-12 14:06:32 +0200144 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node **trees);
Radek Krejcie7b95092019-05-15 11:03:07 +0200145
146/**
Radek Krejci38d85362019-09-05 16:26:38 +0200147 * @brief Validate, canonize and store the given @p value into the attribute according to the metadata annotation type's rules.
148 *
149 * @param[in] attr Data attribute for the @p value.
150 * @param[in] value String value to be parsed, must not be NULL.
151 * @param[in] value_len Length of the give @p value (mandatory).
Michal Vasko90932a92020-02-12 14:33:03 +0100152 * @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 +0200153 * @param[in] second Flag for the second call after returning LY_EINCOMPLETE
154 * @param[in] get_prefix Parser-specific getter to resolve prefixes used in the @p value string.
155 * @param[in] parser Parser's data for @p get_prefix
156 * @param[in] format Input format of the data.
157 * @param[in] trees ([Sized array](@ref sizedarrays)) of data trees (e.g. when validating RPC/Notification) where the required
158 * data instance (leafref target, instance-identifier) can be placed. NULL in case the data tree are not yet complete,
159 * then LY_EINCOMPLETE can be returned.
160 * @return LY_SUCCESS on success
161 * @return LY_EINCOMPLETE in case the @p trees is not provided and it was needed to finish the validation.
162 * @return LY_ERR value if an error occurred.
163 */
Michal Vasko90932a92020-02-12 14:33:03 +0100164LY_ERR lyd_value_parse_attr(struct lyd_attr *attr, const char *value, size_t value_len, int *dynamic, int second,
Radek Krejci38d85362019-09-05 16:26:38 +0200165 ly_clb_resolve_prefix get_prefix, void *parser, LYD_FORMAT format, const struct lyd_node **trees);
166
167/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200168 * @brief Parse XML string as YANG data tree.
169 *
170 * @param[in] ctx libyang context
171 * @param[in] data Pointer to the XML string representation of the YANG data to parse.
172 * @param[in] options @ref dataparseroptions
173 * @param[out] result Resulting list of the parsed data trees. Note that NULL can be a valid result.
174 * @reutn LY_ERR value.
175 */
Michal Vaskoa3881362020-01-21 15:57:35 +0100176LY_ERR lyd_parse_xml(struct ly_ctx *ctx, const char *data, int options, struct lyd_node **result);
Radek Krejcie7b95092019-05-15 11:03:07 +0200177
178/**
179 * @defgroup datahash Data nodes hash manipulation
180 * @ingroup datatree
181 */
182
183/**
Radek Krejci1f05b6a2019-07-18 16:15:06 +0200184 * @brief Generate hash for the node.
185 *
186 * @param[in] node Data node to (re)generate hash value.
187 * @return LY_ERR value.
188 */
189LY_ERR lyd_hash(struct lyd_node *node);
190
191/**
192 * @brief Insert hash of the node into the hash table of its parent.
193 *
194 * @param[in] node Data node which hash will be inserted into the lyd_node_inner::children_hash hash table of its parent.
195 * @return LY_ERR value.
196 */
197LY_ERR lyd_insert_hash(struct lyd_node *node);
198
199/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200200 * @brief Maintain node's parent's children hash table when unlinking the node.
201 *
202 * When completely freeing data tree, it is expected to free the parent's children hash table first, at once.
203 *
204 * @param[in] node The data node being unlinked from its parent.
205 */
206void lyd_unlink_hash(struct lyd_node *node);
207
208/** @} datahash */
209
Radek Krejci084289f2019-07-09 17:35:30 +0200210/**
211 * @brief Free path (target) structure of the lyd_value.
212 *
213 * @param[in] ctx libyang context.
214 * @param[in] path The structure ([sized array](@ref sizedarrays)) to free.
215 */
216void lyd_value_free_path(struct ly_ctx *ctx, struct lyd_value_path *path);
217
Radek Krejcie7b95092019-05-15 11:03:07 +0200218#endif /* LY_TREE_DATA_INTERNAL_H_ */