Kea: translator_database.cc Source File

| 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
9#include <yang/translator_database.h>
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
25TranslatorDatabase::~TranslatorDatabase() {
26}
27
28 ElementPtr
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) {
36 isc_throw(SysrepoError,
37 "sysrepo error getting database access at '" << xpath
38 << "': " << ex.what());
39 }
40 isc_throw(NotImplemented,
41 "getDatabase not implemented for the model: " << model_);
42}
43
44 ElementPtr
45TranslatorDatabase::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 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
77TranslatorDatabase::setDatabase(const string& xpath,
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 {
85 isc_throw(NotImplemented,
86 "setDatabase not implemented for the model: " << model_);
87 }
88 } catch (const sysrepo_exception& ex) {
89 isc_throw(SysrepoError,
90 "sysrepo error setting database access '" << elem->str()
91 << "' at '" << xpath << "': " << ex.what());
92 }
93}
94
95void
96TranslatorDatabase::setDatabaseKea(const string& xpath,
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
135TranslatorDatabases::TranslatorDatabases(S_Session session,
136 const string& model)
137 : TranslatorBasic(session, model),
138 TranslatorDatabase(session, model) {
139}
140
141TranslatorDatabases::~TranslatorDatabases() {
142}
143
144 ConstElementPtr
145TranslatorDatabases::getDatabases(const string& xpath) {
146 try {
147 if ((model_ == KEA_DHCP4_SERVER) ||
148 (model_ == KEA_DHCP6_SERVER)) {
149 return (getDatabasesKea(xpath));
150 }
151 } catch (const sysrepo_exception& ex) {
152 isc_throw(SysrepoError,
153 "sysrepo error getting database accesses at '" << xpath
154 << "': " << ex.what());
155 }
156 isc_throw(NotImplemented,
157 "getDatabases not implemented for the model: " << model_);
158}
159
160 ElementPtr
161TranslatorDatabases::getDatabasesKea(const string& xpath) {
162 return getList<TranslatorDatabase>(xpath, *this,
163 &TranslatorDatabase::getDatabase);
164}
165
166void
167TranslatorDatabases::setDatabases(const string& xpath, ConstElementPtr elem) {
168 try {
169 if ((model_ == KEA_DHCP4_SERVER) ||
170 (model_ == KEA_DHCP6_SERVER)) {
171 setDatabasesKea(xpath, elem);
172 } else {
173 isc_throw(NotImplemented,
174 "setDatabases not implemented for the model: "
175 << model_);
176 }
177 } catch (const sysrepo_exception& ex) {
178 isc_throw(SysrepoError,
179 "sysrepo error setting database accesses '" << elem->str()
180 << "' at '" << xpath << "': " << ex.what());
181 }
182}
183
184void
185TranslatorDatabases::setDatabasesKea(const string& xpath,
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
adaptor.h
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition: exceptions/exceptions.h:132
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition: exceptions/exceptions.cc:32
isc::NotImplemented
A generic exception that is thrown when a function is not implemented.
Definition: exceptions/exceptions.h:166
isc::yang::Adaptor::getContext
static isc::data::ConstElementPtr getContext(isc::data::ConstElementPtr parent)
Get user context.
Definition: adaptor.cc:27
isc::yang::SysrepoError
Sysrepo error.
Definition: sysrepo_error.h:16
isc::yang::TranslatorBasic
Between YANG and JSON translator class for basic values.
Definition: translator.h:19
isc::yang::TranslatorBasic::getItem
isc::data::ElementPtr getItem(const std::string &xpath)
Get and translate basic value from YANG to JSON.
Definition: translator.cc:105
isc::yang::TranslatorBasic::model_
std::string model_
The model.
Definition: translator.h:171
isc::yang::TranslatorBasic::checkAndGetLeaf
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
isc::yang::TranslatorBasic::delItem
void delItem(const std::string &xpath)
Delete basic value from YANG.
Definition: translator.cc:294
isc::yang::TranslatorBasic::setItem
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
isc::yang::TranslatorBasic::checkAndSetLeaf
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
isc::yang::TranslatorDatabase
Database access translation between YANG and JSON.
Definition: translator_database.h:104
isc::yang::TranslatorDatabase::getDatabaseKea
isc::data::ElementPtr getDatabaseKea(const std::string &xpath)
getDatabase JSON for kea-dhcp[46]-server models.
Definition: translator_database.cc:45
isc::yang::TranslatorDatabase::setDatabase
void setDatabase(const std::string &xpath, isc::data::ConstElementPtr elem, bool skip=false)
Translate and set database access from JSON to YANG.
Definition: translator_database.cc:77
isc::yang::TranslatorDatabase::TranslatorDatabase
TranslatorDatabase(sysrepo::S_Session session, const std::string &model)
Constructor.
Definition: translator_database.cc:21
isc::yang::TranslatorDatabase::~TranslatorDatabase
virtual ~TranslatorDatabase()
Destructor.
Definition: translator_database.cc:25
isc::yang::TranslatorDatabase::getDatabase
isc::data::ElementPtr getDatabase(const std::string &xpath)
Get and translate a database access from YANG to JSON.
Definition: translator_database.cc:29
isc::yang::TranslatorDatabase::setDatabaseKea
void setDatabaseKea(const std::string &xpath, isc::data::ConstElementPtr elem, bool skip)
setDatabase for kea-dhcp[46]-server models.
Definition: translator_database.cc:96
isc::yang::TranslatorDatabases::setDatabasesKea
void setDatabasesKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setDatabases for kea-dhcp[46]-server models.
Definition: translator_database.cc:185
isc::yang::TranslatorDatabases::setDatabases
void setDatabases(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set database accesses from JSON to YANG.
Definition: translator_database.cc:167
isc::yang::TranslatorDatabases::getDatabases
isc::data::ConstElementPtr getDatabases(const std::string &xpath)
Get and translate database accesses from YANG to JSON.
Definition: translator_database.cc:145
isc::yang::TranslatorDatabases::TranslatorDatabases
TranslatorDatabases(sysrepo::S_Session session, const std::string &model)
Constructor.
Definition: translator_database.cc:135
isc::yang::TranslatorDatabases::getDatabasesKea
isc::data::ElementPtr getDatabasesKea(const std::string &xpath)
getDatabases JSON for kea-dhcp[46]-server models.
Definition: translator_database.cc:161
isc::yang::TranslatorDatabases::~TranslatorDatabases
virtual ~TranslatorDatabases()
Destructor.
Definition: translator_database.cc:141
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:210
isc::data
Definition: base_stamped_element.cc:12
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
isc
Defines the logger used by the top-level component of kea-lfc.
Definition: agent_parser.cc:143
translator_database.h
yang_models.h