Skip to content

Commit ce74fcc

Browse files
committed
libyang2 and libyang3
wrappers/C macro to be able to compile with both versions. Signed-off-by: Vincent Jardin <[email protected]>
1 parent b23e061 commit ce74fcc

File tree

5 files changed

+111
-18
lines changed

5 files changed

+111
-18
lines changed

lib/mgmt_be_client.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,14 @@ static int mgmt_be_send_notification(void *__be_client, const char *xpath,
342342
if (err) {
343343
flog_err(EC_LIB_LIBYANG,
344344
"%s: error creating notification data: %s", __func__,
345-
ly_strerrcode(err));
345+
#if defined(HAVE_LY_STRERRCODE)
346+
ly_strerrcode(err)
347+
#elif defined(HAVE_LY_STRERR)
348+
ly_strerr(err)
349+
#else
350+
#error missing ly_strerror()
351+
#endif
352+
);
346353
ret = 1;
347354
goto done;
348355
}

lib/northbound.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2134,7 +2134,15 @@ int nb_notification_send(const char *xpath, struct list *arguments)
21342134
lyerr:
21352135
flog_err(EC_LIB_LIBYANG,
21362136
"%s: error creating notification data: %s",
2137-
__func__, ly_strerrcode(err));
2137+
__func__,
2138+
#if defined(HAVE_LY_STRERRCODE)
2139+
ly_strerrcode(err)
2140+
#elif defined(HAVE_LY_STRERR)
2141+
ly_strerr(err)
2142+
#else
2143+
#error missing ly_strerror()
2144+
#endif
2145+
);
21382146
ret += 1;
21392147
goto done;
21402148
}

lib/northbound_cli.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "termtable.h"
1717
#include "db.h"
1818
#include "debug.h"
19+
#include <libyang/version.h>
1920
#include "yang_translator.h"
2021
#include "northbound.h"
2122
#include "northbound_cli.h"
@@ -1434,7 +1435,13 @@ static int nb_cli_oper_data_cb(const struct lysc_node *snode,
14341435
LYD_NEW_PATH_UPDATE, &dnode);
14351436
if (err) {
14361437
flog_warn(EC_LIB_LIBYANG, "%s: lyd_new_path(%s) failed: %s",
1437-
__func__, data->xpath, ly_errmsg(ly_native_ctx));
1438+
__func__, data->xpath,
1439+
#if (LY_VERSION_MAJOR < 3)
1440+
ly_errmsg(ly_native_ctx)
1441+
#else
1442+
ly_last_logmsg()
1443+
#endif
1444+
);
14381445
goto error;
14391446
}
14401447

lib/vty.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "libfrr.h"
4040
#include "frrstr.h"
4141
#include "lib_errors.h"
42+
#include <libyang/version.h>
4243
#include "northbound_cli.h"
4344
#include "printfrr.h"
4445
#include "json.h"
@@ -3670,15 +3671,24 @@ static ssize_t vty_mgmt_libyang_print(void *user_data, const void *buf,
36703671
}
36713672

