blob: 4b9ca8abb0b50d68cb399125a924b2ccc7a88a40 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
2 * @file tree.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Radek Krejci8678fa42020-08-18 16:07:28 +02004 * @brief libyang generic macros and functions to work with YANG schema or data trees.
Radek Krejcie7b95092019-05-15 11:03:07 +02005 *
6 * Copyright (c) 2019 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_TREE_H_
16#define LY_TREE_H_
17
Michal Vaskoae867c72020-10-07 08:54:27 +020018#include <inttypes.h>
Radek Krejci7eb54ba2020-05-18 16:30:04 +020019
Radek Krejcie7b95092019-05-15 11:03:07 +020020#ifdef __cplusplus
21extern "C" {
22#endif
23
24/**
Radek Krejci8678fa42020-08-18 16:07:28 +020025 * @page howtoXPath XPath Addressing
26 *
27 * Internally, XPath evaluation is performed on __when__ and __must__ conditions in the schema. For that almost
28 * a full [XPath 1.0](http://www.w3.org/TR/1999/REC-xpath-19991116/) evaluator was implemented.
29 * In YANG models you can also find paths identifying __augment__ targets, __leafref__ targets, and trivial paths in
30 * __choice default__ and __unique__ statements argument. The exact format of all those paths can be found in the
31 * relevant RFCs. Further will only be discussed paths that are used directly in libyang API functions.
32 *
33 * XPath
34 * =====
35 *
36 * Generally, any xpath argument expects an expression similar to _when_ or _must_ as the same evaluator is used. As for
37 * the format of any prefixes, the standardized JSON ([RFC 7951](https://tools.ietf.org/html/rfc7951#section-6.11))
38 * was used. Summarized, xpath follows these conventions:
39 * - full XPath can be used, but only data nodes (node sets) will always be returned,
40 * - as per the specification, prefixes are actually __module names__,
41 * - also in the specification, for _absolute_ paths, the first (leftmost) node _MUST_ have a prefix,
42 * - for _relative_ paths, you specify the __context node__, which then acts as a parent for the first node in the path,
43 * - nodes always inherit their module (prefix) from their __parent node__ so whenever a node is from a different
44 * module than its parent, it _MUST_ have a prefix,
45 * - nodes from the same module as their __parent__ _MUST NOT_ have a prefix,
46 * - note that non-data nodes/schema-only node (choice, case, uses, input, output) are skipped and _MUST_ not be
47 * included in the path.
48 *
49 * Functions List
50 * --------------
51 * - ::lyd_find_xpath()
52 * - ::lys_find_xpath()
53 *
54 * Path
55 * ====
56 *
57 * The term path is used when a simplified (subset of) XPath is expected. Path is always a valid XPath but not
58 * the other way around. In short, paths only identify a specific (set of) nodes based on their ancestors in the
59 * schema. Predicates are allowed the same as for an [instance-identifier](https://tools.ietf.org/html/rfc7950#section-9.13).
60 * Specifically, key values of a list, leaf-list value, or position of lists without keys can be used.
61 *
62 * Examples
63 * --------
64 *
65 * - get __list__ instance with __key1__ of value __1__ and __key2__ of value __2__ (this can return more __list__ instances if there are more keys than __key1__ and __key2__)
66 *
67 * /module-name:container/list[key1='1'][key2='2']
68 *
69 * - get __leaf-list__ instance with the value __val__
70 *
71 * /module-name:container/leaf-list[.='val']
72 *
73 * - get __3rd list-without-keys__ instance with no keys defined
74 *
75 * /module-name:container/list-without-keys[3]
76 *
77 * - get __aug-list__ with __aug-list-key__, which was added to __module-name__ from an augment module __augment-module__
78 *
79 * /module-name:container/container2/augment-module:aug-cont/aug-list[aug-list-key='value']
80 *
81 * Functions List
82 * --------------
83 * - ::lyd_new_path()
84 * - ::lyd_new_path2()
85 * - ::lyd_path()
Michal Vasko3e1f6552021-01-14 09:27:55 +010086 * - ::lyd_find_path()
Radek Krejcifba9c622020-10-30 08:28:54 +010087 * - ::lys_find_path()
Radek Krejci8678fa42020-08-18 16:07:28 +020088 *
89 */
90
91/**
Radek Krejci2ff0d572020-05-21 15:27:28 +020092 * @defgroup trees Trees
93 *
94 * Generic macros, functions, etc. to work with both [schema](@ref schematree) and [data](@ref datatree) trees.
95 *
96 * @{
97 */
98
99/**
Radek Krejcic4fa0292020-05-14 10:54:49 +0200100 * @brief Type (i.e. size) of the [sized array](@ref sizedarrays)'s size counter.
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200101 *
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200102 * To print the value via a print format, use LY_PRI_ARRAY_COUNT_TYPE specifier.
Radek Krejcic4fa0292020-05-14 10:54:49 +0200103 */
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200104#define LY_ARRAY_COUNT_TYPE uint64_t
Radek Krejcic4fa0292020-05-14 10:54:49 +0200105
106/**
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200107 * @brief Printing format specifier macro for LY_ARRAY_SIZE_TYPE values.
108 */
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200109#define LY_PRI_ARRAY_COUNT_TYPE PRIu64
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200110
111/**
Radek Krejcie7b95092019-05-15 11:03:07 +0200112 * @brief Macro selector for other LY_ARRAY_* macros, do not use directly!
113 */
114#define LY_ARRAY_SELECT(_1, _2, NAME, ...) NAME
115
116/**
117 * @brief Helper macro to go through sized-arrays with a pointer iterator.
118 *
119 * Use with opening curly bracket (`{`).
120 *
121 * @param[in] ARRAY Array to go through
122 * @param[in] TYPE Type of the records in the ARRAY
123 * @param[out] ITER Iterating pointer to the item being processed in each loop
124 */
125#define LY_ARRAY_FOR_ITER(ARRAY, TYPE, ITER) \
126 for (ITER = ARRAY; \
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200127 (ARRAY) && ((void*)ITER - (void*)ARRAY)/(sizeof(TYPE)) < (*((LY_ARRAY_COUNT_TYPE*)(ARRAY) - 1)); \
Radek Krejci6b273352021-04-13 20:33:42 +0200128 ITER = (TYPE*)ITER + 1)
Radek Krejcie7b95092019-05-15 11:03:07 +0200129
130/**
131 * @brief Helper macro to go through sized-arrays with a numeric iterator.
132 *
133 * Use with opening curly bracket (`{`).
134 *
Radek Krejci5ee14f32020-11-05 13:16:42 +0100135 * The item on the current INDEX in the ARRAY can be accessed in a standard C way as ARRAY[INDEX].
Radek Krejcie7b95092019-05-15 11:03:07 +0200136 *
137 * @param[in] ARRAY Array to go through
Radek Krejci5ee14f32020-11-05 13:16:42 +0100138 * @param[out] INDEX Variable for the iterating index of the item being processed in each loop
Radek Krejcie7b95092019-05-15 11:03:07 +0200139 */
140#define LY_ARRAY_FOR_INDEX(ARRAY, INDEX) \
141 for (INDEX = 0; \
Radek Krejcic7d13e32020-12-09 12:32:24 +0100142 INDEX < LY_ARRAY_COUNT(ARRAY); \
Radek Krejcie7b95092019-05-15 11:03:07 +0200143 ++INDEX)
144
145/**
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200146 * @brief Get the number of records in the ARRAY.
Radek Krejcie7b95092019-05-15 11:03:07 +0200147 */
Radek Krejcic7d13e32020-12-09 12:32:24 +0100148#define LY_ARRAY_COUNT(ARRAY) (ARRAY ? (*((LY_ARRAY_COUNT_TYPE*)(ARRAY) - 1)) : 0)
Radek Krejcie7b95092019-05-15 11:03:07 +0200149
150/**
151 * @brief Sized-array iterator (for-loop).
152 *
153 * Use with opening curly bracket (`{`).
154 *
155 * There are 2 variants:
156 *
157 * LY_ARRAY_FOR(ARRAY, TYPE, ITER)
158 *
159 * Where ARRAY is a sized-array to go through, TYPE is the type of the items in the ARRAY and ITER is a pointer variable
160 * providing the items of the ARRAY in the loops. This functionality is provided by LY_ARRAY_FOR_ITER macro
161 *
162 * LY_ARRAY_FOR(ARRAY, INDEX)
163 *
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200164 * The ARRAY is again a sized-array to go through, the INDEX is a variable (LY_ARRAY_COUNT_TYPE) for storing iterating ARRAY's index
Radek Krejcie7b95092019-05-15 11:03:07 +0200165 * to access the items of ARRAY in the loops. This functionality is provided by LY_ARRAY_FOR_INDEX macro.
166 */
167#define LY_ARRAY_FOR(ARRAY, ...) LY_ARRAY_SELECT(__VA_ARGS__, LY_ARRAY_FOR_ITER, LY_ARRAY_FOR_INDEX)(ARRAY, __VA_ARGS__)
168
169/**
170 * @brief Macro to iterate via all sibling elements without affecting the list itself
171 *
172 * Works for all types of nodes despite it is data or schema tree, but all the
173 * parameters must be pointers to the same type.
174 *
175 * Use with opening curly bracket (`{`). All parameters must be of the same type.
176 *
177 * @param START Pointer to the starting element.
178 * @param ELEM Iterator.
179 */
180#define LY_LIST_FOR(START, ELEM) \
181 for ((ELEM) = (START); \
182 (ELEM); \
183 (ELEM) = (ELEM)->next)
184
185/**
186 * @brief Macro to iterate via all sibling elements allowing to modify the list itself (e.g. removing elements)
187 *
188 * Use with opening curly bracket (`{`). All parameters must be of the same type.
189 *
190 * @param START Pointer to the starting element.
191 * @param NEXT Temporary storage to allow removing of the current iterator content.
192 * @param ELEM Iterator.
193 */
194#define LY_LIST_FOR_SAFE(START, NEXT, ELEM) \
195 for ((ELEM) = (START); \
196 (ELEM) ? (NEXT = (ELEM)->next, 1) : 0; \
197 (ELEM) = (NEXT))
198
199/**
200 * @brief YANG built-in types
201 */
202typedef enum
203{
204 LY_TYPE_UNKNOWN = 0, /**< Unknown type */
205 LY_TYPE_BINARY, /**< Any binary data ([RFC 6020 sec 9.8](http://tools.ietf.org/html/rfc6020#section-9.8)) */
206 LY_TYPE_UINT8, /**< 8-bit unsigned integer ([RFC 6020 sec 9.2](http://tools.ietf.org/html/rfc6020#section-9.2)) */
207 LY_TYPE_UINT16, /**< 16-bit unsigned integer ([RFC 6020 sec 9.2](http://tools.ietf.org/html/rfc6020#section-9.2)) */
208 LY_TYPE_UINT32, /**< 32-bit unsigned integer ([RFC 6020 sec 9.2](http://tools.ietf.org/html/rfc6020#section-9.2)) */
209 LY_TYPE_UINT64, /**< 64-bit unsigned integer ([RFC 6020 sec 9.2](http://tools.ietf.org/html/rfc6020#section-9.2)) */
210 LY_TYPE_STRING, /**< Human-readable string ([RFC 6020 sec 9.4](http://tools.ietf.org/html/rfc6020#section-9.4)) */
211 LY_TYPE_BITS, /**< A set of bits or flags ([RFC 6020 sec 9.7](http://tools.ietf.org/html/rfc6020#section-9.7)) */
212 LY_TYPE_BOOL, /**< "true" or "false" ([RFC 6020 sec 9.5](http://tools.ietf.org/html/rfc6020#section-9.5)) */
213 LY_TYPE_DEC64, /**< 64-bit signed decimal number ([RFC 6020 sec 9.3](http://tools.ietf.org/html/rfc6020#section-9.3))*/
214 LY_TYPE_EMPTY, /**< A leaf that does not have any value ([RFC 6020 sec 9.11](http://tools.ietf.org/html/rfc6020#section-9.11)) */
215 LY_TYPE_ENUM, /**< Enumerated strings ([RFC 6020 sec 9.6](http://tools.ietf.org/html/rfc6020#section-9.6)) */
216 LY_TYPE_IDENT, /**< A reference to an abstract identity ([RFC 6020 sec 9.10](http://tools.ietf.org/html/rfc6020#section-9.10)) */
217 LY_TYPE_INST, /**< References a data tree node ([RFC 6020 sec 9.13](http://tools.ietf.org/html/rfc6020#section-9.13)) */
218 LY_TYPE_LEAFREF, /**< A reference to a leaf instance ([RFC 6020 sec 9.9](http://tools.ietf.org/html/rfc6020#section-9.9))*/
219 LY_TYPE_UNION, /**< Choice of member types ([RFC 6020 sec 9.12](http://tools.ietf.org/html/rfc6020#section-9.12)) */
220 LY_TYPE_INT8, /**< 8-bit signed integer ([RFC 6020 sec 9.2](http://tools.ietf.org/html/rfc6020#section-9.2)) */
221 LY_TYPE_INT16, /**< 16-bit signed integer ([RFC 6020 sec 9.2](http://tools.ietf.org/html/rfc6020#section-9.2)) */
222 LY_TYPE_INT32, /**< 32-bit signed integer ([RFC 6020 sec 9.2](http://tools.ietf.org/html/rfc6020#section-9.2)) */
Michal Vasko69730152020-10-09 16:30:07 +0200223 LY_TYPE_INT64 /**< 64-bit signed integer ([RFC 6020 sec 9.2](http://tools.ietf.org/html/rfc6020#section-9.2)) */
Radek Krejcie7b95092019-05-15 11:03:07 +0200224} LY_DATA_TYPE;
225#define LY_DATA_TYPE_COUNT 20 /**< Number of different types */
226
227/**
228 * @brief Stringified YANG built-in data types
229 */
Michal Vasko22df3f02020-08-24 13:29:22 +0200230extern const char *ly_data_type2str[LY_DATA_TYPE_COUNT];
Radek Krejcie7b95092019-05-15 11:03:07 +0200231
Michal Vaskofc2cd072021-02-24 13:17:17 +0100232/**
233 * @brief All kinds of supported prefix mappings to modules.
234 */
235typedef enum {
236 LY_PREF_SCHEMA, /**< value prefixes map to YANG import prefixes */
237 LY_PREF_SCHEMA_RESOLVED, /**< value prefixes map to module structures directly */
238 LY_PREF_XML, /**< value prefixes map to XML namespace prefixes */
239 LY_PREF_JSON /**< value prefixes map to module names */
240} LY_PREFIX_FORMAT;
241
Radek Krejci2ff0d572020-05-21 15:27:28 +0200242/** @} trees */
Radek Krejcie7b95092019-05-15 11:03:07 +0200243
244#ifdef __cplusplus
245}
246#endif
247
248#endif /* LY_TREE_H_ */