blob: 203b61eb307a38273b619eec0a0d44cc61dfa5b2 [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át75877de2019-11-20 17:43:02 +010023bool StaticSchema::isModule(const std::string& name) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020024{
Václav Kubernát744f57f2018-06-29 22:46:26 +020025 return m_modules.find(name) != m_modules.end();
26}
27
Václav Kubernátbddbb172018-06-13 16:27:39 +020028void StaticSchema::addContainer(const std::string& location, const std::string& name, yang::ContainerTraits isPresence)
29{
Václav Kubernátabf52802020-05-19 01:31:17 +020030 m_nodes.at(location).emplace(name, NodeInfo{yang::container{isPresence}, yang::AccessType::Writable});
Václav Kubernátbddbb172018-06-13 16:27:39 +020031
32 //create a new set of children for the new node
33 std::string key = joinPaths(location, name);
Václav Kubernátabf52802020-05-19 01:31:17 +020034 m_nodes.emplace(key, std::unordered_map<std::string, NodeInfo>());
Václav Kubernátbddbb172018-06-13 16:27:39 +020035}
36
Václav Kubernát2eaceb82018-10-08 19:56:30 +020037bool StaticSchema::listHasKey(const schemaPath_& location, const ModuleNodePair& node, const std::string& key) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020038{
Václav Kubernátefcac932020-01-10 15:26:32 +010039 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát744f57f2018-06-29 22:46:26 +020040 assert(isList(location, node));
Václav Kubernátbddbb172018-06-13 16:27:39 +020041
Václav Kubernát744f57f2018-06-29 22:46:26 +020042 const auto& child = children(locationString).at(fullNodeName(location, node));
Václav Kubernát4d8a9c22020-05-26 23:56:10 +020043 const auto& list = std::get<yang::list>(child.m_nodeType);
Václav Kubernátbddbb172018-06-13 16:27:39 +020044 return list.m_keys.find(key) != list.m_keys.end();
45}
46
Václav Kubernát2db124c2020-05-28 21:58:36 +020047bool StaticSchema::listHasKey(const schemaPath_& listPath, const std::string& key) const
48{
49 return listKeys(listPath).count(key);
50}
51
Václav Kubernát2eaceb82018-10-08 19:56:30 +020052const std::set<std::string> StaticSchema::listKeys(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +020053{
Václav Kubernátefcac932020-01-10 15:26:32 +010054 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát744f57f2018-06-29 22:46:26 +020055 assert(isList(location, node));
Václav Kubernátbddbb172018-06-13 16:27:39 +020056
Václav Kubernát744f57f2018-06-29 22:46:26 +020057 const auto& child = children(locationString).at(fullNodeName(location, node));
Václav Kubernát4d8a9c22020-05-26 23:56:10 +020058 const auto& list = std::get<yang::list>(child.m_nodeType);
Václav Kubernátbddbb172018-06-13 16:27:39 +020059 return list.m_keys;
60}
61
Václav Kubernát2db124c2020-05-28 21:58:36 +020062std::string lastNodeOfSchemaPath(const std::string& path)
63{
64 std::string res = path;
65 if (auto pos = res.find_last_of('/'); pos != res.npos) {
66 res.erase(0, pos + 1);
67 }
68 return res;
69}
70
71const std::set<std::string> StaticSchema::listKeys(const schemaPath_& listPath) const
72{
73 auto listPathString = pathToSchemaString(listPath, Prefixes::Always);
74 const auto& child = children(stripLastNodeFromPath(listPathString)).at(lastNodeOfSchemaPath(listPathString));
75 const auto& list = std::get<yang::list>(child.m_nodeType);
76 return list.m_keys;
77}
78
Václav Kubernátbddbb172018-06-13 16:27:39 +020079void StaticSchema::addList(const std::string& location, const std::string& name, const std::set<std::string>& keys)
80{
Václav Kubernátabf52802020-05-19 01:31:17 +020081 m_nodes.at(location).emplace(name, NodeInfo{yang::list{keys}, yang::AccessType::Writable});
Václav Kubernátbddbb172018-06-13 16:27:39 +020082
Václav Kubernát1446fe12019-10-02 19:32:51 +020083 std::string key = joinPaths(location, name);
Václav Kubernátabf52802020-05-19 01:31:17 +020084 m_nodes.emplace(key, std::unordered_map<std::string, NodeInfo>());
Václav Kubernátbddbb172018-06-13 16:27:39 +020085}
86
Václav Kubernát3a99f002020-03-31 02:27:41 +020087std::set<identityRef_> StaticSchema::validIdentities(std::string_view module, std::string_view value)
Václav Kubernátbddbb172018-06-13 16:27:39 +020088{
Václav Kubernát222ecff2020-05-14 23:14:35 +020089 std::set<identityRef_> identities;
90 getIdentSet(identityRef_{std::string{module}, std::string{value}}, identities);
Václav Kubernát3a99f002020-03-31 02:27:41 +020091
Václav Kubernát222ecff2020-05-14 23:14:35 +020092 return identities;
Václav Kubernátbddbb172018-06-13 16:27:39 +020093}
94
Václav Kubernátabf52802020-05-19 01:31:17 +020095void 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 +020096{
Václav Kubernátabf52802020-05-19 01:31:17 +020097 m_nodes.at(location).emplace(name, NodeInfo{yang::leaf{yang::TypeInfo{type, std::nullopt}}, accessType});
Václav Kubernáte69133a2019-11-01 19:01:34 +010098 std::string key = joinPaths(location, name);
Václav Kubernátabf52802020-05-19 01:31:17 +020099 m_nodes.emplace(key, std::unordered_map<std::string, NodeInfo>());
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200100}
101
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200102void StaticSchema::addLeafList(const std::string& location, const std::string& name, const yang::LeafDataType& type)
103{
104 m_nodes.at(location).emplace(name, NodeInfo{yang::leaflist{yang::TypeInfo{type, std::nullopt}}, yang::AccessType::Writable});
105 std::string key = joinPaths(location, name);
106 m_nodes.emplace(key, std::unordered_map<std::string, NodeInfo>());
107}
108
Václav Kubernát744f57f2018-06-29 22:46:26 +0200109void StaticSchema::addModule(const std::string& name)
Václav Kubernátbddbb172018-06-13 16:27:39 +0200110{
Václav Kubernát744f57f2018-06-29 22:46:26 +0200111 m_modules.emplace(name);
112}
Václav Kubernátbddbb172018-06-13 16:27:39 +0200113
Václav Kubernát222ecff2020-05-14 23:14:35 +0200114void StaticSchema::addIdentity(const std::optional<identityRef_>& base, const identityRef_& name)
Václav Kubernáteeb38842019-03-20 19:46:05 +0100115{
116 if (base)
117 m_identities.at(base.value()).emplace(name);
118
Václav Kubernát222ecff2020-05-14 23:14:35 +0200119 m_identities.emplace(name, std::set<identityRef_>());
Václav Kubernáteeb38842019-03-20 19:46:05 +0100120}
Václav Kubernát744f57f2018-06-29 22:46:26 +0200121
Václav Kubernát222ecff2020-05-14 23:14:35 +0200122void StaticSchema::getIdentSet(const identityRef_& ident, std::set<identityRef_>& res) const
Václav Kubernáteeb38842019-03-20 19:46:05 +0100123{
124 res.insert(ident);
125 auto derivedIdentities = m_identities.at(ident);
126 for (auto it : derivedIdentities) {
127 getIdentSet(it, res);
128 }
129}
130
Václav Kubernát13b23d72020-04-16 21:49:51 +0200131yang::TypeInfo StaticSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +0200132{
Václav Kubernátefcac932020-01-10 15:26:32 +0100133 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200134 auto nodeType = children(locationString).at(fullNodeName(location, node)).m_nodeType;
Václav Kubernát4d8a9c22020-05-26 23:56:10 +0200135 if (std::holds_alternative<yang::leaf>(nodeType)) {
136 return std::get<yang::leaf>(nodeType).m_type;
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200137 }
138
Václav Kubernát4d8a9c22020-05-26 23:56:10 +0200139 if (std::holds_alternative<yang::leaflist>(nodeType)) {
140 return std::get<yang::leaflist>(nodeType).m_type;
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200141 }
142
143 throw std::logic_error("StaticSchema::leafType: Path is not a leaf or a leaflist");
Václav Kubernát744f57f2018-06-29 22:46:26 +0200144}
145
Václav Kubernát13b23d72020-04-16 21:49:51 +0200146yang::TypeInfo StaticSchema::leafType(const std::string& path) const
Václav Kubernát9bf36852020-02-18 17:47:56 +0100147{
Václav Kubernát3a99f002020-03-31 02:27:41 +0200148 auto locationString = stripLastNodeFromPath(path);
149 auto node = lastNodeOfSchemaPath(path);
Václav Kubernát4d8a9c22020-05-26 23:56:10 +0200150 return std::get<yang::leaf>(children(locationString).at(node).m_nodeType).m_type;
Václav Kubernát989b5de2019-02-20 16:28:35 +0100151}
152
Václav Kubernát95b08872020-04-28 01:04:17 +0200153std::set<ModuleNodePair> StaticSchema::availableNodes(const boost::variant<dataPath_, schemaPath_, module_>& path, const Recursion recursion) const
Václav Kubernát744f57f2018-06-29 22:46:26 +0200154{
Václav Kubernát3a823f42020-04-29 23:40:21 +0200155 if (recursion == Recursion::Recursive) {
156 throw std::logic_error("Recursive StaticSchema::availableNodes is not implemented. It shouldn't be used in tests.");
157 }
158
Václav Kubernát95b08872020-04-28 01:04:17 +0200159 std::set<ModuleNodePair> res;
Václav Kubernát3a823f42020-04-29 23:40:21 +0200160 if (path.type() == typeid(module_)) {
161 auto topLevelNodes = m_nodes.at("");
162 auto modulePlusColon = boost::get<module_>(path).m_name + ":";
163 for (const auto& it : topLevelNodes) {
164 if (boost::algorithm::starts_with(it.first, modulePlusColon)) {
Václav Kubernát95b08872020-04-28 01:04:17 +0200165 res.insert(splitModuleNode(it.first));
Václav Kubernát3a823f42020-04-29 23:40:21 +0200166 }
167 }
168 return res;
169 }
170
Václav Kubernáte2d629f2020-04-28 11:01:54 +0200171 auto getTopLevelModule = [] (const auto& path) -> boost::optional<std::string> {
172 if (!path.m_nodes.empty()) {
173 return path.m_nodes.begin()->m_prefix.flat_map([] (const auto& module) {return boost::optional<std::string>(module.m_name);});
174 }
175
176 return boost::none;
177 };
178
179 std::string locationString;
180 boost::optional<std::string> topLevelModule;
181 if (path.type() == typeid(schemaPath_)) {
182 locationString = pathToSchemaString(boost::get<schemaPath_>(path), Prefixes::Always);
183 topLevelModule = getTopLevelModule(boost::get<schemaPath_>(path));
184 } else {
185 locationString = pathToSchemaString(boost::get<dataPath_>(path), Prefixes::Always);
186 topLevelModule = getTopLevelModule(boost::get<dataPath_>(path));
187 }
Václav Kubernát744f57f2018-06-29 22:46:26 +0200188
189 auto childrenRef = children(locationString);
190
Václav Kubernáte2d629f2020-04-28 11:01:54 +0200191 std::transform(childrenRef.begin(), childrenRef.end(), std::inserter(res, res.end()), [path, topLevelModule](const auto& it) {
192 auto res = splitModuleNode(it.first);
193 if (topLevelModule == res.first) {
194 res.first = boost::none;
195 }
196 return res;
Václav Kubernát95b08872020-04-28 01:04:17 +0200197 });
Václav Kubernát744f57f2018-06-29 22:46:26 +0200198 return res;
Václav Kubernátbddbb172018-06-13 16:27:39 +0200199}
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200200
Václav Kubernát19a368b2020-05-27 00:05:23 +0200201struct impl_nodeType {
202
203 yang::NodeTypes operator()(const yang::container& cont)
204 {
205 if (cont.m_presence == yang::ContainerTraits::Presence) {
206 return yang::NodeTypes::PresenceContainer;
207 }
208 return yang::NodeTypes::Container;
209 }
210 yang::NodeTypes operator()(const yang::list&)
211 {
212 return yang::NodeTypes::List;
213 }
214 yang::NodeTypes operator()(const yang::leaf&)
215 {
216 return yang::NodeTypes::Leaf;
217
218 }
219 yang::NodeTypes operator()(const yang::leaflist&)
220 {
221 return yang::NodeTypes::LeafList;
222 }
223};
224
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100225yang::NodeTypes StaticSchema::nodeType(const schemaPath_& location, const ModuleNodePair& node) const
226{
227 std::string locationString = pathToSchemaString(location, Prefixes::Always);
228 auto fullName = fullNodeName(location, node);
229 try {
230 auto targetNode = children(locationString).at(fullName);
231
Václav Kubernát19a368b2020-05-27 00:05:23 +0200232 return std::visit(impl_nodeType{}, targetNode.m_nodeType);
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100233 } catch (std::out_of_range&) {
234 throw InvalidNodeException();
235 }
236}
237
Václav Kubernátabf52802020-05-19 01:31:17 +0200238std::string fullNodeName(const std::string& location, const std::string& node)
239{
240 // If the node already contains a module name, just return it.
241 if (node.find_first_of(':') != std::string::npos) {
242 return node;
243 }
244
245 // Otherwise take the module name from the first node of location.
246 return location.substr(location.find_first_not_of('/'), location.find_first_of(':') - 1) + ":" + node;
247}
248
249bool StaticSchema::isConfig(const std::string& leafPath) const
250{
251 auto locationString = stripLastNodeFromPath(leafPath);
252
253 auto node = fullNodeName(locationString, lastNodeOfSchemaPath(leafPath));
254 return children(locationString).at(node).m_configType == yang::AccessType::Writable;
255}
256
Václav Kubernát1e09bd62020-02-17 15:13:38 +0100257std::optional<std::string> StaticSchema::description([[maybe_unused]] const std::string& path) const
258{
259 throw std::runtime_error{"StaticSchema::description not implemented"};
260}
261
Václav Kubernáta1c4c9e2020-04-22 00:37:52 +0200262yang::Status StaticSchema::status([[maybe_unused]] const std::string& location) const
263{
264 throw std::runtime_error{"Internal error: StaticSchema::status(std::string) not implemented. The tests should not have called this overload."};
265}
266
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100267yang::NodeTypes StaticSchema::nodeType([[maybe_unused]] const std::string& path) const
268{
269 throw std::runtime_error{"Internal error: StaticSchema::nodeType(std::string) not implemented. The tests should not have called this overload."};
270}
Václav Kubernátad87ece2020-02-19 15:20:56 +0100271
Václav Kubernátbd5e3c22020-02-19 15:22:00 +0100272std::string StaticSchema::leafrefPath([[maybe_unused]] const std::string& leafrefPath) const
273{
274 throw std::runtime_error{"Internal error: StaticSchema::leafrefPath(std::string) not implemented. The tests should not have called this overload."};
275}
Václav Kubernátc3866792020-02-20 14:12:56 +0100276
277bool StaticSchema::leafIsKey([[maybe_unused]] const std::string& leafPath) const
278{
279 throw std::runtime_error{"Internal error: StaticSchema::leafIsKey(std::string) not implemented. The tests should not have called this overload."};
280}
Václav Kubernát6fcd0282020-02-21 16:33:08 +0100281
282std::optional<std::string> StaticSchema::leafTypeName([[maybe_unused]] const std::string& path) const
283{
284 throw std::runtime_error{"Internal error: StaticSchema::leafTypeName(std::string) not implemented. The tests should not have called this overload."};
285}
Václav Kubernát0599e9f2020-04-21 09:51:33 +0200286
Václav Kubernátb1a75c62020-04-21 15:20:16 +0200287std::optional<std::string> StaticSchema::defaultValue([[maybe_unused]] const std::string& leafPath) const
288{
289 throw std::runtime_error{"Internal error: StaticSchema::defaultValue(std::string) not implemented. The tests should not have called this overload."};
290}