blob: f783bf81a964537ddc1db5cca3991b99f3b6d75d [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 Vasko95ea9ff2021-11-09 12:29:14 +01006 * @copyright
Michal Vaskoc446a382021-06-18 08:54:05 +02007 * Copyright (c) 2015-2021 CESNET, z.s.p.o.
Michal Vasko7bcb48e2016-01-15 10:28:54 +01008 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01009 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010012 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010013 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko7bcb48e2016-01-15 10:28:54 +010014 */
15
16#ifndef NC_MESSAGES_SERVER_H_
17#define NC_MESSAGES_SERVER_H_
18
Michal Vaskoc09730e2019-01-17 10:07:26 +010019#ifdef __cplusplus
20extern "C" {
21#endif
22
roman3f9b65c2023-06-05 14:26:58 +020023#include <stdarg.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020024#include <stdint.h>
Michal Vasko7bcb48e2016-01-15 10:28:54 +010025
roman3f9b65c2023-06-05 14:26:58 +020026#include <libyang/libyang.h>
27
Michal Vasko7bcb48e2016-01-15 10:28:54 +010028#include "netconf.h"
Radek Krejci93e80222016-10-03 13:34:25 +020029#include "session.h"
Michal Vasko7bcb48e2016-01-15 10:28:54 +010030
Michal Vaskof0537d82016-01-29 14:42:38 +010031/**
Radek Krejci6799a052017-05-19 14:23:23 +020032 * @defgroup server_msg Server Messages
33 * @ingroup server
34 *
35 * @brief Functions to create NETCONF Event notifications and replies to the NETCONF RPCs (or actions).
36 * @{
37 */
38
39/**
Michal Vaskof0537d82016-01-29 14:42:38 +010040 * @brief Enumeration of NETCONF errors
41 */
Michal Vasko495c9462016-01-15 11:27:43 +010042typedef enum NC_ERROR {
Michal Vaskof0537d82016-01-29 14:42:38 +010043 NC_ERR_UNKNOWN = 0, /**< unknown error */
44 NC_ERR_IN_USE, /**< in-use error */
45 NC_ERR_INVALID_VALUE, /**< invalid-value error */
46 NC_ERR_TOO_BIG, /**< too-big error */
47 NC_ERR_MISSING_ATTR, /**< missing-attribute error */
48 NC_ERR_BAD_ATTR, /**< bad-attribute error */
49 NC_ERR_UNKNOWN_ATTR, /**< unknown-attribute error */
50 NC_ERR_MISSING_ELEM, /**< missing-element error */
51 NC_ERR_BAD_ELEM, /**< bad-element error */
52 NC_ERR_UNKNOWN_ELEM, /**< unknown-element error */
53 NC_ERR_UNKNOWN_NS, /**< unknown-namespace error */
54 NC_ERR_ACCESS_DENIED, /**< access-denied error */
55 NC_ERR_LOCK_DENIED, /**< lock-denied error */
56 NC_ERR_RES_DENIED, /**< resource-denied error */
57 NC_ERR_ROLLBACK_FAILED, /**< rollback-failed error */
58 NC_ERR_DATA_EXISTS, /**< data-exists error */
59 NC_ERR_DATA_MISSING, /**< data-missing error */
60 NC_ERR_OP_NOT_SUPPORTED, /**< operation-not-supported error */
61 NC_ERR_OP_FAILED, /**< operation-failed error */
62 NC_ERR_MALFORMED_MSG /**< malformed-message error */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010063} NC_ERR;
64
Michal Vaskof0537d82016-01-29 14:42:38 +010065/**
66 * @brief Enumeration of NETCONF error type (layer)
67 */
Michal Vasko495c9462016-01-15 11:27:43 +010068typedef enum NC_ERROR_TYPE {
Michal Vaskof0537d82016-01-29 14:42:38 +010069 NC_ERR_TYPE_UNKNOWN = 0, /**< unknown layer */
70 NC_ERR_TYPE_TRAN, /**< transport layer */
71 NC_ERR_TYPE_RPC, /**< RPC layer */
72 NC_ERR_TYPE_PROT, /**< protocol layer */
73 NC_ERR_TYPE_APP /**< application layer */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010074} NC_ERR_TYPE;
75
76/**
Michal Vasko1a38c862016-01-15 15:50:07 +010077 * @brief NETCONF server rpc-reply object
Michal Vasko7bcb48e2016-01-15 10:28:54 +010078 */
79struct nc_server_reply;
80
Michal Vasko1a38c862016-01-15 15:50:07 +010081/**
Radek Krejci93e80222016-10-03 13:34:25 +020082 * @brief NETCONF server Event Notification object
83 */
84struct nc_server_notif;
85
86/**
Michal Vasko1a38c862016-01-15 15:50:07 +010087 * @brief NETCONF server error structure
88 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010089struct nc_server_error;
90
Michal Vasko1a38c862016-01-15 15:50:07 +010091/**
92 * @brief Create an OK rpc-reply object.
93 *
94 * @return rpc-reply object, NULL on error.
95 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010096struct nc_server_reply *nc_server_reply_ok(void);
97
Michal Vasko1a38c862016-01-15 15:50:07 +010098/**
99 * @brief Create a DATA rpc-reply object.
100 *
Michal Vasko3824fa72022-06-06 11:50:35 +0200101 * @param[in] data Reply data tree pointing to the RPC/action itself. This tree must be valid according to
Michal Vaskob08743b2016-04-13 14:23:49 +0200102 * the RPC output of the RPC this is a reply to.
Radek Krejci36dfdb32016-09-01 16:56:35 +0200103 * @param[in] wd with-default mode if applicable
Michal Vaskoc446a382021-06-18 08:54:05 +0200104 * @param[in] paramtype Determines how the @p data parameter is treated.
Michal Vasko1a38c862016-01-15 15:50:07 +0100105 * @return rpc-reply object, NULL on error.
106 */
Radek Krejci36dfdb32016-09-01 16:56:35 +0200107struct 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 +0100108
Michal Vasko1a38c862016-01-15 15:50:07 +0100109/**
110 * @brief Create an ERROR rpc-reply object.
111 *
roman0f5fa422023-08-07 09:03:24 +0200112 * @param[in] err Errors created by nc_err(). It will be freed with the returned object.
Michal Vasko1a38c862016-01-15 15:50:07 +0100113 * @return rpc-reply object, NULL on error.
114 */
Michal Vasko77367452021-02-16 16:32:18 +0100115struct nc_server_reply *nc_server_reply_err(struct lyd_node *err);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100116
Michal Vasko1a38c862016-01-15 15:50:07 +0100117/**
Michal Vasko77367452021-02-16 16:32:18 +0100118 * @brief Add another error opaque data node tree to an ERROR rpc-reply object.
Michal Vasko1a38c862016-01-15 15:50:07 +0100119 *
120 * @param[in] reply ERROR reply to add to.
roman0f5fa422023-08-07 09:03:24 +0200121 * @param[in] err Error created by nc_err(). It will be freed with the returned object.
Michal Vasko1a38c862016-01-15 15:50:07 +0100122 * @return 0 on success, -1 on errror.
123 */
Michal Vasko77367452021-02-16 16:32:18 +0100124int nc_server_reply_add_err(struct nc_server_reply *reply, struct lyd_node *err);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100125
Michal Vasko1a38c862016-01-15 15:50:07 +0100126/**
Michal Vaskof1c26c22021-04-12 16:34:33 +0200127 * @brief Get last error from an ERROR rpc-reply object.
Michal Vaskoaa2e5a72017-03-16 09:48:44 +0100128 *
129 * @param[in] reply ERROR reply to read from.
Michal Vasko77367452021-02-16 16:32:18 +0100130 * @return Last error opaque data tree, NULL on failure.
Michal Vaskoaa2e5a72017-03-16 09:48:44 +0100131 */
Michal Vasko77367452021-02-16 16:32:18 +0100132const struct lyd_node *nc_server_reply_get_last_err(const struct nc_server_reply *reply);
Michal Vaskoaa2e5a72017-03-16 09:48:44 +0100133
134/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100135 * @brief Create a server error structure. Its \<error-message\> is filled with
136 * a general description of the specific error.
137 *
Michal Vasko77367452021-02-16 16:32:18 +0100138 * @param[in] ctx libyang context to use.
Radek Krejci127f8952016-10-12 14:57:16 +0200139 * @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 +0100140 * specific additional parameters are required:
141 * - #NC_ERR_IN_USE
142 * - #NC_ERR_INVALID_VALUE
143 * - #NC_ERR_ACCESS_DENIED
144 * - #NC_ERR_ROLLBACK_FAILED
Michal Vaskof0537d82016-01-29 14:42:38 +0100145 * - #NC_ERR_OP_NOT_SUPPORTED
Michal Vasko1a38c862016-01-15 15:50:07 +0100146 * - #NC_ERR_TOO_BIG
147 * - #NC_ERR_RES_DENIED
148 * - #NC_ERR_OP_FAILED
149 * - `NC_ERR_TYPE type;` - type (layer) of the error.
150 * - #NC_ERR_MISSING_ATTR
151 * - #NC_ERR_BAD_ATTR
152 * - #NC_ERR_UNKNOWN_ATTR
153 * - `NC_ERR_TYPE type;` - type (layer) of the error.
154 * - `const char *attr_name;` - error \<bad-attribute\> value.
155 * - `const char *elem_name;` - error \<bad-element\> value.
156 * - #NC_ERR_MISSING_ELEM
157 * - #NC_ERR_BAD_ELEM
158 * - #NC_ERR_UNKNOWN_ELEM
159 * - `NC_ERR_TYPE type;` - type (layer) of the error.
160 * - `const char *elem_name;` - error \<bad-element\> value.
161 * - #NC_ERR_UNKNOWN_NS
162 * - `NC_ERR_TYPE type;` - type (layer) of the error.
163 * - `const char *elem_name;` - error \<bad-element\> value.
164 * - `const char *nc_name;` - error \<bad-namespace\> value.
165 * - #NC_ERR_LOCK_DENIED
166 * - `uint32_t session_id;` - error \<session-id\> value.
167 * - #NC_ERR_DATA_EXISTS
168 * - #NC_ERR_DATA_MISSING
Michal Vaskof0537d82016-01-29 14:42:38 +0100169 * - #NC_ERR_MALFORMED_MSG
Michal Vasko1a38c862016-01-15 15:50:07 +0100170 * - no additional arguments
Michal Vasko3824fa72022-06-06 11:50:35 +0200171 * @param[in] ... Additional arguments depending on the @p tag used.
Michal Vasko77367452021-02-16 16:32:18 +0100172 * @return Opaque data node tree representing the error.
Michal Vasko1a38c862016-01-15 15:50:07 +0100173 */
Michal Vasko77367452021-02-16 16:32:18 +0100174struct lyd_node *nc_err(const struct ly_ctx *ctx, NC_ERR tag, ...);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100175
Michal Vasko1a38c862016-01-15 15:50:07 +0100176/**
Michal Vasko8f3198f2016-05-04 10:45:28 +0200177 * @brief Get the \<error-type\> of a server error.
178 *
Michal Vasko77367452021-02-16 16:32:18 +0100179 * @param[in] err Error opaque data node tree to read from.
Michal Vasko8f3198f2016-05-04 10:45:28 +0200180 * @return Server error type, 0 on error.
181 */
Michal Vasko77367452021-02-16 16:32:18 +0100182NC_ERR_TYPE nc_err_get_type(const struct lyd_node *err);
Michal Vasko8f3198f2016-05-04 10:45:28 +0200183
184/**
185 * @brief Get the \<error-tag\> of a server error.
186 *
Michal Vasko77367452021-02-16 16:32:18 +0100187 * @param[in] err Error opaque data node tree to read from.
Michal Vasko8f3198f2016-05-04 10:45:28 +0200188 * @return Server error tag, 0 on error.
189 */
Michal Vasko77367452021-02-16 16:32:18 +0100190NC_ERR nc_err_get_tag(const struct lyd_node *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 *
Michal Vasko77367452021-02-16 16:32:18 +0100195 * @param[in] err Error opaque data node tree to modify.
Michal Vasko1a38c862016-01-15 15:50:07 +0100196 * @param[in] error_app_tag New value of \<error-app-tag\>.
197 * @return 0 on success, -1 on error.
198 */
Michal Vasko77367452021-02-16 16:32:18 +0100199int nc_err_set_app_tag(struct lyd_node *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 *
Michal Vasko77367452021-02-16 16:32:18 +0100204 * @param[in] err Error opaque data node tree to read from.
Michal Vasko8f3198f2016-05-04 10:45:28 +0200205 * @return Server error app tag, NULL on error.
206 */
Michal Vasko77367452021-02-16 16:32:18 +0100207const char *nc_err_get_app_tag(const struct lyd_node *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 *
Michal Vasko77367452021-02-16 16:32:18 +0100212 * @param[in] err Error opaque data node tree to modify.
Michal Vasko1a38c862016-01-15 15:50:07 +0100213 * @param[in] error_path New value of \<error-path\>.
214 * @return 0 on success, -1 on error.
215 */
Michal Vasko77367452021-02-16 16:32:18 +0100216int nc_err_set_path(struct lyd_node *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 *
Michal Vasko77367452021-02-16 16:32:18 +0100221 * @param[in] err Error opaque data node tree to read from.
Michal Vasko8f3198f2016-05-04 10:45:28 +0200222 * @return Server error path, NULL on error.
223 */
Michal Vasko77367452021-02-16 16:32:18 +0100224const char *nc_err_get_path(const struct lyd_node *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 *
Michal Vasko77367452021-02-16 16:32:18 +0100229 * @param[in] err Error opaque data node tree to modify.
Michal Vasko1a38c862016-01-15 15:50:07 +0100230 * @param[in] error_message New value of \<error-message\>.
Michal Vaskoc446a382021-06-18 08:54:05 +0200231 * @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 */
Michal Vasko77367452021-02-16 16:32:18 +0100234int nc_err_set_msg(struct lyd_node *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 *
Michal Vasko77367452021-02-16 16:32:18 +0100239 * @param[in] err Error opaque data node tree to read from.
Michal Vasko8f3198f2016-05-04 10:45:28 +0200240 * @return Server error message, NULL on error.
241 */
Michal Vasko77367452021-02-16 16:32:18 +0100242const char *nc_err_get_msg(const struct lyd_node *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 *
Michal Vasko77367452021-02-16 16:32:18 +0100247 * @param[in] err Error opaque data node tree 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 Vasko77367452021-02-16 16:32:18 +0100251int nc_err_set_sid(struct lyd_node *err, uint32_t session_id);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100252
Michal Vasko1a38c862016-01-15 15:50:07 +0100253/**
254 * @brief Add a \<bad-attribute\> element to an error.
255 *
Michal Vasko77367452021-02-16 16:32:18 +0100256 * @param[in] err Error opaque data node tree to modify.
Michal Vasko1a38c862016-01-15 15:50:07 +0100257 * @param[in] attr_name Value of the new \<bad-attribute\> element.
258 * @return 0 on success, -1 on error.
259 */
Michal Vasko77367452021-02-16 16:32:18 +0100260int nc_err_add_bad_attr(struct lyd_node *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 *
Michal Vasko77367452021-02-16 16:32:18 +0100265 * @param[in] err Error opaque data node tree to modify.
Michal Vasko1a38c862016-01-15 15:50:07 +0100266 * @param[in] elem_name Value of the new \<bad-element\> element.
267 * @return 0 on success, -1 on error.
268 */
Michal Vasko77367452021-02-16 16:32:18 +0100269int nc_err_add_bad_elem(struct lyd_node *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 *
Michal Vasko77367452021-02-16 16:32:18 +0100274 * @param[in] err Error opaque data node tree to modify.
Michal Vasko1a38c862016-01-15 15:50:07 +0100275 * @param[in] ns_name Value of the new \<bad-namespace\> element.
276 * @return 0 on success, -1 on error.
277 */
Michal Vasko77367452021-02-16 16:32:18 +0100278int nc_err_add_bad_ns(struct lyd_node *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 *
Michal Vasko77367452021-02-16 16:32:18 +0100283 * @param[in] err Error opaque data node tree to modify.
284 * @param[in] other Other error opaque data node tree.
Michal Vasko1a38c862016-01-15 15:50:07 +0100285 * @return 0 on success, -1 on error.
286 */
Michal Vasko77367452021-02-16 16:32:18 +0100287int nc_err_add_info_other(struct lyd_node *err, struct lyd_node *other);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100288
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/**
Radek Krejci93e80222016-10-03 13:34:25 +0200297 * @brief Create Event Notification object to be sent to the subscribed client(s).
298 *
299 * @param[in] event Notification data tree (valid as LYD_OPT_NOTIF) from libyang. The tree is directly used in created
300 * object, so the caller is supposed to not free the tree on its own, but only via freeng the created object.
301 * @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 +0200302 * Caller can use nc_timespec2datetime() to create the value from a timespec value.
Michal Vaskofc9dbdd2017-03-17 09:27:57 +0100303 * @param[in] paramtype How to further manage data parameters.
Radek Krejci93e80222016-10-03 13:34:25 +0200304 * @return Newly created structure of the Event Notification object to be sent to the clients via nc_server_send_notif()
305 * and freed using nc_server_notif_free().
306 */
Radek Krejci6799a052017-05-19 14:23:23 +0200307struct nc_server_notif *nc_server_notif_new(struct lyd_node *event, char *eventtime, NC_PARAMTYPE paramtype);
Radek Krejci93e80222016-10-03 13:34:25 +0200308
309/**
310 * @brief Send NETCONF Event Notification via the session.
311 *
312 * @param[in] session NETCONF session where the Event Notification will be written.
313 * @param[in] notif NETCOFN Notification object to send via specified session. Object can be created by
314 * nc_notif_new() function.
315 * @param[in] timeout Timeout for writing in milliseconds. Use negative value for infinite
316 * waiting and 0 for return if data cannot be sent immediately.
317 * @return #NC_MSG_NOTIF on success,
318 * #NC_MSG_WOULDBLOCK in case of a busy session, and
319 * #NC_MSG_ERROR on error.
320 */
321NC_MSG_TYPE nc_server_notif_send(struct nc_session *session, struct nc_server_notif *notif, int timeout);
322
323/**
324 * @brief Free a server Event Notification object.
325 *
326 * @param[in] notif Server Event Notification object to free.
327 */
328void nc_server_notif_free(struct nc_server_notif *notif);
329
Michal Vasko9a2e4d22017-03-17 09:44:49 +0100330/**
331 * @brief Get the notification timestamp.
332 *
333 * @param[in] notif Server notification to read from.
334 * @return Datetime timestamp of the notification, NULL on error.
335 */
336const char *nc_server_notif_get_time(const struct nc_server_notif *notif);
337
Michal Vaskoc446a382021-06-18 08:54:05 +0200338/** @} Client Messages */
Radek Krejci6799a052017-05-19 14:23:23 +0200339
Michal Vaskoc09730e2019-01-17 10:07:26 +0100340#ifdef __cplusplus
341}
342#endif
343
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100344#endif /* NC_MESSAGES_SERVER_H_ */