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
10#include <yang/adaptor.h>
11#include <yang/yang_models.h>
12#include <sstream>
13
14using namespace std;
15using namespace isc::data;
16#ifndef HAVE_PRE_0_7_6_SYSREPO
17using namespace sysrepo;
18#endif
19
20namespace isc {
21namespace yang {
22
23TranslatorLogger::TranslatorLogger(S_Session session, const string& model)
24 : TranslatorBasic(session, model) {
25}
26
28}
29
31TranslatorLogger::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) {
41 "sysrepo error getting logger at '" << xpath
42 << "': " << ex.what());
43 }
45 "getLogger not implemented for the model: " << model_);
46}
47
49TranslatorLogger::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 }
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
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 }
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
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 }
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
119void
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 {
129 "setLogger not implemented for the model: " << model_);
130 }
131 } catch (const sysrepo_exception& ex) {
133 "sysrepo error setting logger '" << elem->str()
134 << "' at '" << xpath << "': " << ex.what());
135 }
136}
137
138void
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
160void
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) {
182 setItem(xpath, list, SR_LIST_T);
183 }
184}
185
186void
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
201TranslatorLoggers::TranslatorLoggers(S_Session session, const string& model)
202 : TranslatorBasic(session, model),
203 TranslatorLogger(session, model) {
204}
205
207}
208
210TranslatorLoggers::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) {
220 "sysrepo error getting loggeres at '" << xpath
221 << "': " << ex.what());
222 }
224 "getLoggers not implemented for the model: " << model_);
225}
226
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 }
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
245void
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 {
255 "setLoggers not implemented for the model: " << model_);
256 }
257 } catch (const sysrepo_exception& ex) {
259 "sysrepo error setting loggeres '" << elem->str()
260 << "' at '" << xpath << "': " << ex.what());
261 }
262}
263
264void
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
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.
A generic exception that is thrown when an unexpected error condition occurs.
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition: data.cc:223
static ElementPtr fromJSON(const std::string &in, bool preproc=false)
These functions will parse the given string (JSON) representation of a compound element.
Definition: data.cc:750
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition: data.cc:268
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition: data.cc:263
static isc::data::ConstElementPtr getContext(isc::data::ConstElementPtr parent)
Get user context.
Definition: adaptor.cc:25
Between YANG and JSON translator class for basic values.
Definition: translator.h:27
std::string getNext(sysrepo::S_Iter_Value iter)
Get xpath of the next YANG list item.
Definition: translator.cc:283
isc::data::ElementPtr getItem(const std::string &xpath)
Get and translate basic value from YANG to JSON.
Definition: translator.cc:104
sysrepo::S_Iter_Value getIter(const std::string &xpath)
List iterator methods keeping the session private.
Definition: translator.cc:278
std::string model_
The model.
Definition: translator.h:132
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
Logger translation between YANG and 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.
TranslatorLogger(sysrepo::S_Session session, const std::string &model)
Constructor.
void setLoggerKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setLogger for kea-logging.
isc::data::ElementPtr getOutputOptions(const std::string &xpath)
Get and translate output options from YANG to JSON.
isc::data::ElementPtr getLoggerKea(const std::string &xpath)
getLogger JSON for kea-logging.
void setOutputOptions(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set output options from JSON to YANG.
isc::data::ElementPtr getLogger(const std::string &xpath)
Get and translate a logger from YANG to JSON.
isc::data::ElementPtr getOutputOption(const std::string &xpath)
Get and translate an output option from YANG to JSON.
virtual ~TranslatorLogger()
Destructor.
virtual ~TranslatorLoggers()
Destructor.
void setLoggers(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set loggeres from JSON to YANG.
TranslatorLoggers(sysrepo::S_Session session, const std::string &model)
Constructor.
isc::data::ElementPtr getLoggersKea(const std::string &xpath)
getLoggers JSON for kea-logging.
void setLoggersKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setLoggers for kea-logging.
isc::data::ConstElementPtr getLoggers(const std::string &xpath)
Get and translate loggeres from YANG to JSON.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
boost::shared_ptr< Element > ElementPtr
Definition: data.h:22
Defines the logger used by the top-level component of kea-dhcp-ddns.