Skip to content

Commit ed37465

Browse files
Pepa Hajekrjarry
authored andcommitted
Add possibility for adding default data nodes
New method DNode.add_default() allows add all missing default nodes.
1 parent fe732f9 commit ed37465

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

cffi/cdefs.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,5 +992,16 @@ struct lysc_when** lysc_node_when(const struct lysc_node *);
992992

993993
LY_ERR lyd_eval_xpath(const struct lyd_node *, const char *, ly_bool *);
994994

995+
typedef LY_ERR(* lyd_merge_cb)(struct lyd_node *, const struct lyd_node *, void *);
996+
LY_ERR lyd_merge_module(struct lyd_node **, const struct lyd_node *, const struct lys_module *, lyd_merge_cb, void *, uint16_t);
997+
998+
#define LYD_IMPLICIT_NO_STATE ...
999+
#define LYD_IMPLICIT_NO_CONFIG ...
1000+
#define LYD_IMPLICIT_OUTPUT ...
1001+
#define LYD_IMPLICIT_NO_DEFAULTS ...
1002+
1003+
LY_ERR lyd_new_implicit_all(struct lyd_node **, const struct ly_ctx *, uint32_t, struct lyd_node **);
1004+
1005+
9951006
/* from libc, needed to free allocated strings */
9961007
void free(void *);

libyang/data.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@
2424
LOG = logging.getLogger(__name__)
2525

2626

27+
# -------------------------------------------------------------------------------------
28+
def implicit_flags(
29+
no_config: bool = False,
30+
no_defaults: bool = False,
31+
no_state: bool = False,
32+
output: bool = False,
33+
) -> int:
34+
flags = 0
35+
if no_config:
36+
flags |= lib.LYD_IMPLICIT_NO_CONFIG
37+
if no_state:
38+
flags |= lib.LYD_IMPLICIT_NO_STATE
39+
if no_defaults:
40+
flags |= lib.LYD_IMPLICIT_NO_DEFAULTS
41+
if output:
42+
flags |= lib.LYD_IMPLICIT_OUTPUT
43+
return flags
44+
45+
2746
# -------------------------------------------------------------------------------------
2847
def printer_flags(
2948
with_siblings: bool = False,
@@ -237,6 +256,25 @@ def meta_free(self, name):
237256
lib.lyd_free_meta_single(item)
238257
break
239258

259+
def add_defaults(
260+
self,
261+
no_config: bool = False,
262+
no_defaults: bool = False,
263+
no_state: bool = False,
264+
output: bool = False,
265+
):
266+
flags = implicit_flags(
267+
no_config=no_config,
268+
no_defaults=no_defaults,
269+
no_state=no_state,
270+
output=output,
271+
)
272+
node_p = ffi.new("struct lyd_node **")
273+
node_p[0] = self.cdata
274+
ret = lib.lyd_new_implicit_all(node_p, self.context.cdata, flags, ffi.NULL)
275+
if ret != lib.LY_SUCCESS:
276+
raise self.context.error("cannot get module")
277+
240278
def flags(self):
241279
ret = {"default": False, "when_true": False, "new": False}
242280
if self.cdata.flags & lib.LYD_DEFAULT:

tests/test_data.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
DAnyxml,
1414
DataType,
1515
DContainer,
16+
DLeaf,
1617
DNode,
1718
DNotif,
1819
DRpc,
@@ -210,7 +211,7 @@ def test_data_parse_config_xml(self):
210211
dnode = self.ctx.parse_data_mem(self.XML_CONFIG, "xml", validate_present=True)
211212
self.assertIsInstance(dnode, DContainer)
212213
try:
213-
xml = dnode.print_mem("xml", with_siblings=True)
214+
xml = dnode.print_mem("xml", with_siblings=True, trim_default_values=True)
214215
self.assertEqual(xml, self.XML_CONFIG)
215216
finally:
216217
dnode.free()
@@ -532,7 +533,7 @@ def test_data_from_dict_leaf(self):
532533
{"hostname": "foo"}, strict=True, validate=True, validate_present=True
533534
)
534535
try:
535-
j = dnode.print_mem("json", pretty=False)
536+
j = dnode.print_mem("json", pretty=False, trim_default_values=True)
536537
finally:
537538
dnode.free()
538539
self.assertEqual(j, '{"yolo-system:state":{"hostname":"foo"}}')
@@ -779,3 +780,15 @@ def test_find_all(self):
779780
self.assertEqual(urls[0].print_dict(absolute=False), expected_url)
780781
finally:
781782
dnode.free()
783+
784+
def test_add_defaults(self):
785+
dnode = self.ctx.parse_data_mem(self.JSON_CONFIG, "json", validate_present=True)
786+
node = dnode.find_path("/yolo-system:conf/speed")
787+
self.assertIsInstance(node, DLeaf)
788+
node.free(with_siblings=False)
789+
node = dnode.find_path("/yolo-system:conf/speed")
790+
self.assertIsNone(node)
791+
dnode.add_defaults()
792+
node = dnode.find_path("/yolo-system:conf/speed")
793+
self.assertIsInstance(node, DLeaf)
794+
self.assertEqual(node.value(), 4321)

tests/test_diff.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
BaseTypeAdded,
99
BaseTypeRemoved,
1010
Context,
11+
DefaultAdded,
1112
EnumRemoved,
1213
EnumStatusAdded,
1314
EnumStatusRemoved,
@@ -34,6 +35,8 @@ class DiffTest(unittest.TestCase):
3435
(BaseTypeAdded, "/yolo-system:state/speed"),
3536
(BaseTypeRemoved, "/yolo-system:conf/speed"),
3637
(BaseTypeRemoved, "/yolo-system:state/speed"),
38+
(DefaultAdded, "/yolo-system:conf/speed"),
39+
(DefaultAdded, "/yolo-system:state/speed"),
3740
(NodeTypeAdded, "/yolo-system:conf/number"),
3841
(NodeTypeAdded, "/yolo-system:state/number"),
3942
(NodeTypeRemoved, "/yolo-system:conf/number"),

tests/yang/yolo/yolo-system.yang

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ module yolo-system {
104104
"The speed.";
105105
if-feature turbo-boost;
106106
type uint32;
107+
default 4321;
107108
}
108109

109110
leaf offline {

0 commit comments

Comments
 (0)