7#ifndef ISC_TRANSLATOR_H
8#define ISC_TRANSLATOR_H 1
13#include <sysrepo-cpp/Connection.hpp>
14#include <sysrepo-cpp/Enum.hpp>
15#include <sysrepo-cpp/Session.hpp>
17#include <unordered_map>
29 Translator(sysrepo::Session session,
const std::string& model);
45 libyang::DataNode
const& data_node,
46 std::string
const& xpath,
48 libyang::Set<libyang::DataNode>
const& nodes(data_node.findXPath(xpath));
51 if (element && !element->empty()) {
52 storage->set(xpath, element);
72 libyang::DataNode
const& data_node,
73 std::string
const& key,
74 std::string
const& xpath,
76 libyang::Set<libyang::DataNode>
const& nodes(data_node.findXPath(xpath));
79 if (element && !element->empty()) {
80 storage->set(key, element);
92 libyang::DataNode
const& data_node,
93 std::string
const& name)
const;
106 libyang::DataNode
const& data_node,
107 std::string
const& name,
108 std::string
const& yang_name)
const;
117 libyang::DataNode
const& data_node,
118 const std::string& name)
const;
129 std::string
const& xpath,
130 std::string
const& name,
131 libyang::LeafBaseType
const type);
145 std::string
const& xpath,
146 std::string
const& name,
147 std::string
const& yang_name,
148 libyang::LeafBaseType
const type);
159 std::string
const& xpath,
160 std::string
const& name,
161 libyang::LeafBaseType
const type);
169 std::string
const& xpath);
179 std::string
const& xpath,
180 std::string
const& name);
198 libyang::DataNode
findXPath(std::string
const& xpath)
const;
210 template <
typename functor_t>
211 void forAll(std::string
const& xpath, functor_t f)
const {
212 std::optional<libyang::DataNode>
const& data_node(
session_.getData(xpath));
217 for (libyang::DataNode
const& sibling : data_node->siblings()) {
218 for (libyang::DataNode
const& n : sibling.childrenDfs()) {
235 std::optional<libyang::DataNode>
getData(std::string
const& xpath)
const;
248 std::string
const& xpath)
const;
273 template <
typename T>
275 std::string
const& xpath,
279 libyang::Set<libyang::DataNode>
const& nodes(data_node.findXPath(xpath));
284 for (libyang::DataNode
const& i : nodes) {
285 result->add((t.*f)(i));
288 }
catch (libyang::Error
const& ex) {
305 libyang::DataNode
const& data_node,
306 std::string
const& name)
const;
322 libyang::DataNode
const& data_node,
323 std::string
const& name,
324 std::string
const& yang_name)
const;
338 void setItem(
const std::string& xpath,
340 libyang::LeafBaseType
const type);
351 std::string
const& xpath,
352 std::string
const& name,
353 libyang::LeafBaseType
const type);
364 std::string
const& xpath,
365 std::string
const& name,
366 std::string
const& yang_name,
367 libyang::LeafBaseType
const type);
383 libyang::LeafBaseType
const type);
392 static std::string
decode64(std::string
const& input);
399 static std::string
encode64(std::string
const& input);
403 std::unordered_map<libyang::LeafBaseType,
414 std::unordered_map<libyang::LeafBaseType,
415 std::function<std::string
const(std::string
const&)>>;
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Between YANG and JSON translator class for basic values.
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...
isc::data::ElementPtr getList(libyang::DataNode const &data_node, std::string const &xpath, T &t, isc::data::ElementPtr(T::*f)(libyang::DataNode const &)) const
Retrieve a list as ElementPtr from sysrepo from a certain xpath.
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...
void checkAndGet(isc::data::ElementPtr const &storage, libyang::DataNode const &data_node, std::string const &xpath, T translate) const
Calls {translate} for the element found at {xpath} relative to {data_node} and sets the result in {st...
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.
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...
void deleteItem(const std::string &xpath)
Delete basic value from YANG.
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...
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.
virtual ~Translator()=default
Destructor.
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.
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.
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.
void forAll(std::string const &xpath, functor_t f) const
Run a function for a node and all its children.
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.
void checkAndGetDiverging(isc::data::ElementPtr const &storage, libyang::DataNode const &data_node, std::string const &key, std::string const &xpath, T translate) const
Calls {translate} for the element found at {xpath} relative to {data_node} and sets the result in {st...
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
Defines the logger used by the top-level component of kea-lfc.