Kea 3.3.3
translator.h
Go to the documentation of this file.
1// Copyright (C) 2018-2026 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef ISC_TRANSLATOR_H
8#define ISC_TRANSLATOR_H 1
9
10#include <cc/data.h>
11#include <yang/netconf_error.h>
12
13#ifdef WDEPRECATED_LITERAL_OPERATOR_FLAG_SUPPORTED
14#pragma GCC diagnostic push
15#pragma GCC diagnostic ignored "-Wdeprecated-literal-operator"
16#endif
17#include <sysrepo-cpp/Connection.hpp>
18#include <sysrepo-cpp/Enum.hpp>
19#include <sysrepo-cpp/Session.hpp>
20#ifdef WDEPRECATED_LITERAL_OPERATOR_FLAG_SUPPORTED
21#pragma GCC diagnostic pop
22#endif
23
24#include <unordered_map>
25
26namespace isc {
27namespace yang {
28
31public:
36 Translator(sysrepo::Session session, const std::string& model);
37
39 virtual ~Translator() = default;
40
50 template <typename T>
52 libyang::DataNode const& data_node,
53 std::string const& xpath,
54 T translate) const {
55 libyang::Set<libyang::DataNode> const& nodes(data_node.findXPath(xpath));
56 if (!nodes.empty()) {
57 isc::data::ElementPtr const& element(translate(nodes.front()));
58 if (element && !element->empty()) {
59 storage->set(xpath, element);
60 }
61 }
62 }
63
77 template <typename T>
79 libyang::DataNode const& data_node,
80 std::string const& key,
81 std::string const& xpath,
82 T translate) const {
83 libyang::Set<libyang::DataNode> const& nodes(data_node.findXPath(xpath));
84 if (!nodes.empty()) {
85 isc::data::ElementPtr const& element(translate(nodes.front()));
86 if (element && !element->empty()) {
87 storage->set(key, element);
88 }
89 }
90 }
91
99 libyang::DataNode const& data_node,
100 std::string const& name) const;
101
113 libyang::DataNode const& data_node,
114 std::string const& name,
115 std::string const& yang_name) const;
116
124 libyang::DataNode const& data_node,
125 const std::string& name) const;
126
136 std::string const& xpath,
137 std::string const& name,
138 libyang::LeafBaseType const type);
139
152 std::string const& xpath,
153 std::string const& name,
154 std::string const& yang_name,
155 libyang::LeafBaseType const type);
156
166 std::string const& xpath,
167 std::string const& name,
168 libyang::LeafBaseType const type);
169
176 std::string const& xpath);
177
186 std::string const& xpath,
187 std::string const& name);
188
192 void deleteItem(const std::string& xpath);
193
205 libyang::DataNode findXPath(std::string const& xpath) const;
206
217 template <typename functor_t>
218 void forAll(std::string const& xpath, functor_t f) const {
219 std::optional<libyang::DataNode> const& data_node(session_.getData(xpath));
220 if (!data_node) {
221 return;
222 }
223
224 for (libyang::DataNode const& sibling : data_node->siblings()) {
225 for (libyang::DataNode const& n : sibling.childrenDfs()) {
226 f(n);
227 }
228 }
229 }
230
242 std::optional<libyang::DataNode> getData(std::string const& xpath) const;
243
254 isc::data::ElementPtr getItem(libyang::DataNode const& data_node,
255 std::string const& xpath) const;
256
267 isc::data::ElementPtr getItemFromAbsoluteXpath(std::string const& xpath) const;
268
280 template <typename T>
281 isc::data::ElementPtr getList(libyang::DataNode const& data_node,
282 std::string const& xpath,
283 T& t,
284 isc::data::ElementPtr (T::*f)(libyang::DataNode const&)) const {
285 try {
286 libyang::Set<libyang::DataNode> const& nodes(data_node.findXPath(xpath));
287 if (nodes.empty()) {
288 return (isc::data::ElementPtr());
289 }
291 for (libyang::DataNode const& i : nodes) {
292 result->add((t.*f)(i));
293 }
294 return (result);
295 } catch (libyang::Error const& ex) {
296 isc_throw(NetconfError, "getting item: " << ex.what());
297 }
298 }
299
312 libyang::DataNode const& data_node,
313 std::string const& name) const;
314
326
329 libyang::DataNode const& data_node,
330 std::string const& name,
331 std::string const& yang_name) const;
332
338 bool schemaNodeExists(std::string const& xpath) const;
339
345 void setItem(const std::string& xpath,
347 libyang::LeafBaseType const type);
348
358 std::string const& xpath,
359 std::string const& name,
360 libyang::LeafBaseType const type);
361
371 std::string const& xpath,
372 std::string const& name,
373 std::string const& yang_name,
374 libyang::LeafBaseType const type);
375
389 static std::string quoteXPathValue(std::string const& value);
390
396 static isc::data::ElementPtr translateFromYang(std::optional<libyang::DataNode> data_node);
397
404 static std::optional<std::string> translateToYang(isc::data::ConstElementPtr const& elem,
405 libyang::LeafBaseType const type);
406
407protected:
414 static std::string decode64(std::string const& input);
415
421 static std::string encode64(std::string const& input);
422
425 std::unordered_map<libyang::LeafBaseType,
426 std::function<isc::data::ElementPtr const(std::string const&)>>;
427
432
436 std::unordered_map<libyang::LeafBaseType,
437 std::function<std::string const(std::string const&)>>;
438
444
446 sysrepo::Session session_;
447
449 std::string model_;
450}; // Translator
451
452} // namespace yang
453} // namespace isc
454
455#endif // ISC_TRANSLATOR_H
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.
Definition data.cc:350
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.
Definition translator.h:281
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.
Definition translator.cc:77
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...
Definition translator.h:435
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...
Definition translator.h:51
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.
Definition translator.cc:32
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...
Definition translator.cc:46
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.
Definition translator.h:424
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...
Definition translator.cc:56
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.
Definition translator.h:449
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.
Definition translator.cc:88
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,...
Definition translator.cc:67
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.
Definition translator.h:218
sysrepo::Session session_
The sysrepo session.
Definition translator.h:446
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...
Definition translator.h:78
Translator(sysrepo::Session session, const std::string &model)
Constructor.
Definition translator.cc:27
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
Defines the logger used by the top-level component of kea-lfc.
Generic NETCONF error.