13#include <sysrepo-cpp/utils/exception.hpp>
21using namespace libyang;
22using namespace sysrepo;
28 : session_(session), model_(model) {
33 DataNode
const& data_node,
34 string const& name)
const {
37 storage->set(name, x);
43 DataNode
const& data_node,
45 string const& yang_name)
const {
48 storage->set(name, x);
54 DataNode
const& data_node,
55 string const& name)
const {
66 LeafBaseType
const type) {
69 setItem(xpath +
"/" + name, x, type);
77 string const& yang_name,
78 LeafBaseType
const type) {
81 setItem(xpath +
"/" + yang_name, x, type);
89 LeafBaseType
const type) {
91 if (leaf_list && !leaf_list->empty()) {
92 for (
ElementPtr const& leaf : leaf_list->listValue()) {
93 setItem(xpath +
"/" + name, leaf, type);
100 string const& xpath) {
104 LeafBaseType::String);
111 string const& name) {
115 setItem(xpath +
"/" + name, json, LeafBaseType::String);
125 }
catch (sysrepo::Error
const& ex) {
133 optional<DataNode>
const& data_node(
getData(xpath));
137 Set<DataNode> at_path(data_node->findXPath(xpath));
138 if (at_path.empty()) {
141 return at_path.front();
146 optional<DataNode> data_node;
148 data_node =
session_.getData(xpath);
149 }
catch (sysrepo::Error
const& ex) {
158 string const& xpath)
const {
160 Set<DataNode>
const& nodes(data_node.findXPath(xpath));
164 DataNode
const& front(nodes.front());
165 NodeType
const node_type(front.schema().nodeType());
168 if (node_type == NodeType::Leaf) {
170 }
else if (node_type == NodeType::Leaflist) {
172 for (DataNode
const& i : nodes) {
178 <<
int(node_type) <<
" not supported, xpath is '" << xpath
182 }
catch (sysrepo::Error
const& ex) {
189 optional<DataNode>
const& data_node(
getData(xpath));
193 return getItem(*data_node, xpath);
198 DataNode
const& data_node,
199 string const& name)
const {
204 storage->set(name, x);
209 DataNode
const& data_node,
211 string const& yang_name)
const {
216 storage->set(name, x);
221 Context
const& context(
session_.getContext());
223 context.findPath(xpath);
224 }
catch (libyang::Error
const& ex) {
233 LeafBaseType
const type) {
237 }
catch (sysrepo::Error
const& ex) {
239 <<
"' at '" << xpath <<
"': " << ex.
what());
248 LeafBaseType
const type) {
253 setItem(xpath +
"/" + name, x, type);
260 string const& yang_name,
261 LeafBaseType
const type) {
266 setItem(xpath +
"/" + yang_name, x, type);
271 NodeType
const node_type(data_node->schema().nodeType());
272 if (node_type == NodeType::Leaf || node_type == NodeType::Leaflist) {
273 DataNodeTerm
const& leaf(data_node->asTerm());
275 if (node_type == NodeType::Leaf) {
276 type = leaf.schema().asLeaf().valueType().base();
278 type = leaf.schema().asLeafList().valueType().base();
282 return deserializer.at(type)(string(leaf.valueStr()));
289 libyang::LeafBaseType
const type) {
290 string string_representation;
302 string_representation = element->stringValue();
305 string_representation = element->str();
309 return serializer.at(type)(string_representation);
314 vector<uint8_t> binary;
317 result.resize(binary.size());
318 memcpy(&result[0], &binary[0], result.size());
324 vector<uint8_t> binary;
325 binary.resize(input.size());
326 memcpy(&binary[0], input.c_str(), binary.size());
334 result.emplace(LeafBaseType::Binary, [](
string const& value) ->
ElementPtr const {
338 for (LeafBaseType
const& i :
339 {LeafBaseType::Bool, LeafBaseType::Dec64, LeafBaseType::Int8, LeafBaseType::Int16,
340 LeafBaseType::Int32, LeafBaseType::Int64, LeafBaseType::Uint8, LeafBaseType::Uint16,
341 LeafBaseType::Uint32, LeafBaseType::Uint64}) {
342 result.emplace(i, [](
string const& value) ->
ElementPtr const {
348 for (LeafBaseType
const& i :
349 {LeafBaseType::Bits, LeafBaseType::Empty, LeafBaseType::Enum, LeafBaseType::IdentityRef,
350 LeafBaseType::InstanceIdentifier, LeafBaseType::Leafref, LeafBaseType::String,
351 LeafBaseType::Union}) {
352 result.emplace(i, [](
string const& value) ->
ElementPtr const {
364 result.emplace(LeafBaseType::Binary, [](
string const& value) ->
string const {
369 for (LeafBaseType
const& i :
370 {LeafBaseType::Bits, LeafBaseType::Bool, LeafBaseType::Dec64, LeafBaseType::Empty,
371 LeafBaseType::Enum, LeafBaseType::IdentityRef, LeafBaseType::InstanceIdentifier,
372 LeafBaseType::Int8, LeafBaseType::Int16, LeafBaseType::Int32, LeafBaseType::Int64,
373 LeafBaseType::Leafref, LeafBaseType::String, LeafBaseType::Uint8, LeafBaseType::Uint16,
374 LeafBaseType::Uint32, LeafBaseType::Uint64, LeafBaseType::Union, LeafBaseType::Unknown}) {
375 result.emplace(i, [](
string const& value) ->
string const {
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 ElementPtr create(const Position &pos=ZERO_POSITION())
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.
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...
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.
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.
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.