Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file tree.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 4 | * @brief libyang generic macros and functions to work with YANG schema or data trees. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 5 | * |
| 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 Vasko | ae867c7 | 2020-10-07 08:54:27 +0200 | [diff] [blame] | 18 | #include <inttypes.h> |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 19 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 20 | #ifdef __cplusplus |
| 21 | extern "C" { |
| 22 | #endif |
| 23 | |
| 24 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 25 | * @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 Vasko | 3e1f655 | 2021-01-14 09:27:55 +0100 | [diff] [blame] | 86 | * - ::lyd_find_path() |
Radek Krejci | fba9c62 | 2020-10-30 08:28:54 +0100 | [diff] [blame] | 87 | * - ::lys_find_path() |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 88 | * |
| 89 | */ |
| 90 | |
| 91 | /** |
Radek Krejci | 2ff0d57 | 2020-05-21 15:27:28 +0200 | [diff] [blame] | 92 | * @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 Krejci | c4fa029 | 2020-05-14 10:54:49 +0200 | [diff] [blame] | 100 | * @brief Type (i.e. size) of the [sized array](@ref sizedarrays)'s size counter. |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 101 | * |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 102 | * To print the value via a print format, use LY_PRI_ARRAY_COUNT_TYPE specifier. |
Radek Krejci | c4fa029 | 2020-05-14 10:54:49 +0200 | [diff] [blame] | 103 | */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 104 | #define LY_ARRAY_COUNT_TYPE uint64_t |
Radek Krejci | c4fa029 | 2020-05-14 10:54:49 +0200 | [diff] [blame] | 105 | |
| 106 | /** |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 107 | * @brief Printing format specifier macro for LY_ARRAY_SIZE_TYPE values. |
| 108 | */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 109 | #define LY_PRI_ARRAY_COUNT_TYPE PRIu64 |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 110 | |
| 111 | /** |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 112 | * @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 Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 127 | (ARRAY) && ((void*)ITER - (void*)ARRAY)/(sizeof(TYPE)) < (*((LY_ARRAY_COUNT_TYPE*)(ARRAY) - 1)); \ |
Radek Krejci | 6b27335 | 2021-04-13 20:33:42 +0200 | [diff] [blame] | 128 | ITER = (TYPE*)ITER + 1) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * @brief Helper macro to go through sized-arrays with a numeric iterator. |
| 132 | * |
| 133 | * Use with opening curly bracket (`{`). |
| 134 | * |
Radek Krejci | 5ee14f3 | 2020-11-05 13:16:42 +0100 | [diff] [blame] | 135 | * The item on the current INDEX in the ARRAY can be accessed in a standard C way as ARRAY[INDEX]. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 136 | * |
| 137 | * @param[in] ARRAY Array to go through |
Radek Krejci | 5ee14f3 | 2020-11-05 13:16:42 +0100 | [diff] [blame] | 138 | * @param[out] INDEX Variable for the iterating index of the item being processed in each loop |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 139 | */ |
| 140 | #define LY_ARRAY_FOR_INDEX(ARRAY, INDEX) \ |
| 141 | for (INDEX = 0; \ |
Radek Krejci | c7d13e3 | 2020-12-09 12:32:24 +0100 | [diff] [blame] | 142 | INDEX < LY_ARRAY_COUNT(ARRAY); \ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 143 | ++INDEX) |
| 144 | |
| 145 | /** |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 146 | * @brief Get the number of records in the ARRAY. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 147 | */ |
Radek Krejci | c7d13e3 | 2020-12-09 12:32:24 +0100 | [diff] [blame] | 148 | #define LY_ARRAY_COUNT(ARRAY) (ARRAY ? (*((LY_ARRAY_COUNT_TYPE*)(ARRAY) - 1)) : 0) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 149 | |
| 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 Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 164 | * 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 Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 165 | * 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 | */ |
| 202 | typedef 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 Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 223 | LY_TYPE_INT64 /**< 64-bit signed integer ([RFC 6020 sec 9.2](http://tools.ietf.org/html/rfc6020#section-9.2)) */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 224 | } 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 Vasko | 22df3f0 | 2020-08-24 13:29:22 +0200 | [diff] [blame] | 230 | extern const char *ly_data_type2str[LY_DATA_TYPE_COUNT]; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 231 | |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 232 | /** |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame^] | 233 | * @brief All kinds of supported value formats and prefix mappings to modules. |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 234 | */ |
| 235 | typedef enum { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame^] | 236 | LY_VALUE_SCHEMA, /**< YANG schema value, prefixes map to YANG import prefixes */ |
| 237 | LY_VALUE_SCHEMA_RESOLVED, /**< resolved YANG schema value, prefixes map to module structures directly */ |
| 238 | LY_VALUE_XML, /**< XML data value, prefixes map to XML namespace prefixes */ |
| 239 | LY_VALUE_JSON /**< JSON data value, prefixes map to module names */ |
| 240 | } LY_VALUE_FORMAT; |
Michal Vasko | fc2cd07 | 2021-02-24 13:17:17 +0100 | [diff] [blame] | 241 | |
Radek Krejci | 2ff0d57 | 2020-05-21 15:27:28 +0200 | [diff] [blame] | 242 | /** @} trees */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 243 | |
| 244 | #ifdef __cplusplus |
| 245 | } |
| 246 | #endif |
| 247 | |
| 248 | #endif /* LY_TREE_H_ */ |