blob: b98f36627b0e7eba9d7364c4ed3a7b19f37c7c0b [file] [log] [blame]
Michal Vasko004d3152020-06-11 19:59:22 +02001/**
2 * @file path.h
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief Path structure and manipulation routines.
5 *
6 * Copyright (c) 2020 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_PATH_H_
16#define LY_PATH_H_
17
Radek Krejciad97c5f2020-06-30 09:19:28 +020018#include <stddef.h>
Michal Vasko004d3152020-06-11 19:59:22 +020019#include <stdint.h>
20
21#include "log.h"
Michal Vasko00cbf532020-06-15 13:58:47 +020022#include "tree.h"
Michal Vasko004d3152020-06-11 19:59:22 +020023#include "tree_data.h"
24
Radek Krejciad97c5f2020-06-30 09:19:28 +020025struct ly_ctx;
Michal Vasko405cc9e2020-12-01 12:01:27 +010026struct lys_glob_unres;
Radek Krejci0aa1f702021-04-01 16:16:19 +020027struct lys_module;
28struct lysc_ext_instance;
29struct lysc_node;
Michal Vasko004d3152020-06-11 19:59:22 +020030struct lyxp_expr;
31
32enum ly_path_pred_type {
33 LY_PATH_PREDTYPE_NONE = 0, /**< no predicate */
34 LY_PATH_PREDTYPE_POSITION, /**< position predicate - [2] */
35 LY_PATH_PREDTYPE_LIST, /**< keys predicate - [key1='val1'][key2='val2']... */
36 LY_PATH_PREDTYPE_LEAFLIST /**< leaflist value predicate - [.='value'] */
37};
38
39/**
Christian Hopps61213e02021-04-12 20:41:32 -040040 * @brief Structure for simple path predicate.
41 */
42struct ly_path_predicate {
43 union {
44 uint64_t position; /**< position value for the position-predicate */
45 struct {
46 const struct lysc_node *key; /**< key node of the predicate, NULL in
47 case of a leaf-list predicate */
48 struct lyd_value value; /**< value representation according to the
49 key's type, its realtype is allocated */
50 };
51 };
52};
53
54/**
55 * @brief Structure for holding one segment of resolved path on schema including
56 * simple predicates. Is used as a [sized array](@ref sizedarrays).
Michal Vasko004d3152020-06-11 19:59:22 +020057 */
58struct ly_path {
59 const struct lysc_node *node; /**< Schema node representing the path segment, first node has special meaning:
60 - is a top-level node - path is absolute,
61 - is inner node - path is relative */
Christian Hopps61213e02021-04-12 20:41:32 -040062 struct ly_path_predicate *predicates; /**< [Sized array](@ref sizedarrays) of the path segment's predicates */
Michal Vasko004d3152020-06-11 19:59:22 +020063 enum ly_path_pred_type pred_type; /**< Predicate type (see YANG ABNF) */
64};
65
66/**
67 * @defgroup path_begin_options Path begin options.
68 * @{
69 */
70#define LY_PATH_BEGIN_ABSOLUTE 0x01 /**< path must be absolute */
aPiecekb0445f22021-06-24 11:34:07 +020071#define LY_PATH_BEGIN_EITHER 0x02 /**< path be be either absolute or relative */
Michal Vasko004d3152020-06-11 19:59:22 +020072/** @} */
73
74/**
Michal Vasko004d3152020-06-11 19:59:22 +020075 * @defgroup path_prefix_options Path prefix options.
76 * @{
77 */
78#define LY_PATH_PREFIX_OPTIONAL 0x10 /**< prefixes in the path are optional */
Michal Vasko8b06a5e2020-08-06 12:13:08 +020079#define LY_PATH_PREFIX_MANDATORY 0x20 /**< prefixes in the path are mandatory (XML instance-identifier) */
80#define LY_PATH_PREFIX_STRICT_INHERIT 0x30 /**< prefixes in the path are mandatory in case they differ from the
81 previous prefixes, otherwise they are prohibited (JSON instance-identifier) */
Michal Vasko004d3152020-06-11 19:59:22 +020082/** @} */
83
84/**
85 * @defgroup path_pred_options Path predicate options.
86 * @{
87 */
88#define LY_PATH_PRED_KEYS 0x40 /* expected predicate only - [node='value']* */
89#define LY_PATH_PRED_SIMPLE 0x80 /* expected predicates - [node='value']*; [.='value']; [1] */
90#define LY_PATH_PRED_LEAFREF 0xC0 /* expected predicates only leafref - [node=current()/../../../node/node];
91 at least 1 ".." and 1 "node" after */
92/** @} */
93
94/**
95 * @brief Parse path into XPath token structure and perform all additional checks.
96 *
97 * @param[in] ctx libyang context.
Michal Vaskoaef00cb2020-10-16 11:58:57 +020098 * @param[in] ctx_node Optional context node, used for logging.
Michal Vasko004d3152020-06-11 19:59:22 +020099 * @param[in] str_path Path to parse.
100 * @param[in] path_len Length of @p str_path.
Michal Vaskoed725d72021-06-23 12:03:45 +0200101 * @param[in] lref Whether leafref is being parsed or not.
Michal Vasko004d3152020-06-11 19:59:22 +0200102 * @param[in] begin Begin option (@ref path_begin_options).
Michal Vasko004d3152020-06-11 19:59:22 +0200103 * @param[in] prefix Prefix option (@ref path_prefix_options).
104 * @param[in] pred Predicate option (@ref path_pred_options).
105 * @param[out] expr Parsed path.
106 * @return LY_ERR value.
107 */
Michal Vasko6b26e742020-07-17 15:02:10 +0200108LY_ERR ly_path_parse(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *str_path, size_t path_len,
Michal Vaskoed725d72021-06-23 12:03:45 +0200109 ly_bool lref, uint8_t begin, uint8_t prefix, uint8_t pred, struct lyxp_expr **expr);
Michal Vasko004d3152020-06-11 19:59:22 +0200110
111/**
112 * @brief Parse predicate into XPath token structure and perform all additional checks.
113 *
114 * @param[in] ctx libyang context.
Michal Vaskoaef00cb2020-10-16 11:58:57 +0200115 * @param[in] cur_node Optional current (original context) node, used for logging.
Michal Vasko004d3152020-06-11 19:59:22 +0200116 * @param[in] str_path Path to parse.
117 * @param[in] path_len Length of @p str_path.
118 * @param[in] prefix Prefix option (@ref path_prefix_options).
119 * @param[in] pred Predicate option (@ref path_pred_options).
120 * @param[out] expr Parsed path.
121 * @return LY_ERR value.
122 */
Michal Vasko6b26e742020-07-17 15:02:10 +0200123LY_ERR ly_path_parse_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const char *str_path,
Radek Krejci0f969882020-08-21 16:56:47 +0200124 size_t path_len, uint8_t prefix, uint8_t pred, struct lyxp_expr **expr);
Michal Vasko004d3152020-06-11 19:59:22 +0200125
126/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200127 * @defgroup path_oper_options Path operation options.
128 * @{
129 */
130#define LY_PATH_OPER_INPUT 0x01 /**< if any RPC/action is traversed, its input nodes are used */
131#define LY_PATH_OPER_OUTPUT 0x02 /**< if any RPC/action is traversed, its output nodes are used */
132/** @} */
133
134/* lref */
135
136/**
137 * @defgroup path_target_options Path target options.
138 * @{
139 */
140#define LY_PATH_TARGET_SINGLE 0x10 /**< last (target) node must identify an exact instance */
141#define LY_PATH_TARGET_MANY 0x20 /**< last (target) node may identify all instances (of leaf-list/list) */
142/** @} */
143
144/**
Michal Vaskoed725d72021-06-23 12:03:45 +0200145 * @brief Compile path into ly_path structure.
Michal Vasko004d3152020-06-11 19:59:22 +0200146 *
Michal Vasko00cbf532020-06-15 13:58:47 +0200147 * @param[in] ctx libyang context.
Michal Vaskoed725d72021-06-23 12:03:45 +0200148 * @param[in] cur_mod Current module of the path (where it was "instantiated"). Used for nodes in schema-nodeid
Radek Krejci84d7fd72021-07-14 18:32:21 +0200149 * without a prefix for ::LY_VALUE_SCHEMA and ::LY_VALUE_SCHEMA_RESOLVED format.
Michal Vasko6b26e742020-07-17 15:02:10 +0200150 * @param[in] ctx_node Optional context node.
Radek Krejcid5d37432021-03-12 13:46:40 +0100151 * @param[in] ext Extension instance containing the definition of the data being created. It is used to find the top-level
152 * node inside the extension instance instead of a module. Note that this is the case not only if the @p ctx_node is NULL,
153 * but also if the relative path starting in @p ctx_node reaches the document root via double dots.
Michal Vasko004d3152020-06-11 19:59:22 +0200154 * @param[in] expr Parsed path.
Michal Vasko00cbf532020-06-15 13:58:47 +0200155 * @param[in] oper Oper option (@ref path_oper_options).
156 * @param[in] target Target option (@ref path_target_options).
Michal Vasko004d3152020-06-11 19:59:22 +0200157 * @param[in] format Format of the path.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200158 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +0200159 * @param[out] path Compiled path.
160 * @return LY_ERR value.
161 */
Michal Vasko00cbf532020-06-15 13:58:47 +0200162LY_ERR ly_path_compile(const struct ly_ctx *ctx, const struct lys_module *cur_mod, const struct lysc_node *ctx_node,
Michal Vaskoed725d72021-06-23 12:03:45 +0200163 const struct lysc_ext_instance *ext, const struct lyxp_expr *expr, uint8_t oper, uint8_t target,
164 LY_VALUE_FORMAT format, void *prefix_data, struct ly_path **path);
165
166/**
167 * @brief Compile path into ly_path structure. Any predicates of a leafref are only checked, not compiled.
168 *
169 * @param[in] ctx libyang context.
170 * @param[in] ctx_node Context node.
171 * @param[in] ext Extension instance containing the definition of the data being created. It is used to find the top-level
172 * node inside the extension instance instead of a module. Note that this is the case not only if the @p ctx_node is NULL,
173 * but also if the relative path starting in @p ctx_node reaches the document root via double dots.
174 * @param[in] expr Parsed path.
175 * @param[in] oper Oper option (@ref path_oper_options).
176 * @param[in] target Target option (@ref path_target_options).
177 * @param[in] format Format of the path.
178 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vaskoed725d72021-06-23 12:03:45 +0200179 * @param[out] path Compiled path.
Michal Vaskoed725d72021-06-23 12:03:45 +0200180 * @return LY_ERR value.
181 */
182LY_ERR ly_path_compile_leafref(const struct ly_ctx *ctx, const struct lysc_node *ctx_node,
183 const struct lysc_ext_instance *ext, const struct lyxp_expr *expr, uint8_t oper, uint8_t target,
Michal Vasko24fc4d12021-07-12 14:41:20 +0200184 LY_VALUE_FORMAT format, void *prefix_data, struct ly_path **path);
Michal Vasko004d3152020-06-11 19:59:22 +0200185
186/**
187 * @brief Compile predicate into ly_path_predicate structure. Only simple predicates (not leafref) are supported.
188 *
Michal Vasko00cbf532020-06-15 13:58:47 +0200189 * @param[in] ctx libyang context.
Michal Vasko6b26e742020-07-17 15:02:10 +0200190 * @param[in] cur_node Optional current (original context) node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200191 * @param[in] cur_mod Current module of the path (where it was "instantiated"). Used for nodes without a prefix
Radek Krejci84d7fd72021-07-14 18:32:21 +0200192 * for ::LY_VALUE_SCHEMA and ::LY_VALUE_SCHEMA_RESOLVED format.
Michal Vasko004d3152020-06-11 19:59:22 +0200193 * @param[in] ctx_node Context node, node for which the predicate is defined.
194 * @param[in] expr Parsed path.
195 * @param[in,out] tok_idx Index in @p expr, is adjusted for parsed tokens.
Michal Vasko004d3152020-06-11 19:59:22 +0200196 * @param[in] format Format of the path.
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200197 * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +0200198 * @param[out] predicates Compiled predicates.
199 * @param[out] pred_type Type of the compiled predicate(s).
200 * @return LY_ERR value.
201 */
Michal Vasko6b26e742020-07-17 15:02:10 +0200202LY_ERR ly_path_compile_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +0200203 const struct lysc_node *ctx_node, const struct lyxp_expr *expr, uint16_t *tok_idx, LY_VALUE_FORMAT format,
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200204 void *prefix_data, struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type);
Michal Vasko00cbf532020-06-15 13:58:47 +0200205
206/**
207 * @brief Resolve at least partially the target defined by ly_path structure. Not supported for leafref!
208 *
209 * @param[in] path Path structure specifying the target.
210 * @param[in] start Starting node for relative paths, can be any for absolute paths.
211 * @param[out] path_idx Last found path segment index, can be NULL, set to 0 if not found.
212 * @param[out] match Last found matching node, can be NULL, set to NULL if not found.
213 * @return LY_ENOTFOUND if no nodes were found,
214 * @return LY_EINCOMPLETE if some node was found but not the last one,
215 * @return LY_SUCCESS when the last node in the path was found,
216 * @return LY_ERR on another error.
217 */
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200218LY_ERR ly_path_eval_partial(const struct ly_path *path, const struct lyd_node *start, LY_ARRAY_COUNT_TYPE *path_idx,
Radek Krejci0f969882020-08-21 16:56:47 +0200219 struct lyd_node **match);
Michal Vasko004d3152020-06-11 19:59:22 +0200220
221/**
222 * @brief Resolve the target defined by ly_path structure. Not supported for leafref!
223 *
224 * @param[in] path Path structure specifying the target.
225 * @param[in] start Starting node for relative paths, can be any for absolute paths.
226 * @param[out] match Found matching node, can be NULL, set to NULL if not found.
227 * @return LY_ENOTFOUND if no nodes were found,
228 * @return LY_SUCCESS when the last node in the path was found,
229 * @return LY_ERR on another error.
230 */
231LY_ERR ly_path_eval(const struct ly_path *path, const struct lyd_node *start, struct lyd_node **match);
232
233/**
234 * @brief Duplicate ly_path structure.
235 *
236 * @param[in] ctx libyang context.
237 * @param[in] path Path to duplicate.
238 * @param[out] dup Duplicated path.
239 * @return LY_ERR value.
240 */
241LY_ERR ly_path_dup(const struct ly_ctx *ctx, const struct ly_path *path, struct ly_path **dup);
242
243/**
244 * @brief Free ly_path_predicate structure.
245 *
246 * @param[in] ctx libyang context.
247 * @param[in] pred_type Predicate type.
Michal Vasko004d3152020-06-11 19:59:22 +0200248 * @param[in] predicates Predicates ([sized array](@ref sizedarrays)) to free.
249 */
Michal Vaskof7e16e22020-10-21 09:24:39 +0200250void ly_path_predicates_free(const struct ly_ctx *ctx, enum ly_path_pred_type pred_type,
Radek Krejci0f969882020-08-21 16:56:47 +0200251 struct ly_path_predicate *predicates);
Michal Vasko004d3152020-06-11 19:59:22 +0200252
253/**
254 * @brief Free ly_path structure.
255 *
256 * @param[in] ctx libyang context.
257 * @param[in] path The structure ([sized array](@ref sizedarrays)) to free.
258 */
259void ly_path_free(const struct ly_ctx *ctx, struct ly_path *path);
260
261#endif /* LY_PATH_H_ */