blob: f6426cf48ed2077d0df8e2b1a3466e0a10becd0d [file] [log] [blame]
Michal Vasko7bcb48e2016-01-15 10:28:54 +01001/**
2 * \file messages_server.h
3 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2's functions and structures of server NETCONF messages.
5 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +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 Vaskoafd416b2016-02-25 14:51:46 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko7bcb48e2016-01-15 10:28:54 +010013 */
14
15#ifndef NC_MESSAGES_SERVER_H_
16#define NC_MESSAGES_SERVER_H_
17
18#include <stdint.h>
19
20#include "netconf.h"
21
Michal Vaskof0537d82016-01-29 14:42:38 +010022/**
23 * @brief Enumeration of NETCONF errors
24 */
Michal Vasko495c9462016-01-15 11:27:43 +010025typedef enum NC_ERROR {
Michal Vaskof0537d82016-01-29 14:42:38 +010026 NC_ERR_UNKNOWN = 0, /**< unknown error */
27 NC_ERR_IN_USE, /**< in-use error */
28 NC_ERR_INVALID_VALUE, /**< invalid-value error */
29 NC_ERR_TOO_BIG, /**< too-big error */
30 NC_ERR_MISSING_ATTR, /**< missing-attribute error */
31 NC_ERR_BAD_ATTR, /**< bad-attribute error */
32 NC_ERR_UNKNOWN_ATTR, /**< unknown-attribute error */
33 NC_ERR_MISSING_ELEM, /**< missing-element error */
34 NC_ERR_BAD_ELEM, /**< bad-element error */
35 NC_ERR_UNKNOWN_ELEM, /**< unknown-element error */
36 NC_ERR_UNKNOWN_NS, /**< unknown-namespace error */
37 NC_ERR_ACCESS_DENIED, /**< access-denied error */
38 NC_ERR_LOCK_DENIED, /**< lock-denied error */
39 NC_ERR_RES_DENIED, /**< resource-denied error */
40 NC_ERR_ROLLBACK_FAILED, /**< rollback-failed error */
41 NC_ERR_DATA_EXISTS, /**< data-exists error */
42 NC_ERR_DATA_MISSING, /**< data-missing error */
43 NC_ERR_OP_NOT_SUPPORTED, /**< operation-not-supported error */
44 NC_ERR_OP_FAILED, /**< operation-failed error */
45 NC_ERR_MALFORMED_MSG /**< malformed-message error */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010046} NC_ERR;
47
Michal Vaskof0537d82016-01-29 14:42:38 +010048/**
49 * @brief Enumeration of NETCONF error type (layer)
50 */
Michal Vasko495c9462016-01-15 11:27:43 +010051typedef enum NC_ERROR_TYPE {
Michal Vaskof0537d82016-01-29 14:42:38 +010052 NC_ERR_TYPE_UNKNOWN = 0, /**< unknown layer */
53 NC_ERR_TYPE_TRAN, /**< transport layer */
54 NC_ERR_TYPE_RPC, /**< RPC layer */
55 NC_ERR_TYPE_PROT, /**< protocol layer */
56 NC_ERR_TYPE_APP /**< application layer */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010057} NC_ERR_TYPE;
58
59/**
Michal Vasko1a38c862016-01-15 15:50:07 +010060 * @brief NETCONF server rpc-reply object
Michal Vasko7bcb48e2016-01-15 10:28:54 +010061 */
62struct nc_server_reply;
63
Michal Vasko1a38c862016-01-15 15:50:07 +010064/**
65 * @brief NETCONF server error structure
66 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010067struct nc_server_error;
68
Michal Vasko1a38c862016-01-15 15:50:07 +010069/**
70 * @brief Create an OK rpc-reply object.
71 *
72 * @return rpc-reply object, NULL on error.
73 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010074struct nc_server_reply *nc_server_reply_ok(void);
75
Michal Vasko1a38c862016-01-15 15:50:07 +010076/**
77 * @brief Create a DATA rpc-reply object.
78 *
Michal Vaskob08743b2016-04-13 14:23:49 +020079 * @param[in] data Reply data tree. This tree must be valid according to
80 * the RPC output of the RPC this is a reply to.
Michal Vasko1a38c862016-01-15 15:50:07 +010081 * @param[in] paramtype Determines how the \p data parameter is treated.
82 * @return rpc-reply object, NULL on error.
83 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010084struct nc_server_reply *nc_server_reply_data(struct lyd_node *data, NC_PARAMTYPE paramtype);
85
Michal Vasko1a38c862016-01-15 15:50:07 +010086/**
87 * @brief Create an ERROR rpc-reply object.
88 *
89 * @param[in] err Structure holding the error information. It will be freed with the returned object.
90 * @return rpc-reply object, NULL on error.
91 */
92struct nc_server_reply *nc_server_reply_err(struct nc_server_error *err);
Michal Vasko7bcb48e2016-01-15 10:28:54 +010093
Michal Vasko1a38c862016-01-15 15:50:07 +010094/**
95 * @brief Add another error to an ERROR rpc-reply object. It will be freed with the returned object.
96 *
97 * @param[in] reply ERROR reply to add to.
98 * @param[in] err Structure holding the additional error information.
99 * @return 0 on success, -1 on errror.
100 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100101int nc_server_reply_add_err(struct nc_server_reply *reply, struct nc_server_error *err);
102
Michal Vasko1a38c862016-01-15 15:50:07 +0100103/**
104 * @brief Create a server error structure. Its \<error-message\> is filled with
105 * a general description of the specific error.
106 *
107 * @param[in] tag \<error-tag\> of the server error. According to the tag, the
108 * specific additional parameters are required:
109 * - #NC_ERR_IN_USE
110 * - #NC_ERR_INVALID_VALUE
111 * - #NC_ERR_ACCESS_DENIED
112 * - #NC_ERR_ROLLBACK_FAILED
Michal Vaskof0537d82016-01-29 14:42:38 +0100113 * - #NC_ERR_OP_NOT_SUPPORTED
Michal Vasko1a38c862016-01-15 15:50:07 +0100114 * - #NC_ERR_TOO_BIG
115 * - #NC_ERR_RES_DENIED
116 * - #NC_ERR_OP_FAILED
117 * - `NC_ERR_TYPE type;` - type (layer) of the error.
118 * - #NC_ERR_MISSING_ATTR
119 * - #NC_ERR_BAD_ATTR
120 * - #NC_ERR_UNKNOWN_ATTR
121 * - `NC_ERR_TYPE type;` - type (layer) of the error.
122 * - `const char *attr_name;` - error \<bad-attribute\> value.
123 * - `const char *elem_name;` - error \<bad-element\> value.
124 * - #NC_ERR_MISSING_ELEM
125 * - #NC_ERR_BAD_ELEM
126 * - #NC_ERR_UNKNOWN_ELEM
127 * - `NC_ERR_TYPE type;` - type (layer) of the error.
128 * - `const char *elem_name;` - error \<bad-element\> value.
129 * - #NC_ERR_UNKNOWN_NS
130 * - `NC_ERR_TYPE type;` - type (layer) of the error.
131 * - `const char *elem_name;` - error \<bad-element\> value.
132 * - `const char *nc_name;` - error \<bad-namespace\> value.
133 * - #NC_ERR_LOCK_DENIED
134 * - `uint32_t session_id;` - error \<session-id\> value.
135 * - #NC_ERR_DATA_EXISTS
136 * - #NC_ERR_DATA_MISSING
Michal Vaskof0537d82016-01-29 14:42:38 +0100137 * - #NC_ERR_MALFORMED_MSG
Michal Vasko1a38c862016-01-15 15:50:07 +0100138 * - no additional arguments
139 * @return Server error structure, NULL on error.
140 */
141struct nc_server_error *nc_err(NC_ERR tag, ...);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100142
Michal Vasko1a38c862016-01-15 15:50:07 +0100143/**
Radek Krejci877e1822016-04-06 16:37:43 +0200144 * @brief Create a server error structure based on libyang error.
145 *
146 * The function should be used immediately when a libyang function fails to generate
147 * NETCONF error structure based on internal libyang error information (ly_errno, ly_errmsg, ...)
148 *
149 * @return Server error structure, NULL on error.
150 */
Michal Vasko7d9b4952016-04-08 10:44:26 +0200151struct nc_server_error *nc_err_libyang(void);
Radek Krejci877e1822016-04-06 16:37:43 +0200152
153/**
Michal Vaskoc9121112016-05-04 10:45:28 +0200154 * @brief Get the \<error-type\> of a server error.
155 *
156 * @param[in] err Server error to read from.
157 * @return Server error type, 0 on error.
158 */
159NC_ERR_TYPE nc_err_get_type(struct nc_server_error *err);
160
161/**
162 * @brief Get the \<error-tag\> of a server error.
163 *
164 * @param[in] err Server error to read from.
165 * @return Server error tag, 0 on error.
166 */
167NC_ERR nc_err_get_tag(struct nc_server_error *err);
168
169/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100170 * @brief Set the \<error-app-tag\> element of an error. Any previous value will be overwritten.
171 *
172 * @param[in] err Error to modify.
173 * @param[in] error_app_tag New value of \<error-app-tag\>.
174 * @return 0 on success, -1 on error.
175 */
176int nc_err_set_app_tag(struct nc_server_error *err, const char *error_app_tag);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100177
Michal Vasko1a38c862016-01-15 15:50:07 +0100178/**
Michal Vaskoc9121112016-05-04 10:45:28 +0200179 * @brief Get the \<error-app-tag\> of a server error.
180 *
181 * @param[in] err Server error to read from.
182 * @return Server error app tag, NULL on error.
183 */
184const char *nc_err_get_app_tag(struct nc_server_error *err);
185
186/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100187 * @brief Set the \<error-path\> element of an error. Any previous value will be overwritten.
188 *
189 * @param[in] err Error to modify.
190 * @param[in] error_path New value of \<error-path\>.
191 * @return 0 on success, -1 on error.
192 */
193int nc_err_set_path(struct nc_server_error *err, const char *error_path);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100194
Michal Vasko1a38c862016-01-15 15:50:07 +0100195/**
Michal Vaskoc9121112016-05-04 10:45:28 +0200196 * @brief Get the \<error-path\> of a server error.
197 *
198 * @param[in] err Server error to read from.
199 * @return Server error path, NULL on error.
200 */
201const char *nc_err_get_path(struct nc_server_error *err);
202
203/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100204 * @brief Set the \<error-message\> element of an error. Any previous value will be overwritten.
205 *
206 * @param[in] err Error to modify.
207 * @param[in] error_message New value of \<error-message\>.
Michal Vaskof0537d82016-01-29 14:42:38 +0100208 * @param[in] lang Optional language of \p error_message.
Michal Vasko1a38c862016-01-15 15:50:07 +0100209 * @return 0 on success, -1 on error.
210 */
211int nc_err_set_msg(struct nc_server_error *err, const char *error_message, const char *lang);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100212
Michal Vasko1a38c862016-01-15 15:50:07 +0100213/**
Michal Vaskoc9121112016-05-04 10:45:28 +0200214 * @brief Get the \<error-message\> of a server error.
215 *
216 * @param[in] err Server error to read from.
217 * @return Server error message, NULL on error.
218 */
219const char *nc_err_get_msg(struct nc_server_error *err);
220
221/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100222 * @brief Set the \<session-id\> element of an error. Any previous value will be overwritten.
223 *
224 * @param[in] err Error to modify.
Michal Vaskof0537d82016-01-29 14:42:38 +0100225 * @param[in] session_id New value of \<session-id\>.
Michal Vasko1a38c862016-01-15 15:50:07 +0100226 * @return 0 on success, -1 on error.
227 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100228int nc_err_set_sid(struct nc_server_error *err, uint32_t session_id);
229
Michal Vasko1a38c862016-01-15 15:50:07 +0100230/**
231 * @brief Add a \<bad-attribute\> element to an error.
232 *
233 * @param[in] err Error to modify.
234 * @param[in] attr_name Value of the new \<bad-attribute\> element.
235 * @return 0 on success, -1 on error.
236 */
237int nc_err_add_bad_attr(struct nc_server_error *err, const char *attr_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100238
Michal Vasko1a38c862016-01-15 15:50:07 +0100239/**
240 * @brief Add a \<bad-element\> element to an error.
241 *
242 * @param[in] err Error to modify.
243 * @param[in] elem_name Value of the new \<bad-element\> element.
244 * @return 0 on success, -1 on error.
245 */
246int nc_err_add_bad_elem(struct nc_server_error *err, const char *elem_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100247
Michal Vasko1a38c862016-01-15 15:50:07 +0100248/**
249 * @brief Add a \<bad-namespace\> element to an error.
250 *
251 * @param[in] err Error to modify.
252 * @param[in] ns_name Value of the new \<bad-namespace\> element.
253 * @return 0 on success, -1 on error.
254 */
255int nc_err_add_bad_ns(struct nc_server_error *err, const char *ns_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100256
Michal Vasko1a38c862016-01-15 15:50:07 +0100257/**
258 * @brief Add an additional custom element to an error.
259 *
260 * @param[in] err Error to modify.
261 * @param[in] other New custom XML element.
262 * @return 0 on success, -1 on error.
263 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100264int nc_err_add_info_other(struct nc_server_error *err, struct lyxml_elem *other);
265
Michal Vasko1a38c862016-01-15 15:50:07 +0100266/**
267 * @brief Free a server rpc-reply object.
268 *
269 * @param[in] reply Server rpc-reply object to free.
270 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100271void nc_server_reply_free(struct nc_server_reply *reply);
272
Michal Vasko1a38c862016-01-15 15:50:07 +0100273/**
274 * @brief Free a server error structure.
275 *
276 * @param[in] err Error structure to free.
277 */
278void nc_err_free(struct nc_server_error *err);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100279
280#endif /* NC_MESSAGES_SERVER_H_ */