From 362f5b256520ee3ae916dbf72a7e18b2e8247ab3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20V=C3=A1zquez?= <32303781+avazquezrd@users.noreply.github.com> Date: Tue, 24 Jan 2023 11:36:21 +0100 Subject: [PATCH] subscription: return parent node to oper_state callback via extra_info MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. Signed-off-by: Adrián Vázquez <32303781+avazquezrd@users.noreply.github.com> --- sysrepo/session.py | 4 ++++ sysrepo/subscription.py | 5 +++++ tests/test_subs_oper.py | 2 ++ 3 files changed, 11 insertions(+) diff --git a/sysrepo/session.py b/sysrepo/session.py index 79c3493..f0ed440 100644 --- a/sysrepo/session.py +++ b/sysrepo/session.py @@ -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 diff --git a/sysrepo/subscription.py b/sysrepo/subscription.py index 342adfb..d2c9588 100644 --- a/sysrepo/subscription.py +++ b/sysrepo/subscription.py @@ -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 = {} diff --git a/tests/test_subs_oper.py b/tests/test_subs_oper.py index c16a2a3..d58f365 100644 --- a/tests/test_subs_oper.py +++ b/tests/test_subs_oper.py @@ -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": {}}