Kea 3.1.1
translator_option_data.cc
Go to the documentation of this file.
1// Copyright (C) 2018-2025 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
14using namespace std;
15using namespace isc::data;
16using namespace libyang;
17using namespace sysrepo;
18
19namespace isc {
20namespace yang {
21
23 const string& model)
24 : Translator(session, model) {
25}
26
28TranslatorOptionData::getOptionData(DataNode const& data_node) {
29 try {
30 if ((model_ == KEA_DHCP4_SERVER) ||
31 (model_ == KEA_DHCP6_SERVER)) {
32 return (getOptionDataKea(data_node));
33 }
34 } catch (Error const& ex) {
36 "getting option data:"
37 << ex.what());
38 }
40 "getOptionData not implemented for the model: " << model_);
41}
42
45 try {
46 return getOptionData(findXPath(xpath));
47 } catch (NetconfError const&) {
48 return ElementPtr();
49 }
50}
51
53TranslatorOptionData::getOptionDataKea(DataNode const& data_node) {
55
56 // Code and space must exist.
57 getMandatoryLeaf(result, data_node, "code");
58 getMandatoryLeaf(result, data_node, "space");
59
60 // Data must exist according to the YANG module too, but empty data is considered no data.
61 ElementPtr const& x(getItem(data_node, "data"));
62 if (x && !x->stringValue().empty()) {
63 result->set("data", x);
64 }
65
66 checkAndGetLeaf(result, data_node, "always-send");
67 checkAndGetLeaf(result, data_node, "csv-format");
68 checkAndGetLeaf(result, data_node, "name");
69 checkAndGetLeaf(result, data_node, "never-send");
70 checkAndGetLeaf(result, data_node, "client-classes");
71
72 checkAndGetAndJsonifyLeaf(result, data_node, "user-context");
73
74 return (result->empty() ? ElementPtr() : result);
75}
76
77void
79 ConstElementPtr elem) {
80 try {
81 if ((model_ == KEA_DHCP4_SERVER) ||
82 (model_ == KEA_DHCP6_SERVER)) {
83 setOptionDataKea(xpath, elem);
84 } else {
86 "setOptionData not implemented for the model: "
87 << model_);
88 }
89 } catch (Error const& ex) {
91 "setting option data '" << elem->str()
92 << "' : " << ex.what());
93 }
94}
95
96void
98 ConstElementPtr elem) {
99 // Set the list element. This is important in case we have no other elements except the keys.
100 setItem(xpath, ElementPtr(), LeafBaseType::Unknown);
101
102 // Skip keys "code", "space", and "data" since they were set with the
103 // list element in the call above with the LeafBaseType::Unknown parameter.
104
105 checkAndSetLeaf(elem, xpath, "always-send", LeafBaseType::Bool);
106 checkAndSetLeaf(elem, xpath, "csv-format", LeafBaseType::Bool);
107 checkAndSetLeaf(elem, xpath, "name", LeafBaseType::String);
108 checkAndSetLeaf(elem, xpath, "never-send", LeafBaseType::Bool);
109 checkAndSetLeafList(elem, xpath, "client-classes", LeafBaseType::String);
110
111 checkAndSetUserContext(elem, xpath);
112}
113
115 const string& model)
116 : Translator(session, model),
117 TranslatorOptionData(session, model) {
118}
119
122 try {
123 if ((model_ == KEA_DHCP4_SERVER) ||
124 (model_ == KEA_DHCP6_SERVER)) {
125 return (getOptionDataListKea(data_node));
126 }
127 } catch (Error const& ex) {
129 "getting option data list:"
130 << ex.what());
131 }
133 "getOptionDataList not implemented for the model: " << model_);
134}
135
138 try {
139 return getOptionDataList(findXPath(xpath));
140 } catch (NetconfError const&) {
141 return ElementPtr();
142 }
143}
144
147 return getList<TranslatorOptionData>(data_node, "option-data", *this,
149}
150
151void
153 ConstElementPtr elem) {
154 try {
155 if ((model_ == KEA_DHCP4_SERVER) ||
156 (model_ == KEA_DHCP6_SERVER)) {
157 setOptionDataListKea(xpath, elem);
158 } else {
160 "setOptionDataList not implemented for the model: "
161 << model_);
162 }
163 } catch (Error const& ex) {
165 "setting option data list '" << elem->str()
166 << "' : " << ex.what());
167 }
168}
169
170void
172 ConstElementPtr elem) {
173 for (size_t i = 0; i < elem->size(); ++i) {
174 ElementPtr option = elem->getNonConst(i);
175
176 // Code and space must exist in the input.
177 if (!option->contains("code")) {
178 isc_throw(BadValue, "option data without code: " << option->str());
179 }
180 unsigned code = static_cast<unsigned>(option->get("code")->intValue());
181 if (!option->contains("space")) {
182 isc_throw(BadValue, "option data without space: " << option->str());
183 }
184 string space = option->get("space")->stringValue();
185
186 // Data must exist according to the YANG module too, but no data in the input is allowed and
187 // converted to an empty string.
188 string data;
189 if (option->contains("data")) {
190 data = option->get("data")->stringValue();
191 }
192
193 ostringstream keys;
194 keys << xpath << "/option-data[code='" << code <<
195 "'][space='" << space <<
196 "'][data='" << data << "']";
197 setOptionData(keys.str(), option);
198 }
199}
200
201} // namespace yang
202} // namespace isc
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
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.
isc::data::ConstElementPtr getOptionDataListKea(libyang::DataNode const &data_node)
getOptionDataList for kea-dhcp[46].
void setOptionDataList(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set option data list from JSON to YANG.
TranslatorOptionDataList(sysrepo::Session session, const std::string &model)
Constructor.
void setOptionDataListKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setOptionDataList for kea-dhcp[46].
isc::data::ConstElementPtr getOptionDataList(libyang::DataNode const &data_node)
Translate option data list from YANG to JSON.
isc::data::ConstElementPtr getOptionDataListFromAbsoluteXpath(std::string const &xpath)
Translate option data list from YANG to JSON.
isc::data::ElementPtr getOptionDataFromAbsoluteXpath(std::string const &xpath)
Translate an option data from YANG to JSON.
isc::data::ElementPtr getOptionDataKea(libyang::DataNode const &data_node)
getOptionData JSON for kea-dhcp[46].
TranslatorOptionData(sysrepo::Session session, const std::string &model)
Constructor.
isc::data::ElementPtr getOptionData(libyang::DataNode const &data_node)
Translate an option data from YANG to JSON.
void setOptionData(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set option data from JSON to YANG.
void setOptionDataKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setOptionData for kea-dhcp[46].
Between YANG and JSON translator class for basic values.
Definition translator.h:23
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:274
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
isc::data::ElementPtr getItem(libyang::DataNode const &data_node, std::string const &xpath) const
Translate a basic value from YANG to JSON for a given xpath that is relative to the given source node...
libyang::DataNode findXPath(std::string const &xpath) const
Retrieves a YANG data node by xpath.
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...
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
void setItem(const std::string &xpath, isc::data::ConstElementPtr const elem, libyang::LeafBaseType const type)
Translate and set basic value from JSON to YANG.
std::string model_
The model.
Definition translator.h:427
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
void checkAndSetLeafList(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 as a leaf-list.
Definition translator.cc:86
Translator(sysrepo::Session session, const std::string &model)
Constructor.
Definition translator.cc:27
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
Defines the logger used by the top-level component of kea-lfc.
Generic NETCONF error.