From 7d6b1a463fdacefaf7a7057564f6f4d4a8cb4632 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Pecka?= <tomas.pecka@cesnet.cz>
Date: Tue, 25 Mar 2025 11:06:26 +0100
Subject: [PATCH 35/38] restconf: refactor getting datastore from string
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Organization: Wires

This will be useful when processing YANG push requests.

Change-Id: I1e76335c8d674e81890d6cd1e973adaf521eb205
Signed-off-by: Mattias Walström <lazzer@gmail.com>
---
 src/restconf/uri.cpp           | 19 +++++--------------
 src/restconf/utils/sysrepo.cpp | 16 ++++++++++++++++
 src/restconf/utils/sysrepo.h   |  1 +
 3 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/src/restconf/uri.cpp b/src/restconf/uri.cpp
index 99c3d1f..6143435 100644
--- a/src/restconf/uri.cpp
+++ b/src/restconf/uri.cpp
@@ -17,6 +17,7 @@
 #include "restconf/Exceptions.h"
 #include "restconf/uri.h"
 #include "restconf/uri_impl.h"
+#include "restconf/utils/sysrepo.h"
 #include "restconf/utils/yang.h"
 
 using namespace std::string_literals;
@@ -298,21 +299,11 @@ std::optional<sysrepo::Datastore> datastoreFromApiIdentifier(const boost::option
         return std::nullopt;
     }
 
-    if (*datastore->prefix == "ietf-datastores") {
-        if (datastore->identifier == "running") {
-            return sysrepo::Datastore::Running;
-        } else if (datastore->identifier == "operational") {
-            return sysrepo::Datastore::Operational;
-        } else if (datastore->identifier == "candidate") {
-            return sysrepo::Datastore::Candidate;
-        } else if (datastore->identifier == "startup") {
-            return sysrepo::Datastore::Startup;
-        } else if (datastore->identifier == "factory-default") {
-            return sysrepo::Datastore::FactoryDefault;
-        }
+    try {
+        return datastoreFromString(*datastore->prefix + ":"s + datastore->identifier);
+    } catch (const std::runtime_error&) {
+        throw ErrorResponse(400, "application", "operation-failed", "Unsupported datastore " + *datastore->prefix + ":" + datastore->identifier);
     }
-
-    throw ErrorResponse(400, "application", "operation-failed", "Unsupported datastore " + *datastore->prefix + ":" + datastore->identifier);
 }
 
 std::optional<std::variant<libyang::Module, libyang::SubmoduleParsed>> getModuleOrSubmodule(const libyang::Context& ctx, const std::string& name, const std::optional<std::string>& revision)
diff --git a/src/restconf/utils/sysrepo.cpp b/src/restconf/utils/sysrepo.cpp
index 622ab27..9f14cc7 100644
--- a/src/restconf/utils/sysrepo.cpp
+++ b/src/restconf/utils/sysrepo.cpp
@@ -22,4 +22,20 @@ ScopedDatastoreSwitch::~ScopedDatastoreSwitch()
     m_session.switchDatastore(m_oldDatastore);
 }
 
+sysrepo::Datastore datastoreFromString(const std::string& datastore)
+{
+    if (datastore == "ietf-datastores:running") {
+        return sysrepo::Datastore::Running;
+    } else if (datastore == "ietf-datastores:operational") {
+        return sysrepo::Datastore::Operational;
+    } else if (datastore == "ietf-datastores:candidate") {
+        return sysrepo::Datastore::Candidate;
+    } else if (datastore == "ietf-datastores:startup") {
+        return sysrepo::Datastore::Startup;
+    } else if (datastore == "ietf-datastores:factory-default") {
+        return sysrepo::Datastore::FactoryDefault;
+    }
+
+    throw std::runtime_error("Unknown datastore '" + datastore + "'");
+}
 }
diff --git a/src/restconf/utils/sysrepo.h b/src/restconf/utils/sysrepo.h
index da6c359..0eced9e 100644
--- a/src/restconf/utils/sysrepo.h
+++ b/src/restconf/utils/sysrepo.h
@@ -26,4 +26,5 @@ public:
     ScopedDatastoreSwitch& operator=(ScopedDatastoreSwitch&&) = delete;
 };
 
+sysrepo::Datastore datastoreFromString(const std::string& datastore);
 }
-- 
2.43.0

