Kea 2.7.4
translator.h
Go to the documentation of this file.
1// Copyright (C) 2018-2024 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#include <sysrepo-cpp/Connection.hpp>
14#include <sysrepo-cpp/Enum.hpp>
15#include <sysrepo-cpp/Session.hpp>
16
17#include <unordered_map>
18
19namespace isc {
20namespace yang {
21
24public:
29 Translator(sysrepo::Session session, const std::string& model);
30
32 virtual ~Translator() = default;
33
43 template <typename T>
45 libyang::DataNode const& data_node,
46 std::string const& xpath,
47 T translate) const {
48 libyang::Set<libyang::DataNode> const& nodes(data_node.findXPath(xpath));
49 if (!nodes.empty()) {
50 isc::data::ElementPtr const& element(translate(nodes.front()));
51 if (element && !element->empty()) {
52 storage->set(xpath, element);
53 }
54 }
55 }
56
70 template <typename T>
72 libyang::DataNode const& data_node,
73 std::string const& key,
74 std::string const& xpath,
75 T translate) const {
76 libyang::Set<libyang::DataNode> const& nodes(data_node.findXPath(xpath));
77 if (!nodes.empty()) {
78 isc::data::ElementPtr const& element(translate(nodes.front()));
79 if (element && !element->empty()) {
80 storage->set(key, element);
81 }
82 }
83 }
84
92 libyang::DataNode const& data_node,
93 std::string const& name) const;
94
106 libyang::DataNode const& data_node,
107 std::string const& name,
108 std::string const& yang_name) const;
109
117 libyang::DataNode const& data_node,
118 const std::string& name) const;
119
129 std::string const& xpath,
130 std::string const& name,
131 libyang::LeafBaseType const type);
132
145 std::string const& xpath,
146 std::string const& name,
147 std::string const& yang_name,
148 libyang::LeafBaseType const type);
149
159 std::string const& xpath,
160 std::string const& name,
161 libyang::LeafBaseType const type);
162
169 std::string const& xpath);
170
179 std::string const& xpath,
180 std::string const& name);
181
185 void deleteItem(const std::string& xpath);
186
198 libyang::DataNode findXPath(std::string const& xpath) const;
199
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));
213 if (!data_node) {
214 return;
215 }
216
217 for (libyang::DataNode const& sibling : data_node->siblings()) {
218 for (libyang::DataNode const& n : sibling.childrenDfs()) {
219 f(n);
220 }
221 }
222 }
223
235 std::optional<libyang::DataNode> getData(std::string const& xpath) const;
236
247 isc::data::ElementPtr getItem(libyang::DataNode const& data_node,
248 std::string const& xpath) const;
249
260 isc::data::ElementPtr getItemFromAbsoluteXpath(std::string const& xpath) const;
261
273 template <typename T>
274 isc::data::ElementPtr getList(libyang::DataNode const& data_node,
275 std::string const& xpath,
276 T& t,
277 isc::data::ElementPtr (T::*f)(libyang::DataNode const&)) const {
278 try {
279 libyang::Set<libyang::DataNode> const& nodes(data_node.findXPath(xpath));
280 if (nodes.empty()) {
281 return (isc::data::ElementPtr());
282 }
284 for (libyang::DataNode const& i : nodes) {
285 result->add((t.*f)(i));
286 }
287 return result;
288 } catch (libyang::Error const& ex) {
289 isc_throw(NetconfError, "getting item: " << ex.what());
290 }
291 }
292
304 void getMandatoryLeaf(isc::data::ElementPtr& storage,
305 libyang::DataNode const& data_node,
306 std::string const& name) const;
307
319
321 void getMandatoryDivergingLeaf(isc::data::ElementPtr& storage,
322 libyang::DataNode const& data_node,
323 std::string const& name,
324 std::string const& yang_name) const;
325
331 bool schemaNodeExists(std::string const& xpath) const;
332
338 void setItem(const std::string& xpath,
340 libyang::LeafBaseType const type);
341
350 void setMandatoryLeaf(isc::data::ConstElementPtr const& from,
351 std::string const& xpath,
352 std::string const& name,
353 libyang::LeafBaseType const type);
354
363 void setMandatoryDivergingLeaf(isc::data::ConstElementPtr const& from,
364 std::string const& xpath,
365 std::string const& name,
366 std::string const& yang_name,
367 libyang::LeafBaseType const type);
368
374 static isc::data::ElementPtr translateFromYang(std::optional<libyang::DataNode> data_node);
375
382 static std::optional<std::string> translateToYang(isc::data::ConstElementPtr const& elem,
383 libyang::LeafBaseType const type);
384
385protected:
392 static std::string decode64(std::string const& input);
393
399 static std::string encode64(std::string const& input);
400
403 std::unordered_map<libyang::LeafBaseType,
404 std::function<isc::data::ElementPtr const(std::string const&)>>;
405
409 static Deserializer initializeDeserializer();
410
414 std::unordered_map<libyang::LeafBaseType,
415 std::function<std::string const(std::string const&)>>;
416
421 static Serializer initializeSerializer();
422
424 sysrepo::Session session_;
425
427 std::string model_;
428}; // Translator
429
430} // namespace yang
431} // namespace isc
432
433#endif // ISC_TRANSLATOR_H
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition data.cc:299
Between YANG and JSON translator class for basic values.
Definition translator.h:23
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:274
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:63
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...
Definition translator.h:44
libyang::DataNode findXPath(std::string const &xpath) const
Retrieves a YANG data node by xpath.
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:32
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.
Definition translator.cc:99
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...
Definition translator.h:413
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:42
std::string model_
The model.
Definition translator.h:427
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:74
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:402
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:53
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.
Definition translator.cc:86
void forAll(std::string const &xpath, functor_t f) const
Run a function for a node and all its children.
Definition translator.h:211
sysrepo::Session session_
The sysrepo session.
Definition translator.h:424
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.
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:71
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:29
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
Defines the logger used by the top-level component of kea-lfc.