Skip to content

Commit

Permalink
netconf UPDATE updated sysrepo netconf err format
Browse files Browse the repository at this point in the history
  • Loading branch information
michalvasko committed Mar 1, 2024
1 parent c719bff commit 025e15f
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 116 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ set(LIBNETCONF2_DEP_SOVERSION 4.1.7)
set(LIBNETCONF2_DEP_SOVERSION_MAJOR 4)

# sysrepo required version
set(SYSREPO_DEP_VERSION 2.2.148)
set(SYSREPO_DEP_SOVERSION 7.22.0)
set(SYSREPO_DEP_VERSION 2.2.159)
set(SYSREPO_DEP_SOVERSION 7.24.0)
set(SYSREPO_DEP_SOVERSION_MAJOR 7)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -std=c99")
Expand Down
20 changes: 10 additions & 10 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ np_ignore_rpc(sr_session_ctx_t *ev_sess, sr_event_t event, int *rc)

if (sr_session_get_orig_name(ev_sess) && strcmp(sr_session_get_orig_name(ev_sess), "netopeer2")) {
/* forbidden */
sr_session_set_error_message(ev_sess, "Non-NETCONF originating RPC will not be executed.");
*rc = SR_ERR_UNSUPPORTED;
sr_session_set_error(ev_sess, NULL, *rc, "Non-NETCONF originating RPC will not be executed.");
return 1;
}

Expand Down Expand Up @@ -621,7 +621,7 @@ url_get(const char *url, sr_session_ctx_t *ev_sess)

if (!np2srv.url_protocols) {
ERR("No URL protocols enabled.");
sr_session_set_error_message(ev_sess, "No URL protocols enabled.");
sr_session_set_error(ev_sess, NULL, SR_ERR_UNSUPPORTED, "No URL protocols enabled.");
return NULL;
}

Expand All @@ -640,7 +640,7 @@ url_get(const char *url, sr_session_ctx_t *ev_sess)
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
ERR("Failed to download data (curl: %s).", curl_buffer);
sr_session_set_error_message(ev_sess, curl_buffer);
sr_session_set_error(ev_sess, NULL, SR_ERR_OPERATION_FAILED, curl_buffer);
goto cleanup;
}

Expand Down Expand Up @@ -674,20 +674,20 @@ op_parse_url(const char *url, int validate, int *rc, sr_session_ctx_t *ev_sess)
/* load the whole config element */
if (lyd_parse_data_mem(ly_ctx, url_data, LYD_XML, LYD_PARSE_OPAQ | LYD_PARSE_ONLY | LYD_PARSE_NO_STATE, 0, &config)) {
*rc = SR_ERR_LY;
sr_session_set_error_message(ev_sess, ly_err_last(ly_ctx)->msg);
sr_session_set_error(ev_sess, NULL, *rc, ly_err_last(ly_ctx)->msg);
goto cleanup;
}

if (!config || config->schema) {
*rc = SR_ERR_UNSUPPORTED;
sr_session_set_error_message(ev_sess, "Missing top-level \"config\" element in URL data.");
sr_session_set_error(ev_sess, NULL, *rc, "Missing top-level \"config\" element in URL data.");
goto cleanup;
}

