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 | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 8 | #include "proxy_datastore.hpp" |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 9 | #include "yang_schema.hpp" |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 10 | |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 11 | 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] | 12 | : m_datastore(datastore) |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 13 | , m_createTemporaryDatastore(createTemporaryDatastore) |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 14 | { |
| 15 | } |
| 16 | |
| 17 | DatastoreAccess::Tree ProxyDatastore::getItems(const std::string& path) const |
| 18 | { |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 19 | return pickDatastore(path)->getItems(path); |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | void ProxyDatastore::setLeaf(const std::string& path, leaf_data_ value) |
| 23 | { |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 24 | pickDatastore(path)->setLeaf(path, value); |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | void ProxyDatastore::createItem(const std::string& path) |
| 28 | { |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 29 | pickDatastore(path)->createItem(path); |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | void ProxyDatastore::deleteItem(const std::string& path) |
| 33 | { |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 34 | pickDatastore(path)->deleteItem(path); |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void ProxyDatastore::moveItem(const std::string& source, std::variant<yang::move::Absolute, yang::move::Relative> move) |
| 38 | { |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 39 | pickDatastore(source)->moveItem(source, move); |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void ProxyDatastore::commitChanges() |
| 43 | { |
| 44 | m_datastore->commitChanges(); |
| 45 | } |
| 46 | |
| 47 | void ProxyDatastore::discardChanges() |
| 48 | { |
| 49 | m_datastore->discardChanges(); |
| 50 | } |
| 51 | |
| 52 | void ProxyDatastore::copyConfig(const Datastore source, const Datastore destination) |
| 53 | { |
| 54 | m_datastore->copyConfig(source, destination); |
| 55 | } |
| 56 | |
| 57 | std::string ProxyDatastore::dump(const DataFormat format) const |
| 58 | { |
| 59 | return m_datastore->dump(format); |
| 60 | } |
| 61 | |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame^] | 62 | namespace { |
| 63 | struct getInputPath |
| 64 | { |
| 65 | template<typename InputType> |
| 66 | auto operator()(const InputType& input) |
| 67 | { |
| 68 | return input.m_path; |
| 69 | } |
| 70 | }; |
| 71 | } |
| 72 | |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 73 | void ProxyDatastore::initiateRpc(const std::string& rpcPath) |
| 74 | { |
| 75 | if (m_inputDatastore) { |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame^] | 76 | throw std::runtime_error("RPC/action input already in progress (" + std::visit(getInputPath{}, m_inputPath) + ")"); |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 77 | } |
| 78 | m_inputDatastore = m_createTemporaryDatastore(m_datastore); |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame^] | 79 | m_inputPath = RpcInput{rpcPath}; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 80 | m_inputDatastore->createItem(rpcPath); |
| 81 | } |
| 82 | |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame^] | 83 | void ProxyDatastore::initiateAction(const std::string& actionPath) |
| 84 | { |
| 85 | if (m_inputDatastore) { |
| 86 | throw std::runtime_error("RPC/action input already in progress (" + std::visit(getInputPath{}, m_inputPath) + ")"); |
| 87 | } |
| 88 | m_inputDatastore = m_createTemporaryDatastore(m_datastore); |
| 89 | m_inputPath = ActionInput{actionPath}; |
| 90 | m_inputDatastore->createItem(actionPath); |
| 91 | } |
| 92 | |
| 93 | DatastoreAccess::Tree ProxyDatastore::execute() |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 94 | { |
| 95 | if (!m_inputDatastore) { |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame^] | 96 | throw std::runtime_error("No RPC/action input in progress"); |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 97 | } |
| 98 | auto inputData = m_inputDatastore->getItems("/"); |
| 99 | m_inputDatastore = nullptr; |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame^] | 100 | if (std::holds_alternative<RpcInput>(m_inputPath)) { |
| 101 | return m_datastore->executeRpc(std::visit(getInputPath{}, m_inputPath), inputData); |
| 102 | } else { |
| 103 | return m_datastore->executeAction(std::visit(getInputPath{}, m_inputPath), inputData); |
| 104 | } |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 105 | } |
| 106 | |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame^] | 107 | void ProxyDatastore::cancel() |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 108 | { |
| 109 | m_inputDatastore = nullptr; |
| 110 | } |
| 111 | |
Václav Kubernát | 48e9dfa | 2020-07-08 10:55:12 +0200 | [diff] [blame] | 112 | std::shared_ptr<Schema> ProxyDatastore::schema() const |
| 113 | { |
| 114 | return m_datastore->schema(); |
| 115 | } |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 116 | |
| 117 | std::shared_ptr<DatastoreAccess> ProxyDatastore::pickDatastore(const std::string& path) const |
| 118 | { |
Václav Kubernát | aa4250a | 2020-07-22 00:02:23 +0200 | [diff] [blame^] | 119 | if (!m_inputDatastore || !boost::starts_with(path, std::visit(getInputPath{}, m_inputPath))) { |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 120 | return m_datastore; |
| 121 | } else { |
| 122 | return m_inputDatastore; |
| 123 | } |
| 124 | } |