Kea  1.9.9-git
translator_option_def.cc
Go to the documentation of this file.
1 // Copyright (C) 2018 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 #ifndef HAVE_PRE_0_7_6_SYSREPO
17 using namespace sysrepo;
18 #endif
19 
20 namespace isc {
21 namespace yang {
22 
23 TranslatorOptionDef::TranslatorOptionDef(S_Session session,
24  const string& model)
25  : TranslatorBasic(session, model) {
26 }
27 
29 }
30 
32 TranslatorOptionDef::getOptionDef(const string& xpath) {
33  try {
34  if ((model_ == KEA_DHCP4_SERVER) ||
35  (model_ == KEA_DHCP6_SERVER)) {
36  return (getOptionDefKea(xpath));
37  }
38  } catch (const sysrepo_exception& ex) {
40  "sysrepo error getting option definition at '" << xpath
41  << "': " << ex.what());
42  }
44  "getOptionDef not implemented for the model: " << model_);
45 }
46 
49  ConstElementPtr code = getItem(xpath + "/code");
50  ConstElementPtr name = getItem(xpath + "/name");
51  ConstElementPtr type = getItem(xpath + "/type");
52  ConstElementPtr space = getItem(xpath + "/space");
53  if (!code || !space) {
54  // Can't happen as code and space are the keys.
55  isc_throw(Unexpected, "getOptionDefKea requires code and space: "
56  << xpath);
57  }
58  if (!name || !type) {
59  isc_throw(BadValue, "getOptionDefKea requires name and type: "
60  << xpath);
61  }
62  ElementPtr result = Element::createMap();
63  result->set("code", code);
64  result->set("name", name);
65  result->set("type", type);
66  result->set("space", getItem(xpath + "/space"));
67  ConstElementPtr record = getItem(xpath + "/record-types");
68  if (record) {
69  result->set("record-types", record);
70  }
71  ConstElementPtr array = getItem(xpath + "/array");
72  if (array) {
73  result->set("array", array);
74  }
75  ConstElementPtr encapsulate = getItem(xpath + "/encapsulate");
76  if (encapsulate) {
77  result->set("encapsulate", encapsulate);
78  }
79  ConstElementPtr context = getItem(xpath + "/user-context");
80  if (context) {
81  result->set("user-context", Element::fromJSON(context->stringValue()));
82  }
83  return (result);
84 }
85 
86 void
88  try {
89  if ((model_ == KEA_DHCP4_SERVER) ||
90  (model_ == KEA_DHCP6_SERVER)) {
91  setOptionDefKea(xpath, elem);
92  } else {
94  "setOptionDef not implemented for the model: "
95  << model_);
96  }
97  } catch (const sysrepo_exception& ex) {
99  "sysrepo error setting option definition '" << elem->str()
100  << "' at '" << xpath << "': " << ex.what());
101  }
102 }
103 
104 void
106  ConstElementPtr elem) {
107  // Skip code and space as they are the keys.
108  ConstElementPtr name = elem->get("name");
109  if (!name) {
110  isc_throw(BadValue, "option definition with name: " << elem->str());
111  }
112  setItem(xpath + "/name", name, SR_STRING_T);
113  ConstElementPtr type = elem->get("type");
114  if (!type) {
115  isc_throw(BadValue, "option definition with type: " << elem->str());
116  }
117  setItem(xpath + "/type", type, SR_STRING_T);
118  ConstElementPtr record = elem->get("record-types");
119  if (record) {
120  setItem(xpath + "/record-types", record, SR_STRING_T);
121  }
122  ConstElementPtr array = elem->get("array");
123  if (array) {
124  setItem(xpath + "/array", array, SR_BOOL_T);
125  }
126  ConstElementPtr encapsulate = elem->get("encapsulate");
127  if (encapsulate) {
128  setItem(xpath + "/encapsulate", encapsulate, SR_STRING_T);
129  }
130  ConstElementPtr context = Adaptor::getContext(elem);
131  if (context) {
132  setItem(xpath + "/user-context", Element::create(context->str()),
133  SR_STRING_T);
134  }
135 }
136 
138  const string& model)
139  : TranslatorBasic(session, model),
140  TranslatorOptionDef(session, model) {
141 }
142 
144 }
145 
148  try {
149  if ((model_ == KEA_DHCP4_SERVER) ||
150  (model_ == KEA_DHCP6_SERVER)) {
151  return (getOptionDefListKea(xpath));
152  }
153  } catch (const sysrepo_exception& ex) {
155  "sysrepo error getting option definition list at '" << xpath
156  << "': " << ex.what());
157  }
159  "getOptionDefList not implemented for the model: " << model_);
160 }
161 
164  ElementPtr result = Element::createList();
165  S_Iter_Value iter = getIter(xpath + "/option-def");
166  if (!iter) {
167  // Can't happen.
168  isc_throw(Unexpected, "getOptionDefListKea: can't get iterator: "
169  << xpath);
170  }
171  for (;;) {
172  const string& def = getNext(iter);
173  if (def.empty()) {
174  break;
175  }
176  result->add(getOptionDef(def));
177  }
178  return (result);
179 }
180 
181 void
183  ConstElementPtr elem) {
184  try {
185  if ((model_ == KEA_DHCP4_SERVER) ||
186  (model_ == KEA_DHCP6_SERVER)) {
187  setOptionDefListKea(xpath, elem);
188  } else {
190  "setOptionDefList not implemented for the model: "
191  << model_);
192  }
193  } catch (const sysrepo_exception& ex) {
195  "sysrepo error setting option definition list '"
196  << elem->str() << "' at '" << xpath << "': " << ex.what());
197  }
198 }
199 
200 void
202  ConstElementPtr elem) {
203  for (size_t i = 0; i < elem->size(); ++i) {
204  ConstElementPtr def = elem->get(i);
205  if (!def->contains("code")) {
207  "option definition without code: " << def->str());
208  }
209  unsigned code = static_cast<unsigned>(def->get("code")->intValue());
210  if (!def->contains("space")) {
212  "option definition without space: " << def->str());
213  }
214  string space = def->get("space")->stringValue();
215  ostringstream keys;
216  keys << xpath << "/option-def[code='" << code
217  << "'][space='" << space << "']";
218  setOptionDef(keys.str(), def);
219  }
220 }
221 
222 }; // end of namespace isc::yang
223 }; // end of 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:27
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
STL namespace.
isc::data::ConstElementPtr getOptionDefList(const std::string &xpath)
Get and translate option definition list from YANG to JSON.
sysrepo::S_Iter_Value getIter(const std::string &xpath)
List iterator methods keeping the session private.
Definition: translator.cc:316
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:288
A generic exception that is thrown when an unexpected error condition occurs.
std::string model_
The model.
Definition: translator.h:132
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
std::string getNext(sysrepo::S_Iter_Value iter)
Get xpath of the next YANG list item.
Definition: translator.cc:321
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.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
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-dhcp-ddns.
isc::data::ElementPtr getItem(const std::string &xpath)
Get and translate basic value from YANG to JSON.
Definition: translator.cc:111
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.