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