Kea 2.7.5
translator_class.cc
Go to the documentation of this file.
1// Copyright (C) 2018-2023 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
22TranslatorClass::TranslatorClass(Session session, const string& model)
23 : Translator(session, model),
24 TranslatorOptionData(session, model),
25 TranslatorOptionDataList(session, model),
26 TranslatorOptionDef(session, model),
27 TranslatorOptionDefList(session, model) {
28}
29
31TranslatorClass::getClass(DataNode const& data_node) {
32 try {
33 if ((model_ == KEA_DHCP4_SERVER) ||
34 (model_ == KEA_DHCP6_SERVER)) {
35 return (getClassKea(data_node));
36 }
37 } catch (Error const& ex) {
39 "getting client class:"
40 << ex.what());
41 }
43 "getClass not implemented for the model: " << model_);
44}
45
48 try {
49 return getClass(findXPath(xpath));
50 } catch (NetconfError const&) {
51 return ElementPtr();
52 }
53}
54
56TranslatorClass::getClassKea(DataNode const& data_node) {
58
59 getMandatoryLeaf(result, data_node, "name");
60
61 checkAndGetLeaf(result, data_node, "max-valid-lifetime");
62 checkAndGetLeaf(result, data_node, "min-valid-lifetime");
63 checkAndGetLeaf(result, data_node, "only-if-required");
64 checkAndGetLeaf(result, data_node, "template-test");
65 checkAndGetLeaf(result, data_node, "test");
66 checkAndGetLeaf(result, data_node, "valid-lifetime");
67
68 checkAndGetAndJsonifyLeaf(result, data_node, "user-context");
69
70 ConstElementPtr options = getOptionDataList(data_node);
71 if (options) {
72 result->set("option-data", options);
73 }
74
75 if (model_ == KEA_DHCP4_SERVER) {
76 checkAndGetLeaf(result, data_node, "boot-file-name");
77 checkAndGetLeaf(result, data_node, "next-server");
78 checkAndGetLeaf(result, data_node, "offer-lifetime");
79 checkAndGetLeaf(result, data_node, "server-hostname");
80
81 ConstElementPtr defs = getOptionDefList(data_node);
82 if (defs) {
83 result->set("option-def", defs);
84 }
85 } else if (model_ == KEA_DHCP6_SERVER) {
86 checkAndGetLeaf(result, data_node, "max-preferred-lifetime");
87 checkAndGetLeaf(result, data_node, "min-preferred-lifetime");
88 checkAndGetLeaf(result, data_node, "preferred-lifetime");
89 }
90
91 return (result->empty() ? ElementPtr() : result);
92}
93
94void
95TranslatorClass::setClass(string const& xpath, ConstElementPtr elem) {
96 try {
97 if ((model_ == KEA_DHCP4_SERVER) ||
98 (model_ == KEA_DHCP6_SERVER)) {
99 setClassKea(xpath, elem);
100 } else {
102 "setClass not implemented for the model: " << model_);
103 }
104 } catch (Error const& ex) {
106 "setting client class '" << elem->str()
107 << "' : " << ex.what());
108 }
109}
110
111void
113 // Keys are set by setting the list itself.
114 setItem(xpath, ElementPtr(), LeafBaseType::Unknown);
115
116 checkAndSetLeaf(elem, xpath, "max-valid-lifetime", LeafBaseType::Uint32);
117 checkAndSetLeaf(elem, xpath, "min-valid-lifetime", LeafBaseType::Uint32);
118 checkAndSetLeaf(elem, xpath, "only-if-required", LeafBaseType::Bool);
119 checkAndSetLeaf(elem, xpath, "template-test", LeafBaseType::String);
120 checkAndSetLeaf(elem, xpath, "test", LeafBaseType::String);
121 checkAndSetLeaf(elem, xpath, "valid-lifetime", LeafBaseType::Uint32);
122
123 ConstElementPtr options = elem->get("option-data");
124 if (options) {
125 setOptionDataList(xpath, options);
126 }
127
128 if (model_ == KEA_DHCP4_SERVER) {
129 checkAndSetLeaf(elem, xpath, "boot-file-name", LeafBaseType::String);
130 checkAndSetLeaf(elem, xpath, "next-server", LeafBaseType::String);
131 checkAndSetLeaf(elem, xpath, "offer-lifetime", LeafBaseType::Uint32);
132 checkAndSetLeaf(elem, xpath, "server-hostname", LeafBaseType::String);
133
134 ConstElementPtr defs = elem->get("option-def");
135 if (defs) {
136 setOptionDefList(xpath, defs);
137 }
138 } else if (model_ == KEA_DHCP6_SERVER) {
139 checkAndSetLeaf(elem, xpath, "max-preferred-lifetime", LeafBaseType::Uint32);
140 checkAndSetLeaf(elem, xpath, "min-preferred-lifetime", LeafBaseType::Uint32);
141 checkAndSetLeaf(elem, xpath, "preferred-lifetime", LeafBaseType::Uint32);
142 }
143
144 checkAndSetUserContext(elem, xpath);
145}
146
147TranslatorClasses::TranslatorClasses(Session session, const string& model)
148 : Translator(session, model),
149 TranslatorOptionData(session, model),
150 TranslatorOptionDataList(session, model),
151 TranslatorOptionDef(session, model),
152 TranslatorOptionDefList(session, model),
153 TranslatorClass(session, model) {
154}
155
157TranslatorClasses::getClasses(DataNode const& data_node) {
158 try {
159 if ((model_ == KEA_DHCP4_SERVER) ||
160 (model_ == KEA_DHCP6_SERVER)) {
161 return (getClassesKea(data_node));
162 }
163 } catch (Error const& ex) {
165 "getting client classes:"
166 << ex.what());
167 }
169 "getClasses not implemented for the model: " << model_);
170}
171
174 try {
175 return getClasses(findXPath(xpath));
176 } catch (NetconfError const&) {
177 return ElementPtr();
178 }
179}
180
182TranslatorClasses::getClassesKea(DataNode const& data_node) {
183 return getList<TranslatorClass>(data_node, "client-class", *this,
185}
186
187void
189 try {
190 if ((model_ == KEA_DHCP4_SERVER) ||
191 (model_ == KEA_DHCP6_SERVER)) {
192 setClassesKea(xpath, elem);
193 } else {
195 "setClasses not implemented for the model: " << model_);
196 }
197 } catch (Error const& ex) {
199 "setting client classes '" << elem->str()
200 << "' : " << ex.what());
201 }
202}
203
204void
206 for (size_t i = 0; i < elem->size(); ++i) {
207 ElementPtr cclass = elem->getNonConst(i);
208 if (!cclass->contains("name")) {
209 isc_throw(BadValue, "client class without name: " << elem->str());
210 }
211 string name = cclass->get("name")->stringValue();
212 ostringstream key;
213 key << xpath << "/client-class[name='" << name << "']";
214 setClass(key.str(), cclass);
215 }
216}
217
218} // namespace yang
219} // 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
Client class translation between YANG and JSON.
TranslatorClass(sysrepo::Session session, const std::string &model)
Constructor.
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 getClassFromAbsoluteXpath(std::string const &xpath)
Translate a client class from YANG to JSON.
isc::data::ElementPtr getClass(libyang::DataNode const &data_node)
Translate a client class from YANG to JSON.
isc::data::ElementPtr getClassKea(libyang::DataNode const &data_node)
getClass JSON for kea-dhcp[46].
isc::data::ElementPtr getClassesKea(libyang::DataNode const &data_node)
getClasses JSON for kea-dhcp[46].
isc::data::ElementPtr getClasses(libyang::DataNode const &data_node)
Translate client classes from YANG to JSON.
void setClassesKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setClasses for kea-dhcp[46].
isc::data::ElementPtr getClassesFromAbsoluteXpath(std::string const &xpath)
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.
TranslatorClasses(sysrepo::Session session, const std::string &model)
Constructor.
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.
Currently supports kea-dhcp[46]-server models.
isc::data::ConstElementPtr getOptionDefList(libyang::DataNode const &data_node)
Translate option definition list from YANG to JSON.
void setOptionDefList(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set option definition list from JSON to YANG.
Option definition translation between YANG and 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
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
#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.