Kea 2.2.0
translator.h
Go to the documentation of this file.
1// Copyright (C) 2018-2021 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/sysrepo_error.h>
12
13#include <sysrepo-cpp/Session.hpp>
14
15namespace isc {
16namespace yang {
17
20public:
25 TranslatorBasic(sysrepo::S_Session session, const std::string& model);
26
28 virtual ~TranslatorBasic();
29
37 static isc::data::ElementPtr value(sysrepo::S_Val s_val);
38
48 isc::data::ElementPtr getItem(const std::string& xpath);
49
55 isc::data::ElementPtr getItems(const std::string& xpath);
56
66 const std::string& xpath,
67 const std::string& name);
68
75 sysrepo::S_Vals getValuesFromItems(std::string const& xpath);
76
83 static sysrepo::S_Val value(isc::data::ConstElementPtr elem,
84 sr_type_t type);
85
91 void setItem(const std::string& xpath, isc::data::ConstElementPtr elem,
92 sr_type_t type);
93
94
104 std::string const& xpath,
105 std::string const& name,
106 sr_type_t const type);
107
108
112 void delItem(const std::string& xpath);
113
121 template <typename functor_t>
122 void forAll(std::string const& xpath, functor_t f) {
123 libyang::S_Data_Node data_node(session_->get_data(xpath.c_str()));
124 if (!data_node) {
125 return;
126 }
127
128 for (libyang::S_Data_Node& root : data_node->tree_for()) {
129 for (libyang::S_Data_Node const& n : root->tree_dfs()) {
130 f(n);
131 }
132 }
133 }
134
146 template <typename T>
147 isc::data::ElementPtr getList(std::string const& xpath,
148 T& t,
149 isc::data::ElementPtr (T::*f)(std::string const& xpath)) {
151 sysrepo::S_Vals values(getValuesFromItems(xpath));
152 if (values) {
153 for (size_t i(0); i < values->val_cnt(); ++i) {
154 isc::data::ElementPtr x((t.*f)(values->val(i)->xpath()));
155 if (x) {
156 if (!result) {
158 }
159 result->add(x);
160 }
161 }
162 }
163 return result;
164 }
165
166protected:
168 sysrepo::S_Session session_;
169
171 std::string model_;
172};
173
174} // namespace yang
175} // namespace isc
176
177#endif // ISC_TRANSLATOR_H
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition: data.cc:286
Between YANG and JSON translator class for basic values.
Definition: translator.h:19
static isc::data::ElementPtr value(sysrepo::S_Val s_val)
Translate basic value from YANG to JSON.
Definition: translator.cc:49
isc::data::ElementPtr getItem(const std::string &xpath)
Get and translate basic value from YANG to JSON.
Definition: translator.cc:105
std::string model_
The model.
Definition: translator.h:171
void forAll(std::string const &xpath, functor_t f)
Run a function for a node and all its children.
Definition: translator.h:122
void checkAndGetLeaf(isc::data::ElementPtr &storage, const std::string &xpath, const std::string &name)
Retrieves an item and stores it in the specified storage.
Definition: translator.cc:274
TranslatorBasic(sysrepo::S_Session session, const std::string &model)
Constructor.
Definition: translator.cc:41
isc::data::ElementPtr getList(std::string const &xpath, T &t, isc::data::ElementPtr(T::*f)(std::string const &xpath))
Retrieve a list as ElementPtr from sysrepo from a certain xpath.
Definition: translator.h:147
void delItem(const std::string &xpath)
Delete basic value from YANG.
Definition: translator.cc:294
void setItem(const std::string &xpath, isc::data::ConstElementPtr elem, sr_type_t type)
Translate and set basic value from JSON to YANG.
Definition: translator.cc:260
sysrepo::S_Vals getValuesFromItems(std::string const &xpath)
Get the values of all siblings at a certain xpath.
Definition: translator.cc:309
sysrepo::S_Session session_
The sysrepo session.
Definition: translator.h:168
isc::data::ElementPtr getItems(const std::string &xpath)
Get and translate a list of basic values from YANG to JSON.
Definition: translator.cc:124
void checkAndSetLeaf(isc::data::ConstElementPtr const &from, std::string const &xpath, std::string const &name, sr_type_t const type)
Get an element from given ElementPtr node and set it in sysrepo at given xpath.
Definition: translator.cc:283
virtual ~TranslatorBasic()
Destructor.
Definition: translator.cc:45
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
Defines the logger used by the top-level component of kea-lfc.