Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file context.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | 0ace61d | 2023-05-09 15:18:58 +0200 | [diff] [blame] | 4 | * @author Michal Vasko <mvasko@cesnet.cz> |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 5 | * @brief internal context structures and functions |
| 6 | * |
Michal Vasko | 0ace61d | 2023-05-09 15:18:58 +0200 | [diff] [blame] | 7 | * Copyright (c) 2015 - 2023 CESNET, z.s.p.o. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
| 15 | |
| 16 | #ifndef LY_CONTEXT_H_ |
| 17 | #define LY_CONTEXT_H_ |
| 18 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 21 | #include "log.h" |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 22 | #include "parser_schema.h" |
Tadeáš Vintrlík | 390b272 | 2021-04-07 13:52:45 +0200 | [diff] [blame] | 23 | #include "tree_data.h" |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 24 | |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 25 | #ifdef __cplusplus |
| 26 | extern "C" { |
| 27 | #endif |
| 28 | |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 29 | struct lys_module; |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 30 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 31 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 32 | * @page howtoContext Context |
Radek Krejci | 52785a2 | 2019-09-11 12:57:26 +0200 | [diff] [blame] | 33 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 34 | * 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] | 35 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 36 | * 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 |
Michal Vasko | 5a5e7f8 | 2021-04-15 11:11:03 +0200 | [diff] [blame] | 38 | * a complete context and after starting working with the data, the context and its content should not change. If it does, |
| 39 | * in most cases it leads to the context being recompiled and any parsed data invalid. Despite the API not enforcing this |
| 40 | * approach, it may change in future versions in the form of a locking mechanism which would allow further |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 41 | * optimization of data manipulation. Also note that modules cannot be removed from their context. If you need to change the set |
| 42 | * 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] | 43 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 44 | * The context has [several options](@ref contextoptions) changing behavior when processing YANG modules being inserted. The |
| 45 | * specific behavior is mentioned below. All the options can be set as a parameter when the context is being created or later |
| 46 | * with ::ly_ctx_set_options(). |
| 47 | * |
| 48 | * When creating a new context, another optional parameter is search_dir It provide directory where libyang |
| 49 | * will automatically search for YANG modules being imported or included. There is actually a set of search paths which can be later |
| 50 | * modified using ::ly_ctx_set_searchdir(), ::ly_ctx_unset_searchdir() and ::ly_ctx_unset_searchdir_last() functions. Before the values |
| 51 | * in the set are used, also the current working directory is (non-recursively) searched. For the case of the explicitly set |
| 52 | * search directories, they are searched recursively - all their subdirectories (and symlinks) are taken into account. Searching |
| 53 | * 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] | 54 | * 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] | 55 | * ::LY_CTX_DISABLE_SEARCHDIRS option (or via ::ly_ctx_set_options()). This automatic searching can be preceded |
| 56 | * by a custom module searching callback (::ly_module_imp_clb) set via ::ly_ctx_set_module_imp_clb(). The algorithm of |
| 57 | * 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] | 58 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 59 | * YANG modules are added into the context using [parser functions](@ref howtoSchemaParsers) - \b lys_parse*(). |
| 60 | * Alternatively, also ::ly_ctx_load_module() can be used - in that case the ::ly_module_imp_clb or automatic |
| 61 | * search in search directories and in the current working directory is used, as described above. YANG submodules cannot be loaded |
| 62 | * or even validated directly, they are loaded always only as includes of YANG modules. Explicitly parsed/loaded modules are |
| 63 | * handled as implemented - libyang is able to instantiate data representing such a module. The modules loaded implicitly, are |
| 64 | * not implemented and serve only as a source of grouping or typedef definitions. Context can hold multiple revisions of the same |
| 65 | * 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] | 66 | * can be found on @ref howtoSchema page. This behavior can be changed with the context's ::LY_CTX_ALL_IMPLEMENTED option, which |
| 67 | * causes that all the parsed modules, whether loaded explicitly or implicitly, are set to be implemented. Note, that as |
| 68 | * a consequence of this option, only a single revision of any module can be present in the context in this case. Also, a less |
| 69 | * 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] | 70 | * |
| 71 | * When loading/importing a module without revision, the latest revision of the required module is supposed to load. |
| 72 | * For a context, the first time the latest revision of a module is requested, it is properly searched for and loaded. |
| 73 | * 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] | 74 | * This has the advantage of not searching for the module repeatedly but there is a drawback in case the content of search |
aPiecek | d4911ee | 2021-07-30 07:40:24 +0200 | [diff] [blame] | 75 | * directories is updated and a later revision become available. |
Radek Krejci | 52785a2 | 2019-09-11 12:57:26 +0200 | [diff] [blame] | 76 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 77 | * 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 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() |
Michal Vasko | 8dc3199 | 2021-02-22 10:30:47 +0100 | [diff] [blame] | 121 | * - ::ly_ctx_get_submodule() |
| 122 | * - ::ly_ctx_get_submodule_latest() |
| 123 | * - ::ly_ctx_get_submodule2() |
| 124 | * - ::ly_ctx_get_submodule2_latest() |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 125 | * |
| 126 | * - ::ly_ctx_get_yanglib_data() |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 127 | * |
Michal Vasko | 794ab4b | 2021-03-31 09:42:19 +0200 | [diff] [blame] | 128 | * - ::ly_ctx_get_change_count() |
Radek Krejci | 0a60f1d | 2020-10-26 22:24:43 +0100 | [diff] [blame] | 129 | * - ::ly_ctx_internal_modules_count() |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 130 | * |
Radek Krejci | 52785a2 | 2019-09-11 12:57:26 +0200 | [diff] [blame] | 131 | * - ::lys_search_localfile() |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 132 | * - ::lys_set_implemented() |
| 133 | * |
| 134 | */ |
| 135 | |
| 136 | /** |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 137 | * @defgroup context Context |
| 138 | * @{ |
| 139 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 140 | * Structures and functions to manipulate with the libyang context containers. |
| 141 | * |
| 142 | * The \em context concept allows callers to work in environments with different sets of YANG schemas. |
| 143 | * More detailed information can be found at @ref howtoContext page. |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 144 | */ |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 145 | |
| 146 | /** |
| 147 | * @struct ly_ctx |
| 148 | * @brief libyang context handler. |
| 149 | */ |
| 150 | struct ly_ctx; |
| 151 | |
| 152 | /** |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 153 | * @ingroup context |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 154 | * @defgroup contextoptions Context options |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 155 | * |
| 156 | * Options to change context behavior. |
Radek Krejci | 474f9b8 | 2019-07-24 11:36:37 +0200 | [diff] [blame] | 157 | * |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 158 | * @{ |
| 159 | */ |
| 160 | |
Michal Vasko | 25d6ad0 | 2020-10-22 12:20:22 +0200 | [diff] [blame] | 161 | #define LY_CTX_ALL_IMPLEMENTED 0x01 /**< All the imported modules of the schema being parsed are implemented. */ |
| 162 | #define LY_CTX_REF_IMPLEMENTED 0x02 /**< Implement all imported modules "referenced" from an implemented module. |
| 163 | Normally, leafrefs, augment and deviation targets are implemented as |
| 164 | specified by YANG 1.1. In addition to this, implement any modules of |
| 165 | nodes referenced by when and must conditions and by any default values. |
| 166 | Generally, only if all these modules are implemented, the explicitly |
| 167 | implemented modules can be properly used and instantiated in data. */ |
| 168 | #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] | 169 | 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] | 170 | the ietf-yang-library module is loaded manually. While any revision |
| 171 | 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] | 172 | 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] | 173 | This option cannot be changed on existing context. */ |
| 174 | #define LY_CTX_DISABLE_SEARCHDIRS 0x08 /**< Do not search for schemas in context's searchdirs neither in current |
| 175 | working directory. It is entirely skipped and the only way to get |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 176 | schema data for imports or for ::ly_ctx_load_module() is to use the |
| 177 | callbacks provided by caller via ::ly_ctx_set_module_imp_clb() */ |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 178 | #define LY_CTX_DISABLE_SEARCHDIR_CWD 0x10 /**< Do not automatically search for schemas in current working |
| 179 | directory, which is by default searched automatically (despite not |
| 180 | recursively). */ |
| 181 | #define LY_CTX_PREFER_SEARCHDIRS 0x20 /**< When searching for schema, prefer searchdirs instead of user callback. */ |
aPiecek | 9922ea9 | 2021-04-12 07:59:20 +0200 | [diff] [blame] | 182 | #define LY_CTX_SET_PRIV_PARSED 0x40 /**< For all compiled nodes, their private objects (::lysc_node.priv) are used |
| 183 | by libyang as a reference to the corresponding parsed node (::lysp_node). |
aPiecek | b0445f2 | 2021-06-24 11:34:07 +0200 | [diff] [blame] | 184 | The exception are \"case\" statements, which are omitted (shorthand), |
aPiecek | 082c7dc | 2021-05-20 08:55:07 +0200 | [diff] [blame] | 185 | in that case the private objects are set to NULL. |
aPiecek | 9922ea9 | 2021-04-12 07:59:20 +0200 | [diff] [blame] | 186 | So if this option is set, the user must not change private objects. |
| 187 | Setting this option by ::ly_ctx_set_options() may result in context recompilation. |
| 188 | Resetting this option by ::ly_ctx_unset_options() cause that private |
| 189 | objects will be set to NULL. */ |
Michal Vasko | 01db7de | 2021-04-16 12:23:30 +0200 | [diff] [blame] | 190 | #define LY_CTX_EXPLICIT_COMPILE 0x80 /**< If this flag is set, the compiled modules and their schema nodes are |
| 191 | not automatically updated (compiled) on any context changes. In other words, they do |
| 192 | not immediately take effect. To do that, call ::ly_ctx_compile(). Changes |
| 193 | requiring compilation include adding new modules, changing their features, |
| 194 | and implementing parsed-only modules. This option allows efficient compiled |
| 195 | context creation without redundant recompilations. */ |
Michal Vasko | c56d637 | 2021-10-19 12:29:00 +0200 | [diff] [blame] | 196 | #define LY_CTX_ENABLE_IMP_FEATURES 0x0100 /**< By default, all features of newly implemented imported modules of |
| 197 | a module that is being loaded are disabled. With this flag they all become |
| 198 | enabled. */ |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 199 | #define LY_CTX_LEAFREF_EXTENDED 0x0200 /**< By default, path attribute of leafref accepts only path as defined in RFC 7950. |
| 200 | By using this option, the path attribute will also allow using XPath functions as deref() */ |
steweg | f9041a2 | 2024-01-18 13:29:12 +0100 | [diff] [blame] | 201 | #define LY_CTX_LEAFREF_LINKING 0x0400 /**< Link valid leafref nodes with its target during validation if leafref node is not using |
| 202 | 'require-instance false;'. It also enables usage of |
| 203 | [lyd_leafref_get_links](@ref lyd_leafref_get_links) and |
| 204 | [lyd_leafref_link_node_tree](@ref lyd_leafref_link_node_tree) APIs. */ |
Michal Vasko | abd34fb | 2024-02-21 09:53:56 +0100 | [diff] [blame] | 205 | #define LY_CTX_BUILTIN_PLUGINS_ONLY 0x0800 /**< By default, context uses all available plugins for types and extensions, |
| 206 | both included and external. This options prevents all included plugins to be |
| 207 | loaded except for built-in YANG types so all derived types will use these and |
| 208 | for all purposes behave as the base type. The option can be used for cases when |
| 209 | invalid data needs to be stored in YANG node values. */ |
Radek Krejci | 0af4629 | 2019-01-11 16:02:31 +0100 | [diff] [blame] | 210 | |
Michal Vasko | b36053d | 2020-03-26 15:49:30 +0100 | [diff] [blame] | 211 | /** @} contextoptions */ |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 212 | |
| 213 | /** |
| 214 | * @brief Create libyang context. |
| 215 | * |
| 216 | * Context is used to hold all information about schemas. Usually, the application is supposed |
| 217 | * to work with a single context in which libyang is holding all schemas (and other internal |
| 218 | * information) according to which the data trees will be processed and validated. So, the schema |
| 219 | * 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] | 220 | * - caller does not need to keep pointers to the schemas returned by ::lys_parse(), context knows |
| 221 | * 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] | 222 | * but they are not internally held by the context. The data tree just points and lean on some data |
| 223 | * 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] | 224 | * 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] | 225 | * also affects the number of instances of both tree types. While you can have only one instance of |
| 226 | * specific schema connected with a single context, number of data tree instances is not connected. |
| 227 | * |
Jan Kundrát | 34bb928 | 2021-12-13 18:45:50 +0100 | [diff] [blame] | 228 | * @param[in] search_dir Directory (or directories) where libyang will search for the imported or included modules |
| 229 | * and submodules. If no such directory is available, NULL is accepted. Several directories can be specified, |
| 230 | * delimited by colon ":" (on Windows, use semicolon ";" instead). |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 231 | * @param[in] options Context options, see @ref contextoptions. |
| 232 | * @param[out] new_ctx Pointer to the created libyang context if LY_SUCCESS returned. |
| 233 | * @return LY_ERR return value. |
| 234 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 235 | LIBYANG_API_DECL 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] | 236 | |
| 237 | /** |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 238 | * @brief Create libyang context according to the provided yang-library data in a file. |
Tadeáš Vintrlík | 390b272 | 2021-04-07 13:52:45 +0200 | [diff] [blame] | 239 | * |
| 240 | * This function loads the yang-library data from the given path. If you need to pass the data as |
| 241 | * string, use ::::ly_ctx_new_ylmem(). Both functions extend functionality of ::ly_ctx_new() by loading |
| 242 | * modules specified in the ietf-yang-library form into the context being created. |
| 243 | * The preferred tree model revision is 2019-01-04. However, only the first module-set is processed and loaded |
| 244 | * into the context. If there are no matching nodes from this tree, the legacy tree (originally from model revision 2016-04-09) |
| 245 | * is processed. Note, that the modules are loaded the same way as in case of ::ly_ctx_load_module(), so the schema paths in the |
| 246 | * yang-library data are ignored and the modules are loaded from the context's search locations. On the other hand, YANG features |
| 247 | * of the modules are set as specified in the yang-library data. |
| 248 | * To get yang library data from a libyang context, use ::ly_ctx_get_yanglib_data(). |
| 249 | * |
| 250 | * @param[in] search_dir Directory where libyang will search for the imported or included modules and submodules. |
| 251 | * If no such directory is available, NULL is accepted. |
| 252 | * @param[in] path Path to the file containing yang-library-data in the specified format |
| 253 | * @param[in] format Format of the data in the provided file. |
| 254 | * @param[in] options Context options, see @ref contextoptions. |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 255 | * @param[in,out] ctx If *ctx is not NULL, the existing libyang context is modified. Otherwise, a pointer to a |
| 256 | * newly created context is returned here if LY_SUCCESS. |
Tadeáš Vintrlík | 390b272 | 2021-04-07 13:52:45 +0200 | [diff] [blame] | 257 | * @return LY_ERR return value |
| 258 | */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 259 | LIBYANG_API_DECL LY_ERR ly_ctx_new_ylpath(const char *search_dir, const char *path, LYD_FORMAT format, int options, |
| 260 | struct ly_ctx **ctx); |
Tadeáš Vintrlík | 390b272 | 2021-04-07 13:52:45 +0200 | [diff] [blame] | 261 | |
| 262 | /** |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 263 | * @brief Create libyang context according to the provided yang-library data in a string. |
Tadeáš Vintrlík | 390b272 | 2021-04-07 13:52:45 +0200 | [diff] [blame] | 264 | * |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 265 | * Details in ::ly_ctx_new_ylpath(). |
Tadeáš Vintrlík | 390b272 | 2021-04-07 13:52:45 +0200 | [diff] [blame] | 266 | * |
| 267 | * @param[in] search_dir Directory where libyang will search for the imported or included modules and submodules. |
| 268 | * If no such directory is available, NULL is accepted. |
| 269 | * @param[in] data String containing yang-library data in the specified format. |
| 270 | * @param[in] format Format of the data in the provided file. |
| 271 | * @param[in] options Context options, see @ref contextoptions. |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 272 | * @param[in,out] ctx If *ctx is not NULL, the existing libyang context is modified. Otherwise, a pointer to a |
| 273 | * newly created context is returned here if LY_SUCCESS. |
Tadeáš Vintrlík | 390b272 | 2021-04-07 13:52:45 +0200 | [diff] [blame] | 274 | * @return LY_ERR return value |
| 275 | */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 276 | LIBYANG_API_DECL LY_ERR ly_ctx_new_ylmem(const char *search_dir, const char *data, LYD_FORMAT format, int options, |
| 277 | struct ly_ctx **ctx); |
| 278 | |
| 279 | /** |
| 280 | * @brief Create libyang context according to the provided yang-library data in a data tree. |
| 281 | * |
| 282 | * Details in ::ly_ctx_new_ylpath(). |
| 283 | * |
| 284 | * @param[in] search_dir Directory where libyang will search for the imported or included modules and submodules. |
| 285 | * If no such directory is available, NULL is accepted. |
| 286 | * @param[in] tree Data tree containing yang-library data. |
| 287 | * @param[in] options Context options, see @ref contextoptions. |
ekinzie | 0ab8b30 | 2022-10-10 03:03:57 -0400 | [diff] [blame] | 288 | * @param[in,out] ctx If *ctx is not NULL, the existing libyang context is modified. Otherwise, a pointer to a |
| 289 | * newly created context is returned here if LY_SUCCESS. |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 290 | * @return LY_ERR return value |
| 291 | */ |
| 292 | LIBYANG_API_DECL LY_ERR ly_ctx_new_yldata(const char *search_dir, const struct lyd_node *tree, int options, |
| 293 | struct ly_ctx **ctx); |
Tadeáš Vintrlík | 390b272 | 2021-04-07 13:52:45 +0200 | [diff] [blame] | 294 | |
| 295 | /** |
Michal Vasko | 01db7de | 2021-04-16 12:23:30 +0200 | [diff] [blame] | 296 | * @brief Compile (recompile) the context applying all the performed changes after the last context compilation. |
| 297 | * Should be used only if ::LY_CTX_EXPLICIT_COMPILE option is set, has no effect otherwise. |
| 298 | * |
| 299 | * @param[in] ctx Context to compile. |
| 300 | * @return LY_ERR return value. |
| 301 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 302 | LIBYANG_API_DECL LY_ERR ly_ctx_compile(struct ly_ctx *ctx); |
Michal Vasko | 01db7de | 2021-04-16 12:23:30 +0200 | [diff] [blame] | 303 | |
| 304 | /** |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 305 | * @brief Add the search path into libyang context |
| 306 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 307 | * 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] | 308 | * set search paths again. |
| 309 | * |
| 310 | * @param[in] ctx Context to be modified. |
| 311 | * @param[in] search_dir New search path to add to the current paths previously set in ctx. |
| 312 | * @return LY_ERR return value. |
| 313 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 314 | LIBYANG_API_DECL LY_ERR ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir); |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 315 | |
| 316 | /** |
| 317 | * @brief Clean the search path(s) from the libyang context |
| 318 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 319 | * 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] | 320 | * |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 321 | * @param[in] ctx Context to be modified. |
| 322 | * @param[in] value Searchdir to be removed, use NULL to remove them all. |
| 323 | * @return LY_ERR return value |
| 324 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 325 | LIBYANG_API_DECL 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] | 326 | |
| 327 | /** |
Radek Krejci | e58f97f | 2020-08-18 11:45:08 +0200 | [diff] [blame] | 328 | * @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] | 329 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 330 | * 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] | 331 | * |
| 332 | * @param[in] ctx Context to be modified. |
Radek Krejci | e58f97f | 2020-08-18 11:45:08 +0200 | [diff] [blame] | 333 | * @param[in] count Number of the searchdirs to be removed (starting by the least recently added). |
| 334 | * If the value is higher then the actual number of search paths, all paths are removed and no error is returned. |
| 335 | * Value 0 does not change the search path set. |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 336 | * @return LY_ERR return value |
| 337 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 338 | LIBYANG_API_DECL 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] | 339 | |
| 340 | /** |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 341 | * @brief Get the NULL-terminated list of the search paths in libyang context. Do not modify the result! |
| 342 | * |
| 343 | * @param[in] ctx Context to query. |
| 344 | * @return NULL-terminated list (array) of the search paths, NULL if no searchpath was set. |
| 345 | * Do not modify the provided data in any way! |
| 346 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 347 | LIBYANG_API_DECL const char * const *ly_ctx_get_searchdirs(const struct ly_ctx *ctx); |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 348 | |
| 349 | /** |
| 350 | * @brief Get the currently set context's options. |
| 351 | * |
| 352 | * @param[in] ctx Context to query. |
| 353 | * @return Combination of all the currently set context's options, see @ref contextoptions. |
| 354 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 355 | LIBYANG_API_DECL uint16_t ly_ctx_get_options(const struct ly_ctx *ctx); |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 356 | |
| 357 | /** |
| 358 | * @brief Set some of the context's options, see @ref contextoptions. |
| 359 | * @param[in] ctx Context to be modified. |
| 360 | * @param[in] option Combination of the context's options to be set, see @ref contextoptions. |
aPiecek | 9922ea9 | 2021-04-12 07:59:20 +0200 | [diff] [blame] | 361 | * If there is to be a change to ::LY_CTX_SET_PRIV_PARSED, the context will be recompiled |
| 362 | * and all ::lysc_node.priv in the modules will be overwritten, see ::LY_CTX_SET_PRIV_PARSED. |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 363 | * @return LY_ERR value. |
| 364 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 365 | LIBYANG_API_DECL 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] | 366 | |
| 367 | /** |
| 368 | * @brief Unset some of the context's options, see @ref contextoptions. |
| 369 | * @param[in] ctx Context to be modified. |
| 370 | * @param[in] option Combination of the context's options to be unset, see @ref contextoptions. |
| 371 | * @return LY_ERR value. |
| 372 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 373 | LIBYANG_API_DECL 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] | 374 | |
| 375 | /** |
Michal Vasko | 794ab4b | 2021-03-31 09:42:19 +0200 | [diff] [blame] | 376 | * @brief Get the change count of the context (module set) during its life-time. |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 377 | * |
| 378 | * @param[in] ctx Context to be examined. |
Michal Vasko | 794ab4b | 2021-03-31 09:42:19 +0200 | [diff] [blame] | 379 | * @return Context change count. |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 380 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 381 | LIBYANG_API_DECL uint16_t ly_ctx_get_change_count(const struct ly_ctx *ctx); |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 382 | |
| 383 | /** |
Michal Vasko | f332f03 | 2023-04-11 13:44:52 +0200 | [diff] [blame] | 384 | * @brief Get the hash of all the modules in the context. Since order of the modules is significant, |
Michal Vasko | f9943e4 | 2023-03-22 07:29:37 +0100 | [diff] [blame] | 385 | * even when 2 contexts have the same modules but loaded in a different order, the hash will differ. |
| 386 | * |
Michal Vasko | f332f03 | 2023-04-11 13:44:52 +0200 | [diff] [blame] | 387 | * Hash consists of all module names (1), their revisions (2), all enabled features (3), and their |
| 388 | * imported/implemented state (4). |
| 389 | * |
Michal Vasko | f9943e4 | 2023-03-22 07:29:37 +0100 | [diff] [blame] | 390 | * @param[in] ctx Context to be examined. |
| 391 | * @return Context modules hash. |
| 392 | */ |
| 393 | LIBYANG_API_DECL uint32_t ly_ctx_get_modules_hash(const struct ly_ctx *ctx); |
| 394 | |
| 395 | /** |
Michal Vasko | b2786d7 | 2020-08-24 13:19:52 +0200 | [diff] [blame] | 396 | * @brief Callback for freeing returned module data in #ly_module_imp_clb. |
| 397 | * |
| 398 | * @param[in] module_data Data to free. |
| 399 | * @param[in] user_data User-supplied callback data, same as for #ly_module_imp_clb. |
| 400 | */ |
| 401 | typedef void (*ly_module_imp_data_free_clb)(void *module_data, void *user_data); |
| 402 | |
| 403 | /** |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 404 | * @brief Callback for retrieving missing included or imported models in a custom way. |
| 405 | * |
Radek Krejci | bb9d224 | 2021-01-04 10:51:31 +0100 | [diff] [blame] | 406 | * When @p submod_name is provided, the submodule is requested instead of the module (in this case only |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 407 | * the module name without its revision is provided). |
| 408 | * |
| 409 | * If an @arg free_module_data callback is provided, it will be used later to free the allegedly const data |
| 410 | * which were returned by this callback. |
| 411 | * |
| 412 | * @param[in] mod_name Missing module name. |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 413 | * @param[in] mod_rev Optional missing module revision. If NULL and submod_name is not provided, the latest revision is |
| 414 | * requested, the parsed module is then marked by the latest_revision flag. |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 415 | * @param[in] submod_name Optional missing submodule name. |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 416 | * @param[in] submod_rev Optional missing submodule revision. If NULL and submod_name is provided, the latest revision is |
| 417 | * requested, the parsed submodule is then marked by the latest_revision flag. |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 418 | * @param[in] user_data User-supplied callback data. |
| 419 | * @param[out] format Format of the returned module data. |
| 420 | * @param[out] module_data Requested module data. |
| 421 | * @param[out] free_module_data Callback for freeing the returned module data. If not set, the data will be left untouched. |
| 422 | * @return LY_ERR value. If the returned value differs from LY_SUCCESS, libyang continue in trying to get the module data |
| 423 | * according to the settings of its mechanism to search for the imported/included schemas. |
| 424 | */ |
Radek Krejci | bb9d224 | 2021-01-04 10:51:31 +0100 | [diff] [blame] | 425 | typedef LY_ERR (*ly_module_imp_clb)(const char *mod_name, const char *mod_rev, const char *submod_name, const char *submod_rev, |
Michal Vasko | b2786d7 | 2020-08-24 13:19:52 +0200 | [diff] [blame] | 426 | 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] | 427 | |
| 428 | /** |
| 429 | * @brief Get the custom callback for missing import/include module retrieval. |
| 430 | * |
| 431 | * @param[in] ctx Context to read from. |
| 432 | * @param[in] user_data Optional pointer for getting the user-supplied callback data. |
| 433 | * @return Callback or NULL if not set. |
| 434 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 435 | LIBYANG_API_DECL ly_module_imp_clb ly_ctx_get_module_imp_clb(const struct ly_ctx *ctx, void **user_data); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 436 | |
| 437 | /** |
| 438 | * @brief Set missing include or import module callback. It is meant to be used when the models |
| 439 | * are not locally available (such as when downloading modules from a NETCONF server), it should |
| 440 | * not be required in other cases. |
| 441 | * |
| 442 | * @param[in] ctx Context that will use this callback. |
| 443 | * @param[in] clb Callback responsible for returning the missing model. |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 444 | * @param[in] user_data Arbitrary data that will always be passed to the callback @p clb. |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 445 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 446 | LIBYANG_API_DECL void ly_ctx_set_module_imp_clb(struct ly_ctx *ctx, ly_module_imp_clb clb, void *user_data); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 447 | |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 448 | /** |
| 449 | * @brief Callback for getting arbitrary run-time data required by an extension instance. |
| 450 | * |
| 451 | * @param[in] ext Compiled extension instance. |
| 452 | * @param[in] user_data User-supplied callback data. |
| 453 | * @param[out] ext_data Provided extension instance data. |
| 454 | * @param[out] ext_data_free Whether the extension instance should free @p ext_data or not. |
| 455 | * @return LY_ERR value. |
| 456 | */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 457 | typedef LY_ERR (*ly_ext_data_clb)(const struct lysc_ext_instance *ext, void *user_data, void **ext_data, |
| 458 | ly_bool *ext_data_free); |
| 459 | |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 460 | /** |
| 461 | * @brief Set callback providing run-time extension instance data. The expected data depend on the extension. |
| 462 | * Data expected by internal extensions: |
| 463 | * |
| 464 | * - *ietf-yang-schema-mount:mount-point* (struct lyd_node \*\*ext_data)\n |
| 465 | * Operational data tree with at least `ietf-yang-library` data describing the mounted schema and |
| 466 | * `ietf-yang-schema-mount` **validated** data describing the specific mount point |
| 467 | * ([ref](https://datatracker.ietf.org/doc/html/rfc8528#section-3.3)). |
| 468 | * |
| 469 | * @param[in] ctx Context that will use this callback. |
| 470 | * @param[in] clb Callback responsible for returning the extension instance data. |
| 471 | * @param[in] user_data Arbitrary data that will always be passed to the callback @p clb. |
| 472 | */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 473 | LIBYANG_API_DECL ly_ext_data_clb ly_ctx_set_ext_data_clb(struct ly_ctx *ctx, ly_ext_data_clb clb, void *user_data); |
| 474 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 475 | /** |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 476 | * @brief Get YANG module of the given name and revision. |
| 477 | * |
| 478 | * @param[in] ctx Context to work in. |
| 479 | * @param[in] name Name of the YANG module to get. |
| 480 | * @param[in] revision Requested revision date of the YANG module to get. If not specified, |
| 481 | * the schema with no revision is returned, if it is present in the context. |
| 482 | * @return Pointer to the YANG module, NULL if no schema in the context follows the name and revision requirements. |
| 483 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 484 | LIBYANG_API_DECL 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] | 485 | |
| 486 | /** |
| 487 | * @brief Get the latest revision of the YANG module specified by its name. |
| 488 | * |
| 489 | * YANG modules with no revision are supposed to be the oldest one. |
| 490 | * |
| 491 | * @param[in] ctx Context where to search. |
| 492 | * @param[in] name Name of the YANG module to get. |
| 493 | * @return The latest revision of the specified YANG module in the given context, NULL if no YANG module of the |
| 494 | * given name is present in the context. |
| 495 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 496 | LIBYANG_API_DECL 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] | 497 | |
| 498 | /** |
| 499 | * @brief Get the (only) implemented YANG module specified by its name. |
| 500 | * |
| 501 | * @param[in] ctx Context where to search. |
| 502 | * @param[in] name Name of the YANG module to get. |
| 503 | * @return The only implemented YANG module revision of the given name in the given context. NULL if there is no |
| 504 | * implemented module of the given name. |
| 505 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 506 | LIBYANG_API_DECL 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] | 507 | |
| 508 | /** |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 509 | * @brief Iterate over all modules in the given context. |
| 510 | * |
| 511 | * @param[in] ctx Context with the modules. |
| 512 | * @param[in,out] index Index of the next module to get. Value of 0 starts from the beginning. |
| 513 | * The value is updated with each call, so to iterate over all modules the same variable is supposed |
| 514 | * to be used in all calls starting with value 0. |
| 515 | * @return Next context module, NULL if the last was already returned. |
| 516 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 517 | LIBYANG_API_DECL 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] | 518 | |
| 519 | /** |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 520 | * @brief Get YANG module of the given namespace and revision. |
| 521 | * |
| 522 | * @param[in] ctx Context to work in. |
| 523 | * @param[in] ns Namespace of the YANG module to get. |
| 524 | * @param[in] revision Requested revision date of the YANG module to get. If not specified, |
| 525 | * the schema with no revision is returned, if it is present in the context. |
| 526 | * @return Pointer to the YANG module, NULL if no schema in the context follows the namespace and revision requirements. |
| 527 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 528 | LIBYANG_API_DECL 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] | 529 | |
| 530 | /** |
| 531 | * @brief Get the latest revision of the YANG module specified by its namespace. |
| 532 | * |
| 533 | * YANG modules with no revision are supposed to be the oldest one. |
| 534 | * |
| 535 | * @param[in] ctx Context where to search. |
| 536 | * @param[in] ns Namespace of the YANG module to get. |
| 537 | * @return The latest revision of the specified YANG module in the given context, NULL if no YANG module of the |
| 538 | * given namespace is present in the context. |
| 539 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 540 | LIBYANG_API_DECL 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] | 541 | |
| 542 | /** |
| 543 | * @brief Get the (only) implemented YANG module specified by its namespace. |
| 544 | * |
| 545 | * @param[in] ctx Context where to search. |
| 546 | * @param[in] ns Namespace of the YANG module to get. |
| 547 | * @return The only implemented YANG module revision of the given namespace in the given context. NULL if there is no |
| 548 | * implemented module of the given namespace. |
| 549 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 550 | LIBYANG_API_DECL 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] | 551 | |
| 552 | /** |
Michal Vasko | 8dc3199 | 2021-02-22 10:30:47 +0100 | [diff] [blame] | 553 | * @brief Get a specific submodule from context. If its belongs-to module is known, use ::ly_ctx_get_submodule2(). |
| 554 | * |
| 555 | * @param[in] ctx libyang context to search in. |
| 556 | * @param[in] submodule Submodule name to find. |
| 557 | * @param[in] revision Revision of the submodule to find, NULL for a submodule without a revision. |
| 558 | * @return Found submodule, NULL if there is none. |
| 559 | */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 560 | LIBYANG_API_DECL const struct lysp_submodule *ly_ctx_get_submodule(const struct ly_ctx *ctx, const char *submodule, |
| 561 | const char *revision); |
Michal Vasko | 8dc3199 | 2021-02-22 10:30:47 +0100 | [diff] [blame] | 562 | |
| 563 | /** |
| 564 | * @brief Get the latests revision of a submodule from context. If its belongs-to module is known, |
| 565 | * use ::ly_ctx_get_submodule2_latest(). |
| 566 | * |
| 567 | * @param[in] ctx libyang context to search in. |
| 568 | * @param[in] submodule Submodule name to find. |
| 569 | * @return Found submodule, NULL if there is none. |
| 570 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 571 | LIBYANG_API_DECL const struct lysp_submodule *ly_ctx_get_submodule_latest(const struct ly_ctx *ctx, const char *submodule); |
Michal Vasko | 8dc3199 | 2021-02-22 10:30:47 +0100 | [diff] [blame] | 572 | |
| 573 | /** |
| 574 | * @brief Get a specific submodule from a module. If the belongs-to module is not known, use ::ly_ctx_get_submodule(). |
| 575 | * |
| 576 | * @param[in] module Belongs-to module to search in. |
| 577 | * @param[in] submodule Submodule name to find. |
| 578 | * @param[in] revision Revision of the submodule to find, NULL for a submodule without a revision. |
| 579 | * @return Found submodule, NULL if there is none. |
| 580 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 581 | LIBYANG_API_DECL const struct lysp_submodule *ly_ctx_get_submodule2(const struct lys_module *module, const char *submodule, |
Michal Vasko | 8dc3199 | 2021-02-22 10:30:47 +0100 | [diff] [blame] | 582 | const char *revision); |
| 583 | |
| 584 | /** |
| 585 | * @brief Get the latest revision of a submodule from a module. If the belongs-to module is not known, |
| 586 | * use ::ly_ctx_get_submodule_latest(). |
| 587 | * |
| 588 | * @param[in] module Belongs-to module to search in. |
| 589 | * @param[in] submodule Submodule name to find. |
| 590 | * @return Found submodule, NULL if there is none. |
| 591 | */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 592 | LIBYANG_API_DECL const struct lysp_submodule *ly_ctx_get_submodule2_latest(const struct lys_module *module, |
| 593 | const char *submodule); |
Michal Vasko | 8dc3199 | 2021-02-22 10:30:47 +0100 | [diff] [blame] | 594 | |
| 595 | /** |
Michal Vasko | de79e22 | 2020-08-10 11:55:46 +0200 | [diff] [blame] | 596 | * @brief Learn the number of internal modules of a context. Internal modules |
| 597 | * is considered one that was loaded during the context creation. |
| 598 | * |
| 599 | * @param[in] ctx libyang context to examine. |
| 600 | * @return Number of internal modules. |
| 601 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 602 | LIBYANG_API_DECL uint32_t ly_ctx_internal_modules_count(const struct ly_ctx *ctx); |
Michal Vasko | de79e22 | 2020-08-10 11:55:46 +0200 | [diff] [blame] | 603 | |
| 604 | /** |
Michal Vasko | 35847dd | 2024-09-10 14:40:44 +0200 | [diff] [blame] | 605 | * @brief Try to find the model in the searchpaths of @p ctx and load it into it. If custom missing |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 606 | * module callback is set, it is used instead. |
| 607 | * |
Michal Vasko | 35847dd | 2024-09-10 14:40:44 +0200 | [diff] [blame] | 608 | * The context itself is searched for the requested module first. If @p revision is not specified |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 609 | * (the module of the latest revision is requested) and there is implemented revision of the requested |
| 610 | * module in the context, this implemented revision is returned despite there might be a newer revision. |
| 611 | * This behavior is cause by the fact that it is not possible to have multiple implemented revisions of |
| 612 | * the same module in the context. |
| 613 | * |
| 614 | * @param[in] ctx Context to add to. |
| 615 | * @param[in] name Name of the module to load. |
| 616 | * @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] | 617 | * @param[in] features Optional array of features ended with NULL to be enabled if the module is being implemented. |
Radek Krejci | 2415f88 | 2021-01-20 16:27:09 +0100 | [diff] [blame] | 618 | * The feature string '*' enables all and array of length 1 with only the terminating NULL explicitly disables all |
| 619 | * the features. In case the parameter is NULL, the features are untouched - left disabled in newly loaded module or |
| 620 | * with the current features settings in case the module is already present in the context. |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 621 | * @return Pointer to the data model structure, NULL if not found or some error occurred. |
| 622 | */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 623 | LIBYANG_API_DECL struct lys_module *ly_ctx_load_module(struct ly_ctx *ctx, const char *name, const char *revision, |
| 624 | const char **features); |
Radek Krejci | ed5acc5 | 2019-04-25 15:57:04 +0200 | [diff] [blame] | 625 | |
| 626 | /** |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 627 | * @brief Get data of the internal ietf-yang-library module with information about all the loaded modules. |
| 628 | * ietf-yang-library module must be loaded. |
| 629 | * |
Michal Vasko | 794ab4b | 2021-03-31 09:42:19 +0200 | [diff] [blame] | 630 | * Note that "/ietf-yang-library:yang-library/datastore" list instances are not created and should be |
| 631 | * appended by the caller. There is a single "/ietf-yang-library:yang-library/schema" instance created |
| 632 | * with the key value "complete". |
| 633 | * |
| 634 | * If the data identifier can be limited to the existence and changes of this context, the following |
| 635 | * last 2 parameters can be used: |
| 636 | * |
Michal Vasko | f9943e4 | 2023-03-22 07:29:37 +0100 | [diff] [blame] | 637 | * "%u" as @p content_id_format and ::ly_ctx_get_change_count() as its parameter; |
| 638 | * "%u" as @p content_id_format and ::ly_ctx_get_modules_hash() as its parameter. |
Michal Vasko | 794ab4b | 2021-03-31 09:42:19 +0200 | [diff] [blame] | 639 | * |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 640 | * @param[in] ctx Context with the modules. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 641 | * @param[out] root Generated yang-library data. |
Michal Vasko | 794ab4b | 2021-03-31 09:42:19 +0200 | [diff] [blame] | 642 | * @param[in] content_id_format Format string (printf-like) for the yang-library data identifier, which is |
| 643 | * the "content_id" node in the 2019-01-04 revision of ietf-yang-library. |
| 644 | * @param[in] ... Parameters for @p content_id_format. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 645 | * @return LY_ERR value |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 646 | */ |
Michal Vasko | 61ad1ff | 2022-02-10 15:48:39 +0100 | [diff] [blame] | 647 | LIBYANG_API_DECL LY_ERR ly_ctx_get_yanglib_data(const struct ly_ctx *ctx, struct lyd_node **root, |
| 648 | const char *content_id_format, ...); |
Michal Vasko | 57c10cd | 2020-05-27 15:57:11 +0200 | [diff] [blame] | 649 | |
| 650 | /** |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 651 | * @brief Free all internal structures of the specified context. |
| 652 | * |
| 653 | * The function should be used before terminating the application to destroy |
| 654 | * and free all structures internally used by libyang. If the caller uses |
| 655 | * multiple contexts, the function should be called for each used context. |
| 656 | * |
Michal Vasko | 1817df8 | 2021-11-12 10:21:54 +0100 | [diff] [blame] | 657 | * All instance data are supposed to be freed before destroying the context using ::lyd_free_all(), for example. |
| 658 | * Data models (schemas) are destroyed automatically as part of ::ly_ctx_destroy() call. |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 659 | * |
Radek Krejci | 90ed21e | 2021-04-12 14:47:46 +0200 | [diff] [blame] | 660 | * Note that the data stored by user into the ::lysc_node.priv pointer are kept |
| 661 | * untouched and the caller is responsible for freeing this private data. |
| 662 | * |
Michal Vasko | cba757c | 2024-09-10 14:46:07 +0200 | [diff] [blame] | 663 | * @param[in] ctx Context to destroy. |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 664 | */ |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 665 | LIBYANG_API_DECL void ly_ctx_destroy(struct ly_ctx *ctx); |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 666 | |
| 667 | /** @} context */ |
| 668 | |
| 669 | #ifdef __cplusplus |
| 670 | } |
| 671 | #endif |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 672 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 673 | #endif /* LY_CONTEXT_H_ */ |