From 28ee0735780ce128e96e926813c73d7368d9cdca Mon Sep 17 00:00:00 2001 From: Jeremie Leska Date: Mon, 27 Jan 2025 14:20:28 +0100 Subject: [PATCH 1/2] session: add subscription update option Sysrepo allows to subscribe to configuration update before they are applied to the datastore so it is possible to edit them. Add the update flag to the subscribe_module_change and the subscribe_module_change_unsafe functions. Signed-off-by: Jeremie Leska --- sysrepo/session.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/sysrepo/session.py b/sysrepo/session.py index a04db6a..f08cf10 100644 --- a/sysrepo/session.py +++ b/sysrepo/session.py @@ -327,6 +327,7 @@ def get_ly_ctx(self) -> libyang.Context: to reject changes. """ + # pylint: disable=too-many-arguments def subscribe_module_change( self, module: str, @@ -338,6 +339,7 @@ def subscribe_module_change( passive: bool = False, done_only: bool = False, enabled: bool = False, + update: bool = False, filter_origin: bool = False, private_data: Any = None, asyncio_register: bool = False, @@ -372,6 +374,8 @@ def subscribe_module_change( :arg enabled: The subscriber wants to be notified about the current configuration at the moment of subscribing. + :arg update: + The subscriber wants to be called before the configuration is applied. :arg filter_origin: Filter events on the originator side to unburden the subscriber, but results in 0 value for filtered-out changes in the subscriber infos. @@ -410,6 +414,7 @@ def subscribe_module_change( passive=passive, done_only=done_only, enabled=enabled, + update=update, filter_origin=filter_origin, ) @@ -464,6 +469,7 @@ def subscribe_module_change_unsafe( passive: bool = False, done_only: bool = False, enabled: bool = False, + update: bool = False, filter_origin: bool = False, private_data: Any = None, asyncio_register: bool = False, @@ -499,6 +505,8 @@ def subscribe_module_change_unsafe( :arg enabled: The subscriber wants to be notified about the current configuration at the moment of subscribing. + :arg update: + The subscriber wants to be called before the configuration is applied. :arg filter_origin: Filter events on the originator side to unburden the subscriber, but results in 0 value for filtered-out changes in the subscriber infos. @@ -531,6 +539,7 @@ def subscribe_module_change_unsafe( passive=passive, done_only=done_only, enabled=enabled, + update=update, filter_origin=filter_origin, ) check_call( @@ -1659,6 +1668,7 @@ def _subscribe_flags( enabled=False, oper_merge=False, filter_origin=False, + update=False, ): flags = 0 if no_thread: @@ -1669,6 +1679,8 @@ def _subscribe_flags( flags |= lib.SR_SUBSCR_DONE_ONLY if enabled: flags |= lib.SR_SUBSCR_ENABLED + if update: + flags |= lib.SR_SUBSCR_UPDATE if oper_merge: flags |= lib.SR_SUBSCR_OPER_MERGE if filter_origin: From 16698b89dfb5a9d04a0c9c47ac6d2f617faa69dc Mon Sep 17 00:00:00 2001 From: Jeremie Leska Date: Fri, 14 Feb 2025 14:30:55 +0100 Subject: [PATCH 2/2] tests: add update parameter to subscription Modify 2 subscriptions to test the update parameter. The module_change_cb is called on sysrepo update events. Signed-off-by: Jeremie Leska --- tests/test_subs_module_change.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_subs_module_change.py b/tests/test_subs_module_change.py index 79b64c1..19ddee3 100644 --- a/tests/test_subs_module_change.py +++ b/tests/test_subs_module_change.py @@ -45,7 +45,7 @@ def test_module_change_sub(self): expected_changes = [] def module_change_cb(event, req_id, changes, private_data): - self.assertIn(event, ("change", "done", "abort")) + self.assertIn(event, ("change", "done", "abort", "update")) self.assertIs(private_data, priv) for c in changes: if c.xpath == "/sysrepo-example:conf/system/hostname": @@ -61,6 +61,7 @@ def module_change_cb(event, req_id, changes, private_data): "/sysrepo-example:conf", module_change_cb, private_data=priv, + update=True, ) with self.conn.start_session("running") as ch_sess: @@ -316,7 +317,7 @@ def test_module_change_sub_unsafe(self): def module_change_cb(session, event, req_id, private_data): self.assertIsInstance(session, SysrepoSession) - self.assertIn(event, ("change", "done", "abort")) + self.assertIn(event, ("change", "done", "abort", "update")) self.assertIsInstance(req_id, int) self.assertIs(private_data, priv) changes = list(session.get_changes("/sysrepo-example:conf//.")) @@ -334,6 +335,7 @@ def module_change_cb(session, event, req_id, private_data): "/sysrepo-example:conf", module_change_cb, private_data=priv, + update=True, ) with self.conn.start_session("running") as ch_sess: