14 #include <boost/lexical_cast.hpp> 21 #ifndef HAVE_PRE_0_7_6_SYSREPO 22 using namespace sysrepo;
28 TranslatorPool::TranslatorPool(S_Session session,
const string& model)
40 if (
model_ == IETF_DHCPV6_SERVER) {
42 }
else if ((
model_ == KEA_DHCP4_SERVER) ||
43 (
model_ == KEA_DHCP6_SERVER)) {
46 }
catch (
const sysrepo_exception& ex) {
48 "sysrepo error getting pool at '" << xpath
49 <<
"': " << ex.
what());
52 "getPool not implemented for the model: " <<
model_);
63 result->set(
"pool", pool);
67 result->set(
"client-class", guard);
71 result->set(
"valid-lifetime", valid_lifetime);
74 getItem(xpath +
"/preferred-lifetime");
75 if (preferred_lifetime) {
76 result->set(
"preferred-lifetime", preferred_lifetime);
80 result->set(
"renew-timer", renew_time);
84 result->set(
"rebind-timer", rebind_time);
98 result->set(
"pool", prefix);
102 if (!start_addr || !end_addr) {
104 "both start and end addresses at " << xpath);
107 range << start_addr->stringValue() <<
" - " 108 << end_addr->stringValue();
109 result->set(
"pool", Element::create(range.str()));
112 if (options && (options->size() > 0)) {
113 result->set(
"option-data", options);
117 result->set(
"client-class", guard);
120 if (required && (required->size() > 0)) {
121 result->set(
"require-client-classes", required);
125 result->set(
"user-context", Element::fromJSON(context->stringValue()));
133 if (
model_ == IETF_DHCPV6_SERVER) {
135 }
else if ((
model_ == KEA_DHCP4_SERVER) ||
136 (
model_ == KEA_DHCP6_SERVER)) {
140 "setPool not implemented for the model: " <<
model_);
142 }
catch (
const sysrepo_exception& ex) {
144 "sysrepo error setting pool '" << elem->str()
145 <<
"' at '" << xpath <<
"': " << ex.
what());
155 string prefix = pool->stringValue();
156 if (prefix.find(
"/") == string::npos) {
158 "setPoolIetf only supports pools in prefix (vs range) " 159 "format and was called with '" << prefix <<
"'");
161 setItem(xpath +
"/pool-prefix", pool, SR_STRING_T);
162 string addr = prefix.substr(0, prefix.find_first_of(
" /"));
163 uint8_t plen = boost::lexical_cast<unsigned>
164 (prefix.substr(prefix.find_last_of(
" /") + 1, string::npos));
166 setItem(xpath +
"/start-address",
169 setItem(xpath +
"/end-address",
173 if (valid_lifetime) {
174 setItem(xpath +
"/valid-lifetime", valid_lifetime, SR_UINT32_T);
177 if (preferred_lifetime) {
178 setItem(xpath +
"/preferred-lifetime",
179 preferred_lifetime, SR_UINT32_T);
183 setItem(xpath +
"/renew-time", renew_timer, SR_UINT32_T);
187 setItem(xpath +
"/rebind-time", rebind_timer, SR_UINT32_T);
192 setItem(xpath +
"/client-class", guard, SR_STRING_T);
197 setItem(xpath +
"/max-address-count",
198 Element::create(
string(
"disabled")),
208 bool created =
false;
209 string prefix = pool->stringValue();
213 if (prefix.find(
"/") != string::npos) {
214 setItem(xpath +
"/prefix", pool, SR_STRING_T);
219 if (options && (options->size() > 0)) {
225 setItem(xpath +
"/client-class", guard, SR_STRING_T);
229 if (required && (required->size() > 0)) {
231 setItem(xpath +
"/require-client-classes", rclass, SR_STRING_T);
237 setItem(xpath +
"/user-context", Element::create(context->str()),
244 setItem(xpath, list, SR_LIST_T);
250 string& start_address,
string& end_address) {
251 size_t slash = prefix.find(
"/");
252 if (slash != string::npos) {
253 string addr = prefix.substr(0, prefix.find_first_of(
" /"));
254 uint8_t plen = boost::lexical_cast<unsigned>
255 (prefix.substr(prefix.find_last_of(
" /") + 1, string::npos));
260 size_t dash = prefix.find(
"-");
261 if (dash == string::npos) {
263 "getAddresses called with invalid prefix: " << prefix);
265 start_address = prefix.substr(0, prefix.find_first_of(
" -"));
266 end_address = prefix.substr(prefix.find_last_of(
" -") + 1, string::npos);
282 if (
model_ == IETF_DHCPV6_SERVER) {
284 }
else if ((
model_ == KEA_DHCP4_SERVER) ||
285 (
model_ == KEA_DHCP6_SERVER)) {
288 }
catch (
const sysrepo_exception& ex) {
290 "sysrepo error getting pools at '" << xpath
291 <<
"': " << ex.
what());
294 "getPools not implemented for the model: " <<
model_);
300 S_Iter_Value iter =
getIter(xpath +
"/address-pool");
306 const string& pool =
getNext(iter);
318 S_Iter_Value iter =
getIter(xpath +
"/pool");
324 const string& pool =
getNext(iter);
336 if (
model_ == IETF_DHCPV6_SERVER) {
338 }
else if ((
model_ == KEA_DHCP4_SERVER) ||
339 (
model_ == KEA_DHCP6_SERVER)) {
343 "setPools not implemented for the model: " <<
model_);
345 }
catch (
const sysrepo_exception& ex) {
347 "sysrepo error setting pools '" << elem->str()
348 <<
"' at '" << xpath <<
"': " << ex.
what());
354 for (
size_t i = 0; i < elem->size(); ++i) {
356 ostringstream prefix;
357 prefix << xpath <<
"/address-pool[pool-id='" << i <<
"']";
365 for (
size_t i = 0; i < elem->size(); ++i) {
367 if (!pool->contains(
"pool")) {
371 string pref = pool->get(
"pool")->stringValue();
375 ostringstream prefix;
376 prefix << xpath <<
"/pool[start-address='" << start_addr
377 <<
"'][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
isc::asiolink::IOAddress firstAddrInPrefix(const isc::asiolink::IOAddress &prefix, uint8_t len)
This code is based on similar code from the Dibbler project.
void setPool(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set (address) pool from JSON to YANG.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
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.
isc::asiolink::IOAddress lastAddrInPrefix(const isc::asiolink::IOAddress &prefix, uint8_t len)
returns a last address in a given prefix
void setPools(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set (address) pools from JSON to YANG.
#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.
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.
std::string toText() const
Convert the address to a string.
void setPoolIetf6(const std::string &xpath, isc::data::ConstElementPtr elem)
setPool for ietf-dhcpv6-server.
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.