Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 1 | /* |
| 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át | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 7 | #include <boost/algorithm/string/predicate.hpp> |
Václav Kubernát | 2edfe54 | 2021-02-03 08:08:29 +0100 | [diff] [blame] | 8 | #include "UniqueResource.hpp" |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 9 | #include "proxy_datastore.hpp" |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 10 | #include "yang_schema.hpp" |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 11 | |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 12 | ProxyDatastore::ProxyDatastore(const std::shared_ptr<DatastoreAccess>& datastore, std::function<std::shared_ptr<DatastoreAccess>(const std::shared_ptr<DatastoreAccess>&)> createTemporaryDatastore) |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 13 | : m_datastore(datastore) |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 14 | , m_createTemporaryDatastore(createTemporaryDatastore) |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 15 | { |
| 16 | } |
| 17 | |
| 18 | DatastoreAccess::Tree ProxyDatastore::getItems(const std::string& path) const |
| 19 | { |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 20 | return pickDatastore(path)->getItems(path); |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | void ProxyDatastore::setLeaf(const std::string& path, leaf_data_ value) |
| 24 | { |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 25 | pickDatastore(path)->setLeaf(path, value); |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | void ProxyDatastore::createItem(const std::string& path) |
| 29 | { |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 30 | pickDatastore(path)->createItem(path); |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void ProxyDatastore::deleteItem(const std::string& path) |
| 34 | { |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 35 | pickDatastore(path)->deleteItem(path); |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void ProxyDatastore::moveItem(const std::string& source, std::variant<yang::move::Absolute, yang::move::Relative> move) |
| 39 | { |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 40 | pickDatastore(source)->moveItem(source, move); |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void ProxyDatastore::commitChanges() |
| 44 | { |
| 45 | m_datastore->commitChanges(); |
| 46 | } |
| 47 | |
| 48 | void ProxyDatastore::discardChanges() |
| 49 | { |
| 50 | m_datastore->discardChanges(); |
| 51 | } |
| 52 | |
| 53 | void ProxyDatastore::copyConfig(const Datastore source, const Datastore destination) |
| 54 | { |
| 55 | m_datastore->copyConfig(source, destination); |
| 56 | } |
| 57 | |
| 58 | std::string ProxyDatastore::dump(const DataFormat format) const |
| 59 | { |
| 60 | return m_datastore->dump(format); |
| 61 | } |
| 62 | |
Václav Kubernát | b3960f8 | 2020-12-01 03:21:48 +0100 | [diff] [blame] | 63 | void ProxyDatastore::initiate(const std::string& path) |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 64 | { |
| 65 | if (m_inputDatastore) { |
Václav Kubernát | 2bed952 | 2020-12-01 03:37:41 +0100 | [diff] [blame] | 66 | throw std::runtime_error("RPC/action input already in progress (" + *m_inputPath + ")"); |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 67 | } |
| 68 | m_inputDatastore = m_createTemporaryDatastore(m_datastore); |
Václav Kubernát | b3960f8 | 2020-12-01 03:21:48 +0100 | [diff] [blame] | 69 | m_inputPath = path; |
| 70 | m_inputDatastore->createItem(path); |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | DatastoreAccess::Tree ProxyDatastore::execute() |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 74 | { |
| 75 | if (!m_inputDatastore) { |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame] | 76 | throw std::runtime_error("No RPC/action input in progress"); |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 77 | } |
Václav Kubernát | 2edfe54 | 2021-02-03 08:08:29 +0100 | [diff] [blame] | 78 | auto cancelOnReturn = make_unique_resource([] {}, [this] { cancel(); }); |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 79 | auto inputData = m_inputDatastore->getItems("/"); |
Václav Kubernát | 2bed952 | 2020-12-01 03:37:41 +0100 | [diff] [blame] | 80 | |
| 81 | auto out = m_datastore->execute(*m_inputPath, inputData); |
Václav Kubernát | 2bed952 | 2020-12-01 03:37:41 +0100 | [diff] [blame] | 82 | |
| 83 | return out; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame] | 86 | void ProxyDatastore::cancel() |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 87 | { |
| 88 | m_inputDatastore = nullptr; |
Václav Kubernát | 2bed952 | 2020-12-01 03:37:41 +0100 | [diff] [blame] | 89 | m_inputPath = std::nullopt; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 92 | std::shared_ptr<Schema> ProxyDatastore::schema() const |
| 93 | { |
| 94 | return m_datastore->schema(); |
| 95 | } |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 96 | |
Václav Kubernát | 2bed952 | 2020-12-01 03:37:41 +0100 | [diff] [blame] | 97 | std::optional<std::string> ProxyDatastore::inputDatastorePath() |
| 98 | { |
| 99 | return m_inputPath; |
| 100 | } |
| 101 | |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 102 | std::shared_ptr<DatastoreAccess> ProxyDatastore::pickDatastore(const std::string& path) const |
| 103 | { |
Václav Kubernát | 2bed952 | 2020-12-01 03:37:41 +0100 | [diff] [blame] | 104 | if (!m_inputDatastore || !boost::starts_with(path, *m_inputPath)) { |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 105 | return m_datastore; |
| 106 | } else { |
| 107 | return m_inputDatastore; |
| 108 | } |
| 109 | } |
Václav Kubernát | 162165e | 2021-02-22 09:46:53 +0100 | [diff] [blame] | 110 | |
Václav Kubernát | e066fe2 | 2022-04-06 00:32:26 +0200 | [diff] [blame] | 111 | DatastoreTarget ProxyDatastore::target() const |
| 112 | { |
| 113 | return m_datastore->target(); |
| 114 | } |
| 115 | |
Václav Kubernát | 162165e | 2021-02-22 09:46:53 +0100 | [diff] [blame] | 116 | void ProxyDatastore::setTarget(const DatastoreTarget target) |
| 117 | { |
| 118 | m_datastore->setTarget(target); |
| 119 | } |