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 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 27 | #include "tree_schema.h" |
| 28 | #include "tree_data.h" |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 29 | #include "xml.h" |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 30 | |
Radek Krejci | 39d8d0d | 2015-08-17 13:42:45 +0200 | [diff] [blame] | 31 | #ifdef __cplusplus |
| 32 | extern "C" { |
| 33 | #endif |
| 34 | |
Radek Krejci | 26715a4 | 2015-07-29 14:10:45 +0200 | [diff] [blame] | 35 | /** |
| 36 | * @page howto How To ... |
| 37 | * |
| 38 | * - @subpage howtocontext |
Radek Krejci | d9ba3e3 | 2015-07-30 15:08:18 +0200 | [diff] [blame] | 39 | * - @subpage howtoschemas |
| 40 | * - @subpage howtodata |
Radek Krejci | 26715a4 | 2015-07-29 14:10:45 +0200 | [diff] [blame] | 41 | * - @subpage howtologger |
| 42 | */ |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 43 | |
Radek Krejci | 26715a4 | 2015-07-29 14:10:45 +0200 | [diff] [blame] | 44 | /** @page howtocontext Context |
| 45 | * |
Radek Krejci | d9ba3e3 | 2015-07-30 15:08:18 +0200 | [diff] [blame] | 46 | * The context concept allows callers to work in environments with different sets of YANG schemas. |
Radek Krejci | 26715a4 | 2015-07-29 14:10:45 +0200 | [diff] [blame] | 47 | * |
| 48 | * The first step in libyang is to create a new context using ly_ctx_new(). It returns a handler |
| 49 | * used in the following work. |
| 50 | * |
| 51 | * When creating a new context, search dir can be specified (NULL is accepted) to provide directory |
| 52 | * where libyang will automatically search for schemas being imported or included. The search path |
| 53 | * can be later changed via ly_ctx_set_searchdir() function. Before exploring the specified search |
| 54 | * dir, libyang tries to get imported and included schemas from the current working directory first. |
| 55 | * |
Radek Krejci | d9ba3e3 | 2015-07-30 15:08:18 +0200 | [diff] [blame] | 56 | * Schemas are added into the context using [parser functions](@ref parsers) - lys_parse() or lys_read(). |
| 57 | * Note, that parser functions for schemas have \b lys_ prefix while instance data parser functions have |
| 58 | * \b lyd_ prefix. |
| 59 | * |
Radek Krejci | f647e61 | 2015-07-30 11:36:07 +0200 | [diff] [blame] | 60 | * Context can hold multiple revisons of the same schema. |
Radek Krejci | 26715a4 | 2015-07-29 14:10:45 +0200 | [diff] [blame] | 61 | * |
Radek Krejci | d9ba3e3 | 2015-07-30 15:08:18 +0200 | [diff] [blame] | 62 | * Context holds all modules and their submodules internally. The list of available module names is |
| 63 | * provided via ly_ctx_get_module_names() functions. Similarly, caller can get also a list of submodules |
| 64 | * names of a specific module using ly_ctx_get_submodule_names() function. The returned names can be |
| 65 | * subsequently used to get the (sub)module structures using ly_ctx_get_module() and ly_ctx_get_submodule(). |
| 66 | * |
| 67 | * Modules held by a context cannot be removed one after one. The only way how to \em change modules in the |
| 68 | * context is to create a new context and remove the old one. To remove a context, there is ly_ctx_destroy() |
| 69 | * function. |
| 70 | * |
| 71 | * \note API for this group of functions is available in the [context module](@ref context). |
| 72 | * |
| 73 | */ |
| 74 | |
| 75 | /** |
| 76 | * @page howtoschemas Schemas |
| 77 | * |
| 78 | * Schema is an internal libyang's representation of a YANG data model. Each schema is connected with |
| 79 | * its [context](@ref howtocontext) and loaded using [parser functions](@ref parsers). It means, that |
| 80 | * the schema cannot be created (nor changed) programatically. In libyang, schemas are used only to |
| 81 | * access data model definitions. |
| 82 | * |
| 83 | * \note There are many functions to access information from the schema trees. Details are available in |
| 84 | * the [Schema Tree module](@ref schematree). |
| 85 | * |
| 86 | * YANG Features Manipulation |
| 87 | * -------------------------- |
| 88 | * |
| 89 | * The group of functions prefixed by \b lys_features_ are used to access and manipulate with the schema's |
| 90 | * features. |
| 91 | * |
| 92 | * The first two functions are used to access information about the features in the schema. |
| 93 | * lys_features_list() provides list of all features defined in the specific schema and its |
| 94 | * submodules. Optionally, it can also provides information about the state of all features. |
| 95 | * Alternatively, caller can use lys_features_state() function to get state of one specific |
| 96 | * feature. |
| 97 | * |
| 98 | * The remaining two functions, lys_features_enable() and lys_features_disable(), are used |
| 99 | * to enable and disable the specific feature. By default, when the module is loaded by libyang |
| 100 | * parser, all features are disabled. |
| 101 | * |
| 102 | * Note, that feature's state can affect some of the output formats (e.g. Tree format). |
| 103 | * |
| 104 | */ |
| 105 | |
| 106 | /** |
| 107 | * @page howtodata Data Instances |
Radek Krejci | 26715a4 | 2015-07-29 14:10:45 +0200 | [diff] [blame] | 108 | * |
| 109 | */ |
Radek Krejci | 94ca54b | 2015-07-08 15:48:47 +0200 | [diff] [blame] | 110 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 111 | /** |
Radek Krejci | 26715a4 | 2015-07-29 14:10:45 +0200 | [diff] [blame] | 112 | * |
| 113 | * @page howtologger Logger |
| 114 | * |
| 115 | * There are 4 verbosity levels defined as ::LY_LOG_LEVEL. The level can be |
| 116 | * changed by the ly_verb() function. By default, the verbosity level is |
| 117 | * set to #LY_LLERR value. |
| 118 | * |
| 119 | * In case the logger has an error message (LY_LLERR) to print, also an error |
| 120 | * code is recorded in extern ly_errno variable. Possible values are of type |
| 121 | * ::LY_ERR. |
| 122 | * |
Radek Krejci | d9ba3e3 | 2015-07-30 15:08:18 +0200 | [diff] [blame] | 123 | * \note API for this group of functions is available in the [logger module](@ref logger). |
Radek Krejci | 26715a4 | 2015-07-29 14:10:45 +0200 | [diff] [blame] | 124 | */ |
| 125 | |
| 126 | /** |
| 127 | * @defgroup context Context |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 128 | * @{ |
| 129 | * |
Radek Krejci | 26715a4 | 2015-07-29 14:10:45 +0200 | [diff] [blame] | 130 | * Structures and functions to manipulate with the libyang "containers". The \em context concept allows callers |
| 131 | * to work in environments with different sets of YANG schemas. More detailed information can be found at |
| 132 | * @ref howtocontext page. |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 133 | */ |
| 134 | |
| 135 | /** |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 136 | * @brief libyang context handler. |
| 137 | */ |
| 138 | struct ly_ctx; |
| 139 | |
| 140 | /** |
| 141 | * @brief Create libyang context |
| 142 | * |
Radek Krejci | 26715a4 | 2015-07-29 14:10:45 +0200 | [diff] [blame] | 143 | * Context is used to hold all information about schemas. Usually, the application is supposed |
Radek Krejci | 91b833c | 2015-09-04 11:49:43 +0200 | [diff] [blame] | 144 | * to work with a single context in which libyang is holding all schemas (and other internal |
| 145 | * information) according to which the data trees will be processed and validated. So, the schema |
| 146 | * trees are tightly connected with the specific context and they are held by the context internally |
| 147 | * - caller does not need to keep pointers to the schemas returned by lys_parse(), context knows |
| 148 | * about them. The data trees created with lyd_parse() are still connected with the specific context, |
| 149 | * but they are not internally held by the context. The data tree just points and lean on some data |
| 150 | * held by the context (schema tree, string dictionary, etc.). Therefore, in case of data trees, caller |
| 151 | * is supposed to keep pointers returned by the lyd_parse() and manage the data tree on its own. This |
| 152 | * also affects the number of instances of both tree types. While you can have only one instance of |
| 153 | * specific schema connected with a single context, number of data tree instances is not connected. |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 154 | * |
Radek Krejci | 26715a4 | 2015-07-29 14:10:45 +0200 | [diff] [blame] | 155 | * @param[in] search_dir Directory where libyang will search for the imported or included modules |
| 156 | * and submodules. If no such directory is available, NULL is accepted. |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 157 | * |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 158 | * @return Pointer to the created libyang context, NULL in case of error. |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 159 | */ |
| 160 | struct ly_ctx *ly_ctx_new(const char *search_dir); |
| 161 | |
| 162 | /** |
Michal Vasko | 60ba9a6 | 2015-07-03 14:42:31 +0200 | [diff] [blame] | 163 | * @brief Change the search path in libyang context |
| 164 | * |
| 165 | * @param[in] ctx Context to be modified. |
| 166 | * @param[in] search_dir New search path to replace the current one in ctx. |
| 167 | */ |
| 168 | void ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir); |
| 169 | |
| 170 | /** |
Radek Krejci | 7ab2515 | 2015-08-07 14:48:45 +0200 | [diff] [blame] | 171 | * @brief Get data of an internal ietf-yang-library module. |
| 172 | * |
| 173 | * @param[in] ctx Context with the modules. |
| 174 | * @return Root data node corresponding to the model, NULL on error. |
| 175 | * Caller is responsible for freeing the returned data tree using lyd_free(). |
| 176 | */ |
| 177 | struct lyd_node *ly_ctx_info(struct ly_ctx *ctx); |
| 178 | |
| 179 | /** |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 180 | * @brief Get the names of the loaded modules. |
| 181 | * |
| 182 | * @param[in] ctx Context with the modules. |
| 183 | * @return NULL-terminated array of the module names, |
| 184 | * NULL on error. The returned array must be freed by the caller, do not free |
| 185 | * names in the array. Also remember that the names will be freed with freeing |
Radek Krejci | d9ba3e3 | 2015-07-30 15:08:18 +0200 | [diff] [blame] | 186 | * the context. |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 187 | */ |
| 188 | const char **ly_ctx_get_module_names(struct ly_ctx *ctx); |
| 189 | |
| 190 | /** |
| 191 | * @brief Get the names of the loaded submodules of the specified module. |
| 192 | * |
| 193 | * @param[in] ctx Context with the modules. |
| 194 | * @param[in] module_name Name of the parent module. |
| 195 | * @return NULL-terminated array of submodule names of the parent module, |
| 196 | * NULL on error. The returned array must be freed by the caller, do not free |
| 197 | * names in the array. Also remember that the names will be freed with freeing |
Radek Krejci | d9ba3e3 | 2015-07-30 15:08:18 +0200 | [diff] [blame] | 198 | * the context. |
Radek Krejci | 96a10da | 2015-07-30 11:00:14 +0200 | [diff] [blame] | 199 | */ |
| 200 | const char **ly_ctx_get_submodule_names(struct ly_ctx *ctx, const char *module_name); |
| 201 | |
| 202 | /** |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 203 | * @brief Get pointer to the schema tree of the module of the specified name. |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 204 | * |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 205 | * @param[in] ctx Context to work in. |
| 206 | * @param[in] name Name of the YANG module to get. |
Radek Krejci | f647e61 | 2015-07-30 11:36:07 +0200 | [diff] [blame] | 207 | * @param[in] revision Optional revision date of the YANG module to get. If not specified, |
| 208 | * the schema in the newest revision is returned if any. |
| 209 | * @return Pointer to the data model structure, NULL if no schema following the name and |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 210 | * revision requirements is present in the context. |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 211 | */ |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 212 | struct lys_module *ly_ctx_get_module(struct ly_ctx *ctx, const char *name, const char *revision); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 213 | |
| 214 | /** |
Radek Krejci | fd4e6e3 | 2015-08-10 15:00:51 +0200 | [diff] [blame] | 215 | * @brief Get pointer to the schema tree of the module of the specified namespace |
| 216 | * |
| 217 | * @param[in] ctx Context to work in. |
| 218 | * @param[in] ns Namespace of the YANG module to get. |
| 219 | * @param[in] revision Optional revision date of the YANG module to get. If not specified, |
| 220 | * the schema in the newest revision is returned if any. |
| 221 | * @return Pointer to the data model structure, NULL if no schema following the namespace and |
| 222 | * revision requirements is present in the context. |
| 223 | */ |
| 224 | struct lys_module *ly_ctx_get_module_by_ns(struct ly_ctx *ctx, const char *ns, const char *revision); |
| 225 | |
| 226 | /** |
Michal Vasko | 7bf0688 | 2015-07-03 15:33:56 +0200 | [diff] [blame] | 227 | * @brief Get submodule from the context's search dir. |
| 228 | * |
| 229 | * @param[in] module Parent (belongs-to) module. |
| 230 | * @param[in] name Name of the YANG submodule to get. |
| 231 | * @param[in] revision Optional revision date of the YANG submodule to get. If |
| 232 | * not specified, the newest revision is returned (TODO). |
Michal Vasko | 7bf0688 | 2015-07-03 15:33:56 +0200 | [diff] [blame] | 233 | * @return Pointer to the data model structure. |
| 234 | */ |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 235 | struct lys_submodule *ly_ctx_get_submodule(struct lys_module *module, const char *name, const char *revision); |
Michal Vasko | 7bf0688 | 2015-07-03 15:33:56 +0200 | [diff] [blame] | 236 | |
| 237 | /** |
Radek Krejci | 704a7a9 | 2015-08-07 12:50:15 +0200 | [diff] [blame] | 238 | * @brief Get schema node according to the given absolute schema node identifier. |
| 239 | * |
| 240 | * TODO not implemented |
| 241 | * |
| 242 | * @param[in] ctx Context to work in. |
| 243 | * @param[in] nodeid Absolute schema node identifier with module names used as |
| 244 | * prefixes. Prefix (module name) must be used whenever the child node is from |
| 245 | * other module (augments the parent node). The first node in the path must be |
| 246 | * always specified with the prefix. Here are some examples: |
| 247 | * |
| 248 | * - /ietf-netconf-monitoring:get-schema/input/identifier |
| 249 | * - /ietf-interfaces:interfaces/interface/ietf-ip:ipv4/address/ip |
| 250 | */ |
| 251 | struct lys_node *lys_ctx_get_node(struct ly_ctx *ctx, const char *nodeid); |
| 252 | |
| 253 | /** |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 254 | * @brief Free all internal structures of the specified context. |
| 255 | * |
| 256 | * The function should be used before terminating the application to destroy |
| 257 | * and free all structures internally used by libyang. If the caller uses |
| 258 | * multiple contexts, the function should be called for each used context. |
| 259 | * |
| 260 | * All instance data are supposed to be freed before destroying the context. |
| 261 | * Data models are destroyed automatically as part of ly_ctx_destroy() call. |
| 262 | * |
| 263 | * @param[in] ctx libyang context to destroy |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 264 | */ |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 265 | void ly_ctx_destroy(struct ly_ctx *ctx); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 266 | |
Radek Krejci | 26715a4 | 2015-07-29 14:10:45 +0200 | [diff] [blame] | 267 | /**@} context */ |
| 268 | |
| 269 | /** |
| 270 | * @defgroup parsers Parsers |
| 271 | * @{ |
| 272 | * |
| 273 | * Parsers allows to read schema and data trees from a specific format. |
| 274 | * |
| 275 | * For schemas, the following formats are supported: |
| 276 | * - YANG |
| 277 | * |
| 278 | * Basic YANG schemas format described in [RFC 6020](http://tools.ietf.org/html/rfc6020). |
| 279 | * Currently, only YANG 1.0 is supported. |
| 280 | * |
| 281 | * \todo YANG input is not yet implemented |
| 282 | * |
| 283 | * - YIN |
| 284 | * |
| 285 | * Alternative XML-based format to YANG. The details can be found in |
| 286 | * [RFC 6020](http://tools.ietf.org/html/rfc6020#section-11). |
| 287 | * |
Radek Krejci | d9ba3e3 | 2015-07-30 15:08:18 +0200 | [diff] [blame] | 288 | * For data instances, the following formats are supported: |
| 289 | * - \todo TBD |
| 290 | * |
Radek Krejci | 26715a4 | 2015-07-29 14:10:45 +0200 | [diff] [blame] | 291 | */ |
| 292 | |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 293 | /** |
Radek Krejci | 704a7a9 | 2015-08-07 12:50:15 +0200 | [diff] [blame] | 294 | * @brief Load a schema into the specified context. |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 295 | * |
Radek Krejci | b20c62d | 2015-07-07 17:07:14 +0200 | [diff] [blame] | 296 | * LY_IN_YANG (YANG) format is not yet supported. |
| 297 | * |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 298 | * @param[in] ctx libyang context where to process the data model. |
| 299 | * @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] | 300 | * format. |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 301 | * @param[in] format Format of the input data (YANG or YIN). |
| 302 | * @return Pointer to the data model structure or NULL on error. |
| 303 | */ |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 304 | struct lys_module *lys_parse(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 305 | |
Radek Krejci | 704a7a9 | 2015-08-07 12:50:15 +0200 | [diff] [blame] | 306 | /** |
| 307 | * @brief Read a schema from file into the specified context. |
| 308 | * |
| 309 | * LY_IN_YANG (YANG) format is not yet supported. |
| 310 | * |
| 311 | * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc. |
| 312 | * |
| 313 | * @param[in] ctx libyang context where to process the data model. |
| 314 | * @param[in] fd The standard file descriptor of the file containing the schema in the specified format. |
| 315 | * @param[in] format Format of the input data (YANG or YIN). |
| 316 | * @return Pointer to the data model structure or NULL on error. |
| 317 | */ |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 318 | struct lys_module *lys_read(struct ly_ctx *ctx, int fd, LYS_INFORMAT format); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 319 | |
| 320 | /** |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 321 | * @defgroup parseroptions Data parser options |
| 322 | * @ingroup parsers |
| 323 | * |
| 324 | * Various options to change the data tree parsers behavior. |
| 325 | * |
| 326 | * By default, parser silently ignores the data without a matching node in schema trees. If the caller want to stop |
| 327 | * parsing in case of presence of unknown data, the #LYD_OPT_STRICT can be used. The strict mode is useful for |
| 328 | * NETCONF servers, since NETCONF clients should always send data according to the capabilities announced by the server. |
| 329 | * On the other hand, the default non-strict mode is useful for clients receiving data from NETCONF server since |
| 330 | * clients are not required to understand everything the server does. Of course, the optimal strategy is to use |
| 331 | * filtering to get only the required data. |
| 332 | * |
| 333 | * Parser also expects that the provided data are complete and performs data validation according to all |
| 334 | * implemented YANG rules. This can be problem in case of representing NETCONF's subtree filter data or |
| 335 | * edit-config's data which do not represent a complete data set. Therefore there are two options to make parser to |
Michal Vasko | 163faae | 2015-10-02 15:23:00 +0200 | [diff] [blame] | 336 | * accept such a data: #LYD_OPT_FILTER and #LYD_OPT_EDIT. However, both these options are ignored when parsing |
| 337 | * an RPC or a notification. |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 338 | * |
| 339 | * @{ |
| 340 | */ |
Michal Vasko | 163faae | 2015-10-02 15:23:00 +0200 | [diff] [blame] | 341 | #define LYD_OPT_STRICT 0x01 /**< instead of silent ignoring data without schema definition, raise an error. |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 342 | Having an unknown element of the known namespace is always an error. */ |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 343 | #define LYD_OPT_EDIT 0x02 /**< make validation to accept NETCONF edit-config's content: |
| 344 | - mandatory nodes can be omitted |
Radek Krejci | bc9ee4f | 2015-08-19 13:44:51 +0200 | [diff] [blame] | 345 | - leafrefs and instance-identifier are not resolved |
| 346 | - status data are not allowed */ |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 347 | #define LYD_OPT_FILTER 0x04 /**< make validation to accept NETCONF subtree filter data: |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 348 | - leafs/leaf-lists with no data are allowed (even not allowed e.g. by length restriction) |
| 349 | - multiple instances of container/leaf/.. are allowed |
| 350 | - list's keys are not required |
Radek Krejci | b1c1251 | 2015-08-11 11:22:04 +0200 | [diff] [blame] | 351 | - mandatory nodes can be omitted |
Radek Krejci | bc9ee4f | 2015-08-19 13:44:51 +0200 | [diff] [blame] | 352 | - leafrefs and instance-identifier are not resolved |
| 353 | - data from different choice's branches are allowed */ |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 354 | /** |
| 355 | * @} |
| 356 | */ |
| 357 | |
| 358 | /** |
Radek Krejci | b20c62d | 2015-07-07 17:07:14 +0200 | [diff] [blame] | 359 | * @brief Parse (and validate according to appropriate schema from the given context) data. |
| 360 | * |
Radek Krejci | 4f0a874 | 2015-08-12 10:09:28 +0200 | [diff] [blame] | 361 | * In case of LY_XML format, the data string is expected to contain XML data under a single |
| 362 | * XML element. The element is not parsed, but it is expected to keep XML data well formed in all |
| 363 | * cases. There are no restrictions for the element name or its namespace. |
Radek Krejci | b20c62d | 2015-07-07 17:07:14 +0200 | [diff] [blame] | 364 | * |
| 365 | * LY_JSON format is not yet supported. |
Radek Krejci | 26715a4 | 2015-07-29 14:10:45 +0200 | [diff] [blame] | 366 | * |
| 367 | * @param[in] ctx Context to connect with the data tree being built here. |
| 368 | * @param[in] data Serialized data in the specified format. |
| 369 | * @param[in] format Format of the input data to be parsed. |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 370 | * @param[in] options Parser options, see @ref parseroptions. |
Radek Krejci | 26715a4 | 2015-07-29 14:10:45 +0200 | [diff] [blame] | 371 | * @return Pointer to the built data tree. To free the returned structure, use lyd_free(). |
Radek Krejci | b20c62d | 2015-07-07 17:07:14 +0200 | [diff] [blame] | 372 | */ |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 373 | struct lyd_node *lyd_parse(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options); |
Radek Krejci | b20c62d | 2015-07-07 17:07:14 +0200 | [diff] [blame] | 374 | |
Radek Krejci | 704a7a9 | 2015-08-07 12:50:15 +0200 | [diff] [blame] | 375 | /** |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 376 | * @brief Parse (and validate according to appropriate schema from the given context) XML tree. |
| 377 | * |
| 378 | * The output data tree is parsed from the given XML tree previously parsed by one of the |
| 379 | * lyxml_read* functions. Note, that the parser removes successfully parsed data from the |
| 380 | * XML tree except the root element (see the note about XML format in lyd_parse()). When |
| 381 | * the given XML tree is successfully parsed, the given \p root is kept but it has no children |
| 382 | * which are returned as a top level nodes in the output data tree. |
| 383 | * |
| 384 | * The context must be the same as the context used to parse XML tree by lyxml_read* function. |
| 385 | * |
| 386 | * @param[in] ctx Context to connect with the data tree being built here. |
| 387 | * @param[in] root XML tree to parse (convert) to data tree. |
| 388 | * @param[in] options Parser options, see @ref parseroptions. |
| 389 | * @return Pointer to the built data tree. To free the returned structure, use lyd_free(). |
| 390 | */ |
| 391 | struct lyd_node *lyd_parse_xml(struct ly_ctx *ctx, struct lyxml_elem *root, int options); |
| 392 | |
| 393 | /** |
Radek Krejci | 704a7a9 | 2015-08-07 12:50:15 +0200 | [diff] [blame] | 394 | * @brief Read data from the given file |
| 395 | * |
| 396 | * TODO not implemented |
| 397 | * |
| 398 | * @param[in] ctx Context to connect with the data tree being built here. |
| 399 | * @param[in] fd The standard file descriptor of the file containing the data tree in the specified format. |
| 400 | * @param[in] format Format of the input data to be parsed. |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 401 | * @param[in] options Parser options, see @ref parseroptions. |
Radek Krejci | 704a7a9 | 2015-08-07 12:50:15 +0200 | [diff] [blame] | 402 | * @return Pointer to the built data tree. To free the returned structure, use lyd_free(). |
| 403 | */ |
Radek Krejci | 25b9fd3 | 2015-08-10 15:06:07 +0200 | [diff] [blame] | 404 | struct lyd_node *lyd_read(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options); |
Radek Krejci | 704a7a9 | 2015-08-07 12:50:15 +0200 | [diff] [blame] | 405 | |
Radek Krejci | 26715a4 | 2015-07-29 14:10:45 +0200 | [diff] [blame] | 406 | /**@} parsers */ |
| 407 | |
Radek Krejci | d9ba3e3 | 2015-07-30 15:08:18 +0200 | [diff] [blame] | 408 | /** |
| 409 | * @defgroup schematree Schema Tree |
| 410 | * @{ |
| 411 | * |
| 412 | * Data structures and functions to manipulate and access schema tree. |
| 413 | * |
| 414 | * @} |
| 415 | */ |
| 416 | |
| 417 | /** |
| 418 | * @defgroup datatree Data Tree |
| 419 | * @{ |
| 420 | * |
| 421 | * Data structures and functions to manipulate and access instance data tree. |
| 422 | * |
| 423 | * @} |
| 424 | */ |
Radek Krejci | 9b4ca39 | 2015-04-10 08:31:27 +0200 | [diff] [blame] | 425 | |
| 426 | /** |
Radek Krejci | 26715a4 | 2015-07-29 14:10:45 +0200 | [diff] [blame] | 427 | * @defgroup printers Printers |
| 428 | * @{ |
Radek Krejci | 9b4ca39 | 2015-04-10 08:31:27 +0200 | [diff] [blame] | 429 | * |
Radek Krejci | 26715a4 | 2015-07-29 14:10:45 +0200 | [diff] [blame] | 430 | * Printers allows to serialize schema and data trees in a specific format. |
Radek Krejci | 9b4ca39 | 2015-04-10 08:31:27 +0200 | [diff] [blame] | 431 | * |
Radek Krejci | 26715a4 | 2015-07-29 14:10:45 +0200 | [diff] [blame] | 432 | * For schemas, the following formats are supported: |
| 433 | * - YANG |
Radek Krejci | 9b4ca39 | 2015-04-10 08:31:27 +0200 | [diff] [blame] | 434 | * |
Radek Krejci | 26715a4 | 2015-07-29 14:10:45 +0200 | [diff] [blame] | 435 | * Basic YANG schemas format described in [RFC 6020](http://tools.ietf.org/html/rfc6020). |
| 436 | * Currently, only YANG 1.0 is supported. |
| 437 | * |
| 438 | * - YIN |
| 439 | * |
| 440 | * Alternative XML-based format to YANG. The details can be found in |
| 441 | * [RFC 6020](http://tools.ietf.org/html/rfc6020#section-11). |
| 442 | * |
| 443 | * \todo YIN output is not yet implemented |
| 444 | * |
| 445 | * - Tree |
| 446 | * |
| 447 | * Simple tree structure of the module. |
| 448 | * |
| 449 | * - Info |
| 450 | * |
| 451 | * Detailed information about the specific node in the schema tree. |
Michal Vasko | 6d6076e | 2015-08-13 15:53:09 +0200 | [diff] [blame] | 452 | * The target can be more specific than the module itself: |
Radek Krejci | 26715a4 | 2015-07-29 14:10:45 +0200 | [diff] [blame] | 453 | * |
Michal Vasko | 6d6076e | 2015-08-13 15:53:09 +0200 | [diff] [blame] | 454 | * - absolute-schema-nodeid \a /modules/module-set-id in \a ietf-yang-library |
| 455 | * - <b>typedef/</b>typedef-name \a typedef/revision-identifier in \a ietf-yang-library |
| 456 | * - <b>feature/</b>feature-name \a feature/ssh in \a ietf-netconf-server |
| 457 | * - <b>grouping/</b>grouping-name/descendant-schema-nodeid \a grouping/module or \a grouping/module/module/submodules |
| 458 | * in \a ietf-yang-library |
| 459 | * - <b>type/</b>leaf-or-leaflist \a type/modules/module-set-id in \a ietf-yang-library |
Radek Krejci | d9ba3e3 | 2015-07-30 15:08:18 +0200 | [diff] [blame] | 460 | * |
| 461 | * For data instances, the following formats are supported: |
| 462 | * - \todo TBD |
| 463 | * |
Radek Krejci | 9b4ca39 | 2015-04-10 08:31:27 +0200 | [diff] [blame] | 464 | */ |
| 465 | |
| 466 | /** |
Radek Krejci | 912da45 | 2015-07-29 14:10:06 +0200 | [diff] [blame] | 467 | * @brief Print schema tree in the specified format. |
| 468 | * |
Radek Krejci | 87f5237 | 2015-10-09 09:34:46 +0200 | [diff] [blame] | 469 | * To write data into a file descriptor, use lys_print_fd(). |
| 470 | * |
Radek Krejci | 912da45 | 2015-07-29 14:10:06 +0200 | [diff] [blame] | 471 | * @param[in] module Schema tree to print. |
| 472 | * @param[in] f File stream where to print the schema. |
| 473 | * @param[in] format Schema output format. |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 474 | * @param[in] target_node Optional parameter for ::LYS_OUT_INFO format. It specifies which particular |
Radek Krejci | 912da45 | 2015-07-29 14:10:06 +0200 | [diff] [blame] | 475 | * node in the module will be printed. |
| 476 | * @return 0 on success, 1 on failure (#ly_errno is set). |
| 477 | */ |
Radek Krejci | b804869 | 2015-08-05 13:36:34 +0200 | [diff] [blame] | 478 | int lys_print(FILE *f, struct lys_module *module, LYS_OUTFORMAT format, const char *target_node); |
Radek Krejci | 912da45 | 2015-07-29 14:10:06 +0200 | [diff] [blame] | 479 | |
| 480 | /** |
Radek Krejci | 87f5237 | 2015-10-09 09:34:46 +0200 | [diff] [blame] | 481 | * @brief Print schema tree in the specified format. |
| 482 | * |
| 483 | * Same as lys_print(), but output is written into the specified file descriptor. |
| 484 | * |
| 485 | * @param[in] module Schema tree to print. |
| 486 | * @param[in] fd File descriptor where to print the data. |
| 487 | * @param[in] format Schema output format. |
| 488 | * @param[in] target_node Optional parameter for ::LYS_OUT_INFO format. It specifies which particular |
| 489 | * node in the module will be printed. |
| 490 | * @return 0 on success, 1 on failure (#ly_errno is set). |
| 491 | */ |
| 492 | int lys_print_fd(int fd, struct lys_module *module, LYS_OUTFORMAT format, const char *target_node); |
| 493 | |
| 494 | /** |
Radek Krejci | 6140e4e | 2015-10-09 15:50:55 +0200 | [diff] [blame] | 495 | * @brief Print schema tree in the specified format. |
| 496 | * |
| 497 | * Same as lys_print(), but output is written via provided callback. |
| 498 | * |
| 499 | * @param[in] module Schema tree to print. |
| 500 | * @param[in] writeclb Callback function to write the data (see write(1)). |
Radek Krejci | 50929eb | 2015-10-09 18:14:15 +0200 | [diff] [blame^] | 501 | * @param[in] arg Optional caller-specific argument to be passed to the \p writeclb callback. |
Radek Krejci | 6140e4e | 2015-10-09 15:50:55 +0200 | [diff] [blame] | 502 | * @param[in] format Schema output format. |
| 503 | * @param[in] target_node Optional parameter for ::LYS_OUT_INFO format. It specifies which particular |
| 504 | * node in the module will be printed. |
| 505 | * @return 0 on success, 1 on failure (#ly_errno is set). |
| 506 | */ |
Radek Krejci | 50929eb | 2015-10-09 18:14:15 +0200 | [diff] [blame^] | 507 | int lys_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg, struct lys_module *module, LYS_OUTFORMAT format, const char *target_node); |
Radek Krejci | 6140e4e | 2015-10-09 15:50:55 +0200 | [diff] [blame] | 508 | |
| 509 | /** |
Radek Krejci | 912da45 | 2015-07-29 14:10:06 +0200 | [diff] [blame] | 510 | * @brief Print data tree in the specified format. |
| 511 | * |
Radek Krejci | 87f5237 | 2015-10-09 09:34:46 +0200 | [diff] [blame] | 512 | * To write data into a file descriptor, use lyd_print_fd(). |
| 513 | * |
Radek Krejci | 912da45 | 2015-07-29 14:10:06 +0200 | [diff] [blame] | 514 | * @param[in] root Root node of the data tree to print. It can be actually any (not only real root) |
| 515 | * node of the data tree to print the specific subtree. |
| 516 | * @param[in] f File stream where to print the data. |
| 517 | * @param[in] format Data output format. |
| 518 | * @return 0 on success, 1 on failure (#ly_errno is set). |
| 519 | */ |
Radek Krejci | a9167ef | 2015-08-03 11:01:11 +0200 | [diff] [blame] | 520 | int lyd_print(FILE *f, struct lyd_node *root, LYD_FORMAT format); |
Radek Krejci | 912da45 | 2015-07-29 14:10:06 +0200 | [diff] [blame] | 521 | |
Radek Krejci | 87f5237 | 2015-10-09 09:34:46 +0200 | [diff] [blame] | 522 | /** |
| 523 | * @brief Print data tree in the specified format. |
| 524 | * |
| 525 | * Same as lyd_print(), but output is written into the specified file descriptor. |
| 526 | * |
| 527 | * @param[in] root Root node of the data tree to print. It can be actually any (not only real root) |
| 528 | * node of the data tree to print the specific subtree. |
| 529 | * @param[in] fd File descriptor where to print the data. |
| 530 | * @param[in] format Data output format. |
| 531 | * @return 0 on success, 1 on failure (#ly_errno is set). |
| 532 | */ |
| 533 | int lyd_print_fd(int fd, struct lyd_node *root, LYD_FORMAT format); |
| 534 | |
Radek Krejci | 6140e4e | 2015-10-09 15:50:55 +0200 | [diff] [blame] | 535 | /** |
| 536 | * @brief Print data tree in the specified format. |
| 537 | * |
| 538 | * Same as lyd_print(), but output is written via provided callback. |
| 539 | * |
| 540 | * @param[in] root Root node of the data tree to print. It can be actually any (not only real root) |
| 541 | * node of the data tree to print the specific subtree. |
| 542 | * @param[in] writeclb Callback function to write the data (see write(1)). |
Radek Krejci | 50929eb | 2015-10-09 18:14:15 +0200 | [diff] [blame^] | 543 | * @param[in] arg Optional caller-specific argument to be passed to the \p writeclb callback. |
Radek Krejci | 6140e4e | 2015-10-09 15:50:55 +0200 | [diff] [blame] | 544 | * @param[in] format Data output format. |
| 545 | * @return 0 on success, 1 on failure (#ly_errno is set). |
| 546 | */ |
Radek Krejci | 50929eb | 2015-10-09 18:14:15 +0200 | [diff] [blame^] | 547 | int lyd_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg, struct lyd_node *root, LYD_FORMAT format); |
Radek Krejci | 6140e4e | 2015-10-09 15:50:55 +0200 | [diff] [blame] | 548 | |
Radek Krejci | 912da45 | 2015-07-29 14:10:06 +0200 | [diff] [blame] | 549 | /**@} printers */ |
| 550 | |
| 551 | /** |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 552 | * @defgroup logger Logger |
| 553 | * @{ |
| 554 | * |
| 555 | * Publicly visible functions and values of the libyang logger. For more |
| 556 | * information, see \ref howtologger. |
| 557 | */ |
| 558 | |
| 559 | /** |
| 560 | * @typedef LY_LOG_LEVEL |
| 561 | * @brief Verbosity levels of the libyang logger. |
| 562 | */ |
| 563 | typedef enum { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 564 | LY_LLERR, /**< Print only error messages. */ |
| 565 | LY_LLWRN, /**< Print error and warning messages. */ |
| 566 | LY_LLVRB, /**< Besides errors and warnings, print some other verbose messages. */ |
| 567 | LY_LLDBG /**< Print all messages including some development debug messages. */ |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 568 | } LY_LOG_LEVEL; |
| 569 | |
| 570 | /** |
| 571 | * @brief Set logger verbosity level. |
| 572 | * @param[in] level Verbosity level. |
| 573 | */ |
| 574 | void ly_verb(LY_LOG_LEVEL level); |
| 575 | |
| 576 | /** |
| 577 | * @typedef LY_ERR |
Radek Krejci | 26715a4 | 2015-07-29 14:10:45 +0200 | [diff] [blame] | 578 | * @brief libyang's error codes available via ly_errno extern variable. |
Radek Krejci | 9b4ca39 | 2015-04-10 08:31:27 +0200 | [diff] [blame] | 579 | * @ingroup logger |
| 580 | */ |
| 581 | typedef enum { |
Radek Krejci | ae6817a | 2015-08-10 14:02:06 +0200 | [diff] [blame] | 582 | LY_SUCCESS, /**< no error, not set by functions, included just to complete #LY_ERR enumeration */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 583 | LY_EMEM, /**< Memory allocation failure */ |
| 584 | LY_ESYS, /**< System call failure */ |
| 585 | LY_EINVAL, /**< Invalid value */ |
| 586 | LY_EINT, /**< Internal error */ |
| 587 | LY_EVALID /**< Validation failure */ |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 588 | } LY_ERR; |
Radek Krejci | 26715a4 | 2015-07-29 14:10:45 +0200 | [diff] [blame] | 589 | /** |
| 590 | * @brief libyang specific errno. |
| 591 | */ |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 592 | extern LY_ERR ly_errno; |
Radek Krejci | 9b4ca39 | 2015-04-10 08:31:27 +0200 | [diff] [blame] | 593 | |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 594 | /**@} logger */ |
Radek Krejci | 9b4ca39 | 2015-04-10 08:31:27 +0200 | [diff] [blame] | 595 | |
Radek Krejci | 39d8d0d | 2015-08-17 13:42:45 +0200 | [diff] [blame] | 596 | #ifdef __cplusplus |
| 597 | } |
| 598 | #endif |
| 599 | |
Radek Krejci | 9b4ca39 | 2015-04-10 08:31:27 +0200 | [diff] [blame] | 600 | #endif /* LY_LIBYANG_H_ */ |