Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1 | /** |
| 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 Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 18 | #include <stddef.h> |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | |
| 21 | #include "log.h" |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 22 | #include "tree.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 23 | #include "tree_data.h" |
| 24 | |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 25 | struct ly_ctx; |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 26 | struct lys_glob_unres; |
Radek Krejci | 0aa1f70 | 2021-04-01 16:16:19 +0200 | [diff] [blame] | 27 | struct lys_module; |
| 28 | struct lysc_ext_instance; |
| 29 | struct lysc_node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 30 | struct lyxp_expr; |
| 31 | |
| 32 | enum 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 Hopps | 61213e0 | 2021-04-12 20:41:32 -0400 | [diff] [blame] | 40 | * @brief Structure for simple path predicate. |
| 41 | */ |
| 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 |
| 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 Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 57 | */ |
| 58 | struct 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 Hopps | 61213e0 | 2021-04-12 20:41:32 -0400 | [diff] [blame] | 62 | struct ly_path_predicate *predicates; /**< [Sized array](@ref sizedarrays) of the path segment's predicates */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 63 | 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 */ |
| 71 | #define LY_PATH_BEGIN_EITHER 0x02 /**< path be be iether absolute or relative */ |
| 72 | /** @} */ |
| 73 | |
| 74 | /** |
| 75 | * @defgroup path_lref_options Path leafref options. |
| 76 | * @{ |
| 77 | */ |
| 78 | #define LY_PATH_LREF_FALSE 0x04 /**< path does not represent leafref */ |
| 79 | #define LY_PATH_LREF_TRUE 0x08 /* '..' in path allowed, special leafref predicates expected (but are not compiled), |
| 80 | implement traversed modules */ |
| 81 | /** @} */ |
| 82 | |
| 83 | /** |
| 84 | * @defgroup path_prefix_options Path prefix options. |
| 85 | * @{ |
| 86 | */ |
| 87 | #define LY_PATH_PREFIX_OPTIONAL 0x10 /**< prefixes in the path are optional */ |
Michal Vasko | 8b06a5e | 2020-08-06 12:13:08 +0200 | [diff] [blame] | 88 | #define LY_PATH_PREFIX_MANDATORY 0x20 /**< prefixes in the path are mandatory (XML instance-identifier) */ |
| 89 | #define LY_PATH_PREFIX_STRICT_INHERIT 0x30 /**< prefixes in the path are mandatory in case they differ from the |
| 90 | previous prefixes, otherwise they are prohibited (JSON instance-identifier) */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 91 | /** @} */ |
| 92 | |
| 93 | /** |
| 94 | * @defgroup path_pred_options Path predicate options. |
| 95 | * @{ |
| 96 | */ |
| 97 | #define LY_PATH_PRED_KEYS 0x40 /* expected predicate only - [node='value']* */ |
| 98 | #define LY_PATH_PRED_SIMPLE 0x80 /* expected predicates - [node='value']*; [.='value']; [1] */ |
| 99 | #define LY_PATH_PRED_LEAFREF 0xC0 /* expected predicates only leafref - [node=current()/../../../node/node]; |
| 100 | at least 1 ".." and 1 "node" after */ |
| 101 | /** @} */ |
| 102 | |
| 103 | /** |
| 104 | * @brief Parse path into XPath token structure and perform all additional checks. |
| 105 | * |
| 106 | * @param[in] ctx libyang context. |
Michal Vasko | aef00cb | 2020-10-16 11:58:57 +0200 | [diff] [blame] | 107 | * @param[in] ctx_node Optional context node, used for logging. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 108 | * @param[in] str_path Path to parse. |
| 109 | * @param[in] path_len Length of @p str_path. |
| 110 | * @param[in] begin Begin option (@ref path_begin_options). |
| 111 | * @param[in] lref Lref option (@ref path_lref_options). |
| 112 | * @param[in] prefix Prefix option (@ref path_prefix_options). |
| 113 | * @param[in] pred Predicate option (@ref path_pred_options). |
| 114 | * @param[out] expr Parsed path. |
| 115 | * @return LY_ERR value. |
| 116 | */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 117 | LY_ERR ly_path_parse(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *str_path, size_t path_len, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 118 | uint8_t begin, uint8_t lref, uint8_t prefix, uint8_t pred, struct lyxp_expr **expr); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * @brief Parse predicate into XPath token structure and perform all additional checks. |
| 122 | * |
| 123 | * @param[in] ctx libyang context. |
Michal Vasko | aef00cb | 2020-10-16 11:58:57 +0200 | [diff] [blame] | 124 | * @param[in] cur_node Optional current (original context) node, used for logging. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 125 | * @param[in] str_path Path to parse. |
| 126 | * @param[in] path_len Length of @p str_path. |
| 127 | * @param[in] prefix Prefix option (@ref path_prefix_options). |
| 128 | * @param[in] pred Predicate option (@ref path_pred_options). |
| 129 | * @param[out] expr Parsed path. |
| 130 | * @return LY_ERR value. |
| 131 | */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 132 | LY_ERR ly_path_parse_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const char *str_path, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 133 | size_t path_len, uint8_t prefix, uint8_t pred, struct lyxp_expr **expr); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 134 | |
| 135 | /** |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 136 | * @defgroup path_oper_options Path operation options. |
| 137 | * @{ |
| 138 | */ |
| 139 | #define LY_PATH_OPER_INPUT 0x01 /**< if any RPC/action is traversed, its input nodes are used */ |
| 140 | #define LY_PATH_OPER_OUTPUT 0x02 /**< if any RPC/action is traversed, its output nodes are used */ |
| 141 | /** @} */ |
| 142 | |
| 143 | /* lref */ |
| 144 | |
| 145 | /** |
| 146 | * @defgroup path_target_options Path target options. |
| 147 | * @{ |
| 148 | */ |
| 149 | #define LY_PATH_TARGET_SINGLE 0x10 /**< last (target) node must identify an exact instance */ |
| 150 | #define LY_PATH_TARGET_MANY 0x20 /**< last (target) node may identify all instances (of leaf-list/list) */ |
| 151 | /** @} */ |
| 152 | |
| 153 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 154 | * @brief Compile path into ly_path structure. Any predicates of a leafref are only checked, not compiled. |
| 155 | * |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 156 | * @param[in] ctx libyang context. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 157 | * @param[in] cur_mod Current module of the path (where it was "instantiated"). Used for nodes without a prefix |
| 158 | * for ::LY_PREF_SCHEMA* format. |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 159 | * @param[in] ctx_node Optional context node. |
Radek Krejci | d5d3743 | 2021-03-12 13:46:40 +0100 | [diff] [blame] | 160 | * @param[in] ext Extension instance containing the definition of the data being created. It is used to find the top-level |
| 161 | * node inside the extension instance instead of a module. Note that this is the case not only if the @p ctx_node is NULL, |
| 162 | * but also if the relative path starting in @p ctx_node reaches the document root via double dots. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 163 | * @param[in] expr Parsed path. |
| 164 | * @param[in] lref Lref option (@ref path_lref_options). |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 165 | * @param[in] oper Oper option (@ref path_oper_options). |
| 166 | * @param[in] target Target option (@ref path_target_options). |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 167 | * @param[in] format Format of the path. |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 168 | * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix). |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 169 | * @param[in,out] unres Global unres structure for newly implemented modules, needed only if @p lref is ::LY_PATH_LREF_TRUE. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 170 | * @param[out] path Compiled path. |
| 171 | * @return LY_ERR value. |
| 172 | */ |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 173 | LY_ERR ly_path_compile(const struct ly_ctx *ctx, const struct lys_module *cur_mod, const struct lysc_node *ctx_node, |
Radek Krejci | d5d3743 | 2021-03-12 13:46:40 +0100 | [diff] [blame] | 174 | const struct lysc_ext_instance *ext, const struct lyxp_expr *expr, uint8_t lref, uint8_t oper, uint8_t target, |
| 175 | LY_PREFIX_FORMAT format, void *prefix_data, struct lys_glob_unres *unres, struct ly_path **path); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 176 | |
| 177 | /** |
| 178 | * @brief Compile predicate into ly_path_predicate structure. Only simple predicates (not leafref) are supported. |
| 179 | * |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 180 | * @param[in] ctx libyang context. |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 181 | * @param[in] cur_node Optional current (original context) node. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 182 | * @param[in] cur_mod Current module of the path (where it was "instantiated"). Used for nodes without a prefix |
| 183 | * for ::LY_PREF_SCHEMA* format. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 184 | * @param[in] ctx_node Context node, node for which the predicate is defined. |
| 185 | * @param[in] expr Parsed path. |
| 186 | * @param[in,out] tok_idx Index in @p expr, is adjusted for parsed tokens. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 187 | * @param[in] format Format of the path. |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 188 | * @param[in] prefix_data Format-specific data for resolving any prefixes (see ::ly_resolve_prefix). |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 189 | * @param[out] predicates Compiled predicates. |
| 190 | * @param[out] pred_type Type of the compiled predicate(s). |
| 191 | * @return LY_ERR value. |
| 192 | */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 193 | LY_ERR ly_path_compile_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_node, const struct lys_module *cur_mod, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 194 | const struct lysc_node *ctx_node, const struct lyxp_expr *expr, uint16_t *tok_idx, LY_PREFIX_FORMAT format, |
| 195 | void *prefix_data, struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 196 | |
| 197 | /** |
| 198 | * @brief Resolve at least partially the target defined by ly_path structure. Not supported for leafref! |
| 199 | * |
| 200 | * @param[in] path Path structure specifying the target. |
| 201 | * @param[in] start Starting node for relative paths, can be any for absolute paths. |
| 202 | * @param[out] path_idx Last found path segment index, can be NULL, set to 0 if not found. |
| 203 | * @param[out] match Last found matching node, can be NULL, set to NULL if not found. |
| 204 | * @return LY_ENOTFOUND if no nodes were found, |
| 205 | * @return LY_EINCOMPLETE if some node was found but not the last one, |
| 206 | * @return LY_SUCCESS when the last node in the path was found, |
| 207 | * @return LY_ERR on another error. |
| 208 | */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 209 | LY_ERR ly_path_eval_partial(const struct ly_path *path, const struct lyd_node *start, LY_ARRAY_COUNT_TYPE *path_idx, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 210 | struct lyd_node **match); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 211 | |
| 212 | /** |
| 213 | * @brief Resolve the target defined by ly_path structure. Not supported for leafref! |
| 214 | * |
| 215 | * @param[in] path Path structure specifying the target. |
| 216 | * @param[in] start Starting node for relative paths, can be any for absolute paths. |
| 217 | * @param[out] match Found matching node, can be NULL, set to NULL if not found. |
| 218 | * @return LY_ENOTFOUND if no nodes were found, |
| 219 | * @return LY_SUCCESS when the last node in the path was found, |
| 220 | * @return LY_ERR on another error. |
| 221 | */ |
| 222 | LY_ERR ly_path_eval(const struct ly_path *path, const struct lyd_node *start, struct lyd_node **match); |
| 223 | |
| 224 | /** |
| 225 | * @brief Duplicate ly_path structure. |
| 226 | * |
| 227 | * @param[in] ctx libyang context. |
| 228 | * @param[in] path Path to duplicate. |
| 229 | * @param[out] dup Duplicated path. |
| 230 | * @return LY_ERR value. |
| 231 | */ |
| 232 | LY_ERR ly_path_dup(const struct ly_ctx *ctx, const struct ly_path *path, struct ly_path **dup); |
| 233 | |
| 234 | /** |
| 235 | * @brief Free ly_path_predicate structure. |
| 236 | * |
| 237 | * @param[in] ctx libyang context. |
| 238 | * @param[in] pred_type Predicate type. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 239 | * @param[in] predicates Predicates ([sized array](@ref sizedarrays)) to free. |
| 240 | */ |
Michal Vasko | f7e16e2 | 2020-10-21 09:24:39 +0200 | [diff] [blame] | 241 | void ly_path_predicates_free(const struct ly_ctx *ctx, enum ly_path_pred_type pred_type, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 242 | struct ly_path_predicate *predicates); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 243 | |
| 244 | /** |
| 245 | * @brief Free ly_path structure. |
| 246 | * |
| 247 | * @param[in] ctx libyang context. |
| 248 | * @param[in] path The structure ([sized array](@ref sizedarrays)) to free. |
| 249 | */ |
| 250 | void ly_path_free(const struct ly_ctx *ctx, struct ly_path *path); |
| 251 | |
| 252 | #endif /* LY_PATH_H_ */ |