Skip to content

Commit b5bdeb9

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#2352 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]>
1 parent d8acc34 commit b5bdeb9

File tree

5 files changed

+234
-1
lines changed

5 files changed

+234
-1
lines changed

cffi/cdefs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,8 @@ 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_ERR lysc_node_lref_targets(const struct lysc_node *, struct ly_set **);
865+
LY_ERR lysc_node_lref_backlinks(const struct ly_ctx *, const struct lysc_node *, ly_bool, struct ly_set **);
864866

865867
typedef enum {
866868
LYD_PATH_STD,

libyang/context.py

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# SPDX-License-Identifier: MIT
55

66
import os
7-
from typing import IO, Any, Callable, Iterator, Optional, Sequence, Tuple, Union
7+
from typing import IO, Any, Callable, Iterator, Optional, Sequence, Tuple, Union, List
88

99
from _libyang import ffi, lib
1010
from .data import (
@@ -655,6 +655,104 @@ def parse_data_file(
655655
json_null=json_null,
656656
)
657657

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

tests/test_schema.py

Lines changed: 58 additions & 0 deletions
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):
Lines changed: 39 additions & 0 deletions
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+
}
Lines changed: 36 additions & 0 deletions
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)