Skip to content

Commit 21849df

Browse files
stewegsamuel-gauthier
authored andcommitted
data: add insert_sibling function to DNode
This patch introduce the insert_sibling function usable for inserting data nodes on the module level. Add a unit test. A presence container is added on the conf container of the yolo-nodetypes module to avoid this error: > libyang.util.LibyangError: failed to parse data tree: Too few "list1" instances. Fixes: #95 Signed-off-by: Stefan Gula <steweg@gmail.com> Signed-off-by: Samuel Gauthier <samuel.gauthier@6wind.com>
1 parent 4b5b425 commit 21849df

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

cffi/cdefs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,7 @@ LY_ERR lyd_any_value_str(const struct lyd_node *, char **);
971971
LY_ERR lyd_merge_tree(struct lyd_node **, const struct lyd_node *, uint16_t);
972972
LY_ERR lyd_merge_siblings(struct lyd_node **, const struct lyd_node *, uint16_t);
973973
LY_ERR lyd_insert_child(struct lyd_node *, struct lyd_node *);
974+
LY_ERR lyd_insert_sibling(struct lyd_node *, struct lyd_node *, struct lyd_node **);
974975
LY_ERR lyd_diff_apply_all(struct lyd_node **, const struct lyd_node *);
975976

976977
#define LYD_DUP_NO_META ...

libyang/data.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,11 @@ def insert_child(self, node):
322322
if ret != lib.LY_SUCCESS:
323323
raise self.context.error("cannot insert node")
324324

325+
def insert_sibling(self, node):
326+
ret = lib.lyd_insert_sibling(self.cdata, node.cdata, ffi.NULL)
327+
if ret != lib.LY_SUCCESS:
328+
raise self.context.error("cannot insert sibling")
329+
325330
def name(self) -> str:
326331
return c2str(self.cdata.schema.name)
327332

tests/test_data.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
IOType,
2121
LibyangError,
2222
)
23+
from libyang.data import dict_to_dnode
2324

2425

2526
YANG_DIR = os.path.join(os.path.dirname(__file__), "yang")
@@ -32,6 +33,7 @@ def setUp(self):
3233
modules = [
3334
self.ctx.load_module("ietf-netconf"),
3435
self.ctx.load_module("yolo-system"),
36+
self.ctx.load_module("yolo-nodetypes"),
3537
]
3638

3739
for mod in modules:
@@ -922,3 +924,18 @@ def test_dnode_unlink(self):
922924
self.assertIsNone(child, None)
923925
finally:
924926
dnode.free()
927+
928+
def test_dnode_insert_sibling(self):
929+
MAIN = {"yolo-nodetypes:conf": {"percentage": "20.2"}}
930+
SIBLING = {"yolo-nodetypes:test1": 10}
931+
module = self.ctx.get_module("yolo-nodetypes")
932+
dnode1 = dict_to_dnode(MAIN, module, None, validate=False)
933+
dnode2 = dict_to_dnode(SIBLING, module, None, validate=False)
934+
self.assertEqual(len(list(dnode1.siblings(include_self=False))), 0)
935+
self.assertEqual(len(list(dnode2.siblings(include_self=False))), 0)
936+
dnode2.insert_sibling(dnode1)
937+
self.assertEqual(len(list(dnode1.siblings(include_self=False))), 1)
938+
self.assertEqual(len(list(dnode2.siblings(include_self=False))), 1)
939+
sibling = next(dnode1.siblings(include_self=False), None)
940+
self.assertIsInstance(sibling, DLeaf)
941+
self.assertEqual(sibling.cdata, dnode2.cdata)

tests/yang/yolo/yolo-nodetypes.yang

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module yolo-nodetypes {
1212
}
1313

1414
container conf {
15+
presence "enable conf";
1516
description
1617
"Configuration.";
1718
leaf percentage {
@@ -62,4 +63,8 @@ module yolo-nodetypes {
6263
type string;
6364
}
6465
}
66+
67+
leaf test1 {
68+
type uint8;
69+
}
6570
}

0 commit comments

Comments
 (0)