Kea 2.7.6
translator_logger.cc
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#include <config.h>
8
10#include <yang/yang_models.h>
11
12#include <sstream>
13
14using namespace std;
15using namespace isc::data;
16using namespace libyang;
17using namespace sysrepo;
18
19namespace isc {
20namespace yang {
21
22TranslatorLogger::TranslatorLogger(Session session, const string& model)
23 : Translator(session, model) {
24}
25
27TranslatorLogger::getLogger(DataNode const& data_node) {
28 try {
29 if ((model_ == KEA_DHCP4_SERVER) ||
30 (model_ == KEA_DHCP6_SERVER) ||
31 (model_ == KEA_DHCP_DDNS) ||
32 (model_ == KEA_CTRL_AGENT)) {
33 return (getLoggerKea(data_node));
34 }
35 } catch (Error const& ex) {
37 "getting logger: " << ex.what());
38 }
40 "getLogger not implemented for the model: " << model_);
41}
42
44TranslatorLogger::getLoggerKea(DataNode const& data_node) {
46
47 getMandatoryLeaf(result, data_node, "name");
48
49 checkAndGetLeaf(result, data_node, "debuglevel");
50 checkAndGetLeaf(result, data_node, "severity");
51
52 checkAndGetAndJsonifyLeaf(result, data_node, "user-context");
53
54 ConstElementPtr options = getOutputOptions(data_node);
55 if (options) {
56 result->set("output-options", options);
57 }
58
59 return (result->empty() ? ElementPtr() : result);
60}
61
63TranslatorLogger::getOutputOption(DataNode const& data_node) {
65
66 getMandatoryLeaf(result, data_node, "output");
67
68 checkAndGetLeaf(result, data_node, "flush");
69 checkAndGetLeaf(result, data_node, "maxsize");
70 checkAndGetLeaf(result, data_node, "maxver");
71 checkAndGetLeaf(result, data_node, "pattern");
72
73 return (result->empty() ? ElementPtr() : result);
74}
75
77TranslatorLogger::getOutputOptions(DataNode const& data_node) {
78 return getList(data_node, "output-option", *this,
80}
81
82void
83TranslatorLogger::setLogger(string const& xpath, ConstElementPtr elem) {
84 try {
85 if ((model_ == KEA_DHCP4_SERVER) ||
86 (model_ == KEA_DHCP6_SERVER) ||
87 (model_ == KEA_DHCP_DDNS) ||
88 (model_ == KEA_CTRL_AGENT)) {
89 setLoggerKea(xpath, elem);
90 } else {
92 "setLogger not implemented for the model: " << model_);
93 }
94 } catch (Error const& ex) {
96 "setting logger '" << elem->str()
97 << "' : " << ex.what());
98 }
99}
100
101void
103 // Set the list element. This is important in case we have no other elements except the key.
104 setItem(xpath, ElementPtr(), LeafBaseType::Unknown);
105
106 // Skip key "name" since it was set with the list element in the call above
107 // with the LeafBaseType::Unknown parameter.
108
109 checkAndSetLeaf(elem, xpath, "debuglevel", LeafBaseType::Uint8);
110 checkAndSetLeaf(elem, xpath, "severity", LeafBaseType::Enum);
111 checkAndSetUserContext(elem, xpath);
112
113 ConstElementPtr options = elem->get("output-options");
114 if (options && !options->empty()) {
115 setOutputOptions(xpath, options);
116 }
117}
118
119void
121 // Keys are set by setting the list itself.
122 setItem(xpath, ElementPtr(), LeafBaseType::Unknown);
123
124 checkAndSetLeaf(elem, xpath, "flush", LeafBaseType::Bool);
125 checkAndSetLeaf(elem, xpath, "maxsize", LeafBaseType::Uint32);
126 checkAndSetLeaf(elem, xpath, "maxver", LeafBaseType::Uint32);
127 checkAndSetLeaf(elem, xpath, "pattern", LeafBaseType::String);
128}
129
130void
132 for (size_t i = 0; i < elem->size(); ++i) {
133 ElementPtr option = elem->getNonConst(i);
134 if (!option->contains("output")) {
135 isc_throw(BadValue, "output-option without output: "
136 << option->str());
137 }
138 string output = option->get("output")->stringValue();
139 ostringstream key;
140 key << xpath << "/output-option[output='" << output << "']";
141 setOutputOption(key.str(), option);
142 }
143}
144
145TranslatorLoggers::TranslatorLoggers(Session session, const string& model)
146 : Translator(session, model),
147 TranslatorLogger(session, model) {
148}
149
151TranslatorLoggers::getLoggers(DataNode const& data_node) {
152 try {
153 if ((model_ == KEA_DHCP4_SERVER) ||
154 (model_ == KEA_DHCP6_SERVER) ||
155 (model_ == KEA_DHCP_DDNS) ||
156 (model_ == KEA_CTRL_AGENT)) {
157 return (getLoggersKea(data_node));
158 }
159 } catch (Error const& ex) {
161 "getting loggers: " << ex.what());
162 }
164 "getLoggers not implemented for the model: " << model_);
165}
166
169 try {
170 return getLoggers(findXPath(xpath));
171 } catch (NetconfError const&) {
172 return ElementPtr();
173 }
174}
175
177TranslatorLoggers::getLoggersKea(DataNode const& data_node) {
178 return getList<TranslatorLogger>(data_node, "logger", *this,
180}
181
182void
184 try {
185 if ((model_ == KEA_DHCP4_SERVER) ||
186 (model_ == KEA_DHCP6_SERVER) ||
187 (model_ == KEA_DHCP_DDNS) ||
188 (model_ == KEA_CTRL_AGENT)) {
189 setLoggersKea(xpath, elem);
190 } else {
192 "setLoggers not implemented for the model: " << model_);
193 }
194 } catch (Error const& ex) {
196 "setting loggers '" << elem->str()
197 << "' : " << ex.what());
198 }
199}
200
201void
203 for (size_t i = 0; i < elem->size(); ++i) {
204 ElementPtr logger = elem->getNonConst(i);
205 if (!logger->contains("name")) {
206 isc_throw(BadValue, "logger without name: " << logger->str());
207 }
208 string name = logger->get("name")->stringValue();
209 ostringstream key;
210 key << xpath << "/logger[name='" << name << "']";
211 setLogger(key.str(), logger);
212 }
213}
214
215} // namespace yang
216} // namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
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 createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
Logger translation between YANG and JSON.
isc::data::ElementPtr getOutputOptions(libyang::DataNode const &data_node)
Translate output options from YANG to JSON.
void setLogger(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set logger from JSON to YANG.
void setOutputOption(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set an output option from JSON to YANG.
void setLoggerKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setLogger for loggers.
isc::data::ElementPtr getLoggerKea(libyang::DataNode const &data_node)
getLogger JSON for loggers.
isc::data::ElementPtr getOutputOption(libyang::DataNode const &data_node)
Translate an output option from YANG to JSON.
TranslatorLogger(sysrepo::Session session, const std::string &model)
Constructor.
isc::data::ElementPtr getLogger(libyang::DataNode const &data_node)
Translate a logger from YANG to JSON.
void setOutputOptions(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set output options from JSON to YANG.
void setLoggers(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set loggers from JSON to YANG.
isc::data::ConstElementPtr getLoggersFromAbsoluteXpath(std::string const &xpath)
Translate loggers from YANG to JSON.
isc::data::ElementPtr getLoggersKea(libyang::DataNode const &data_node)
getLoggers JSON for loggers.
isc::data::ConstElementPtr getLoggers(libyang::DataNode const &data_node)
Translate loggers from YANG to JSON.
void setLoggersKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setLoggers for loggers.
TranslatorLoggers(sysrepo::Session session, const std::string &model)
Constructor.
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
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
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: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 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:427
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
#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.
Generic NETCONF error.