Kea 2.7.5
translator_option_data.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
28TranslatorOptionData::getOptionData(DataNode const& data_node) {
29 try {
30 if ((model_ == KEA_DHCP4_SERVER) ||
31 (model_ == KEA_DHCP6_SERVER)) {
32 return (getOptionDataKea(data_node));
33 }
34 } catch (Error const& ex) {
36 "getting option data:"
37 << ex.what());
38 }
40 "getOptionData not implemented for the model: " << model_);
41}
42
45 try {
46 return getOptionData(findXPath(xpath));
47 } catch (NetconfError const&) {
48 return ElementPtr();
49 }
50}
51
53TranslatorOptionData::getOptionDataKea(DataNode const& data_node) {
55
56 // Code and space must exist.
57 getMandatoryLeaf(result, data_node, "code");
58 getMandatoryLeaf(result, data_node, "space");
59
60 // Data must exist according to the YANG module too, but empty data is considered no data.
61 ElementPtr const& x(getItem(data_node, "data"));
62 if (x && !x->stringValue().empty()) {
63 result->set("data", x);
64 }
65
66 checkAndGetLeaf(result, data_node, "always-send");
67 checkAndGetLeaf(result, data_node, "csv-format");
68 checkAndGetLeaf(result, data_node, "name");
69 checkAndGetLeaf(result, data_node, "never-send");
70
71 checkAndGetAndJsonifyLeaf(result, data_node, "user-context");
72
73 return (result->empty() ? ElementPtr() : result);
74}
75
76void
78 ConstElementPtr elem) {
79 try {
80 if ((model_ == KEA_DHCP4_SERVER) ||
81 (model_ == KEA_DHCP6_SERVER)) {
82 setOptionDataKea(xpath, elem);
83 } else {
85 "setOptionData not implemented for the model: "
86 << model_);
87 }
88 } catch (Error const& ex) {
90 "setting option data '" << elem->str()
91 << "' : " << ex.what());
92 }
93}
94
95void
97 ConstElementPtr elem) {
98 // Set the list element. This is important in case we have no other elements except the keys.
99 setItem(xpath, ElementPtr(), LeafBaseType::Unknown);
100
101 // Skip keys "code", "space", and "data" since they were set with the
102 // list element in the call above with the LeafBaseType::Unknown parameter.
103
104 checkAndSetLeaf(elem, xpath, "always-send", LeafBaseType::Bool);
105 checkAndSetLeaf(elem, xpath, "csv-format", LeafBaseType::Bool);
106 checkAndSetLeaf(elem, xpath, "name", LeafBaseType::String);
107 checkAndSetLeaf(elem, xpath, "never-send", LeafBaseType::Bool);
108
109 checkAndSetUserContext(elem, xpath);
110}
111
113 const string& model)
114 : Translator(session, model),
115 TranslatorOptionData(session, model) {
116}
117
120 try {
121 if ((model_ == KEA_DHCP4_SERVER) ||
122 (model_ == KEA_DHCP6_SERVER)) {
123 return (getOptionDataListKea(data_node));
124 }
125 } catch (Error const& ex) {
127 "getting option data list:"
128 << ex.what());
129 }
131 "getOptionDataList not implemented for the model: " << model_);
132}
133
136 try {
137 return getOptionDataList(findXPath(xpath));
138 } catch (NetconfError const&) {
139 return ElementPtr();
140 }
141}
142
145 return getList<TranslatorOptionData>(data_node, "option-data", *this,
147}
148
149void
151 ConstElementPtr elem) {
152 try {
153 if ((model_ == KEA_DHCP4_SERVER) ||
154 (model_ == KEA_DHCP6_SERVER)) {
155 setOptionDataListKea(xpath, elem);
156 } else {
158 "setOptionDataList not implemented for the model: "
159 << model_);
160 }
161 } catch (Error const& ex) {
163 "setting option data list '" << elem->str()
164 << "' : " << ex.what());
165 }
166}
167
168void
170 ConstElementPtr elem) {
171 for (size_t i = 0; i < elem->size(); ++i) {
172 ElementPtr option = elem->getNonConst(i);
173
174 // Code and space must exist in the input.
175 if (!option->contains("code")) {
176 isc_throw(BadValue, "option data without code: " << option->str());
177 }
178 unsigned code = static_cast<unsigned>(option->get("code")->intValue());
179 if (!option->contains("space")) {
180 isc_throw(BadValue, "option data without space: " << option->str());
181 }
182 string space = option->get("space")->stringValue();
183
184 // Data must exist according to the YANG module too, but no data in the input is allowed and
185 // converted to an empty string.
186 string data;
187 if (option->contains("data")) {
188 data = option->get("data")->stringValue();
189 }
190
191 ostringstream keys;
192 keys << xpath << "/option-data[code='" << code <<
193 "'][space='" << space <<
194 "'][data='" << data << "']";
195 setOptionData(keys.str(), option);
196 }
197}
198
199} // namespace yang
200} // 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 getOptionDataListKea(libyang::DataNode const &data_node)
getOptionDataList for kea-dhcp[46].
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.
void setOptionDataListKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setOptionDataList for kea-dhcp[46].
isc::data::ConstElementPtr getOptionDataList(libyang::DataNode const &data_node)
Translate option data list from YANG to JSON.
isc::data::ConstElementPtr getOptionDataListFromAbsoluteXpath(std::string const &xpath)
Translate option data list from YANG to JSON.
Option data translation between YANG and JSON.
isc::data::ElementPtr getOptionDataFromAbsoluteXpath(std::string const &xpath)
Translate an option data from YANG to JSON.
isc::data::ElementPtr getOptionDataKea(libyang::DataNode const &data_node)
getOptionData JSON for kea-dhcp[46].
TranslatorOptionData(sysrepo::Session session, const std::string &model)
Constructor.
isc::data::ElementPtr getOptionData(libyang::DataNode const &data_node)
Translate an option data from YANG to JSON.
void setOptionData(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set option data from JSON to YANG.
void setOptionDataKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setOptionData for kea-dhcp[46].
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
isc::data::ElementPtr getItem(libyang::DataNode const &data_node, std::string const &xpath) const
Translate a basic value from YANG to JSON for a given xpath that is relative to the given source node...
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.