Kea: translator_logger.cc Source File

| Kea 3.0.0

translator_logger.cc
Go to the documentation of this file.
1// Copyright (C) 2018-2025 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
9#include <yang/translator_logger.h>
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
26 ElementPtr
27TranslatorLogger::getLogger(DataNode const& data_node) {
28 try {
29 if ((model_ == KEA_DHCP4_SERVER) ||
30 (model_ == KEA_DHCP6_SERVER)) {
31 return (getLoggerKea(data_node));
32 }
33 } catch (Error const& ex) {
34 isc_throw(NetconfError,
35 "getting logger: " << ex.what());
36 }
37 isc_throw(NotImplemented,
38 "getLogger not implemented for the model: " << model_);
39}
40
41 ElementPtr
42TranslatorLogger::getLoggerKea(DataNode const& data_node) {
43 ElementPtr result = Element::createMap();
44
45 getMandatoryLeaf(result, data_node, "name");
46
47 checkAndGetLeaf(result, data_node, "debuglevel");
48 checkAndGetLeaf(result, data_node, "severity");
49
50 checkAndGetAndJsonifyLeaf(result, data_node, "user-context");
51
52 ConstElementPtr options = getOutputOptions(data_node);
53 if (options) {
54 result->set("output-options", options);
55 }
56
57 return (result->empty() ? ElementPtr() : result);
58}
59
60 ElementPtr
61TranslatorLogger::getOutputOption(DataNode const& data_node) {
62 ElementPtr result = Element::createMap();
63
64 getMandatoryLeaf(result, data_node, "output");
65
66 checkAndGetLeaf(result, data_node, "flush");
67 checkAndGetLeaf(result, data_node, "maxsize");
68 checkAndGetLeaf(result, data_node, "maxver");
69 checkAndGetLeaf(result, data_node, "pattern");
70
71 return (result->empty() ? ElementPtr() : result);
72}
73
74 ElementPtr
75TranslatorLogger::getOutputOptions(DataNode const& data_node) {
76 return getList(data_node, "output-option", *this,
77 &TranslatorLogger::getOutputOption);
78}
79
80void
81TranslatorLogger::setLogger(string const& xpath, ConstElementPtr elem) {
82 try {
83 if ((model_ == KEA_DHCP4_SERVER) ||
84 (model_ == KEA_DHCP6_SERVER)) {
85 setLoggerKea(xpath, elem);
86 } else {
87 isc_throw(NotImplemented,
88 "setLogger not implemented for the model: " << model_);
89 }
90 } catch (Error const& ex) {
91 isc_throw(NetconfError,
92 "setting logger '" << elem->str()
93 << "' : " << ex.what());
94 }
95}
96
97void
98TranslatorLogger::setLoggerKea(string const& xpath, ConstElementPtr elem) {
99 // Set the list element. This is important in case we have no other elements except the key.
100 setItem(xpath, ElementPtr(), LeafBaseType::Unknown);
101
102 // Skip key "name" since it was set with the list element in the call above
103 // with the LeafBaseType::Unknown parameter.
104
105 checkAndSetLeaf(elem, xpath, "debuglevel", LeafBaseType::Uint8);
106 checkAndSetLeaf(elem, xpath, "severity", LeafBaseType::Enum);
107 checkAndSetUserContext(elem, xpath);
108
109 ConstElementPtr options = elem->get("output-options");
110 if (options && !options->empty()) {
111 setOutputOptions(xpath, options);
112 }
113}
114
115void
116TranslatorLogger::setOutputOption(string const& xpath, ConstElementPtr elem) {
117 // Keys are set by setting the list itself.
118 setItem(xpath, ElementPtr(), LeafBaseType::Unknown);
119
120 checkAndSetLeaf(elem, xpath, "flush", LeafBaseType::Bool);
121 checkAndSetLeaf(elem, xpath, "maxsize", LeafBaseType::Uint32);
122 checkAndSetLeaf(elem, xpath, "maxver", LeafBaseType::Uint32);
123 checkAndSetLeaf(elem, xpath, "pattern", LeafBaseType::String);
124}
125
126void
127TranslatorLogger::setOutputOptions(string const& xpath, ConstElementPtr elem) {
128 for (size_t i = 0; i < elem->size(); ++i) {
129 ElementPtr option = elem->getNonConst(i);
130 if (!option->contains("output")) {
131 isc_throw(BadValue, "output-option without output: "
132 << option->str());
133 }
134 string output = option->get("output")->stringValue();
135 ostringstream key;
136 key << xpath << "/output-option[output='" << output << "']";
137 setOutputOption(key.str(), option);
138 }
139}
140
141TranslatorLoggers::TranslatorLoggers(Session session, const string& model)
142 : Translator(session, model),
143 TranslatorLogger(session, model) {
144}
145
146 ConstElementPtr
147TranslatorLoggers::getLoggers(DataNode const& data_node) {
148 try {
149 if ((model_ == KEA_DHCP4_SERVER) ||
150 (model_ == KEA_DHCP6_SERVER)) {
151 return (getLoggersKea(data_node));
152 }
153 } catch (Error const& ex) {
154 isc_throw(NetconfError,
155 "getting loggers: " << ex.what());
156 }
157 isc_throw(NotImplemented,
158 "getLoggers not implemented for the model: " << model_);
159}
160
161 ConstElementPtr
162TranslatorLoggers::getLoggersFromAbsoluteXpath(string const& xpath) {
163 try {
164 return getLoggers(findXPath(xpath));
165 } catch (NetconfError const&) {
166 return ElementPtr();
167 }
168}
169
170 ElementPtr
171TranslatorLoggers::getLoggersKea(DataNode const& data_node) {
172 return getList<TranslatorLogger>(data_node, "logger", *this,
173 &TranslatorLogger::getLogger);
174}
175
176void
177TranslatorLoggers::setLoggers(string const& xpath, ConstElementPtr elem) {
178 try {
179 if ((model_ == KEA_DHCP4_SERVER) ||
180 (model_ == KEA_DHCP6_SERVER)) {
181 setLoggersKea(xpath, elem);
182 } else {
183 isc_throw(NotImplemented,
184 "setLoggers not implemented for the model: " << model_);
185 }
186 } catch (Error const& ex) {
187 isc_throw(NetconfError,
188 "setting loggers '" << elem->str()
189 << "' : " << ex.what());
190 }
191}
192
193void
194TranslatorLoggers::setLoggersKea(string const& xpath, ConstElementPtr elem) {
195 for (size_t i = 0; i < elem->size(); ++i) {
196 ElementPtr logger = elem->getNonConst(i);
197 if (!logger->contains("name")) {
198 isc_throw(BadValue, "logger without name: " << logger->str());
199 }
200 string name = logger->get("name")->stringValue();
201 ostringstream key;
202 key << xpath << "/logger[name='" << name << "']";
203 setLogger(key.str(), logger);
204 }
205}
206
207} // namespace yang
208} // namespace isc
Element::createMap
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition exceptions/exceptions.h:132
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition exceptions/exceptions.cc:32
isc::NotImplemented
A generic exception that is thrown when a function is not implemented.
Definition exceptions/exceptions.h:166
isc::yang::TranslatorLogger::getOutputOptions
isc::data::ElementPtr getOutputOptions(libyang::DataNode const &data_node)
Translate output options from YANG to JSON.
Definition translator_logger.cc:75
isc::yang::TranslatorLogger::setLogger
void setLogger(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set logger from JSON to YANG.
Definition translator_logger.cc:81
isc::yang::TranslatorLogger::setOutputOption
void setOutputOption(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set an output option from JSON to YANG.
Definition translator_logger.cc:116
isc::yang::TranslatorLogger::setLoggerKea
void setLoggerKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setLogger for loggers.
Definition translator_logger.cc:98
isc::yang::TranslatorLogger::getLoggerKea
isc::data::ElementPtr getLoggerKea(libyang::DataNode const &data_node)
getLogger JSON for loggers.
Definition translator_logger.cc:42
isc::yang::TranslatorLogger::getOutputOption
isc::data::ElementPtr getOutputOption(libyang::DataNode const &data_node)
Translate an output option from YANG to JSON.
Definition translator_logger.cc:61
isc::yang::TranslatorLogger::TranslatorLogger
TranslatorLogger(sysrepo::Session session, const std::string &model)
Constructor.
Definition translator_logger.cc:22
isc::yang::TranslatorLogger::getLogger
isc::data::ElementPtr getLogger(libyang::DataNode const &data_node)
Translate a logger from YANG to JSON.
Definition translator_logger.cc:27
isc::yang::TranslatorLogger::setOutputOptions
void setOutputOptions(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set output options from JSON to YANG.
Definition translator_logger.cc:127
isc::yang::TranslatorLoggers::setLoggers
void setLoggers(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set loggers from JSON to YANG.
Definition translator_logger.cc:177
isc::yang::TranslatorLoggers::getLoggersFromAbsoluteXpath
isc::data::ConstElementPtr getLoggersFromAbsoluteXpath(std::string const &xpath)
Translate loggers from YANG to JSON.
Definition translator_logger.cc:162
isc::yang::TranslatorLoggers::getLoggersKea
isc::data::ElementPtr getLoggersKea(libyang::DataNode const &data_node)
getLoggers JSON for loggers.
Definition translator_logger.cc:171
isc::yang::TranslatorLoggers::getLoggers
isc::data::ConstElementPtr getLoggers(libyang::DataNode const &data_node)
Translate loggers from YANG to JSON.
Definition translator_logger.cc:147
isc::yang::TranslatorLoggers::setLoggersKea
void setLoggersKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setLoggers for loggers.
Definition translator_logger.cc:194
isc::yang::TranslatorLoggers::TranslatorLoggers
TranslatorLoggers(sysrepo::Session session, const std::string &model)
Constructor.
Definition translator_logger.cc:141
isc::yang::Translator
Between YANG and JSON translator class for basic values.
Definition translator.h:23
isc::yang::Translator::getList
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::yang::Translator::checkAndSetLeaf
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::yang::Translator::findXPath
libyang::DataNode findXPath(std::string const &xpath) const
Retrieves a YANG data node by xpath.
Definition translator.cc:132
isc::yang::Translator::getMandatoryLeaf
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...
Definition translator.cc:197
isc::yang::Translator::checkAndGetLeaf
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
isc::yang::Translator::checkAndSetUserContext
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
isc::yang::Translator::setItem
void setItem(const std::string &xpath, isc::data::ConstElementPtr const elem, libyang::LeafBaseType const type)
Translate and set basic value from JSON to YANG.
Definition translator.cc:231
isc::yang::Translator::model_
std::string model_
The model.
Definition translator.h:427
isc::yang::Translator::checkAndGetAndJsonifyLeaf
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
isc::yang::Translator::Translator
Translator(sysrepo::Session session, const std::string &model)
Constructor.
Definition translator.cc:27
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
isc::data
Definition base_stamped_element.cc:12
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
isc::yang
Definition adaptor.cc:17
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
isc::yang::NetconfError
Generic NETCONF error.
Definition netconf_error.h:30
translator_logger.h
yang_models.h