Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file context.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief internal context structures and functions |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2017 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_CONTEXT_H_ |
| 16 | #define LY_CONTEXT_H_ |
| 17 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 18 | #include <stdint.h> |
| 19 | |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 20 | #include "log.h" |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 21 | #include "parser_schema.h" |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 22 | |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 23 | #ifdef __cplusplus |
| 24 | extern "C" { |
| 25 | #endif |
| 26 | |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame^] | 27 | struct lysc_node; |
| 28 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 29 | /** |
Radek Krejci | 52785a2 | 2019-09-11 12:57:26 +0200 | [diff] [blame] | 30 | * @page howtocontext Context |
| 31 | * |
| 32 | * The context concept allows callers to work in environments with different sets of YANG schemas. |
| 33 | * |
| 34 | * The first step in libyang is to create a new context using ly_ctx_new(). It returns a handler |
| 35 | * used in the following work. |
| 36 | * |
| 37 | * When creating a new context, search dir can be specified (NULL is accepted) to provide directory |
| 38 | * where libyang will automatically search for schemas being imported or included. The search path |
| 39 | * can be later changed via ly_ctx_set_searchdir() and ly_ctx_unset_searchdir() functions. Before the search dirs, |
| 40 | * also the current working directory is (non-recursively) searched. For the case of the explicitly set search |
| 41 | * dirs, also all their subdirectories (and symlinks) are taken into account. Searching in the current working |
| 42 | * directory can be avoided with the context's #LY_CTX_DISABLE_SEARCHDIR_CWD option (or via ly_ctx_set_options()). |
| 43 | * Searching in all the context's search dirs (without removing them) can be avoided with the context's |
| 44 | * #LY_CTX_DISABLE_SEARCHDIRS option (or via ly_ctx_set_options()). This automatic searching can be preceded |
| 45 | * by a custom module searching callback (#ly_module_imp_clb) set via ly_ctx_set_module_imp_clb(). The algorithm of |
| 46 | * searching in search dirs is also available via API as lys_search_localfile() function. |
| 47 | * |
| 48 | * Schemas are added into the context using [parser functions](@ref howtoschemasparsers) - \b lys_parse_*(). |
| 49 | * Alternatively, also ly_ctx_load_module() can be used - in that case the #ly_module_imp_clb or automatic |
| 50 | * search in search dir and in the current working directory is used. YANG submodules cannot be loaded or even validated |
| 51 | * directly, they are loaded always only as includes of YANG modules. |
| 52 | * |
| 53 | * YANG schemas are loaded in two steps. First, the input YANG/YIN data are parsed into \b lysp_* structures that reflect |
| 54 | * the structure of the input schema. Mostly just syntax checks are done, no reference or type checking is performed in |
| 55 | * this step. If the module is supposed to be implemented, not just imported by another module, the second step is to compile |
| 56 | * it. The compiled schema may significantly differ in structure from the source schema structure. All the references |
| 57 | * are resolved, groupings are instantiated, types are resolved (and compiled by grouping all the relevant restrictions |
| 58 | * when derived from another types) and many other syntactical checks are done. |
| 59 | * |
| 60 | * Similarly, data trees can be parsed by \b lyd_parse_*() functions. Note, that functions for schemas have \b lys_ |
| 61 | * prefix (or \b lysp_ for the parsed and \b lysc_ for the compiled schema) while functions for instance data have |
| 62 | * \b lyd_ prefix. It can happen during data parsing that a schema is required and __not found__ in the context or |
| 63 | * the schema is found, but is __only imported__, not implemented (so the data cannot actually be instantiated). |
| 64 | * In these cases, a callback is called, which should add this schema into the context or change its conformance |
| 65 | * to implemented. You can set the callback using ly_ctx_set_module_data_clb() (more in @ref howtodataparsers |
| 66 | * and @ref howtodatavalidation). |
| 67 | * |
| 68 | * Context can hold multiple revisions of the same schema, but only one of them can be implemented. The schema is not |
| 69 | * implemented in case it is automatically loaded as import for another module and it is not referenced in such |
| 70 | * a module (and no other) as target of leafref, augment or deviation. All modules with deviation definition are always |
| 71 | * marked as implemented. The imported (not implemented) module can be set implemented by lys_set_implemented(). But |
| 72 | * the implemented module cannot be changed back to just imported module. The imported modules are used only as a |
| 73 | * source of definitions for types and groupings for uses statements. The data in such modules are ignored - caller |
| 74 | * is not allowed to create the data (including instantiating identities) defined in the model via data parsers, |
| 75 | * the default nodes are not added into any data tree and mandatory nodes are not checked in the data trees. This |
| 76 | * can be changed by ly_ctx_new()'s #LY_CTX_ALLIMPLEMENTED option (or via ly_ctx_set_options()), which causes that |
| 77 | * all the imported modules are automatically set to be implemented. |
| 78 | * |
| 79 | * When loading/importing a module without revision, the latest revision of the required module is supposed to load. |
| 80 | * For a context, the first time the latest revision of a module is requested, it is properly searched for and loaded. |
| 81 | * However, when this module is requested (without revision) the second time, the one found previously is returned. |
| 82 | * This has the advantage of not searching for the module repeatedly but the drawback that if a later revision |
| 83 | * of the module is later made available, this context will not use it. However, to force libyang to re-search the |
| 84 | * latest revision, ly_ctx_reset_latests() can be used (not that it applies to all the schemas in the context). |
| 85 | * |
| 86 | * Context holds all schema modules internally. To get a specific module, use ly_ctx_get_module() (or ly_ctx_get_module_ns()) |
| 87 | * The returned structure includes both, parsed and compiled, schema variants. If you need to do something with all the modules |
| 88 | * in the context, it is advised to iterate over them using ly_ctx_get_module_iter(), it is the most efficient way. |
| 89 | * Alternatively, the ly_ctx_info() function can be used to get complex information about the schemas in the context |
| 90 | * in the form of data tree defined by <a href="https://tools.ietf.org/html/rfc7895">ietf-yang-library</a> schema. |
| 91 | * To get a specific node defined in a module in the context, ly_ctx_find_path() or ly_ctx_get_node() can be used. |
| 92 | * |
| 93 | * Modules cannot be removed from their context. If you need to change the set of the schema modules in the context |
| 94 | * (use only a subset), a new context must be created. To remove the context, there is ly_ctx_destroy() function. |
| 95 | * |
| 96 | * - @subpage howtocontextdict |
| 97 | * |
| 98 | * \note API for this group of functions is available in the [context module](@ref context). |
| 99 | * |
| 100 | * Functions List |
| 101 | * -------------- |
| 102 | * - ::ly_ctx_new() |
| 103 | * - ::ly_ctx_set_searchdir() |
| 104 | * - ::ly_ctx_unset_searchdir() |
| 105 | * - ::ly_ctx_unset_searchdirs() |
| 106 | * - ::ly_ctx_get_searchdirs() |
| 107 | * - ::ly_ctx_set_module_imp_clb() |
| 108 | * - ::ly_ctx_get_module_imp_clb() |
| 109 | * - ::ly_ctx_set_module_data_clb() |
| 110 | * - ::ly_ctx_get_module_data_clb() |
| 111 | * - ::ly_ctx_set_options() |
| 112 | * - ::ly_ctx_unset_options() |
| 113 | * - ::ly_ctx_get_options() |
| 114 | * - ::ly_ctx_load_module() |
| 115 | * - ::ly_ctx_info() |
| 116 | * - ::ly_ctx_get_module_iter() |
| 117 | * - ::ly_ctx_get_module() |
| 118 | * - ::ly_ctx_get_module_ns() |
| 119 | * - ::ly_ctx_get_module_implemented() |
| 120 | * - ::ly_ctx_get_module_implemented_ns() |
| 121 | * - ::ly_ctx_get_module_latest() |
| 122 | * - ::ly_ctx_get_module_latest_ns() |
| 123 | * - ::ly_ctx_reset_latests() |
| 124 | * - ::ly_ctx_get_module_set_id() |
| 125 | * - ::ly_ctx_get_node() |
| 126 | * - ::ly_ctx_find_path() |
| 127 | * - ::ly_ctx_destroy() |
| 128 | * - ::lys_set_implemented() |
| 129 | * - ::lys_search_localfile() |
| 130 | */ |
| 131 | |
| 132 | /** |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 133 | * @defgroup context Context |
| 134 | * @{ |
| 135 | * |
| 136 | * Structures and functions to manipulate with the libyang "containers". The \em context concept allows callers |
| 137 | * to work in environments with different sets of YANG schemas. More detailed information can be found at |
| 138 | * @ref howtocontext page. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 139 | */ |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 140 | |
| 141 | /** |
| 142 | * @struct ly_ctx |
| 143 | * @brief libyang context handler. |
| 144 | */ |
| 145 | struct ly_ctx; |
| 146 | |
| 147 | /** |
| 148 | * @defgroup contextoptions Context options |
| 149 | * @ingroup context |
| 150 | * |
| 151 | * Options to change context behavior. |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 152 | * |
| 153 | * Note that the flags 0xFF00 are reserved for internal use. |
| 154 | * |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 155 | * @{ |
| 156 | */ |
| 157 | |
| 158 | #define LY_CTX_ALLIMPLEMENTED 0x01 /**< All the imports of the schema being parsed are treated implemented. */ |
| 159 | #define LY_CTX_TRUSTED 0x02 /**< Handle the schema being parsed as trusted and skip its validation |
| 160 | tests. Note that while this option improves performance, it can |
| 161 | lead to an undefined behavior if the schema is not correct. */ |
| 162 | #define LY_CTX_NOYANGLIBRARY 0x04 /**< Do not internally implement ietf-yang-library module. The option |
| 163 | causes that function ly_ctx_info() does not work (returns NULL) until |
| 164 | the ietf-yang-library module is loaded manually. While any revision |
| 165 | of this schema can be loaded with this option, note that the only |
| 166 | revisions implemented by ly_ctx_info() are 2016-04-09 and 2018-01-17. |
| 167 | This option cannot be changed on existing context. */ |
| 168 | #define LY_CTX_DISABLE_SEARCHDIRS 0x08 /**< Do not search for schemas in context's searchdirs neither in current |
| 169 | working directory. It is entirely skipped and the only way to get |
| 170 | schema data for imports or for ly_ctx_load_module() is to use the |
| 171 | callbacks provided by caller via ly_ctx_set_module_imp_clb() */ |
| 172 | #define LY_CTX_DISABLE_SEARCHDIR_CWD 0x10 /**< Do not automatically search for schemas in current working |
| 173 | directory, which is by default searched automatically (despite not |
| 174 | recursively). */ |
| 175 | #define LY_CTX_PREFER_SEARCHDIRS 0x20 /**< When searching for schema, prefer searchdirs instead of user callback. */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 176 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 177 | /** @} contextoptions */ |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 178 | |
| 179 | /** |
| 180 | * @brief Create libyang context. |
| 181 | * |
| 182 | * Context is used to hold all information about schemas. Usually, the application is supposed |
| 183 | * to work with a single context in which libyang is holding all schemas (and other internal |
| 184 | * information) according to which the data trees will be processed and validated. So, the schema |
| 185 | * trees are tightly connected with the specific context and they are held by the context internally |
| 186 | * - caller does not need to keep pointers to the schemas returned by lys_parse(), context knows |
| 187 | * about them. The data trees created with lyd_parse() are still connected with the specific context, |
| 188 | * but they are not internally held by the context. The data tree just points and lean on some data |
| 189 | * held by the context (schema tree, string dictionary, etc.). Therefore, in case of data trees, caller |
| 190 | * is supposed to keep pointers returned by the lyd_parse() and manage the data tree on its own. This |
| 191 | * also affects the number of instances of both tree types. While you can have only one instance of |
| 192 | * specific schema connected with a single context, number of data tree instances is not connected. |
| 193 | * |
| 194 | * @param[in] search_dir Directory where libyang will search for the imported or included modules |
| 195 | * and submodules. If no such directory is available, NULL is accepted. |
| 196 | * @param[in] options Context options, see @ref contextoptions. |
| 197 | * @param[out] new_ctx Pointer to the created libyang context if LY_SUCCESS returned. |
| 198 | * @return LY_ERR return value. |
| 199 | */ |
| 200 | LY_ERR ly_ctx_new(const char *search_dir, int options, struct ly_ctx **new_ctx); |
| 201 | |
| 202 | /** |
| 203 | * @brief Add the search path into libyang context |
| 204 | * |
| 205 | * To reset search paths set in the context, use ly_ctx_unset_searchdirs() and then |
| 206 | * set search paths again. |
| 207 | * |
| 208 | * @param[in] ctx Context to be modified. |
| 209 | * @param[in] search_dir New search path to add to the current paths previously set in ctx. |
| 210 | * @return LY_ERR return value. |
| 211 | */ |
| 212 | LY_ERR ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir); |
| 213 | |
| 214 | /** |
| 215 | * @brief Clean the search path(s) from the libyang context |
| 216 | * |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 217 | * To remove the search path by its index, use ly_ctx_unset_searchdir(). |
| 218 | * |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 219 | * @param[in] ctx Context to be modified. |
| 220 | * @param[in] value Searchdir to be removed, use NULL to remove them all. |
| 221 | * @return LY_ERR return value |
| 222 | */ |
| 223 | LY_ERR ly_ctx_unset_searchdirs(struct ly_ctx *ctx, const char *value); |
| 224 | |
| 225 | /** |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 226 | * @brief Remove the specific search path from the libyang context. |
| 227 | * |
| 228 | * To remove the search path by its value, use ly_ctx_unset_searchdirs(). |
| 229 | * |
| 230 | * @param[in] ctx Context to be modified. |
| 231 | * @param[in] index Index of the searchdir to be removed. |
| 232 | * @return LY_ERR return value |
| 233 | */ |
| 234 | LY_ERR ly_ctx_unset_searchdir(struct ly_ctx *ctx, unsigned int index); |
| 235 | |
| 236 | /** |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 237 | * @brief Get the NULL-terminated list of the search paths in libyang context. Do not modify the result! |
| 238 | * |
| 239 | * @param[in] ctx Context to query. |
| 240 | * @return NULL-terminated list (array) of the search paths, NULL if no searchpath was set. |
| 241 | * Do not modify the provided data in any way! |
| 242 | */ |
| 243 | const char * const *ly_ctx_get_searchdirs(const struct ly_ctx *ctx); |
| 244 | |
| 245 | /** |
| 246 | * @brief Get the currently set context's options. |
| 247 | * |
| 248 | * @param[in] ctx Context to query. |
| 249 | * @return Combination of all the currently set context's options, see @ref contextoptions. |
| 250 | */ |
| 251 | int ly_ctx_get_options(const struct ly_ctx *ctx); |
| 252 | |
| 253 | /** |
| 254 | * @brief Set some of the context's options, see @ref contextoptions. |
| 255 | * @param[in] ctx Context to be modified. |
| 256 | * @param[in] option Combination of the context's options to be set, see @ref contextoptions. |
| 257 | * @return LY_ERR value. |
| 258 | */ |
Radek Krejci | 3fa46b6 | 2019-09-11 10:47:30 +0200 | [diff] [blame] | 259 | LY_ERR ly_ctx_set_options(struct ly_ctx *ctx, int option); |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 260 | |
| 261 | /** |
| 262 | * @brief Unset some of the context's options, see @ref contextoptions. |
| 263 | * @param[in] ctx Context to be modified. |
| 264 | * @param[in] option Combination of the context's options to be unset, see @ref contextoptions. |
| 265 | * @return LY_ERR value. |
| 266 | */ |
Radek Krejci | 3fa46b6 | 2019-09-11 10:47:30 +0200 | [diff] [blame] | 267 | LY_ERR ly_ctx_unset_options(struct ly_ctx *ctx, int option); |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 268 | |
| 269 | /** |
| 270 | * @brief Get current ID of the modules set. The value is available also |
| 271 | * as module-set-id in ly_ctx_info() result. |
| 272 | * |
| 273 | * @param[in] ctx Context to be examined. |
| 274 | * @return Numeric identifier of the current context's modules set. |
| 275 | */ |
| 276 | uint16_t ly_ctx_get_module_set_id(const struct ly_ctx *ctx); |
| 277 | |
| 278 | /** |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 279 | * @brief Callback for retrieving missing included or imported models in a custom way. |
| 280 | * |
| 281 | * When submod_name is provided, the submodule is requested instead of the module (in this case only |
| 282 | * the module name without its revision is provided). |
| 283 | * |
| 284 | * If an @arg free_module_data callback is provided, it will be used later to free the allegedly const data |
| 285 | * which were returned by this callback. |
| 286 | * |
| 287 | * @param[in] mod_name Missing module name. |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 288 | * @param[in] mod_rev Optional missing module revision. If NULL and submod_name is not provided, the latest revision is |
| 289 | * requested, the parsed module is then marked by the latest_revision flag. |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 290 | * @param[in] submod_name Optional missing submodule name. |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 291 | * @param[in] submod_rev Optional missing submodule revision. If NULL and submod_name is provided, the latest revision is |
| 292 | * requested, the parsed submodule is then marked by the latest_revision flag. |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 293 | * @param[in] user_data User-supplied callback data. |
| 294 | * @param[out] format Format of the returned module data. |
| 295 | * @param[out] module_data Requested module data. |
| 296 | * @param[out] free_module_data Callback for freeing the returned module data. If not set, the data will be left untouched. |
| 297 | * @return LY_ERR value. If the returned value differs from LY_SUCCESS, libyang continue in trying to get the module data |
| 298 | * according to the settings of its mechanism to search for the imported/included schemas. |
| 299 | */ |
| 300 | typedef LY_ERR (*ly_module_imp_clb)(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev, |
| 301 | void *user_data, LYS_INFORMAT *format, const char **module_data, |
| 302 | void (**free_module_data)(void *model_data, void *user_data)); |
| 303 | |
| 304 | /** |
| 305 | * @brief Get the custom callback for missing import/include module retrieval. |
| 306 | * |
| 307 | * @param[in] ctx Context to read from. |
| 308 | * @param[in] user_data Optional pointer for getting the user-supplied callback data. |
| 309 | * @return Callback or NULL if not set. |
| 310 | */ |
| 311 | ly_module_imp_clb ly_ctx_get_module_imp_clb(const struct ly_ctx *ctx, void **user_data); |
| 312 | |
| 313 | /** |
| 314 | * @brief Set missing include or import module callback. It is meant to be used when the models |
| 315 | * are not locally available (such as when downloading modules from a NETCONF server), it should |
| 316 | * not be required in other cases. |
| 317 | * |
| 318 | * @param[in] ctx Context that will use this callback. |
| 319 | * @param[in] clb Callback responsible for returning the missing model. |
| 320 | * @param[in] user_data Arbitrary data that will always be passed to the callback \p clb. |
| 321 | */ |
| 322 | void ly_ctx_set_module_imp_clb(struct ly_ctx *ctx, ly_module_imp_clb clb, void *user_data); |
| 323 | |
| 324 | /** |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 325 | * @brief Get YANG module of the given name and revision. |
| 326 | * |
| 327 | * @param[in] ctx Context to work in. |
| 328 | * @param[in] name Name of the YANG module to get. |
| 329 | * @param[in] revision Requested revision date of the YANG module to get. If not specified, |
| 330 | * the schema with no revision is returned, if it is present in the context. |
| 331 | * @return Pointer to the YANG module, NULL if no schema in the context follows the name and revision requirements. |
| 332 | */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 333 | struct lys_module *ly_ctx_get_module(const struct ly_ctx *ctx, const char *name, const char *revision); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 334 | |
| 335 | /** |
| 336 | * @brief Get the latest revision of the YANG module specified by its name. |
| 337 | * |
| 338 | * YANG modules with no revision are supposed to be the oldest one. |
| 339 | * |
| 340 | * @param[in] ctx Context where to search. |
| 341 | * @param[in] name Name of the YANG module to get. |
| 342 | * @return The latest revision of the specified YANG module in the given context, NULL if no YANG module of the |
| 343 | * given name is present in the context. |
| 344 | */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 345 | struct lys_module *ly_ctx_get_module_latest(const struct ly_ctx *ctx, const char *name); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 346 | |
| 347 | /** |
| 348 | * @brief Get the (only) implemented YANG module specified by its name. |
| 349 | * |
| 350 | * @param[in] ctx Context where to search. |
| 351 | * @param[in] name Name of the YANG module to get. |
| 352 | * @return The only implemented YANG module revision of the given name in the given context. NULL if there is no |
| 353 | * implemented module of the given name. |
| 354 | */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 355 | struct lys_module *ly_ctx_get_module_implemented(const struct ly_ctx *ctx, const char *name); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 356 | |
| 357 | /** |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 358 | * @brief Iterate over all modules in the given context. |
| 359 | * |
| 360 | * @param[in] ctx Context with the modules. |
| 361 | * @param[in,out] index Index of the next module to get. Value of 0 starts from the beginning. |
| 362 | * The value is updated with each call, so to iterate over all modules the same variable is supposed |
| 363 | * to be used in all calls starting with value 0. |
| 364 | * @return Next context module, NULL if the last was already returned. |
| 365 | */ |
| 366 | const struct lys_module *ly_ctx_get_module_iter(const struct ly_ctx *ctx, unsigned int *index); |
| 367 | |
| 368 | /** |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 369 | * @brief Get YANG module of the given namespace and revision. |
| 370 | * |
| 371 | * @param[in] ctx Context to work in. |
| 372 | * @param[in] ns Namespace of the YANG module to get. |
| 373 | * @param[in] revision Requested revision date of the YANG module to get. If not specified, |
| 374 | * the schema with no revision is returned, if it is present in the context. |
| 375 | * @return Pointer to the YANG module, NULL if no schema in the context follows the namespace and revision requirements. |
| 376 | */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 377 | struct lys_module *ly_ctx_get_module_ns(const struct ly_ctx *ctx, const char *ns, const char *revision); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 378 | |
| 379 | /** |
| 380 | * @brief Get the latest revision of the YANG module specified by its namespace. |
| 381 | * |
| 382 | * YANG modules with no revision are supposed to be the oldest one. |
| 383 | * |
| 384 | * @param[in] ctx Context where to search. |
| 385 | * @param[in] ns Namespace of the YANG module to get. |
| 386 | * @return The latest revision of the specified YANG module in the given context, NULL if no YANG module of the |
| 387 | * given namespace is present in the context. |
| 388 | */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 389 | struct lys_module *ly_ctx_get_module_latest_ns(const struct ly_ctx *ctx, const char *ns); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 390 | |
| 391 | /** |
| 392 | * @brief Get the (only) implemented YANG module specified by its namespace. |
| 393 | * |
| 394 | * @param[in] ctx Context where to search. |
| 395 | * @param[in] ns Namespace of the YANG module to get. |
| 396 | * @return The only implemented YANG module revision of the given namespace in the given context. NULL if there is no |
| 397 | * implemented module of the given namespace. |
| 398 | */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 399 | struct lys_module *ly_ctx_get_module_implemented_ns(const struct ly_ctx *ctx, const char *ns); |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 400 | |
| 401 | /** |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 402 | * @brief Reset cached latest revision information of the schemas in the context. |
| 403 | * |
| 404 | * When a (sub)module is imported/included without revision, the latest revision is |
| 405 | * searched. libyang searches for the latest revision in searchdirs and/or via provided |
| 406 | * import callback ly_module_imp_clb() just once. Then it is expected that the content |
| 407 | * of searchdirs or data returned by the callback does not change. So when it changes, |
| 408 | * it is necessary to force searching for the latest revision in case of loading another |
| 409 | * module, which what this function does. |
| 410 | * |
| 411 | * The latest revision information is also reset when the searchdirs set changes via |
| 412 | * ly_ctx_set_searchdir(). |
| 413 | * |
| 414 | * @param[in] ctx libyang context where the latest revision information is going to be reset. |
| 415 | */ |
| 416 | void ly_ctx_reset_latests(struct ly_ctx *ctx); |
| 417 | |
| 418 | /** |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 419 | * @brief Try to find the model in the searchpaths of \p ctx and load it into it. If custom missing |
| 420 | * module callback is set, it is used instead. |
| 421 | * |
| 422 | * The context itself is searched for the requested module first. If \p revision is not specified |
| 423 | * (the module of the latest revision is requested) and there is implemented revision of the requested |
| 424 | * module in the context, this implemented revision is returned despite there might be a newer revision. |
| 425 | * This behavior is cause by the fact that it is not possible to have multiple implemented revisions of |
| 426 | * the same module in the context. |
| 427 | * |
| 428 | * @param[in] ctx Context to add to. |
| 429 | * @param[in] name Name of the module to load. |
| 430 | * @param[in] revision Optional revision date of the module. If not specified, the latest revision is loaded. |
| 431 | * @return Pointer to the data model structure, NULL if not found or some error occurred. |
| 432 | */ |
| 433 | const struct lys_module *ly_ctx_load_module(struct ly_ctx *ctx, const char *name, const char *revision); |
| 434 | |
| 435 | /** |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 436 | * @brief Get current ID of the modules set. The value is available also |
| 437 | * as module-set-id in ::ly_ctx_get_yanglib_data() result. |
| 438 | * |
| 439 | * @param[in] ctx Context to be examined. |
| 440 | * @return Numeric identifier of the current context's modules set. |
| 441 | */ |
| 442 | uint16_t ly_ctx_get_yanglib_id(const struct ly_ctx *ctx); |
| 443 | |
| 444 | /** |
| 445 | * @brief Get data of the internal ietf-yang-library module with information about all the loaded modules. |
| 446 | * ietf-yang-library module must be loaded. |
| 447 | * |
| 448 | * @param[in] ctx Context with the modules. |
| 449 | * @return Generated data, must be freed, |
| 450 | * @return NULL on error. |
| 451 | */ |
| 452 | struct lyd_node *ly_ctx_get_yanglib_data(const struct ly_ctx *ctx); |
| 453 | |
| 454 | /** |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 455 | * @brief Free all internal structures of the specified context. |
| 456 | * |
| 457 | * The function should be used before terminating the application to destroy |
| 458 | * and free all structures internally used by libyang. If the caller uses |
| 459 | * multiple contexts, the function should be called for each used context. |
| 460 | * |
| 461 | * All instance data are supposed to be freed before destroying the context. |
| 462 | * Data models are destroyed automatically as part of ly_ctx_destroy() call. |
| 463 | * |
| 464 | * @param[in] ctx libyang context to destroy |
| 465 | * @param[in] private_destructor Optional destructor function for private objects assigned |
| 466 | * to the nodes via lys_set_private(). If NULL, the private objects are not freed by libyang. |
| 467 | * Remember the differences between the structures derived from ::lysc_node and always check |
| 468 | * ::lysc_node#nodetype. |
| 469 | */ |
| 470 | void ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lysc_node *node, void *priv)); |
| 471 | |
| 472 | /** @} context */ |
| 473 | |
| 474 | #ifdef __cplusplus |
| 475 | } |
| 476 | #endif |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 477 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 478 | #endif /* LY_CONTEXT_H_ */ |