16using namespace sysrepo;
20string encode64(
const string& input) {
21 vector<uint8_t> binary;
22 binary.resize(input.size());
23 memmove(&binary[0], input.c_str(), binary.size());
27string decode64(
const string& input) {
28 vector<uint8_t> binary;
31 result.resize(binary.size());
32 memmove(&result[0], &binary[0], result.size());
42 : session_(session), model_(model) {
53 switch (s_val->type()) {
55 case SR_CONTAINER_PRESENCE_T:
60 return (Element::createList());
63 return (Element::create(
string(s_val->data()->get_string())));
66 return (Element::create(s_val->data()->get_bool() ?
true :
false));
69 return (Element::create(
static_cast<long long>(s_val->data()->get_uint8())));
72 return (Element::create(
static_cast<long long>(s_val->data()->get_uint16())));
75 return (Element::create(
static_cast<long long>(s_val->data()->get_uint32())));
78 return (Element::create(
static_cast<long long>(s_val->data()->get_int8())));
81 return (Element::create(
static_cast<long long>(s_val->data()->get_int16())));
84 return (Element::create(
static_cast<long long>(s_val->data()->get_int32())));
87 return (Element::create(s_val->data()->get_decimal64()));
89 case SR_IDENTITYREF_T:
90 return (Element::create(
string(s_val->data()->get_identityref())));
93 return (Element::create(
string(s_val->data()->get_enum())));
96 return (Element::create(decode64(s_val->data()->get_binary())));
100 "value called with unsupported type: " << s_val->type());
108 s_val =
session_->get_item(xpath.c_str());
109 }
catch (
const sysrepo_exception& ex) {
110 if (std::string(ex.what()).find(
"Item not found") != string::npos) {
115 <<
"': " << ex.
what());
120 return (
value(s_val));
131 for (
size_t i = 0; i < s_vals->val_cnt(); ++i) {
132 S_Val s_val = s_vals->val(i);
133 result->add(
value(s_val));
135 }
catch (
const sysrepo_exception& ex) {
137 << xpath <<
"': " << ex.
what());
150 case SR_CONTAINER_PRESENCE_T:
154 if (elem->getType() != Element::list) {
162 case SR_IDENTITYREF_T:
164 if (elem->getType() != Element::string) {
166 "value for a string called with not a string: "
169 s_val.reset(
new Val(elem->stringValue().c_str(), type));
173 if (elem->getType() != Element::boolean) {
175 "value for a boolean called with not a boolean: "
178 s_val.reset(
new Val(elem->boolValue(), type));
182 if (elem->getType() != Element::integer) {
184 "value for an integer called with not an integer: "
187 s_val.reset(
new Val(elem->intValue(), type));
191 if (elem->getType() != Element::integer) {
193 "value for an integer called with not an integer: "
196 s_val.reset(
new Val(elem->intValue(), type));
200 if (elem->getType() != Element::integer) {
202 "value for an integer called with not an integer: "
205 s_val.reset(
new Val(elem->intValue(), type));
209 if (elem->getType() != Element::integer) {
211 "value for an integer called with not an integer: "
214 s_val.reset(
new Val(elem->intValue(), type));
218 if (elem->getType() != Element::integer) {
220 "value for an integer called with not an integer: "
223 s_val.reset(
new Val(elem->intValue(), type));
227 if (elem->getType() != Element::integer) {
229 "value for an integer called with not an integer: "
232 s_val.reset(
new Val(elem->intValue(), type));
236 if (elem->getType() != Element::real) {
239 s_val.reset(
new Val(elem->doubleValue()));
243 if (elem->getType() != Element::string) {
245 "value for a base64 called with not a string: "
248 s_val.reset(
new Val(encode64(elem->stringValue()).c_str(), type));
253 "value called with unsupported type: " << type);
262 S_Val s_val =
value(elem, type);
264 session_->set_item(xpath.c_str(), s_val);
265 }
catch (
const sysrepo_exception& ex) {
267 "sysrepo error setting item '" << elem->str()
268 <<
"' at '" << xpath <<
"': " << ex.
what());
275 const std::string& xpath,
276 const std::string& name) {
279 storage->set(name, x);
286 sr_type_t
const type) {
289 setItem(xpath +
"/" + name, x, type);
296 session_->delete_item(xpath.c_str());
297 }
catch (
const sysrepo_exception& ex) {
298 if (std::string(ex.what()).find(
"Invalid argument") != string::npos) {
303 "sysrepo error deleting item at '"
304 << xpath <<
"': " << ex.
what());
311 return session_->get_items(xpath.c_str());
312 }
catch (sysrepo::sysrepo_exception
const& exception) {
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 isc::data::ElementPtr value(sysrepo::S_Val s_val)
Translate basic value from YANG to JSON.
isc::data::ElementPtr getItem(const std::string &xpath)
Get and translate basic value from YANG to JSON.
void checkAndGetLeaf(isc::data::ElementPtr &storage, const std::string &xpath, const std::string &name)
Retrieves an item and stores it in the specified storage.
TranslatorBasic(sysrepo::S_Session session, const std::string &model)
Constructor.
void delItem(const std::string &xpath)
Delete basic value from YANG.
void setItem(const std::string &xpath, isc::data::ConstElementPtr elem, sr_type_t type)
Translate and set basic value from JSON to YANG.
sysrepo::S_Vals getValuesFromItems(std::string const &xpath)
Get the values of all siblings at a certain xpath.
sysrepo::S_Session session_
The sysrepo session.
isc::data::ElementPtr getItems(const std::string &xpath)
Get and translate a list of basic values from YANG to JSON.
void checkAndSetLeaf(isc::data::ConstElementPtr const &from, std::string const &xpath, std::string const &name, sr_type_t const type)
Get an element from given ElementPtr node and set it in sysrepo at given xpath.
virtual ~TranslatorBasic()
Destructor.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
boost::shared_ptr< Element > ElementPtr
void decodeBase64(const std::string &input, std::vector< uint8_t > &result)
Decode a text encoded in the base64 format into the original data.
std::string encodeBase64(const std::vector< uint8_t > &binary)
Encode binary data in the base64 format.
Defines the logger used by the top-level component of kea-lfc.