Migrate to libyang2

libnetconf2: getSchema and getConfig were no longer used in netconf-cli,
so I deleted them. They can get readded once the bindings get split into
a separate project.

sysrepo_access: Some sr_val stuff was removed.

YangSchema: type descriptions are not available
            availableNodes returns only input nodes for RPC nodes
            impl_getSchemaNode: no longer disables error printing

libyang: No longer supports leafrefs without the leaf it points to.

Depends-on: https://cesnet-gerrit-czechlight/c/CzechLight/dependencies/+/5171
Depends-on: https://gerrit.cesnet.cz/c/CzechLight/dependencies/+/5171
Change-Id: Ie49381a003a61a7bb028be7b2fa1d9d926ac4e58
diff --git a/tests/cleanup_datastore.bash.in b/tests/cleanup_datastore.bash.in
index f5f71ac..7fdd986 100755
--- a/tests/cleanup_datastore.bash.in
+++ b/tests/cleanup_datastore.bash.in
@@ -28,8 +28,8 @@
     # https://stackoverflow.com/a/41613532
     tail --pid="$NETOPEER_PID" -f /dev/null
 
-    rm "$NETOPEER_SOCKET"
+    rm -f "$NETOPEER_SOCKET"
 fi
 
-rm -r "$SYSREPO_REPOSITORY_PATH"
+rm -rf "$SYSREPO_REPOSITORY_PATH"
 rm -rf "/dev/shm/$SYSREPO_SHM_PREFIX"*
