blob: 85ec9c528e096c8181c0a0f02a754f8e4be7b3f7 [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
Radek Krejci3045cf32015-05-28 10:58:52 +020027#include "tree.h"
Radek Krejcida04f4a2015-05-21 12:54:09 +020028
Michal Vasko85217a32015-07-07 11:40:08 +020029int ly_model_print(FILE * f, struct ly_module *module, LY_MOUTFORMAT format, const char *target_node);
Radek Krejcida04f4a2015-05-21 12:54:09 +020030
Radek Krejci94ca54b2015-07-08 15:48:47 +020031int ly_data_print(FILE * f, struct lyd_node *root, LY_DFORMAT format);
32
Radek Krejcida04f4a2015-05-21 12:54:09 +020033/**
Radek Krejci3045cf32015-05-28 10:58:52 +020034 * @defgroup libyang libyang
35 * @{
36 *
37 * General libyang functions and structures.
38 */
39
40/**
Radek Krejcida04f4a2015-05-21 12:54:09 +020041 * @brief libyang context handler.
42 */
43struct 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 Krejci3045cf32015-05-28 10:58:52 +020058 * @return Pointer to the created libyang context, NULL in case of error.
Radek Krejcida04f4a2015-05-21 12:54:09 +020059 */
60struct ly_ctx *ly_ctx_new(const char *search_dir);
61
62/**
Michal Vasko60ba9a62015-07-03 14:42:31 +020063 * @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 */
68void ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir);
69
70/**
Radek Krejcida04f4a2015-05-21 12:54:09 +020071 * @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 Krejciefaeba32015-05-27 14:30:57 +020079 * specified, the newest revision is returned (TODO).
Radek Krejci0af13872015-05-30 11:50:52 +020080 * @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 Vaskod8aa32d2015-07-24 11:50:01 +020082 * @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 Krejci0af13872015-05-30 11:50:52 +020085 * @return Pointer to the data model structure.
Radek Krejcida04f4a2015-05-21 12:54:09 +020086 */
Michal Vaskod8aa32d2015-07-24 11:50:01 +020087struct ly_module *ly_ctx_get_module(struct ly_ctx *ctx, const char *name, const char *revision, int read, int implement);
Radek Krejcida04f4a2015-05-21 12:54:09 +020088
89/**
Michal Vasko7bf06882015-07-03 15:33:56 +020090 * @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 Vaskod8aa32d2015-07-24 11:50:01 +020098 * @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 Vasko7bf06882015-07-03 15:33:56 +0200100 * @return Pointer to the data model structure.
101 */
Michal Vaskod8aa32d2015-07-24 11:50:01 +0200102struct ly_submodule *ly_ctx_get_submodule(struct ly_module *module, const char *name, const char *revision, int read, int implement);
Michal Vasko7bf06882015-07-03 15:33:56 +0200103
104/**
Michal Vasko3ec07dc2015-06-30 15:51:30 +0200105 * @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 */
111char **ly_ctx_get_module_names(struct ly_ctx *ctx);
112
113/**
Michal Vaskofa8c8282015-07-03 15:14:59 +0200114 * @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 */
121char **ly_ctx_get_submodule_names(struct ly_ctx *ctx, const char *name);
122
123/**
Radek Krejci3045cf32015-05-28 10:58:52 +0200124 * @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 Krejcida04f4a2015-05-21 12:54:09 +0200134 */
Radek Krejci3045cf32015-05-28 10:58:52 +0200135void ly_ctx_destroy(struct ly_ctx *ctx);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200136
137/**
138 * @brief Load a data model into the specified context.
139 *
Radek Krejcib20c62d2015-07-07 17:07:14 +0200140 * LY_IN_YANG (YANG) format is not yet supported.
141 *
Radek Krejcida04f4a2015-05-21 12:54:09 +0200142 * @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 Vaskod8aa32d2015-07-24 11:50:01 +0200144 * format.
Radek Krejcida04f4a2015-05-21 12:54:09 +0200145 * @param[in] format Format of the input data (YANG or YIN).
Michal Vaskod8aa32d2015-07-24 11:50:01 +0200146 * @param[in] implement Flag set if the module is implemented, not just
147 * imported from another module.
Radek Krejcida04f4a2015-05-21 12:54:09 +0200148 * @return Pointer to the data model structure or NULL on error.
149 */
Michal Vaskod8aa32d2015-07-24 11:50:01 +0200150struct ly_module *ly_module_read(struct ly_ctx *ctx, const char *data, LY_MINFORMAT format, int implement);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200151
Michal Vaskod8aa32d2015-07-24 11:50:01 +0200152struct ly_module *ly_module_read_fd(struct ly_ctx *ctx, int fd, LY_MINFORMAT format, int implement);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200153
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 Krejciefaeba32015-05-27 14:30:57 +0200162void ly_module_free(struct ly_module *module);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200163
Radek Krejcib20c62d2015-07-07 17:07:14 +0200164/**
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 */
172struct lyd_node *ly_data_read(struct ly_ctx *ctx, const char *data, LY_DFORMAT format);
173
Radek Krejci3045cf32015-05-28 10:58:52 +0200174/**@} libyang */
Radek Krejcida04f4a2015-05-21 12:54:09 +0200175
Radek Krejci9b4ca392015-04-10 08:31:27 +0200176/**
177 * @page howto How To ...
178 *
179 * - @subpage howtologger
180 */
181
182/**
Radek Krejci9b4ca392015-04-10 08:31:27 +0200183 *
184 * @page howtologger Logger
185 *
Radek Krejci3045cf32015-05-28 10:58:52 +0200186 * 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 Krejci9b4ca392015-04-10 08:31:27 +0200189 *
Radek Krejci3045cf32015-05-28 10:58:52 +0200190 * 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 Krejci9b4ca392015-04-10 08:31:27 +0200193 */
194
195/**
Radek Krejci3045cf32015-05-28 10:58:52 +0200196 * @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 */
207typedef enum {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200208 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 Krejci3045cf32015-05-28 10:58:52 +0200212} LY_LOG_LEVEL;
213
214/**
215 * @brief Set logger verbosity level.
216 * @param[in] level Verbosity level.
217 */
218void 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 Krejci9b4ca392015-04-10 08:31:27 +0200223 * @ingroup logger
224 */
225typedef enum {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200226 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 Krejci3045cf32015-05-28 10:58:52 +0200231} LY_ERR;
232extern LY_ERR ly_errno;
Radek Krejci9b4ca392015-04-10 08:31:27 +0200233
Radek Krejci3045cf32015-05-28 10:58:52 +0200234/**@} logger */
Radek Krejci9b4ca392015-04-10 08:31:27 +0200235
236#endif /* LY_LIBYANG_H_ */