blob: 44659d0ee16a83cf38a10e42bd8a171cc3b342d2 [file] [log] [blame]
Michal Vasko7bcb48e2016-01-15 10:28:54 +01001/**
Michal Vaskoc446a382021-06-18 08:54:05 +02002 * @file messages_server.h
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief libnetconf2's functions and structures of server NETCONF messages.
Michal Vasko7bcb48e2016-01-15 10:28:54 +01005 *
Michal Vaskoc446a382021-06-18 08:54:05 +02006 * Copyright (c) 2015-2021 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 Vasko488f4802018-09-07 13:04:41 +020022#include <libyang/libyang.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020023#include <stdint.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 Vaskoc446a382021-06-18 08:54:05 +0200101 * @param[in] paramtype Determines how the @p data parameter is treated.
Michal Vasko1a38c862016-01-15 15:50:07 +0100102 * @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 *
Michal Vasko77367452021-02-16 16:32:18 +0100109 * @param[in] err Errors as opaque data node tree. It will be freed with the returned object.
Michal Vasko1a38c862016-01-15 15:50:07 +0100110 * @return rpc-reply object, NULL on error.
111 */
Michal Vasko77367452021-02-16 16:32:18 +0100112struct nc_server_reply *nc_server_reply_err(struct lyd_node *err);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100113
Michal Vasko1a38c862016-01-15 15:50:07 +0100114/**
Michal Vasko77367452021-02-16 16:32:18 +0100115 * @brief Add another error opaque data node tree to an ERROR rpc-reply object.
Michal Vasko1a38c862016-01-15 15:50:07 +0100116 *
117 * @param[in] reply ERROR reply to add to.
Michal Vasko77367452021-02-16 16:32:18 +0100118 * @param[in] err Error as opaque data node tree. It will be freed with the returned object.
Michal Vasko1a38c862016-01-15 15:50:07 +0100119 * @return 0 on success, -1 on errror.
120 */
Michal Vasko77367452021-02-16 16:32:18 +0100121int nc_server_reply_add_err(struct nc_server_reply *reply, struct lyd_node *err);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100122
Michal Vasko1a38c862016-01-15 15:50:07 +0100123/**
Michal Vaskof1c26c22021-04-12 16:34:33 +0200124 * @brief Get last error from an ERROR rpc-reply object.
Michal Vaskoaa2e5a72017-03-16 09:48:44 +0100125 *
126 * @param[in] reply ERROR reply to read from.
Michal Vasko77367452021-02-16 16:32:18 +0100127 * @return Last error opaque data tree, NULL on failure.
Michal Vaskoaa2e5a72017-03-16 09:48:44 +0100128 */
Michal Vasko77367452021-02-16 16:32:18 +0100129const struct lyd_node *nc_server_reply_get_last_err(const struct nc_server_reply *reply);
Michal Vaskoaa2e5a72017-03-16 09:48:44 +0100130
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 *
Michal Vasko77367452021-02-16 16:32:18 +0100135 * @param[in] ctx libyang context to use.
Radek Krejci127f8952016-10-12 14:57:16 +0200136 * @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 +0100137 * specific additional parameters are required:
138 * - #NC_ERR_IN_USE
139 * - #NC_ERR_INVALID_VALUE
140 * - #NC_ERR_ACCESS_DENIED
141 * - #NC_ERR_ROLLBACK_FAILED
Michal Vaskof0537d82016-01-29 14:42:38 +0100142 * - #NC_ERR_OP_NOT_SUPPORTED
Michal Vasko1a38c862016-01-15 15:50:07 +0100143 * - #NC_ERR_TOO_BIG
144 * - #NC_ERR_RES_DENIED
145 * - #NC_ERR_OP_FAILED
146 * - `NC_ERR_TYPE type;` - type (layer) of the error.
147 * - #NC_ERR_MISSING_ATTR
148 * - #NC_ERR_BAD_ATTR
149 * - #NC_ERR_UNKNOWN_ATTR
150 * - `NC_ERR_TYPE type;` - type (layer) of the error.
151 * - `const char *attr_name;` - error \<bad-attribute\> value.
152 * - `const char *elem_name;` - error \<bad-element\> value.
153 * - #NC_ERR_MISSING_ELEM
154 * - #NC_ERR_BAD_ELEM
155 * - #NC_ERR_UNKNOWN_ELEM
156 * - `NC_ERR_TYPE type;` - type (layer) of the error.
157 * - `const char *elem_name;` - error \<bad-element\> value.
158 * - #NC_ERR_UNKNOWN_NS
159 * - `NC_ERR_TYPE type;` - type (layer) of the error.
160 * - `const char *elem_name;` - error \<bad-element\> value.
161 * - `const char *nc_name;` - error \<bad-namespace\> value.
162 * - #NC_ERR_LOCK_DENIED
163 * - `uint32_t session_id;` - error \<session-id\> value.
164 * - #NC_ERR_DATA_EXISTS
165 * - #NC_ERR_DATA_MISSING
Michal Vaskof0537d82016-01-29 14:42:38 +0100166 * - #NC_ERR_MALFORMED_MSG
Michal Vasko1a38c862016-01-15 15:50:07 +0100167 * - no additional arguments
Michal Vasko77367452021-02-16 16:32:18 +0100168 * @return Opaque data node tree representing the error.
Michal Vasko1a38c862016-01-15 15:50:07 +0100169 */
Michal Vasko77367452021-02-16 16:32:18 +0100170struct lyd_node *nc_err(const struct ly_ctx *ctx, NC_ERR tag, ...);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100171
Michal Vasko1a38c862016-01-15 15:50:07 +0100172/**
Michal Vasko8f3198f2016-05-04 10:45:28 +0200173 * @brief Get the \<error-type\> of a server error.
174 *
Michal Vasko77367452021-02-16 16:32:18 +0100175 * @param[in] err Error opaque data node tree to read from.
Michal Vasko8f3198f2016-05-04 10:45:28 +0200176 * @return Server error type, 0 on error.
177 */
Michal Vasko77367452021-02-16 16:32:18 +0100178NC_ERR_TYPE nc_err_get_type(const struct lyd_node *err);
Michal Vasko8f3198f2016-05-04 10:45:28 +0200179
180/**
181 * @brief Get the \<error-tag\> of a server error.
182 *
Michal Vasko77367452021-02-16 16:32:18 +0100183 * @param[in] err Error opaque data node tree to read from.
Michal Vasko8f3198f2016-05-04 10:45:28 +0200184 * @return Server error tag, 0 on error.
185 */
Michal Vasko77367452021-02-16 16:32:18 +0100186NC_ERR nc_err_get_tag(const struct lyd_node *err);
Michal Vasko8f3198f2016-05-04 10:45:28 +0200187
188/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100189 * @brief Set the \<error-app-tag\> element of an error. Any previous value will be overwritten.
190 *
Michal Vasko77367452021-02-16 16:32:18 +0100191 * @param[in] err Error opaque data node tree to modify.
Michal Vasko1a38c862016-01-15 15:50:07 +0100192 * @param[in] error_app_tag New value of \<error-app-tag\>.
193 * @return 0 on success, -1 on error.
194 */
Michal Vasko77367452021-02-16 16:32:18 +0100195int nc_err_set_app_tag(struct lyd_node *err, const char *error_app_tag);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100196
Michal Vasko1a38c862016-01-15 15:50:07 +0100197/**
Michal Vasko8f3198f2016-05-04 10:45:28 +0200198 * @brief Get the \<error-app-tag\> of a server error.
199 *
Michal Vasko77367452021-02-16 16:32:18 +0100200 * @param[in] err Error opaque data node tree to read from.
Michal Vasko8f3198f2016-05-04 10:45:28 +0200201 * @return Server error app tag, NULL on error.
202 */
Michal Vasko77367452021-02-16 16:32:18 +0100203const char *nc_err_get_app_tag(const struct lyd_node *err);
Michal Vasko8f3198f2016-05-04 10:45:28 +0200204
205/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100206 * @brief Set the \<error-path\> element of an error. Any previous value will be overwritten.
207 *
Michal Vasko77367452021-02-16 16:32:18 +0100208 * @param[in] err Error opaque data node tree to modify.
Michal Vasko1a38c862016-01-15 15:50:07 +0100209 * @param[in] error_path New value of \<error-path\>.
210 * @return 0 on success, -1 on error.
211 */
Michal Vasko77367452021-02-16 16:32:18 +0100212int nc_err_set_path(struct lyd_node *err, const char *error_path);
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-path\> of a server error.
216 *
Michal Vasko77367452021-02-16 16:32:18 +0100217 * @param[in] err Error opaque data node tree to read from.
Michal Vasko8f3198f2016-05-04 10:45:28 +0200218 * @return Server error path, NULL on error.
219 */
Michal Vasko77367452021-02-16 16:32:18 +0100220const char *nc_err_get_path(const struct lyd_node *err);
Michal Vasko8f3198f2016-05-04 10:45:28 +0200221
222/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100223 * @brief Set the \<error-message\> element of an error. Any previous value will be overwritten.
224 *
Michal Vasko77367452021-02-16 16:32:18 +0100225 * @param[in] err Error opaque data node tree to modify.
Michal Vasko1a38c862016-01-15 15:50:07 +0100226 * @param[in] error_message New value of \<error-message\>.
Michal Vaskoc446a382021-06-18 08:54:05 +0200227 * @param[in] lang Optional language of @p error_message.
Michal Vasko1a38c862016-01-15 15:50:07 +0100228 * @return 0 on success, -1 on error.
229 */
Michal Vasko77367452021-02-16 16:32:18 +0100230int nc_err_set_msg(struct lyd_node *err, const char *error_message, const char *lang);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100231
Michal Vasko1a38c862016-01-15 15:50:07 +0100232/**
Michal Vasko8f3198f2016-05-04 10:45:28 +0200233 * @brief Get the \<error-message\> of a server error.
234 *
Michal Vasko77367452021-02-16 16:32:18 +0100235 * @param[in] err Error opaque data node tree to read from.
Michal Vasko8f3198f2016-05-04 10:45:28 +0200236 * @return Server error message, NULL on error.
237 */
Michal Vasko77367452021-02-16 16:32:18 +0100238const char *nc_err_get_msg(const struct lyd_node *err);
Michal Vasko8f3198f2016-05-04 10:45:28 +0200239
240/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100241 * @brief Set the \<session-id\> element of an error. Any previous value will be overwritten.
242 *
Michal Vasko77367452021-02-16 16:32:18 +0100243 * @param[in] err Error opaque data node tree to modify.
Michal Vaskof0537d82016-01-29 14:42:38 +0100244 * @param[in] session_id New value of \<session-id\>.
Michal Vasko1a38c862016-01-15 15:50:07 +0100245 * @return 0 on success, -1 on error.
246 */
Michal Vasko77367452021-02-16 16:32:18 +0100247int nc_err_set_sid(struct lyd_node *err, uint32_t session_id);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100248
Michal Vasko1a38c862016-01-15 15:50:07 +0100249/**
250 * @brief Add a \<bad-attribute\> element to an error.
251 *
Michal Vasko77367452021-02-16 16:32:18 +0100252 * @param[in] err Error opaque data node tree to modify.
Michal Vasko1a38c862016-01-15 15:50:07 +0100253 * @param[in] attr_name Value of the new \<bad-attribute\> element.
254 * @return 0 on success, -1 on error.
255 */
Michal Vasko77367452021-02-16 16:32:18 +0100256int nc_err_add_bad_attr(struct lyd_node *err, const char *attr_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100257
Michal Vasko1a38c862016-01-15 15:50:07 +0100258/**
259 * @brief Add a \<bad-element\> element to an error.
260 *
Michal Vasko77367452021-02-16 16:32:18 +0100261 * @param[in] err Error opaque data node tree to modify.
Michal Vasko1a38c862016-01-15 15:50:07 +0100262 * @param[in] elem_name Value of the new \<bad-element\> element.
263 * @return 0 on success, -1 on error.
264 */
Michal Vasko77367452021-02-16 16:32:18 +0100265int nc_err_add_bad_elem(struct lyd_node *err, const char *elem_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100266
Michal Vasko1a38c862016-01-15 15:50:07 +0100267/**
268 * @brief Add a \<bad-namespace\> element to an error.
269 *
Michal Vasko77367452021-02-16 16:32:18 +0100270 * @param[in] err Error opaque data node tree to modify.
Michal Vasko1a38c862016-01-15 15:50:07 +0100271 * @param[in] ns_name Value of the new \<bad-namespace\> element.
272 * @return 0 on success, -1 on error.
273 */
Michal Vasko77367452021-02-16 16:32:18 +0100274int nc_err_add_bad_ns(struct lyd_node *err, const char *ns_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100275
Michal Vasko1a38c862016-01-15 15:50:07 +0100276/**
277 * @brief Add an additional custom element to an error.
278 *
Michal Vasko77367452021-02-16 16:32:18 +0100279 * @param[in] err Error opaque data node tree to modify.
280 * @param[in] other Other error opaque data node tree.
Michal Vasko1a38c862016-01-15 15:50:07 +0100281 * @return 0 on success, -1 on error.
282 */
Michal Vasko77367452021-02-16 16:32:18 +0100283int nc_err_add_info_other(struct lyd_node *err, struct lyd_node *other);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100284
Michal Vasko1a38c862016-01-15 15:50:07 +0100285/**
286 * @brief Free a server rpc-reply object.
287 *
288 * @param[in] reply Server rpc-reply object to free.
289 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100290void nc_server_reply_free(struct nc_server_reply *reply);
291
Michal Vasko1a38c862016-01-15 15:50:07 +0100292/**
Radek Krejci93e80222016-10-03 13:34:25 +0200293 * @brief Create Event Notification object to be sent to the subscribed client(s).
294 *
295 * @param[in] event Notification data tree (valid as LYD_OPT_NOTIF) from libyang. The tree is directly used in created
296 * object, so the caller is supposed to not free the tree on its own, but only via freeng the created object.
297 * @param[in] eventtime YANG dateTime format value of the time when the event was generated by the event source.
Michal Vasko49eb3f42021-05-19 10:20:57 +0200298 * Caller can use nc_timespec2datetime() to create the value from a timespec value.
Michal Vaskofc9dbdd2017-03-17 09:27:57 +0100299 * @param[in] paramtype How to further manage data parameters.
Radek Krejci93e80222016-10-03 13:34:25 +0200300 * @return Newly created structure of the Event Notification object to be sent to the clients via nc_server_send_notif()
301 * and freed using nc_server_notif_free().
302 */
Radek Krejci6799a052017-05-19 14:23:23 +0200303struct nc_server_notif *nc_server_notif_new(struct lyd_node *event, char *eventtime, NC_PARAMTYPE paramtype);
Radek Krejci93e80222016-10-03 13:34:25 +0200304
305/**
306 * @brief Send NETCONF Event Notification via the session.
307 *
308 * @param[in] session NETCONF session where the Event Notification will be written.
309 * @param[in] notif NETCOFN Notification object to send via specified session. Object can be created by
310 * nc_notif_new() function.
311 * @param[in] timeout Timeout for writing in milliseconds. Use negative value for infinite
312 * waiting and 0 for return if data cannot be sent immediately.
313 * @return #NC_MSG_NOTIF on success,
314 * #NC_MSG_WOULDBLOCK in case of a busy session, and
315 * #NC_MSG_ERROR on error.
316 */
317NC_MSG_TYPE nc_server_notif_send(struct nc_session *session, struct nc_server_notif *notif, int timeout);
318
319/**
320 * @brief Free a server Event Notification object.
321 *
322 * @param[in] notif Server Event Notification object to free.
323 */
324void nc_server_notif_free(struct nc_server_notif *notif);
325
Michal Vasko9a2e4d22017-03-17 09:44:49 +0100326/**
327 * @brief Get the notification timestamp.
328 *
329 * @param[in] notif Server notification to read from.
330 * @return Datetime timestamp of the notification, NULL on error.
331 */
332const char *nc_server_notif_get_time(const struct nc_server_notif *notif);
333
Michal Vaskoc446a382021-06-18 08:54:05 +0200334/** @} Client Messages */
Radek Krejci6799a052017-05-19 14:23:23 +0200335
Michal Vaskoc09730e2019-01-17 10:07:26 +0100336#ifdef __cplusplus
337}
338#endif
339
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100340#endif /* NC_MESSAGES_SERVER_H_ */