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
10#include <yang/adaptor.h>
11#include <yang/yang_models.h>
12#include <sstream>
13
14using namespace std;
15using namespace isc::data;
16#ifndef HAVE_PRE_0_7_6_SYSREPO
17using namespace sysrepo;
18#endif
19
20namespace isc {
21namespace yang {
22
23TranslatorDatabase::TranslatorDatabase(S_Session session, const string& model)
24 : TranslatorBasic(session, model) {
25}
26
28}
29
31TranslatorDatabase::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) {
39 "sysrepo error getting database access at '" << xpath
40 << "': " << ex.what());
41 }
43 "getDatabase not implemented for the model: " << model_);
44}
45
48 ConstElementPtr type = getItem(xpath + "/database-type");
49 if (!type) {
50 return (ElementPtr());
51 }
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
125void
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 {
135 "setDatabase not implemented for the model: " << model_);
136 }
137 } catch (const sysrepo_exception& ex) {
139 "sysrepo error setting database access '" << elem->str()
140 << "' at '" << xpath << "': " << ex.what());
141 }
142}
143
144void
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
232 const string& model)
233 : TranslatorBasic(session, model),
234 TranslatorDatabase(session, model) {
235}
236
238}
239
242 try {
243 if ((model_ == KEA_DHCP4_SERVER) ||
244 (model_ == KEA_DHCP6_SERVER)) {
245 return (getDatabasesKea(xpath));
246 }
247 } catch (const sysrepo_exception& ex) {
249 "sysrepo error getting database accesses at '" << xpath
250 << "': " << ex.what());
251 }
253 "getDatabases not implemented for the model: " << model_);
254}
255
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 }
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
278void
280 try {
281 if ((model_ == KEA_DHCP4_SERVER) ||
282 (model_ == KEA_DHCP6_SERVER)) {
283 setDatabasesKea(xpath, elem);
284 } else {
286 "setDatabases not implemented for the model: "
287 << model_);
288 }
289 } catch (const sysrepo_exception& ex) {
291 "sysrepo error setting database accesses '" << elem->str()
292 << "' at '" << xpath << "': " << ex.what());
293 }
294}
295
296void
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
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.
A generic exception that is thrown when an unexpected error condition occurs.
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition: data.cc:223
static ElementPtr fromJSON(const std::string &in, bool preproc=false)
These functions will parse the given string (JSON) representation of a compound element.
Definition: data.cc:750
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition: data.cc:268
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition: data.cc:263
static isc::data::ConstElementPtr getContext(isc::data::ConstElementPtr parent)
Get user context.
Definition: adaptor.cc:25
Between YANG and JSON translator class for basic values.
Definition: translator.h:27
std::string getNext(sysrepo::S_Iter_Value iter)
Get xpath of the next YANG list item.
Definition: translator.cc:283
isc::data::ElementPtr getItem(const std::string &xpath)
Get and translate basic value from YANG to JSON.
Definition: translator.cc:104
sysrepo::S_Iter_Value getIter(const std::string &xpath)
List iterator methods keeping the session private.
Definition: translator.cc:278
std::string model_
The model.
Definition: translator.h:132
void delItem(const std::string &xpath)
Delete basic value from YANG.
Definition: translator.cc:266
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
Database access translation between YANG and JSON.
isc::data::ElementPtr getDatabaseKea(const std::string &xpath)
getDatabase JSON for kea-dhcp[46]-server models.
void setDatabase(const std::string &xpath, isc::data::ConstElementPtr elem, bool skip=false)
Translate and set database access from JSON to YANG.
TranslatorDatabase(sysrepo::S_Session session, const std::string &model)
Constructor.
virtual ~TranslatorDatabase()
Destructor.
isc::data::ElementPtr getDatabase(const std::string &xpath)
Get and translate a database access from YANG to JSON.
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.
void setDatabases(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set database accesses from JSON to YANG.
isc::data::ConstElementPtr getDatabases(const std::string &xpath)
Get and translate database accesses from YANG to JSON.
TranslatorDatabases(sysrepo::S_Session session, const std::string &model)
Constructor.
isc::data::ElementPtr getDatabasesKea(const std::string &xpath)
getDatabases JSON for kea-dhcp[46]-server models.
virtual ~TranslatorDatabases()
Destructor.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
boost::shared_ptr< Element > ElementPtr
Definition: data.h:22
Defines the logger used by the top-level component of kea-dhcp-ddns.