Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file libyang.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief The main libyang public header. |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2018 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_LIBYANG_H_ |
| 16 | #define LY_LIBYANG_H_ |
| 17 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 18 | #include <stdint.h> |
| 19 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 20 | #ifdef __cplusplus |
| 21 | extern "C" { |
| 22 | #endif |
| 23 | |
| 24 | /** |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 25 | * @defgroup context Context |
| 26 | * @{ |
| 27 | * |
| 28 | * Structures and functions to manipulate with the libyang "containers". The \em context concept allows callers |
| 29 | * to work in environments with different sets of YANG schemas. More detailed information can be found at |
| 30 | * @ref howtocontext page. |
| 31 | */ |
| 32 | |
| 33 | /** |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 34 | * @struct ly_ctx |
| 35 | * @brief libyang context handler. |
| 36 | */ |
| 37 | struct ly_ctx; |
| 38 | |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 39 | /**@} context */ |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 40 | |
| 41 | #include "log.h" |
| 42 | #include "set.h" |
| 43 | #include "dict.h" |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 44 | #include "tree_schema.h" |
| 45 | |
| 46 | /** |
| 47 | * @ingroup context |
| 48 | * @{ |
| 49 | */ |
| 50 | |
| 51 | /** |
| 52 | * @defgroup contextoptions Context options |
| 53 | * @ingroup context |
| 54 | * |
| 55 | * Options to change context behavior. |
| 56 | * @{ |
| 57 | */ |
| 58 | |
| 59 | #define LY_CTX_ALLIMPLEMENTED 0x01 /**< All the imports of the schema being parsed are treated implemented. */ |
| 60 | #define LY_CTX_TRUSTED 0x02 /**< Handle the schema being parsed as trusted and skip its validation |
| 61 | tests. Note that while this option improves performance, it can |
| 62 | lead to an undefined behavior if the schema is not correct. */ |
| 63 | #define LY_CTX_NOYANGLIBRARY 0x04 /**< Do not internally implement ietf-yang-library module. The option |
| 64 | causes that function ly_ctx_info() does not work (returns NULL) until |
| 65 | the ietf-yang-library module is loaded manually. While any revision |
| 66 | of this schema can be loaded with this option, note that the only |
| 67 | revisions implemented by ly_ctx_info() are 2016-04-09 and 2018-01-17. |
| 68 | This option cannot be used with ly_ctx_new_yl*() functions. */ |
| 69 | #define LY_CTX_DISABLE_SEARCHDIRS 0x08 /**< Do not search for schemas in context's searchdirs neither in current |
| 70 | working directory. It is entirely skipped and the only way to get |
| 71 | schema data for imports or for ly_ctx_load_module() is to use the |
| 72 | callbacks provided by caller via ly_ctx_set_module_imp_clb() */ |
| 73 | #define LY_CTX_DISABLE_SEARCHDIR_CWD 0x10 /**< Do not automatically search for schemas in current working |
| 74 | directory, which is by default searched automatically (despite not |
| 75 | recursively). */ |
| 76 | #define LY_CTX_PREFER_SEARCHDIRS 0x20 /**< When searching for schema, prefer searchdirs instead of user callback. */ |
| 77 | /**@} contextoptions */ |
| 78 | |
| 79 | /** |
| 80 | * @brief Create libyang context. |
| 81 | * |
| 82 | * Context is used to hold all information about schemas. Usually, the application is supposed |
| 83 | * to work with a single context in which libyang is holding all schemas (and other internal |
| 84 | * information) according to which the data trees will be processed and validated. So, the schema |
| 85 | * trees are tightly connected with the specific context and they are held by the context internally |
| 86 | * - caller does not need to keep pointers to the schemas returned by lys_parse(), context knows |
| 87 | * about them. The data trees created with lyd_parse() are still connected with the specific context, |
| 88 | * but they are not internally held by the context. The data tree just points and lean on some data |
| 89 | * held by the context (schema tree, string dictionary, etc.). Therefore, in case of data trees, caller |
| 90 | * is supposed to keep pointers returned by the lyd_parse() and manage the data tree on its own. This |
| 91 | * also affects the number of instances of both tree types. While you can have only one instance of |
| 92 | * specific schema connected with a single context, number of data tree instances is not connected. |
| 93 | * |
| 94 | * @param[in] search_dir Directory where libyang will search for the imported or included modules |
| 95 | * and submodules. If no such directory is available, NULL is accepted. |
| 96 | * @param[in] options Context options, see @ref contextoptions. |
| 97 | * @param[out] new_ctx Pointer to the created libyang context if LY_SUCCESS returned. |
| 98 | * @return LY_ERR return value. |
| 99 | */ |
| 100 | LY_ERR ly_ctx_new(const char *search_dir, int options, struct ly_ctx **new_ctx); |
| 101 | |
| 102 | /** |
| 103 | * @brief Add the search path into libyang context |
| 104 | * |
| 105 | * To reset search paths set in the context, use ly_ctx_unset_searchdirs() and then |
| 106 | * set search paths again. |
| 107 | * |
| 108 | * @param[in] ctx Context to be modified. |
| 109 | * @param[in] search_dir New search path to add to the current paths previously set in ctx. |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 110 | * @return LY_ERR return value. |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 111 | */ |
Radek Krejci | ad57350 | 2018-09-07 15:26:55 +0200 | [diff] [blame] | 112 | LY_ERR ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir); |
Radek Krejci | 0af5f5d | 2018-09-07 15:00:30 +0200 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * @brief Clean the search path(s) from the libyang context |
| 116 | * |
| 117 | * @param[in] ctx Context to be modified. |
| 118 | * @param[in] index Index of the search path to be removed, use negative value to remove them all. |
| 119 | * Correct index value can be checked via ly_ctx_get_searchdirs(). |
| 120 | * @return LY_ERR return value |
| 121 | */ |
| 122 | LY_ERR ly_ctx_unset_searchdirs(struct ly_ctx *ctx, int index); |
| 123 | |
| 124 | /** |
| 125 | * @brief Get the NULL-terminated list of the search paths in libyang context. Do not modify the result! |
| 126 | * |
| 127 | * @param[in] ctx Context to query. |
| 128 | * @return NULL-terminated list (array) of the search paths, NULL if no searchpath was set. |
| 129 | * Do not modify the provided data in any way! |
| 130 | */ |
| 131 | const char * const *ly_ctx_get_searchdirs(const struct ly_ctx *ctx); |
| 132 | |
| 133 | /** |
| 134 | * @brief Get the currently set context's options. |
| 135 | * |
| 136 | * @param[in] ctx Context to query. |
| 137 | * @return Combination of all the currently set context's options, see @ref contextoptions. |
| 138 | */ |
| 139 | int ly_ctx_get_options(struct ly_ctx *ctx); |
| 140 | |
| 141 | /** |
| 142 | * @brief Make context to stop searching for schemas (imported, included or requested via ly_ctx_load_module()) |
| 143 | * in searchdirs set via ly_ctx_set_searchdir() functions. Searchdirs are still stored in the context, so by |
| 144 | * unsetting the option by ly_ctx_unset_disable_searchdirs() searching in all previously searchdirs is restored. |
| 145 | * |
| 146 | * The same effect is achieved by using #LY_CTX_DISABLE_SEARCHDIRS option when creating new context or parsing |
| 147 | * a specific schema. |
| 148 | * |
| 149 | * @param[in] ctx Context to be modified. |
| 150 | */ |
| 151 | void ly_ctx_set_disable_searchdirs(struct ly_ctx *ctx); |
| 152 | |
| 153 | /** |
| 154 | * @brief Reverse function to ly_ctx_set_disable_searchdirs(). |
| 155 | * |
| 156 | * @param[in] ctx Context to be modified. |
| 157 | */ |
| 158 | void ly_ctx_unset_disable_searchdirs(struct ly_ctx *ctx); |
| 159 | |
| 160 | /** |
| 161 | * @brief Make context to stop implicitly searching for schemas (imported, included or requested via ly_ctx_load_module()) |
| 162 | * in current working directory. This flag can be unset by ly_ctx_unset_disable_searchdir_cwd(). |
| 163 | * |
| 164 | * The same effect is achieved by using #LY_CTX_DISABLE_SEARCHDIR_CWD option when creating new context or parsing |
| 165 | * a specific schema. |
| 166 | * |
| 167 | * @param[in] ctx Context to be modified. |
| 168 | */ |
| 169 | void ly_ctx_set_disable_searchdir_cwd(struct ly_ctx *ctx); |
| 170 | |
| 171 | /** |
| 172 | * @brief Reverse function to ly_ctx_set_disable_searchdir_cwd(). |
| 173 | * |
| 174 | * @param[in] ctx Context to be modified. |
| 175 | */ |
| 176 | void ly_ctx_unset_disable_searchdir_cwd(struct ly_ctx *ctx); |
| 177 | |
| 178 | /** |
| 179 | * @brief Prefer context's searchdirs before the user callback (ly_module_imp_clb) provided via ly_ctx_set_module_imp_clb()). |
| 180 | * |
| 181 | * The same effect is achieved by using #LY_CTX_PREFER_SEARCHDIRS option when creating new context or parsing |
| 182 | * a specific schema. |
| 183 | * |
| 184 | * @param[in] ctx Context to be modified. |
| 185 | */ |
| 186 | void ly_ctx_set_prefer_searchdirs(struct ly_ctx *ctx); |
| 187 | |
| 188 | /** |
| 189 | * @brief Reverse function to ly_ctx_set_prefer_searchdirs(). |
| 190 | * |
| 191 | * @param[in] ctx Context to be modified. |
| 192 | */ |
| 193 | void ly_ctx_unset_prefer_searchdirs(struct ly_ctx *ctx); |
| 194 | |
| 195 | /** |
| 196 | * @brief Make context to set all the imported modules to be implemented. By default, |
| 197 | * if the imported module is not used in leafref's path, augment or deviation, it is |
| 198 | * imported and its data tree is not taken into account. |
| 199 | * |
| 200 | * The same effect is achieved by using #LY_CTX_ALLIMPLEMENTED option when creating new context or parsing |
| 201 | * a specific schema. |
| 202 | * |
| 203 | * Note, that function does not make the currently loaded modules, it just change the |
| 204 | * schema parser behavior for the future parsing. This flag can be unset by ly_ctx_unset_allimplemented(). |
| 205 | * |
| 206 | * @param[in] ctx Context to be modified. |
| 207 | */ |
| 208 | void ly_ctx_set_allimplemented(struct ly_ctx *ctx); |
| 209 | |
| 210 | /** |
| 211 | * @brief Reverse function to ly_ctx_set_allimplemented(). |
| 212 | * |
| 213 | * @param[in] ctx Context to be modified. |
| 214 | */ |
| 215 | void ly_ctx_unset_allimplemented(struct ly_ctx *ctx); |
| 216 | |
| 217 | /** |
| 218 | * @brief Change the schema parser behavior when parsing new schemas forcing it to skip some of the schema |
| 219 | * validation checks to improve performance. Note that parsing invalid schemas this way may lead to an |
| 220 | * undefined behavior later, e.g. when working with data trees. |
| 221 | * |
| 222 | * The same effect is achieved by using #LY_CTX_TRUSTED option when creating new context or parsing |
| 223 | * a specific schema. |
| 224 | * |
| 225 | * This flag can be unset by ly_ctx_unset_trusted(). |
| 226 | * |
| 227 | * @param[in] ctx Context to be modified. |
| 228 | */ |
| 229 | void ly_ctx_set_trusted(struct ly_ctx *ctx); |
| 230 | |
| 231 | /** |
| 232 | * @brief Reverse function to ly_ctx_set_trusted(). |
| 233 | * |
| 234 | * @param[in] ctx Context to be modified. |
| 235 | */ |
| 236 | void ly_ctx_unset_trusted(struct ly_ctx *ctx); |
| 237 | |
| 238 | /** |
| 239 | * @brief Get current ID of the modules set. The value is available also |
| 240 | * as module-set-id in ly_ctx_info() result. |
| 241 | * |
| 242 | * @param[in] ctx Context to be examined. |
| 243 | * @return Numeric identifier of the current context's modules set. |
| 244 | */ |
| 245 | uint16_t ly_ctx_get_module_set_id(const struct ly_ctx *ctx); |
| 246 | |
| 247 | /** |
| 248 | * @brief Free all internal structures of the specified context. |
| 249 | * |
| 250 | * The function should be used before terminating the application to destroy |
| 251 | * and free all structures internally used by libyang. If the caller uses |
| 252 | * multiple contexts, the function should be called for each used context. |
| 253 | * |
| 254 | * All instance data are supposed to be freed before destroying the context. |
| 255 | * Data models are destroyed automatically as part of ly_ctx_destroy() call. |
| 256 | * |
| 257 | * @param[in] ctx libyang context to destroy |
| 258 | * @param[in] private_destructor Optional destructor function for private objects assigned |
| 259 | * to the nodes via lys_set_private(). If NULL, the private objects are not freed by libyang. |
| 260 | * Remember the differences between the structures derived from ::lys_node and always check |
| 261 | * ::lys_node#nodetype. |
| 262 | */ |
| 263 | void ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lysc_node *node, void *priv)); |
| 264 | |
| 265 | /** @} context */ |
| 266 | |
| 267 | #ifdef __cplusplus |
| 268 | } |
| 269 | #endif |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 270 | |
| 271 | #endif /* LY_LIBYANG_H_ */ |