blob: 151e50c3eb26b21709f211fd0bdb3fee070ca11d [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>
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/**
Radek Krejci6799a052017-05-19 14:23:23 +020024 * @defgroup server_msg Server Messages
25 * @ingroup server
26 *
27 * @brief Functions to create NETCONF Event notifications and replies to the NETCONF RPCs (or actions).
28 * @{
29 */
30
31/**
Michal Vaskof0537d82016-01-29 14:42:38 +010032 * @brief Enumeration of NETCONF errors
33 */
Michal Vasko495c9462016-01-15 11:27:43 +010034typedef enum NC_ERROR {
Michal Vaskof0537d82016-01-29 14:42:38 +010035 NC_ERR_UNKNOWN = 0, /**< unknown error */
36 NC_ERR_IN_USE, /**< in-use error */
37 NC_ERR_INVALID_VALUE, /**< invalid-value error */
38 NC_ERR_TOO_BIG, /**< too-big error */
39 NC_ERR_MISSING_ATTR, /**< missing-attribute error */
40 NC_ERR_BAD_ATTR, /**< bad-attribute error */
41 NC_ERR_UNKNOWN_ATTR, /**< unknown-attribute error */
42 NC_ERR_MISSING_ELEM, /**< missing-element error */
43 NC_ERR_BAD_ELEM, /**< bad-element error */
44 NC_ERR_UNKNOWN_ELEM, /**< unknown-element error */
45 NC_ERR_UNKNOWN_NS, /**< unknown-namespace error */
46 NC_ERR_ACCESS_DENIED, /**< access-denied error */
47 NC_ERR_LOCK_DENIED, /**< lock-denied error */
48 NC_ERR_RES_DENIED, /**< resource-denied error */
49 NC_ERR_ROLLBACK_FAILED, /**< rollback-failed error */
50 NC_ERR_DATA_EXISTS, /**< data-exists error */
51 NC_ERR_DATA_MISSING, /**< data-missing error */
52 NC_ERR_OP_NOT_SUPPORTED, /**< operation-not-supported error */
53 NC_ERR_OP_FAILED, /**< operation-failed error */
54 NC_ERR_MALFORMED_MSG /**< malformed-message error */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010055} NC_ERR;
56
Michal Vaskof0537d82016-01-29 14:42:38 +010057/**
58 * @brief Enumeration of NETCONF error type (layer)
59 */
Michal Vasko495c9462016-01-15 11:27:43 +010060typedef enum NC_ERROR_TYPE {
Michal Vaskof0537d82016-01-29 14:42:38 +010061 NC_ERR_TYPE_UNKNOWN = 0, /**< unknown layer */
62 NC_ERR_TYPE_TRAN, /**< transport layer */
63 NC_ERR_TYPE_RPC, /**< RPC layer */
64 NC_ERR_TYPE_PROT, /**< protocol layer */
65 NC_ERR_TYPE_APP /**< application layer */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010066} NC_ERR_TYPE;
67
68/**
Michal Vasko1a38c862016-01-15 15:50:07 +010069 * @brief NETCONF server rpc-reply object
Michal Vasko7bcb48e2016-01-15 10:28:54 +010070 */
71struct nc_server_reply;
72
Michal Vasko1a38c862016-01-15 15:50:07 +010073/**
Radek Krejci93e80222016-10-03 13:34:25 +020074 * @brief NETCONF server Event Notification object
75 */
76struct nc_server_notif;
77
78/**
Michal Vasko1a38c862016-01-15 15:50:07 +010079 * @brief NETCONF server error structure
80 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010081struct nc_server_error;
82
Michal Vasko1a38c862016-01-15 15:50:07 +010083/**
84 * @brief Create an OK rpc-reply object.
85 *
86 * @return rpc-reply object, NULL on error.
87 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010088struct nc_server_reply *nc_server_reply_ok(void);
89
Michal Vasko1a38c862016-01-15 15:50:07 +010090/**
91 * @brief Create a DATA rpc-reply object.
92 *
Michal Vaskob08743b2016-04-13 14:23:49 +020093 * @param[in] data Reply data tree. This tree must be valid according to
94 * the RPC output of the RPC this is a reply to.
Radek Krejci36dfdb32016-09-01 16:56:35 +020095 * @param[in] wd with-default mode if applicable
Michal Vasko1a38c862016-01-15 15:50:07 +010096 * @param[in] paramtype Determines how the \p data parameter is treated.
97 * @return rpc-reply object, NULL on error.
98 */
Radek Krejci36dfdb32016-09-01 16:56:35 +020099struct 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 +0100100
Michal Vasko1a38c862016-01-15 15:50:07 +0100101/**
102 * @brief Create an ERROR rpc-reply object.
103 *
104 * @param[in] err Structure holding the error information. It will be freed with the returned object.
105 * @return rpc-reply object, NULL on error.
106 */
107struct nc_server_reply *nc_server_reply_err(struct nc_server_error *err);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100108
Michal Vasko1a38c862016-01-15 15:50:07 +0100109/**
110 * @brief Add another error to an ERROR rpc-reply object. It will be freed with the returned object.
111 *
112 * @param[in] reply ERROR reply to add to.
113 * @param[in] err Structure holding the additional error information.
114 * @return 0 on success, -1 on errror.
115 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100116int nc_server_reply_add_err(struct nc_server_reply *reply, struct nc_server_error *err);
117
Michal Vasko1a38c862016-01-15 15:50:07 +0100118/**
Michal Vaskoaa2e5a72017-03-16 09:48:44 +0100119 * @brief Get last error from an ERROR rpy-reply object.
120 *
121 * @param[in] reply ERROR reply to read from.
122 * @return Last error, NULL on failure.
123 */
124const struct nc_server_error *nc_server_reply_get_last_err(const struct nc_server_reply *reply);
125
126/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100127 * @brief Create a server error structure. Its \<error-message\> is filled with
128 * a general description of the specific error.
129 *
Radek Krejci127f8952016-10-12 14:57:16 +0200130 * @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 +0100131 * specific additional parameters are required:
132 * - #NC_ERR_IN_USE
133 * - #NC_ERR_INVALID_VALUE
134 * - #NC_ERR_ACCESS_DENIED
135 * - #NC_ERR_ROLLBACK_FAILED
Michal Vaskof0537d82016-01-29 14:42:38 +0100136 * - #NC_ERR_OP_NOT_SUPPORTED
Michal Vasko1a38c862016-01-15 15:50:07 +0100137 * - #NC_ERR_TOO_BIG
138 * - #NC_ERR_RES_DENIED
139 * - #NC_ERR_OP_FAILED
140 * - `NC_ERR_TYPE type;` - type (layer) of the error.
141 * - #NC_ERR_MISSING_ATTR
142 * - #NC_ERR_BAD_ATTR
143 * - #NC_ERR_UNKNOWN_ATTR
144 * - `NC_ERR_TYPE type;` - type (layer) of the error.
145 * - `const char *attr_name;` - error \<bad-attribute\> value.
146 * - `const char *elem_name;` - error \<bad-element\> value.
147 * - #NC_ERR_MISSING_ELEM
148 * - #NC_ERR_BAD_ELEM
149 * - #NC_ERR_UNKNOWN_ELEM
150 * - `NC_ERR_TYPE type;` - type (layer) of the error.
151 * - `const char *elem_name;` - error \<bad-element\> value.
152 * - #NC_ERR_UNKNOWN_NS
153 * - `NC_ERR_TYPE type;` - type (layer) of the error.
154 * - `const char *elem_name;` - error \<bad-element\> value.
155 * - `const char *nc_name;` - error \<bad-namespace\> value.
156 * - #NC_ERR_LOCK_DENIED
157 * - `uint32_t session_id;` - error \<session-id\> value.
158 * - #NC_ERR_DATA_EXISTS
159 * - #NC_ERR_DATA_MISSING
Michal Vaskof0537d82016-01-29 14:42:38 +0100160 * - #NC_ERR_MALFORMED_MSG
Michal Vasko1a38c862016-01-15 15:50:07 +0100161 * - no additional arguments
162 * @return Server error structure, NULL on error.
163 */
Radek Krejci127f8952016-10-12 14:57:16 +0200164struct nc_server_error *nc_err(int tag, ...);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100165
Michal Vasko1a38c862016-01-15 15:50:07 +0100166/**
Radek Krejci877e1822016-04-06 16:37:43 +0200167 * @brief Create a server error structure based on libyang error.
168 *
169 * The function should be used immediately when a libyang function fails to generate
170 * NETCONF error structure based on internal libyang error information (ly_errno, ly_errmsg, ...)
171 *
172 * @return Server error structure, NULL on error.
173 */
Michal Vasko7d9b4952016-04-08 10:44:26 +0200174struct nc_server_error *nc_err_libyang(void);
Radek Krejci877e1822016-04-06 16:37:43 +0200175
176/**
Michal Vasko8f3198f2016-05-04 10:45:28 +0200177 * @brief Get the \<error-type\> of a server error.
178 *
179 * @param[in] err Server error to read from.
180 * @return Server error type, 0 on error.
181 */
Michal Vaskoea201782017-03-16 09:49:54 +0100182NC_ERR_TYPE nc_err_get_type(const struct nc_server_error *err);
Michal Vasko8f3198f2016-05-04 10:45:28 +0200183
184/**
185 * @brief Get the \<error-tag\> of a server error.
186 *
187 * @param[in] err Server error to read from.
188 * @return Server error tag, 0 on error.
189 */
Michal Vaskoea201782017-03-16 09:49:54 +0100190NC_ERR nc_err_get_tag(const struct nc_server_error *err);
Michal Vasko8f3198f2016-05-04 10:45:28 +0200191
192/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100193 * @brief Set the \<error-app-tag\> element of an error. Any previous value will be overwritten.
194 *
195 * @param[in] err Error to modify.
196 * @param[in] error_app_tag New value of \<error-app-tag\>.
197 * @return 0 on success, -1 on error.
198 */
199int nc_err_set_app_tag(struct nc_server_error *err, const char *error_app_tag);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100200
Michal Vasko1a38c862016-01-15 15:50:07 +0100201/**
Michal Vasko8f3198f2016-05-04 10:45:28 +0200202 * @brief Get the \<error-app-tag\> of a server error.
203 *
204 * @param[in] err Server error to read from.
205 * @return Server error app tag, NULL on error.
206 */
Michal Vaskoea201782017-03-16 09:49:54 +0100207const char *nc_err_get_app_tag(const struct nc_server_error *err);
Michal Vasko8f3198f2016-05-04 10:45:28 +0200208
209/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100210 * @brief Set the \<error-path\> element of an error. Any previous value will be overwritten.
211 *
212 * @param[in] err Error to modify.
213 * @param[in] error_path New value of \<error-path\>.
214 * @return 0 on success, -1 on error.
215 */
216int nc_err_set_path(struct nc_server_error *err, const char *error_path);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100217
Michal Vasko1a38c862016-01-15 15:50:07 +0100218/**
Michal Vasko8f3198f2016-05-04 10:45:28 +0200219 * @brief Get the \<error-path\> of a server error.
220 *
221 * @param[in] err Server error to read from.
222 * @return Server error path, NULL on error.
223 */
Michal Vaskoea201782017-03-16 09:49:54 +0100224const char *nc_err_get_path(const struct nc_server_error *err);
Michal Vasko8f3198f2016-05-04 10:45:28 +0200225
226/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100227 * @brief Set the \<error-message\> element of an error. Any previous value will be overwritten.
228 *
229 * @param[in] err Error to modify.
230 * @param[in] error_message New value of \<error-message\>.
Michal Vaskof0537d82016-01-29 14:42:38 +0100231 * @param[in] lang Optional language of \p error_message.
Michal Vasko1a38c862016-01-15 15:50:07 +0100232 * @return 0 on success, -1 on error.
233 */
234int nc_err_set_msg(struct nc_server_error *err, const char *error_message, const char *lang);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100235
Michal Vasko1a38c862016-01-15 15:50:07 +0100236/**
Michal Vasko8f3198f2016-05-04 10:45:28 +0200237 * @brief Get the \<error-message\> of a server error.
238 *
239 * @param[in] err Server error to read from.
240 * @return Server error message, NULL on error.
241 */
Michal Vaskoea201782017-03-16 09:49:54 +0100242const char *nc_err_get_msg(const struct nc_server_error *err);
Michal Vasko8f3198f2016-05-04 10:45:28 +0200243
244/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100245 * @brief Set the \<session-id\> element of an error. Any previous value will be overwritten.
246 *
247 * @param[in] err Error to modify.
Michal Vaskof0537d82016-01-29 14:42:38 +0100248 * @param[in] session_id New value of \<session-id\>.
Michal Vasko1a38c862016-01-15 15:50:07 +0100249 * @return 0 on success, -1 on error.
250 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100251int nc_err_set_sid(struct nc_server_error *err, uint32_t session_id);
252
Michal Vasko1a38c862016-01-15 15:50:07 +0100253/**
254 * @brief Add a \<bad-attribute\> element to an error.
255 *
256 * @param[in] err Error to modify.
257 * @param[in] attr_name Value of the new \<bad-attribute\> element.
258 * @return 0 on success, -1 on error.
259 */
260int nc_err_add_bad_attr(struct nc_server_error *err, const char *attr_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100261
Michal Vasko1a38c862016-01-15 15:50:07 +0100262/**
263 * @brief Add a \<bad-element\> element to an error.
264 *
265 * @param[in] err Error to modify.
266 * @param[in] elem_name Value of the new \<bad-element\> element.
267 * @return 0 on success, -1 on error.
268 */
269int nc_err_add_bad_elem(struct nc_server_error *err, const char *elem_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100270
Michal Vasko1a38c862016-01-15 15:50:07 +0100271/**
272 * @brief Add a \<bad-namespace\> element to an error.
273 *
274 * @param[in] err Error to modify.
275 * @param[in] ns_name Value of the new \<bad-namespace\> element.
276 * @return 0 on success, -1 on error.
277 */
278int nc_err_add_bad_ns(struct nc_server_error *err, const char *ns_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100279
Michal Vasko1a38c862016-01-15 15:50:07 +0100280/**
281 * @brief Add an additional custom element to an error.
282 *
283 * @param[in] err Error to modify.
284 * @param[in] other New custom XML element.
285 * @return 0 on success, -1 on error.
286 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100287int nc_err_add_info_other(struct nc_server_error *err, struct lyxml_elem *other);
288
Michal Vasko1a38c862016-01-15 15:50:07 +0100289/**
290 * @brief Free a server rpc-reply object.
291 *
292 * @param[in] reply Server rpc-reply object to free.
293 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100294void nc_server_reply_free(struct nc_server_reply *reply);
295
Michal Vasko1a38c862016-01-15 15:50:07 +0100296/**
297 * @brief Free a server error structure.
298 *
299 * @param[in] err Error structure to free.
300 */
301void nc_err_free(struct nc_server_error *err);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100302
Radek Krejci93e80222016-10-03 13:34:25 +0200303/**
304 * @brief Create Event Notification object to be sent to the subscribed client(s).
305 *
306 * @param[in] event Notification data tree (valid as LYD_OPT_NOTIF) from libyang. The tree is directly used in created
307 * object, so the caller is supposed to not free the tree on its own, but only via freeng the created object.
308 * @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 +0100309 * Caller can use nc_time2datetime() to create the value from the time_t value.
310 * @param[in] paramtype How to further manage data parameters.
Radek Krejci93e80222016-10-03 13:34:25 +0200311 * @return Newly created structure of the Event Notification object to be sent to the clients via nc_server_send_notif()
312 * and freed using nc_server_notif_free().
313 */
Radek Krejci6799a052017-05-19 14:23:23 +0200314struct nc_server_notif *nc_server_notif_new(struct lyd_node *event, char *eventtime, NC_PARAMTYPE paramtype);
Radek Krejci93e80222016-10-03 13:34:25 +0200315
316/**
317 * @brief Send NETCONF Event Notification via the session.
318 *
319 * @param[in] session NETCONF session where the Event Notification will be written.
320 * @param[in] notif NETCOFN Notification object to send via specified session. Object can be created by
321 * nc_notif_new() function.
322 * @param[in] timeout Timeout for writing in milliseconds. Use negative value for infinite
323 * waiting and 0 for return if data cannot be sent immediately.
324 * @return #NC_MSG_NOTIF on success,
325 * #NC_MSG_WOULDBLOCK in case of a busy session, and
326 * #NC_MSG_ERROR on error.
327 */
328NC_MSG_TYPE nc_server_notif_send(struct nc_session *session, struct nc_server_notif *notif, int timeout);
329
330/**
331 * @brief Free a server Event Notification object.
332 *
333 * @param[in] notif Server Event Notification object to free.
334 */
335void nc_server_notif_free(struct nc_server_notif *notif);
336
Michal Vasko9a2e4d22017-03-17 09:44:49 +0100337/**
338 * @brief Get the notification timestamp.
339 *
340 * @param[in] notif Server notification to read from.
341 * @return Datetime timestamp of the notification, NULL on error.
342 */
343const char *nc_server_notif_get_time(const struct nc_server_notif *notif);
344
Radek Krejci6799a052017-05-19 14:23:23 +0200345/**@} Client Messages */
346
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100347#endif /* NC_MESSAGES_SERVER_H_ */