16 #ifndef HAVE_PRE_0_7_6_SYSREPO
22 string encode64(
const string& input) {
23 vector<uint8_t> binary;
24 binary.resize(input.size());
25 memmove(&binary[0], input.c_str(), binary.size());
29 string decode64(
const string& input) {
30 vector<uint8_t> binary;
33 result.resize(binary.size());
34 memmove(&result[0], &binary[0], result.size());
43 TranslatorBasic::TranslatorBasic(S_Session session,
const string& model)
44 : session_(session), model_(model) {
51 #ifndef HAVE_PRE_0_7_6_SYSREPO
59 switch (s_val->type()) {
61 case SR_CONTAINER_PRESENCE_T:
66 return (Element::createList());
69 return (Element::create(
string(s_val->data()->get_string())));
72 return (Element::create(s_val->data()->get_bool() ?
true :
false));
75 return (Element::create(static_cast<long long>(s_val->data()->get_uint8())));
78 return (Element::create(static_cast<long long>(s_val->data()->get_uint16())));
81 return (Element::create(static_cast<long long>(s_val->data()->get_uint32())));
84 return (Element::create(static_cast<long long>(s_val->data()->get_int8())));
87 return (Element::create(static_cast<long long>(s_val->data()->get_int16())));
90 return (Element::create(static_cast<long long>(s_val->data()->get_int32())));
93 return (Element::create(s_val->data()->get_decimal64()));
95 case SR_IDENTITYREF_T:
96 return (Element::create(
string(s_val->data()->get_identityref())));
99 return (Element::create(
string(s_val->data()->get_enum())));
102 return (Element::create(decode64(s_val->data()->get_binary())));
106 "value called with unsupported type: " << s_val->type());
114 s_val =
session_->get_item(xpath.c_str());
115 }
catch (
const sysrepo_exception& ex) {
117 <<
"': " << ex.
what());
122 return (
value(s_val));
129 s_vals =
session_->get_items(xpath.c_str());
134 for (
size_t i = 0; i < s_vals->val_cnt(); ++i) {
135 S_Val s_val = s_vals->val(i);
136 result->add(
value(s_val));
139 }
catch (
const sysrepo_exception& ex) {
141 "sysrepo error getting item at '" << xpath
142 <<
"': " << ex.
what());
154 case SR_CONTAINER_PRESENCE_T:
158 if (elem->getType() != Element::list) {
166 case SR_IDENTITYREF_T:
168 if (elem->getType() != Element::string) {
170 "value for a string called with not a string: "
173 s_val.reset(
new Val(elem->stringValue().c_str(), type));
177 if (elem->getType() != Element::boolean) {
179 "value for a boolean called with not a boolean: "
182 s_val.reset(
new Val(elem->boolValue(), type));
186 if (elem->getType() != Element::integer) {
188 "value for an integer called with not an integer: "
191 #ifdef HAVE_POST_0_7_7_SYSREPO
192 s_val.reset(
new Val(static_cast<uint8_t>(elem->intValue())));
194 s_val.reset(
new Val(static_cast<uint8_t>(elem->intValue()), type));
199 if (elem->getType() != Element::integer) {
201 "value for an integer called with not an integer: "
204 #ifdef HAVE_POST_0_7_7_SYSREPO
205 s_val.reset(
new Val(static_cast<uint16_t>(elem->intValue())));
207 s_val.reset(
new Val(static_cast<uint16_t>(elem->intValue()), type));
212 if (elem->getType() != Element::integer) {
214 "value for an integer called with not an integer: "
217 #ifdef HAVE_POST_0_7_7_SYSREPO
218 s_val.reset(
new Val(static_cast<uint32_t>(elem->intValue())));
220 s_val.reset(
new Val(static_cast<uint32_t>(elem->intValue()), type));
225 if (elem->getType() != Element::integer) {
227 "value for an integer called with not an integer: "
230 #ifdef HAVE_POST_0_7_7_SYSREPO
231 s_val.reset(
new Val(static_cast<int8_t>(elem->intValue())));
233 s_val.reset(
new Val(static_cast<int8_t>(elem->intValue()), type));
238 if (elem->getType() != Element::integer) {
240 "value for an integer called with not an integer: "
243 #ifdef HAVE_POST_0_7_7_SYSREPO
244 s_val.reset(
new Val(static_cast<int16_t>(elem->intValue())));
246 s_val.reset(
new Val(static_cast<int16_t>(elem->intValue()), type));
251 if (elem->getType() != Element::integer) {
253 "value for an integer called with not an integer: "
256 #ifdef HAVE_POST_0_7_7_SYSREPO
257 s_val.reset(
new Val(static_cast<int32_t>(elem->intValue())));
259 s_val.reset(
new Val(static_cast<int32_t>(elem->intValue()), type));
264 if (elem->getType() != Element::real) {
267 s_val.reset(
new Val(elem->doubleValue()));
271 if (elem->getType() != Element::string) {
273 "value for a base64 called with not a string: "
276 s_val.reset(
new Val(encode64(elem->stringValue()).c_str(), type));
281 "value called with unsupported type: " << type);
290 S_Val s_val =
value(elem, type);
291 if (!s_val && (type != SR_LIST_T)) {
295 session_->set_item(xpath.c_str(), s_val);
296 }
catch (
const sysrepo_exception& ex) {
298 "sysrepo error setting item '" << elem->str()
299 <<
"' at '" << xpath <<
"': " << ex.
what());
306 session_->delete_item(xpath.c_str());
307 }
catch (
const sysrepo_exception& ex) {
309 "sysrepo error deleting item at '"
310 << xpath <<
"': " << ex.
what());
317 return (
session_->get_items_iter(xpath.c_str()));
327 s_val =
session_->get_item_next(iter);
328 }
catch (
const sysrepo_exception&) {
335 return (s_val->xpath());
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.
virtual ~TranslatorBasic()
Destructor.
boost::shared_ptr< Element > ElementPtr
static isc::data::ElementPtr value(sysrepo::S_Val s_val)
Translate basic value from YANG to JSON.
sysrepo::S_Iter_Value getIter(const std::string &xpath)
List iterator methods keeping the session private.
#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.
boost::shared_ptr< const Element > ConstElementPtr
std::string getNext(sysrepo::S_Iter_Value iter)
Get xpath of the next YANG list item.
void decodeBase64(const std::string &input, std::vector< uint8_t > &result)
Decode a text encoded in the base64 format into the original data.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
void delItem(const std::string &xpath)
Delete basic value from YANG.
Defines the logger used by the top-level component of kea-dhcp-ddns.
isc::data::ElementPtr getItem(const std::string &xpath)
Get and translate basic value from YANG to JSON.
std::string encodeBase64(const std::vector< uint8_t > &binary)
Encode binary data in the base64 format.
sysrepo::S_Session session_
The sysrepo session.