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. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 81 | * @param[in] paramtype Determines how the \p data parameter is treated. |
| 82 | * @return rpc-reply object, NULL on error. |
| 83 | */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 84 | struct nc_server_reply *nc_server_reply_data(struct lyd_node *data, NC_PARAMTYPE paramtype); |
| 85 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 86 | /** |
| 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 | */ |
| 92 | 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] | 93 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 94 | /** |
| 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 Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 101 | int nc_server_reply_add_err(struct nc_server_reply *reply, struct nc_server_error *err); |
| 102 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 103 | /** |
| 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 Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 113 | * - #NC_ERR_OP_NOT_SUPPORTED |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 114 | * - #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 Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 137 | * - #NC_ERR_MALFORMED_MSG |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 138 | * - no additional arguments |
| 139 | * @return Server error structure, NULL on error. |
| 140 | */ |
| 141 | struct nc_server_error *nc_err(NC_ERR tag, ...); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 142 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 143 | /** |
Radek Krejci | 877e182 | 2016-04-06 16:37:43 +0200 | [diff] [blame] | 144 | * @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 Vasko | 7d9b495 | 2016-04-08 10:44:26 +0200 | [diff] [blame] | 151 | struct nc_server_error *nc_err_libyang(void); |
Radek Krejci | 877e182 | 2016-04-06 16:37:43 +0200 | [diff] [blame] | 152 | |
| 153 | /** |
Michal Vasko | c912111 | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 154 | * @brief Get the \<error-type\> of a server error. |
| 155 | * |
| 156 | * @param[in] err Server error to read from. |
| 157 | * @return Server error type, 0 on error. |
| 158 | */ |
| 159 | NC_ERR_TYPE nc_err_get_type(struct nc_server_error *err); |
| 160 | |
| 161 | /** |
| 162 | * @brief Get the \<error-tag\> of a server error. |
| 163 | * |
| 164 | * @param[in] err Server error to read from. |
| 165 | * @return Server error tag, 0 on error. |
| 166 | */ |
| 167 | NC_ERR nc_err_get_tag(struct nc_server_error *err); |
| 168 | |
| 169 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 170 | * @brief Set the \<error-app-tag\> element of an error. Any previous value will be overwritten. |
| 171 | * |
| 172 | * @param[in] err Error to modify. |
| 173 | * @param[in] error_app_tag New value of \<error-app-tag\>. |
| 174 | * @return 0 on success, -1 on error. |
| 175 | */ |
| 176 | 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] | 177 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 178 | /** |
Michal Vasko | c912111 | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 179 | * @brief Get the \<error-app-tag\> of a server error. |
| 180 | * |
| 181 | * @param[in] err Server error to read from. |
| 182 | * @return Server error app tag, NULL on error. |
| 183 | */ |
| 184 | const char *nc_err_get_app_tag(struct nc_server_error *err); |
| 185 | |
| 186 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 187 | * @brief Set the \<error-path\> element of an error. Any previous value will be overwritten. |
| 188 | * |
| 189 | * @param[in] err Error to modify. |
| 190 | * @param[in] error_path New value of \<error-path\>. |
| 191 | * @return 0 on success, -1 on error. |
| 192 | */ |
| 193 | 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] | 194 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 195 | /** |
Michal Vasko | c912111 | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 196 | * @brief Get the \<error-path\> of a server error. |
| 197 | * |
| 198 | * @param[in] err Server error to read from. |
| 199 | * @return Server error path, NULL on error. |
| 200 | */ |
| 201 | const char *nc_err_get_path(struct nc_server_error *err); |
| 202 | |
| 203 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 204 | * @brief Set the \<error-message\> element of an error. Any previous value will be overwritten. |
| 205 | * |
| 206 | * @param[in] err Error to modify. |
| 207 | * @param[in] error_message New value of \<error-message\>. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 208 | * @param[in] lang Optional language of \p error_message. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 209 | * @return 0 on success, -1 on error. |
| 210 | */ |
| 211 | 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] | 212 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 213 | /** |
Michal Vasko | c912111 | 2016-05-04 10:45:28 +0200 | [diff] [blame] | 214 | * @brief Get the \<error-message\> of a server error. |
| 215 | * |
| 216 | * @param[in] err Server error to read from. |
| 217 | * @return Server error message, NULL on error. |
| 218 | */ |
| 219 | const char *nc_err_get_msg(struct nc_server_error *err); |
| 220 | |
| 221 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 222 | * @brief Set the \<session-id\> element of an error. Any previous value will be overwritten. |
| 223 | * |
| 224 | * @param[in] err Error to modify. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame] | 225 | * @param[in] session_id New value of \<session-id\>. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 226 | * @return 0 on success, -1 on error. |
| 227 | */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 228 | int nc_err_set_sid(struct nc_server_error *err, uint32_t session_id); |
| 229 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 230 | /** |
| 231 | * @brief Add a \<bad-attribute\> element to an error. |
| 232 | * |
| 233 | * @param[in] err Error to modify. |
| 234 | * @param[in] attr_name Value of the new \<bad-attribute\> element. |
| 235 | * @return 0 on success, -1 on error. |
| 236 | */ |
| 237 | 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] | 238 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 239 | /** |
| 240 | * @brief Add a \<bad-element\> element to an error. |
| 241 | * |
| 242 | * @param[in] err Error to modify. |
| 243 | * @param[in] elem_name Value of the new \<bad-element\> element. |
| 244 | * @return 0 on success, -1 on error. |
| 245 | */ |
| 246 | 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] | 247 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 248 | /** |
| 249 | * @brief Add a \<bad-namespace\> element to an error. |
| 250 | * |
| 251 | * @param[in] err Error to modify. |
| 252 | * @param[in] ns_name Value of the new \<bad-namespace\> element. |
| 253 | * @return 0 on success, -1 on error. |
| 254 | */ |
| 255 | 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] | 256 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 257 | /** |
| 258 | * @brief Add an additional custom element to an error. |
| 259 | * |
| 260 | * @param[in] err Error to modify. |
| 261 | * @param[in] other New custom XML element. |
| 262 | * @return 0 on success, -1 on error. |
| 263 | */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 264 | int nc_err_add_info_other(struct nc_server_error *err, struct lyxml_elem *other); |
| 265 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 266 | /** |
| 267 | * @brief Free a server rpc-reply object. |
| 268 | * |
| 269 | * @param[in] reply Server rpc-reply object to free. |
| 270 | */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 271 | void nc_server_reply_free(struct nc_server_reply *reply); |
| 272 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 273 | /** |
| 274 | * @brief Free a server error structure. |
| 275 | * |
| 276 | * @param[in] err Error structure to free. |
| 277 | */ |
| 278 | void nc_err_free(struct nc_server_error *err); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 279 | |
| 280 | #endif /* NC_MESSAGES_SERVER_H_ */ |