blob: c973add5f60b54467c69eff0849da1ab89f3e90a [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"
21#include "tree_data.h"
22
23struct lysc_node;
24struct lyxp_expr;
25
26enum ly_path_pred_type {
27 LY_PATH_PREDTYPE_NONE = 0, /**< no predicate */
28 LY_PATH_PREDTYPE_POSITION, /**< position predicate - [2] */
29 LY_PATH_PREDTYPE_LIST, /**< keys predicate - [key1='val1'][key2='val2']... */
30 LY_PATH_PREDTYPE_LEAFLIST /**< leaflist value predicate - [.='value'] */
31};
32
33/**
34 * @brief Structure for holding one segment of resolved path on schema including simple predicates.
35 * Is used as a [sized array](@ref sizedarrays).
36 */
37struct ly_path {
38 const struct lysc_node *node; /**< Schema node representing the path segment, first node has special meaning:
39 - is a top-level node - path is absolute,
40 - is inner node - path is relative */
41 struct ly_path_predicate {
42 union {
43 uint64_t position; /**< position value for the position-predicate */
44 struct {
45 const struct lysc_node *key; /**< key node of the predicate, NULL in case of a leaf-list predicate */
46 struct lyd_value value; /**< value representation according to the key's type */
47 };
48 };
49 } *predicates; /**< [Sized array](@ref sizedarrays) of the path segment's predicates */
50 enum ly_path_pred_type pred_type; /**< Predicate type (see YANG ABNF) */
51};
52
53/**
54 * @defgroup path_begin_options Path begin options.
55 * @{
56 */
57#define LY_PATH_BEGIN_ABSOLUTE 0x01 /**< path must be absolute */
58#define LY_PATH_BEGIN_EITHER 0x02 /**< path be be iether absolute or relative */
59/** @} */
60
61/**
62 * @defgroup path_lref_options Path leafref options.
63 * @{
64 */
65#define LY_PATH_LREF_FALSE 0x04 /**< path does not represent leafref */
66#define LY_PATH_LREF_TRUE 0x08 /* '..' in path allowed, special leafref predicates expected (but are not compiled),
67 implement traversed modules */
68/** @} */
69
70/**
71 * @defgroup path_prefix_options Path prefix options.
72 * @{
73 */
74#define LY_PATH_PREFIX_OPTIONAL 0x10 /**< prefixes in the path are optional */
75#define LY_PATH_PREFIX_MANDATORY 0x20 /**< prefixes in the path are mandatory (XML insatnce-identifier) */
76/** @} */
77
78/**
79 * @defgroup path_pred_options Path predicate options.
80 * @{
81 */
82#define LY_PATH_PRED_KEYS 0x40 /* expected predicate only - [node='value']* */
83#define LY_PATH_PRED_SIMPLE 0x80 /* expected predicates - [node='value']*; [.='value']; [1] */
84#define LY_PATH_PRED_LEAFREF 0xC0 /* expected predicates only leafref - [node=current()/../../../node/node];
85 at least 1 ".." and 1 "node" after */
86/** @} */
87
88/**
89 * @brief Parse path into XPath token structure and perform all additional checks.
90 *
91 * @param[in] ctx libyang context.
92 * @param[in] str_path Path to parse.
93 * @param[in] path_len Length of @p str_path.
94 * @param[in] begin Begin option (@ref path_begin_options).
95 * @param[in] lref Lref option (@ref path_lref_options).
96 * @param[in] prefix Prefix option (@ref path_prefix_options).
97 * @param[in] pred Predicate option (@ref path_pred_options).
98 * @param[out] expr Parsed path.
99 * @return LY_ERR value.
100 */
101LY_ERR ly_path_parse(const struct ly_ctx *ctx, const char *str_path, size_t path_len, uint8_t begin, uint8_t lref,
102 uint8_t prefix, uint8_t pred, struct lyxp_expr **expr);
103
104/**
105 * @brief Parse predicate into XPath token structure and perform all additional checks.
106 *
107 * @param[in] ctx libyang context.
108 * @param[in] str_path Path to parse.
109 * @param[in] path_len Length of @p str_path.
110 * @param[in] prefix Prefix option (@ref path_prefix_options).
111 * @param[in] pred Predicate option (@ref path_pred_options).
112 * @param[out] expr Parsed path.
113 * @return LY_ERR value.
114 */
115LY_ERR ly_path_parse_predicate(const struct ly_ctx *ctx, const char *str_path, size_t path_len, uint8_t prefix,
116 uint8_t pred, struct lyxp_expr **expr);
117
118/**
119 * @brief Compile path into ly_path structure. Any predicates of a leafref are only checked, not compiled.
120 *
121 * @param[in] cur_mod Module of the current (original context) node. Used for nodes without prefix for ::LYD_SCHEMA format.
122 * @param[in] ctx_node Context node. Can be NULL for absolute paths.
123 * @param[in] expr Parsed path.
124 * @param[in] lref Lref option (@ref path_lref_options).
125 * @param[in] resolve_prefix Callback for prefix resolution.
126 * @param[in] prefix_data Data for @p resolve_prefix.
127 * @param[in] format Format of the path.
128 * @param[out] path Compiled path.
129 * @return LY_ERR value.
130 */
131LY_ERR ly_path_compile(const struct lys_module *cur_mod, const struct lysc_node *ctx_node, const struct lyxp_expr *expr,
132 uint8_t lref, ly_clb_resolve_prefix resolve_prefix, void *prefix_data, LYD_FORMAT format,
133 struct ly_path **path);
134
135/**
136 * @brief Compile predicate into ly_path_predicate structure. Only simple predicates (not leafref) are supported.
137 *
138 * @param[in] cur_mod Module of the current (original context) node. Used for nodes without prefix for ::LYD_SCHEMA format.
139 * @param[in] ctx_node Context node, node for which the predicate is defined.
140 * @param[in] expr Parsed path.
141 * @param[in,out] tok_idx Index in @p expr, is adjusted for parsed tokens.
142 * @param[in] resolve_prefix Callback for prefix resolution.
143 * @param[in] prefix_data Data for @p resolve_prefix.
144 * @param[in] format Format of the path.
145 * @param[out] predicates Compiled predicates.
146 * @param[out] pred_type Type of the compiled predicate(s).
147 * @return LY_ERR value.
148 */
149LY_ERR ly_path_compile_predicate(const struct lys_module *cur_mod, const struct lysc_node *ctx_node,
150 const struct lyxp_expr *expr, uint16_t *tok_idx, ly_clb_resolve_prefix resolve_prefix,
151 void *prefix_data, LYD_FORMAT format, struct ly_path_predicate **predicates,
152 enum ly_path_pred_type *pred_type);
153
154/**
155 * @brief Resolve the target defined by ly_path structure. Not supported for leafref!
156 *
157 * @param[in] path Path structure specifying the target.
158 * @param[in] start Starting node for relative paths, can be any for absolute paths.
159 * @param[out] match Found matching node, can be NULL, set to NULL if not found.
160 * @return LY_ENOTFOUND if no nodes were found,
161 * @return LY_SUCCESS when the last node in the path was found,
162 * @return LY_ERR on another error.
163 */
164LY_ERR ly_path_eval(const struct ly_path *path, const struct lyd_node *start, struct lyd_node **match);
165
166/**
167 * @brief Duplicate ly_path structure.
168 *
169 * @param[in] ctx libyang context.
170 * @param[in] path Path to duplicate.
171 * @param[out] dup Duplicated path.
172 * @return LY_ERR value.
173 */
174LY_ERR ly_path_dup(const struct ly_ctx *ctx, const struct ly_path *path, struct ly_path **dup);
175
176/**
177 * @brief Free ly_path_predicate structure.
178 *
179 * @param[in] ctx libyang context.
180 * @param[in] pred_type Predicate type.
181 * @param[in] llist Leaf-list in case of leaf-list predicate.
182 * @param[in] predicates Predicates ([sized array](@ref sizedarrays)) to free.
183 */
184void ly_path_predicates_free(const struct ly_ctx *ctx, enum ly_path_pred_type pred_type, const struct lysc_node *llist,
185 struct ly_path_predicate *predicates);
186
187/**
188 * @brief Free ly_path structure.
189 *
190 * @param[in] ctx libyang context.
191 * @param[in] path The structure ([sized array](@ref sizedarrays)) to free.
192 */
193void ly_path_free(const struct ly_ctx *ctx, struct ly_path *path);
194
195#endif /* LY_PATH_H_ */