Skip to content

Commit ba34331

Browse files
committed
schema/context: restore some backlinks support
In libyang v1 the schema nodes had a backlinks member to be able to look up dependents of the node. SONiC depends on this to provide functionality it uses and it needs to be exposed via the python module. In theory, exposing the 'dfs' functions could make this work, but it would likely be cost prohibitive since walking the tree would be expensive to create a python node for evaluation in native python. Instead this PR depends on the this libyang PR: CESNET/libyang#2351 And adds thin wrappers. This implementation provides 2 python functions: * Context.find_backlinks_paths() - This function can take the path of the base node and find all dependents. If no path is specified, then it will return all nodes that contain a leafref reference. * Context.find_leafref_path_target_paths() - This function takes an xpath, then returns all target nodes the xpath may reference. Typically only one will be returned, but multiples may be in the case of a union. A user can build a cache by combining Context.find_backlinks_paths() with no path set and building a reverse table using Context.find_leafref_path_target_paths() Signed-off-by: Brad House <[email protected]> fix fix fix
1 parent 8534053 commit ba34331

File tree

5 files changed

+234
-0
lines changed

5 files changed

+234
-0
lines changed

cffi/cdefs.h

+3
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,9 @@ const struct lysc_node* lys_find_child(const struct lysc_node *, const struct ly
861861
const struct lysc_node* lysc_node_child(const struct lysc_node *);
862862
const struct lysc_node_action* lysc_node_actions(const struct lysc_node *);
863863
const struct lysc_node_notif* lysc_node_notifs(const struct lysc_node *);
864+
ly_bool lysc_node_has_ancestor(const struct lysc_node *, const struct lysc_node *);
865+
LY_ERR lysc_node_find_lref_targets(const struct lysc_node *, struct ly_set **);
866+
LY_ERR lys_find_backlinks(const struct ly_ctx *, const struct lysc_node *, ly_bool, struct ly_set **);
864867

865868
typedef enum {
866869
LYD_PATH_STD,

libyang/context.py

+98
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,104 @@ def parse_data_file(
646646
json_null=json_null,
647647
)
648648

649+
def find_leafref_path_target_paths(self, leafref_path: str) -> list[str]:
650+
"""
651+
Fetch all leafref targets of the specified path
652+
653+
This is an enhanced version of lysc_node_lref_target() which will return
654+
a set of leafref target paths retrieved from the specified schema path.
655+
While lysc_node_lref_target() will only work on nodetype of LYS_LEAF and
656+
LYS_LEAFLIST this function will also evaluate other datatypes that may
657+
contain leafrefs such as LYS_UNION. This does not, however, search for
658+
children with leafref targets.
659+
660+
:arg self
661+
This instance on context
662+
:arg leafref_path:
663+
Path to node to search for leafref targets
664+
:returns List of target paths that the leafrefs of the specified node
665+
point to.
666+
"""
667+
if self.cdata is None:
668+
raise RuntimeError("context already destroyed")
669+
if leafref_path is None:
670+
raise RuntimeError("leafref_path must be defined")
671+
672+
out = []
673+
674+
node = lib.lys_find_path(self.cdata, ffi.NULL, str2c(leafref_path), 0)
675+
if node == ffi.NULL:
676+
raise self.error("leafref_path not found")
677+
678+
node_set = ffi.new("struct ly_set **")
679+
if (lib.lysc_node_find_lref_targets(node, node_set) != lib.LY_SUCCESS or
680+
node_set[0] == ffi.NULL or node_set[0].count == 0):
681+
raise self.error("leafref_path does not contain any leafref targets")
682+
683+
node_set = node_set[0]
684+
for i in range(node_set.count):
685+
path = lib.lysc_path(node_set.snodes[i], lib.LYSC_PATH_DATA, ffi.NULL, 0);
686+
out.append(c2str(path))
687+
lib.free(path)
688+
689+
lib.ly_set_free(node_set, ffi.NULL)
690+
691+
return out
692+
693+
694+
def find_backlinks_paths(self, match_path: str = None, match_ancestors: bool = False) -> list[str]:
695+
"""
696+
Search entire schema for nodes that contain leafrefs and return as a
697+
list of schema node paths.
698+
699+
Perform a complete scan of the schema tree looking for nodes that
700+
contain leafref entries. When a node contains a leafref entry, and
701+
match_path is specified, determine if reference points to match_path,
702+
if so add the node's path to returned list. If no match_path is
703+
specified, the node containing the leafref is always added to the
704+
returned set. When match_ancestors is true, will evaluate if match_path
705+
is self or an ansestor of self.
706+
707+
This does not return the leafref targets, but the actual node that
708+
contains a leafref.
709+
710+
:arg self
711+
This instance on context
712+
:arg match_path:
713+
Target path to use for matching
714+
:arg match_ancestors:
715+
Whether match_path is a base ancestor or an exact node
716+
:returns List of paths. Exception of match_path is not found or if no
717+
backlinks are found.
718+
"""
719+
if self.cdata is None:
720+
raise RuntimeError("context already destroyed")
721+
out = []
722+
723+
match_node = ffi.NULL
724+
if match_path is not None and match_path == "/" or match_path == "":
725+
match_path = None
726+
727+
if match_path:
728+
match_node = lib.lys_find_path(self.cdata, ffi.NULL, str2c(match_path), 0)
729+
if match_node == ffi.NULL:
730+
raise self.error("match_path not found")
731+
732+
node_set = ffi.new("struct ly_set **")
733+
if (lib.lys_find_backlinks(self.cdata, match_node, match_ancestors, node_set)
734+
!= lib.LY_SUCCESS or node_set[0] == ffi.NULL or node_set[0].count == 0):
735+
raise self.error("backlinks not found")
736+
737+
node_set = node_set[0]
738+
for i in range(node_set.count):
739+
path = lib.lysc_path(node_set.snodes[i], lib.LYSC_PATH_DATA, ffi.NULL, 0);
740+
out.append(c2str(path))
741+
lib.free(path)
742+
743+
lib.ly_set_free(node_set, ffi.NULL)
744+
745+
return out
746+
649747
def __iter__(self) -> Iterator[Module]:
650748
"""
651749
Return an iterator that yields all implemented modules from the context

tests/test_schema.py

+58
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,64 @@ def test_leaf_list_parsed(self):
801801
self.assertFalse(pnode.ordered())
802802

803803

804+
# -------------------------------------------------------------------------------------
805+
class BacklinksTest(unittest.TestCase):
806+
def setUp(self):
807+
self.ctx = Context(YANG_DIR)
808+
self.ctx.load_module("yolo-leafref-search")
809+
self.ctx.load_module("yolo-leafref-search-extmod")
810+
def tearDown(self):
811+
self.ctx.destroy()
812+
self.ctx = None
813+
def test_backlinks_all_nodes(self):
814+
expected = [
815+
"/yolo-leafref-search-extmod:my_extref_list/my_extref",
816+
"/yolo-leafref-search:refstr",
817+
"/yolo-leafref-search:refnum",
818+
"/yolo-leafref-search-extmod:my_extref_list/my_extref_union"
819+
]
820+
refs = self.ctx.find_backlinks_paths()
821+
expected.sort()
822+
refs.sort()
823+
self.assertEqual(expected, refs)
824+
def test_backlinks_one(self):
825+
expected = [
826+
"/yolo-leafref-search-extmod:my_extref_list/my_extref",
827+
"/yolo-leafref-search:refstr",
828+
"/yolo-leafref-search-extmod:my_extref_list/my_extref_union"
829+
]
830+
refs = self.ctx.find_backlinks_paths(
831+
match_path="/yolo-leafref-search:my_list/my_leaf_string"
832+
)
833+
expected.sort()
834+
refs.sort()
835+
self.assertEqual(expected, refs)
836+
def test_backlinks_children(self):
837+
expected = [
838+
"/yolo-leafref-search-extmod:my_extref_list/my_extref",
839+
"/yolo-leafref-search:refstr",
840+
"/yolo-leafref-search:refnum",
841+
"/yolo-leafref-search-extmod:my_extref_list/my_extref_union"
842+
]
843+
refs = self.ctx.find_backlinks_paths(
844+
match_path="/yolo-leafref-search:my_list",
845+
match_ancestors=True
846+
)
847+
expected.sort()
848+
refs.sort()
849+
self.assertEqual(expected, refs)
850+
def test_backlinks_leafref_target_paths(self):
851+
expected = [
852+
"/yolo-leafref-search:my_list/my_leaf_string"
853+
]
854+
refs = self.ctx.find_leafref_path_target_paths(
855+
"/yolo-leafref-search-extmod:my_extref_list/my_extref"
856+
)
857+
expected.sort()
858+
refs.sort()
859+
self.assertEqual(expected, refs)
860+
861+
804862
# -------------------------------------------------------------------------------------
805863
class ChoiceTest(unittest.TestCase):
806864
def setUp(self):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module yolo-leafref-search-extmod {
2+
yang-version 1.1;
3+
namespace "urn:yang:yolo:leafref-search-extmod";
4+
prefix leafref-search-extmod;
5+
6+
import wtf-types { prefix types; }
7+
8+
import yolo-leafref-search {
9+
prefix leafref-search;
10+
}
11+
12+
revision 2025-02-11 {
13+
description
14+
"Initial version.";
15+
}
16+
17+
list my_extref_list {
18+
key my_leaf_string;
19+
leaf my_leaf_string {
20+
type string;
21+
}
22+
leaf my_extref {
23+
type leafref {
24+
path "/leafref-search:my_list/leafref-search:my_leaf_string";
25+
}
26+
}
27+
leaf my_extref_union {
28+
type union {
29+
type leafref {
30+
path "/leafref-search:my_list/leafref-search:my_leaf_string";
31+
}
32+
type leafref {
33+
path "/leafref-search:my_list/leafref-search:my_leaf_number";
34+
}
35+
type types:number;
36+
}
37+
}
38+
}
39+
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module yolo-leafref-search {
2+
yang-version 1.1;
3+
namespace "urn:yang:yolo:leafref-search";
4+
prefix leafref-search;
5+
6+
import wtf-types { prefix types; }
7+
8+
revision 2025-02-11 {
9+
description
10+
"Initial version.";
11+
}
12+
13+
list my_list {
14+
key my_leaf_string;
15+
leaf my_leaf_string {
16+
type string;
17+
}
18+
leaf my_leaf_number {
19+
description
20+
"A number.";
21+
type types:number;
22+
}
23+
}
24+
25+
leaf refstr {
26+
type leafref {
27+
path "../my_list/my_leaf_string";
28+
}
29+
}
30+
31+
leaf refnum {
32+
type leafref {
33+
path "../my_list/my_leaf_number";
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)