Kea: translator_option_def.cc Source File

| Kea 3.0.2-git

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
9#include <yang/translator_option_def.h>
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
22TranslatorOptionDef::TranslatorOptionDef(Session session,
23 const string& model)
24 : Translator(session, model) {
25}
26
27 ElementPtr
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) {
35 isc_throw(NetconfError,
36 "getting option definition:"
37 << ex.what());
38 }
39 isc_throw(NotImplemented,
40 "getOptionDef not implemented for the model: " << model_);
41}
42
43 ElementPtr
44TranslatorOptionDef::getOptionDefFromAbsoluteXpath(string const& xpath) {
45 try {
46 return getOptionDef(findXPath(xpath));
47 } catch (NetconfError const&) {
48 return ElementPtr();
49 }
50}
51
52 ElementPtr
53TranslatorOptionDef::getOptionDefKea(DataNode const& data_node) {
54 ElementPtr result = Element::createMap();
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
71TranslatorOptionDef::setOptionDef(string const& xpath, ConstElementPtr elem) {
72 try {
73 if ((model_ == KEA_DHCP4_SERVER) ||
74 (model_ == KEA_DHCP6_SERVER)) {
75 setOptionDefKea(xpath, elem);
76 } else {
77 isc_throw(NotImplemented,
78 "setOptionDef not implemented for the model: "
79 << model_);
80 }
81 } catch (Error const& ex) {
82 isc_throw(NetconfError,
83 "setting option definition '" << elem->str()
84 << "' : " << ex.what());
85 }
86}
87
88void
89TranslatorOptionDef::setOptionDefKea(string const& xpath,
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
107TranslatorOptionDefList::TranslatorOptionDefList(Session session,
108 const string& model)
109 : Translator(session, model),
110 TranslatorOptionDef(session, model) {
111}
112
113 ConstElementPtr
114TranslatorOptionDefList::getOptionDefList(DataNode const& data_node) {
115 try {
116 if ((model_ == KEA_DHCP4_SERVER) ||
117 (model_ == KEA_DHCP6_SERVER)) {
118 return (getOptionDefListKea(data_node));
119 }
120 } catch (Error const& ex) {
121 isc_throw(NetconfError,
122 "getting option definition list:"
123 << ex.what());
124 }
125 isc_throw(NotImplemented,
126 "getOptionDefList not implemented for the model: " << model_);
127}
128
129 ConstElementPtr
130TranslatorOptionDefList::getOptionDefListFromAbsoluteXpath(string const& xpath) {
131 try {
132 return getOptionDefList(findXPath(xpath));
133 } catch (NetconfError const&) {
134 return ElementPtr();
135 }
136}
137
138 ConstElementPtr
139TranslatorOptionDefList::getOptionDefListKea(DataNode const& data_node) {
140 return getList<TranslatorOptionDef>(data_node, "option-def", *this,
141 &TranslatorOptionDefList::getOptionDef);
142}
143
144void
145TranslatorOptionDefList::setOptionDefList(string const& xpath,
146 ConstElementPtr elem) {
147 try {
148 if ((model_ == KEA_DHCP4_SERVER) ||
149 (model_ == KEA_DHCP6_SERVER)) {
150 setOptionDefListKea(xpath, elem);
151 } else {
152 isc_throw(NotImplemented,
153 "setOptionDefList not implemented for the model: "
154 << model_);
155 }
156 } catch (Error const& ex) {
157 isc_throw(NetconfError,
158 "setting option definition list '"
159 << elem->str() << "' : " << ex.what());
160 }
161}
162
163void
164TranslatorOptionDefList::setOptionDefListKea(string const& xpath,
165 ConstElementPtr elem) {
166 for (size_t i = 0; i < elem->size(); ++i) {
167 ElementPtr def = elem->getNonConst(i);
168 if (!def->contains("code")) {
169 isc_throw(BadValue,
170 "option definition without code: " << def->str());
171 }
172 unsigned code = static_cast<unsigned>(def->get("code")->intValue());
173 if (!def->contains("space")) {
174 isc_throw(BadValue,
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
Element::createMap
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition exceptions/exceptions.h:132
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition exceptions/exceptions.cc:32
isc::NotImplemented
A generic exception that is thrown when a function is not implemented.
Definition exceptions/exceptions.h:166
isc::yang::TranslatorOptionDefList::getOptionDefListKea
isc::data::ConstElementPtr getOptionDefListKea(libyang::DataNode const &data_node)
getOptionDefList implementation specific to kea-dhcp[46]-server models.
Definition translator_option_def.cc:139
isc::yang::TranslatorOptionDefList::setOptionDefListKea
void setOptionDefListKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setOptionDefList implementation specific to kea-dhcp[46]-server models.
Definition translator_option_def.cc:164
isc::yang::TranslatorOptionDefList::getOptionDefList
isc::data::ConstElementPtr getOptionDefList(libyang::DataNode const &data_node)
Translate option definition list from YANG to JSON.
Definition translator_option_def.cc:114
isc::yang::TranslatorOptionDefList::TranslatorOptionDefList
TranslatorOptionDefList(sysrepo::Session session, const std::string &model)
Constructor.
Definition translator_option_def.cc:107
isc::yang::TranslatorOptionDefList::getOptionDefListFromAbsoluteXpath
isc::data::ConstElementPtr getOptionDefListFromAbsoluteXpath(std::string const &xpath)
Translate option definition list from YANG to JSON.
Definition translator_option_def.cc:130
isc::yang::TranslatorOptionDefList::setOptionDefList
void setOptionDefList(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set option definition list from JSON to YANG.
Definition translator_option_def.cc:145
isc::yang::TranslatorOptionDef::getOptionDefFromAbsoluteXpath
isc::data::ElementPtr getOptionDefFromAbsoluteXpath(std::string const &xpath)
Translate an option definition from YANG to JSON.
Definition translator_option_def.cc:44
isc::yang::TranslatorOptionDef::getOptionDefKea
isc::data::ElementPtr getOptionDefKea(libyang::DataNode const &data_node)
getOptionDef implementation specific to kea-dhcp[46]-server models.
Definition translator_option_def.cc:53
isc::yang::TranslatorOptionDef::setOptionDef
void setOptionDef(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set option definition from JSON to YANG.
Definition translator_option_def.cc:71
isc::yang::TranslatorOptionDef::setOptionDefKea
void setOptionDefKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setOptionDef implementation specific to kea-dhcp[46]-server models.
Definition translator_option_def.cc:89
isc::yang::TranslatorOptionDef::TranslatorOptionDef
TranslatorOptionDef(sysrepo::Session session, const std::string &model)
Constructor.
Definition translator_option_def.cc:22
isc::yang::TranslatorOptionDef::getOptionDef
isc::data::ElementPtr getOptionDef(libyang::DataNode const &data_node)
Translate an option definition from YANG to JSON.
Definition translator_option_def.cc:28
isc::yang::Translator
Between YANG and JSON translator class for basic values.
Definition translator.h:23
isc::yang::Translator::getList
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
isc::yang::Translator::setMandatoryLeaf
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.
Definition translator.cc:245
isc::yang::Translator::checkAndSetLeaf
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::yang::Translator::findXPath
libyang::DataNode findXPath(std::string const &xpath) const
Retrieves a YANG data node by xpath.
Definition translator.cc:132
isc::yang::Translator::getMandatoryLeaf
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...
Definition translator.cc:197
isc::yang::Translator::checkAndGetLeaf
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
isc::yang::Translator::checkAndSetUserContext
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
isc::yang::Translator::setItem
void setItem(const std::string &xpath, isc::data::ConstElementPtr const elem, libyang::LeafBaseType const type)
Translate and set basic value from JSON to YANG.
Definition translator.cc:231
isc::yang::Translator::model_
std::string model_
The model.
Definition translator.h:427
isc::yang::Translator::checkAndGetAndJsonifyLeaf
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
isc::yang::Translator::Translator
Translator(sysrepo::Session session, const std::string &model)
Constructor.
Definition translator.cc:27
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
isc::data
Definition base_stamped_element.cc:12
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
isc::yang
Definition adaptor.cc:17
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
isc::yang::NetconfError
Generic NETCONF error.
Definition netconf_error.h:30
translator_option_def.h
yang_models.h