Kea  1.9.9-git
translator_database.cc
Go to the documentation of this file.
1 // Copyright (C) 2018-2019 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 
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 
28 }
29 
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) {
39  "sysrepo error getting database access at '" << xpath
40  << "': " << ex.what());
41  }
43  "getDatabase not implemented for the model: " << model_);
44 }
45 
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 consistency = getItem(xpath + "/consistency");
119  if (consistency) {
120  result->set("consistency", consistency);
121  }
122  ConstElementPtr serial_consistency = getItem(xpath + "/serial-consistency");
123  if (serial_consistency) {
124  result->set("serial-consistency", serial_consistency);
125  }
126  ConstElementPtr max_row_errors = getItem(xpath + "/max-row-errors");
127  if (max_row_errors) {
128  result->set("max-row-errors", max_row_errors);
129  }
130  ConstElementPtr context = getItem(xpath + "/user-context");
131  if (context) {
132  result->set("user-context", Element::fromJSON(context->stringValue()));
133  }
134  return (result);
135 }
136 
137 void
139  ConstElementPtr elem,
140  bool skip) {
141  try {
142  if ((model_ == KEA_DHCP4_SERVER) ||
143  (model_ == KEA_DHCP6_SERVER)) {
144  setDatabaseKea(xpath, elem, skip);
145  } else {
147  "setDatabase not implemented for the model: " << model_);
148  }
149  } catch (const sysrepo_exception& ex) {
151  "sysrepo error setting database access '" << elem->str()
152  << "' at '" << xpath << "': " << ex.what());
153  }
154 }
155 
156 void
158  ConstElementPtr elem,
159  bool skip) {
160  if (!elem) {
161  delItem(xpath);
162  return;
163  }
164  if (!skip) {
165  ConstElementPtr type = elem->get("type");
166  if (!type) {
167  isc_throw(BadValue, "setDatabase requires database type: "
168  << elem->str());
169  }
170  setItem(xpath + "/database-type", type, SR_STRING_T);
171  }
172  ConstElementPtr user = elem->get("user");
173  if (user) {
174  setItem(xpath + "/user", user, SR_STRING_T);
175  }
176  ConstElementPtr password = elem->get("password");
177  if (password) {
178  setItem(xpath + "/password", password, SR_STRING_T);
179  }
180  ConstElementPtr host = elem->get("host");
181  if (host) {
182  setItem(xpath + "/host", host, SR_STRING_T);
183  }
184  ConstElementPtr name = elem->get("name");
185  if (name) {
186  setItem(xpath + "/name", name, SR_STRING_T);
187  }
188  ConstElementPtr persist = elem->get("persist");
189  if (persist) {
190  setItem(xpath + "/persist", persist, SR_BOOL_T);
191  }
192  ConstElementPtr port = elem->get("port");
193  if (port) {
194  setItem(xpath + "/port", port, SR_UINT16_T);
195  }
196  ConstElementPtr lfc_interval = elem->get("lfc-interval");
197  if (lfc_interval) {
198  setItem(xpath + "/lfc-interval", lfc_interval, SR_UINT32_T);
199  }
200  ConstElementPtr readonly = elem->get("readonly");
201  if (readonly) {
202  setItem(xpath + "/readonly", readonly, SR_BOOL_T);
203  }
204  ConstElementPtr connect_timeout = elem->get("connect-timeout");
205  if (connect_timeout) {
206  setItem(xpath + "/connect-timeout", connect_timeout, SR_UINT32_T);
207  }
208  ConstElementPtr contact_points = elem->get("contact-points");
209  if (contact_points) {
210  setItem(xpath + "/contact-points", contact_points, SR_STRING_T);
211  }
212  ConstElementPtr keyspace = elem->get("keyspace");
213  if (keyspace) {
214  setItem(xpath + "/keyspace", keyspace, SR_STRING_T);
215  }
216  ConstElementPtr max_reconnect = elem->get("max-reconnect-tries");
217  if (max_reconnect) {
218  setItem(xpath + "/max-reconnect-tries", max_reconnect, SR_UINT32_T);
219  }
220  ConstElementPtr reconnect_wait = elem->get("reconnect-wait-time");
221  if (reconnect_wait) {
222  setItem(xpath + "/reconnect-wait-time", reconnect_wait, SR_UINT32_T);
223  }
224  ConstElementPtr request_timeout = elem->get("request-timeout");
225  if (request_timeout) {
226  setItem(xpath + "/request-timeout", request_timeout, SR_UINT32_T);
227  }
228  ConstElementPtr keepalive = elem->get("tcp-keepalive");
229  if (keepalive) {
230  setItem(xpath + "/tcp-keepalive", keepalive, SR_UINT32_T);
231  }
232  ConstElementPtr nodelay = elem->get("tcp-nodelay");
233  if (nodelay) {
234  setItem(xpath + "/tcp-nodelay", nodelay, SR_BOOL_T);
235  }
236  ConstElementPtr consistency = elem->get("consistency");
237  if (consistency) {
238  setItem(xpath + "/consistency", consistency, SR_STRING_T);
239  }
240  ConstElementPtr serial_consistency = elem->get("serial-consistency");
241  if (serial_consistency) {
242  setItem(xpath + "/serial-consistency", serial_consistency, SR_STRING_T);
243  }
244  ConstElementPtr max_row_errors = elem->get("max-row-errors");
245  if (max_row_errors) {
246  setItem(xpath + "/max-row-errors", max_row_errors, SR_UINT32_T);
247  }
248  ConstElementPtr context = Adaptor::getContext(elem);
249  if (context) {
250  setItem(xpath + "/user-context", Element::create(context->str()),
251  SR_STRING_T);
252  }
253 }
254 
256  const string& model)
257  : TranslatorBasic(session, model),
258  TranslatorDatabase(session, model) {
259 }
260 
262 }
263 
265 TranslatorDatabases::getDatabases(const string& xpath) {
266  try {
267  if ((model_ == KEA_DHCP4_SERVER) ||
268  (model_ == KEA_DHCP6_SERVER)) {
269  return (getDatabasesKea(xpath));
270  }
271  } catch (const sysrepo_exception& ex) {
273  "sysrepo error getting database accesses at '" << xpath
274  << "': " << ex.what());
275  }
277  "getDatabases not implemented for the model: " << model_);
278 }
279 
282  S_Iter_Value iter = getIter(xpath);
283  if (!iter) {
284  // Can't happen.
285  isc_throw(Unexpected, "getDatabasesKea can't get iterator: " << xpath);
286  }
287  ElementPtr result = Element::createList();
288  for (;;) {
289  const string& database = getNext(iter);
290  if (database.empty()) {
291  break;
292  }
293  result->add(getDatabase(database));
294  }
295  if (result->size() > 0) {
296  return (result);
297  } else {
298  return (ElementPtr());
299  }
300 }
301 
302 void
304  try {
305  if ((model_ == KEA_DHCP4_SERVER) ||
306  (model_ == KEA_DHCP6_SERVER)) {
307  setDatabasesKea(xpath, elem);
308  } else {
310  "setDatabases not implemented for the model: "
311  << model_);
312  }
313  } catch (const sysrepo_exception& ex) {
315  "sysrepo error setting database accesses '" << elem->str()
316  << "' at '" << xpath << "': " << ex.what());
317  }
318 }
319 
320 void
322  ConstElementPtr elem) {
323  if (!elem) {
324  delItem(xpath);
325  return;
326  }
327  for (size_t i = 0; i < elem->size(); ++i) {
328  ConstElementPtr database = elem->get(i);
329  if (!database->contains("type")) {
330  isc_throw(BadValue, "database without type: " << database->str());
331  }
332  string type = database->get("type")->stringValue();
333  ostringstream key;
334  key << xpath << "[database-type='" << type << "']";
335  setDatabase(key.str(), database, true);
336  }
337 }
338 
339 }; // end of namespace isc::yang
340 }; // end of namespace isc
A generic exception that is thrown when a function is not implemented.
isc::data::ConstElementPtr getDatabases(const std::string &xpath)
Get and 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 getDatabase(const std::string &xpath)
Get and translate a database access from YANG to JSON.
Between YANG and JSON translator class for basic values.
Definition: translator.h:27
TranslatorDatabases(sysrepo::S_Session session, const std::string &model)
Constructor.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
STL namespace.
isc::data::ElementPtr getDatabaseKea(const std::string &xpath)
getDatabase JSON for kea-dhcp[46]-server models.
sysrepo::S_Iter_Value getIter(const std::string &xpath)
List iterator methods keeping the session private.
Definition: translator.cc:316
isc::data::ElementPtr getDatabasesKea(const std::string &xpath)
getDatabases JSON for kea-dhcp[46]-server models.
virtual ~TranslatorDatabase()
Destructor.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
void setDatabasesKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setDatabases for kea-dhcp[46]-server models.
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:288
A generic exception that is thrown when an unexpected error condition occurs.
std::string model_
The model.
Definition: translator.h:132
Database access translation between YANG and JSON.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
std::string getNext(sysrepo::S_Iter_Value iter)
Get xpath of the next YANG list item.
Definition: translator.cc:321
static isc::data::ConstElementPtr getContext(isc::data::ConstElementPtr parent)
Get user context.
Definition: adaptor.cc:27
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
void delItem(const std::string &xpath)
Delete basic value from YANG.
Definition: translator.cc:304
Defines the logger used by the top-level component of kea-dhcp-ddns.
isc::data::ElementPtr getItem(const std::string &xpath)
Get and translate basic value from YANG to JSON.
Definition: translator.cc:111
virtual ~TranslatorDatabases()
Destructor.
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.