Skip to content

Commit

Permalink
subscription: return parent node to oper_state callback via extra_info
Browse files Browse the repository at this point in the history
The only way to differentiate two calls when there is a subscription
on an internal element of a list is by using the parent node. Moreover,
in these cases, it is on this parent node that the data structure to
be returned must be built.
  • Loading branch information
avazquezrd committed Apr 10, 2024
1 parent 956e214 commit 0df4d2e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions sysrepo/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,10 @@ def subscribe_module_change_unsafe(
* netconf_id: the NETCONF session ID set for the event originator
sysrepo session
* user: the effective username of the event originator sysrepo session
* parent_xpath: XPath to an existing parent of the requested nodes. It is
None for top-level nodes. Caller is supposed to append the requested
nodes to this data subtree and return either the original parent or a
top-level node.
The callback is expected to return a python dictionary containing the operational
data. The dictionary should be in the libyang "dict" format. It will be parsed to a
Expand Down
5 changes: 5 additions & 0 deletions sysrepo/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,14 @@ def oper_data_callback(session, sub_id, module, xpath, req_xpath, req_id, parent
callback = subscription.callback
private_data = subscription.private_data
if subscription.extra_info:
parent_xpath = None
if parent[0]:
with session.get_ly_ctx() as ly_ctx:
parent_xpath = DNode.new(ly_ctx, parent[0]).path()
extra_info = {
"netconf_id": session.get_netconf_id(),
"user": session.get_user(),
"parent_xpath": parent_xpath,
}
else:
extra_info = {}
Expand Down
2 changes: 2 additions & 0 deletions tests/test_subs_oper.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def oper_data_cb(xpath, private_data, **kwargs):
self.assertEqual(getpass.getuser(), kwargs["user"])
self.assertIn("netconf_id", kwargs)
self.assertEqual(kwargs["netconf_id"], 12)
self.assertIn("parent_xpath", kwargs)
self.assertIsNone(kwargs["parent_xpath"])
calls.append((xpath, private_data, kwargs))
return {"state": {}}

Expand Down

0 comments on commit 0df4d2e

Please sign in to comment.