opaq = (struct lyd_node_opaq *)config;
if (strcmp(opaq->name.name, "config") || strcmp(opaq->name.module_ns, "urn:ietf:params:xml:ns:netconf:base:1.0")) {
*rc = SR_ERR_UNSUPPORTED;
sr_session_set_error_message(ev_sess, "Invalid top-level element in URL data, expected \"config\" with "
sr_session_set_error(ev_sess, NULL, *rc, "Invalid top-level element in URL data, expected \"config\" with "
"namespace \"urn:ietf:params:xml:ns:netconf:base:1.0\".");
goto cleanup;
}
Expand All @@ -700,7 +700,7 @@ op_parse_url(const char *url, int validate, int *rc, sr_session_ctx_t *ev_sess)
/* separate validation if requested */
if (lyd_validate_all(&data, NULL, LYD_VALIDATE_NO_STATE, NULL)) {
*rc = SR_ERR_LY;
sr_session_set_error_message(ev_sess, ly_err_last(ly_ctx)->msg);
sr_session_set_error(ev_sess, NULL, *rc, ly_err_last(ly_ctx)->msg);
goto cleanup;
}
}
Expand Down Expand Up @@ -732,7 +732,7 @@ op_export_url(const char *url, struct lyd_node *data, uint32_t print_options, in
/* print the config as expected by the other end */
if (lyd_new_opaq2(NULL, ly_ctx, "config", NULL, NULL, "urn:ietf:params:xml:ns:netconf:base:1.0", &config)) {
*rc = SR_ERR_LY;
sr_session_set_error_message(ev_sess, ly_err_last(ly_ctx)->msg);
sr_session_set_error(ev_sess, NULL, *rc, ly_err_last(ly_ctx)->msg);
ret = -1;
goto cleanup;
}
Expand Down Expand Up @@ -767,7 +767,7 @@ op_export_url(const char *url, struct lyd_node *data, uint32_t print_options, in
if (res != CURLE_OK) {
ERR("Failed to upload data (curl: %s).", curl_buffer);
*rc = SR_ERR_SYS;
sr_session_set_error_message(ev_sess, curl_buffer);
sr_session_set_error(ev_sess, NULL, *rc, curl_buffer);
ret = -1;
goto curl_cleanup;
}
Expand Down Expand Up @@ -829,7 +829,7 @@ op_parse_config(struct lyd_node_any *config, uint32_t parse_options, int *rc, sr
}
if (lyrc) {
*rc = SR_ERR_LY;
sr_session_set_error_message(ev_sess, ly_err_last(ly_ctx)->msg);
sr_session_set_error(ev_sess, NULL, *rc, ly_err_last(ly_ctx)->msg);
}

return root;
Expand Down
2 changes: 1 addition & 1 deletion src/err_netconf.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ np_err_sr2nc_edit(sr_session_ctx_t *ev_sess, const sr_session_ctx_t *err_sess)
return;

mem_error:
sr_session_set_error_message(ev_sess, "Memory allocation failed.");
sr_session_set_error(ev_sess, NULL, SR_ERR_NO_MEMORY, "Memory allocation failed.");

free(path);
free(str);
Expand Down
135 changes: 63 additions & 72 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,96 +198,86 @@ np2srv_err_nc(sr_error_info_err_t *err)
struct lyd_node *e_first = NULL, *e, *err_info;
const struct ly_ctx *ly_ctx;
const char *err_type, *err_tag, *err_app_tag, *err_path, *err_msg, **err_info_elem = NULL, **err_info_val = NULL, *ns;
uint32_t err_info_count, i, j;
int r;
uint32_t err_info_count, j;

/* only dictionary used, no need to keep locked */
ly_ctx = sr_acquire_context(np2srv.sr_conn);
sr_release_context(np2srv.sr_conn);

i = 0;
while (1) {
/* read the next error */
r = sr_err_get_netconf_error_idx(err, i, &err_type, &err_tag, &err_app_tag, &err_path, &err_msg, &err_info_elem,
&err_info_val, &err_info_count);
if (r == SR_ERR_NOT_FOUND) {
/* no more errors */
break;
} else if (r) {
goto error;
}
/* read the error */
if (sr_err_get_netconf_error(err, &err_type, &err_tag, &err_app_tag, &err_path, &err_msg, &err_info_elem,
&err_info_val, &err_info_count)) {
goto error;
}

/* rpc-error */
if (lyd_new_opaq2(NULL, ly_ctx, "rpc-error", NULL, NULL, NC_NS_BASE, &e)) {
goto error;
}
/* rpc-error */
if (lyd_new_opaq2(NULL, ly_ctx, "rpc-error", NULL, NULL, NC_NS_BASE, &e)) {
goto error;
}

/* error-type */
if (lyd_new_opaq2(e, NULL, "error-type", err_type, NULL, NC_NS_BASE, NULL)) {
goto error;
}
/* error-type */
if (lyd_new_opaq2(e, NULL, "error-type", err_type, NULL, NC_NS_BASE, NULL)) {
goto error;
}

/* error-tag */
if (lyd_new_opaq2(e, NULL, "error-tag", err_tag, NULL, NC_NS_BASE, NULL)) {
goto error;
}

/* error-tag */
if (lyd_new_opaq2(e, NULL, "error-tag", err_tag, NULL, NC_NS_BASE, NULL)) {
/* error-severity */
if (lyd_new_opaq2(e, NULL, "error-severity", "error", NULL, NC_NS_BASE, NULL)) {
goto error;
}

if (err_app_tag) {
/* error-app-tag */
if (nc_err_set_app_tag(e, err_app_tag)) {
goto error;
}
}

/* error-severity */
if (lyd_new_opaq2(e, NULL, "error-severity", "error", NULL, NC_NS_BASE, NULL)) {
if (err_path) {
/* error-path */
if (nc_err_set_path(e, err_path)) {
goto error;
}
}

if (err_app_tag) {
/* error-app-tag */
if (nc_err_set_app_tag(e, err_app_tag)) {
goto error;
}
}
/* error-message */
if (nc_err_set_msg(e, err_msg, "en")) {
goto error;
}

if (err_path) {
/* error-path */
if (nc_err_set_path(e, err_path)) {
/* error-info */
err_info = NULL;
for (j = 0; j < err_info_count; ++j) {
if (!err_info) {
if (lyd_new_opaq2(e, NULL, "error-info", NULL, NULL, NC_NS_BASE, &err_info)) {
goto error;
}
}

/* error-message */
if (nc_err_set_msg(e, err_msg, "en")) {
goto error;
if (!strcmp(err_info_elem[j], "bad-attribute") || !strcmp(err_info_elem[j], "bad-element") ||
!strcmp(err_info_elem[j], "bad-namespace") || !strcmp(err_info_elem[j], "session-id")) {
/* NETCONF error-info */
ns = NC_NS_BASE;
} else if (!strcmp(err_info_elem[j], "non-unique") || !strcmp(err_info_elem[j], "missing-choice")) {
/* YANG error-info */
ns = "urn:ietf:params:xml:ns:yang:1";
} else {
/* custom (unknown) */
ns = "urn:netconf:custom-error-info";
}

/* error-info */
err_info = NULL;
for (j = 0; j < err_info_count; ++j) {
if (!err_info) {
if (lyd_new_opaq2(e, NULL, "error-info", NULL, NULL, NC_NS_BASE, &err_info)) {
goto error;
}
}
if (!strcmp(err_info_elem[j], "bad-attribute") || !strcmp(err_info_elem[j], "bad-element") ||
!strcmp(err_info_elem[j], "bad-namespace") || !strcmp(err_info_elem[j], "session-id")) {
/* NETCONF error-info */
ns = NC_NS_BASE;
} else if (!strcmp(err_info_elem[j], "non-unique") || !strcmp(err_info_elem[j], "missing-choice")) {
/* YANG error-info */
ns = "urn:ietf:params:xml:ns:yang:1";
} else {
/* custom (unknown) */
ns = "urn:netconf:custom-error-info";
}
if (lyd_new_opaq2(err_info, NULL, err_info_elem[j], err_info_val[j], NULL, ns, NULL)) {
goto error;
}
if (lyd_new_opaq2(err_info, NULL, err_info_elem[j], err_info_val[j], NULL, ns, NULL)) {
goto error;
}

/* append */
lyd_insert_sibling(e_first, e, &e_first);

free(err_info_elem);
free(err_info_val);
++i;
}

/* append */
lyd_insert_sibling(e_first, e, &e_first);

free(err_info_elem);
free(err_info_val);
return e_first;

error:
Expand All @@ -311,15 +301,17 @@ np2srv_err_reply_sr(const sr_error_info_t *err_info)
const struct ly_ctx *ly_ctx;
size_t i;

/* try to find a NETCONF error */
/* try to find a NETCONF error(s) */
for (i = 0; i < err_info->err_count; ++i) {
if (err_info->err[i].error_format && !strcmp(err_info->err[i].error_format, "NETCONF")) {
/* NETCONF error */
e = np2srv_err_nc(&err_info->err[i]);
if (e) {

if (reply) {
nc_server_reply_add_err(reply, e);
} else {
reply = nc_server_reply_err(e);
}
break;
}
}

Expand All @@ -339,7 +331,6 @@ np2srv_err_reply_sr(const sr_error_info_t *err_info)
} else {
reply = nc_server_reply_err(e);
}
e = NULL;
}
sr_release_context(np2srv.sr_conn);

Expand Down
Loading

0 comments on commit 025e15f

Please sign in to comment.