blob: 95cd4ae9c69c7e8d057f08d173ba4b6ada7a4329 [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
99void Schema::addLeaf(const std::string& location, const std::string& name)
100{
101 m_nodes.at(location).emplace(name, yang::leaf{});
102}
103
104bool Schema::isLeaf(const path_& location, const std::string& name) const
105{
Václav Kubernát24df80e2018-06-06 15:18:03 +0200106 std::string locationString = pathToSchemaString(location);
Václav Kubernát07204242018-06-04 18:12:09 +0200107 if (!nodeExists(locationString, name))
108 return false;
109
110 return children(locationString).at(name).type() == typeid(yang::leaf);
111}