Skip to content

Commit 01a2614

Browse files
committed
messages server UPDATE use rpc-error internal data nodes
Present in updated libyang. Also, the error-path is now properly printed in the appropriate format (XML) instead of always using JSON format.
1 parent 88e8638 commit 01a2614

File tree

5 files changed

+134
-66
lines changed

5 files changed

+134
-66
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ set(LIBNETCONF2_SOVERSION_FULL ${LIBNETCONF2_MAJOR_SOVERSION}.${LIBNETCONF2_MINO
7171
set(LIBNETCONF2_SOVERSION ${LIBNETCONF2_MAJOR_SOVERSION})
7272

7373
# Version of libyang library that this project depends on
74-
set(LIBYANG_DEP_VERSION 2.0.0)
75-
set(LIBYANG_DEP_SOVERSION 3.0.0)
74+
set(LIBYANG_DEP_VERSION 3.12.0)
75+
set(LIBYANG_DEP_SOVERSION 3.9.0)
7676
set(LIBYANG_DEP_SOVERSION_MAJOR 3)
7777

7878
# global C flags

src/messages_server.c

Lines changed: 71 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,21 @@ nc_err(const struct ly_ctx *ctx, NC_ERR tag, ...)
261261
{
262262
va_list ap;
263263
struct lyd_node *err = NULL;
264+
const struct lys_module *nc_mod;
264265
NC_ERR_TYPE type;
265266
const char *arg1, *arg2;
266267
uint32_t sid;
267268

268-
NC_CHECK_ARG_RET(NULL, tag, NULL);
269+
NC_CHECK_ARG_RET(NULL, tag, ctx, NULL);
270+
271+
nc_mod = ly_ctx_get_module_implemented(ctx, "ietf-netconf");
272+
if (!nc_mod) {
273+
ERR(NULL, "Module \"ietf-netconf\" missing in the context.");
274+
return NULL;
275+
}
269276

270277
/* rpc-error */
271-
if (lyd_new_opaq2(NULL, ctx, "rpc-error", NULL, NULL, NC_NS_BASE, &err)) {
278+
if (lyd_new_inner(NULL, nc_mod, "rpc-error", 0, &err)) {
272279
return NULL;
273280
}
274281

@@ -337,17 +344,17 @@ nc_err(const struct ly_ctx *ctx, NC_ERR tag, ...)
337344
ERRARG(NULL, "tag");
338345
goto fail;
339346
}
340-
if (lyd_new_opaq2(err, NULL, "error-type", nc_err_type2str(type), NULL, NC_NS_BASE, NULL)) {
347+
if (lyd_new_term(err, NULL, "error-type", nc_err_type2str(type), 0, NULL)) {
341348
goto fail;
342349
}
343350

344351
/* error-tag */
345-
if (lyd_new_opaq2(err, NULL, "error-tag", nc_err_tag2str(tag), NULL, NC_NS_BASE, NULL)) {
352+
if (lyd_new_term(err, NULL, "error-tag", nc_err_tag2str(tag), 0, NULL)) {
346353
goto fail;
347354
}
348355

349356
/* error-severity */
350-
if (lyd_new_opaq2(err, NULL, "error-severity", "error", NULL, NC_NS_BASE, NULL)) {
357+
if (lyd_new_term(err, NULL, "error-severity", "error", 0, NULL)) {
351358
goto fail;
352359
}
353360

@@ -474,13 +481,17 @@ nc_err(const struct ly_ctx *ctx, NC_ERR tag, ...)
474481
API NC_ERR_TYPE
475482
nc_err_get_type(const struct lyd_node *err)
476483
{
477-
struct lyd_node *match;
484+
const struct lysc_node *schema;
485+
struct lyd_node *match = NULL;
478486

479487
NC_CHECK_ARG_RET(NULL, err, 0);
480488

481-
lyd_find_sibling_opaq_next(lyd_child(err), "error-type", &match);
489+
schema = lys_find_path(NULL, err->schema, "error-type", 0);
490+
if (schema) {
491+
lyd_find_sibling_val(lyd_child(err), schema, NULL, 0, &match);
492+
}
482493
if (match) {
483-
return nc_err_str2type(((struct lyd_node_opaq *)match)->value);
494+
return nc_err_str2type(lyd_get_value(match));
484495
}
485496

486497
return 0;
@@ -489,13 +500,17 @@ nc_err_get_type(const struct lyd_node *err)
489500
API NC_ERR
490501
nc_err_get_tag(const struct lyd_node *err)
491502
{
492-
struct lyd_node *match;
503+
const struct lysc_node *schema;
504+
struct lyd_node *match = NULL;
493505

494506
NC_CHECK_ARG_RET(NULL, err, 0);
495507

496-
lyd_find_sibling_opaq_next(lyd_child(err), "error-tag", &match);
508+
schema = lys_find_path(NULL, err->schema, "error-tag", 0);
509+
if (schema) {
510+
lyd_find_sibling_val(lyd_child(err), schema, NULL, 0, &match);
511+
}
497512
if (match) {
498-
return nc_err_str2tag(((struct lyd_node_opaq *)match)->value);
513+
return nc_err_str2tag(lyd_get_value(match));
499514
}
500515

501516
return 0;
@@ -504,40 +519,45 @@ nc_err_get_tag(const struct lyd_node *err)
504519
API int
505520
nc_err_set_app_tag(struct lyd_node *err, const char *error_app_tag)
506521
{
507-
struct lyd_node *match, *prev_anchor;
522+
const struct lysc_node *schema;
523+
struct lyd_node *match;
508524

509525
NC_CHECK_ARG_RET(NULL, err, error_app_tag, -1);
510526

527+
/* find the schema node */
528+
schema = lys_find_path(NULL, err->schema, "error-app-tag", 0);
529+
if (!schema) {
530+
return -1;
531+
}
532+
511533
/* remove previous node */
512-
lyd_find_sibling_opaq_next(lyd_child(err), "error-app-tag", &match);
534+
lyd_find_sibling_val(lyd_child(err), schema, NULL, 0, &match);
513535
if (match) {
514536
lyd_free_tree(match);
515537
}
516538

517-
/* find the previous node anchor */
518-
lyd_find_sibling_opaq_next(lyd_child(err), "error-severity", &prev_anchor);
519-
520-
/* create the node at the right place */
521-
if (lyd_new_opaq2(err, NULL, "error-app-tag", error_app_tag, NULL, NC_NS_BASE, &match)) {
539+
/* create the node */
540+
if (lyd_new_term(err, NULL, "error-app-tag", error_app_tag, 0, &match)) {
522541
return -1;
523542
}
524-
if (prev_anchor) {
525-
lyd_insert_after(prev_anchor, match);
526-
}
527543

528544
return 0;
529545
}
530546

531547
API const char *
532548
nc_err_get_app_tag(const struct lyd_node *err)
533549
{
534-
struct lyd_node *match;
550+
const struct lysc_node *schema;
551+
struct lyd_node *match = NULL;
535552

536553
NC_CHECK_ARG_RET(NULL, err, NULL);
537554

538-
lyd_find_sibling_opaq_next(lyd_child(err), "error-app-tag", &match);
555+
schema = lys_find_path(NULL, err->schema, "error-tag", 0);
556+
if (schema) {
557+
lyd_find_sibling_val(lyd_child(err), schema, NULL, 0, &match);
558+
}
539559
if (match) {
540-
return ((struct lyd_node_opaq *)match)->value;
560+
return lyd_get_value(match);
541561
}
542562

543563
return NULL;
@@ -546,43 +566,45 @@ nc_err_get_app_tag(const struct lyd_node *err)
546566
API int
547567
nc_err_set_path(struct lyd_node *err, const char *error_path)
548568
{
549-
struct lyd_node *match, *prev_anchor;
569+
const struct lysc_node *schema;
570+
struct lyd_node *match;
550571

551572
NC_CHECK_ARG_RET(NULL, err, error_path, -1);
552573

574+
/* find the schema node */
575+
schema = lys_find_path(NULL, err->schema, "error-path", 0);
576+
if (!schema) {
577+
return -1;
578+
}
579+
553580
/* remove previous node */
554-
lyd_find_sibling_opaq_next(lyd_child(err), "error-path", &match);
581+
lyd_find_sibling_val(lyd_child(err), schema, NULL, 0, &match);
555582
if (match) {
556583
lyd_free_tree(match);
557584
}
558585

559-
/* find the previous node anchor */
560-
lyd_find_sibling_opaq_next(lyd_child(err), "error-app-tag", &prev_anchor);
561-
if (!prev_anchor) {
562-
lyd_find_sibling_opaq_next(lyd_child(err), "error-severity", &prev_anchor);
563-
}
564-
565-
/* create the node at the right place */
566-
if (lyd_new_opaq2(err, NULL, "error-path", error_path, NULL, NC_NS_BASE, &match)) {
586+
/* create the node */
587+
if (lyd_new_term(err, NULL, "error-path", error_path, 0, &match)) {
567588
return -1;
568589
}
569-
if (prev_anchor) {
570-
lyd_insert_after(prev_anchor, match);
571-
}
572590

573591
return 0;
574592
}
575593

576594
API const char *
577595
nc_err_get_path(const struct lyd_node *err)
578596
{
579-
struct lyd_node *match;
597+
const struct lysc_node *schema;
598+
struct lyd_node *match = NULL;
580599

581600
NC_CHECK_ARG_RET(NULL, err, NULL);
582601

583-
lyd_find_sibling_opaq_next(lyd_child(err), "error-path", &match);
602+
schema = lys_find_path(NULL, err->schema, "error-path", 0);
603+
if (schema) {
604+
lyd_find_sibling_val(lyd_child(err), schema, NULL, 0, &match);
605+
}
584606
if (match) {
585-
return ((struct lyd_node_opaq *)match)->value;
607+
return lyd_get_value(match);
586608
}
587609

588610
return NULL;
@@ -602,13 +624,13 @@ nc_err_set_msg(struct lyd_node *err, const char *error_message, const char *lang
602624
lyd_free_tree(match);
603625
}
604626

605-
/* find the previous node anchor */
606-
lyd_find_sibling_opaq_next(lyd_child(err), "error-path", &prev_anchor);
607-
if (!prev_anchor) {
608-
lyd_find_sibling_opaq_next(lyd_child(err), "error-app-tag", &prev_anchor);
627+
/* find the previous node anchor (last non-opaque node) */
628+
prev_anchor = lyd_child(err);
629+
while (prev_anchor && prev_anchor->next && prev_anchor->next->schema) {
630+
prev_anchor = prev_anchor->next;
609631
}
610-
if (!prev_anchor) {
611-
lyd_find_sibling_opaq_next(lyd_child(err), "error-severity", &prev_anchor);
632+
if (!prev_anchor->schema) {
633+
prev_anchor = NULL;
612634
}
613635

614636
/* create the node at the right place */
@@ -617,6 +639,9 @@ nc_err_set_msg(struct lyd_node *err, const char *error_message, const char *lang
617639
}
618640
if (prev_anchor) {
619641
lyd_insert_after(prev_anchor, match);
642+
} else if (match->prev != match) {
643+
/* some opaque nodes existed, this must be the first */
644+
lyd_insert_before(lyd_child(err), match);
620645
}
621646

622647
if (lang && lyd_new_attr(match, NULL, "xml:lang", lang, &attr)) {

src/messages_server.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ const char *nc_err_get_app_tag(const struct lyd_node *err);
210210
* @brief Set the \<error-path\> element of an error. Any previous value will be overwritten.
211211
*
212212
* @param[in] err Error opaque data node tree to modify.
213-
* @param[in] error_path New value of \<error-path\>.
213+
* @param[in] error_path New value of \<error-path\> in JSON format.
214214
* @return 0 on success, -1 on error.
215215
*/
216216
int nc_err_set_path(struct lyd_node *err, const char *error_path);

tests/test_fd_comm.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ test_send_recv_error(void)
240240
NC_MSG_TYPE msgtype;
241241
struct nc_rpc *rpc;
242242
struct lyd_node *envp, *op, *node;
243+
const struct lysc_node *schema;
243244
struct nc_pollsession *ps;
244245

245246
/* client RPC */
@@ -266,9 +267,10 @@ test_send_recv_error(void)
266267

267268
nc_rpc_free(rpc);
268269
assert_string_equal(LYD_NAME(lyd_child(envp)), "rpc-error");
269-
lyd_find_sibling_opaq_next(lyd_child(lyd_child(envp)), "error-tag", &node);
270+
schema = lys_find_path(LYD_CTX(envp), NULL, "/ietf-netconf:rpc-error/error-tag", 0);
271+
lyd_find_sibling_val(lyd_child(lyd_child(envp)), schema, NULL, 0, &node);
270272
assert_non_null(node);
271-
assert_string_equal(((struct lyd_node_opaq *)node)->value, "operation-not-supported");
273+
assert_string_equal(lyd_get_value(node), "operation-not-supported");
272274
lyd_free_tree(envp);
273275
assert_null(op);
274276
}
@@ -508,6 +510,7 @@ test_send_recv_malformed_10(void **state)
508510
struct nc_pollsession *ps;
509511
struct nc_rpc *rpc;
510512
struct lyd_node *envp, *op, *node;
513+
const struct lysc_node *schema;
511514
NC_MSG_TYPE msgtype;
512515
const char *msg;
513516

@@ -543,9 +546,10 @@ test_send_recv_malformed_10(void **state)
543546

544547
nc_rpc_free(rpc);
545548
assert_string_equal(LYD_NAME(lyd_child(envp)), "rpc-error");
546-
lyd_find_sibling_opaq_next(lyd_child(lyd_child(envp)), "error-tag", &node);
549+
schema = lys_find_path(LYD_CTX(envp), NULL, "/ietf-netconf:rpc-error/error-tag", 0);
550+
lyd_find_sibling_val(lyd_child(lyd_child(envp)), schema, NULL, 0, &node);
547551
assert_non_null(node);
548-
assert_string_equal(((struct lyd_node_opaq *)node)->value, "missing-attribute");
552+
assert_string_equal(lyd_get_value(node), "missing-attribute");
549553
lyd_free_tree(envp);
550554
assert_null(op);
551555
}

0 commit comments

Comments
 (0)