Kea 2.2.0
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
14using namespace std;
15using namespace isc::data;
16using namespace sysrepo;
17
18namespace isc {
19namespace yang {
20
21TranslatorDatabase::TranslatorDatabase(S_Session session, const string& model)
22 : TranslatorBasic(session, model) {
23}
24
26}
27
29TranslatorDatabase::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
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 checkAndGetLeaf(result, xpath, "user");
53 checkAndGetLeaf(result, xpath, "password");
54 checkAndGetLeaf(result, xpath, "host");
55 checkAndGetLeaf(result, xpath, "name");
56 checkAndGetLeaf(result, xpath, "persist");
57 checkAndGetLeaf(result, xpath, "port");
58 checkAndGetLeaf(result, xpath, "lfc-interval");
59 checkAndGetLeaf(result, xpath, "readonly");
60 checkAndGetLeaf(result, xpath, "trust-anchor");
61 checkAndGetLeaf(result, xpath, "cert-file");
62 checkAndGetLeaf(result, xpath, "key-file");
63 checkAndGetLeaf(result, xpath, "cipher-list");
64 checkAndGetLeaf(result, xpath, "connect-timeout");
65 checkAndGetLeaf(result, xpath, "max-reconnect-tries");
66 checkAndGetLeaf(result, xpath, "reconnect-wait-time");
67 checkAndGetLeaf(result, xpath, "max-row-errors");
68 checkAndGetLeaf(result, xpath, "on-fail");
69 ConstElementPtr context = getItem(xpath + "/user-context");
70 if (context) {
71 result->set("user-context", Element::fromJSON(context->stringValue()));
72 }
73 return (result);
74}
75
76void
78 ConstElementPtr elem,
79 bool skip) {
80 try {
81 if ((model_ == KEA_DHCP4_SERVER) ||
82 (model_ == KEA_DHCP6_SERVER)) {
83 setDatabaseKea(xpath, elem, skip);
84 } else {
86 "setDatabase not implemented for the model: " << model_);
87 }
88 } catch (const sysrepo_exception& ex) {
90 "sysrepo error setting database access '" << elem->str()
91 << "' at '" << xpath << "': " << ex.what());
92 }
93}
94
95void
97 ConstElementPtr elem,
98 bool skip) {
99 if (!elem) {
100 delItem(xpath);
101 return;
102 }
103 if (!skip) {
104 ConstElementPtr type = elem->get("type");
105 if (!type) {
106 isc_throw(BadValue, "setDatabase requires database type: "
107 << elem->str());
108 }
109 setItem(xpath + "/database-type", type, SR_STRING_T);
110 }
111 checkAndSetLeaf(elem, xpath, "user", SR_STRING_T);
112 checkAndSetLeaf(elem, xpath, "password", SR_STRING_T);
113 checkAndSetLeaf(elem, xpath, "host", SR_STRING_T);
114 checkAndSetLeaf(elem, xpath, "name", SR_STRING_T);
115 checkAndSetLeaf(elem, xpath, "persist", SR_BOOL_T);
116 checkAndSetLeaf(elem, xpath, "port", SR_UINT16_T);
117 checkAndSetLeaf(elem, xpath, "lfc-interval", SR_UINT32_T);
118 checkAndSetLeaf(elem, xpath, "readonly", SR_BOOL_T);
119 checkAndSetLeaf(elem, xpath, "trust-anchor", SR_STRING_T);
120 checkAndSetLeaf(elem, xpath, "cert-file", SR_STRING_T);
121 checkAndSetLeaf(elem, xpath, "key-file", SR_STRING_T);
122 checkAndSetLeaf(elem, xpath, "cipher-list", SR_STRING_T);
123 checkAndSetLeaf(elem, xpath, "connect-timeout", SR_UINT32_T);
124 checkAndSetLeaf(elem, xpath, "max-reconnect-tries", SR_UINT32_T);
125 checkAndSetLeaf(elem, xpath, "reconnect-wait-time", SR_UINT32_T);
126 checkAndSetLeaf(elem, xpath, "max-row-errors", SR_UINT32_T);
127 checkAndSetLeaf(elem, xpath, "on-fail", SR_STRING_T);
128 ConstElementPtr context = Adaptor::getContext(elem);
129 if (context) {
130 setItem(xpath + "/user-context", Element::create(context->str()),
131 SR_STRING_T);
132 }
133}
134
136 const string& model)
137 : TranslatorBasic(session, model),
138 TranslatorDatabase(session, model) {
139}
140
142}
143
146 try {
147 if ((model_ == KEA_DHCP4_SERVER) ||
148 (model_ == KEA_DHCP6_SERVER)) {
149 return (getDatabasesKea(xpath));
150 }
151 } catch (const sysrepo_exception& ex) {
153 "sysrepo error getting database accesses at '" << xpath
154 << "': " << ex.what());
155 }
157 "getDatabases not implemented for the model: " << model_);
158}
159
162 return getList<TranslatorDatabase>(xpath, *this,
164}
165
166void
168 try {
169 if ((model_ == KEA_DHCP4_SERVER) ||
170 (model_ == KEA_DHCP6_SERVER)) {
171 setDatabasesKea(xpath, elem);
172 } else {
174 "setDatabases not implemented for the model: "
175 << model_);
176 }
177 } catch (const sysrepo_exception& ex) {
179 "sysrepo error setting database accesses '" << elem->str()
180 << "' at '" << xpath << "': " << ex.what());
181 }
182}
183
184void
186 ConstElementPtr elem) {
187 if (!elem) {
188 delItem(xpath);
189 return;
190 }
191 for (size_t i = 0; i < elem->size(); ++i) {
192 ConstElementPtr database = elem->get(i);
193 if (!database->contains("type")) {
194 isc_throw(BadValue, "database without type: " << database->str());
195 }
196 string type = database->get("type")->stringValue();
197 ostringstream key;
198 key << xpath << "[database-type='" << type << "']";
199 setDatabase(key.str(), database, true);
200 }
201}
202
203} // namespace yang
204} // 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.
static isc::data::ConstElementPtr getContext(isc::data::ConstElementPtr parent)
Get user context.
Definition: adaptor.cc:27
Between YANG and JSON translator class for basic values.
Definition: translator.h:19
isc::data::ElementPtr getItem(const std::string &xpath)
Get and translate basic value from YANG to JSON.
Definition: translator.cc:105
std::string model_
The model.
Definition: translator.h:171
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
void delItem(const std::string &xpath)
Delete basic value from YANG.
Definition: translator.cc:294
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
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
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:27
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
Defines the logger used by the top-level component of kea-lfc.