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 | * |
| 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 Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame^] | 30 | /** |
| 31 | * @brief Enumeration of NETCONF errors |
| 32 | */ |
Michal Vasko | 495c946 | 2016-01-15 11:27:43 +0100 | [diff] [blame] | 33 | typedef enum NC_ERROR { |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame^] | 34 | NC_ERR_UNKNOWN = 0, /**< unknown error */ |
| 35 | NC_ERR_IN_USE, /**< in-use error */ |
| 36 | NC_ERR_INVALID_VALUE, /**< invalid-value error */ |
| 37 | NC_ERR_TOO_BIG, /**< too-big error */ |
| 38 | NC_ERR_MISSING_ATTR, /**< missing-attribute error */ |
| 39 | NC_ERR_BAD_ATTR, /**< bad-attribute error */ |
| 40 | NC_ERR_UNKNOWN_ATTR, /**< unknown-attribute error */ |
| 41 | NC_ERR_MISSING_ELEM, /**< missing-element error */ |
| 42 | NC_ERR_BAD_ELEM, /**< bad-element error */ |
| 43 | NC_ERR_UNKNOWN_ELEM, /**< unknown-element error */ |
| 44 | NC_ERR_UNKNOWN_NS, /**< unknown-namespace error */ |
| 45 | NC_ERR_ACCESS_DENIED, /**< access-denied error */ |
| 46 | NC_ERR_LOCK_DENIED, /**< lock-denied error */ |
| 47 | NC_ERR_RES_DENIED, /**< resource-denied error */ |
| 48 | NC_ERR_ROLLBACK_FAILED, /**< rollback-failed error */ |
| 49 | NC_ERR_DATA_EXISTS, /**< data-exists error */ |
| 50 | NC_ERR_DATA_MISSING, /**< data-missing error */ |
| 51 | NC_ERR_OP_NOT_SUPPORTED, /**< operation-not-supported error */ |
| 52 | NC_ERR_OP_FAILED, /**< operation-failed error */ |
| 53 | NC_ERR_MALFORMED_MSG /**< malformed-message error */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 54 | } NC_ERR; |
| 55 | |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame^] | 56 | /** |
| 57 | * @brief Enumeration of NETCONF error type (layer) |
| 58 | */ |
Michal Vasko | 495c946 | 2016-01-15 11:27:43 +0100 | [diff] [blame] | 59 | typedef enum NC_ERROR_TYPE { |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame^] | 60 | NC_ERR_TYPE_UNKNOWN = 0, /**< unknown layer */ |
| 61 | NC_ERR_TYPE_TRAN, /**< transport layer */ |
| 62 | NC_ERR_TYPE_RPC, /**< RPC layer */ |
| 63 | NC_ERR_TYPE_PROT, /**< protocol layer */ |
| 64 | NC_ERR_TYPE_APP /**< application layer */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 65 | } NC_ERR_TYPE; |
| 66 | |
| 67 | /** |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 68 | * @brief NETCONF server rpc-reply object |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 69 | */ |
| 70 | struct nc_server_reply; |
| 71 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 72 | /** |
| 73 | * @brief NETCONF server error structure |
| 74 | */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 75 | struct nc_server_error; |
| 76 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 77 | /** |
| 78 | * @brief Create an OK rpc-reply object. |
| 79 | * |
| 80 | * @return rpc-reply object, NULL on error. |
| 81 | */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 82 | struct nc_server_reply *nc_server_reply_ok(void); |
| 83 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 84 | /** |
| 85 | * @brief Create a DATA rpc-reply object. |
| 86 | * |
| 87 | * @param[in] data Tree with the data. |
| 88 | * @param[in] paramtype Determines how the \p data parameter is treated. |
| 89 | * @return rpc-reply object, NULL on error. |
| 90 | */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 91 | struct nc_server_reply *nc_server_reply_data(struct lyd_node *data, NC_PARAMTYPE paramtype); |
| 92 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 93 | /** |
| 94 | * @brief Create an ERROR rpc-reply object. |
| 95 | * |
| 96 | * @param[in] err Structure holding the error information. It will be freed with the returned object. |
| 97 | * @return rpc-reply object, NULL on error. |
| 98 | */ |
| 99 | 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] | 100 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 101 | /** |
| 102 | * @brief Add another error to an ERROR rpc-reply object. It will be freed with the returned object. |
| 103 | * |
| 104 | * @param[in] reply ERROR reply to add to. |
| 105 | * @param[in] err Structure holding the additional error information. |
| 106 | * @return 0 on success, -1 on errror. |
| 107 | */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 108 | int nc_server_reply_add_err(struct nc_server_reply *reply, struct nc_server_error *err); |
| 109 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 110 | /** |
| 111 | * @brief Create a server error structure. Its \<error-message\> is filled with |
| 112 | * a general description of the specific error. |
| 113 | * |
| 114 | * @param[in] tag \<error-tag\> of the server error. According to the tag, the |
| 115 | * specific additional parameters are required: |
| 116 | * - #NC_ERR_IN_USE |
| 117 | * - #NC_ERR_INVALID_VALUE |
| 118 | * - #NC_ERR_ACCESS_DENIED |
| 119 | * - #NC_ERR_ROLLBACK_FAILED |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame^] | 120 | * - #NC_ERR_OP_NOT_SUPPORTED |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 121 | * - #NC_ERR_TOO_BIG |
| 122 | * - #NC_ERR_RES_DENIED |
| 123 | * - #NC_ERR_OP_FAILED |
| 124 | * - `NC_ERR_TYPE type;` - type (layer) of the error. |
| 125 | * - #NC_ERR_MISSING_ATTR |
| 126 | * - #NC_ERR_BAD_ATTR |
| 127 | * - #NC_ERR_UNKNOWN_ATTR |
| 128 | * - `NC_ERR_TYPE type;` - type (layer) of the error. |
| 129 | * - `const char *attr_name;` - error \<bad-attribute\> value. |
| 130 | * - `const char *elem_name;` - error \<bad-element\> value. |
| 131 | * - #NC_ERR_MISSING_ELEM |
| 132 | * - #NC_ERR_BAD_ELEM |
| 133 | * - #NC_ERR_UNKNOWN_ELEM |
| 134 | * - `NC_ERR_TYPE type;` - type (layer) of the error. |
| 135 | * - `const char *elem_name;` - error \<bad-element\> value. |
| 136 | * - #NC_ERR_UNKNOWN_NS |
| 137 | * - `NC_ERR_TYPE type;` - type (layer) of the error. |
| 138 | * - `const char *elem_name;` - error \<bad-element\> value. |
| 139 | * - `const char *nc_name;` - error \<bad-namespace\> value. |
| 140 | * - #NC_ERR_LOCK_DENIED |
| 141 | * - `uint32_t session_id;` - error \<session-id\> value. |
| 142 | * - #NC_ERR_DATA_EXISTS |
| 143 | * - #NC_ERR_DATA_MISSING |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame^] | 144 | * - #NC_ERR_MALFORMED_MSG |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 145 | * - no additional arguments |
| 146 | * @return Server error structure, NULL on error. |
| 147 | */ |
| 148 | struct nc_server_error *nc_err(NC_ERR tag, ...); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 149 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 150 | /** |
| 151 | * @brief Set the \<error-app-tag\> element of an error. Any previous value will be overwritten. |
| 152 | * |
| 153 | * @param[in] err Error to modify. |
| 154 | * @param[in] error_app_tag New value of \<error-app-tag\>. |
| 155 | * @return 0 on success, -1 on error. |
| 156 | */ |
| 157 | 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] | 158 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 159 | /** |
| 160 | * @brief Set the \<error-path\> element of an error. Any previous value will be overwritten. |
| 161 | * |
| 162 | * @param[in] err Error to modify. |
| 163 | * @param[in] error_path New value of \<error-path\>. |
| 164 | * @return 0 on success, -1 on error. |
| 165 | */ |
| 166 | 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] | 167 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 168 | /** |
| 169 | * @brief Set the \<error-message\> element of an error. Any previous value will be overwritten. |
| 170 | * |
| 171 | * @param[in] err Error to modify. |
| 172 | * @param[in] error_message New value of \<error-message\>. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame^] | 173 | * @param[in] lang Optional language of \p error_message. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 174 | * @return 0 on success, -1 on error. |
| 175 | */ |
| 176 | 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] | 177 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 178 | /** |
| 179 | * @brief Set the \<session-id\> element of an error. Any previous value will be overwritten. |
| 180 | * |
| 181 | * @param[in] err Error to modify. |
Michal Vasko | f0537d8 | 2016-01-29 14:42:38 +0100 | [diff] [blame^] | 182 | * @param[in] session_id New value of \<session-id\>. |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 183 | * @return 0 on success, -1 on error. |
| 184 | */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 185 | int nc_err_set_sid(struct nc_server_error *err, uint32_t session_id); |
| 186 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 187 | /** |
| 188 | * @brief Add a \<bad-attribute\> element to an error. |
| 189 | * |
| 190 | * @param[in] err Error to modify. |
| 191 | * @param[in] attr_name Value of the new \<bad-attribute\> element. |
| 192 | * @return 0 on success, -1 on error. |
| 193 | */ |
| 194 | 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] | 195 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 196 | /** |
| 197 | * @brief Add a \<bad-element\> element to an error. |
| 198 | * |
| 199 | * @param[in] err Error to modify. |
| 200 | * @param[in] elem_name Value of the new \<bad-element\> element. |
| 201 | * @return 0 on success, -1 on error. |
| 202 | */ |
| 203 | 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] | 204 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 205 | /** |
| 206 | * @brief Add a \<bad-namespace\> element to an error. |
| 207 | * |
| 208 | * @param[in] err Error to modify. |
| 209 | * @param[in] ns_name Value of the new \<bad-namespace\> element. |
| 210 | * @return 0 on success, -1 on error. |
| 211 | */ |
| 212 | 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] | 213 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 214 | /** |
| 215 | * @brief Add an additional custom element to an error. |
| 216 | * |
| 217 | * @param[in] err Error to modify. |
| 218 | * @param[in] other New custom XML element. |
| 219 | * @return 0 on success, -1 on error. |
| 220 | */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 221 | int nc_err_add_info_other(struct nc_server_error *err, struct lyxml_elem *other); |
| 222 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 223 | /** |
| 224 | * @brief Free a server rpc-reply object. |
| 225 | * |
| 226 | * @param[in] reply Server rpc-reply object to free. |
| 227 | */ |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 228 | void nc_server_reply_free(struct nc_server_reply *reply); |
| 229 | |
Michal Vasko | 1a38c86 | 2016-01-15 15:50:07 +0100 | [diff] [blame] | 230 | /** |
| 231 | * @brief Free a server error structure. |
| 232 | * |
| 233 | * @param[in] err Error structure to free. |
| 234 | */ |
| 235 | void nc_err_free(struct nc_server_error *err); |
Michal Vasko | 7bcb48e | 2016-01-15 10:28:54 +0100 | [diff] [blame] | 236 | |
| 237 | #endif /* NC_MESSAGES_SERVER_H_ */ |