Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 1 | /** |
| 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 Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 8 | * 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 Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 13 | */ |
| 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 Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 22 | /** |
| 23 | * @brief Enumeration of NETCONF errors |
| 24 | */ |
Michal Vasko | 495c946 | 2016-01-15 11:27:43 +0100 | [diff] [blame] | 25 | typedef enum NC_ERROR { |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 26 | 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 Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 46 | } NC_ERR; |
| 47 | |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 48 | /** |
| 49 | * @brief Enumeration of NETCONF error type (layer) |
| 50 | */ |
Michal Vasko | 495c946 | 2016-01-15 11:27:43 +0100 | [diff] [blame] | 51 | typedef enum NC_ERROR_TYPE { |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 52 | 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 Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 57 | } NC_ERR_TYPE; |
| 58 | |
| 59 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 60 | * @brief NETCONF server rpc-reply object |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 61 | */ |
| 62 | struct nc_server_reply; |
| 63 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 64 | /** |
| 65 | * @brief NETCONF server error structure |
| 66 | */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 67 | struct nc_server_error; |
| 68 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 69 | /** |
| 70 | * @brief Create an OK rpc-reply object. |
| 71 | * |
| 72 | * @return rpc-reply object, NULL on error. |
| 73 | */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 74 | struct nc_server_reply *nc_server_reply_ok(void); |
| 75 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 76 | /** |
| 77 | * @brief Create a DATA rpc-reply object. |
| 78 | * |
Michal Vasko | b08743b | 2016-04-13 14:23:49 +0200 | [diff] [blame] | 79 | * @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. |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 81 | * @param[in] wd with-default mode if applicable |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 82 | * @param[in] paramtype Determines how the \p data parameter is treated. |
| 83 | * @return rpc-reply object, NULL on error. |
| 84 | */ |
Radek Krejci | 36dfdb3 | 2016-09-01 16:56:35 +0200 | [diff] [blame] | 85 | struct nc_server_reply *nc_server_reply_data(struct lyd_node *data, NC_WD_MODE wd, NC_PARAMTYPE paramtype); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 86 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 87 | /** |
| 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 | */ |
| 93 | struct nc_server_reply *nc_server_reply_err(struct nc_server_error *err); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 94 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 95 | /** |
| 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 Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 102 | int nc_server_reply_add_err(struct nc_server_reply *reply, struct nc_server_error *err); |
| 103 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 104 | /** |
| 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 |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 114 | * - #NC_ERR_OP_NOT_SUPPORTED |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 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 |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 138 | * - #NC_ERR_MALFORMED_MSG |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 139 | * - no additional arguments |
| 140 | * @return Server error structure, NULL on error. |
| 141 | */ |
| 142 | struct nc_server_error *nc_err(NC_ERR tag, ...); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 143 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 144 | /** |
Radek Krejci | 877e182 | 2016-04-06 16:37:43 +0200 | [diff] [blame] | 145 | * @brief Create a server error structure based on libyang error. |
| 146 | * |
| 147 | * The function should be used immediately when a libyang function fails to generate |
| 148 | * NETCONF error structure based on internal libyang error information (ly_errno, ly_errmsg, ...) |
| 149 | * |
| 150 | * @return Server error structure, NULL on error. |
| 151 | */ |
Michal Vasko | 7d9b495 | 2016-04-08 10:44:26 +0200 | [diff] [blame] | 152 | struct nc_server_error *nc_err_libyang(void); |
Radek Krejci | 877e182 | 2016-04-06 16:37:43 +0200 | [diff] [blame] | 153 | |
| 154 | /** |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 155 | * @brief Get the \<error-type\> of a server error. |
| 156 | * |
| 157 | * @param[in] err Server error to read from. |
| 158 | * @return Server error type, 0 on error. |
| 159 | */ |
| 160 | NC_ERR_TYPE nc_err_get_type(struct nc_server_error *err); |
| 161 | |
| 162 | /** |
| 163 | * @brief Get the \<error-tag\> of a server error. |
| 164 | * |
| 165 | * @param[in] err Server error to read from. |
| 166 | * @return Server error tag, 0 on error. |
| 167 | */ |
| 168 | NC_ERR nc_err_get_tag(struct nc_server_error *err); |
| 169 | |
| 170 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 171 | * @brief Set the \<error-app-tag\> element of an error. Any previous value will be overwritten. |
| 172 | * |
| 173 | * @param[in] err Error to modify. |
| 174 | * @param[in] error_app_tag New value of \<error-app-tag\>. |
| 175 | * @return 0 on success, -1 on error. |
| 176 | */ |
| 177 | int nc_err_set_app_tag(struct nc_server_error *err, const char *error_app_tag); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 178 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 179 | /** |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 180 | * @brief Get the \<error-app-tag\> of a server error. |
| 181 | * |
| 182 | * @param[in] err Server error to read from. |
| 183 | * @return Server error app tag, NULL on error. |
| 184 | */ |
| 185 | const char *nc_err_get_app_tag(struct nc_server_error *err); |
| 186 | |
| 187 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 188 | * @brief Set the \<error-path\> element of an error. Any previous value will be overwritten. |
| 189 | * |
| 190 | * @param[in] err Error to modify. |
| 191 | * @param[in] error_path New value of \<error-path\>. |
| 192 | * @return 0 on success, -1 on error. |
| 193 | */ |
| 194 | int nc_err_set_path(struct nc_server_error *err, const char *error_path); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 195 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 196 | /** |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 197 | * @brief Get the \<error-path\> of a server error. |
| 198 | * |
| 199 | * @param[in] err Server error to read from. |
| 200 | * @return Server error path, NULL on error. |
| 201 | */ |
| 202 | const char *nc_err_get_path(struct nc_server_error *err); |
| 203 | |
| 204 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 205 | * @brief Set the \<error-message\> element of an error. Any previous value will be overwritten. |
| 206 | * |
| 207 | * @param[in] err Error to modify. |
| 208 | * @param[in] error_message New value of \<error-message\>. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 209 | * @param[in] lang Optional language of \p error_message. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 210 | * @return 0 on success, -1 on error. |
| 211 | */ |
| 212 | int nc_err_set_msg(struct nc_server_error *err, const char *error_message, const char *lang); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 213 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 214 | /** |
Michal Vasko | 8f3198f | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 215 | * @brief Get the \<error-message\> of a server error. |
| 216 | * |
| 217 | * @param[in] err Server error to read from. |
| 218 | * @return Server error message, NULL on error. |
| 219 | */ |
| 220 | const char *nc_err_get_msg(struct nc_server_error *err); |
| 221 | |
| 222 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 223 | * @brief Set the \<session-id\> element of an error. Any previous value will be overwritten. |
| 224 | * |
| 225 | * @param[in] err Error to modify. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 226 | * @param[in] session_id New value of \<session-id\>. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 227 | * @return 0 on success, -1 on error. |
| 228 | */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 229 | int nc_err_set_sid(struct nc_server_error *err, uint32_t session_id); |
| 230 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 231 | /** |
| 232 | * @brief Add a \<bad-attribute\> element to an error. |
| 233 | * |
| 234 | * @param[in] err Error to modify. |
| 235 | * @param[in] attr_name Value of the new \<bad-attribute\> element. |
| 236 | * @return 0 on success, -1 on error. |
| 237 | */ |
| 238 | int nc_err_add_bad_attr(struct nc_server_error *err, const char *attr_name); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 239 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 240 | /** |
| 241 | * @brief Add a \<bad-element\> element to an error. |
| 242 | * |
| 243 | * @param[in] err Error to modify. |
| 244 | * @param[in] elem_name Value of the new \<bad-element\> element. |
| 245 | * @return 0 on success, -1 on error. |
| 246 | */ |
| 247 | int nc_err_add_bad_elem(struct nc_server_error *err, const char *elem_name); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 248 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 249 | /** |
| 250 | * @brief Add a \<bad-namespace\> element to an error. |
| 251 | * |
| 252 | * @param[in] err Error to modify. |
| 253 | * @param[in] ns_name Value of the new \<bad-namespace\> element. |
| 254 | * @return 0 on success, -1 on error. |
| 255 | */ |
| 256 | int nc_err_add_bad_ns(struct nc_server_error *err, const char *ns_name); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 257 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 258 | /** |
| 259 | * @brief Add an additional custom element to an error. |
| 260 | * |
| 261 | * @param[in] err Error to modify. |
| 262 | * @param[in] other New custom XML element. |
| 263 | * @return 0 on success, -1 on error. |
| 264 | */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 265 | int nc_err_add_info_other(struct nc_server_error *err, struct lyxml_elem *other); |
| 266 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 267 | /** |
| 268 | * @brief Free a server rpc-reply object. |
| 269 | * |
| 270 | * @param[in] reply Server rpc-reply object to free. |
| 271 | */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 272 | void nc_server_reply_free(struct nc_server_reply *reply); |
| 273 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 274 | /** |
| 275 | * @brief Free a server error structure. |
| 276 | * |
| 277 | * @param[in] err Error structure to free. |
| 278 | */ |
| 279 | void nc_err_free(struct nc_server_error *err); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 280 | |
| 281 | #endif /* NC_MESSAGES_SERVER_H_ */ |