blob: 65aa954f58c3d5d3f74b78e7a5b815ecd03905e5 [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 *
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 Krejci6caa6ab2018-10-24 10:04:48 +020018#include "log.h"
Radek Krejci5aeea3a2018-09-05 13:29:36 +020019#include "tree_schema.h"
20
Radek Krejci6caa6ab2018-10-24 10:04:48 +020021#ifdef __cplusplus
22extern "C" {
23#endif
24
Radek Krejci5aeea3a2018-09-05 13:29:36 +020025/**
Radek Krejci6caa6ab2018-10-24 10:04:48 +020026 * @defgroup context Context
27 * @{
28 *
29 * Structures and functions to manipulate with the libyang "containers". The \em context concept allows callers
30 * to work in environments with different sets of YANG schemas. More detailed information can be found at
31 * @ref howtocontext page.
Radek Krejci5aeea3a2018-09-05 13:29:36 +020032 */
Radek Krejci6caa6ab2018-10-24 10:04:48 +020033
34/**
35 * @struct ly_ctx
36 * @brief libyang context handler.
37 */
38struct ly_ctx;
39
40/**
41 * @defgroup contextoptions Context options
42 * @ingroup context
43 *
44 * Options to change context behavior.
45 * @{
46 */
47
48#define LY_CTX_ALLIMPLEMENTED 0x01 /**< All the imports of the schema being parsed are treated implemented. */
49#define LY_CTX_TRUSTED 0x02 /**< Handle the schema being parsed as trusted and skip its validation
50 tests. Note that while this option improves performance, it can
51 lead to an undefined behavior if the schema is not correct. */
52#define LY_CTX_NOYANGLIBRARY 0x04 /**< Do not internally implement ietf-yang-library module. The option
53 causes that function ly_ctx_info() does not work (returns NULL) until
54 the ietf-yang-library module is loaded manually. While any revision
55 of this schema can be loaded with this option, note that the only
56 revisions implemented by ly_ctx_info() are 2016-04-09 and 2018-01-17.
57 This option cannot be changed on existing context. */
58#define LY_CTX_DISABLE_SEARCHDIRS 0x08 /**< Do not search for schemas in context's searchdirs neither in current
59 working directory. It is entirely skipped and the only way to get
60 schema data for imports or for ly_ctx_load_module() is to use the
61 callbacks provided by caller via ly_ctx_set_module_imp_clb() */
62#define LY_CTX_DISABLE_SEARCHDIR_CWD 0x10 /**< Do not automatically search for schemas in current working
63 directory, which is by default searched automatically (despite not
64 recursively). */
65#define LY_CTX_PREFER_SEARCHDIRS 0x20 /**< When searching for schema, prefer searchdirs instead of user callback. */
Radek Krejci0af46292019-01-11 16:02:31 +010066
Radek Krejci6caa6ab2018-10-24 10:04:48 +020067/**@} contextoptions */
68
69/**
70 * @brief Create libyang context.
71 *
72 * Context is used to hold all information about schemas. Usually, the application is supposed
73 * to work with a single context in which libyang is holding all schemas (and other internal
74 * information) according to which the data trees will be processed and validated. So, the schema
75 * trees are tightly connected with the specific context and they are held by the context internally
76 * - caller does not need to keep pointers to the schemas returned by lys_parse(), context knows
77 * about them. The data trees created with lyd_parse() are still connected with the specific context,
78 * but they are not internally held by the context. The data tree just points and lean on some data
79 * held by the context (schema tree, string dictionary, etc.). Therefore, in case of data trees, caller
80 * is supposed to keep pointers returned by the lyd_parse() and manage the data tree on its own. This
81 * also affects the number of instances of both tree types. While you can have only one instance of
82 * specific schema connected with a single context, number of data tree instances is not connected.
83 *
84 * @param[in] search_dir Directory where libyang will search for the imported or included modules
85 * and submodules. If no such directory is available, NULL is accepted.
86 * @param[in] options Context options, see @ref contextoptions.
87 * @param[out] new_ctx Pointer to the created libyang context if LY_SUCCESS returned.
88 * @return LY_ERR return value.
89 */
90LY_ERR ly_ctx_new(const char *search_dir, int options, struct ly_ctx **new_ctx);
91
92/**
93 * @brief Add the search path into libyang context
94 *
95 * To reset search paths set in the context, use ly_ctx_unset_searchdirs() and then
96 * set search paths again.
97 *
98 * @param[in] ctx Context to be modified.
99 * @param[in] search_dir New search path to add to the current paths previously set in ctx.
100 * @return LY_ERR return value.
101 */
102LY_ERR ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir);
103
104/**
105 * @brief Clean the search path(s) from the libyang context
106 *
Radek Krejcied5acc52019-04-25 15:57:04 +0200107 * To remove the search path by its index, use ly_ctx_unset_searchdir().
108 *
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200109 * @param[in] ctx Context to be modified.
110 * @param[in] value Searchdir to be removed, use NULL to remove them all.
111 * @return LY_ERR return value
112 */
113LY_ERR ly_ctx_unset_searchdirs(struct ly_ctx *ctx, const char *value);
114
115/**
Radek Krejcied5acc52019-04-25 15:57:04 +0200116 * @brief Remove the specific search path from the libyang context.
117 *
118 * To remove the search path by its value, use ly_ctx_unset_searchdirs().
119 *
120 * @param[in] ctx Context to be modified.
121 * @param[in] index Index of the searchdir to be removed.
122 * @return LY_ERR return value
123 */
124LY_ERR ly_ctx_unset_searchdir(struct ly_ctx *ctx, unsigned int index);
125
126/**
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200127 * @brief Get the NULL-terminated list of the search paths in libyang context. Do not modify the result!
128 *
129 * @param[in] ctx Context to query.
130 * @return NULL-terminated list (array) of the search paths, NULL if no searchpath was set.
131 * Do not modify the provided data in any way!
132 */
133const char * const *ly_ctx_get_searchdirs(const struct ly_ctx *ctx);
134
135/**
136 * @brief Get the currently set context's options.
137 *
138 * @param[in] ctx Context to query.
139 * @return Combination of all the currently set context's options, see @ref contextoptions.
140 */
141int ly_ctx_get_options(const struct ly_ctx *ctx);
142
143/**
144 * @brief Set some of the context's options, see @ref contextoptions.
145 * @param[in] ctx Context to be modified.
146 * @param[in] option Combination of the context's options to be set, see @ref contextoptions.
147 * @return LY_ERR value.
148 */
149LY_ERR ly_ctx_set_option(struct ly_ctx *ctx, int option);
150
151/**
152 * @brief Unset some of the context's options, see @ref contextoptions.
153 * @param[in] ctx Context to be modified.
154 * @param[in] option Combination of the context's options to be unset, see @ref contextoptions.
155 * @return LY_ERR value.
156 */
157LY_ERR ly_ctx_unset_option(struct ly_ctx *ctx, int option);
158
159/**
160 * @brief Get current ID of the modules set. The value is available also
161 * as module-set-id in ly_ctx_info() result.
162 *
163 * @param[in] ctx Context to be examined.
164 * @return Numeric identifier of the current context's modules set.
165 */
166uint16_t ly_ctx_get_module_set_id(const struct ly_ctx *ctx);
167
168/**
Radek Krejcid33273d2018-10-25 14:55:52 +0200169 * @brief Callback for retrieving missing included or imported models in a custom way.
170 *
171 * When submod_name is provided, the submodule is requested instead of the module (in this case only
172 * the module name without its revision is provided).
173 *
174 * If an @arg free_module_data callback is provided, it will be used later to free the allegedly const data
175 * which were returned by this callback.
176 *
177 * @param[in] mod_name Missing module name.
Radek Krejci086c7132018-10-26 15:29:04 +0200178 * @param[in] mod_rev Optional missing module revision. If NULL and submod_name is not provided, the latest revision is
179 * requested, the parsed module is then marked by the latest_revision flag.
Radek Krejcid33273d2018-10-25 14:55:52 +0200180 * @param[in] submod_name Optional missing submodule name.
Radek Krejci086c7132018-10-26 15:29:04 +0200181 * @param[in] submod_rev Optional missing submodule revision. If NULL and submod_name is provided, the latest revision is
182 * requested, the parsed submodule is then marked by the latest_revision flag.
Radek Krejcid33273d2018-10-25 14:55:52 +0200183 * @param[in] user_data User-supplied callback data.
184 * @param[out] format Format of the returned module data.
185 * @param[out] module_data Requested module data.
186 * @param[out] free_module_data Callback for freeing the returned module data. If not set, the data will be left untouched.
187 * @return LY_ERR value. If the returned value differs from LY_SUCCESS, libyang continue in trying to get the module data
188 * according to the settings of its mechanism to search for the imported/included schemas.
189 */
190typedef LY_ERR (*ly_module_imp_clb)(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev,
191 void *user_data, LYS_INFORMAT *format, const char **module_data,
192 void (**free_module_data)(void *model_data, void *user_data));
193
194/**
195 * @brief Get the custom callback for missing import/include module retrieval.
196 *
197 * @param[in] ctx Context to read from.
198 * @param[in] user_data Optional pointer for getting the user-supplied callback data.
199 * @return Callback or NULL if not set.
200 */
201ly_module_imp_clb ly_ctx_get_module_imp_clb(const struct ly_ctx *ctx, void **user_data);
202
203/**
204 * @brief Set missing include or import module callback. It is meant to be used when the models
205 * are not locally available (such as when downloading modules from a NETCONF server), it should
206 * not be required in other cases.
207 *
208 * @param[in] ctx Context that will use this callback.
209 * @param[in] clb Callback responsible for returning the missing model.
210 * @param[in] user_data Arbitrary data that will always be passed to the callback \p clb.
211 */
212void ly_ctx_set_module_imp_clb(struct ly_ctx *ctx, ly_module_imp_clb clb, void *user_data);
213
214/**
Radek Krejcib7db73a2018-10-24 14:18:40 +0200215 * @brief Get YANG module of the given name and revision.
216 *
217 * @param[in] ctx Context to work in.
218 * @param[in] name Name of the YANG module to get.
219 * @param[in] revision Requested revision date of the YANG module to get. If not specified,
220 * the schema with no revision is returned, if it is present in the context.
221 * @return Pointer to the YANG module, NULL if no schema in the context follows the name and revision requirements.
222 */
Radek Krejci0af46292019-01-11 16:02:31 +0100223struct lys_module *ly_ctx_get_module(const struct ly_ctx *ctx, const char *name, const char *revision);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200224
225/**
226 * @brief Get the latest revision of the YANG module specified by its name.
227 *
228 * YANG modules with no revision are supposed to be the oldest one.
229 *
230 * @param[in] ctx Context where to search.
231 * @param[in] name Name of the YANG module to get.
232 * @return The latest revision of the specified YANG module in the given context, NULL if no YANG module of the
233 * given name is present in the context.
234 */
Radek Krejci0af46292019-01-11 16:02:31 +0100235struct lys_module *ly_ctx_get_module_latest(const struct ly_ctx *ctx, const char *name);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200236
237/**
238 * @brief Get the (only) implemented YANG module specified by its name.
239 *
240 * @param[in] ctx Context where to search.
241 * @param[in] name Name of the YANG module to get.
242 * @return The only implemented YANG module revision of the given name in the given context. NULL if there is no
243 * implemented module of the given name.
244 */
Radek Krejci0af46292019-01-11 16:02:31 +0100245struct lys_module *ly_ctx_get_module_implemented(const struct ly_ctx *ctx, const char *name);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200246
247/**
Radek Krejcied5acc52019-04-25 15:57:04 +0200248 * @brief Get the (only) implemented YANG module specified by its name.
249 *
250 * @param[in] ctx Context where to search.
251 * @param[in] name Name of the YANG module to get.
252 * @return The only implemented YANG module revision of the given name in the given context. NULL if there is no
253 * implemented module of the given name.
254 */
255/**
256 * @brief Iterate over all modules in the given context.
257 *
258 * @param[in] ctx Context with the modules.
259 * @param[in,out] index Index of the next module to get. Value of 0 starts from the beginning.
260 * The value is updated with each call, so to iterate over all modules the same variable is supposed
261 * to be used in all calls starting with value 0.
262 * @return Next context module, NULL if the last was already returned.
263 */
264const struct lys_module *ly_ctx_get_module_iter(const struct ly_ctx *ctx, unsigned int *index);
265
266/**
Radek Krejcib7db73a2018-10-24 14:18:40 +0200267 * @brief Get YANG module of the given namespace and revision.
268 *
269 * @param[in] ctx Context to work in.
270 * @param[in] ns Namespace of the YANG module to get.
271 * @param[in] revision Requested revision date of the YANG module to get. If not specified,
272 * the schema with no revision is returned, if it is present in the context.
273 * @return Pointer to the YANG module, NULL if no schema in the context follows the namespace and revision requirements.
274 */
Radek Krejci0af46292019-01-11 16:02:31 +0100275struct 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 +0200276
277/**
278 * @brief Get the latest revision of the YANG module specified by its namespace.
279 *
280 * YANG modules with no revision are supposed to be the oldest one.
281 *
282 * @param[in] ctx Context where to search.
283 * @param[in] ns Namespace of the YANG module to get.
284 * @return The latest revision of the specified YANG module in the given context, NULL if no YANG module of the
285 * given namespace is present in the context.
286 */
Radek Krejci0af46292019-01-11 16:02:31 +0100287struct lys_module *ly_ctx_get_module_latest_ns(const struct ly_ctx *ctx, const char *ns);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200288
289/**
290 * @brief Get the (only) implemented YANG module specified by its namespace.
291 *
292 * @param[in] ctx Context where to search.
293 * @param[in] ns Namespace of the YANG module to get.
294 * @return The only implemented YANG module revision of the given namespace in the given context. NULL if there is no
295 * implemented module of the given namespace.
296 */
Radek Krejci0af46292019-01-11 16:02:31 +0100297struct lys_module *ly_ctx_get_module_implemented_ns(const struct ly_ctx *ctx, const char *ns);
Radek Krejcib7db73a2018-10-24 14:18:40 +0200298
299/**
Radek Krejcie9e987e2018-10-31 12:50:27 +0100300 * @brief Reset cached latest revision information of the schemas in the context.
301 *
302 * When a (sub)module is imported/included without revision, the latest revision is
303 * searched. libyang searches for the latest revision in searchdirs and/or via provided
304 * import callback ly_module_imp_clb() just once. Then it is expected that the content
305 * of searchdirs or data returned by the callback does not change. So when it changes,
306 * it is necessary to force searching for the latest revision in case of loading another
307 * module, which what this function does.
308 *
309 * The latest revision information is also reset when the searchdirs set changes via
310 * ly_ctx_set_searchdir().
311 *
312 * @param[in] ctx libyang context where the latest revision information is going to be reset.
313 */
314void ly_ctx_reset_latests(struct ly_ctx *ctx);
315
316/**
Radek Krejci0af46292019-01-11 16:02:31 +0100317 * @brief Make the specific module implemented.
318 *
319 * @param[in] ctx libyang context to change.
320 * @param[in] mod Module from the given context to make implemented. It is not an error
321 * to provide already implemented module, it just does nothing.
322 * @return LY_SUCCESS or LY_EDENIED in case the context contains some other revision of the
323 * same module which is already implemented.
324 */
325LY_ERR ly_ctx_module_implement(struct ly_ctx *ctx, struct lys_module *mod);
326
327/**
Radek Krejcied5acc52019-04-25 15:57:04 +0200328 * @brief Try to find the model in the searchpaths of \p ctx and load it into it. If custom missing
329 * module callback is set, it is used instead.
330 *
331 * The context itself is searched for the requested module first. If \p revision is not specified
332 * (the module of the latest revision is requested) and there is implemented revision of the requested
333 * module in the context, this implemented revision is returned despite there might be a newer revision.
334 * This behavior is cause by the fact that it is not possible to have multiple implemented revisions of
335 * the same module in the context.
336 *
337 * @param[in] ctx Context to add to.
338 * @param[in] name Name of the module to load.
339 * @param[in] revision Optional revision date of the module. If not specified, the latest revision is loaded.
340 * @return Pointer to the data model structure, NULL if not found or some error occurred.
341 */
342const struct lys_module *ly_ctx_load_module(struct ly_ctx *ctx, const char *name, const char *revision);
343
344/**
Radek Krejci6caa6ab2018-10-24 10:04:48 +0200345 * @brief Free all internal structures of the specified context.
346 *
347 * The function should be used before terminating the application to destroy
348 * and free all structures internally used by libyang. If the caller uses
349 * multiple contexts, the function should be called for each used context.
350 *
351 * All instance data are supposed to be freed before destroying the context.
352 * Data models are destroyed automatically as part of ly_ctx_destroy() call.
353 *
354 * @param[in] ctx libyang context to destroy
355 * @param[in] private_destructor Optional destructor function for private objects assigned
356 * to the nodes via lys_set_private(). If NULL, the private objects are not freed by libyang.
357 * Remember the differences between the structures derived from ::lysc_node and always check
358 * ::lysc_node#nodetype.
359 */
360void ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lysc_node *node, void *priv));
361
362/** @} context */
363
364#ifdef __cplusplus
365}
366#endif
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200367
Radek Krejci5aeea3a2018-09-05 13:29:36 +0200368#endif /* LY_CONTEXT_H_ */