blob: cbbef4162d8a4fd34b0322ec88b2c40ce03df296 [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
29int ly_model_print(FILE *f, struct ly_module *module, LY_MFORMAT format);
30
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/**
Radek Krejcida04f4a2015-05-21 12:54:09 +020061 * @brief Get pointer to the data model structure of the specified name.
62 *
63 * If the module is not yet loaded in the context, libyang tries to find it in
64 * the search directory specified when the context was created by ly_ctx_new().
65 *
66 * @param[in] ctx Context to work in.
67 * @param[in] name Name of the YANG module to get.
68 * @param[in] revision Optional revision date of the YANG module to get. If not
Radek Krejciefaeba32015-05-27 14:30:57 +020069 * specified, the newest revision is returned (TODO).
Radek Krejci0af13872015-05-30 11:50:52 +020070 * @param[in] read Flag for reading the module from a file. If set to 0, libyang
71 * searches for the module only in the modules already loaded to the context.
72 * @return Pointer to the data model structure.
Radek Krejcida04f4a2015-05-21 12:54:09 +020073 */
Radek Krejciefaeba32015-05-27 14:30:57 +020074struct ly_module *ly_ctx_get_module(struct ly_ctx *ctx, const char *name,
Radek Krejci0af13872015-05-30 11:50:52 +020075 const char *revision, int read);
Radek Krejcida04f4a2015-05-21 12:54:09 +020076
77/**
Radek Krejci3045cf32015-05-28 10:58:52 +020078 * @brief Free all internal structures of the specified context.
79 *
80 * The function should be used before terminating the application to destroy
81 * and free all structures internally used by libyang. If the caller uses
82 * multiple contexts, the function should be called for each used context.
83 *
84 * All instance data are supposed to be freed before destroying the context.
85 * Data models are destroyed automatically as part of ly_ctx_destroy() call.
86 *
87 * @param[in] ctx libyang context to destroy
Radek Krejcida04f4a2015-05-21 12:54:09 +020088 */
Radek Krejci3045cf32015-05-28 10:58:52 +020089void ly_ctx_destroy(struct ly_ctx *ctx);
Radek Krejcida04f4a2015-05-21 12:54:09 +020090
91/**
92 * @brief Load a data model into the specified context.
93 *
94 * @param[in] ctx libyang context where to process the data model.
95 * @param[in] data The string containing the dumped data model in the specified
96 * format
97 * @param[in] format Format of the input data (YANG or YIN).
98 * @return Pointer to the data model structure or NULL on error.
99 */
Radek Krejciefaeba32015-05-27 14:30:57 +0200100struct ly_module *ly_module_read(struct ly_ctx *ctx, const char *data,
101 LY_MFORMAT format);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200102
103
Radek Krejciefaeba32015-05-27 14:30:57 +0200104struct ly_module *ly_module_read_fd(struct ly_ctx *ctx, int fd,
105 LY_MFORMAT format);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200106
107/**
108 * @brief Free data model
109 *
110 * It is up to the caller that there is no instance data using the module
111 * being freed.
112 *
113 * @param[in] module Data model to free.
114 */
Radek Krejciefaeba32015-05-27 14:30:57 +0200115void ly_module_free(struct ly_module *module);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200116
Radek Krejci3045cf32015-05-28 10:58:52 +0200117/**@} libyang */
Radek Krejcida04f4a2015-05-21 12:54:09 +0200118
Radek Krejci9b4ca392015-04-10 08:31:27 +0200119/**
120 * @page howto How To ...
121 *
122 * - @subpage howtologger
123 */
124
125/**
Radek Krejci9b4ca392015-04-10 08:31:27 +0200126 *
127 * @page howtologger Logger
128 *
Radek Krejci3045cf32015-05-28 10:58:52 +0200129 * There are 4 verbosity levels defined as ::LY_LOG_LEVEL. The level can be
130 * changed by the ly_verb() function. By default, the verbosity level is
131 * set to #LY_LLERR value.
Radek Krejci9b4ca392015-04-10 08:31:27 +0200132 *
Radek Krejci3045cf32015-05-28 10:58:52 +0200133 * In case the logger has an error message (LY_LLERR) to print, also an error
134 * code is recorded in extern ly_errno variable. Possible values are of type
135 * ::LY_ERR.
Radek Krejci9b4ca392015-04-10 08:31:27 +0200136 */
137
138/**
Radek Krejci3045cf32015-05-28 10:58:52 +0200139 * @defgroup logger Logger
140 * @{
141 *
142 * Publicly visible functions and values of the libyang logger. For more
143 * information, see \ref howtologger.
144 */
145
146/**
147 * @typedef LY_LOG_LEVEL
148 * @brief Verbosity levels of the libyang logger.
149 */
150typedef enum {
151 LY_LLERR, /**< Print only error messages. */
152 LY_LLWRN, /**< Print error and warning messages. */
153 LY_LLVRB, /**< Besides errors and warnings, print some other verbose messages. */
154 LY_LLDBG /**< Print all messages including some development debug messages. */
155} LY_LOG_LEVEL;
156
157/**
158 * @brief Set logger verbosity level.
159 * @param[in] level Verbosity level.
160 */
161void ly_verb(LY_LOG_LEVEL level);
162
163/**
164 * @typedef LY_ERR
165 * @brief libyang's error codes available via ly_errno extern variable
Radek Krejci9b4ca392015-04-10 08:31:27 +0200166 * @ingroup logger
167 */
168typedef enum {
Radek Krejci812b10a2015-05-28 16:48:25 +0200169 LY_EMEM, /**< Memory allocation failure */
170 LY_ESYS, /**< System call failure */
171 LY_EINVAL, /**< Invalid value */
172 LY_EINT, /**< Internal error */
173 LY_EVALID /**< Validation failure */
Radek Krejci3045cf32015-05-28 10:58:52 +0200174} LY_ERR;
175extern LY_ERR ly_errno;
Radek Krejci9b4ca392015-04-10 08:31:27 +0200176
Radek Krejci3045cf32015-05-28 10:58:52 +0200177/**@} logger */
Radek Krejci9b4ca392015-04-10 08:31:27 +0200178
179#endif /* LY_LIBYANG_H_ */