Kea  2.1.7-git
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 
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 using namespace sysrepo;
17 
18 namespace isc {
19 namespace yang {
20 
21 TranslatorLogger::TranslatorLogger(S_Session session, const string& model)
22  : TranslatorBasic(session, model) {
23 }
24 
26 }
27 
29 TranslatorLogger::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) {
39  "sysrepo error getting logger at '" << xpath
40  << "': " << ex.what());
41  }
43  "getLogger not implemented for the model: " << model_);
44 }
45 
47 TranslatorLogger::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 
75 TranslatorLogger::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 
104  return getList(xpath + "/output-option", *this,
106 }
107 
108 void
109 TranslatorLogger::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 {
118  "setLogger not implemented for the model: " << model_);
119  }
120  } catch (const sysrepo_exception& ex) {
122  "sysrepo error setting logger '" << elem->str()
123  << "' at '" << xpath << "': " << ex.what());
124  }
125 }
126 
127 void
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 
149 void
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 
180 void
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 
195 TranslatorLoggers::TranslatorLoggers(S_Session session, const string& model)
196  : TranslatorBasic(session, model),
197  TranslatorLogger(session, model) {
198 }
199 
201 }
202 
204 TranslatorLoggers::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) {
214  "sysrepo error getting loggeres at '" << xpath
215  << "': " << ex.what());
216  }
218  "getLoggers not implemented for the model: " << model_);
219 }
220 
222 TranslatorLoggers::getLoggersKea(const string& xpath) {
223  return getList<TranslatorLogger>(xpath + "/logger", *this,
225 }
226 
227 void
228 TranslatorLoggers::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 {
237  "setLoggers not implemented for the model: " << model_);
238  }
239  } catch (const sysrepo_exception& ex) {
241  "sysrepo error setting loggeres '" << elem->str()
242  << "' at '" << xpath << "': " << ex.what());
243  }
244 }
245 
246 void
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
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:19
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
STL namespace.
isc::data::ConstElementPtr getLoggers(const std::string &xpath)
Get and translate loggeres from YANG to JSON.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
#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:260
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:171
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:27
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.
Logger translation between YANG and JSON.
Defines the logger used by the top-level component of kea-lfc.
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:105
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 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::data::ElementPtr getLogger(const std::string &xpath)
Get and translate a logger from YANG to JSON.