blob: 066dc3b0a9d0274f029713b01d291f0294cb75c8 [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át48fc3832018-05-28 14:21:22 +020014Schema::Schema()
15{
16 m_nodes.emplace("", std::unordered_map<std::string, NodeType>());
17}
18
19const std::unordered_map<std::string, NodeType>& Schema::children(const std::string& name) const
20{
21 return m_nodes.at(name);
22}
23
24bool Schema::nodeExists(const std::string& location, const std::string& name) const
25{
26 if (name.empty())
27 return true;
28 const auto& childrenRef = children(location);
29
30 return childrenRef.find(name) != childrenRef.end();
31}
32
33bool Schema::isContainer(const path_& location, const std::string& name) const
34{
Václav Kubernát24df80e2018-06-06 15:18:03 +020035 std::string locationString = pathToSchemaString(location);
Václav Kubernát48fc3832018-05-28 14:21:22 +020036 if (!nodeExists(locationString, name))
37 return false;
38
39 return children(locationString).at(name).type() == typeid(yang::container);
40}
41
Václav Kubernátb61336d2018-05-28 17:35:03 +020042void Schema::addContainer(const std::string& location, const std::string& name, yang::ContainerTraits isPresence)
Václav Kubernát48fc3832018-05-28 14:21:22 +020043{
Václav Kubernátb61336d2018-05-28 17:35:03 +020044 m_nodes.at(location).emplace(name, yang::container{isPresence});
Václav Kubernát48fc3832018-05-28 14:21:22 +020045
46 //create a new set of children for the new node
47 std::string key = joinPaths(location, name);
48 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
49}
50
51
52bool Schema::listHasKey(const path_& location, const std::string& name, const std::string& key) const
53{
Václav Kubernát24df80e2018-06-06 15:18:03 +020054 std::string locationString = pathToSchemaString(location);
Václav Kubernát48fc3832018-05-28 14:21:22 +020055 assert(isList(location, name));
56
57 const auto& child = children(locationString).at(name);
58 const auto& list = boost::get<yang::list>(child);
59 return list.m_keys.find(key) != list.m_keys.end();
60}
61
62const std::set<std::string>& Schema::listKeys(const path_& location, const std::string& name) const
63{
Václav Kubernát24df80e2018-06-06 15:18:03 +020064 std::string locationString = pathToSchemaString(location);
Václav Kubernát48fc3832018-05-28 14:21:22 +020065 assert(isList(location, name));
66
67 const auto& child = children(locationString).at(name);
68 const auto& list = boost::get<yang::list>(child);
69 return list.m_keys;
70}
71
72bool Schema::isList(const path_& location, const std::string& name) const
73{
Václav Kubernát24df80e2018-06-06 15:18:03 +020074 std::string locationString = pathToSchemaString(location);
Václav Kubernát48fc3832018-05-28 14:21:22 +020075 if (!nodeExists(locationString, name))
76 return false;
77 const auto& child = children(locationString).at(name);
78 if (child.type() != typeid(yang::list))
79 return false;
80
81 return true;
82}
83
84void Schema::addList(const std::string& location, const std::string& name, const std::set<std::string>& keys)
85{
86 m_nodes.at(location).emplace(name, yang::list{keys});
87
88 m_nodes.emplace(name, std::unordered_map<std::string, NodeType>());
89}
Václav Kubernátb61336d2018-05-28 17:35:03 +020090
91bool Schema::isPresenceContainer(const path_& location, const std::string& name) const
92{
93 if (!isContainer(location, name))
94 return false;
Václav Kubernát24df80e2018-06-06 15:18:03 +020095 std::string locationString = pathToSchemaString(location);
Václav Kubernátb61336d2018-05-28 17:35:03 +020096 return boost::get<yang::container>(children(locationString).at(name)).m_presence == yang::ContainerTraits::Presence;
97}
Václav Kubernát07204242018-06-04 18:12:09 +020098
Václav Kubernátebca2552018-06-08 19:06:02 +020099void Schema::addLeaf(const std::string& location, const std::string& name, const yang::LeafDataTypes& type)
Václav Kubernát07204242018-06-04 18:12:09 +0200100{
Václav Kubernátebca2552018-06-08 19:06:02 +0200101 m_nodes.at(location).emplace(name, yang::leaf{type, {}});
102}
103
104void Schema::addLeafEnum(const std::string& location, const std::string& name, std::set<std::string> enumValues)
105{
106 m_nodes.at(location).emplace(name, yang::leaf{yang::LeafDataTypes::Enum, enumValues});
107}
108
109bool Schema::leafEnumHasValue(const path_& location, const std::string& name, const std::string& value) const
110{
111 std::string locationString = pathToSchemaString(location);
112 assert(isLeaf(location, name));
113
114 const auto& child = children(locationString).at(name);
115 const auto& list = boost::get<yang::leaf>(child);
116 return list.m_enumValues.find(value) != list.m_enumValues.end();
Václav Kubernát07204242018-06-04 18:12:09 +0200117}
118
119bool Schema::isLeaf(const path_& location, const std::string& name) const
120{
Václav Kubernát24df80e2018-06-06 15:18:03 +0200121 std::string locationString = pathToSchemaString(location);
Václav Kubernát07204242018-06-04 18:12:09 +0200122 if (!nodeExists(locationString, name))
123 return false;
124
125 return children(locationString).at(name).type() == typeid(yang::leaf);
126}
Václav Kubernátebca2552018-06-08 19:06:02 +0200127
128yang::LeafDataTypes Schema::leafType(const path_& location, const std::string& name) const
129{
130 std::string locationString = pathToSchemaString(location);
131 return boost::get<yang::leaf>(children(locationString).at(name)).m_type;
132}