blob: 0c24000cf73704261777dd5e139abfa1a439552a [file] [log] [blame]
Radek Krejci9b4ca392015-04-10 08:31:27 +02001/**
Radek Krejci3045cf32015-05-28 10:58:52 +02002 * @file libyang.h
Radek Krejci9b4ca392015-04-10 08:31:27 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Radek Krejci3045cf32015-05-28 10:58:52 +02004 * @brief The main libyang public header.
Radek Krejci9b4ca392015-04-10 08:31:27 +02005 *
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 Krejcida04f4a2015-05-21 12:54:09 +020025#include <stdio.h>
26
Michal Vasko2d162e12015-09-24 14:33:29 +020027#include "tree_schema.h"
28#include "tree_data.h"
Radek Krejcic6704c82015-10-06 11:12:45 +020029#include "xml.h"
Radek Krejci41912fe2015-10-22 10:22:12 +020030#include "dict.h"
Radek Krejcida04f4a2015-05-21 12:54:09 +020031
Radek Krejci39d8d0d2015-08-17 13:42:45 +020032#ifdef __cplusplus
33extern "C" {
34#endif
35
Radek Krejci26715a42015-07-29 14:10:45 +020036/**
37 * @page howto How To ...
38 *
39 * - @subpage howtocontext
Radek Krejcid9ba3e32015-07-30 15:08:18 +020040 * - @subpage howtoschemas
41 * - @subpage howtodata
Radek Krejci26715a42015-07-29 14:10:45 +020042 * - @subpage howtologger
43 */
Radek Krejcida04f4a2015-05-21 12:54:09 +020044
Radek Krejci26715a42015-07-29 14:10:45 +020045/** @page howtocontext Context
46 *
Radek Krejcid9ba3e32015-07-30 15:08:18 +020047 * The context concept allows callers to work in environments with different sets of YANG schemas.
Radek Krejci26715a42015-07-29 14:10:45 +020048 *
49 * The first step in libyang is to create a new context using ly_ctx_new(). It returns a handler
50 * used in the following work.
51 *
52 * When creating a new context, search dir can be specified (NULL is accepted) to provide directory
53 * where libyang will automatically search for schemas being imported or included. The search path
54 * can be later changed via ly_ctx_set_searchdir() function. Before exploring the specified search
55 * dir, libyang tries to get imported and included schemas from the current working directory first.
56 *
Radek Krejcid9ba3e32015-07-30 15:08:18 +020057 * Schemas are added into the context using [parser functions](@ref parsers) - lys_parse() or lys_read().
58 * Note, that parser functions for schemas have \b lys_ prefix while instance data parser functions have
59 * \b lyd_ prefix.
60 *
Radek Krejcif647e612015-07-30 11:36:07 +020061 * Context can hold multiple revisons of the same schema.
Radek Krejci26715a42015-07-29 14:10:45 +020062 *
Radek Krejcid9ba3e32015-07-30 15:08:18 +020063 * Context holds all modules and their submodules internally. The list of available module names is
64 * provided via ly_ctx_get_module_names() functions. Similarly, caller can get also a list of submodules
65 * names of a specific module using ly_ctx_get_submodule_names() function. The returned names can be
66 * subsequently used to get the (sub)module structures using ly_ctx_get_module() and ly_ctx_get_submodule().
67 *
68 * Modules held by a context cannot be removed one after one. The only way how to \em change modules in the
69 * context is to create a new context and remove the old one. To remove a context, there is ly_ctx_destroy()
70 * function.
71 *
72 * \note API for this group of functions is available in the [context module](@ref context).
73 *
74 */
75
76/**
77 * @page howtoschemas Schemas
78 *
79 * Schema is an internal libyang's representation of a YANG data model. Each schema is connected with
80 * its [context](@ref howtocontext) and loaded using [parser functions](@ref parsers). It means, that
81 * the schema cannot be created (nor changed) programatically. In libyang, schemas are used only to
82 * access data model definitions.
83 *
84 * \note There are many functions to access information from the schema trees. Details are available in
85 * the [Schema Tree module](@ref schematree).
86 *
87 * YANG Features Manipulation
88 * --------------------------
89 *
90 * The group of functions prefixed by \b lys_features_ are used to access and manipulate with the schema's
91 * features.
92 *
93 * The first two functions are used to access information about the features in the schema.
94 * lys_features_list() provides list of all features defined in the specific schema and its
95 * submodules. Optionally, it can also provides information about the state of all features.
96 * Alternatively, caller can use lys_features_state() function to get state of one specific
97 * feature.
98 *
99 * The remaining two functions, lys_features_enable() and lys_features_disable(), are used
100 * to enable and disable the specific feature. By default, when the module is loaded by libyang
101 * parser, all features are disabled.
102 *
103 * Note, that feature's state can affect some of the output formats (e.g. Tree format).
104 *
105 */
106
107/**
108 * @page howtodata Data Instances
Radek Krejci26715a42015-07-29 14:10:45 +0200109 *
110 */
Radek Krejci94ca54b2015-07-08 15:48:47 +0200111
Radek Krejcida04f4a2015-05-21 12:54:09 +0200112/**
Radek Krejci26715a42015-07-29 14:10:45 +0200113 *
114 * @page howtologger Logger
115 *
116 * There are 4 verbosity levels defined as ::LY_LOG_LEVEL. The level can be
117 * changed by the ly_verb() function. By default, the verbosity level is
118 * set to #LY_LLERR value.
119 *
120 * In case the logger has an error message (LY_LLERR) to print, also an error
121 * code is recorded in extern ly_errno variable. Possible values are of type
122 * ::LY_ERR.
123 *
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200124 * \note API for this group of functions is available in the [logger module](@ref logger).
Radek Krejci26715a42015-07-29 14:10:45 +0200125 */
126
127/**
128 * @defgroup context Context
Radek Krejci3045cf32015-05-28 10:58:52 +0200129 * @{
130 *
Radek Krejci26715a42015-07-29 14:10:45 +0200131 * Structures and functions to manipulate with the libyang "containers". The \em context concept allows callers
132 * to work in environments with different sets of YANG schemas. More detailed information can be found at
133 * @ref howtocontext page.
Radek Krejci3045cf32015-05-28 10:58:52 +0200134 */
135
136/**
Radek Krejcida04f4a2015-05-21 12:54:09 +0200137 * @brief libyang context handler.
138 */
139struct ly_ctx;
140
141/**
142 * @brief Create libyang context
143 *
Radek Krejci26715a42015-07-29 14:10:45 +0200144 * Context is used to hold all information about schemas. Usually, the application is supposed
Radek Krejci91b833c2015-09-04 11:49:43 +0200145 * to work with a single context in which libyang is holding all schemas (and other internal
146 * information) according to which the data trees will be processed and validated. So, the schema
147 * trees are tightly connected with the specific context and they are held by the context internally
148 * - caller does not need to keep pointers to the schemas returned by lys_parse(), context knows
149 * about them. The data trees created with lyd_parse() are still connected with the specific context,
150 * but they are not internally held by the context. The data tree just points and lean on some data
151 * held by the context (schema tree, string dictionary, etc.). Therefore, in case of data trees, caller
152 * is supposed to keep pointers returned by the lyd_parse() and manage the data tree on its own. This
153 * also affects the number of instances of both tree types. While you can have only one instance of
154 * specific schema connected with a single context, number of data tree instances is not connected.
Radek Krejcida04f4a2015-05-21 12:54:09 +0200155 *
Radek Krejci26715a42015-07-29 14:10:45 +0200156 * @param[in] search_dir Directory where libyang will search for the imported or included modules
157 * and submodules. If no such directory is available, NULL is accepted.
Radek Krejcida04f4a2015-05-21 12:54:09 +0200158 *
Radek Krejci3045cf32015-05-28 10:58:52 +0200159 * @return Pointer to the created libyang context, NULL in case of error.
Radek Krejcida04f4a2015-05-21 12:54:09 +0200160 */
161struct ly_ctx *ly_ctx_new(const char *search_dir);
162
163/**
Michal Vasko60ba9a62015-07-03 14:42:31 +0200164 * @brief Change the search path in libyang context
165 *
166 * @param[in] ctx Context to be modified.
167 * @param[in] search_dir New search path to replace the current one in ctx.
168 */
169void ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir);
170
171/**
Radek Krejci5a797572015-10-21 15:45:45 +0200172 * @brief Get current value of the search path in libyang context
173 *
174 * @param[in] ctx Context to query.
175 * @return Current value of the search path.
176 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100177const char *ly_ctx_get_searchdir(const struct ly_ctx *ctx);
Radek Krejci5a797572015-10-21 15:45:45 +0200178
179/**
Radek Krejci7ab25152015-08-07 14:48:45 +0200180 * @brief Get data of an internal ietf-yang-library module.
181 *
182 * @param[in] ctx Context with the modules.
183 * @return Root data node corresponding to the model, NULL on error.
184 * Caller is responsible for freeing the returned data tree using lyd_free().
185 */
186struct lyd_node *ly_ctx_info(struct ly_ctx *ctx);
187
188/**
Radek Krejci96a10da2015-07-30 11:00:14 +0200189 * @brief Get the names of the loaded modules.
190 *
191 * @param[in] ctx Context with the modules.
192 * @return NULL-terminated array of the module names,
193 * NULL on error. The returned array must be freed by the caller, do not free
194 * names in the array. Also remember that the names will be freed with freeing
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200195 * the context.
Radek Krejci96a10da2015-07-30 11:00:14 +0200196 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100197const char **ly_ctx_get_module_names(const struct ly_ctx *ctx);
Radek Krejci96a10da2015-07-30 11:00:14 +0200198
199/**
200 * @brief Get the names of the loaded submodules of the specified module.
201 *
202 * @param[in] ctx Context with the modules.
203 * @param[in] module_name Name of the parent module.
204 * @return NULL-terminated array of submodule names of the parent module,
205 * NULL on error. The returned array must be freed by the caller, do not free
206 * names in the array. Also remember that the names will be freed with freeing
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200207 * the context.
Radek Krejci96a10da2015-07-30 11:00:14 +0200208 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100209const char **ly_ctx_get_submodule_names(const struct ly_ctx *ctx, const char *module_name);
Radek Krejci96a10da2015-07-30 11:00:14 +0200210
211/**
Radek Krejcifd4e6e32015-08-10 15:00:51 +0200212 * @brief Get pointer to the schema tree of the module of the specified name.
Radek Krejcida04f4a2015-05-21 12:54:09 +0200213 *
Radek Krejcida04f4a2015-05-21 12:54:09 +0200214 * @param[in] ctx Context to work in.
215 * @param[in] name Name of the YANG module to get.
Radek Krejcif647e612015-07-30 11:36:07 +0200216 * @param[in] revision Optional revision date of the YANG module to get. If not specified,
217 * the schema in the newest revision is returned if any.
218 * @return Pointer to the data model structure, NULL if no schema following the name and
Radek Krejcifd4e6e32015-08-10 15:00:51 +0200219 * revision requirements is present in the context.
Radek Krejcida04f4a2015-05-21 12:54:09 +0200220 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100221const struct lys_module *ly_ctx_get_module(const struct ly_ctx *ctx, const char *name, const char *revision);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200222
223/**
Michal Vasko99b0aad2015-12-01 12:28:51 +0100224 * @brief Try to find the model in the searchpath of \p ctx and load it into it. If custom missing
225 * module callback is set, it is used instead.
Michal Vasko82465962015-11-10 11:03:11 +0100226 *
227 * @param[in] ctx Context to add to.
Michal Vasko82465962015-11-10 11:03:11 +0100228 * @param[in] name Name of the module to load.
229 * @param[in] revision Optional revision date of the module. If not specified, it is
230 * assumed that there is only one model revision in the searchpath (the first matching file
231 * is parsed).
232 * @return Pointer to the data model structure, NULL if not found or some error occured.
233 */
Michal Vasko99b0aad2015-12-01 12:28:51 +0100234const struct lys_module *ly_ctx_load_module(struct ly_ctx *ctx, const char *name, const char *revision);
235
236/**
237 * @brief Callback for retrieving missing included or imported models in a custom way.
238 *
239 * @param[in] name Missing module name.
240 * @param[in] revision Optional missing module revision.
241 * @param[in] user_data User-supplied callback data.
242 * @param[out] format Format of the returned module data.
243 * @param[out] free_module_data Optional callback for freeing the returned module data. If not set, free() is used.
244 * @return Requested module data or NULL on error.
245 */
246typedef char *(*ly_module_clb)(const char *name, const char *revision, void *user_data, LYS_INFORMAT *format,
247 void (**free_module_data)(char *model_data));
248
249/**
250 * @brief Set missing include or import model callback.
251 *
252 * @param[in] ctx Context that will use this callback.
253 * @param[in] clb Callback responsible for returning a missing model.
254 * @param[in] user_data Arbitrary data that will always be passed to the callback \p clb.
255 */
256void ly_ctx_set_module_clb(struct ly_ctx *ctx, ly_module_clb clb, void *user_data);
257
258/**
259 * @brief Get the custom callback for missing module retrieval.
260 *
261 * @param[in] ctx Context to read from.
262 * @param[in] user_data Optional pointer for getting the user-supplied callbck data.
263 * @return Custom user missing module callback or NULL if not set.
264 */
265ly_module_clb ly_ctx_get_module_clb(const struct ly_ctx *ctx, void **user_data);
Michal Vasko82465962015-11-10 11:03:11 +0100266
267/**
Radek Krejcifd4e6e32015-08-10 15:00:51 +0200268 * @brief Get pointer to the schema tree of the module of the specified namespace
269 *
270 * @param[in] ctx Context to work in.
271 * @param[in] ns Namespace of the YANG module to get.
272 * @param[in] revision Optional revision date of the YANG module to get. If not specified,
273 * the schema in the newest revision is returned if any.
274 * @return Pointer to the data model structure, NULL if no schema following the namespace and
275 * revision requirements is present in the context.
276 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100277const struct lys_module *ly_ctx_get_module_by_ns(const struct ly_ctx *ctx, const char *ns, const char *revision);
Radek Krejcifd4e6e32015-08-10 15:00:51 +0200278
279/**
Michal Vasko7bf06882015-07-03 15:33:56 +0200280 * @brief Get submodule from the context's search dir.
281 *
282 * @param[in] module Parent (belongs-to) module.
283 * @param[in] name Name of the YANG submodule to get.
284 * @param[in] revision Optional revision date of the YANG submodule to get. If
285 * not specified, the newest revision is returned (TODO).
Michal Vasko7bf06882015-07-03 15:33:56 +0200286 * @return Pointer to the data model structure.
287 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100288const struct lys_submodule *ly_ctx_get_submodule(const struct lys_module *module, const char *name,
289 const char *revision);
Michal Vasko7bf06882015-07-03 15:33:56 +0200290
291/**
Radek Krejci704a7a92015-08-07 12:50:15 +0200292 * @brief Get schema node according to the given absolute schema node identifier.
293 *
Michal Vasko8afe75e2015-12-11 12:02:20 +0100294 * Prefix (module name) must be used in two cases. Firstly, the first node MUST
295 * have a prefix, which is treated as the current module. Secondly, every other
296 * node MUST have a prefix if its module differs from the current module.
297 * Here are some examples:
Radek Krejci704a7a92015-08-07 12:50:15 +0200298 *
Michal Vasko8afe75e2015-12-11 12:02:20 +0100299 * /ietf-netconf-monitoring:get-schema/input/identifier
300 * /ietf-interfaces:interfaces/interface/ietf-ip:ipv4/ietf-ip:address/ietf-ip:ip
301 *
302 * @param[in] ctx Context to work in.
303 * @param[in] nodeid Absolute schema node identifier.
304 * @return Resolved schema node or NULL.
Radek Krejci704a7a92015-08-07 12:50:15 +0200305 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100306const struct lys_node *ly_ctx_get_node(const struct ly_ctx *ctx, const char *nodeid);
Radek Krejci704a7a92015-08-07 12:50:15 +0200307
308/**
Radek Krejci3045cf32015-05-28 10:58:52 +0200309 * @brief Free all internal structures of the specified context.
310 *
311 * The function should be used before terminating the application to destroy
312 * and free all structures internally used by libyang. If the caller uses
313 * multiple contexts, the function should be called for each used context.
314 *
315 * All instance data are supposed to be freed before destroying the context.
316 * Data models are destroyed automatically as part of ly_ctx_destroy() call.
317 *
318 * @param[in] ctx libyang context to destroy
Radek Krejcida04f4a2015-05-21 12:54:09 +0200319 */
Radek Krejci3045cf32015-05-28 10:58:52 +0200320void ly_ctx_destroy(struct ly_ctx *ctx);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200321
Radek Krejci26715a42015-07-29 14:10:45 +0200322/**@} context */
323
324/**
325 * @defgroup parsers Parsers
326 * @{
327 *
328 * Parsers allows to read schema and data trees from a specific format.
329 *
330 * For schemas, the following formats are supported:
331 * - YANG
332 *
333 * Basic YANG schemas format described in [RFC 6020](http://tools.ietf.org/html/rfc6020).
334 * Currently, only YANG 1.0 is supported.
335 *
336 * \todo YANG input is not yet implemented
337 *
338 * - YIN
339 *
340 * Alternative XML-based format to YANG. The details can be found in
341 * [RFC 6020](http://tools.ietf.org/html/rfc6020#section-11).
342 *
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200343 * For data instances, the following formats are supported:
344 * - \todo TBD
345 *
Radek Krejci26715a42015-07-29 14:10:45 +0200346 */
347
Radek Krejcida04f4a2015-05-21 12:54:09 +0200348/**
Radek Krejci704a7a92015-08-07 12:50:15 +0200349 * @brief Load a schema into the specified context.
Radek Krejcida04f4a2015-05-21 12:54:09 +0200350 *
Radek Krejcib20c62d2015-07-07 17:07:14 +0200351 * LY_IN_YANG (YANG) format is not yet supported.
352 *
Radek Krejcida04f4a2015-05-21 12:54:09 +0200353 * @param[in] ctx libyang context where to process the data model.
354 * @param[in] data The string containing the dumped data model in the specified
Michal Vaskod8aa32d2015-07-24 11:50:01 +0200355 * format.
Radek Krejcida04f4a2015-05-21 12:54:09 +0200356 * @param[in] format Format of the input data (YANG or YIN).
357 * @return Pointer to the data model structure or NULL on error.
358 */
Michal Vasko662610a2015-12-07 11:25:45 +0100359const struct lys_module *lys_parse_data(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200360
Radek Krejci704a7a92015-08-07 12:50:15 +0200361/**
Michal Vasko662610a2015-12-07 11:25:45 +0100362 * @brief Load a schema into the specified context from a file.
363 *
364 * LY_IN_YANG (YANG) format is not yet supported.
365 *
366 * @param[in] ctx libyang context where to process the data model.
367 * @param[in] path Path to the file with the model in the specified format.
368 * @param[in] format Format of the input data (YANG or YIN).
369 * @return Pointer to the data model structure or NULL on error.
370 */
371const struct lys_module *lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format);
372
373/**
374 * @brief Read a schema from file descriptor into the specified context.
Radek Krejci704a7a92015-08-07 12:50:15 +0200375 *
376 * LY_IN_YANG (YANG) format is not yet supported.
377 *
378 * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc.
379 *
380 * @param[in] ctx libyang context where to process the data model.
Radek Krejci4910e242015-11-30 10:40:45 +0100381 * @param[in] fd File descriptor of a regular file (e.g. sockets are not supported) containing the schema
382 * in the specified format.
Radek Krejci704a7a92015-08-07 12:50:15 +0200383 * @param[in] format Format of the input data (YANG or YIN).
384 * @return Pointer to the data model structure or NULL on error.
385 */
Michal Vasko662610a2015-12-07 11:25:45 +0100386const struct lys_module *lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200387
388/**
Radek Krejci25b9fd32015-08-10 15:06:07 +0200389 * @defgroup parseroptions Data parser options
390 * @ingroup parsers
391 *
392 * Various options to change the data tree parsers behavior.
393 *
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100394 * Default behavior:
395 * - in case of XML, parser reads all data from its input (file, memory, XML tree) including the case of not well-formed
396 * XML document (multiple top-level elements) and if there is an unknown element, it is skipped including its subtree
397 * (see the next point). This can be changed by the #LYD_OPT_NOSIBLINGS option which make parser to read only a single
398 * tree (with a single root element) from its input.
399 * - parser silently ignores the data without a matching node in schema trees. If the caller want to stop
Radek Krejci25b9fd32015-08-10 15:06:07 +0200400 * parsing in case of presence of unknown data, the #LYD_OPT_STRICT can be used. The strict mode is useful for
401 * NETCONF servers, since NETCONF clients should always send data according to the capabilities announced by the server.
402 * On the other hand, the default non-strict mode is useful for clients receiving data from NETCONF server since
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100403 * clients are not required to understand everything the server does. Of course, the optimal strategy for clients is
404 * to use filtering to get only the required data. Having an unknown element of the known namespace is always an error.
405 * The behavior can be changed by #LYD_OPT_STRICT option.
406 * - using obsolete statements (status set to obsolete) just generates a warning, but the processing continues. The
407 * behavior can be changed by #LYD_OPT_OBSOLETE option.
408 * - parser expects that the provided data provides complete datastore content (both the configuration and state data)
409 * and performs data validation according to all YANG rules. This can be a problem in case of representing NETCONF's
410 * subtree filter data, edit-config's data or other type of data set - such data do not represent a complete data set
411 * and some of the validation rules can fail. Therefore there are other options (within lower 8 bits) to make parser
412 * to accept such a data.
Radek Krejci25b9fd32015-08-10 15:06:07 +0200413 * @{
414 */
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100415
416#define LYD_OPT_DATA 0x00 /**< Default type of data - complete datastore content with configuration as well as
417 state data. */
418#define LYD_OPT_CONFIG 0x01 /**< A configuration datastore - complete datastore without state data.
419 Validation modifications:
420 - status data are not allowed */
421#define LYD_OPT_GET 0x02 /**< Data content from a NETCONF reply message to the NETCONF \<get\> operation.
422 Validation modifications:
423 - mandatory nodes can be omitted
424 - leafrefs and instance-identifier are not resolved
425 - list's keys/unique nodes are not required (so duplication is not checked) */
426#define LYD_OPT_GETCONFIG 0x04 /**< Data content from a NETCONF reply message to the NETCONF \<get-config\> operation
427 Validation modifications:
428 - mandatory nodes can be omitted
429 - leafrefs and instance-identifier are not resolved
430 - list's keys/unique nodes are not required (so duplication is not checked)
431 - status data are not allowed */
432#define LYD_OPT_EDIT 0x08 /**< Content of the NETCONF \<edit-config\>'s config element.
433 Validation modifications:
Radek Krejci20cdf632015-11-09 14:44:58 +0100434 - mandatory nodes can be omitted
435 - leafrefs and instance-identifier are not resolved
436 - status data are not allowed */
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100437#define LYD_OPT_RPC 0x10 /**< Data represents RPC's input parameters. */
438#define LYD_OPT_RPCREPLY 0x20 /**< Data represents RPC's output parameters (maps to NETCONF <rpc-reply> data). */
439#define LYD_OPT_NOTIF 0x40 /**< Data represents an event notification data. */
440#define LYD_OPT_FILTER 0x80 /**< Data represents NETCONF subtree filter. Validation modifications:
Radek Krejci20cdf632015-11-09 14:44:58 +0100441 - leafs/leaf-lists with no data are allowed (even not allowed e.g. by length restriction)
442 - multiple instances of container/leaf/.. are allowed
443 - list's keys/unique nodes are not required
444 - mandatory nodes can be omitted
445 - leafrefs and instance-identifier are not resolved
446 - data from different choice's branches are allowed */
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100447#define LYD_OPT_TYPEMASK 0xff /**< Mask to filter data type options. Always only a single data type option (only
448 single bit from the lower 8 bits) can be set. */
449
450#define LYD_OPT_STRICT 0x0100 /**< Instead of silent ignoring data without schema definition, raise an error. */
451#define LYD_OPT_DESTRUCT 0x0200 /**< Free the provided XML tree during parsing the data. With this option, the
452 provided XML tree is affected and all succesfully parsed data are freed.
453 This option is applicable only to lyd_parse_xml() function. */
454#define LYD_OPT_OBSOLETE 0x0400 /**< Raise an error when an obsolete statement (status set to obsolete) is used. */
455#define LYD_OPT_NOSIBLINGS 0x0800 /**< Parse only a single XML tree from the input. This option applies only to
456 XML input data. */
Radek Krejci04b97de2015-10-31 23:09:15 +0100457
Radek Krejci25b9fd32015-08-10 15:06:07 +0200458/**
459 * @}
460 */
461
462/**
Radek Krejcib20c62d2015-07-07 17:07:14 +0200463 * @brief Parse (and validate according to appropriate schema from the given context) data.
464 *
Radek Krejci86538212015-12-17 15:59:01 +0100465 * In case of LY_XML format, the data string is parsed completely. It means that when it contains
466 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
467 * returned data node is a root of the first tree with other trees connected via the next pointer.
468 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
Radek Krejcib20c62d2015-07-07 17:07:14 +0200469 *
Radek Krejci26715a42015-07-29 14:10:45 +0200470 * @param[in] ctx Context to connect with the data tree being built here.
471 * @param[in] data Serialized data in the specified format.
472 * @param[in] format Format of the input data to be parsed.
Radek Krejci25b9fd32015-08-10 15:06:07 +0200473 * @param[in] options Parser options, see @ref parseroptions.
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100474 * @param[in] ... Additional argument must be supplied when #LYD_OPT_RPCREPLY value is specified in \p options. The
475 * argument is supposed to provide pointer to the RPC schema node for the reply's request
476 * (const struct ::lys_node* rpc).
Radek Krejci26715a42015-07-29 14:10:45 +0200477 * @return Pointer to the built data tree. To free the returned structure, use lyd_free().
Radek Krejcib20c62d2015-07-07 17:07:14 +0200478 */
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100479struct lyd_node *lyd_parse_data(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, ...);
Michal Vasko36ef6932015-12-01 14:30:17 +0100480
481/**
Radek Krejcic6704c82015-10-06 11:12:45 +0200482 * @brief Parse (and validate according to appropriate schema from the given context) XML tree.
483 *
484 * The output data tree is parsed from the given XML tree previously parsed by one of the
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100485 * lyxml_read* functions.
Radek Krejci86538212015-12-17 15:59:01 +0100486 *
487 * If there are some sibling elements of the \p root (data were read with #LYXML_READ_MULTIROOT option
488 * or the provided root is a root element of a subtree), all the sibling nodes (previous as well as
489 * following) are processed as well. The returned data node is a root of the first tree with other
490 * trees connected via the next pointer. This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
491 *
492 * When the function is used with #LYD_OPT_DESTRUCT, all the successfully parsed data including the
493 * XML \p root and all its siblings (if #LYD_OPT_NOSIBLINGS is not used) are freed. Only with
494 * #LYD_OPT_DESTRUCT option the \p root pointer is changed - if all the data are parsed, it is set
495 * to NULL, otherwise it will hold the XML tree without the successfully parsed elements.
Radek Krejcic6704c82015-10-06 11:12:45 +0200496 *
497 * The context must be the same as the context used to parse XML tree by lyxml_read* function.
498 *
499 * @param[in] ctx Context to connect with the data tree being built here.
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100500 * @param[in,out] root XML tree to parse (convert) to data tree. By default, parser do not change the XML tree. However,
501 * when #LYD_OPT_DESTRUCT is specified in \p options, parser frees all successfully parsed data.
Radek Krejcic6704c82015-10-06 11:12:45 +0200502 * @param[in] options Parser options, see @ref parseroptions.
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100503 * @param[in] ... Additional argument must be supplied when #LYD_OPT_RPCREPLY value is specified in \p options. The
504 * argument is supposed to provide pointer to the RPC schema node for the reply's request
505 * (const struct ::lys_node* rpc).
Radek Krejcic6704c82015-10-06 11:12:45 +0200506 * @return Pointer to the built data tree. To free the returned structure, use lyd_free().
507 */
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100508struct lyd_node *lyd_parse_xml(struct ly_ctx *ctx, struct lyxml_elem **root, int options,...);
Michal Vasko36ef6932015-12-01 14:30:17 +0100509
510/**
Michal Vasko662610a2015-12-07 11:25:45 +0100511 * @brief Read data from the given file descriptor.
Radek Krejci704a7a92015-08-07 12:50:15 +0200512 *
Michal Vasko662610a2015-12-07 11:25:45 +0100513 * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc.
Radek Krejci704a7a92015-08-07 12:50:15 +0200514 *
Radek Krejci86538212015-12-17 15:59:01 +0100515 * In case of LY_XML format, the file content is parsed completely. It means that when it contains
516 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
517 * returned data node is a root of the first tree with other trees connected via the next pointer.
518 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
519 *
Radek Krejci704a7a92015-08-07 12:50:15 +0200520 * @param[in] ctx Context to connect with the data tree being built here.
521 * @param[in] fd The standard file descriptor of the file containing the data tree in the specified format.
522 * @param[in] format Format of the input data to be parsed.
Radek Krejci25b9fd32015-08-10 15:06:07 +0200523 * @param[in] options Parser options, see @ref parseroptions.
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100524 * @param[in] ... Additional argument must be supplied when #LYD_OPT_RPCREPLY value is specified in \p options. The
525 * argument is supposed to provide pointer to the RPC schema node for the reply's request
526 * (const struct ::lys_node* rpc).
Radek Krejci704a7a92015-08-07 12:50:15 +0200527 * @return Pointer to the built data tree. To free the returned structure, use lyd_free().
528 */
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100529struct lyd_node *lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, ...);
Michal Vasko662610a2015-12-07 11:25:45 +0100530
531/**
532 * @brief Read data from the given file path.
533 *
Radek Krejci86538212015-12-17 15:59:01 +0100534 * In case of LY_XML format, the file content is parsed completely. It means that when it contains
535 * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The
536 * returned data node is a root of the first tree with other trees connected via the next pointer.
537 * This behavior can be changed by #LYD_OPT_NOSIBLINGS option.
538 *
Michal Vasko662610a2015-12-07 11:25:45 +0100539 * @param[in] ctx Context to connect with the data tree being built here.
540 * @param[in] path Path to the file containing the data tree in the specified format.
541 * @param[in] format Format of the input data to be parsed.
542 * @param[in] options Parser options, see @ref parseroptions.
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100543 * @param[in] ... Additional argument must be supplied when #LYD_OPT_RPCREPLY value is specified in \p options. The
544 * argument is supposed to provide pointer to the RPC schema node for the reply's request
545 * (const struct ::lys_node* rpc).
Michal Vasko662610a2015-12-07 11:25:45 +0100546 * @return Pointer to the built data tree. To free the returned structure, use lyd_free().
547 */
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100548struct lyd_node *lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options, ...);
Radek Krejci704a7a92015-08-07 12:50:15 +0200549
Radek Krejci26715a42015-07-29 14:10:45 +0200550/**@} parsers */
551
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200552/**
553 * @defgroup schematree Schema Tree
554 * @{
555 *
556 * Data structures and functions to manipulate and access schema tree.
557 *
558 * @}
559 */
560
561/**
562 * @defgroup datatree Data Tree
563 * @{
564 *
565 * Data structures and functions to manipulate and access instance data tree.
566 *
567 * @}
568 */
Radek Krejci9b4ca392015-04-10 08:31:27 +0200569
570/**
Radek Krejci26715a42015-07-29 14:10:45 +0200571 * @defgroup printers Printers
572 * @{
Radek Krejci9b4ca392015-04-10 08:31:27 +0200573 *
Radek Krejci26715a42015-07-29 14:10:45 +0200574 * Printers allows to serialize schema and data trees in a specific format.
Radek Krejci9b4ca392015-04-10 08:31:27 +0200575 *
Radek Krejci26715a42015-07-29 14:10:45 +0200576 * For schemas, the following formats are supported:
577 * - YANG
Radek Krejci9b4ca392015-04-10 08:31:27 +0200578 *
Radek Krejci26715a42015-07-29 14:10:45 +0200579 * Basic YANG schemas format described in [RFC 6020](http://tools.ietf.org/html/rfc6020).
580 * Currently, only YANG 1.0 is supported.
581 *
582 * - YIN
583 *
584 * Alternative XML-based format to YANG. The details can be found in
585 * [RFC 6020](http://tools.ietf.org/html/rfc6020#section-11).
586 *
587 * \todo YIN output is not yet implemented
588 *
589 * - Tree
590 *
591 * Simple tree structure of the module.
592 *
593 * - Info
594 *
595 * Detailed information about the specific node in the schema tree.
Michal Vasko6d6076e2015-08-13 15:53:09 +0200596 * The target can be more specific than the module itself:
Radek Krejci26715a42015-07-29 14:10:45 +0200597 *
Michal Vasko6d6076e2015-08-13 15:53:09 +0200598 * - absolute-schema-nodeid&nbsp;&nbsp;&nbsp;&nbsp;\a /modules/module-set-id in \a ietf-yang-library
599 * - <b>typedef/</b>typedef-name&nbsp;&nbsp;&nbsp;&nbsp;\a typedef/revision-identifier in \a ietf-yang-library
600 * - <b>feature/</b>feature-name&nbsp;&nbsp;&nbsp;&nbsp;\a feature/ssh in \a ietf-netconf-server
601 * - <b>grouping/</b>grouping-name/descendant-schema-nodeid&nbsp;&nbsp;&nbsp;&nbsp;\a grouping/module or \a grouping/module/module/submodules
602 * in \a ietf-yang-library
603 * - <b>type/</b>leaf-or-leaflist&nbsp;&nbsp;&nbsp;&nbsp;\a type/modules/module-set-id in \a ietf-yang-library
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200604 *
605 * For data instances, the following formats are supported:
606 * - \todo TBD
607 *
Radek Krejci9b4ca392015-04-10 08:31:27 +0200608 */
609
610/**
Radek Krejci912da452015-07-29 14:10:06 +0200611 * @brief Print schema tree in the specified format.
612 *
Radek Krejci87f52372015-10-09 09:34:46 +0200613 * To write data into a file descriptor, use lys_print_fd().
614 *
Radek Krejci912da452015-07-29 14:10:06 +0200615 * @param[in] module Schema tree to print.
616 * @param[in] f File stream where to print the schema.
617 * @param[in] format Schema output format.
Radek Krejcia9167ef2015-08-03 11:01:11 +0200618 * @param[in] target_node Optional parameter for ::LYS_OUT_INFO format. It specifies which particular
Radek Krejci912da452015-07-29 14:10:06 +0200619 * node in the module will be printed.
620 * @return 0 on success, 1 on failure (#ly_errno is set).
621 */
Michal Vasko662610a2015-12-07 11:25:45 +0100622int lys_print_file(FILE *f, const struct lys_module *module, LYS_OUTFORMAT format, const char *target_node);
Radek Krejci912da452015-07-29 14:10:06 +0200623
624/**
Radek Krejci87f52372015-10-09 09:34:46 +0200625 * @brief Print schema tree in the specified format.
626 *
627 * Same as lys_print(), but output is written into the specified file descriptor.
628 *
629 * @param[in] module Schema tree to print.
630 * @param[in] fd File descriptor where to print the data.
631 * @param[in] format Schema output format.
632 * @param[in] target_node Optional parameter for ::LYS_OUT_INFO format. It specifies which particular
633 * node in the module will be printed.
634 * @return 0 on success, 1 on failure (#ly_errno is set).
635 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100636int lys_print_fd(int fd, const struct lys_module *module, LYS_OUTFORMAT format, const char *target_node);
Radek Krejci87f52372015-10-09 09:34:46 +0200637
638/**
Radek Krejci6140e4e2015-10-09 15:50:55 +0200639 * @brief Print schema tree in the specified format.
640 *
Radek Krejci2fa0fc12015-10-14 18:14:29 +0200641 * Same as lys_print(), but it allocates memory and store the data into it.
642 * It is up to caller to free the returned string by free().
643 *
644 * @param[out] strp Pointer to store the resulting dump.
645 * @param[in] module Schema tree to print.
646 * @param[in] format Schema output format.
647 * @param[in] target_node Optional parameter for ::LYS_OUT_INFO format. It specifies which particular
648 * node in the module will be printed.
649 * @return 0 on success, 1 on failure (#ly_errno is set).
650 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100651int lys_print_mem(char **strp, const struct lys_module *module, LYS_OUTFORMAT format, const char *target_node);
Radek Krejci2fa0fc12015-10-14 18:14:29 +0200652
653/**
654 * @brief Print schema tree in the specified format.
655 *
Radek Krejci6140e4e2015-10-09 15:50:55 +0200656 * Same as lys_print(), but output is written via provided callback.
657 *
658 * @param[in] module Schema tree to print.
659 * @param[in] writeclb Callback function to write the data (see write(1)).
Radek Krejci50929eb2015-10-09 18:14:15 +0200660 * @param[in] arg Optional caller-specific argument to be passed to the \p writeclb callback.
Radek Krejci6140e4e2015-10-09 15:50:55 +0200661 * @param[in] format Schema output format.
662 * @param[in] target_node Optional parameter for ::LYS_OUT_INFO format. It specifies which particular
663 * node in the module will be printed.
664 * @return 0 on success, 1 on failure (#ly_errno is set).
665 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100666int lys_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg,
667 const struct lys_module *module, LYS_OUTFORMAT format, const char *target_node);
Radek Krejci6140e4e2015-10-09 15:50:55 +0200668
669/**
Radek Krejci5044be32016-01-18 17:05:51 +0100670 * @defgroup printerflags Printer flags
671 * @ingroup printers
672 *
673 * Validity flags for data nodes.
674 *
675 * @{
676 */
677#define LYP_WITHSIBLINGS 0x01 /**< Flag for printing also the (following) sibling nodes of the data node. */
678
679/**
680 * @}
681 */
682
683/**
Radek Krejci912da452015-07-29 14:10:06 +0200684 * @brief Print data tree in the specified format.
685 *
Radek Krejci87f52372015-10-09 09:34:46 +0200686 * To write data into a file descriptor, use lyd_print_fd().
687 *
Radek Krejci912da452015-07-29 14:10:06 +0200688 * @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
689 * node of the data tree to print the specific subtree.
690 * @param[in] f File stream where to print the data.
691 * @param[in] format Data output format.
Radek Krejci5044be32016-01-18 17:05:51 +0100692 * @param[in] options [printer flags](@ref printerflags).
Radek Krejci912da452015-07-29 14:10:06 +0200693 * @return 0 on success, 1 on failure (#ly_errno is set).
694 */
Radek Krejci5044be32016-01-18 17:05:51 +0100695int lyd_print_file(FILE *f, const struct lyd_node *root, LYD_FORMAT format, int options);
Radek Krejci912da452015-07-29 14:10:06 +0200696
Radek Krejci87f52372015-10-09 09:34:46 +0200697/**
698 * @brief Print data tree in the specified format.
699 *
700 * Same as lyd_print(), but output is written into the specified file descriptor.
701 *
702 * @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
703 * node of the data tree to print the specific subtree.
704 * @param[in] fd File descriptor where to print the data.
705 * @param[in] format Data output format.
Radek Krejci5044be32016-01-18 17:05:51 +0100706 * @param[in] options [printer flags](@ref printerflags).
Radek Krejci87f52372015-10-09 09:34:46 +0200707 * @return 0 on success, 1 on failure (#ly_errno is set).
708 */
Radek Krejci5044be32016-01-18 17:05:51 +0100709int lyd_print_fd(int fd, const struct lyd_node *root, LYD_FORMAT format, int options);
Radek Krejci87f52372015-10-09 09:34:46 +0200710
Radek Krejci2fa0fc12015-10-14 18:14:29 +0200711
712 /**
713 * @brief Print data tree in the specified format.
714 *
715 * Same as lyd_print(), but it allocates memory and store the data into it.
716 * It is up to caller to free the returned string by free().
717 *
718 * @param[out] strp Pointer to store the resulting dump.
719 * @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
720 * node of the data tree to print the specific subtree.
721 * @param[in] format Data output format.
Radek Krejci5044be32016-01-18 17:05:51 +0100722 * @param[in] options [printer flags](@ref printerflags).
Radek Krejci2fa0fc12015-10-14 18:14:29 +0200723 * @return 0 on success, 1 on failure (#ly_errno is set).
724 */
Radek Krejci5044be32016-01-18 17:05:51 +0100725int lyd_print_mem(char **strp, const struct lyd_node *root, LYD_FORMAT format, int options);
Radek Krejci2fa0fc12015-10-14 18:14:29 +0200726
Radek Krejci6140e4e2015-10-09 15:50:55 +0200727/**
728 * @brief Print data tree in the specified format.
729 *
730 * Same as lyd_print(), but output is written via provided callback.
731 *
732 * @param[in] root Root node of the data tree to print. It can be actually any (not only real root)
733 * node of the data tree to print the specific subtree.
734 * @param[in] writeclb Callback function to write the data (see write(1)).
Radek Krejci50929eb2015-10-09 18:14:15 +0200735 * @param[in] arg Optional caller-specific argument to be passed to the \p writeclb callback.
Radek Krejci6140e4e2015-10-09 15:50:55 +0200736 * @param[in] format Data output format.
Radek Krejci5044be32016-01-18 17:05:51 +0100737 * @param[in] options [printer flags](@ref printerflags).
Radek Krejci6140e4e2015-10-09 15:50:55 +0200738 * @return 0 on success, 1 on failure (#ly_errno is set).
739 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100740int lyd_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg,
Radek Krejci5044be32016-01-18 17:05:51 +0100741 const struct lyd_node *root, LYD_FORMAT format, int options);
Radek Krejci6140e4e2015-10-09 15:50:55 +0200742
Radek Krejci912da452015-07-29 14:10:06 +0200743/**@} printers */
744
745/**
Radek Krejci3045cf32015-05-28 10:58:52 +0200746 * @defgroup logger Logger
747 * @{
748 *
749 * Publicly visible functions and values of the libyang logger. For more
750 * information, see \ref howtologger.
751 */
752
753/**
754 * @typedef LY_LOG_LEVEL
755 * @brief Verbosity levels of the libyang logger.
756 */
757typedef enum {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200758 LY_LLERR, /**< Print only error messages. */
759 LY_LLWRN, /**< Print error and warning messages. */
760 LY_LLVRB, /**< Besides errors and warnings, print some other verbose messages. */
761 LY_LLDBG /**< Print all messages including some development debug messages. */
Radek Krejci3045cf32015-05-28 10:58:52 +0200762} LY_LOG_LEVEL;
763
764/**
765 * @brief Set logger verbosity level.
766 * @param[in] level Verbosity level.
767 */
768void ly_verb(LY_LOG_LEVEL level);
769
770/**
Michal Vaskof1d62cf2015-12-07 13:17:11 +0100771 * @brief Set logger callback.
772 * @param[in] clb Logging callback.
773 */
774void ly_set_log_clb(void (*clb)(LY_LOG_LEVEL, const char *));
775
776/**
777 * @brief Get logger callback.
778 * @return Logger callback (can be NULL).
779 */
780void (*ly_get_log_clb(void))(LY_LOG_LEVEL, const char *);
781
782/**
Radek Krejci3045cf32015-05-28 10:58:52 +0200783 * @typedef LY_ERR
Radek Krejci26715a42015-07-29 14:10:45 +0200784 * @brief libyang's error codes available via ly_errno extern variable.
Radek Krejci9b4ca392015-04-10 08:31:27 +0200785 * @ingroup logger
786 */
787typedef enum {
Radek Krejciae6817a2015-08-10 14:02:06 +0200788 LY_SUCCESS, /**< no error, not set by functions, included just to complete #LY_ERR enumeration */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200789 LY_EMEM, /**< Memory allocation failure */
790 LY_ESYS, /**< System call failure */
791 LY_EINVAL, /**< Invalid value */
792 LY_EINT, /**< Internal error */
793 LY_EVALID /**< Validation failure */
Radek Krejci3045cf32015-05-28 10:58:52 +0200794} LY_ERR;
Radek Krejci26715a42015-07-29 14:10:45 +0200795/**
796 * @brief libyang specific errno.
797 */
Radek Krejci3045cf32015-05-28 10:58:52 +0200798extern LY_ERR ly_errno;
Radek Krejci9b4ca392015-04-10 08:31:27 +0200799
Radek Krejci3045cf32015-05-28 10:58:52 +0200800/**@} logger */
Radek Krejci9b4ca392015-04-10 08:31:27 +0200801
Radek Krejci39d8d0d2015-08-17 13:42:45 +0200802#ifdef __cplusplus
803}
804#endif
805
Radek Krejci9b4ca392015-04-10 08:31:27 +0200806#endif /* LY_LIBYANG_H_ */