Kea 3.1.1
translator_pd_pool.cc
Go to the documentation of this file.
1// Copyright (C) 2018-2025 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 <boost/lexical_cast.hpp>
13
14#include <sstream>
15
16using namespace std;
17using namespace isc::data;
18using namespace libyang;
19using namespace sysrepo;
20
21namespace isc {
22namespace yang {
23
24TranslatorPdPool::TranslatorPdPool(Session session, const string& model)
25 : Translator(session, model),
26 TranslatorOptionData(session, model),
27 TranslatorOptionDataList(session, model) {
28}
29
31TranslatorPdPool::getPdPool(DataNode const& data_node) {
32 try {
33 if (model_ == IETF_DHCPV6_SERVER) {
34 return (getPdPoolIetf6(data_node));
35 } else if (model_ == KEA_DHCP6_SERVER) {
36 return (getPdPoolKea(data_node));
37 }
38 } catch (Error const& ex) {
40 "getting pd-pool:"
41 << ex.what());
42 }
44 "getPdPool not implemented for the model: " << model_);
45}
46
49 try {
50 return getPdPool(findXPath(xpath));
51 } catch (NetconfError const&) {
52 return ElementPtr();
53 }
54}
55
57TranslatorPdPool::getPdPoolIetf6(DataNode const& data_node) {
59
60 ConstElementPtr pref = getItem(data_node, "prefix");
61 if (!pref) {
62 isc_throw(MissingNode, "getPdPoolIetf6: prefix is required");
63 }
64 const string& prefix = pref->stringValue();
65 size_t slash = prefix.find("/");
66 if (slash == string::npos) {
68 "getPdPoolIetf6: no '/' in prefix '" << prefix << "'");
69 }
70 const string& address = prefix.substr(0, slash);
71 if (address.empty()) {
73 "getPdPoolIetf6: malformed prefix '" << prefix << "'");
74 }
75 result->set("prefix", Element::create(address));
76
77 // Silly: the prefix length is specified twice...
78 getMandatoryDivergingLeaf(result, data_node, "prefix-len", "prefix-length");
79
80 checkAndGetLeaf(result, data_node, "preferred-lifetime");
81 checkAndGetLeaf(result, data_node, "client-class");
82 checkAndGetLeaf(result, data_node, "valid-lifetime");
83
84 checkAndGetDivergingLeaf(result, data_node, "rebind-timer", "rebind-time");
85 checkAndGetDivergingLeaf(result, data_node, "renew-timer", "renew-time");
86
87 // no evaluate-additional-classes nor user-context.
88 // Skip max-pd-space-utilization.
89 // Skip rapid-commit.
90 // @todo: option-data
91
92 return (result->empty() ? ElementPtr() : result);
93}
94
96TranslatorPdPool::getPdPoolKea(DataNode const& data_node) {
98 ConstElementPtr pref = getItem(data_node, "prefix");
99 if (!pref) {
100 isc_throw(MissingNode, "getPdPoolKea: no prefix defined");
101 }
102 const string& prefix = pref->stringValue();
103 size_t slash = prefix.find("/");
104 if (slash == string::npos) {
106 "getPdPoolKea: no '/' in prefix '" << prefix << "'");
107 }
108 const string& address = prefix.substr(0, slash);
109 const string& length = prefix.substr(slash + 1, string::npos);
110 if (address.empty() || length.empty()) {
112 "getPdPoolKea: malformed prefix '" << prefix << "'");
113 }
114 result->set("prefix", Element::create(address));
115 try {
116 unsigned len = boost::lexical_cast<unsigned>(length);
117 result->set("prefix-len", Element::create(static_cast<int>(len)));
118 } catch (const boost::bad_lexical_cast&) {
120 "getPdPoolKea: bad prefix length in '" << prefix << "'");
121 }
122 ConstElementPtr xpref = getItem(data_node, "excluded-prefix");
123 if (xpref) {
124 const string& xprefix = xpref->stringValue();
125 size_t xslash = xprefix.find("/");
126 if (xslash == string::npos) {
128 "getPdPoolKea: no '/' in excluded prefix '"
129 << xprefix << "'");
130 }
131 const string& xaddress = xprefix.substr(0, xslash);
132 const string& xlength = xprefix.substr(xslash + 1, string::npos);
133 if (xaddress.empty() || xlength.empty()) {
135 "getPdPoolKea: malformed excluded prefix '"
136 << xprefix << "'");
137 }
138 result->set("excluded-prefix", Element::create(xaddress));
139 try {
140 unsigned xlen = boost::lexical_cast<unsigned>(xlength);
141 result->set("excluded-prefix-len",
142 Element::create(static_cast<int>(xlen)));
143 } catch (const boost::bad_lexical_cast&) {
145 "getPdPoolKea: bad excluded prefix length in '"
146 << xprefix << "'");
147 }
148 }
149
150 checkAndGetLeaf(result, data_node, "ddns-generated-prefix");
151 checkAndGetLeaf(result, data_node, "ddns-override-client-update");
152 checkAndGetLeaf(result, data_node, "ddns-override-no-update");
153 checkAndGetLeaf(result, data_node, "ddns-qualifying-suffix");
154 checkAndGetLeaf(result, data_node, "ddns-replace-client-name");
155 checkAndGetLeaf(result, data_node, "ddns-send-updates");
156 checkAndGetLeaf(result, data_node, "ddns-ttl-percent");
157 checkAndGetLeaf(result, data_node, "ddns-ttl");
158 checkAndGetLeaf(result, data_node, "ddns-ttl-min");
159 checkAndGetLeaf(result, data_node, "ddns-ttl-max");
160 checkAndGetLeaf(result, data_node, "ddns-update-on-renew");
161 checkAndGetLeaf(result, data_node, "ddns-use-conflict-resolution");
162 checkAndGetLeaf(result, data_node, "ddns-conflict-resolution-mode");
163 checkAndGetLeaf(result, data_node, "hostname-char-replacement");
164 checkAndGetLeaf(result, data_node, "hostname-char-set");
165 checkAndGetLeaf(result, data_node, "client-class");
166 checkAndGetLeaf(result, data_node, "client-classes");
167 checkAndGetLeaf(result, data_node, "delegated-len");
168 checkAndGetLeaf(result, data_node, "require-client-classes");
169 checkAndGetLeaf(result, data_node, "evaluate-additional-classes");
170 checkAndGetLeaf(result, data_node, "pool-id");
171 checkAndGetAndJsonifyLeaf(result, data_node, "user-context");
172
173 ConstElementPtr options = getOptionDataList(data_node);
174 if (options) {
175 result->set("option-data", options);
176 }
177
178 return (result->empty() ? ElementPtr() : result);
179}
180
181void
183 try {
184 if (model_ == IETF_DHCPV6_SERVER) {
185 setPdPoolIetf6(xpath, elem);
186 } else if (model_ == KEA_DHCP6_SERVER) {
187 setPdPoolKea(xpath, elem);
188 } else {
190 "setPdPool not implemented for the model: " << model_);
191 }
192 } catch (Error const& ex) {
194 "setting pd-pool '" << elem->str()
195 << "' : " << ex.what());
196 }
197}
198
199void
201 ConstElementPtr base = elem->get("prefix");
202 ConstElementPtr length = elem->get("prefix-len");
203 if (!base || !length) {
205 "setPdPoolIetf6 requires prefix and prefix length: "
206 << elem->str());
207 }
208 ostringstream prefix;
209 prefix << base->stringValue() << "/" << length->intValue();
210 setItem(xpath + "/prefix", Element::create(prefix.str()), LeafBaseType::String);
211 setItem(xpath + "/prefix-length", length, LeafBaseType::Uint8);
212
213 checkAndSetLeaf(elem, xpath, "client-class", LeafBaseType::String);
214 checkAndSetLeaf(elem, xpath, "preferred-lifetime", LeafBaseType::Uint32);
215 checkAndSetLeaf(elem, xpath, "valid-lifetime", LeafBaseType::Uint32);
216
217 checkAndSetDivergingLeaf(elem, xpath, "rebind-timer", "rebind-time", LeafBaseType::Uint32);
218 checkAndSetDivergingLeaf(elem, xpath, "renew-timer", "renew-time", LeafBaseType::Uint32);
219
220 // Set max pd space utilization to disabled.
221 setItem(xpath + "/max-pd-space-utilization", Element::create("disabled"),
222 LeafBaseType::Enum);
223
224 // Skip rapid-commit.
225 // @todo: option-data
226}
227
228void
230 // Keys are set by setting the list itself.
231 setItem(xpath, ElementPtr(), LeafBaseType::Unknown);
232
233 checkAndSetLeaf(elem, xpath, "client-class", LeafBaseType::String);
234 checkAndSetLeaf(elem, xpath, "delegated-len", LeafBaseType::Uint8);
235 checkAndSetLeaf(elem, xpath, "ddns-generated-prefix", LeafBaseType::String);
236 checkAndSetLeaf(elem, xpath, "ddns-override-client-update", LeafBaseType::Bool);
237 checkAndSetLeaf(elem, xpath, "ddns-override-no-update", LeafBaseType::Bool);
238 checkAndSetLeaf(elem, xpath, "ddns-qualifying-suffix", LeafBaseType::String);
239 checkAndSetLeaf(elem, xpath, "ddns-replace-client-name", LeafBaseType::String);
240 checkAndSetLeaf(elem, xpath, "ddns-send-updates", LeafBaseType::Bool);
241 checkAndSetLeaf(elem, xpath, "ddns-ttl-percent", LeafBaseType::Dec64);
242 checkAndSetLeaf(elem, xpath, "ddns-ttl", LeafBaseType::Uint32);
243 checkAndSetLeaf(elem, xpath, "ddns-ttl-min", LeafBaseType::Uint32);
244 checkAndSetLeaf(elem, xpath, "ddns-ttl-max", LeafBaseType::Uint32);
245 checkAndSetLeaf(elem, xpath, "ddns-update-on-renew", LeafBaseType::Bool);
246 checkAndSetLeaf(elem, xpath, "ddns-use-conflict-resolution", LeafBaseType::Bool);
247 checkAndSetLeaf(elem, xpath, "ddns-conflict-resolution-mode", LeafBaseType::Enum);
248 checkAndSetLeaf(elem, xpath, "hostname-char-replacement", LeafBaseType::String);
249 checkAndSetLeaf(elem, xpath, "hostname-char-set", LeafBaseType::String);
250 checkAndSetLeafList(elem, xpath, "client-classes", LeafBaseType::String);
251 checkAndSetLeafList(elem, xpath, "require-client-classes", LeafBaseType::String);
252 checkAndSetLeafList(elem, xpath, "evaluate-additional-classes", LeafBaseType::String);
253 checkAndSetLeaf(elem, xpath, "pool-id", LeafBaseType::Dec64);
254 checkAndSetUserContext(elem, xpath);
255
256 ConstElementPtr xprefix = elem->get("excluded-prefix");
257 ConstElementPtr xlen = elem->get("excluded-prefix-len");
258 if (xprefix && xlen) {
259 ostringstream xpref;
260 xpref << xprefix->stringValue() << "/" << xlen->intValue();
261 setItem(xpath + "/excluded-prefix", Element::create(xpref.str()), LeafBaseType::String);
262 }
263 ConstElementPtr options = elem->get("option-data");
264 if (options && !options->empty()) {
265 setOptionDataList(xpath, options);
266 }
267}
268
269TranslatorPdPools::TranslatorPdPools(Session session, const string& model)
270 : Translator(session, model),
271 TranslatorOptionData(session, model),
272 TranslatorOptionDataList(session, model),
273 TranslatorPdPool(session, model) {
274}
275
277TranslatorPdPools::getPdPools(DataNode const& data_node) {
278 try {
279 if ((model_ == IETF_DHCPV6_SERVER) ||
280 (model_ == KEA_DHCP6_SERVER)) {
281 return (getPdPoolsCommon(data_node));
282 }
283 } catch (Error const& ex) {
285 "getting pd-pools:"
286 << ex.what());
287 }
289 "getPdPools not implemented for the model: " << model_);
290}
291
294 try {
295 return getPdPools(findXPath(xpath));
296 } catch (NetconfError const&) {
297 return ElementPtr();
298 }
299}
300
302TranslatorPdPools::getPdPoolsCommon(DataNode const& data_node) {
303 return getList<TranslatorPdPool>(data_node, "pd-pool", *this,
305}
306
307void
309 try {
310 if (model_ == IETF_DHCPV6_SERVER) {
311 setPdPoolsId(xpath, elem);
312 } else if (model_ == KEA_DHCP6_SERVER) {
313 setPdPoolsPrefix(xpath, elem);
314 } else {
316 "setPdPools not implemented for the model: " << model_);
317 }
318 } catch (Error const& ex) {
320 "setting pools '" << elem->str()
321 << "' : " << ex.what());
322 }
323}
324
325void
327 for (size_t i = 0; i < elem->size(); ++i) {
328 ElementPtr pool = elem->getNonConst(i);
329 ostringstream prefix;
330 prefix << xpath << "/pd-pool[pool-id='" << i << "']";
331 setPdPool(prefix.str(), pool);
332 }
333}
334
335void
337 ConstElementPtr elem) {
338 for (size_t i = 0; i < elem->size(); ++i) {
339 ElementPtr pool = elem->getNonConst(i);
340 if (!pool->contains("prefix") || !pool->contains("prefix-len")) {
341 isc_throw(BadValue, "pd-pool requires prefix and prefix length: "
342 << pool->str());
343 }
344 ostringstream prefix;
345 prefix << xpath << "/pd-pool[prefix='"
346 << pool->get("prefix")->stringValue() << "/"
347 << pool->get("prefix-len")->intValue() << "']";
348 setPdPool(prefix.str(), pool);
349 }
350}
351
352} // namespace yang
353} // namespace isc
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition data.cc:249
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
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.
A translator class for converting an option data list between YANG and JSON.
void setOptionDataList(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set option data list from JSON to YANG.
TranslatorOptionDataList(sysrepo::Session session, const std::string &model)
Constructor.
isc::data::ConstElementPtr getOptionDataList(libyang::DataNode const &data_node)
Translate option data list from YANG to JSON.
Option data translation between YANG and JSON.
void setPdPoolIetf6(const std::string &xpath, isc::data::ConstElementPtr elem)
setPdPool for ietf-dhcpv6-server.
isc::data::ElementPtr getPdPoolKea(libyang::DataNode const &data_node)
getPdPool for kea-dhcp6-server.
isc::data::ElementPtr getPdPoolFromAbsoluteXpath(std::string const &xpath)
Translate a pd-pool from YANG to JSON.
void setPdPool(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set pd-pool from JSON to YANG.
isc::data::ElementPtr getPdPoolIetf6(libyang::DataNode const &data_node)
getPdPool for ietf-dhcpv6-server.
void setPdPoolKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setPdPool for kea-dhcp6-server.
isc::data::ElementPtr getPdPool(libyang::DataNode const &data_node)
Translate a pd-pool from YANG to JSON.
TranslatorPdPool(sysrepo::Session session, const std::string &model)
Constructor.
void setPdPoolsId(const std::string &xpath, isc::data::ConstElementPtr elem)
setPdPools using pool-id.
void setPdPoolsPrefix(const std::string &xpath, isc::data::ConstElementPtr elem)
setPdPools using prefix.
TranslatorPdPools(sysrepo::Session session, const std::string &model)
Constructor.
void setPdPools(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set pd-pools from JSON to YANG.
isc::data::ElementPtr getPdPoolsFromAbsoluteXpath(std::string const &xpath)
Translate pd-pools from YANG to JSON.
isc::data::ElementPtr getPdPools(libyang::DataNode const &data_node)
Translate pd-pools from YANG to JSON.
isc::data::ElementPtr getPdPoolsCommon(libyang::DataNode const &data_node)
getPdPools common part.
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...
isc::data::ElementPtr getList(libyang::DataNode const &data_node, std::string const &xpath, T &t, isc::data::ElementPtr(T::*f)(libyang::DataNode const &)) const
Retrieve a list as ElementPtr from sysrepo from a certain xpath.
Definition translator.h:274
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
isc::data::ElementPtr getItem(libyang::DataNode const &data_node, std::string const &xpath) const
Translate a basic value from YANG to JSON for a given xpath that is relative to the given source node...
libyang::DataNode findXPath(std::string const &xpath) const
Retrieves a YANG data node by xpath.
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 checkAndGetDivergingLeaf(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 name from the given parent YANG container node and sto...
Definition translator.cc:42
void setItem(const std::string &xpath, isc::data::ConstElementPtr const elem, libyang::LeafBaseType const type)
Translate and set basic value from JSON to YANG.
std::string model_
The model.
Definition translator.h:427
void checkAndSetDivergingLeaf(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:74
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
void checkAndSetLeafList(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 as a leaf-list.
Definition translator.cc:86
#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.
Missing node error.
Generic NETCONF error.