Kea: translator_database.cc Source File

| Kea 3.2.0-git

translator_database.cc
Go to the documentation of this file.
1// Copyright (C) 2018-2026 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
9#include <yang/translator_database.h>
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
26 ElementPtr
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) {
34 isc_throw(NetconfError,
35 "getting database access: " << ex.what());
36 }
37 isc_throw(NotImplemented,
38 "getDatabase not implemented for the model: " << model_);
39}
40
41 ElementPtr
42TranslatorDatabase::getDatabaseFromAbsoluteXpath(string const& xpath) {
43 try {
44 return getDatabase(findXPath(xpath));
45 } catch (NetconfError const&) {
46 return ElementPtr();
47 }
48}
49
50 ElementPtr
51TranslatorDatabase::getDatabaseKea(DataNode const& data_node) {
52 ElementPtr result = Element::createMap();
53
54 checkAndGetDivergingLeaf(result, data_node, "type", "database-type");
55 checkAndGetLeaf(result, data_node, "cert-file");
56 checkAndGetLeaf(result, data_node, "cipher-list");
57 checkAndGetLeaf(result, data_node, "connect-timeout");
58 checkAndGetLeaf(result, data_node, "host");
59 checkAndGetLeaf(result, data_node, "key-file");
60 checkAndGetLeaf(result, data_node, "lfc-interval");
61 checkAndGetLeaf(result, data_node, "max-reconnect-tries");
62 checkAndGetLeaf(result, data_node, "max-row-errors");
63 checkAndGetLeaf(result, data_node, "name");
64 checkAndGetLeaf(result, data_node, "on-fail");
65 checkAndGetLeaf(result, data_node, "retry-on-startup");
66 checkAndGetLeaf(result, data_node, "password");
67 checkAndGetLeaf(result, data_node, "persist");
68 checkAndGetLeaf(result, data_node, "port");
69 checkAndGetLeaf(result, data_node, "read-timeout");
70 checkAndGetLeaf(result, data_node, "readonly");
71 checkAndGetLeaf(result, data_node, "reconnect-wait-time");
72 checkAndGetLeaf(result, data_node, "tcp-user-timeout");
73 checkAndGetLeaf(result, data_node, "trust-anchor");
74 checkAndGetLeaf(result, data_node, "user");
75 checkAndGetLeaf(result, data_node, "write-timeout");
76 checkAndGetLeaf(result, data_node, "ssl-mode");
77
78 checkAndGetAndJsonifyLeaf(result, data_node, "user-context");
79
80 return (result->empty() ? ElementPtr() : result);
81}
82
83void
84TranslatorDatabase::setDatabase(string const& xpath, ConstElementPtr elem, bool has_mandatory_key) {
85 try {
86 if ((model_ == KEA_DHCP4_SERVER) ||
87 (model_ == KEA_DHCP6_SERVER)) {
88 setDatabaseKea(xpath, elem, has_mandatory_key);
89 } else {
90 isc_throw(NotImplemented,
91 "setDatabase not implemented for the model: " << model_);
92 }
93 } catch (Error const& ex) {
94 isc_throw(NetconfError,
95 "setting database access '" << elem->str()
96 << "' : " << ex.what());
97 }
98}
99
100void
101TranslatorDatabase::setDatabaseKea(string const& xpath, ConstElementPtr elem, bool has_mandatory_key) {
102 if (!elem) {
103 deleteItem(xpath);
104 return;
105 }
106
107 if (has_mandatory_key) {
108 // Set the list element. This is important in case we have no other elements except the key.
109 setItem(xpath, ElementPtr(), LeafBaseType::Unknown);
110 } else {
111 checkAndSetDivergingLeaf(elem, xpath, "type", "database-type", LeafBaseType::String);
112 }
113
114 checkAndSetLeaf(elem, xpath, "connect-timeout", LeafBaseType::Uint32);
115 checkAndSetLeaf(elem, xpath, "cert-file", LeafBaseType::String);
116 checkAndSetLeaf(elem, xpath, "cipher-list", LeafBaseType::String);
117 checkAndSetLeaf(elem, xpath, "host", LeafBaseType::String);
118 checkAndSetLeaf(elem, xpath, "key-file", LeafBaseType::String);
119 checkAndSetLeaf(elem, xpath, "lfc-interval", LeafBaseType::Uint32);
120 checkAndSetLeaf(elem, xpath, "max-reconnect-tries", LeafBaseType::Uint32);
121 checkAndSetLeaf(elem, xpath, "max-row-errors", LeafBaseType::Uint32);
122 checkAndSetLeaf(elem, xpath, "name", LeafBaseType::String);
123 checkAndSetLeaf(elem, xpath, "on-fail", LeafBaseType::String);
124 checkAndSetLeaf(elem, xpath, "retry-on-startup", LeafBaseType::Bool);
125 checkAndSetLeaf(elem, xpath, "password", LeafBaseType::String);
126 checkAndSetLeaf(elem, xpath, "persist", LeafBaseType::Bool);
127 checkAndSetLeaf(elem, xpath, "port", LeafBaseType::Uint16);
128 checkAndSetLeaf(elem, xpath, "readonly", LeafBaseType::Bool);
129 checkAndSetLeaf(elem, xpath, "read-timeout", LeafBaseType::Uint32);
130 checkAndSetLeaf(elem, xpath, "reconnect-wait-time", LeafBaseType::Uint32);
131 checkAndSetLeaf(elem, xpath, "tcp-user-timeout", LeafBaseType::Uint32);
132 checkAndSetLeaf(elem, xpath, "trust-anchor", LeafBaseType::String);
133 checkAndSetLeaf(elem, xpath, "user", LeafBaseType::String);
134 checkAndSetLeaf(elem, xpath, "write-timeout", LeafBaseType::Uint32);
135 checkAndSetLeaf(elem, xpath, "ssl-mode", LeafBaseType::String);
136
137 checkAndSetUserContext(elem, xpath);
138}
139
140TranslatorDatabases::TranslatorDatabases(Session session,
141 const string& model)
142 : Translator(session, model),
143 TranslatorDatabase(session, model) {
144}
145
146 ElementPtr
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) {
155 isc_throw(NetconfError,
156 "getting database accesses: " << ex.what());
157 }
158 isc_throw(NotImplemented,
159 "getDatabases not implemented for the model: " << model_);
160}
161
162 ElementPtr
163TranslatorDatabases::getDatabasesFromAbsoluteXpath(string const& xpath) {
164 try {
165 return getDatabases(findXPath(xpath), xpath);
166 } catch (NetconfError const&) {
167 return ElementPtr();
168 }
169}
170
171 ElementPtr
172TranslatorDatabases::getDatabasesKea(DataNode const& data_node, string const& xpath) {
173 return getList<TranslatorDatabase>(data_node, xpath, *this,
174 &TranslatorDatabase::getDatabase);
175}
176
177void
178TranslatorDatabases::setDatabases(string const& xpath, ConstElementPtr elem) {
179 try {
180 if ((model_ == KEA_DHCP4_SERVER) ||
181 (model_ == KEA_DHCP6_SERVER)) {
182 setDatabasesKea(xpath, elem);
183 } else {
184 isc_throw(NotImplemented,
185 "setDatabases not implemented for the model: "
186 << model_);
187 }
188 } catch (Error const& ex) {
189 isc_throw(NetconfError,
190 "setting database accesses '" << elem->str()
191 << "' : " << ex.what());
192 }
193}
194
195void
196TranslatorDatabases::setDatabasesKea(string const& xpath,
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, /* has_mandatory_key = */ true);
211 }
212}
213
214} // namespace yang
215} // namespace isc
Element::createMap
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:354
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition exceptions/exceptions.h:132
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition exceptions/exceptions.cc:32
isc::NotImplemented
A generic exception that is thrown when a function is not implemented.
Definition exceptions/exceptions.h:166
isc::yang::TranslatorDatabase::getDatabaseKea
isc::data::ElementPtr getDatabaseKea(libyang::DataNode const &data_node)
getDatabase JSON for kea-dhcp[46]-server models.
Definition translator_database.cc:51
isc::yang::TranslatorDatabase::getDatabase
isc::data::ElementPtr getDatabase(libyang::DataNode const &data_node)
Translate a database access from YANG to JSON.
Definition translator_database.cc:27
isc::yang::TranslatorDatabase::setDatabase
void setDatabase(const std::string &xpath, isc::data::ConstElementPtr elem, bool has_mandatory_key)
Translate and set database access from JSON to YANG.
Definition translator_database.cc:84
isc::yang::TranslatorDatabase::TranslatorDatabase
TranslatorDatabase(sysrepo::Session session, const std::string &model)
Constructor.
Definition translator_database.cc:22
isc::yang::TranslatorDatabase::getDatabaseFromAbsoluteXpath
isc::data::ElementPtr getDatabaseFromAbsoluteXpath(std::string const &xpath)
Translate a database access from YANG to JSON.
Definition translator_database.cc:42
isc::yang::TranslatorDatabase::setDatabaseKea
void setDatabaseKea(const std::string &xpath, isc::data::ConstElementPtr elem, bool has_mandatory_key)
setDatabase for kea-dhcp[46]-server models.
Definition translator_database.cc:101
isc::yang::TranslatorDatabases::setDatabasesKea
void setDatabasesKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setDatabases for kea-dhcp[46]-server models.
Definition translator_database.cc:196
isc::yang::TranslatorDatabases::getDatabasesFromAbsoluteXpath
isc::data::ElementPtr getDatabasesFromAbsoluteXpath(std::string const &xpath)
Translate database accesses from YANG to JSON.
Definition translator_database.cc:163
isc::yang::TranslatorDatabases::setDatabases
void setDatabases(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set database accesses from JSON to YANG.
Definition translator_database.cc:178
isc::yang::TranslatorDatabases::getDatabases
isc::data::ElementPtr getDatabases(libyang::DataNode const &data_node, std::string const &xpath)
Translate database accesses from YANG to JSON.
Definition translator_database.cc:147
isc::yang::TranslatorDatabases::TranslatorDatabases
TranslatorDatabases(sysrepo::Session session, const std::string &model)
Constructor.
Definition translator_database.cc:140
isc::yang::TranslatorDatabases::getDatabasesKea
isc::data::ElementPtr getDatabasesKea(libyang::DataNode const &data_node, std::string const &xpath)
getDatabases JSON for kea-dhcp[46]-server models.
Definition translator_database.cc:172
isc::yang::Translator
Between YANG and JSON translator class for basic values.
Definition translator.h:30
isc::yang::Translator::getList
isc::data::ElementPtr getList(libyang::DataNode const &data_node, std::string const &xpath, T &t, isc::data::ElementPtr(T::*f)(libyang::DataNode const &)) const
Retrieve a list as ElementPtr from sysrepo from a certain xpath.
Definition translator.h:281
isc::yang::Translator::checkAndSetLeaf
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
isc::yang::Translator::findXPath
libyang::DataNode findXPath(std::string const &xpath) const
Retrieves a YANG data node by xpath.
Definition translator.cc:132
isc::yang::Translator::checkAndGetLeaf
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
isc::yang::Translator::checkAndSetUserContext
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
isc::yang::Translator::deleteItem
void deleteItem(const std::string &xpath)
Delete basic value from YANG.
Definition translator.cc:120
isc::yang::Translator::checkAndGetDivergingLeaf
void checkAndGetDivergingLeaf(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 name from the given parent YANG container node and sto...
Definition translator.cc:42
isc::yang::Translator::setItem
void setItem(const std::string &xpath, isc::data::ConstElementPtr const elem, libyang::LeafBaseType const type)
Translate and set basic value from JSON to YANG.
Definition translator.cc:231
isc::yang::Translator::model_
std::string model_
The model.
Definition translator.h:434
isc::yang::Translator::checkAndSetDivergingLeaf
void checkAndSetDivergingLeaf(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.
Definition translator.cc:74
isc::yang::Translator::checkAndGetAndJsonifyLeaf
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
isc::yang::Translator::Translator
Translator(sysrepo::Session session, const std::string &model)
Constructor.
Definition translator.cc:27
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
isc::data
Definition base_stamped_element.cc:12
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
isc::yang
Definition adaptor.cc:17
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18
isc::yang::NetconfError
Generic NETCONF error.
Definition netconf_error.h:30
translator_database.h
yang_models.h