Kea 1.5.0
translator_class.cc
Go to the documentation of this file.
1// Copyright (C) 2018 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;
16#ifndef HAVE_PRE_0_7_6_SYSREPO
17using namespace sysrepo;
18#endif
19
20namespace isc {
21namespace yang {
22
23TranslatorClass::TranslatorClass(S_Session session, const string& model)
24 : TranslatorBasic(session, model),
25 TranslatorOptionData(session, model),
26 TranslatorOptionDataList(session, model),
27 TranslatorOptionDef(session, model),
28 TranslatorOptionDefList(session, model) {
29}
30
32}
33
35TranslatorClass::getClass(const string& xpath) {
36 try {
37 if ((model_ == KEA_DHCP4_SERVER) ||
38 (model_ == KEA_DHCP6_SERVER)) {
39 return (getClassKea(xpath));
40 }
41 } catch (const sysrepo_exception& ex) {
43 "sysrepo error getting client class at '" << xpath
44 << "': " << ex.what());
45 }
47 "getClass not implemented for the model: " << model_);
48}
49
51TranslatorClass::getClassKea(const string& xpath) {
52 ConstElementPtr name = getItem(xpath + "/name");
53 if (!name) {
54 // Can't happen as the name is the key.
55 isc_throw(Unexpected, "getClassKea requires name: " << xpath);
56 }
58 result->set("name", name);
59 ConstElementPtr test = getItem(xpath + "/test");
60 if (test) {
61 result->set("test", test);
62 }
63 ConstElementPtr required = getItem(xpath + "/only-if-required");
64 if (required) {
65 result->set("only-if-required", required);
66 }
67 ConstElementPtr options = getOptionDataList(xpath);
68 if (options && (options->size() > 0)) {
69 result->set("option-data", options);
70 }
71 if (model_ == KEA_DHCP4_SERVER) {
73 if (defs && (defs->size() > 0)) {
74 result->set("option-def", defs);
75 }
76 ConstElementPtr next = getItem(xpath + "/next-server");
77 if (next) {
78 result->set("next-server", next);
79 }
80 ConstElementPtr hostname = getItem(xpath + "/server-hostname");
81 if (hostname) {
82 result->set("server-hostname", hostname);
83 }
84 ConstElementPtr boot = getItem(xpath + "/boot-file-name");
85 if (boot) {
86 result->set("boot-file-name", boot);
87 }
88 }
89 ConstElementPtr context = getItem(xpath + "/user-context");
90 if (context) {
91 result->set("user-context", Element::fromJSON(context->stringValue()));
92 }
93 return (result);
94}
95
96void
97TranslatorClass::setClass(const string& xpath, ConstElementPtr elem) {
98 try {
99 if ((model_ == KEA_DHCP4_SERVER) ||
100 (model_ == KEA_DHCP6_SERVER)) {
101 setClassKea(xpath, elem);
102 } else {
104 "setClass not implemented for the model: " << model_);
105 }
106 } catch (const sysrepo_exception& ex) {
108 "sysrepo error setting client class '" << elem->str()
109 << "' at '" << xpath << "': " << ex.what());
110 }
111}
112
113void
115 bool created = false;
116 // Skip key name.
117 ConstElementPtr test = elem->get("test");
118 if (test) {
119 setItem(xpath + "/test", test, SR_STRING_T);
120 created = true;
121 }
122 ConstElementPtr required = elem->get("only-if-required");
123 if (required) {
124 setItem(xpath + "/only-if-required", required, SR_BOOL_T);
125 created = true;
126 }
127 ConstElementPtr options = elem->get("option-data");
128 if (options) {
129 setOptionDataList(xpath, options);
130 created = true;
131 }
132 if (model_ == KEA_DHCP4_SERVER) {
133 ConstElementPtr defs = elem->get("option-def");
134 if (defs) {
135 setOptionDefList(xpath, defs);
136 created = true;
137 }
138 ConstElementPtr next = elem->get("next-server");
139 if (next) {
140 setItem(xpath + "/next-server", next, SR_STRING_T);
141 created = true;
142 }
143 ConstElementPtr hostname = elem->get("server-hostname");
144 if (hostname) {
145 setItem(xpath + "/server-hostname", hostname, SR_STRING_T);
146 created = true;
147 }
148 ConstElementPtr boot = elem->get("boot-file-name");
149 if (boot) {
150 setItem(xpath + "/boot-file-name", boot, SR_STRING_T);
151 created = true;
152 }
153 }
154 ConstElementPtr context = Adaptor::getContext(elem);
155 if (context) {
156 setItem(xpath + "/user-context", Element::create(context->str()),
157 SR_STRING_T);
158 created = true;
159 }
160 // There is no mandatory fields outside the key so force creation.
161 if (!created) {
163 setItem(xpath, list, SR_LIST_T);
164 }
165}
166
167TranslatorClasses::TranslatorClasses(S_Session session, const string& model)
168 : TranslatorBasic(session, model),
169 TranslatorOptionData(session, model),
170 TranslatorOptionDataList(session, model),
171 TranslatorOptionDef(session, model),
172 TranslatorOptionDefList(session, model),
173 TranslatorClass(session, model) {
174}
175
177}
178
180TranslatorClasses::getClasses(const string& xpath) {
181 try {
182 if ((model_ == KEA_DHCP4_SERVER) ||
183 (model_ == KEA_DHCP6_SERVER)) {
184 return (getClassesKea(xpath));
185 }
186 } catch (const sysrepo_exception& ex) {
188 "sysrepo error getting client classes at '" << xpath
189 << "': " << ex.what());
190 }
192 "getClasses not implemented for the model: " << model_);
193}
194
197 S_Iter_Value iter = getIter(xpath + "/client-class");
198 if (!iter) {
199 // Can't happen.
200 isc_throw(Unexpected, "getClassesKea: can't get iterator: " << xpath);
201 }
203 for (;;) {
204 const string& cclass = getNext(iter);
205 if (cclass.empty()) {
206 break;
207 }
208 result->add(getClass(cclass));
209 }
210 if (result->size() > 0) {
211 return (result);
212 } else {
213 return (ElementPtr());
214 }
215}
216
217void
219 try {
220 if ((model_ == KEA_DHCP4_SERVER) ||
221 (model_ == KEA_DHCP6_SERVER)) {
222 setClassesKea(xpath, elem);
223 } else {
225 "setClasses not implemented for the model: " << model_);
226 }
227 } catch (const sysrepo_exception& ex) {
229 "sysrepo error setting client classes '" << elem->str()
230 << "' at '" << xpath << "': " << ex.what());
231 }
232}
233
234void
236 for (size_t i = 0; i < elem->size(); ++i) {
237 ConstElementPtr cclass = elem->get(i);
238 if (!cclass->contains("name")) {
239 isc_throw(BadValue, "client class without name: " << elem->str());
240 }
241 string name = cclass->get("name")->stringValue();
242 ostringstream key;
243 key << xpath << "/client-class[name='" << name << "']";
244 setClass(key.str(), cclass);
245 }
246}
247
248}; // end of namespace isc::yang
249}; // end of 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 ElementPtr create(const Position &pos=ZERO_POSITION())
Definition: data.cc:223
static ElementPtr fromJSON(const std::string &in, bool preproc=false)
These functions will parse the given string (JSON) representation of a compound element.
Definition: data.cc:750
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition: data.cc:268
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition: data.cc:263
static isc::data::ConstElementPtr getContext(isc::data::ConstElementPtr parent)
Get user context.
Definition: adaptor.cc:25
Between YANG and JSON translator class for basic values.
Definition: translator.h:27
std::string getNext(sysrepo::S_Iter_Value iter)
Get xpath of the next YANG list item.
Definition: translator.cc:283
isc::data::ElementPtr getItem(const std::string &xpath)
Get and translate basic value from YANG to JSON.
Definition: translator.cc:104
sysrepo::S_Iter_Value getIter(const std::string &xpath)
List iterator methods keeping the session private.
Definition: translator.cc:278
std::string model_
The model.
Definition: translator.h:132
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:250
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:23
boost::shared_ptr< Element > ElementPtr
Definition: data.h:22
Defines the logger used by the top-level component of kea-dhcp-ddns.