blob: bd71dbdeab18ef2848d531619b93672802a76c4a [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 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of the Company nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 */
22
23#ifndef NC_MESSAGES_SERVER_H_
24#define NC_MESSAGES_SERVER_H_
25
26#include <stdint.h>
27
28#include "netconf.h"
29
Michal Vasko495c9462016-01-15 11:27:43 +010030typedef enum NC_ERROR {
Michal Vasko7bcb48e2016-01-15 10:28:54 +010031 NC_ERR_UNKNOWN = 0,
32 NC_ERR_IN_USE,
33 NC_ERR_INVALID_VALUE,
34 NC_ERR_TOO_BIG,
35 NC_ERR_MISSING_ATTR,
36 NC_ERR_BAD_ATTR,
37 NC_ERR_UNKNOWN_ATTR,
38 NC_ERR_MISSING_ELEM,
39 NC_ERR_BAD_ELEM,
40 NC_ERR_UNKNOWN_ELEM,
41 NC_ERR_UNKNOWN_NS,
42 NC_ERR_ACCESS_DENIED,
43 NC_ERR_LOCK_DENIED,
44 NC_ERR_RES_DENIED,
45 NC_ERR_ROLLBACK_FAILED,
46 NC_ERR_DATA_EXISTS,
47 NC_ERR_DATA_MISSING,
48 NC_ERR_OP_NOT_SUPPORTED,
49 NC_ERR_OP_FAILED,
50 NC_ERR_MALFORMED_MSG
51} NC_ERR;
52
Michal Vasko495c9462016-01-15 11:27:43 +010053typedef enum NC_ERROR_TYPE {
Michal Vasko7bcb48e2016-01-15 10:28:54 +010054 NC_ERR_TYPE_UNKNOWN = 0,
55 NC_ERR_TYPE_TRAN,
56 NC_ERR_TYPE_RPC,
57 NC_ERR_TYPE_PROT,
58 NC_ERR_TYPE_APP
59} NC_ERR_TYPE;
60
61/**
Michal Vasko1a38c862016-01-15 15:50:07 +010062 * @brief NETCONF server rpc-reply object
Michal Vasko7bcb48e2016-01-15 10:28:54 +010063 */
64struct nc_server_reply;
65
Michal Vasko1a38c862016-01-15 15:50:07 +010066/**
67 * @brief NETCONF server error structure
68 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010069struct nc_server_error;
70
Michal Vasko1a38c862016-01-15 15:50:07 +010071/**
72 * @brief Create an OK rpc-reply object.
73 *
74 * @return rpc-reply object, NULL on error.
75 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010076struct nc_server_reply *nc_server_reply_ok(void);
77
Michal Vasko1a38c862016-01-15 15:50:07 +010078/**
79 * @brief Create a DATA rpc-reply object.
80 *
81 * @param[in] data Tree with the data.
82 * @param[in] paramtype Determines how the \p data parameter is treated.
83 * @return rpc-reply object, NULL on error.
84 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +010085struct nc_server_reply *nc_server_reply_data(struct lyd_node *data, NC_PARAMTYPE paramtype);
86
Michal Vasko1a38c862016-01-15 15:50:07 +010087/**
88 * @brief Create an ERROR rpc-reply object.
89 *
90 * @param[in] err Structure holding the error information. It will be freed with the returned object.
91 * @return rpc-reply object, NULL on error.
92 */
93struct nc_server_reply *nc_server_reply_err(struct nc_server_error *err);
Michal Vasko7bcb48e2016-01-15 10:28:54 +010094
Michal Vasko1a38c862016-01-15 15:50:07 +010095/**
96 * @brief Add another error to an ERROR rpc-reply object. It will be freed with the returned object.
97 *
98 * @param[in] reply ERROR reply to add to.
99 * @param[in] err Structure holding the additional error information.
100 * @return 0 on success, -1 on errror.
101 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100102int nc_server_reply_add_err(struct nc_server_reply *reply, struct nc_server_error *err);
103
Michal Vasko1a38c862016-01-15 15:50:07 +0100104/**
105 * @brief Create a server error structure. Its \<error-message\> is filled with
106 * a general description of the specific error.
107 *
108 * @param[in] tag \<error-tag\> of the server error. According to the tag, the
109 * specific additional parameters are required:
110 * - #NC_ERR_IN_USE
111 * - #NC_ERR_INVALID_VALUE
112 * - #NC_ERR_ACCESS_DENIED
113 * - #NC_ERR_ROLLBACK_FAILED
114 * - #NC_ERR_NOT_SUPPORTED
115 * - #NC_ERR_TOO_BIG
116 * - #NC_ERR_RES_DENIED
117 * - #NC_ERR_OP_FAILED
118 * - `NC_ERR_TYPE type;` - type (layer) of the error.
119 * - #NC_ERR_MISSING_ATTR
120 * - #NC_ERR_BAD_ATTR
121 * - #NC_ERR_UNKNOWN_ATTR
122 * - `NC_ERR_TYPE type;` - type (layer) of the error.
123 * - `const char *attr_name;` - error \<bad-attribute\> value.
124 * - `const char *elem_name;` - error \<bad-element\> value.
125 * - #NC_ERR_MISSING_ELEM
126 * - #NC_ERR_BAD_ELEM
127 * - #NC_ERR_UNKNOWN_ELEM
128 * - `NC_ERR_TYPE type;` - type (layer) of the error.
129 * - `const char *elem_name;` - error \<bad-element\> value.
130 * - #NC_ERR_UNKNOWN_NS
131 * - `NC_ERR_TYPE type;` - type (layer) of the error.
132 * - `const char *elem_name;` - error \<bad-element\> value.
133 * - `const char *nc_name;` - error \<bad-namespace\> value.
134 * - #NC_ERR_LOCK_DENIED
135 * - `uint32_t session_id;` - error \<session-id\> value.
136 * - #NC_ERR_DATA_EXISTS
137 * - #NC_ERR_DATA_MISSING
138 * - #NC_ERR_MALFORMED_MESSAGE
139 * - no additional arguments
140 * @return Server error structure, NULL on error.
141 */
142struct nc_server_error *nc_err(NC_ERR tag, ...);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100143
Michal Vasko1a38c862016-01-15 15:50:07 +0100144/**
145 * @brief Set the \<error-app-tag\> element of an error. Any previous value will be overwritten.
146 *
147 * @param[in] err Error to modify.
148 * @param[in] error_app_tag New value of \<error-app-tag\>.
149 * @return 0 on success, -1 on error.
150 */
151int nc_err_set_app_tag(struct nc_server_error *err, const char *error_app_tag);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100152
Michal Vasko1a38c862016-01-15 15:50:07 +0100153/**
154 * @brief Set the \<error-path\> element of an error. Any previous value will be overwritten.
155 *
156 * @param[in] err Error to modify.
157 * @param[in] error_path New value of \<error-path\>.
158 * @return 0 on success, -1 on error.
159 */
160int nc_err_set_path(struct nc_server_error *err, const char *error_path);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100161
Michal Vasko1a38c862016-01-15 15:50:07 +0100162/**
163 * @brief Set the \<error-message\> element of an error. Any previous value will be overwritten.
164 *
165 * @param[in] err Error to modify.
166 * @param[in] error_message New value of \<error-message\>.
167 * @param[in] lang Optional language of \p error-message.
168 * @return 0 on success, -1 on error.
169 */
170int nc_err_set_msg(struct nc_server_error *err, const char *error_message, const char *lang);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100171
Michal Vasko1a38c862016-01-15 15:50:07 +0100172/**
173 * @brief Set the \<session-id\> element of an error. Any previous value will be overwritten.
174 *
175 * @param[in] err Error to modify.
176 * @param[in] session-id New value of \<session-id\>.
177 * @return 0 on success, -1 on error.
178 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100179int nc_err_set_sid(struct nc_server_error *err, uint32_t session_id);
180
Michal Vasko1a38c862016-01-15 15:50:07 +0100181/**
182 * @brief Add a \<bad-attribute\> element to an error.
183 *
184 * @param[in] err Error to modify.
185 * @param[in] attr_name Value of the new \<bad-attribute\> element.
186 * @return 0 on success, -1 on error.
187 */
188int nc_err_add_bad_attr(struct nc_server_error *err, const char *attr_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100189
Michal Vasko1a38c862016-01-15 15:50:07 +0100190/**
191 * @brief Add a \<bad-element\> element to an error.
192 *
193 * @param[in] err Error to modify.
194 * @param[in] elem_name Value of the new \<bad-element\> element.
195 * @return 0 on success, -1 on error.
196 */
197int nc_err_add_bad_elem(struct nc_server_error *err, const char *elem_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100198
Michal Vasko1a38c862016-01-15 15:50:07 +0100199/**
200 * @brief Add a \<bad-namespace\> element to an error.
201 *
202 * @param[in] err Error to modify.
203 * @param[in] ns_name Value of the new \<bad-namespace\> element.
204 * @return 0 on success, -1 on error.
205 */
206int nc_err_add_bad_ns(struct nc_server_error *err, const char *ns_name);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100207
Michal Vasko1a38c862016-01-15 15:50:07 +0100208/**
209 * @brief Add an additional custom element to an error.
210 *
211 * @param[in] err Error to modify.
212 * @param[in] other New custom XML element.
213 * @return 0 on success, -1 on error.
214 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100215int nc_err_add_info_other(struct nc_server_error *err, struct lyxml_elem *other);
216
Michal Vasko1a38c862016-01-15 15:50:07 +0100217/**
218 * @brief Free a server rpc-reply object.
219 *
220 * @param[in] reply Server rpc-reply object to free.
221 */
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100222void nc_server_reply_free(struct nc_server_reply *reply);
223
Michal Vasko1a38c862016-01-15 15:50:07 +0100224/**
225 * @brief Free a server error structure.
226 *
227 * @param[in] err Error structure to free.
228 */
229void nc_err_free(struct nc_server_error *err);
Michal Vasko7bcb48e2016-01-15 10:28:54 +0100230
231#endif /* NC_MESSAGES_SERVER_H_ */