Kea 2.5.8
translator_database.cc
Go to the documentation of this file.
1// Copyright (C) 2018-2023 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/yang_models.h>
11
12#include <sstream>
13
14using namespace std;
15using namespace isc::data;
16using namespace libyang;
17using namespace sysrepo;
18
19namespace isc {
20namespace yang {
21
22TranslatorDatabase::TranslatorDatabase(Session session, const string& model)
23 : Translator(session, model) {
24}
25
27TranslatorDatabase::getDatabase(DataNode const& data_node) {
28 try {
29 if ((model_ == KEA_DHCP4_SERVER) ||
30 (model_ == KEA_DHCP6_SERVER)) {
31 return (getDatabaseKea(data_node));
32 }
33 } catch (Error const& ex) {
35 "getting database access: " << ex.what());
36 }
38 "getDatabase not implemented for the model: " << model_);
39}
40
43 try {
44 return getDatabase(findXPath(xpath));
45 } catch (NetconfError const&) {
46 return ElementPtr();
47 }
48}
49
51TranslatorDatabase::getDatabaseKea(DataNode const& data_node) {
53
54 getMandatoryDivergingLeaf(result, data_node, "type", "database-type");
55
56 checkAndGetLeaf(result, data_node, "cert-file");
57 checkAndGetLeaf(result, data_node, "cipher-list");
58 checkAndGetLeaf(result, data_node, "connect-timeout");
59 checkAndGetLeaf(result, data_node, "host");
60 checkAndGetLeaf(result, data_node, "key-file");
61 checkAndGetLeaf(result, data_node, "lfc-interval");
62 checkAndGetLeaf(result, data_node, "max-reconnect-tries");
63 checkAndGetLeaf(result, data_node, "max-row-errors");
64 checkAndGetLeaf(result, data_node, "name");
65 checkAndGetLeaf(result, data_node, "on-fail");
66 checkAndGetLeaf(result, data_node, "password");
67 checkAndGetLeaf(result, data_node, "persist");
68 checkAndGetLeaf(result, data_node, "port");
69 checkAndGetLeaf(result, data_node, "read-timeout");
70 checkAndGetLeaf(result, data_node, "readonly");
71 checkAndGetLeaf(result, data_node, "reconnect-wait-time");
72 checkAndGetLeaf(result, data_node, "tcp-user-timeout");
73 checkAndGetLeaf(result, data_node, "trust-anchor");
74 checkAndGetLeaf(result, data_node, "user");
75 checkAndGetLeaf(result, data_node, "write-timeout");
76
77 checkAndGetAndJsonifyLeaf(result, data_node, "user-context");
78
79 return (result->empty() ? ElementPtr() : result);
80}
81
82void
84 ConstElementPtr elem,
85 bool skip) {
86 try {
87 if ((model_ == KEA_DHCP4_SERVER) ||
88 (model_ == KEA_DHCP6_SERVER)) {
89 setDatabaseKea(xpath, elem, skip);
90 } else {
92 "setDatabase not implemented for the model: " << model_);
93 }
94 } catch (Error const& ex) {
96 "setting database access '" << elem->str()
97 << "' : " << ex.what());
98 }
99}
100
101void
103 ConstElementPtr elem,
104 bool skip) {
105 if (!elem) {
106 deleteItem(xpath);
107 return;
108 }
109
110 checkAndSetLeaf(elem, xpath, "connect-timeout", LeafBaseType::Uint32);
111 checkAndSetLeaf(elem, xpath, "cert-file", LeafBaseType::String);
112 checkAndSetLeaf(elem, xpath, "cipher-list", LeafBaseType::String);
113 checkAndSetLeaf(elem, xpath, "host", LeafBaseType::String);
114 checkAndSetLeaf(elem, xpath, "key-file", LeafBaseType::String);
115 checkAndSetLeaf(elem, xpath, "lfc-interval", LeafBaseType::Uint32);
116 checkAndSetLeaf(elem, xpath, "max-reconnect-tries", LeafBaseType::Uint32);
117 checkAndSetLeaf(elem, xpath, "max-row-errors", LeafBaseType::Uint32);
118 checkAndSetLeaf(elem, xpath, "name", LeafBaseType::String);
119 checkAndSetLeaf(elem, xpath, "on-fail", LeafBaseType::String);
120 checkAndSetLeaf(elem, xpath, "password", LeafBaseType::String);
121 checkAndSetLeaf(elem, xpath, "persist", LeafBaseType::Bool);
122 checkAndSetLeaf(elem, xpath, "port", LeafBaseType::Uint16);
123 checkAndSetLeaf(elem, xpath, "readonly", LeafBaseType::Bool);
124 checkAndSetLeaf(elem, xpath, "read-timeout", LeafBaseType::Uint32);
125 checkAndSetLeaf(elem, xpath, "reconnect-wait-time", LeafBaseType::Uint32);
126 checkAndSetLeaf(elem, xpath, "tcp-user-timeout", LeafBaseType::Uint32);
127 checkAndSetLeaf(elem, xpath, "trust-anchor", LeafBaseType::String);
128 checkAndSetLeaf(elem, xpath, "user", LeafBaseType::String);
129 checkAndSetLeaf(elem, xpath, "write-timeout", LeafBaseType::Uint32);
130
131 checkAndSetUserContext(elem, xpath);
132
133 if (!skip) {
134 setMandatoryDivergingLeaf(elem, xpath, "type", "database-type", LeafBaseType::String);
135 }
136}
137
139 const string& model)
140 : Translator(session, model),
141 TranslatorDatabase(session, model) {
142}
143
145TranslatorDatabases::getDatabases(DataNode const& data_node,
146 string const& xpath) {
147 try {
148 if ((model_ == KEA_DHCP4_SERVER) ||
149 (model_ == KEA_DHCP6_SERVER)) {
150 return (getDatabasesKea(data_node, xpath));
151 }
152 } catch (Error const& ex) {
154 "getting database accesses: " << ex.what());
155 }
157 "getDatabases not implemented for the model: " << model_);
158}
159
162 try {
163 return getDatabases(findXPath(xpath), xpath);
164 } catch (NetconfError const&) {
165 return ElementPtr();
166 }
167}
168
170TranslatorDatabases::getDatabasesKea(DataNode const& data_node, string const& xpath) {
171 return getList<TranslatorDatabase>(data_node, xpath, *this,
173}
174
175void
177 try {
178 if ((model_ == KEA_DHCP4_SERVER) ||
179 (model_ == KEA_DHCP6_SERVER)) {
180 setDatabasesKea(xpath, elem);
181 } else {
183 "setDatabases not implemented for the model: "
184 << model_);
185 }
186 } catch (Error const& ex) {
188 "setting database accesses '" << elem->str()
189 << "' : " << ex.what());
190 }
191}
192
193void
195 ConstElementPtr elem) {
196 if (!elem) {
197 deleteItem(xpath);
198 return;
199 }
200 for (size_t i = 0; i < elem->size(); ++i) {
201 ElementPtr database = elem->getNonConst(i);
202 if (!database->contains("type")) {
203 isc_throw(BadValue, "database without type: " << database->str());
204 }
205 string type = database->get("type")->stringValue();
206 ostringstream key;
207 key << xpath << "[database-type='" << type << "']";
208 setDatabase(key.str(), database, true);
209 }
210}
211
212} // namespace yang
213} // 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 ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition: data.cc:304
Database access translation between YANG and JSON.
isc::data::ElementPtr getDatabaseKea(libyang::DataNode const &data_node)
getDatabase JSON for kea-dhcp[46]-server models.
isc::data::ElementPtr getDatabase(libyang::DataNode const &data_node)
Translate a database access from YANG to JSON.
TranslatorDatabase(sysrepo::Session session, const std::string &model)
Constructor.
isc::data::ElementPtr getDatabaseFromAbsoluteXpath(std::string const &xpath)
Translate a database access from YANG to JSON.
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.
void setDatabasesKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setDatabases for kea-dhcp[46]-server models.
isc::data::ElementPtr getDatabasesFromAbsoluteXpath(std::string const &xpath)
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 getDatabases(libyang::DataNode const &data_node, std::string const &xpath)
Translate database accesses from YANG to JSON.
TranslatorDatabases(sysrepo::Session session, const std::string &model)
Constructor.
isc::data::ElementPtr getDatabasesKea(libyang::DataNode const &data_node, std::string const &xpath)
getDatabases JSON for kea-dhcp[46]-server models.
Between YANG and JSON translator class for basic values.
Definition: translator.h:23
void getMandatoryDivergingLeaf(isc::data::ElementPtr &storage, libyang::DataNode const &data_node, std::string const &name, std::string const &yang_name) const
Retrieves a child YANG data node identified by one name from the given parent YANG container node and...
Definition: translator.cc:208
void checkAndSetLeaf(isc::data::ConstElementPtr const &from, std::string const &xpath, std::string const &name, libyang::LeafBaseType const type)
Get an element from given ElementPtr node and set it in sysrepo at given xpath.
Definition: translator.cc:63
void setMandatoryDivergingLeaf(isc::data::ConstElementPtr const &from, std::string const &xpath, std::string const &name, std::string const &yang_name, libyang::LeafBaseType const type)
Get an element from given ElementPtr node and set it in sysrepo at given xpath.
Definition: translator.cc:257
libyang::DataNode findXPath(std::string const &xpath) const
Retrieves a YANG data node by xpath.
Definition: translator.cc:132
void checkAndGetLeaf(isc::data::ElementPtr &storage, libyang::DataNode const &data_node, std::string const &name) const
Retrieves a child YANG data node identified by name from the given parent YANG container node and sto...
Definition: translator.cc:32
void checkAndSetUserContext(isc::data::ConstElementPtr const &from, std::string const &xpath)
Get an element from given ElementPtr node and set it in sysrepo at given xpath.
Definition: translator.cc:99
void deleteItem(const std::string &xpath)
Delete basic value from YANG.
Definition: translator.cc:120
std::string model_
The model.
Definition: translator.h:427
void checkAndGetAndJsonifyLeaf(isc::data::ElementPtr &storage, libyang::DataNode const &data_node, const std::string &name) const
Retrieves a child YANG data node identified by name from the given parent YANG container node,...
Definition: translator.cc:53
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:29
boost::shared_ptr< Element > ElementPtr
Definition: data.h:28
Defines the logger used by the top-level component of kea-lfc.
Generic NETCONF error.
Definition: netconf_error.h:30