Kea 2.7.5
translator_shared_network.cc
Go to the documentation of this file.
1// Copyright (C) 2018-2024 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 TranslatorOptionData(session, model),
26 TranslatorOptionDataList(session, model),
27 TranslatorPool(session, model),
28 TranslatorPools(session, model),
29 TranslatorPdPool(session, model),
30 TranslatorPdPools(session, model),
31 TranslatorHost(session, model),
32 TranslatorHosts(session, model),
33 TranslatorSubnet(session, model),
34 TranslatorSubnets(session, model) {
35}
36
39 try {
40 if (model_ == KEA_DHCP4_SERVER) {
41 return (getSharedNetworkKea(data_node, "subnet4"));
42 } else if (model_ == KEA_DHCP6_SERVER) {
43 return (getSharedNetworkKea(data_node, "subnet6"));
44 }
45 } catch (Error const& ex) {
47 "getting shared network:"
48 << ex.what());
49 }
51 "getSharedNetwork not implemented for the model: " << model_);
52}
53
56 try {
57 return getSharedNetwork(findXPath(xpath));
58 } catch (NetconfError const&) {
59 return ElementPtr();
60 }
61}
62
65 string const& subsel) {
67
68 getMandatoryLeaf(result, data_node, "name");
69
70 checkAndGetLeaf(result, data_node, "allocator");
71 checkAndGetLeaf(result, data_node, "cache-max-age");
72 checkAndGetLeaf(result, data_node, "cache-threshold");
73 checkAndGetLeaf(result, data_node, "calculate-tee-times");
74 checkAndGetLeaf(result, data_node, "client-class");
75 checkAndGetLeaf(result, data_node, "ddns-generated-prefix");
76 checkAndGetLeaf(result, data_node, "ddns-override-client-update");
77 checkAndGetLeaf(result, data_node, "ddns-override-no-update");
78 checkAndGetLeaf(result, data_node, "ddns-qualifying-suffix");
79 checkAndGetLeaf(result, data_node, "ddns-replace-client-name");
80 checkAndGetLeaf(result, data_node, "ddns-send-updates");
81 checkAndGetLeaf(result, data_node, "ddns-ttl-percent");
82 checkAndGetLeaf(result, data_node, "ddns-update-on-renew");
83 checkAndGetLeaf(result, data_node, "ddns-use-conflict-resolution");
84 checkAndGetLeaf(result, data_node, "ddns-conflict-resolution-mode");
85 checkAndGetLeaf(result, data_node, "hostname-char-replacement");
86 checkAndGetLeaf(result, data_node, "hostname-char-set");
87 checkAndGetLeaf(result, data_node, "interface");
88 checkAndGetLeaf(result, data_node, "max-valid-lifetime");
89 checkAndGetLeaf(result, data_node, "min-valid-lifetime");
90 checkAndGetLeaf(result, data_node, "rebind-timer");
91 checkAndGetLeaf(result, data_node, "renew-timer");
92 checkAndGetLeaf(result, data_node, "require-client-classes");
93 checkAndGetLeaf(result, data_node, "reservations-global");
94 checkAndGetLeaf(result, data_node, "reservations-in-subnet");
95 checkAndGetLeaf(result, data_node, "reservations-out-of-pool");
96 checkAndGetLeaf(result, data_node, "store-extended-info");
97 checkAndGetLeaf(result, data_node, "t1-percent");
98 checkAndGetLeaf(result, data_node, "t2-percent");
99 checkAndGetLeaf(result, data_node, "valid-lifetime");
100
101 checkAndGetAndJsonifyLeaf(result, data_node, "user-context");
102
103 ConstElementPtr options = getOptionDataList(data_node);
104 if (options) {
105 result->set("option-data", options);
106 }
107
108 ConstElementPtr subnets = getSubnets(data_node);
109 if (subnets) {
110 result->set(subsel, subnets);
111 }
112
113 checkAndGet(result, data_node, "relay",
114 [&](DataNode const& node) -> ElementPtr const {
116 checkAndGetLeaf(relay, node, "ip-addresses");
117 return relay;
118 });
119
120 if (subsel == "subnet6") {
121 checkAndGetLeaf(result, data_node, "interface-id");
122 checkAndGetLeaf(result, data_node, "max-preferred-lifetime");
123 checkAndGetLeaf(result, data_node, "min-preferred-lifetime");
124 checkAndGetLeaf(result, data_node, "pd-allocator");
125 checkAndGetLeaf(result, data_node, "preferred-lifetime");
126 checkAndGetLeaf(result, data_node, "rapid-commit");
127 } else if (subsel == "subnet4") {
128 checkAndGetLeaf(result, data_node, "authoritative");
129 checkAndGetLeaf(result, data_node, "boot-file-name");
130 checkAndGetLeaf(result, data_node, "match-client-id");
131 checkAndGetLeaf(result, data_node, "next-server");
132 checkAndGetLeaf(result, data_node, "offer-lifetime");
133 checkAndGetLeaf(result, data_node, "server-hostname");
134 }
135
136 return (result->empty() ? ElementPtr() : result);
137}
138
139void
141 ConstElementPtr elem) {
142 try {
143 if (model_ == KEA_DHCP4_SERVER) {
144 setSharedNetworkKea(xpath, elem, "subnet4");
145 } else if (model_ == KEA_DHCP6_SERVER) {
146 setSharedNetworkKea(xpath, elem, "subnet6");
147 } else {
149 "setSharedNetwork not implemented for the model: "
150 << model_);
151 }
152 } catch (Error const& ex) {
154 "setting shared network '" << elem->str()
155 << "' : " << ex.what());
156 }
157}
158
159void
161 ConstElementPtr elem,
162 string const& subsel) {
163 // Set the list element. This is important in case we have no other elements except the key.
164 setItem(xpath, ElementPtr(), LeafBaseType::Unknown);
165
166 // Skip key "name" since it was set with the list element in the call above
167 // with the LeafBaseType::Unknown parameter.
168
169 checkAndSetLeaf(elem, xpath, "allocator", LeafBaseType::String);
170 checkAndSetLeaf(elem, xpath, "cache-max-age", LeafBaseType::Uint32);
171 checkAndSetLeaf(elem, xpath, "cache-threshold", LeafBaseType::Dec64);
172 checkAndSetLeaf(elem, xpath, "calculate-tee-times", LeafBaseType::Bool);
173 checkAndSetLeaf(elem, xpath, "client-class", LeafBaseType::String);
174 checkAndSetLeaf(elem, xpath, "ddns-generated-prefix", LeafBaseType::String);
175 checkAndSetLeaf(elem, xpath, "ddns-override-client-update", LeafBaseType::Bool);
176 checkAndSetLeaf(elem, xpath, "ddns-override-no-update", LeafBaseType::Bool);
177 checkAndSetLeaf(elem, xpath, "ddns-qualifying-suffix", LeafBaseType::String);
178 checkAndSetLeaf(elem, xpath, "ddns-replace-client-name", LeafBaseType::String);
179 checkAndSetLeaf(elem, xpath, "ddns-send-updates", LeafBaseType::Bool);
180 checkAndSetLeaf(elem, xpath, "ddns-ttl-percent", LeafBaseType::Dec64);
181 checkAndSetLeaf(elem, xpath, "ddns-update-on-renew", LeafBaseType::Bool);
182 checkAndSetLeaf(elem, xpath, "ddns-use-conflict-resolution", LeafBaseType::Bool);
183 checkAndSetLeaf(elem, xpath, "ddns-conflict-resolution-mode", LeafBaseType::Enum);
184 checkAndSetLeaf(elem, xpath, "hostname-char-replacement", LeafBaseType::String);
185 checkAndSetLeaf(elem, xpath, "hostname-char-set", LeafBaseType::String);
186 checkAndSetLeaf(elem, xpath, "interface", LeafBaseType::String);
187 checkAndSetLeaf(elem, xpath, "max-valid-lifetime", LeafBaseType::Uint32);
188 checkAndSetLeaf(elem, xpath, "min-valid-lifetime", LeafBaseType::Uint32);
189 checkAndSetLeaf(elem, xpath, "rebind-timer", LeafBaseType::Uint32);
190 checkAndSetLeaf(elem, xpath, "renew-timer", LeafBaseType::Uint32);
191 checkAndSetLeaf(elem, xpath, "reservations-global", LeafBaseType::Bool);
192 checkAndSetLeaf(elem, xpath, "reservations-in-subnet", LeafBaseType::Bool);
193 checkAndSetLeaf(elem, xpath, "reservations-out-of-pool", LeafBaseType::Bool);
194 checkAndSetLeaf(elem, xpath, "store-extended-info", LeafBaseType::Bool);
195 checkAndSetLeaf(elem, xpath, "t1-percent", LeafBaseType::Dec64);
196 checkAndSetLeaf(elem, xpath, "t2-percent", LeafBaseType::Dec64);
197 checkAndSetLeaf(elem, xpath, "valid-lifetime", LeafBaseType::Uint32);
198
199 checkAndSetLeafList(elem, xpath, "require-client-classes", LeafBaseType::String);
200
201 checkAndSetUserContext(elem, xpath);
202
203 ConstElementPtr relay = elem->get("relay");
204 if (relay) {
205 ConstElementPtr addresses = relay->get("ip-addresses");
206 if (addresses && !addresses->empty()) {
207 for (ElementPtr const& addr : addresses->listValue()) {
208 setItem(xpath + "/relay/ip-addresses", addr, LeafBaseType::String);
209 }
210 }
211 }
212
213 ConstElementPtr options = elem->get("option-data");
214 if (options && !options->empty()) {
215 setOptionDataList(xpath, options);
216 }
217
218 ConstElementPtr subnets = elem->get(subsel);
219 if (subnets && !subnets->empty()) {
220 setSubnets(xpath, subnets);
221 }
222
223 if (subsel == "subnet6") {
224 checkAndSetLeaf(elem, xpath, "interface-id", LeafBaseType::String);
225 checkAndSetLeaf(elem, xpath, "max-preferred-lifetime", LeafBaseType::Uint32);
226 checkAndSetLeaf(elem, xpath, "min-preferred-lifetime", LeafBaseType::Uint32);
227 checkAndSetLeaf(elem, xpath, "pd-allocator", LeafBaseType::String);
228 checkAndSetLeaf(elem, xpath, "preferred-lifetime", LeafBaseType::Uint32);
229 checkAndSetLeaf(elem, xpath, "rapid-commit", LeafBaseType::Bool);
230 } else {
231 checkAndSetLeaf(elem, xpath, "authoritative", LeafBaseType::Bool);
232 checkAndSetLeaf(elem, xpath, "boot-file-name", LeafBaseType::String);
233 checkAndSetLeaf(elem, xpath, "match-client-id", LeafBaseType::Bool);
234 checkAndSetLeaf(elem, xpath, "next-server", LeafBaseType::String);
235 checkAndSetLeaf(elem, xpath, "offer-lifetime", LeafBaseType::Uint32);
236 checkAndSetLeaf(elem, xpath, "server-hostname", LeafBaseType::String);
237 }
238}
239
241 const string& model)
242 : Translator(session, model),
243 TranslatorOptionData(session, model),
244 TranslatorOptionDataList(session, model),
245 TranslatorPool(session, model),
246 TranslatorPools(session, model),
247 TranslatorPdPool(session, model),
248 TranslatorPdPools(session, model),
249 TranslatorHost(session, model),
250 TranslatorHosts(session, model),
251 TranslatorSubnet(session, model),
252 TranslatorSubnets(session, model),
253 TranslatorSharedNetwork(session, model) {
254}
255
258 return getList<TranslatorSharedNetwork>(data_node, "shared-network", *this,
260}
261
264 try {
265 return getSharedNetworks(findXPath(xpath));
266 } catch (NetconfError const&) {
267 return ElementPtr();
268 }
269}
270
271void
273 ConstElementPtr elem) {
274 try {
275 if ((model_ == KEA_DHCP4_SERVER) ||
276 (model_ == KEA_DHCP6_SERVER)) {
277 setSharedNetworksKea(xpath, elem);
278 } else {
280 "setSharedNetworks not implemented for the model: "
281 << model_);
282 }
283 } catch (Error const& ex) {
285 "setting shared networks '" << elem->str()
286 << "' : " << ex.what());
287 }
288}
289
290void
292 ConstElementPtr elem) {
293 for (size_t i = 0; i < elem->size(); ++i) {
294 ElementPtr network = elem->getNonConst(i);
295 if (!network->contains("name")) {
296 isc_throw(BadValue, "setSharedNetworksKea requires name: "
297 << network->str());
298 }
299 string name = network->get("name")->stringValue();
300 ostringstream key;
301 key<< xpath << "/shared-network[name='" << name << "']";
302 setSharedNetwork(key.str(), network);
303 }
304}
305
306} // namespace yang
307} // 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.
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
Translation between YANG and JSON for a single host reservation.
A translator class for converting host reservations list between YANG and JSON.
A translator class for converting an option data list between YANG and JSON.
void setOptionDataList(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set option data list from JSON to YANG.
isc::data::ConstElementPtr getOptionDataList(libyang::DataNode const &data_node)
Translate option data list from YANG to JSON.
Option data translation between YANG and JSON.
Prefix delegation pool translation between YANG and JSON.
A translator class for converting a pd-pool list between YANG and JSON.
A translator class for converting a pool between YANG and JSON.
A translator class for converting pools between YANG and JSON.
Shared network translation between YANG and JSON.
isc::data::ElementPtr getSharedNetworkFromAbsoluteXpath(std::string const &xpath)
Translate a shared network from YANG to JSON.
TranslatorSharedNetwork(sysrepo::Session session, const std::string &model)
Constructor.
isc::data::ElementPtr getSharedNetwork(libyang::DataNode const &data_node)
Translate a shared network from YANG to JSON.
void setSharedNetworkKea(const std::string &xpath, isc::data::ConstElementPtr elem, const std::string &subsel)
setSharedNetwork for kea-dhcp4-server and kea-dhcp6-server models
isc::data::ElementPtr getSharedNetworkKea(libyang::DataNode const &data_node, const std::string &subsel)
getSharedNetwork for kea-dhcp4-server and kea-dhcp6-server models
void setSharedNetwork(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set shared network from JSON to YANG.
TranslatorSharedNetworks(sysrepo::Session session, const std::string &model)
Constructor.
void setSharedNetworksKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setSharedNetworks for kea-dhcp4-server and kea-dhcp6-server
isc::data::ElementPtr getSharedNetworks(libyang::DataNode const &data_node)
Translate shared networks from YANG to JSON.
isc::data::ElementPtr getSharedNetworksFromAbsoluteXpath(std::string const &xpath)
Translate shared networks from YANG to JSON.
void setSharedNetworks(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set shared networks from JSON to YANG.
Subnet (aka network range) translation between YANG and JSON.
A translator class for converting a subnet list between YANG and JSON.
void setSubnets(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set subnets from JSON to YANG.
isc::data::ElementPtr getSubnets(libyang::DataNode const &data_node)
Get and translate subnets from YANG to JSON.
Between YANG and JSON translator class for basic values.
Definition translator.h:23
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 checkAndGet(isc::data::ElementPtr const &storage, libyang::DataNode const &data_node, std::string const &xpath, T translate) const
Calls {translate} for the element found at {xpath} relative to {data_node} and sets the result in {st...
Definition translator.h:44
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
#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.