blob: a816485fb76db205bfdfe3cb1aa02e1c60225571 [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 *
Michal Vaskofc9dbdd2017-03-17 09:27:57 +01006 * Copyright (c) 2015-2017 CESNET, z.s.p.o.
Michal Vasko7bcb48e2016-01-15 10:28:54 +01007 *
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
Michal Vaskoc09730e2019-01-17 10:07:26 +010018#ifdef __cplusplus
19extern "C" {
20#endif
21
Michal Vasko7bcb48e2016-01-15 10:28:54 +010022#include <stdint.h>
Michal Vasko488f4802018-09-07 13:04:41 +020023#include <libyang/libyang.h>
Michal Vasko7bcb48e2016-01-15 10:28:54 +010024
25#include "netconf.h"
Radek Krejci93e80222016-10-03 13:34:25 +020026#include "session.h"
Michal Vasko7bcb48e2016-01-15 10:28:54 +010027
Michal Vaskof0537d82016-01-29 14:42:38 +010028/**
Radek Krejci6799a052017-05-19 14:23:23 +020029 * @defgroup server_msg Server Messages
30 * @ingroup server
31 *
32 * @brief Functions to create NETCONF Event notifications and replies to the NETCONF RPCs (or actions).
33 * @{
34 */
35
36/**
Michal Vaskof0537d82016-01-29 14:42:38 +010037 * @brief Enumeration of NETCONF errors
38 */
Michal Vasko495c9462016-01-15 11:27:43 +010039typedef enum NC_ERROR {
Michal Vaskof0537d82016-01-29 14:42:38 +010040 NC_ERR_UNKNOWN = 0, /**< unknown error */
41 NC_ERR_IN_USE, /**< in-use error */
42 NC_ERR_INVALID_VALUE, /**< invalid-value error */
43 NC_ERR_TOO_BIG, /**< too-big error */
44 NC_ERR_MISSING_ATTR, /**< missing-attribute error */
45 NC_ERR_BAD_ATTR, /**< bad-attribute error */
46 NC_ERR_UNKNOWN_ATTR, /**< unknown-attribute error */
47 NC_ERR_MISSING_ELEM, /**< missing-element error */
48 NC_ERR_BAD_ELEM, /**< bad-element error */
49 NC_ERR_UNKNOWN_ELEM, /**< unknown-element error */
50 NC_ERR_UNKNOWN_NS, /**< unknown-namespace error */
51 NC_ERR_ACCESS_DENIED, /**< access-denied error */
52 NC_ERR_LOCK_DENIED, /**< lock-denied error */
53 NC_ERR_RES_DENIED, /**< resource-denied error */
54 NC_ERR_ROLLBACK_FAILED, /**< rollback-failed error */
55 NC_ERR_DATA_EXISTS, /**< data-exists error */
56 NC_ERR_DATA_MISSING, /**< data-missing error */
57 NC_ERR_OP_NOT_SUPPORTED, /**< operation-not-supported error */
58 NC_ERR_OP_FAILED, /**< operation-failed error */
59 NC_ERR_MALFORMED_MSG /**< malformed-message error */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010060} NC_ERR;
61
Michal Vaskof0537d82016-01-29 14:42:38 +010062/**
63 * @brief Enumeration of NETCONF error type (layer)
64 */
Michal Vasko495c9462016-01-15 11:27:43 +010065typedef enum NC_ERROR_TYPE {
Michal Vaskof0537d82016-01-29 14:42:38 +010066 NC_ERR_TYPE_UNKNOWN = 0, /**< unknown layer */
67 NC_ERR_TYPE_TRAN, /**< transport layer */
68 NC_ERR_TYPE_RPC, /**< RPC layer */
69 NC_ERR_TYPE_PROT, /**< protocol layer */
70 NC_ERR_TYPE_APP /**< application layer */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010071} NC_ERR_TYPE;
72
73/**
Michal Vasko1a38c862016-01-15 15:50:07 +010074 * @brief NETCONF server rpc-reply object
Michal Vasko7bcb48e2016-01-15 10:28:54 +010075 */
76struct nc_server_reply;
77
Michal Vasko1a38c862016-01-15 15:50:07 +010078/**
Radek Krejci93e80222016-10-03 13:34:25 +020079 * @brief NETCONF server Event Notification object
80 */
81struct nc_server_notif;
82
83/**
Michal Vasko1a38c862016-01-15 15:50:07 +010084 * @brief NETCONF server error structure
85 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010086struct nc_server_error;
87
Michal Vasko1a38c862016-01-15 15:50:07 +010088/**
89 * @brief Create an OK rpc-reply object.
90 *
91 * @return rpc-reply object, NULL on error.
92 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010093struct nc_server_reply *nc_server_reply_ok(void);
94
Michal Vasko1a38c862016-01-15 15:50:07 +010095/**
96 * @brief Create a DATA rpc-reply object.
97 *
Michal Vaskob08743b2016-04-13 14:23:49 +020098 * @param[in] data Reply data tree. This tree must be valid according to
99 * the RPC output of the RPC this is a reply to.
Radek Krejci36dfdb32016-09-01 16:56:35 +0200100 * @param[in] wd with-default mode if applicable
Michal Vasko1a38c862016-01-15 15:50:07 +0100101 * @param[in] paramtype Determines how the \p data parameter is treated.
102 * @return rpc-reply object, NULL on error.
103 */
Radek Krejci36dfdb32016-09-01 16:56:35 +0200104struct 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 +0100105
Michal Vasko1a38c862016-01-15 15:50:07 +0100106/**
107 * @brief Create an ERROR rpc-reply object.
108 *
109 * @param[in] err Structure holding the error information. It will be freed with the returned object.
110 * @return rpc-reply object, NULL on error.
111 */
112struct nc_server_reply *nc_server_reply_err(struct nc_server_error *err);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100113
Michal Vasko1a38c862016-01-15 15:50:07 +0100114/**
115 * @brief Add another error to an ERROR rpc-reply object. It will be freed with the returned object.
116 *
117 * @param[in] reply ERROR reply to add to.
118 * @param[in] err Structure holding the additional error information.
119 * @return 0 on success, -1 on errror.
120 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100121int nc_server_reply_add_err(struct nc_server_reply *reply, struct nc_server_error *err);
122
Michal Vasko1a38c862016-01-15 15:50:07 +0100123/**
Michal Vaskoaa2e5a72017-03-16 09:48:44 +0100124 * @brief Get last error from an ERROR rpy-reply object.
125 *
126 * @param[in] reply ERROR reply to read from.
127 * @return Last error, NULL on failure.
128 */
129const struct nc_server_error *nc_server_reply_get_last_err(const struct nc_server_reply *reply);
130
131/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100132 * @brief Create a server error structure. Its \<error-message\> is filled with
133 * a general description of the specific error.
134 *
Radek Krejci127f8952016-10-12 14:57:16 +0200135 * @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 +0100136 * specific additional parameters are required:
137 * - #NC_ERR_IN_USE
138 * - #NC_ERR_INVALID_VALUE
139 * - #NC_ERR_ACCESS_DENIED
140 * - #NC_ERR_ROLLBACK_FAILED
Michal Vaskof0537d82016-01-29 14:42:38 +0100141 * - #NC_ERR_OP_NOT_SUPPORTED
Michal Vasko1a38c862016-01-15 15:50:07 +0100142 * - #NC_ERR_TOO_BIG
143 * - #NC_ERR_RES_DENIED
144 * - #NC_ERR_OP_FAILED
145 * - `NC_ERR_TYPE type;` - type (layer) of the error.
146 * - #NC_ERR_MISSING_ATTR
147 * - #NC_ERR_BAD_ATTR
148 * - #NC_ERR_UNKNOWN_ATTR
149 * - `NC_ERR_TYPE type;` - type (layer) of the error.
150 * - `const char *attr_name;` - error \<bad-attribute\> value.
151 * - `const char *elem_name;` - error \<bad-element\> value.
152 * - #NC_ERR_MISSING_ELEM
153 * - #NC_ERR_BAD_ELEM
154 * - #NC_ERR_UNKNOWN_ELEM
155 * - `NC_ERR_TYPE type;` - type (layer) of the error.
156 * - `const char *elem_name;` - error \<bad-element\> value.
157 * - #NC_ERR_UNKNOWN_NS
158 * - `NC_ERR_TYPE type;` - type (layer) of the error.
159 * - `const char *elem_name;` - error \<bad-element\> value.
160 * - `const char *nc_name;` - error \<bad-namespace\> value.
161 * - #NC_ERR_LOCK_DENIED
162 * - `uint32_t session_id;` - error \<session-id\> value.
163 * - #NC_ERR_DATA_EXISTS
164 * - #NC_ERR_DATA_MISSING
Michal Vaskof0537d82016-01-29 14:42:38 +0100165 * - #NC_ERR_MALFORMED_MSG
Michal Vasko1a38c862016-01-15 15:50:07 +0100166 * - no additional arguments
167 * @return Server error structure, NULL on error.
168 */
Radek Krejci127f8952016-10-12 14:57:16 +0200169struct nc_server_error *nc_err(int tag, ...);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100170
Michal Vasko1a38c862016-01-15 15:50:07 +0100171/**
Radek Krejci877e1822016-04-06 16:37:43 +0200172 * @brief Create a server error structure based on libyang error.
173 *
174 * The function should be used immediately when a libyang function fails to generate
175 * NETCONF error structure based on internal libyang error information (ly_errno, ly_errmsg, ...)
176 *
Michal Vaskoc9970242018-02-14 16:03:35 +0100177 * @param[in] ctx Libyang context to read the error from.
Radek Krejci877e1822016-04-06 16:37:43 +0200178 * @return Server error structure, NULL on error.
179 */
Michal Vaskoc9970242018-02-14 16:03:35 +0100180struct nc_server_error *nc_err_libyang(struct ly_ctx *ctx);
Radek Krejci877e1822016-04-06 16:37:43 +0200181
182/**
Michal Vasko8f3198f2016-05-04 10:45:28 +0200183 * @brief Get the \<error-type\> of a server error.
184 *
185 * @param[in] err Server error to read from.
186 * @return Server error type, 0 on error.
187 */
Michal Vaskoea201782017-03-16 09:49:54 +0100188NC_ERR_TYPE nc_err_get_type(const struct nc_server_error *err);
Michal Vasko8f3198f2016-05-04 10:45:28 +0200189
190/**
191 * @brief Get the \<error-tag\> of a server error.
192 *
193 * @param[in] err Server error to read from.
194 * @return Server error tag, 0 on error.
195 */
Michal Vaskoea201782017-03-16 09:49:54 +0100196NC_ERR nc_err_get_tag(const struct nc_server_error *err);
Michal Vasko8f3198f2016-05-04 10:45:28 +0200197
198/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100199 * @brief Set the \<error-app-tag\> element of an error. Any previous value will be overwritten.
200 *
201 * @param[in] err Error to modify.
202 * @param[in] error_app_tag New value of \<error-app-tag\>.
203 * @return 0 on success, -1 on error.
204 */
205int nc_err_set_app_tag(struct nc_server_error *err, const char *error_app_tag);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100206
Michal Vasko1a38c862016-01-15 15:50:07 +0100207/**
Michal Vasko8f3198f2016-05-04 10:45:28 +0200208 * @brief Get the \<error-app-tag\> of a server error.
209 *
210 * @param[in] err Server error to read from.
211 * @return Server error app tag, NULL on error.
212 */
Michal Vaskoea201782017-03-16 09:49:54 +0100213const char *nc_err_get_app_tag(const struct nc_server_error *err);
Michal Vasko8f3198f2016-05-04 10:45:28 +0200214
215/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100216 * @brief Set the \<error-path\> element of an error. Any previous value will be overwritten.
217 *
218 * @param[in] err Error to modify.
219 * @param[in] error_path New value of \<error-path\>.
220 * @return 0 on success, -1 on error.
221 */
222int nc_err_set_path(struct nc_server_error *err, const char *error_path);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100223
Michal Vasko1a38c862016-01-15 15:50:07 +0100224/**
Michal Vasko8f3198f2016-05-04 10:45:28 +0200225 * @brief Get the \<error-path\> of a server error.
226 *
227 * @param[in] err Server error to read from.
228 * @return Server error path, NULL on error.
229 */
Michal Vaskoea201782017-03-16 09:49:54 +0100230const char *nc_err_get_path(const struct nc_server_error *err);
Michal Vasko8f3198f2016-05-04 10:45:28 +0200231
232/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100233 * @brief Set the \<error-message\> element of an error. Any previous value will be overwritten.
234 *
235 * @param[in] err Error to modify.
236 * @param[in] error_message New value of \<error-message\>.
Michal Vaskof0537d82016-01-29 14:42:38 +0100237 * @param[in] lang Optional language of \p error_message.
Michal Vasko1a38c862016-01-15 15:50:07 +0100238 * @return 0 on success, -1 on error.
239 */
240int nc_err_set_msg(struct nc_server_error *err, const char *error_message, const char *lang);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100241
Michal Vasko1a38c862016-01-15 15:50:07 +0100242/**
Michal Vasko8f3198f2016-05-04 10:45:28 +0200243 * @brief Get the \<error-message\> of a server error.
244 *
245 * @param[in] err Server error to read from.
246 * @return Server error message, NULL on error.
247 */
Michal Vaskoea201782017-03-16 09:49:54 +0100248const char *nc_err_get_msg(const struct nc_server_error *err);
Michal Vasko8f3198f2016-05-04 10:45:28 +0200249
250/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100251 * @brief Set the \<session-id\> element of an error. Any previous value will be overwritten.
252 *
253 * @param[in] err Error to modify.
Michal Vaskof0537d82016-01-29 14:42:38 +0100254 * @param[in] session_id New value of \<session-id\>.
Michal Vasko1a38c862016-01-15 15:50:07 +0100255 * @return 0 on success, -1 on error.
256 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100257int nc_err_set_sid(struct nc_server_error *err, uint32_t session_id);
258
Michal Vasko1a38c862016-01-15 15:50:07 +0100259/**
260 * @brief Add a \<bad-attribute\> element to an error.
261 *
262 * @param[in] err Error to modify.
263 * @param[in] attr_name Value of the new \<bad-attribute\> element.
264 * @return 0 on success, -1 on error.
265 */
266int nc_err_add_bad_attr(struct nc_server_error *err, const char *attr_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100267
Michal Vasko1a38c862016-01-15 15:50:07 +0100268/**
269 * @brief Add a \<bad-element\> element to an error.
270 *
271 * @param[in] err Error to modify.
272 * @param[in] elem_name Value of the new \<bad-element\> element.
273 * @return 0 on success, -1 on error.
274 */
275int nc_err_add_bad_elem(struct nc_server_error *err, const char *elem_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100276
Michal Vasko1a38c862016-01-15 15:50:07 +0100277/**
278 * @brief Add a \<bad-namespace\> element to an error.
279 *
280 * @param[in] err Error to modify.
281 * @param[in] ns_name Value of the new \<bad-namespace\> element.
282 * @return 0 on success, -1 on error.
283 */
284int nc_err_add_bad_ns(struct nc_server_error *err, const char *ns_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100285
Michal Vasko1a38c862016-01-15 15:50:07 +0100286/**
287 * @brief Add an additional custom element to an error.
288 *
289 * @param[in] err Error to modify.
290 * @param[in] other New custom XML element.
291 * @return 0 on success, -1 on error.
292 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100293int nc_err_add_info_other(struct nc_server_error *err, struct lyxml_elem *other);
294
Michal Vasko1a38c862016-01-15 15:50:07 +0100295/**
296 * @brief Free a server rpc-reply object.
297 *
298 * @param[in] reply Server rpc-reply object to free.
299 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100300void nc_server_reply_free(struct nc_server_reply *reply);
301
Michal Vasko1a38c862016-01-15 15:50:07 +0100302/**
303 * @brief Free a server error structure.
304 *
305 * @param[in] err Error structure to free.
306 */
307void nc_err_free(struct nc_server_error *err);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100308
Radek Krejci93e80222016-10-03 13:34:25 +0200309/**
310 * @brief Create Event Notification object to be sent to the subscribed client(s).
311 *
312 * @param[in] event Notification data tree (valid as LYD_OPT_NOTIF) from libyang. The tree is directly used in created
313 * object, so the caller is supposed to not free the tree on its own, but only via freeng the created object.
314 * @param[in] eventtime YANG dateTime format value of the time when the event was generated by the event source.
Michal Vaskofc9dbdd2017-03-17 09:27:57 +0100315 * Caller can use nc_time2datetime() to create the value from the time_t value.
316 * @param[in] paramtype How to further manage data parameters.
Radek Krejci93e80222016-10-03 13:34:25 +0200317 * @return Newly created structure of the Event Notification object to be sent to the clients via nc_server_send_notif()
318 * and freed using nc_server_notif_free().
319 */
Radek Krejci6799a052017-05-19 14:23:23 +0200320struct nc_server_notif *nc_server_notif_new(struct lyd_node *event, char *eventtime, NC_PARAMTYPE paramtype);
Radek Krejci93e80222016-10-03 13:34:25 +0200321
322/**
323 * @brief Send NETCONF Event Notification via the session.
324 *
325 * @param[in] session NETCONF session where the Event Notification will be written.
326 * @param[in] notif NETCOFN Notification object to send via specified session. Object can be created by
327 * nc_notif_new() function.
328 * @param[in] timeout Timeout for writing in milliseconds. Use negative value for infinite
329 * waiting and 0 for return if data cannot be sent immediately.
330 * @return #NC_MSG_NOTIF on success,
331 * #NC_MSG_WOULDBLOCK in case of a busy session, and
332 * #NC_MSG_ERROR on error.
333 */
334NC_MSG_TYPE nc_server_notif_send(struct nc_session *session, struct nc_server_notif *notif, int timeout);
335
336/**
337 * @brief Free a server Event Notification object.
338 *
339 * @param[in] notif Server Event Notification object to free.
340 */
341void nc_server_notif_free(struct nc_server_notif *notif);
342
Michal Vasko9a2e4d22017-03-17 09:44:49 +0100343/**
344 * @brief Get the notification timestamp.
345 *
346 * @param[in] notif Server notification to read from.
347 * @return Datetime timestamp of the notification, NULL on error.
348 */
349const char *nc_server_notif_get_time(const struct nc_server_notif *notif);
350
Radek Krejci6799a052017-05-19 14:23:23 +0200351/**@} Client Messages */
352
Michal Vaskoc09730e2019-01-17 10:07:26 +0100353#ifdef __cplusplus
354}
355#endif
356
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100357#endif /* NC_MESSAGES_SERVER_H_ */