Kea  2.1.7-git
translator_shared_network.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 TranslatorSharedNetwork::TranslatorSharedNetwork(S_Session session,
22  const string& model)
23  : TranslatorBasic(session, model),
24  TranslatorOptionData(session, model),
25  TranslatorOptionDataList(session, model),
26  TranslatorPool(session, model),
27  TranslatorPools(session, model),
28  TranslatorPdPool(session, model),
29  TranslatorPdPools(session, model),
30  TranslatorHost(session, model),
31  TranslatorHosts(session, model),
32  TranslatorSubnet(session, model),
33  TranslatorSubnets(session, model) {
34 }
35 
37 }
38 
41  try {
42  if (model_ == KEA_DHCP4_SERVER) {
43  return (getSharedNetworkKea(xpath, "subnet4"));
44  } else if (model_ == KEA_DHCP6_SERVER) {
45  return (getSharedNetworkKea(xpath, "subnet6"));
46  }
47  } catch (const sysrepo_exception& ex) {
49  "sysrepo error getting shared network at '" << xpath
50  << "': " << ex.what());
51  }
53  "getSharedNetwork not implemented for the model: " << model_);
54 }
55 
58  const std::string& subsel) {
59  ElementPtr result = Element::createMap();
60  ConstElementPtr name = getItem(xpath + "/name");
61  if (!name) {
62  // Can't happen as the name is the key.
63  isc_throw(Unexpected, "getSharedNetworkKea requires name: " << xpath);
64  }
65  result->set("name", name);
66  ConstElementPtr subnets = getSubnets(xpath);
67  if (subnets && (subnets->size() > 0)) {
68  result->set(subsel, subnets);
69  }
70  if (subsel == "subnet6") {
71  ConstElementPtr preferred = getItem(xpath + "/preferred-lifetime");
72  if (preferred) {
73  result->set("preferred-lifetime", preferred);
74  }
75  ConstElementPtr min_pref = getItem(xpath + "/min-preferred-lifetime");
76  if (min_pref) {
77  result->set("min-preferred-lifetime", min_pref);
78  }
79  ConstElementPtr max_pref = getItem(xpath + "/max-preferred-lifetime");
80  if (max_pref) {
81  result->set("max-preferred-lifetime", max_pref);
82  }
83  }
84  ConstElementPtr valid = getItem(xpath + "/valid-lifetime");
85  if (valid) {
86  result->set("valid-lifetime", valid);
87  }
88  ConstElementPtr min_valid = getItem(xpath + "/min-valid-lifetime");
89  if (min_valid) {
90  result->set("min-valid-lifetime", min_valid);
91  }
92  ConstElementPtr max_valid = getItem(xpath + "/max-valid-lifetime");
93  if (max_valid) {
94  result->set("max-valid-lifetime", max_valid);
95  }
96  ConstElementPtr renew = getItem(xpath + "/renew-timer");
97  if (renew) {
98  result->set("renew-timer", renew);
99  }
100  ConstElementPtr rebind = getItem(xpath + "/rebind-timer");
101  if (rebind) {
102  result->set("rebind-timer", rebind);
103  }
104  ConstElementPtr calculate = getItem(xpath + "/calculate-tee-times");
105  if (calculate) {
106  result->set("calculate-tee-times", calculate);
107  }
108  ConstElementPtr t1_percent = getItem(xpath + "/t1-percent");
109  if (t1_percent) {
110  result->set("t1-percent", t1_percent);
111  }
112  ConstElementPtr t2_percent = getItem(xpath + "/t2-percent");
113  if (t2_percent) {
114  result->set("t2-percent", t2_percent);
115  }
116  ConstElementPtr options = getOptionDataList(xpath);
117  if (options && (options->size() > 0)) {
118  result->set("option-data", options);
119  }
120  ConstElementPtr interface = getItem(xpath + "/interface");
121  if (interface) {
122  result->set("interface", interface);
123  }
124  if (subsel == "subnet6") {
125  ConstElementPtr interface_id = getItem(xpath + "/interface-id");
126  if (interface_id) {
127  result->set("interface-id", interface_id);
128  }
129  ConstElementPtr rapid_commit = getItem(xpath + "/rapid-commit");
130  if (rapid_commit) {
131  result->set("rapid-commit", rapid_commit);
132  }
133  }
134  ConstElementPtr guard = getItem(xpath + "/client-class");
135  if (guard) {
136  result->set("client-class", guard);
137  }
138  ConstElementPtr required = getItems(xpath + "/require-client-classes");
139  if (required && (required->size() > 0)) {
140  result->set("require-client-classes", required);
141  }
142  ConstElementPtr mode = getItem(xpath + "/reservation-mode");
143  if (mode) {
144  result->set("reservation-mode", mode);
145  }
146  ConstElementPtr relay = getItems(xpath + "/relay/ip-addresses");
147  if (relay && (relay->size() > 0)) {
148  ElementPtr relay_map = Element::createMap();
149  relay_map->set("ip-addresses", relay);
150  result->set("relay", relay_map);
151  }
152  if (subsel == "subnet4") {
153  ConstElementPtr match = getItem(xpath + "/match-client-id");
154  if (match) {
155  result->set("match-client-id", match);
156  }
157  ConstElementPtr auth = getItem(xpath + "/authoritative");
158  if (auth) {
159  result->set("authoritative", auth);
160  }
161  ConstElementPtr next = getItem(xpath + "/next-server");
162  if (next) {
163  result->set("next-server", next);
164  }
165  ConstElementPtr hostname = getItem(xpath + "/server-hostname");
166  if (hostname) {
167  result->set("server-hostname", hostname);
168  }
169  ConstElementPtr boot = getItem(xpath + "/boot-file-name");
170  if (boot) {
171  result->set("boot-file-name", boot);
172  }
173  }
174  ConstElementPtr context = getItem(xpath + "/user-context");
175  if (context) {
176  result->set("user-context", Element::fromJSON(context->stringValue()));
177  }
178  checkAndGetLeaf(result, xpath, "cache-max-age");
179  checkAndGetLeaf(result, xpath, "cache-threshold");
180  checkAndGetLeaf(result, xpath, "ddns-generated-prefix");
181  checkAndGetLeaf(result, xpath, "ddns-override-client-update");
182  checkAndGetLeaf(result, xpath, "ddns-override-no-update");
183  checkAndGetLeaf(result, xpath, "ddns-qualifying-suffix");
184  checkAndGetLeaf(result, xpath, "ddns-replace-client-name");
185  checkAndGetLeaf(result, xpath, "ddns-send-updates");
186  checkAndGetLeaf(result, xpath, "ddns-update-on-renew");
187  checkAndGetLeaf(result, xpath, "ddns-use-conflict-resolution");
188  checkAndGetLeaf(result, xpath, "hostname-char-replacement");
189  checkAndGetLeaf(result, xpath, "hostname-char-set");
190  checkAndGetLeaf(result, xpath, "reservations-global");
191  checkAndGetLeaf(result, xpath, "reservations-in-subnet");
192  checkAndGetLeaf(result, xpath, "reservations-out-of-pool");
193  checkAndGetLeaf(result, xpath, "store-extended-info");
194  return (result);
195 }
196 
197 void
199  ConstElementPtr elem) {
200  try {
201  if (model_ == KEA_DHCP4_SERVER) {
202  setSharedNetworkKea(xpath, elem, "subnet4");
203  } else if (model_ == KEA_DHCP6_SERVER) {
204  setSharedNetworkKea(xpath, elem, "subnet6");
205  } else {
207  "setSharedNetwork not implemented for the model: "
208  << model_);
209  }
210  } catch (const sysrepo_exception& ex) {
212  "sysrepo error setting shared network '" << elem->str()
213  << "' at '" << xpath << "': " << ex.what());
214  }
215 }
216 
217 void
219  ConstElementPtr elem,
220  const std::string& subsel) {
221  // Skip name which is the key.
222  ConstElementPtr subnets = elem->get(subsel);
223  if (subnets && (subnets->size() > 0)) {
224  setSubnets(xpath, subnets);
225  }
226  if (subsel == "subnet6") {
227  ConstElementPtr preferred = elem->get("preferred-lifetime");
228  if (preferred) {
229  setItem(xpath + "/preferred-lifetime", preferred, SR_UINT32_T);
230  }
231  ConstElementPtr min_pref = elem->get("min-preferred-lifetime");
232  if (min_pref) {
233  setItem(xpath + "/min-preferred-lifetime", min_pref, SR_UINT32_T);
234  }
235  ConstElementPtr max_pref = elem->get("max-preferred-lifetime");
236  if (max_pref) {
237  setItem(xpath + "/max-preferred-lifetime", max_pref, SR_UINT32_T);
238  }
239  }
240  ConstElementPtr valid = elem->get("valid-lifetime");
241  if (valid) {
242  setItem(xpath + "/valid-lifetime", valid, SR_UINT32_T);
243  }
244  ConstElementPtr min_valid = elem->get("min-valid-lifetime");
245  if (min_valid) {
246  setItem(xpath + "/min-valid-lifetime", min_valid, SR_UINT32_T);
247  }
248  ConstElementPtr max_valid = elem->get("max-valid-lifetime");
249  if (max_valid) {
250  setItem(xpath + "/max-valid-lifetime", max_valid, SR_UINT32_T);
251  }
252  ConstElementPtr renew = elem->get("renew-timer");
253  if (renew) {
254  setItem(xpath + "/renew-timer", renew, SR_UINT32_T);
255  }
256  ConstElementPtr rebind = elem->get("rebind-timer");
257  if (rebind) {
258  setItem(xpath + "/rebind-timer", rebind, SR_UINT32_T);
259  }
260  ConstElementPtr calculate = elem->get("calculate-tee-times");
261  if (calculate) {
262  setItem(xpath + "/calculate-tee-times", calculate, SR_BOOL_T);
263  }
264  ConstElementPtr t1_percent = elem->get("t1-percent");
265  if (t1_percent) {
266  setItem(xpath + "/t1-percent", t1_percent, SR_DECIMAL64_T);
267  }
268  ConstElementPtr t2_percent = elem->get("t2-percent");
269  if (t2_percent) {
270  setItem(xpath + "/t2-percent", t2_percent, SR_DECIMAL64_T);
271  }
272  ConstElementPtr options = elem->get("option-data");
273  if (options && (options->size() > 0)) {
274  setOptionDataList(xpath, options);
275  }
276  ConstElementPtr interface = elem->get("interface");
277  if (interface) {
278  setItem(xpath + "/interface", interface, SR_STRING_T);
279  }
280  if (subsel == "subnet6") {
281  ConstElementPtr interface_id = elem->get("interface-id");
282  if (interface_id) {
283  setItem(xpath + "/interface-id", interface_id, SR_STRING_T);
284  }
285  ConstElementPtr rapid_commit = elem->get("rapid-commit");
286  if (rapid_commit) {
287  setItem(xpath + "/rapid-commit", rapid_commit, SR_BOOL_T);
288  }
289  }
290  ConstElementPtr guard = elem->get("client-class");
291  if (guard) {
292  setItem(xpath + "/client-class", guard, SR_STRING_T);
293  }
294  ConstElementPtr required = elem->get("require-client-classes");
295  if (required && (required->size() > 0)) {
296  for (ConstElementPtr rclass : required->listValue()) {
297  setItem(xpath + "/require-client-classes", rclass, SR_STRING_T);
298  }
299  }
300  ConstElementPtr mode = elem->get("reservation-mode");
301  if (mode) {
302  setItem(xpath + "/reservation-mode", mode, SR_ENUM_T);
303  }
304  ConstElementPtr relay = elem->get("relay");
305  if (relay) {
306  ConstElementPtr address = relay->get("ip-address");
307  ConstElementPtr addresses = relay->get("ip-addresses");
308  if (address) {
309  setItem(xpath + "/relay/ip-addresses", address, SR_STRING_T);
310  } else if (addresses && (addresses->size() > 0)) {
311  for (ConstElementPtr addr : addresses->listValue()) {
312  setItem(xpath + "/relay/ip-addresses", addr, SR_STRING_T);
313  }
314  }
315  }
316  if (subsel == "subnet4") {
317  ConstElementPtr match = elem->get("match-client-id");
318  if (match) {
319  setItem(xpath + "/match-client-id", match, SR_BOOL_T);
320  }
321  ConstElementPtr auth = elem->get("authoritative");
322  if (auth) {
323  setItem(xpath + "/authoritative", auth, SR_BOOL_T);
324  }
325  ConstElementPtr next = elem->get("next-server");
326  if (next) {
327  setItem(xpath + "/next-server", next, SR_STRING_T);
328  }
329  ConstElementPtr hostname = elem->get("server-hostname");
330  if (hostname) {
331  setItem(xpath + "/server-hostname", hostname, SR_STRING_T);
332  }
333  ConstElementPtr boot = elem->get("boot-file-name");
334  if (boot) {
335  setItem(xpath + "/boot-file-name", boot, SR_STRING_T);
336  }
337  }
338  ConstElementPtr context = Adaptor::getContext(elem);
339  if (context) {
340  ConstElementPtr repr = Element::create(context->str());
341  setItem(xpath + "/user-context", repr, SR_STRING_T);
342  }
343  checkAndSetLeaf(elem, xpath, "cache-max-age", SR_UINT32_T);
344  checkAndSetLeaf(elem, xpath, "cache-threshold", SR_DECIMAL64_T);
345  checkAndSetLeaf(elem, xpath, "ddns-generated-prefix", SR_STRING_T);
346  checkAndSetLeaf(elem, xpath, "ddns-override-client-update", SR_BOOL_T);
347  checkAndSetLeaf(elem, xpath, "ddns-override-no-update", SR_BOOL_T);
348  checkAndSetLeaf(elem, xpath, "ddns-qualifying-suffix", SR_STRING_T);
349  checkAndSetLeaf(elem, xpath, "ddns-replace-client-name", SR_STRING_T);
350  checkAndSetLeaf(elem, xpath, "ddns-send-updates", SR_BOOL_T);
351  checkAndSetLeaf(elem, xpath, "ddns-update-on-renew", SR_BOOL_T);
352  checkAndSetLeaf(elem, xpath, "ddns-use-conflict-resolution", SR_BOOL_T);
353  checkAndSetLeaf(elem, xpath, "hostname-char-replacement", SR_STRING_T);
354  checkAndSetLeaf(elem, xpath, "hostname-char-set", SR_STRING_T);
355  checkAndSetLeaf(elem, xpath, "reservations-global", SR_BOOL_T);
356  checkAndSetLeaf(elem, xpath, "reservations-in-subnet", SR_BOOL_T);
357  checkAndSetLeaf(elem, xpath, "reservations-out-of-pool", SR_BOOL_T);
358  checkAndSetLeaf(elem, xpath, "store-extended-info", SR_BOOL_T);
359 }
360 
362  const string& model)
363  : TranslatorBasic(session, model),
364  TranslatorOptionData(session, model),
365  TranslatorOptionDataList(session, model),
366  TranslatorPool(session, model),
367  TranslatorPools(session, model),
368  TranslatorPdPool(session, model),
369  TranslatorPdPools(session, model),
370  TranslatorHost(session, model),
371  TranslatorHosts(session, model),
372  TranslatorSubnet(session, model),
373  TranslatorSubnets(session, model),
374  TranslatorSharedNetwork(session, model) {
375 }
376 
378 }
379 
382  return getList<TranslatorSharedNetwork>(xpath + "/shared-network", *this,
384 }
385 
386 void
388  ConstElementPtr elem) {
389  try {
390  if ((model_ == KEA_DHCP4_SERVER) ||
391  (model_ == KEA_DHCP6_SERVER)) {
392  setSharedNetworksKea(xpath, elem);
393  } else {
395  "setSharedNetworks not implemented for the model: "
396  << model_);
397  }
398  } catch (const sysrepo_exception& ex) {
400  "sysrepo error setting shared networks '" << elem->str()
401  << "' at '" << xpath << "': " << ex.what());
402  }
403 }
404 
405 void
407  ConstElementPtr elem) {
408  for (size_t i = 0; i < elem->size(); ++i) {
409  ConstElementPtr network = elem->get(i);
410  if (!network->contains("name")) {
411  isc_throw(BadValue, "setSharedNetworksKea requires name: "
412  << network->str());
413  }
414  string name = network->get("name")->stringValue();
415  ostringstream key;
416  key<< xpath << "/shared-network[name='" << name << "']";
417  setSharedNetwork(key.str(), network);
418  }
419 }
420 
421 } // namespace yang
422 } // namespace isc
isc::data::ElementPtr getItems(const std::string &xpath)
Get and translate a list of basic values from YANG to JSON.
Definition: translator.cc:124
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.
void checkAndSetLeaf(isc::data::ConstElementPtr const &from, std::string const &xpath, std::string const &name, sr_type_t const type)
Get an element from given ElementPtr node and set it in sysrepo at given xpath.
Definition: translator.cc:283
void setSharedNetworkKea(const std::string &xpath, isc::data::ConstElementPtr elem, const std::string &subsel)
setSharedNetwork for kea-dhcp4-server and kea-dhcp6-server models
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
isc::data::ElementPtr getSharedNetwork(const std::string &xpath)
Get and translate a shared network from YANG to JSON.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
STL namespace.
A translator class for converting a pd-pool list between YANG and JSON.
isc::data::ElementPtr getSharedNetworks(const std::string &xpath)
Get and translate shared networks from YANG to JSON.
TranslatorSharedNetworks(sysrepo::S_Session session, const std::string &model)
Constructor.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Shared network translation between YANG and JSON.
void setSharedNetworksKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setSharedNetworks for kea-dhcp4-server and kea-dhcp6-server
#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...
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
Translation between YANG and JSON for a single host reservation.
A translator class for converting an option data list between YANG and JSON.
A generic exception that is thrown when an unexpected error condition occurs.
isc::data::ElementPtr getSubnets(const std::string &xpath)
Get and translate subnets from YANG to JSON.
std::string model_
The model.
Definition: translator.h:171
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
A translator class for converting a subnet list between YANG and JSON.
Defines the logger used by the top-level component of kea-lfc.
isc::data::ConstElementPtr getOptionDataList(const std::string &xpath)
Get and translate option data list from YANG to JSON.
void setSharedNetwork(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set shared network from JSON to YANG.
isc::data::ElementPtr getItem(const std::string &xpath)
Get and translate basic value from YANG to JSON.
Definition: translator.cc:105
Prefix delegation pool translation between YANG and JSON.
A translator class for converting a pool between YANG and JSON.
void checkAndGetLeaf(isc::data::ElementPtr &storage, const std::string &xpath, const std::string &name)
Retrieves an item and stores it in the specified storage.
Definition: translator.cc:274
A translator class for converting pools between YANG and JSON.
void setSubnets(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set subnets from JSON to YANG.
Option data translation 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::ElementPtr getSharedNetworkKea(const std::string &xpath, const std::string &subsel)
getSharedNetwork for kea-dhcp4-server and kea-dhcp6-server models
A translator class for converting host reservations list between YANG and JSON.