blob: a1a56021e20051fcf4a7b7ea0d2ac7fd87f1b4bd [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átabf52802020-05-19 01:31:17 +020015 m_nodes.emplace("/", std::unordered_map<std::string, NodeInfo>());
Václav Kubernátbddbb172018-06-13 16:27:39 +020016}
17
Václav Kubernátabf52802020-05-19 01:31:17 +020018const std::unordered_map<std::string, NodeInfo>& StaticSchema::children(const std::string& name) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020019{
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{
Václav Kubernátabf52802020-05-19 01:31:17 +020039 m_nodes.at(location).emplace(name, NodeInfo{yang::container{isPresence}, yang::AccessType::Writable});
Václav Kubernátbddbb172018-06-13 16:27:39 +020040
41 //create a new set of children for the new node
42 std::string key = joinPaths(location, name);
Václav Kubernátabf52802020-05-19 01:31:17 +020043 m_nodes.emplace(key, std::unordered_map<std::string, NodeInfo>());
Václav Kubernátbddbb172018-06-13 16:27:39 +020044}
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át4d8a9c22020-05-26 23:56:10 +020052 const auto& list = std::get<yang::list>(child.m_nodeType);
Václav Kubernátbddbb172018-06-13 16:27:39 +020053 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át4d8a9c22020-05-26 23:56:10 +020062 const auto& list = std::get<yang::list>(child.m_nodeType);
Václav Kubernátbddbb172018-06-13 16:27:39 +020063 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{
Václav Kubernátabf52802020-05-19 01:31:17 +020068 m_nodes.at(location).emplace(name, NodeInfo{yang::list{keys}, yang::AccessType::Writable});
Václav Kubernátbddbb172018-06-13 16:27:39 +020069
Václav Kubernát1446fe12019-10-02 19:32:51 +020070 std::string key = joinPaths(location, name);
Václav Kubernátabf52802020-05-19 01:31:17 +020071 m_nodes.emplace(key, std::unordered_map<std::string, NodeInfo>());
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átabf52802020-05-19 01:31:17 +020082void StaticSchema::addLeaf(const std::string& location, const std::string& name, const yang::LeafDataType& type, const yang::AccessType accessType)
Václav Kubernátbddbb172018-06-13 16:27:39 +020083{
Václav Kubernátabf52802020-05-19 01:31:17 +020084 m_nodes.at(location).emplace(name, NodeInfo{yang::leaf{yang::TypeInfo{type, std::nullopt}}, accessType});
Václav Kubernáte69133a2019-11-01 19:01:34 +010085 std::string key = joinPaths(location, name);
Václav Kubernátabf52802020-05-19 01:31:17 +020086 m_nodes.emplace(key, std::unordered_map<std::string, NodeInfo>());
Václav Kubernát6a8d1d92019-04-24 20:30:36 +020087}
88
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020089void StaticSchema::addLeafList(const std::string& location, const std::string& name, const yang::LeafDataType& type)
90{
91 m_nodes.at(location).emplace(name, NodeInfo{yang::leaflist{yang::TypeInfo{type, std::nullopt}}, yang::AccessType::Writable});
92 std::string key = joinPaths(location, name);
93 m_nodes.emplace(key, std::unordered_map<std::string, NodeInfo>());
94}
95
Václav Kubernát744f57f2018-06-29 22:46:26 +020096void StaticSchema::addModule(const std::string& name)
Václav Kubernátbddbb172018-06-13 16:27:39 +020097{
Václav Kubernát744f57f2018-06-29 22:46:26 +020098 m_modules.emplace(name);
99}
Václav Kubernátbddbb172018-06-13 16:27:39 +0200100
Václav Kubernát222ecff2020-05-14 23:14:35 +0200101void StaticSchema::addIdentity(const std::optional<identityRef_>& base, const identityRef_& name)
Václav Kubernáteeb38842019-03-20 19:46:05 +0100102{
103 if (base)
104 m_identities.at(base.value()).emplace(name);
105
Václav Kubernát222ecff2020-05-14 23:14:35 +0200106 m_identities.emplace(name, std::set<identityRef_>());
Václav Kubernáteeb38842019-03-20 19:46:05 +0100107}
Václav Kubernát744f57f2018-06-29 22:46:26 +0200108
Václav Kubernát222ecff2020-05-14 23:14:35 +0200109void StaticSchema::getIdentSet(const identityRef_& ident, std::set<identityRef_>& res) const
Václav Kubernáteeb38842019-03-20 19:46:05 +0100110{
111 res.insert(ident);
112 auto derivedIdentities = m_identities.at(ident);
113 for (auto it : derivedIdentities) {
114 getIdentSet(it, res);
115 }
116}
117
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200118std::string lastNodeOfSchemaPath(const std::string& path)
119{
120 std::string res = path;
Václav Kubernát2d7b05c2020-04-05 14:02:37 +0200121 if (auto pos = res.find_last_of('/'); pos != res.npos) {
122 res.erase(0, pos + 1);
Václav Kubernátefcac932020-01-10 15:26:32 +0100123 }
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200124 return res;
125}
126
Václav Kubernát13b23d72020-04-16 21:49:51 +0200127yang::TypeInfo StaticSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +0200128{
Václav Kubernátefcac932020-01-10 15:26:32 +0100129 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200130 auto nodeType = children(locationString).at(fullNodeName(location, node)).m_nodeType;
Václav Kubernát4d8a9c22020-05-26 23:56:10 +0200131 if (std::holds_alternative<yang::leaf>(nodeType)) {
132 return std::get<yang::leaf>(nodeType).m_type;
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200133 }
134
Václav Kubernát4d8a9c22020-05-26 23:56:10 +0200135 if (std::holds_alternative<yang::leaflist>(nodeType)) {
136 return std::get<yang::leaflist>(nodeType).m_type;
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200137 }
138
139 throw std::logic_error("StaticSchema::leafType: Path is not a leaf or a leaflist");
Václav Kubernát744f57f2018-06-29 22:46:26 +0200140}
141
Václav Kubernát13b23d72020-04-16 21:49:51 +0200142yang::TypeInfo StaticSchema::leafType(const std::string& path) const
Václav Kubernát9bf36852020-02-18 17:47:56 +0100143{
Václav Kubernát3a99f002020-03-31 02:27:41 +0200144 auto locationString = stripLastNodeFromPath(path);
145 auto node = lastNodeOfSchemaPath(path);
Václav Kubernát4d8a9c22020-05-26 23:56:10 +0200146 return std::get<yang::leaf>(children(locationString).at(node).m_nodeType).m_type;
Václav Kubernát989b5de2019-02-20 16:28:35 +0100147}
148
Václav Kubernát95b08872020-04-28 01:04:17 +0200149std::set<ModuleNodePair> StaticSchema::availableNodes(const boost::variant<dataPath_, schemaPath_, module_>& path, const Recursion recursion) const
Václav Kubernát744f57f2018-06-29 22:46:26 +0200150{
Václav Kubernát3a823f42020-04-29 23:40:21 +0200151 if (recursion == Recursion::Recursive) {
152 throw std::logic_error("Recursive StaticSchema::availableNodes is not implemented. It shouldn't be used in tests.");
153 }
154
Václav Kubernát95b08872020-04-28 01:04:17 +0200155 std::set<ModuleNodePair> res;
Václav Kubernát3a823f42020-04-29 23:40:21 +0200156 if (path.type() == typeid(module_)) {
157 auto topLevelNodes = m_nodes.at("");
158 auto modulePlusColon = boost::get<module_>(path).m_name + ":";
159 for (const auto& it : topLevelNodes) {
160 if (boost::algorithm::starts_with(it.first, modulePlusColon)) {
Václav Kubernát95b08872020-04-28 01:04:17 +0200161 res.insert(splitModuleNode(it.first));
Václav Kubernát3a823f42020-04-29 23:40:21 +0200162 }
163 }
164 return res;
165 }
166
Václav Kubernáte2d629f2020-04-28 11:01:54 +0200167 auto getTopLevelModule = [] (const auto& path) -> boost::optional<std::string> {
168 if (!path.m_nodes.empty()) {
169 return path.m_nodes.begin()->m_prefix.flat_map([] (const auto& module) {return boost::optional<std::string>(module.m_name);});
170 }
171
172 return boost::none;
173 };
174
175 std::string locationString;
176 boost::optional<std::string> topLevelModule;
177 if (path.type() == typeid(schemaPath_)) {
178 locationString = pathToSchemaString(boost::get<schemaPath_>(path), Prefixes::Always);
179 topLevelModule = getTopLevelModule(boost::get<schemaPath_>(path));
180 } else {
181 locationString = pathToSchemaString(boost::get<dataPath_>(path), Prefixes::Always);
182 topLevelModule = getTopLevelModule(boost::get<dataPath_>(path));
183 }
Václav Kubernát744f57f2018-06-29 22:46:26 +0200184
185 auto childrenRef = children(locationString);
186
Václav Kubernáte2d629f2020-04-28 11:01:54 +0200187 std::transform(childrenRef.begin(), childrenRef.end(), std::inserter(res, res.end()), [path, topLevelModule](const auto& it) {
188 auto res = splitModuleNode(it.first);
189 if (topLevelModule == res.first) {
190 res.first = boost::none;
191 }
192 return res;
Václav Kubernát95b08872020-04-28 01:04:17 +0200193 });
Václav Kubernát744f57f2018-06-29 22:46:26 +0200194 return res;
Václav Kubernátbddbb172018-06-13 16:27:39 +0200195}
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200196
Václav Kubernát19a368b2020-05-27 00:05:23 +0200197struct impl_nodeType {
198
199 yang::NodeTypes operator()(const yang::container& cont)
200 {
201 if (cont.m_presence == yang::ContainerTraits::Presence) {
202 return yang::NodeTypes::PresenceContainer;
203 }
204 return yang::NodeTypes::Container;
205 }
206 yang::NodeTypes operator()(const yang::list&)
207 {
208 return yang::NodeTypes::List;
209 }
210 yang::NodeTypes operator()(const yang::leaf&)
211 {
212 return yang::NodeTypes::Leaf;
213
214 }
215 yang::NodeTypes operator()(const yang::leaflist&)
216 {
217 return yang::NodeTypes::LeafList;
218 }
219};
220
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100221yang::NodeTypes StaticSchema::nodeType(const schemaPath_& location, const ModuleNodePair& node) const
222{
223 std::string locationString = pathToSchemaString(location, Prefixes::Always);
224 auto fullName = fullNodeName(location, node);
225 try {
226 auto targetNode = children(locationString).at(fullName);
227
Václav Kubernát19a368b2020-05-27 00:05:23 +0200228 return std::visit(impl_nodeType{}, targetNode.m_nodeType);
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100229 } catch (std::out_of_range&) {
230 throw InvalidNodeException();
231 }
232}
233
Václav Kubernátabf52802020-05-19 01:31:17 +0200234std::string fullNodeName(const std::string& location, const std::string& node)
235{
236 // If the node already contains a module name, just return it.
237 if (node.find_first_of(':') != std::string::npos) {
238 return node;
239 }
240
241 // Otherwise take the module name from the first node of location.
242 return location.substr(location.find_first_not_of('/'), location.find_first_of(':') - 1) + ":" + node;
243}
244
245bool StaticSchema::isConfig(const std::string& leafPath) const
246{
247 auto locationString = stripLastNodeFromPath(leafPath);
248
249 auto node = fullNodeName(locationString, lastNodeOfSchemaPath(leafPath));
250 return children(locationString).at(node).m_configType == yang::AccessType::Writable;
251}
252
Václav Kubernát1e09bd62020-02-17 15:13:38 +0100253std::optional<std::string> StaticSchema::description([[maybe_unused]] const std::string& path) const
254{
255 throw std::runtime_error{"StaticSchema::description not implemented"};
256}
257
Václav Kubernáta1c4c9e2020-04-22 00:37:52 +0200258yang::Status StaticSchema::status([[maybe_unused]] const std::string& location) const
259{
260 throw std::runtime_error{"Internal error: StaticSchema::status(std::string) not implemented. The tests should not have called this overload."};
261}
262
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100263yang::NodeTypes StaticSchema::nodeType([[maybe_unused]] const std::string& path) const
264{
265 throw std::runtime_error{"Internal error: StaticSchema::nodeType(std::string) not implemented. The tests should not have called this overload."};
266}
Václav Kubernátad87ece2020-02-19 15:20:56 +0100267
Václav Kubernátbd5e3c22020-02-19 15:22:00 +0100268std::string StaticSchema::leafrefPath([[maybe_unused]] const std::string& leafrefPath) const
269{
270 throw std::runtime_error{"Internal error: StaticSchema::leafrefPath(std::string) not implemented. The tests should not have called this overload."};
271}
Václav Kubernátc3866792020-02-20 14:12:56 +0100272
273bool StaticSchema::leafIsKey([[maybe_unused]] const std::string& leafPath) const
274{
275 throw std::runtime_error{"Internal error: StaticSchema::leafIsKey(std::string) not implemented. The tests should not have called this overload."};
276}
Václav Kubernát6fcd0282020-02-21 16:33:08 +0100277
278std::optional<std::string> StaticSchema::leafTypeName([[maybe_unused]] const std::string& path) const
279{
280 throw std::runtime_error{"Internal error: StaticSchema::leafTypeName(std::string) not implemented. The tests should not have called this overload."};
281}
Václav Kubernát0599e9f2020-04-21 09:51:33 +0200282
Václav Kubernátb1a75c62020-04-21 15:20:16 +0200283std::optional<std::string> StaticSchema::defaultValue([[maybe_unused]] const std::string& leafPath) const
284{
285 throw std::runtime_error{"Internal error: StaticSchema::defaultValue(std::string) not implemented. The tests should not have called this overload."};
286}