Skip to content

Commit 180c2d5

Browse files
committed
cffi: allow usage of libyang v5
Adapt CFFI bindings for libyang v5 (soversion 5.0.0+) Update cdefs.h, source.c, and Python call sites to match the libyang 4.2.2 -> 5.0.0 API changes: - parent field type changed from lyd_node_inner* to lyd_node* across all node structs - lyd_any_value_str gained a LYD_FORMAT parameter - lys_find_child signature was reworked - lyd_node_any lost the union lyd_any_value/LYD_ANYDATA_VALUETYPE fields - LYD_NEW_VAL_BIN / LYS_GETNEXT_WITHSCHEMAMOUNT removed tests: fix failures caused by libyang v5 changes Add missing ietf-inet-types YANG module to the test search path, as libyang v5 no longer resolves it automatically. Update the module data callback test to handle the ietf-inet-types dependency, and adjust expected error paths to match libyang v5 output. Signed-off-by: Antonio Prcela <antonio.prcela@gmail.com>
1 parent a69eb4a commit 180c2d5

7 files changed

Lines changed: 484 additions & 47 deletions

File tree

cffi/cdefs.h

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ struct lyd_node {
262262
uint32_t hash;
263263
uint32_t flags;
264264
const struct lysc_node *schema;
265-
struct lyd_node_inner *parent;
265+
struct lyd_node *parent;
266266
struct lyd_node *next;
267267
struct lyd_node *prev;
268268
struct lyd_meta *meta;
@@ -273,7 +273,6 @@ LY_ERR lys_set_implemented(struct lys_module *, const char **);
273273

274274
#define LYD_NEW_VAL_OUTPUT ...
275275
#define LYD_NEW_VAL_STORE_ONLY ...
276-
#define LYD_NEW_VAL_BIN ...
277276
#define LYD_NEW_VAL_CANON ...
278277
#define LYD_NEW_META_CLEAR_DFLT ...
279278
#define LYD_NEW_PATH_UPDATE ...
@@ -876,9 +875,8 @@ struct lysc_ext {
876875
#define LYS_GETNEXT_WITHCASE ...
877876
#define LYS_GETNEXT_INTONPCONT ...
878877
#define LYS_GETNEXT_OUTPUT ...
879-
#define LYS_GETNEXT_WITHSCHEMAMOUNT ...
880878

881-
const struct lysc_node* lys_find_child(const struct lysc_node *, const struct lys_module *, const char *, size_t, uint16_t, uint32_t);
879+
const struct lysc_node* lys_find_child(const struct ly_ctx *, const struct lysc_node *, const struct lys_module *, const char *, uint32_t, const char *, uint32_t, uint32_t);
882880
const struct lysc_node* lysc_node_child(const struct lysc_node *);
883881
const struct lysc_node_action* lysc_node_actions(const struct lysc_node *);
884882
const struct lysc_node_notif* lysc_node_notifs(const struct lysc_node *);
@@ -902,7 +900,7 @@ struct lyd_node_inner {
902900
uint32_t hash;
903901
uint32_t flags;
904902
const struct lysc_node *schema;
905-
struct lyd_node_inner *parent;
903+
struct lyd_node *parent;
906904
struct lyd_node *next;
907905
struct lyd_node *prev;
908906
struct lyd_meta *meta;
@@ -924,7 +922,7 @@ struct lyd_node_term {
924922
uint32_t hash;
925923
uint32_t flags;
926924
const struct lysc_node *schema;
927-
struct lyd_node_inner *parent;
925+
struct lyd_node *parent;
928926
struct lyd_node *next;
929927
struct lyd_node *prev;
930928
struct lyd_meta *meta;
@@ -984,9 +982,6 @@ struct lysc_must {
984982
struct lysc_ext_instance *exts;
985983
};
986984

987-
struct pcre2_real_code;
988-
typedef struct pcre2_real_code pcre2_code;
989-
990985
struct lysc_pattern {
991986
const char *expr;
992987
const char *dsc;
@@ -1161,39 +1156,25 @@ struct lyd_meta {
11611156
struct lyd_value value;
11621157
};
11631158

1164-
typedef enum {
1165-
LYD_ANYDATA_DATATREE,
1166-
LYD_ANYDATA_STRING,
1167-
LYD_ANYDATA_XML,
1168-
LYD_ANYDATA_JSON
1169-
} LYD_ANYDATA_VALUETYPE;
1170-
1171-
union lyd_any_value {
1172-
struct lyd_node *tree;
1173-
const char *str;
1174-
const char *xml;
1175-
const char *json;
1176-
};
1177-
11781159
struct lyd_node_any {
11791160
union {
11801161
struct lyd_node node;
11811162
struct {
11821163
uint32_t hash;
11831164
uint32_t flags;
11841165
const struct lysc_node *schema;
1185-
struct lyd_node_inner *parent;
1166+
struct lyd_node *parent;
11861167
struct lyd_node *next;
11871168
struct lyd_node *prev;
11881169
struct lyd_meta *meta;
11891170
void *priv;
11901171
};
11911172
};
1192-
union lyd_any_value value;
1193-
LYD_ANYDATA_VALUETYPE value_type;
1173+
struct lyd_node *child;
1174+
...;
11941175
};
11951176

1196-
LY_ERR lyd_any_value_str(const struct lyd_node *, char **);
1177+
LY_ERR lyd_any_value_str(const struct lyd_node *, LYD_FORMAT, char **);
11971178

11981179
#define LYD_MERGE_DEFAULTS ...
11991180
#define LYD_MERGE_DESTRUCT ...
@@ -1212,8 +1193,8 @@ LY_ERR lyd_diff_apply_all(struct lyd_node **, const struct lyd_node *);
12121193
#define LYD_DUP_WITH_FLAGS ...
12131194
#define LYD_DUP_WITH_PARENTS ...
12141195

1215-
LY_ERR lyd_dup_siblings(const struct lyd_node *, struct lyd_node_inner *, uint32_t, struct lyd_node **);
1216-
LY_ERR lyd_dup_single(const struct lyd_node *, struct lyd_node_inner *, uint32_t, struct lyd_node **);
1196+
LY_ERR lyd_dup_siblings(const struct lyd_node *, struct lyd_node *, uint32_t, struct lyd_node **);
1197+
LY_ERR lyd_dup_single(const struct lyd_node *, struct lyd_node *, uint32_t, struct lyd_node **);
12171198
void lyd_free_meta_single(struct lyd_meta *);
12181199

12191200
struct lysp_tpdf {
@@ -1291,7 +1272,7 @@ struct lyd_node_opaq {
12911272
uint32_t hash;
12921273
uint32_t flags;
12931274
const struct lysc_node *schema;
1294-
struct lyd_node_inner *parent;
1275+
struct lyd_node *parent;
12951276
struct lyd_node *next;
12961277
struct lyd_node *prev;
12971278
struct lyd_meta *meta;

cffi/source.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
#include <libyang/libyang.h>
77
#include <libyang/version.h>
88

9-
#if LY_VERSION_MAJOR * 10000 + LY_VERSION_MINOR * 100 + LY_VERSION_MICRO < 40202
10-
#error "This version of libyang bindings only works with libyang soversion 4.2.2+"
9+
#if LY_VERSION_MAJOR * 10000 + LY_VERSION_MINOR * 100 + LY_VERSION_MICRO < 50000
10+
#error "This version of libyang bindings only works with libyang soversion 5.0.0+"
1111
#endif

libyang/data.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ def newval_flags(
9494
flags |= lib.LYD_NEW_VAL_OUTPUT
9595
if store_only:
9696
flags |= lib.LYD_NEW_VAL_STORE_ONLY
97-
if bin_value:
98-
flags |= lib.LYD_NEW_VAL_BIN
9997
if canon_value:
10098
flags |= lib.LYD_NEW_VAL_CANON
10199
if meta_clear_default:
@@ -1174,7 +1172,7 @@ class DNotif(DContainer):
11741172
class DAnyxml(DNode):
11751173
def value(self, fmt: str = "xml"):
11761174
anystr = ffi.new("char **", ffi.NULL)
1177-
ret = lib.lyd_any_value_str(self.cdata, anystr)
1175+
ret = lib.lyd_any_value_str(self.cdata, data_format(fmt), anystr)
11781176
if ret != lib.LY_SUCCESS:
11791177
raise self.context.error("cannot get data")
11801178
return c2str(anystr[0])
@@ -1185,7 +1183,7 @@ def value(self, fmt: str = "xml"):
11851183
class DAnydata(DNode):
11861184
def value(self, fmt: str = "xml"):
11871185
anystr = ffi.new("char **", ffi.NULL)
1188-
ret = lib.lyd_any_value_str(self.cdata, anystr)
1186+
ret = lib.lyd_any_value_str(self.cdata, data_format(fmt), anystr)
11891187
if ret != lib.LY_SUCCESS:
11901188
raise self.context.error("cannot get data")
11911189
return c2str(anystr[0])

libyang/schema.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,8 +1885,6 @@ def iter_children_options(
18851885
options |= lib.LYS_GETNEXT_INTONPCONT
18861886
if output:
18871887
options |= lib.LYS_GETNEXT_OUTPUT
1888-
if with_schema_mount:
1889-
options |= lib.LYS_GETNEXT_WITHSCHEMAMOUNT
18901888
return options
18911889

18921890

tests/test_context.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,14 @@ def test_ctx_disable_searchdirs(self):
154154

155155
def test_ctx_using_clb(self):
156156
def get_module_valid_clb(mod_name, *_):
157-
YOLO_NODETYPES_MOD_PATH = os.path.join(YANG_DIR, "yolo/yolo-nodetypes.yang")
158-
self.assertEqual(mod_name, "yolo-nodetypes")
159-
with open(YOLO_NODETYPES_MOD_PATH, encoding="utf-8") as f:
157+
MOD_PATHS = {
158+
"yolo-nodetypes": os.path.join(YANG_DIR, "yolo/yolo-nodetypes.yang"),
159+
"ietf-inet-types": os.path.join(
160+
YANG_DIR, "ietf/ietf-inet-types@2013-07-15.yang"
161+
),
162+
}
163+
self.assertIn(mod_name, MOD_PATHS)
164+
with open(MOD_PATHS[mod_name], encoding="utf-8") as f:
160165
mod_str = f.read()
161166
return "yang", mod_str
162167

tests/test_data.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,21 +288,19 @@ def test_data_parse_config_xml_multi_error(self):
288288
self.assertEqual(
289289
str(cm.exception),
290290
'failed to parse data tree: Invalid boolean value "abcd".: '
291-
"Data path: /yolo-system:conf/url[proto='https']/enabled (line 6): "
291+
"Data path: /yolo-system:url[proto='https']/enabled (line 6): "
292292
'List instance is missing its key "host".: '
293-
"Data path: /yolo-system:conf/url[proto='https'] (line 7)",
293+
"Data path: /yolo-system:url[proto='https'] (line 7)",
294294
)
295295

296296
first = cm.exception.errors[0]
297297
self.assertEqual(first.msg, 'Invalid boolean value "abcd".')
298-
self.assertEqual(
299-
first.data_path, "/yolo-system:conf/url[proto='https']/enabled"
300-
)
298+
self.assertEqual(first.data_path, "/yolo-system:url[proto='https']/enabled")
301299
self.assertEqual(first.line, 6)
302300

303301
second = cm.exception.errors[1]
304302
self.assertEqual(second.msg, 'List instance is missing its key "host".')
305-
self.assertEqual(second.data_path, "/yolo-system:conf/url[proto='https']")
303+
self.assertEqual(second.data_path, "/yolo-system:url[proto='https']")
306304
self.assertEqual(second.line, 7)
307305

308306
XML_STATE = """<state xmlns="urn:yang:yolo:system">

0 commit comments

Comments
 (0)