Kea: translator_shared_network.cc Source File

| Kea 3.0.3-git

translator_shared_network.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
9#include <yang/translator_shared_network.h>
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
22TranslatorSharedNetwork::TranslatorSharedNetwork(Session session,
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
37 ElementPtr
38TranslatorSharedNetwork::getSharedNetwork(DataNode const& data_node) {
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) {
46 isc_throw(NetconfError,
47 "getting shared network:"
48 << ex.what());
49 }
50 isc_throw(NotImplemented,
51 "getSharedNetwork not implemented for the model: " << model_);
52}
53
54 ElementPtr
55TranslatorSharedNetwork::getSharedNetworkFromAbsoluteXpath(string const& xpath) {
56 try {
57 return getSharedNetwork(findXPath(xpath));
58 } catch (NetconfError const&) {
59 return ElementPtr();
60 }
61}
62
63 ElementPtr
64TranslatorSharedNetwork::getSharedNetworkKea(DataNode const& data_node,
65 string const& subsel) {
66 ElementPtr result = Element::createMap();
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, "client-classes");
76 checkAndGetLeaf(result, data_node, "ddns-generated-prefix");
77 checkAndGetLeaf(result, data_node, "ddns-override-client-update");
78 checkAndGetLeaf(result, data_node, "ddns-override-no-update");
79 checkAndGetLeaf(result, data_node, "ddns-qualifying-suffix");
80 checkAndGetLeaf(result, data_node, "ddns-replace-client-name");
81 checkAndGetLeaf(result, data_node, "ddns-send-updates");
82 checkAndGetLeaf(result, data_node, "ddns-ttl-percent");
83 checkAndGetLeaf(result, data_node, "ddns-ttl");
84 checkAndGetLeaf(result, data_node, "ddns-ttl-min");
85 checkAndGetLeaf(result, data_node, "ddns-ttl-max");
86 checkAndGetLeaf(result, data_node, "ddns-update-on-renew");
87 checkAndGetLeaf(result, data_node, "ddns-use-conflict-resolution");
88 checkAndGetLeaf(result, data_node, "ddns-conflict-resolution-mode");
89 checkAndGetLeaf(result, data_node, "hostname-char-replacement");
90 checkAndGetLeaf(result, data_node, "hostname-char-set");
91 checkAndGetLeaf(result, data_node, "interface");
92 checkAndGetLeaf(result, data_node, "max-valid-lifetime");
93 checkAndGetLeaf(result, data_node, "min-valid-lifetime");
94 checkAndGetLeaf(result, data_node, "rebind-timer");
95 checkAndGetLeaf(result, data_node, "renew-timer");
96 checkAndGetLeaf(result, data_node, "require-client-classes");
97 checkAndGetLeaf(result, data_node, "evaluate-additional-classes");
98 checkAndGetLeaf(result, data_node, "reservations-global");
99 checkAndGetLeaf(result, data_node, "reservations-in-subnet");
100 checkAndGetLeaf(result, data_node, "reservations-out-of-pool");
101 checkAndGetLeaf(result, data_node, "store-extended-info");
102 checkAndGetLeaf(result, data_node, "t1-percent");
103 checkAndGetLeaf(result, data_node, "t2-percent");
104 checkAndGetLeaf(result, data_node, "valid-lifetime");
105
106 checkAndGetAndJsonifyLeaf(result, data_node, "user-context");
107
108 ConstElementPtr options = getOptionDataList(data_node);
109 if (options) {
110 result->set("option-data", options);
111 }
112
113 ConstElementPtr subnets = getSubnets(data_node);
114 if (subnets) {
115 result->set(subsel, subnets);
116 }
117
118 checkAndGet(result, data_node, "relay",
119 [&](DataNode const& node) -> ElementPtr const {
120 ElementPtr relay(Element::createMap());
121 checkAndGetLeaf(relay, node, "ip-addresses");
122 return relay;
123 });
124
125 if (subsel == "subnet6") {
126 checkAndGetLeaf(result, data_node, "interface-id");
127 checkAndGetLeaf(result, data_node, "max-preferred-lifetime");
128 checkAndGetLeaf(result, data_node, "min-preferred-lifetime");
129 checkAndGetLeaf(result, data_node, "pd-allocator");
130 checkAndGetLeaf(result, data_node, "preferred-lifetime");
131 checkAndGetLeaf(result, data_node, "rapid-commit");
132 } else if (subsel == "subnet4") {
133 checkAndGetLeaf(result, data_node, "authoritative");
134 checkAndGetLeaf(result, data_node, "boot-file-name");
135 checkAndGetLeaf(result, data_node, "match-client-id");
136 checkAndGetLeaf(result, data_node, "next-server");
137 checkAndGetLeaf(result, data_node, "offer-lifetime");
138 checkAndGetLeaf(result, data_node, "server-hostname");
139 }
140
141 return (result->empty() ? ElementPtr() : result);
142}
143
144void
145TranslatorSharedNetwork::setSharedNetwork(string const& xpath,
146 ConstElementPtr elem) {
147 try {
148 if (model_ == KEA_DHCP4_SERVER) {
149 setSharedNetworkKea(xpath, elem, "subnet4");
150 } else if (model_ == KEA_DHCP6_SERVER) {
151 setSharedNetworkKea(xpath, elem, "subnet6");
152 } else {
153 isc_throw(NotImplemented,
154 "setSharedNetwork not implemented for the model: "
155 << model_);
156 }
157 } catch (Error const& ex) {
158 isc_throw(NetconfError,
159 "setting shared network '" << elem->str()
160 << "' : " << ex.what());
161 }
162}
163
164void
165TranslatorSharedNetwork::setSharedNetworkKea(string const& xpath,
166 ConstElementPtr elem,
167 string const& subsel) {
168 // Set the list element. This is important in case we have no other elements except the key.
169 setItem(xpath, ElementPtr(), LeafBaseType::Unknown);
170
171 // Skip key "name" since it was set with the list element in the call above
172 // with the LeafBaseType::Unknown parameter.
173
174 checkAndSetLeaf(elem, xpath, "allocator", LeafBaseType::String);
175 checkAndSetLeaf(elem, xpath, "cache-max-age", LeafBaseType::Uint32);
176 checkAndSetLeaf(elem, xpath, "cache-threshold", LeafBaseType::Dec64);
177 checkAndSetLeaf(elem, xpath, "calculate-tee-times", LeafBaseType::Bool);
178 checkAndSetLeaf(elem, xpath, "client-class", LeafBaseType::String);
179 checkAndSetLeaf(elem, xpath, "ddns-generated-prefix", LeafBaseType::String);
180 checkAndSetLeaf(elem, xpath, "ddns-override-client-update", LeafBaseType::Bool);
181 checkAndSetLeaf(elem, xpath, "ddns-override-no-update", LeafBaseType::Bool);
182 checkAndSetLeaf(elem, xpath, "ddns-qualifying-suffix", LeafBaseType::String);
183 checkAndSetLeaf(elem, xpath, "ddns-replace-client-name", LeafBaseType::String);
184 checkAndSetLeaf(elem, xpath, "ddns-send-updates", LeafBaseType::Bool);
185 checkAndSetLeaf(elem, xpath, "ddns-ttl-percent", LeafBaseType::Dec64);
186 checkAndSetLeaf(elem, xpath, "ddns-ttl", LeafBaseType::Uint32);
187 checkAndSetLeaf(elem, xpath, "ddns-ttl-min", LeafBaseType::Uint32);
188 checkAndSetLeaf(elem, xpath, "ddns-ttl-max", LeafBaseType::Uint32);
189 checkAndSetLeaf(elem, xpath, "ddns-update-on-renew", LeafBaseType::Bool);
190 checkAndSetLeaf(elem, xpath, "ddns-use-conflict-resolution", LeafBaseType::Bool);
191 checkAndSetLeaf(elem, xpath, "ddns-conflict-resolution-mode", LeafBaseType::Enum);
192 checkAndSetLeaf(elem, xpath, "hostname-char-replacement", LeafBaseType::String);
193 checkAndSetLeaf(elem, xpath, "hostname-char-set", LeafBaseType::String);
194 checkAndSetLeaf(elem, xpath, "interface", LeafBaseType::String);
195 checkAndSetLeaf(elem, xpath, "max-valid-lifetime", LeafBaseType::Uint32);
196 checkAndSetLeaf(elem, xpath, "min-valid-lifetime", LeafBaseType::Uint32);
197 checkAndSetLeaf(elem, xpath, "rebind-timer", LeafBaseType::Uint32);
198 checkAndSetLeaf(elem, xpath, "renew-timer", LeafBaseType::Uint32);
199 checkAndSetLeaf(elem, xpath, "reservations-global", LeafBaseType::Bool);
200 checkAndSetLeaf(elem, xpath, "reservations-in-subnet", LeafBaseType::Bool);
201 checkAndSetLeaf(elem, xpath, "reservations-out-of-pool", LeafBaseType::Bool);
202 checkAndSetLeaf(elem, xpath, "store-extended-info", LeafBaseType::Bool);
203 checkAndSetLeaf(elem, xpath, "t1-percent", LeafBaseType::Dec64);
204 checkAndSetLeaf(elem, xpath, "t2-percent", LeafBaseType::Dec64);
205 checkAndSetLeaf(elem, xpath, "valid-lifetime", LeafBaseType::Uint32);
206 checkAndSetLeafList(elem, xpath, "client-classes", LeafBaseType::String);
207 checkAndSetLeafList(elem, xpath, "require-client-classes", LeafBaseType::String);
208 checkAndSetLeafList(elem, xpath, "evaluate-additional-classes", LeafBaseType::String);
209
210 checkAndSetUserContext(elem, xpath);
211
212 ConstElementPtr relay = elem->get("relay");
213 if (relay) {
214 ConstElementPtr addresses = relay->get("ip-addresses");
215 if (addresses && !addresses->empty()) {
216 for (ElementPtr const& addr : addresses->listValue()) {
217 setItem(xpath + "/relay/ip-addresses", addr, LeafBaseType::String);
218 }
219 }
220 }
221
222 ConstElementPtr options = elem->get("option-data");
223 if (options && !options->empty()) {
224 setOptionDataList(xpath, options);
225 }
226
227 ConstElementPtr subnets = elem->get(subsel);
228 if (subnets && !subnets->empty()) {
229 setSubnets(xpath, subnets);
230 }
231
232 if (subsel == "subnet6") {
233 checkAndSetLeaf(elem, xpath, "interface-id", LeafBaseType::String);
234 checkAndSetLeaf(elem, xpath, "max-preferred-lifetime", LeafBaseType::Uint32);
235 checkAndSetLeaf(elem, xpath, "min-preferred-lifetime", LeafBaseType::Uint32);
236 checkAndSetLeaf(elem, xpath, "pd-allocator", LeafBaseType::String);
237 checkAndSetLeaf(elem, xpath, "preferred-lifetime", LeafBaseType::Uint32);
238 checkAndSetLeaf(elem, xpath, "rapid-commit", LeafBaseType::Bool);
239 } else {
240 checkAndSetLeaf(elem, xpath, "authoritative", LeafBaseType::Bool);
241 checkAndSetLeaf(elem, xpath, "boot-file-name", LeafBaseType::String);
242 checkAndSetLeaf(elem, xpath, "match-client-id", LeafBaseType::Bool);
243 checkAndSetLeaf(elem, xpath, "next-server", LeafBaseType::String);
244 checkAndSetLeaf(elem, xpath, "offer-lifetime", LeafBaseType::Uint32);
245 checkAndSetLeaf(elem, xpath, "server-hostname", LeafBaseType::String);
246 }
247}
248
249TranslatorSharedNetworks::TranslatorSharedNetworks(Session session,
250 const string& model)
251 : Translator(session, model),
252 TranslatorOptionData(session, model),
253 TranslatorOptionDataList(session, model),
254 TranslatorPool(session, model),
255 TranslatorPools(session, model),
256 TranslatorPdPool(session, model),
257 TranslatorPdPools(session, model),
258 TranslatorHost(session, model),
259 TranslatorHosts(session, model),
260 TranslatorSubnet(session, model),
261 TranslatorSubnets(session, model),
262 TranslatorSharedNetwork(session, model) {
263}
264
265 ElementPtr
266TranslatorSharedNetworks::getSharedNetworks(DataNode const& data_node) {
267 return getList<TranslatorSharedNetwork>(data_node, "shared-network", *this,
268 &TranslatorSharedNetwork::getSharedNetwork);
269}
270
271 ElementPtr
272TranslatorSharedNetworks::getSharedNetworksFromAbsoluteXpath(string const& xpath) {
273 try {
274 return getSharedNetworks(findXPath(xpath));
275 } catch (NetconfError const&) {
276 return ElementPtr();
277 }
278}
279
280void
281TranslatorSharedNetworks::setSharedNetworks(string const& xpath,
282 ConstElementPtr elem) {
283 try {
284 if ((model_ == KEA_DHCP4_SERVER) ||
285 (model_ == KEA_DHCP6_SERVER)) {
286 setSharedNetworksKea(xpath, elem);
287 } else {
288 isc_throw(NotImplemented,
289 "setSharedNetworks not implemented for the model: "
290 << model_);
291 }
292 } catch (Error const& ex) {
293 isc_throw(NetconfError,
294 "setting shared networks '" << elem->str()
295 << "' : " << ex.what());
296 }
297}
298
299void
300TranslatorSharedNetworks::setSharedNetworksKea(string const& xpath,
301 ConstElementPtr elem) {
302 for (size_t i = 0; i < elem->size(); ++i) {
303 ElementPtr network = elem->getNonConst(i);
304 if (!network->contains("name")) {
305 isc_throw(BadValue, "setSharedNetworksKea requires name: "
306 << network->str());
307 }
308 string name = network->get("name")->stringValue();
309 ostringstream key;
310 key<< xpath << "/shared-network[name='" << name << "']";
311 setSharedNetwork(key.str(), network);
312 }
313}
314
315} // namespace yang
316} // namespace isc
Element::createMap
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:354
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition exceptions/exceptions.h:132
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition exceptions/exceptions.cc:32
isc::NotImplemented
A generic exception that is thrown when a function is not implemented.
Definition exceptions/exceptions.h:166
isc::yang::TranslatorHost
Translation between YANG and JSON for a single host reservation.
Definition translator_host.h:113
isc::yang::TranslatorHosts
A translator class for converting host reservations list between YANG and JSON.
Definition translator_host.h:176
isc::yang::TranslatorOptionDataList
A translator class for converting an option data list between YANG and JSON.
Definition translator_option_data.h:136
isc::yang::TranslatorOptionDataList::setOptionDataList
void setOptionDataList(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set option data list from JSON to YANG.
Definition translator_option_data.cc:152
isc::yang::TranslatorOptionDataList::getOptionDataList
isc::data::ConstElementPtr getOptionDataList(libyang::DataNode const &data_node)
Translate option data list from YANG to JSON.
Definition translator_option_data.cc:121
isc::yang::TranslatorOptionData
Option data translation between YANG and JSON.
Definition translator_option_data.h:78
isc::yang::TranslatorPdPool
Prefix delegation pool translation between YANG and JSON.
Definition translator_pd_pool.h:121
isc::yang::TranslatorPdPools
A translator class for converting a pd-pool list between YANG and JSON.
Definition translator_pd_pool.h:199
isc::yang::TranslatorPool
A translator class for converting a pool between YANG and JSON.
Definition translator_pool.h:129
isc::yang::TranslatorPools
A translator class for converting pools between YANG and JSON.
Definition translator_pool.h:213
isc::yang::TranslatorSharedNetwork::getSharedNetworkFromAbsoluteXpath
isc::data::ElementPtr getSharedNetworkFromAbsoluteXpath(std::string const &xpath)
Translate a shared network from YANG to JSON.
Definition translator_shared_network.cc:55
isc::yang::TranslatorSharedNetwork::TranslatorSharedNetwork
TranslatorSharedNetwork(sysrepo::Session session, const std::string &model)
Constructor.
Definition translator_shared_network.cc:22
isc::yang::TranslatorSharedNetwork::getSharedNetwork
isc::data::ElementPtr getSharedNetwork(libyang::DataNode const &data_node)
Translate a shared network from YANG to JSON.
Definition translator_shared_network.cc:38
isc::yang::TranslatorSharedNetwork::setSharedNetworkKea
void setSharedNetworkKea(const std::string &xpath, isc::data::ConstElementPtr elem, const std::string &subsel)
setSharedNetwork for kea-dhcp4-server and kea-dhcp6-server models
Definition translator_shared_network.cc:165
isc::yang::TranslatorSharedNetwork::getSharedNetworkKea
isc::data::ElementPtr getSharedNetworkKea(libyang::DataNode const &data_node, const std::string &subsel)
getSharedNetwork for kea-dhcp4-server and kea-dhcp6-server models
Definition translator_shared_network.cc:64
isc::yang::TranslatorSharedNetwork::setSharedNetwork
void setSharedNetwork(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set shared network from JSON to YANG.
Definition translator_shared_network.cc:145
isc::yang::TranslatorSharedNetworks::TranslatorSharedNetworks
TranslatorSharedNetworks(sysrepo::Session session, const std::string &model)
Constructor.
Definition translator_shared_network.cc:249
isc::yang::TranslatorSharedNetworks::setSharedNetworksKea
void setSharedNetworksKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setSharedNetworks for kea-dhcp4-server and kea-dhcp6-server
Definition translator_shared_network.cc:300
isc::yang::TranslatorSharedNetworks::getSharedNetworks
isc::data::ElementPtr getSharedNetworks(libyang::DataNode const &data_node)
Translate shared networks from YANG to JSON.
Definition translator_shared_network.cc:266
isc::yang::TranslatorSharedNetworks::getSharedNetworksFromAbsoluteXpath
isc::data::ElementPtr getSharedNetworksFromAbsoluteXpath(std::string const &xpath)
Translate shared networks from YANG to JSON.
Definition translator_shared_network.cc:272
isc::yang::TranslatorSharedNetworks::setSharedNetworks
void setSharedNetworks(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set shared networks from JSON to YANG.
Definition translator_shared_network.cc:281
isc::yang::TranslatorSubnet
Subnet (aka network range) translation between YANG and JSON.
Definition translator_subnet.h:277
isc::yang::TranslatorSubnets
A translator class for converting a subnet list between YANG and JSON.
Definition translator_subnet.h:349
isc::yang::TranslatorSubnets::setSubnets
void setSubnets(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set subnets from JSON to YANG.
Definition translator_subnet.cc:394
isc::yang::TranslatorSubnets::getSubnets
isc::data::ElementPtr getSubnets(libyang::DataNode const &data_node)
Get and translate subnets from YANG to JSON.
Definition translator_subnet.cc:359
isc::yang::TranslatorSubnets::TranslatorSubnets
TranslatorSubnets(sysrepo::Session session, const std::string &model)
Constructor.
Definition translator_subnet.cc:345
isc::yang::Translator
Between YANG and JSON translator class for basic values.
Definition translator.h:23
isc::yang::Translator::getList
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
isc::yang::Translator::checkAndSetLeaf
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::yang::Translator::checkAndGet
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
isc::yang::Translator::findXPath
libyang::DataNode findXPath(std::string const &xpath) const
Retrieves a YANG data node by xpath.
Definition translator.cc:132
isc::yang::Translator::getMandatoryLeaf
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
isc::yang::Translator::checkAndGetLeaf
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
isc::yang::Translator::checkAndSetUserContext
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
isc::yang::Translator::setItem
void setItem(const std::string &xpath, isc::data::ConstElementPtr const elem, libyang::LeafBaseType const type)
Translate and set basic value from JSON to YANG.
Definition translator.cc:231
isc::yang::Translator::model_
std::string model_
The model.
Definition translator.h:427
isc::yang::Translator::checkAndGetAndJsonifyLeaf
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
isc::yang::Translator::checkAndSetLeafList
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
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
isc::data
Definition base_stamped_element.cc:12
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
isc::yang
Definition adaptor.cc:17
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
isc::yang::NetconfError
Generic NETCONF error.
Definition netconf_error.h:30
translator_shared_network.h
yang_models.h