blob: 80e6195a424c0ebf8cc8ba1f5f8875d5ac76fb3f [file] [log] [blame]
Václav Kubernát96344a12018-05-28 16:33:39 +02001/*
2 * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
3 * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
4 *
5 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
6 *
7*/
8
Václav Kubernátb61336d2018-05-28 17:35:03 +02009#include <iostream>
Václav Kubernát6415b822018-08-22 17:40:01 +020010#include "datastore_access.hpp"
Václav Kubernát96344a12018-05-28 16:33:39 +020011#include "interpreter.hpp"
12
Václav Kubernátebca2552018-06-08 19:06:02 +020013struct leafDataToString : boost::static_visitor<std::string> {
14 std::string operator()(const enum_& data) const
15 {
16 return data.m_value;
17 }
18 template <typename T>
19 std::string operator()(const T& data) const
20 {
21 std::stringstream stream;
22 stream << data;
23 return stream.str();
24 }
25};
26
Václav Kubernát812ee282018-08-30 17:10:03 +020027void Interpreter::operator()(const commit_&) const
28{
29 m_datastore.commitChanges();
30}
31
Václav Kubernát6d791432018-10-25 16:00:35 +020032void Interpreter::operator()(const discard_&) const
33{
34 m_datastore.discardChanges();
35}
36
Václav Kubernát07204242018-06-04 18:12:09 +020037void Interpreter::operator()(const set_& set) const
38{
Václav Kubernát6415b822018-08-22 17:40:01 +020039 m_datastore.setLeaf(absolutePathFromCommand(set), set.m_data);
Václav Kubernát07204242018-06-04 18:12:09 +020040}
41
Václav Kubernátb6ff0b62018-08-30 16:14:53 +020042void Interpreter::operator()(const get_& get) const
43{
44 auto items = m_datastore.getItems(absolutePathFromCommand(get));
45 for (auto it : items) {
46 std::cout << it.first << " = " << boost::apply_visitor(leafDataToString(), it.second) << std::endl;
47 }
48}
49
Václav Kubernát96344a12018-05-28 16:33:39 +020050void Interpreter::operator()(const cd_& cd) const
51{
52 m_parser.changeNode(cd.m_path);
53}
54
Václav Kubernátb61336d2018-05-28 17:35:03 +020055void Interpreter::operator()(const create_& create) const
56{
Václav Kubernát6415b822018-08-22 17:40:01 +020057 m_datastore.createPresenceContainer(absolutePathFromCommand(create));
Václav Kubernátb61336d2018-05-28 17:35:03 +020058}
59
60void Interpreter::operator()(const delete_& delet) const
61{
Václav Kubernát6415b822018-08-22 17:40:01 +020062 m_datastore.deletePresenceContainer(absolutePathFromCommand(delet));
Václav Kubernátb61336d2018-05-28 17:35:03 +020063}
64
Václav Kubernát11afac72018-07-18 14:59:53 +020065void Interpreter::operator()(const ls_& ls) const
66{
67 std::cout << "Possible nodes:" << std::endl;
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020068 auto recursion{Recursion::NonRecursive};
69 for (auto it : ls.m_options) {
70 if (it == LsOption::Recursive)
71 recursion = Recursion::Recursive;
72 }
Václav Kubernát11afac72018-07-18 14:59:53 +020073
Václav Kubernáte7d4aea2018-09-11 18:15:48 +020074 for (const auto& it : m_parser.availableNodes(ls.m_path, recursion))
Václav Kubernát11afac72018-07-18 14:59:53 +020075 std::cout << it << std::endl;
76}
77
Václav Kubernát6415b822018-08-22 17:40:01 +020078template <typename T>
79std::string Interpreter::absolutePathFromCommand(const T& command) const
80{
Václav Kubernát37171a12018-08-31 17:01:48 +020081 if (command.m_path.m_scope == Scope::Absolute)
82 return "/" + pathToDataString(command.m_path);
83 else
84 return joinPaths(m_parser.currentNode(), pathToDataString(command.m_path));
Václav Kubernát6415b822018-08-22 17:40:01 +020085}
86
Václav Kubernát5c75b252018-10-10 18:33:47 +020087struct pathToStringVisitor : boost::static_visitor<std::string> {
88 std::string operator()(const schemaPath_& path) const
89 {
90 return pathToSchemaString(path);
91 }
92 std::string operator()(const dataPath_& path) const
93 {
94 return pathToDataString(path);
95 }
96};
97
98struct getPathScopeVisitor : boost::static_visitor<Scope> {
99 template <typename T>
100 Scope operator()(const T& path) const
101 {
102 return path.m_scope;
103 }
104};
105
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200106std::string Interpreter::absolutePathFromCommand(const get_& get) const
107{
108 if (!get.m_path) {
109 return m_parser.currentNode();
Václav Kubernát5c75b252018-10-10 18:33:47 +0200110 }
111
112 const auto path = *get.m_path;
113 std::string pathString = boost::apply_visitor(pathToStringVisitor(), path);
114 auto pathScope{boost::apply_visitor(getPathScopeVisitor(), path)};
115
116 if (pathScope == Scope::Absolute) {
117 return "/" + pathString;
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200118 } else {
Václav Kubernát5c75b252018-10-10 18:33:47 +0200119 return joinPaths(m_parser.currentNode(), pathString);
Václav Kubernátb6ff0b62018-08-30 16:14:53 +0200120 }
121}
122
Václav Kubernát6415b822018-08-22 17:40:01 +0200123Interpreter::Interpreter(Parser& parser, DatastoreAccess& datastore)
Václav Kubernát812ee282018-08-30 17:10:03 +0200124 : m_parser(parser)
125 , m_datastore(datastore)
Václav Kubernát96344a12018-05-28 16:33:39 +0200126{
127}