blob: 67a3cb3427d88d000aa76b919c82b0c72728a0b2 [file] [log] [blame]
Václav Kubernát48e9dfa2020-07-08 10:55:12 +02001/*
2 * Copyright (C) 2020 CESNET, https://photonics.cesnet.cz/
3 *
4 * Written by Václav Kubernát <kubernat@cesnet.cz>
5 *
6*/
Václav Kubernáte7248b22020-06-26 15:38:59 +02007#include <boost/algorithm/string/predicate.hpp>
Václav Kubernát2edfe542021-02-03 08:08:29 +01008#include "UniqueResource.hpp"
Václav Kubernát48e9dfa2020-07-08 10:55:12 +02009#include "proxy_datastore.hpp"
Václav Kubernáte7248b22020-06-26 15:38:59 +020010#include "yang_schema.hpp"
Václav Kubernát48e9dfa2020-07-08 10:55:12 +020011
Václav Kubernáte7248b22020-06-26 15:38:59 +020012ProxyDatastore::ProxyDatastore(const std::shared_ptr<DatastoreAccess>& datastore, std::function<std::shared_ptr<DatastoreAccess>(const std::shared_ptr<DatastoreAccess>&)> createTemporaryDatastore)
Václav Kubernát48e9dfa2020-07-08 10:55:12 +020013 : m_datastore(datastore)
Václav Kubernáte7248b22020-06-26 15:38:59 +020014 , m_createTemporaryDatastore(createTemporaryDatastore)
Václav Kubernát48e9dfa2020-07-08 10:55:12 +020015{
16}
17
18DatastoreAccess::Tree ProxyDatastore::getItems(const std::string& path) const
19{
Václav Kubernáte7248b22020-06-26 15:38:59 +020020 return pickDatastore(path)->getItems(path);
Václav Kubernát48e9dfa2020-07-08 10:55:12 +020021}
22
23void ProxyDatastore::setLeaf(const std::string& path, leaf_data_ value)
24{
Václav Kubernáte7248b22020-06-26 15:38:59 +020025 pickDatastore(path)->setLeaf(path, value);
Václav Kubernát48e9dfa2020-07-08 10:55:12 +020026}
27
28void ProxyDatastore::createItem(const std::string& path)
29{
Václav Kubernáte7248b22020-06-26 15:38:59 +020030 pickDatastore(path)->createItem(path);
Václav Kubernát48e9dfa2020-07-08 10:55:12 +020031}
32
33void ProxyDatastore::deleteItem(const std::string& path)
34{
Václav Kubernáte7248b22020-06-26 15:38:59 +020035 pickDatastore(path)->deleteItem(path);
Václav Kubernát48e9dfa2020-07-08 10:55:12 +020036}
37
38void ProxyDatastore::moveItem(const std::string& source, std::variant<yang::move::Absolute, yang::move::Relative> move)
39{
Václav Kubernáte7248b22020-06-26 15:38:59 +020040 pickDatastore(source)->moveItem(source, move);
Václav Kubernát48e9dfa2020-07-08 10:55:12 +020041}
42
43void ProxyDatastore::commitChanges()
44{
45 m_datastore->commitChanges();
46}
47
48void ProxyDatastore::discardChanges()
49{
50 m_datastore->discardChanges();
51}
52
53void ProxyDatastore::copyConfig(const Datastore source, const Datastore destination)
54{
55 m_datastore->copyConfig(source, destination);
56}
57
58std::string ProxyDatastore::dump(const DataFormat format) const
59{
60 return m_datastore->dump(format);
61}
62
Václav Kubernátb3960f82020-12-01 03:21:48 +010063void ProxyDatastore::initiate(const std::string& path)
Václav Kubernáte7248b22020-06-26 15:38:59 +020064{
65 if (m_inputDatastore) {
Václav Kubernát2bed9522020-12-01 03:37:41 +010066 throw std::runtime_error("RPC/action input already in progress (" + *m_inputPath + ")");
Václav Kubernáte7248b22020-06-26 15:38:59 +020067 }
68 m_inputDatastore = m_createTemporaryDatastore(m_datastore);
Václav Kubernátb3960f82020-12-01 03:21:48 +010069 m_inputPath = path;
70 m_inputDatastore->createItem(path);
Václav Kubernátaa4250a2020-07-22 00:02:23 +020071}
72
73DatastoreAccess::Tree ProxyDatastore::execute()
Václav Kubernáte7248b22020-06-26 15:38:59 +020074{
75 if (!m_inputDatastore) {
Václav Kubernátaa4250a2020-07-22 00:02:23 +020076 throw std::runtime_error("No RPC/action input in progress");
Václav Kubernáte7248b22020-06-26 15:38:59 +020077 }
Václav Kubernát2edfe542021-02-03 08:08:29 +010078 auto cancelOnReturn = make_unique_resource([] {}, [this] { cancel(); });
Václav Kubernáte7248b22020-06-26 15:38:59 +020079 auto inputData = m_inputDatastore->getItems("/");
Václav Kubernát2bed9522020-12-01 03:37:41 +010080
81 auto out = m_datastore->execute(*m_inputPath, inputData);
Václav Kubernát2bed9522020-12-01 03:37:41 +010082
83 return out;
Václav Kubernáte7248b22020-06-26 15:38:59 +020084}
85
Václav Kubernátaa4250a2020-07-22 00:02:23 +020086void ProxyDatastore::cancel()
Václav Kubernáte7248b22020-06-26 15:38:59 +020087{
88 m_inputDatastore = nullptr;
Václav Kubernát2bed9522020-12-01 03:37:41 +010089 m_inputPath = std::nullopt;
Václav Kubernáte7248b22020-06-26 15:38:59 +020090}
91
Václav Kubernát48e9dfa2020-07-08 10:55:12 +020092std::shared_ptr<Schema> ProxyDatastore::schema() const
93{
94 return m_datastore->schema();
95}
Václav Kubernáte7248b22020-06-26 15:38:59 +020096
Václav Kubernát2bed9522020-12-01 03:37:41 +010097std::optional<std::string> ProxyDatastore::inputDatastorePath()
98{
99 return m_inputPath;
100}
101
Václav Kubernáte7248b22020-06-26 15:38:59 +0200102std::shared_ptr<DatastoreAccess> ProxyDatastore::pickDatastore(const std::string& path) const
103{
Václav Kubernát2bed9522020-12-01 03:37:41 +0100104 if (!m_inputDatastore || !boost::starts_with(path, *m_inputPath)) {
Václav Kubernáte7248b22020-06-26 15:38:59 +0200105 return m_datastore;
106 } else {
107 return m_inputDatastore;
108 }
109}
Václav Kubernát162165e2021-02-22 09:46:53 +0100110
Václav Kubernáte066fe22022-04-06 00:32:26 +0200111DatastoreTarget ProxyDatastore::target() const
112{
113 return m_datastore->target();
114}
115
Václav Kubernát162165e2021-02-22 09:46:53 +0100116void ProxyDatastore::setTarget(const DatastoreTarget target)
117{
118 m_datastore->setTarget(target);
119}