blob: d2287dc3c1328af68a63d72f742fc1169a39ebab [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átbddbb172018-06-13 16:27:39 +020074void StaticSchema::addLeaf(const std::string& location, const std::string& name, const yang::LeafDataTypes& type)
75{
Václav Kubernát6a8d1d92019-04-24 20:30:36 +020076 m_nodes.at(location).emplace(name, yang::leaf{type, {}, {}, {}});
Václav Kubernáte69133a2019-11-01 19:01:34 +010077 std::string key = joinPaths(location, name);
78 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
Václav Kubernátbddbb172018-06-13 16:27:39 +020079}
80
81void StaticSchema::addLeafEnum(const std::string& location, const std::string& name, std::set<std::string> enumValues)
82{
Václav Kubernáteeb38842019-03-20 19:46:05 +010083 yang::leaf toAdd;
84 toAdd.m_type = yang::LeafDataTypes::Enum;
85 toAdd.m_enumValues = enumValues;
86 m_nodes.at(location).emplace(name, toAdd);
Václav Kubernáte69133a2019-11-01 19:01:34 +010087 std::string key = joinPaths(location, name);
88 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
Václav Kubernáteeb38842019-03-20 19:46:05 +010089}
90
91void StaticSchema::addLeafIdentityRef(const std::string& location, const std::string& name, const ModuleValuePair& base)
92{
93 assert(base.first); // base identity cannot have an empty module
94 yang::leaf toAdd;
95 toAdd.m_type = yang::LeafDataTypes::IdentityRef;
96 toAdd.m_identBase = base;
97 m_nodes.at(location).emplace(name, toAdd);
Václav Kubernáte69133a2019-11-01 19:01:34 +010098 std::string key = joinPaths(location, name);
99 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
Václav Kubernátbddbb172018-06-13 16:27:39 +0200100}
101
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200102void StaticSchema::addLeafRef(const std::string& location, const std::string& name, const std::string& source)
103{
104 yang::leaf toAdd;
105 toAdd.m_type = yang::LeafDataTypes::LeafRef;
106 toAdd.m_leafRefSource = source;
107 m_nodes.at(location).emplace(name, toAdd);
Václav Kubernáte69133a2019-11-01 19:01:34 +0100108 std::string key = joinPaths(location, name);
109 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200110}
111
Václav Kubernát744f57f2018-06-29 22:46:26 +0200112void StaticSchema::addModule(const std::string& name)
Václav Kubernátbddbb172018-06-13 16:27:39 +0200113{
Václav Kubernát744f57f2018-06-29 22:46:26 +0200114 m_modules.emplace(name);
115}
Václav Kubernátbddbb172018-06-13 16:27:39 +0200116
Václav Kubernáteeb38842019-03-20 19:46:05 +0100117void StaticSchema::addIdentity(const std::optional<ModuleValuePair>& base, const ModuleValuePair& name)
118{
119 if (base)
120 m_identities.at(base.value()).emplace(name);
121
122 m_identities.emplace(name, std::set<ModuleValuePair>());
123}
Václav Kubernát744f57f2018-06-29 22:46:26 +0200124
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200125bool StaticSchema::leafEnumHasValue(const schemaPath_& location, const ModuleNodePair& node, const std::string& value) const
Václav Kubernát744f57f2018-06-29 22:46:26 +0200126{
Václav Kubernát989b5de2019-02-20 16:28:35 +0100127 auto enums = enumValues(location, node);
128 return enums.find(value) != enums.end();
Václav Kubernátbddbb172018-06-13 16:27:39 +0200129}
130
Václav Kubernáteeb38842019-03-20 19:46:05 +0100131void StaticSchema::getIdentSet(const ModuleValuePair& ident, std::set<ModuleValuePair>& res) const
132{
133 res.insert(ident);
134 auto derivedIdentities = m_identities.at(ident);
135 for (auto it : derivedIdentities) {
136 getIdentSet(it, res);
137 }
138}
139
140const std::set<std::string> StaticSchema::validIdentities(const schemaPath_& location, const ModuleNodePair& node, const Prefixes prefixes) const
141{
Václav Kubernátefcac932020-01-10 15:26:32 +0100142 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernáteeb38842019-03-20 19:46:05 +0100143 assert(isLeaf(location, node));
144
145 const auto& child = children(locationString).at(fullNodeName(location, node));
146 const auto& leaf = boost::get<yang::leaf>(child);
147
148 std::set<ModuleValuePair> identSet;
149 getIdentSet(leaf.m_identBase, identSet);
150
151 std::set<std::string> res;
152 std::transform(identSet.begin(), identSet.end(), std::inserter(res, res.end()), [location, node, prefixes](const auto& it) {
153 auto topLevelModule = location.m_nodes.empty() ? node.first.get() : location.m_nodes.front().m_prefix.get().m_name;
154 std::string stringIdent;
155 if (prefixes == Prefixes::Always || (it.first && it.first.value() != topLevelModule)) {
156 stringIdent += it.first ? it.first.value() : topLevelModule;
157 stringIdent += ":";
158 }
159 stringIdent += it.second;
160 return stringIdent;
161 });
162
163 return res;
164}
165
166bool StaticSchema::leafIdentityIsValid(const schemaPath_& location, const ModuleNodePair& node, const ModuleValuePair& value) const
167{
168 auto identities = validIdentities(location, node, Prefixes::Always);
169
170 auto topLevelModule = location.m_nodes.empty() ? node.first.get() : location.m_nodes.front().m_prefix.get().m_name;
171 auto identModule = value.first ? value.first.value() : topLevelModule;
Václav Kubernát1bf704e2019-04-12 13:30:50 +0200172 return std::any_of(identities.begin(), identities.end(), [toFind = identModule + ":" + value.second](const auto& x) { return x == toFind; });
Václav Kubernáteeb38842019-03-20 19:46:05 +0100173}
174
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200175std::string lastNodeOfSchemaPath(const std::string& path)
176{
177 std::string res = path;
178 auto pos = res.find_last_of('/');
Václav Kubernátefcac932020-01-10 15:26:32 +0100179 if (pos == 0) { // path had only one path fragment - "/something:something"
180 res.erase(0, 1);
181 return res;
182 }
183 if (pos != res.npos) { // path had more fragments
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200184 res.erase(0, pos);
Václav Kubernátefcac932020-01-10 15:26:32 +0100185 return res;
186 }
187
188 // path was empty
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200189 return res;
190}
191
192yang::LeafDataTypes StaticSchema::leafrefBase(const schemaPath_& location, const ModuleNodePair& node) const
193{
Václav Kubernátefcac932020-01-10 15:26:32 +0100194 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát6a8d1d92019-04-24 20:30:36 +0200195 auto leaf{boost::get<yang::leaf>(children(locationString).at(fullNodeName(location, node)))};
196 auto locationOfSource = stripLastNodeFromPath(leaf.m_leafRefSource);
197 auto nameOfSource = lastNodeOfSchemaPath(leaf.m_leafRefSource);
198 return boost::get<yang::leaf>(children(locationOfSource).at(nameOfSource)).m_type;
199}
200
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200201yang::LeafDataTypes StaticSchema::leafType(const schemaPath_& location, const ModuleNodePair& node) const
Václav Kubernátbddbb172018-06-13 16:27:39 +0200202{
Václav Kubernátefcac932020-01-10 15:26:32 +0100203 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát744f57f2018-06-29 22:46:26 +0200204 return boost::get<yang::leaf>(children(locationString).at(fullNodeName(location, node))).m_type;
205}
206
Václav Kubernát989b5de2019-02-20 16:28:35 +0100207const std::set<std::string> StaticSchema::enumValues(const schemaPath_& location, const ModuleNodePair& node) const
208{
Václav Kubernátefcac932020-01-10 15:26:32 +0100209 std::string locationString = pathToSchemaString(location, Prefixes::Always);
Václav Kubernát989b5de2019-02-20 16:28:35 +0100210 assert(isLeaf(location, node));
211
212 const auto& child = children(locationString).at(fullNodeName(location, node));
213 const auto& leaf = boost::get<yang::leaf>(child);
214 return leaf.m_enumValues;
215}
216
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200217// We do not test StaticSchema, so we don't need to implement recursive childNodes
218// for this class.
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200219std::set<std::string> StaticSchema::childNodes(const schemaPath_& path, const Recursion) const
Václav Kubernát744f57f2018-06-29 22:46:26 +0200220{
Václav Kubernátefcac932020-01-10 15:26:32 +0100221 std::string locationString = pathToSchemaString(path, Prefixes::Always);
Václav Kubernát744f57f2018-06-29 22:46:26 +0200222 std::set<std::string> res;
223
224 auto childrenRef = children(locationString);
225
Václav Kubernát90de9502019-11-20 17:19:44 +0100226 std::transform(childrenRef.begin(), childrenRef.end(), std::inserter(res, res.end()), [](auto it) { return it.first; });
Václav Kubernát744f57f2018-06-29 22:46:26 +0200227 return res;
Václav Kubernátbddbb172018-06-13 16:27:39 +0200228}
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200229
230// We do not test StaticSchema, so we don't need to implement recursive moduleNodes
231// for this class.
232std::set<std::string> StaticSchema::moduleNodes(const module_& module, const Recursion) const
233{
234 std::set<std::string> res;
235 auto topLevelNodes = m_nodes.at("");
236 auto modulePlusColon = module.m_name + ":";
237 for (const auto& it : topLevelNodes) {
238 if (boost::algorithm::starts_with(it.first, modulePlusColon)) {
239 res.insert(it.first);
240 }
241 }
242 return res;
243}
Václav Kubernát34ee85a2020-02-18 17:12:12 +0100244
245yang::NodeTypes StaticSchema::nodeType(const schemaPath_& location, const ModuleNodePair& node) const
246{
247 std::string locationString = pathToSchemaString(location, Prefixes::Always);
248 auto fullName = fullNodeName(location, node);
249 try {
250 auto targetNode = children(locationString).at(fullName);
251
252 if (targetNode.type() == typeid(yang::container)) {
253 if (boost::get<yang::container>(targetNode).m_presence == yang::ContainerTraits::Presence) {
254 return yang::NodeTypes::PresenceContainer;
255 }
256 return yang::NodeTypes::Container;
257 }
258
259 if (targetNode.type() == typeid(yang::list)) {
260 return yang::NodeTypes::List;
261 }
262
263 if (targetNode.type() == typeid(yang::leaf)) {
264 return yang::NodeTypes::Leaf;
265 }
266
267 throw std::runtime_error{"YangSchema::nodeType: unsupported type"};
268
269 } catch (std::out_of_range&) {
270 throw InvalidNodeException();
271 }
272}
273
274yang::NodeTypes StaticSchema::nodeType([[maybe_unused]] const std::string& path) const
275{
276 throw std::runtime_error{"Internal error: StaticSchema::nodeType(std::string) not implemented. The tests should not have called this overload."};
277}