36723673
static void vty_out_yang_error(struct vty *vty, LYD_FORMAT format,
3673-
struct ly_err_item *ei)
3674+
const struct ly_err_item *ei)
36743675
{
3676+
#if (LY_VERSION_MAJOR < 3)
3677+
#define data_path path
3678+
#else
3679+
#define data_path data_path
3680+
#endif
36753681
bool have_apptag = ei->apptag && ei->apptag[0] != 0;
3676-
bool have_path = ei->path && ei->path[0] != 0;
3682+
bool have_path = ei->data_path && ei->data_path[0] != 0;
36773683
bool have_msg = ei->msg && ei->msg[0] != 0;
36783684
const char *severity = NULL;
36793685
const char *evalid = NULL;
36803686
const char *ecode = NULL;
3687+
#if (LY_VERSION_MAJOR < 3)
36813688
LY_ERR err = ei->no;
3689+
#else
3690+
LY_ERR err = ei->err;
3691+
#endif
36823692

36833693
if (ei->level == LY_LLERR)
36843694
severity = "error";
@@ -3703,7 +3713,8 @@ static void vty_out_yang_error(struct vty *vty, LYD_FORMAT format,
37033713
vty_out(vty, "<error-validation>%s</error-validation>\n",
37043714
evalid);
37053715
if (have_path)
3706-
vty_out(vty, "<error-path>%s</error-path>\n", ei->path);
3716+
vty_out(vty, "<error-path>%s</error-path>\n",
3717+
ei->data_path);
37073718
if (have_apptag)
37083719
vty_out(vty, "<error-app-tag>%s</error-app-tag>\n",
37093720
ei->apptag);
@@ -3722,7 +3733,7 @@ static void vty_out_yang_error(struct vty *vty, LYD_FORMAT format,
37223733
if (evalid)
37233734
vty_out(vty, ", \"error-validation\": \"%s\"", evalid);
37243735
if (have_path)
3725-
vty_out(vty, ", \"error-path\": \"%s\"", ei->path);
3736+
vty_out(vty, ", \"error-path\": \"%s\"", ei->data_path);
37263737
if (have_apptag)
37273738
vty_out(vty, ", \"error-app-tag\": \"%s\"", ei->apptag);
37283739
if (have_msg)
@@ -3739,18 +3750,19 @@ static void vty_out_yang_error(struct vty *vty, LYD_FORMAT format,
37393750
if (evalid)
37403751
vty_out(vty, " invalid: %s", evalid);
37413752
if (have_path)
3742-
vty_out(vty, " path: %s", ei->path);
3753+
vty_out(vty, " path: %s", ei->data_path);
37433754
if (have_apptag)
37443755
vty_out(vty, " app-tag: %s", ei->apptag);
37453756
if (have_msg)
37463757
vty_out(vty, " msg: %s", ei->msg);
37473758
break;
37483759
}
3760+
#undef data_path
37493761
}
37503762

37513763
static uint vty_out_yang_errors(struct vty *vty, LYD_FORMAT format)
37523764
{
3753-
struct ly_err_item *ei = ly_err_first(ly_native_ctx);
3765+
const struct ly_err_item *ei = ly_err_first(ly_native_ctx);
37543766
uint count;
37553767

37563768
if (!ei)

lib/yang.c

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "lib_errors.h"
1212
#include "yang.h"
1313
#include "yang_translator.h"
14+
#include <libyang/version.h>
1415
#include "northbound.h"
1516

1617
#include "lib/config_paths.h"
@@ -701,7 +702,12 @@ struct yang_data *yang_data_list_find(const struct list *list,
701702
}
702703

703704
/* Make libyang log its errors using FRR logging infrastructure. */
704-
static void ly_log_cb(LY_LOG_LEVEL level, const char *msg, const char *path)
705+
static void ly_zlog_cb(LY_LOG_LEVEL level, const char *msg, const char *data_path
706+
#if !(LY_VERSION_MAJOR < 3)
707+
,
708+
const char *schema_path, uint64_t line
709+
#endif
710+
)
705711
{
706712
int priority = LOG_ERR;
707713

@@ -718,8 +724,14 @@ static void ly_log_cb(LY_LOG_LEVEL level, const char *msg, const char *path)
718724
break;
719725
}
720726

721-
if (path)
722-
zlog(priority, "libyang: %s (%s)", msg, path);
727+
if (data_path)
728+
zlog(priority, "libyang: %s (%s)", msg, data_path);
729+
#if !(LY_VERSION_MAJOR < 3)
730+
else if (schema_path)
731+
zlog(priority, "libyang %s (%s)\n", msg, schema_path);
732+
else if (line)
733+
zlog(priority, "libyang %s (line %" PRIu64 ")\n", msg, line);
734+
#endif
723735
else
724736
zlog(priority, "libyang: %s", msg);
725737
}
@@ -734,21 +746,38 @@ LY_ERR yang_parse_notification(const char *xpath, LYD_FORMAT format,
734746

735747
err = ly_in_new_memory(data, &in);
736748
if (err) {
749+
#if (LY_VERSION_MAJOR < 3)
737750
zlog_err("Failed to initialize ly_in: %s", ly_last_errmsg());
751+
#else
752+
zlog_err("Failed to initialize ly_in: %s", ly_last_logmsg());
753+
#endif
738754
return err;
739755
}
740756

741757
err = lyd_parse_op(ly_native_ctx, NULL, in, format, LYD_TYPE_NOTIF_YANG,
742758
&tree, NULL);
743759
ly_in_free(in, 0);
744760
if (err) {
761+
#if (LY_VERSION_MAJOR < 3)
745762
zlog_err("Failed to parse notification: %s", ly_last_errmsg());
763+
#else
764+
zlog_err("Failed to parse notification: %s", ly_last_logmsg());
765+
#endif
746766
return err;
747767
}
748768

769+
#if (LY_VERSION_MAJOR < 3)
749770
err = lyd_find_xpath3(NULL, tree, xpath, NULL, &set);
771+
#else
772+
err = lyd_find_xpath3(NULL, tree, xpath, LY_VALUE_JSON, NULL, NULL,
773+
&set);
774+
#endif
750775
if (err) {
776+
#if (LY_VERSION_MAJOR < 3)
751777
zlog_err("Failed to parse notification: %s", ly_last_errmsg());
778+
#else
779+
zlog_err("Failed to parse notification: %s", ly_last_logmsg());
780+
#endif
752781
lyd_free_all(tree);
753782
return err;
754783
}
@@ -778,7 +807,11 @@ LY_ERR yang_print_tree_append(uint8_t **darr, const struct lyd_node *root,
778807

779808
err = lyd_print_clb(yang_print_darr, darr, root, format, options);
780809
if (err)
810+
#if (LY_VERSION_MAJOR < 3)
781811
zlog_err("Failed to save yang tree: %s", ly_last_errmsg());
812+
#else
813+
zlog_err("Failed to save yang tree: %s", ly_last_logmsg());
814+
#endif
782815
else if (format != LYD_LYB)
783816
*darr_append(*darr) = 0;
784817
return err;
@@ -819,7 +852,12 @@ char *yang_convert_lyd_format(const char *data, size_t data_len,
819852
if (err) {
820853
flog_err_sys(EC_LIB_LIBYANG,
821854
"cannot parse input data to convert: %s",
822-
ly_last_errmsg());
855+
#if (LY_VERSION_MAJOR < 3)
856+
ly_last_errmsg()
857+
#else
858+
ly_last_logmsg()
859+
#endif
860+
);
823861
return NULL;
824862
}
825863

@@ -839,23 +877,29 @@ char *yang_convert_lyd_format(const char *data, size_t data_len,
839877

840878
const char *yang_print_errors(struct ly_ctx *ly_ctx, char *buf, size_t buf_len)
841879
{
842-
struct ly_err_item *ei;
880+
const struct ly_err_item *ei;
843881

844882
ei = ly_err_first(ly_ctx);
845883
if (!ei)
846884
return "";
847885

848886
strlcpy(buf, "YANG error(s):\n", buf_len);
887+
#if (LY_VERSION_MAJOR < 3)
888+
#define data_path path
889+
#else
890+
#define data_path data_path
891+
#endif
849892
for (; ei; ei = ei->next) {
850-
if (ei->path) {
893+
if (ei->data_path) {
851894
strlcat(buf, " Path: ", buf_len);
852-
strlcat(buf, ei->path, buf_len);
895+
strlcat(buf, ei->data_path, buf_len);
853896
strlcat(buf, "\n", buf_len);
854897
}
855898
strlcat(buf, " Error: ", buf_len);
856899
strlcat(buf, ei->msg, buf_len);
857900
strlcat(buf, "\n", buf_len);
858901
}
902+
#undef data_path
859903

860904
ly_err_clean(ly_ctx, NULL);
861905

@@ -907,7 +951,12 @@ struct ly_ctx *yang_ctx_new_setup(bool embedded_modules, bool explicit_compile)
907951
void yang_init(bool embedded_modules, bool defer_compile)
908952
{
909953
/* Initialize libyang global parameters that affect all containers. */
910-
ly_set_log_clb(ly_log_cb, 1);
954+
ly_set_log_clb(ly_zlog_cb
955+
#if (LY_VERSION_MAJOR < 3)
956+
,
957+
1
958+
#endif
959+
);
911960
ly_log_options(LY_LOLOG | LY_LOSTORE);
912961

913962
/* Initialize libyang container for native models. */
@@ -926,7 +975,12 @@ void yang_init_loading_complete(void)
926975
if (ly_ctx_compile(ly_native_ctx) != LY_SUCCESS) {
927976
flog_err(EC_LIB_YANG_MODULE_LOAD,
928977
"%s: failed to compile loaded modules: %s", __func__,
929-
ly_errmsg(ly_native_ctx));
978+
#if (LY_VERSION_MAJOR < 3)
979+
ly_errmsg(ly_native_ctx)
980+
#else
981+
ly_last_logmsg()
982+
#endif
983+
);
930984
exit(1);
931985
}
932986
}
@@ -1219,7 +1273,12 @@ LY_ERR yang_lyd_trim_xpath(struct lyd_node **root, const char *xpath)
12191273

12201274
*root = lyd_first_sibling(*root);
12211275

1276+
#if (LY_VERSION_MAJOR < 3)
12221277
err = lyd_find_xpath3(NULL, *root, xpath, NULL, &set);
1278+
#else
1279+
err = lyd_find_xpath3(NULL, *root, xpath, LY_VALUE_JSON, NULL, NULL,
1280+
&set);
1281+
#endif
12231282
if (err) {
12241283
flog_err_sys(EC_LIB_LIBYANG,
12251284
"cannot obtain specific result for xpath \"%s\": %s",

0 commit comments

Comments
 (0)