Skip to content

Commit b960058

Browse files
committed
add excluded changes to YANG push on-change wrapper
Change-Id: Idb7bb48a3c60ff70286441cd4d0a5c1c28acee15
1 parent 9d81220 commit b960058

File tree

5 files changed

+92
-1
lines changed

5 files changed

+92
-1
lines changed

include/sysrepo-cpp/Enum.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,18 @@ constexpr GetOptions operator|(const GetOptions a, const GetOptions b)
209209
return implEnumBitOr(a, b);
210210
}
211211

212+
/**
213+
* @brief Wraps `srsn_yang_push_change_t`
214+
*/
215+
enum class YangPushChange : uint32_t {
216+
Create = 0, /**< SRSN_YP_CHANGE_CREATE */
217+
Delete = 1, /**< SRSN_YP_CHANGE_DELETE */
218+
Insert = 2, /**< SRSN_YP_CHANGE_INSERT */
219+
Move = 3, /**< SRSN_YP_CHANGE_MOVE */
220+
Replace = 4, /**< SRSN_YP_CHANGE_REPLACE */
221+
EnumCount = 5, /**< SRSN_COUNT_YP_CHANGE */
222+
};
223+
212224
std::ostream& operator<<(std::ostream& os, const NotificationType& type);
213225
std::ostream& operator<<(std::ostream& os, const Event& event);
214226
std::ostream& operator<<(std::ostream& os, const ChangeOperation& changeOp);

include/sysrepo-cpp/Session.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ class Session {
145145
const std::optional<std::variant<std::string, libyang::DataNodeAny>>& filter,
146146
const std::optional<std::chrono::milliseconds>& dampeningPeriod = std::nullopt,
147147
SyncOnStart syncOnStart = SyncOnStart::No,
148+
const std::set<YangPushChange>& excludedChanges = {},
148149
const std::optional<NotificationTimeStamp>& stopTime = std::nullopt);
149150
[[nodiscard]] DynamicSubscription subscribeNotifications(
150151
const std::optional<std::variant<std::string, libyang::DataNodeAny>>& filter,

src/Session.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,19 +607,29 @@ DynamicSubscription Session::yangPushOnChange(
607607
const std::optional<std::variant<std::string, libyang::DataNodeAny>>& filter,
608608
const std::optional<std::chrono::milliseconds>& dampeningPeriod,
609609
SyncOnStart syncOnStart,
610+
const std::set<YangPushChange>& excludedChanges,
610611
const std::optional<NotificationTimeStamp>& stopTime)
611612
{
612613
int fd;
613614
uint32_t subId;
614615
auto stopSpec = stopTime ? std::optional{toTimespec(*stopTime)} : std::nullopt;
615616
auto xpathFilter = constructXPathFilter(filter);
616617

618+
/* The enum values are not used as the other enum flags in sysrepo-cpp.
619+
* srsn_yang_push_on_change expects an integer array of size EnumCount with 0 or 1 values.
620+
*/
621+
using YangPushChangeUnderlying = std::underlying_type_t<YangPushChange>;
622+
std::array<int, static_cast<YangPushChangeUnderlying>(YangPushChange::EnumCount)> excludedChangesArray{};
623+
for (const auto& change: excludedChanges) {
624+
excludedChangesArray[static_cast<YangPushChangeUnderlying>(change)] = 1;
625+
}
626+
617627
auto res = srsn_yang_push_on_change(m_sess.get(),
618628
toDatastore(activeDatastore()),
619629
xpathFilter ? xpathFilter->c_str() : nullptr,
620630
dampeningPeriod ? dampeningPeriod->count() : 0,
621631
syncOnStart == SyncOnStart::Yes,
622-
nullptr,
632+
excludedChangesArray.data(),
623633
stopSpec ? &stopSpec.value() : nullptr,
624634
0,
625635
nullptr,

src/utils/enum.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <type_traits>
1010
extern "C" {
1111
#include <sysrepo.h>
12+
#include <sysrepo/subscribed_notifications.h>
1213
}
1314
#include <sysrepo-cpp/Enum.hpp>
1415

@@ -171,4 +172,11 @@ constexpr sr_get_options_t toGetOptions(const GetOptions opts)
171172
{
172173
return static_cast<sr_get_options_t>(opts);
173174
}
175+
176+
static_assert(static_cast<YangPushChange>(SRSN_YP_CHANGE_CREATE) == YangPushChange::Create);
177+
static_assert(static_cast<YangPushChange>(SRSN_YP_CHANGE_DELETE) == YangPushChange::Delete);
178+
static_assert(static_cast<YangPushChange>(SRSN_YP_CHANGE_INSERT) == YangPushChange::Insert);
179+
static_assert(static_cast<YangPushChange>(SRSN_YP_CHANGE_MOVE) == YangPushChange::Move);
180+
static_assert(static_cast<YangPushChange>(SRSN_YP_CHANGE_REPLACE) == YangPushChange::Replace);
181+
static_assert(static_cast<YangPushChange>(SRSN_COUNT_YP_CHANGE) == YangPushChange::EnumCount);
174182
}

tests/subscriptions-dynamic.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,66 @@ TEST_CASE("Dynamic subscriptions")
497497
sub.terminate();
498498
REQUIRE_PIPE_HANGUP(sub);
499499
}
500+
501+
DOCTEST_SUBCASE("Excluded changes")
502+
{
503+
auto sub = sess.yangPushOnChange(std::nullopt, std::nullopt, sysrepo::SyncOnStart::No, {sysrepo::YangPushChange::Create});
504+
505+
client.setItem("/test_module:leafInt32", "123");
506+
client.applyChanges(); // excluded (create)
507+
client.setItem("/test_module:leafInt32", "124");
508+
client.applyChanges();
509+
client.setItem("/test_module:leafInt32", "125");
510+
client.applyChanges();
511+
512+
REQUIRE_YANG_PUSH_UPDATE(sub, R"({
513+
"ietf-yang-push:push-change-update": {
514+
"datastore-changes": {
515+
"yang-patch": {
516+
"patch-id": "patch-1",
517+
"edit": [
518+
{
519+
"edit-id": "edit-1",
520+
"operation": "replace",
521+
"target": "/test_module:leafInt32",
522+
"value": {
523+
"test_module:leafInt32": 124
524+
}
525+
}
526+
]
527+
}
528+
}
529+
}
530+
}
531+
)");
532+
533+
READ_YANG_PUSH_UPDATE(sub);
534+
535+
REQUIRE_YANG_PUSH_UPDATE(sub, R"({
536+
"ietf-yang-push:push-change-update": {
537+
"datastore-changes": {
538+
"yang-patch": {
539+
"patch-id": "patch-2",
540+
"edit": [
541+
{
542+
"edit-id": "edit-1",
543+
"operation": "replace",
544+
"target": "/test_module:leafInt32",
545+
"value": {
546+
"test_module:leafInt32": 125
547+
}
548+
}
549+
]
550+
}
551+
}
552+
}
553+
}
554+
)");
555+
READ_YANG_PUSH_UPDATE(sub);
556+
557+
sub.terminate();
558+
REQUIRE_PIPE_HANGUP(sub);
559+
}
500560
}
501561

502562
DOCTEST_SUBCASE("YANG Push periodic")

0 commit comments

Comments
 (0)