blob: 25aa0a77f4b3824ce71e9fd80aea7d6ad838b889 [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 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
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"
21
Michal Vaskof0537d82016-01-29 14:42:38 +010022/**
23 * @brief Enumeration of NETCONF errors
24 */
Michal Vasko495c9462016-01-15 11:27:43 +010025typedef enum NC_ERROR {
Michal Vaskof0537d82016-01-29 14:42:38 +010026 NC_ERR_UNKNOWN = 0, /**< unknown error */
27 NC_ERR_IN_USE, /**< in-use error */
28 NC_ERR_INVALID_VALUE, /**< invalid-value error */
29 NC_ERR_TOO_BIG, /**< too-big error */
30 NC_ERR_MISSING_ATTR, /**< missing-attribute error */
31 NC_ERR_BAD_ATTR, /**< bad-attribute error */
32 NC_ERR_UNKNOWN_ATTR, /**< unknown-attribute error */
33 NC_ERR_MISSING_ELEM, /**< missing-element error */
34 NC_ERR_BAD_ELEM, /**< bad-element error */
35 NC_ERR_UNKNOWN_ELEM, /**< unknown-element error */
36 NC_ERR_UNKNOWN_NS, /**< unknown-namespace error */
37 NC_ERR_ACCESS_DENIED, /**< access-denied error */
38 NC_ERR_LOCK_DENIED, /**< lock-denied error */
39 NC_ERR_RES_DENIED, /**< resource-denied error */
40 NC_ERR_ROLLBACK_FAILED, /**< rollback-failed error */
41 NC_ERR_DATA_EXISTS, /**< data-exists error */
42 NC_ERR_DATA_MISSING, /**< data-missing error */
43 NC_ERR_OP_NOT_SUPPORTED, /**< operation-not-supported error */
44 NC_ERR_OP_FAILED, /**< operation-failed error */
45 NC_ERR_MALFORMED_MSG /**< malformed-message error */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010046} NC_ERR;
47
Michal Vaskof0537d82016-01-29 14:42:38 +010048/**
49 * @brief Enumeration of NETCONF error type (layer)
50 */
Michal Vasko495c9462016-01-15 11:27:43 +010051typedef enum NC_ERROR_TYPE {
Michal Vaskof0537d82016-01-29 14:42:38 +010052 NC_ERR_TYPE_UNKNOWN = 0, /**< unknown layer */
53 NC_ERR_TYPE_TRAN, /**< transport layer */
54 NC_ERR_TYPE_RPC, /**< RPC layer */
55 NC_ERR_TYPE_PROT, /**< protocol layer */
56 NC_ERR_TYPE_APP /**< application layer */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010057} NC_ERR_TYPE;
58
59/**
Michal Vasko1a38c862016-01-15 15:50:07 +010060 * @brief NETCONF server rpc-reply object
Michal Vasko7bcb48e2016-01-15 10:28:54 +010061 */
62struct nc_server_reply;
63
Michal Vasko1a38c862016-01-15 15:50:07 +010064/**
65 * @brief NETCONF server error structure
66 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010067struct nc_server_error;
68
Michal Vasko1a38c862016-01-15 15:50:07 +010069/**
70 * @brief Create an OK rpc-reply object.
71 *
72 * @return rpc-reply object, NULL on error.
73 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010074struct nc_server_reply *nc_server_reply_ok(void);
75
Michal Vasko1a38c862016-01-15 15:50:07 +010076/**
77 * @brief Create a DATA rpc-reply object.
78 *
Michal Vaskob08743b2016-04-13 14:23:49 +020079 * @param[in] data Reply data tree. This tree must be valid according to
80 * the RPC output of the RPC this is a reply to.
Michal Vasko1a38c862016-01-15 15:50:07 +010081 * @param[in] paramtype Determines how the \p data parameter is treated.
82 * @return rpc-reply object, NULL on error.
83 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010084struct nc_server_reply *nc_server_reply_data(struct lyd_node *data, NC_PARAMTYPE paramtype);
85
Michal Vasko1a38c862016-01-15 15:50:07 +010086/**
87 * @brief Create an ERROR rpc-reply object.
88 *
89 * @param[in] err Structure holding the error information. It will be freed with the returned object.
90 * @return rpc-reply object, NULL on error.
91 */
92struct nc_server_reply *nc_server_reply_err(struct nc_server_error *err);
Michal Vasko7bcb48e2016-01-15 10:28:54 +010093
Michal Vasko1a38c862016-01-15 15:50:07 +010094/**
95 * @brief Add another error to an ERROR rpc-reply object. It will be freed with the returned object.
96 *
97 * @param[in] reply ERROR reply to add to.
98 * @param[in] err Structure holding the additional error information.
99 * @return 0 on success, -1 on errror.
100 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100101int nc_server_reply_add_err(struct nc_server_reply *reply, struct nc_server_error *err);
102
Michal Vasko1a38c862016-01-15 15:50:07 +0100103/**
104 * @brief Create a server error structure. Its \<error-message\> is filled with
105 * a general description of the specific error.
106 *
107 * @param[in] tag \<error-tag\> of the server error. According to the tag, the
108 * specific additional parameters are required:
109 * - #NC_ERR_IN_USE
110 * - #NC_ERR_INVALID_VALUE
111 * - #NC_ERR_ACCESS_DENIED
112 * - #NC_ERR_ROLLBACK_FAILED
Michal Vaskof0537d82016-01-29 14:42:38 +0100113 * - #NC_ERR_OP_NOT_SUPPORTED
Michal Vasko1a38c862016-01-15 15:50:07 +0100114 * - #NC_ERR_TOO_BIG
115 * - #NC_ERR_RES_DENIED
116 * - #NC_ERR_OP_FAILED
117 * - `NC_ERR_TYPE type;` - type (layer) of the error.
118 * - #NC_ERR_MISSING_ATTR
119 * - #NC_ERR_BAD_ATTR
120 * - #NC_ERR_UNKNOWN_ATTR
121 * - `NC_ERR_TYPE type;` - type (layer) of the error.
122 * - `const char *attr_name;` - error \<bad-attribute\> value.
123 * - `const char *elem_name;` - error \<bad-element\> value.
124 * - #NC_ERR_MISSING_ELEM
125 * - #NC_ERR_BAD_ELEM
126 * - #NC_ERR_UNKNOWN_ELEM
127 * - `NC_ERR_TYPE type;` - type (layer) of the error.
128 * - `const char *elem_name;` - error \<bad-element\> value.
129 * - #NC_ERR_UNKNOWN_NS
130 * - `NC_ERR_TYPE type;` - type (layer) of the error.
131 * - `const char *elem_name;` - error \<bad-element\> value.
132 * - `const char *nc_name;` - error \<bad-namespace\> value.
133 * - #NC_ERR_LOCK_DENIED
134 * - `uint32_t session_id;` - error \<session-id\> value.
135 * - #NC_ERR_DATA_EXISTS
136 * - #NC_ERR_DATA_MISSING
Michal Vaskof0537d82016-01-29 14:42:38 +0100137 * - #NC_ERR_MALFORMED_MSG
Michal Vasko1a38c862016-01-15 15:50:07 +0100138 * - no additional arguments
139 * @return Server error structure, NULL on error.
140 */
141struct nc_server_error *nc_err(NC_ERR tag, ...);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100142
Michal Vasko1a38c862016-01-15 15:50:07 +0100143/**
Radek Krejci877e1822016-04-06 16:37:43 +0200144 * @brief Create a server error structure based on libyang error.
145 *
146 * The function should be used immediately when a libyang function fails to generate
147 * NETCONF error structure based on internal libyang error information (ly_errno, ly_errmsg, ...)
148 *
149 * @return Server error structure, NULL on error.
150 */
Michal Vasko7d9b4952016-04-08 10:44:26 +0200151struct nc_server_error *nc_err_libyang(void);
Radek Krejci877e1822016-04-06 16:37:43 +0200152
153/**
Michal Vasko1a38c862016-01-15 15:50:07 +0100154 * @brief Set the \<error-app-tag\> element of an error. Any previous value will be overwritten.
155 *
156 * @param[in] err Error to modify.
157 * @param[in] error_app_tag New value of \<error-app-tag\>.
158 * @return 0 on success, -1 on error.
159 */
160int nc_err_set_app_tag(struct nc_server_error *err, const char *error_app_tag);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100161
Michal Vasko1a38c862016-01-15 15:50:07 +0100162/**
163 * @brief Set the \<error-path\> element of an error. Any previous value will be overwritten.
164 *
165 * @param[in] err Error to modify.
166 * @param[in] error_path New value of \<error-path\>.
167 * @return 0 on success, -1 on error.
168 */
169int nc_err_set_path(struct nc_server_error *err, const char *error_path);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100170
Michal Vasko1a38c862016-01-15 15:50:07 +0100171/**
172 * @brief Set the \<error-message\> element of an error. Any previous value will be overwritten.
173 *
174 * @param[in] err Error to modify.
175 * @param[in] error_message New value of \<error-message\>.
Michal Vaskof0537d82016-01-29 14:42:38 +0100176 * @param[in] lang Optional language of \p error_message.
Michal Vasko1a38c862016-01-15 15:50:07 +0100177 * @return 0 on success, -1 on error.
178 */
179int nc_err_set_msg(struct nc_server_error *err, const char *error_message, const char *lang);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100180
Michal Vasko1a38c862016-01-15 15:50:07 +0100181/**
182 * @brief Set the \<session-id\> element of an error. Any previous value will be overwritten.
183 *
184 * @param[in] err Error to modify.
Michal Vaskof0537d82016-01-29 14:42:38 +0100185 * @param[in] session_id New value of \<session-id\>.
Michal Vasko1a38c862016-01-15 15:50:07 +0100186 * @return 0 on success, -1 on error.
187 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100188int nc_err_set_sid(struct nc_server_error *err, uint32_t session_id);
189
Michal Vasko1a38c862016-01-15 15:50:07 +0100190/**
191 * @brief Add a \<bad-attribute\> element to an error.
192 *
193 * @param[in] err Error to modify.
194 * @param[in] attr_name Value of the new \<bad-attribute\> element.
195 * @return 0 on success, -1 on error.
196 */
197int nc_err_add_bad_attr(struct nc_server_error *err, const char *attr_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100198
Michal Vasko1a38c862016-01-15 15:50:07 +0100199/**
200 * @brief Add a \<bad-element\> element to an error.
201 *
202 * @param[in] err Error to modify.
203 * @param[in] elem_name Value of the new \<bad-element\> element.
204 * @return 0 on success, -1 on error.
205 */
206int nc_err_add_bad_elem(struct nc_server_error *err, const char *elem_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100207
Michal Vasko1a38c862016-01-15 15:50:07 +0100208/**
209 * @brief Add a \<bad-namespace\> element to an error.
210 *
211 * @param[in] err Error to modify.
212 * @param[in] ns_name Value of the new \<bad-namespace\> element.
213 * @return 0 on success, -1 on error.
214 */
215int nc_err_add_bad_ns(struct nc_server_error *err, const char *ns_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100216
Michal Vasko1a38c862016-01-15 15:50:07 +0100217/**
218 * @brief Add an additional custom element to an error.
219 *
220 * @param[in] err Error to modify.
221 * @param[in] other New custom XML element.
222 * @return 0 on success, -1 on error.
223 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100224int nc_err_add_info_other(struct nc_server_error *err, struct lyxml_elem *other);
225
Michal Vasko1a38c862016-01-15 15:50:07 +0100226/**
227 * @brief Free a server rpc-reply object.
228 *
229 * @param[in] reply Server rpc-reply object to free.
230 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100231void nc_server_reply_free(struct nc_server_reply *reply);
232
Michal Vasko1a38c862016-01-15 15:50:07 +0100233/**
234 * @brief Free a server error structure.
235 *
236 * @param[in] err Error structure to free.
237 */
238void nc_err_free(struct nc_server_error *err);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100239
240#endif /* NC_MESSAGES_SERVER_H_ */