blob: fcabf180231df85f9d4739db838a662a15e715fe [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 *
Michal Vaskod025ee32018-06-28 10:04:19 +02006 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
Radek Krejci9b4ca392015-04-10 08:31:27 +02007 *
Radek Krejci54f6fb32016-02-24 12:56:39 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vasko8de098c2016-02-26 10:00:25 +010011 *
Radek Krejci54f6fb32016-02-24 12:56:39 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci9b4ca392015-04-10 08:31:27 +020013 */
14
15#ifndef LY_LIBYANG_H_
16#define LY_LIBYANG_H_
17
Radek Krejcida04f4a2015-05-21 12:54:09 +020018#include <stdio.h>
19
Michal Vaskod025ee32018-06-28 10:04:19 +020020/**
21 * @brief Whether to enable schema and data cache.
22 */
23#cmakedefine LY_ENABLED_CACHE
Michal Vaskofcd974b2017-08-22 10:17:49 +020024
Michal Vaskod025ee32018-06-28 10:04:19 +020025/**
26 * @brief Whether to allow reusing latest revisions of schemas.
27 */
28#cmakedefine LY_ENABLED_LATEST_REVISIONS
29
30/**
31 * @brief Whether to add priv pointer to struct lyd_node.
32 */
33#cmakedefine LY_ENABLED_LYD_PRIV
Michal Vasko8f3160e2017-09-27 11:25:26 +020034
Michal Vasko70bf8e52018-03-26 11:32:33 +020035/**
36 * @brief Compiler flag for packed data types.
37 */
38#define _PACKED @COMPILER_PACKED_ATTR@
39
Michal Vasko2d162e12015-09-24 14:33:29 +020040#include "tree_schema.h"
41#include "tree_data.h"
Radek Krejcic6704c82015-10-06 11:12:45 +020042#include "xml.h"
Radek Krejci41912fe2015-10-22 10:22:12 +020043#include "dict.h"
Radek Krejcida04f4a2015-05-21 12:54:09 +020044
Radek Krejci39d8d0d2015-08-17 13:42:45 +020045#ifdef __cplusplus
46extern "C" {
47#endif
48
Radek Krejci60cdf282016-10-10 16:22:22 +020049#define LY_VERSION_MAJOR @LIBYANG_MAJOR_VERSION@ /**< libyang major version number */
50#define LY_VERSION_MINOR @LIBYANG_MINOR_VERSION@ /**< libyang minor version number */
51#define LY_VERSION_MICRO @LIBYANG_MICRO_VERSION@ /**< libyang micro version number */
52
Radek Krejci26715a42015-07-29 14:10:45 +020053/**
Radek Krejcidef50022016-02-01 16:38:32 +010054 * @mainpage About
55 *
56 * libyang is a library implementing processing of the YANG schemas and data modeled by the YANG language. The
57 * library is implemented in C for GNU/Linux and provides C API.
58 *
59 * @section about-features Main Features
60 *
Radek Krejci89db0592017-02-16 15:07:13 +010061 * - [Parsing (and validating) schemas](@ref howtoschemasparsers) in YANG format.
62 * - [Parsing (and validating) schemas](@ref howtoschemasparsers) in YIN format.
63 * - [Parsing, validating and printing instance data](@ref howtodata) in XML format.
64 * - [Parsing, validating and printing instance data](@ref howtodata) in JSON format
65 * ([RFC 7951](https://tools.ietf.org/html/rfc7951)).
66 * - [Manipulation with the instance data](@ref howtodatamanipulators).
Radek Krejci89db0592017-02-16 15:07:13 +010067 * - Support for [default values in the instance data](@ref howtodatawd) ([RFC 6243](https://tools.ietf.org/html/rfc6243)).
Michal Vaskoc6cd3f02018-03-02 14:07:42 +010068 * - Support for [YANG extensions and user types](@ref howtoschemaplugins).
Radek Krejci792f52f2017-02-28 16:36:31 +010069 * - Support for [YANG Metadata](@ref howtoschemametadata) ([RFC 7952](https://tools.ietf.org/html/rfc6243)).
Radek Krejcidef50022016-02-01 16:38:32 +010070 *
Radek Krejcidaa080f2016-10-05 08:38:09 +020071 * The current implementation covers YANG 1.0 ([RFC 6020](https://tools.ietf.org/html/rfc6020)) as well as
72 * YANG 1.1 ([RFC 7950](https://tools.ietf.org/html/rfc7950)).
Radek Krejci8b13fc02016-04-18 13:08:04 +020073 *
Radek Krejcidef50022016-02-01 16:38:32 +010074 * @subsection about-features-others Extra (side-effect) Features
75 *
76 * - XML parser.
77 * - Optimized string storage (dictionary).
78 *
79 * @section about-license License
80 *
Radek Krejci792f52f2017-02-28 16:36:31 +010081 * Copyright (c) 2015-2017 CESNET, z.s.p.o.
Radek Krejcidef50022016-02-01 16:38:32 +010082 *
83 * (The BSD 3-Clause License)
84 *
85 * Redistribution and use in source and binary forms, with or without
86 * modification, are permitted provided that the following conditions
87 * are met:
88 * 1. Redistributions of source code must retain the above copyright
89 * notice, this list of conditions and the following disclaimer.
90 * 2. Redistributions in binary form must reproduce the above copyright
91 * notice, this list of conditions and the following disclaimer in
92 * the documentation and/or other materials provided with the
93 * distribution.
94 * 3. Neither the name of the Company nor the names of its contributors
95 * may be used to endorse or promote products derived from this
96 * software without specific prior written permission.
97 */
98
99/**
Radek Krejci26715a42015-07-29 14:10:45 +0200100 * @page howto How To ...
101 *
102 * - @subpage howtocontext
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200103 * - @subpage howtoschemas
104 * - @subpage howtodata
Michal Vasko0f14ba62016-03-21 15:38:11 +0100105 * - @subpage howtoxpath
Radek Krejcidef50022016-02-01 16:38:32 +0100106 * - @subpage howtoxml
107 * - @subpage howtothreads
Radek Krejci26715a42015-07-29 14:10:45 +0200108 * - @subpage howtologger
109 */
Radek Krejcida04f4a2015-05-21 12:54:09 +0200110
Radek Krejci26715a42015-07-29 14:10:45 +0200111/** @page howtocontext Context
112 *
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200113 * The context concept allows callers to work in environments with different sets of YANG schemas.
Radek Krejci26715a42015-07-29 14:10:45 +0200114 *
115 * The first step in libyang is to create a new context using ly_ctx_new(). It returns a handler
116 * used in the following work.
117 *
118 * When creating a new context, search dir can be specified (NULL is accepted) to provide directory
119 * where libyang will automatically search for schemas being imported or included. The search path
Radek Krejci804b4ba2018-08-27 10:08:01 +0200120 * can be later changed via ly_ctx_set_searchdir() and ly_ctx_unset_searchdirs() functions. Before the search dirs,
121 * also the current working directory is (non-recursively) searched. For the case of the explicitly set search
122 * dirs, also all their subdirectories (and symlinks) are taken into account. Searching in the current working
123 * directory can be avoided with context's #LY_CTX_DISABLE_SEARCHDIR_CWD option (or via ly_ctx_set_disable_searchdir_cwd()).
124 * Searching in all the context's search dirs (without removing them) can be avoided with the context's
125 * #LY_CTX_DISABLE_SEARCHDIRS option (or via ly_ctx_set_disable_searchdirs()). This automatic searching can be preceded
126 * by a custom module searching callback (#ly_module_imp_clb) set via ly_ctx_set_module_imp_clb(). The algorithm of
127 * searching in search dirs is also available via API as lys_search_localfile() function.
Radek Krejci26715a42015-07-29 14:10:45 +0200128 *
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200129 * Schemas are added into the context using [parser functions](@ref howtoschemasparsers) - \b lys_parse_*().
Michal Vaskof53187d2017-01-13 13:23:14 +0100130 * In case of schemas, also ly_ctx_load_module() can be used - in that case the #ly_module_imp_clb or automatic
Radek Krejci1fbe8582016-09-15 09:40:11 +0200131 * search in search dir and in the current working directory is used.
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200132 *
133 * Similarly, data trees can be parsed by \b lyd_parse_*() functions. Note, that functions for schemas have \b lys_
Michal Vasko346b5f32017-03-07 10:42:40 +0100134 * prefix while functions for instance data have \b lyd_ prefix. It can happen during data parsing that a schema is
135 * required and __not found__ in the context or the schema is found, but is __only imported__, not implemented (so the
136 * data cannot actually be instantiated). In these cases, a callback is called, which should add this schema into
137 * the context or change its conformance to implemented. You can set the callback using ly_ctx_set_module_data_clb()
138 * (more in @ref howtodataparsers and @ref howtodatavalidation).
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200139 *
Radek Krejciee674072016-09-15 10:37:40 +0200140 * Context can hold multiple revisions of the same schema, but only one of them can be implemented. The schema is not
141 * implemented in case it is automatically loaded as import for another module and it is not referenced in such
142 * a module (and no other) as target of leafref, augment or deviation. All modules with deviation definition are always
143 * marked as implemented. The imported (not implemented) module can be set implemented by lys_set_implemented(). But
144 * the implemented module cannot be changed back to just imported module. The imported modules are used only as a
Michal Vasko8f3160e2017-09-27 11:25:26 +0200145 * source of definitions for types and groupings for uses statements. The data in such modules are ignored - caller
146 * is not allowed to create the data (including instantiating identities) defined in the model via data parsers,
147 * the default nodes are not added into any data tree and mandatory nodes are not checked in the data trees. This
148 * can be changed by ly_ctx_set_allimplemented() function, which causes that all the imported modules are automatically
149 * set to be implemented.
150 *
151 * When loading/importing a module without revision, the latest revision of the required module is supposed to load.
152 * For a context, the first time the latest revision of a module is requested, it is properly searched for and loaded.
153 * However, when this module is requested (without revision) the second time, the one found previously is returned.
154 * This has the advantage of not searching for the module repeatedly but the drawback that if a later revision
155 * of the module is later made available, this context will not use it. If you are aware of a case when this
156 * optimization could cause problems, you can disable it using a cmake(1) build option (variable).
Radek Krejci26715a42015-07-29 14:10:45 +0200157 *
Radek Krejci31fb8be2016-06-23 15:26:26 +0200158 * Context holds all modules and their submodules internally. To get
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200159 * a specific module or submodule, use ly_ctx_get_module() and ly_ctx_get_submodule(). There are some additional
Radek Krejci31fb8be2016-06-23 15:26:26 +0200160 * alternatives to these functions (with different parameters). If you need to do something with all the modules or
Michal Vasko462be9a2016-04-05 11:24:08 +0200161 * submodules in the context, it is advised to iterate over them using ly_ctx_get_module_iter(), it is
162 * the most efficient way. Alternatively, the ly_ctx_info() function can be used to get complex information
163 * about the schemas in the context in the form of data tree defined by
Radek Krejcibd9e8d22016-02-03 14:11:48 +0100164 * <a href="https://tools.ietf.org/html/draft-ietf-netconf-yang-library-04">ietf-yang-library</a> schema.
Radek Krejcic683acd2018-01-22 14:51:52 +0100165 * To get a specific node defined in a module in the context, ly_ctx_find_path() or ly_ctx_get_node() can be used.
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200166 *
Radek Krejci0ec51da2016-12-14 16:42:03 +0100167 * Modules held by a context can be removed by ly_ctx_remove_module(). Besides removing the module, it is possible
168 * just to disable it with lys_set_disabled(). In this case the module is hidden in the context (disabled modules can
169 * be iterated via ly_ctx_get_disabled_module_iter()) and not used during the common work with the context. The module
170 * is enabled explicitly by calling lys_set_enabled() or implicitly by the request to load the schema (directly or
171 * indirectly via import of another module) into the context.
172 *
173 * To clean the context from all the loaded modules (except the [internal modules](@ref howtoschemasparsers)), the
174 * ly_ctx_clean() function can be used. To remove the context, there is ly_ctx_destroy() function.
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200175 *
Radek Krejcidef50022016-02-01 16:38:32 +0100176 * - @subpage howtocontextdict
177 *
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200178 * \note API for this group of functions is available in the [context module](@ref context).
179 *
Radek Krejcidef50022016-02-01 16:38:32 +0100180 * Functions List
181 * --------------
182 * - ly_ctx_new()
183 * - ly_ctx_set_searchdir()
Radek Krejcida9f8392017-03-25 19:40:56 -0500184 * - ly_ctx_unset_searchdirs()
Radek Krejci14116592018-05-16 12:26:06 +0200185 * - ly_ctx_get_searchdirs()
Michal Vaskof53187d2017-01-13 13:23:14 +0100186 * - ly_ctx_set_module_imp_clb()
187 * - ly_ctx_get_module_imp_clb()
188 * - ly_ctx_set_module_data_clb()
189 * - ly_ctx_get_module_data_clb()
Radek Krejci819dd4b2017-03-07 15:35:48 +0100190 * - ly_ctx_set_allimplemented()
191 * - ly_ctx_unset_allimplemented()
Radek Krejci88b78422018-08-16 16:07:34 +0200192 * - ly_ctx_set_disable_searchdirs()
193 * - ly_ctx_unset_disable_searchdirs()
Radek Krejcicf3b7ca2018-08-17 09:55:06 +0200194 * - ly_ctx_set_disable_searchdir_cwd()
195 * - ly_ctx_unset_disable_searchdir_cwd()
Radek Krejcidef50022016-02-01 16:38:32 +0100196 * - ly_ctx_load_module()
197 * - ly_ctx_info()
Radek Krejci14116592018-05-16 12:26:06 +0200198 * - ly_ctx_get_module_set_id()
Michal Vaskod7957c02016-04-01 10:27:26 +0200199 * - ly_ctx_get_module_iter()
Radek Krejci0ec51da2016-12-14 16:42:03 +0100200 * - ly_ctx_get_disabled_module_iter()
Radek Krejcidef50022016-02-01 16:38:32 +0100201 * - ly_ctx_get_module()
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200202 * - ly_ctx_get_module_older()
Radek Krejcidef50022016-02-01 16:38:32 +0100203 * - ly_ctx_get_module_by_ns()
Radek Krejcidef50022016-02-01 16:38:32 +0100204 * - ly_ctx_get_submodule()
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200205 * - ly_ctx_get_submodule2()
Michal Vasko3edeaf72016-02-11 13:17:43 +0100206 * - ly_ctx_get_node()
Radek Krejcic683acd2018-01-22 14:51:52 +0100207 * - ly_ctx_find_path()
Radek Krejci0ec51da2016-12-14 16:42:03 +0100208 * - ly_ctx_remove_module()
209 * - ly_ctx_clean()
Radek Krejcidef50022016-02-01 16:38:32 +0100210 * - ly_ctx_destroy()
Radek Krejciee674072016-09-15 10:37:40 +0200211 * - lys_set_implemented()
Radek Krejci0ec51da2016-12-14 16:42:03 +0100212 * - lys_set_disabled()
213 * - lys_set_enabled()
Radek Krejci72cdfac2018-08-15 14:47:33 +0200214 * - lys_search_localfile()
Radek Krejcidef50022016-02-01 16:38:32 +0100215 */
216
217/**
218 * @page howtocontextdict Context Dictionary
219 *
220 * Context includes dictionary to store strings more effectively. The most of strings repeats quite often in schema
221 * as well as data trees. Therefore, instead of allocating those strings each time they appear, libyang stores them
222 * as records in the dictionary. The basic API to the context dictionary is public, so even a caller application can
223 * use the dictionary.
224 *
225 * To insert a string into the dictionary, caller can use lydict_insert() (adding a constant string) or
226 * lydict_insert_zc() (for dynamically allocated strings that won't be used by the caller after its insertion into
227 * the dictionary). Both functions return the pointer to the inserted string in the dictionary record.
228 *
229 * To remove (reference of the) string from the context dictionary, lydict_remove() is supposed to be used.
230 *
231 * \note Incorrect usage of the dictionary can break libyang functionality.
232 *
233 * \note API for this group of functions is described in the [XML Parser module](@ref dict).
234 *
235 * Functions List
236 * --------------
237 * - lydict_insert()
238 * - lydict_insert_zc()
239 * - lydict_remove()
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200240 */
241
242/**
243 * @page howtoschemas Schemas
244 *
Radek Krejcidef50022016-02-01 16:38:32 +0100245 *
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200246 * Schema is an internal libyang's representation of a YANG data model. Each schema is connected with
Radek Krejcidef50022016-02-01 16:38:32 +0100247 * its [context](@ref howtocontext) and loaded using [parser functions](@ref howtoschemasparsers). It means, that
248 * the schema cannot be created (nor changed) programmatically. In libyang, schemas are used only to
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200249 * access data model definitions.
250 *
Radek Krejcidef50022016-02-01 16:38:32 +0100251 * Schema tree nodes are able to hold private objects (via a pointer to a structure, function, variable, ...) used by
252 * a caller application. Such an object can be assigned to a specific node using lys_set_private() function.
253 * Note that the object is not freed by libyang when the context is being destroyed. So the caller is responsible
254 * for freeing the provided structure after the context is destroyed or the private pointer is set to NULL in
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200255 * appropriate schema nodes where the object was previously set. This can be automated via destructor function
256 * to free these private objects. The destructor is passed to the ly_ctx_destroy() function. On the other hand,
257 * freeing the object while the schema tree is still in use can lead to a segmentation fault.
Radek Krejcidef50022016-02-01 16:38:32 +0100258 *
259 * - @subpage howtoschemasparsers
260 * - @subpage howtoschemasfeatures
Michal Vaskoc6cd3f02018-03-02 14:07:42 +0100261 * - @subpage howtoschemaplugins
Radek Krejcidef50022016-02-01 16:38:32 +0100262 * - @subpage howtoschemasprinters
263 *
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200264 * \note There are many functions to access information from the schema trees. Details are available in
265 * the [Schema Tree module](@ref schematree).
266 *
Radek Krejciee674072016-09-15 10:37:40 +0200267 * For information about difference between implemented and imported modules, see the
268 * [context description](@ref howtocontext).
269 *
Radek Krejcidef50022016-02-01 16:38:32 +0100270 * Functions List (not assigned to above subsections)
271 * --------------------------------------------------
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200272 * - lys_getnext()
Radek Krejcidef50022016-02-01 16:38:32 +0100273 * - lys_parent()
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200274 * - lys_module()
275 * - lys_node_module()
Radek Krejcidef50022016-02-01 16:38:32 +0100276 * - lys_set_private()
Radek Krejciee674072016-09-15 10:37:40 +0200277 * - lys_set_implemented()
Radek Krejci0ec51da2016-12-14 16:42:03 +0100278 * - lys_set_disabled()
279 * - lys_set_enabled()
Radek Krejcidef50022016-02-01 16:38:32 +0100280 */
281
282/**
283 * @page howtoschemasparsers Parsing Schemas
284 *
285 * Schema parser allows to read schema from a specific format. libyang supports the following schema formats:
286 *
287 * - YANG
288 *
Radek Krejci792f52f2017-02-28 16:36:31 +0100289 * Basic YANG schemas format described in [RFC 6020](http://tools.ietf.org/html/rfc6020) and
290 * [RFC 7951](http://tools.ietf.org/html/rfc7951) (so both YANG 1.0 and YANG 1.1 versions are supported).
Radek Krejcidef50022016-02-01 16:38:32 +0100291 *
Radek Krejcidef50022016-02-01 16:38:32 +0100292 * - YIN
293 *
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200294 * Alternative XML-based format to YANG - YANG Independent Notation. The details can be found in
Radek Krejci792f52f2017-02-28 16:36:31 +0100295 * [RFC 6020](http://tools.ietf.org/html/rfc6020#section-11) and
296 * [RFC 7951](http://tools.ietf.org/html/rfc7951#section-13).
Radek Krejcidef50022016-02-01 16:38:32 +0100297 *
Radek Krejci792f52f2017-02-28 16:36:31 +0100298 * When the [context](@ref howtocontext) is created, it already contains the following schemas, which
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200299 * are implemented internally by libyang:
Radek Krejci792f52f2017-02-28 16:36:31 +0100300 * - ietf-yang-metadata@2016-08-05
301 * - yang@2017-02-20
Radek Krejcidef50022016-02-01 16:38:32 +0100302 * - ietf-inet-types@2013-07-15
303 * - ietf-yang-types@2013-07-15
Radek Krejci792f52f2017-02-28 16:36:31 +0100304 * - ietf-yang-library@2016-06-21
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200305 *
Radek Krejci792f52f2017-02-28 16:36:31 +0100306 * The `yang` schema is the libyang's internal module to provide namespace and definitions of for various YANG
307 * attributes described in [RFC 7951](https://tools.ietf.org/html/rfc6243) (such as `insert` attribute for
308 * edit-config's data).
Radek Krejcidef50022016-02-01 16:38:32 +0100309 *
310 * Other schemas can be added to the context manually as described in [context page](@ref howtocontext) by the functions
311 * listed below. Besides the schema parser functions, it is also possible to use ly_ctx_load_module() which tries to
Michal Vaskof53187d2017-01-13 13:23:14 +0100312 * find the required schema automatically - using #ly_module_imp_clb or automatic search in working directory and in the
Radek Krejcidef50022016-02-01 16:38:32 +0100313 * context's searchpath.
314 *
315 * Functions List
316 * --------------
Radek Krejci722b0072016-02-01 17:09:45 +0100317 * - lys_parse_mem()
Radek Krejcidef50022016-02-01 16:38:32 +0100318 * - lys_parse_fd()
319 * - lys_parse_path()
Radek Krejci72cdfac2018-08-15 14:47:33 +0200320 * - lys_search_localfile()
Michal Vaskof53187d2017-01-13 13:23:14 +0100321 * - ly_ctx_set_module_imp_clb()
Radek Krejcidef50022016-02-01 16:38:32 +0100322 * - ly_ctx_load_module()
323 */
324
325/**
326 * @page howtoschemasfeatures YANG Features Manipulation
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200327 *
328 * The group of functions prefixed by \b lys_features_ are used to access and manipulate with the schema's
329 * features.
330 *
331 * The first two functions are used to access information about the features in the schema.
332 * lys_features_list() provides list of all features defined in the specific schema and its
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200333 * submodules. Optionally, it can also provide information about the state of all features.
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200334 * Alternatively, caller can use lys_features_state() function to get state of one specific
335 * feature.
336 *
337 * The remaining two functions, lys_features_enable() and lys_features_disable(), are used
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200338 * to enable and disable the specific feature (or all via the '`*`' value). By default, when the module
Radek Krejcidef50022016-02-01 16:38:32 +0100339 * is loaded by libyang parser, all features are disabled.
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200340 *
Radek Krejcidef50022016-02-01 16:38:32 +0100341 * To get know, if a specific schema node is currently disabled or enable, the lys_is_disabled() function can be used.
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200342 *
Radek Krejcidef50022016-02-01 16:38:32 +0100343 * Note, that the feature's state can affect some of the output formats (e.g. Tree format).
344 *
345 * Functions List
346 * --------------
347 * - lys_features_list()
348 * - lys_features_enable()
349 * - lys_features_disable()
350 * - lys_features_state()
351 * - lys_is_disabled()
352 */
353
354/**
Michal Vaskoc6cd3f02018-03-02 14:07:42 +0100355 * @page howtoschemaplugins YANG Extension and User Type Support
356 *
357 * Extensions and user types are supported in the form of **plugins**. These are loaded from the plugin directory
358 * (`LIBDIR/libyang/`) whenever a context is created. However, the list of plugins can be refreshed manually by ly_load_plugins().
359 * The extension plugin directory path (default `LIBDIR/libyang/extensions/`) can be change via the
360 * `LIBYANG_EXTENSIONS_PLUGINS_DIR` environment variable and similarly the user type directory (default `LIBDIR/libyang/user_types/`)
361 * via `LIBYANG_USER_TYPES_PLUGINS_DIR`. Note, that unavailable plugins are not removed, only
362 * any new plugins are loaded. Also note that the availability of new plugins does not affect the current schemas in the
363 * contexts, they are applied only to the newly parsed schemas.
364 *
365 * The plugins list can be cleaned by ly_clean_plugins(). However, since various contexts (respectively their
366 * schemas) can link to the plugins, the cleanup is successful only when there is no remaining context.
367 *
368 * @section extensions Extensions
Radek Krejci16d64a52017-01-31 14:10:15 +0100369 *
370 * YANG provides extensions as a mechanism how to add new statements into the language. Since they are very generic -
371 * extension instance can appear anywhere, they can contain any other YANG statement including extension instances and
372 * their specification has a form of the description text, it is very challenging for libyang to provide working and
373 * useful implementation. Therefore, you can reach some limitation regarding the extensions support, but the common
374 * use cases should be covered and supported.
375 *
376 * Since libyang does not understand human text, it is not possible to get the complete defintion of the extension from
Michal Vaskoc6cd3f02018-03-02 14:07:42 +0100377 * its description statement. Therefore, libyang allows the schema authors to provide @link extplugins extension
Radek Krejci89db0592017-02-16 15:07:13 +0100378 * plugin@endlink that provides information from the extension description to libyang.
Radek Krejci16d64a52017-01-31 14:10:15 +0100379 *
380 * Here are some notes about the implementation of the particular YANG extensions features
381 *
382 * - Extension instance can appear anywhere
383 * This is completely supported. In addition, the extension plugins are allowed to provide callback function to
384 * check if the extension instance is actually allowed to be instantiated as a substatement of the particular node.
Radek Krejci89db0592017-02-16 15:07:13 +0100385 * In some case the extension instance is not stored in the libyang structures inside the statement it is
386 * instantiated since some statements (such as description, position and several others listed as #LYEXT_SUBSTMT) are
387 * not stored as a structure, but directly as a value. The extension instances of such statements are then actually
388 * stored besides the extension instances of the parent structure (e.g. in ::lys_type_bit in case of position's
389 * extension instances). These extension instances are indicated by the ::lys_ext_instance#insubstmt value which
390 * refers (sometimes in combination with ::lys_ext_instance#insubstmt_index) to the substatement where the particular
391 * extension is actually instantiated.
Radek Krejci16d64a52017-01-31 14:10:15 +0100392 *
393 * - Extension instance can contain any other YANG statement
Radek Krejci89db0592017-02-16 15:07:13 +0100394 * This is supported with exceptions - the list of YANG statements is defined as #LY_STMT enumeration with the
395 * description if the specific statement can appear as an extension instance substatement or if there is some
396 * limitation regarding its cardinality. It also specifies how the specific statement is stored in the extension
397 * instance (which data type or structure is used). In some cases, like `include` or `yang-version` statements, it
398 * make no sense to have them inside an extension instance, so they are not supported by libyang.
Radek Krejci16d64a52017-01-31 14:10:15 +0100399 *
Radek Krejci89db0592017-02-16 15:07:13 +0100400 * The list of allowed substatement for the specific extension instances is provided by the extension plugin. Besides
401 * the list of substatement, the plugin also specifies where the data are stored and the cardinality for each
402 * substatement. Other extension instances as an extension instance substatements are always allowed. Note, that if
403 * there is no extension plugin defined or if the list of the allowed substatements is empty, no substatement (except
404 * extension instances) is allowed.
405 *
406 * There are several extension types (enumerated as #LYEXT_TYPE) according to their purpose and content:
407 *
408 * - #LYEXT_FLAG - simple extension with no content (no substatement is allowed). The extension instance serves just as
409 * a flag with or without an argument and its value. The example of such an extension is NACM's __default-deny-write__
410 * or __default-deny-all__. This is the default type of extension for the case there is no extension plugin available
411 * for the specific extension. Extension instances of this type are represented as ::lys_ext_instance.
412 *
413 * - #LYEXT_COMPLEX - complex extension with content. The specific rules for the content and how it is stored in the
414 * extension instance structure is specified by the plugin. Extension instances of this type are represented as
415 * ::lys_ext_instance_complex.
416 *
417 * Note that the ::lys_ext_instance structure serves as a generic structure for all extension instances. In case the
418 * ::lys_ext_instance#ext_type is set to a different value than #LYEXT_FLAG, the structure can be cast to the particular
419 * extension instance structure to access the type-specific members.
Radek Krejci16d64a52017-01-31 14:10:15 +0100420 *
Michal Vaskoc6cd3f02018-03-02 14:07:42 +0100421 * @subsection extplugins Extension Plugins
422 *
Radek Krejci16d64a52017-01-31 14:10:15 +0100423 * Extension plugins provide more detailed information about the extension in a understandable form for libyang. These
424 * information is usually provided in a text form in the extension's description statement. libyang provides several
425 * plugins for the common IETF extensions (NACM, Metadata, ...) that can be used as a code examples for other
426 * extensions.
427 *
Radek Krejci89db0592017-02-16 15:07:13 +0100428 * There are several types of extension plugins which corresponds to the extension types:
429 *
430 * - ::lyext_plugin - the plugin for a simple extensions of type #LYEXT_FLAG. The plugin is supposed to provide
431 * callbacks for:
432 * + @link lyext_check_position_clb checking extension position @endlink (if the extension instance can be instantiated
433 * in a particular parent)
434 * + @link lyext_check_inherit_clb deciding if the extension is supposed to be inherited @endlink
435 * (see #LYEXT_OPT_INHERIT). Note that the inherited extension instances are available in the libyang data
436 * structures, but they are not part of the schema so they are not printed into the schema output formats.
437 * + @link lyext_check_result_clb checking the result @endlink, the callback is called when the schema is completely
438 * parsed and all the data are available for the final (possibly complex) checking (or modification)
439 * - ::lyext_plugin_complex - the plugin for the complex extension instances (#LYEXT_COMPLEX). The structure extends
440 * the ::lyext_plugin structure by:
441 * + instance_size - the overall size of the ::lyext_plugin_complex structure to allocate.
442 * The substatement list is flexible array, so the size of the plugin structure depends on the content of the array.
443 * + substmt - the list of the allowed substatements to the extension instance, besides the substatement id, it
444 * also specifies its cardinality and the specific position in the ::lys_ext_instance_complex where the data are
445 * stored (as offset to the ::lys_ext_instance_complex#content member). The way how the data are stored is
446 * specified descriptions of #LY_STMT values.
447 *
Michal Vaskoc6cd3f02018-03-02 14:07:42 +0100448 * @subsection howtoschemametadata Metadata Support
Radek Krejci792f52f2017-02-28 16:36:31 +0100449 *
450 * YANG Metadata annotations are defined in [RFC 7952](https://tools.ietf.org/html/rfc6243) as YANG extension. In
451 * practice, it allows to have XML attributes (there is also a special encoding for JSON) in YANG modeled data.
452 * libyang does not allow to have any XML attribute without the appropriate annotation definition describing the
453 * data as it is done e.g. for leafs. When an attribute without a matching annotation definition is found in the
454 * input data, it is silently dropped (with warning) or an error is reported in case the #LYD_OPT_STRICT parser
455 * option is provided to the [parser function](@ref howtodataparsers).
456 *
457 * There are some XML attributes, described by [YANG](https://tools.ietf.org/html/rfc7951) and
458 * [NETCONF](https://tools.ietf.org/html/rfc6241) specifications, which are not defined as annotations, but libyang
459 * implements them this way. In case of attributes in the YANG namespace (`insert`, `value` and `key` attributes
460 * for the NETCONF edit-config operation), they are defined in special libyang's internal module `yang`, which is
461 * available in each context and the content of this schema can be printed via
462 * [schema printers](@ref howtoschemasprinters).
463 *
464 * In case of the attributes described in [NETCONF specification](https://tools.ietf.org/html/rfc6241), the libyang's
465 * annotations structures are hidden and cannot be printed despite, internally, they are part of the `ietf-netconf`'s
466 * schema structure. Therefore, these attributes are available only when the `ietf-netconf` schema is loaded in the
467 * context. The definitions of these annotations are as follows:
468 *
469 * md:annotation operation {
470 * type enumeration {
471 * enum merge;
472 * enum replace;
473 * enum create;
474 * enum delete;
475 * enum remove;
476 * }
477 * }
478 *
479 * md:annotation type {
480 * type enumeration {
481 * enum subtree;
482 * enum xpath {
483 * if-feature "nc:xpath";
484 * }
485 * }
486 * }
487 *
488 * md:annotation select {
489 * type string;
490 * }
491 *
492 * Note, that, following the specification,
493 * - the `type` and `select` XML attributes are supposed to be unqualified (without namespace) and that
494 * - the `select`'s content is XPath and it is internally transformed by libyang into the format where the
495 * XML namespace prefixes are replaced by the YANG module names.
496 *
Michal Vaskoc6cd3f02018-03-02 14:07:42 +0100497 * @section usertypes User Types
498 *
499 * Using this plugin mechanism, it is also possible to define what can be called **user types**. Values are
500 * always stored as a string in addition to being in a #lyd_val union. It is possible to customize how
501 * the value is stored in the union using a #lytype_store_clb callback.
502 *
503 * Generally, it is meant for storing certain types more effectively. For instance, when working with **ipv4-address**
504 * from the *ietf-inet-types* model, an application will most likely use the address in a binary form, not as a string.
505 * So, in the callback the value is simply transformed into the desired format and saved into #lyd_val value. However,
506 * the callback is allowed to store anything in the union. Another example, if there are many strings being created and
507 * handled, is to store the string length instead having 2 pointers to the same string.
508 *
Michal Vasko25b59c72018-03-21 11:49:23 +0100509 * Also, it is possible to perform some additional validation of the value except for the standard YANG one. Even if
510 * the value should only be validated, this callback can be defined and used, it will just keep the #lyd_val value
511 * unchanged.
512 *
Michal Vaskoc6cd3f02018-03-02 14:07:42 +0100513 * @subsection typeplugins User Type Plugins
514 *
Michal Vasko25b59c72018-03-21 11:49:23 +0100515 * There are simple example user type plugins in `src/user_types`.
Michal Vaskoc6cd3f02018-03-02 14:07:42 +0100516 *
517 * - ::lytype_plugin_list - plugin is supposed to provide callbacks for:
518 * + @link lytype_store_clb storing the value itself @endlink
519 * + freeing the stored value (optionally, if the store callback allocates memory)
520 *
Radek Krejci16d64a52017-01-31 14:10:15 +0100521 * Functions List
522 * --------------
Radek Krejci16d64a52017-01-31 14:10:15 +0100523 * - lys_ext_instance_presence()
524 * - lys_ext_instance_substmt()
Michal Vaskoc6cd3f02018-03-02 14:07:42 +0100525 * - ly_load_plugins()
526 * - ly_clean_plugins()
Radek Krejci16d64a52017-01-31 14:10:15 +0100527 */
528
529/**
Radek Krejcidef50022016-02-01 16:38:32 +0100530 * @page howtoschemasprinters Printing Schemas
531 *
532 * Schema printers allows to serialize internal representation of a schema module in a specific format. libyang
533 * supports the following schema formats for printing:
534 *
535 * - YANG
536 *
Radek Krejci792f52f2017-02-28 16:36:31 +0100537 * Basic YANG schemas format described in [RFC 6020](http://tools.ietf.org/html/rfc6020) and
538 * [RFC 7951](http://tools.ietf.org/html/rfc7951) (so both YANG 1.0 and YANG 1.1 versions are supported).
Radek Krejcidef50022016-02-01 16:38:32 +0100539 *
540 * - YIN
541 *
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200542 * Alternative XML-based format to YANG - YANG Independent Notation. The details can be found in
Radek Krejci792f52f2017-02-28 16:36:31 +0100543 * [RFC 6020](http://tools.ietf.org/html/rfc6020#section-11) and
544 * [RFC 7951](http://tools.ietf.org/html/rfc7951#section-13).
Radek Krejcidef50022016-02-01 16:38:32 +0100545 *
Radek Krejcidef50022016-02-01 16:38:32 +0100546 * - Tree
547 *
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200548 * Simple tree structure of the module where each node is printed as:
549 *
550 * <status> <flags> <name> <opts> <type> <if-features>
551 *
552 * - `<status>` is one of:
553 * - `+` for current
554 * - `x` for deprecated
555 * - `o` for obsolete
556 *
557 * - `<flags>` is one of:
558 * - `rw` for configuration data
559 * - `ro` for status data
560 * - `-x` for RPCs
561 * - `-n` for Notification
562 *
563 * - `<name>` is the name of the node
564 * - `(<name>)` means that the node is a choice node
565 * - `:(<name>)` means that the node is a case node
566 * - if the node is augmented into the tree from another module, it is printed with the module name as
567 * `<module-name>:<name>`.
568 *
569 * - `<opts>` is one of:
570 * - `?` for an optional leaf or choice
571 * - `!` for a presence container
572 * - `*` for a leaf-list or list
573 * - `[<keys>]` for a list's keys
574 *
575 * - `<type>` is the name of the type for leafs and leaf-lists
576 * - if there is a default value defined, it is printed within angle brackets `<default-value>`
577 * - if the type is a leafref, the type is printed as -> TARGET`
578 *
579 * - `<if-features>` is the list of features this node depends on, printed within curly brackets and
580 * a question mark `{...}?`
581 *
Radek Krejcidef50022016-02-01 16:38:32 +0100582 *
583 * - Info
584 *
585 * Detailed information about the specific node in the schema tree.
586 * It allows to print information not only about a specific module, but also about its specific part:
587 *
588 * - absolute-schema-nodeid
589 *
590 * e.g. \a `/modules/module-set-id` in \a `ietf-yang-library` module
591 *
592 * - <b>typedef/</b>typedef-name
593 *
594 * e.g. \a `typedef/revision-identifier` in \a `ietf-yang-library` module
595 *
596 * - <b>feature/</b>feature-name
597 *
598 * e.g. \a `feature/ssh` in \a `ietf-netconf-server` module
599 *
600 * - <b>grouping/</b>grouping-name/descendant-schema-nodeid
601 *
602 * e.g. \a `grouping/module` or \a `grouping/module/module/submodules` in \a `ietf-yang-library` module
603 *
604 * - <b>type/</b>leaf-or-leaflist
605 *
606 * e.g. \a `type/modules/module-set-id` in \a `ietf-yang-library` module
607 *
608 * Printer functions allow to print to the different outputs including a callback function which allows caller
609 * to have a full control of the output data - libyang passes to the callback a private argument (some internal
610 * data provided by a caller of lys_print_clb()), string buffer and number of characters to print. Note that the
611 * callback is supposed to be called multiple times during the lys_print_clb() execution.
612 *
613 * Functions List
614 * --------------
615 * - lys_print_mem()
616 * - lys_print_fd()
617 * - lys_print_file()
618 * - lys_print_clb()
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200619 */
620
621/**
622 * @page howtodata Data Instances
Radek Krejci26715a42015-07-29 14:10:45 +0200623 *
Radek Krejcidef50022016-02-01 16:38:32 +0100624 * All data nodes in data trees are connected with their schema node - libyang is not able to represent data of an
625 * unknown schema.
626 *
Michal Vasko42aa0ab2016-10-25 15:09:32 +0200627 * Connected to the aforementioned, it is important to mention that there are several kinds of data trees libyang
628 * can work with. Except for a standard full data tree and configuration data tree, there are trees specific for use
629 * in NETCONF, which are \<get\> or \<get-config\> replies, \<edit-config\> input content, generic RPC/action request,
630 * RPC/action reply, or a notification.
631 *
632 * Special care is needed when working with action input and RPC/action output, because in these cases the libyang data
633 * tree does not match the actual data tree NETCONF expects.
634 *
635 * <b>Action input</b> differentiates from a standard RPC that the child of \<rpc\> element is not the specific RPC
636 * request being sent, but a special \<action\> element in the base YANG namespace. It is expected to be present when
637 * parsing an action input and can optionally be included when the action is printed.
638 *
639 * As for <b>RPC output</b>, NETCONF expects the output parameters as direct children of the \<rpc-reply\> element.
640 * However, libyang encapsulates these parameters in the specific RPC request container. Same as before, it is expected
641 * to be without this container when parsing and can be printed out.
642 *
643 * Lastly, <b>action output</b> is almost similar to RPC output and in NETCONF you actually cannot distinguish them,
644 * but in case of action output, libyang puts all the output parameters under the action container, which is also
645 * just a child to all the schema tree ancestors up to the schema top-level, just like in an action input. Same applies
646 * even in this case, parsed data should be only the output parameters, you can decide how to print it.
647 *
648 * So, you do not have to use any special flag when you want to print the data tree the way libyang internally stores
649 * it, although you will not be able to parse it back, as was mentioned in the paragraphs above. To print data in the
650 * way NETCONF and libyang expects them to be, use #LYP_NETCONF flag for any printing function.
651 *
Michal Vasko1ec579e2016-09-13 11:24:28 +0200652 * Please, continue reading a specific subsection or go through all the subsections if you are a new user of libyang.
Radek Krejcidef50022016-02-01 16:38:32 +0100653 *
654 * - @subpage howtodataparsers
655 * - @subpage howtodatamanipulators
Michal Vasko1ec579e2016-09-13 11:24:28 +0200656 * - @subpage howtodatavalidation
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200657 * - @subpage howtodatawd
Radek Krejcidef50022016-02-01 16:38:32 +0100658 * - @subpage howtodataprinters
659 *
660 * \note API for this group of functions is described in the [Data Instances module](@ref datatree).
661 *
662 * Functions List (not assigned to above subsections)
663 * --------------------------------------------------
Michal Vaskof06fb5b2016-09-08 10:05:56 +0200664 * - lyd_find_instance()
665 * - lyd_find_xpath()
Radek Krejciae1c3b12016-09-20 16:30:32 +0200666 * - lyd_leaf_type()
Radek Krejcidef50022016-02-01 16:38:32 +0100667 */
668
669/**
670 * @page howtodataparsers Parsing Data
671 *
672 * Data parser allows to read instances from a specific format. libyang supports the following data formats:
673 *
674 * - XML
675 *
676 * Original data format used in NETCONF protocol. XML mapping is part of the YANG specification
677 * ([RFC 6020](http://tools.ietf.org/html/rfc6020)).
678 *
679 * - JSON
680 *
681 * The alternative data format available in RESTCONF protocol. Specification of JSON encoding of data modeled by YANG
682 * can be found in [this draft](https://tools.ietf.org/html/draft-ietf-netmod-yang-json-05).
683 *
684 * Besides the format of input data, the parser functions accepts additional [options](@ref parseroptions) to specify
685 * how the input data should be processed.
686 *
687 * In contrast to the schema parser, data parser also accepts empty input data if such an empty data tree is valid
688 * according to the schemas in the libyang context.
689 *
Michal Vasko346b5f32017-03-07 10:42:40 +0100690 * If a node from a schema, which is not present in the context or is not implemented, is parsed, a callback set by
691 * ly_ctx_set_module_data_clb() is called and this way the application is given the opportunity to add this schema into
692 * the context or change its conformance before the parsing would fail.
Michal Vaskof53187d2017-01-13 13:23:14 +0100693 *
Radek Krejcidef50022016-02-01 16:38:32 +0100694 * In case of XML input data, there is one additional way to parse input data. Besides parsing the data from a string
695 * in memory or a file, caller is able to build an XML tree using [libyang XML parser](@ref howtoxml) and then use
696 * this tree (or a part of it) as input to the lyd_parse_xml() function.
697 *
698 * Functions List
699 * --------------
Radek Krejci722b0072016-02-01 17:09:45 +0100700 * - lyd_parse_mem()
Radek Krejcidef50022016-02-01 16:38:32 +0100701 * - lyd_parse_fd()
702 * - lyd_parse_path()
703 * - lyd_parse_xml()
704 */
705
706/**
707 * @page howtodatamanipulators Manipulating Data
708 *
709 * There are many functions to create or modify an existing data tree. You can add new nodes, reconnect nodes from
710 * one tree to another (or e.g. from one list instance to another) or remove nodes. The functions doesn't allow you
711 * to put a node to a wrong place (by checking the module), but not all validation checks can be made directly
712 * (or you have to make a valid change by multiple tree modifications) when the tree is being changed. Therefore,
Michal Vasko58f74f12016-03-24 13:26:06 +0100713 * there is lyd_validate() function supposed to be called to make sure that the current data tree is valid. If
714 * working with RPCs, they are invalid also in case the data nodes are not ordered according to the schema, which
715 * you can fix easily with lyd_schema_sort(). Note, that not performing validation after some data tree changes
Michal Vasko299f9832017-01-06 13:29:22 +0100716 * can cause failure of various libyang functions later. All functions performing data tree changes are marked
717 * with the __PARTIAL CHANGE__ flag in their documentation meaning they leave at least partly non-validated data tree.
Radek Krejcidef50022016-02-01 16:38:32 +0100718 *
Michal Vasko0f14ba62016-03-21 15:38:11 +0100719 * Creating data is generally possible in two ways, they can be combined. You can add nodes one-by-one based on
Michal Vasko1ec579e2016-09-13 11:24:28 +0200720 * the node name and/or its parent (lyd_new(), \b lyd_new_anydata_*(), lyd_new_leaf(), and their output variants) or
Michal Vasko58f74f12016-03-24 13:26:06 +0100721 * address the nodes using a simple XPath addressing (lyd_new_path()). The latter enables to create a whole path
722 * of nodes, requires less information about the modified data, and is generally simpler to use. The path format
723 * specifics can be found [here](@ref howtoxpath).
Michal Vasko0f14ba62016-03-21 15:38:11 +0100724 *
Michal Vasko3c126822016-09-22 13:48:42 +0200725 * Working with two data subtrees can also be performed two ways. Usually, you would use lyd_insert*() functions.
726 * They are generally meant for simple inserts of a node into a data tree. For more complicated inserts and when
727 * merging 2 trees use lyd_merge(). It offers additional options and is basically a more powerful insert.
Michal Vasko45fb2822016-04-18 13:32:17 +0200728 *
Radek Krejcidef50022016-02-01 16:38:32 +0100729 * Also remember, that when you are creating/inserting a node, all the objects in that operation must belong to the
730 * same context.
731 *
732 * Modifying the single data tree in multiple threads is not safe.
733 *
734 * Functions List
735 * --------------
736 * - lyd_dup()
Radek Krejciab80e3a2017-01-09 13:07:31 +0100737 * - lyd_dup_to_ctx()
Radek Krejcidef50022016-02-01 16:38:32 +0100738 * - lyd_change_leaf()
739 * - lyd_insert()
Radek Krejcidb6b1662016-09-15 10:40:16 +0200740 * - lyd_insert_sibling()
Radek Krejcidef50022016-02-01 16:38:32 +0100741 * - lyd_insert_before()
742 * - lyd_insert_after()
743 * - lyd_insert_attr()
Michal Vasko45fb2822016-04-18 13:32:17 +0200744 * - lyd_merge()
Radek Krejciab80e3a2017-01-09 13:07:31 +0100745 * - lyd_merge_to_ctx()
Radek Krejcidef50022016-02-01 16:38:32 +0100746 * - lyd_new()
Michal Vasko0845b112016-09-08 10:07:08 +0200747 * - lyd_new_anydata()
Radek Krejcidef50022016-02-01 16:38:32 +0100748 * - lyd_new_leaf()
Michal Vaskof5299282016-03-16 13:32:02 +0100749 * - lyd_new_path()
Michal Vasko0ba46152016-05-11 14:16:55 +0200750 * - lyd_new_output()
Michal Vasko0845b112016-09-08 10:07:08 +0200751 * - lyd_new_output_anydata()
Michal Vasko0ba46152016-05-11 14:16:55 +0200752 * - lyd_new_output_leaf()
Michal Vaskoae5a53e2017-01-05 10:33:41 +0100753 * - lyd_list_pos()
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200754 * - lyd_schema_sort()
Radek Krejcidef50022016-02-01 16:38:32 +0100755 * - lyd_unlink()
756 * - lyd_free()
757 * - lyd_free_attr()
758 * - lyd_free_withsiblings()
Michal Vasko1ec579e2016-09-13 11:24:28 +0200759 */
760
761/**
762 * @page howtodatavalidation Validating Data
763 *
764 * By default, the represented data are supposed to represent a full YANG datastore content. So if a schema declares
765 * some mandatory nodes, despite configuration or status, the data are supposed to be present in the data tree being
766 * loaded or validated. However, it is possible to specify other kinds of data (see @ref parseroptions) allowing some
767 * exceptions to the validation process.
768 *
769 * Data validation is performed implicitly to the input data processed by the parser (\b lyd_parse_*() functions) and
770 * on demand via the lyd_validate() function. The lyd_validate() is supposed to be used when a (complex or simple)
771 * change is done on the data tree (via a combination of \b lyd_change_*(), \b lyd_insert*(), \b lyd_new*(),
772 * lyd_unlink() and lyd_free() functions).
773 *
Michal Vaskof53187d2017-01-13 13:23:14 +0100774 * Part of data validation is resolving leafrefs and instance-identifiers. Leafrefs are resolved only when a change occured
775 * in the data tree that could have broken the link. However, as instance-identifiers can point to any node whatsoever
776 * without an import, it would not be effective to store metadata as in the case of leafrefs. That is why they are resolved
777 * during every validation. Also, for the same reason, it can easily happen that when parsing/validating data with
778 * an instance-identifier, it will target a remote node, whose schema is not currently present in the context. To handle
779 * this case, a callback should be set using ly_ctx_set_module_data_clb(), which can load the schema when required.
780 *
Michal Vasko1ec579e2016-09-13 11:24:28 +0200781 * Must And When Conditions Accessible Tree
782 * ----------------------------------------
783 *
784 * In YANG 1.1, there can be \b must and/or \b when expressions in RPC/action input or output, or in notifications that
785 * require access to the configuration datastore and/or state data. Normally, when working with any of the aforementioned
786 * data trees, they must contain only the RPC/action/notification itself, without any additional configuration or state
787 * data. So how can then these conditions be verified during validation?
788 *
789 * There is an option to pass this additional data tree to all the functions that perform \b must and \b when condition
Michal Vasko346d2b32018-07-10 16:14:09 +0200790 * checking (\b lyd_parse_*() and lyd_validate()). Also, there are 3 flags of \b struct lys_node that
791 * mark schema nodes that include paths that require foreign nodes (outside their subtree) for their evaluation.
792 * #LYS_XPCONF_DEP marks nodes with such must and/or when expressions that require some configuration data,
793 * #LYS_XPSTATE_DEP that require some state data, and #LYS_LEAFREF_DEP with such a leafref.
Michal Vasko1ec579e2016-09-13 11:24:28 +0200794 * The subtree root is always the particular operation data node (for RPC it is the RPC data node and all
795 * the input or output nodes as its children and similarly for action and notification). Note that for action and
796 * not-top-level notification this means that all their parents are not considered as belonging to their subtree even though
797 * they are included in their data tree and must be present for the operation validation to pass. The reason for this is that if
798 * there are any lists in those parents, we cannot know if there are not some other instances of them in the standard
799 * data tree in addition to the one used in the action/notification invocation.
800 *
801 * There were 2 ways of using this mechanism envisioned (explained below), but you can combine or modify them.
802 *
803 * ### Fine-grained Data Retrieval ###
804 *
805 * This approach is recommended when you do not maintain a full configuration data tree with state data at all times.
806 *
807 * Firstly, you should somehow learn that the operation data tree you are currently working with includes some schema
808 * node instances that have conditions that require foreign data. You can either know this about every operation beforehand
Michal Vasko346d2b32018-07-10 16:14:09 +0200809 * or you go through all the schema nodes looking for the flags #LYS_XPCONF_DEP, #LYS_XPSTATE_DEP, and #LYS_LEAFREF_DEP.
810 * Then you should use lys_node_xpath_atomize()
Michal Vasko1ec579e2016-09-13 11:24:28 +0200811 * to retrieve all XPath condition dependencies (in the form of schema nodes) outside the operation subtree. You will likely
812 * want to use the flag #LYXP_NO_LOCAL to get rid of all the nodes from inside the subtree (you should already have those).
813 * The last thing to do is to build a data tree that includes at least all the instances of the nodes obtained from lys_node_xpath_atomize()
814 * (it will be expected). Then you pass this tree to the validation and it should now have access to all the nodes that
815 * can potentially affect the XPath evaluation and no other.
816 *
817 * ### Maintaining Configuration And State Data Tree ###
818 *
819 * If you have a full data tree with state data available for the validation process then it is quite simple (compared
820 * to the first approach). You can simply always pass it to validation of these operations and in cases it is not required
821 * (no nodes with conditions traversing foreign nodes) only a negligible amount of redundant work is performed and you can
822 * skip the process of learning whether it is required or not.
823 *
824 * Functions List
825 * --------------
Radek Krejcidef50022016-02-01 16:38:32 +0100826 * - lyd_validate()
827 */
828
829/**
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200830 * @page howtodatawd Default Values
831 *
Radek Krejcidb6b1662016-09-15 10:40:16 +0200832 * libyang provides support for work with default values as defined in [RFC 6243](https://tools.ietf.org/html/rfc6243).
Radek Krejci46180b52016-08-31 16:01:32 +0200833 * This document defines 4 modes for handling default nodes in a data tree, libyang adds the fifth mode:
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200834 * - \b explicit - Only the explicitly set configuration data. But in the case of status data, missing default
Radek Krejci46180b52016-08-31 16:01:32 +0200835 * data are added into the tree. In libyang, this mode is represented by #LYP_WD_EXPLICIT option.
836 * - \b trim - Data nodes containing the schema default value are removed. This mode is applied using #LYP_WD_TRIM option.
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200837 * - \b report-all - All the missing default data are added into the data tree. This mode is represented by
Radek Krejci46180b52016-08-31 16:01:32 +0200838 * #LYP_WD_ALL option.
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200839 * - \b report-all-tagged - In this case, all the missing default data are added as in case of the `report-all` mode,
840 * but additionally all the nodes (existing as well as added) containing the schema default value
Radek Krejci46180b52016-08-31 16:01:32 +0200841 * are tagged (see the note below). libyang uses #LYP_WD_ALL_TAG option for this mode.
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200842 * - \b report-implicit-tagged - The last mode is similar to the previous one, except only the added nodes are tagged.
Radek Krejci46180b52016-08-31 16:01:32 +0200843 * This is the libyang's extension and it is represented by #LYP_WD_IMPL_TAG option.
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200844 *
Radek Krejci46180b52016-08-31 16:01:32 +0200845 * libyang automatically adds/maintain the default nodes when a data tree is being parsed or validated. Note, that in a
Michal Vasko5a078f82018-03-02 15:58:35 +0100846 * modified data tree (via e.g. lyd_insert() or lyd_free()), some of the default nodes can be missing or they can be
Radek Krejci46180b52016-08-31 16:01:32 +0200847 * present by mistake. Such a data tree is again corrected during the next lyd_validate() call.
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200848 *
Radek Krejci46180b52016-08-31 16:01:32 +0200849 * The implicit (default) nodes, created by libyang, are marked with the ::lyd_node#dflt flag which applies to the
Radek Krejcid3cfbc92016-09-15 10:39:33 +0200850 * leafs and leaf-lists. In case of containers, the flag means that the container holds only a default node(s) or it
851 * is an empty container (according to YANG 1.1 spec, all such containers are part of the accessible data tree).
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200852 *
Radek Krejci46180b52016-08-31 16:01:32 +0200853 * The presence of the default nodes during the data tree lifetime is affected by the LYD_OPT_ flag used to
854 * parse/validate the tree:
855 * - #LYD_OPT_DATA - all the default nodes are present despite they are configuration or status nodes
856 * - #LYD_OPT_CONFIG - only the configuration data nodes are added into the tree
857 * - #LYD_OPT_GET, #LYD_OPT_GETCONFIG, #LYD_OPT_EDIT - no default nodes are added
858 * - #LYD_OPT_RPC, #LYD_OPT_RPCREPLY, #LYD_OPT_NOTIF - the default nodes from the particular subtree are added
859 *
Radek Krejcidb6b1662016-09-15 10:40:16 +0200860 * The with-default modes described above are supported when the data tree is being printed with the
Radek Krejci46180b52016-08-31 16:01:32 +0200861 * [LYP_WD_ printer flags](@ref printerflags). Note, that in case of #LYP_WD_ALL_TAG and #LYP_WD_IMPL_TAG modes,
862 * the XML/JSON attributes are printed only if the context includes the ietf-netconf-with-defaults schema. Otherwise,
Radek Krejcid3cfbc92016-09-15 10:39:33 +0200863 * these modes have the same result as #LYP_WD_ALL. The presence of empty containers (despite they were added explicitly
864 * or implicitly as part of accessible data tree) depends on #LYP_KEEPEMPTYCONT option.
Radek Krejci46180b52016-08-31 16:01:32 +0200865 *
866 * To get know if the particular leaf or leaf-list node contains default value (despite implicit or explicit), you can
Radek Krejcidb6b1662016-09-15 10:40:16 +0200867 * use lyd_wd_default() function.
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200868 *
869 * Functions List
870 * --------------
Radek Krejci46180b52016-08-31 16:01:32 +0200871 * - lyd_wd_default()
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200872 *
873 * - lyd_parse_mem()
874 * - lyd_parse_fd()
875 * - lyd_parse_path()
876 * - lyd_parse_xml()
877 * - lyd_validate()
Radek Krejci46180b52016-08-31 16:01:32 +0200878 * - lyd_print_mem()
879 * - lyd_print_fd()
880 * - lyd_print_file()
881 * - lyd_print_clb()
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200882 */
883
884/**
Radek Krejcidef50022016-02-01 16:38:32 +0100885 * @page howtodataprinters Printing Data
886 *
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200887 * Data printers allows to serialize internal representation of a data tree in a specific format. libyang
888 * supports the following data formats for printing:
Radek Krejcidef50022016-02-01 16:38:32 +0100889 *
890 * - XML
891 *
892 * Basic format as specified in rules of mapping YANG modeled data to XML in
893 * [RFC 6020](http://tools.ietf.org/html/rfc6020). It is possible to specify if
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200894 * the indentation (formatting) will be used (by #LYP_FORMAT @ref printerflags "printer option").
Radek Krejcidef50022016-02-01 16:38:32 +0100895 *
896 * - JSON
897 *
898 * The alternative data format available in RESTCONF protocol. Specification of JSON encoding of data modeled by YANG
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200899 * can be found in [this draft](https://tools.ietf.org/html/draft-ietf-netmod-yang-json-05).It is possible to specify
900 * if the indentation (formatting) will be used (by #LYP_FORMAT @ref printerflags "printer option").
Radek Krejcidef50022016-02-01 16:38:32 +0100901 *
902 * Printer functions allow to print to the different outputs including a callback function which allows caller
903 * to have a full control of the output data - libyang passes to the callback a private argument (some internal
904 * data provided by a caller of lyd_print_clb()), string buffer and number of characters to print. Note that the
905 * callback is supposed to be called multiple times during the lyd_print_clb() execution.
906 *
Radek Krejci46180b52016-08-31 16:01:32 +0200907 * To print the data tree with default nodes according to the with-defaults capability defined in
908 * [RFC 6243](https://tools.ietf.org/html/rfc6243), check the [page about the default values](@ref howtodatawd).
909 *
Michal Vasko42aa0ab2016-10-25 15:09:32 +0200910 * Also, to print the data in NETCONF format, use the #LYP_NETCONF flag. More information can be found on the page
911 * @ref howtodata.
912 *
Radek Krejcidef50022016-02-01 16:38:32 +0100913 * Functions List
914 * --------------
915 * - lyd_print_mem()
916 * - lyd_print_fd()
917 * - lyd_print_file()
918 * - lyd_print_clb()
919 */
920
921/**
Radek Krejcib50551c2016-04-19 09:15:38 +0200922 * @page howtoxpath XPath Addressing
923 *
Michal Vasko50576712017-07-28 12:28:33 +0200924 * Internally, XPath evaluation is performed on __when__ and __must__ conditions in the schema. For that almost
925 * a full XPath 1.0 evaluator was implemented. In YANG models you can also find paths identifying __augment__
926 * targets, __leafref__ targets, and trivial paths in __choice default__ and __unique__ statements argument.
927 * The exact format of all those paths can be found in the relevant RFCs. Further will only be discussed
928 * paths that are used directly in libyang API functions.
929 *
930 * Schema
931 * ======
932 *
933 * Regarding identifying schema nodes, we use a slightly modified version of YANG __augment__ target path:
934 * - strictly speaking, most XPath expressions are not accepted, only simple paths (no predicates,
935 * numbers, literals, operators, ...),
936 * - whenever a prefix is used for a node, it is not the import prefix, but the __module name__ itself,
937 * - __current module__ is specified separately for _absolute_ paths and is the module of the start
938 * (current) node for _relative_ paths,
939 * - unprefixed nodes all use the prefix of the __current module__ so all nodes from other modules than
940 * the __current module__ _MUST_ have prefixes,
941 * - nodes from the __current module__ _MAY_ have prefixes,
942 *
943 * Examples
944 * --------
Michal Vasko8e627692016-04-19 12:15:47 +0200945 *
Michal Vaskoebea7012016-04-19 14:15:22 +0200946 * - get all top-level nodes of the __module-name__
947 *
948 * /module-name:*
949 *
950 * - get all the descendants of __container__ (excluding __container__)
951 *
952 * /module-name:container//\asterisk
953 *
Michal Vaskoebea7012016-04-19 14:15:22 +0200954 * - get __aug-leaf__, which was added to __module-name__ from an augment module __augment-module__
955 *
Michal Vasko50576712017-07-28 12:28:33 +0200956 * /module-name:container/container2/augment-module:aug-cont/augment-module:aug-leaf
Radek Krejcib50551c2016-04-19 09:15:38 +0200957 *
958 * Functions List
959 * --------------
Michal Vasko50576712017-07-28 12:28:33 +0200960 * - lys_find_path()
961 * - lys_path()
Michal Vasko40702732017-10-25 11:43:23 +0200962 * - ly_path_data2schema()
Michal Vasko50576712017-07-28 12:28:33 +0200963 *
964 *
965 * Data
966 * ====
967 *
968 * As for paths evaluated on YANG data, we opted for standardized JSON paths ([RFC 7951](https://tools.ietf.org/html/rfc7951#section-6.11)). Summarized, it follows these conventions:
969 * - generally, you can use almost a full XPath in these paths where it makes sense, but only data nodes (node sets)
970 * will always be returned (except for paths, predicates are mostly used),
971 * - as per the specification, prefixes are actually __module names__,
972 * - also in the specification, for _absolute_ paths, the first (leftmost) node _MUST_ have a prefix,
973 * - for _relative_ paths, you specify the __context node__, which then acts as a parent for the first node in the path,
974 * - nodes always inherit their module (prefix) from their __parent node__ so whenever a node is from a different
975 * module than its parent, it _MUST_ have a prefix,
976 * - nodes from the same module as their __parent__ _MUST NOT_ have a prefix,
977 * - different from schema paths, non-data nodes (choice, case, uses, input, output) are skipped and not included
978 * in the path.
979 *
980 * Examples
981 * --------
982 *
983 * - get __list__ instance with __key1__ of value __1__ and __key2__ of value __2__ (this can return more __list__ instances if there are more keys than __key1__ and __key2__)
984 *
985 * /module-name:container/list[key1='1'][key2='2']
986 *
987 * - get __leaf-list__ instance with the value __val__
988 *
989 * /module-name:container/leaf-list[.='val']
990 *
991 * - get __aug-list__ with __aug-list-key__, which was added to __module-name__ from an augment module __augment-module__
992 *
993 * /module-name:container/container2/augment-module:aug-cont/aug-list[aug-list-key='value']
994 *
995 * Functions List
996 * --------------
997 * - lyd_find_path()
Radek Krejcib50551c2016-04-19 09:15:38 +0200998 * - lyd_new_path()
Michal Vasko50576712017-07-28 12:28:33 +0200999 * - lyd_path()
1000 * - lys_data_path()
Michal Vaskob3744402017-08-03 14:23:58 +02001001 * - ly_ctx_get_node()
Radek Krejcic683acd2018-01-22 14:51:52 +01001002 * - ly_ctx_find_path()
Michal Vasko50576712017-07-28 12:28:33 +02001003 *
Radek Krejcib50551c2016-04-19 09:15:38 +02001004 */
1005
1006/**
Radek Krejcidef50022016-02-01 16:38:32 +01001007 * @page howtoxml libyang XML Support
1008 *
Radek Krejcib50551c2016-04-19 09:15:38 +02001009 * libyang XML parser is able to parse XML documents. The main purpose is to load data modeled by YANG. However, it can
1010 * be used as a standalone XML parser with the following limitations in comparison to a full-featured XML parsers:
Radek Krejcidef50022016-02-01 16:38:32 +01001011 * - comments are ignored
1012 * - Doctype declaration is ignored
1013 * - CData sections are ignored
1014 * - Process Instructions (PI) are ignored
1015 *
1016 * The API is designed to almost only read-only access. You can simply load XML document, go through the tree as
1017 * you wish and dump the tree to an output. The only "write" functions are lyxml_free() and lyxml_unlink() to remove
1018 * part of the tree or to unlink (separate) a subtree.
1019 *
Radek Krejcib50551c2016-04-19 09:15:38 +02001020 * XML parser is used internally by libyang for parsing YIN schemas and data instances in XML format.
Radek Krejcidef50022016-02-01 16:38:32 +01001021 *
1022 * \note API for this group of functions is described in the [XML Parser module](@ref xmlparser).
1023 *
1024 * Functions List
1025 * --------------
Radek Krejci722b0072016-02-01 17:09:45 +01001026 * - lyxml_parse_mem()
1027 * - lyxml_parse_path()
Radek Krejcidef50022016-02-01 16:38:32 +01001028 * - lyxml_get_attr()
1029 * - lyxml_get_ns()
Radek Krejci722b0072016-02-01 17:09:45 +01001030 * - lyxml_print_mem()
1031 * - lyxml_print_fd()
1032 * - lyxml_print_file()
1033 * - lyxml_print_clb()
Radek Krejcidef50022016-02-01 16:38:32 +01001034 * - lyxml_unlink()
1035 * - lyxml_free()
1036 */
1037
1038/**
1039 * @page howtothreads libyang in Threads
1040 *
Radek Krejcib50551c2016-04-19 09:15:38 +02001041 * libyang can be used in multithreaded applications keeping in mind the following rules:
Radek Krejcidef50022016-02-01 16:38:32 +01001042 * - libyang context manipulation (adding new schemas) is not thread safe and it is supposed to be done in a main
Radek Krejcib50551c2016-04-19 09:15:38 +02001043 * thread before any other work with context, schemas or data instances. Destroying the context is supposed to
Michal Vaskoe77dc992017-01-18 12:09:42 +01001044 * be done when no other thread accesses context, schemas nor data trees,
1045 * - data parser (\b lyd_parse*() functions) can be used simultaneously in multiple threads (also the returned
1046 * #ly_errno is thread safe),
1047 * - data manipulation (lyd_new(), lyd_insert(), lyd_unlink(), lyd_free() and many other
1048 * functions) a single data tree is not thread safe,
1049 * - data printing of a single data tree is thread-safe.
Radek Krejci26715a42015-07-29 14:10:45 +02001050 */
Radek Krejci94ca54b2015-07-08 15:48:47 +02001051
Radek Krejcida04f4a2015-05-21 12:54:09 +02001052/**
Radek Krejci26715a42015-07-29 14:10:45 +02001053 * @page howtologger Logger
1054 *
1055 * There are 4 verbosity levels defined as ::LY_LOG_LEVEL. The level can be
1056 * changed by the ly_verb() function. By default, the verbosity level is
1057 * set to #LY_LLERR value.
1058 *
Michal Vasko53b7da02018-02-13 15:28:42 +01001059 * All the logging operations are tied to the specific **thread** and **context**.
1060 * The default behaviour is that the last message (error or warning, verbose and debug
1061 * messages are never stored) is always stored and can be accessed using ly_errmsg(). On error,
1062 * #ly_errno is set. If that was a validation error (#ly_errno is set to #LY_EVALID),
1063 * also validation error code (via ly_vecode()) and path to the error node (via ly_errpath()) are available.
Radek Krejcib50551c2016-04-19 09:15:38 +02001064 *
1065 * For some specific cases, a YANG schema can define error message and/or error tag (mainly for
Michal Vaskoebea7012016-04-19 14:15:22 +02001066 * use in NETCONF). If a message is set, it is provided via ly_errmsg(). If a tag is set in schema,
Michal Vaskoe378c0e2017-10-31 13:29:53 +01001067 * it is available via ly_errapptag() (if not set, the returned string is empty).
Radek Krejcib50551c2016-04-19 09:15:38 +02001068 *
Michal Vaskoebea7012016-04-19 14:15:22 +02001069 * By default, all libyang messages are printed to `stderr`. However, the caller is able to set their own logging
Radek Krejcib50551c2016-04-19 09:15:38 +02001070 * callback function. In that case, instead of printing messages, libyang passes error level, message and path
1071 * (if any) to the caller's callback function. In case of error level, the message and path are still
1072 * automatically stored and available via the functions and macros described above.
Radek Krejci26715a42015-07-29 14:10:45 +02001073 *
Michal Vasko53b7da02018-02-13 15:28:42 +01001074 * This is the basic way of working with errors but another, more sophisticated is also available. With ly_log_options()
1075 * you can modify what is done with all the messages. Default flags are #LY_LOLOG and #LY_LOSTORE_LAST so that messages
1076 * are logged and the last one is stored. If you set the flag #LY_LOSTORE, all the messages will be stored. Be careful
Michal Vasko10d6f2f2018-02-14 10:56:01 +01001077 * because unless you regularly clean them, the error list will grow indefinitely. With ly_err_first() you can retrieve
1078 * the first generated error structure ly_err_item. It is a linked-list so you can get next errors using the **next** pointer.
Michal Vasko20fadd52018-02-14 09:33:22 +01001079 * Being processed (for instance printed with ly_err_print()), you can then free them with ly_err_clean().
Michal Vasko53b7da02018-02-13 15:28:42 +01001080 *
Radek Krejcidef50022016-02-01 16:38:32 +01001081 * \note API for this group of functions is described in the [logger module](@ref logger).
1082 *
1083 * Functions List
1084 * --------------
1085 * - ly_verb()
1086 * - ly_set_log_clb()
1087 * - ly_get_log_clb()
Michal Vasko53b7da02018-02-13 15:28:42 +01001088 * - ly_log_options()
1089 * - #ly_errno
1090 * - ly_vecode()
Radek Krejcib50551c2016-04-19 09:15:38 +02001091 * - ly_errmsg()
1092 * - ly_errpath()
1093 * - ly_errapptag()
Michal Vasko10d6f2f2018-02-14 10:56:01 +01001094 * - ly_err_first()
Michal Vasko20fadd52018-02-14 09:33:22 +01001095 * - ly_err_print()
Michal Vasko53b7da02018-02-13 15:28:42 +01001096 * - ly_err_clean()
Radek Krejci26715a42015-07-29 14:10:45 +02001097 */
1098
1099/**
1100 * @defgroup context Context
Radek Krejci3045cf32015-05-28 10:58:52 +02001101 * @{
1102 *
Radek Krejci26715a42015-07-29 14:10:45 +02001103 * Structures and functions to manipulate with the libyang "containers". The \em context concept allows callers
1104 * to work in environments with different sets of YANG schemas. More detailed information can be found at
1105 * @ref howtocontext page.
Radek Krejci3045cf32015-05-28 10:58:52 +02001106 */
1107
1108/**
Radek Krejcid6a3b4a2018-08-22 08:31:49 +02001109 * @struct ly_ctx
Radek Krejcida04f4a2015-05-21 12:54:09 +02001110 * @brief libyang context handler.
1111 */
1112struct ly_ctx;
1113
1114/**
Radek Krejcidd3263a2017-07-15 11:50:09 +02001115 * @defgroup contextoptions Context options
1116 * @ingroup context
1117 *
1118 * Options to change context behavior.
Radek Krejci84fa2b22018-08-27 10:08:21 +02001119 * @{
Radek Krejcidd3263a2017-07-15 11:50:09 +02001120 */
1121
1122#define LY_CTX_ALLIMPLEMENTED 0x01 /**< All the imports of the schema being parsed are treated implemented. */
1123#define LY_CTX_TRUSTED 0x02 /**< Handle the schema being parsed as trusted and skip its validation
1124 tests. Note that while this option improves performance, it can
1125 lead to an undefined behavior if the schema is not correct. */
Radek Krejcidfb00d62017-09-06 09:39:35 +02001126#define LY_CTX_NOYANGLIBRARY 0x04 /**< Do not internally implement ietf-yang-library module. The option
1127 causes that function ly_ctx_info() does not work (returns NULL) until
1128 the ietf-yang-library module is loaded manually. While any revision
1129 of this schema can be loaded with this option, note that the only
Michal Vaskoeb3bd0e2018-01-26 11:52:11 +01001130 revisions implemented by ly_ctx_info() are 2016-04-09 and 2018-01-17.
Radek Krejcidfb00d62017-09-06 09:39:35 +02001131 This option cannot be used with ly_ctx_new_yl*() functions. */
Radek Krejci38f8cea2018-08-17 09:32:07 +02001132#define LY_CTX_DISABLE_SEARCHDIRS 0x08 /**< Do not search for schemas in context's searchdirs neither in current
Radek Krejcib2848032018-08-15 15:16:28 +02001133 working directory. It is entirely skipped and the only way to get
1134 schema data for imports or for ly_ctx_load_module() is to use the
1135 callbacks provided by caller via ly_ctx_set_module_imp_clb() */
Radek Krejcicf3b7ca2018-08-17 09:55:06 +02001136#define LY_CTX_DISABLE_SEARCHDIR_CWD 0x10 /**< Do not automatically search for schemas in current working
1137 directory, which is by default searched automatically (despite not
1138 recursively). */
Radek Krejcidd3263a2017-07-15 11:50:09 +02001139/**@} contextoptions */
1140
1141/**
Radek Krejcida04f4a2015-05-21 12:54:09 +02001142 * @brief Create libyang context
1143 *
Radek Krejci26715a42015-07-29 14:10:45 +02001144 * Context is used to hold all information about schemas. Usually, the application is supposed
Radek Krejci91b833c2015-09-04 11:49:43 +02001145 * to work with a single context in which libyang is holding all schemas (and other internal
1146 * information) according to which the data trees will be processed and validated. So, the schema
1147 * trees are tightly connected with the specific context and they are held by the context internally
1148 * - caller does not need to keep pointers to the schemas returned by lys_parse(), context knows
1149 * about them. The data trees created with lyd_parse() are still connected with the specific context,
1150 * but they are not internally held by the context. The data tree just points and lean on some data
1151 * held by the context (schema tree, string dictionary, etc.). Therefore, in case of data trees, caller
1152 * is supposed to keep pointers returned by the lyd_parse() and manage the data tree on its own. This
1153 * also affects the number of instances of both tree types. While you can have only one instance of
1154 * specific schema connected with a single context, number of data tree instances is not connected.
Radek Krejcida04f4a2015-05-21 12:54:09 +02001155 *
Radek Krejci26715a42015-07-29 14:10:45 +02001156 * @param[in] search_dir Directory where libyang will search for the imported or included modules
1157 * and submodules. If no such directory is available, NULL is accepted.
Radek Krejcidd3263a2017-07-15 11:50:09 +02001158 * @param[in] options Context options, see @ref contextoptions.
Radek Krejcida04f4a2015-05-21 12:54:09 +02001159 *
Radek Krejci3045cf32015-05-28 10:58:52 +02001160 * @return Pointer to the created libyang context, NULL in case of error.
Radek Krejcida04f4a2015-05-21 12:54:09 +02001161 */
Radek Krejcidd3263a2017-07-15 11:50:09 +02001162struct ly_ctx *ly_ctx_new(const char *search_dir, int options);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001163
1164/**
Radek Krejci69333c92017-03-17 16:14:43 +01001165 * @brief Create libyang context according to the content of the given yang-library data.
1166 *
1167 * This function loads the yang-library data from the file at the given path. If you need
1168 * to pass the data as string, use ly_ctx_new_ylmem(). Both functions extend functionality of
1169 * ly_ctx_new() by loading modules specified in the ietf-yang-library form into the context
1170 * being created.
1171 *
Michal Vaskoeb3bd0e2018-01-26 11:52:11 +01001172 * The preferred tree model revision is 2018-01-17. However, only the first module-set is processed
1173 * and loaded into the context. If there are no matching nodes from this tree, the legacy tree
1174 * (originally from model revision 2016-04-09) is processed.
1175 *
Radek Krejci69333c92017-03-17 16:14:43 +01001176 * Note, that the modules are loaded the same way as in case of ly_ctx_load_module(), so the schema
1177 * paths in the yang-library data are ignored and the modules are loaded from the context's search
1178 * locations. On the other hand, YANG features of the modules are set as specified in the yang-library
1179 * data.
1180 *
1181 * To get yang library data from a libyang context, use ly_ctx_info().
1182 *
1183 * @param[in] search_dir Directory where libyang will search for the imported or included modules
1184 * and submodules. If no such directory is available, NULL is accepted.
1185 * @param[in] path Path to the file containing yang-library data in the specified \p format.
1186 * @param[in] format Format of the data in the provided file.
Radek Krejcidd3263a2017-07-15 11:50:09 +02001187 * @param[in] options Context options, see @ref contextoptions.
Radek Krejci69333c92017-03-17 16:14:43 +01001188 * @return Pointer to the created libyang context, NULL in case of error.
1189 */
Radek Krejcidd3263a2017-07-15 11:50:09 +02001190struct ly_ctx *ly_ctx_new_ylpath(const char *search_dir, const char *path, LYD_FORMAT format, int options);
Radek Krejci69333c92017-03-17 16:14:43 +01001191
1192/**
1193 * @brief Create libyang context according to the content of the given yang-library data.
1194 *
1195 * This function loads the yang-library data from the given string. If you need to pass the data
1196 * as path to a file holding the data, use ly_ctx_new_ylpath(). Both functions extend functionality of
1197 * ly_ctx_new() by loading modules specified in the ietf-yang-library form into the context
1198 * being created.
1199 *
Michal Vaskoeb3bd0e2018-01-26 11:52:11 +01001200 * The preferred tree model revision is 2018-01-17. However, only the first module-set is processed
1201 * and loaded into the context. If there are no matching nodes from this tree, the legacy tree
1202 * (originally from model revision 2016-04-09) is processed.
1203 *
Radek Krejci69333c92017-03-17 16:14:43 +01001204 * Note, that the modules are loaded the same way as in case of ly_ctx_load_module(), so the schema
1205 * paths in the yang-library data are ignored and the modules are loaded from the context's search
1206 * locations. On the other hand, YANG features of the modules are set as specified in the yang-library
1207 * data.
1208 *
1209 * To get yang library data from a libyang context, use ly_ctx_info().
1210 *
1211 * @param[in] search_dir Directory where libyang will search for the imported or included modules
1212 * and submodules. If no such directory is available, NULL is accepted.
1213 * @param[in] data String containing yang-library data in the specified \p format.
1214 * @param[in] format Format of the data in the provided file.
Radek Krejcidd3263a2017-07-15 11:50:09 +02001215 * @param[in] options Context options, see @ref contextoptions.
Radek Krejci69333c92017-03-17 16:14:43 +01001216 * @return Pointer to the created libyang context, NULL in case of error.
1217 */
Radek Krejcidd3263a2017-07-15 11:50:09 +02001218struct ly_ctx *ly_ctx_new_ylmem(const char *search_dir, const char *data, LYD_FORMAT format, int options);
Radek Krejci69333c92017-03-17 16:14:43 +01001219
1220/**
Radek Krejcidfb00d62017-09-06 09:39:35 +02001221 * @brief Number of internal modules, which are in the context and cannot be removed nor disabled.
1222 * @param[in] ctx Context to investigate.
1223 * @return Number of internal modules, 0 in case of invalid parameter.
1224 */
1225unsigned int ly_ctx_internal_modules_count(struct ly_ctx *ctx);
Radek Krejci0ec51da2016-12-14 16:42:03 +01001226
Michal Vaskod7957c02016-04-01 10:27:26 +02001227/**
Radek Krejcida9f8392017-03-25 19:40:56 -05001228 * @brief Add the search path into libyang context
1229 *
1230 * To reset search paths set in the context, use ly_ctx_unset_searchdirs() and then
1231 * set search paths again.
Radek Krejci7ab25152015-08-07 14:48:45 +02001232 *
1233 * @param[in] ctx Context to be modified.
Radek Krejcida9f8392017-03-25 19:40:56 -05001234 * @param[in] search_dir New search path to add to the current paths previously set in ctx.
Igor Ternovsky6e6543d2017-09-12 10:37:28 +10001235 * @return EXIT_SUCCESS, EXIT_FAILURE.
Radek Krejci7ab25152015-08-07 14:48:45 +02001236 */
Igor Ternovsky6e6543d2017-09-12 10:37:28 +10001237int ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir);
Radek Krejci7ab25152015-08-07 14:48:45 +02001238
1239/**
Radek Krejcidb4e9ff2017-06-13 16:26:14 +02001240 * @brief Clean the search path(s) from the libyang context
Radek Krejcida9f8392017-03-25 19:40:56 -05001241 *
1242 * @param[in] ctx Context to be modified.
Radek Krejcidb4e9ff2017-06-13 16:26:14 +02001243 * @param[in] index Index of the search path to be removed, use negative value to remove them all.
1244 * Correct index value can be checked via ly_ctx_get_searchdirs().
Radek Krejcida9f8392017-03-25 19:40:56 -05001245 */
Radek Krejcidb4e9ff2017-06-13 16:26:14 +02001246void ly_ctx_unset_searchdirs(struct ly_ctx *ctx, int index);
Radek Krejcida9f8392017-03-25 19:40:56 -05001247
1248/**
Radek Krejci426ea2b2017-06-13 12:41:51 +02001249 * @brief Get the NULL-terminated list of the search paths in libyang context.
Radek Krejci7ab25152015-08-07 14:48:45 +02001250 *
Radek Krejci96a10da2015-07-30 11:00:14 +02001251 * @param[in] ctx Context to query.
Radek Krejci426ea2b2017-06-13 12:41:51 +02001252 * @return NULL-terminated list (array) of the search paths, NULL if no searchpath was set.
Radek Krejci96a10da2015-07-30 11:00:14 +02001253 */
Radek Krejci426ea2b2017-06-13 12:41:51 +02001254const char * const *ly_ctx_get_searchdirs(const struct ly_ctx *ctx);
Radek Krejci96a10da2015-07-30 11:00:14 +02001255
1256/**
Radek Krejci4fb7d7d2018-08-17 10:08:59 +02001257 * @brief Get the currently set context's options.
1258 *
1259 * @param[in] ctx Context to query.
1260 * @return Combination of all the currently set context's options, see @ref contextoptions.
1261 */
1262int ly_ctx_get_options(struct ly_ctx *ctx);
1263
1264/**
Radek Krejci88b78422018-08-16 16:07:34 +02001265 * @brief Make context to stop searching for schemas (imported, included or requested via ly_ctx_load_module())
Radek Krejci38f8cea2018-08-17 09:32:07 +02001266 * in searchdirs set via ly_ctx_set_searchdir() functions. Searchdirs are still stored in the context, so by
Radek Krejci88b78422018-08-16 16:07:34 +02001267 * unsetting the option by ly_ctx_unset_disable_searchdirs() searching in all previously searchdirs is restored.
1268 *
Radek Krejci38f8cea2018-08-17 09:32:07 +02001269 * The same effect is achieved by using #LY_CTX_DISABLE_SEARCHDIRS option when creating new context or parsing
Radek Krejci88b78422018-08-16 16:07:34 +02001270 * a specific schema.
1271 *
1272 * @param[in] ctx Context to be modified.
1273 */
1274void ly_ctx_set_disable_searchdirs(struct ly_ctx *ctx);
1275
1276/**
1277 * @brief Reverse function to ly_ctx_set_disable_searchdirs().
1278 *
1279 * @param[in] ctx Context to be modified.
1280 */
1281void ly_ctx_unset_disable_searchdirs(struct ly_ctx *ctx);
1282
1283/**
Radek Krejcicf3b7ca2018-08-17 09:55:06 +02001284 * @brief Make context to stop implicitly searching for schemas (imported, included or requested via ly_ctx_load_module())
1285 * in current working directory. This flag can be unset by ly_ctx_unset_disable_searchdir_cwd().
1286 *
1287 * The same effect is achieved by using #LY_CTX_DISABLE_SEARCHDIR_CWD option when creating new context or parsing
1288 * a specific schema.
1289 *
1290 * @param[in] ctx Context to be modified.
1291 */
1292void ly_ctx_set_disable_searchdir_cwd(struct ly_ctx *ctx);
1293
1294/**
1295 * @brief Reverse function to ly_ctx_set_disable_searchdir_cwd().
1296 *
1297 * @param[in] ctx Context to be modified.
1298 */
1299void ly_ctx_unset_disable_searchdir_cwd(struct ly_ctx *ctx);
1300
1301/**
Radek Krejcid9ba3e32015-07-30 15:08:18 +02001302 * @brief Make context to set all the imported modules to be implemented. By default,
Radek Krejci96a10da2015-07-30 11:00:14 +02001303 * if the imported module is not used in leafref's path, augment or deviation, it is
Michal Vasko1e62a092015-12-01 12:27:20 +01001304 * imported and its data tree is not taken into account.
Radek Krejci96a10da2015-07-30 11:00:14 +02001305 *
Radek Krejcidd3263a2017-07-15 11:50:09 +02001306 * The same effect is achieved by using #LY_CTX_ALLIMPLEMENTED option when creating new context or parsing
1307 * a specific schema.
1308 *
Radek Krejci96a10da2015-07-30 11:00:14 +02001309 * Note, that function does not make the currently loaded modules, it just change the
Michal Vaskod7957c02016-04-01 10:27:26 +02001310 * schema parser behavior for the future parsing. This flag can be unset by ly_ctx_unset_allimplemented().
1311 *
1312 * @param[in] ctx Context to be modified.
1313 */
1314void ly_ctx_set_allimplemented(struct ly_ctx *ctx);
1315
1316/**
1317 * @brief Reverse function to ly_ctx_set_allimplemented().
1318 *
Radek Krejci0ec51da2016-12-14 16:42:03 +01001319 * @param[in] ctx Context to be modified.
1320 */
1321void ly_ctx_unset_allimplemented(struct ly_ctx *ctx);
1322
1323/**
Radek Krejcidd3263a2017-07-15 11:50:09 +02001324 * @brief Change the schema parser behavior when parsing new schemas forcing it to skip some of the schema
1325 * validation checks to improve performance. Note that parsing invalid schemas this way may lead to an
1326 * undefined behavior later, e.g. when working with data trees.
1327 *
1328 * The same effect is achieved by using #LY_CTX_TRUSTED option when creating new context or parsing
1329 * a specific schema.
1330 *
1331 * This flag can be unset by ly_ctx_unset_trusted().
1332 *
1333 * @param[in] ctx Context to be modified.
1334 */
1335void ly_ctx_set_trusted(struct ly_ctx *ctx);
1336
1337/**
1338 * @brief Reverse function to ly_ctx_set_trusted().
1339 *
1340 * @param[in] ctx Context to be modified.
1341 */
1342void ly_ctx_unset_trusted(struct ly_ctx *ctx);
1343
1344/**
Radek Krejci14116592018-05-16 12:26:06 +02001345 * @brief Get current ID of the modules set. The value is available also
1346 * as module-set-id in ly_ctx_info() result.
1347 *
1348 * @param[in] ctx Context to be examined.
1349 * @return Numeric identifier of the current context's modules set.
1350 */
1351uint16_t ly_ctx_get_module_set_id(const struct ly_ctx *ctx);
1352
1353/**
Radek Krejci96a10da2015-07-30 11:00:14 +02001354 * @brief Get data of an internal ietf-yang-library module.
1355 *
1356 * @param[in] ctx Context with the modules.
1357 * @return Root data node corresponding to the model, NULL on error.
1358 * Caller is responsible for freeing the returned data tree using lyd_free().
1359 */
1360struct lyd_node *ly_ctx_info(struct ly_ctx *ctx);
1361
1362/**
1363 * @brief Iterate over all (enabled) modules in a context.
1364 *
1365 * @param[in] ctx Context with the modules.
1366 * @param[in,out] idx Index of the next module to be returned. Value of 0 starts from the beginning.
1367 * @return Next context module, NULL if the last was already returned.
Radek Krejcid9ba3e32015-07-30 15:08:18 +02001368 */
Radek Krejci96a10da2015-07-30 11:00:14 +02001369const struct lys_module *ly_ctx_get_module_iter(const struct ly_ctx *ctx, uint32_t *idx);
Michal Vasko1e62a092015-12-01 12:27:20 +01001370
Radek Krejci96a10da2015-07-30 11:00:14 +02001371/**
Radek Krejci0ec51da2016-12-14 16:42:03 +01001372 * @brief Iterate over the disabled modules in a context.
1373 *
1374 * @param[in] ctx Context with the modules.
1375 * @param[in,out] idx Index of the next module to be returned. Value of 0 starts from the beginning.
1376 * @return Next context module, NULL if the last was already returned.
1377 */
1378const struct lys_module *ly_ctx_get_disabled_module_iter(const struct ly_ctx *ctx, uint32_t *idx);
1379
1380/**
Radek Krejcifd4e6e32015-08-10 15:00:51 +02001381 * @brief Get pointer to the schema tree of the module of the specified name.
Radek Krejcida04f4a2015-05-21 12:54:09 +02001382 *
Radek Krejcida04f4a2015-05-21 12:54:09 +02001383 * @param[in] ctx Context to work in.
1384 * @param[in] name Name of the YANG module to get.
Radek Krejcif647e612015-07-30 11:36:07 +02001385 * @param[in] revision Optional revision date of the YANG module to get. If not specified,
Radek Krejcidfb00d62017-09-06 09:39:35 +02001386 * the schema in the newest/implemented revision (see \p implemented parameter) is returned if any.
1387 * @param[in] implemented In case the revision is not specified, require the implemented module
1388 * instead of the newest revision of the module.
Radek Krejcif647e612015-07-30 11:36:07 +02001389 * @return Pointer to the data model structure, NULL if no schema following the name and
Radek Krejcifd4e6e32015-08-10 15:00:51 +02001390 * revision requirements is present in the context.
Radek Krejcida04f4a2015-05-21 12:54:09 +02001391 */
Radek Krejcidfb00d62017-09-06 09:39:35 +02001392const struct lys_module *ly_ctx_get_module(const struct ly_ctx *ctx, const char *name, const char *revision, int implemented);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001393
1394/**
Radek Krejci21601a32016-03-07 11:39:27 +01001395 * @brief Get pointer to the older schema tree to the specified one in the provided context.
1396 *
1397 * The module is not necessarily from the provided \p ctx. If there are multiple schemas older than the
1398 * provided one, the newest of them is returned.
1399 *
1400 * The function can be used in combination with ly_ctx_get_module() to get all revisions of a module in a context:
1401 * \code{.c}
1402 * for (mod = ly_ctx_get_module(ctx, name, NULL); mod; mod = ly_ctx_get_module_older(ctx, mod)) {
1403 * ...
1404 * }
1405 * \endcode
1406 *
1407 * @param[in] ctx Context to work in.
1408 * @param[in] module YANG module to compare with
1409 * @return Pointer to the data model structure, NULL if no older schema is present in the context.
1410 */
1411const struct lys_module *ly_ctx_get_module_older(const struct ly_ctx *ctx, const struct lys_module *module);
1412
1413/**
Michal Vasko99b0aad2015-12-01 12:28:51 +01001414 * @brief Try to find the model in the searchpath of \p ctx and load it into it. If custom missing
1415 * module callback is set, it is used instead.
Michal Vasko82465962015-11-10 11:03:11 +01001416 *
Radek Krejci31fb8be2016-06-23 15:26:26 +02001417 * If there is a possibility that the requested module is already in the context, you should call
1418 * the ly_ctx_get_module() first to avoid a lot of work performed by ly_ctx_load_module().
1419 *
Michal Vasko82465962015-11-10 11:03:11 +01001420 * @param[in] ctx Context to add to.
Michal Vasko82465962015-11-10 11:03:11 +01001421 * @param[in] name Name of the module to load.
1422 * @param[in] revision Optional revision date of the module. If not specified, it is
1423 * assumed that there is only one model revision in the searchpath (the first matching file
1424 * is parsed).
1425 * @return Pointer to the data model structure, NULL if not found or some error occured.
1426 */
Michal Vasko99b0aad2015-12-01 12:28:51 +01001427const struct lys_module *ly_ctx_load_module(struct ly_ctx *ctx, const char *name, const char *revision);
1428
1429/**
1430 * @brief Callback for retrieving missing included or imported models in a custom way.
1431 *
Michal Vasko84475152016-07-25 16:16:25 +02001432 * @param[in] mod_name Missing module name.
1433 * @param[in] mod_rev Optional missing module revision.
1434 * @param[in] submod_name Optional missing submodule name.
1435 * @param[in] submod_rev Optional missing submodule revision.
Michal Vasko99b0aad2015-12-01 12:28:51 +01001436 * @param[in] user_data User-supplied callback data.
1437 * @param[out] format Format of the returned module data.
Michal Vasko880dceb2016-03-03 15:44:56 +01001438 * @param[out] free_module_data Callback for freeing the returned module data. If not set, the data will be left untouched.
Michal Vasko8f3160e2017-09-27 11:25:26 +02001439 * @return Requested module data, NULL if the module is supposed to be loaded
1440 * using standard mechanisms (searched for in the filesystem), NULL and #ly_errno set if
1441 * the callback failed resulting in the module failing to load.
Michal Vasko99b0aad2015-12-01 12:28:51 +01001442 */
Michal Vaskof53187d2017-01-13 13:23:14 +01001443typedef char *(*ly_module_imp_clb)(const char *mod_name, const char *mod_rev, const char *submod_name, const char *sub_rev,
1444 void *user_data, LYS_INFORMAT *format, void (**free_module_data)(void *model_data));
Michal Vasko99b0aad2015-12-01 12:28:51 +01001445
1446/**
Michal Vasko98b5f952016-10-26 12:14:40 +02001447 * @brief Set missing include or import module callback. It is meant to be used when the models
1448 * are not locally available (such as when downloading modules from a NETCONF server), it should
1449 * not be required in other cases.
Michal Vasko99b0aad2015-12-01 12:28:51 +01001450 *
1451 * @param[in] ctx Context that will use this callback.
Michal Vaskof53187d2017-01-13 13:23:14 +01001452 * @param[in] clb Callback responsible for returning the missing model.
Michal Vasko99b0aad2015-12-01 12:28:51 +01001453 * @param[in] user_data Arbitrary data that will always be passed to the callback \p clb.
1454 */
Michal Vaskof53187d2017-01-13 13:23:14 +01001455void ly_ctx_set_module_imp_clb(struct ly_ctx *ctx, ly_module_imp_clb clb, void *user_data);
Michal Vasko99b0aad2015-12-01 12:28:51 +01001456
1457/**
Michal Vaskof53187d2017-01-13 13:23:14 +01001458 * @brief Get the custom callback for missing import/include module retrieval.
1459 *
1460 * @param[in] ctx Context to read from.
1461 * @param[in] user_data Optional pointer for getting the user-supplied callback data.
1462 * @return Callback or NULL if not set.
1463 */
1464ly_module_imp_clb ly_ctx_get_module_imp_clb(const struct ly_ctx *ctx, void **user_data);
1465
1466/**
1467 * @brief Callback for retrieving missing modules in the context, for which some data was found.
1468 *
1469 * Either \p name or \p ns is ALWAYS set, but both can also be set.
1470 *
1471 * @param[in,out] ctx Context with the missing module.
1472 * @param[in] name Missing module name.
1473 * @param[in] ns Missing module namespace.
1474 * @param[in] options Bitmask of LY_MODCLB_* values or 0.
1475 * @param[in] user_data User-supplied callback data.
1476 * @return Newly added or modified module, NULL on failure.
1477 */
1478typedef const struct lys_module *(*ly_module_data_clb)(struct ly_ctx *ctx, const char *name, const char *ns,
1479 int options, void *user_data);
1480
1481/* Module is in the context, but is not implemented, so to continue with the data operation, it must be implemented. */
1482#define LY_MODCLB_NOT_IMPLEMENTED 0x01
1483
1484/**
1485 * @brief Set the missing data module callback. It will be called when some data is parsed or searched for and their module
1486 * is not found in the context or is not implemented.
1487 *
1488 * @param[in] ctx Context that will use this callback.
1489 * @param[in] clb Callback responsible for returning the missing model.
1490 * @param[in] user_data Arbitrary data that will always be passed to the callback \p clb.
1491 */
1492void ly_ctx_set_module_data_clb(struct ly_ctx *ctx, ly_module_data_clb clb, void *user_data);
1493
1494/**
1495 * @brief Get the missing data module calback.
Michal Vasko99b0aad2015-12-01 12:28:51 +01001496 *
1497 * @param[in] ctx Context to read from.
1498 * @param[in] user_data Optional pointer for getting the user-supplied callbck data.
Michal Vaskof53187d2017-01-13 13:23:14 +01001499 * @return Callback or NULL if not set.
Michal Vasko99b0aad2015-12-01 12:28:51 +01001500 */
Michal Vaskof53187d2017-01-13 13:23:14 +01001501ly_module_data_clb ly_ctx_get_module_data_clb(const struct ly_ctx *ctx, void **user_data);
Michal Vasko82465962015-11-10 11:03:11 +01001502
Michal Vaskoe385e902018-07-02 10:15:11 +02001503#ifdef LY_ENABLED_LYD_PRIV
1504
1505void ly_ctx_set_priv_dup_clb(struct ly_ctx *ctx, void *(*priv_dup_clb)(const void *priv));
1506
1507#endif
1508
Michal Vasko82465962015-11-10 11:03:11 +01001509/**
Radek Krejcifd4e6e32015-08-10 15:00:51 +02001510 * @brief Get pointer to the schema tree of the module of the specified namespace
1511 *
1512 * @param[in] ctx Context to work in.
1513 * @param[in] ns Namespace of the YANG module to get.
1514 * @param[in] revision Optional revision date of the YANG module to get. If not specified,
Radek Krejcidfb00d62017-09-06 09:39:35 +02001515 * the schema in the newest/implemented revision (see \p implemented parameter) is returned if any.
1516 * @param[in] implemented In case the revision is not specified, require the implemented module
1517 * instead of the newest revision of the module.
Radek Krejcifd4e6e32015-08-10 15:00:51 +02001518 * @return Pointer to the data model structure, NULL if no schema following the namespace and
1519 * revision requirements is present in the context.
1520 */
Radek Krejcidfb00d62017-09-06 09:39:35 +02001521const struct lys_module *ly_ctx_get_module_by_ns(const struct ly_ctx *ctx, const char *ns, const char *revision, int implemented);
Radek Krejcifd4e6e32015-08-10 15:00:51 +02001522
1523/**
Radek Krejci62f0da72016-03-07 11:35:43 +01001524 * @brief Get submodule of a main module.
1525 *
1526 * If you already have the pointer to the submodule's main module, use ly_ctx_get_submodule2() instead.
Michal Vasko7bf06882015-07-03 15:33:56 +02001527 *
Radek Krejcia7533f22016-03-07 07:37:45 +01001528 * @param[in] ctx Context to work in.
Michal Vaskof6d94c62016-04-05 11:21:54 +02001529 * @param[in] module Name of the main (belongs-to) module. If NULL, all module submodules are searched.
1530 * @param[in] revision Optional revision date of \p module. If NULL, all revisions of \p module
1531 * are searched. If set, \p module must also be set.
Radek Krejcia7533f22016-03-07 07:37:45 +01001532 * @param[in] submodule Name of the submodule to get.
Michal Vaskof6d94c62016-04-05 11:21:54 +02001533 * @param[in] sub_revision Optional revision date of \p submodule. If NULL, the newest revision of \p submodule
1534 * is returned.
Michal Vasko7bf06882015-07-03 15:33:56 +02001535 * @return Pointer to the data model structure.
1536 */
Radek Krejcia7533f22016-03-07 07:37:45 +01001537const struct lys_submodule *ly_ctx_get_submodule(const struct ly_ctx *ctx, const char *module, const char *revision,
Michal Vaskof6d94c62016-04-05 11:21:54 +02001538 const char *submodule, const char *sub_revision);
Michal Vasko7bf06882015-07-03 15:33:56 +02001539
1540/**
Radek Krejci62f0da72016-03-07 11:35:43 +01001541 * @brief Get submodule of a main module.
1542 *
1543 * If you have only the name (and optionally revision) of the submodule's main module, use ly_ctx_get_submodule()
1544 * instead.
1545 *
1546 * @param[in] main_module Main module (belongs to) of the searched submodule.
1547 * @param[in] submodule Name of the submodule to get.
1548 * @return Pointer to the data model structure.
1549 */
1550const struct lys_submodule *ly_ctx_get_submodule2(const struct lys_module *main_module, const char *submodule);
1551
1552/**
Michal Vaskob3744402017-08-03 14:23:58 +02001553 * @brief Get schema node according to the given data path (JSON format, see @ref howtoxpath).
1554 *
Michal Vaskod245e0b2018-01-22 15:39:32 +01001555 * This function is meant as a utility function to ease getting a specific schema node
1556 * directly from context having \p data_path a const immutable string. Generally,
1557 * it is better to use lyd_find_path(), lys_find_path(), or ly_ctx_find_path(). Also,
1558 * it is possible to transform data paths into schema paths using ly_path_data2schema().
Michal Vaskob3744402017-08-03 14:23:58 +02001559 *
Michal Vaskob3a6e482017-09-14 13:50:28 +02001560 * @param[in] ctx Context to work in. Must be set if \p start is NULL.
Michal Vaskod245e0b2018-01-22 15:39:32 +01001561 * @param[in] start Starting node for a relative data node identifier, in which
Michal Vaskob3a6e482017-09-14 13:50:28 +02001562 * case it is mandatory and \p ctx can be NULL.
Michal Vaskod245e0b2018-01-22 15:39:32 +01001563 * @param[in] data_path JSON data path of the node to get.
Michal Vaskob3744402017-08-03 14:23:58 +02001564 * @param[in] output Search operation output instead input.
1565 * @return Resolved schema node or NULL.
1566 */
1567const struct lys_node *ly_ctx_get_node(struct ly_ctx *ctx, const struct lys_node *start, const char *data_path, int output);
1568
1569/**
Radek Krejci749ebf52018-01-22 11:40:36 +01001570 * @brief Get schema node according to the given schema path (see @ref howtoxpath).
1571 *
1572 * Note that the given path must be absolute and fully prefixed (which is the default output
1573 * of lys_path() function). To get node specified by a relative path, use lys_find_path() instead.
1574 *
1575 * @param[in] ctx Context to work in.
1576 * @param[in] path Schema path of the node to find.
Radek Krejcic683acd2018-01-22 14:51:52 +01001577 * @return Set of found schema nodes. If no nodes are matching \p path the returned set is empty.
1578 * In case of an error, NULL is returned.
Radek Krejci749ebf52018-01-22 11:40:36 +01001579 */
Radek Krejcic683acd2018-01-22 14:51:52 +01001580struct ly_set *ly_ctx_find_path(struct ly_ctx *ctx, const char *path);
Radek Krejci749ebf52018-01-22 11:40:36 +01001581
1582/**
Radek Krejci0ec51da2016-12-14 16:42:03 +01001583 * @brief Remove the specified module from its context.
Radek Krejci8c107fe2016-10-17 16:00:18 +02001584 *
Radek Krejci0ec51da2016-12-14 16:42:03 +01001585 * Beside the selected module, also all other modules depending on all the modules being removed
1586 * will be removed as well.
Radek Krejci8c107fe2016-10-17 16:00:18 +02001587 *
1588 * libyang internal modules (those present when the context is created) cannot be removed.
1589 *
Radek Krejci0ec51da2016-12-14 16:42:03 +01001590 * @param[in] module Module to be removed.
Radek Krejci8c107fe2016-10-17 16:00:18 +02001591 * @param[in] private_destructor Optional destructor function for private objects assigned
1592 * to the nodes via lys_set_private(). If NULL, the private objects are not freed by libyang.
Radek Krejci0ec51da2016-12-14 16:42:03 +01001593 * @return EXIT_SUCCESS or EXIT_FAILURE (in case of invalid parameter).
Radek Krejci8c107fe2016-10-17 16:00:18 +02001594 */
Radek Krejci0ec51da2016-12-14 16:42:03 +01001595int ly_ctx_remove_module(const struct lys_module *module,
Radek Krejci8c107fe2016-10-17 16:00:18 +02001596 void (*private_destructor)(const struct lys_node *node, void *priv));
1597
1598/**
1599 * @brief Remove all the modules from the context except the internal modules. Also the addition data in
1600 * dictionary are kept.
1601 *
1602 * @param[in] ctx Context to work in.
1603 * @param[in] private_destructor Optional destructor function for private objects assigned
1604 * to the nodes via lys_set_private(). If NULL, the private objects are not freed by libyang.
1605 */
1606void ly_ctx_clean(struct ly_ctx *ctx, void (*private_destructor)(const struct lys_node *node, void *priv));
1607
1608/**
Radek Krejci3045cf32015-05-28 10:58:52 +02001609 * @brief Free all internal structures of the specified context.
1610 *
1611 * The function should be used before terminating the application to destroy
1612 * and free all structures internally used by libyang. If the caller uses
1613 * multiple contexts, the function should be called for each used context.
1614 *
1615 * All instance data are supposed to be freed before destroying the context.
1616 * Data models are destroyed automatically as part of ly_ctx_destroy() call.
1617 *
1618 * @param[in] ctx libyang context to destroy
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001619 * @param[in] private_destructor Optional destructor function for private objects assigned
1620 * to the nodes via lys_set_private(). If NULL, the private objects are not freed by libyang.
Radek Krejci5138e9f2017-04-12 13:10:46 +02001621 * Remember the differences between the structures derived from ::lys_node and always check
1622 * ::lys_node#nodetype.
Radek Krejcida04f4a2015-05-21 12:54:09 +02001623 */
Radek Krejcifa0b5e02016-02-04 13:57:03 +01001624void ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lys_node *node, void *priv));
Radek Krejcida04f4a2015-05-21 12:54:09 +02001625
Michal Vasko40702732017-10-25 11:43:23 +02001626/**
Michal Vasko065c5652018-03-08 15:35:21 +01001627 * @brief Transform a data path in XML format (node prefixes are XML namespace prefixes of module namespaces)
1628 * to JSON format (node prefixes are module names directly).
1629 *
1630 * @param[in] ctx Context to work in.
1631 * @param[in] xml_path Path in XML format to transform.
1632 * @param[in] xml XML tree to be used for XML namespace prefix resolution.
1633 * @return Transformed JSON data path, needs to be freed. NULL on error.
1634 */
1635char *ly_path_xml2json(struct ly_ctx *ctx, const char *xml_path, struct lyxml_elem *xml);
1636
1637/**
Michal Vasko40702732017-10-25 11:43:23 +02001638 * @brief Transform a data path into schema path (see @ref howtoxpath).
1639 *
Michal Vaskod245e0b2018-01-22 15:39:32 +01001640 * Some XPath tokens such as "*" or "//" should not be used because they cannot be properly
1641 * transformed into schema path. If used, any following nodes will not be transformed and simply copied,
1642 * so use at your own risk! It causes no problems if the path ends with the aforementioned tokens.
Michal Vasko40702732017-10-25 11:43:23 +02001643 *
1644 * @param[in] ctx Context to work in.
1645 * @param[in] data_path Data path to be transformed.
1646 * @return Created schema path, NULL on error.
1647 */
Michal Vasko53b7da02018-02-13 15:28:42 +01001648char *ly_path_data2schema(struct ly_ctx *ctx, const char *data_path);
Michal Vasko40702732017-10-25 11:43:23 +02001649
Michal Vasko065c5652018-03-08 15:35:21 +01001650/** @} context */
Radek Krejci26715a42015-07-29 14:10:45 +02001651
1652/**
Radek Krejcidef50022016-02-01 16:38:32 +01001653 * @defgroup nodeset Tree nodes set
Radek Krejcidc154432016-01-21 11:10:59 +01001654 * @ingroup datatree
1655 * @ingroup schematree
1656 * @{
1657 *
Radek Krejcidef50022016-02-01 16:38:32 +01001658 * Structure and functions to hold and manipulate with sets of nodes from schema or data trees.
1659 */
1660
1661/**
Radek Krejci8f08df12016-03-21 11:11:30 +01001662 * @brief set array of ::ly_set
1663 * It is kept in union to keep ::ly_set generic for data as well as schema trees
1664 */
1665union ly_set_set {
1666 struct lys_node **s; /**< array of pointers to a ::lys_node objects */
1667 struct lyd_node **d; /**< array of pointers to a ::lyd_node objects */
1668 void **g; /**< dummy array for generic work */
1669};
1670
1671/**
Radek Krejcidc154432016-01-21 11:10:59 +01001672 * @brief Structure to hold a set of (not necessary somehow connected) ::lyd_node or ::lys_node objects.
1673 * Caller is supposed to not mix the type of objects added to the set and according to its knowledge about
1674 * the set content, it is supposed to access the set via the sset, dset or set members of the structure.
1675 *
Radek Krejci09891a22016-06-10 10:59:22 +02001676 * Until ly_set_rm() or ly_set_rm_index() is used, the set keeps the order of the inserted items as they
1677 * were added into the set, so the first added item is on array index 0.
1678 *
Radek Krejcidef50022016-02-01 16:38:32 +01001679 * To free the structure, use ly_set_free() function, to manipulate with the structure, use other
1680 * ly_set_* functions.
Radek Krejcidc154432016-01-21 11:10:59 +01001681 */
1682struct ly_set {
1683 unsigned int size; /**< allocated size of the set array */
1684 unsigned int number; /**< number of elements in (used size of) the set array */
Radek Krejci8f08df12016-03-21 11:11:30 +01001685 union ly_set_set set; /**< set array - union to keep ::ly_set generic for data as well as schema trees */
Radek Krejcidc154432016-01-21 11:10:59 +01001686};
1687
1688/**
Radek Krejci09891a22016-06-10 10:59:22 +02001689 * @brief Option for ly_set_add() to allow duplicities in the ly_set structure so the
1690 * set is not used as a set, but as a list of (container for) items.
1691 */
1692#define LY_SET_OPT_USEASLIST 0x01
1693
1694/**
Radek Krejcidef50022016-02-01 16:38:32 +01001695 * @brief Create and initiate new ::ly_set structure.
Radek Krejcidc154432016-01-21 11:10:59 +01001696 *
Radek Krejcidef50022016-02-01 16:38:32 +01001697 * @return Created ::ly_set structure or NULL in case of error.
Radek Krejcidc154432016-01-21 11:10:59 +01001698 */
1699struct ly_set *ly_set_new(void);
1700
1701/**
Radek Krejcie8c1b572016-07-26 15:09:52 +02001702 * @brief Duplicate the existing set.
1703 *
1704 * @param[in] set Original set to duplicate
1705 * @return Duplication of the original set.
1706 */
1707struct ly_set *ly_set_dup(const struct ly_set *set);
1708
1709/**
Radek Krejcidc154432016-01-21 11:10:59 +01001710 * @brief Add a ::lyd_node or ::lys_node object into the set
1711 *
Radek Krejci29cb50d2016-05-09 16:31:13 +02001712 * Since it is a set, the function checks for duplicity and if the
1713 * node is already in the set, the index of the previously added
1714 * node is returned.
1715 *
Radek Krejcidc154432016-01-21 11:10:59 +01001716 * @param[in] set Set where the \p node will be added.
1717 * @param[in] node The ::lyd_node or ::lys_node object to be added into the \p set;
Radek Krejci09891a22016-06-10 10:59:22 +02001718 * @param[in] options Options to change behavior of the function. Accepted options are:
1719 * - #LY_SET_OPT_USEASLIST - do not check for duplicities
Radek Krejci29cb50d2016-05-09 16:31:13 +02001720 * @return -1 on failure, index of the \p node in the set on success
Radek Krejcidc154432016-01-21 11:10:59 +01001721 */
Radek Krejci09891a22016-06-10 10:59:22 +02001722int ly_set_add(struct ly_set *set, void *node, int options);
Radek Krejcidc154432016-01-21 11:10:59 +01001723
1724/**
Michal Vasko563cfea2017-11-28 14:26:07 +01001725 * @brief Add all objects from \p src to \p trg.
1726 *
1727 * Since it is a set, the function checks for duplicities.
1728 * After success, \p src is completely freed.
1729 *
1730 * @param[in] trg Target (result) set.
1731 * @param[in] src Source set.
1732 * @param[in] options Options to change behavior of the function. Accepted options are:
1733 * - #LY_SET_OPT_USEASLIST - do not check for duplicities
1734 * @return -1 on failure, number of objects added into \p trg on success.
1735 */
1736int ly_set_merge(struct ly_set *trg, struct ly_set *src, int options);
1737
1738/**
Radek Krejci99e6d9a2016-10-20 13:01:43 +02001739 * @brief Get know if the set contains the specified object.
1740 * @param[in] set Set to explore.
1741 * @param[in] node Object to be found in the set.
1742 * @return Index of the object in the set or -1 if the object is not present in the set.
Radek Krejci3e112292016-10-20 13:10:30 +02001743 */
Radek Krejci99e6d9a2016-10-20 13:01:43 +02001744int ly_set_contains(const struct ly_set *set, void *node);
1745
1746/**
Radek Krejci29ed4082016-05-09 14:25:56 +02001747 * @brief Remove all objects from the set, but keep the set container for further use.
1748 *
1749 * @param[in] set Set to clean.
1750 * @return 0 on success
1751 */
1752int ly_set_clean(struct ly_set *set);
1753
1754/**
Radek Krejcidc154432016-01-21 11:10:59 +01001755 * @brief Remove a ::lyd_node or ::lys_node object from the set.
1756 *
1757 * Note that after removing a node from a set, indexes of other nodes in the set can change
1758 * (the last object is placed instead of the removed object).
1759 *
1760 * @param[in] set Set from which the \p node will be removed.
1761 * @param[in] node The ::lyd_node or ::lys_node object to be removed from the \p set;
1762 * @return 0 on success
1763 */
1764int ly_set_rm(struct ly_set *set, void *node);
1765
1766/**
1767 * @brief Remove a ::lyd_node or ::lys_node object from the set index.
1768 *
1769 * Note that after removing a node from a set, indexes of other nodes in the set can change
1770 * (the last object is placed instead of the removed object).
1771 *
1772 * @param[in] set Set from which a node will be removed.
1773 * @param[in] index Index of the ::lyd_node or ::lys_node object in the \p set to be removed from the \p set;
1774 * @return 0 on success
1775 */
1776int ly_set_rm_index(struct ly_set *set, unsigned int index);
1777
1778/**
Radek Krejcidef50022016-02-01 16:38:32 +01001779 * @brief Free the ::ly_set data. Frees only the set structure content, not the referred data.
Radek Krejcidc154432016-01-21 11:10:59 +01001780 *
1781 * @param[in] set The set to be freed.
1782 */
1783void ly_set_free(struct ly_set *set);
1784
Radek Krejcidef50022016-02-01 16:38:32 +01001785/**@} nodeset */
Radek Krejci6140e4e2015-10-09 15:50:55 +02001786
1787/**
Radek Krejci5044be32016-01-18 17:05:51 +01001788 * @defgroup printerflags Printer flags
Radek Krejcidef50022016-02-01 16:38:32 +01001789 * @ingroup datatree
Radek Krejci5044be32016-01-18 17:05:51 +01001790 *
1791 * Validity flags for data nodes.
1792 *
1793 * @{
1794 */
Michal Vasko6216f272016-10-18 11:49:47 +02001795#define LYP_WITHSIBLINGS 0x01 /**< Flag for printing also the (following) sibling nodes of the data node. */
1796#define LYP_FORMAT 0x02 /**< Flag for formatted output. */
1797#define LYP_KEEPEMPTYCONT 0x04 /**< Preserve empty non-presence containers */
1798#define LYP_WD_MASK 0xF0 /**< Mask for with-defaults modes */
1799#define LYP_WD_EXPLICIT 0x00 /**< Explicit mode - print only data explicitly being present in the data tree.
1800 Note that this is the default value when no WD option is specified. */
1801#define LYP_WD_TRIM 0x10 /**< Do not print the nodes with the value equal to their default value */
1802#define LYP_WD_ALL 0x20 /**< Include implicit default nodes */
1803#define LYP_WD_ALL_TAG 0x40 /**< Same as #LYP_WD_ALL but also adds attribute 'default' with value 'true' to
1804 all nodes that has its default value. The 'default' attribute has namespace:
1805 urn:ietf:params:xml:ns:netconf:default:1.0 and thus the attributes are
1806 printed only when the ietf-netconf-with-defaults module is present in libyang
Michal Vaskoe77dc992017-01-18 12:09:42 +01001807 context (but in that case this namespace is always printed). */
Michal Vasko6216f272016-10-18 11:49:47 +02001808#define LYP_WD_IMPL_TAG 0x80 /**< Same as LYP_WD_ALL_TAG but the attributes are added only to the nodes that
1809 are not explicitly present in the original data tree despite their
1810 value is equal to their default value. There is the same limitation regarding
1811 the presence of ietf-netconf-with-defaults module in libyang context. */
Michal Vaskoafa7a642016-10-18 15:11:38 +02001812#define LYP_NETCONF 0x100 /**< Print the data tree for use in NETCONF meaning:
Michal Vasko6216f272016-10-18 11:49:47 +02001813 - for RPC output - skip the top-level RPC node,
1814 - for action output - skip all the parents of and the action node itself,
1815 - for action input - enclose the data in an action element in the base YANG namespace,
1816 - for all other data - print the whole data tree normally. */
Radek Krejci5044be32016-01-18 17:05:51 +01001817
1818/**
1819 * @}
1820 */
1821
1822/**
Radek Krejci3045cf32015-05-28 10:58:52 +02001823 * @defgroup logger Logger
1824 * @{
1825 *
1826 * Publicly visible functions and values of the libyang logger. For more
1827 * information, see \ref howtologger.
1828 */
1829
1830/**
1831 * @typedef LY_LOG_LEVEL
1832 * @brief Verbosity levels of the libyang logger.
1833 */
1834typedef enum {
Michal Vasko8f7e8d92016-07-01 11:33:58 +02001835 LY_LLERR = 0, /**< Print only error messages, default value. */
Michal Vasko53b7da02018-02-13 15:28:42 +01001836 LY_LLWRN = 1, /**< Print error and warning messages. */
1837 LY_LLVRB = 2, /**< Besides errors and warnings, print some other verbose messages. */
1838 LY_LLDBG = 3 /**< Print all messages including some development debug messages (be careful,
Michal Vasko3e3228d2017-02-24 14:55:32 +01001839 without subsequently calling ly_verb_dbg() no debug messages will be printed!). */
Radek Krejci3045cf32015-05-28 10:58:52 +02001840} LY_LOG_LEVEL;
1841
1842/**
1843 * @brief Set logger verbosity level.
1844 * @param[in] level Verbosity level.
Radek Krejci5f9127b2017-06-12 16:42:00 +02001845 * @return Previous verbosity level.
Radek Krejci3045cf32015-05-28 10:58:52 +02001846 */
Radek Krejci5f9127b2017-06-12 16:42:00 +02001847LY_LOG_LEVEL ly_verb(LY_LOG_LEVEL level);
Radek Krejci3045cf32015-05-28 10:58:52 +02001848
Michal Vasko53b7da02018-02-13 15:28:42 +01001849/**
1850 * @defgroup logopts Logging options
1851 * @ingroup logger
1852 *
1853 * Logging option bits of libyang.
1854 *
1855 * @{
1856 */
1857#define LY_LOLOG 0x01 /**< Log messages normally, using callback if set. If not set, messages will
1858 not be printed by libyang. */
1859#define LY_LOSTORE 0x02 /**< Store any generated errors or warnings, never verbose or debug messages.
1860 Note that if #LY_LOLOG is not set then verbose and debug messages are always lost. */
1861#define LY_LOSTORE_LAST 0x06 /**< Store any generated errors or warnings but only the last message, always overwrite
1862 the previous one. */
1863
1864/**
1865 * @}
1866 */
1867
1868/**
1869 * @brief Set additional logger options. Default is #LY_LOLOG | #LY_LOSTORE_LAST.
1870 *
1871 * @param[in] opts Bitfield of @ref logopts.
1872 * @return Previous logger options.
1873 */
1874int ly_log_options(int opts);
1875
Michal Vasko3e3228d2017-02-24 14:55:32 +01001876#ifndef NDEBUG
1877
1878/**
Michal Vasko53b7da02018-02-13 15:28:42 +01001879 * @defgroup dbggroup Debug message groups
1880 * @ingroup logger
1881 *
1882 * Selected displayed debug message groups.
1883 *
1884 * @{
Michal Vasko3e3228d2017-02-24 14:55:32 +01001885 */
Michal Vasko53b7da02018-02-13 15:28:42 +01001886
1887#define LY_LDGDICT 0x01 /**< Dictionary additions and deletions. */
1888#define LY_LDGYANG 0x02 /**< YANG parser messages. */
1889#define LY_LDGYIN 0x04 /**< YIN parser messages. */
1890#define LY_LDGXPATH 0x08 /**< XPath parsing end evaluation. */
1891#define LY_LDGDIFF 0x10 /**< Diff processing and creation. */
1892
1893/**
1894 * @}
1895 */
Michal Vasko3e3228d2017-02-24 14:55:32 +01001896
1897/**
1898 * @brief Enable specific debugging messages (independent of log level).
Michal Vasko53b7da02018-02-13 15:28:42 +01001899 * @param[in] dbg_groups Bitfield of enabled debug message groups (see @ref dbggroup).
Michal Vasko3e3228d2017-02-24 14:55:32 +01001900 */
1901void ly_verb_dbg(int dbg_groups);
1902
1903#endif
1904
Radek Krejci3045cf32015-05-28 10:58:52 +02001905/**
Michal Vaskof1d62cf2015-12-07 13:17:11 +01001906 * @brief Set logger callback.
Michal Vasko13661142016-04-11 10:53:53 +02001907 *
1908 * !IMPORTANT! If an error has a specific error-app-tag defined in the model, it will NOT be set
1909 * at the time of calling this callback. It will be set right after, so to retrieve it
1910 * it must be checked afterwards with ly_errapptag().
1911 *
Michal Vaskof1d62cf2015-12-07 13:17:11 +01001912 * @param[in] clb Logging callback.
Radek Krejciadb57612016-02-16 13:34:34 +01001913 * @param[in] path flag to resolve and provide path as the third parameter of the callback function. In case of
1914 * validation and some other errors, it can be useful to get the path to the problematic element. Note,
1915 * that according to the tree type and the specific situation, the path can slightly differs (keys
1916 * presence) or it can be NULL, so consider it as an optional parameter. If the flag is 0, libyang will
1917 * not bother with resolving the path.
Michal Vaskof1d62cf2015-12-07 13:17:11 +01001918 */
Radek Krejciadb57612016-02-16 13:34:34 +01001919void ly_set_log_clb(void (*clb)(LY_LOG_LEVEL level, const char *msg, const char *path), int path);
Michal Vaskof1d62cf2015-12-07 13:17:11 +01001920
1921/**
1922 * @brief Get logger callback.
1923 * @return Logger callback (can be NULL).
1924 */
Radek Krejciadb57612016-02-16 13:34:34 +01001925void (*ly_get_log_clb(void))(LY_LOG_LEVEL, const char *, const char *);
Michal Vaskof1d62cf2015-12-07 13:17:11 +01001926
1927/**
Radek Krejci3045cf32015-05-28 10:58:52 +02001928 * @typedef LY_ERR
Radek Krejci26715a42015-07-29 14:10:45 +02001929 * @brief libyang's error codes available via ly_errno extern variable.
Radek Krejci9b4ca392015-04-10 08:31:27 +02001930 * @ingroup logger
1931 */
1932typedef enum {
Michal Vasko8f3160e2017-09-27 11:25:26 +02001933 LY_SUCCESS = 0, /**< no error, not set by functions, included just to complete #LY_ERR enumeration */
1934 LY_EMEM, /**< Memory allocation failure */
1935 LY_ESYS, /**< System call failure */
1936 LY_EINVAL, /**< Invalid value */
1937 LY_EINT, /**< Internal error */
1938 LY_EVALID, /**< Validation failure */
Michal Vaskoc6cd3f02018-03-02 14:07:42 +01001939 LY_EPLUGIN /**< Error reported by a plugin */
Radek Krejci3045cf32015-05-28 10:58:52 +02001940} LY_ERR;
Radek Krejci7d9f46a2016-01-29 13:53:18 +01001941
Radek Krejci26715a42015-07-29 14:10:45 +02001942/**
Michal Vaskof5035ce2016-03-11 10:21:31 +01001943 * @typedef LY_VECODE
1944 * @brief libyang's codes of validation error. Whenever ly_errno is set to LY_EVALID, the ly_vecode is also set
1945 * to the appropriate LY_VECODE value.
Radek Krejcia37b39c2016-03-09 16:38:18 +01001946 * @ingroup logger
1947 */
1948typedef enum {
Michal Vaskof5035ce2016-03-11 10:21:31 +01001949 LYVE_SUCCESS = 0, /**< no error */
Radek Krejcia37b39c2016-03-09 16:38:18 +01001950
Michal Vaskof5035ce2016-03-11 10:21:31 +01001951 LYVE_XML_MISS, /**< missing XML object */
1952 LYVE_XML_INVAL, /**< invalid XML object */
1953 LYVE_XML_INCHAR, /**< invalid XML character */
Radek Krejcia37b39c2016-03-09 16:38:18 +01001954
Michal Vaskof5035ce2016-03-11 10:21:31 +01001955 LYVE_EOF, /**< unexpected end of input data */
1956 LYVE_INSTMT, /**< invalid statement (schema) */
1957 /* */
Michal Vaskoca7cbc42016-07-01 11:36:53 +02001958 LYVE_INPAR, /**< invalid (in)direct parent (schema) */
Michal Vaskof5035ce2016-03-11 10:21:31 +01001959 LYVE_INID, /**< invalid identifier (schema) */
1960 LYVE_INDATE, /**< invalid date format */
1961 LYVE_INARG, /**< invalid value of a statement argument (schema) */
1962 LYVE_MISSSTMT, /**< missing required statement (schema) */
1963 /* */
1964 LYVE_MISSARG, /**< missing required statement argument (schema) */
1965 LYVE_TOOMANY, /**< too many instances of some object */
1966 LYVE_DUPID, /**< duplicated identifier (schema) */
1967 LYVE_DUPLEAFLIST, /**< multiple instances of leaf-list */
1968 LYVE_DUPLIST, /**< multiple instances of list */
Michal Vaskoa540df22016-04-11 16:14:35 +02001969 LYVE_NOUNIQ, /**< unique leaves match on 2 list instances (data) */
Radek Krejcie663e012016-08-01 17:12:34 +02001970 LYVE_ENUM_INVAL, /**< invalid enum value (schema) */
1971 LYVE_ENUM_INNAME, /**< invalid enum name (schema) */
1972 /* */
1973 /* */
Michal Vaskof5035ce2016-03-11 10:21:31 +01001974 LYVE_ENUM_WS, /**< enum name with leading/trailing whitespaces (schema) */
Radek Krejcie663e012016-08-01 17:12:34 +02001975 LYVE_BITS_INVAL, /**< invalid bits value (schema) */
1976 LYVE_BITS_INNAME, /**< invalid bits name (schema) */
1977 /* */
1978 /* */
Michal Vaskof5035ce2016-03-11 10:21:31 +01001979 LYVE_INMOD, /**< invalid module name */
1980 /* */
1981 LYVE_KEY_NLEAF, /**< list key is not a leaf (schema) */
1982 LYVE_KEY_TYPE, /**< invalid list key type (schema) */
1983 LYVE_KEY_CONFIG, /**< key config value differs from the list config value */
1984 LYVE_KEY_MISS, /**< list key not found (schema) */
1985 LYVE_KEY_DUP, /**< duplicated key identifier (schema) */
1986 LYVE_INREGEX, /**< invalid regular expression (schema) */
1987 LYVE_INRESOLV, /**< no resolvents found (schema) */
1988 LYVE_INSTATUS, /**< invalid derivation because of status (schema) */
Radek Krejcid8fb03c2016-06-13 15:52:22 +02001989 LYVE_CIRC_LEAFREFS,/**< circular chain of leafrefs detected (schema) */
Radek Krejcie8c1b572016-07-26 15:09:52 +02001990 LYVE_CIRC_FEATURES,/**< circular chain of features detected (schema) */
Radek Krejci151b8cc2016-06-22 10:14:21 +02001991 LYVE_CIRC_IMPORTS, /**< circular chain of imports detected (schema) */
1992 LYVE_CIRC_INCLUDES,/**< circular chain of includes detected (schema) */
Michal Vasko88de3e42016-06-29 11:05:32 +02001993 LYVE_INVER, /**< non-matching YANG versions of module and its submodules (schema) */
Radek Krejci0fd6a992016-11-08 19:42:20 +01001994 LYVE_SUBMODULE, /**< submodule given instead of a module */
Radek Krejcia37b39c2016-03-09 16:38:18 +01001995
Michal Vaskof5035ce2016-03-11 10:21:31 +01001996 LYVE_OBSDATA, /**< obsolete data instantiation (data) */
1997 /* */
1998 LYVE_NORESOLV, /**< no resolvents found for an expression (data) */
1999 LYVE_INELEM, /**< invalid element (data) */
2000 /* */
2001 LYVE_MISSELEM, /**< missing required element (data) */
2002 LYVE_INVAL, /**< invalid value of an element (data) */
Michal Vasko4bff75f2017-03-02 10:48:44 +01002003 LYVE_INMETA, /**< invalid metadata (attribute) value (data) */
Michal Vaskof5035ce2016-03-11 10:21:31 +01002004 LYVE_INATTR, /**< invalid attribute in an element (data) */
2005 LYVE_MISSATTR, /**< missing attribute in an element (data) */
Michal Vasko6ac68282016-04-11 10:56:47 +02002006 LYVE_NOCONSTR, /**< value out of range/length/pattern (data) */
Michal Vaskof5035ce2016-03-11 10:21:31 +01002007 LYVE_INCHAR, /**< unexpected characters (data) */
2008 LYVE_INPRED, /**< predicate resolution fail (data) */
2009 LYVE_MCASEDATA, /**< data for more cases of a choice (data) */
Michal Vasko6ac68282016-04-11 10:56:47 +02002010 LYVE_NOMUST, /**< unsatisfied must condition (data) */
2011 LYVE_NOWHEN, /**< unsatisfied when condition (data) */
Michal Vaskof5035ce2016-03-11 10:21:31 +01002012 LYVE_INORDER, /**< invalid order of elements (data) */
Radek Krejci03b71f72016-03-16 11:10:09 +01002013 LYVE_INWHEN, /**< irresolvable when condition (data) */
Michal Vasko6ac68282016-04-11 10:56:47 +02002014 LYVE_NOMIN, /**< min-elements constraint not honored (data) */
2015 LYVE_NOMAX, /**< max-elements constraint not honored (data) */
2016 LYVE_NOREQINS, /**< required instance does not exits (data) */
2017 LYVE_NOLEAFREF, /**< leaf pointed to by leafref does not exist (data) */
2018 LYVE_NOMANDCHOICE, /**< no mandatory choice case branch exists (data) */
Radek Krejcia37b39c2016-03-09 16:38:18 +01002019
Michal Vaskof5035ce2016-03-11 10:21:31 +01002020 LYVE_XPATH_INTOK, /**< unexpected XPath token */
2021 LYVE_XPATH_EOF, /**< unexpected end of an XPath expression */
2022 LYVE_XPATH_INOP, /**< invalid XPath operation operands */
2023 /* */
2024 LYVE_XPATH_INCTX, /**< invalid XPath context type */
Michal Vasko541dff42016-10-26 11:29:25 +02002025 LYVE_XPATH_INMOD, /**< invalid module name */
Michal Vasko93909772016-10-26 10:32:10 +02002026 LYVE_XPATH_INFUNC, /**< invalid XPath function name */
Michal Vaskof5035ce2016-03-11 10:21:31 +01002027 LYVE_XPATH_INARGCOUNT, /**< invalid number of arguments for an XPath function */
Michal Vasko6fae1362016-03-11 15:10:00 +01002028 LYVE_XPATH_INARGTYPE, /**< invalid type of arguments for an XPath function */
Michal Vasko283bf382017-05-03 12:29:40 +02002029 LYVE_XPATH_DUMMY, /**< invalid use of the XPath dummy node */
2030 LYVE_XPATH_NOEND, /**< unterminated string */
Michal Vasko6fae1362016-03-11 15:10:00 +01002031
2032 LYVE_PATH_INCHAR, /**< invalid characters (path) */
Michal Vaskoe733d682016-03-14 09:08:27 +01002033 LYVE_PATH_INMOD, /**< invalid module name (path) */
2034 LYVE_PATH_MISSMOD, /**< missing module name (path) */
Michal Vasko6fae1362016-03-11 15:10:00 +01002035 LYVE_PATH_INNODE, /**< invalid node name (path) */
Michal Vasko6fae1362016-03-11 15:10:00 +01002036 LYVE_PATH_INKEY, /**< invalid key name (path) */
2037 LYVE_PATH_MISSKEY, /**< missing some list keys (path) */
Michal Vasko310bc582018-05-22 10:47:59 +02002038 LYVE_PATH_INIDENTREF, /**< missing module name prefix in identityref predicate value (path) */
Michal Vasko6fae1362016-03-11 15:10:00 +01002039 LYVE_PATH_EXISTS, /**< target node already exists (path) */
2040 LYVE_PATH_MISSPAR, /**< some parent of the target node is missing (path) */
Michal Vaskofebd13d2018-05-17 10:42:24 +02002041 LYVE_PATH_PREDTOOMANY, /**< too many predicates specified (path) */
Michal Vaskof5035ce2016-03-11 10:21:31 +01002042} LY_VECODE;
Radek Krejcia37b39c2016-03-09 16:38:18 +01002043
2044/**
Radek Krejci7d9f46a2016-01-29 13:53:18 +01002045 * @cond INTERNAL
Michal Vasko53b7da02018-02-13 15:28:42 +01002046 * Get address of (thread-specific) `ly_errno' variable.
Radek Krejci26715a42015-07-29 14:10:45 +02002047 */
Michal Vasko53b7da02018-02-13 15:28:42 +01002048LY_ERR *ly_errno_glob_address(void);
Radek Krejcia37b39c2016-03-09 16:38:18 +01002049
Radek Krejci7d9f46a2016-01-29 13:53:18 +01002050/**
2051 * @endcond INTERNAL
Radek Krejcidef50022016-02-01 16:38:32 +01002052 * @brief libyang specific (thread-safe) errno (see #LY_ERR for the list of possible values and their meaning).
Radek Krejci7d9f46a2016-01-29 13:53:18 +01002053 */
Michal Vasko53b7da02018-02-13 15:28:42 +01002054#define ly_errno (*ly_errno_glob_address())
Radek Krejci9b4ca392015-04-10 08:31:27 +02002055
Radek Krejci386714d2016-02-15 10:24:30 +01002056/**
Michal Vasko53b7da02018-02-13 15:28:42 +01002057 * @brief Get the last (thread, context-specific) validation error code.
2058 *
2059 * This value is set only if ly_errno is #LY_EVALID.
2060 *
2061 * @param[in] ctx Relative context.
2062 * @return Validation error code.
Radek Krejcia37b39c2016-03-09 16:38:18 +01002063 */
Michal Vasko53b7da02018-02-13 15:28:42 +01002064LY_VECODE ly_vecode(const struct ly_ctx *ctx);
Radek Krejcia37b39c2016-03-09 16:38:18 +01002065
2066/**
Michal Vasko53b7da02018-02-13 15:28:42 +01002067 * @brief Get the last (thread, context-specific) error message. If the coresponding module defined
Michal Vasko13661142016-04-11 10:53:53 +02002068 * a specific error message, it will be used instead the default one.
Radek Krejci6e8fc0b2016-02-16 14:33:37 +01002069 *
Michal Vasko53b7da02018-02-13 15:28:42 +01002070 * Sometimes, the error message is extended with path of the element where the problem is.
Radek Krejci6e8fc0b2016-02-16 14:33:37 +01002071 * The path is available via ly_errpath().
2072 *
Michal Vasko53b7da02018-02-13 15:28:42 +01002073 * @param[in] ctx Relative context.
Radek Krejcib50551c2016-04-19 09:15:38 +02002074 * @return Text of the last error message, empty string if there is no error.
Radek Krejci386714d2016-02-15 10:24:30 +01002075 */
Michal Vasko53b7da02018-02-13 15:28:42 +01002076const char *ly_errmsg(const struct ly_ctx *ctx);
Radek Krejci386714d2016-02-15 10:24:30 +01002077
Radek Krejci6e8fc0b2016-02-16 14:33:37 +01002078/**
Michal Vasko53b7da02018-02-13 15:28:42 +01002079 * @brief Get the last (thread, context-specific) path of the element where was an error.
Radek Krejci6e8fc0b2016-02-16 14:33:37 +01002080 *
2081 * The path always corresponds to the error message available via ly_errmsg(), so
2082 * whenever a subsequent error message is printed, the path is erased or rewritten.
Radek Krejci3cc10962016-04-13 15:03:27 +02002083 * The path reflects the type of the processed tree - data path for data tree functions
2084 * and schema path in case of schema tree functions. In case of processing YIN schema
2085 * or XML data, the path can be just XML path. In such a case, the corresponding
2086 * ly_vecode (value 1-3) is set.
Radek Krejci6e8fc0b2016-02-16 14:33:37 +01002087 *
Michal Vasko53b7da02018-02-13 15:28:42 +01002088 * @param[in] ctx Relative context.
Radek Krejcib50551c2016-04-19 09:15:38 +02002089 * @return Path of the error element, empty string if error path does not apply to the last error.
Radek Krejci6e8fc0b2016-02-16 14:33:37 +01002090 */
Michal Vasko53b7da02018-02-13 15:28:42 +01002091const char *ly_errpath(const struct ly_ctx *ctx);
Radek Krejci6e8fc0b2016-02-16 14:33:37 +01002092
Michal Vasko13661142016-04-11 10:53:53 +02002093/**
Michal Vasko53b7da02018-02-13 15:28:42 +01002094 * @brief Get the last (thread, context-specific) error-app-tag if there was a specific one defined
Michal Vasko13661142016-04-11 10:53:53 +02002095 * in the module for the last error.
2096 *
2097 * The app-tag always corresponds to the error message available via ly_errmsg(), so
2098 * whenever a subsequent error message is printed, the app-tag is erased or rewritten.
2099 *
Michal Vasko53b7da02018-02-13 15:28:42 +01002100 * @param[in] ctx Relative context.
Radek Krejcib50551c2016-04-19 09:15:38 +02002101 * @return Error-app-tag of the last error, empty string if the error-app-tag does not apply to the last error.
Michal Vasko13661142016-04-11 10:53:53 +02002102 */
Michal Vasko53b7da02018-02-13 15:28:42 +01002103const char *ly_errapptag(const struct ly_ctx *ctx);
Michal Vasko13661142016-04-11 10:53:53 +02002104
Michal Vasko53b7da02018-02-13 15:28:42 +01002105/**
2106 * @brief Libyang full error structure.
2107 */
2108struct ly_err_item {
2109 LY_LOG_LEVEL level;
2110 LY_ERR no;
2111 LY_VECODE vecode;
2112 char *msg;
2113 char *path;
2114 char *apptag;
2115 struct ly_err_item *next;
2116 struct ly_err_item *prev; /* first item's prev points to the last item */
2117};
2118
2119/**
Michal Vasko10d6f2f2018-02-14 10:56:01 +01002120 * @brief Get the first (thread, context-specific) generated error structure.
Michal Vasko53b7da02018-02-13 15:28:42 +01002121 *
2122 * @param[in] ctx Relative context.
Michal Vasko10d6f2f2018-02-14 10:56:01 +01002123 * @return First error structure (can be NULL), do not modify!
Michal Vasko53b7da02018-02-13 15:28:42 +01002124 */
Michal Vasko10d6f2f2018-02-14 10:56:01 +01002125struct ly_err_item *ly_err_first(const struct ly_ctx *ctx);
Michal Vasko53b7da02018-02-13 15:28:42 +01002126
2127/**
Michal Vasko20fadd52018-02-14 09:33:22 +01002128 * @brief Print the error structure as if just generated.
2129 *
2130 * @param[in] eitem Error item structure to print.
2131 */
2132void ly_err_print(struct ly_err_item *eitem);
2133
2134/**
Michal Vasko53b7da02018-02-13 15:28:42 +01002135 * @brief Free error structures from a context.
2136 *
2137 * If \p eitem is not set, free all the error structures.
2138 *
2139 * @param[in] ctx Relative context.
2140 * @param[in] eitem Oldest error structure to remove, optional.
2141 */
2142void ly_err_clean(struct ly_ctx *ctx, struct ly_err_item *eitem);
2143
2144/**
2145 * @} logger
2146 */
Radek Krejci9b4ca392015-04-10 08:31:27 +02002147
Radek Krejci39d8d0d2015-08-17 13:42:45 +02002148#ifdef __cplusplus
2149}
2150#endif
2151
Radek Krejci9b4ca392015-04-10 08:31:27 +02002152#endif /* LY_LIBYANG_H_ */