Kea  2.3.7
translator_logger.cc
Go to the documentation of this file.
1 // Copyright (C) 2018-2022 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/yang_models.h>
11 
12 #include <sstream>
13 
14 using namespace std;
15 using namespace isc::data;
16 using namespace libyang;
17 using namespace sysrepo;
18 
19 namespace isc {
20 namespace yang {
21 
22 TranslatorLogger::TranslatorLogger(Session session, const string& model)
23  : Translator(session, model) {
24 }
25 
27 TranslatorLogger::getLogger(DataNode const& data_node) {
28  try {
29  if ((model_ == KEA_DHCP4_SERVER) ||
30  (model_ == KEA_DHCP6_SERVER) ||
31  (model_ == KEA_DHCP_DDNS) ||
32  (model_ == KEA_CTRL_AGENT)) {
33  return (getLoggerKea(data_node));
34  }
35  } catch (Error const& ex) {
37  "getting logger: " << ex.what());
38  }
40  "getLogger not implemented for the model: " << model_);
41 }
42 
44 TranslatorLogger::getLoggerKea(DataNode const& data_node) {
45  ElementPtr result = Element::createMap();
46 
47  getMandatoryLeaf(result, data_node, "name");
48 
49  checkAndGetLeaf(result, data_node, "debuglevel");
50  checkAndGetLeaf(result, data_node, "severity");
51 
52  checkAndGetAndJsonifyLeaf(result, data_node, "user-context");
53 
54  ConstElementPtr options = getOutputOptions(data_node);
55  if (options) {
56  result->set("output_options", options);
57  }
58 
59  return (result->empty() ? ElementPtr() : result);
60 }
61 
63 TranslatorLogger::getOutputOption(DataNode const& data_node) {
64  ElementPtr result = Element::createMap();
65 
66  getMandatoryLeaf(result, data_node, "output");
67 
68  checkAndGetLeaf(result, data_node, "flush");
69  checkAndGetLeaf(result, data_node, "maxsize");
70  checkAndGetLeaf(result, data_node, "maxver");
71  checkAndGetLeaf(result, data_node, "pattern");
72 
73  return (result->empty() ? ElementPtr() : result);
74 }
75 
77 TranslatorLogger::getOutputOptions(DataNode const& data_node) {
78  return getList(data_node, "output-option", *this,
80 }
81 
82 void
83 TranslatorLogger::setLogger(string const& xpath, ConstElementPtr elem) {
84  try {
85  if ((model_ == KEA_DHCP4_SERVER) ||
86  (model_ == KEA_DHCP6_SERVER) ||
87  (model_ == KEA_DHCP_DDNS) ||
88  (model_ == KEA_CTRL_AGENT)) {
89  setLoggerKea(xpath, elem);
90  } else {
92  "setLogger not implemented for the model: " << model_);
93  }
94  } catch (Error const& ex) {
96  "setting logger '" << elem->str()
97  << "' : " << ex.what());
98  }
99 }
100 
101 void
103  // Skip key "name".
104 
105  checkAndSetLeaf(elem, xpath, "debuglevel", LeafBaseType::Uint8);
106  checkAndSetLeaf(elem, xpath, "severity", LeafBaseType::Enum);
107  checkAndSetUserContext(elem, xpath);
108 
109  ConstElementPtr options = elem->get("output_options");
110  if (options && !options->empty()) {
111  setOutputOptions(xpath, options);
112  }
113 }
114 
115 void
117  // Keys are set by setting the list itself.
118  setItem(xpath, ElementPtr(), LeafBaseType::Unknown);
119 
120  checkAndSetLeaf(elem, xpath, "flush", LeafBaseType::Bool);
121  checkAndSetLeaf(elem, xpath, "maxsize", LeafBaseType::Uint32);
122  checkAndSetLeaf(elem, xpath, "maxver", LeafBaseType::Uint32);
123  checkAndSetLeaf(elem, xpath, "pattern", LeafBaseType::String);
124 }
125 
126 void
128  for (size_t i = 0; i < elem->size(); ++i) {
129  ElementPtr option = elem->getNonConst(i);
130  if (!option->contains("output")) {
131  isc_throw(BadValue, "output-option without output: "
132  << option->str());
133  }
134  string output = option->get("output")->stringValue();
135  ostringstream key;
136  key << xpath << "/output-option[output='" << output << "']";
137  setOutputOption(key.str(), option);
138  }
139 }
140 
141 TranslatorLoggers::TranslatorLoggers(Session session, const string& model)
142  : Translator(session, model),
143  TranslatorLogger(session, model) {
144 }
145 
147 TranslatorLoggers::getLoggers(DataNode const& data_node) {
148  try {
149  if ((model_ == KEA_DHCP4_SERVER) ||
150  (model_ == KEA_DHCP6_SERVER) ||
151  (model_ == KEA_DHCP_DDNS) ||
152  (model_ == KEA_CTRL_AGENT)) {
153  return (getLoggersKea(data_node));
154  }
155  } catch (Error const& ex) {
157  "getting loggers: " << ex.what());
158  }
160  "getLoggers not implemented for the model: " << model_);
161 }
162 
165  try {
166  return getLoggers(findXPath(xpath));
167  } catch (NetconfError const&) {
168  return ElementPtr();
169  }
170 }
171 
173 TranslatorLoggers::getLoggersKea(DataNode const& data_node) {
174  return getList<TranslatorLogger>(data_node, "logger", *this,
176 }
177 
178 void
179 TranslatorLoggers::setLoggers(string const& xpath, ConstElementPtr elem) {
180  try {
181  if ((model_ == KEA_DHCP4_SERVER) ||
182  (model_ == KEA_DHCP6_SERVER) ||
183  (model_ == KEA_DHCP_DDNS) ||
184  (model_ == KEA_CTRL_AGENT)) {
185  setLoggersKea(xpath, elem);
186  } else {
188  "setLoggers not implemented for the model: " << model_);
189  }
190  } catch (Error const& ex) {
192  "setting loggers '" << elem->str()
193  << "' : " << ex.what());
194  }
195 }
196 
197 void
199  for (size_t i = 0; i < elem->size(); ++i) {
200  ElementPtr logger = elem->getNonConst(i);
201  if (!logger->contains("name")) {
202  isc_throw(BadValue, "logger without name: " << logger->str());
203  }
204  string name = logger->get("name")->stringValue();
205  ostringstream key;
206  key << xpath << "/logger[name='" << name << "']";
207  setLogger(key.str(), logger);
208  }
209 }
210 
211 } // namespace yang
212 } // 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.
Logger translation between YANG and JSON.
isc::data::ElementPtr getOutputOptions(libyang::DataNode const &data_node)
Translate output options from YANG to 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.
void setLoggerKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setLogger for loggers.
isc::data::ElementPtr getLoggerKea(libyang::DataNode const &data_node)
getLogger JSON for loggers.
isc::data::ElementPtr getOutputOption(libyang::DataNode const &data_node)
Translate an output option from YANG to JSON.
isc::data::ElementPtr getLogger(libyang::DataNode const &data_node)
Translate a logger from YANG to JSON.
void setOutputOptions(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set output options from JSON to YANG.
void setLoggers(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set loggers from JSON to YANG.
isc::data::ConstElementPtr getLoggersFromAbsoluteXpath(std::string const &xpath)
Translate loggers from YANG to JSON.
isc::data::ElementPtr getLoggersKea(libyang::DataNode const &data_node)
getLoggers JSON for loggers.
isc::data::ConstElementPtr getLoggers(libyang::DataNode const &data_node)
Translate loggers from YANG to JSON.
void setLoggersKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setLoggers for loggers.
TranslatorLoggers(sysrepo::Session session, const std::string &model)
Constructor.
Between YANG and JSON translator class for basic values.
Definition: translator.h:22
isc::data::ElementPtr getList(libyang::DataNode const &data_node, std::string const &xpath, T &t, isc::data::ElementPtr(T::*f)(libyang::DataNode const &)) const
Retrieve a list as ElementPtr from sysrepo from a certain xpath.
Definition: translator.h:273
void checkAndSetLeaf(isc::data::ConstElementPtr const &from, std::string const &xpath, std::string const &name, libyang::LeafBaseType const type)
Get an element from given ElementPtr node and set it in sysrepo at given xpath.
Definition: translator.cc:63
void setItem(const std::string &xpath, isc::data::ConstElementPtr elem, libyang::LeafBaseType type)
Translate and set basic value from JSON to YANG.
Definition: translator.cc:231
libyang::DataNode findXPath(std::string const &xpath) const
Retrieves a YANG data node by xpath.
Definition: translator.cc:132
void getMandatoryLeaf(isc::data::ElementPtr &storage, libyang::DataNode const &data_node, std::string const &name) const
Retrieves a child YANG data node identified by name from the given parent YANG container node and sto...
Definition: translator.cc:197
void checkAndGetLeaf(isc::data::ElementPtr &storage, libyang::DataNode const &data_node, std::string const &name) const
Retrieves a child YANG data node identified by name from the given parent YANG container node and sto...
Definition: translator.cc:32
void checkAndSetUserContext(isc::data::ConstElementPtr const &from, std::string const &xpath)
Get an element from given ElementPtr node and set it in sysrepo at given xpath.
Definition: translator.cc:99
std::string model_
The model.
Definition: translator.h:426
void checkAndGetAndJsonifyLeaf(isc::data::ElementPtr &storage, libyang::DataNode const &data_node, const std::string &name) const
Retrieves a child YANG data node identified by name from the given parent YANG container node,...
Definition: translator.cc:53
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
isc::log::Logger logger("asiodns")
Use the ASIO logger.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
Defines the logger used by the top-level component of kea-lfc.
Generic NETCONF error.
Definition: netconf_error.h:30