Kea 2.7.6
translator_option_def.cc
Go to the documentation of this file.
1// Copyright (C) 2018-2024 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
23 const string& model)
24 : Translator(session, model) {
25}
26
28TranslatorOptionDef::getOptionDef(DataNode const& data_node) {
29 try {
30 if ((model_ == KEA_DHCP4_SERVER) ||
31 (model_ == KEA_DHCP6_SERVER)) {
32 return (getOptionDefKea(data_node));
33 }
34 } catch (Error const& ex) {
36 "getting option definition:"
37 << ex.what());
38 }
40 "getOptionDef not implemented for the model: " << model_);
41}
42
45 try {
46 return getOptionDef(findXPath(xpath));
47 } catch (NetconfError const&) {
48 return ElementPtr();
49 }
50}
51
53TranslatorOptionDef::getOptionDefKea(DataNode const& data_node) {
55
56 getMandatoryLeaf(result, data_node, "code");
57 getMandatoryLeaf(result, data_node, "name");
58 getMandatoryLeaf(result, data_node, "space");
59 getMandatoryLeaf(result, data_node, "type");
60
61 checkAndGetLeaf(result, data_node, "array");
62 checkAndGetLeaf(result, data_node, "encapsulate");
63 checkAndGetLeaf(result, data_node, "record-types");
64
65 checkAndGetAndJsonifyLeaf(result, data_node, "user-context");
66
67 return (result->empty() ? ElementPtr() : result);
68}
69
70void
72 try {
73 if ((model_ == KEA_DHCP4_SERVER) ||
74 (model_ == KEA_DHCP6_SERVER)) {
75 setOptionDefKea(xpath, elem);
76 } else {
78 "setOptionDef not implemented for the model: "
79 << model_);
80 }
81 } catch (Error const& ex) {
83 "setting option definition '" << elem->str()
84 << "' : " << ex.what());
85 }
86}
87
88void
90 ConstElementPtr elem) {
91 // Set the list element. This is important in case we have no other elements except the keys.
92 setItem(xpath, ElementPtr(), LeafBaseType::Unknown);
93
94 // Skip keys "code" and "space" since they were set with the
95 // list element in the call above with the LeafBaseType::Unknown parameter.
96
97 setMandatoryLeaf(elem, xpath, "name", LeafBaseType::String);
98 setMandatoryLeaf(elem, xpath, "type", LeafBaseType::String);
99
100 checkAndSetLeaf(elem, xpath, "array", LeafBaseType::Bool);
101 checkAndSetLeaf(elem, xpath, "encapsulate", LeafBaseType::String);
102 checkAndSetLeaf(elem, xpath, "record-types", LeafBaseType::String);
103
104 checkAndSetUserContext(elem, xpath);
105}
106
108 const string& model)
109 : Translator(session, model),
110 TranslatorOptionDef(session, model) {
111}
112
115 try {
116 if ((model_ == KEA_DHCP4_SERVER) ||
117 (model_ == KEA_DHCP6_SERVER)) {
118 return (getOptionDefListKea(data_node));
119 }
120 } catch (Error const& ex) {
122 "getting option definition list:"
123 << ex.what());
124 }
126 "getOptionDefList not implemented for the model: " << model_);
127}
128
131 try {
132 return getOptionDefList(findXPath(xpath));
133 } catch (NetconfError const&) {
134 return ElementPtr();
135 }
136}
137
140 return getList<TranslatorOptionDef>(data_node, "option-def", *this,
142}
143
144void
146 ConstElementPtr elem) {
147 try {
148 if ((model_ == KEA_DHCP4_SERVER) ||
149 (model_ == KEA_DHCP6_SERVER)) {
150 setOptionDefListKea(xpath, elem);
151 } else {
153 "setOptionDefList not implemented for the model: "
154 << model_);
155 }
156 } catch (Error const& ex) {
158 "setting option definition list '"
159 << elem->str() << "' : " << ex.what());
160 }
161}
162
163void
165 ConstElementPtr elem) {
166 for (size_t i = 0; i < elem->size(); ++i) {
167 ElementPtr def = elem->getNonConst(i);
168 if (!def->contains("code")) {
170 "option definition without code: " << def->str());
171 }
172 unsigned code = static_cast<unsigned>(def->get("code")->intValue());
173 if (!def->contains("space")) {
175 "option definition without space: " << def->str());
176 }
177 string space = def->get("space")->stringValue();
178 ostringstream keys;
179 keys << xpath << "/option-def[code='" << code
180 << "'][space='" << space << "']";
181 setOptionDef(keys.str(), def);
182 }
183}
184
185} // namespace yang
186} // 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
isc::data::ConstElementPtr getOptionDefListKea(libyang::DataNode const &data_node)
getOptionDefList implementation specific to kea-dhcp[46]-server models.
void setOptionDefListKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setOptionDefList implementation specific to 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.
isc::data::ConstElementPtr getOptionDefListFromAbsoluteXpath(std::string const &xpath)
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.
isc::data::ElementPtr getOptionDefFromAbsoluteXpath(std::string const &xpath)
Translate an option definition from YANG to JSON.
isc::data::ElementPtr getOptionDefKea(libyang::DataNode const &data_node)
getOptionDef implementation specific to kea-dhcp[46]-server models.
void setOptionDef(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set option definition from JSON to YANG.
void setOptionDefKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setOptionDef implementation specific to kea-dhcp[46]-server models.
TranslatorOptionDef(sysrepo::Session session, const std::string &model)
Constructor.
isc::data::ElementPtr getOptionDef(libyang::DataNode const &data_node)
Translate an option definition from YANG to 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 setMandatoryLeaf(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.
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.