Kea 2.7.6
translator_pd_pool.cc
Go to the documentation of this file.
1// Copyright (C) 2018-2024 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 require-client-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, "client-class");
151 checkAndGetLeaf(result, data_node, "delegated-len");
152 checkAndGetLeaf(result, data_node, "require-client-classes");
153
154 checkAndGetLeaf(result, data_node, "pool-id");
155
156 checkAndGetAndJsonifyLeaf(result, data_node, "user-context");
157
158 ConstElementPtr options = getOptionDataList(data_node);
159 if (options) {
160 result->set("option-data", options);
161 }
162
163 return (result->empty() ? ElementPtr() : result);
164}
165
166void
168 try {
169 if (model_ == IETF_DHCPV6_SERVER) {
170 setPdPoolIetf6(xpath, elem);
171 } else if (model_ == KEA_DHCP6_SERVER) {
172 setPdPoolKea(xpath, elem);
173 } else {
175 "setPdPool not implemented for the model: " << model_);
176 }
177 } catch (Error const& ex) {
179 "setting pd-pool '" << elem->str()
180 << "' : " << ex.what());
181 }
182}
183
184void
186 ConstElementPtr base = elem->get("prefix");
187 ConstElementPtr length = elem->get("prefix-len");
188 if (!base || !length) {
190 "setPdPoolIetf6 requires prefix and prefix length: "
191 << elem->str());
192 }
193 ostringstream prefix;
194 prefix << base->stringValue() << "/" << length->intValue();
195 setItem(xpath + "/prefix", Element::create(prefix.str()), LeafBaseType::String);
196 setItem(xpath + "/prefix-length", length, LeafBaseType::Uint8);
197
198 checkAndSetLeaf(elem, xpath, "client-class", LeafBaseType::String);
199 checkAndSetLeaf(elem, xpath, "preferred-lifetime", LeafBaseType::Uint32);
200 checkAndSetLeaf(elem, xpath, "valid-lifetime", LeafBaseType::Uint32);
201
202 checkAndSetDivergingLeaf(elem, xpath, "rebind-timer", "rebind-time", LeafBaseType::Uint32);
203 checkAndSetDivergingLeaf(elem, xpath, "renew-timer", "renew-time", LeafBaseType::Uint32);
204
205 // Set max pd space utilization to disabled.
206 setItem(xpath + "/max-pd-space-utilization", Element::create("disabled"),
207 LeafBaseType::Enum);
208
209 // Skip rapid-commit.
210 // @todo: option-data
211}
212
213void
215 // Keys are set by setting the list itself.
216 setItem(xpath, ElementPtr(), LeafBaseType::Unknown);
217
218 checkAndSetLeaf(elem, xpath, "client-class", LeafBaseType::String);
219 checkAndSetLeaf(elem, xpath, "delegated-len", LeafBaseType::Uint8);
220
221 checkAndSetLeafList(elem, xpath, "require-client-classes", LeafBaseType::String);
222
223 checkAndSetLeaf(elem, xpath, "pool-id", LeafBaseType::Dec64);
224
225 checkAndSetUserContext(elem, xpath);
226
227 ConstElementPtr xprefix = elem->get("excluded-prefix");
228 ConstElementPtr xlen = elem->get("excluded-prefix-len");
229 if (xprefix && xlen) {
230 ostringstream xpref;
231 xpref << xprefix->stringValue() << "/" << xlen->intValue();
232 setItem(xpath + "/excluded-prefix", Element::create(xpref.str()), LeafBaseType::String);
233 }
234 ConstElementPtr options = elem->get("option-data");
235 if (options && !options->empty()) {
236 setOptionDataList(xpath, options);
237 }
238}
239
240TranslatorPdPools::TranslatorPdPools(Session session, const string& model)
241 : Translator(session, model),
242 TranslatorOptionData(session, model),
243 TranslatorOptionDataList(session, model),
244 TranslatorPdPool(session, model) {
245}
246
248TranslatorPdPools::getPdPools(DataNode const& data_node) {
249 try {
250 if ((model_ == IETF_DHCPV6_SERVER) ||
251 (model_ == KEA_DHCP6_SERVER)) {
252 return (getPdPoolsCommon(data_node));
253 }
254 } catch (Error const& ex) {
256 "getting pd-pools:"
257 << ex.what());
258 }
260 "getPdPools not implemented for the model: " << model_);
261}
262
265 try {
266 return getPdPools(findXPath(xpath));
267 } catch (NetconfError const&) {
268 return ElementPtr();
269 }
270}
271
273TranslatorPdPools::getPdPoolsCommon(DataNode const& data_node) {
274 return getList<TranslatorPdPool>(data_node, "pd-pool", *this,
276}
277
278void
280 try {
281 if (model_ == IETF_DHCPV6_SERVER) {
282 setPdPoolsId(xpath, elem);
283 } else if (model_ == KEA_DHCP6_SERVER) {
284 setPdPoolsPrefix(xpath, elem);
285 } else {
287 "setPdPools not implemented for the model: " << model_);
288 }
289 } catch (Error const& ex) {
291 "setting pools '" << elem->str()
292 << "' : " << ex.what());
293 }
294}
295
296void
298 for (size_t i = 0; i < elem->size(); ++i) {
299 ElementPtr pool = elem->getNonConst(i);
300 ostringstream prefix;
301 prefix << xpath << "/pd-pool[pool-id='" << i << "']";
302 setPdPool(prefix.str(), pool);
303 }
304}
305
306void
308 ConstElementPtr elem) {
309 for (size_t i = 0; i < elem->size(); ++i) {
310 ElementPtr pool = elem->getNonConst(i);
311 if (!pool->contains("prefix") || !pool->contains("prefix-len")) {
312 isc_throw(BadValue, "pd-pool requires prefix and prefix length: "
313 << pool->str());
314 }
315 ostringstream prefix;
316 prefix << xpath << "/pd-pool[prefix='"
317 << pool->get("prefix")->stringValue() << "/"
318 << pool->get("prefix-len")->intValue() << "']";
319 setPdPool(prefix.str(), pool);
320 }
321}
322
323} // namespace yang
324} // 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 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 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.
isc::data::ConstElementPtr getOptionDataList(libyang::DataNode const &data_node)
Translate option data list from YANG to JSON.
Option data translation between YANG and JSON.
Prefix delegation pool 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.