| Kea 3.0.2-git

translator_database.cc
Go to the documentation of this file.
1// Copyright (C) 2018-2025 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
14 using namespace std;
15 using namespace isc::data;
16 using namespace libyang;
17 using namespace sysrepo;
18
19 namespace isc {
20 namespace yang {
21
22 TranslatorDatabase::TranslatorDatabase(Session session, const string& model)
23 : Translator(session, model) {
24}
25
26 ElementPtr
27 TranslatorDatabase::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
42 TranslatorDatabase::getDatabaseFromAbsoluteXpath(string const& xpath) {
43 try {
44 return getDatabase(findXPath(xpath));
45 } catch (NetconfError const&) {
46 return ElementPtr();
47 }
48}
49
50 ElementPtr
51 TranslatorDatabase::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
77 checkAndGetAndJsonifyLeaf(result, data_node, "user-context");
78
79 return (result->empty() ? ElementPtr() : result);
80}
81
82 void
83 TranslatorDatabase::setDatabase(string const& xpath, ConstElementPtr elem, bool has_mandatory_key) {
84 try {
85 if ((model_ == KEA_DHCP4_SERVER) ||
86 (model_ == KEA_DHCP6_SERVER)) {
87 setDatabaseKea(xpath, elem, has_mandatory_key);
88 } else {
89 isc_throw(NotImplemented,
90 "setDatabase not implemented for the model: " << model_);
91 }
92 } catch (Error const& ex) {
93 isc_throw(NetconfError,
94 "setting database access '" << elem->str()
95 << "' : " << ex.what());
96 }
97}
98
99 void
100 TranslatorDatabase::setDatabaseKea(string const& xpath, ConstElementPtr elem, bool has_mandatory_key) {
101 if (!elem) {
102 deleteItem(xpath);
103 return;
104 }
105
106 if (has_mandatory_key) {
107 // Set the list element. This is important in case we have no other elements except the key.
108 setItem(xpath, ElementPtr(), LeafBaseType::Unknown);
109 } else {
110 checkAndSetDivergingLeaf(elem, xpath, "type", "database-type", LeafBaseType::String);
111 }
112
113 checkAndSetLeaf(elem, xpath, "connect-timeout", LeafBaseType::Uint32);
114 checkAndSetLeaf(elem, xpath, "cert-file", LeafBaseType::String);
115 checkAndSetLeaf(elem, xpath, "cipher-list", LeafBaseType::String);
116 checkAndSetLeaf(elem, xpath, "host", LeafBaseType::String);
117 checkAndSetLeaf(elem, xpath, "key-file", LeafBaseType::String);
118 checkAndSetLeaf(elem, xpath, "lfc-interval", LeafBaseType::Uint32);
119 checkAndSetLeaf(elem, xpath, "max-reconnect-tries", LeafBaseType::Uint32);
120 checkAndSetLeaf(elem, xpath, "max-row-errors", LeafBaseType::Uint32);
121 checkAndSetLeaf(elem, xpath, "name", LeafBaseType::String);
122 checkAndSetLeaf(elem, xpath, "on-fail", LeafBaseType::String);
123 checkAndSetLeaf(elem, xpath, "retry-on-startup", LeafBaseType::Bool);
124 checkAndSetLeaf(elem, xpath, "password", LeafBaseType::String);
125 checkAndSetLeaf(elem, xpath, "persist", LeafBaseType::Bool);
126 checkAndSetLeaf(elem, xpath, "port", LeafBaseType::Uint16);
127 checkAndSetLeaf(elem, xpath, "readonly", LeafBaseType::Bool);
128 checkAndSetLeaf(elem, xpath, "read-timeout", LeafBaseType::Uint32);
129 checkAndSetLeaf(elem, xpath, "reconnect-wait-time", LeafBaseType::Uint32);
130 checkAndSetLeaf(elem, xpath, "tcp-user-timeout", LeafBaseType::Uint32);
131 checkAndSetLeaf(elem, xpath, "trust-anchor", LeafBaseType::String);
132 checkAndSetLeaf(elem, xpath, "user", LeafBaseType::String);
133 checkAndSetLeaf(elem, xpath, "write-timeout", LeafBaseType::Uint32);
134
135 checkAndSetUserContext(elem, xpath);
136}
137
138 TranslatorDatabases::TranslatorDatabases(Session session,
139 const string& model)
140 : Translator(session, model),
141 TranslatorDatabase(session, model) {
142}
143
144 ElementPtr
145 TranslatorDatabases::getDatabases(DataNode const& data_node,
146 string const& xpath) {
147 try {
148 if ((model_ == KEA_DHCP4_SERVER) ||
149 (model_ == KEA_DHCP6_SERVER)) {
150 return (getDatabasesKea(data_node, xpath));
151 }
152 } catch (Error const& ex) {
153 isc_throw(NetconfError,
154 "getting database accesses: " << ex.what());
155 }
156 isc_throw(NotImplemented,
157 "getDatabases not implemented for the model: " << model_);
158}
159
160 ElementPtr
161 TranslatorDatabases::getDatabasesFromAbsoluteXpath(string const& xpath) {
162 try {
163 return getDatabases(findXPath(xpath), xpath);
164 } catch (NetconfError const&) {
165 return ElementPtr();
166 }
167}
168
169 ElementPtr
170 TranslatorDatabases::getDatabasesKea(DataNode const& data_node, string const& xpath) {
171 return getList<TranslatorDatabase>(data_node, xpath, *this,
172 &TranslatorDatabase::getDatabase);
173}
174
175 void
176 TranslatorDatabases::setDatabases(string const& xpath, ConstElementPtr elem) {
177 try {
178 if ((model_ == KEA_DHCP4_SERVER) ||
179 (model_ == KEA_DHCP6_SERVER)) {
180 setDatabasesKea(xpath, elem);
181 } else {
182 isc_throw(NotImplemented,
183 "setDatabases not implemented for the model: "
184 << model_);
185 }
186 } catch (Error const& ex) {
187 isc_throw(NetconfError,
188 "setting database accesses '" << elem->str()
189 << "' : " << ex.what());
190 }
191}
192
193 void
194 TranslatorDatabases::setDatabasesKea(string const& xpath,
195 ConstElementPtr elem) {
196 if (!elem) {
197 deleteItem(xpath);
198 return;
199 }
200 for (size_t i = 0; i < elem->size(); ++i) {
201 ElementPtr database = elem->getNonConst(i);
202 if (!database->contains("type")) {
203 isc_throw(BadValue, "database without type: " << database->str());
204 }
205 string type = database->get("type")->stringValue();
206 ostringstream key;
207 key << xpath << "[database-type='" << type << "']";
208 setDatabase(key.str(), database, /* has_mandatory_key = */ true);
209 }
210}
211
212} // namespace yang
213} // namespace isc
Element::createMap
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
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:83
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:100
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:194
isc::yang::TranslatorDatabases::getDatabasesFromAbsoluteXpath
isc::data::ElementPtr getDatabasesFromAbsoluteXpath(std::string const &xpath)
Translate database accesses from YANG to JSON.
Definition translator_database.cc:161
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:176
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:145
isc::yang::TranslatorDatabases::TranslatorDatabases
TranslatorDatabases(sysrepo::Session session, const std::string &model)
Constructor.
Definition translator_database.cc:138
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:170
isc::yang::Translator
Between YANG and JSON translator class for basic values.
Definition translator.h:23
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:274
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:427
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:29
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
isc::yang
Definition adaptor.cc:17
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
isc::yang::NetconfError
Generic NETCONF error.
Definition netconf_error.h:30
translator_database.h
yang_models.h