14 #include <boost/lexical_cast.hpp>
20 #ifndef HAVE_PRE_0_7_6_SYSREPO
27 TranslatorPool::TranslatorPool(S_Session session,
const string& model)
39 if (
model_ == IETF_DHCPV6_SERVER) {
41 }
else if ((
model_ == KEA_DHCP4_SERVER) ||
42 (
model_ == KEA_DHCP6_SERVER)) {
45 }
catch (
const sysrepo_exception& ex) {
47 "sysrepo error getting pool at '" << xpath
48 <<
"': " << ex.
what());
51 "getPool not implemented for the model: " <<
model_);
62 result->set(
"pool", pool);
66 result->set(
"client-class", guard);
70 result->set(
"valid-lifetime", valid_lifetime);
73 getItem(xpath +
"/preferred-lifetime");
74 if (preferred_lifetime) {
75 result->set(
"preferred-lifetime", preferred_lifetime);
79 result->set(
"renew-timer", renew_time);
83 result->set(
"rebind-timer", rebind_time);
97 result->set(
"pool", prefix);
101 if (!start_addr || !end_addr) {
103 "both start and end addresses at " << xpath);
106 range << start_addr->stringValue() <<
" - "
107 << end_addr->stringValue();
108 result->set(
"pool", Element::create(range.str()));
111 if (options && (options->size() > 0)) {
112 result->set(
"option-data", options);
116 result->set(
"client-class", guard);
119 if (required && (required->size() > 0)) {
120 result->set(
"require-client-classes", required);
124 result->set(
"user-context", Element::fromJSON(context->stringValue()));
132 if (
model_ == IETF_DHCPV6_SERVER) {
134 }
else if ((
model_ == KEA_DHCP4_SERVER) ||
135 (
model_ == KEA_DHCP6_SERVER)) {
139 "setPool not implemented for the model: " <<
model_);
141 }
catch (
const sysrepo_exception& ex) {
143 "sysrepo error setting pool '" << elem->str()
144 <<
"' at '" << xpath <<
"': " << ex.
what());
154 string prefix = pool->stringValue();
155 if (prefix.find(
"/") == string::npos) {
157 "setPoolIetf only supports pools in prefix (vs range) "
158 "format and was called with '" << prefix <<
"'");
160 setItem(xpath +
"/pool-prefix", pool, SR_STRING_T);
161 string addr = prefix.substr(0, prefix.find_first_of(
" /"));
162 uint8_t plen = boost::lexical_cast<
unsigned>
163 (prefix.substr(prefix.find_last_of(
" /") + 1, string::npos));
165 setItem(xpath +
"/start-address",
168 setItem(xpath +
"/end-address",
172 if (valid_lifetime) {
173 setItem(xpath +
"/valid-lifetime", valid_lifetime, SR_UINT32_T);
176 if (preferred_lifetime) {
177 setItem(xpath +
"/preferred-lifetime",
178 preferred_lifetime, SR_UINT32_T);
182 setItem(xpath +
"/renew-time", renew_timer, SR_UINT32_T);
186 setItem(xpath +
"/rebind-time", rebind_timer, SR_UINT32_T);
191 setItem(xpath +
"/client-class", guard, SR_STRING_T);
196 setItem(xpath +
"/max-address-count",
197 Element::create(
string(
"disabled")),
207 bool created =
false;
208 string prefix = pool->stringValue();
212 if (prefix.find(
"/") != string::npos) {
213 setItem(xpath +
"/prefix", pool, SR_STRING_T);
218 if (options && (options->size() > 0)) {
224 setItem(xpath +
"/client-class", guard, SR_STRING_T);
228 if (required && (required->size() > 0)) {
230 setItem(xpath +
"/require-client-classes", rclass, SR_STRING_T);
236 setItem(xpath +
"/user-context", Element::create(context->str()),
243 setItem(xpath, list, SR_LIST_T);
249 string& start_address,
string& end_address) {
250 size_t slash = prefix.find(
"/");
251 if (slash != string::npos) {
252 string addr = prefix.substr(0, prefix.find_first_of(
" /"));
253 uint8_t plen = boost::lexical_cast<
unsigned>
254 (prefix.substr(prefix.find_last_of(
" /") + 1, string::npos));
259 size_t dash = prefix.find(
"-");
260 if (dash == string::npos) {
262 "getAddresses called with invalid prefix: " << prefix);
264 start_address = prefix.substr(0, prefix.find_first_of(
" -"));
265 end_address = prefix.substr(prefix.find_last_of(
" -") + 1, string::npos);
281 if (
model_ == IETF_DHCPV6_SERVER) {
283 }
else if ((
model_ == KEA_DHCP4_SERVER) ||
284 (
model_ == KEA_DHCP6_SERVER)) {
287 }
catch (
const sysrepo_exception& ex) {
289 "sysrepo error getting pools at '" << xpath
290 <<
"': " << ex.
what());
293 "getPools not implemented for the model: " <<
model_);
299 S_Iter_Value iter =
getIter(xpath +
"/address-pool");
305 const string& pool =
getNext(iter);
317 S_Iter_Value iter =
getIter(xpath +
"/pool");
323 const string& pool =
getNext(iter);
335 if (
model_ == IETF_DHCPV6_SERVER) {
337 }
else if ((
model_ == KEA_DHCP4_SERVER) ||
338 (
model_ == KEA_DHCP6_SERVER)) {
342 "setPools not implemented for the model: " <<
model_);
344 }
catch (
const sysrepo_exception& ex) {
346 "sysrepo error setting pools '" << elem->str()
347 <<
"' at '" << xpath <<
"': " << ex.
what());
353 for (
size_t i = 0; i < elem->size(); ++i) {
355 ostringstream prefix;
356 prefix << xpath <<
"/address-pool[pool-id='" << i <<
"']";
364 for (
size_t i = 0; i < elem->size(); ++i) {
366 if (!pool->contains(
"pool")) {
370 string pref = pool->get(
"pool")->stringValue();
374 ostringstream prefix;
375 prefix << xpath <<
"/pool[start-address='" << start_addr
376 <<
"'][end-address='" << end_addr <<
"']";
isc::data::ElementPtr getPool(const std::string &xpath)
Get and translate a pool from YANG to JSON.
isc::data::ElementPtr getItems(const std::string &xpath)
Get and translate a list of basic values from YANG to JSON.
A generic exception that is thrown when a function is not implemented.
isc::data::ElementPtr getPoolIetf6(const std::string &xpath)
getPool for ietf-dhcpv6-server.
virtual ~TranslatorPools()
Destructor.
isc::data::ElementPtr getPoolsKea(const std::string &xpath)
getPools for kea-dhcp[46]-server.
isc::data::ElementPtr getPoolsIetf(const std::string &xpath)
getPools for ietf-dhcpv6-server.
Between YANG and JSON translator class for basic values.
boost::shared_ptr< Element > ElementPtr
void setPool(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set (address) pool from JSON to YANG.
sysrepo::S_Iter_Value getIter(const std::string &xpath)
List iterator methods keeping the session private.
static void getAddresses(const std::string &prefix, std::string &start_address, std::string &end_address)
Get start and end addresses from prefix.
void setPools(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set (address) pools from JSON to YANG.
IOAddress firstAddrInPrefix(const IOAddress &prefix, uint8_t len)
This code is based on similar code from the Dibbler project.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
void setItem(const std::string &xpath, isc::data::ConstElementPtr elem, sr_type_t type)
Translate and set basic value from JSON to YANG.
void setPoolsById(const std::string &xpath, isc::data::ConstElementPtr elem)
setPools using pool-id.
TranslatorPools(sysrepo::S_Session session, const std::string &model)
Constructor.
void setPoolKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setPool for kea-dhcp[46]-server.
A translator class for converting an option data list between YANG and JSON.
A generic exception that is thrown when an unexpected error condition occurs.
std::string model_
The model.
boost::shared_ptr< const Element > ConstElementPtr
std::string getNext(sysrepo::S_Iter_Value iter)
Get xpath of the next YANG list item.
static isc::data::ConstElementPtr getContext(isc::data::ConstElementPtr parent)
Get user context.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Defines the logger used by the top-level component of kea-dhcp-ddns.
isc::data::ConstElementPtr getOptionDataList(const std::string &xpath)
Get and translate option data list from YANG to JSON.
isc::data::ElementPtr getItem(const std::string &xpath)
Get and translate basic value from YANG to JSON.
A translator class for converting a pool between YANG and JSON.
IOAddress lastAddrInPrefix(const IOAddress &prefix, uint8_t len)
returns a last address in a given prefix
void setPoolIetf6(const std::string &xpath, isc::data::ConstElementPtr elem)
setPool for ietf-dhcpv6-server.
std::string toText() const
Convert the address to a string.
isc::data::ElementPtr getPools(const std::string &xpath)
Get and translate pools from YANG to JSON.
Option data translation between YANG and JSON.
void setOptionDataList(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set option data list from JSON to YANG.
void setPoolsByAddresses(const std::string &xpath, isc::data::ConstElementPtr elem)
setPools using address pair.
virtual ~TranslatorPool()
Destructor.
The IOAddress class represents an IP addresses (version agnostic)
isc::data::ElementPtr getPoolKea(const std::string &xpath)
getPool for kea-dhcp[46]-server.