blob: daafea26b01a10c0ae7d3d928b4012b2614baf89 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
Michal Vasko52927e22020-03-16 17:26:14 +01002 * @file printer_data.h
Radek Krejcie7b95092019-05-15 11:03:07 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko52927e22020-03-16 17:26:14 +01004 * @brief Data printers for libyang
Radek Krejcie7b95092019-05-15 11:03:07 +02005 *
6 * Copyright (c) 2015-2019 CESNET, z.s.p.o.
7 *
8 * 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
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#ifndef LY_PRINTER_DATA_H_
16#define LY_PRINTER_DATA_H_
17
Radek Krejci26da5222020-06-09 17:43:17 +020018#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <unistd.h>
20
Radek Krejci26da5222020-06-09 17:43:17 +020021#include "log.h"
Michal Vaskoce2b8742020-08-24 13:20:25 +020022#include "printer.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include "tree_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020024
Radek Krejcica376bd2020-06-11 16:04:06 +020025#ifdef __cplusplus
26extern "C" {
27#endif
28
Radek Krejci535ea9f2020-05-29 16:01:05 +020029struct ly_out;
Radek Krejcie7b95092019-05-15 11:03:07 +020030
31/**
Radek Krejci8678fa42020-08-18 16:07:28 +020032 * @page howtoDataPrinters Printing Data
Radek Krejcie7b95092019-05-15 11:03:07 +020033 *
Radek Krejci8678fa42020-08-18 16:07:28 +020034 * Data printers allows to serialize internal representation of a data tree in a specific format. libyang
35 * supports the following data formats for printing:
36 *
37 * - XML
38 *
39 * Basic format as specified in rules of mapping YANG modeled data to XML in
40 * [RFC 6020](http://tools.ietf.org/html/rfc6020).
41 *
42 * - JSON
43 *
44 * The alternative data format available in RESTCONF protocol. Specification of JSON encoding of data modeled by YANG
45 * can be found in [RFC 7951](https://tools.ietf.org/html/rfc7951).
46 *
47 * By default, both formats are printed with indentation (formatting), which can be avoided by ::LYD_PRINT_SHRINK
48 * [printer option](@ref dataprinterflags)). Other options adjust e.g. [with-defaults mode](@ref howtoDataWD).
49 *
50 * Besides the legacy functions from libyang 1.x (::lyd_print_clb(), ::lyd_print_fd(), ::lyd_print_file(), ::lyd_print_mem()
51 * and ::lyd_print_path()) printing data into the specified output, there are also new functions using
52 * [output handler](@ref howtoPrinters) introduced in libyang 2.0. In contrast to
53 * [schema printers](@ref howtoSchemaPrinters), there is no limit of the functionality and the functions can be used
54 * interchangeable. The only think to note is that the new functions ::lyd_print_all() and ::lyd_print_tree() does not
55 * accept ::LYD_PRINT_WITHSIBLINGS [printer option](@ref dataprinterflags)) since this flag differentiate the functions
56 * themselves.
57 *
58 * Functions List
59 * --------------
60 * - ::lyd_print_all()
61 * - ::lyd_print_tree()
62 * - ::lyd_print_mem()
63 * - ::lyd_print_fd()
64 * - ::lyd_print_file()
65 * - ::lyd_print_path()
66 * - ::lyd_print_clb()
67 */
68
69/**
70 * @ingroup datatree
71 * @defgroup dataprinterflags Data printer flags
72 *
73 * Options to change default behavior of the data printers.
Radek Krejcie7b95092019-05-15 11:03:07 +020074 *
75 * @{
76 */
Radek Krejci8678fa42020-08-18 16:07:28 +020077#define LYD_PRINT_WITHSIBLINGS 0x01 /**< Flag for printing also the (following) sibling nodes of the data node.
78 The flag is not allowed for ::lyd_print_all() and ::lyd_print_tree(). */
Radek Krejci52f65552020-09-01 17:03:35 +020079#define LYD_PRINT_SHRINK LY_PRINT_SHRINK /**< Flag for output without indentation and formatting new lines. */
80#define LYD_PRINT_KEEPEMPTYCONT 0x04 /**< Preserve empty non-presence containers */
81#define LYD_PRINT_WD_MASK 0xF0 /**< Mask for with-defaults modes */
Radek Krejci8678fa42020-08-18 16:07:28 +020082#define LYD_PRINT_WD_EXPLICIT 0x00 /**< Explicit with-defaults mode. Only the data explicitly being present in
83 the data tree are printed, so the implicitly added default nodes are
84 not printed. Note that this is the default value when no WD option is
85 specified. */
86#define LYD_PRINT_WD_TRIM 0x10 /**< Trim mode avoids printing the nodes with the value equal to their
87 default value */
88#define LYD_PRINT_WD_ALL 0x20 /**< With this option, theInclude implicit default nodes */
89#define LYD_PRINT_WD_ALL_TAG 0x40 /**< Same as ::LYD_PRINT_WD_ALL but also adds attribute 'default' with value 'true' to
Radek Krejci52f65552020-09-01 17:03:35 +020090 all nodes that has its default value. The 'default' attribute has namespace:
91 urn:ietf:params:xml:ns:netconf:default:1.0 and thus the attributes are
92 printed only when the ietf-netconf-with-defaults module is present in libyang
93 context (but in that case this namespace is always printed). */
Radek Krejci8678fa42020-08-18 16:07:28 +020094#define LYD_PRINT_WD_IMPL_TAG 0x80 /**< Same as ::LYD_PRINT_WD_ALL_TAG but the attributes are added only to the nodes that
Radek Krejci52f65552020-09-01 17:03:35 +020095 are not explicitly present in the original data tree despite their
96 value is equal to their default value. There is the same limitation regarding
97 the presence of ietf-netconf-with-defaults module in libyang context. */
Radek Krejcie7b95092019-05-15 11:03:07 +020098/**
99 * @}
100 */
101
102/**
Michal Vasko3a41dff2020-07-15 14:30:28 +0200103 * @brief Print the whole data tree of the root, including all the siblings.
Radek Krejcie7b95092019-05-15 11:03:07 +0200104 *
Radek Krejci241f6b52020-05-21 18:13:49 +0200105 * @param[in] out Printer handler for a specific output. Use ly_out_*() functions to create and free the handler.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200106 * @param[in] root The root element of the tree to print, can be any sibling.
Radek Krejcia5bba312020-01-09 15:41:20 +0100107 * @param[in] format Output format.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200108 * @param[in] options [Data printer flags](@ref dataprinterflags) except ::LYD_PRINT_WITHSIBLINGS.
Michal Vasko63f3d842020-07-08 10:10:14 +0200109 * @return LY_ERR value.
Radek Krejcie7b95092019-05-15 11:03:07 +0200110 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200111LY_ERR lyd_print_all(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, uint32_t options);
Michal Vasko3a41dff2020-07-15 14:30:28 +0200112
113/**
114 * @brief Print the selected data subtree.
115 *
116 * @param[in] out Printer handler for a specific output. Use ly_out_*() functions to create and free the handler.
117 * @param[in] root The root element of the subtree to print.
118 * @param[in] format Output format.
119 * @param[in] options [Data printer flags](@ref dataprinterflags) except ::LYD_PRINT_WITHSIBLINGS.
120 * @return LY_ERR value.
121 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200122LY_ERR lyd_print_tree(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, uint32_t options);
Radek Krejcie7b95092019-05-15 11:03:07 +0200123
Radek Krejci26da5222020-06-09 17:43:17 +0200124/**
Michal Vaskodcd356f2020-07-14 12:26:51 +0200125 * @brief Print data tree in the specified format.
126 *
127 * @param[out] strp Pointer to store the resulting dump.
Radek Krejci26da5222020-06-09 17:43:17 +0200128 * @param[in] root The root element of the (sub)tree to print.
129 * @param[in] format Output format.
Michal Vaskodcd356f2020-07-14 12:26:51 +0200130 * @param[in] options [Data printer flags](@ref dataprinterflags).
131 * @return LY_ERR value.
132 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200133LY_ERR lyd_print_mem(char **strp, const struct lyd_node *root, LYD_FORMAT format, uint32_t options);
Radek Krejci26da5222020-06-09 17:43:17 +0200134
135/**
136 * @brief Print data tree in the specified format.
137 *
138 * @param[in] fd File descriptor where to print the data.
139 * @param[in] root The root element of the (sub)tree to print.
140 * @param[in] format Output format.
Michal Vaskodcd356f2020-07-14 12:26:51 +0200141 * @param[in] options [Data printer flags](@ref dataprinterflags).
Radek Krejci26da5222020-06-09 17:43:17 +0200142 * @return LY_ERR value.
143 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200144LY_ERR lyd_print_fd(int fd, const struct lyd_node *root, LYD_FORMAT format, uint32_t options);
Radek Krejci26da5222020-06-09 17:43:17 +0200145
146/**
147 * @brief Print data tree in the specified format.
148 *
149 * @param[in] f File stream where to print the data.
150 * @param[in] root The root element of the (sub)tree to print.
151 * @param[in] format Output format.
Michal Vaskodcd356f2020-07-14 12:26:51 +0200152 * @param[in] options [Data printer flags](@ref dataprinterflags).
Radek Krejci26da5222020-06-09 17:43:17 +0200153 * @return LY_ERR value.
154 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200155LY_ERR lyd_print_file(FILE *f, const struct lyd_node *root, LYD_FORMAT format, uint32_t options);
Radek Krejci26da5222020-06-09 17:43:17 +0200156
157/**
158 * @brief Print data tree in the specified format.
159 *
160 * @param[in] path File path where to print the data.
161 * @param[in] root The root element of the (sub)tree to print.
162 * @param[in] format Output format.
Michal Vaskodcd356f2020-07-14 12:26:51 +0200163 * @param[in] options [Data printer flags](@ref dataprinterflags).
Radek Krejci26da5222020-06-09 17:43:17 +0200164 * @return LY_ERR value.
165 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200166LY_ERR lyd_print_path(const char *path, const struct lyd_node *root, LYD_FORMAT format, uint32_t options);
Radek Krejci26da5222020-06-09 17:43:17 +0200167
168/**
169 * @brief Print data tree in the specified format.
170 *
171 * @param[in] writeclb Callback function to write the data (see write(1)).
Michal Vaskoce2b8742020-08-24 13:20:25 +0200172 * @param[in] user_data Optional caller-specific argument to be passed to the \p writeclb callback.
Radek Krejci26da5222020-06-09 17:43:17 +0200173 * @param[in] root The root element of the (sub)tree to print.
174 * @param[in] format Output format.
Michal Vaskodcd356f2020-07-14 12:26:51 +0200175 * @param[in] options [Data printer flags](@ref dataprinterflags).
Radek Krejci26da5222020-06-09 17:43:17 +0200176 * @return LY_ERR value.
177 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200178LY_ERR lyd_print_clb(ly_write_clb writeclb, void *user_data, const struct lyd_node *root, LYD_FORMAT format, uint32_t options);
Radek Krejci26da5222020-06-09 17:43:17 +0200179
180#ifdef __cplusplus
181}
182#endif
183
Radek Krejcie7b95092019-05-15 11:03:07 +0200184#endif /* LY_PRINTER_DATA_H_ */