blob: 5ffb019a449fd3bf48062ea54bfbdd572fef804d [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
18#include <stdint.h>
Michal Vasko488f4802018-09-07 13:04:41 +020019#include <libyang/libyang.h>
Michal Vasko7bcb48e2016-01-15 10:28:54 +010020
21#include "netconf.h"
Radek Krejci93e80222016-10-03 13:34:25 +020022#include "session.h"
Michal Vasko7bcb48e2016-01-15 10:28:54 +010023
Michal Vaskof0537d82016-01-29 14:42:38 +010024/**
Radek Krejci6799a052017-05-19 14:23:23 +020025 * @defgroup server_msg Server Messages
26 * @ingroup server
27 *
28 * @brief Functions to create NETCONF Event notifications and replies to the NETCONF RPCs (or actions).
29 * @{
30 */
31
32/**
Michal Vaskof0537d82016-01-29 14:42:38 +010033 * @brief Enumeration of NETCONF errors
34 */
Michal Vasko495c9462016-01-15 11:27:43 +010035typedef enum NC_ERROR {
Michal Vaskof0537d82016-01-29 14:42:38 +010036 NC_ERR_UNKNOWN = 0, /**< unknown error */
37 NC_ERR_IN_USE, /**< in-use error */
38 NC_ERR_INVALID_VALUE, /**< invalid-value error */
39 NC_ERR_TOO_BIG, /**< too-big error */
40 NC_ERR_MISSING_ATTR, /**< missing-attribute error */
41 NC_ERR_BAD_ATTR, /**< bad-attribute error */
42 NC_ERR_UNKNOWN_ATTR, /**< unknown-attribute error */
43 NC_ERR_MISSING_ELEM, /**< missing-element error */
44 NC_ERR_BAD_ELEM, /**< bad-element error */
45 NC_ERR_UNKNOWN_ELEM, /**< unknown-element error */
46 NC_ERR_UNKNOWN_NS, /**< unknown-namespace error */
47 NC_ERR_ACCESS_DENIED, /**< access-denied error */
48 NC_ERR_LOCK_DENIED, /**< lock-denied error */
49 NC_ERR_RES_DENIED, /**< resource-denied error */
50 NC_ERR_ROLLBACK_FAILED, /**< rollback-failed error */
51 NC_ERR_DATA_EXISTS, /**< data-exists error */
52 NC_ERR_DATA_MISSING, /**< data-missing error */
53 NC_ERR_OP_NOT_SUPPORTED, /**< operation-not-supported error */
54 NC_ERR_OP_FAILED, /**< operation-failed error */
55 NC_ERR_MALFORMED_MSG /**< malformed-message error */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010056} NC_ERR;
57
Michal Vaskof0537d82016-01-29 14:42:38 +010058/**
59 * @brief Enumeration of NETCONF error type (layer)
60 */
Michal Vasko495c9462016-01-15 11:27:43 +010061typedef enum NC_ERROR_TYPE {
Michal Vaskof0537d82016-01-29 14:42:38 +010062 NC_ERR_TYPE_UNKNOWN = 0, /**< unknown layer */
63 NC_ERR_TYPE_TRAN, /**< transport layer */
64 NC_ERR_TYPE_RPC, /**< RPC layer */
65 NC_ERR_TYPE_PROT, /**< protocol layer */
66 NC_ERR_TYPE_APP /**< application layer */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010067} NC_ERR_TYPE;
68
69/**
Michal Vasko1a38c862016-01-15 15:50:07 +010070 * @brief NETCONF server rpc-reply object
Michal Vasko7bcb48e2016-01-15 10:28:54 +010071 */
72struct nc_server_reply;
73
Michal Vasko1a38c862016-01-15 15:50:07 +010074/**
Radek Krejci93e80222016-10-03 13:34:25 +020075 * @brief NETCONF server Event Notification object
76 */
77struct nc_server_notif;
78
79/**
Michal Vasko1a38c862016-01-15 15:50:07 +010080 * @brief NETCONF server error structure
81 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010082struct nc_server_error;
83
Michal Vasko1a38c862016-01-15 15:50:07 +010084/**
85 * @brief Create an OK rpc-reply object.
86 *
87 * @return rpc-reply object, NULL on error.
88 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010089struct nc_server_reply *nc_server_reply_ok(void);
90
Michal Vasko1a38c862016-01-15 15:50:07 +010091/**
92 * @brief Create a DATA rpc-reply object.
93 *
Michal Vaskob08743b2016-04-13 14:23:49 +020094 * @param[in] data Reply data tree. This tree must be valid according to
95 * the RPC output of the RPC this is a reply to.
Radek Krejci36dfdb32016-09-01 16:56:35 +020096 * @param[in] wd with-default mode if applicable
Michal Vasko1a38c862016-01-15 15:50:07 +010097 * @param[in] paramtype Determines how the \p data parameter is treated.
98 * @return rpc-reply object, NULL on error.
99 */
Radek Krejci36dfdb32016-09-01 16:56:35 +0200100struct 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 +0100101
Michal Vasko1a38c862016-01-15 15:50:07 +0100102/**
103 * @brief Create an ERROR rpc-reply object.
104 *
105 * @param[in] err Structure holding the error information. It will be freed with the returned object.
106 * @return rpc-reply object, NULL on error.
107 */
108struct nc_server_reply *nc_server_reply_err(struct nc_server_error *err);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100109
Michal Vasko1a38c862016-01-15 15:50:07 +0100110/**
111 * @brief Add another error to an ERROR rpc-reply object. It will be freed with the returned object.
112 *
113 * @param[in] reply ERROR reply to add to.
114 * @param[in] err Structure holding the additional error information.
115 * @return 0 on success, -1 on errror.
116 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100117int nc_server_reply_add_err(struct nc_server_reply *reply, struct nc_server_error *err);
118
Michal Vasko1a38c862016-01-15 15:50:07 +0100119/**
Michal Vaskoaa2e5a72017-03-16 09:48:44 +0100120 * @brief Get last error from an ERROR rpy-reply object.
121 *
122 * @param[in] reply ERROR reply to read from.
123 * @return Last error, NULL on failure.
124 */
125const struct nc_server_error *nc_server_reply_get_last_err(const struct nc_server_reply *reply);
126
127/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100128 * @brief Create a server error structure. Its \<error-message\> is filled with
129 * a general description of the specific error.
130 *
Radek Krejci127f8952016-10-12 14:57:16 +0200131 * @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 +0100132 * specific additional parameters are required:
133 * - #NC_ERR_IN_USE
134 * - #NC_ERR_INVALID_VALUE
135 * - #NC_ERR_ACCESS_DENIED
136 * - #NC_ERR_ROLLBACK_FAILED
Michal Vaskof0537d82016-01-29 14:42:38 +0100137 * - #NC_ERR_OP_NOT_SUPPORTED
Michal Vasko1a38c862016-01-15 15:50:07 +0100138 * - #NC_ERR_TOO_BIG
139 * - #NC_ERR_RES_DENIED
140 * - #NC_ERR_OP_FAILED
141 * - `NC_ERR_TYPE type;` - type (layer) of the error.
142 * - #NC_ERR_MISSING_ATTR
143 * - #NC_ERR_BAD_ATTR
144 * - #NC_ERR_UNKNOWN_ATTR
145 * - `NC_ERR_TYPE type;` - type (layer) of the error.
146 * - `const char *attr_name;` - error \<bad-attribute\> value.
147 * - `const char *elem_name;` - error \<bad-element\> value.
148 * - #NC_ERR_MISSING_ELEM
149 * - #NC_ERR_BAD_ELEM
150 * - #NC_ERR_UNKNOWN_ELEM
151 * - `NC_ERR_TYPE type;` - type (layer) of the error.
152 * - `const char *elem_name;` - error \<bad-element\> value.
153 * - #NC_ERR_UNKNOWN_NS
154 * - `NC_ERR_TYPE type;` - type (layer) of the error.
155 * - `const char *elem_name;` - error \<bad-element\> value.
156 * - `const char *nc_name;` - error \<bad-namespace\> value.
157 * - #NC_ERR_LOCK_DENIED
158 * - `uint32_t session_id;` - error \<session-id\> value.
159 * - #NC_ERR_DATA_EXISTS
160 * - #NC_ERR_DATA_MISSING
Michal Vaskof0537d82016-01-29 14:42:38 +0100161 * - #NC_ERR_MALFORMED_MSG
Michal Vasko1a38c862016-01-15 15:50:07 +0100162 * - no additional arguments
163 * @return Server error structure, NULL on error.
164 */
Radek Krejci127f8952016-10-12 14:57:16 +0200165struct nc_server_error *nc_err(int tag, ...);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100166
Michal Vasko1a38c862016-01-15 15:50:07 +0100167/**
Radek Krejci877e1822016-04-06 16:37:43 +0200168 * @brief Create a server error structure based on libyang error.
169 *
170 * The function should be used immediately when a libyang function fails to generate
171 * NETCONF error structure based on internal libyang error information (ly_errno, ly_errmsg, ...)
172 *
Michal Vaskoc9970242018-02-14 16:03:35 +0100173 * @param[in] ctx Libyang context to read the error from.
Radek Krejci877e1822016-04-06 16:37:43 +0200174 * @return Server error structure, NULL on error.
175 */
Michal Vaskoc9970242018-02-14 16:03:35 +0100176struct nc_server_error *nc_err_libyang(struct ly_ctx *ctx);
Radek Krejci877e1822016-04-06 16:37:43 +0200177
178/**
Michal Vasko8f3198f2016-05-04 10:45:28 +0200179 * @brief Get the \<error-type\> of a server error.
180 *
181 * @param[in] err Server error to read from.
182 * @return Server error type, 0 on error.
183 */
Michal Vaskoea201782017-03-16 09:49:54 +0100184NC_ERR_TYPE nc_err_get_type(const struct nc_server_error *err);
Michal Vasko8f3198f2016-05-04 10:45:28 +0200185
186/**
187 * @brief Get the \<error-tag\> of a server error.
188 *
189 * @param[in] err Server error to read from.
190 * @return Server error tag, 0 on error.
191 */
Michal Vaskoea201782017-03-16 09:49:54 +0100192NC_ERR nc_err_get_tag(const struct nc_server_error *err);
Michal Vasko8f3198f2016-05-04 10:45:28 +0200193
194/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100195 * @brief Set the \<error-app-tag\> element of an error. Any previous value will be overwritten.
196 *
197 * @param[in] err Error to modify.
198 * @param[in] error_app_tag New value of \<error-app-tag\>.
199 * @return 0 on success, -1 on error.
200 */
201int nc_err_set_app_tag(struct nc_server_error *err, const char *error_app_tag);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100202
Michal Vasko1a38c862016-01-15 15:50:07 +0100203/**
Michal Vasko8f3198f2016-05-04 10:45:28 +0200204 * @brief Get the \<error-app-tag\> of a server error.
205 *
206 * @param[in] err Server error to read from.
207 * @return Server error app tag, NULL on error.
208 */
Michal Vaskoea201782017-03-16 09:49:54 +0100209const char *nc_err_get_app_tag(const struct nc_server_error *err);
Michal Vasko8f3198f2016-05-04 10:45:28 +0200210
211/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100212 * @brief Set the \<error-path\> element of an error. Any previous value will be overwritten.
213 *
214 * @param[in] err Error to modify.
215 * @param[in] error_path New value of \<error-path\>.
216 * @return 0 on success, -1 on error.
217 */
218int nc_err_set_path(struct nc_server_error *err, const char *error_path);
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-path\> of a server error.
222 *
223 * @param[in] err Server error to read from.
224 * @return Server error path, NULL on error.
225 */
Michal Vaskoea201782017-03-16 09:49:54 +0100226const char *nc_err_get_path(const struct nc_server_error *err);
Michal Vasko8f3198f2016-05-04 10:45:28 +0200227
228/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100229 * @brief Set the \<error-message\> element of an error. Any previous value will be overwritten.
230 *
231 * @param[in] err Error to modify.
232 * @param[in] error_message New value of \<error-message\>.
Michal Vaskof0537d82016-01-29 14:42:38 +0100233 * @param[in] lang Optional language of \p error_message.
Michal Vasko1a38c862016-01-15 15:50:07 +0100234 * @return 0 on success, -1 on error.
235 */
236int nc_err_set_msg(struct nc_server_error *err, const char *error_message, const char *lang);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100237
Michal Vasko1a38c862016-01-15 15:50:07 +0100238/**
Michal Vasko8f3198f2016-05-04 10:45:28 +0200239 * @brief Get the \<error-message\> of a server error.
240 *
241 * @param[in] err Server error to read from.
242 * @return Server error message, NULL on error.
243 */
Michal Vaskoea201782017-03-16 09:49:54 +0100244const char *nc_err_get_msg(const struct nc_server_error *err);
Michal Vasko8f3198f2016-05-04 10:45:28 +0200245
246/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100247 * @brief Set the \<session-id\> element of an error. Any previous value will be overwritten.
248 *
249 * @param[in] err Error to modify.
Michal Vaskof0537d82016-01-29 14:42:38 +0100250 * @param[in] session_id New value of \<session-id\>.
Michal Vasko1a38c862016-01-15 15:50:07 +0100251 * @return 0 on success, -1 on error.
252 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100253int nc_err_set_sid(struct nc_server_error *err, uint32_t session_id);
254
Michal Vasko1a38c862016-01-15 15:50:07 +0100255/**
256 * @brief Add a \<bad-attribute\> element to an error.
257 *
258 * @param[in] err Error to modify.
259 * @param[in] attr_name Value of the new \<bad-attribute\> element.
260 * @return 0 on success, -1 on error.
261 */
262int nc_err_add_bad_attr(struct nc_server_error *err, const char *attr_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100263
Michal Vasko1a38c862016-01-15 15:50:07 +0100264/**
265 * @brief Add a \<bad-element\> element to an error.
266 *
267 * @param[in] err Error to modify.
268 * @param[in] elem_name Value of the new \<bad-element\> element.
269 * @return 0 on success, -1 on error.
270 */
271int nc_err_add_bad_elem(struct nc_server_error *err, const char *elem_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100272
Michal Vasko1a38c862016-01-15 15:50:07 +0100273/**
274 * @brief Add a \<bad-namespace\> element to an error.
275 *
276 * @param[in] err Error to modify.
277 * @param[in] ns_name Value of the new \<bad-namespace\> element.
278 * @return 0 on success, -1 on error.
279 */
280int nc_err_add_bad_ns(struct nc_server_error *err, const char *ns_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100281
Michal Vasko1a38c862016-01-15 15:50:07 +0100282/**
283 * @brief Add an additional custom element to an error.
284 *
285 * @param[in] err Error to modify.
286 * @param[in] other New custom XML element.
287 * @return 0 on success, -1 on error.
288 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100289int nc_err_add_info_other(struct nc_server_error *err, struct lyxml_elem *other);
290
Michal Vasko1a38c862016-01-15 15:50:07 +0100291/**
292 * @brief Free a server rpc-reply object.
293 *
294 * @param[in] reply Server rpc-reply object to free.
295 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100296void nc_server_reply_free(struct nc_server_reply *reply);
297
Michal Vasko1a38c862016-01-15 15:50:07 +0100298/**
299 * @brief Free a server error structure.
300 *
301 * @param[in] err Error structure to free.
302 */
303void nc_err_free(struct nc_server_error *err);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100304
Radek Krejci93e80222016-10-03 13:34:25 +0200305/**
306 * @brief Create Event Notification object to be sent to the subscribed client(s).
307 *
308 * @param[in] event Notification data tree (valid as LYD_OPT_NOTIF) from libyang. The tree is directly used in created
309 * object, so the caller is supposed to not free the tree on its own, but only via freeng the created object.
310 * @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 +0100311 * Caller can use nc_time2datetime() to create the value from the time_t value.
312 * @param[in] paramtype How to further manage data parameters.
Radek Krejci93e80222016-10-03 13:34:25 +0200313 * @return Newly created structure of the Event Notification object to be sent to the clients via nc_server_send_notif()
314 * and freed using nc_server_notif_free().
315 */
Radek Krejci6799a052017-05-19 14:23:23 +0200316struct nc_server_notif *nc_server_notif_new(struct lyd_node *event, char *eventtime, NC_PARAMTYPE paramtype);
Radek Krejci93e80222016-10-03 13:34:25 +0200317
318/**
319 * @brief Send NETCONF Event Notification via the session.
320 *
321 * @param[in] session NETCONF session where the Event Notification will be written.
322 * @param[in] notif NETCOFN Notification object to send via specified session. Object can be created by
323 * nc_notif_new() function.
324 * @param[in] timeout Timeout for writing in milliseconds. Use negative value for infinite
325 * waiting and 0 for return if data cannot be sent immediately.
326 * @return #NC_MSG_NOTIF on success,
327 * #NC_MSG_WOULDBLOCK in case of a busy session, and
328 * #NC_MSG_ERROR on error.
329 */
330NC_MSG_TYPE nc_server_notif_send(struct nc_session *session, struct nc_server_notif *notif, int timeout);
331
332/**
333 * @brief Free a server Event Notification object.
334 *
335 * @param[in] notif Server Event Notification object to free.
336 */
337void nc_server_notif_free(struct nc_server_notif *notif);
338
Michal Vasko9a2e4d22017-03-17 09:44:49 +0100339/**
340 * @brief Get the notification timestamp.
341 *
342 * @param[in] notif Server notification to read from.
343 * @return Datetime timestamp of the notification, NULL on error.
344 */
345const char *nc_server_notif_get_time(const struct nc_server_notif *notif);
346
Radek Krejci6799a052017-05-19 14:23:23 +0200347/**@} Client Messages */
348
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100349#endif /* NC_MESSAGES_SERVER_H_ */