Kea: translator_logger.cc Source File

| Kea 1.5.0

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