blob: 433f1ed5518879d2752a27c8f02095d89bf4cbcb [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 *
Radek Krejcidef50022016-02-01 16:38:32 +01006 * Copyright (c) 2015-2016 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 Vasko2d162e12015-09-24 14:33:29 +020020#include "tree_schema.h"
21#include "tree_data.h"
Radek Krejcic6704c82015-10-06 11:12:45 +020022#include "xml.h"
Radek Krejci41912fe2015-10-22 10:22:12 +020023#include "dict.h"
Radek Krejcida04f4a2015-05-21 12:54:09 +020024
Radek Krejci39d8d0d2015-08-17 13:42:45 +020025#ifdef __cplusplus
26extern "C" {
27#endif
28
Radek Krejci26715a42015-07-29 14:10:45 +020029/**
Radek Krejcidef50022016-02-01 16:38:32 +010030 * @mainpage About
31 *
32 * libyang is a library implementing processing of the YANG schemas and data modeled by the YANG language. The
33 * library is implemented in C for GNU/Linux and provides C API.
34 *
35 * @section about-features Main Features
36 *
Radek Krejcib4e72e52016-04-13 15:10:51 +020037 * - Parsing (and validating) schemas in YANG format.
Radek Krejcidef50022016-02-01 16:38:32 +010038 * - Parsing (and validating) schemas in YIN format.
39 * - Parsing, validating and printing instance data in XML format.
40 * - Parsing, validating and printing instance data in JSON format.
41 * - Manipulation with the instance data.
Radek Krejcib4e72e52016-04-13 15:10:51 +020042 * - Support for adding default values into instance data.
Radek Krejcidef50022016-02-01 16:38:32 +010043 *
Radek Krejci8b13fc02016-04-18 13:08:04 +020044 * The current implementation covers YANG 1.0 specified in [RFC 6020](https://tools.ietf.org/html/rfc6020).
45 * Future plans include support for [YANG 1.1](https://tools.ietf.org/html/draft-ietf-netmod-rfc6020bis-11).
46 *
Radek Krejcidef50022016-02-01 16:38:32 +010047 * @subsection about-features-others Extra (side-effect) Features
48 *
49 * - XML parser.
50 * - Optimized string storage (dictionary).
51 *
52 * @section about-license License
53 *
54 * Copyright (c) 2015-2016 CESNET, z.s.p.o.
55 *
56 * (The BSD 3-Clause License)
57 *
58 * Redistribution and use in source and binary forms, with or without
59 * modification, are permitted provided that the following conditions
60 * are met:
61 * 1. Redistributions of source code must retain the above copyright
62 * notice, this list of conditions and the following disclaimer.
63 * 2. Redistributions in binary form must reproduce the above copyright
64 * notice, this list of conditions and the following disclaimer in
65 * the documentation and/or other materials provided with the
66 * distribution.
67 * 3. Neither the name of the Company nor the names of its contributors
68 * may be used to endorse or promote products derived from this
69 * software without specific prior written permission.
70 */
71
72/**
Radek Krejci26715a42015-07-29 14:10:45 +020073 * @page howto How To ...
74 *
75 * - @subpage howtocontext
Radek Krejcid9ba3e32015-07-30 15:08:18 +020076 * - @subpage howtoschemas
77 * - @subpage howtodata
Michal Vasko0f14ba62016-03-21 15:38:11 +010078 * - @subpage howtoxpath
Radek Krejcidef50022016-02-01 16:38:32 +010079 * - @subpage howtoxml
80 * - @subpage howtothreads
Radek Krejci26715a42015-07-29 14:10:45 +020081 * - @subpage howtologger
82 */
Radek Krejcida04f4a2015-05-21 12:54:09 +020083
Radek Krejci26715a42015-07-29 14:10:45 +020084/** @page howtocontext Context
85 *
Radek Krejcid9ba3e32015-07-30 15:08:18 +020086 * The context concept allows callers to work in environments with different sets of YANG schemas.
Radek Krejci26715a42015-07-29 14:10:45 +020087 *
88 * The first step in libyang is to create a new context using ly_ctx_new(). It returns a handler
89 * used in the following work.
90 *
91 * When creating a new context, search dir can be specified (NULL is accepted) to provide directory
92 * where libyang will automatically search for schemas being imported or included. The search path
93 * can be later changed via ly_ctx_set_searchdir() function. Before exploring the specified search
94 * dir, libyang tries to get imported and included schemas from the current working directory first.
Radek Krejcidef50022016-02-01 16:38:32 +010095 * This automatic searching can be completely avoided when the caller sets module searching callback
96 * (#ly_module_clb) via ly_ctx_set_module_clb().
Radek Krejci26715a42015-07-29 14:10:45 +020097 *
Radek Krejcif6ab2cd2016-04-18 17:15:26 +020098 * Schemas are added into the context using [parser functions](@ref howtoschemasparsers) - \b lys_parse_*().
Radek Krejcidef50022016-02-01 16:38:32 +010099 * In case of schemas, also ly_ctx_load_module() can be used - in that case the #ly_module_clb or automatic
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200100 * search in working directory and in the searchpath is used.
101 *
102 * Similarly, data trees can be parsed by \b lyd_parse_*() functions. Note, that functions for schemas have \b lys_
Radek Krejcidef50022016-02-01 16:38:32 +0100103 * prefix while functions for instance data have \b lyd_ prefix.
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200104 *
Radek Krejcif647e612015-07-30 11:36:07 +0200105 * Context can hold multiple revisons of the same schema.
Radek Krejci26715a42015-07-29 14:10:45 +0200106 *
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200107 * Context holds all modules and their submodules internally and they can appear in multiple revisions. To get
108 * a specific module or submodule, use ly_ctx_get_module() and ly_ctx_get_submodule(). There are some additional
109 * 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 +0200110 * submodules in the context, it is advised to iterate over them using ly_ctx_get_module_iter(), it is
111 * the most efficient way. Alternatively, the ly_ctx_info() function can be used to get complex information
112 * about the schemas in the context in the form of data tree defined by
Radek Krejcibd9e8d22016-02-03 14:11:48 +0100113 * <a href="https://tools.ietf.org/html/draft-ietf-netconf-yang-library-04">ietf-yang-library</a> schema.
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200114 * To get a specific node defined in a module in the context, ly_ctx_get_node() and ly_ctx_get_node2() can be used.
115 * They differ in parameters used to identify the schema node.
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200116 *
117 * Modules held by a context cannot be removed one after one. The only way how to \em change modules in the
118 * context is to create a new context and remove the old one. To remove a context, there is ly_ctx_destroy()
119 * function.
120 *
Radek Krejcidef50022016-02-01 16:38:32 +0100121 * - @subpage howtocontextdict
122 *
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200123 * \note API for this group of functions is available in the [context module](@ref context).
124 *
Radek Krejcidef50022016-02-01 16:38:32 +0100125 * Functions List
126 * --------------
127 * - ly_ctx_new()
128 * - ly_ctx_set_searchdir()
129 * - ly_ctx_get_searchdir()
130 * - ly_ctx_set_module_clb()
131 * - ly_ctx_get_module_clb()
132 * - ly_ctx_load_module()
133 * - ly_ctx_info()
Michal Vaskod7957c02016-04-01 10:27:26 +0200134 * - ly_ctx_get_module_iter()
Radek Krejcidef50022016-02-01 16:38:32 +0100135 * - ly_ctx_get_module()
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200136 * - ly_ctx_get_module_older()
Radek Krejcidef50022016-02-01 16:38:32 +0100137 * - ly_ctx_get_module_by_ns()
Radek Krejcidef50022016-02-01 16:38:32 +0100138 * - ly_ctx_get_submodule()
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200139 * - ly_ctx_get_submodule2()
Michal Vasko3edeaf72016-02-11 13:17:43 +0100140 * - ly_ctx_get_node()
Michal Vasko9fd98e22016-04-07 15:44:19 +0200141 * - ly_ctx_get_node2()
Radek Krejcidef50022016-02-01 16:38:32 +0100142 * - ly_ctx_destroy()
143 */
144
145/**
146 * @page howtocontextdict Context Dictionary
147 *
148 * Context includes dictionary to store strings more effectively. The most of strings repeats quite often in schema
149 * as well as data trees. Therefore, instead of allocating those strings each time they appear, libyang stores them
150 * as records in the dictionary. The basic API to the context dictionary is public, so even a caller application can
151 * use the dictionary.
152 *
153 * To insert a string into the dictionary, caller can use lydict_insert() (adding a constant string) or
154 * lydict_insert_zc() (for dynamically allocated strings that won't be used by the caller after its insertion into
155 * the dictionary). Both functions return the pointer to the inserted string in the dictionary record.
156 *
157 * To remove (reference of the) string from the context dictionary, lydict_remove() is supposed to be used.
158 *
159 * \note Incorrect usage of the dictionary can break libyang functionality.
160 *
161 * \note API for this group of functions is described in the [XML Parser module](@ref dict).
162 *
163 * Functions List
164 * --------------
165 * - lydict_insert()
166 * - lydict_insert_zc()
167 * - lydict_remove()
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200168 */
169
170/**
171 * @page howtoschemas Schemas
172 *
Radek Krejcidef50022016-02-01 16:38:32 +0100173 *
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200174 * Schema is an internal libyang's representation of a YANG data model. Each schema is connected with
Radek Krejcidef50022016-02-01 16:38:32 +0100175 * its [context](@ref howtocontext) and loaded using [parser functions](@ref howtoschemasparsers). It means, that
176 * the schema cannot be created (nor changed) programmatically. In libyang, schemas are used only to
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200177 * access data model definitions.
178 *
Radek Krejcidef50022016-02-01 16:38:32 +0100179 * Schema tree nodes are able to hold private objects (via a pointer to a structure, function, variable, ...) used by
180 * a caller application. Such an object can be assigned to a specific node using lys_set_private() function.
181 * Note that the object is not freed by libyang when the context is being destroyed. So the caller is responsible
182 * 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 +0200183 * appropriate schema nodes where the object was previously set. This can be automated via destructor function
184 * to free these private objects. The destructor is passed to the ly_ctx_destroy() function. On the other hand,
185 * freeing the object while the schema tree is still in use can lead to a segmentation fault.
Radek Krejcidef50022016-02-01 16:38:32 +0100186 *
187 * - @subpage howtoschemasparsers
188 * - @subpage howtoschemasfeatures
189 * - @subpage howtoschemasprinters
190 *
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200191 * \note There are many functions to access information from the schema trees. Details are available in
192 * the [Schema Tree module](@ref schematree).
193 *
Radek Krejcidef50022016-02-01 16:38:32 +0100194 * Functions List (not assigned to above subsections)
195 * --------------------------------------------------
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200196 * - lys_getnext()
Radek Krejcidef50022016-02-01 16:38:32 +0100197 * - lys_parent()
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200198 * - lys_module()
199 * - lys_node_module()
Radek Krejcidef50022016-02-01 16:38:32 +0100200 * - lys_set_private()
201 */
202
203/**
204 * @page howtoschemasparsers Parsing Schemas
205 *
206 * Schema parser allows to read schema from a specific format. libyang supports the following schema formats:
207 *
208 * - YANG
209 *
210 * Basic YANG schemas format described in [RFC 6020](http://tools.ietf.org/html/rfc6020).
211 * Currently, only YANG 1.0 is supported.
212 *
Radek Krejcidef50022016-02-01 16:38:32 +0100213 * - YIN
214 *
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200215 * Alternative XML-based format to YANG - YANG Independent Notation. The details can be found in
Radek Krejcidef50022016-02-01 16:38:32 +0100216 * [RFC 6020](http://tools.ietf.org/html/rfc6020#section-11).
217 *
218 * When the [context](@ref howtocontext) is created, it already contains the following three schemas, which
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200219 * are implemented internally by libyang:
Radek Krejcidef50022016-02-01 16:38:32 +0100220 * - ietf-inet-types@2013-07-15
221 * - ietf-yang-types@2013-07-15
222 * - ietf-yang-library@2015-07-03
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200223 * - yang@2016-02-11
224 *
225 * The last one is libyang's internal module to provide namespace for various YANG attributes defined in RFC 6020
226 * (such as `insert` attribute for edit-config's data).
Radek Krejcidef50022016-02-01 16:38:32 +0100227 *
228 * Other schemas can be added to the context manually as described in [context page](@ref howtocontext) by the functions
229 * listed below. Besides the schema parser functions, it is also possible to use ly_ctx_load_module() which tries to
230 * find the required schema automatically - using #ly_module_clb or automatic search in working directory and in the
231 * context's searchpath.
232 *
233 * Functions List
234 * --------------
Radek Krejci722b0072016-02-01 17:09:45 +0100235 * - lys_parse_mem()
Radek Krejcidef50022016-02-01 16:38:32 +0100236 * - lys_parse_fd()
237 * - lys_parse_path()
238 * - ly_ctx_set_module_clb()
239 * - ly_ctx_load_module()
240 */
241
242/**
243 * @page howtoschemasfeatures YANG Features Manipulation
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200244 *
245 * The group of functions prefixed by \b lys_features_ are used to access and manipulate with the schema's
246 * features.
247 *
248 * The first two functions are used to access information about the features in the schema.
249 * lys_features_list() provides list of all features defined in the specific schema and its
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200250 * submodules. Optionally, it can also provide information about the state of all features.
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200251 * Alternatively, caller can use lys_features_state() function to get state of one specific
252 * feature.
253 *
254 * The remaining two functions, lys_features_enable() and lys_features_disable(), are used
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200255 * to enable and disable the specific feature (or all via the '`*`' value). By default, when the module
Radek Krejcidef50022016-02-01 16:38:32 +0100256 * is loaded by libyang parser, all features are disabled.
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200257 *
Radek Krejcidef50022016-02-01 16:38:32 +0100258 * 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 +0200259 *
Radek Krejcidef50022016-02-01 16:38:32 +0100260 * Note, that the feature's state can affect some of the output formats (e.g. Tree format).
261 *
262 * Functions List
263 * --------------
264 * - lys_features_list()
265 * - lys_features_enable()
266 * - lys_features_disable()
267 * - lys_features_state()
268 * - lys_is_disabled()
269 */
270
271/**
272 * @page howtoschemasprinters Printing Schemas
273 *
274 * Schema printers allows to serialize internal representation of a schema module in a specific format. libyang
275 * supports the following schema formats for printing:
276 *
277 * - YANG
278 *
279 * Basic YANG schemas format described in [RFC 6020](http://tools.ietf.org/html/rfc6020).
280 * Currently, only YANG 1.0 is supported.
281 *
282 * - YIN
283 *
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200284 * Alternative XML-based format to YANG - YANG Independent Notation. The details can be found in
Radek Krejcidef50022016-02-01 16:38:32 +0100285 * [RFC 6020](http://tools.ietf.org/html/rfc6020#section-11).
286 *
Radek Krejcidef50022016-02-01 16:38:32 +0100287 * - Tree
288 *
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200289 * Simple tree structure of the module where each node is printed as:
290 *
291 * <status> <flags> <name> <opts> <type> <if-features>
292 *
293 * - `<status>` is one of:
294 * - `+` for current
295 * - `x` for deprecated
296 * - `o` for obsolete
297 *
298 * - `<flags>` is one of:
299 * - `rw` for configuration data
300 * - `ro` for status data
301 * - `-x` for RPCs
302 * - `-n` for Notification
303 *
304 * - `<name>` is the name of the node
305 * - `(<name>)` means that the node is a choice node
306 * - `:(<name>)` means that the node is a case node
307 * - if the node is augmented into the tree from another module, it is printed with the module name as
308 * `<module-name>:<name>`.
309 *
310 * - `<opts>` is one of:
311 * - `?` for an optional leaf or choice
312 * - `!` for a presence container
313 * - `*` for a leaf-list or list
314 * - `[<keys>]` for a list's keys
315 *
316 * - `<type>` is the name of the type for leafs and leaf-lists
317 * - if there is a default value defined, it is printed within angle brackets `<default-value>`
318 * - if the type is a leafref, the type is printed as -> TARGET`
319 *
320 * - `<if-features>` is the list of features this node depends on, printed within curly brackets and
321 * a question mark `{...}?`
322 *
Radek Krejcidef50022016-02-01 16:38:32 +0100323 *
324 * - Info
325 *
326 * Detailed information about the specific node in the schema tree.
327 * It allows to print information not only about a specific module, but also about its specific part:
328 *
329 * - absolute-schema-nodeid
330 *
331 * e.g. \a `/modules/module-set-id` in \a `ietf-yang-library` module
332 *
333 * - <b>typedef/</b>typedef-name
334 *
335 * e.g. \a `typedef/revision-identifier` in \a `ietf-yang-library` module
336 *
337 * - <b>feature/</b>feature-name
338 *
339 * e.g. \a `feature/ssh` in \a `ietf-netconf-server` module
340 *
341 * - <b>grouping/</b>grouping-name/descendant-schema-nodeid
342 *
343 * e.g. \a `grouping/module` or \a `grouping/module/module/submodules` in \a `ietf-yang-library` module
344 *
345 * - <b>type/</b>leaf-or-leaflist
346 *
347 * e.g. \a `type/modules/module-set-id` in \a `ietf-yang-library` module
348 *
349 * Printer functions allow to print to the different outputs including a callback function which allows caller
350 * to have a full control of the output data - libyang passes to the callback a private argument (some internal
351 * data provided by a caller of lys_print_clb()), string buffer and number of characters to print. Note that the
352 * callback is supposed to be called multiple times during the lys_print_clb() execution.
353 *
354 * Functions List
355 * --------------
356 * - lys_print_mem()
357 * - lys_print_fd()
358 * - lys_print_file()
359 * - lys_print_clb()
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200360 */
361
362/**
363 * @page howtodata Data Instances
Radek Krejci26715a42015-07-29 14:10:45 +0200364 *
Radek Krejcidef50022016-02-01 16:38:32 +0100365 * All data nodes in data trees are connected with their schema node - libyang is not able to represent data of an
366 * unknown schema.
367 *
368 * By default, the represented data are supposed to represent a full YANG datastore content. So if a schema declares
369 * some mandatory nodes, despite configuration or status, the data are supposed to be present in the data tree being
370 * loaded or validated. However, it is possible to specify other kinds of data (see @ref parseroptions) allowing some
371 * exceptions to the validation process.
372 *
373 * Data validation is performed implicitly to the input data processed by the parser (\b lyd_parse_*() functions) and
374 * on demand via the lyd_validate() function. The lyd_validate() is supposed to be used when a (complex or simple)
375 * change is done on the data tree (via a combination of \b lyd_change_*(), \b lyd_insert*(), \b lyd_new*(),
376 * lyd_unlink() and lyd_free() functions).
377 *
378 * - @subpage howtodataparsers
379 * - @subpage howtodatamanipulators
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200380 * - @subpage howtodatawd
Radek Krejcidef50022016-02-01 16:38:32 +0100381 * - @subpage howtodataprinters
382 *
383 * \note API for this group of functions is described in the [Data Instances module](@ref datatree).
384 *
385 * Functions List (not assigned to above subsections)
386 * --------------------------------------------------
387 * - lyd_get_node()
Michal Vasko105cef12016-02-04 12:06:26 +0100388 * - lyd_get_node2()
Radek Krejcidef50022016-02-01 16:38:32 +0100389 */
390
391/**
392 * @page howtodataparsers Parsing Data
393 *
394 * Data parser allows to read instances from a specific format. libyang supports the following data formats:
395 *
396 * - XML
397 *
398 * Original data format used in NETCONF protocol. XML mapping is part of the YANG specification
399 * ([RFC 6020](http://tools.ietf.org/html/rfc6020)).
400 *
401 * - JSON
402 *
403 * The alternative data format available in RESTCONF protocol. Specification of JSON encoding of data modeled by YANG
404 * can be found in [this draft](https://tools.ietf.org/html/draft-ietf-netmod-yang-json-05).
405 *
406 * Besides the format of input data, the parser functions accepts additional [options](@ref parseroptions) to specify
407 * how the input data should be processed.
408 *
409 * In contrast to the schema parser, data parser also accepts empty input data if such an empty data tree is valid
410 * according to the schemas in the libyang context.
411 *
412 * In case of XML input data, there is one additional way to parse input data. Besides parsing the data from a string
413 * in memory or a file, caller is able to build an XML tree using [libyang XML parser](@ref howtoxml) and then use
414 * this tree (or a part of it) as input to the lyd_parse_xml() function.
415 *
416 * Functions List
417 * --------------
Radek Krejci722b0072016-02-01 17:09:45 +0100418 * - lyd_parse_mem()
Radek Krejcidef50022016-02-01 16:38:32 +0100419 * - lyd_parse_fd()
420 * - lyd_parse_path()
421 * - lyd_parse_xml()
422 */
423
424/**
425 * @page howtodatamanipulators Manipulating Data
426 *
427 * There are many functions to create or modify an existing data tree. You can add new nodes, reconnect nodes from
428 * one tree to another (or e.g. from one list instance to another) or remove nodes. The functions doesn't allow you
429 * to put a node to a wrong place (by checking the module), but not all validation checks can be made directly
430 * (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 +0100431 * there is lyd_validate() function supposed to be called to make sure that the current data tree is valid. If
432 * working with RPCs, they are invalid also in case the data nodes are not ordered according to the schema, which
433 * you can fix easily with lyd_schema_sort(). Note, that not performing validation after some data tree changes
434 * can cause failure of various libyang functions later.
Radek Krejcidef50022016-02-01 16:38:32 +0100435 *
Michal Vasko0f14ba62016-03-21 15:38:11 +0100436 * Creating data is generally possible in two ways, they can be combined. You can add nodes one-by-one based on
Michal Vaskof748dbc2016-04-05 11:27:47 +0200437 * the node name and/or its parent (lyd_new(), lyd_new_anyxml_*(), lyd_new_leaf(), adn their output variants) or
Michal Vasko58f74f12016-03-24 13:26:06 +0100438 * address the nodes using a simple XPath addressing (lyd_new_path()). The latter enables to create a whole path
439 * of nodes, requires less information about the modified data, and is generally simpler to use. The path format
440 * specifics can be found [here](@ref howtoxpath).
Michal Vasko0f14ba62016-03-21 15:38:11 +0100441 *
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200442 * Working with two data subtrees can also be preformed two ways. Usually, you should use lyd_insert*() functions.
Michal Vasko45fb2822016-04-18 13:32:17 +0200443 * But they always work with a single subtree and it must be placed on an exact and correct location in the other
444 * tree. If using lyd_merge(), this information is learnt internally and duplicities (that would invalidate
445 * the final data tree) are filtered out at the cost of somewhat reduced effectivity.
446 *
Radek Krejcidef50022016-02-01 16:38:32 +0100447 * Also remember, that when you are creating/inserting a node, all the objects in that operation must belong to the
448 * same context.
449 *
450 * Modifying the single data tree in multiple threads is not safe.
451 *
452 * Functions List
453 * --------------
454 * - lyd_dup()
455 * - lyd_change_leaf()
456 * - lyd_insert()
457 * - lyd_insert_before()
458 * - lyd_insert_after()
459 * - lyd_insert_attr()
Michal Vasko45fb2822016-04-18 13:32:17 +0200460 * - lyd_merge()
Radek Krejcidef50022016-02-01 16:38:32 +0100461 * - lyd_new()
Michal Vaskof748dbc2016-04-05 11:27:47 +0200462 * - lyd_new_anyxml_str()
463 * - lyd_new_anyxml_xml()
Radek Krejcidef50022016-02-01 16:38:32 +0100464 * - lyd_new_leaf()
Michal Vaskof5299282016-03-16 13:32:02 +0100465 * - lyd_new_path()
Radek Krejcidef50022016-02-01 16:38:32 +0100466 * - lyd_output_new()
Michal Vaskof748dbc2016-04-05 11:27:47 +0200467 * - lyd_output_new_anyxml_str()
468 * - lyd_output_new_anyxml_xml()
Radek Krejcidef50022016-02-01 16:38:32 +0100469 * - lyd_output_new_leaf()
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200470 * - lyd_schema_sort()
Radek Krejcidef50022016-02-01 16:38:32 +0100471 * - lyd_unlink()
472 * - lyd_free()
473 * - lyd_free_attr()
474 * - lyd_free_withsiblings()
475 * - lyd_validate()
476 */
477
478/**
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200479 * @page howtodatawd Default Values
480 *
481 * libyang provide support for work with default values as defined in [RFC 6243](https://tools.ietf.org/html/rfc6243).
482 * This document defines 4 modes for adding/removing default nodes to/from a data tree, libyang adds the fifth mode:
483 * - \b explicit - Only the explicitly set configuration data. But in the case of status data, missing default
484 * data are added into the tree. In libyang, this mode is represented by #LYD_WD_EXPLICIT option.
485 * - \b trim - Data nodes containing the schema default value are removed. This mode is applied using #LYD_WD_TRIM option.
486 * - \b report-all - All the missing default data are added into the data tree. This mode is represented by
487 * #LYD_WD_ALL option.
488 * - \b report-all-tagged - In this case, all the missing default data are added as in case of the `report-all` mode,
489 * but additionally all the nodes (existing as well as added) containing the schema default value
490 * are tagged (see the note below). libyang uses #LYD_WD_ALL_TAG option for this mode.
491 * - \b report-implicit-tagged - The last mode is similar to the previous one, except only the added nodes are tagged.
492 * This is the libyang's extension and it is represented by #LYD_WD_IMPL_TAG option.
493 *
494 * In the data nodes, the tag is represented as set ::lyd_node's `dflt` member. However, when the data tree is printed,
495 * the tag is automatically printed as XML/JSON attribute as defined in [RFC 6243](https://tools.ietf.org/html/rfc6243).
496 * This conversion is done only if the context includes the ietf-netconf-with-defaults schema. Otherwise, both
497 * #LYD_WD_ALL_TAG and #LYD_WD_IMPL_TAG have the same result as #LYD_WD_ALL.
498 *
499 * The base function for with-defaults capability is lyd_wd_add(), which modifies the data tree according to the
500 * required with-defaults mode. However, the with-defaults modes can be applied directly by the data parser
501 * functions and by lyd_validate().
502 *
503 * With the lyd_wd_cleanup(), caller is able to remove all the data nodes marked with the defaults tag (set via
504 * #LYD_WD_ALL_TAG or #LYD_WD_IMPL_TAG).
505 *
506 * Functions List
507 * --------------
508 * - lyd_wd_add()
509 * - lyd_wd_cleanup()
510 *
511 * - lyd_parse_mem()
512 * - lyd_parse_fd()
513 * - lyd_parse_path()
514 * - lyd_parse_xml()
515 * - lyd_validate()
516 */
517
518/**
Radek Krejcidef50022016-02-01 16:38:32 +0100519 * @page howtodataprinters Printing Data
520 *
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200521 * Data printers allows to serialize internal representation of a data tree in a specific format. libyang
522 * supports the following data formats for printing:
Radek Krejcidef50022016-02-01 16:38:32 +0100523 *
524 * - XML
525 *
526 * Basic format as specified in rules of mapping YANG modeled data to XML in
527 * [RFC 6020](http://tools.ietf.org/html/rfc6020). It is possible to specify if
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200528 * the indentation (formatting) will be used (by #LYP_FORMAT @ref printerflags "printer option").
Radek Krejcidef50022016-02-01 16:38:32 +0100529 *
530 * - JSON
531 *
532 * The alternative data format available in RESTCONF protocol. Specification of JSON encoding of data modeled by YANG
Radek Krejcif6ab2cd2016-04-18 17:15:26 +0200533 * can be found in [this draft](https://tools.ietf.org/html/draft-ietf-netmod-yang-json-05).It is possible to specify
534 * if the indentation (formatting) will be used (by #LYP_FORMAT @ref printerflags "printer option").
Radek Krejcidef50022016-02-01 16:38:32 +0100535 *
536 * Printer functions allow to print to the different outputs including a callback function which allows caller
537 * to have a full control of the output data - libyang passes to the callback a private argument (some internal
538 * data provided by a caller of lyd_print_clb()), string buffer and number of characters to print. Note that the
539 * callback is supposed to be called multiple times during the lyd_print_clb() execution.
540 *
541 * Functions List
542 * --------------
543 * - lyd_print_mem()
544 * - lyd_print_fd()
545 * - lyd_print_file()
546 * - lyd_print_clb()
547 */
548
549/**
Radek Krejcib50551c2016-04-19 09:15:38 +0200550 * @page howtoxpath XPath Addressing
551 *
552 * Internally, XPath evaluation is performed on \b when and \b must conditions in the schema. For that almost
553 * a full XPath 1.0 evaluator was implemented. This XPath implementation is available on data trees by calling
554 * lyd_get_node() except that only node sets are returned. This XPath conforms to the YANG specification
555 * (RFC 6020 section 6.4).
556 *
557 * A very small subset of this full XPath is recognized by lyd_new_path(). Basically, only a relative or absolute
558 * path can be specified to identify a new data node. However, lists must be identified by all their keys and created
559 * with all of them, so for those cases predicates are allowed. Predicates must be ordered the way the keys are ordered
560 * and all the keys must be specified. Every predicate includes a single key with its value. These paths are valid XPath
561 * expressions. Example:
562 *
563 * /ietf-yang-library:modules-state/module[name='ietf-yang-library'][revision='']/submodules
564 *
565 * Almost the same XPath is accepted by ly_ctx_get_node(). The difference is that it is not used on data, but schema,
566 * which means there are no key values and only one node matches one path. In effect, lists do not have to have any
567 * predicates. If they do, they do not need to have all the keys specified and if values are included, they are ignored.
568 * Nevertheless, any such expression is still a valid XPath, but can return more nodes if executed on a data tree.
569 * Examples (all returning the same node):
570 *
571 * /ietf-yang-library:modules-state/module/submodules
572 * /ietf-yang-library:modules-state/module[name]/submodules
573 * /ietf-yang-library:modules-state/module[name][revision]/submodules
574 * /ietf-yang-library:modules-state/module[name='ietf-yang-library'][revision]/submodules
575 *
576 * Also, `choice`, `case`, `input`, and `output` nodes need to be specified and cannot be skipped in schema XPaths. Use
577 * ly_ctx_get_node2() if you want to search based on a data XPath, the same format as what lyd_new_path() uses.
578 *
579 * Also note, that in all cases the node's prefix is specified as the name of the appropriate YANG schema. Any node
580 * can be prefixed by the module name. However, if the prefix is omitted, the module name is inherited from the previous
581 * (parent) node. It means, that the first node in the path is always supposed to have a prefix.
582 *
583 * Functions List
584 * --------------
585 * - lyd_get_node()
586 * - lyd_new_path()
587 * - ly_ctx_get_node()
588 * - ly_ctx_get_node2()
589 */
590
591/**
Radek Krejcidef50022016-02-01 16:38:32 +0100592 * @page howtoxml libyang XML Support
593 *
Radek Krejcib50551c2016-04-19 09:15:38 +0200594 * libyang XML parser is able to parse XML documents. The main purpose is to load data modeled by YANG. However, it can
595 * 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 +0100596 * - comments are ignored
597 * - Doctype declaration is ignored
598 * - CData sections are ignored
599 * - Process Instructions (PI) are ignored
600 *
601 * The API is designed to almost only read-only access. You can simply load XML document, go through the tree as
602 * you wish and dump the tree to an output. The only "write" functions are lyxml_free() and lyxml_unlink() to remove
603 * part of the tree or to unlink (separate) a subtree.
604 *
Radek Krejcib50551c2016-04-19 09:15:38 +0200605 * XML parser is used internally by libyang for parsing YIN schemas and data instances in XML format.
Radek Krejcidef50022016-02-01 16:38:32 +0100606 *
607 * \note API for this group of functions is described in the [XML Parser module](@ref xmlparser).
608 *
609 * Functions List
610 * --------------
Radek Krejci722b0072016-02-01 17:09:45 +0100611 * - lyxml_parse_mem()
612 * - lyxml_parse_path()
Radek Krejcidef50022016-02-01 16:38:32 +0100613 * - lyxml_get_attr()
614 * - lyxml_get_ns()
Radek Krejci722b0072016-02-01 17:09:45 +0100615 * - lyxml_print_mem()
616 * - lyxml_print_fd()
617 * - lyxml_print_file()
618 * - lyxml_print_clb()
Radek Krejcidef50022016-02-01 16:38:32 +0100619 * - lyxml_unlink()
620 * - lyxml_free()
621 */
622
623/**
624 * @page howtothreads libyang in Threads
625 *
Radek Krejcib50551c2016-04-19 09:15:38 +0200626 * libyang can be used in multithreaded applications keeping in mind the following rules:
Radek Krejcidef50022016-02-01 16:38:32 +0100627 * - 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 +0200628 * thread before any other work with context, schemas or data instances. Destroying the context is supposed to
Radek Krejcidef50022016-02-01 16:38:32 +0100629 * be done when no other thread accesses context, schemas nor data trees
630 * - Data parser (\b lyd_parse*() functions) can be used simultaneously in multiple threads (also the returned
631 * #ly_errno is thread safe).
632 * - Modifying (lyd_new(), lyd_insert(), lyd_unlink(), lyd_free() and many other functions) a single data tree is not
633 * thread safe.
Radek Krejci26715a42015-07-29 14:10:45 +0200634 */
Radek Krejci94ca54b2015-07-08 15:48:47 +0200635
Radek Krejcida04f4a2015-05-21 12:54:09 +0200636/**
Radek Krejci26715a42015-07-29 14:10:45 +0200637 *
638 * @page howtologger Logger
639 *
640 * There are 4 verbosity levels defined as ::LY_LOG_LEVEL. The level can be
641 * changed by the ly_verb() function. By default, the verbosity level is
642 * set to #LY_LLERR value.
643 *
Radek Krejcib50551c2016-04-19 09:15:38 +0200644 * When an error is encountered, the error message and error number are stored for
645 * later use. Caller is able to access the last error message via ly_errmsg() and the
646 * corresponding last error code via #ly_errno. If that was a validation error (#ly_errno
647 * is set to #LY_EVALID), also validation error code (via #ly_vecode) and path to the
648 * error node (via ly_errpath()) are available.
649 *
650 * For some specific cases, a YANG schema can define error message and/or error tag (mainly for
651 * use in NETCONF). If a message iss set, it is provided via ly_errmsg(). If a tag is set in schema
652 * it is available via ly_erraptag() (if not set, the returned string is empty).
653 *
654 * By default, all libyang messages are printed to `stderr`. However, caller is able to set its own logging
655 * callback function. In that case, instead of printing messages, libyang passes error level, message and path
656 * (if any) to the caller's callback function. In case of error level, the message and path are still
657 * automatically stored and available via the functions and macros described above.
Radek Krejci26715a42015-07-29 14:10:45 +0200658 *
Radek Krejcidef50022016-02-01 16:38:32 +0100659 * \note API for this group of functions is described in the [logger module](@ref logger).
660 *
661 * Functions List
662 * --------------
663 * - ly_verb()
664 * - ly_set_log_clb()
665 * - ly_get_log_clb()
Radek Krejcib50551c2016-04-19 09:15:38 +0200666 * - ly_errmsg()
667 * - ly_errpath()
668 * - ly_errapptag()
669 * - #ly_errno
670 * - #ly_vecode
Radek Krejci26715a42015-07-29 14:10:45 +0200671 */
672
673/**
674 * @defgroup context Context
Radek Krejci3045cf32015-05-28 10:58:52 +0200675 * @{
676 *
Radek Krejci26715a42015-07-29 14:10:45 +0200677 * Structures and functions to manipulate with the libyang "containers". The \em context concept allows callers
678 * to work in environments with different sets of YANG schemas. More detailed information can be found at
679 * @ref howtocontext page.
Radek Krejci3045cf32015-05-28 10:58:52 +0200680 */
681
682/**
Radek Krejcida04f4a2015-05-21 12:54:09 +0200683 * @brief libyang context handler.
684 */
685struct ly_ctx;
686
687/**
688 * @brief Create libyang context
689 *
Radek Krejci26715a42015-07-29 14:10:45 +0200690 * Context is used to hold all information about schemas. Usually, the application is supposed
Radek Krejci91b833c2015-09-04 11:49:43 +0200691 * to work with a single context in which libyang is holding all schemas (and other internal
692 * information) according to which the data trees will be processed and validated. So, the schema
693 * trees are tightly connected with the specific context and they are held by the context internally
694 * - caller does not need to keep pointers to the schemas returned by lys_parse(), context knows
695 * about them. The data trees created with lyd_parse() are still connected with the specific context,
696 * but they are not internally held by the context. The data tree just points and lean on some data
697 * held by the context (schema tree, string dictionary, etc.). Therefore, in case of data trees, caller
698 * is supposed to keep pointers returned by the lyd_parse() and manage the data tree on its own. This
699 * also affects the number of instances of both tree types. While you can have only one instance of
700 * specific schema connected with a single context, number of data tree instances is not connected.
Radek Krejcida04f4a2015-05-21 12:54:09 +0200701 *
Radek Krejci26715a42015-07-29 14:10:45 +0200702 * @param[in] search_dir Directory where libyang will search for the imported or included modules
703 * and submodules. If no such directory is available, NULL is accepted.
Radek Krejcida04f4a2015-05-21 12:54:09 +0200704 *
Radek Krejci3045cf32015-05-28 10:58:52 +0200705 * @return Pointer to the created libyang context, NULL in case of error.
Radek Krejcida04f4a2015-05-21 12:54:09 +0200706 */
707struct ly_ctx *ly_ctx_new(const char *search_dir);
708
709/**
Michal Vasko60ba9a62015-07-03 14:42:31 +0200710 * @brief Change the search path in libyang context
711 *
712 * @param[in] ctx Context to be modified.
713 * @param[in] search_dir New search path to replace the current one in ctx.
714 */
715void ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir);
716
717/**
Radek Krejci5a797572015-10-21 15:45:45 +0200718 * @brief Get current value of the search path in libyang context
719 *
720 * @param[in] ctx Context to query.
721 * @return Current value of the search path.
722 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100723const char *ly_ctx_get_searchdir(const struct ly_ctx *ctx);
Radek Krejci5a797572015-10-21 15:45:45 +0200724
725/**
Radek Krejci7ab25152015-08-07 14:48:45 +0200726 * @brief Get data of an internal ietf-yang-library module.
727 *
728 * @param[in] ctx Context with the modules.
729 * @return Root data node corresponding to the model, NULL on error.
730 * Caller is responsible for freeing the returned data tree using lyd_free().
731 */
732struct lyd_node *ly_ctx_info(struct ly_ctx *ctx);
733
734/**
Michal Vaskod7957c02016-04-01 10:27:26 +0200735 * @brief Iterate over all modules in a context.
736 *
737 * @param[in] ctx Context with the modules.
738 * @param[in,out] idx Index of the next module to be returned. Value of 0 starts from the beginning.
739 * @return Next context module, NULL if the last was already returned.
740 */
741const struct lys_module *ly_ctx_get_module_iter(const struct ly_ctx *ctx, uint32_t *idx);
742
743/**
Radek Krejcifd4e6e32015-08-10 15:00:51 +0200744 * @brief Get pointer to the schema tree of the module of the specified name.
Radek Krejcida04f4a2015-05-21 12:54:09 +0200745 *
Radek Krejcida04f4a2015-05-21 12:54:09 +0200746 * @param[in] ctx Context to work in.
747 * @param[in] name Name of the YANG module to get.
Radek Krejcif647e612015-07-30 11:36:07 +0200748 * @param[in] revision Optional revision date of the YANG module to get. If not specified,
749 * the schema in the newest revision is returned if any.
750 * @return Pointer to the data model structure, NULL if no schema following the name and
Radek Krejcifd4e6e32015-08-10 15:00:51 +0200751 * revision requirements is present in the context.
Radek Krejcida04f4a2015-05-21 12:54:09 +0200752 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100753const struct lys_module *ly_ctx_get_module(const struct ly_ctx *ctx, const char *name, const char *revision);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200754
755/**
Radek Krejci21601a32016-03-07 11:39:27 +0100756 * @brief Get pointer to the older schema tree to the specified one in the provided context.
757 *
758 * The module is not necessarily from the provided \p ctx. If there are multiple schemas older than the
759 * provided one, the newest of them is returned.
760 *
761 * The function can be used in combination with ly_ctx_get_module() to get all revisions of a module in a context:
762 * \code{.c}
763 * for (mod = ly_ctx_get_module(ctx, name, NULL); mod; mod = ly_ctx_get_module_older(ctx, mod)) {
764 * ...
765 * }
766 * \endcode
767 *
768 * @param[in] ctx Context to work in.
769 * @param[in] module YANG module to compare with
770 * @return Pointer to the data model structure, NULL if no older schema is present in the context.
771 */
772const struct lys_module *ly_ctx_get_module_older(const struct ly_ctx *ctx, const struct lys_module *module);
773
774/**
Michal Vasko99b0aad2015-12-01 12:28:51 +0100775 * @brief Try to find the model in the searchpath of \p ctx and load it into it. If custom missing
776 * module callback is set, it is used instead.
Michal Vasko82465962015-11-10 11:03:11 +0100777 *
778 * @param[in] ctx Context to add to.
Michal Vasko82465962015-11-10 11:03:11 +0100779 * @param[in] name Name of the module to load.
780 * @param[in] revision Optional revision date of the module. If not specified, it is
781 * assumed that there is only one model revision in the searchpath (the first matching file
782 * is parsed).
783 * @return Pointer to the data model structure, NULL if not found or some error occured.
784 */
Michal Vasko99b0aad2015-12-01 12:28:51 +0100785const struct lys_module *ly_ctx_load_module(struct ly_ctx *ctx, const char *name, const char *revision);
786
787/**
788 * @brief Callback for retrieving missing included or imported models in a custom way.
789 *
790 * @param[in] name Missing module name.
791 * @param[in] revision Optional missing module revision.
792 * @param[in] user_data User-supplied callback data.
793 * @param[out] format Format of the returned module data.
Michal Vasko880dceb2016-03-03 15:44:56 +0100794 * @param[out] free_module_data Callback for freeing the returned module data. If not set, the data will be left untouched.
Michal Vasko99b0aad2015-12-01 12:28:51 +0100795 * @return Requested module data or NULL on error.
796 */
797typedef char *(*ly_module_clb)(const char *name, const char *revision, void *user_data, LYS_INFORMAT *format,
Michal Vaskod3e975b2016-03-03 15:40:21 +0100798 void (**free_module_data)(void *model_data));
Michal Vasko99b0aad2015-12-01 12:28:51 +0100799
800/**
801 * @brief Set missing include or import model callback.
802 *
803 * @param[in] ctx Context that will use this callback.
804 * @param[in] clb Callback responsible for returning a missing model.
805 * @param[in] user_data Arbitrary data that will always be passed to the callback \p clb.
806 */
807void ly_ctx_set_module_clb(struct ly_ctx *ctx, ly_module_clb clb, void *user_data);
808
809/**
810 * @brief Get the custom callback for missing module retrieval.
811 *
812 * @param[in] ctx Context to read from.
813 * @param[in] user_data Optional pointer for getting the user-supplied callbck data.
814 * @return Custom user missing module callback or NULL if not set.
815 */
816ly_module_clb ly_ctx_get_module_clb(const struct ly_ctx *ctx, void **user_data);
Michal Vasko82465962015-11-10 11:03:11 +0100817
818/**
Radek Krejcifd4e6e32015-08-10 15:00:51 +0200819 * @brief Get pointer to the schema tree of the module of the specified namespace
820 *
821 * @param[in] ctx Context to work in.
822 * @param[in] ns Namespace of the YANG module to get.
823 * @param[in] revision Optional revision date of the YANG module to get. If not specified,
824 * the schema in the newest revision is returned if any.
825 * @return Pointer to the data model structure, NULL if no schema following the namespace and
826 * revision requirements is present in the context.
827 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100828const struct lys_module *ly_ctx_get_module_by_ns(const struct ly_ctx *ctx, const char *ns, const char *revision);
Radek Krejcifd4e6e32015-08-10 15:00:51 +0200829
830/**
Radek Krejci62f0da72016-03-07 11:35:43 +0100831 * @brief Get submodule of a main module.
832 *
833 * 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 +0200834 *
Radek Krejcia7533f22016-03-07 07:37:45 +0100835 * @param[in] ctx Context to work in.
Michal Vaskof6d94c62016-04-05 11:21:54 +0200836 * @param[in] module Name of the main (belongs-to) module. If NULL, all module submodules are searched.
837 * @param[in] revision Optional revision date of \p module. If NULL, all revisions of \p module
838 * are searched. If set, \p module must also be set.
Radek Krejcia7533f22016-03-07 07:37:45 +0100839 * @param[in] submodule Name of the submodule to get.
Michal Vaskof6d94c62016-04-05 11:21:54 +0200840 * @param[in] sub_revision Optional revision date of \p submodule. If NULL, the newest revision of \p submodule
841 * is returned.
Michal Vasko7bf06882015-07-03 15:33:56 +0200842 * @return Pointer to the data model structure.
843 */
Radek Krejcia7533f22016-03-07 07:37:45 +0100844const 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 +0200845 const char *submodule, const char *sub_revision);
Michal Vasko7bf06882015-07-03 15:33:56 +0200846
847/**
Radek Krejci62f0da72016-03-07 11:35:43 +0100848 * @brief Get submodule of a main module.
849 *
850 * If you have only the name (and optionally revision) of the submodule's main module, use ly_ctx_get_submodule()
851 * instead.
852 *
853 * @param[in] main_module Main module (belongs to) of the searched submodule.
854 * @param[in] submodule Name of the submodule to get.
855 * @return Pointer to the data model structure.
856 */
857const struct lys_submodule *ly_ctx_get_submodule2(const struct lys_module *main_module, const char *submodule);
858
859/**
Michal Vasko3547c532016-03-14 09:40:50 +0100860 * @brief Get schema node according to the given schema node identifier in JSON format.
Michal Vasko3edeaf72016-02-11 13:17:43 +0100861 *
Michal Vasko3547c532016-03-14 09:40:50 +0100862 * If the \p nodeid is absolute, the first node identifier must be prefixed with
863 * the module name. Then every other identifier either has an explicit module name or
864 * the module name of the previous node is assumed. Examples:
Michal Vasko3edeaf72016-02-11 13:17:43 +0100865 *
866 * /ietf-netconf-monitoring:get-schema/input/identifier
867 * /ietf-interfaces:interfaces/interface/ietf-ip:ipv4/address/ip
868 *
Michal Vasko3547c532016-03-14 09:40:50 +0100869 * If the \p nodeid is relative, \p start is mandatory and is the starting point
870 * for the resolution. The first node identifier does not need a module name.
871 *
Michal Vasko3edeaf72016-02-11 13:17:43 +0100872 * @param[in] ctx Context to work in.
Michal Vasko3547c532016-03-14 09:40:50 +0100873 * @param[in] start Starting node for a relative schema node identifier, in which
874 * case it is mandatory.
875 * @param[in] nodeid JSON schema node identifier.
Michal Vasko3edeaf72016-02-11 13:17:43 +0100876 * @return Resolved schema node or NULL.
877 */
Michal Vasko3547c532016-03-14 09:40:50 +0100878const struct lys_node *ly_ctx_get_node(struct ly_ctx *ctx, const struct lys_node *start, const char *nodeid);
Michal Vasko3edeaf72016-02-11 13:17:43 +0100879
880/**
Michal Vasko9fd98e22016-04-07 15:44:19 +0200881 * @brief Get schema node according to the given data node identifier in JSON format.
882 *
883 * The functionality is almost the same as ly_ctx_get_node(), but this function accepts
884 * the data node identifier format (skipped choices, cases, inputs, and outputs). Examples:
885 *
886 * /ietf-netconf-monitoring:get-schema/identifier
887 * /ietf-interfaces:interfaces/interface/ietf-ip:ipv4/address/ip
888 *
889 * Since input and output is skipped, there could arise ambiguities if one RPC input
890 * contains a parameter with the same name as is in output, hence the flag.
891 *
892 * @param[in] ctx Context to work in.
893 * @param[in] start Starting node for a relative schema node identifier, in which
894 * case it is mandatory.
895 * @param[in] nodeid JSON schema node identifier.
896 * @param[in] rpc_output Whether to search in RPC output parameters instead input ones.
897 * @return Resolved schema node or NULL.
898 */
899const struct lys_node *ly_ctx_get_node2(struct ly_ctx *ctx, const struct lys_node *start, const char *nodeid, int rpc_output);
900
901/**
Radek Krejci3045cf32015-05-28 10:58:52 +0200902 * @brief Free all internal structures of the specified context.
903 *
904 * The function should be used before terminating the application to destroy
905 * and free all structures internally used by libyang. If the caller uses
906 * multiple contexts, the function should be called for each used context.
907 *
908 * All instance data are supposed to be freed before destroying the context.
909 * Data models are destroyed automatically as part of ly_ctx_destroy() call.
910 *
911 * @param[in] ctx libyang context to destroy
Radek Krejcifa0b5e02016-02-04 13:57:03 +0100912 * @param[in] private_destructor Optional destructor function for private objects assigned
913 * to the nodes via lys_set_private(). If NULL, the private objects are not freed by libyang.
Radek Krejcida04f4a2015-05-21 12:54:09 +0200914 */
Radek Krejcifa0b5e02016-02-04 13:57:03 +0100915void ly_ctx_destroy(struct ly_ctx *ctx, void (*private_destructor)(const struct lys_node *node, void *priv));
Radek Krejcida04f4a2015-05-21 12:54:09 +0200916
Radek Krejci26715a42015-07-29 14:10:45 +0200917/**@} context */
918
919/**
Radek Krejcidef50022016-02-01 16:38:32 +0100920 * @defgroup nodeset Tree nodes set
Radek Krejcidc154432016-01-21 11:10:59 +0100921 * @ingroup datatree
922 * @ingroup schematree
923 * @{
924 *
Radek Krejcidef50022016-02-01 16:38:32 +0100925 * Structure and functions to hold and manipulate with sets of nodes from schema or data trees.
926 */
927
928/**
Radek Krejci8f08df12016-03-21 11:11:30 +0100929 * @brief set array of ::ly_set
930 * It is kept in union to keep ::ly_set generic for data as well as schema trees
931 */
932union ly_set_set {
933 struct lys_node **s; /**< array of pointers to a ::lys_node objects */
934 struct lyd_node **d; /**< array of pointers to a ::lyd_node objects */
935 void **g; /**< dummy array for generic work */
936};
937
938/**
Radek Krejcidc154432016-01-21 11:10:59 +0100939 * @brief Structure to hold a set of (not necessary somehow connected) ::lyd_node or ::lys_node objects.
940 * Caller is supposed to not mix the type of objects added to the set and according to its knowledge about
941 * the set content, it is supposed to access the set via the sset, dset or set members of the structure.
942 *
Radek Krejcidef50022016-02-01 16:38:32 +0100943 * To free the structure, use ly_set_free() function, to manipulate with the structure, use other
944 * ly_set_* functions.
Radek Krejcidc154432016-01-21 11:10:59 +0100945 */
946struct ly_set {
947 unsigned int size; /**< allocated size of the set array */
948 unsigned int number; /**< number of elements in (used size of) the set array */
Radek Krejci8f08df12016-03-21 11:11:30 +0100949 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 +0100950};
951
952/**
Radek Krejcidef50022016-02-01 16:38:32 +0100953 * @brief Create and initiate new ::ly_set structure.
Radek Krejcidc154432016-01-21 11:10:59 +0100954 *
Radek Krejcidef50022016-02-01 16:38:32 +0100955 * @return Created ::ly_set structure or NULL in case of error.
Radek Krejcidc154432016-01-21 11:10:59 +0100956 */
957struct ly_set *ly_set_new(void);
958
959/**
960 * @brief Add a ::lyd_node or ::lys_node object into the set
961 *
962 * @param[in] set Set where the \p node will be added.
963 * @param[in] node The ::lyd_node or ::lys_node object to be added into the \p set;
964 * @return 0 on success
965 */
966int ly_set_add(struct ly_set *set, void *node);
967
968/**
969 * @brief Remove a ::lyd_node or ::lys_node object from the set.
970 *
971 * Note that after removing a node from a set, indexes of other nodes in the set can change
972 * (the last object is placed instead of the removed object).
973 *
974 * @param[in] set Set from which the \p node will be removed.
975 * @param[in] node The ::lyd_node or ::lys_node object to be removed from the \p set;
976 * @return 0 on success
977 */
978int ly_set_rm(struct ly_set *set, void *node);
979
980/**
981 * @brief Remove a ::lyd_node or ::lys_node object from the set index.
982 *
983 * Note that after removing a node from a set, indexes of other nodes in the set can change
984 * (the last object is placed instead of the removed object).
985 *
986 * @param[in] set Set from which a node will be removed.
987 * @param[in] index Index of the ::lyd_node or ::lys_node object in the \p set to be removed from the \p set;
988 * @return 0 on success
989 */
990int ly_set_rm_index(struct ly_set *set, unsigned int index);
991
992/**
Radek Krejcidef50022016-02-01 16:38:32 +0100993 * @brief Free the ::ly_set data. Frees only the set structure content, not the referred data.
Radek Krejcidc154432016-01-21 11:10:59 +0100994 *
995 * @param[in] set The set to be freed.
996 */
997void ly_set_free(struct ly_set *set);
998
Radek Krejcidef50022016-02-01 16:38:32 +0100999/**@} nodeset */
Radek Krejci6140e4e2015-10-09 15:50:55 +02001000
1001/**
Radek Krejci5044be32016-01-18 17:05:51 +01001002 * @defgroup printerflags Printer flags
Radek Krejcidef50022016-02-01 16:38:32 +01001003 * @ingroup datatree
Radek Krejci5044be32016-01-18 17:05:51 +01001004 *
1005 * Validity flags for data nodes.
1006 *
1007 * @{
1008 */
1009#define LYP_WITHSIBLINGS 0x01 /**< Flag for printing also the (following) sibling nodes of the data node. */
Michal Vasko95068c42016-03-24 14:58:11 +01001010#define LYP_FORMAT 0x02 /**< Flag for formatted output. */
Radek Krejci5044be32016-01-18 17:05:51 +01001011
1012/**
1013 * @}
1014 */
1015
1016/**
Radek Krejci3045cf32015-05-28 10:58:52 +02001017 * @defgroup logger Logger
1018 * @{
1019 *
1020 * Publicly visible functions and values of the libyang logger. For more
1021 * information, see \ref howtologger.
1022 */
1023
1024/**
1025 * @typedef LY_LOG_LEVEL
1026 * @brief Verbosity levels of the libyang logger.
1027 */
1028typedef enum {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001029 LY_LLERR, /**< Print only error messages. */
1030 LY_LLWRN, /**< Print error and warning messages. */
1031 LY_LLVRB, /**< Besides errors and warnings, print some other verbose messages. */
1032 LY_LLDBG /**< Print all messages including some development debug messages. */
Radek Krejci3045cf32015-05-28 10:58:52 +02001033} LY_LOG_LEVEL;
1034
1035/**
1036 * @brief Set logger verbosity level.
1037 * @param[in] level Verbosity level.
1038 */
1039void ly_verb(LY_LOG_LEVEL level);
1040
1041/**
Michal Vaskof1d62cf2015-12-07 13:17:11 +01001042 * @brief Set logger callback.
Michal Vasko13661142016-04-11 10:53:53 +02001043 *
1044 * !IMPORTANT! If an error has a specific error-app-tag defined in the model, it will NOT be set
1045 * at the time of calling this callback. It will be set right after, so to retrieve it
1046 * it must be checked afterwards with ly_errapptag().
1047 *
Michal Vaskof1d62cf2015-12-07 13:17:11 +01001048 * @param[in] clb Logging callback.
Radek Krejciadb57612016-02-16 13:34:34 +01001049 * @param[in] path flag to resolve and provide path as the third parameter of the callback function. In case of
1050 * validation and some other errors, it can be useful to get the path to the problematic element. Note,
1051 * that according to the tree type and the specific situation, the path can slightly differs (keys
1052 * presence) or it can be NULL, so consider it as an optional parameter. If the flag is 0, libyang will
1053 * not bother with resolving the path.
Michal Vaskof1d62cf2015-12-07 13:17:11 +01001054 */
Radek Krejciadb57612016-02-16 13:34:34 +01001055void 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 +01001056
1057/**
1058 * @brief Get logger callback.
1059 * @return Logger callback (can be NULL).
1060 */
Radek Krejciadb57612016-02-16 13:34:34 +01001061void (*ly_get_log_clb(void))(LY_LOG_LEVEL, const char *, const char *);
Michal Vaskof1d62cf2015-12-07 13:17:11 +01001062
1063/**
Radek Krejci3045cf32015-05-28 10:58:52 +02001064 * @typedef LY_ERR
Radek Krejci26715a42015-07-29 14:10:45 +02001065 * @brief libyang's error codes available via ly_errno extern variable.
Radek Krejci9b4ca392015-04-10 08:31:27 +02001066 * @ingroup logger
1067 */
1068typedef enum {
Radek Krejciae6817a2015-08-10 14:02:06 +02001069 LY_SUCCESS, /**< no error, not set by functions, included just to complete #LY_ERR enumeration */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001070 LY_EMEM, /**< Memory allocation failure */
1071 LY_ESYS, /**< System call failure */
1072 LY_EINVAL, /**< Invalid value */
1073 LY_EINT, /**< Internal error */
1074 LY_EVALID /**< Validation failure */
Radek Krejci3045cf32015-05-28 10:58:52 +02001075} LY_ERR;
Radek Krejci7d9f46a2016-01-29 13:53:18 +01001076
Radek Krejci26715a42015-07-29 14:10:45 +02001077/**
Michal Vaskof5035ce2016-03-11 10:21:31 +01001078 * @typedef LY_VECODE
1079 * @brief libyang's codes of validation error. Whenever ly_errno is set to LY_EVALID, the ly_vecode is also set
1080 * to the appropriate LY_VECODE value.
Radek Krejcia37b39c2016-03-09 16:38:18 +01001081 * @ingroup logger
1082 */
1083typedef enum {
Michal Vaskof5035ce2016-03-11 10:21:31 +01001084 LYVE_SUCCESS = 0, /**< no error */
Radek Krejcia37b39c2016-03-09 16:38:18 +01001085
Michal Vaskof5035ce2016-03-11 10:21:31 +01001086 LYVE_XML_MISS, /**< missing XML object */
1087 LYVE_XML_INVAL, /**< invalid XML object */
1088 LYVE_XML_INCHAR, /**< invalid XML character */
Radek Krejcia37b39c2016-03-09 16:38:18 +01001089
Michal Vaskof5035ce2016-03-11 10:21:31 +01001090 LYVE_EOF, /**< unexpected end of input data */
1091 LYVE_INSTMT, /**< invalid statement (schema) */
1092 /* */
1093 LYVE_INID, /**< invalid identifier (schema) */
1094 LYVE_INDATE, /**< invalid date format */
1095 LYVE_INARG, /**< invalid value of a statement argument (schema) */
1096 LYVE_MISSSTMT, /**< missing required statement (schema) */
1097 /* */
1098 LYVE_MISSARG, /**< missing required statement argument (schema) */
1099 LYVE_TOOMANY, /**< too many instances of some object */
1100 LYVE_DUPID, /**< duplicated identifier (schema) */
1101 LYVE_DUPLEAFLIST, /**< multiple instances of leaf-list */
1102 LYVE_DUPLIST, /**< multiple instances of list */
Michal Vaskoa540df22016-04-11 16:14:35 +02001103 LYVE_NOUNIQ, /**< unique leaves match on 2 list instances (data) */
Michal Vaskof5035ce2016-03-11 10:21:31 +01001104 LYVE_ENUM_DUPVAL, /**< duplicated enum value (schema) */
1105 LYVE_ENUM_DUPNAME, /**< duplicated enum name (schema) */
1106 LYVE_ENUM_WS, /**< enum name with leading/trailing whitespaces (schema) */
1107 LYVE_BITS_DUPVAL, /**< duplicated bits value (schema) */
1108 LYVE_BITS_DUPNAME, /**< duplicated bits name (schema) */
1109 LYVE_INMOD, /**< invalid module name */
1110 /* */
1111 LYVE_KEY_NLEAF, /**< list key is not a leaf (schema) */
1112 LYVE_KEY_TYPE, /**< invalid list key type (schema) */
1113 LYVE_KEY_CONFIG, /**< key config value differs from the list config value */
1114 LYVE_KEY_MISS, /**< list key not found (schema) */
1115 LYVE_KEY_DUP, /**< duplicated key identifier (schema) */
1116 LYVE_INREGEX, /**< invalid regular expression (schema) */
1117 LYVE_INRESOLV, /**< no resolvents found (schema) */
1118 LYVE_INSTATUS, /**< invalid derivation because of status (schema) */
Radek Krejcia37b39c2016-03-09 16:38:18 +01001119
Michal Vaskof5035ce2016-03-11 10:21:31 +01001120 LYVE_OBSDATA, /**< obsolete data instantiation (data) */
1121 /* */
1122 LYVE_NORESOLV, /**< no resolvents found for an expression (data) */
1123 LYVE_INELEM, /**< invalid element (data) */
1124 /* */
1125 LYVE_MISSELEM, /**< missing required element (data) */
1126 LYVE_INVAL, /**< invalid value of an element (data) */
Radek Krejci9bfcbde2016-04-07 16:30:15 +02001127 LYVE_INVALATTR, /**< invalid attribute value (data) */
Michal Vaskof5035ce2016-03-11 10:21:31 +01001128 LYVE_INATTR, /**< invalid attribute in an element (data) */
1129 LYVE_MISSATTR, /**< missing attribute in an element (data) */
Michal Vasko6ac68282016-04-11 10:56:47 +02001130 LYVE_NOCONSTR, /**< value out of range/length/pattern (data) */
Michal Vaskof5035ce2016-03-11 10:21:31 +01001131 LYVE_INCHAR, /**< unexpected characters (data) */
1132 LYVE_INPRED, /**< predicate resolution fail (data) */
1133 LYVE_MCASEDATA, /**< data for more cases of a choice (data) */
Michal Vasko6ac68282016-04-11 10:56:47 +02001134 LYVE_NOMUST, /**< unsatisfied must condition (data) */
1135 LYVE_NOWHEN, /**< unsatisfied when condition (data) */
Michal Vaskof5035ce2016-03-11 10:21:31 +01001136 LYVE_INORDER, /**< invalid order of elements (data) */
Radek Krejci03b71f72016-03-16 11:10:09 +01001137 LYVE_INWHEN, /**< irresolvable when condition (data) */
Michal Vasko6ac68282016-04-11 10:56:47 +02001138 LYVE_NOMIN, /**< min-elements constraint not honored (data) */
1139 LYVE_NOMAX, /**< max-elements constraint not honored (data) */
1140 LYVE_NOREQINS, /**< required instance does not exits (data) */
1141 LYVE_NOLEAFREF, /**< leaf pointed to by leafref does not exist (data) */
1142 LYVE_NOMANDCHOICE, /**< no mandatory choice case branch exists (data) */
Radek Krejcia37b39c2016-03-09 16:38:18 +01001143
Michal Vaskof5035ce2016-03-11 10:21:31 +01001144 LYVE_XPATH_INTOK, /**< unexpected XPath token */
1145 LYVE_XPATH_EOF, /**< unexpected end of an XPath expression */
1146 LYVE_XPATH_INOP, /**< invalid XPath operation operands */
1147 /* */
1148 LYVE_XPATH_INCTX, /**< invalid XPath context type */
1149 LYVE_XPATH_INARGCOUNT, /**< invalid number of arguments for an XPath function */
Michal Vasko6fae1362016-03-11 15:10:00 +01001150 LYVE_XPATH_INARGTYPE, /**< invalid type of arguments for an XPath function */
1151
1152 LYVE_PATH_INCHAR, /**< invalid characters (path) */
Michal Vaskoe733d682016-03-14 09:08:27 +01001153 LYVE_PATH_INMOD, /**< invalid module name (path) */
1154 LYVE_PATH_MISSMOD, /**< missing module name (path) */
Michal Vasko6fae1362016-03-11 15:10:00 +01001155 LYVE_PATH_INNODE, /**< invalid node name (path) */
Michal Vasko6fae1362016-03-11 15:10:00 +01001156 LYVE_PATH_INKEY, /**< invalid key name (path) */
1157 LYVE_PATH_MISSKEY, /**< missing some list keys (path) */
1158 LYVE_PATH_EXISTS, /**< target node already exists (path) */
1159 LYVE_PATH_MISSPAR, /**< some parent of the target node is missing (path) */
Michal Vaskof5035ce2016-03-11 10:21:31 +01001160} LY_VECODE;
Radek Krejcia37b39c2016-03-09 16:38:18 +01001161
1162/**
Radek Krejci7d9f46a2016-01-29 13:53:18 +01001163 * @cond INTERNAL
Radek Krejci386714d2016-02-15 10:24:30 +01001164 * Get address of (thread-specific) `ly_errno' variable.
Radek Krejci26715a42015-07-29 14:10:45 +02001165 */
Radek Krejci7d9f46a2016-01-29 13:53:18 +01001166LY_ERR *ly_errno_location(void);
1167
Michal Vaskof5035ce2016-03-11 10:21:31 +01001168LY_VECODE *ly_vecode_location(void);
Radek Krejcia37b39c2016-03-09 16:38:18 +01001169
Radek Krejci7d9f46a2016-01-29 13:53:18 +01001170/**
1171 * @endcond INTERNAL
Radek Krejcidef50022016-02-01 16:38:32 +01001172 * @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 +01001173 */
1174#define ly_errno (*ly_errno_location())
Radek Krejci9b4ca392015-04-10 08:31:27 +02001175
Radek Krejci386714d2016-02-15 10:24:30 +01001176/**
Radek Krejcia37b39c2016-03-09 16:38:18 +01001177 * @brief libyang's validation error code
1178 */
Michal Vaskof5035ce2016-03-11 10:21:31 +01001179#define ly_vecode (*ly_vecode_location())
Radek Krejcia37b39c2016-03-09 16:38:18 +01001180
1181/**
Michal Vasko13661142016-04-11 10:53:53 +02001182 * @brief Get the last (thread-specific) error message. If the coresponding module defined
1183 * a specific error message, it will be used instead the default one.
Radek Krejci6e8fc0b2016-02-16 14:33:37 +01001184 *
1185 * Sometimes, the error message is extended with path of the element where is the problem.
1186 * The path is available via ly_errpath().
1187 *
Radek Krejcib50551c2016-04-19 09:15:38 +02001188 * @return Text of the last error message, empty string if there is no error.
Radek Krejci386714d2016-02-15 10:24:30 +01001189 */
1190const char *ly_errmsg(void);
1191
Radek Krejci6e8fc0b2016-02-16 14:33:37 +01001192/**
1193 * @brief Get the last (thread-specific) path of the element where was an error.
1194 *
1195 * The path always corresponds to the error message available via ly_errmsg(), so
1196 * whenever a subsequent error message is printed, the path is erased or rewritten.
Radek Krejci3cc10962016-04-13 15:03:27 +02001197 * The path reflects the type of the processed tree - data path for data tree functions
1198 * and schema path in case of schema tree functions. In case of processing YIN schema
1199 * or XML data, the path can be just XML path. In such a case, the corresponding
1200 * ly_vecode (value 1-3) is set.
Radek Krejci6e8fc0b2016-02-16 14:33:37 +01001201 *
Radek Krejcib50551c2016-04-19 09:15:38 +02001202 * @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 +01001203 */
1204const char *ly_errpath(void);
1205
Michal Vasko13661142016-04-11 10:53:53 +02001206/**
1207 * @brief Get the last (thread-specific) error-app-tag if there was a specific one defined
1208 * in the module for the last error.
1209 *
1210 * The app-tag always corresponds to the error message available via ly_errmsg(), so
1211 * whenever a subsequent error message is printed, the app-tag is erased or rewritten.
1212 *
Radek Krejcib50551c2016-04-19 09:15:38 +02001213 * @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 +02001214 */
1215const char *ly_errapptag(void);
1216
Radek Krejci3045cf32015-05-28 10:58:52 +02001217/**@} logger */
Radek Krejci9b4ca392015-04-10 08:31:27 +02001218
Radek Krejci39d8d0d2015-08-17 13:42:45 +02001219#ifdef __cplusplus
1220}
1221#endif
1222
Radek Krejci9b4ca392015-04-10 08:31:27 +02001223#endif /* LY_LIBYANG_H_ */