Kea 2.2.0
translator_class.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
14using namespace std;
15using namespace isc::data;
16using namespace sysrepo;
17
18namespace isc {
19namespace yang {
20
21TranslatorClass::TranslatorClass(S_Session session, const string& model)
22 : TranslatorBasic(session, model),
23 TranslatorOptionData(session, model),
24 TranslatorOptionDataList(session, model),
25 TranslatorOptionDef(session, model),
26 TranslatorOptionDefList(session, model) {
27}
28
30}
31
33TranslatorClass::getClass(const string& xpath) {
34 try {
35 if ((model_ == KEA_DHCP4_SERVER) ||
36 (model_ == KEA_DHCP6_SERVER)) {
37 return (getClassKea(xpath));
38 }
39 } catch (const sysrepo_exception& ex) {
41 "sysrepo error getting client class at '" << xpath
42 << "': " << ex.what());
43 }
45 "getClass not implemented for the model: " << model_);
46}
47
49TranslatorClass::getClassKea(const string& xpath) {
50 ConstElementPtr name = getItem(xpath + "/name");
51 if (!name) {
52 // Can't happen as the name is the key.
53 isc_throw(Unexpected, "getClassKea requires name: " << xpath);
54 }
55 ElementPtr result = Element::createMap();
56 result->set("name", name);
57 ConstElementPtr test = getItem(xpath + "/test");
58 if (test) {
59 result->set("test", test);
60 }
61 ConstElementPtr required = getItem(xpath + "/only-if-required");
62 if (required) {
63 result->set("only-if-required", required);
64 }
65 ConstElementPtr options = getOptionDataList(xpath);
66 if (options && (options->size() > 0)) {
67 result->set("option-data", options);
68 }
69 checkAndGetLeaf(result, xpath, "valid-lifetime");
70 checkAndGetLeaf(result, xpath, "min-valid-lifetime");
71 checkAndGetLeaf(result, xpath, "max-valid-lifetime");
72 if (model_ == KEA_DHCP4_SERVER) {
74 if (defs && (defs->size() > 0)) {
75 result->set("option-def", defs);
76 }
77 ConstElementPtr next = getItem(xpath + "/next-server");
78 if (next) {
79 result->set("next-server", next);
80 }
81 ConstElementPtr hostname = getItem(xpath + "/server-hostname");
82 if (hostname) {
83 result->set("server-hostname", hostname);
84 }
85 ConstElementPtr boot = getItem(xpath + "/boot-file-name");
86 if (boot) {
87 result->set("boot-file-name", boot);
88 }
89 } else if (model_ == KEA_DHCP6_SERVER) {
90 checkAndGetLeaf(result, xpath, "preferred-lifetime");
91 checkAndGetLeaf(result, xpath, "min-preferred-lifetime");
92 checkAndGetLeaf(result, xpath, "max-preferred-lifetime");
93 }
94 ConstElementPtr context = getItem(xpath + "/user-context");
95 if (context) {
96 result->set("user-context", Element::fromJSON(context->stringValue()));
97 }
98 return (result);
99}
100
101void
102TranslatorClass::setClass(const string& xpath, ConstElementPtr elem) {
103 try {
104 if ((model_ == KEA_DHCP4_SERVER) ||
105 (model_ == KEA_DHCP6_SERVER)) {
106 setClassKea(xpath, elem);
107 } else {
109 "setClass not implemented for the model: " << model_);
110 }
111 } catch (const sysrepo_exception& ex) {
113 "sysrepo error setting client class '" << elem->str()
114 << "' at '" << xpath << "': " << ex.what());
115 }
116}
117
118void
120 bool created = false;
121 // Skip key name.
122 ConstElementPtr test = elem->get("test");
123 if (test) {
124 setItem(xpath + "/test", test, SR_STRING_T);
125 created = true;
126 }
127 ConstElementPtr required = elem->get("only-if-required");
128 if (required) {
129 setItem(xpath + "/only-if-required", required, SR_BOOL_T);
130 created = true;
131 }
132 ConstElementPtr options = elem->get("option-data");
133 if (options) {
134 setOptionDataList(xpath, options);
135 created = true;
136 }
137 checkAndSetLeaf(elem, xpath, "valid-lifetime", SR_UINT32_T);
138 checkAndSetLeaf(elem, xpath, "min-valid-lifetime", SR_UINT32_T);
139 checkAndSetLeaf(elem, xpath, "max-valid-lifetime", SR_UINT32_T);
140 if (model_ == KEA_DHCP4_SERVER) {
141 ConstElementPtr defs = elem->get("option-def");
142 if (defs) {
143 setOptionDefList(xpath, defs);
144 created = true;
145 }
146 ConstElementPtr next = elem->get("next-server");
147 if (next) {
148 setItem(xpath + "/next-server", next, SR_STRING_T);
149 created = true;
150 }
151 ConstElementPtr hostname = elem->get("server-hostname");
152 if (hostname) {
153 setItem(xpath + "/server-hostname", hostname, SR_STRING_T);
154 created = true;
155 }
156 ConstElementPtr boot = elem->get("boot-file-name");
157 if (boot) {
158 setItem(xpath + "/boot-file-name", boot, SR_STRING_T);
159 created = true;
160 }
161 } else if (model_ == KEA_DHCP6_SERVER) {
162 checkAndSetLeaf(elem, xpath, "preferred-lifetime", SR_UINT32_T);
163 checkAndSetLeaf(elem, xpath, "min-preferred-lifetime", SR_UINT32_T);
164 checkAndSetLeaf(elem, xpath, "max-preferred-lifetime", SR_UINT32_T);
165 }
166 ConstElementPtr context = Adaptor::getContext(elem);
167 if (context) {
168 setItem(xpath + "/user-context", Element::create(context->str()),
169 SR_STRING_T);
170 created = true;
171 }
172 // There is no mandatory fields outside the key so force creation.
173 if (!created) {
174 ConstElementPtr list = Element::createList();
175 setItem(xpath, list, SR_LIST_T);
176 }
177}
178
179TranslatorClasses::TranslatorClasses(S_Session session, const string& model)
180 : TranslatorBasic(session, model),
181 TranslatorOptionData(session, model),
182 TranslatorOptionDataList(session, model),
183 TranslatorOptionDef(session, model),
184 TranslatorOptionDefList(session, model),
185 TranslatorClass(session, model) {
186}
187
189}
190
192TranslatorClasses::getClasses(const string& xpath) {
193 try {
194 if ((model_ == KEA_DHCP4_SERVER) ||
195 (model_ == KEA_DHCP6_SERVER)) {
196 return (getClassesKea(xpath));
197 }
198 } catch (const sysrepo_exception& ex) {
200 "sysrepo error getting client classes at '" << xpath
201 << "': " << ex.what());
202 }
204 "getClasses not implemented for the model: " << model_);
205}
206
209 return getList<TranslatorClass>(xpath + "/client-class", *this,
211}
212
213void
215 try {
216 if ((model_ == KEA_DHCP4_SERVER) ||
217 (model_ == KEA_DHCP6_SERVER)) {
218 setClassesKea(xpath, elem);
219 } else {
221 "setClasses not implemented for the model: " << model_);
222 }
223 } catch (const sysrepo_exception& ex) {
225 "sysrepo error setting client classes '" << elem->str()
226 << "' at '" << xpath << "': " << ex.what());
227 }
228}
229
230void
232 for (size_t i = 0; i < elem->size(); ++i) {
233 ConstElementPtr cclass = elem->get(i);
234 if (!cclass->contains("name")) {
235 isc_throw(BadValue, "client class without name: " << elem->str());
236 }
237 string name = cclass->get("name")->stringValue();
238 ostringstream key;
239 key << xpath << "/client-class[name='" << name << "']";
240 setClass(key.str(), cclass);
241 }
242}
243
244} // namespace yang
245} // 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.
A generic exception that is thrown when an unexpected error condition occurs.
static isc::data::ConstElementPtr getContext(isc::data::ConstElementPtr parent)
Get user context.
Definition: adaptor.cc:27
Between YANG and JSON translator class for basic values.
Definition: translator.h:19
isc::data::ElementPtr getItem(const std::string &xpath)
Get and translate basic value from YANG to JSON.
Definition: translator.cc:105
std::string model_
The model.
Definition: translator.h:171
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
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
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
Client class translation between YANG and JSON.
TranslatorClass(sysrepo::S_Session session, const std::string &model)
Constructor.
isc::data::ElementPtr getClass(const std::string &xpath)
Get and translate a client class from YANG to JSON.
virtual ~TranslatorClass()
Destructor.
void setClassKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setClass for kea-dhcp[46].
void setClass(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set client class from JSON to YANG.
isc::data::ElementPtr getClassKea(const std::string &xpath)
getClass JSON for kea-dhcp[46].
TranslatorClasses(sysrepo::S_Session session, const std::string &model)
Constructor.
void setClassesKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setClasses for kea-dhcp[46].
isc::data::ElementPtr getClassesKea(const std::string &xpath)
getClasses JSON for kea-dhcp[46].
isc::data::ConstElementPtr getClasses(const std::string &xpath)
Get and translate client classes from YANG to JSON.
void setClasses(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set client classes from JSON to YANG.
virtual ~TranslatorClasses()
Destructor.
A translator class for converting an option data list between YANG and JSON.
isc::data::ConstElementPtr getOptionDataList(const std::string &xpath)
Get and translate option data list from YANG to JSON.
void setOptionDataList(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set option data list from JSON to YANG.
Option data translation between YANG and JSON.
Currently supports kea-dhcp[46]-server models.
void setOptionDefList(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set option definition list from JSON to YANG.
isc::data::ConstElementPtr getOptionDefList(const std::string &xpath)
Get and translate option definition list from YANG to JSON.
Option definition translation between YANG and JSON.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
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.