blob: 8197fb6c83b94ea537af772b08396a0b1673ca7b [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
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020029int ly_model_print(FILE * f, struct ly_module *module, LY_MOUTFORMAT format);
Radek Krejcida04f4a2015-05-21 12:54:09 +020030
31/**
Radek Krejci3045cf32015-05-28 10:58:52 +020032 * @defgroup libyang libyang
33 * @{
34 *
35 * General libyang functions and structures.
36 */
37
38/**
Radek Krejcida04f4a2015-05-21 12:54:09 +020039 * @brief libyang context handler.
40 */
41struct ly_ctx;
42
43/**
44 * @brief Create libyang context
45 *
46 * Context is used to hold all information about data models. Usually, the
47 * application is supposed to work with a single context in which libyang is
48 * holding all data models and other internal information according to which
49 * the instance data will be processed and validated. Therefore, also each
50 * instance data are connected
51 *
52 * @param[in] search_dir Directory where libyang will search for the imported
53 * or included modules and submodules. If no such directory is available, NULL
54 * is accepted.
55 *
Radek Krejci3045cf32015-05-28 10:58:52 +020056 * @return Pointer to the created libyang context, NULL in case of error.
Radek Krejcida04f4a2015-05-21 12:54:09 +020057 */
58struct ly_ctx *ly_ctx_new(const char *search_dir);
59
60/**
Michal Vasko60ba9a62015-07-03 14:42:31 +020061 * @brief Change the search path in libyang context
62 *
63 * @param[in] ctx Context to be modified.
64 * @param[in] search_dir New search path to replace the current one in ctx.
65 */
66void ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir);
67
68/**
Radek Krejcida04f4a2015-05-21 12:54:09 +020069 * @brief Get pointer to the data model structure of the specified name.
70 *
71 * If the module is not yet loaded in the context, libyang tries to find it in
72 * the search directory specified when the context was created by ly_ctx_new().
73 *
74 * @param[in] ctx Context to work in.
75 * @param[in] name Name of the YANG module to get.
76 * @param[in] revision Optional revision date of the YANG module to get. If not
Radek Krejciefaeba32015-05-27 14:30:57 +020077 * specified, the newest revision is returned (TODO).
Radek Krejci0af13872015-05-30 11:50:52 +020078 * @param[in] read Flag for reading the module from a file. If set to 0, libyang
79 * searches for the module only in the modules already loaded to the context.
80 * @return Pointer to the data model structure.
Radek Krejcida04f4a2015-05-21 12:54:09 +020081 */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020082struct ly_module *ly_ctx_get_module(struct ly_ctx *ctx, const char *name, const char *revision, int read);
Radek Krejcida04f4a2015-05-21 12:54:09 +020083
84/**
Michal Vasko7bf06882015-07-03 15:33:56 +020085 * @brief Get submodule from the context's search dir.
86 *
87 * @param[in] module Parent (belongs-to) module.
88 * @param[in] name Name of the YANG submodule to get.
89 * @param[in] revision Optional revision date of the YANG submodule to get. If
90 * not specified, the newest revision is returned (TODO).
91 * @param[in] read Flag for reading the submodule from a file. If set to 0, libyang
92 * searches for the submodule only in the submodules already loaded to the context.
93 * @return Pointer to the data model structure.
94 */
95struct ly_submodule *ly_ctx_get_submodule(struct ly_module *module, const char *name, const char *revision, int read);
96
97/**
Michal Vasko3ec07dc2015-06-30 15:51:30 +020098 * @brief Get the names of the loaded modules.
99 *
100 * @param[in] ctx Context with the modules.
101 * @return NULL-terminated array of the module names,
102 * NULL on error. The result must be freed by the caller.
103 */
104char **ly_ctx_get_module_names(struct ly_ctx *ctx);
105
106/**
Michal Vaskofa8c8282015-07-03 15:14:59 +0200107 * @brief Get the names of the loaded submodules of a loaded module.
108 *
109 * @param[in] ctx Context with the modules.
110 * @param[in] name Name of the parent module.
111 * @return NULL-terminated array of submodule names of the parent module,
112 * NULL on error. The result must be freed by the caller.
113 */
114char **ly_ctx_get_submodule_names(struct ly_ctx *ctx, const char *name);
115
116/**
Radek Krejci3045cf32015-05-28 10:58:52 +0200117 * @brief Free all internal structures of the specified context.
118 *
119 * The function should be used before terminating the application to destroy
120 * and free all structures internally used by libyang. If the caller uses
121 * multiple contexts, the function should be called for each used context.
122 *
123 * All instance data are supposed to be freed before destroying the context.
124 * Data models are destroyed automatically as part of ly_ctx_destroy() call.
125 *
126 * @param[in] ctx libyang context to destroy
Radek Krejcida04f4a2015-05-21 12:54:09 +0200127 */
Radek Krejci3045cf32015-05-28 10:58:52 +0200128void ly_ctx_destroy(struct ly_ctx *ctx);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200129
130/**
131 * @brief Load a data model into the specified context.
132 *
133 * @param[in] ctx libyang context where to process the data model.
134 * @param[in] data The string containing the dumped data model in the specified
135 * format
136 * @param[in] format Format of the input data (YANG or YIN).
137 * @return Pointer to the data model structure or NULL on error.
138 */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200139struct ly_module *ly_module_read(struct ly_ctx *ctx, const char *data, LY_MINFORMAT format);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200140
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200141struct ly_module *ly_module_read_fd(struct ly_ctx *ctx, int fd, LY_MINFORMAT format);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200142
143/**
144 * @brief Free data model
145 *
146 * It is up to the caller that there is no instance data using the module
147 * being freed.
148 *
149 * @param[in] module Data model to free.
150 */
Radek Krejciefaeba32015-05-27 14:30:57 +0200151void ly_module_free(struct ly_module *module);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200152
Radek Krejci3045cf32015-05-28 10:58:52 +0200153/**@} libyang */
Radek Krejcida04f4a2015-05-21 12:54:09 +0200154
Radek Krejci9b4ca392015-04-10 08:31:27 +0200155/**
156 * @page howto How To ...
157 *
158 * - @subpage howtologger
159 */
160
161/**
Radek Krejci9b4ca392015-04-10 08:31:27 +0200162 *
163 * @page howtologger Logger
164 *
Radek Krejci3045cf32015-05-28 10:58:52 +0200165 * There are 4 verbosity levels defined as ::LY_LOG_LEVEL. The level can be
166 * changed by the ly_verb() function. By default, the verbosity level is
167 * set to #LY_LLERR value.
Radek Krejci9b4ca392015-04-10 08:31:27 +0200168 *
Radek Krejci3045cf32015-05-28 10:58:52 +0200169 * In case the logger has an error message (LY_LLERR) to print, also an error
170 * code is recorded in extern ly_errno variable. Possible values are of type
171 * ::LY_ERR.
Radek Krejci9b4ca392015-04-10 08:31:27 +0200172 */
173
174/**
Radek Krejci3045cf32015-05-28 10:58:52 +0200175 * @defgroup logger Logger
176 * @{
177 *
178 * Publicly visible functions and values of the libyang logger. For more
179 * information, see \ref howtologger.
180 */
181
182/**
183 * @typedef LY_LOG_LEVEL
184 * @brief Verbosity levels of the libyang logger.
185 */
186typedef enum {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200187 LY_LLERR, /**< Print only error messages. */
188 LY_LLWRN, /**< Print error and warning messages. */
189 LY_LLVRB, /**< Besides errors and warnings, print some other verbose messages. */
190 LY_LLDBG /**< Print all messages including some development debug messages. */
Radek Krejci3045cf32015-05-28 10:58:52 +0200191} LY_LOG_LEVEL;
192
193/**
194 * @brief Set logger verbosity level.
195 * @param[in] level Verbosity level.
196 */
197void ly_verb(LY_LOG_LEVEL level);
198
199/**
200 * @typedef LY_ERR
201 * @brief libyang's error codes available via ly_errno extern variable
Radek Krejci9b4ca392015-04-10 08:31:27 +0200202 * @ingroup logger
203 */
204typedef enum {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200205 LY_EMEM, /**< Memory allocation failure */
206 LY_ESYS, /**< System call failure */
207 LY_EINVAL, /**< Invalid value */
208 LY_EINT, /**< Internal error */
209 LY_EVALID /**< Validation failure */
Radek Krejci3045cf32015-05-28 10:58:52 +0200210} LY_ERR;
211extern LY_ERR ly_errno;
Radek Krejci9b4ca392015-04-10 08:31:27 +0200212
Radek Krejci3045cf32015-05-28 10:58:52 +0200213/**@} logger */
Radek Krejci9b4ca392015-04-10 08:31:27 +0200214
215#endif /* LY_LIBYANG_H_ */