Kea  2.1.7-git
translator_option_def.cc
Go to the documentation of this file.
1 // Copyright (C) 2018-2021 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/adaptor.h>
11 #include <yang/yang_models.h>
12 #include <sstream>
13 
14 using namespace std;
15 using namespace isc::data;
16 using namespace sysrepo;
17 
18 namespace isc {
19 namespace yang {
20 
21 TranslatorOptionDef::TranslatorOptionDef(S_Session session,
22  const string& model)
23  : TranslatorBasic(session, model) {
24 }
25 
27 }
28 
30 TranslatorOptionDef::getOptionDef(const string& xpath) {
31  try {
32  if ((model_ == KEA_DHCP4_SERVER) ||
33  (model_ == KEA_DHCP6_SERVER)) {
34  return (getOptionDefKea(xpath));
35  }
36  } catch (const sysrepo_exception& ex) {
38  "sysrepo error getting option definition at '" << xpath
39  << "': " << ex.what());
40  }
42  "getOptionDef not implemented for the model: " << model_);
43 }
44 
47  ConstElementPtr code = getItem(xpath + "/code");
48  ConstElementPtr name = getItem(xpath + "/name");
49  ConstElementPtr type = getItem(xpath + "/type");
50  ConstElementPtr space = getItem(xpath + "/space");
51  if (!code || !space) {
52  // Can't happen as code and space are the keys.
53  isc_throw(Unexpected, "getOptionDefKea requires code and space: "
54  << xpath);
55  }
56  if (!name || !type) {
57  isc_throw(BadValue, "getOptionDefKea requires name and type: "
58  << xpath);
59  }
60  ElementPtr result = Element::createMap();
61  result->set("code", code);
62  result->set("name", name);
63  result->set("type", type);
64  result->set("space", getItem(xpath + "/space"));
65  ConstElementPtr record = getItem(xpath + "/record-types");
66  if (record) {
67  result->set("record-types", record);
68  }
69  ConstElementPtr array = getItem(xpath + "/array");
70  if (array) {
71  result->set("array", array);
72  }
73  ConstElementPtr encapsulate = getItem(xpath + "/encapsulate");
74  if (encapsulate) {
75  result->set("encapsulate", encapsulate);
76  }
77  ConstElementPtr context = getItem(xpath + "/user-context");
78  if (context) {
79  result->set("user-context", Element::fromJSON(context->stringValue()));
80  }
81  return (result);
82 }
83 
84 void
86  try {
87  if ((model_ == KEA_DHCP4_SERVER) ||
88  (model_ == KEA_DHCP6_SERVER)) {
89  setOptionDefKea(xpath, elem);
90  } else {
92  "setOptionDef not implemented for the model: "
93  << model_);
94  }
95  } catch (const sysrepo_exception& ex) {
97  "sysrepo error setting option definition '" << elem->str()
98  << "' at '" << xpath << "': " << ex.what());
99  }
100 }
101 
102 void
104  ConstElementPtr elem) {
105  // Skip code and space as they are the keys.
106  ConstElementPtr name = elem->get("name");
107  if (!name) {
108  isc_throw(BadValue, "option definition with name: " << elem->str());
109  }
110  setItem(xpath + "/name", name, SR_STRING_T);
111  ConstElementPtr type = elem->get("type");
112  if (!type) {
113  isc_throw(BadValue, "option definition with type: " << elem->str());
114  }
115  setItem(xpath + "/type", type, SR_STRING_T);
116  ConstElementPtr record = elem->get("record-types");
117  if (record) {
118  setItem(xpath + "/record-types", record, SR_STRING_T);
119  }
120  ConstElementPtr array = elem->get("array");
121  if (array) {
122  setItem(xpath + "/array", array, SR_BOOL_T);
123  }
124  ConstElementPtr encapsulate = elem->get("encapsulate");
125  if (encapsulate) {
126  setItem(xpath + "/encapsulate", encapsulate, SR_STRING_T);
127  }
128  ConstElementPtr context = Adaptor::getContext(elem);
129  if (context) {
130  setItem(xpath + "/user-context", Element::create(context->str()),
131  SR_STRING_T);
132  }
133 }
134 
136  const string& model)
137  : TranslatorBasic(session, model),
138  TranslatorOptionDef(session, model) {
139 }
140 
142 }
143 
146  try {
147  if ((model_ == KEA_DHCP4_SERVER) ||
148  (model_ == KEA_DHCP6_SERVER)) {
149  return (getOptionDefListKea(xpath));
150  }
151  } catch (const sysrepo_exception& ex) {
153  "sysrepo error getting option definition list at '" << xpath
154  << "': " << ex.what());
155  }
157  "getOptionDefList not implemented for the model: " << model_);
158 }
159 
162  return getList<TranslatorOptionDef>(xpath + "/option-def", *this,
164 }
165 
166 void
168  ConstElementPtr elem) {
169  try {
170  if ((model_ == KEA_DHCP4_SERVER) ||
171  (model_ == KEA_DHCP6_SERVER)) {
172  setOptionDefListKea(xpath, elem);
173  } else {
175  "setOptionDefList not implemented for the model: "
176  << model_);
177  }
178  } catch (const sysrepo_exception& ex) {
180  "sysrepo error setting option definition list '"
181  << elem->str() << "' at '" << xpath << "': " << ex.what());
182  }
183 }
184 
185 void
187  ConstElementPtr elem) {
188  for (size_t i = 0; i < elem->size(); ++i) {
189  ConstElementPtr def = elem->get(i);
190  if (!def->contains("code")) {
192  "option definition without code: " << def->str());
193  }
194  unsigned code = static_cast<unsigned>(def->get("code")->intValue());
195  if (!def->contains("space")) {
197  "option definition without space: " << def->str());
198  }
199  string space = def->get("space")->stringValue();
200  ostringstream keys;
201  keys << xpath << "/option-def[code='" << code
202  << "'][space='" << space << "']";
203  setOptionDef(keys.str(), def);
204  }
205 }
206 
207 } // namespace yang
208 } // namespace isc
A generic exception that is thrown when a function is not implemented.
Between YANG and JSON translator class for basic values.
Definition: translator.h:19
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
STL namespace.
isc::data::ConstElementPtr getOptionDefList(const std::string &xpath)
Get and translate option definition list from YANG to JSON.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
virtual ~TranslatorOptionDef()
Destructor.
void setOptionDefKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setOptionDef implementation specific to kea-dhcp[46]-server models.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
void setItem(const std::string &xpath, isc::data::ConstElementPtr elem, sr_type_t type)
Translate and set basic value from JSON to YANG.
Definition: translator.cc:260
A generic exception that is thrown when an unexpected error condition occurs.
std::string model_
The model.
Definition: translator.h:171
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
static isc::data::ConstElementPtr getContext(isc::data::ConstElementPtr parent)
Get user context.
Definition: adaptor.cc:27
TranslatorOptionDefList(sysrepo::S_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.
Defines the logger used by the top-level component of kea-lfc.
isc::data::ElementPtr getItem(const std::string &xpath)
Get and translate basic value from YANG to JSON.
Definition: translator.cc:105
isc::data::ConstElementPtr getOptionDefListKea(const std::string &xpath)
getOptionDefList implementation specific to kea-dhcp[46]-server models.
isc::data::ElementPtr getOptionDefKea(const std::string &xpath)
getOptionDef 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::ElementPtr getOptionDef(const std::string &xpath)
Get and translate an option definition from YANG to JSON.
Option definition translation between YANG and JSON.
void setOptionDef(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set option definition from JSON to YANG.