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/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;