16 #ifndef HAVE_PRE_0_7_6_SYSREPO 17 using namespace 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) {
55 switch (s_val->type()) {
57 case SR_CONTAINER_PRESENCE_T:
62 return (Element::createList());
65 return (Element::create(
string(s_val->data()->get_string())));
68 return (Element::create(s_val->data()->get_bool() ? true :
false));
71 return (Element::create(static_cast<long long>(s_val->data()->get_uint8())));
74 return (Element::create(static_cast<long long>(s_val->data()->get_uint16())));
77 return (Element::create(static_cast<long long>(s_val->data()->get_uint32())));
80 return (Element::create(static_cast<long long>(s_val->data()->get_int8())));
83 return (Element::create(static_cast<long long>(s_val->data()->get_int16())));
86 return (Element::create(static_cast<long long>(s_val->data()->get_int32())));
88 case SR_IDENTITYREF_T:
89 return (Element::create(
string(s_val->data()->get_identityref())));
92 return (Element::create(
string(s_val->data()->get_enum())));
95 return (Element::create(decode64(s_val->data()->get_binary())));
99 "value called with unupported type: " << s_val->type());
107 s_val =
session_->get_item(xpath.c_str());
108 }
catch (
const sysrepo_exception& ex) {
110 <<
"': " << ex.
what());
115 return (
value(s_val));
122 s_vals =
session_->get_items(xpath.c_str());
127 for (
size_t i = 0; i < s_vals->val_cnt(); ++i) {
128 S_Val s_val = s_vals->val(i);
129 result->add(
value(s_val));
132 }
catch (
const sysrepo_exception& ex) {
134 "sysrepo error getting item at '" << xpath
135 <<
"': " << ex.
what());
147 case SR_CONTAINER_PRESENCE_T:
151 if (elem->getType() != Element::list) {
159 case SR_IDENTITYREF_T:
161 if (elem->getType() != Element::string) {
163 "value for a string called with not a string: " 166 s_val.reset(
new Val(elem->stringValue().c_str(), type));
170 if (elem->getType() != Element::boolean) {
172 "value for a boolean called with not a boolean: " 175 s_val.reset(
new Val(elem->boolValue(), type));
179 if (elem->getType() != Element::integer) {
181 "value for an integer called with not an integer: " 184 s_val.reset(
new Val(static_cast<uint8_t>(elem->intValue()), type));
188 if (elem->getType() != Element::integer) {
190 "value for an integer called with not an integer: " 193 s_val.reset(
new Val(static_cast<uint16_t>(elem->intValue()), type));
197 if (elem->getType() != Element::integer) {
199 "value for an integer called with not an integer: " 202 s_val.reset(
new Val(static_cast<uint32_t>(elem->intValue()), type));
206 if (elem->getType() != Element::integer) {
208 "value for an integer called with not an integer: " 211 s_val.reset(
new Val(static_cast<int8_t>(elem->intValue()), type));
215 if (elem->getType() != Element::integer) {
217 "value for an integer called with not an integer: " 220 s_val.reset(
new Val(static_cast<int16_t>(elem->intValue()), type));
224 if (elem->getType() != Element::integer) {
226 "value for an integer called with not an integer: " 229 s_val.reset(
new Val(static_cast<int32_t>(elem->intValue()), type));
233 if (elem->getType() != Element::string) {
235 "value for a base64 called with not a string: " 238 s_val.reset(
new Val(encode64(elem->stringValue()).c_str(), type));
243 "value called with unupported type: " << type);
252 S_Val s_val =
value(elem, type);
253 if (!s_val && (type != SR_LIST_T)) {
257 session_->set_item(xpath.c_str(), s_val);
258 }
catch (
const sysrepo_exception& ex) {
260 "sysrepo error setting item '" << elem->str()
261 <<
"' at '" << xpath <<
"': " << ex.
what());
268 session_->delete_item(xpath.c_str());
269 }
catch (
const sysrepo_exception& ex) {
271 "sysrepo error deleting item at '" 272 << xpath <<
"': " << ex.
what());
279 return (
session_->get_items_iter(xpath.c_str()));
289 s_val =
session_->get_item_next(iter);
290 }
catch (
const sysrepo_exception&) {
297 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
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.
#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.
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.
static isc::data::ElementPtr value(sysrepo::S_Val s_val)
Translate basic value from YANG to JSON.
sysrepo::S_Session session_
The sysrepo session.