Kea: translator_logger.cc Source File

| Kea 2.2.0

translator_logger.cc
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#include <config.h>
8
9#include <yang/translator_logger.h>
10#include <yang/adaptor.h>
11#include <yang/yang_models.h>
12#include <sstream>
13
14using namespace std;
15using namespace isc::data;
16using namespace sysrepo;
17
18namespace isc {
19namespace yang {
20
21TranslatorLogger::TranslatorLogger(S_Session session, const string& model)
22 : TranslatorBasic(session, model) {
23}
24
25TranslatorLogger::~TranslatorLogger() {
26}
27
28 ElementPtr
29TranslatorLogger::getLogger(const string& xpath) {
30 try {
31 if ((model_ == KEA_DHCP4_SERVER) ||
32 (model_ == KEA_DHCP6_SERVER) ||
33 (model_ == KEA_DHCP_DDNS) ||
34 (model_ == KEA_CTRL_AGENT)) {
35 return (getLoggerKea(xpath));
36 }
37 } catch (const sysrepo_exception& ex) {
38 isc_throw(SysrepoError,
39 "sysrepo error getting logger at '" << xpath
40 << "': " << ex.what());
41 }
42 isc_throw(NotImplemented,
43 "getLogger not implemented for the model: " << model_);
44}
45
46 ElementPtr
47TranslatorLogger::getLoggerKea(const string& xpath) {
48 ConstElementPtr name = getItem(xpath + "/name");
49 if (!name) {
50 // Can't happen as name is the key.
51 isc_throw(Unexpected, "getLoggerKea requires name: " << xpath);
52 }
53 ElementPtr result = Element::createMap();
54 result->set("name", name);
55 ConstElementPtr options = getOutputOptions(xpath);
56 if (options && (options->size() > 0)) {
57 result->set("output_options", options);
58 }
59 ConstElementPtr severity = getItem(xpath + "/severity");
60 if (severity) {
61 result->set("severity", severity);
62 }
63 ConstElementPtr debuglevel = getItem(xpath + "/debuglevel");
64 if (debuglevel) {
65 result->set("debuglevel", debuglevel);
66 }
67 ConstElementPtr context = getItem(xpath + "/user-context");
68 if (context) {
69 result->set("user-context", Element::fromJSON(context->stringValue()));
70 }
71 return (result);
72}
73
74 ElementPtr
75TranslatorLogger::getOutputOption(const string& xpath) {
76 ConstElementPtr output = getItem(xpath + "/output");
77 if (!output) {
78 // Can't happen as output is the key.
79 isc_throw(Unexpected, "getOutputOption requires (!output): " << xpath);
80 }
81 ElementPtr result = Element::createMap();
82 result->set("output", output);
83 ConstElementPtr maxver = getItem(xpath + "/maxver");
84 if (maxver) {
85 result->set("maxver", maxver);
86 }
87 ConstElementPtr maxsize = getItem(xpath + "/maxsize");
88 if (maxsize) {
89 result->set("maxsize", maxsize);
90 }
91 ConstElementPtr flush = getItem(xpath + "/flush");
92 if (flush) {
93 result->set("flush", flush);
94 }
95 ConstElementPtr pattern = getItem(xpath + "/pattern");
96 if (pattern) {
97 result->set("pattern", pattern);
98 }
99 return (result);
100}
101
102 ElementPtr
103TranslatorLogger::getOutputOptions(const string& xpath) {
104 return getList(xpath + "/output-option", *this,
105 &TranslatorLogger::getOutputOption);
106}
107
108void
109TranslatorLogger::setLogger(const string& xpath, ConstElementPtr elem) {
110 try {
111 if ((model_ == KEA_DHCP4_SERVER) ||
112 (model_ == KEA_DHCP6_SERVER) ||
113 (model_ == KEA_DHCP_DDNS) ||
114 (model_ == KEA_CTRL_AGENT)) {
115 setLoggerKea(xpath, elem);
116 } else {
117 isc_throw(NotImplemented,
118 "setLogger not implemented for the model: " << model_);
119 }
120 } catch (const sysrepo_exception& ex) {
121 isc_throw(SysrepoError,
122 "sysrepo error setting logger '" << elem->str()
123 << "' at '" << xpath << "': " << ex.what());
124 }
125}
126
127void
128TranslatorLogger::setLoggerKea(const string& xpath, ConstElementPtr elem) {
129 // Skip name as it is the key.
130 ConstElementPtr options = elem->get("output_options");
131 if (options && (options->size() > 0)) {
132 setOutputOptions(xpath, options);
133 }
134 ConstElementPtr debuglevel = elem->get("debuglevel");
135 if (debuglevel) {
136 setItem(xpath + "/debuglevel", debuglevel, SR_UINT8_T);
137 }
138 ConstElementPtr severity = elem->get("severity");
139 if (severity) {
140 setItem(xpath + "/severity", severity, SR_ENUM_T);
141 }
142 ConstElementPtr context = Adaptor::getContext(elem);
143 if (context) {
144 setItem(xpath + "/user-context", Element::create(context->str()),
145 SR_STRING_T);
146 }
147}
148
149void
150TranslatorLogger::setOutputOption(const string& xpath, ConstElementPtr elem) {
151 bool created = false;
152 // Skip output as it is the key.
153 ConstElementPtr maxver = elem->get("maxver");
154 if (maxver) {
155 setItem(xpath + "/maxver", maxver, SR_UINT32_T);
156 created = true;
157 }
158 ConstElementPtr maxsize = elem->get("maxsize");
159 if (maxsize) {
160 setItem(xpath + "/maxsize", maxsize, SR_UINT32_T);
161 created = true;
162 }
163 ConstElementPtr flush = elem->get("flush");
164 if (flush) {
165 setItem(xpath + "/flush", flush, SR_BOOL_T);
166 created = true;
167 }
168 ConstElementPtr pattern = elem->get("pattern");
169 if (pattern) {
170 setItem(xpath + "/pattern", pattern, SR_STRING_T);
171 created = true;
172 }
173 // There is no mandatory fields outside the key so force creation.
174 if (!created) {
175 ConstElementPtr list = Element::createList();
176 setItem(xpath, list, SR_LIST_T);
177 }
178}
179
180void
181TranslatorLogger::setOutputOptions(const string& xpath, ConstElementPtr elem) {
182 for (size_t i = 0; i < elem->size(); ++i) {
183 ConstElementPtr option = elem->get(i);
184 if (!option->contains("output")) {
185 isc_throw(BadValue, "output-option without output: "
186 << option->str());
187 }
188 string output = option->get("output")->stringValue();
189 ostringstream key;
190 key << xpath << "/output-option[output='" << output << "']";
191 setOutputOption(key.str(), option);
192 }
193}
194
195TranslatorLoggers::TranslatorLoggers(S_Session session, const string& model)
196 : TranslatorBasic(session, model),
197 TranslatorLogger(session, model) {
198}
199
200TranslatorLoggers::~TranslatorLoggers() {
201}
202
203 ConstElementPtr
204TranslatorLoggers::getLoggers(const string& xpath) {
205 try {
206 if ((model_ == KEA_DHCP4_SERVER) ||
207 (model_ == KEA_DHCP6_SERVER) ||
208 (model_ == KEA_DHCP_DDNS) ||
209 (model_ == KEA_CTRL_AGENT)) {
210 return (getLoggersKea(xpath));
211 }
212 } catch (const sysrepo_exception& ex) {
213 isc_throw(SysrepoError,
214 "sysrepo error getting loggeres at '" << xpath
215 << "': " << ex.what());
216 }
217 isc_throw(NotImplemented,
218 "getLoggers not implemented for the model: " << model_);
219}
220
221 ElementPtr
222TranslatorLoggers::getLoggersKea(const string& xpath) {
223 return getList<TranslatorLogger>(xpath + "/logger", *this,
224 &TranslatorLogger::getLogger);
225}
226
227void
228TranslatorLoggers::setLoggers(const string& xpath, ConstElementPtr elem) {
229 try {
230 if ((model_ == KEA_DHCP4_SERVER) ||
231 (model_ == KEA_DHCP6_SERVER) ||
232 (model_ == KEA_DHCP_DDNS) ||
233 (model_ == KEA_CTRL_AGENT)) {
234 setLoggersKea(xpath, elem);
235 } else {
236 isc_throw(NotImplemented,
237 "setLoggers not implemented for the model: " << model_);
238 }
239 } catch (const sysrepo_exception& ex) {
240 isc_throw(SysrepoError,
241 "sysrepo error setting loggeres '" << elem->str()
242 << "' at '" << xpath << "': " << ex.what());
243 }
244}
245
246void
247TranslatorLoggers::setLoggersKea(const string& xpath, ConstElementPtr elem) {
248 for (size_t i = 0; i < elem->size(); ++i) {
249 ConstElementPtr logger = elem->get(i);
250 if (!logger->contains("name")) {
251 isc_throw(BadValue, "logger without name: " << logger->str());
252 }
253 string name = logger->get("name")->stringValue();
254 ostringstream key;
255 key << xpath << "/logger[name='" << name << "']";
256 setLogger(key.str(), logger);
257 }
258}
259
260} // namespace yang
261} // namespace isc
adaptor.h
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::Unexpected
A generic exception that is thrown when an unexpected error condition occurs.
Definition: exceptions/exceptions.h:153
isc::yang::Adaptor::getContext
static isc::data::ConstElementPtr getContext(isc::data::ConstElementPtr parent)
Get user context.
Definition: adaptor.cc:27
isc::yang::SysrepoError
Sysrepo error.
Definition: sysrepo_error.h:16
isc::yang::TranslatorBasic
Between YANG and JSON translator class for basic values.
Definition: translator.h:19
isc::yang::TranslatorBasic::getItem
isc::data::ElementPtr getItem(const std::string &xpath)
Get and translate basic value from YANG to JSON.
Definition: translator.cc:105
isc::yang::TranslatorBasic::model_
std::string model_
The model.
Definition: translator.h:171
isc::yang::TranslatorBasic::getList
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
isc::yang::TranslatorBasic::setItem
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
isc::yang::TranslatorLogger
Logger translation between YANG and JSON.
Definition: translator_logger.h:94
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:109
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:150
isc::yang::TranslatorLogger::TranslatorLogger
TranslatorLogger(sysrepo::S_Session session, const std::string &model)
Constructor.
Definition: translator_logger.cc:21
isc::yang::TranslatorLogger::setLoggerKea
void setLoggerKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setLogger for loggers.
Definition: translator_logger.cc:128
isc::yang::TranslatorLogger::getOutputOptions
isc::data::ElementPtr getOutputOptions(const std::string &xpath)
Get and translate output options from YANG to JSON.
Definition: translator_logger.cc:103
isc::yang::TranslatorLogger::getLoggerKea
isc::data::ElementPtr getLoggerKea(const std::string &xpath)
getLogger JSON for loggers.
Definition: translator_logger.cc:47
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:181
isc::yang::TranslatorLogger::getLogger
isc::data::ElementPtr getLogger(const std::string &xpath)
Get and translate a logger from YANG to JSON.
Definition: translator_logger.cc:29
isc::yang::TranslatorLogger::getOutputOption
isc::data::ElementPtr getOutputOption(const std::string &xpath)
Get and translate an output option from YANG to JSON.
Definition: translator_logger.cc:75
isc::yang::TranslatorLogger::~TranslatorLogger
virtual ~TranslatorLogger()
Destructor.
Definition: translator_logger.cc:25
isc::yang::TranslatorLoggers::~TranslatorLoggers
virtual ~TranslatorLoggers()
Destructor.
Definition: translator_logger.cc:200
isc::yang::TranslatorLoggers::setLoggers
void setLoggers(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set loggeres from JSON to YANG.
Definition: translator_logger.cc:228
isc::yang::TranslatorLoggers::TranslatorLoggers
TranslatorLoggers(sysrepo::S_Session session, const std::string &model)
Constructor.
Definition: translator_logger.cc:195
isc::yang::TranslatorLoggers::getLoggersKea
isc::data::ElementPtr getLoggersKea(const std::string &xpath)
getLoggers JSON for loggers.
Definition: translator_logger.cc:222
isc::yang::TranslatorLoggers::setLoggersKea
void setLoggersKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setLoggers for loggers.
Definition: translator_logger.cc:247
isc::yang::TranslatorLoggers::getLoggers
isc::data::ConstElementPtr getLoggers(const std::string &xpath)
Get and translate loggeres from YANG to JSON.
Definition: translator_logger.cc:204
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:210
isc::asiodns::logger
isc::log::Logger logger("asiodns")
Use the ASIO logger.
Definition: asiodns/logger.h:15
isc::data
Definition: base_stamped_element.cc:12
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
isc
Defines the logger used by the top-level component of kea-lfc.
Definition: agent_parser.cc:143
translator_logger.h
yang_models.h