blob: abe4e6450da374d5e1e72a96eeedc309035069b1 [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 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of the Company nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 */
21
22#ifndef LY_LIBYANG_H_
23#define LY_LIBYANG_H_
24
Radek Krejcida04f4a2015-05-21 12:54:09 +020025#include <stdio.h>
26
Michal Vasko2d162e12015-09-24 14:33:29 +020027#include "tree_schema.h"
28#include "tree_data.h"
Radek Krejcic6704c82015-10-06 11:12:45 +020029#include "xml.h"
Radek Krejci41912fe2015-10-22 10:22:12 +020030#include "dict.h"
Radek Krejcida04f4a2015-05-21 12:54:09 +020031
Radek Krejci39d8d0d2015-08-17 13:42:45 +020032#ifdef __cplusplus
33extern "C" {
34#endif
35
Radek Krejci26715a42015-07-29 14:10:45 +020036/**
Radek Krejcidef50022016-02-01 16:38:32 +010037 * @mainpage About
38 *
39 * libyang is a library implementing processing of the YANG schemas and data modeled by the YANG language. The
40 * library is implemented in C for GNU/Linux and provides C API.
41 *
42 * @section about-features Main Features
43 *
44 * - Parsing (and validating) schemas in YIN format.
45 * - Parsing, validating and printing instance data in XML format.
46 * - Parsing, validating and printing instance data in JSON format.
47 * - Manipulation with the instance data.
48 *
49 * - \todo Parsing (and validating) schemas in YANG format.
50 *
51 * @subsection about-features-others Extra (side-effect) Features
52 *
53 * - XML parser.
54 * - Optimized string storage (dictionary).
55 *
56 * @section about-license License
57 *
58 * Copyright (c) 2015-2016 CESNET, z.s.p.o.
59 *
60 * (The BSD 3-Clause License)
61 *
62 * Redistribution and use in source and binary forms, with or without
63 * modification, are permitted provided that the following conditions
64 * are met:
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 * 3. Neither the name of the Company nor the names of its contributors
72 * may be used to endorse or promote products derived from this
73 * software without specific prior written permission.
74 */
75
76/**
Radek Krejci26715a42015-07-29 14:10:45 +020077 * @page howto How To ...
78 *
79 * - @subpage howtocontext
Radek Krejcid9ba3e32015-07-30 15:08:18 +020080 * - @subpage howtoschemas
81 * - @subpage howtodata
Radek Krejcidef50022016-02-01 16:38:32 +010082 * - @subpage howtoxml
83 * - @subpage howtothreads
Radek Krejci26715a42015-07-29 14:10:45 +020084 * - @subpage howtologger
85 */
Radek Krejcida04f4a2015-05-21 12:54:09 +020086
Radek Krejci26715a42015-07-29 14:10:45 +020087/** @page howtocontext Context
88 *
Radek Krejcid9ba3e32015-07-30 15:08:18 +020089 * The context concept allows callers to work in environments with different sets of YANG schemas.
Radek Krejci26715a42015-07-29 14:10:45 +020090 *
91 * The first step in libyang is to create a new context using ly_ctx_new(). It returns a handler
92 * used in the following work.
93 *
94 * When creating a new context, search dir can be specified (NULL is accepted) to provide directory
95 * where libyang will automatically search for schemas being imported or included. The search path
96 * can be later changed via ly_ctx_set_searchdir() function. Before exploring the specified search
97 * dir, libyang tries to get imported and included schemas from the current working directory first.
Radek Krejcidef50022016-02-01 16:38:32 +010098 * This automatic searching can be completely avoided when the caller sets module searching callback
99 * (#ly_module_clb) via ly_ctx_set_module_clb().
Radek Krejci26715a42015-07-29 14:10:45 +0200100 *
Radek Krejcidef50022016-02-01 16:38:32 +0100101 * Schemas are added into the context using [parser functions](@ref howtoschemasparsers) - \b lys_parse_*() or \b lyd_parse_*().
102 * In case of schemas, also ly_ctx_load_module() can be used - in that case the #ly_module_clb or automatic
103 * search in working directory and in the searchpath is used. Note, that functions for schemas have \b lys_
104 * prefix while functions for instance data have \b lyd_ prefix.
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200105 *
Radek Krejcif647e612015-07-30 11:36:07 +0200106 * Context can hold multiple revisons of the same schema.
Radek Krejci26715a42015-07-29 14:10:45 +0200107 *
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200108 * Context holds all modules and their submodules internally. The list of available module names is
109 * provided via ly_ctx_get_module_names() functions. Similarly, caller can get also a list of submodules
110 * names of a specific module using ly_ctx_get_submodule_names() function. The returned names can be
111 * subsequently used to get the (sub)module structures using ly_ctx_get_module() and ly_ctx_get_submodule().
Radek Krejcidef50022016-02-01 16:38:32 +0100112 * Alternatively, the ly_ctx_info() function can be used to get complex information about the schemas in the context
113 * in the form of data tree defined by
Radek Krejcibd9e8d22016-02-03 14:11:48 +0100114 * <a href="https://tools.ietf.org/html/draft-ietf-netconf-yang-library-04">ietf-yang-library</a> schema.
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200115 *
116 * Modules held by a context cannot be removed one after one. The only way how to \em change modules in the
117 * context is to create a new context and remove the old one. To remove a context, there is ly_ctx_destroy()
118 * function.
119 *
Radek Krejcidef50022016-02-01 16:38:32 +0100120 * - @subpage howtocontextdict
121 *
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200122 * \note API for this group of functions is available in the [context module](@ref context).
123 *
Radek Krejcidef50022016-02-01 16:38:32 +0100124 * Functions List
125 * --------------
126 * - ly_ctx_new()
127 * - ly_ctx_set_searchdir()
128 * - ly_ctx_get_searchdir()
129 * - ly_ctx_set_module_clb()
130 * - ly_ctx_get_module_clb()
131 * - ly_ctx_load_module()
132 * - ly_ctx_info()
133 * - ly_ctx_get_module_names()
134 * - ly_ctx_get_module()
135 * - ly_ctx_get_module_by_ns()
136 * - ly_ctx_get_submodule_names()
137 * - ly_ctx_get_submodule()
138 * - ly_ctx_destroy()
139 */
140
141/**
142 * @page howtocontextdict Context Dictionary
143 *
144 * Context includes dictionary to store strings more effectively. The most of strings repeats quite often in schema
145 * as well as data trees. Therefore, instead of allocating those strings each time they appear, libyang stores them
146 * as records in the dictionary. The basic API to the context dictionary is public, so even a caller application can
147 * use the dictionary.
148 *
149 * To insert a string into the dictionary, caller can use lydict_insert() (adding a constant string) or
150 * lydict_insert_zc() (for dynamically allocated strings that won't be used by the caller after its insertion into
151 * the dictionary). Both functions return the pointer to the inserted string in the dictionary record.
152 *
153 * To remove (reference of the) string from the context dictionary, lydict_remove() is supposed to be used.
154 *
155 * \note Incorrect usage of the dictionary can break libyang functionality.
156 *
157 * \note API for this group of functions is described in the [XML Parser module](@ref dict).
158 *
159 * Functions List
160 * --------------
161 * - lydict_insert()
162 * - lydict_insert_zc()
163 * - lydict_remove()
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200164 */
165
166/**
167 * @page howtoschemas Schemas
168 *
Radek Krejcidef50022016-02-01 16:38:32 +0100169 *
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200170 * Schema is an internal libyang's representation of a YANG data model. Each schema is connected with
Radek Krejcidef50022016-02-01 16:38:32 +0100171 * its [context](@ref howtocontext) and loaded using [parser functions](@ref howtoschemasparsers). It means, that
172 * the schema cannot be created (nor changed) programmatically. In libyang, schemas are used only to
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200173 * access data model definitions.
174 *
Radek Krejcidef50022016-02-01 16:38:32 +0100175 * Schema tree nodes are able to hold private objects (via a pointer to a structure, function, variable, ...) used by
176 * a caller application. Such an object can be assigned to a specific node using lys_set_private() function.
177 * Note that the object is not freed by libyang when the context is being destroyed. So the caller is responsible
178 * for freeing the provided structure after the context is destroyed or the private pointer is set to NULL in
179 * appropriate schema nodes where the object was previously set. On the other hand, freeing the object while the schema
180 * tree is still used can lead to a segmentation fault.
181 *
182 * - @subpage howtoschemasparsers
183 * - @subpage howtoschemasfeatures
184 * - @subpage howtoschemasprinters
185 *
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200186 * \note There are many functions to access information from the schema trees. Details are available in
187 * the [Schema Tree module](@ref schematree).
188 *
Radek Krejcidef50022016-02-01 16:38:32 +0100189 * Functions List (not assigned to above subsections)
190 * --------------------------------------------------
191 * - lys_get_node()
192 * - lys_get_next()
193 * - lys_parent()
194 * - lys_set_private()
195 */
196
197/**
198 * @page howtoschemasparsers Parsing Schemas
199 *
200 * Schema parser allows to read schema from a specific format. libyang supports the following schema formats:
201 *
202 * - YANG
203 *
204 * Basic YANG schemas format described in [RFC 6020](http://tools.ietf.org/html/rfc6020).
205 * Currently, only YANG 1.0 is supported.
206 *
207 * \todo YANG input is not yet implemented
208 *
209 * - YIN
210 *
211 * Alternative XML-based format to YANG. The details can be found in
212 * [RFC 6020](http://tools.ietf.org/html/rfc6020#section-11).
213 *
214 * When the [context](@ref howtocontext) is created, it already contains the following three schemas, which
215 * are implemented internally by libyang: *
216 * - ietf-inet-types@2013-07-15
217 * - ietf-yang-types@2013-07-15
218 * - ietf-yang-library@2015-07-03
219 *
220 * Other schemas can be added to the context manually as described in [context page](@ref howtocontext) by the functions
221 * listed below. Besides the schema parser functions, it is also possible to use ly_ctx_load_module() which tries to
222 * find the required schema automatically - using #ly_module_clb or automatic search in working directory and in the
223 * context's searchpath.
224 *
225 * Functions List
226 * --------------
Radek Krejci722b0072016-02-01 17:09:45 +0100227 * - lys_parse_mem()
Radek Krejcidef50022016-02-01 16:38:32 +0100228 * - lys_parse_fd()
229 * - lys_parse_path()
230 * - ly_ctx_set_module_clb()
231 * - ly_ctx_load_module()
232 */
233
234/**
235 * @page howtoschemasfeatures YANG Features Manipulation
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200236 *
237 * The group of functions prefixed by \b lys_features_ are used to access and manipulate with the schema's
238 * features.
239 *
240 * The first two functions are used to access information about the features in the schema.
241 * lys_features_list() provides list of all features defined in the specific schema and its
242 * submodules. Optionally, it can also provides information about the state of all features.
243 * Alternatively, caller can use lys_features_state() function to get state of one specific
244 * feature.
245 *
246 * The remaining two functions, lys_features_enable() and lys_features_disable(), are used
Radek Krejcidef50022016-02-01 16:38:32 +0100247 * to enable and disable the specific feature (or all via \b "*"). By default, when the module
248 * is loaded by libyang parser, all features are disabled.
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200249 *
Radek Krejcidef50022016-02-01 16:38:32 +0100250 * 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 +0200251 *
Radek Krejcidef50022016-02-01 16:38:32 +0100252 * Note, that the feature's state can affect some of the output formats (e.g. Tree format).
253 *
254 * Functions List
255 * --------------
256 * - lys_features_list()
257 * - lys_features_enable()
258 * - lys_features_disable()
259 * - lys_features_state()
260 * - lys_is_disabled()
261 */
262
263/**
264 * @page howtoschemasprinters Printing Schemas
265 *
266 * Schema printers allows to serialize internal representation of a schema module in a specific format. libyang
267 * supports the following schema formats for printing:
268 *
269 * - YANG
270 *
271 * Basic YANG schemas format described in [RFC 6020](http://tools.ietf.org/html/rfc6020).
272 * Currently, only YANG 1.0 is supported.
273 *
274 * - YIN
275 *
276 * Alternative XML-based format to YANG. The details can be found in
277 * [RFC 6020](http://tools.ietf.org/html/rfc6020#section-11).
278 *
279 * \todo YIN output is not yet implemented
280 *
281 * - Tree
282 *
283 * Simple tree structure of the module.
284 *
285 * - Info
286 *
287 * Detailed information about the specific node in the schema tree.
288 * It allows to print information not only about a specific module, but also about its specific part:
289 *
290 * - absolute-schema-nodeid
291 *
292 * e.g. \a `/modules/module-set-id` in \a `ietf-yang-library` module
293 *
294 * - <b>typedef/</b>typedef-name
295 *
296 * e.g. \a `typedef/revision-identifier` in \a `ietf-yang-library` module
297 *
298 * - <b>feature/</b>feature-name
299 *
300 * e.g. \a `feature/ssh` in \a `ietf-netconf-server` module
301 *
302 * - <b>grouping/</b>grouping-name/descendant-schema-nodeid
303 *
304 * e.g. \a `grouping/module` or \a `grouping/module/module/submodules` in \a `ietf-yang-library` module
305 *
306 * - <b>type/</b>leaf-or-leaflist
307 *
308 * e.g. \a `type/modules/module-set-id` in \a `ietf-yang-library` module
309 *
310 * Printer functions allow to print to the different outputs including a callback function which allows caller
311 * to have a full control of the output data - libyang passes to the callback a private argument (some internal
312 * data provided by a caller of lys_print_clb()), string buffer and number of characters to print. Note that the
313 * callback is supposed to be called multiple times during the lys_print_clb() execution.
314 *
315 * Functions List
316 * --------------
317 * - lys_print_mem()
318 * - lys_print_fd()
319 * - lys_print_file()
320 * - lys_print_clb()
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200321 */
322
323/**
324 * @page howtodata Data Instances
Radek Krejci26715a42015-07-29 14:10:45 +0200325 *
Radek Krejcidef50022016-02-01 16:38:32 +0100326 * All data nodes in data trees are connected with their schema node - libyang is not able to represent data of an
327 * unknown schema.
328 *
329 * By default, the represented data are supposed to represent a full YANG datastore content. So if a schema declares
330 * some mandatory nodes, despite configuration or status, the data are supposed to be present in the data tree being
331 * loaded or validated. However, it is possible to specify other kinds of data (see @ref parseroptions) allowing some
332 * exceptions to the validation process.
333 *
334 * Data validation is performed implicitly to the input data processed by the parser (\b lyd_parse_*() functions) and
335 * on demand via the lyd_validate() function. The lyd_validate() is supposed to be used when a (complex or simple)
336 * change is done on the data tree (via a combination of \b lyd_change_*(), \b lyd_insert*(), \b lyd_new*(),
337 * lyd_unlink() and lyd_free() functions).
338 *
339 * - @subpage howtodataparsers
340 * - @subpage howtodatamanipulators
341 * - @subpage howtodataprinters
342 *
343 * \note API for this group of functions is described in the [Data Instances module](@ref datatree).
344 *
345 * Functions List (not assigned to above subsections)
346 * --------------------------------------------------
347 * - lyd_get_node()
Michal Vasko105cef12016-02-04 12:06:26 +0100348 * - lyd_get_node2()
Radek Krejcidef50022016-02-01 16:38:32 +0100349 */
350
351/**
352 * @page howtodataparsers Parsing Data
353 *
354 * Data parser allows to read instances from a specific format. libyang supports the following data formats:
355 *
356 * - XML
357 *
358 * Original data format used in NETCONF protocol. XML mapping is part of the YANG specification
359 * ([RFC 6020](http://tools.ietf.org/html/rfc6020)).
360 *
361 * - JSON
362 *
363 * The alternative data format available in RESTCONF protocol. Specification of JSON encoding of data modeled by YANG
364 * can be found in [this draft](https://tools.ietf.org/html/draft-ietf-netmod-yang-json-05).
365 *
366 * Besides the format of input data, the parser functions accepts additional [options](@ref parseroptions) to specify
367 * how the input data should be processed.
368 *
369 * In contrast to the schema parser, data parser also accepts empty input data if such an empty data tree is valid
370 * according to the schemas in the libyang context.
371 *
372 * In case of XML input data, there is one additional way to parse input data. Besides parsing the data from a string
373 * in memory or a file, caller is able to build an XML tree using [libyang XML parser](@ref howtoxml) and then use
374 * this tree (or a part of it) as input to the lyd_parse_xml() function.
375 *
376 * Functions List
377 * --------------
Radek Krejci722b0072016-02-01 17:09:45 +0100378 * - lyd_parse_mem()
Radek Krejcidef50022016-02-01 16:38:32 +0100379 * - lyd_parse_fd()
380 * - lyd_parse_path()
381 * - lyd_parse_xml()
382 */
383
384/**
385 * @page howtodatamanipulators Manipulating Data
386 *
387 * There are many functions to create or modify an existing data tree. You can add new nodes, reconnect nodes from
388 * one tree to another (or e.g. from one list instance to another) or remove nodes. The functions doesn't allow you
389 * to put a node to a wrong place (by checking the module), but not all validation checks can be made directly
390 * (or you have to make a valid change by multiple tree modifications) when the tree is being changed. Therefore,
391 * there is lyd_validate() function supposed to be called to make sure that the current data tree is valid. Note,
392 * that not calling this function after the performed changes can cause failure of various libyang functions later.
393 *
394 * Also remember, that when you are creating/inserting a node, all the objects in that operation must belong to the
395 * same context.
396 *
397 * Modifying the single data tree in multiple threads is not safe.
398 *
399 * Functions List
400 * --------------
401 * - lyd_dup()
402 * - lyd_change_leaf()
403 * - lyd_insert()
404 * - lyd_insert_before()
405 * - lyd_insert_after()
406 * - lyd_insert_attr()
407 * - lyd_new()
408 * - lyd_new_anyxml()
409 * - lyd_new_leaf()
410 * - lyd_output_new()
411 * - lyd_output_new_anyxml()
412 * - lyd_output_new_leaf()
413 * - lyd_unlink()
414 * - lyd_free()
415 * - lyd_free_attr()
416 * - lyd_free_withsiblings()
417 * - lyd_validate()
418 */
419
420/**
421 * @page howtodataprinters Printing Data
422 *
423 * Schema printers allows to serialize internal representation of a schema module in a specific format. libyang
424 * supports the following schema formats for printing:
425 *
426 * - XML
427 *
428 * Basic format as specified in rules of mapping YANG modeled data to XML in
429 * [RFC 6020](http://tools.ietf.org/html/rfc6020). It is possible to specify if
430 * the indentation will be used.
431 *
432 * - JSON
433 *
434 * The alternative data format available in RESTCONF protocol. Specification of JSON encoding of data modeled by YANG
435 * can be found in [this draft](https://tools.ietf.org/html/draft-ietf-netmod-yang-json-05).
436 *
437 * Printer functions allow to print to the different outputs including a callback function which allows caller
438 * to have a full control of the output data - libyang passes to the callback a private argument (some internal
439 * data provided by a caller of lyd_print_clb()), string buffer and number of characters to print. Note that the
440 * callback is supposed to be called multiple times during the lyd_print_clb() execution.
441 *
442 * Functions List
443 * --------------
444 * - lyd_print_mem()
445 * - lyd_print_fd()
446 * - lyd_print_file()
447 * - lyd_print_clb()
448 */
449
450/**
451 * @page howtoxml libyang XML Support
452 *
453 * libyang XML parser is able to parse XML documents used to represent data modeled by YANG. Therefore, there are
454 * some limitations in comparison to a full-featured XML parsers:
455 * - comments are ignored
456 * - Doctype declaration is ignored
457 * - CData sections are ignored
458 * - Process Instructions (PI) are ignored
459 *
460 * The API is designed to almost only read-only access. You can simply load XML document, go through the tree as
461 * you wish and dump the tree to an output. The only "write" functions are lyxml_free() and lyxml_unlink() to remove
462 * part of the tree or to unlink (separate) a subtree.
463 *
464 * XML parser is also used internally by libyang for parsing YIN schemas and data instances in XML format.
465 *
466 * \note API for this group of functions is described in the [XML Parser module](@ref xmlparser).
467 *
468 * Functions List
469 * --------------
Radek Krejci722b0072016-02-01 17:09:45 +0100470 * - lyxml_parse_mem()
471 * - lyxml_parse_path()
Radek Krejcidef50022016-02-01 16:38:32 +0100472 * - lyxml_get_attr()
473 * - lyxml_get_ns()
Radek Krejci722b0072016-02-01 17:09:45 +0100474 * - lyxml_print_mem()
475 * - lyxml_print_fd()
476 * - lyxml_print_file()
477 * - lyxml_print_clb()
Radek Krejcidef50022016-02-01 16:38:32 +0100478 * - lyxml_unlink()
479 * - lyxml_free()
480 */
481
482/**
483 * @page howtothreads libyang in Threads
484 *
485 * libyang can be used in multithreaded application keeping in mind the following rules:
486 * - libyang context manipulation (adding new schemas) is not thread safe and it is supposed to be done in a main
487 * thread before any other work with context, schemas or data instances. And destroying the context is supposed to
488 * be done when no other thread accesses context, schemas nor data trees
489 * - Data parser (\b lyd_parse*() functions) can be used simultaneously in multiple threads (also the returned
490 * #ly_errno is thread safe).
491 * - Modifying (lyd_new(), lyd_insert(), lyd_unlink(), lyd_free() and many other functions) a single data tree is not
492 * thread safe.
Radek Krejci26715a42015-07-29 14:10:45 +0200493 */
Radek Krejci94ca54b2015-07-08 15:48:47 +0200494
Radek Krejcida04f4a2015-05-21 12:54:09 +0200495/**
Radek Krejci26715a42015-07-29 14:10:45 +0200496 *
497 * @page howtologger Logger
498 *
499 * There are 4 verbosity levels defined as ::LY_LOG_LEVEL. The level can be
500 * changed by the ly_verb() function. By default, the verbosity level is
501 * set to #LY_LLERR value.
502 *
503 * In case the logger has an error message (LY_LLERR) to print, also an error
504 * code is recorded in extern ly_errno variable. Possible values are of type
505 * ::LY_ERR.
506 *
Radek Krejcidef50022016-02-01 16:38:32 +0100507 * \note API for this group of functions is described in the [logger module](@ref logger).
508 *
509 * Functions List
510 * --------------
511 * - ly_verb()
512 * - ly_set_log_clb()
513 * - ly_get_log_clb()
Radek Krejci26715a42015-07-29 14:10:45 +0200514 */
515
516/**
517 * @defgroup context Context
Radek Krejci3045cf32015-05-28 10:58:52 +0200518 * @{
519 *
Radek Krejci26715a42015-07-29 14:10:45 +0200520 * Structures and functions to manipulate with the libyang "containers". The \em context concept allows callers
521 * to work in environments with different sets of YANG schemas. More detailed information can be found at
522 * @ref howtocontext page.
Radek Krejci3045cf32015-05-28 10:58:52 +0200523 */
524
525/**
Radek Krejcida04f4a2015-05-21 12:54:09 +0200526 * @brief libyang context handler.
527 */
528struct ly_ctx;
529
530/**
531 * @brief Create libyang context
532 *
Radek Krejci26715a42015-07-29 14:10:45 +0200533 * Context is used to hold all information about schemas. Usually, the application is supposed
Radek Krejci91b833c2015-09-04 11:49:43 +0200534 * to work with a single context in which libyang is holding all schemas (and other internal
535 * information) according to which the data trees will be processed and validated. So, the schema
536 * trees are tightly connected with the specific context and they are held by the context internally
537 * - caller does not need to keep pointers to the schemas returned by lys_parse(), context knows
538 * about them. The data trees created with lyd_parse() are still connected with the specific context,
539 * but they are not internally held by the context. The data tree just points and lean on some data
540 * held by the context (schema tree, string dictionary, etc.). Therefore, in case of data trees, caller
541 * is supposed to keep pointers returned by the lyd_parse() and manage the data tree on its own. This
542 * also affects the number of instances of both tree types. While you can have only one instance of
543 * specific schema connected with a single context, number of data tree instances is not connected.
Radek Krejcida04f4a2015-05-21 12:54:09 +0200544 *
Radek Krejci26715a42015-07-29 14:10:45 +0200545 * @param[in] search_dir Directory where libyang will search for the imported or included modules
546 * and submodules. If no such directory is available, NULL is accepted.
Radek Krejcida04f4a2015-05-21 12:54:09 +0200547 *
Radek Krejci3045cf32015-05-28 10:58:52 +0200548 * @return Pointer to the created libyang context, NULL in case of error.
Radek Krejcida04f4a2015-05-21 12:54:09 +0200549 */
550struct ly_ctx *ly_ctx_new(const char *search_dir);
551
552/**
Michal Vasko60ba9a62015-07-03 14:42:31 +0200553 * @brief Change the search path in libyang context
554 *
555 * @param[in] ctx Context to be modified.
556 * @param[in] search_dir New search path to replace the current one in ctx.
557 */
558void ly_ctx_set_searchdir(struct ly_ctx *ctx, const char *search_dir);
559
560/**
Radek Krejci5a797572015-10-21 15:45:45 +0200561 * @brief Get current value of the search path in libyang context
562 *
563 * @param[in] ctx Context to query.
564 * @return Current value of the search path.
565 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100566const char *ly_ctx_get_searchdir(const struct ly_ctx *ctx);
Radek Krejci5a797572015-10-21 15:45:45 +0200567
568/**
Radek Krejci7ab25152015-08-07 14:48:45 +0200569 * @brief Get data of an internal ietf-yang-library module.
570 *
571 * @param[in] ctx Context with the modules.
572 * @return Root data node corresponding to the model, NULL on error.
573 * Caller is responsible for freeing the returned data tree using lyd_free().
574 */
575struct lyd_node *ly_ctx_info(struct ly_ctx *ctx);
576
577/**
Radek Krejci96a10da2015-07-30 11:00:14 +0200578 * @brief Get the names of the loaded modules.
579 *
580 * @param[in] ctx Context with the modules.
581 * @return NULL-terminated array of the module names,
582 * NULL on error. The returned array must be freed by the caller, do not free
583 * names in the array. Also remember that the names will be freed with freeing
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200584 * the context.
Radek Krejci96a10da2015-07-30 11:00:14 +0200585 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100586const char **ly_ctx_get_module_names(const struct ly_ctx *ctx);
Radek Krejci96a10da2015-07-30 11:00:14 +0200587
588/**
589 * @brief Get the names of the loaded submodules of the specified module.
590 *
591 * @param[in] ctx Context with the modules.
592 * @param[in] module_name Name of the parent module.
593 * @return NULL-terminated array of submodule names of the parent module,
594 * NULL on error. The returned array must be freed by the caller, do not free
595 * names in the array. Also remember that the names will be freed with freeing
Radek Krejcid9ba3e32015-07-30 15:08:18 +0200596 * the context.
Radek Krejci96a10da2015-07-30 11:00:14 +0200597 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100598const char **ly_ctx_get_submodule_names(const struct ly_ctx *ctx, const char *module_name);
Radek Krejci96a10da2015-07-30 11:00:14 +0200599
600/**
Radek Krejcifd4e6e32015-08-10 15:00:51 +0200601 * @brief Get pointer to the schema tree of the module of the specified name.
Radek Krejcida04f4a2015-05-21 12:54:09 +0200602 *
Radek Krejcida04f4a2015-05-21 12:54:09 +0200603 * @param[in] ctx Context to work in.
604 * @param[in] name Name of the YANG module to get.
Radek Krejcif647e612015-07-30 11:36:07 +0200605 * @param[in] revision Optional revision date of the YANG module to get. If not specified,
606 * the schema in the newest revision is returned if any.
607 * @return Pointer to the data model structure, NULL if no schema following the name and
Radek Krejcifd4e6e32015-08-10 15:00:51 +0200608 * revision requirements is present in the context.
Radek Krejcida04f4a2015-05-21 12:54:09 +0200609 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100610const 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 +0200611
612/**
Michal Vasko99b0aad2015-12-01 12:28:51 +0100613 * @brief Try to find the model in the searchpath of \p ctx and load it into it. If custom missing
614 * module callback is set, it is used instead.
Michal Vasko82465962015-11-10 11:03:11 +0100615 *
616 * @param[in] ctx Context to add to.
Michal Vasko82465962015-11-10 11:03:11 +0100617 * @param[in] name Name of the module to load.
618 * @param[in] revision Optional revision date of the module. If not specified, it is
619 * assumed that there is only one model revision in the searchpath (the first matching file
620 * is parsed).
621 * @return Pointer to the data model structure, NULL if not found or some error occured.
622 */
Michal Vasko99b0aad2015-12-01 12:28:51 +0100623const struct lys_module *ly_ctx_load_module(struct ly_ctx *ctx, const char *name, const char *revision);
624
625/**
626 * @brief Callback for retrieving missing included or imported models in a custom way.
627 *
628 * @param[in] name Missing module name.
629 * @param[in] revision Optional missing module revision.
630 * @param[in] user_data User-supplied callback data.
631 * @param[out] format Format of the returned module data.
632 * @param[out] free_module_data Optional callback for freeing the returned module data. If not set, free() is used.
633 * @return Requested module data or NULL on error.
634 */
635typedef char *(*ly_module_clb)(const char *name, const char *revision, void *user_data, LYS_INFORMAT *format,
636 void (**free_module_data)(char *model_data));
637
638/**
639 * @brief Set missing include or import model callback.
640 *
641 * @param[in] ctx Context that will use this callback.
642 * @param[in] clb Callback responsible for returning a missing model.
643 * @param[in] user_data Arbitrary data that will always be passed to the callback \p clb.
644 */
645void ly_ctx_set_module_clb(struct ly_ctx *ctx, ly_module_clb clb, void *user_data);
646
647/**
648 * @brief Get the custom callback for missing module retrieval.
649 *
650 * @param[in] ctx Context to read from.
651 * @param[in] user_data Optional pointer for getting the user-supplied callbck data.
652 * @return Custom user missing module callback or NULL if not set.
653 */
654ly_module_clb ly_ctx_get_module_clb(const struct ly_ctx *ctx, void **user_data);
Michal Vasko82465962015-11-10 11:03:11 +0100655
656/**
Radek Krejcifd4e6e32015-08-10 15:00:51 +0200657 * @brief Get pointer to the schema tree of the module of the specified namespace
658 *
659 * @param[in] ctx Context to work in.
660 * @param[in] ns Namespace of the YANG module to get.
661 * @param[in] revision Optional revision date of the YANG module to get. If not specified,
662 * the schema in the newest revision is returned if any.
663 * @return Pointer to the data model structure, NULL if no schema following the namespace and
664 * revision requirements is present in the context.
665 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100666const 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 +0200667
668/**
Michal Vasko7bf06882015-07-03 15:33:56 +0200669 * @brief Get submodule from the context's search dir.
670 *
671 * @param[in] module Parent (belongs-to) module.
672 * @param[in] name Name of the YANG submodule to get.
673 * @param[in] revision Optional revision date of the YANG submodule to get. If
674 * not specified, the newest revision is returned (TODO).
Michal Vasko7bf06882015-07-03 15:33:56 +0200675 * @return Pointer to the data model structure.
676 */
Michal Vasko1e62a092015-12-01 12:27:20 +0100677const struct lys_submodule *ly_ctx_get_submodule(const struct lys_module *module, const char *name,
678 const char *revision);
Michal Vasko7bf06882015-07-03 15:33:56 +0200679
680/**
Radek Krejci3045cf32015-05-28 10:58:52 +0200681 * @brief Free all internal structures of the specified context.
682 *
683 * The function should be used before terminating the application to destroy
684 * and free all structures internally used by libyang. If the caller uses
685 * multiple contexts, the function should be called for each used context.
686 *
687 * All instance data are supposed to be freed before destroying the context.
688 * Data models are destroyed automatically as part of ly_ctx_destroy() call.
689 *
690 * @param[in] ctx libyang context to destroy
Radek Krejcida04f4a2015-05-21 12:54:09 +0200691 */
Radek Krejci3045cf32015-05-28 10:58:52 +0200692void ly_ctx_destroy(struct ly_ctx *ctx);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200693
Radek Krejci26715a42015-07-29 14:10:45 +0200694/**@} context */
695
696/**
Radek Krejcidef50022016-02-01 16:38:32 +0100697 * @defgroup nodeset Tree nodes set
Radek Krejcidc154432016-01-21 11:10:59 +0100698 * @ingroup datatree
699 * @ingroup schematree
700 * @{
701 *
Radek Krejcidef50022016-02-01 16:38:32 +0100702 * Structure and functions to hold and manipulate with sets of nodes from schema or data trees.
703 */
704
705/**
Radek Krejcidc154432016-01-21 11:10:59 +0100706 * @brief Structure to hold a set of (not necessary somehow connected) ::lyd_node or ::lys_node objects.
707 * Caller is supposed to not mix the type of objects added to the set and according to its knowledge about
708 * the set content, it is supposed to access the set via the sset, dset or set members of the structure.
709 *
Radek Krejcidef50022016-02-01 16:38:32 +0100710 * To free the structure, use ly_set_free() function, to manipulate with the structure, use other
711 * ly_set_* functions.
Radek Krejcidc154432016-01-21 11:10:59 +0100712 */
713struct ly_set {
714 unsigned int size; /**< allocated size of the set array */
715 unsigned int number; /**< number of elements in (used size of) the set array */
716 union {
717 struct lys_node **sset; /**< array of pointers to a ::lys_node objects */
718 struct lyd_node **dset; /**< array of pointers to a ::lyd_node objects */
719 void **set; /**< dummy array for generic work */
720 };
721};
722
723/**
Radek Krejcidef50022016-02-01 16:38:32 +0100724 * @brief Create and initiate new ::ly_set structure.
Radek Krejcidc154432016-01-21 11:10:59 +0100725 *
Radek Krejcidef50022016-02-01 16:38:32 +0100726 * @return Created ::ly_set structure or NULL in case of error.
Radek Krejcidc154432016-01-21 11:10:59 +0100727 */
728struct ly_set *ly_set_new(void);
729
730/**
731 * @brief Add a ::lyd_node or ::lys_node object into the set
732 *
733 * @param[in] set Set where the \p node will be added.
734 * @param[in] node The ::lyd_node or ::lys_node object to be added into the \p set;
735 * @return 0 on success
736 */
737int ly_set_add(struct ly_set *set, void *node);
738
739/**
740 * @brief Remove a ::lyd_node or ::lys_node object from the set.
741 *
742 * Note that after removing a node from a set, indexes of other nodes in the set can change
743 * (the last object is placed instead of the removed object).
744 *
745 * @param[in] set Set from which the \p node will be removed.
746 * @param[in] node The ::lyd_node or ::lys_node object to be removed from the \p set;
747 * @return 0 on success
748 */
749int ly_set_rm(struct ly_set *set, void *node);
750
751/**
752 * @brief Remove a ::lyd_node or ::lys_node object from the set index.
753 *
754 * Note that after removing a node from a set, indexes of other nodes in the set can change
755 * (the last object is placed instead of the removed object).
756 *
757 * @param[in] set Set from which a node will be removed.
758 * @param[in] index Index of the ::lyd_node or ::lys_node object in the \p set to be removed from the \p set;
759 * @return 0 on success
760 */
761int ly_set_rm_index(struct ly_set *set, unsigned int index);
762
763/**
Radek Krejcidef50022016-02-01 16:38:32 +0100764 * @brief Free the ::ly_set data. Frees only the set structure content, not the referred data.
Radek Krejcidc154432016-01-21 11:10:59 +0100765 *
766 * @param[in] set The set to be freed.
767 */
768void ly_set_free(struct ly_set *set);
769
Radek Krejcidef50022016-02-01 16:38:32 +0100770/**@} nodeset */
Radek Krejci6140e4e2015-10-09 15:50:55 +0200771
772/**
Radek Krejci5044be32016-01-18 17:05:51 +0100773 * @defgroup printerflags Printer flags
Radek Krejcidef50022016-02-01 16:38:32 +0100774 * @ingroup datatree
Radek Krejci5044be32016-01-18 17:05:51 +0100775 *
776 * Validity flags for data nodes.
777 *
778 * @{
779 */
780#define LYP_WITHSIBLINGS 0x01 /**< Flag for printing also the (following) sibling nodes of the data node. */
781
782/**
783 * @}
784 */
785
786/**
Radek Krejci3045cf32015-05-28 10:58:52 +0200787 * @defgroup logger Logger
788 * @{
789 *
790 * Publicly visible functions and values of the libyang logger. For more
791 * information, see \ref howtologger.
792 */
793
794/**
795 * @typedef LY_LOG_LEVEL
796 * @brief Verbosity levels of the libyang logger.
797 */
798typedef enum {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200799 LY_LLERR, /**< Print only error messages. */
800 LY_LLWRN, /**< Print error and warning messages. */
801 LY_LLVRB, /**< Besides errors and warnings, print some other verbose messages. */
802 LY_LLDBG /**< Print all messages including some development debug messages. */
Radek Krejci3045cf32015-05-28 10:58:52 +0200803} LY_LOG_LEVEL;
804
805/**
806 * @brief Set logger verbosity level.
807 * @param[in] level Verbosity level.
808 */
809void ly_verb(LY_LOG_LEVEL level);
810
811/**
Michal Vaskof1d62cf2015-12-07 13:17:11 +0100812 * @brief Set logger callback.
813 * @param[in] clb Logging callback.
814 */
815void ly_set_log_clb(void (*clb)(LY_LOG_LEVEL, const char *));
816
817/**
818 * @brief Get logger callback.
819 * @return Logger callback (can be NULL).
820 */
821void (*ly_get_log_clb(void))(LY_LOG_LEVEL, const char *);
822
823/**
Radek Krejci3045cf32015-05-28 10:58:52 +0200824 * @typedef LY_ERR
Radek Krejci26715a42015-07-29 14:10:45 +0200825 * @brief libyang's error codes available via ly_errno extern variable.
Radek Krejci9b4ca392015-04-10 08:31:27 +0200826 * @ingroup logger
827 */
828typedef enum {
Radek Krejciae6817a2015-08-10 14:02:06 +0200829 LY_SUCCESS, /**< no error, not set by functions, included just to complete #LY_ERR enumeration */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200830 LY_EMEM, /**< Memory allocation failure */
831 LY_ESYS, /**< System call failure */
832 LY_EINVAL, /**< Invalid value */
833 LY_EINT, /**< Internal error */
834 LY_EVALID /**< Validation failure */
Radek Krejci3045cf32015-05-28 10:58:52 +0200835} LY_ERR;
Radek Krejci7d9f46a2016-01-29 13:53:18 +0100836
Radek Krejci26715a42015-07-29 14:10:45 +0200837/**
Radek Krejci7d9f46a2016-01-29 13:53:18 +0100838 * @cond INTERNAL
839 * Function to get address of global `ly_errno' variable.
Radek Krejci26715a42015-07-29 14:10:45 +0200840 */
Radek Krejci7d9f46a2016-01-29 13:53:18 +0100841LY_ERR *ly_errno_location(void);
842
843/**
844 * @endcond INTERNAL
Radek Krejcidef50022016-02-01 16:38:32 +0100845 * @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 +0100846 */
847#define ly_errno (*ly_errno_location())
Radek Krejci9b4ca392015-04-10 08:31:27 +0200848
Radek Krejci3045cf32015-05-28 10:58:52 +0200849/**@} logger */
Radek Krejci9b4ca392015-04-10 08:31:27 +0200850
Radek Krejci39d8d0d2015-08-17 13:42:45 +0200851#ifdef __cplusplus
852}
853#endif
854
Radek Krejci9b4ca392015-04-10 08:31:27 +0200855#endif /* LY_LIBYANG_H_ */