Kea: translator_database.cc Source File

| Kea 1.5.0

translator_database.cc
Go to the documentation of this file.
1 // Copyright (C) 2018 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/adaptor.h>
11 #include <yang/yang_models.h>
12 #include <sstream>
13
14 using namespace std;
15 using namespace isc::data;
16 #ifndef HAVE_PRE_0_7_6_SYSREPO
17 using namespace sysrepo;
18 #endif
19
20 namespace isc {
21 namespace yang {
22
23 TranslatorDatabase::TranslatorDatabase(S_Session session, const string& model)
24 : TranslatorBasic(session, model) {
25 }
26
27 TranslatorDatabase::~TranslatorDatabase() {
28 }
29
30 ElementPtr
31 TranslatorDatabase::getDatabase(const string& xpath) {
32 try {
33 if ((model_ == KEA_DHCP4_SERVER) ||
34 (model_ == KEA_DHCP6_SERVER)) {
35 return (getDatabaseKea(xpath));
36 }
37 } catch (const sysrepo_exception& ex) {
38 isc_throw(SysrepoError,
39 "sysrepo error getting database access at '" << xpath
40 << "': " << ex.what());
41 }
42 isc_throw(NotImplemented,
43 "getDatabase not implemented for the model: " << model_);
44 }
45
46 ElementPtr
47 TranslatorDatabase::getDatabaseKea(const string& xpath) {
48 ConstElementPtr type = getItem(xpath + "/database-type");
49 if (!type) {
50 return (ElementPtr());
51 }
52 ElementPtr result = Element::createMap();
53 result->set("type", type);
54 ConstElementPtr user = getItem(xpath + "/user");
55 if (user) {
56 result->set("user", user);
57 }
58 ConstElementPtr password = getItem(xpath + "/password");
59 if (password) {
60 result->set("password", password);
61 }
62 ConstElementPtr host = getItem(xpath + "/host");
63 if (host) {
64 result->set("host", host);
65 }
66 ConstElementPtr name = getItem(xpath + "/name");
67 if (name) {
68 result->set("name", name);
69 }
70 ConstElementPtr persist = getItem(xpath + "/persist");
71 if (persist) {
72 result->set("persist", persist);
73 }
74 ConstElementPtr port = getItem(xpath + "/port");
75 if (port) {
76 result->set("port", port);
77 }
78 ConstElementPtr lfc_interval = getItem(xpath + "/lfc-interval");
79 if (lfc_interval) {
80 result->set("lfc-interval", lfc_interval);
81 }
82 ConstElementPtr readonly = getItem(xpath + "/readonly");
83 if (readonly) {
84 result->set("readonly", readonly);
85 }
86 ConstElementPtr connect_timeout = getItem(xpath + "/connect-timeout");
87 if (connect_timeout) {
88 result->set("connect-timeout", connect_timeout);
89 }
90 ConstElementPtr contact_points = getItem(xpath + "/contact-points");
91 if (contact_points) {
92 result->set("contact-points", contact_points);
93 }
94 ConstElementPtr keyspace = getItem(xpath + "/keyspace");
95 if (keyspace) {
96 result->set("keyspace", keyspace);
97 }
98 ConstElementPtr max_reconnect = getItem(xpath + "/max-reconnect-tries");
99 if (max_reconnect) {
100 result->set("max-reconnect-tries", max_reconnect);
101 }
102 ConstElementPtr reconnect_time = getItem(xpath + "/reconnect-wait-time");
103 if (reconnect_time) {
104 result->set("reconnect-wait-time", reconnect_time);
105 }
106 ConstElementPtr request_timeout = getItem(xpath + "/request-timeout");
107 if (request_timeout) {
108 result->set("request-timeout", request_timeout);
109 }
110 ConstElementPtr keepalive = getItem(xpath + "/tcp-keepalive");
111 if (keepalive) {
112 result->set("tcp-keepalive", keepalive);
113 }
114 ConstElementPtr nodelay = getItem(xpath + "/tcp-nodelay");
115 if (nodelay) {
116 result->set("tcp-nodelay", nodelay);
117 }
118 ConstElementPtr context = getItem(xpath + "/user-context");
119 if (context) {
120 result->set("user-context", Element::fromJSON(context->stringValue()));
121 }
122 return (result);
123 }
124
125 void
126 TranslatorDatabase::setDatabase(const string& xpath,
127 ConstElementPtr elem,
128 bool skip) {
129 try {
130 if ((model_ == KEA_DHCP4_SERVER) ||
131 (model_ == KEA_DHCP6_SERVER)) {
132 setDatabaseKea(xpath, elem, skip);
133 } else {
134 isc_throw(NotImplemented,
135 "setDatabase not implemented for the model: " << model_);
136 }
137 } catch (const sysrepo_exception& ex) {
138 isc_throw(SysrepoError,
139 "sysrepo error setting database access '" << elem->str()
140 << "' at '" << xpath << "': " << ex.what());
141 }
142 }
143
144 void
145 TranslatorDatabase::setDatabaseKea(const string& xpath,
146 ConstElementPtr elem,
147 bool skip) {
148 if (!elem) {
149 delItem(xpath);
150 return;
151 }
152 if (!skip) {
153 ConstElementPtr type = elem->get("type");
154 if (!type) {
155 isc_throw(BadValue, "setDatabase requires database type: "
156 << elem->str());
157 }
158 setItem(xpath + "/database-type", type, SR_STRING_T);
159 }
160 ConstElementPtr user = elem->get("user");
161 if (user) {
162 setItem(xpath + "/user", user, SR_STRING_T);
163 }
164 ConstElementPtr password = elem->get("password");
165 if (password) {
166 setItem(xpath + "/password", password, SR_STRING_T);
167 }
168 ConstElementPtr host = elem->get("host");
169 if (host) {
170 setItem(xpath + "/host", host, SR_STRING_T);
171 }
172 ConstElementPtr name = elem->get("name");
173 if (name) {
174 setItem(xpath + "/name", name, SR_STRING_T);
175 }
176 ConstElementPtr persist = elem->get("persist");
177 if (persist) {
178 setItem(xpath + "/persist", persist, SR_BOOL_T);
179 }
180 ConstElementPtr port = elem->get("port");
181 if (port) {
182 setItem(xpath + "/port", port, SR_UINT16_T);
183 }
184 ConstElementPtr lfc_interval = elem->get("lfc-interval");
185 if (lfc_interval) {
186 setItem(xpath + "/lfc-interval", lfc_interval, SR_UINT32_T);
187 }
188 ConstElementPtr readonly = elem->get("readonly");
189 if (readonly) {
190 setItem(xpath + "/readonly", readonly, SR_BOOL_T);
191 }
192 ConstElementPtr connect_timeout = elem->get("connect-timeout");
193 if (connect_timeout) {
194 setItem(xpath + "/connect-timeout", connect_timeout, SR_UINT32_T);
195 }
196 ConstElementPtr contact_points = elem->get("contact-points");
197 if (contact_points) {
198 setItem(xpath + "/contact-points", contact_points, SR_STRING_T);
199 }
200 ConstElementPtr keyspace = elem->get("keyspace");
201 if (keyspace) {
202 setItem(xpath + "/keyspace", keyspace, SR_STRING_T);
203 }
204 ConstElementPtr max_reconnect = elem->get("max-reconnect-tries");
205 if (max_reconnect) {
206 setItem(xpath + "/max-reconnect-tries", max_reconnect, SR_UINT32_T);
207 }
208 ConstElementPtr reconnect_wait = elem->get("reconnect-wait-time");
209 if (reconnect_wait) {
210 setItem(xpath + "/reconnect-wait-time", reconnect_wait, SR_UINT32_T);
211 }
212 ConstElementPtr request_timeout = elem->get("request-timeout");
213 if (request_timeout) {
214 setItem(xpath + "/request-timeout", request_timeout, SR_UINT32_T);
215 }
216 ConstElementPtr keepalive = elem->get("tcp-keepalive");
217 if (keepalive) {
218 setItem(xpath + "/tcp-keepalive", keepalive, SR_UINT32_T);
219 }
220 ConstElementPtr nodelay = elem->get("tcp-nodelay");
221 if (nodelay) {
222 setItem(xpath + "/tcp-nodelay", nodelay, SR_BOOL_T);
223 }
224 ConstElementPtr context = Adaptor::getContext(elem);
225 if (context) {
226 setItem(xpath + "/user-context", Element::create(context->str()),
227 SR_STRING_T);
228 }
229 }
230
231 TranslatorDatabases::TranslatorDatabases(S_Session session,
232 const string& model)
233 : TranslatorBasic(session, model),
234 TranslatorDatabase(session, model) {
235 }
236
237 TranslatorDatabases::~TranslatorDatabases() {
238 }
239
240 ConstElementPtr
241 TranslatorDatabases::getDatabases(const string& xpath) {
242 try {
243 if ((model_ == KEA_DHCP4_SERVER) ||
244 (model_ == KEA_DHCP6_SERVER)) {
245 return (getDatabasesKea(xpath));
246 }
247 } catch (const sysrepo_exception& ex) {
248 isc_throw(SysrepoError,
249 "sysrepo error getting database accesses at '" << xpath
250 << "': " << ex.what());
251 }
252 isc_throw(NotImplemented,
253 "getDatabases not implemented for the model: " << model_);
254 }
255
256 ElementPtr
257 TranslatorDatabases::getDatabasesKea(const string& xpath) {
258 S_Iter_Value iter = getIter(xpath);
259 if (!iter) {
260 // Can't happen.
261 isc_throw(Unexpected, "getDatabasesKea can't get iterator: " << xpath);
262 }
263 ElementPtr result = Element::createList();
264 for (;;) {
265 const string& database = getNext(iter);
266 if (database.empty()) {
267 break;
268 }
269 result->add(getDatabase(database));
270 }
271 if (result->size() > 0) {
272 return (result);
273 } else {
274 return (ElementPtr());
275 }
276 }
277
278 void
279 TranslatorDatabases::setDatabases(const string& xpath, ConstElementPtr elem) {
280 try {
281 if ((model_ == KEA_DHCP4_SERVER) ||
282 (model_ == KEA_DHCP6_SERVER)) {
283 setDatabasesKea(xpath, elem);
284 } else {
285 isc_throw(NotImplemented,
286 "setDatabases not implemented for the model: "
287 << model_);
288 }
289 } catch (const sysrepo_exception& ex) {
290 isc_throw(SysrepoError,
291 "sysrepo error setting database accesses '" << elem->str()
292 << "' at '" << xpath << "': " << ex.what());
293 }
294 }
295
296 void
297 TranslatorDatabases::setDatabasesKea(const string& xpath,
298 ConstElementPtr elem) {
299 if (!elem) {
300 delItem(xpath);
301 return;
302 }
303 for (size_t i = 0; i < elem->size(); ++i) {
304 ConstElementPtr database = elem->get(i);
305 if (!database->contains("type")) {
306 isc_throw(BadValue, "database without type: " << database->str());
307 }
308 string type = database->get("type")->stringValue();
309 ostringstream key;
310 key << xpath << "[database-type='" << type << "']";
311 setDatabase(key.str(), database, true);
312 }
313 }
314
315 }; // end of namespace isc::yang
316 }; // end of namespace isc
isc::Unexpected
A generic exception that is thrown when an unexpected error condition occurs.
Definition: exceptions/exceptions.h:153
isc::yang::TranslatorBasic::getIter
sysrepo::S_Iter_Value getIter(const std::string &xpath)
List iterator methods keeping the session private.
Definition: translator.cc:278
isc::yang::TranslatorDatabase::setDatabaseKea
void setDatabaseKea(const std::string &xpath, isc::data::ConstElementPtr elem, bool skip)
setDatabase for kea-dhcp[46]-server models.
Definition: translator_database.cc:145
isc::yang::TranslatorDatabases::~TranslatorDatabases
virtual ~TranslatorDatabases()
Destructor.
Definition: translator_database.cc:237
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:297
isc::yang::TranslatorDatabases::getDatabasesKea
isc::data::ElementPtr getDatabasesKea(const std::string &xpath)
getDatabases JSON for kea-dhcp[46]-server models.
Definition: translator_database.cc:257
isc::yang::TranslatorDatabase::getDatabase
isc::data::ElementPtr getDatabase(const std::string &xpath)
Get and translate a database access from YANG to JSON.
Definition: translator_database.cc:31
isc::yang::TranslatorBasic::getNext
std::string getNext(sysrepo::S_Iter_Value iter)
Get xpath of the next YANG list item.
Definition: translator.cc:283
isc::yang::TranslatorDatabases::getDatabases
isc::data::ConstElementPtr getDatabases(const std::string &xpath)
Get and translate database accesses from YANG to JSON.
Definition: translator_database.cc:241
isc::yang::TranslatorBasic::getItem
isc::data::ElementPtr getItem(const std::string &xpath)
Get and translate basic value from YANG to JSON.
Definition: translator.cc:104
isc::data
Definition: cfg_to_element.h:25
translator_database.h
isc::yang::TranslatorDatabase::~TranslatorDatabase
virtual ~TranslatorDatabase()
Destructor.
Definition: translator_database.cc:27
adaptor.h
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
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
yang_models.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
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::yang::TranslatorBasic::delItem
void delItem(const std::string &xpath)
Delete basic value from YANG.
Definition: translator.cc:266
isc::yang::TranslatorBasic::setItem
void setItem(const std::string &xpath, isc::data::ConstElementPtr elem, sr_type_t type)
Translate and set basic value from JSON to YANG.
Definition: translator.cc:250
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:279
isc::yang::TranslatorDatabases::TranslatorDatabases
TranslatorDatabases(sysrepo::S_Session session, const std::string &model)
Constructor.
Definition: translator_database.cc:231
isc::yang::TranslatorDatabase::setDatabase
void setDatabase(const std::string &xpath, isc::data::ConstElementPtr elem, bool skip=false)
Translate and set database access from JSON to YANG.
Definition: translator_database.cc:126
isc::yang::TranslatorBasic
Between YANG and JSON translator class for basic values.
Definition: translator.h:27
isc::yang::TranslatorDatabase::getDatabaseKea
isc::data::ElementPtr getDatabaseKea(const std::string &xpath)
getDatabase JSON for kea-dhcp[46]-server models.
Definition: translator_database.cc:47
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
isc::NotImplemented
A generic exception that is thrown when a function is not implemented.
Definition: exceptions/exceptions.h:165
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
isc::yang::TranslatorBasic::model_
std::string model_
The model.
Definition: translator.h:132
isc::yang::Adaptor::getContext
static isc::data::ConstElementPtr getContext(isc::data::ConstElementPtr parent)
Get user context.
Definition: adaptor.cc:25
isc::yang::SysrepoError
Sysrepo error.
Definition: sysrepo_error.h:16
isc::yang::TranslatorDatabase
Database access translation between YANG and JSON.
Definition: translator_database.h:104