Kea  1.9.9-git
translator_logger.cc
Go to the documentation of this file.
1 // Copyright (C) 2018-2019 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 
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 
28 }
29 
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) {
41  "sysrepo error getting logger at '" << xpath
42  << "': " << ex.what());
43  }
45  "getLogger not implemented for the model: " << model_);
46 }
47 
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 
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  ConstElementPtr pattern = getItem(xpath + "/pattern");
98  if (pattern) {
99  result->set("pattern", pattern);
100  }
101  return (result);
102 }
103 
106  S_Iter_Value iter = getIter(xpath + "/output-option");
107  if (!iter) {
108  // Can't happen.
109  isc_throw(Unexpected, "getOutputOptions: can't get iterator: "
110  << xpath);
111  }
112  ElementPtr result = Element::createList();
113  for (;;) {
114  const string& option = getNext(iter);
115  if (option.empty()) {
116  break;
117  }
118  result->add(getOutputOption(option));
119  }
120  return (result);
121 }
122 
123 void
124 TranslatorLogger::setLogger(const string& xpath, ConstElementPtr elem) {
125  try {
126  if ((model_ == KEA_DHCP4_SERVER) ||
127  (model_ == KEA_DHCP6_SERVER) ||
128  (model_ == KEA_DHCP_DDNS) ||
129  (model_ == KEA_CTRL_AGENT)) {
130  setLoggerKea(xpath, elem);
131  } else {
133  "setLogger not implemented for the model: " << model_);
134  }
135  } catch (const sysrepo_exception& ex) {
137  "sysrepo error setting logger '" << elem->str()
138  << "' at '" << xpath << "': " << ex.what());
139  }
140 }
141 
142 void
144  // Skip name as it is the key.
145  ConstElementPtr options = elem->get("output_options");
146  if (options && (options->size() > 0)) {
147  setOutputOptions(xpath, options);
148  }
149  ConstElementPtr debuglevel = elem->get("debuglevel");
150  if (debuglevel) {
151  setItem(xpath + "/debuglevel", debuglevel, SR_UINT8_T);
152  }
153  ConstElementPtr severity = elem->get("severity");
154  if (severity) {
155  setItem(xpath + "/severity", severity, SR_ENUM_T);
156  }
157  ConstElementPtr context = Adaptor::getContext(elem);
158  if (context) {
159  setItem(xpath + "/user-context", Element::create(context->str()),
160  SR_STRING_T);
161  }
162 }
163 
164 void
166  bool created = false;
167  // Skip output as it is the key.
168  ConstElementPtr maxver = elem->get("maxver");
169  if (maxver) {
170  setItem(xpath + "/maxver", maxver, SR_UINT32_T);
171  created = true;
172  }
173  ConstElementPtr maxsize = elem->get("maxsize");
174  if (maxsize) {
175  setItem(xpath + "/maxsize", maxsize, SR_UINT32_T);
176  created = true;
177  }
178  ConstElementPtr flush = elem->get("flush");
179  if (flush) {
180  setItem(xpath + "/flush", flush, SR_BOOL_T);
181  created = true;
182  }
183  ConstElementPtr pattern = elem->get("pattern");
184  if (pattern) {
185  setItem(xpath + "/pattern", pattern, SR_STRING_T);
186  created = true;
187  }
188  // There is no mandatory fields outside the key so force creation.
189  if (!created) {
190  ConstElementPtr list = Element::createList();
191  setItem(xpath, list, SR_LIST_T);
192  }
193 }
194 
195 void
197  for (size_t i = 0; i < elem->size(); ++i) {
198  ConstElementPtr option = elem->get(i);
199  if (!option->contains("output")) {
200  isc_throw(BadValue, "output-options without output: "
201  << option->str());
202  }
203  string output = option->get("output")->stringValue();
204  ostringstream key;
205  key << xpath << "/output-option[output='" << output << "']";
206  setOutputOption(key.str(), option);
207  }
208 }
209 
210 TranslatorLoggers::TranslatorLoggers(S_Session session, const string& model)
211  : TranslatorBasic(session, model),
212  TranslatorLogger(session, model) {
213 }
214 
216 }
217 
219 TranslatorLoggers::getLoggers(const string& xpath) {
220  try {
221  if ((model_ == KEA_DHCP4_SERVER) ||
222  (model_ == KEA_DHCP6_SERVER) ||
223  (model_ == KEA_DHCP_DDNS) ||
224  (model_ == KEA_CTRL_AGENT)) {
225  return (getLoggersKea(xpath));
226  }
227  } catch (const sysrepo_exception& ex) {
229  "sysrepo error getting loggeres at '" << xpath
230  << "': " << ex.what());
231  }
233  "getLoggers not implemented for the model: " << model_);
234 }
235 
237 TranslatorLoggers::getLoggersKea(const string& xpath) {
238  S_Iter_Value iter = getIter(xpath + "/logger");
239  if (!iter) {
240  // Can't happen.
241  isc_throw(Unexpected, "getLoggersKea: can't get iterator: " << xpath);
242  }
243  ElementPtr result = Element::createList();
244  for (;;) {
245  const string& logger = getNext(iter);
246  if (logger.empty()) {
247  break;
248  }
249  result->add(getLogger(logger));
250  }
251  return (result);
252 }
253 
254 void
255 TranslatorLoggers::setLoggers(const string& xpath, ConstElementPtr elem) {
256  try {
257  if ((model_ == KEA_DHCP4_SERVER) ||
258  (model_ == KEA_DHCP6_SERVER) ||
259  (model_ == KEA_DHCP_DDNS) ||
260  (model_ == KEA_CTRL_AGENT)) {
261  setLoggersKea(xpath, elem);
262  } else {
264  "setLoggers not implemented for the model: " << model_);
265  }
266  } catch (const sysrepo_exception& ex) {
268  "sysrepo error setting loggeres '" << elem->str()
269  << "' at '" << xpath << "': " << ex.what());
270  }
271 }
272 
273 void
275  for (size_t i = 0; i < elem->size(); ++i) {
276  ConstElementPtr logger = elem->get(i);
277  if (!logger->contains("name")) {
278  isc_throw(BadValue, "logger without name: " << logger->str());
279  }
280  string name = logger->get("name")->stringValue();
281  ostringstream key;
282  key << xpath << "/logger[name='" << name << "']";
283  setLogger(key.str(), logger);
284  }
285 }
286 
287 }; // end of namespace isc::yang
288 }; // end of namespace isc
TranslatorLoggers(sysrepo::S_Session session, const std::string &model)
Constructor.
A generic exception that is thrown when a function is not implemented.
Between YANG and JSON translator class for basic values.
Definition: translator.h:27
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
STL namespace.
isc::data::ConstElementPtr getLoggers(const std::string &xpath)
Get and translate loggeres from YANG to JSON.
sysrepo::S_Iter_Value getIter(const std::string &xpath)
List iterator methods keeping the session private.
Definition: translator.cc:316
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
isc::data::ElementPtr getOutputOption(const std::string &xpath)
Get and translate an output option from YANG to JSON.
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:288
void setLoggerKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setLogger for loggers.
isc::data::ElementPtr getLoggerKea(const std::string &xpath)
getLogger JSON for loggers.
void setLoggersKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setLoggers for loggers.
A generic exception that is thrown when an unexpected error condition occurs.
isc::data::ElementPtr getLoggersKea(const std::string &xpath)
getLoggers JSON for loggers.
std::string model_
The model.
Definition: translator.h:132
void setLogger(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set logger from JSON to YANG.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
std::string getNext(sysrepo::S_Iter_Value iter)
Get xpath of the next YANG list item.
Definition: translator.cc:321
static isc::data::ConstElementPtr getContext(isc::data::ConstElementPtr parent)
Get user context.
Definition: adaptor.cc:27
isc::data::ElementPtr getOutputOptions(const std::string &xpath)
Get and translate output options from YANG to JSON.
void setLoggers(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set loggeres from JSON to YANG.
void setOutputOption(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set an output option from JSON to YANG.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Logger translation between YANG and JSON.
Defines the logger used by the top-level component of kea-dhcp-ddns.
isc::log::Logger logger("asiodns")
Use the ASIO logger.
isc::data::ElementPtr getItem(const std::string &xpath)
Get and translate basic value from YANG to JSON.
Definition: translator.cc:111
void setOutputOptions(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set output options from JSON to YANG.
virtual ~TranslatorLogger()
Destructor.
virtual ~TranslatorLoggers()
Destructor.
isc::data::ElementPtr getLogger(const std::string &xpath)
Get and translate a logger from YANG to JSON.