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 | * |
Michal Vasko | 25d6ad0 | 2020-10-22 12:20:22 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2020 CESNET, z.s.p.o. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 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 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 27 | struct lyd_node; |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 28 | struct lysc_node; |
| 29 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 30 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 31 | * @page howtoContext Context |
Radek Krejci | 52785a2 | 2019-09-11 12:57:26 +0200 | [diff] [blame] | 32 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 33 | * The context concept allows callers to work in environments with different sets of YANG modules. |
Radek Krejci | 52785a2 | 2019-09-11 12:57:26 +0200 | [diff] [blame] | 34 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 35 | * The first step with libyang is to create a new context using ::ly_ctx_new(). It returns a handler used in the following work. |
| 36 | * Note that the context is supposed to provide a stable environment for work with the data. Therefore the caller should prepare |
| 37 | * a complete context and after starting working with the data, the context and its content should not change. Despite the API |
| 38 | * does not enforce this approach, it may change in future versions in the form of a locking mechanism which would allow further |
| 39 | * optimization of data manipulation. Also note that modules cannot be removed from their context. If you need to change the set |
| 40 | * of the schema modules in the context, the recommended way is to create a new context. To remove the context, there is ::ly_ctx_destroy() function. |
Radek Krejci | 52785a2 | 2019-09-11 12:57:26 +0200 | [diff] [blame] | 41 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 42 | * The context has [several options](@ref contextoptions) changing behavior when processing YANG modules being inserted. The |
| 43 | * specific behavior is mentioned below. All the options can be set as a parameter when the context is being created or later |
| 44 | * with ::ly_ctx_set_options(). |
| 45 | * |
| 46 | * When creating a new context, another optional parameter is search_dir It provide directory where libyang |
| 47 | * will automatically search for YANG modules being imported or included. There is actually a set of search paths which can be later |
| 48 | * modified using ::ly_ctx_set_searchdir(), ::ly_ctx_unset_searchdir() and ::ly_ctx_unset_searchdir_last() functions. Before the values |
| 49 | * in the set are used, also the current working directory is (non-recursively) searched. For the case of the explicitly set |
| 50 | * search directories, they are searched recursively - all their subdirectories (and symlinks) are taken into account. Searching |
| 51 | * in the current working directory can be avoided with the context's ::LY_CTX_DISABLE_SEARCHDIR_CWD option. |
Radek Krejci | 52785a2 | 2019-09-11 12:57:26 +0200 | [diff] [blame] | 52 | * Searching in all the context's search dirs (without removing them) can be avoided with the context's |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 53 | * ::LY_CTX_DISABLE_SEARCHDIRS option (or via ::ly_ctx_set_options()). This automatic searching can be preceded |
| 54 | * by a custom module searching callback (::ly_module_imp_clb) set via ::ly_ctx_set_module_imp_clb(). The algorithm of |
| 55 | * searching in search dirs is also available via API as ::lys_search_localfile() function. |
Radek Krejci | 52785a2 | 2019-09-11 12:57:26 +0200 | [diff] [blame] | 56 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 57 | * YANG modules are added into the context using [parser functions](@ref howtoSchemaParsers) - \b lys_parse*(). |
| 58 | * Alternatively, also ::ly_ctx_load_module() can be used - in that case the ::ly_module_imp_clb or automatic |
| 59 | * search in search directories and in the current working directory is used, as described above. YANG submodules cannot be loaded |
| 60 | * or even validated directly, they are loaded always only as includes of YANG modules. Explicitly parsed/loaded modules are |
| 61 | * handled as implemented - libyang is able to instantiate data representing such a module. The modules loaded implicitly, are |
| 62 | * not implemented and serve only as a source of grouping or typedef definitions. Context can hold multiple revisions of the same |
| 63 | * YANG module, but only one of them can be implemented. Details about the difference between implemented and imported modules |
Michal Vasko | 25d6ad0 | 2020-10-22 12:20:22 +0200 | [diff] [blame] | 64 | * can be found on @ref howtoSchema page. This behavior can be changed with the context's ::LY_CTX_ALL_IMPLEMENTED option, which |
| 65 | * causes that all the parsed modules, whether loaded explicitly or implicitly, are set to be implemented. Note, that as |
| 66 | * a consequence of this option, only a single revision of any module can be present in the context in this case. Also, a less |
| 67 | * crude option ::LY_CTX_REF_IMPLEMENTED can be used to implement only referenced modules that should also be implemented. |
Radek Krejci | 52785a2 | 2019-09-11 12:57:26 +0200 | [diff] [blame] | 68 | * |
| 69 | * When loading/importing a module without revision, the latest revision of the required module is supposed to load. |
| 70 | * For a context, the first time the latest revision of a module is requested, it is properly searched for and loaded. |
| 71 | * However, when this module is requested (without revision) the second time, the one found previously is returned. |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 72 | * This has the advantage of not searching for the module repeatedly but there is a drawback in case the content of search |
| 73 | * directories is updated and a later revision become available. However, to force libyang to re-search the |
| 74 | * latest revision, ::ly_ctx_reset_latests() can be used (note that it applies to all the modules in the context). |
Radek Krejci | 52785a2 | 2019-09-11 12:57:26 +0200 | [diff] [blame] | 75 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 76 | * Context holds all the schema modules internally. To get a specific module, use ::ly_ctx_get_module() (or some of its |
| 77 | * variants). If you need to do something with all the modules in the context, it is advised to iterate over them using |
| 78 | * ::ly_ctx_get_module_iter(). Alternatively, the ::ly_ctx_get_yanglib_data() function can be used to get complex information about the schemas in the context |
| 79 | * in the form of data tree defined by <a href="https://tools.ietf.org/html/rfc7895">ietf-yang-library</a> module. |
| 80 | * To get a specific node defined in a module in the context, use ::ly_ctx_get_node(). |
Radek Krejci | 52785a2 | 2019-09-11 12:57:26 +0200 | [diff] [blame] | 81 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 82 | * YANG data can be parsed by \b lyd_parse_*() functions. Note, that functions for schema have \b lys_ |
| 83 | * prefix (or \b lysp_ for the parsed and \b lysc_ for the compiled schema - for details see @ref howtoSchema page) while |
| 84 | * functions for instance data have \b lyd_ prefix. Details about data formats or handling data without the appropriate |
| 85 | * YANG module in context can be found on @ref howtoData page. |
Radek Krejci | 52785a2 | 2019-09-11 12:57:26 +0200 | [diff] [blame] | 86 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 87 | * Besides the YANG modules, context holds also [error information](@ref howtoErrors) and |
| 88 | * [database of strings](@ref howtoContextDict), both connected with the processed YANG modules and data. |
| 89 | * |
| 90 | * - @subpage howtoErrors |
| 91 | * - @subpage howtoContextDict |
Radek Krejci | 52785a2 | 2019-09-11 12:57:26 +0200 | [diff] [blame] | 92 | * |
| 93 | * \note API for this group of functions is available in the [context module](@ref context). |
| 94 | * |
| 95 | * Functions List |
| 96 | * -------------- |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 97 | * |
Radek Krejci | 52785a2 | 2019-09-11 12:57:26 +0200 | [diff] [blame] | 98 | * - ::ly_ctx_new() |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 99 | * - ::ly_ctx_destroy() |
| 100 | * |
Radek Krejci | 52785a2 | 2019-09-11 12:57:26 +0200 | [diff] [blame] | 101 | * - ::ly_ctx_set_searchdir() |
Radek Krejci | 52785a2 | 2019-09-11 12:57:26 +0200 | [diff] [blame] | 102 | * - ::ly_ctx_get_searchdirs() |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 103 | * - ::ly_ctx_unset_searchdir() |
| 104 | * - ::ly_ctx_unset_searchdir_last() |
| 105 | * |
| 106 | * - ::ly_ctx_set_options() |
| 107 | * - ::ly_ctx_get_options() |
| 108 | * - ::ly_ctx_unset_options() |
| 109 | * |
Radek Krejci | 52785a2 | 2019-09-11 12:57:26 +0200 | [diff] [blame] | 110 | * - ::ly_ctx_set_module_imp_clb() |
| 111 | * - ::ly_ctx_get_module_imp_clb() |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 112 | * |
Radek Krejci | 52785a2 | 2019-09-11 12:57:26 +0200 | [diff] [blame] | 113 | * - ::ly_ctx_load_module() |
Radek Krejci | 52785a2 | 2019-09-11 12:57:26 +0200 | [diff] [blame] | 114 | * - ::ly_ctx_get_module_iter() |
| 115 | * - ::ly_ctx_get_module() |
| 116 | * - ::ly_ctx_get_module_ns() |
| 117 | * - ::ly_ctx_get_module_implemented() |
| 118 | * - ::ly_ctx_get_module_implemented_ns() |
| 119 | * - ::ly_ctx_get_module_latest() |
| 120 | * - ::ly_ctx_get_module_latest_ns() |
| 121 | * - ::ly_ctx_reset_latests() |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 122 | * |
| 123 | * - ::ly_ctx_get_yanglib_data() |
| 124 | * - ::ly_ctx_get_yanglib_id() |
| 125 | * |
Radek Krejci | 52785a2 | 2019-09-11 12:57:26 +0200 | [diff] [blame] | 126 | * - ::ly_ctx_get_node() |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 127 | * - ::ly_ctx_get_module_set_id() |
| 128 | * - ::ly_ctx_internal_module_count() |
| 129 | * |
Radek Krejci | 52785a2 | 2019-09-11 12:57:26 +0200 | [diff] [blame] | 130 | * - ::lys_search_localfile() |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 131 | * - ::lys_set_implemented() |
| 132 | * |
| 133 | */ |
| 134 | |
| 135 | /** |
| 136 | * |
Radek Krejci | 52785a2 | 2019-09-11 12:57:26 +0200 | [diff] [blame] | 137 | */ |
| 138 | |
| 139 | /** |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 140 | * @defgroup context Context |
| 141 | * @{ |
| 142 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 143 | * Structures and functions to manipulate with the libyang context containers. |
| 144 | * |
| 145 | * The \em context concept allows callers to work in environments with different sets of YANG schemas. |
| 146 | * More detailed information can be found at @ref howtoContext page. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 147 | */ |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * @struct ly_ctx |
| 151 | * @brief libyang context handler. |
| 152 | */ |
| 153 | struct ly_ctx; |
| 154 | |
| 155 | /** |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 156 | * @ingroup context |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 157 | * @defgroup contextoptions Context options |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 158 | * |
| 159 | * Options to change context behavior. |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 160 | * |
| 161 | * Note that the flags 0xFF00 are reserved for internal use. |
| 162 | * |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 163 | * @{ |
| 164 | */ |
| 165 | |
Michal Vasko | 25d6ad0 | 2020-10-22 12:20:22 +0200 | [diff] [blame] | 166 | #define LY_CTX_ALL_IMPLEMENTED 0x01 /**< All the imported modules of the schema being parsed are implemented. */ |
| 167 | #define LY_CTX_REF_IMPLEMENTED 0x02 /**< Implement all imported modules "referenced" from an implemented module. |
| 168 | Normally, leafrefs, augment and deviation targets are implemented as |
| 169 | specified by YANG 1.1. In addition to this, implement any modules of |
| 170 | nodes referenced by when and must conditions and by any default values. |
| 171 | Generally, only if all these modules are implemented, the explicitly |
| 172 | implemented modules can be properly used and instantiated in data. */ |
| 173 | #define LY_CTX_NO_YANGLIBRARY 0x04 /**< Do not internally implement ietf-yang-library module. The option |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 174 | causes that function ::ly_ctx_get_yanglib_data() does not work (returns ::LY_EINVAL) until |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 175 | the ietf-yang-library module is loaded manually. While any revision |
| 176 | of this schema can be loaded with this option, note that the only |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 177 | revisions implemented by ::ly_ctx_get_yanglib_data() are 2016-06-21 and 2019-01-04. |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 178 | This option cannot be changed on existing context. */ |
| 179 | #define LY_CTX_DISABLE_SEARCHDIRS 0x08 /**< Do not search for schemas in context's searchdirs neither in current |
| 180 | working directory. It is entirely skipped and the only way to get |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 181 | schema data for imports or for ::ly_ctx_load_module() is to use the |
| 182 | callbacks provided by caller via ::ly_ctx_set_module_imp_clb() */ |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 183 | #define LY_CTX_DISABLE_SEARCHDIR_CWD 0x10 /**< Do not automatically search for schemas in current working |
| 184 | directory, which is by default searched automatically (despite not |
| 185 | recursively). */ |
| 186 | #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] | 187 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 188 | /** @} contextoptions */ |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 189 | |
| 190 | /** |
| 191 | * @brief Create libyang context. |
| 192 | * |
| 193 | * Context is used to hold all information about schemas. Usually, the application is supposed |
| 194 | * to work with a single context in which libyang is holding all schemas (and other internal |
| 195 | * information) according to which the data trees will be processed and validated. So, the schema |
| 196 | * trees are tightly connected with the specific context and they are held by the context internally |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 197 | * - caller does not need to keep pointers to the schemas returned by ::lys_parse(), context knows |
| 198 | * about them. The data trees created with \b lyd_parse_*() are still connected with the specific context, |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 199 | * but they are not internally held by the context. The data tree just points and lean on some data |
| 200 | * held by the context (schema tree, string dictionary, etc.). Therefore, in case of data trees, caller |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 201 | * is supposed to keep pointers returned by the \b lyd_parse_*() functions and manage the data tree on its own. This |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 202 | * also affects the number of instances of both tree types. While you can have only one instance of |
| 203 | * specific schema connected with a single context, number of data tree instances is not connected. |
| 204 | * |
| 205 | * @param[in] search_dir Directory where libyang will search for the imported or included modules |
| 206 | * and submodules. If no such directory is available, NULL is accepted. |
| 207 | * @param[in] options Context options, see @ref contextoptions. |
| 208 | * @param[out] new_ctx Pointer to the created libyang context if LY_SUCCESS returned. |
| 209 | * @return LY_ERR return value. |
| 210 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 211 | LY_ERR ly_ctx_new(const char *search_dir, uint16_t options, struct ly_ctx **new_ctx); |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 212 | |
| 213 | /** |
| 214 | * @brief Add the search path into libyang context |
| 215 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 216 | * To reset search paths set in the context, use ::ly_ctx_unset_searchdir() and then |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 217 | * set search paths again. |
| 218 | * |
| 219 | * @param[in] ctx Context to be modified. |
| 220 | * @param[in] search_dir New search path to add to the current paths previously set in ctx. |
| 221 | * @return LY_ERR return value. |
| 222 | */ |
| 223 | LY_ERR ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir); |
| 224 | |
| 225 | /** |
| 226 | * @brief Clean the search path(s) from the libyang context |
| 227 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 228 | * To remove the recently added search path(s), use ::ly_ctx_unset_searchdir_last(). |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 229 | * |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 230 | * @param[in] ctx Context to be modified. |
| 231 | * @param[in] value Searchdir to be removed, use NULL to remove them all. |
| 232 | * @return LY_ERR return value |
| 233 | */ |
Radek Krejci | e58f97f | 2020-08-18 11:45:08 +0200 | [diff] [blame] | 234 | LY_ERR ly_ctx_unset_searchdir(struct ly_ctx *ctx, const char *value); |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 235 | |
| 236 | /** |
Radek Krejci | e58f97f | 2020-08-18 11:45:08 +0200 | [diff] [blame] | 237 | * @brief Remove the least recently added search path(s) from the libyang context. |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 238 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 239 | * To remove a specific search path by its value, use ::ly_ctx_unset_searchdir(). |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 240 | * |
| 241 | * @param[in] ctx Context to be modified. |
Radek Krejci | e58f97f | 2020-08-18 11:45:08 +0200 | [diff] [blame] | 242 | * @param[in] count Number of the searchdirs to be removed (starting by the least recently added). |
| 243 | * If the value is higher then the actual number of search paths, all paths are removed and no error is returned. |
| 244 | * Value 0 does not change the search path set. |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 245 | * @return LY_ERR return value |
| 246 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 247 | LY_ERR ly_ctx_unset_searchdir_last(struct ly_ctx *ctx, uint32_t count); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 248 | |
| 249 | /** |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 250 | * @brief Get the NULL-terminated list of the search paths in libyang context. Do not modify the result! |
| 251 | * |
| 252 | * @param[in] ctx Context to query. |
| 253 | * @return NULL-terminated list (array) of the search paths, NULL if no searchpath was set. |
| 254 | * Do not modify the provided data in any way! |
| 255 | */ |
| 256 | const char * const *ly_ctx_get_searchdirs(const struct ly_ctx *ctx); |
| 257 | |
| 258 | /** |
| 259 | * @brief Get the currently set context's options. |
| 260 | * |
| 261 | * @param[in] ctx Context to query. |
| 262 | * @return Combination of all the currently set context's options, see @ref contextoptions. |
| 263 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 264 | uint16_t ly_ctx_get_options(const struct ly_ctx *ctx); |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 265 | |
| 266 | /** |
| 267 | * @brief Set some of the context's options, see @ref contextoptions. |
| 268 | * @param[in] ctx Context to be modified. |
| 269 | * @param[in] option Combination of the context's options to be set, see @ref contextoptions. |
| 270 | * @return LY_ERR value. |
| 271 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 272 | LY_ERR ly_ctx_set_options(struct ly_ctx *ctx, uint16_t option); |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 273 | |
| 274 | /** |
| 275 | * @brief Unset some of the context's options, see @ref contextoptions. |
| 276 | * @param[in] ctx Context to be modified. |
| 277 | * @param[in] option Combination of the context's options to be unset, see @ref contextoptions. |
| 278 | * @return LY_ERR value. |
| 279 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 280 | LY_ERR ly_ctx_unset_options(struct ly_ctx *ctx, uint16_t option); |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 281 | |
| 282 | /** |
| 283 | * @brief Get current ID of the modules set. The value is available also |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 284 | * as module-set-id in ::ly_ctx_get_yanglib_data() result. |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 285 | * |
| 286 | * @param[in] ctx Context to be examined. |
| 287 | * @return Numeric identifier of the current context's modules set. |
| 288 | */ |
| 289 | uint16_t ly_ctx_get_module_set_id(const struct ly_ctx *ctx); |
| 290 | |
| 291 | /** |
Michal Vasko | b2786d7 | 2020-08-24 13:19:52 +0200 | [diff] [blame] | 292 | * @brief Callback for freeing returned module data in #ly_module_imp_clb. |
| 293 | * |
| 294 | * @param[in] module_data Data to free. |
| 295 | * @param[in] user_data User-supplied callback data, same as for #ly_module_imp_clb. |
| 296 | */ |
| 297 | typedef void (*ly_module_imp_data_free_clb)(void *module_data, void *user_data); |
| 298 | |
| 299 | /** |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 300 | * @brief Callback for retrieving missing included or imported models in a custom way. |
| 301 | * |
| 302 | * When submod_name is provided, the submodule is requested instead of the module (in this case only |
| 303 | * the module name without its revision is provided). |
| 304 | * |
| 305 | * If an @arg free_module_data callback is provided, it will be used later to free the allegedly const data |
| 306 | * which were returned by this callback. |
| 307 | * |
| 308 | * @param[in] mod_name Missing module name. |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 309 | * @param[in] mod_rev Optional missing module revision. If NULL and submod_name is not provided, the latest revision is |
| 310 | * requested, the parsed module is then marked by the latest_revision flag. |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 311 | * @param[in] submod_name Optional missing submodule name. |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 312 | * @param[in] submod_rev Optional missing submodule revision. If NULL and submod_name is provided, the latest revision is |
| 313 | * requested, the parsed submodule is then marked by the latest_revision flag. |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 314 | * @param[in] user_data User-supplied callback data. |
| 315 | * @param[out] format Format of the returned module data. |
| 316 | * @param[out] module_data Requested module data. |
| 317 | * @param[out] free_module_data Callback for freeing the returned module data. If not set, the data will be left untouched. |
| 318 | * @return LY_ERR value. If the returned value differs from LY_SUCCESS, libyang continue in trying to get the module data |
| 319 | * according to the settings of its mechanism to search for the imported/included schemas. |
| 320 | */ |
| 321 | typedef LY_ERR (*ly_module_imp_clb)(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev, |
Michal Vasko | b2786d7 | 2020-08-24 13:19:52 +0200 | [diff] [blame] | 322 | void *user_data, LYS_INFORMAT *format, const char **module_data, ly_module_imp_data_free_clb *free_module_data); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 323 | |
| 324 | /** |
| 325 | * @brief Get the custom callback for missing import/include module retrieval. |
| 326 | * |
| 327 | * @param[in] ctx Context to read from. |
| 328 | * @param[in] user_data Optional pointer for getting the user-supplied callback data. |
| 329 | * @return Callback or NULL if not set. |
| 330 | */ |
| 331 | ly_module_imp_clb ly_ctx_get_module_imp_clb(const struct ly_ctx *ctx, void **user_data); |
| 332 | |
| 333 | /** |
| 334 | * @brief Set missing include or import module callback. It is meant to be used when the models |
| 335 | * are not locally available (such as when downloading modules from a NETCONF server), it should |
| 336 | * not be required in other cases. |
| 337 | * |
| 338 | * @param[in] ctx Context that will use this callback. |
| 339 | * @param[in] clb Callback responsible for returning the missing model. |
| 340 | * @param[in] user_data Arbitrary data that will always be passed to the callback \p clb. |
| 341 | */ |
| 342 | void ly_ctx_set_module_imp_clb(struct ly_ctx *ctx, ly_module_imp_clb clb, void *user_data); |
| 343 | |
| 344 | /** |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 345 | * @brief Get YANG module of the given name and revision. |
| 346 | * |
| 347 | * @param[in] ctx Context to work in. |
| 348 | * @param[in] name Name of the YANG module to get. |
| 349 | * @param[in] revision Requested revision date of the YANG module to get. If not specified, |
| 350 | * the schema with no revision is returned, if it is present in the context. |
| 351 | * @return Pointer to the YANG module, NULL if no schema in the context follows the name and revision requirements. |
| 352 | */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 353 | 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] | 354 | |
| 355 | /** |
| 356 | * @brief Get the latest revision of the YANG module specified by its name. |
| 357 | * |
| 358 | * YANG modules with no revision are supposed to be the oldest one. |
| 359 | * |
| 360 | * @param[in] ctx Context where to search. |
| 361 | * @param[in] name Name of the YANG module to get. |
| 362 | * @return The latest revision of the specified YANG module in the given context, NULL if no YANG module of the |
| 363 | * given name is present in the context. |
| 364 | */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 365 | 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] | 366 | |
| 367 | /** |
| 368 | * @brief Get the (only) implemented YANG module specified by its name. |
| 369 | * |
| 370 | * @param[in] ctx Context where to search. |
| 371 | * @param[in] name Name of the YANG module to get. |
| 372 | * @return The only implemented YANG module revision of the given name in the given context. NULL if there is no |
| 373 | * implemented module of the given name. |
| 374 | */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 375 | 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] | 376 | |
| 377 | /** |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 378 | * @brief Iterate over all modules in the given context. |
| 379 | * |
| 380 | * @param[in] ctx Context with the modules. |
| 381 | * @param[in,out] index Index of the next module to get. Value of 0 starts from the beginning. |
| 382 | * The value is updated with each call, so to iterate over all modules the same variable is supposed |
| 383 | * to be used in all calls starting with value 0. |
| 384 | * @return Next context module, NULL if the last was already returned. |
| 385 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 386 | const struct lys_module *ly_ctx_get_module_iter(const struct ly_ctx *ctx, uint32_t *index); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 387 | |
| 388 | /** |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 389 | * @brief Get YANG module of the given namespace and revision. |
| 390 | * |
| 391 | * @param[in] ctx Context to work in. |
| 392 | * @param[in] ns Namespace of the YANG module to get. |
| 393 | * @param[in] revision Requested revision date of the YANG module to get. If not specified, |
| 394 | * the schema with no revision is returned, if it is present in the context. |
| 395 | * @return Pointer to the YANG module, NULL if no schema in the context follows the namespace and revision requirements. |
| 396 | */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 397 | 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] | 398 | |
| 399 | /** |
| 400 | * @brief Get the latest revision of the YANG module specified by its namespace. |
| 401 | * |
| 402 | * YANG modules with no revision are supposed to be the oldest one. |
| 403 | * |
| 404 | * @param[in] ctx Context where to search. |
| 405 | * @param[in] ns Namespace of the YANG module to get. |
| 406 | * @return The latest revision of the specified YANG module in the given context, NULL if no YANG module of the |
| 407 | * given namespace is present in the context. |
| 408 | */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 409 | 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] | 410 | |
| 411 | /** |
| 412 | * @brief Get the (only) implemented YANG module specified by its namespace. |
| 413 | * |
| 414 | * @param[in] ctx Context where to search. |
| 415 | * @param[in] ns Namespace of the YANG module to get. |
| 416 | * @return The only implemented YANG module revision of the given namespace in the given context. NULL if there is no |
| 417 | * implemented module of the given namespace. |
| 418 | */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 419 | 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] | 420 | |
| 421 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 422 | * @brief Get a schema node based on the given data path (JSON format, see @ref howtoXPath). |
Michal Vasko | 308d0a7 | 2020-08-03 11:56:25 +0200 | [diff] [blame] | 423 | * |
| 424 | * @param[in] ctx libyang context, set for absolute paths. |
| 425 | * @param[in] ctx_node Starting context node for a relative data path, set for relative paths. |
Michal Vasko | 6229db0 | 2020-10-07 08:54:15 +0200 | [diff] [blame] | 426 | * @param[in] path JSON path of the node to get. |
Michal Vasko | 308d0a7 | 2020-08-03 11:56:25 +0200 | [diff] [blame] | 427 | * @param[in] output Search operation output instead of input. |
| 428 | * @return Found schema node or NULL. |
| 429 | */ |
Michal Vasko | 6229db0 | 2020-10-07 08:54:15 +0200 | [diff] [blame] | 430 | const struct lysc_node *ly_ctx_get_node(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *path, |
| 431 | ly_bool output); |
Michal Vasko | 308d0a7 | 2020-08-03 11:56:25 +0200 | [diff] [blame] | 432 | |
| 433 | /** |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 434 | * @brief Reset cached latest revision information of the schemas in the context. |
| 435 | * |
| 436 | * When a (sub)module is imported/included without revision, the latest revision is |
| 437 | * searched. libyang searches for the latest revision in searchdirs and/or via provided |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 438 | * import callback ::ly_module_imp_clb() just once. Then it is expected that the content |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 439 | * of searchdirs or data returned by the callback does not change. So when it changes, |
| 440 | * it is necessary to force searching for the latest revision in case of loading another |
| 441 | * module, which what this function does. |
| 442 | * |
| 443 | * The latest revision information is also reset when the searchdirs set changes via |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 444 | * ::ly_ctx_set_searchdir(). |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame] | 445 | * |
| 446 | * @param[in] ctx libyang context where the latest revision information is going to be reset. |
| 447 | */ |
| 448 | void ly_ctx_reset_latests(struct ly_ctx *ctx); |
| 449 | |
| 450 | /** |
Michal Vasko | de79e22 | 2020-08-10 11:55:46 +0200 | [diff] [blame] | 451 | * @brief Learn the number of internal modules of a context. Internal modules |
| 452 | * is considered one that was loaded during the context creation. |
| 453 | * |
| 454 | * @param[in] ctx libyang context to examine. |
| 455 | * @return Number of internal modules. |
| 456 | */ |
| 457 | uint32_t ly_ctx_internal_module_count(const struct ly_ctx *ctx); |
| 458 | |
| 459 | /** |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 460 | * @brief Try to find the model in the searchpaths of \p ctx and load it into it. If custom missing |
| 461 | * module callback is set, it is used instead. |
| 462 | * |
| 463 | * The context itself is searched for the requested module first. If \p revision is not specified |
| 464 | * (the module of the latest revision is requested) and there is implemented revision of the requested |
| 465 | * module in the context, this implemented revision is returned despite there might be a newer revision. |
| 466 | * This behavior is cause by the fact that it is not possible to have multiple implemented revisions of |
| 467 | * the same module in the context. |
| 468 | * |
| 469 | * @param[in] ctx Context to add to. |
| 470 | * @param[in] name Name of the module to load. |
| 471 | * @param[in] revision Optional revision date of the module. If not specified, the latest revision is loaded. |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 472 | * @param[in] features Optional array of features ended with NULL to be enabled if the module is being implemented. |
| 473 | * NULL for all features disabled and '*' for all enabled. |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 474 | * @return Pointer to the data model structure, NULL if not found or some error occurred. |
| 475 | */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 476 | const struct lys_module *ly_ctx_load_module(struct ly_ctx *ctx, const char *name, const char *revision, |
| 477 | const char **features); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 478 | |
| 479 | /** |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 480 | * @brief Get current ID of the modules set. The value is available also |
| 481 | * as module-set-id in ::ly_ctx_get_yanglib_data() result. |
| 482 | * |
| 483 | * @param[in] ctx Context to be examined. |
| 484 | * @return Numeric identifier of the current context's modules set. |
| 485 | */ |
| 486 | uint16_t ly_ctx_get_yanglib_id(const struct ly_ctx *ctx); |
| 487 | |
| 488 | /** |
| 489 | * @brief Get data of the internal ietf-yang-library module with information about all the loaded modules. |
| 490 | * ietf-yang-library module must be loaded. |
| 491 | * |
| 492 | * @param[in] ctx Context with the modules. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 493 | * @param[out] root Generated yang-library data. |
| 494 | * @return LY_ERR value |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 495 | */ |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 496 | LY_ERR ly_ctx_get_yanglib_data(const struct ly_ctx *ctx, struct lyd_node **root); |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 497 | |
| 498 | /** |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 499 | * @brief Free all internal structures of the specified context. |
| 500 | * |
| 501 | * The function should be used before terminating the application to destroy |
| 502 | * and free all structures internally used by libyang. If the caller uses |
| 503 | * multiple contexts, the function should be called for each used context. |
| 504 | * |
| 505 | * All instance data are supposed to be freed before destroying the context. |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 506 | * Data models are destroyed automatically as part of ::ly_ctx_destroy() call. |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 507 | * |
| 508 | * @param[in] ctx libyang context to destroy |
| 509 | * @param[in] private_destructor Optional destructor function for private objects assigned |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 510 | * to the nodes via ::lysc_set_private(). If NULL, the private objects are not freed by libyang. |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 511 | * Remember the differences between the structures derived from ::lysc_node and always check |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 512 | * ::lysc_node.nodetype. |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 513 | */ |
| 514 | void ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lysc_node *node, void *priv)); |
| 515 | |
| 516 | /** @} context */ |
| 517 | |
| 518 | #ifdef __cplusplus |
| 519 | } |
| 520 | #endif |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 521 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 522 | #endif /* LY_CONTEXT_H_ */ |