13#include <sysrepo-cpp/utils/exception.hpp>
21using namespace libyang;
22using namespace sysrepo;
33 bool const has_single(value.find(
'\'') != string::npos);
34 bool const has_double(value.find(
'"') != string::npos);
35 if (has_single && has_double) {
37 "quotes and cannot be safely quoted: " << value);
40 return (
"\"" + value +
"\"");
42 return (
"'" + value +
"'");
47 DataNode
const& data_node,
48 string const& name)
const {
51 storage->set(name, x);
57 DataNode
const& data_node,
59 string const& yang_name)
const {
62 storage->set(name, x);
68 DataNode
const& data_node,
69 string const& name)
const {
80 LeafBaseType
const type) {
83 setItem(xpath +
"/" + name, x, type);
91 string const& yang_name,
92 LeafBaseType
const type) {
95 setItem(xpath +
"/" + yang_name, x, type);
103 LeafBaseType
const type) {
105 if (leaf_list && !leaf_list->empty()) {
106 for (
ElementPtr const& leaf : leaf_list->listValue()) {
107 setItem(xpath +
"/" + name, leaf, type);
114 string const& xpath) {
118 LeafBaseType::String);
125 string const& name) {
129 setItem(xpath +
"/" + name, json, LeafBaseType::String);
139 }
catch (sysrepo::Error
const& ex) {
147 optional<DataNode>
const& data_node(
getData(xpath));
151 Set<DataNode> at_path(data_node->findXPath(xpath));
152 if (at_path.empty()) {
155 return at_path.front();
160 optional<DataNode> data_node;
162 data_node =
session_.getData(xpath);
163 }
catch (sysrepo::Error
const& ex) {
172 string const& xpath)
const {
174 Set<DataNode>
const& nodes(data_node.findXPath(xpath));
178 DataNode
const& front(nodes.front());
179 NodeType
const node_type(front.schema().nodeType());
182 if (node_type == NodeType::Leaf) {
184 }
else if (node_type == NodeType::Leaflist) {
186 for (DataNode
const& i : nodes) {
192 << node_type <<
" not supported, xpath is '" << xpath
196 }
catch (sysrepo::Error
const& ex) {
203 optional<DataNode>
const& data_node(
getData(xpath));
207 return getItem(*data_node, xpath);
212 DataNode
const& data_node,
213 string const& name)
const {
218 storage->set(name, x);
223 DataNode
const& data_node,
225 string const& yang_name)
const {
230 storage->set(name, x);
235 Context
const& context(
session_.getContext());
237 context.findPath(xpath);
238 }
catch (libyang::Error
const& ex) {
247 LeafBaseType
const type) {
251 }
catch (sysrepo::Error
const& ex) {
253 <<
"' at '" << xpath <<
"': " << ex.what());
262 LeafBaseType
const type) {
267 setItem(xpath +
"/" + name, x, type);
274 string const& yang_name,
275 LeafBaseType
const type) {
280 setItem(xpath +
"/" + yang_name, x, type);
285 NodeType
const node_type(data_node->schema().nodeType());
286 if (node_type == NodeType::Leaf || node_type == NodeType::Leaflist) {
287 DataNodeTerm
const& leaf(data_node->asTerm());
289 if (node_type == NodeType::Leaf) {
290 type = leaf.schema().asLeaf().valueType().base();
292 type = leaf.schema().asLeafList().valueType().base();
296 return deserializer.at(type)(string(leaf.valueStr()));
303 libyang::LeafBaseType
const type) {
304 string string_representation;
316 string_representation = element->stringValue();
319 string_representation = element->str();
323 return serializer.at(type)(string_representation);
328 vector<uint8_t> binary;
331 result.resize(binary.size());
332 if (!result.empty()) {
333 memcpy(&result[0], &binary[0], result.size());
340 vector<uint8_t> binary;
341 binary.resize(input.size());
342 if (!binary.empty()) {
343 memcpy(&binary[0], input.c_str(), binary.size());
352 result.emplace(LeafBaseType::Binary, [](
string const& value) ->
ElementPtr const {
356 for (LeafBaseType
const& i :
357 {LeafBaseType::Bool, LeafBaseType::Dec64, LeafBaseType::Int8, LeafBaseType::Int16,
358 LeafBaseType::Int32, LeafBaseType::Int64, LeafBaseType::Uint8, LeafBaseType::Uint16,
359 LeafBaseType::Uint32, LeafBaseType::Uint64}) {
360 result.emplace(i, [](
string const& value) ->
ElementPtr const {
366 for (LeafBaseType
const& i :
367 {LeafBaseType::Bits, LeafBaseType::Empty, LeafBaseType::Enum, LeafBaseType::IdentityRef,
368 LeafBaseType::InstanceIdentifier, LeafBaseType::Leafref, LeafBaseType::String,
369 LeafBaseType::Union}) {
370 result.emplace(i, [](
string const& value) ->
ElementPtr const {
382 result.emplace(LeafBaseType::Binary, [](
string const& value) ->
string const {
387 for (LeafBaseType
const& i :
388 {LeafBaseType::Bits, LeafBaseType::Bool, LeafBaseType::Dec64, LeafBaseType::Empty,
389 LeafBaseType::Enum, LeafBaseType::IdentityRef, LeafBaseType::InstanceIdentifier,
390 LeafBaseType::Int8, LeafBaseType::Int16, LeafBaseType::Int32, LeafBaseType::Int64,
391 LeafBaseType::Leafref, LeafBaseType::String, LeafBaseType::Uint8, LeafBaseType::Uint16,
392 LeafBaseType::Uint32, LeafBaseType::Uint64, LeafBaseType::Union, LeafBaseType::Unknown}) {
393 result.emplace(i, [](
string const& value) ->
string const {
static ElementPtr create(const Position &pos=ZERO_POSITION())
Create a NullElement.
static ElementPtr fromJSON(const std::string &in, bool preproc=false)
These functions will parse the given string (JSON) representation of a compound element.
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
A generic exception that is thrown when a function is not implemented.
static isc::data::ConstElementPtr getContext(isc::data::ConstElementPtr parent)
Get user context.
void getMandatoryDivergingLeaf(isc::data::ElementPtr &storage, libyang::DataNode const &data_node, std::string const &name, std::string const &yang_name) const
Retrieves a child YANG data node identified by one name from the given parent YANG container node and...
void setMandatoryLeaf(isc::data::ConstElementPtr const &from, std::string const &xpath, std::string const &name, libyang::LeafBaseType const type)
Get an element from given ElementPtr node and set it in sysrepo at given xpath.
isc::data::ElementPtr getItemFromAbsoluteXpath(std::string const &xpath) const
Translate a basic value from YANG to JSON for a given absolute xpath.
void checkAndSetLeaf(isc::data::ConstElementPtr const &from, std::string const &xpath, std::string const &name, libyang::LeafBaseType const type)
Get an element from given ElementPtr node and set it in sysrepo at given xpath.
static std::string decode64(std::string const &input)
Decode a YANG element of binary type to a string that can be stored in an Element::string JSON.
isc::data::ElementPtr getItem(libyang::DataNode const &data_node, std::string const &xpath) const
Translate a basic value from YANG to JSON for a given xpath that is relative to the given source node...
std::unordered_map< libyang::LeafBaseType, std::function< std::string const (std::string const &)> > Serializer
Maps YANG types to functions that transform the string representation of an Element into a string tha...
static isc::data::ElementPtr translateFromYang(std::optional< libyang::DataNode > data_node)
Translate basic value from the given YANG data node to JSON element.
void setMandatoryDivergingLeaf(isc::data::ConstElementPtr const &from, std::string const &xpath, std::string const &name, std::string const &yang_name, libyang::LeafBaseType const type)
Get an element from given ElementPtr node and set it in sysrepo at given xpath.
static std::string quoteXPathValue(std::string const &value)
Quote a value for use in an XPath predicate literal.
libyang::DataNode findXPath(std::string const &xpath) const
Retrieves a YANG data node by xpath.
void getMandatoryLeaf(isc::data::ElementPtr &storage, libyang::DataNode const &data_node, std::string const &name) const
Retrieves a child YANG data node identified by name from the given parent YANG container node and sto...
void checkAndGetLeaf(isc::data::ElementPtr &storage, libyang::DataNode const &data_node, std::string const &name) const
Retrieves a child YANG data node identified by name from the given parent YANG container node and sto...
void checkAndSetUserContext(isc::data::ConstElementPtr const &from, std::string const &xpath)
Get an element from given ElementPtr node and set it in sysrepo at given xpath.
static Serializer initializeSerializer()
Initializes the serializer which is used to translate the string value of an Element to a string that...
std::unordered_map< libyang::LeafBaseType, std::function< isc::data::ElementPtr const (std::string const &)> > Deserializer
Maps YANG types to functions that transform a YANG type into an ElementPtr.
void deleteItem(const std::string &xpath)
Delete basic value from YANG.
void checkAndGetDivergingLeaf(isc::data::ElementPtr &storage, libyang::DataNode const &data_node, std::string const &name, std::string const &yang_name) const
Retrieves a child YANG data node identified by name from the given parent YANG container node and sto...
void setItem(const std::string &xpath, isc::data::ConstElementPtr const elem, libyang::LeafBaseType const type)
Translate and set basic value from JSON to YANG.
std::string model_
The model.
void checkAndSetDivergingLeaf(isc::data::ConstElementPtr const &from, std::string const &xpath, std::string const &name, std::string const &yang_name, libyang::LeafBaseType const type)
Get an element from given ElementPtr node and set it in sysrepo at given xpath.
bool schemaNodeExists(std::string const &xpath) const
Checks whether a YANG node exists in the schema.
void checkAndGetAndJsonifyLeaf(isc::data::ElementPtr &storage, libyang::DataNode const &data_node, const std::string &name) const
Retrieves a child YANG data node identified by name from the given parent YANG container node,...
void checkAndSetLeafList(isc::data::ConstElementPtr const &from, std::string const &xpath, std::string const &name, libyang::LeafBaseType const type)
Get an element from given ElementPtr node and set it in sysrepo at given xpath as a leaf-list.
sysrepo::Session session_
The sysrepo session.
std::optional< libyang::DataNode > getData(std::string const &xpath) const
Get a YANG data node found at the given absolute xpath.
void checkAndStringifyAndSetLeaf(isc::data::ConstElementPtr const &from, std::string const &xpath, std::string const &name)
Get an element from given ElementPtr node and set it in sysrepo at given xpath.
static std::optional< std::string > translateToYang(isc::data::ConstElementPtr const &elem, libyang::LeafBaseType const type)
Translate basic value from JSON to YANG.
static Deserializer initializeDeserializer()
Initializes the deserializer which is used to translate a YANG node to an ElementPtr.
static std::string encode64(std::string const &input)
Encode a string such that it can be stored in a YANG element of binary type.
Translator(sysrepo::Session session, const std::string &model)
Constructor.
#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
string encodeBase64(const vector< uint8_t > &binary)
Encode binary data in the base64 format.
void decodeBase64(const std::string &encoded_str, std::vector< uint8_t > &output)
Decode a base64 encoded string into binary data.
Defines the logger used by the top-level component of kea-lfc.