blob: c615786f9cb39f341af2b038cdbe509315156109 [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"
Radek Krejci93e80222016-10-03 13:34:25 +020021#include "session.h"
Michal Vasko7bcb48e2016-01-15 10:28:54 +010022
Michal Vaskof0537d82016-01-29 14:42:38 +010023/**
24 * @brief Enumeration of NETCONF errors
25 */
Michal Vasko495c9462016-01-15 11:27:43 +010026typedef enum NC_ERROR {
Michal Vaskof0537d82016-01-29 14:42:38 +010027 NC_ERR_UNKNOWN = 0, /**< unknown error */
28 NC_ERR_IN_USE, /**< in-use error */
29 NC_ERR_INVALID_VALUE, /**< invalid-value error */
30 NC_ERR_TOO_BIG, /**< too-big error */
31 NC_ERR_MISSING_ATTR, /**< missing-attribute error */
32 NC_ERR_BAD_ATTR, /**< bad-attribute error */
33 NC_ERR_UNKNOWN_ATTR, /**< unknown-attribute error */
34 NC_ERR_MISSING_ELEM, /**< missing-element error */
35 NC_ERR_BAD_ELEM, /**< bad-element error */
36 NC_ERR_UNKNOWN_ELEM, /**< unknown-element error */
37 NC_ERR_UNKNOWN_NS, /**< unknown-namespace error */
38 NC_ERR_ACCESS_DENIED, /**< access-denied error */
39 NC_ERR_LOCK_DENIED, /**< lock-denied error */
40 NC_ERR_RES_DENIED, /**< resource-denied error */
41 NC_ERR_ROLLBACK_FAILED, /**< rollback-failed error */
42 NC_ERR_DATA_EXISTS, /**< data-exists error */
43 NC_ERR_DATA_MISSING, /**< data-missing error */
44 NC_ERR_OP_NOT_SUPPORTED, /**< operation-not-supported error */
45 NC_ERR_OP_FAILED, /**< operation-failed error */
46 NC_ERR_MALFORMED_MSG /**< malformed-message error */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010047} NC_ERR;
48
Michal Vaskof0537d82016-01-29 14:42:38 +010049/**
50 * @brief Enumeration of NETCONF error type (layer)
51 */
Michal Vasko495c9462016-01-15 11:27:43 +010052typedef enum NC_ERROR_TYPE {
Michal Vaskof0537d82016-01-29 14:42:38 +010053 NC_ERR_TYPE_UNKNOWN = 0, /**< unknown layer */
54 NC_ERR_TYPE_TRAN, /**< transport layer */
55 NC_ERR_TYPE_RPC, /**< RPC layer */
56 NC_ERR_TYPE_PROT, /**< protocol layer */
57 NC_ERR_TYPE_APP /**< application layer */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010058} NC_ERR_TYPE;
59
60/**
Michal Vasko1a38c862016-01-15 15:50:07 +010061 * @brief NETCONF server rpc-reply object
Michal Vasko7bcb48e2016-01-15 10:28:54 +010062 */
63struct nc_server_reply;
64
Michal Vasko1a38c862016-01-15 15:50:07 +010065/**
Radek Krejci93e80222016-10-03 13:34:25 +020066 * @brief NETCONF server Event Notification object
67 */
68struct nc_server_notif;
69
70/**
Michal Vasko1a38c862016-01-15 15:50:07 +010071 * @brief NETCONF server error structure
72 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010073struct nc_server_error;
74
Michal Vasko1a38c862016-01-15 15:50:07 +010075/**
76 * @brief Create an OK rpc-reply object.
77 *
78 * @return rpc-reply object, NULL on error.
79 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010080struct nc_server_reply *nc_server_reply_ok(void);
81
Michal Vasko1a38c862016-01-15 15:50:07 +010082/**
83 * @brief Create a DATA rpc-reply object.
84 *
Michal Vaskob08743b2016-04-13 14:23:49 +020085 * @param[in] data Reply data tree. This tree must be valid according to
86 * the RPC output of the RPC this is a reply to.
Radek Krejci36dfdb32016-09-01 16:56:35 +020087 * @param[in] wd with-default mode if applicable
Michal Vasko1a38c862016-01-15 15:50:07 +010088 * @param[in] paramtype Determines how the \p data parameter is treated.
89 * @return rpc-reply object, NULL on error.
90 */
Radek Krejci36dfdb32016-09-01 16:56:35 +020091struct 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 +010092
Michal Vasko1a38c862016-01-15 15:50:07 +010093/**
94 * @brief Create an ERROR rpc-reply object.
95 *
96 * @param[in] err Structure holding the error information. It will be freed with the returned object.
97 * @return rpc-reply object, NULL on error.
98 */
99struct nc_server_reply *nc_server_reply_err(struct nc_server_error *err);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100100
Michal Vasko1a38c862016-01-15 15:50:07 +0100101/**
102 * @brief Add another error to an ERROR rpc-reply object. It will be freed with the returned object.
103 *
104 * @param[in] reply ERROR reply to add to.
105 * @param[in] err Structure holding the additional error information.
106 * @return 0 on success, -1 on errror.
107 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100108int nc_server_reply_add_err(struct nc_server_reply *reply, struct nc_server_error *err);
109
Michal Vasko1a38c862016-01-15 15:50:07 +0100110/**
111 * @brief Create a server error structure. Its \<error-message\> is filled with
112 * a general description of the specific error.
113 *
Radek Krejci127f8952016-10-12 14:57:16 +0200114 * @param[in] tag \<error-tag\> of the server error specified as #NC_ERR value. According to the tag, the
Michal Vasko1a38c862016-01-15 15:50:07 +0100115 * specific additional parameters are required:
116 * - #NC_ERR_IN_USE
117 * - #NC_ERR_INVALID_VALUE
118 * - #NC_ERR_ACCESS_DENIED
119 * - #NC_ERR_ROLLBACK_FAILED
Michal Vaskof0537d82016-01-29 14:42:38 +0100120 * - #NC_ERR_OP_NOT_SUPPORTED
Michal Vasko1a38c862016-01-15 15:50:07 +0100121 * - #NC_ERR_TOO_BIG
122 * - #NC_ERR_RES_DENIED
123 * - #NC_ERR_OP_FAILED
124 * - `NC_ERR_TYPE type;` - type (layer) of the error.
125 * - #NC_ERR_MISSING_ATTR
126 * - #NC_ERR_BAD_ATTR
127 * - #NC_ERR_UNKNOWN_ATTR
128 * - `NC_ERR_TYPE type;` - type (layer) of the error.
129 * - `const char *attr_name;` - error \<bad-attribute\> value.
130 * - `const char *elem_name;` - error \<bad-element\> value.
131 * - #NC_ERR_MISSING_ELEM
132 * - #NC_ERR_BAD_ELEM
133 * - #NC_ERR_UNKNOWN_ELEM
134 * - `NC_ERR_TYPE type;` - type (layer) of the error.
135 * - `const char *elem_name;` - error \<bad-element\> value.
136 * - #NC_ERR_UNKNOWN_NS
137 * - `NC_ERR_TYPE type;` - type (layer) of the error.
138 * - `const char *elem_name;` - error \<bad-element\> value.
139 * - `const char *nc_name;` - error \<bad-namespace\> value.
140 * - #NC_ERR_LOCK_DENIED
141 * - `uint32_t session_id;` - error \<session-id\> value.
142 * - #NC_ERR_DATA_EXISTS
143 * - #NC_ERR_DATA_MISSING
Michal Vaskof0537d82016-01-29 14:42:38 +0100144 * - #NC_ERR_MALFORMED_MSG
Michal Vasko1a38c862016-01-15 15:50:07 +0100145 * - no additional arguments
146 * @return Server error structure, NULL on error.
147 */
Radek Krejci127f8952016-10-12 14:57:16 +0200148struct nc_server_error *nc_err(int tag, ...);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100149
Michal Vasko1a38c862016-01-15 15:50:07 +0100150/**
Radek Krejci877e1822016-04-06 16:37:43 +0200151 * @brief Create a server error structure based on libyang error.
152 *
153 * The function should be used immediately when a libyang function fails to generate
154 * NETCONF error structure based on internal libyang error information (ly_errno, ly_errmsg, ...)
155 *
156 * @return Server error structure, NULL on error.
157 */
Michal Vasko7d9b4952016-04-08 10:44:26 +0200158struct nc_server_error *nc_err_libyang(void);
Radek Krejci877e1822016-04-06 16:37:43 +0200159
160/**
Michal Vasko8f3198f2016-05-04 10:45:28 +0200161 * @brief Get the \<error-type\> of a server error.
162 *
163 * @param[in] err Server error to read from.
164 * @return Server error type, 0 on error.
165 */
166NC_ERR_TYPE nc_err_get_type(struct nc_server_error *err);
167
168/**
169 * @brief Get the \<error-tag\> of a server error.
170 *
171 * @param[in] err Server error to read from.
172 * @return Server error tag, 0 on error.
173 */
174NC_ERR nc_err_get_tag(struct nc_server_error *err);
175
176/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100177 * @brief Set the \<error-app-tag\> element of an error. Any previous value will be overwritten.
178 *
179 * @param[in] err Error to modify.
180 * @param[in] error_app_tag New value of \<error-app-tag\>.
181 * @return 0 on success, -1 on error.
182 */
183int nc_err_set_app_tag(struct nc_server_error *err, const char *error_app_tag);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100184
Michal Vasko1a38c862016-01-15 15:50:07 +0100185/**
Michal Vasko8f3198f2016-05-04 10:45:28 +0200186 * @brief Get the \<error-app-tag\> of a server error.
187 *
188 * @param[in] err Server error to read from.
189 * @return Server error app tag, NULL on error.
190 */
191const char *nc_err_get_app_tag(struct nc_server_error *err);
192
193/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100194 * @brief Set the \<error-path\> element of an error. Any previous value will be overwritten.
195 *
196 * @param[in] err Error to modify.
197 * @param[in] error_path New value of \<error-path\>.
198 * @return 0 on success, -1 on error.
199 */
200int nc_err_set_path(struct nc_server_error *err, const char *error_path);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100201
Michal Vasko1a38c862016-01-15 15:50:07 +0100202/**
Michal Vasko8f3198f2016-05-04 10:45:28 +0200203 * @brief Get the \<error-path\> of a server error.
204 *
205 * @param[in] err Server error to read from.
206 * @return Server error path, NULL on error.
207 */
208const char *nc_err_get_path(struct nc_server_error *err);
209
210/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100211 * @brief Set the \<error-message\> element of an error. Any previous value will be overwritten.
212 *
213 * @param[in] err Error to modify.
214 * @param[in] error_message New value of \<error-message\>.
Michal Vaskof0537d82016-01-29 14:42:38 +0100215 * @param[in] lang Optional language of \p error_message.
Michal Vasko1a38c862016-01-15 15:50:07 +0100216 * @return 0 on success, -1 on error.
217 */
218int nc_err_set_msg(struct nc_server_error *err, const char *error_message, const char *lang);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100219
Michal Vasko1a38c862016-01-15 15:50:07 +0100220/**
Michal Vasko8f3198f2016-05-04 10:45:28 +0200221 * @brief Get the \<error-message\> of a server error.
222 *
223 * @param[in] err Server error to read from.
224 * @return Server error message, NULL on error.
225 */
226const char *nc_err_get_msg(struct nc_server_error *err);
227
228/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100229 * @brief Set the \<session-id\> element of an error. Any previous value will be overwritten.
230 *
231 * @param[in] err Error to modify.
Michal Vaskof0537d82016-01-29 14:42:38 +0100232 * @param[in] session_id New value of \<session-id\>.
Michal Vasko1a38c862016-01-15 15:50:07 +0100233 * @return 0 on success, -1 on error.
234 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100235int nc_err_set_sid(struct nc_server_error *err, uint32_t session_id);
236
Michal Vasko1a38c862016-01-15 15:50:07 +0100237/**
238 * @brief Add a \<bad-attribute\> element to an error.
239 *
240 * @param[in] err Error to modify.
241 * @param[in] attr_name Value of the new \<bad-attribute\> element.
242 * @return 0 on success, -1 on error.
243 */
244int nc_err_add_bad_attr(struct nc_server_error *err, const char *attr_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100245
Michal Vasko1a38c862016-01-15 15:50:07 +0100246/**
247 * @brief Add a \<bad-element\> element to an error.
248 *
249 * @param[in] err Error to modify.
250 * @param[in] elem_name Value of the new \<bad-element\> element.
251 * @return 0 on success, -1 on error.
252 */
253int nc_err_add_bad_elem(struct nc_server_error *err, const char *elem_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100254
Michal Vasko1a38c862016-01-15 15:50:07 +0100255/**
256 * @brief Add a \<bad-namespace\> element to an error.
257 *
258 * @param[in] err Error to modify.
259 * @param[in] ns_name Value of the new \<bad-namespace\> element.
260 * @return 0 on success, -1 on error.
261 */
262int nc_err_add_bad_ns(struct nc_server_error *err, const char *ns_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100263
Michal Vasko1a38c862016-01-15 15:50:07 +0100264/**
265 * @brief Add an additional custom element to an error.
266 *
267 * @param[in] err Error to modify.
268 * @param[in] other New custom XML element.
269 * @return 0 on success, -1 on error.
270 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100271int nc_err_add_info_other(struct nc_server_error *err, struct lyxml_elem *other);
272
Michal Vasko1a38c862016-01-15 15:50:07 +0100273/**
274 * @brief Free a server rpc-reply object.
275 *
276 * @param[in] reply Server rpc-reply object to free.
277 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100278void nc_server_reply_free(struct nc_server_reply *reply);
279
Michal Vasko1a38c862016-01-15 15:50:07 +0100280/**
281 * @brief Free a server error structure.
282 *
283 * @param[in] err Error structure to free.
284 */
285void nc_err_free(struct nc_server_error *err);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100286
Radek Krejci93e80222016-10-03 13:34:25 +0200287/**
288 * @brief Create Event Notification object to be sent to the subscribed client(s).
289 *
290 * @param[in] event Notification data tree (valid as LYD_OPT_NOTIF) from libyang. The tree is directly used in created
291 * object, so the caller is supposed to not free the tree on its own, but only via freeng the created object.
292 * @param[in] eventtime YANG dateTime format value of the time when the event was generated by the event source.
293 * Caller can use nc_time2datetime() to create the value from the time_t value. By default, the \p eventtime is handled
294 * the same way as the \p event tree - it is directly used in the created object and caller is supposed to avoid any
295 * further manipulation of the string. This can be changed by the \p eventtime_const parameter, which (set to nonzero)
296 * cause to make copy of the provided \p eventtime instead of using it directly.
297 * @param[in] eventtime_const Flag for changing the \p eventtime handling.
298 * @return Newly created structure of the Event Notification object to be sent to the clients via nc_server_send_notif()
299 * and freed using nc_server_notif_free().
300 */
301struct nc_server_notif *nc_server_notif_new(struct lyd_node* event, char *eventtime, int eventtime_const);
302
303/**
304 * @brief Send NETCONF Event Notification via the session.
305 *
306 * @param[in] session NETCONF session where the Event Notification will be written.
307 * @param[in] notif NETCOFN Notification object to send via specified session. Object can be created by
308 * nc_notif_new() function.
309 * @param[in] timeout Timeout for writing in milliseconds. Use negative value for infinite
310 * waiting and 0 for return if data cannot be sent immediately.
311 * @return #NC_MSG_NOTIF on success,
312 * #NC_MSG_WOULDBLOCK in case of a busy session, and
313 * #NC_MSG_ERROR on error.
314 */
315NC_MSG_TYPE nc_server_notif_send(struct nc_session *session, struct nc_server_notif *notif, int timeout);
316
317/**
318 * @brief Free a server Event Notification object.
319 *
320 * @param[in] notif Server Event Notification object to free.
321 */
322void nc_server_notif_free(struct nc_server_notif *notif);
323
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100324#endif /* NC_MESSAGES_SERVER_H_ */