diff --git a/tests/data_query.cpp b/tests/data_query.cpp
index 64250da..642a0e0 100644
--- a/tests/data_query.cpp
+++ b/tests/data_query.cpp
@@ -29,9 +29,8 @@
 {
     trompeloeil::sequence seq1;
     {
-        auto conn = std::make_shared<sysrepo::Connection>();
-        auto sess = std::make_shared<sysrepo::Session>(conn);
-        sess->copy_config(SR_DS_STARTUP, "example-schema", 1000, true);
+        auto sess = sysrepo::Connection{}.sessionStart();
+        sess.copyConfig(sysrepo::Datastore::Startup, "example-schema", std::chrono::milliseconds{1000});
     }
     SysrepoSubscription subscriptionExample("example-schema");
     SysrepoSubscription subscriptionOther("other-module");
diff --git a/tests/datastore_access.cpp b/tests/datastore_access.cpp
index bbcae25..8a7a9f5 100644
--- a/tests/datastore_access.cpp
+++ b/tests/datastore_access.cpp
@@ -8,6 +8,8 @@
 
 #include "trompeloeil_doctest.hpp"
 #include <sysrepo-cpp/Session.hpp>
+#include <sysrepo-cpp/utils/exception.hpp>
+#include <sysrepo-cpp/utils/utils.hpp>
 #include "proxy_datastore.hpp"
 #include "yang_schema.hpp"
 
@@ -15,9 +17,9 @@
 #include "sysrepo_access.hpp"
 using OnInvalidSchemaPathCreate = DatastoreException;
 using OnInvalidSchemaPathDelete = DatastoreException;
-using OnInvalidSchemaPathMove = sysrepo::sysrepo_exception;
+using OnInvalidSchemaPathMove = sysrepo::ErrorWithCode;
 using OnInvalidRpcPath = std::runtime_error;
-using OnInvalidRpcInput = sysrepo::sysrepo_exception;
+using OnInvalidRpcInput = sysrepo::ErrorWithCode;
 using OnKeyNotFound = void;
 using OnExec = void;
 #elif defined(netconf_BACKEND)
@@ -51,7 +53,7 @@
 
 class MockRecorder : public trompeloeil::mock_interface<Recorder> {
 public:
-    IMPLEMENT_MOCK3(write);
+    IMPLEMENT_MOCK5(write);
 };
 
 class MockDataSupplier : public trompeloeil::mock_interface<DataSupplier> {
@@ -73,8 +75,8 @@
         REQUIRE_THROWS_AS(what(), std::logic_error);
     } else if constexpr (std::is_same<Exception, DatastoreException>()) {
         REQUIRE_THROWS_AS(what(), DatastoreException);
-    } else if constexpr (std::is_same<Exception, sysrepo::sysrepo_exception>()) {
-        REQUIRE_THROWS_AS(what(), sysrepo::sysrepo_exception);
+    } else if constexpr (std::is_same<Exception, sysrepo::ErrorWithCode>()) {
+        REQUIRE_THROWS_AS(what(), sysrepo::ErrorWithCode);
     } else {
         static_assert(always_false<Exception>); // https://stackoverflow.com/a/53945549/2245623
     }
@@ -111,17 +113,15 @@
 
 TEST_CASE("setting/getting values")
 {
-    sr_log_stderr(SR_LL_DBG);
+    sysrepo::setLogLevelStderr(sysrepo::LogLevel::Information);
     trompeloeil::sequence seq1;
     MockRecorder mockRunning;
     MockRecorder mockStartup;
-    {
-        auto conn = std::make_shared<sysrepo::Connection>();
-        auto sess = std::make_shared<sysrepo::Session>(conn);
-        sess->copy_config(SR_DS_STARTUP, "example-schema", 1000, true);
-    }
+
+    sysrepo::Connection{}.sessionStart().copyConfig(sysrepo::Datastore::Startup, "example-schema", std::chrono::milliseconds(1000));
+
     SysrepoSubscription subRunning("example-schema", &mockRunning);
-    SysrepoSubscription subStartup("example-schema", &mockStartup, SR_DS_STARTUP);
+    SysrepoSubscription subStartup("example-schema", &mockStartup, sysrepo::Datastore::Startup);
 
 #ifdef sysrepo_BACKEND
     SysrepoAccess datastore;
@@ -139,83 +139,83 @@
 
     SECTION("set leafInt8 to -128")
     {
-        REQUIRE_CALL(mockRunning, write("/example-schema:leafInt8", std::nullopt, "-128"s));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:leafInt8", std::nullopt, "-128"s, std::nullopt));
         datastore.setLeaf("/example-schema:leafInt8", int8_t{-128});
         datastore.commitChanges();
     }
 
     SECTION("set leafInt16 to -32768")
     {
-        REQUIRE_CALL(mockRunning, write("/example-schema:leafInt16", std::nullopt, "-32768"s));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:leafInt16", std::nullopt, "-32768"s, std::nullopt));
         datastore.setLeaf("/example-schema:leafInt16", int16_t{-32768});
         datastore.commitChanges();
     }
 
     SECTION("set leafInt32 to -2147483648")
     {
-        REQUIRE_CALL(mockRunning, write("/example-schema:leafInt32", std::nullopt, "-2147483648"s));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:leafInt32", std::nullopt, "-2147483648"s, std::nullopt));
         datastore.setLeaf("/example-schema:leafInt32", int32_t{-2147483648});
         datastore.commitChanges();
     }
 
     SECTION("set leafInt64 to -50000000000")
     {
-        REQUIRE_CALL(mockRunning, write("/example-schema:leafInt64", std::nullopt, "-50000000000"s));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:leafInt64", std::nullopt, "-50000000000"s, std::nullopt));
         datastore.setLeaf("/example-schema:leafInt64", int64_t{-50000000000});
         datastore.commitChanges();
     }
 
     SECTION("set leafUInt8 to 255")
     {
-        REQUIRE_CALL(mockRunning, write("/example-schema:leafUInt8", std::nullopt, "255"s));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:leafUInt8", std::nullopt, "255"s, std::nullopt));
         datastore.setLeaf("/example-schema:leafUInt8", uint8_t{255});
         datastore.commitChanges();
     }
 
     SECTION("set leafUInt16 to 65535")
     {
-        REQUIRE_CALL(mockRunning, write("/example-schema:leafUInt16", std::nullopt, "65535"s));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:leafUInt16", std::nullopt, "65535"s, std::nullopt));
         datastore.setLeaf("/example-schema:leafUInt16", uint16_t{65535});
         datastore.commitChanges();
     }
 
     SECTION("set leafUInt32 to 4294967295")
     {
-        REQUIRE_CALL(mockRunning, write("/example-schema:leafUInt32", std::nullopt, "4294967295"s));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:leafUInt32", std::nullopt, "4294967295"s, std::nullopt));
         datastore.setLeaf("/example-schema:leafUInt32", uint32_t{4294967295});
         datastore.commitChanges();
     }
 
     SECTION("set leafUInt64 to 50000000000")
     {
-        REQUIRE_CALL(mockRunning, write("/example-schema:leafUInt64", std::nullopt, "50000000000"s));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:leafUInt64", std::nullopt, "50000000000"s, std::nullopt));
         datastore.setLeaf("/example-schema:leafUInt64", uint64_t{50000000000});
         datastore.commitChanges();
     }
 
     SECTION("set leafEnum to coze")
     {
-        REQUIRE_CALL(mockRunning, write("/example-schema:leafEnum", std::nullopt, "coze"s));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:leafEnum", std::nullopt, "coze"s, std::nullopt));
         datastore.setLeaf("/example-schema:leafEnum", enum_{"coze"});
         datastore.commitChanges();
     }
 
     SECTION("set leafDecimal to 123.544")
     {
-        REQUIRE_CALL(mockRunning, write("/example-schema:leafDecimal", std::nullopt, "123.544"s));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:leafDecimal", std::nullopt, "123.544"s, std::nullopt));
         datastore.setLeaf("/example-schema:leafDecimal", 123.544);
         datastore.commitChanges();
     }
 
     SECTION("set a string, then delete it")
     {
-        REQUIRE_CALL(mockRunning, write("/example-schema:leafString", std::nullopt, "blah"s));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:leafString", std::nullopt, "blah"s, std::nullopt));
         datastore.setLeaf("/example-schema:leafString", "blah"s);
         datastore.commitChanges();
         DatastoreAccess::Tree expected{{"/example-schema:leafString", "blah"s}};
         REQUIRE(datastore.getItems("/example-schema:leafString") == expected);
 
-        REQUIRE_CALL(mockRunning, write("/example-schema:leafString", "blah"s, std::nullopt));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Deleted, "/example-schema:leafString", "blah"s, std::nullopt, std::nullopt));
         datastore.deleteItem("/example-schema:leafString");
         datastore.commitChanges();
         expected.clear();
@@ -224,7 +224,7 @@
 
     SECTION("set a string, then set it to something else without commiting")
     {
-        REQUIRE_CALL(mockRunning, write("/example-schema:leafString", std::nullopt, "oops"s));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:leafString", std::nullopt, "oops"s, std::nullopt));
         datastore.setLeaf("/example-schema:leafString", "blah"s);
         datastore.setLeaf("/example-schema:leafString", "oops"s);
         datastore.commitChanges();
@@ -243,7 +243,7 @@
     SECTION("create presence container")
     {
         REQUIRE(datastore.dump(DataFormat::Json).find("example-schema:pContainer") == std::string::npos);
-        REQUIRE_CALL(mockRunning, write("/example-schema:pContainer", std::nullopt, ""s));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:pContainer", std::nullopt, std::nullopt, std::nullopt));
         datastore.createItem("/example-schema:pContainer");
         datastore.commitChanges();
         REQUIRE(datastore.dump(DataFormat::Json).find("example-schema:pContainer") != std::string::npos);
@@ -252,14 +252,14 @@
     SECTION("create/delete a list instance")
     {
         {
-            REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Nguyen']", std::nullopt, ""s));
-            REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Nguyen']/name", std::nullopt, "Nguyen"s));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:person[name='Nguyen']", std::nullopt, std::nullopt, std::nullopt));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:person[name='Nguyen']/name", std::nullopt, "Nguyen"s, std::nullopt));
             datastore.createItem("/example-schema:person[name='Nguyen']");
             datastore.commitChanges();
         }
         {
-            REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Nguyen']", ""s, std::nullopt));
-            REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Nguyen']/name", "Nguyen"s, std::nullopt));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Deleted, "/example-schema:person[name='Nguyen']", std::nullopt, std::nullopt, std::nullopt));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Deleted, "/example-schema:person[name='Nguyen']/name", "Nguyen"s, std::nullopt, std::nullopt));
             datastore.deleteItem("/example-schema:person[name='Nguyen']");
             datastore.commitChanges();
         }
@@ -288,12 +288,12 @@
     SECTION("leafref pointing to a key of a list")
     {
         {
-            REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Dan']", std::nullopt, ""s));
-            REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Dan']/name", std::nullopt, "Dan"s));
-            REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Elfi']", std::nullopt, ""s));
-            REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Elfi']/name", std::nullopt, "Elfi"s));
-            REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Kolafa']", std::nullopt, ""s));
-            REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Kolafa']/name", std::nullopt, "Kolafa"s));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:person[name='Dan']", std::nullopt, std::nullopt, std::nullopt));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:person[name='Dan']/name", std::nullopt, "Dan"s, std::nullopt));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:person[name='Elfi']", std::nullopt, std::nullopt, std::nullopt));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:person[name='Elfi']/name", std::nullopt, "Elfi"s, std::nullopt));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:person[name='Kolafa']", std::nullopt, std::nullopt, std::nullopt));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:person[name='Kolafa']/name", std::nullopt, "Kolafa"s, std::nullopt));
             datastore.createItem("/example-schema:person[name='Dan']");
             datastore.createItem("/example-schema:person[name='Elfi']");
             datastore.createItem("/example-schema:person[name='Kolafa']");
@@ -318,15 +318,16 @@
 
         datastore.setLeaf("/example-schema:bossPerson", value);
         {
-            REQUIRE_CALL(mockRunning, write("/example-schema:bossPerson", std::nullopt, value));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:bossPerson", std::nullopt, value, std::nullopt));
             datastore.commitChanges();
         }
         REQUIRE(datastore.getItems("/example-schema:bossPerson") == DatastoreAccess::Tree{{"/example-schema:bossPerson", value}});
     }
+
     SECTION("bool values get correctly represented as bools")
     {
         {
-            REQUIRE_CALL(mockRunning, write("/example-schema:down", std::nullopt, "true"s));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:down", std::nullopt, "true"s, std::nullopt));
             datastore.setLeaf("/example-schema:down", bool{true});
             datastore.commitChanges();
         }
@@ -338,8 +339,8 @@
     SECTION("getting items from the whole module")
     {
         {
-            REQUIRE_CALL(mockRunning, write("/example-schema:up", std::nullopt, "true"s));
-            REQUIRE_CALL(mockRunning, write("/example-schema:down", std::nullopt, "false"s));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:up", std::nullopt, "true"s, std::nullopt));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:down", std::nullopt, "false"s, std::nullopt));
             datastore.setLeaf("/example-schema:up", bool{true});
             datastore.setLeaf("/example-schema:down", bool{false});
             datastore.commitChanges();
@@ -355,7 +356,7 @@
     SECTION("getItems returns correct datatypes")
     {
         {
-            REQUIRE_CALL(mockRunning, write("/example-schema:leafEnum", std::nullopt, "lol"s));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:leafEnum", std::nullopt, "lol"s, std::nullopt));
             datastore.setLeaf("/example-schema:leafEnum", enum_{"lol"});
             datastore.commitChanges();
         }
@@ -367,12 +368,12 @@
     SECTION("getItems on a list")
     {
         {
-            REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Jan']", std::nullopt, ""s));
-            REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Jan']/name", std::nullopt, "Jan"s));
-            REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Michal']", std::nullopt, ""s));
-            REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Michal']/name", std::nullopt, "Michal"s));
-            REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Petr']", std::nullopt, ""s));
-            REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Petr']/name", std::nullopt, "Petr"s));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:person[name='Jan']", std::nullopt, std::nullopt, std::nullopt));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:person[name='Jan']/name", std::nullopt, "Jan"s, std::nullopt));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:person[name='Michal']", std::nullopt, std::nullopt, std::nullopt));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:person[name='Michal']/name", std::nullopt, "Michal"s, std::nullopt));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:person[name='Petr']", std::nullopt, std::nullopt, std::nullopt));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:person[name='Petr']/name", std::nullopt, "Petr"s, std::nullopt));
             datastore.createItem("/example-schema:person[name='Jan']");
             datastore.createItem("/example-schema:person[name='Michal']");
             datastore.createItem("/example-schema:person[name='Petr']");
@@ -397,7 +398,7 @@
         REQUIRE(datastore.getItems("/example-schema:pContainer") == expected);
 
         {
-            REQUIRE_CALL(mockRunning, write("/example-schema:pContainer", std::nullopt, ""s));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:pContainer", std::nullopt, std::nullopt, std::nullopt));
             datastore.createItem("/example-schema:pContainer");
             datastore.commitChanges();
         }
@@ -408,7 +409,7 @@
 
         // Make sure it's not there after we delete it
         {
-            REQUIRE_CALL(mockRunning, write("/example-schema:pContainer", ""s, std::nullopt));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Deleted, "/example-schema:pContainer", std::nullopt, std::nullopt, std::nullopt));
             datastore.deleteItem("/example-schema:pContainer");
             datastore.commitChanges();
         }
@@ -438,7 +439,7 @@
         // Make sure it's not there before we create it
         REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected);
         {
-            REQUIRE_CALL(mockRunning, write("/example-schema:inventory/stuff", std::nullopt, ""s));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:inventory/stuff", std::nullopt, std::nullopt, std::nullopt));
             datastore.createItem("/example-schema:inventory/stuff");
             datastore.commitChanges();
         }
@@ -447,7 +448,7 @@
         };
         REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected);
         {
-            REQUIRE_CALL(mockRunning, write("/example-schema:inventory/stuff", ""s, std::nullopt));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Deleted, "/example-schema:inventory/stuff", std::nullopt, std::nullopt, std::nullopt));
             datastore.deleteItem("/example-schema:inventory/stuff");
             datastore.commitChanges();
         }
@@ -458,7 +459,7 @@
     SECTION("floats")
     {
         datastore.setLeaf("/example-schema:leafDecimal", 123.4);
-        REQUIRE_CALL(mockRunning, write("/example-schema:leafDecimal", std::nullopt, "123.4"s));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:leafDecimal", std::nullopt, "123.4"s, std::nullopt));
         datastore.commitChanges();
         DatastoreAccess::Tree expected{
             {"/example-schema:leafDecimal", 123.4},
@@ -469,7 +470,7 @@
     SECTION("unions")
     {
         datastore.setLeaf("/example-schema:unionIntString", int32_t{10});
-        REQUIRE_CALL(mockRunning, write("/example-schema:unionIntString", std::nullopt, "10"s));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:unionIntString", std::nullopt, "10"s, std::nullopt));
         datastore.commitChanges();
         DatastoreAccess::Tree expected{
             {"/example-schema:unionIntString", int32_t{10}},
@@ -480,7 +481,7 @@
     SECTION("identityref")
     {
         datastore.setLeaf("/example-schema:beast", identityRef_{"example-schema", "Mammal"});
-        REQUIRE_CALL(mockRunning, write("/example-schema:beast", std::nullopt, "example-schema:Mammal"s));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:beast", std::nullopt, "example-schema:Mammal"s, std::nullopt));
         datastore.commitChanges();
         DatastoreAccess::Tree expected{
             {"/example-schema:beast", identityRef_{"example-schema", "Mammal"}},
@@ -488,7 +489,7 @@
         REQUIRE(datastore.getItems("/example-schema:beast") == expected);
 
         datastore.setLeaf("/example-schema:beast", identityRef_{"Whale"});
-        REQUIRE_CALL(mockRunning, write("/example-schema:beast", "example-schema:Mammal", "example-schema:Whale"s));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Modified, "/example-schema:beast", "example-schema:Mammal", "example-schema:Whale"s, std::nullopt));
         datastore.commitChanges();
         expected = {
             {"/example-schema:beast", identityRef_{"example-schema", "Whale"}},
@@ -499,7 +500,7 @@
     SECTION("binary")
     {
         datastore.setLeaf("/example-schema:blob", binary_{"cHduegByIQ=="s});
-        REQUIRE_CALL(mockRunning, write("/example-schema:blob", std::nullopt, "cHduegByIQ=="s));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:blob", std::nullopt, "cHduegByIQ=="s, std::nullopt));
         datastore.commitChanges();
         DatastoreAccess::Tree expected{
             {"/example-schema:blob", binary_{"cHduegByIQ=="s}},
@@ -510,7 +511,7 @@
     SECTION("empty")
     {
         datastore.setLeaf("/example-schema:dummy", empty_{});
-        REQUIRE_CALL(mockRunning, write("/example-schema:dummy", std::nullopt, ""s));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:dummy", std::nullopt, ""s, std::nullopt));
         datastore.commitChanges();
         DatastoreAccess::Tree expected{
             {"/example-schema:dummy", empty_{}},
@@ -521,7 +522,7 @@
     SECTION("bits")
     {
         datastore.setLeaf("/example-schema:flags", bits_{{"sign", "carry"}});
-        REQUIRE_CALL(mockRunning, write("/example-schema:flags", std::nullopt, "carry sign"s));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:flags", std::nullopt, "carry sign"s, std::nullopt));
         datastore.commitChanges();
         DatastoreAccess::Tree expected{
             {"/example-schema:flags", bits_{{"carry", "sign"}}},
@@ -564,8 +565,8 @@
     SECTION("leaf list")
     {
         DatastoreAccess::Tree expected;
-        REQUIRE_CALL(mockRunning, write("/example-schema:addresses", std::nullopt, "0.0.0.0"s));
-        REQUIRE_CALL(mockRunning, write("/example-schema:addresses", std::nullopt, "127.0.0.1"s));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:addresses[.='0.0.0.0']", std::nullopt, "0.0.0.0"s, std::nullopt));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:addresses[.='127.0.0.1']", std::nullopt, "127.0.0.1"s, std::nullopt));
         datastore.createItem("/example-schema:addresses[.='0.0.0.0']");
         datastore.createItem("/example-schema:addresses[.='127.0.0.1']");
         datastore.commitChanges();
@@ -576,7 +577,7 @@
         };
         REQUIRE(datastore.getItems("/example-schema:addresses") == expected);
 
-        REQUIRE_CALL(mockRunning, write("/example-schema:addresses", "0.0.0.0"s, std::nullopt));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Deleted, "/example-schema:addresses[.='0.0.0.0']", "0.0.0.0"s, std::nullopt, std::nullopt));
         datastore.deleteItem("/example-schema:addresses[.='0.0.0.0']");
         datastore.commitChanges();
         expected = {
@@ -585,7 +586,7 @@
         };
         REQUIRE(datastore.getItems("/example-schema:addresses") == expected);
 
-        REQUIRE_CALL(mockRunning, write("/example-schema:addresses", "127.0.0.1"s, std::nullopt));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Deleted, "/example-schema:addresses[.='127.0.0.1']", "127.0.0.1"s, std::nullopt, std::nullopt));
         datastore.deleteItem("/example-schema:addresses[.='127.0.0.1']");
         datastore.commitChanges();
         expected = {};
@@ -617,12 +618,12 @@
     {
         {
             REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{});
-            REQUIRE_CALL(mockRunning, write("/example-schema:leafInt16", std::nullopt, "123"s));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:leafInt16", std::nullopt, "123"s, std::nullopt));
             datastore.setLeaf("/example-schema:leafInt16", int16_t{123});
             datastore.commitChanges();
         }
         REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{{"/example-schema:leafInt16", int16_t{123}}});
-        REQUIRE_CALL(mockRunning, write("/example-schema:leafInt16", "123"s, std::nullopt));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Deleted, "/example-schema:leafInt16", "123"s, std::nullopt, std::nullopt));
         datastore.copyConfig(Datastore::Startup, Datastore::Running);
         REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{});
     }
@@ -631,13 +632,9 @@
     {
         DatastoreAccess::Tree expected;
         {
-            REQUIRE_CALL(mockRunning, write("/example-schema:protocols", std::nullopt, "http"s));
-            // FIXME: Why no notifications for these??
-            // ... possibly because my subscription doesn't extract it properly?
-            // REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "ftp"s));
-            // REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "pop3"s));
-            REQUIRE_CALL(mockRunning, write("/example-schema:protocols", "http"s, "ftp"s));
-            REQUIRE_CALL(mockRunning, write("/example-schema:protocols", "ftp"s, "pop3"s));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:protocols[.='http']", "", "http"s, std::nullopt));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:protocols[.='ftp']", "http"s, "ftp"s, std::nullopt));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:protocols[.='pop3']", "ftp"s, "pop3"s, std::nullopt));
             datastore.createItem("/example-schema:protocols[.='http']");
             datastore.createItem("/example-schema:protocols[.='ftp']");
             datastore.createItem("/example-schema:protocols[.='pop3']");
@@ -654,7 +651,7 @@
         std::string sourcePath;
         SECTION("begin")
         {
-            REQUIRE_CALL(mockRunning, write("/example-schema:protocols", std::nullopt, "pop3"s));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Moved, "/example-schema:protocols[.='pop3']", ""s, "pop3"s, std::nullopt));
             sourcePath = "/example-schema:protocols[.='pop3']";
             datastore.moveItem(sourcePath, yang::move::Absolute::Begin);
             datastore.commitChanges();
@@ -670,7 +667,16 @@
         SECTION("end")
         {
             sourcePath = "/example-schema:protocols[.='http']";
-            REQUIRE_CALL(mockRunning, write("/example-schema:protocols", "pop3"s, "http"s));
+
+#if defined(yang_BACKEND) || defined(netconf_BACKEND)
+            // Due to the libyang diff algorithm being imperfect, the move operations differ between backends.
+            // The same applies for the stuff below.
+            // https://github.com/sysrepo/sysrepo/issues/2732
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Moved, "/example-schema:protocols[.='ftp']", ""s, "ftp"s, std::nullopt));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Moved, "/example-schema:protocols[.='pop3']", "ftp"s, "pop3"s, std::nullopt));
+#else
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Moved, "/example-schema:protocols[.='http']", "pop3"s, "http"s, std::nullopt));
+#endif
             datastore.moveItem(sourcePath, yang::move::Absolute::End);
             datastore.commitChanges();
             expected = {
@@ -685,7 +691,12 @@
         SECTION("after")
         {
             sourcePath = "/example-schema:protocols[.='http']";
-            REQUIRE_CALL(mockRunning, write("/example-schema:protocols", "ftp"s, "http"s));
+#if defined(yang_BACKEND) || defined(netconf_BACKEND)
+            // see the test for "end" for explanation if this #ifdef
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Moved, "/example-schema:protocols[.='ftp']", ""s, "ftp"s, std::nullopt));
+#else
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Moved, "/example-schema:protocols[.='http']", "ftp"s, "http"s, std::nullopt));
+#endif
             datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::After, {{".", "ftp"s}}});
             datastore.commitChanges();
             expected = {
@@ -700,7 +711,12 @@
         SECTION("before")
         {
             sourcePath = "/example-schema:protocols[.='http']";
-            REQUIRE_CALL(mockRunning, write("/example-schema:protocols", "ftp"s, "http"s));
+#if defined(yang_BACKEND) || defined(netconf_BACKEND)
+            // see the test for "end" for explanation if this #ifdef
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Moved, "/example-schema:protocols[.='ftp']", ""s, "ftp"s, std::nullopt));
+#else
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Moved, "/example-schema:protocols[.='http']", "ftp"s, "http"s, std::nullopt));
+#endif
             datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::Before, {{".", "pop3"s}}});
             datastore.commitChanges();
             expected = {
@@ -725,12 +741,12 @@
     {
         DatastoreAccess::Tree expected;
         {
-            REQUIRE_CALL(mockRunning, write("/example-schema:players[name='John']", std::nullopt, ""s));
-            REQUIRE_CALL(mockRunning, write("/example-schema:players[name='John']/name", std::nullopt, "John"s));
-            REQUIRE_CALL(mockRunning, write("/example-schema:players[name='Eve']", ""s, ""s));
-            REQUIRE_CALL(mockRunning, write("/example-schema:players[name='Eve']/name", std::nullopt, "Eve"s));
-            REQUIRE_CALL(mockRunning, write("/example-schema:players[name='Adam']", ""s, ""s));
-            REQUIRE_CALL(mockRunning, write("/example-schema:players[name='Adam']/name", std::nullopt, "Adam"s));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:players[name='John']", std::nullopt, std::nullopt, ""s));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:players[name='John']/name", std::nullopt, "John"s, std::nullopt));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:players[name='Eve']", std::nullopt, std::nullopt, "[name='John']"));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:players[name='Eve']/name", std::nullopt, "Eve"s, std::nullopt));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:players[name='Adam']", std::nullopt, std::nullopt, "[name='Eve']"));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:players[name='Adam']/name", std::nullopt, "Adam"s, std::nullopt));
             datastore.createItem("/example-schema:players[name='John']");
             datastore.createItem("/example-schema:players[name='Eve']");
             datastore.createItem("/example-schema:players[name='Adam']");
