blob: 9832c451bbe3b677361c369c338092626988e730 [file] [log] [blame]
Václav Kubernát48fc3832018-05-28 14:21:22 +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át48fc3832018-05-28 14:21:22 +02009#include "schema.hpp"
10#include "utils.hpp"
11
12InvalidNodeException::~InvalidNodeException() = default;
13
Václav Kubernátf2e463f2018-05-28 15:51:08 +020014struct nodeToSchemaString : public boost::static_visitor<std::string> {
15 std::string operator()(const nodeup_&) const
16 {
17 return "..";
18 }
19 template <class T>
20 std::string operator()(const T& node) const
21 {
22 return node.m_name;
23 }
24};
Václav Kubernát48fc3832018-05-28 14:21:22 +020025
26std::string pathToString(const path_& path)
27{
28 std::string res;
29 for (const auto it : path.m_nodes)
Václav Kubernátf2e463f2018-05-28 15:51:08 +020030 res = joinPaths(res, boost::apply_visitor(nodeToSchemaString(), it));
Václav Kubernát48fc3832018-05-28 14:21:22 +020031 return res;
32}
33
34Schema::Schema()
35{
36 m_nodes.emplace("", std::unordered_map<std::string, NodeType>());
37}
38
39const std::unordered_map<std::string, NodeType>& Schema::children(const std::string& name) const
40{
41 return m_nodes.at(name);
42}
43
44bool Schema::nodeExists(const std::string& location, const std::string& name) const
45{
46 if (name.empty())
47 return true;
48 const auto& childrenRef = children(location);
49
50 return childrenRef.find(name) != childrenRef.end();
51}
52
53bool Schema::isContainer(const path_& location, const std::string& name) const
54{
55 std::string locationString = pathToString(location);
56 if (!nodeExists(locationString, name))
57 return false;
58
59 return children(locationString).at(name).type() == typeid(yang::container);
60}
61
Václav Kubernátb61336d2018-05-28 17:35:03 +020062void Schema::addContainer(const std::string& location, const std::string& name, yang::ContainerTraits isPresence)
Václav Kubernát48fc3832018-05-28 14:21:22 +020063{
Václav Kubernátb61336d2018-05-28 17:35:03 +020064 m_nodes.at(location).emplace(name, yang::container{isPresence});
Václav Kubernát48fc3832018-05-28 14:21:22 +020065
66 //create a new set of children for the new node
67 std::string key = joinPaths(location, name);
68 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
69}
70
71
72bool Schema::listHasKey(const path_& location, const std::string& name, const std::string& key) const
73{
74 std::string locationString = pathToString(location);
75 assert(isList(location, name));
76
77 const auto& child = children(locationString).at(name);
78 const auto& list = boost::get<yang::list>(child);
79 return list.m_keys.find(key) != list.m_keys.end();
80}
81
82const std::set<std::string>& Schema::listKeys(const path_& location, const std::string& name) const
83{
84 std::string locationString = pathToString(location);
85 assert(isList(location, name));
86
87 const auto& child = children(locationString).at(name);
88 const auto& list = boost::get<yang::list>(child);
89 return list.m_keys;
90}
91
92bool Schema::isList(const path_& location, const std::string& name) const
93{
94 std::string locationString = pathToString(location);
95 if (!nodeExists(locationString, name))
96 return false;
97 const auto& child = children(locationString).at(name);
98 if (child.type() != typeid(yang::list))
99 return false;
100
101 return true;
102}
103
104void Schema::addList(const std::string& location, const std::string& name, const std::set<std::string>& keys)
105{
106 m_nodes.at(location).emplace(name, yang::list{keys});
107
108 m_nodes.emplace(name, std::unordered_map<std::string, NodeType>());
109}
Václav Kubernátb61336d2018-05-28 17:35:03 +0200110
111bool Schema::isPresenceContainer(const path_& location, const std::string& name) const
112{
113 if (!isContainer(location, name))
114 return false;
115 std::string locationString = pathToString(location);
116 return boost::get<yang::container>(children(locationString).at(name)).m_presence == yang::ContainerTraits::Presence;
117}
Václav Kubernát07204242018-06-04 18:12:09 +0200118
119void Schema::addLeaf(const std::string& location, const std::string& name)
120{
121 m_nodes.at(location).emplace(name, yang::leaf{});
122}
123
124bool Schema::isLeaf(const path_& location, const std::string& name) const
125{
126 std::string locationString = pathToString(location);
127 if (!nodeExists(locationString, name))
128 return false;
129
130 return children(locationString).at(name).type() == typeid(yang::leaf);
131}