Kea 2.7.0
translator_database.cc
Go to the documentation of this file.
1// Copyright (C) 2018-2024 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
10#include <yang/yang_models.h>
11
12#include <sstream>
13
14using namespace std;
15using namespace isc::data;
16using namespace libyang;
17using namespace sysrepo;
18
19namespace isc {
20namespace yang {
21
22TranslatorDatabase::TranslatorDatabase(Session session, const string& model)
23 : Translator(session, model) {
24}
25
27TranslatorDatabase::getDatabase(DataNode const& data_node) {
28 try {
29 if ((model_ == KEA_DHCP4_SERVER) ||
30 (model_ == KEA_DHCP6_SERVER)) {
31 return (getDatabaseKea(data_node));
32 }
33 } catch (Error const& ex) {
35 "getting database access: " << ex.what());
36 }
38 "getDatabase not implemented for the model: " << model_);
39}
40
43 try {
44 return getDatabase(findXPath(xpath));
45 } catch (NetconfError const&) {
46 return ElementPtr();
47 }
48}
49
51TranslatorDatabase::getDatabaseKea(DataNode const& data_node) {
53
54 getMandatoryDivergingLeaf(result, data_node, "type", "database-type");
55
56 checkAndGetLeaf(result, data_node, "cert-file");
57 checkAndGetLeaf(result, data_node, "cipher-list");
58 checkAndGetLeaf(result, data_node, "connect-timeout");
59 checkAndGetLeaf(result, data_node, "host");
60 checkAndGetLeaf(result, data_node, "key-file");
61 checkAndGetLeaf(result, data_node, "lfc-interval");
62 checkAndGetLeaf(result, data_node, "max-reconnect-tries");
63 checkAndGetLeaf(result, data_node, "max-row-errors");
64 checkAndGetLeaf(result, data_node, "name");
65 checkAndGetLeaf(result, data_node, "on-fail");
66 checkAndGetLeaf(result, data_node, "retry-on-startup");
67 checkAndGetLeaf(result, data_node, "password");
68 checkAndGetLeaf(result, data_node, "persist");
69 checkAndGetLeaf(result, data_node, "port");
70 checkAndGetLeaf(result, data_node, "read-timeout");
71 checkAndGetLeaf(result, data_node, "readonly");
72 checkAndGetLeaf(result, data_node, "reconnect-wait-time");
73 checkAndGetLeaf(result, data_node, "tcp-user-timeout");
74 checkAndGetLeaf(result, data_node, "trust-anchor");
75 checkAndGetLeaf(result, data_node, "user");
76 checkAndGetLeaf(result, data_node, "write-timeout");
77
78 checkAndGetAndJsonifyLeaf(result, data_node, "user-context");
79
80 return (result->empty() ? ElementPtr() : result);
81}
82
83void
85 ConstElementPtr elem,
86 bool skip) {
87 try {
88 if ((model_ == KEA_DHCP4_SERVER) ||
89 (model_ == KEA_DHCP6_SERVER)) {
90 setDatabaseKea(xpath, elem, skip);
91 } else {
93 "setDatabase not implemented for the model: " << model_);
94 }
95 } catch (Error const& ex) {
97 "setting database access '" << elem->str()
98 << "' : " << ex.what());
99 }
100}
101
102void
104 ConstElementPtr elem,
105 bool skip) {
106 if (!elem) {
107 deleteItem(xpath);
108 return;
109 }
110
111 checkAndSetLeaf(elem, xpath, "connect-timeout", LeafBaseType::Uint32);
112 checkAndSetLeaf(elem, xpath, "cert-file", LeafBaseType::String);
113 checkAndSetLeaf(elem, xpath, "cipher-list", LeafBaseType::String);
114 checkAndSetLeaf(elem, xpath, "host", LeafBaseType::String);
115 checkAndSetLeaf(elem, xpath, "key-file", LeafBaseType::String);
116 checkAndSetLeaf(elem, xpath, "lfc-interval", LeafBaseType::Uint32);
117 checkAndSetLeaf(elem, xpath, "max-reconnect-tries", LeafBaseType::Uint32);
118 checkAndSetLeaf(elem, xpath, "max-row-errors", LeafBaseType::Uint32);
119 checkAndSetLeaf(elem, xpath, "name", LeafBaseType::String);
120 checkAndSetLeaf(elem, xpath, "on-fail", LeafBaseType::String);
121 checkAndSetLeaf(elem, xpath, "retry-on-startup", LeafBaseType::Bool);
122 checkAndSetLeaf(elem, xpath, "password", LeafBaseType::String);
123 checkAndSetLeaf(elem, xpath, "persist", LeafBaseType::Bool);
124 checkAndSetLeaf(elem, xpath, "port", LeafBaseType::Uint16);
125 checkAndSetLeaf(elem, xpath, "readonly", LeafBaseType::Bool);
126 checkAndSetLeaf(elem, xpath, "read-timeout", LeafBaseType::Uint32);
127 checkAndSetLeaf(elem, xpath, "reconnect-wait-time", LeafBaseType::Uint32);
128 checkAndSetLeaf(elem, xpath, "tcp-user-timeout", LeafBaseType::Uint32);
129 checkAndSetLeaf(elem, xpath, "trust-anchor", LeafBaseType::String);
130 checkAndSetLeaf(elem, xpath, "user", LeafBaseType::String);
131 checkAndSetLeaf(elem, xpath, "write-timeout", LeafBaseType::Uint32);
132
133 checkAndSetUserContext(elem, xpath);
134
135 if (!skip) {
136 setMandatoryDivergingLeaf(elem, xpath, "type", "database-type", LeafBaseType::String);
137 }
138}
139
141 const string& model)
142 : Translator(session, model),
143 TranslatorDatabase(session, model) {
144}
145
147TranslatorDatabases::getDatabases(DataNode const& data_node,
148 string const& xpath) {
149 try {
150 if ((model_ == KEA_DHCP4_SERVER) ||
151 (model_ == KEA_DHCP6_SERVER)) {
152 return (getDatabasesKea(data_node, xpath));
153 }
154 } catch (Error const& ex) {
156 "getting database accesses: " << ex.what());
157 }
159 "getDatabases not implemented for the model: " << model_);
160}
161
164 try {
165 return getDatabases(findXPath(xpath), xpath);
166 } catch (NetconfError const&) {
167 return ElementPtr();
168 }
169}
170
172TranslatorDatabases::getDatabasesKea(DataNode const& data_node, string const& xpath) {
173 return getList<TranslatorDatabase>(data_node, xpath, *this,
175}
176
177void
179 try {
180 if ((model_ == KEA_DHCP4_SERVER) ||
181 (model_ == KEA_DHCP6_SERVER)) {
182 setDatabasesKea(xpath, elem);
183 } else {
185 "setDatabases not implemented for the model: "
186 << model_);
187 }
188 } catch (Error const& ex) {
190 "setting database accesses '" << elem->str()
191 << "' : " << ex.what());
192 }
193}
194
195void
197 ConstElementPtr elem) {
198 if (!elem) {
199 deleteItem(xpath);
200 return;
201 }
202 for (size_t i = 0; i < elem->size(); ++i) {
203 ElementPtr database = elem->getNonConst(i);
204 if (!database->contains("type")) {
205 isc_throw(BadValue, "database without type: " << database->str());
206 }
207 string type = database->get("type")->stringValue();
208 ostringstream key;
209 key << xpath << "[database-type='" << type << "']";
210 setDatabase(key.str(), database, true);
211 }
212}
213
214} // namespace yang
215} // namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic exception that is thrown when a function is not implemented.
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
Database access translation between YANG and JSON.
isc::data::ElementPtr getDatabaseKea(libyang::DataNode const &data_node)
getDatabase JSON for kea-dhcp[46]-server models.
isc::data::ElementPtr getDatabase(libyang::DataNode const &data_node)
Translate a database access from YANG to JSON.
TranslatorDatabase(sysrepo::Session session, const std::string &model)
Constructor.
isc::data::ElementPtr getDatabaseFromAbsoluteXpath(std::string const &xpath)
Translate a database access from YANG to JSON.
void setDatabase(const std::string &xpath, isc::data::ConstElementPtr elem, bool skip=false)
Translate and set database access from JSON to YANG.
void setDatabaseKea(const std::string &xpath, isc::data::ConstElementPtr elem, bool skip)
setDatabase for kea-dhcp[46]-server models.
void setDatabasesKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setDatabases for kea-dhcp[46]-server models.
isc::data::ElementPtr getDatabasesFromAbsoluteXpath(std::string const &xpath)
Translate database accesses from YANG to JSON.
void setDatabases(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set database accesses from JSON to YANG.
isc::data::ElementPtr getDatabases(libyang::DataNode const &data_node, std::string const &xpath)
Translate database accesses from YANG to JSON.
TranslatorDatabases(sysrepo::Session session, const std::string &model)
Constructor.
isc::data::ElementPtr getDatabasesKea(libyang::DataNode const &data_node, std::string const &xpath)
getDatabases JSON for kea-dhcp[46]-server models.
Between YANG and JSON translator class for basic values.
Definition translator.h:23
void getMandatoryDivergingLeaf(isc::data::ElementPtr &storage, libyang::DataNode const &data_node, std::string const &name, std::string const &yang_name) const
Retrieves a child YANG data node identified by one name from the given parent YANG container node and...
void checkAndSetLeaf(isc::data::ConstElementPtr const &from, std::string const &xpath, std::string const &name, libyang::LeafBaseType const type)
Get an element from given ElementPtr node and set it in sysrepo at given xpath.
Definition translator.cc:63
void setMandatoryDivergingLeaf(isc::data::ConstElementPtr const &from, std::string const &xpath, std::string const &name, std::string const &yang_name, libyang::LeafBaseType const type)
Get an element from given ElementPtr node and set it in sysrepo at given xpath.
libyang::DataNode findXPath(std::string const &xpath) const
Retrieves a YANG data node by xpath.
void checkAndGetLeaf(isc::data::ElementPtr &storage, libyang::DataNode const &data_node, std::string const &name) const
Retrieves a child YANG data node identified by name from the given parent YANG container node and sto...
Definition translator.cc:32
void checkAndSetUserContext(isc::data::ConstElementPtr const &from, std::string const &xpath)
Get an element from given ElementPtr node and set it in sysrepo at given xpath.
Definition translator.cc:99
void deleteItem(const std::string &xpath)
Delete basic value from YANG.
std::string model_
The model.
Definition translator.h:427
void checkAndGetAndJsonifyLeaf(isc::data::ElementPtr &storage, libyang::DataNode const &data_node, const std::string &name) const
Retrieves a child YANG data node identified by name from the given parent YANG container node,...
Definition translator.cc:53
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
Defines the logger used by the top-level component of kea-lfc.
Generic NETCONF error.