Radek Krejci | 9b4ca39 | 2015-04-10 08:31:27 +0200 | [diff] [blame] | 1 | /** |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 2 | * @file libyang.h |
Radek Krejci | 9b4ca39 | 2015-04-10 08:31:27 +0200 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 4 | * @brief The main libyang public header. |
Radek Krejci | 9b4ca39 | 2015-04-10 08:31:27 +0200 | [diff] [blame] | 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in |
| 15 | * the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * 3. Neither the name of the Company nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this |
| 19 | * software without specific prior written permission. |
| 20 | */ |
| 21 | |
| 22 | #ifndef LY_LIBYANG_H_ |
| 23 | #define LY_LIBYANG_H_ |
| 24 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 27 | #include "tree.h" |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 28 | |
Michal Vasko | 85217a3 | 2015-07-07 11:40:08 +0200 | [diff] [blame] | 29 | int ly_model_print(FILE * f, struct ly_module *module, LY_MOUTFORMAT format, const char *target_node); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 30 | |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 31 | int ly_data_print(FILE * f, struct lyd_node *root, LY_DFORMAT format); |
| 32 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 33 | /** |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 34 | * @defgroup libyang libyang |
| 35 | * @{ |
| 36 | * |
| 37 | * General libyang functions and structures. |
| 38 | */ |
| 39 | |
| 40 | /** |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 41 | * @brief libyang context handler. |
| 42 | */ |
| 43 | struct ly_ctx; |
| 44 | |
| 45 | /** |
| 46 | * @brief Create libyang context |
| 47 | * |
| 48 | * Context is used to hold all information about data models. Usually, the |
| 49 | * application is supposed to work with a single context in which libyang is |
| 50 | * holding all data models and other internal information according to which |
| 51 | * the instance data will be processed and validated. Therefore, also each |
| 52 | * instance data are connected |
| 53 | * |
| 54 | * @param[in] search_dir Directory where libyang will search for the imported |
| 55 | * or included modules and submodules. If no such directory is available, NULL |
| 56 | * is accepted. |
| 57 | * |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 58 | * @return Pointer to the created libyang context, NULL in case of error. |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 59 | */ |
| 60 | struct ly_ctx *ly_ctx_new(const char *search_dir); |
| 61 | |
| 62 | /** |
Michal Vasko | 60ba9a6 | 2015-07-03 14:42:31 +0200 | [diff] [blame] | 63 | * @brief Change the search path in libyang context |
| 64 | * |
| 65 | * @param[in] ctx Context to be modified. |
| 66 | * @param[in] search_dir New search path to replace the current one in ctx. |
| 67 | */ |
| 68 | void ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir); |
| 69 | |
| 70 | /** |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 71 | * @brief Get pointer to the data model structure of the specified name. |
| 72 | * |
| 73 | * If the module is not yet loaded in the context, libyang tries to find it in |
| 74 | * the search directory specified when the context was created by ly_ctx_new(). |
| 75 | * |
| 76 | * @param[in] ctx Context to work in. |
| 77 | * @param[in] name Name of the YANG module to get. |
| 78 | * @param[in] revision Optional revision date of the YANG module to get. If not |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 79 | * specified, the newest revision is returned (TODO). |
Radek Krejci | 0af1387 | 2015-05-30 11:50:52 +0200 | [diff] [blame] | 80 | * @param[in] read Flag for reading the module from a file. If set to 0, libyang |
| 81 | * searches for the module only in the modules already loaded to the context. |
Michal Vasko | d8aa32d | 2015-07-24 11:50:01 +0200 | [diff] [blame^] | 82 | * @param[in] implement Flag set if the module is implemented, not just imported |
| 83 | * from another module. Specially, -1 means do not change it (only with |
| 84 | * read == 0). |
Radek Krejci | 0af1387 | 2015-05-30 11:50:52 +0200 | [diff] [blame] | 85 | * @return Pointer to the data model structure. |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 86 | */ |
Michal Vasko | d8aa32d | 2015-07-24 11:50:01 +0200 | [diff] [blame^] | 87 | struct ly_module *ly_ctx_get_module(struct ly_ctx *ctx, const char *name, const char *revision, int read, int implement); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 88 | |
| 89 | /** |
Michal Vasko | 7bf0688 | 2015-07-03 15:33:56 +0200 | [diff] [blame] | 90 | * @brief Get submodule from the context's search dir. |
| 91 | * |
| 92 | * @param[in] module Parent (belongs-to) module. |
| 93 | * @param[in] name Name of the YANG submodule to get. |
| 94 | * @param[in] revision Optional revision date of the YANG submodule to get. If |
| 95 | * not specified, the newest revision is returned (TODO). |
| 96 | * @param[in] read Flag for reading the submodule from a file. If set to 0, libyang |
| 97 | * searches for the submodule only in the submodules already loaded to the context. |
Michal Vasko | d8aa32d | 2015-07-24 11:50:01 +0200 | [diff] [blame^] | 98 | * @param[in] implement Flag set is the submodule is implemented, not just imported |
| 99 | * from another module. Specially, -1 means do not change it (only with read == 0). |
Michal Vasko | 7bf0688 | 2015-07-03 15:33:56 +0200 | [diff] [blame] | 100 | * @return Pointer to the data model structure. |
| 101 | */ |
Michal Vasko | d8aa32d | 2015-07-24 11:50:01 +0200 | [diff] [blame^] | 102 | struct ly_submodule *ly_ctx_get_submodule(struct ly_module *module, const char *name, const char *revision, int read, int implement); |
Michal Vasko | 7bf0688 | 2015-07-03 15:33:56 +0200 | [diff] [blame] | 103 | |
| 104 | /** |
Michal Vasko | 3ec07dc | 2015-06-30 15:51:30 +0200 | [diff] [blame] | 105 | * @brief Get the names of the loaded modules. |
| 106 | * |
| 107 | * @param[in] ctx Context with the modules. |
| 108 | * @return NULL-terminated array of the module names, |
| 109 | * NULL on error. The result must be freed by the caller. |
| 110 | */ |
| 111 | char **ly_ctx_get_module_names(struct ly_ctx *ctx); |
| 112 | |
| 113 | /** |
Michal Vasko | fa8c828 | 2015-07-03 15:14:59 +0200 | [diff] [blame] | 114 | * @brief Get the names of the loaded submodules of a loaded module. |
| 115 | * |
| 116 | * @param[in] ctx Context with the modules. |
| 117 | * @param[in] name Name of the parent module. |
| 118 | * @return NULL-terminated array of submodule names of the parent module, |
| 119 | * NULL on error. The result must be freed by the caller. |
| 120 | */ |
| 121 | char **ly_ctx_get_submodule_names(struct ly_ctx *ctx, const char *name); |
| 122 | |
| 123 | /** |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 124 | * @brief Free all internal structures of the specified context. |
| 125 | * |
| 126 | * The function should be used before terminating the application to destroy |
| 127 | * and free all structures internally used by libyang. If the caller uses |
| 128 | * multiple contexts, the function should be called for each used context. |
| 129 | * |
| 130 | * All instance data are supposed to be freed before destroying the context. |
| 131 | * Data models are destroyed automatically as part of ly_ctx_destroy() call. |
| 132 | * |
| 133 | * @param[in] ctx libyang context to destroy |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 134 | */ |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 135 | void ly_ctx_destroy(struct ly_ctx *ctx); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 136 | |
| 137 | /** |
| 138 | * @brief Load a data model into the specified context. |
| 139 | * |
Radek Krejci | b20c62d | 2015-07-07 17:07:14 +0200 | [diff] [blame] | 140 | * LY_IN_YANG (YANG) format is not yet supported. |
| 141 | * |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 142 | * @param[in] ctx libyang context where to process the data model. |
| 143 | * @param[in] data The string containing the dumped data model in the specified |
Michal Vasko | d8aa32d | 2015-07-24 11:50:01 +0200 | [diff] [blame^] | 144 | * format. |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 145 | * @param[in] format Format of the input data (YANG or YIN). |
Michal Vasko | d8aa32d | 2015-07-24 11:50:01 +0200 | [diff] [blame^] | 146 | * @param[in] implement Flag set if the module is implemented, not just |
| 147 | * imported from another module. |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 148 | * @return Pointer to the data model structure or NULL on error. |
| 149 | */ |
Michal Vasko | d8aa32d | 2015-07-24 11:50:01 +0200 | [diff] [blame^] | 150 | struct ly_module *ly_module_read(struct ly_ctx *ctx, const char *data, LY_MINFORMAT format, int implement); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 151 | |
Michal Vasko | d8aa32d | 2015-07-24 11:50:01 +0200 | [diff] [blame^] | 152 | struct ly_module *ly_module_read_fd(struct ly_ctx *ctx, int fd, LY_MINFORMAT format, int implement); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 153 | |
| 154 | /** |
| 155 | * @brief Free data model |
| 156 | * |
| 157 | * It is up to the caller that there is no instance data using the module |
| 158 | * being freed. |
| 159 | * |
| 160 | * @param[in] module Data model to free. |
| 161 | */ |
Radek Krejci | efaeba3 | 2015-05-27 14:30:57 +0200 | [diff] [blame] | 162 | void ly_module_free(struct ly_module *module); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 163 | |
Radek Krejci | b20c62d | 2015-07-07 17:07:14 +0200 | [diff] [blame] | 164 | /** |
| 165 | * @brief Parse (and validate according to appropriate schema from the given context) data. |
| 166 | * |
| 167 | * In case of LY_XML format, the data string is expected to contain XML data under the single |
| 168 | * \<config\> or \<data\> element in the "urn:ietf:params:xml:ns:netconf:base:1.0" namespace. |
| 169 | * |
| 170 | * LY_JSON format is not yet supported. |
| 171 | */ |
| 172 | struct lyd_node *ly_data_read(struct ly_ctx *ctx, const char *data, LY_DFORMAT format); |
| 173 | |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 174 | /**@} libyang */ |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 175 | |
Radek Krejci | 9b4ca39 | 2015-04-10 08:31:27 +0200 | [diff] [blame] | 176 | /** |
| 177 | * @page howto How To ... |
| 178 | * |
| 179 | * - @subpage howtologger |
| 180 | */ |
| 181 | |
| 182 | /** |
Radek Krejci | 9b4ca39 | 2015-04-10 08:31:27 +0200 | [diff] [blame] | 183 | * |
| 184 | * @page howtologger Logger |
| 185 | * |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 186 | * There are 4 verbosity levels defined as ::LY_LOG_LEVEL. The level can be |
| 187 | * changed by the ly_verb() function. By default, the verbosity level is |
| 188 | * set to #LY_LLERR value. |
Radek Krejci | 9b4ca39 | 2015-04-10 08:31:27 +0200 | [diff] [blame] | 189 | * |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 190 | * In case the logger has an error message (LY_LLERR) to print, also an error |
| 191 | * code is recorded in extern ly_errno variable. Possible values are of type |
| 192 | * ::LY_ERR. |
Radek Krejci | 9b4ca39 | 2015-04-10 08:31:27 +0200 | [diff] [blame] | 193 | */ |
| 194 | |
| 195 | /** |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 196 | * @defgroup logger Logger |
| 197 | * @{ |
| 198 | * |
| 199 | * Publicly visible functions and values of the libyang logger. For more |
| 200 | * information, see \ref howtologger. |
| 201 | */ |
| 202 | |
| 203 | /** |
| 204 | * @typedef LY_LOG_LEVEL |
| 205 | * @brief Verbosity levels of the libyang logger. |
| 206 | */ |
| 207 | typedef enum { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 208 | LY_LLERR, /**< Print only error messages. */ |
| 209 | LY_LLWRN, /**< Print error and warning messages. */ |
| 210 | LY_LLVRB, /**< Besides errors and warnings, print some other verbose messages. */ |
| 211 | LY_LLDBG /**< Print all messages including some development debug messages. */ |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 212 | } LY_LOG_LEVEL; |
| 213 | |
| 214 | /** |
| 215 | * @brief Set logger verbosity level. |
| 216 | * @param[in] level Verbosity level. |
| 217 | */ |
| 218 | void ly_verb(LY_LOG_LEVEL level); |
| 219 | |
| 220 | /** |
| 221 | * @typedef LY_ERR |
| 222 | * @brief libyang's error codes available via ly_errno extern variable |
Radek Krejci | 9b4ca39 | 2015-04-10 08:31:27 +0200 | [diff] [blame] | 223 | * @ingroup logger |
| 224 | */ |
| 225 | typedef enum { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 226 | LY_EMEM, /**< Memory allocation failure */ |
| 227 | LY_ESYS, /**< System call failure */ |
| 228 | LY_EINVAL, /**< Invalid value */ |
| 229 | LY_EINT, /**< Internal error */ |
| 230 | LY_EVALID /**< Validation failure */ |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 231 | } LY_ERR; |
| 232 | extern LY_ERR ly_errno; |
Radek Krejci | 9b4ca39 | 2015-04-10 08:31:27 +0200 | [diff] [blame] | 233 | |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 234 | /**@} logger */ |
Radek Krejci | 9b4ca39 | 2015-04-10 08:31:27 +0200 | [diff] [blame] | 235 | |
| 236 | #endif /* LY_LIBYANG_H_ */ |