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