blob: aecad5a926ba35732543d51196ce502f5c929f63 [file] [log] [blame]
Václav Kubernátbddbb172018-06-13 16:27: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át9456b5c2019-10-02 21:14:52 +02009#include <boost/algorithm/string/predicate.hpp>
Václav Kubernátbddbb172018-06-13 16:27:39 +020010#include "static_schema.hpp"
11#include "utils.hpp"
12
Václav Kubernátbddbb172018-06-13 16:27:39 +020013StaticSchema::StaticSchema()
14{
Václav Kubernátefcac932020-01-10 15:26:32 +010015 m_nodes.emplace("/", std::unordered_map<std::string, NodeType>());
Václav Kubernátbddbb172018-06-13 16:27:39 +020016}
17
18const std::unordered_map<std::string, NodeType>& StaticSchema::children(const std::string& name) const
19{
20 return m_nodes.at(name);
21}
22
Václav Kubernát744f57f2018-06-29 22:46:26 +020023bool StaticSchema::nodeExists(const std::string& location, const std::string& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020024{
Václav Kubernát744f57f2018-06-29 22:46:26 +020025 if (node.empty())
Václav Kubernátbddbb172018-06-13 16:27:39 +020026 return true;
27 const auto& childrenRef = children(location);
28
Václav Kubernát744f57f2018-06-29 22:46:26 +020029 return childrenRef.find(node) != childrenRef.end();
Václav Kubernátbddbb172018-06-13 16:27:39 +020030}
31
Václav Kubernát75877de2019-11-20 17:43:02 +010032bool StaticSchema::isModule(const std::string& name) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020033{
Václav Kubernát744f57f2018-06-29 22:46:26 +020034 return m_modules.find(name) != m_modules.end();
35}
36
Václav Kubernátbddbb172018-06-13 16:27:39 +020037void StaticSchema::addContainer(const std::string& location, const std::string& name, yang::ContainerTraits isPresence)
38{
39 m_nodes.at(location).emplace(name, yang::container{isPresence});
40
41 //create a new set of children for the new node
42 std::string key = joinPaths(location, name);
43 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
44}
45
Václav Kubernát2eaceb82018-10-08 19:56:30 +020046bool StaticSchema::listHasKey(const schemaPath_& location, const ModuleNodePair& node, const std::string& key) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020047{
Václav Kubernátefcac932020-01-10 15:26:32 +010048 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát744f57f2018-06-29 22:46:26 +020049 assert(isList(location, node));
Václav Kubernátbddbb172018-06-13 16:27:39 +020050
Václav Kubernát744f57f2018-06-29 22:46:26 +020051 const auto& child = children(locationString).at(fullNodeName(location, node));
Václav Kubernátbddbb172018-06-13 16:27:39 +020052 const auto& list = boost::get<yang::list>(child);
53 return list.m_keys.find(key) != list.m_keys.end();
54}
55
Václav Kubernát2eaceb82018-10-08 19:56:30 +020056const std::set<std::string> StaticSchema::listKeys(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020057{
Václav Kubernátefcac932020-01-10 15:26:32 +010058 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát744f57f2018-06-29 22:46:26 +020059 assert(isList(location, node));
Václav Kubernátbddbb172018-06-13 16:27:39 +020060
Václav Kubernát744f57f2018-06-29 22:46:26 +020061 const auto& child = children(locationString).at(fullNodeName(location, node));
Václav Kubernátbddbb172018-06-13 16:27:39 +020062 const auto& list = boost::get<yang::list>(child);
63 return list.m_keys;
64}
65
Václav Kubernátbddbb172018-06-13 16:27:39 +020066void StaticSchema::addList(const std::string& location, const std::string& name, const std::set<std::string>& keys)
67{
68 m_nodes.at(location).emplace(name, yang::list{keys});
69
Václav Kubernát1446fe12019-10-02 19:32:51 +020070 std::string key = joinPaths(location, name);
71 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
Václav Kubernátbddbb172018-06-13 16:27:39 +020072}
73
Václav Kubernát3a99f002020-03-31 02:27:41 +020074std::set<identityRef_> StaticSchema::validIdentities(std::string_view module, std::string_view value)
Václav Kubernátbddbb172018-06-13 16:27:39 +020075{
Václav Kubernát222ecff2020-05-14 23:14:35 +020076 std::set<identityRef_> identities;
77 getIdentSet(identityRef_{std::string{module}, std::string{value}}, identities);
Václav Kubernát3a99f002020-03-31 02:27:41 +020078
Václav Kubernát222ecff2020-05-14 23:14:35 +020079 return identities;
Václav Kubernátbddbb172018-06-13 16:27:39 +020080}
81
Václav Kubernát3a99f002020-03-31 02:27:41 +020082void StaticSchema::addLeaf(const std::string& location, const std::string& name, const yang::LeafDataType& type)
Václav Kubernátbddbb172018-06-13 16:27:39 +020083{
Václav Kubernát13b23d72020-04-16 21:49:51 +020084 m_nodes.at(location).emplace(name, yang::leaf{yang::TypeInfo{type, std::nullopt}});
Václav Kubernáte69133a2019-11-01 19:01:34 +010085 std::string key = joinPaths(location, name);
86 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
Václav Kubernát6a8d1d92019-04-24 20:30:36 +020087}
88
Václav Kubernát744f57f2018-06-29 22:46:26 +020089void StaticSchema::addModule(const std::string& name)
Václav Kubernátbddbb172018-06-13 16:27:39 +020090{
Václav Kubernát744f57f2018-06-29 22:46:26 +020091 m_modules.emplace(name);
92}
Václav Kubernátbddbb172018-06-13 16:27:39 +020093
Václav Kubernát222ecff2020-05-14 23:14:35 +020094void StaticSchema::addIdentity(const std::optional<identityRef_>& base, const identityRef_& name)
Václav Kubernáteeb38842019-03-20 19:46:05 +010095{
96 if (base)
97 m_identities.at(base.value()).emplace(name);
98
Václav Kubernát222ecff2020-05-14 23:14:35 +020099 m_identities.emplace(name, std::set<identityRef_>());
Václav Kubernáteeb38842019-03-20 19:46:05 +0100100}
Václav Kubernát744f57f2018-06-29 22:46:26 +0200101
Václav Kubernát222ecff2020-05-14 23:14:35 +0200102void StaticSchema::getIdentSet(const identityRef_& ident, std::set<identityRef_>& res) const
Václav Kubernáteeb38842019-03-20 19:46:05 +0100103{
104 res.insert(ident);
105 auto derivedIdentities = m_identities.at(ident);
106 for (auto it : derivedIdentities) {
107 getIdentSet(it, res);
108 }
109}
110
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200111std::string lastNodeOfSchemaPath(const std::string& path)
112{
113 std::string res = path;
Václav Kubernát2d7b05c2020-04-05 14:02:37 +0200114 if (auto pos = res.find_last_of('/'); pos != res.npos) {
115 res.erase(0, pos + 1);
Václav Kubernátefcac932020-01-10 15:26:32 +0100116 }
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200117 return res;
118}
119
Václav Kubernát13b23d72020-04-16 21:49:51 +0200120yang::TypeInfo StaticSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +0200121{
Václav Kubernátefcac932020-01-10 15:26:32 +0100122 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát744f57f2018-06-29 22:46:26 +0200123 return boost::get<yang::leaf>(children(locationString).at(fullNodeName(location, node))).m_type;
124}
125
Václav Kubernát13b23d72020-04-16 21:49:51 +0200126yang::TypeInfo StaticSchema::leafType(const std::string& path) const
Václav Kubernát9bf36852020-02-18 17:47:56 +0100127{
Václav Kubernát3a99f002020-03-31 02:27:41 +0200128 auto locationString = stripLastNodeFromPath(path);
129 auto node = lastNodeOfSchemaPath(path);
130 return boost::get<yang::leaf>(children(locationString).at(node)).m_type;
Václav Kubernát989b5de2019-02-20 16:28:35 +0100131}
132
Václav Kubernát95b08872020-04-28 01:04:17 +0200133std::set<ModuleNodePair> StaticSchema::availableNodes(const boost::variant<dataPath_, schemaPath_, module_>& path, const Recursion recursion) const
Václav Kubernát744f57f2018-06-29 22:46:26 +0200134{
Václav Kubernát3a823f42020-04-29 23:40:21 +0200135 if (recursion == Recursion::Recursive) {
136 throw std::logic_error("Recursive StaticSchema::availableNodes is not implemented. It shouldn't be used in tests.");
137 }
138
Václav Kubernát95b08872020-04-28 01:04:17 +0200139 std::set<ModuleNodePair> res;
Václav Kubernát3a823f42020-04-29 23:40:21 +0200140 if (path.type() == typeid(module_)) {
141 auto topLevelNodes = m_nodes.at("");
142 auto modulePlusColon = boost::get<module_>(path).m_name + ":";
143 for (const auto& it : topLevelNodes) {
144 if (boost::algorithm::starts_with(it.first, modulePlusColon)) {
Václav Kubernát95b08872020-04-28 01:04:17 +0200145 res.insert(splitModuleNode(it.first));
Václav Kubernát3a823f42020-04-29 23:40:21 +0200146 }
147 }
148 return res;
149 }
150
Václav Kubernáte2d629f2020-04-28 11:01:54 +0200151 auto getTopLevelModule = [] (const auto& path) -> boost::optional<std::string> {
152 if (!path.m_nodes.empty()) {
153 return path.m_nodes.begin()->m_prefix.flat_map([] (const auto& module) {return boost::optional<std::string>(module.m_name);});
154 }
155
156 return boost::none;
157 };
158
159 std::string locationString;
160 boost::optional<std::string> topLevelModule;
161 if (path.type() == typeid(schemaPath_)) {
162 locationString = pathToSchemaString(boost::get<schemaPath_>(path), Prefixes::Always);
163 topLevelModule = getTopLevelModule(boost::get<schemaPath_>(path));
164 } else {
165 locationString = pathToSchemaString(boost::get<dataPath_>(path), Prefixes::Always);
166 topLevelModule = getTopLevelModule(boost::get<dataPath_>(path));
167 }
Václav Kubernát744f57f2018-06-29 22:46:26 +0200168
169 auto childrenRef = children(locationString);
170
Václav Kubernáte2d629f2020-04-28 11:01:54 +0200171 std::transform(childrenRef.begin(), childrenRef.end(), std::inserter(res, res.end()), [path, topLevelModule](const auto& it) {
172 auto res = splitModuleNode(it.first);
173 if (topLevelModule == res.first) {
174 res.first = boost::none;
175 }
176 return res;
Václav Kubernát95b08872020-04-28 01:04:17 +0200177 });
Václav Kubernát744f57f2018-06-29 22:46:26 +0200178 return res;
Václav Kubernátbddbb172018-06-13 16:27:39 +0200179}
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200180
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100181yang::NodeTypes StaticSchema::nodeType(const schemaPath_& location, const ModuleNodePair& node) const
182{
183 std::string locationString = pathToSchemaString(location, Prefixes::Always);
184 auto fullName = fullNodeName(location, node);
185 try {
186 auto targetNode = children(locationString).at(fullName);
187
188 if (targetNode.type() == typeid(yang::container)) {
189 if (boost::get<yang::container>(targetNode).m_presence == yang::ContainerTraits::Presence) {
190 return yang::NodeTypes::PresenceContainer;
191 }
192 return yang::NodeTypes::Container;
193 }
194
195 if (targetNode.type() == typeid(yang::list)) {
196 return yang::NodeTypes::List;
197 }
198
199 if (targetNode.type() == typeid(yang::leaf)) {
200 return yang::NodeTypes::Leaf;
201 }
202
203 throw std::runtime_error{"YangSchema::nodeType: unsupported type"};
204
205 } catch (std::out_of_range&) {
206 throw InvalidNodeException();
207 }
208}
209
Václav Kubernát1e09bd62020-02-17 15:13:38 +0100210std::optional<std::string> StaticSchema::description([[maybe_unused]] const std::string& path) const
211{
212 throw std::runtime_error{"StaticSchema::description not implemented"};
213}
214
Václav Kubernáta1c4c9e2020-04-22 00:37:52 +0200215yang::Status StaticSchema::status([[maybe_unused]] const std::string& location) const
216{
217 throw std::runtime_error{"Internal error: StaticSchema::status(std::string) not implemented. The tests should not have called this overload."};
218}
219
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100220yang::NodeTypes StaticSchema::nodeType([[maybe_unused]] const std::string& path) const
221{
222 throw std::runtime_error{"Internal error: StaticSchema::nodeType(std::string) not implemented. The tests should not have called this overload."};
223}
Václav Kubernátad87ece2020-02-19 15:20:56 +0100224
Václav Kubernátbd5e3c22020-02-19 15:22:00 +0100225std::string StaticSchema::leafrefPath([[maybe_unused]] const std::string& leafrefPath) const
226{
227 throw std::runtime_error{"Internal error: StaticSchema::leafrefPath(std::string) not implemented. The tests should not have called this overload."};
228}
Václav Kubernátc3866792020-02-20 14:12:56 +0100229
230bool StaticSchema::leafIsKey([[maybe_unused]] const std::string& leafPath) const
231{
232 throw std::runtime_error{"Internal error: StaticSchema::leafIsKey(std::string) not implemented. The tests should not have called this overload."};
233}
Václav Kubernát6fcd0282020-02-21 16:33:08 +0100234
235std::optional<std::string> StaticSchema::leafTypeName([[maybe_unused]] const std::string& path) const
236{
237 throw std::runtime_error{"Internal error: StaticSchema::leafTypeName(std::string) not implemented. The tests should not have called this overload."};
238}
Václav Kubernát0599e9f2020-04-21 09:51:33 +0200239
240bool StaticSchema::isConfig([[maybe_unused]] const std::string& leafPath) const
241{
242 throw std::runtime_error{"Internal error: StaticSchema::isConfigLeaf(std::string) not implemented. The tests should not have called this overload."};
243}
Václav Kubernátb1a75c62020-04-21 15:20:16 +0200244
245std::optional<std::string> StaticSchema::defaultValue([[maybe_unused]] const std::string& leafPath) const
246{
247 throw std::runtime_error{"Internal error: StaticSchema::defaultValue(std::string) not implemented. The tests should not have called this overload."};
248}