@@ -750,7 +766,7 @@
         SECTION("begin")
         {
             sourcePath = "/example-schema:players[name='Adam']";
-            REQUIRE_CALL(mockRunning, write("/example-schema:players[name='Adam']", std::nullopt, ""s));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Moved, "/example-schema:players[name='Adam']", std::nullopt, std::nullopt, ""s));
             datastore.moveItem(sourcePath, yang::move::Absolute::Begin);
             datastore.commitChanges();
             expected = {
@@ -767,7 +783,14 @@
         SECTION("end")
         {
             sourcePath = "/example-schema:players[name='John']";
-            REQUIRE_CALL(mockRunning, write("/example-schema:players[name='John']", ""s, ""s));
+#if defined(yang_BACKEND) || defined(netconf_BACKEND)
+            // TODO: see TODO comment in leaflist/end
+            // Although these make much less sense
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Moved, "/example-schema:players[name='Eve']", std::nullopt, std::nullopt, ""));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Moved, "/example-schema:players[name='Adam']", std::nullopt, std::nullopt, "[name='Eve']"));
+#else
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Moved, "/example-schema:players[name='John']", std::nullopt, std::nullopt, "[name='Adam']"));
+#endif
             datastore.moveItem(sourcePath, yang::move::Absolute::End);
             datastore.commitChanges();
             expected = {
@@ -784,7 +807,13 @@
         SECTION("after")
         {
             sourcePath = "/example-schema:players[name='John']";
-            REQUIRE_CALL(mockRunning, write("/example-schema:players[name='John']", ""s, ""s));
+#if defined(yang_BACKEND) || defined(netconf_BACKEND)
+            // TODO: see TODO comment in leaflist/end
+            // Although these make much less sense
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Moved, "/example-schema:players[name='Eve']", std::nullopt, std::nullopt, ""));
+#else
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Moved, "/example-schema:players[name='John']", std::nullopt, std::nullopt, "[name='Eve']"));
+#endif
             datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::After, {{"name", "Eve"s}}});
             datastore.commitChanges();
             expected = {
@@ -801,7 +830,13 @@
         SECTION("before")
         {
             sourcePath = "/example-schema:players[name='John']";
-            REQUIRE_CALL(mockRunning, write("/example-schema:players[name='John']", ""s, ""s));
+#if defined(yang_BACKEND) || defined(netconf_BACKEND)
+            // TODO: see TODO comment in leaflist/end
+            // Although these make much less sense
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Moved, "/example-schema:players[name='Eve']", std::nullopt, std::nullopt, ""));
+#else
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Moved, "/example-schema:players[name='John']", std::nullopt, std::nullopt, "[name='Eve']"));
+#endif
             datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::Before, {{"name", "Adam"s}}});
             datastore.commitChanges();
             expected = {
@@ -819,7 +854,7 @@
     SECTION("getting /")
     {
         {
-            REQUIRE_CALL(mockRunning, write("/example-schema:leafInt32", std::nullopt, "64"s));
+            REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:leafInt32", std::nullopt, "64"s, std::nullopt));
             datastore.setLeaf("/example-schema:leafInt32", 64);
             datastore.commitChanges();
         }
@@ -841,9 +876,9 @@
 
     SECTION("two key lists")
     {
-        REQUIRE_CALL(mockRunning, write("/example-schema:point[x='12'][y='10']", std::nullopt, ""s));
-        REQUIRE_CALL(mockRunning, write("/example-schema:point[x='12'][y='10']/x", std::nullopt, "12"s));
-        REQUIRE_CALL(mockRunning, write("/example-schema:point[x='12'][y='10']/y", std::nullopt, "10"s));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:point[x='12'][y='10']", std::nullopt, std::nullopt, std::nullopt));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:point[x='12'][y='10']/x", std::nullopt, "12"s, std::nullopt));
+        REQUIRE_CALL(mockRunning, write(sysrepo::ChangeOperation::Created, "/example-schema:point[x='12'][y='10']/y", std::nullopt, "10"s, std::nullopt));
         datastore.createItem("/example-schema:point[x='12'][y='10']");
         datastore.commitChanges();
         REQUIRE(datastore.dump(DataFormat::Json).find("example-schema:point") != std::string::npos);
@@ -853,69 +888,74 @@
 }
 
 struct ActionCb {
-    int operator()(sysrepo::S_Session session,
-            const char* xpath,
-            [[maybe_unused]] const sysrepo::S_Vals input,
-            [[maybe_unused]] sr_event_t event,
-            [[maybe_unused]] uint32_t request_id,
-            sysrepo::S_Vals_Holder output)
+    sysrepo::ErrorCode operator()(
+        [[maybe_unused]] sysrepo::Session session,
+        [[maybe_unused]] uint32_t subscriptionId,
+        std::string_view xpath,
+        [[maybe_unused]] const libyang::DataNode input,
+        [[maybe_unused]] sysrepo::Event event,
+        [[maybe_unused]] uint32_t requestId,
+        libyang::DataNode output)
     {
-        if (session->get_context()->get_node(nullptr, xpath)->path(LYS_PATH_FIRST_PREFIX) == "/example-schema:ports/shutdown") {
-            auto buf = output->allocate(1);
-            buf->val(0)->set(joinPaths(xpath, "success").c_str(), true);
-            return SR_ERR_OK;
+        if (session.getContext().findPath(xpath.data()).path() == "/example-schema:ports/shutdown") {
+            // `xpath` holds the subscription xpath which won't have list keys. We need the path with list keys and
+            // we'll find that in the input.
+            auto inputPath = input.findXPath("/example-schema:ports/shutdown").front().path();
+            output.newPath(joinPaths(std::string{inputPath}, "success").c_str(), "true", libyang::CreationOptions::Output);
+            return sysrepo::ErrorCode::Ok;
         }
         throw std::runtime_error("unrecognized RPC");
     }
 };
 
 struct RpcCb {
-    int operator()([[maybe_unused]] sysrepo::S_Session session,
-            const char* xpath,
-            const sysrepo::S_Vals input,
-            [[maybe_unused]] sr_event_t event,
-            [[maybe_unused]] uint32_t request_id,
-            sysrepo::S_Vals_Holder output)
+    sysrepo::ErrorCode operator()(
+        [[maybe_unused]] sysrepo::Session session,
+        [[maybe_unused]] uint32_t subscriptionId,
+        std::string_view xpath,
+        const libyang::DataNode input,
+        [[maybe_unused]] sysrepo::Event event,
+        [[maybe_unused]] uint32_t requestId,
+        libyang::DataNode output)
     {
         const auto nukes = "/example-schema:launch-nukes"s;
         if (xpath == "/example-schema:noop"s || xpath == "/example-schema:fire"s) {
-            return SR_ERR_OK;
+            return sysrepo::ErrorCode::Ok;
         }
 
         if (xpath == nukes) {
             uint64_t kilotons = 0;
             bool hasCities = false;
-            for (size_t i = 0; i < input->val_cnt(); ++i) {
-                const auto& val = input->val(i);
-                if (val->xpath() == nukes + "/payload") {
+            for (const auto& inputNode : input.childrenDfs()) {
+                if (inputNode.path() == nukes) {
+                    continue; // ignore, top-level RPC
+                }
+                if (inputNode.path() == nukes + "/payload") {
                     continue; // ignore, container
                 }
-                if (val->xpath() == nukes + "/description") {
+                if (inputNode.path() == nukes + "/description") {
                     continue; // unused
                 }
 
-                if (val->xpath() == nukes + "/payload/kilotons") {
-                    kilotons = val->data()->get_uint64();
-                } else if (std::string_view{val->xpath()}.find(nukes + "/cities") == 0) {
+                if (inputNode.path() == nukes + "/payload/kilotons") {
+                    kilotons = std::get<uint64_t>(inputNode.asTerm().value());
+                } else if (std::string_view{inputNode.path().get().get()}.find(nukes + "/cities") == 0) {
                     hasCities = true;
                 } else {
-                    throw std::runtime_error("RPC launch-nukes: unexpected input "s + val->xpath());
+                    throw std::runtime_error("RPC launch-nukes: unexpected input "s + inputNode.path().get().get());
                 }
             }
             if (kilotons == 333'666) {
                 // magic, just do not generate any output. This is important because the NETCONF RPC returns just <ok/>.
-                return SR_ERR_OK;
+                return sysrepo::ErrorCode::Ok;
             }
-            auto buf = output->allocate(2);
-            size_t i = 0;
-            buf->val(i++)->set((nukes + "/blast-radius").c_str(), uint32_t{33'666});
-            buf->val(i++)->set((nukes + "/actual-yield").c_str(), static_cast<uint64_t>(1.33 * kilotons));
+            output.newPath((nukes + "/blast-radius").c_str(), "33666", libyang::CreationOptions::Output);
+            output.newPath((nukes + "/actual-yield").c_str(), std::to_string(static_cast<uint64_t>(1.33 * kilotons)).c_str(), libyang::CreationOptions::Output);
             if (hasCities) {
-                buf = output->reallocate(output->val_cnt() + 2);
-                buf->val(i++)->set((nukes + "/damaged-places/targets[city='London']/city").c_str(), "London");
-                buf->val(i++)->set((nukes + "/damaged-places/targets[city='Berlin']/city").c_str(), "Berlin");
+                output.newPath((nukes + "/damaged-places/targets[city='London']/city").c_str(), "London", libyang::CreationOptions::Output);
+                output.newPath((nukes + "/damaged-places/targets[city='Berlin']/city").c_str(), "Berlin", libyang::CreationOptions::Output);
             }
-            return SR_ERR_OK;
+            return sysrepo::ErrorCode::Ok;
         }
         throw std::runtime_error("unrecognized RPC");
     }
@@ -938,18 +978,14 @@
 #error "Unknown backend"
 #endif
 
-    auto srConn = std::make_shared<sysrepo::Connection>();
-    auto srSession = std::make_shared<sysrepo::Session>(srConn);
-    auto srSubscription = std::make_shared<sysrepo::Subscribe>(srSession);
-    auto rpcCb = std::make_shared<RpcCb>();
-    auto actionCb = std::make_shared<ActionCb>();
-    sysrepo::Logs{}.set_stderr(SR_LL_INF);
+    sysrepo::setLogLevelStderr(sysrepo::LogLevel::Information);
+
+    auto srSubscription = sysrepo::Connection{}.sessionStart().onRPCAction("/example-schema:noop", RpcCb{});
+    srSubscription.onRPCAction("/example-schema:launch-nukes", RpcCb{});
+    srSubscription.onRPCAction("/example-schema:fire", RpcCb{});
+    srSubscription.onRPCAction("/example-schema:ports/shutdown", ActionCb{});
+
     SysrepoSubscription subscription("example-schema", nullptr);
-    // careful here, sysrepo insists on module_change CBs being registered before RPC CBs, otherwise there's a memleak
-    srSubscription->rpc_subscribe("/example-schema:noop", RpcCb{}, 0, SR_SUBSCR_CTX_REUSE);
-    srSubscription->rpc_subscribe("/example-schema:launch-nukes", RpcCb{}, 0, SR_SUBSCR_CTX_REUSE);
-    srSubscription->rpc_subscribe("/example-schema:fire", RpcCb{}, 0, SR_SUBSCR_CTX_REUSE);
-    srSubscription->rpc_subscribe("/example-schema:ports/shutdown", ActionCb{}, 0, SR_SUBSCR_CTX_REUSE);
 
     SECTION("rpc")
     {
@@ -1086,13 +1122,12 @@
 {
     const auto testNode = "/example-schema:leafInt32";
     {
-        auto conn = std::make_shared<sysrepo::Connection>();
-        auto sess = std::make_shared<sysrepo::Session>(conn);
-        sess->delete_item(testNode);
-        sess->apply_changes(1000, 1);
-        sess->session_switch_ds(SR_DS_STARTUP);
-        sess->delete_item(testNode);
-        sess->apply_changes(1000, 1);
+        auto sess = sysrepo::Connection{}.sessionStart();
+        sess.deleteItem(testNode);
+        sess.applyChanges(std::chrono::milliseconds{1000});
+        sess.switchDatastore(sysrepo::Datastore::Startup);
+        sess.deleteItem(testNode);
+        sess.applyChanges(std::chrono::milliseconds{1000});
     }
     MockRecorder mockRunning;
     MockRecorder mockStartup;
diff --git a/tests/init_datastore.bash.in b/tests/init_datastore.bash.in
index 6eee340..d8eb2c3 100755
--- a/tests/init_datastore.bash.in
+++ b/tests/init_datastore.bash.in
@@ -22,7 +22,7 @@
 shift
 
 # Install the module
-"$SYSREPOCTL" --search-dirs "$YANG_DIR" --install "$MODULE" -a
+"$SYSREPOCTL" --search-dirs "$YANG_DIR" --install "$MODULE" -v3
 
 BACKEND="$1"
 shift
diff --git a/tests/mock/sysrepo_subscription.cpp b/tests/mock/sysrepo_subscription.cpp
index 8faffa5..d4827bb 100644
--- a/tests/mock/sysrepo_subscription.cpp
+++ b/tests/mock/sysrepo_subscription.cpp
@@ -21,29 +21,44 @@
     {
     }
 
-    int operator()(
-        sysrepo::S_Session sess,
-        [[maybe_unused]] const char* module_name,
-        [[maybe_unused]] const char* xpath,
-        [[maybe_unused]] sr_event_t event,
-        [[maybe_unused]] uint32_t request_id)
+    sysrepo::ErrorCode operator()(
+        sysrepo::Session sess,
+        uint32_t /* sub_id */,
+        std::string_view module_name,
+        std::optional<std::string_view> /* sub_xpath */,
+        sysrepo::Event event,
+        uint32_t /* request_id */)
     {
         using namespace std::string_literals;
-        if (event == SR_EV_CHANGE) {
-            return SR_ERR_OK;
+        if (event == sysrepo::Event::Change) {
+            return sysrepo::ErrorCode::Ok;
         }
 
-        auto it = sess->get_changes_iter(("/"s + module_name + ":*//.").c_str());
+        for (const auto& it : sess.getChanges(("/"s + module_name.data() + ":*//.").c_str())) {
+            auto xpath = it.node.path();
+            std::optional<std::string> oldValue;
+            std::optional<std::string> newValue;
+            if (it.operation == sysrepo::ChangeOperation::Deleted) {
+                oldValue = it.node.schema().nodeType() == libyang::NodeType::Leaf || it.node.schema().nodeType() == libyang::NodeType::Leaflist ?
+                    std::optional<std::string>{it.node.asTerm().valueStr()} :
+                    std::nullopt;
+            } else {
+                oldValue = std::optional<std::string>{it.previousValue};
+                newValue = it.node.schema().nodeType() == libyang::NodeType::Leaf || it.node.schema().nodeType() == libyang::NodeType::Leaflist ?
+                    std::optional<std::string>{it.node.asTerm().valueStr()} :
+                    std::nullopt;
 
-        while (auto change = sess->get_change_next(it)) {
-            auto xpath = (change->new_val() ? change->new_val() : change->old_val())->xpath();
+            }
+            std::optional<std::string> previousList;
 
-            auto oldValue = change->old_val() ? std::optional{change->old_val()->val_to_string()} : std::nullopt;
-            auto newValue = change->new_val() ? std::optional{change->new_val()->val_to_string()} : std::nullopt;
-            m_recorder->write(xpath, oldValue, newValue);
+            if (it.previousList) {
+                previousList = std::string{*it.previousList};
+            }
+
+            m_recorder->write(it.operation, std::string{xpath}, oldValue, newValue, previousList);
         }
 
-        return SR_ERR_OK;
+        return sysrepo::ErrorCode::Ok;
     }
 
 private:
@@ -55,111 +70,39 @@
 
 DataSupplier::~DataSupplier() = default;
 
-SysrepoSubscription::SysrepoSubscription(const std::string& moduleName, Recorder* rec, sr_datastore_t ds)
-    : m_connection(std::make_shared<sysrepo::Connection>())
+SysrepoSubscription::SysrepoSubscription(const std::string& moduleName, Recorder* rec, sysrepo::Datastore ds)
+    : m_subscription([&moduleName, &rec, ds] { // This is an immediately invoked lambda.
+        return sysrepo::Connection{}.sessionStart(ds).onModuleChange(moduleName.c_str(),
+                rec ? sysrepo::ModuleChangeCb{MyCallback{moduleName, rec}}
+                : sysrepo::ModuleChangeCb{[](auto, auto, auto, auto, auto, auto) { return sysrepo::ErrorCode::Ok; }});
+    }())
 {
-    m_session = std::make_shared<sysrepo::Session>(m_connection, ds);
-    m_subscription = std::make_shared<sysrepo::Subscribe>(m_session);
-    sysrepo::ModuleChangeCb cb;
-    if (rec) {
-        cb = MyCallback{moduleName, rec};
-    } else {
-        cb = [](auto, auto, auto, auto, auto) { return SR_ERR_OK; };
-    }
-
-    m_subscription->module_change_subscribe(moduleName.c_str(), cb);
 }
 
-
-struct leafDataToSysrepoVal {
-    leafDataToSysrepoVal(sysrepo::S_Val v, const std::string& xpath)
-        : v(v)
-        , xpath(xpath)
-    {
-    }
-
-    void operator()(const binary_& what)
-    {
-        v->set(xpath.c_str(), what.m_value.c_str(), SR_BINARY_T);
-    }
-
-    void operator()(const enum_& what)
-    {
-        v->set(xpath.c_str(), what.m_value.c_str(), SR_ENUM_T);
-    }
-
-    void operator()(const identityRef_& what)
-    {
-        v->set(xpath.c_str(), (what.m_prefix->m_name + what.m_value).c_str(), SR_IDENTITYREF_T);
-    }
-
-    void operator()(const empty_)
-    {
-        v->set(xpath.c_str(), nullptr, SR_LEAF_EMPTY_T);
-    }
-
-    void operator()(const std::string& what)
-    {
-        v->set(xpath.c_str(), what.c_str());
-    }
-
-    void operator()(const bits_& what)
-    {
-        std::stringstream ss;
-        std::copy(what.m_bits.begin(), what.m_bits.end(), std::experimental::make_ostream_joiner(ss, " "));
-        v->set(xpath.c_str(), ss.str().c_str());
-    }
-
-    template <typename Type>
-    void operator()(const Type what)
-    {
-        v->set(xpath.c_str(), what);
-    }
-
-    void operator()([[maybe_unused]] const special_ what)
-    {
-        throw std::logic_error("Attempted to create a SR val from a special_ value");
-    }
-
-    ::sysrepo::S_Val v;
-    std::string xpath;
-};
-
 class OperationalDataCallback {
 public:
     OperationalDataCallback(const DataSupplier& dataSupplier)
         : m_dataSupplier(dataSupplier)
     {
     }
-    int operator()(
-        [[maybe_unused]] sysrepo::S_Session sess,
-        [[maybe_unused]] const char* module_name,
-        const char* path,
-        [[maybe_unused]] const char* request_xpath,
-        [[maybe_unused]] uint32_t request_id,
-        libyang::S_Data_Node& parent)
+    sysrepo::ErrorCode operator()(
+            sysrepo::Session session,
+            [[maybe_unused]] uint32_t subscriptionId,
+            [[maybe_unused]] std::string_view moduleName,
+            std::optional<std::string_view> subXPath,
+            [[maybe_unused]] std::optional<std::string_view> requestXPath,
+            [[maybe_unused]] uint32_t requestId,
+            std::optional<libyang::DataNode>& output)
     {
-        auto data = m_dataSupplier.get_data(path);
-        libyang::S_Data_Node res;
+        auto data = m_dataSupplier.get_data(subXPath->data());
         for (const auto& [p, v] : data) {
-            if (!res) {
-                res = std::make_shared<libyang::Data_Node>(
-                    sess->get_context(),
-                    p.c_str(),
-                    v.type() == typeid(empty_) ? nullptr : leafDataToString(v).c_str(),
-                    LYD_ANYDATA_CONSTSTRING,
-                    0);
+            if (!output) {
+                output = session.getContext().newPath(p.c_str(), v.type() == typeid(empty_) ? nullptr : leafDataToString(v).c_str());
             } else {
-                res->new_path(
-                    sess->get_context(),
-                    p.c_str(),
-                    v.type() == typeid(empty_) ? nullptr : leafDataToString(v).c_str(),
-                    LYD_ANYDATA_CONSTSTRING,
-                    0);
+                output->newPath(p.c_str(), v.type() == typeid(empty_) ? nullptr : leafDataToString(v).c_str());
             }
         }
-        parent = res;
-        return SR_ERR_OK;
+        return sysrepo::ErrorCode::Ok;
     }
 
 private:
@@ -167,9 +110,6 @@
 };
 
 OperationalDataSubscription::OperationalDataSubscription(const std::string& moduleName, const std::string& path, const DataSupplier& dataSupplier)
-    : m_connection(std::make_shared<sysrepo::Connection>())
-    , m_session(std::make_shared<sysrepo::Session>(m_connection))
-    , m_subscription(std::make_shared<sysrepo::Subscribe>(m_session))
+    : m_subscription(sysrepo::Connection{}.sessionStart().onOperGet(moduleName.c_str(), OperationalDataCallback{dataSupplier}, path.c_str()))
 {
-    m_subscription->oper_get_items_subscribe(moduleName.c_str(), OperationalDataCallback{dataSupplier}, path.c_str());
 }
diff --git a/tests/mock/sysrepo_subscription.hpp b/tests/mock/sysrepo_subscription.hpp
index fc2e499..85d048d 100644
--- a/tests/mock/sysrepo_subscription.hpp
+++ b/tests/mock/sysrepo_subscription.hpp
@@ -10,21 +10,15 @@
 
 #include <memory>
 #include <optional>
-#include <sysrepo-cpp/Session.hpp>
+#include <sysrepo-cpp/Connection.hpp>
 #include "datastore_access.hpp"
 
-namespace sysrepo {
-class Callback;
-class Connection;
-class Session;
-class Subscribe;
-}
 class YangSchema;
 
 class Recorder {
 public:
     virtual ~Recorder();
-    virtual void write(const std::string& xpath, const std::optional<std::string>& oldValue, const std::optional<std::string>& newValue) = 0;
+    virtual void write(const sysrepo::ChangeOperation operation, const std::string& xpath, const std::optional<std::string>& oldValue, const std::optional<std::string>& newValue, const std::optional<std::string> previousList) = 0;
 };
 
 class DataSupplier {
@@ -36,13 +30,10 @@
 
 class SysrepoSubscription {
 public:
-    SysrepoSubscription(const std::string& moduleName, Recorder* rec = nullptr, sr_datastore_t ds = SR_DS_RUNNING);
+    SysrepoSubscription(const std::string& moduleName, Recorder* rec = nullptr, sysrepo::Datastore ds = sysrepo::Datastore::Running);
 
 private:
-    std::shared_ptr<sysrepo::Connection> m_connection;
-    std::shared_ptr<sysrepo::Session> m_session;
-    std::shared_ptr<YangSchema> m_schema;
-    std::shared_ptr<sysrepo::Subscribe> m_subscription;
+    sysrepo::Subscription m_subscription;
 };
 
 class OperationalDataSubscription {
@@ -50,8 +41,6 @@
     OperationalDataSubscription(const std::string& moduleName, const std::string& path, const DataSupplier& dataSupplier);
 
 private:
-    std::shared_ptr<sysrepo::Connection> m_connection;
-    std::shared_ptr<sysrepo::Session> m_session;
     std::shared_ptr<YangSchema> m_schema;
-    std::shared_ptr<sysrepo::Subscribe> m_subscription;
+    sysrepo::Subscription m_subscription;
 };
diff --git a/tests/utils.cpp b/tests/utils.cpp
index 187cdf3..c24f4de 100644
--- a/tests/utils.cpp
+++ b/tests/utils.cpp
@@ -7,6 +7,7 @@
 */
 
 #include "trompeloeil_doctest.hpp"
+#include <libyang-cpp/Context.hpp>
 #include "completion.hpp"
 #include "leaf_data_helpers.hpp"
 #include "libyang_utils.hpp"
@@ -206,12 +207,6 @@
         }
     }
 
-    leaf leafRefNonPresent {
-        type leafref {
-            path ../stuff/name;
-        }
-    }
-
     container users {
         config false;
         list userList {
@@ -238,7 +233,7 @@
     "test-schema:enum": "A",
     "test-schema:identityRef": "apple",
     "test-schema:binary": "QUhPSgo=",
-    "test-schema:empty": "",
+    "test-schema:empty": [null],
     "test-schema:bits": "a AHOJ",
     "test-schema:capabilities": "switch hub",
     "test-schema:dec64": "43242.43260",
@@ -248,7 +243,6 @@
         }
     ],
     "test-schema:leafRefPresent": "Xaver",
-    "test-schema:leafRefNonPresent": "Lucas",
     "test-schema:users": {
         "userList": [
             {
@@ -268,9 +262,9 @@
 
 TEST_CASE("libyang_utils")
 {
-    auto ctx = std::make_shared<libyang::Context>();
-    ctx->parse_module_mem(schema, LYS_IN_YANG);
-    auto dataNode = ctx->parse_data_mem(data, LYD_JSON, LYD_OPT_DATA_NO_YANGLIB | LYD_OPT_NOEXTDEPS | LYD_OPT_STRICT);
+    libyang::Context ctx;
+    ctx.parseModuleMem(schema, libyang::SchemaFormat::YANG);
+    auto dataNode = ctx.parseDataMem(data, libyang::DataFormat::JSON, std::nullopt, libyang::ValidationOptions::Present);
 
     SECTION("leafValueFromNode")
     {
@@ -362,16 +356,9 @@
             path = "test-schema:leafRefPresent";
             expectedLeafData = std::string{"Xaver"};
         }
-        SECTION("test-schema:leafRefNonPresent")
-        {
-            path = "test-schema:leafRefNonPresent";
-            expectedLeafData = std::string{"Lucas"};
-        }
 
-        auto leaf = dataNode->find_path(("/" + path).c_str());
-        REQUIRE(leaf->number() == 1);
-        auto firstLeaf = std::make_shared<libyang::Data_Node_Leaf_List>(leaf->data().front());
-        REQUIRE(leafValueFromNode(firstLeaf) == expectedLeafData);
+        auto leaf = dataNode->findPath(("/" + path).c_str());
+        REQUIRE(leafValueFromNode(leaf->asTerm()) == expectedLeafData);
     }
 
     SECTION("lyNodesToTree")
@@ -397,7 +384,6 @@
             {"/test-schema:stuff[name='Xaver']", special_{SpecialValue::List}},
             {"/test-schema:stuff[name='Xaver']/name", std::string{"Xaver"}},
             {"/test-schema:leafRefPresent", std::string{"Xaver"}},
-            {"/test-schema:leafRefNonPresent", std::string{"Lucas"}},
             {"/test-schema:users/userList[1]", special_{SpecialValue::List}},
             {"/test-schema:users/userList[1]/name", std::string{"John"}},
             {"/test-schema:users/userList[2]", special_{SpecialValue::List}},
@@ -407,7 +393,7 @@
         };
 
         DatastoreAccess::Tree tree;
-        lyNodesToTree(tree, {dataNode->tree_for()});
+        lyNodesToTree(tree, dataNode->siblings());
         REQUIRE(tree == expected);
     }
 }
diff --git a/tests/yang.cpp b/tests/yang.cpp
index ed4da88..205616c 100644
--- a/tests/yang.cpp
+++ b/tests/yang.cpp
@@ -683,7 +683,7 @@
                 }
                 SECTION("bigPizzas enabled")
                 {
-                    ys.enableFeature("example-schema", "bigPizzas");
+                    ys.setEnabledFeatures("example-schema", {"bigPizzas"});
                     type = createEnum({"small", "medium", "large"});
                 }
             }
@@ -727,7 +727,7 @@
                 node.first = "example-schema";
                 node.second = "activeNumber";
                 type.emplace<yang::LeafRef>(
-                    "/example-schema:_list/number",
+                    "/_list/number",
                     std::make_unique<yang::TypeInfo>(ys.leafType("/example-schema:_list/number"))
                 );
             }
@@ -744,22 +744,19 @@
                 }
                 SECTION("weird ports enabled")
                 {
-                    ys.enableFeature("example-schema", "weirdPortNames");
+                    ys.setEnabledFeatures("example-schema", {"weirdPortNames"});
                     enums = createEnum({"WEIRD", "utf2", "utf3"});
                 }
 
                 type = yang::Union{{
                     yang::TypeInfo{createEnum({"wlan0", "wlan1"})},
                     yang::TypeInfo{yang::LeafRef{
-                        "/example-schema:portSettings/port",
+                        "../portSettings/port",
                         std::make_unique<yang::TypeInfo>(createEnum({"eth0", "eth1", "eth2"}))
                     }},
                     yang::TypeInfo{yang::LeafRef{
-                        "/example-schema:activeMappedPort",
-                        std::make_unique<yang::TypeInfo>(yang::LeafRef{
-                                "/example-schema:portMapping/port",
-                                std::make_unique<yang::TypeInfo>(enums)
-                        })
+                        "../activeMappedPort",
+                        std::make_unique<yang::TypeInfo>(enums)
                     }},
                     yang::TypeInfo{yang::Empty{}},
                 }};
@@ -939,6 +936,7 @@
                         {"example-schema"s, "zero"},
                         {"example-schema"s, "subLeaf"}
                     };
+
                     expectedRecursive = {
                         {boost::none, "/example-schema:_list"},
                         {boost::none, "/example-schema:_list/contInList"},
@@ -966,10 +964,10 @@
                         {boost::none, "/example-schema:foodDrinkIdentLeaf"},
                         {boost::none, "/example-schema:foodDrinkIdentLeaf"},
                         {boost::none, "/example-schema:foodIdentLeaf"},
-                        {boost::none, "/example-schema:interface/caseEthernet/ethernet"},
-                        {boost::none, "/example-schema:interface/caseEthernet/ethernet/ip"},
-                        {boost::none, "/example-schema:interface/caseLoopback/loopback"},
-                        {boost::none, "/example-schema:interface/caseLoopback/loopback/ip"},
+                        {boost::none, "/example-schema:ethernet"},
+                        {boost::none, "/example-schema:ethernet/ip"},
+                        {boost::none, "/example-schema:loopback"},
+                        {boost::none, "/example-schema:loopback/ip"},
                         {boost::none, "/example-schema:interrupt"},
                         {boost::none, "/example-schema:leafBool"},
                         {boost::none, "/example-schema:leafDecimal"},
@@ -990,16 +988,10 @@
                         {boost::none, "/example-schema:leafUint8"},
                         {boost::none, "/example-schema:length"},
                         {boost::none, "/example-schema:myRpc"},
-                        {boost::none, "/example-schema:myRpc/input"},
-                        {boost::none, "/example-schema:myRpc/output"},
                         {boost::none, "/example-schema:rpcOneOutput"},
-                        {boost::none, "/example-schema:rpcOneOutput/input"},
-                        {boost::none, "/example-schema:rpcOneOutput/output"},
-                        {boost::none, "/example-schema:rpcOneOutput/output/ahoj"},
+                        {boost::none, "/example-schema:rpcOneOutput/ahoj"},
                         {boost::none, "/example-schema:rpcOneInput"},
-                        {boost::none, "/example-schema:rpcOneInput/input"},
-                        {boost::none, "/example-schema:rpcOneInput/input/ahoj"},
-                        {boost::none, "/example-schema:rpcOneInput/output"},
+                        {boost::none, "/example-schema:rpcOneInput/ahoj"},
                         {boost::none, "/example-schema:numberOrString"},
                         {boost::none, "/example-schema:obsoleteLeaf"},
                         {boost::none, "/example-schema:obsoleteLeafWithDeprecatedType"},
@@ -1010,10 +1002,6 @@
                         {boost::none, "/example-schema:portMapping/port"},
                         {boost::none, "/example-schema:portSettings"},
                         {boost::none, "/example-schema:portSettings/port"},
-                        {boost::none, "/example-schema:portSettings/shutdown"},
-                        {boost::none, "/example-schema:portSettings/shutdown/input"},
-                        {boost::none, "/example-schema:portSettings/shutdown/output"},
-                        {boost::none, "/example-schema:portSettings/shutdown/output/success"},
                         {boost::none, "/example-schema:systemStats"},
                         {boost::none, "/example-schema:systemStats/upTime"},
                         {boost::none, "/example-schema:subLeaf"},
@@ -1196,7 +1184,7 @@
 
         SECTION("leafrefPath")
         {
-            REQUIRE(ys.leafrefPath("/example-schema:activeNumber") == "/example-schema:_list/number");
+            REQUIRE(ys.leafrefPath("/example-schema:activeNumber") == "/_list/number");
         }
 
         SECTION("isConfig")
@@ -1217,7 +1205,7 @@
         SECTION("leafTypeName")
         {
             REQUIRE(ys.leafTypeName("/example-schema:leafEnumTypedefRestricted") == "enumTypedef");
-            REQUIRE(ys.leafTypeName("/example-schema:leafInt32") == std::nullopt);
+            REQUIRE(ys.leafTypeName("/example-schema:leafInt32") == "int32");
         }
 
         SECTION("dataPathToSchemaPath")
@@ -1334,14 +1322,14 @@
             REQUIRE_THROWS(ys.nodeType(path, node));
         }
 
-        SECTION("enableFeature - non existing module")
+        SECTION("setEnabledFeatures - non existing module")
         {
-            REQUIRE_THROWS_AS(ys.enableFeature("non-existing", "just-no"), std::runtime_error);
+            REQUIRE_THROWS_AS(ys.setEnabledFeatures("non-existing", {"just-no"}), std::runtime_error);
         }
 
-        SECTION("enableFeature - non existing feature")
+        SECTION("setEnabledFeatures - non existing feature")
         {
-            REQUIRE_THROWS_AS(ys.enableFeature("example-schema", "just-no"), std::runtime_error);
+            REQUIRE_THROWS_AS(ys.setEnabledFeatures("example-schema", {"just-no"}), std::runtime_error);
         }
     }
 }