Kea  2.1.7-git
translator_database.cc
Go to the documentation of this file.
1 // Copyright (C) 2018-2022 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 using namespace sysrepo;
17 
18 namespace isc {
19 namespace yang {
20 
21 TranslatorDatabase::TranslatorDatabase(S_Session session, const string& model)
22  : TranslatorBasic(session, model) {
23 }
24 
26 }
27 
29 TranslatorDatabase::getDatabase(const string& xpath) {
30  try {
31  if ((model_ == KEA_DHCP4_SERVER) ||
32  (model_ == KEA_DHCP6_SERVER)) {
33  return (getDatabaseKea(xpath));
34  }
35  } catch (const sysrepo_exception& ex) {
37  "sysrepo error getting database access at '" << xpath
38  << "': " << ex.what());
39  }
41  "getDatabase not implemented for the model: " << model_);
42 }
43 
45 TranslatorDatabase::getDatabaseKea(const string& xpath) {
46  ConstElementPtr type = getItem(xpath + "/database-type");
47  if (!type) {
48  return (ElementPtr());
49  }
50  ElementPtr result = Element::createMap();
51  result->set("type", type);
52  ConstElementPtr user = getItem(xpath + "/user");
53  if (user) {
54  result->set("user", user);
55  }
56  ConstElementPtr password = getItem(xpath + "/password");
57  if (password) {
58  result->set("password", password);
59  }
60  ConstElementPtr host = getItem(xpath + "/host");
61  if (host) {
62  result->set("host", host);
63  }
64  ConstElementPtr name = getItem(xpath + "/name");
65  if (name) {
66  result->set("name", name);
67  }
68  ConstElementPtr persist = getItem(xpath + "/persist");
69  if (persist) {
70  result->set("persist", persist);
71  }
72  ConstElementPtr port = getItem(xpath + "/port");
73  if (port) {
74  result->set("port", port);
75  }
76  ConstElementPtr lfc_interval = getItem(xpath + "/lfc-interval");
77  if (lfc_interval) {
78  result->set("lfc-interval", lfc_interval);
79  }
80  ConstElementPtr readonly = getItem(xpath + "/readonly");
81  if (readonly) {
82  result->set("readonly", readonly);
83  }
84  ConstElementPtr connect_timeout = getItem(xpath + "/connect-timeout");
85  if (connect_timeout) {
86  result->set("connect-timeout", connect_timeout);
87  }
88  ConstElementPtr max_reconnect = getItem(xpath + "/max-reconnect-tries");
89  if (max_reconnect) {
90  result->set("max-reconnect-tries", max_reconnect);
91  }
92  ConstElementPtr reconnect_time = getItem(xpath + "/reconnect-wait-time");
93  if (reconnect_time) {
94  result->set("reconnect-wait-time", reconnect_time);
95  }
96  ConstElementPtr max_row_errors = getItem(xpath + "/max-row-errors");
97  if (max_row_errors) {
98  result->set("max-row-errors", max_row_errors);
99  }
100  checkAndGetLeaf(result, xpath, "on-fail");
101  ConstElementPtr context = getItem(xpath + "/user-context");
102  if (context) {
103  result->set("user-context", Element::fromJSON(context->stringValue()));
104  }
105  return (result);
106 }
107 
108 void
110  ConstElementPtr elem,
111  bool skip) {
112  try {
113  if ((model_ == KEA_DHCP4_SERVER) ||
114  (model_ == KEA_DHCP6_SERVER)) {
115  setDatabaseKea(xpath, elem, skip);
116  } else {
118  "setDatabase not implemented for the model: " << model_);
119  }
120  } catch (const sysrepo_exception& ex) {
122  "sysrepo error setting database access '" << elem->str()
123  << "' at '" << xpath << "': " << ex.what());
124  }
125 }
126 
127 void
129  ConstElementPtr elem,
130  bool skip) {
131  if (!elem) {
132  delItem(xpath);
133  return;
134  }
135  if (!skip) {
136  ConstElementPtr type = elem->get("type");
137  if (!type) {
138  isc_throw(BadValue, "setDatabase requires database type: "
139  << elem->str());
140  }
141  setItem(xpath + "/database-type", type, SR_STRING_T);
142  }
143  ConstElementPtr user = elem->get("user");
144  if (user) {
145  setItem(xpath + "/user", user, SR_STRING_T);
146  }
147  ConstElementPtr password = elem->get("password");
148  if (password) {
149  setItem(xpath + "/password", password, SR_STRING_T);
150  }
151  ConstElementPtr host = elem->get("host");
152  if (host) {
153  setItem(xpath + "/host", host, SR_STRING_T);
154  }
155  ConstElementPtr name = elem->get("name");
156  if (name) {
157  setItem(xpath + "/name", name, SR_STRING_T);
158  }
159  ConstElementPtr persist = elem->get("persist");
160  if (persist) {
161  setItem(xpath + "/persist", persist, SR_BOOL_T);
162  }
163  ConstElementPtr port = elem->get("port");
164  if (port) {
165  setItem(xpath + "/port", port, SR_UINT16_T);
166  }
167  ConstElementPtr lfc_interval = elem->get("lfc-interval");
168  if (lfc_interval) {
169  setItem(xpath + "/lfc-interval", lfc_interval, SR_UINT32_T);
170  }
171  ConstElementPtr readonly = elem->get("readonly");
172  if (readonly) {
173  setItem(xpath + "/readonly", readonly, SR_BOOL_T);
174  }
175  ConstElementPtr connect_timeout = elem->get("connect-timeout");
176  if (connect_timeout) {
177  setItem(xpath + "/connect-timeout", connect_timeout, SR_UINT32_T);
178  }
179  ConstElementPtr max_reconnect = elem->get("max-reconnect-tries");
180  if (max_reconnect) {
181  setItem(xpath + "/max-reconnect-tries", max_reconnect, SR_UINT32_T);
182  }
183  ConstElementPtr reconnect_wait = elem->get("reconnect-wait-time");
184  if (reconnect_wait) {
185  setItem(xpath + "/reconnect-wait-time", reconnect_wait, SR_UINT32_T);
186  }
187  ConstElementPtr max_row_errors = elem->get("max-row-errors");
188  if (max_row_errors) {
189  setItem(xpath + "/max-row-errors", max_row_errors, SR_UINT32_T);
190  }
191  checkAndSetLeaf(elem, xpath, "on-fail", SR_STRING_T);
192  ConstElementPtr context = Adaptor::getContext(elem);
193  if (context) {
194  setItem(xpath + "/user-context", Element::create(context->str()),
195  SR_STRING_T);
196  }
197 }
198 
200  const string& model)
201  : TranslatorBasic(session, model),
202  TranslatorDatabase(session, model) {
203 }
204 
206 }
207 
209 TranslatorDatabases::getDatabases(const string& xpath) {
210  try {
211  if ((model_ == KEA_DHCP4_SERVER) ||
212  (model_ == KEA_DHCP6_SERVER)) {
213  return (getDatabasesKea(xpath));
214  }
215  } catch (const sysrepo_exception& ex) {
217  "sysrepo error getting database accesses at '" << xpath
218  << "': " << ex.what());
219  }
221  "getDatabases not implemented for the model: " << model_);
222 }
223 
226  return getList<TranslatorDatabase>(xpath, *this,
228 }
229 
230 void
232  try {
233  if ((model_ == KEA_DHCP4_SERVER) ||
234  (model_ == KEA_DHCP6_SERVER)) {
235  setDatabasesKea(xpath, elem);
236  } else {
238  "setDatabases not implemented for the model: "
239  << model_);
240  }
241  } catch (const sysrepo_exception& ex) {
243  "sysrepo error setting database accesses '" << elem->str()
244  << "' at '" << xpath << "': " << ex.what());
245  }
246 }
247 
248 void
250  ConstElementPtr elem) {
251  if (!elem) {
252  delItem(xpath);
253  return;
254  }
255  for (size_t i = 0; i < elem->size(); ++i) {
256  ConstElementPtr database = elem->get(i);
257  if (!database->contains("type")) {
258  isc_throw(BadValue, "database without type: " << database->str());
259  }
260  string type = database->get("type")->stringValue();
261  ostringstream key;
262  key << xpath << "[database-type='" << type << "']";
263  setDatabase(key.str(), database, true);
264  }
265 }
266 
267 } // namespace yang
268 } // namespace isc
void checkAndSetLeaf(isc::data::ConstElementPtr const &from, std::string const &xpath, std::string const &name, sr_type_t const type)
Get an element from given ElementPtr node and set it in sysrepo at given xpath.
Definition: translator.cc:283
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:19
TranslatorDatabases(sysrepo::S_Session session, const std::string &model)
Constructor.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
STL namespace.
isc::data::ElementPtr getDatabaseKea(const std::string &xpath)
getDatabase JSON for kea-dhcp[46]-server models.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
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:260
std::string model_
The model.
Definition: translator.h:171
Database access translation between YANG and JSON.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
static isc::data::ConstElementPtr getContext(isc::data::ConstElementPtr parent)
Get user context.
Definition: adaptor.cc:27
void delItem(const std::string &xpath)
Delete basic value from YANG.
Definition: translator.cc:294
Defines the logger used by the top-level component of kea-lfc.
isc::data::ElementPtr getItem(const std::string &xpath)
Get and translate basic value from YANG to JSON.
Definition: translator.cc:105
void checkAndGetLeaf(isc::data::ElementPtr &storage, const std::string &xpath, const std::string &name)
Retrieves an item and stores it in the specified storage.
Definition: translator.cc:274
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.