blob: ee48010d8f0bab3e87f148a21ebd98269469ec75 [file] [log] [blame]
Radek Krejci5aeea3a2018-09-05 13:29:36 +02001/**
2 * @file context.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief internal context structures and functions
5 *
Michal Vasko25d6ad02020-10-22 12:20:22 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejci5aeea3a2018-09-05 13:29:36 +02007 *
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 Krejcie7b95092019-05-15 11:03:07 +020018#include <stdint.h>
19
Radek Krejci6caa6ab2018-10-24 10:04:48 +020020#include "log.h"
Radek Krejcif0e1ba52020-05-22 15:14:35 +020021#include "parser_schema.h"
Radek Krejci5aeea3a2018-09-05 13:29:36 +020022
Radek Krejci6caa6ab2018-10-24 10:04:48 +020023#ifdef __cplusplus
24extern "C" {
25#endif
26
Michal Vasko3a41dff2020-07-15 14:30:28 +020027struct lyd_node;
Radek Krejci77114102021-03-10 15:21:57 +010028struct lys_module;
Radek Krejcica376bd2020-06-11 16:04:06 +020029struct lysc_node;
30
Radek Krejci5aeea3a2018-09-05 13:29:36 +020031/**
Radek Krejci8678fa42020-08-18 16:07:28 +020032 * @page howtoContext Context
Radek Krejci52785a22019-09-11 12:57:26 +020033 *
Radek Krejci8678fa42020-08-18 16:07:28 +020034 * The context concept allows callers to work in environments with different sets of YANG modules.
Radek Krejci52785a22019-09-11 12:57:26 +020035 *
Radek Krejci8678fa42020-08-18 16:07:28 +020036 * The first step with libyang is to create a new context using ::ly_ctx_new(). It returns a handler used in the following work.
37 * Note that the context is supposed to provide a stable environment for work with the data. Therefore the caller should prepare
38 * a complete context and after starting working with the data, the context and its content should not change. Despite the API
39 * does not enforce this approach, it may change in future versions in the form of a locking mechanism which would allow further
40 * optimization of data manipulation. Also note that modules cannot be removed from their context. If you need to change the set
41 * 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 Krejci52785a22019-09-11 12:57:26 +020042 *
Radek Krejci8678fa42020-08-18 16:07:28 +020043 * The context has [several options](@ref contextoptions) changing behavior when processing YANG modules being inserted. The
44 * specific behavior is mentioned below. All the options can be set as a parameter when the context is being created or later
45 * with ::ly_ctx_set_options().
46 *
47 * When creating a new context, another optional parameter is search_dir It provide directory where libyang
48 * will automatically search for YANG modules being imported or included. There is actually a set of search paths which can be later
49 * modified using ::ly_ctx_set_searchdir(), ::ly_ctx_unset_searchdir() and ::ly_ctx_unset_searchdir_last() functions. Before the values
50 * in the set are used, also the current working directory is (non-recursively) searched. For the case of the explicitly set
51 * search directories, they are searched recursively - all their subdirectories (and symlinks) are taken into account. Searching
52 * in the current working directory can be avoided with the context's ::LY_CTX_DISABLE_SEARCHDIR_CWD option.
Radek Krejci52785a22019-09-11 12:57:26 +020053 * Searching in all the context's search dirs (without removing them) can be avoided with the context's
Radek Krejci8678fa42020-08-18 16:07:28 +020054 * ::LY_CTX_DISABLE_SEARCHDIRS option (or via ::ly_ctx_set_options()). This automatic searching can be preceded
55 * by a custom module searching callback (::ly_module_imp_clb) set via ::ly_ctx_set_module_imp_clb(). The algorithm of
56 * searching in search dirs is also available via API as ::lys_search_localfile() function.
Radek Krejci52785a22019-09-11 12:57:26 +020057 *
Radek Krejci8678fa42020-08-18 16:07:28 +020058 * YANG modules are added into the context using [parser functions](@ref howtoSchemaParsers) - \b lys_parse*().
59 * Alternatively, also ::ly_ctx_load_module() can be used - in that case the ::ly_module_imp_clb or automatic
60 * search in search directories and in the current working directory is used, as described above. YANG submodules cannot be loaded
61 * or even validated directly, they are loaded always only as includes of YANG modules. Explicitly parsed/loaded modules are
62 * handled as implemented - libyang is able to instantiate data representing such a module. The modules loaded implicitly, are
63 * not implemented and serve only as a source of grouping or typedef definitions. Context can hold multiple revisions of the same
64 * YANG module, but only one of them can be implemented. Details about the difference between implemented and imported modules
Michal Vasko25d6ad02020-10-22 12:20:22 +020065 * can be found on @ref howtoSchema page. This behavior can be changed with the context's ::LY_CTX_ALL_IMPLEMENTED option, which
66 * causes that all the parsed modules, whether loaded explicitly or implicitly, are set to be implemented. Note, that as
67 * a consequence of this option, only a single revision of any module can be present in the context in this case. Also, a less
68 * crude option ::LY_CTX_REF_IMPLEMENTED can be used to implement only referenced modules that should also be implemented.
Radek Krejci52785a22019-09-11 12:57:26 +020069 *
70 * When loading/importing a module without revision, the latest revision of the required module is supposed to load.
71 * For a context, the first time the latest revision of a module is requested, it is properly searched for and loaded.
72 * However, when this module is requested (without revision) the second time, the one found previously is returned.
Radek Krejci8678fa42020-08-18 16:07:28 +020073 * This has the advantage of not searching for the module repeatedly but there is a drawback in case the content of search
74 * directories is updated and a later revision become available. However, to force libyang to re-search the
75 * latest revision, ::ly_ctx_reset_latests() can be used (note that it applies to all the modules in the context).
Radek Krejci52785a22019-09-11 12:57:26 +020076 *
Radek Krejci8678fa42020-08-18 16:07:28 +020077 * Context holds all the schema modules internally. To get a specific module, use ::ly_ctx_get_module() (or some of its
78 * variants). If you need to do something with all the modules in the context, it is advised to iterate over them using
79 * ::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
80 * in the form of data tree defined by <a href="https://tools.ietf.org/html/rfc7895">ietf-yang-library</a> module.
Radek Krejci52785a22019-09-11 12:57:26 +020081 *
Radek Krejci8678fa42020-08-18 16:07:28 +020082 * 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 Krejci52785a22019-09-11 12:57:26 +020086 *
Radek Krejci8678fa42020-08-18 16:07:28 +020087 * 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 Krejci52785a22019-09-11 12:57:26 +020092 *
93 * \note API for this group of functions is available in the [context module](@ref context).
94 *
95 * Functions List
96 * --------------
Radek Krejci8678fa42020-08-18 16:07:28 +020097 *
Radek Krejci52785a22019-09-11 12:57:26 +020098 * - ::ly_ctx_new()
Radek Krejci8678fa42020-08-18 16:07:28 +020099 * - ::ly_ctx_destroy()
100 *
Radek Krejci52785a22019-09-11 12:57:26 +0200101 * - ::ly_ctx_set_searchdir()
Radek Krejci52785a22019-09-11 12:57:26 +0200102 * - ::ly_ctx_get_searchdirs()
Radek Krejci8678fa42020-08-18 16:07:28 +0200103 * - ::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 Krejci52785a22019-09-11 12:57:26 +0200110 * - ::ly_ctx_set_module_imp_clb()
111 * - ::ly_ctx_get_module_imp_clb()
Radek Krejci8678fa42020-08-18 16:07:28 +0200112 *
Radek Krejci52785a22019-09-11 12:57:26 +0200113 * - ::ly_ctx_load_module()
Radek Krejci52785a22019-09-11 12:57:26 +0200114 * - ::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()
Michal Vasko8dc31992021-02-22 10:30:47 +0100121 * - ::ly_ctx_get_submodule()
122 * - ::ly_ctx_get_submodule_latest()
123 * - ::ly_ctx_get_submodule2()
124 * - ::ly_ctx_get_submodule2_latest()
Radek Krejci52785a22019-09-11 12:57:26 +0200125 * - ::ly_ctx_reset_latests()
Radek Krejci8678fa42020-08-18 16:07:28 +0200126 *
127 * - ::ly_ctx_get_yanglib_data()
128 * - ::ly_ctx_get_yanglib_id()
129 *
Radek Krejci8678fa42020-08-18 16:07:28 +0200130 * - ::ly_ctx_get_module_set_id()
Radek Krejci0a60f1d2020-10-26 22:24:43 +0100131 * - ::ly_ctx_internal_modules_count()
Radek Krejci8678fa42020-08-18 16:07:28 +0200132 *
Radek Krejci52785a22019-09-11 12:57:26 +0200133 * - ::lys_search_localfile()
Radek Krejci8678fa42020-08-18 16:07:28 +0200134 * - ::lys_set_implemented()
135 *
136 */
137
138/**
139 *
Radek Krejci52785a22019-09-11 12:57:26 +0200140 */
141
142/**
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200143 * @defgroup context Context
144 * @{
145 *
Radek Krejci8678fa42020-08-18 16:07:28 +0200146 * Structures and functions to manipulate with the libyang context containers.
147 *
148 * The \em context concept allows callers to work in environments with different sets of YANG schemas.
149 * More detailed information can be found at @ref howtoContext page.
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200150 */
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200151
152/**
153 * @struct ly_ctx
154 * @brief libyang context handler.
155 */
156struct ly_ctx;
157
158/**
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200159 * @ingroup context
Radek Krejci8678fa42020-08-18 16:07:28 +0200160 * @defgroup contextoptions Context options
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200161 *
162 * Options to change context behavior.
Radek Krejci474f9b82019-07-24 11:36:37 +0200163 *
164 * Note that the flags 0xFF00 are reserved for internal use.
165 *
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200166 * @{
167 */
168
Michal Vasko25d6ad02020-10-22 12:20:22 +0200169#define LY_CTX_ALL_IMPLEMENTED 0x01 /**< All the imported modules of the schema being parsed are implemented. */
170#define LY_CTX_REF_IMPLEMENTED 0x02 /**< Implement all imported modules "referenced" from an implemented module.
171 Normally, leafrefs, augment and deviation targets are implemented as
172 specified by YANG 1.1. In addition to this, implement any modules of
173 nodes referenced by when and must conditions and by any default values.
174 Generally, only if all these modules are implemented, the explicitly
175 implemented modules can be properly used and instantiated in data. */
176#define LY_CTX_NO_YANGLIBRARY 0x04 /**< Do not internally implement ietf-yang-library module. The option
Radek Krejci8678fa42020-08-18 16:07:28 +0200177 causes that function ::ly_ctx_get_yanglib_data() does not work (returns ::LY_EINVAL) until
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200178 the ietf-yang-library module is loaded manually. While any revision
179 of this schema can be loaded with this option, note that the only
Radek Krejci8678fa42020-08-18 16:07:28 +0200180 revisions implemented by ::ly_ctx_get_yanglib_data() are 2016-06-21 and 2019-01-04.
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200181 This option cannot be changed on existing context. */
182#define LY_CTX_DISABLE_SEARCHDIRS 0x08 /**< Do not search for schemas in context's searchdirs neither in current
183 working directory. It is entirely skipped and the only way to get
Radek Krejci8678fa42020-08-18 16:07:28 +0200184 schema data for imports or for ::ly_ctx_load_module() is to use the
185 callbacks provided by caller via ::ly_ctx_set_module_imp_clb() */
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200186#define LY_CTX_DISABLE_SEARCHDIR_CWD 0x10 /**< Do not automatically search for schemas in current working
187 directory, which is by default searched automatically (despite not
188 recursively). */
189#define LY_CTX_PREFER_SEARCHDIRS 0x20 /**< When searching for schema, prefer searchdirs instead of user callback. */
Radek Krejci0af46292019-01-11 16:02:31 +0100190
Michal Vaskob36053d2020-03-26 15:49:30 +0100191/** @} contextoptions */
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200192
193/**
194 * @brief Create libyang context.
195 *
196 * Context is used to hold all information about schemas. Usually, the application is supposed
197 * to work with a single context in which libyang is holding all schemas (and other internal
198 * information) according to which the data trees will be processed and validated. So, the schema
199 * trees are tightly connected with the specific context and they are held by the context internally
Radek Krejci8678fa42020-08-18 16:07:28 +0200200 * - caller does not need to keep pointers to the schemas returned by ::lys_parse(), context knows
201 * about them. The data trees created with \b lyd_parse_*() are still connected with the specific context,
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200202 * but they are not internally held by the context. The data tree just points and lean on some data
203 * held by the context (schema tree, string dictionary, etc.). Therefore, in case of data trees, caller
Radek Krejci8678fa42020-08-18 16:07:28 +0200204 * is supposed to keep pointers returned by the \b lyd_parse_*() functions and manage the data tree on its own. This
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200205 * also affects the number of instances of both tree types. While you can have only one instance of
206 * specific schema connected with a single context, number of data tree instances is not connected.
207 *
208 * @param[in] search_dir Directory where libyang will search for the imported or included modules
209 * and submodules. If no such directory is available, NULL is accepted.
210 * @param[in] options Context options, see @ref contextoptions.
211 * @param[out] new_ctx Pointer to the created libyang context if LY_SUCCESS returned.
212 * @return LY_ERR return value.
213 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200214LY_ERR ly_ctx_new(const char *search_dir, uint16_t options, struct ly_ctx **new_ctx);
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200215
216/**
217 * @brief Add the search path into libyang context
218 *
Radek Krejci8678fa42020-08-18 16:07:28 +0200219 * To reset search paths set in the context, use ::ly_ctx_unset_searchdir() and then
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200220 * set search paths again.
221 *
222 * @param[in] ctx Context to be modified.
223 * @param[in] search_dir New search path to add to the current paths previously set in ctx.
224 * @return LY_ERR return value.
225 */
226LY_ERR ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir);
227
228/**
229 * @brief Clean the search path(s) from the libyang context
230 *
Radek Krejci8678fa42020-08-18 16:07:28 +0200231 * To remove the recently added search path(s), use ::ly_ctx_unset_searchdir_last().
Radek Krejcied5acc52019-04-25 15:57:04 +0200232 *
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200233 * @param[in] ctx Context to be modified.
234 * @param[in] value Searchdir to be removed, use NULL to remove them all.
235 * @return LY_ERR return value
236 */
Radek Krejcie58f97f2020-08-18 11:45:08 +0200237LY_ERR ly_ctx_unset_searchdir(struct ly_ctx *ctx, const char *value);
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200238
239/**
Radek Krejcie58f97f2020-08-18 11:45:08 +0200240 * @brief Remove the least recently added search path(s) from the libyang context.
Radek Krejcied5acc52019-04-25 15:57:04 +0200241 *
Radek Krejci8678fa42020-08-18 16:07:28 +0200242 * To remove a specific search path by its value, use ::ly_ctx_unset_searchdir().
Radek Krejcied5acc52019-04-25 15:57:04 +0200243 *
244 * @param[in] ctx Context to be modified.
Radek Krejcie58f97f2020-08-18 11:45:08 +0200245 * @param[in] count Number of the searchdirs to be removed (starting by the least recently added).
246 * If the value is higher then the actual number of search paths, all paths are removed and no error is returned.
247 * Value 0 does not change the search path set.
Radek Krejcied5acc52019-04-25 15:57:04 +0200248 * @return LY_ERR return value
249 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200250LY_ERR ly_ctx_unset_searchdir_last(struct ly_ctx *ctx, uint32_t count);
Radek Krejcied5acc52019-04-25 15:57:04 +0200251
252/**
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200253 * @brief Get the NULL-terminated list of the search paths in libyang context. Do not modify the result!
254 *
255 * @param[in] ctx Context to query.
256 * @return NULL-terminated list (array) of the search paths, NULL if no searchpath was set.
257 * Do not modify the provided data in any way!
258 */
259const char * const *ly_ctx_get_searchdirs(const struct ly_ctx *ctx);
260
261/**
262 * @brief Get the currently set context's options.
263 *
264 * @param[in] ctx Context to query.
265 * @return Combination of all the currently set context's options, see @ref contextoptions.
266 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200267uint16_t ly_ctx_get_options(const struct ly_ctx *ctx);
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200268
269/**
270 * @brief Set some of the context's options, see @ref contextoptions.
271 * @param[in] ctx Context to be modified.
272 * @param[in] option Combination of the context's options to be set, see @ref contextoptions.
273 * @return LY_ERR value.
274 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200275LY_ERR ly_ctx_set_options(struct ly_ctx *ctx, uint16_t option);
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200276
277/**
278 * @brief Unset some of the context's options, see @ref contextoptions.
279 * @param[in] ctx Context to be modified.
280 * @param[in] option Combination of the context's options to be unset, see @ref contextoptions.
281 * @return LY_ERR value.
282 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200283LY_ERR ly_ctx_unset_options(struct ly_ctx *ctx, uint16_t option);
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200284
285/**
286 * @brief Get current ID of the modules set. The value is available also
Radek Krejci8678fa42020-08-18 16:07:28 +0200287 * as module-set-id in ::ly_ctx_get_yanglib_data() result.
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200288 *
289 * @param[in] ctx Context to be examined.
290 * @return Numeric identifier of the current context's modules set.
291 */
292uint16_t ly_ctx_get_module_set_id(const struct ly_ctx *ctx);
293
294/**
Michal Vaskob2786d72020-08-24 13:19:52 +0200295 * @brief Callback for freeing returned module data in #ly_module_imp_clb.
296 *
297 * @param[in] module_data Data to free.
298 * @param[in] user_data User-supplied callback data, same as for #ly_module_imp_clb.
299 */
300typedef void (*ly_module_imp_data_free_clb)(void *module_data, void *user_data);
301
302/**
Radek Krejcid33273d2018-10-25 14:55:52 +0200303 * @brief Callback for retrieving missing included or imported models in a custom way.
304 *
Radek Krejcibb9d2242021-01-04 10:51:31 +0100305 * When @p submod_name is provided, the submodule is requested instead of the module (in this case only
Radek Krejcid33273d2018-10-25 14:55:52 +0200306 * the module name without its revision is provided).
307 *
308 * If an @arg free_module_data callback is provided, it will be used later to free the allegedly const data
309 * which were returned by this callback.
310 *
311 * @param[in] mod_name Missing module name.
Radek Krejci086c7132018-10-26 15:29:04 +0200312 * @param[in] mod_rev Optional missing module revision. If NULL and submod_name is not provided, the latest revision is
313 * requested, the parsed module is then marked by the latest_revision flag.
Radek Krejcid33273d2018-10-25 14:55:52 +0200314 * @param[in] submod_name Optional missing submodule name.
Radek Krejci086c7132018-10-26 15:29:04 +0200315 * @param[in] submod_rev Optional missing submodule revision. If NULL and submod_name is provided, the latest revision is
316 * requested, the parsed submodule is then marked by the latest_revision flag.
Radek Krejcid33273d2018-10-25 14:55:52 +0200317 * @param[in] user_data User-supplied callback data.
318 * @param[out] format Format of the returned module data.
319 * @param[out] module_data Requested module data.
320 * @param[out] free_module_data Callback for freeing the returned module data. If not set, the data will be left untouched.
321 * @return LY_ERR value. If the returned value differs from LY_SUCCESS, libyang continue in trying to get the module data
322 * according to the settings of its mechanism to search for the imported/included schemas.
323 */
Radek Krejcibb9d2242021-01-04 10:51:31 +0100324typedef LY_ERR (*ly_module_imp_clb)(const char *mod_name, const char *mod_rev, const char *submod_name, const char *submod_rev,
Michal Vaskob2786d72020-08-24 13:19:52 +0200325 void *user_data, LYS_INFORMAT *format, const char **module_data, ly_module_imp_data_free_clb *free_module_data);
Radek Krejcid33273d2018-10-25 14:55:52 +0200326
327/**
328 * @brief Get the custom callback for missing import/include module retrieval.
329 *
330 * @param[in] ctx Context to read from.
331 * @param[in] user_data Optional pointer for getting the user-supplied callback data.
332 * @return Callback or NULL if not set.
333 */
334ly_module_imp_clb ly_ctx_get_module_imp_clb(const struct ly_ctx *ctx, void **user_data);
335
336/**
337 * @brief Set missing include or import module callback. It is meant to be used when the models
338 * are not locally available (such as when downloading modules from a NETCONF server), it should
339 * not be required in other cases.
340 *
341 * @param[in] ctx Context that will use this callback.
342 * @param[in] clb Callback responsible for returning the missing model.
343 * @param[in] user_data Arbitrary data that will always be passed to the callback \p clb.
344 */
345void ly_ctx_set_module_imp_clb(struct ly_ctx *ctx, ly_module_imp_clb clb, void *user_data);
346
347/**
Radek Krejcib7db73a2018-10-24 14:18:40 +0200348 * @brief Get YANG module of the given name and revision.
349 *
350 * @param[in] ctx Context to work in.
351 * @param[in] name Name of the YANG module to get.
352 * @param[in] revision Requested revision date of the YANG module to get. If not specified,
353 * the schema with no revision is returned, if it is present in the context.
354 * @return Pointer to the YANG module, NULL if no schema in the context follows the name and revision requirements.
355 */
Radek Krejci0af46292019-01-11 16:02:31 +0100356struct lys_module *ly_ctx_get_module(const struct ly_ctx *ctx, const char *name, const char *revision);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200357
358/**
359 * @brief Get the latest revision of the YANG module specified by its name.
360 *
361 * YANG modules with no revision are supposed to be the oldest one.
362 *
363 * @param[in] ctx Context where to search.
364 * @param[in] name Name of the YANG module to get.
365 * @return The latest revision of the specified YANG module in the given context, NULL if no YANG module of the
366 * given name is present in the context.
367 */
Radek Krejci0af46292019-01-11 16:02:31 +0100368struct lys_module *ly_ctx_get_module_latest(const struct ly_ctx *ctx, const char *name);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200369
370/**
371 * @brief Get the (only) implemented YANG module specified by its name.
372 *
373 * @param[in] ctx Context where to search.
374 * @param[in] name Name of the YANG module to get.
375 * @return The only implemented YANG module revision of the given name in the given context. NULL if there is no
376 * implemented module of the given name.
377 */
Radek Krejci0af46292019-01-11 16:02:31 +0100378struct lys_module *ly_ctx_get_module_implemented(const struct ly_ctx *ctx, const char *name);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200379
380/**
Radek Krejcied5acc52019-04-25 15:57:04 +0200381 * @brief Iterate over all modules in the given context.
382 *
383 * @param[in] ctx Context with the modules.
384 * @param[in,out] index Index of the next module to get. Value of 0 starts from the beginning.
385 * The value is updated with each call, so to iterate over all modules the same variable is supposed
386 * to be used in all calls starting with value 0.
387 * @return Next context module, NULL if the last was already returned.
388 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200389const struct lys_module *ly_ctx_get_module_iter(const struct ly_ctx *ctx, uint32_t *index);
Radek Krejcied5acc52019-04-25 15:57:04 +0200390
391/**
Radek Krejcib7db73a2018-10-24 14:18:40 +0200392 * @brief Get YANG module of the given namespace and revision.
393 *
394 * @param[in] ctx Context to work in.
395 * @param[in] ns Namespace of the YANG module to get.
396 * @param[in] revision Requested revision date of the YANG module to get. If not specified,
397 * the schema with no revision is returned, if it is present in the context.
398 * @return Pointer to the YANG module, NULL if no schema in the context follows the namespace and revision requirements.
399 */
Radek Krejci0af46292019-01-11 16:02:31 +0100400struct lys_module *ly_ctx_get_module_ns(const struct ly_ctx *ctx, const char *ns, const char *revision);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200401
402/**
403 * @brief Get the latest revision of the YANG module specified by its namespace.
404 *
405 * YANG modules with no revision are supposed to be the oldest one.
406 *
407 * @param[in] ctx Context where to search.
408 * @param[in] ns Namespace of the YANG module to get.
409 * @return The latest revision of the specified YANG module in the given context, NULL if no YANG module of the
410 * given namespace is present in the context.
411 */
Radek Krejci0af46292019-01-11 16:02:31 +0100412struct lys_module *ly_ctx_get_module_latest_ns(const struct ly_ctx *ctx, const char *ns);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200413
414/**
415 * @brief Get the (only) implemented YANG module specified by its namespace.
416 *
417 * @param[in] ctx Context where to search.
418 * @param[in] ns Namespace of the YANG module to get.
419 * @return The only implemented YANG module revision of the given namespace in the given context. NULL if there is no
420 * implemented module of the given namespace.
421 */
Radek Krejci0af46292019-01-11 16:02:31 +0100422struct lys_module *ly_ctx_get_module_implemented_ns(const struct ly_ctx *ctx, const char *ns);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200423
424/**
Michal Vasko8dc31992021-02-22 10:30:47 +0100425 * @brief Get a specific submodule from context. If its belongs-to module is known, use ::ly_ctx_get_submodule2().
426 *
427 * @param[in] ctx libyang context to search in.
428 * @param[in] submodule Submodule name to find.
429 * @param[in] revision Revision of the submodule to find, NULL for a submodule without a revision.
430 * @return Found submodule, NULL if there is none.
431 */
432const struct lysp_submodule *ly_ctx_get_submodule(const struct ly_ctx *ctx, const char *submodule, const char *revision);
433
434/**
435 * @brief Get the latests revision of a submodule from context. If its belongs-to module is known,
436 * use ::ly_ctx_get_submodule2_latest().
437 *
438 * @param[in] ctx libyang context to search in.
439 * @param[in] submodule Submodule name to find.
440 * @return Found submodule, NULL if there is none.
441 */
442const struct lysp_submodule *ly_ctx_get_submodule_latest(const struct ly_ctx *ctx, const char *submodule);
443
444/**
445 * @brief Get a specific submodule from a module. If the belongs-to module is not known, use ::ly_ctx_get_submodule().
446 *
447 * @param[in] module Belongs-to module to search in.
448 * @param[in] submodule Submodule name to find.
449 * @param[in] revision Revision of the submodule to find, NULL for a submodule without a revision.
450 * @return Found submodule, NULL if there is none.
451 */
452const struct lysp_submodule *ly_ctx_get_submodule2(const struct lys_module *module, const char *submodule,
453 const char *revision);
454
455/**
456 * @brief Get the latest revision of a submodule from a module. If the belongs-to module is not known,
457 * use ::ly_ctx_get_submodule_latest().
458 *
459 * @param[in] module Belongs-to module to search in.
460 * @param[in] submodule Submodule name to find.
461 * @return Found submodule, NULL if there is none.
462 */
463const struct lysp_submodule *ly_ctx_get_submodule2_latest(const struct lys_module *module, const char *submodule);
464
465/**
Radek Krejcie9e987e2018-10-31 12:50:27 +0100466 * @brief Reset cached latest revision information of the schemas in the context.
467 *
468 * When a (sub)module is imported/included without revision, the latest revision is
469 * searched. libyang searches for the latest revision in searchdirs and/or via provided
Radek Krejci8678fa42020-08-18 16:07:28 +0200470 * import callback ::ly_module_imp_clb() just once. Then it is expected that the content
Radek Krejcie9e987e2018-10-31 12:50:27 +0100471 * of searchdirs or data returned by the callback does not change. So when it changes,
472 * it is necessary to force searching for the latest revision in case of loading another
473 * module, which what this function does.
474 *
475 * The latest revision information is also reset when the searchdirs set changes via
Radek Krejci8678fa42020-08-18 16:07:28 +0200476 * ::ly_ctx_set_searchdir().
Radek Krejcie9e987e2018-10-31 12:50:27 +0100477 *
478 * @param[in] ctx libyang context where the latest revision information is going to be reset.
479 */
480void ly_ctx_reset_latests(struct ly_ctx *ctx);
481
482/**
Michal Vaskode79e222020-08-10 11:55:46 +0200483 * @brief Learn the number of internal modules of a context. Internal modules
484 * is considered one that was loaded during the context creation.
485 *
486 * @param[in] ctx libyang context to examine.
487 * @return Number of internal modules.
488 */
Radek Krejci0a60f1d2020-10-26 22:24:43 +0100489uint32_t ly_ctx_internal_modules_count(const struct ly_ctx *ctx);
Michal Vaskode79e222020-08-10 11:55:46 +0200490
491/**
Radek Krejcied5acc52019-04-25 15:57:04 +0200492 * @brief Try to find the model in the searchpaths of \p ctx and load it into it. If custom missing
493 * module callback is set, it is used instead.
494 *
495 * The context itself is searched for the requested module first. If \p revision is not specified
496 * (the module of the latest revision is requested) and there is implemented revision of the requested
497 * module in the context, this implemented revision is returned despite there might be a newer revision.
498 * This behavior is cause by the fact that it is not possible to have multiple implemented revisions of
499 * the same module in the context.
500 *
501 * @param[in] ctx Context to add to.
502 * @param[in] name Name of the module to load.
503 * @param[in] revision Optional revision date of the module. If not specified, the latest revision is loaded.
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100504 * @param[in] features Optional array of features ended with NULL to be enabled if the module is being implemented.
Radek Krejci2415f882021-01-20 16:27:09 +0100505 * The feature string '*' enables all and array of length 1 with only the terminating NULL explicitly disables all
506 * the features. In case the parameter is NULL, the features are untouched - left disabled in newly loaded module or
507 * with the current features settings in case the module is already present in the context.
Radek Krejcied5acc52019-04-25 15:57:04 +0200508 * @return Pointer to the data model structure, NULL if not found or some error occurred.
509 */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100510const struct lys_module *ly_ctx_load_module(struct ly_ctx *ctx, const char *name, const char *revision,
511 const char **features);
Radek Krejcied5acc52019-04-25 15:57:04 +0200512
513/**
Michal Vasko57c10cd2020-05-27 15:57:11 +0200514 * @brief Get current ID of the modules set. The value is available also
515 * as module-set-id in ::ly_ctx_get_yanglib_data() result.
516 *
517 * @param[in] ctx Context to be examined.
518 * @return Numeric identifier of the current context's modules set.
519 */
520uint16_t ly_ctx_get_yanglib_id(const struct ly_ctx *ctx);
521
522/**
523 * @brief Get data of the internal ietf-yang-library module with information about all the loaded modules.
524 * ietf-yang-library module must be loaded.
525 *
526 * @param[in] ctx Context with the modules.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200527 * @param[out] root Generated yang-library data.
528 * @return LY_ERR value
Michal Vasko57c10cd2020-05-27 15:57:11 +0200529 */
Michal Vasko3a41dff2020-07-15 14:30:28 +0200530LY_ERR ly_ctx_get_yanglib_data(const struct ly_ctx *ctx, struct lyd_node **root);
Michal Vasko57c10cd2020-05-27 15:57:11 +0200531
532/**
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200533 * @brief Free all internal structures of the specified context.
534 *
535 * The function should be used before terminating the application to destroy
536 * and free all structures internally used by libyang. If the caller uses
537 * multiple contexts, the function should be called for each used context.
538 *
539 * All instance data are supposed to be freed before destroying the context.
Radek Krejci8678fa42020-08-18 16:07:28 +0200540 * Data models are destroyed automatically as part of ::ly_ctx_destroy() call.
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200541 *
542 * @param[in] ctx libyang context to destroy
543 * @param[in] private_destructor Optional destructor function for private objects assigned
Michal Vasko14ed9cd2021-01-28 14:16:25 +0100544 * to the schema nodes' priv pointer. If NULL, the private objects are not freed by libyang.
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200545 */
546void ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lysc_node *node, void *priv));
547
548/** @} context */
549
550#ifdef __cplusplus
551}
552#endif
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200553
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200554#endif /* LY_CONTEXT_H_ */