blob: 733fe5cd7c89d32456bdd43abefa48d49c803871 [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.
Radek Krejci36dfdb32016-09-01 16:56:35 +020081 * @param[in] wd with-default mode if applicable
Michal Vasko1a38c862016-01-15 15:50:07 +010082 * @param[in] paramtype Determines how the \p data parameter is treated.
83 * @return rpc-reply object, NULL on error.
84 */
Radek Krejci36dfdb32016-09-01 16:56:35 +020085struct nc_server_reply *nc_server_reply_data(struct lyd_node *data, NC_WD_MODE wd, NC_PARAMTYPE paramtype);
Michal Vasko7bcb48e2016-01-15 10:28:54 +010086
Michal Vasko1a38c862016-01-15 15:50:07 +010087/**
88 * @brief Create an ERROR rpc-reply object.
89 *
90 * @param[in] err Structure holding the error information. It will be freed with the returned object.
91 * @return rpc-reply object, NULL on error.
92 */
93struct nc_server_reply *nc_server_reply_err(struct nc_server_error *err);
Michal Vasko7bcb48e2016-01-15 10:28:54 +010094
Michal Vasko1a38c862016-01-15 15:50:07 +010095/**
96 * @brief Add another error to an ERROR rpc-reply object. It will be freed with the returned object.
97 *
98 * @param[in] reply ERROR reply to add to.
99 * @param[in] err Structure holding the additional error information.
100 * @return 0 on success, -1 on errror.
101 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100102int nc_server_reply_add_err(struct nc_server_reply *reply, struct nc_server_error *err);
103
Michal Vasko1a38c862016-01-15 15:50:07 +0100104/**
105 * @brief Create a server error structure. Its \<error-message\> is filled with
106 * a general description of the specific error.
107 *
108 * @param[in] tag \<error-tag\> of the server error. According to the tag, the
109 * specific additional parameters are required:
110 * - #NC_ERR_IN_USE
111 * - #NC_ERR_INVALID_VALUE
112 * - #NC_ERR_ACCESS_DENIED
113 * - #NC_ERR_ROLLBACK_FAILED
Michal Vaskof0537d82016-01-29 14:42:38 +0100114 * - #NC_ERR_OP_NOT_SUPPORTED
Michal Vasko1a38c862016-01-15 15:50:07 +0100115 * - #NC_ERR_TOO_BIG
116 * - #NC_ERR_RES_DENIED
117 * - #NC_ERR_OP_FAILED
118 * - `NC_ERR_TYPE type;` - type (layer) of the error.
119 * - #NC_ERR_MISSING_ATTR
120 * - #NC_ERR_BAD_ATTR
121 * - #NC_ERR_UNKNOWN_ATTR
122 * - `NC_ERR_TYPE type;` - type (layer) of the error.
123 * - `const char *attr_name;` - error \<bad-attribute\> value.
124 * - `const char *elem_name;` - error \<bad-element\> value.
125 * - #NC_ERR_MISSING_ELEM
126 * - #NC_ERR_BAD_ELEM
127 * - #NC_ERR_UNKNOWN_ELEM
128 * - `NC_ERR_TYPE type;` - type (layer) of the error.
129 * - `const char *elem_name;` - error \<bad-element\> value.
130 * - #NC_ERR_UNKNOWN_NS
131 * - `NC_ERR_TYPE type;` - type (layer) of the error.
132 * - `const char *elem_name;` - error \<bad-element\> value.
133 * - `const char *nc_name;` - error \<bad-namespace\> value.
134 * - #NC_ERR_LOCK_DENIED
135 * - `uint32_t session_id;` - error \<session-id\> value.
136 * - #NC_ERR_DATA_EXISTS
137 * - #NC_ERR_DATA_MISSING
Michal Vaskof0537d82016-01-29 14:42:38 +0100138 * - #NC_ERR_MALFORMED_MSG
Michal Vasko1a38c862016-01-15 15:50:07 +0100139 * - no additional arguments
140 * @return Server error structure, NULL on error.
141 */
142struct nc_server_error *nc_err(NC_ERR tag, ...);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100143
Michal Vasko1a38c862016-01-15 15:50:07 +0100144/**
Radek Krejci877e1822016-04-06 16:37:43 +0200145 * @brief Create a server error structure based on libyang error.
146 *
147 * The function should be used immediately when a libyang function fails to generate
148 * NETCONF error structure based on internal libyang error information (ly_errno, ly_errmsg, ...)
149 *
150 * @return Server error structure, NULL on error.
151 */
Michal Vasko7d9b4952016-04-08 10:44:26 +0200152struct nc_server_error *nc_err_libyang(void);
Radek Krejci877e1822016-04-06 16:37:43 +0200153
154/**
Michal Vasko8f3198f2016-05-04 10:45:28 +0200155 * @brief Get the \<error-type\> of a server error.
156 *
157 * @param[in] err Server error to read from.
158 * @return Server error type, 0 on error.
159 */
160NC_ERR_TYPE nc_err_get_type(struct nc_server_error *err);
161
162/**
163 * @brief Get the \<error-tag\> of a server error.
164 *
165 * @param[in] err Server error to read from.
166 * @return Server error tag, 0 on error.
167 */
168NC_ERR nc_err_get_tag(struct nc_server_error *err);
169
170/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100171 * @brief Set the \<error-app-tag\> element of an error. Any previous value will be overwritten.
172 *
173 * @param[in] err Error to modify.
174 * @param[in] error_app_tag New value of \<error-app-tag\>.
175 * @return 0 on success, -1 on error.
176 */
177int nc_err_set_app_tag(struct nc_server_error *err, const char *error_app_tag);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100178
Michal Vasko1a38c862016-01-15 15:50:07 +0100179/**
Michal Vasko8f3198f2016-05-04 10:45:28 +0200180 * @brief Get the \<error-app-tag\> of a server error.
181 *
182 * @param[in] err Server error to read from.
183 * @return Server error app tag, NULL on error.
184 */
185const char *nc_err_get_app_tag(struct nc_server_error *err);
186
187/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100188 * @brief Set the \<error-path\> element of an error. Any previous value will be overwritten.
189 *
190 * @param[in] err Error to modify.
191 * @param[in] error_path New value of \<error-path\>.
192 * @return 0 on success, -1 on error.
193 */
194int nc_err_set_path(struct nc_server_error *err, const char *error_path);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100195
Michal Vasko1a38c862016-01-15 15:50:07 +0100196/**
Michal Vasko8f3198f2016-05-04 10:45:28 +0200197 * @brief Get the \<error-path\> of a server error.
198 *
199 * @param[in] err Server error to read from.
200 * @return Server error path, NULL on error.
201 */
202const char *nc_err_get_path(struct nc_server_error *err);
203
204/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100205 * @brief Set the \<error-message\> element of an error. Any previous value will be overwritten.
206 *
207 * @param[in] err Error to modify.
208 * @param[in] error_message New value of \<error-message\>.
Michal Vaskof0537d82016-01-29 14:42:38 +0100209 * @param[in] lang Optional language of \p error_message.
Michal Vasko1a38c862016-01-15 15:50:07 +0100210 * @return 0 on success, -1 on error.
211 */
212int nc_err_set_msg(struct nc_server_error *err, const char *error_message, const char *lang);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100213
Michal Vasko1a38c862016-01-15 15:50:07 +0100214/**
Michal Vasko8f3198f2016-05-04 10:45:28 +0200215 * @brief Get the \<error-message\> of a server error.
216 *
217 * @param[in] err Server error to read from.
218 * @return Server error message, NULL on error.
219 */
220const char *nc_err_get_msg(struct nc_server_error *err);
221
222/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100223 * @brief Set the \<session-id\> element of an error. Any previous value will be overwritten.
224 *
225 * @param[in] err Error to modify.
Michal Vaskof0537d82016-01-29 14:42:38 +0100226 * @param[in] session_id New value of \<session-id\>.
Michal Vasko1a38c862016-01-15 15:50:07 +0100227 * @return 0 on success, -1 on error.
228 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100229int nc_err_set_sid(struct nc_server_error *err, uint32_t session_id);
230
Michal Vasko1a38c862016-01-15 15:50:07 +0100231/**
232 * @brief Add a \<bad-attribute\> element to an error.
233 *
234 * @param[in] err Error to modify.
235 * @param[in] attr_name Value of the new \<bad-attribute\> element.
236 * @return 0 on success, -1 on error.
237 */
238int nc_err_add_bad_attr(struct nc_server_error *err, const char *attr_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100239
Michal Vasko1a38c862016-01-15 15:50:07 +0100240/**
241 * @brief Add a \<bad-element\> element to an error.
242 *
243 * @param[in] err Error to modify.
244 * @param[in] elem_name Value of the new \<bad-element\> element.
245 * @return 0 on success, -1 on error.
246 */
247int nc_err_add_bad_elem(struct nc_server_error *err, const char *elem_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100248
Michal Vasko1a38c862016-01-15 15:50:07 +0100249/**
250 * @brief Add a \<bad-namespace\> element to an error.
251 *
252 * @param[in] err Error to modify.
253 * @param[in] ns_name Value of the new \<bad-namespace\> element.
254 * @return 0 on success, -1 on error.
255 */
256int nc_err_add_bad_ns(struct nc_server_error *err, const char *ns_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100257
Michal Vasko1a38c862016-01-15 15:50:07 +0100258/**
259 * @brief Add an additional custom element to an error.
260 *
261 * @param[in] err Error to modify.
262 * @param[in] other New custom XML element.
263 * @return 0 on success, -1 on error.
264 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100265int nc_err_add_info_other(struct nc_server_error *err, struct lyxml_elem *other);
266
Michal Vasko1a38c862016-01-15 15:50:07 +0100267/**
268 * @brief Free a server rpc-reply object.
269 *
270 * @param[in] reply Server rpc-reply object to free.
271 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100272void nc_server_reply_free(struct nc_server_reply *reply);
273
Michal Vasko1a38c862016-01-15 15:50:07 +0100274/**
275 * @brief Free a server error structure.
276 *
277 * @param[in] err Error structure to free.
278 */
279void nc_err_free(struct nc_server_error *err);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100280
281#endif /* NC_MESSAGES_SERVER_H_ */