blob: 2e32f91a8fd7cb41011248f3904a79eb04e5da34 [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;
26struct lys_module;
Michal Vasko004d3152020-06-11 19:59:22 +020027struct lysc_node;
28struct lyxp_expr;
29
30enum ly_path_pred_type {
31 LY_PATH_PREDTYPE_NONE = 0, /**< no predicate */
32 LY_PATH_PREDTYPE_POSITION, /**< position predicate - [2] */
33 LY_PATH_PREDTYPE_LIST, /**< keys predicate - [key1='val1'][key2='val2']... */
34 LY_PATH_PREDTYPE_LEAFLIST /**< leaflist value predicate - [.='value'] */
35};
36
37/**
38 * @brief Structure for holding one segment of resolved path on schema including simple predicates.
39 * Is used as a [sized array](@ref sizedarrays).
40 */
41struct ly_path {
42 const struct lysc_node *node; /**< Schema node representing the path segment, first node has special meaning:
43 - is a top-level node - path is absolute,
44 - is inner node - path is relative */
45 struct ly_path_predicate {
46 union {
47 uint64_t position; /**< position value for the position-predicate */
48 struct {
49 const struct lysc_node *key; /**< key node of the predicate, NULL in case of a leaf-list predicate */
50 struct lyd_value value; /**< value representation according to the key's type */
51 };
52 };
53 } *predicates; /**< [Sized array](@ref sizedarrays) of the path segment's predicates */
54 enum ly_path_pred_type pred_type; /**< Predicate type (see YANG ABNF) */
55};
56
57/**
58 * @defgroup path_begin_options Path begin options.
59 * @{
60 */
61#define LY_PATH_BEGIN_ABSOLUTE 0x01 /**< path must be absolute */
62#define LY_PATH_BEGIN_EITHER 0x02 /**< path be be iether absolute or relative */
63/** @} */
64
65/**
66 * @defgroup path_lref_options Path leafref options.
67 * @{
68 */
69#define LY_PATH_LREF_FALSE 0x04 /**< path does not represent leafref */
70#define LY_PATH_LREF_TRUE 0x08 /* '..' in path allowed, special leafref predicates expected (but are not compiled),
71 implement traversed modules */
72/** @} */
73
74/**
75 * @defgroup path_prefix_options Path prefix options.
76 * @{
77 */
78#define LY_PATH_PREFIX_OPTIONAL 0x10 /**< prefixes in the path are optional */
79#define LY_PATH_PREFIX_MANDATORY 0x20 /**< prefixes in the path are mandatory (XML insatnce-identifier) */
80/** @} */
81
82/**
83 * @defgroup path_pred_options Path predicate options.
84 * @{
85 */
86#define LY_PATH_PRED_KEYS 0x40 /* expected predicate only - [node='value']* */
87#define LY_PATH_PRED_SIMPLE 0x80 /* expected predicates - [node='value']*; [.='value']; [1] */
88#define LY_PATH_PRED_LEAFREF 0xC0 /* expected predicates only leafref - [node=current()/../../../node/node];
89 at least 1 ".." and 1 "node" after */
90/** @} */
91
92/**
93 * @brief Parse path into XPath token structure and perform all additional checks.
94 *
95 * @param[in] ctx libyang context.
Michal Vasko6b26e742020-07-17 15:02:10 +020096 * @param[in] ctx_node Optional context node.
Michal Vasko004d3152020-06-11 19:59:22 +020097 * @param[in] str_path Path to parse.
98 * @param[in] path_len Length of @p str_path.
99 * @param[in] begin Begin option (@ref path_begin_options).
100 * @param[in] lref Lref option (@ref path_lref_options).
101 * @param[in] prefix Prefix option (@ref path_prefix_options).
102 * @param[in] pred Predicate option (@ref path_pred_options).
103 * @param[out] expr Parsed path.
104 * @return LY_ERR value.
105 */
Michal Vasko6b26e742020-07-17 15:02:10 +0200106LY_ERR ly_path_parse(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *str_path, size_t path_len,
107 uint8_t begin, uint8_t lref, uint8_t prefix, uint8_t pred, struct lyxp_expr **expr);
Michal Vasko004d3152020-06-11 19:59:22 +0200108
109/**
110 * @brief Parse predicate into XPath token structure and perform all additional checks.
111 *
112 * @param[in] ctx libyang context.
Michal Vasko6b26e742020-07-17 15:02:10 +0200113 * @param[in] cur_node Optional current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +0200114 * @param[in] str_path Path to parse.
115 * @param[in] path_len Length of @p str_path.
116 * @param[in] prefix Prefix option (@ref path_prefix_options).
117 * @param[in] pred Predicate option (@ref path_pred_options).
118 * @param[out] expr Parsed path.
119 * @return LY_ERR value.
120 */
Michal Vasko6b26e742020-07-17 15:02:10 +0200121LY_ERR ly_path_parse_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const char *str_path,
122 size_t path_len, uint8_t prefix, uint8_t pred, struct lyxp_expr **expr);
Michal Vasko004d3152020-06-11 19:59:22 +0200123
124/**
Michal Vasko00cbf532020-06-15 13:58:47 +0200125 * @defgroup path_oper_options Path operation options.
126 * @{
127 */
128#define LY_PATH_OPER_INPUT 0x01 /**< if any RPC/action is traversed, its input nodes are used */
129#define LY_PATH_OPER_OUTPUT 0x02 /**< if any RPC/action is traversed, its output nodes are used */
130/** @} */
131
132/* lref */
133
134/**
135 * @defgroup path_target_options Path target options.
136 * @{
137 */
138#define LY_PATH_TARGET_SINGLE 0x10 /**< last (target) node must identify an exact instance */
139#define LY_PATH_TARGET_MANY 0x20 /**< last (target) node may identify all instances (of leaf-list/list) */
140/** @} */
141
142/**
Michal Vasko004d3152020-06-11 19:59:22 +0200143 * @brief Compile path into ly_path structure. Any predicates of a leafref are only checked, not compiled.
144 *
Michal Vasko00cbf532020-06-15 13:58:47 +0200145 * @param[in] ctx libyang context.
Michal Vasko004d3152020-06-11 19:59:22 +0200146 * @param[in] cur_mod Module of the current (original context) node. Used for nodes without prefix for ::LYD_SCHEMA format.
Michal Vasko6b26e742020-07-17 15:02:10 +0200147 * @param[in] ctx_node Optional context node.
Michal Vasko004d3152020-06-11 19:59:22 +0200148 * @param[in] expr Parsed path.
149 * @param[in] lref Lref option (@ref path_lref_options).
Michal Vasko00cbf532020-06-15 13:58:47 +0200150 * @param[in] oper Oper option (@ref path_oper_options).
151 * @param[in] target Target option (@ref path_target_options).
Michal Vasko004d3152020-06-11 19:59:22 +0200152 * @param[in] resolve_prefix Callback for prefix resolution.
153 * @param[in] prefix_data Data for @p resolve_prefix.
154 * @param[in] format Format of the path.
155 * @param[out] path Compiled path.
156 * @return LY_ERR value.
157 */
Michal Vasko00cbf532020-06-15 13:58:47 +0200158LY_ERR ly_path_compile(const struct ly_ctx *ctx, const struct lys_module *cur_mod, const struct lysc_node *ctx_node,
159 const struct lyxp_expr *expr, uint8_t lref, uint8_t oper, uint8_t target,
160 ly_clb_resolve_prefix resolve_prefix, void *prefix_data, LYD_FORMAT format, struct ly_path **path);
Michal Vasko004d3152020-06-11 19:59:22 +0200161
162/**
163 * @brief Compile predicate into ly_path_predicate structure. Only simple predicates (not leafref) are supported.
164 *
Michal Vasko00cbf532020-06-15 13:58:47 +0200165 * @param[in] ctx libyang context.
Michal Vasko6b26e742020-07-17 15:02:10 +0200166 * @param[in] cur_node Optional current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +0200167 * @param[in] cur_mod Module of the current (original context) node. Used for nodes without prefix for ::LYD_SCHEMA format.
168 * @param[in] ctx_node Context node, node for which the predicate is defined.
169 * @param[in] expr Parsed path.
170 * @param[in,out] tok_idx Index in @p expr, is adjusted for parsed tokens.
171 * @param[in] resolve_prefix Callback for prefix resolution.
172 * @param[in] prefix_data Data for @p resolve_prefix.
173 * @param[in] format Format of the path.
174 * @param[out] predicates Compiled predicates.
175 * @param[out] pred_type Type of the compiled predicate(s).
176 * @return LY_ERR value.
177 */
Michal Vasko6b26e742020-07-17 15:02:10 +0200178LY_ERR ly_path_compile_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const struct lys_module *cur_mod,
Michal Vasko00cbf532020-06-15 13:58:47 +0200179 const struct lysc_node *ctx_node, const struct lyxp_expr *expr, uint16_t *tok_idx,
180 ly_clb_resolve_prefix resolve_prefix, void *prefix_data, LYD_FORMAT format,
181 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type);
182
183/**
184 * @brief Resolve at least partially the target defined by ly_path structure. Not supported for leafref!
185 *
186 * @param[in] path Path structure specifying the target.
187 * @param[in] start Starting node for relative paths, can be any for absolute paths.
188 * @param[out] path_idx Last found path segment index, can be NULL, set to 0 if not found.
189 * @param[out] match Last found matching node, can be NULL, set to NULL if not found.
190 * @return LY_ENOTFOUND if no nodes were found,
191 * @return LY_EINCOMPLETE if some node was found but not the last one,
192 * @return LY_SUCCESS when the last node in the path was found,
193 * @return LY_ERR on another error.
194 */
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200195LY_ERR ly_path_eval_partial(const struct ly_path *path, const struct lyd_node *start, LY_ARRAY_COUNT_TYPE *path_idx,
Michal Vasko00cbf532020-06-15 13:58:47 +0200196 struct lyd_node **match);
Michal Vasko004d3152020-06-11 19:59:22 +0200197
198/**
199 * @brief Resolve the target defined by ly_path structure. Not supported for leafref!
200 *
201 * @param[in] path Path structure specifying the target.
202 * @param[in] start Starting node for relative paths, can be any for absolute paths.
203 * @param[out] match Found matching node, can be NULL, set to NULL if not found.
204 * @return LY_ENOTFOUND if no nodes were found,
205 * @return LY_SUCCESS when the last node in the path was found,
206 * @return LY_ERR on another error.
207 */
208LY_ERR ly_path_eval(const struct ly_path *path, const struct lyd_node *start, struct lyd_node **match);
209
210/**
211 * @brief Duplicate ly_path structure.
212 *
213 * @param[in] ctx libyang context.
214 * @param[in] path Path to duplicate.
215 * @param[out] dup Duplicated path.
216 * @return LY_ERR value.
217 */
218LY_ERR ly_path_dup(const struct ly_ctx *ctx, const struct ly_path *path, struct ly_path **dup);
219
220/**
221 * @brief Free ly_path_predicate structure.
222 *
223 * @param[in] ctx libyang context.
224 * @param[in] pred_type Predicate type.
225 * @param[in] llist Leaf-list in case of leaf-list predicate.
226 * @param[in] predicates Predicates ([sized array](@ref sizedarrays)) to free.
227 */
228void ly_path_predicates_free(const struct ly_ctx *ctx, enum ly_path_pred_type pred_type, const struct lysc_node *llist,
229 struct ly_path_predicate *predicates);
230
231/**
232 * @brief Free ly_path structure.
233 *
234 * @param[in] ctx libyang context.
235 * @param[in] path The structure ([sized array](@ref sizedarrays)) to free.
236 */
237void ly_path_free(const struct ly_ctx *ctx, struct ly_path *path);
238
239#endif /* LY_PATH_H_ */