Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/leafref linking #108

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cffi/cdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct ly_ctx;
#define LY_CTX_REF_IMPLEMENTED ...
#define LY_CTX_SET_PRIV_PARSED ...
#define LY_CTX_LEAFREF_EXTENDED ...
#define LY_CTX_LEAFREF_LINKING ...


typedef enum {
Expand Down Expand Up @@ -1117,5 +1118,14 @@ struct lyd_attr {
LY_ERR lyd_new_attr(struct lyd_node *, const char *, const char *, const char *, struct lyd_attr **);
void lyd_free_attr_single(const struct ly_ctx *ctx, struct lyd_attr *attr);

struct lyd_leafref_links_rec {
const struct lyd_node_term *node;
const struct lyd_node_term **leafref_nodes;
const struct lyd_node_term **target_nodes;
};

LY_ERR lyd_leafref_get_links(const struct lyd_node_term *e, const struct lyd_leafref_links_rec **);
LY_ERR lyd_leafref_link_node_tree(struct lyd_node *);

/* from libc, needed to free allocated strings */
void free(void *);
3 changes: 3 additions & 0 deletions libyang/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(
disable_searchdir_cwd: bool = True,
explicit_compile: Optional[bool] = False,
leafref_extended: bool = False,
leafref_linking: bool = False,
yanglib_path: Optional[str] = None,
yanglib_fmt: str = "json",
cdata=None, # C type: "struct ly_ctx *"
Expand All @@ -44,6 +45,8 @@ def __init__(
options |= lib.LY_CTX_EXPLICIT_COMPILE
if leafref_extended:
options |= lib.LY_CTX_LEAFREF_EXTENDED
if leafref_linking:
options |= lib.LY_CTX_LEAFREF_LINKING
# force priv parsed
options |= lib.LY_CTX_SET_PRIV_PARSED

Expand Down
17 changes: 16 additions & 1 deletion libyang/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
SRpc,
Type,
)
from .util import DataType, IOType, LibyangError, c2str, str2c
from .util import DataType, IOType, LibyangError, c2str, ly_array_iter, str2c


LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -972,6 +972,21 @@ def free(self, with_siblings: bool = True) -> None:
finally:
self.cdata = ffi.NULL

def leafref_link_node_tree(self) -> None:
if self.cdata is None or self.cdata == ffi.NULL:
return
lib.lyd_leafref_link_node_tree(self.cdata)

def leafref_nodes(self) -> Iterator["DNode"]:
if self.cdata == ffi.NULL:
return
term_node = ffi.cast("struct lyd_node_term *", self.cdata)
out = ffi.new("const struct lyd_leafref_links_rec **")
if lib.lyd_leafref_get_links(term_node, out) != lib.LY_SUCCESS:
return
for n in ly_array_iter(out[0].leafref_nodes):
yield DNode.new(self.context, n)

def __repr__(self):
cls = self.__class__
return "<%s.%s: %s>" % (cls.__module__, cls.__name__, str(self))
Expand Down
26 changes: 26 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
DRpc,
IOType,
LibyangError,
Module,
)
from libyang.data import dict_to_dnode

Expand Down Expand Up @@ -1036,3 +1037,28 @@ def test_dnode_attrs_set_and_remove_multiple(self):

attrs.remove("ietf-netconf:operation")
self.assertEqual(len(attrs), 0)

def test_dnode_leafref_linking(self):
MAIN = """{
"yolo-leafref-extended:list1": [{
"leaf1": "val1",
"leaflist2": ["val2", "val3"]
}],
"yolo-leafref-extended:ref1": "val1"
}"""
self.ctx.destroy()
self.ctx = Context(YANG_DIR, leafref_extended=True, leafref_linking=True)
mod = self.ctx.load_module("yolo-leafref-extended")
self.assertIsInstance(mod, Module)
dnode1 = self.ctx.parse_data_mem(MAIN, "json", parse_only=True)
self.assertIsInstance(dnode1, DList)
dnode2 = next(dnode1.siblings(include_self=False))
self.assertIsInstance(dnode2, DLeaf)
dnode3 = next(dnode1.children())
self.assertIsInstance(dnode3, DLeaf)
self.assertIsNone(next(dnode3.leafref_nodes(), None))
dnode2.leafref_link_node_tree()
dnode4 = next(dnode3.leafref_nodes())
self.assertIsInstance(dnode4, DLeaf)
self.assertEqual(dnode4.cdata, dnode2.cdata)
dnode1.free()
Loading