blob: e6878cb5cef29cb7a770d2bdcacc008303805dbc [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át48e9dfa2020-07-08 10:55:12 +02008#include "proxy_datastore.hpp"
Václav Kubernáte7248b22020-06-26 15:38:59 +02009#include "yang_schema.hpp"
Václav Kubernát48e9dfa2020-07-08 10:55:12 +020010
Václav Kubernáte7248b22020-06-26 15:38:59 +020011ProxyDatastore::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 +020012 : m_datastore(datastore)
Václav Kubernáte7248b22020-06-26 15:38:59 +020013 , m_createTemporaryDatastore(createTemporaryDatastore)
Václav Kubernát48e9dfa2020-07-08 10:55:12 +020014{
15}
16
17DatastoreAccess::Tree ProxyDatastore::getItems(const std::string& path) const
18{
Václav Kubernáte7248b22020-06-26 15:38:59 +020019 return pickDatastore(path)->getItems(path);
Václav Kubernát48e9dfa2020-07-08 10:55:12 +020020}
21
22void ProxyDatastore::setLeaf(const std::string& path, leaf_data_ value)
23{
Václav Kubernáte7248b22020-06-26 15:38:59 +020024 pickDatastore(path)->setLeaf(path, value);
Václav Kubernát48e9dfa2020-07-08 10:55:12 +020025}
26
27void ProxyDatastore::createItem(const std::string& path)
28{
Václav Kubernáte7248b22020-06-26 15:38:59 +020029 pickDatastore(path)->createItem(path);
Václav Kubernát48e9dfa2020-07-08 10:55:12 +020030}
31
32void ProxyDatastore::deleteItem(const std::string& path)
33{
Václav Kubernáte7248b22020-06-26 15:38:59 +020034 pickDatastore(path)->deleteItem(path);
Václav Kubernát48e9dfa2020-07-08 10:55:12 +020035}
36
37void ProxyDatastore::moveItem(const std::string& source, std::variant<yang::move::Absolute, yang::move::Relative> move)
38{
Václav Kubernáte7248b22020-06-26 15:38:59 +020039 pickDatastore(source)->moveItem(source, move);
Václav Kubernát48e9dfa2020-07-08 10:55:12 +020040}
41
42void ProxyDatastore::commitChanges()
43{
44 m_datastore->commitChanges();
45}
46
47void ProxyDatastore::discardChanges()
48{
49 m_datastore->discardChanges();
50}
51
52void ProxyDatastore::copyConfig(const Datastore source, const Datastore destination)
53{
54 m_datastore->copyConfig(source, destination);
55}
56
57std::string ProxyDatastore::dump(const DataFormat format) const
58{
59 return m_datastore->dump(format);
60}
61
Václav Kubernáte7248b22020-06-26 15:38:59 +020062void ProxyDatastore::initiateRpc(const std::string& rpcPath)
63{
64 if (m_inputDatastore) {
65 throw std::runtime_error("RPC input already in progress (" + m_rpcPath + ")");
66 }
67 m_inputDatastore = m_createTemporaryDatastore(m_datastore);
68 m_rpcPath = rpcPath;
69 m_inputDatastore->createItem(rpcPath);
70}
71
72DatastoreAccess::Tree ProxyDatastore::executeRpc()
73{
74 if (!m_inputDatastore) {
75 throw std::runtime_error("No RPC input in progress");
76 }
77 auto inputData = m_inputDatastore->getItems("/");
78 m_inputDatastore = nullptr;
79 return m_datastore->executeRpc(m_rpcPath, inputData);
80}
81
82void ProxyDatastore::cancelRpc()
83{
84 m_inputDatastore = nullptr;
85}
86
Václav Kubernát48e9dfa2020-07-08 10:55:12 +020087std::shared_ptr<Schema> ProxyDatastore::schema() const
88{
89 return m_datastore->schema();
90}
Václav Kubernáte7248b22020-06-26 15:38:59 +020091
92std::shared_ptr<DatastoreAccess> ProxyDatastore::pickDatastore(const std::string& path) const
93{
94 if (!m_inputDatastore || !boost::starts_with(path, m_rpcPath)) {
95 return m_datastore;
96 } else {
97 return m_inputDatastore;
98 }
99}