Kea 3.1.1
translator_class.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
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, "only-in-additional-list");
65 checkAndGetLeaf(result, data_node, "template-test");
66 checkAndGetLeaf(result, data_node, "test");
67 checkAndGetLeaf(result, data_node, "valid-lifetime");
68
69 checkAndGetAndJsonifyLeaf(result, data_node, "user-context");
70
71 ConstElementPtr options = getOptionDataList(data_node);
72 if (options) {
73 result->set("option-data", options);
74 }
75
76 if (model_ == KEA_DHCP4_SERVER) {
77 checkAndGetLeaf(result, data_node, "boot-file-name");
78 checkAndGetLeaf(result, data_node, "next-server");
79 checkAndGetLeaf(result, data_node, "offer-lifetime");
80 checkAndGetLeaf(result, data_node, "server-hostname");
81
82 ConstElementPtr defs = getOptionDefList(data_node);
83 if (defs) {
84 result->set("option-def", defs);
85 }
86 } else if (model_ == KEA_DHCP6_SERVER) {
87 checkAndGetLeaf(result, data_node, "max-preferred-lifetime");
88 checkAndGetLeaf(result, data_node, "min-preferred-lifetime");
89 checkAndGetLeaf(result, data_node, "preferred-lifetime");
90 }
91
92 return (result->empty() ? ElementPtr() : result);
93}
94
95void
96TranslatorClass::setClass(string const& xpath, ConstElementPtr elem) {
97 try {
98 if ((model_ == KEA_DHCP4_SERVER) ||
99 (model_ == KEA_DHCP6_SERVER)) {
100 setClassKea(xpath, elem);
101 } else {
103 "setClass not implemented for the model: " << model_);
104 }
105 } catch (Error const& ex) {
107 "setting client class '" << elem->str()
108 << "' : " << ex.what());
109 }
110}
111
112void
114 // Keys are set by setting the list itself.
115 setItem(xpath, ElementPtr(), LeafBaseType::Unknown);
116
117 checkAndSetLeaf(elem, xpath, "max-valid-lifetime", LeafBaseType::Uint32);
118 checkAndSetLeaf(elem, xpath, "min-valid-lifetime", LeafBaseType::Uint32);
119 checkAndSetLeaf(elem, xpath, "only-if-required", LeafBaseType::Bool);
120 checkAndSetLeaf(elem, xpath, "only-in-additional-list", LeafBaseType::Bool);
121 checkAndSetLeaf(elem, xpath, "template-test", LeafBaseType::String);
122 checkAndSetLeaf(elem, xpath, "test", LeafBaseType::String);
123 checkAndSetLeaf(elem, xpath, "valid-lifetime", LeafBaseType::Uint32);
124
125 ConstElementPtr options = elem->get("option-data");
126 if (options) {
127 setOptionDataList(xpath, options);
128 }
129
130 if (model_ == KEA_DHCP4_SERVER) {
131 checkAndSetLeaf(elem, xpath, "boot-file-name", LeafBaseType::String);
132 checkAndSetLeaf(elem, xpath, "next-server", LeafBaseType::String);
133 checkAndSetLeaf(elem, xpath, "offer-lifetime", LeafBaseType::Uint32);
134 checkAndSetLeaf(elem, xpath, "server-hostname", LeafBaseType::String);
135
136 ConstElementPtr defs = elem->get("option-def");
137 if (defs) {
138 setOptionDefList(xpath, defs);
139 }
140 } else if (model_ == KEA_DHCP6_SERVER) {
141 checkAndSetLeaf(elem, xpath, "max-preferred-lifetime", LeafBaseType::Uint32);
142 checkAndSetLeaf(elem, xpath, "min-preferred-lifetime", LeafBaseType::Uint32);
143 checkAndSetLeaf(elem, xpath, "preferred-lifetime", LeafBaseType::Uint32);
144 }
145
146 checkAndSetUserContext(elem, xpath);
147}
148
149TranslatorClasses::TranslatorClasses(Session session, const string& model)
150 : Translator(session, model),
151 TranslatorOptionData(session, model),
152 TranslatorOptionDataList(session, model),
153 TranslatorOptionDef(session, model),
154 TranslatorOptionDefList(session, model),
155 TranslatorClass(session, model) {
156}
157
159TranslatorClasses::getClasses(DataNode const& data_node) {
160 try {
161 if ((model_ == KEA_DHCP4_SERVER) ||
162 (model_ == KEA_DHCP6_SERVER)) {
163 return (getClassesKea(data_node));
164 }
165 } catch (Error const& ex) {
167 "getting client classes:"
168 << ex.what());
169 }
171 "getClasses not implemented for the model: " << model_);
172}
173
176 try {
177 return getClasses(findXPath(xpath));
178 } catch (NetconfError const&) {
179 return ElementPtr();
180 }
181}
182
184TranslatorClasses::getClassesKea(DataNode const& data_node) {
185 return getList<TranslatorClass>(data_node, "client-class", *this,
187}
188
189void
191 try {
192 if ((model_ == KEA_DHCP4_SERVER) ||
193 (model_ == KEA_DHCP6_SERVER)) {
194 setClassesKea(xpath, elem);
195 } else {
197 "setClasses not implemented for the model: " << model_);
198 }
199 } catch (Error const& ex) {
201 "setting client classes '" << elem->str()
202 << "' : " << ex.what());
203 }
204}
205
206void
208 for (size_t i = 0; i < elem->size(); ++i) {
209 ElementPtr cclass = elem->getNonConst(i);
210 if (!cclass->contains("name")) {
211 isc_throw(BadValue, "client class without name: " << elem->str());
212 }
213 string name = cclass->get("name")->stringValue();
214 ostringstream key;
215 key << xpath << "/client-class[name='" << name << "']";
216 setClass(key.str(), cclass);
217 }
218}
219
220} // namespace yang
221} // namespace isc
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
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.
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.
TranslatorOptionDataList(sysrepo::Session session, const std::string &model)
Constructor.
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.
TranslatorOptionDefList(sysrepo::Session session, const std::string &model)
Constructor.
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
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
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.