Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file context.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief internal context structures and functions |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2017 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | #ifndef LY_CONTEXT_H_ |
| 16 | #define LY_CONTEXT_H_ |
| 17 | |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 18 | #include "log.h" |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 19 | #include "tree_schema.h" |
| 20 | |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 21 | #ifdef __cplusplus |
| 22 | extern "C" { |
| 23 | #endif |
| 24 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 25 | /** |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 26 | * @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 Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 32 | */ |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * @struct ly_ctx |
| 36 | * @brief libyang context handler. |
| 37 | */ |
| 38 | struct 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. */ |
| 66 | /**@} contextoptions */ |
| 67 | |
| 68 | /** |
| 69 | * @brief Create libyang context. |
| 70 | * |
| 71 | * Context is used to hold all information about schemas. Usually, the application is supposed |
| 72 | * to work with a single context in which libyang is holding all schemas (and other internal |
| 73 | * information) according to which the data trees will be processed and validated. So, the schema |
| 74 | * trees are tightly connected with the specific context and they are held by the context internally |
| 75 | * - caller does not need to keep pointers to the schemas returned by lys_parse(), context knows |
| 76 | * about them. The data trees created with lyd_parse() are still connected with the specific context, |
| 77 | * but they are not internally held by the context. The data tree just points and lean on some data |
| 78 | * held by the context (schema tree, string dictionary, etc.). Therefore, in case of data trees, caller |
| 79 | * is supposed to keep pointers returned by the lyd_parse() and manage the data tree on its own. This |
| 80 | * also affects the number of instances of both tree types. While you can have only one instance of |
| 81 | * specific schema connected with a single context, number of data tree instances is not connected. |
| 82 | * |
| 83 | * @param[in] search_dir Directory where libyang will search for the imported or included modules |
| 84 | * and submodules. If no such directory is available, NULL is accepted. |
| 85 | * @param[in] options Context options, see @ref contextoptions. |
| 86 | * @param[out] new_ctx Pointer to the created libyang context if LY_SUCCESS returned. |
| 87 | * @return LY_ERR return value. |
| 88 | */ |
| 89 | LY_ERR ly_ctx_new(const char *search_dir, int options, struct ly_ctx **new_ctx); |
| 90 | |
| 91 | /** |
| 92 | * @brief Add the search path into libyang context |
| 93 | * |
| 94 | * To reset search paths set in the context, use ly_ctx_unset_searchdirs() and then |
| 95 | * set search paths again. |
| 96 | * |
| 97 | * @param[in] ctx Context to be modified. |
| 98 | * @param[in] search_dir New search path to add to the current paths previously set in ctx. |
| 99 | * @return LY_ERR return value. |
| 100 | */ |
| 101 | LY_ERR ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir); |
| 102 | |
| 103 | /** |
| 104 | * @brief Clean the search path(s) from the libyang context |
| 105 | * |
| 106 | * @param[in] ctx Context to be modified. |
| 107 | * @param[in] value Searchdir to be removed, use NULL to remove them all. |
| 108 | * @return LY_ERR return value |
| 109 | */ |
| 110 | LY_ERR ly_ctx_unset_searchdirs(struct ly_ctx *ctx, const char *value); |
| 111 | |
| 112 | /** |
| 113 | * @brief Get the NULL-terminated list of the search paths in libyang context. Do not modify the result! |
| 114 | * |
| 115 | * @param[in] ctx Context to query. |
| 116 | * @return NULL-terminated list (array) of the search paths, NULL if no searchpath was set. |
| 117 | * Do not modify the provided data in any way! |
| 118 | */ |
| 119 | const char * const *ly_ctx_get_searchdirs(const struct ly_ctx *ctx); |
| 120 | |
| 121 | /** |
| 122 | * @brief Get the currently set context's options. |
| 123 | * |
| 124 | * @param[in] ctx Context to query. |
| 125 | * @return Combination of all the currently set context's options, see @ref contextoptions. |
| 126 | */ |
| 127 | int ly_ctx_get_options(const struct ly_ctx *ctx); |
| 128 | |
| 129 | /** |
| 130 | * @brief Set some of the context's options, see @ref contextoptions. |
| 131 | * @param[in] ctx Context to be modified. |
| 132 | * @param[in] option Combination of the context's options to be set, see @ref contextoptions. |
| 133 | * @return LY_ERR value. |
| 134 | */ |
| 135 | LY_ERR ly_ctx_set_option(struct ly_ctx *ctx, int option); |
| 136 | |
| 137 | /** |
| 138 | * @brief Unset some of the context's options, see @ref contextoptions. |
| 139 | * @param[in] ctx Context to be modified. |
| 140 | * @param[in] option Combination of the context's options to be unset, see @ref contextoptions. |
| 141 | * @return LY_ERR value. |
| 142 | */ |
| 143 | LY_ERR ly_ctx_unset_option(struct ly_ctx *ctx, int option); |
| 144 | |
| 145 | /** |
| 146 | * @brief Get current ID of the modules set. The value is available also |
| 147 | * as module-set-id in ly_ctx_info() result. |
| 148 | * |
| 149 | * @param[in] ctx Context to be examined. |
| 150 | * @return Numeric identifier of the current context's modules set. |
| 151 | */ |
| 152 | uint16_t ly_ctx_get_module_set_id(const struct ly_ctx *ctx); |
| 153 | |
| 154 | /** |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 155 | * @brief Callback for retrieving missing included or imported models in a custom way. |
| 156 | * |
| 157 | * When submod_name is provided, the submodule is requested instead of the module (in this case only |
| 158 | * the module name without its revision is provided). |
| 159 | * |
| 160 | * If an @arg free_module_data callback is provided, it will be used later to free the allegedly const data |
| 161 | * which were returned by this callback. |
| 162 | * |
| 163 | * @param[in] mod_name Missing module name. |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 164 | * @param[in] mod_rev Optional missing module revision. If NULL and submod_name is not provided, the latest revision is |
| 165 | * requested, the parsed module is then marked by the latest_revision flag. |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 166 | * @param[in] submod_name Optional missing submodule name. |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 167 | * @param[in] submod_rev Optional missing submodule revision. If NULL and submod_name is provided, the latest revision is |
| 168 | * requested, the parsed submodule is then marked by the latest_revision flag. |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 169 | * @param[in] user_data User-supplied callback data. |
| 170 | * @param[out] format Format of the returned module data. |
| 171 | * @param[out] module_data Requested module data. |
| 172 | * @param[out] free_module_data Callback for freeing the returned module data. If not set, the data will be left untouched. |
| 173 | * @return LY_ERR value. If the returned value differs from LY_SUCCESS, libyang continue in trying to get the module data |
| 174 | * according to the settings of its mechanism to search for the imported/included schemas. |
| 175 | */ |
| 176 | typedef LY_ERR (*ly_module_imp_clb)(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev, |
| 177 | void *user_data, LYS_INFORMAT *format, const char **module_data, |
| 178 | void (**free_module_data)(void *model_data, void *user_data)); |
| 179 | |
| 180 | /** |
| 181 | * @brief Get the custom callback for missing import/include module retrieval. |
| 182 | * |
| 183 | * @param[in] ctx Context to read from. |
| 184 | * @param[in] user_data Optional pointer for getting the user-supplied callback data. |
| 185 | * @return Callback or NULL if not set. |
| 186 | */ |
| 187 | ly_module_imp_clb ly_ctx_get_module_imp_clb(const struct ly_ctx *ctx, void **user_data); |
| 188 | |
| 189 | /** |
| 190 | * @brief Set missing include or import module callback. It is meant to be used when the models |
| 191 | * are not locally available (such as when downloading modules from a NETCONF server), it should |
| 192 | * not be required in other cases. |
| 193 | * |
| 194 | * @param[in] ctx Context that will use this callback. |
| 195 | * @param[in] clb Callback responsible for returning the missing model. |
| 196 | * @param[in] user_data Arbitrary data that will always be passed to the callback \p clb. |
| 197 | */ |
| 198 | void ly_ctx_set_module_imp_clb(struct ly_ctx *ctx, ly_module_imp_clb clb, void *user_data); |
| 199 | |
| 200 | /** |
Radek Krejci | b7db73a | 2018-10-24 14:18:40 +0200 | [diff] [blame] | 201 | * @brief Get YANG module of the given name and revision. |
| 202 | * |
| 203 | * @param[in] ctx Context to work in. |
| 204 | * @param[in] name Name of the YANG module to get. |
| 205 | * @param[in] revision Requested revision date of the YANG module to get. If not specified, |
| 206 | * the schema with no revision is returned, if it is present in the context. |
| 207 | * @return Pointer to the YANG module, NULL if no schema in the context follows the name and revision requirements. |
| 208 | */ |
| 209 | const struct lys_module *ly_ctx_get_module(const struct ly_ctx *ctx, const char *name, const char *revision); |
| 210 | |
| 211 | /** |
| 212 | * @brief Get the latest revision of the YANG module specified by its name. |
| 213 | * |
| 214 | * YANG modules with no revision are supposed to be the oldest one. |
| 215 | * |
| 216 | * @param[in] ctx Context where to search. |
| 217 | * @param[in] name Name of the YANG module to get. |
| 218 | * @return The latest revision of the specified YANG module in the given context, NULL if no YANG module of the |
| 219 | * given name is present in the context. |
| 220 | */ |
| 221 | const struct lys_module *ly_ctx_get_module_latest(const struct ly_ctx *ctx, const char *name); |
| 222 | |
| 223 | /** |
| 224 | * @brief Get the (only) implemented YANG module specified by its name. |
| 225 | * |
| 226 | * @param[in] ctx Context where to search. |
| 227 | * @param[in] name Name of the YANG module to get. |
| 228 | * @return The only implemented YANG module revision of the given name in the given context. NULL if there is no |
| 229 | * implemented module of the given name. |
| 230 | */ |
| 231 | const struct lys_module *ly_ctx_get_module_implemented(const struct ly_ctx *ctx, const char *name); |
| 232 | |
| 233 | /** |
| 234 | * @brief Get YANG module of the given namespace and revision. |
| 235 | * |
| 236 | * @param[in] ctx Context to work in. |
| 237 | * @param[in] ns Namespace of the YANG module to get. |
| 238 | * @param[in] revision Requested revision date of the YANG module to get. If not specified, |
| 239 | * the schema with no revision is returned, if it is present in the context. |
| 240 | * @return Pointer to the YANG module, NULL if no schema in the context follows the namespace and revision requirements. |
| 241 | */ |
| 242 | const struct lys_module *ly_ctx_get_module_ns(const struct ly_ctx *ctx, const char *ns, const char *revision); |
| 243 | |
| 244 | /** |
| 245 | * @brief Get the latest revision of the YANG module specified by its namespace. |
| 246 | * |
| 247 | * YANG modules with no revision are supposed to be the oldest one. |
| 248 | * |
| 249 | * @param[in] ctx Context where to search. |
| 250 | * @param[in] ns Namespace of the YANG module to get. |
| 251 | * @return The latest revision of the specified YANG module in the given context, NULL if no YANG module of the |
| 252 | * given namespace is present in the context. |
| 253 | */ |
| 254 | const struct lys_module *ly_ctx_get_module_latest_ns(const struct ly_ctx *ctx, const char *ns); |
| 255 | |
| 256 | /** |
| 257 | * @brief Get the (only) implemented YANG module specified by its namespace. |
| 258 | * |
| 259 | * @param[in] ctx Context where to search. |
| 260 | * @param[in] ns Namespace of the YANG module to get. |
| 261 | * @return The only implemented YANG module revision of the given namespace in the given context. NULL if there is no |
| 262 | * implemented module of the given namespace. |
| 263 | */ |
| 264 | const struct lys_module *ly_ctx_get_module_implemented_ns(const struct ly_ctx *ctx, const char *ns); |
| 265 | |
| 266 | /** |
Radek Krejci | e9e987e | 2018-10-31 12:50:27 +0100 | [diff] [blame^] | 267 | * @brief Reset cached latest revision information of the schemas in the context. |
| 268 | * |
| 269 | * When a (sub)module is imported/included without revision, the latest revision is |
| 270 | * searched. libyang searches for the latest revision in searchdirs and/or via provided |
| 271 | * import callback ly_module_imp_clb() just once. Then it is expected that the content |
| 272 | * of searchdirs or data returned by the callback does not change. So when it changes, |
| 273 | * it is necessary to force searching for the latest revision in case of loading another |
| 274 | * module, which what this function does. |
| 275 | * |
| 276 | * The latest revision information is also reset when the searchdirs set changes via |
| 277 | * ly_ctx_set_searchdir(). |
| 278 | * |
| 279 | * @param[in] ctx libyang context where the latest revision information is going to be reset. |
| 280 | */ |
| 281 | void ly_ctx_reset_latests(struct ly_ctx *ctx); |
| 282 | |
| 283 | /** |
Radek Krejci | 6caa6ab | 2018-10-24 10:04:48 +0200 | [diff] [blame] | 284 | * @brief Free all internal structures of the specified context. |
| 285 | * |
| 286 | * The function should be used before terminating the application to destroy |
| 287 | * and free all structures internally used by libyang. If the caller uses |
| 288 | * multiple contexts, the function should be called for each used context. |
| 289 | * |
| 290 | * All instance data are supposed to be freed before destroying the context. |
| 291 | * Data models are destroyed automatically as part of ly_ctx_destroy() call. |
| 292 | * |
| 293 | * @param[in] ctx libyang context to destroy |
| 294 | * @param[in] private_destructor Optional destructor function for private objects assigned |
| 295 | * to the nodes via lys_set_private(). If NULL, the private objects are not freed by libyang. |
| 296 | * Remember the differences between the structures derived from ::lysc_node and always check |
| 297 | * ::lysc_node#nodetype. |
| 298 | */ |
| 299 | void ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lysc_node *node, void *priv)); |
| 300 | |
| 301 | /** @} context */ |
| 302 | |
| 303 | #ifdef __cplusplus |
| 304 | } |
| 305 | #endif |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 306 | |
Radek Krejci | 5aeea3a | 2018-09-05 13:29:36 +0200 | [diff] [blame] | 307 | #endif /* LY_CONTEXT_H_ */ |