Kea  1.5.0
translator.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 
9 #include <yang/translator.h>
10 #include <util/encode/base64.h>
11 #include <cstring>
12 
13 using namespace std;
14 using namespace isc::data;
15 using namespace isc::util::encode;
16 #ifndef HAVE_PRE_0_7_6_SYSREPO
17 using namespace sysrepo;
18 #endif
19 
20 namespace {
21 
22 string encode64(const string& input) {
23  vector<uint8_t> binary;
24  binary.resize(input.size());
25  memmove(&binary[0], input.c_str(), binary.size());
26  return (encodeBase64(binary));
27 }
28 
29 string decode64(const string& input) {
30  vector<uint8_t> binary;
31  decodeBase64(input, binary);
32  string result;
33  result.resize(binary.size());
34  memmove(&result[0], &binary[0], result.size());
35  return (result);
36 }
37 
38 } // end of anonymous namespace
39 
40 namespace isc {
41 namespace yang {
42 
43 TranslatorBasic::TranslatorBasic(S_Session session, const string& model)
44  : session_(session), model_(model) {
45 }
46 
48 }
49 
51 TranslatorBasic::value(S_Val s_val) {
52  if (!s_val) {
53  isc_throw(BadValue, "value called with null");
54  }
55  switch (s_val->type()) {
56  case SR_CONTAINER_T:
57  case SR_CONTAINER_PRESENCE_T:
58  // Internal node.
59  return (ElementPtr());
60 
61  case SR_LIST_T:
62  return (Element::createList());
63 
64  case SR_STRING_T:
65  return (Element::create(string(s_val->data()->get_string())));
66 
67  case SR_BOOL_T:
68  return (Element::create(s_val->data()->get_bool() ? true : false));
69 
70  case SR_UINT8_T:
71  return (Element::create(static_cast<long long>(s_val->data()->get_uint8())));
72 
73  case SR_UINT16_T:
74  return (Element::create(static_cast<long long>(s_val->data()->get_uint16())));
75 
76  case SR_UINT32_T:
77  return (Element::create(static_cast<long long>(s_val->data()->get_uint32())));
78 
79  case SR_INT8_T:
80  return (Element::create(static_cast<long long>(s_val->data()->get_int8())));
81 
82  case SR_INT16_T:
83  return (Element::create(static_cast<long long>(s_val->data()->get_int16())));
84 
85  case SR_INT32_T:
86  return (Element::create(static_cast<long long>(s_val->data()->get_int32())));
87 
88  case SR_IDENTITYREF_T:
89  return (Element::create(string(s_val->data()->get_identityref())));
90 
91  case SR_ENUM_T:
92  return (Element::create(string(s_val->data()->get_enum())));
93 
94  case SR_BINARY_T:
95  return (Element::create(decode64(s_val->data()->get_binary())));
96 
97  default:
99  "value called with unupported type: " << s_val->type());
100  }
101 }
102 
104 TranslatorBasic::getItem(const string& xpath) {
105  S_Val s_val;
106  try {
107  s_val = session_->get_item(xpath.c_str());
108  } catch (const sysrepo_exception& ex) {
109  isc_throw(SysrepoError, "sysrepo error getting item at '" << xpath
110  << "': " << ex.what());
111  }
112  if (!s_val) {
113  return (ElementPtr());
114  }
115  return (value(s_val));
116 }
117 
119 TranslatorBasic::getItems(const string& xpath) {
120  S_Vals s_vals;
121  try {
122  s_vals = session_->get_items(xpath.c_str());
123  if (!s_vals) {
124  return (ElementPtr());
125  }
126  ElementPtr result = Element::createList();
127  for (size_t i = 0; i < s_vals->val_cnt(); ++i) {
128  S_Val s_val = s_vals->val(i);
129  result->add(value(s_val));
130  }
131  return (result);
132  } catch (const sysrepo_exception& ex) {
134  "sysrepo error getting item at '" << xpath
135  << "': " << ex.what());
136  }
137 }
138 
139 S_Val
141  if (!elem) {
142  isc_throw(BadValue, "value called with null");
143  }
144  S_Val s_val;
145  switch (type) {
146  case SR_CONTAINER_T:
147  case SR_CONTAINER_PRESENCE_T:
148  isc_throw(NotImplemented, "value called for a container");
149 
150  case SR_LIST_T:
151  if (elem->getType() != Element::list) {
152  isc_throw(BadValue, "value for a list called with not a list: "
153  << elem->str());
154  }
155  // Return null.
156  break;
157 
158  case SR_STRING_T:
159  case SR_IDENTITYREF_T:
160  case SR_ENUM_T:
161  if (elem->getType() != Element::string) {
163  "value for a string called with not a string: "
164  << elem->str());
165  }
166  s_val.reset(new Val(elem->stringValue().c_str(), type));
167  break;
168 
169  case SR_BOOL_T:
170  if (elem->getType() != Element::boolean) {
172  "value for a boolean called with not a boolean: "
173  << elem->str());
174  }
175  s_val.reset(new Val(elem->boolValue(), type));
176  break;
177 
178  case SR_UINT8_T:
179  if (elem->getType() != Element::integer) {
181  "value for an integer called with not an integer: "
182  << elem->str());
183  }
184  s_val.reset(new Val(static_cast<uint8_t>(elem->intValue()), type));
185  break;
186 
187  case SR_UINT16_T:
188  if (elem->getType() != Element::integer) {
190  "value for an integer called with not an integer: "
191  << elem->str());
192  }
193  s_val.reset(new Val(static_cast<uint16_t>(elem->intValue()), type));
194  break;
195 
196  case SR_UINT32_T:
197  if (elem->getType() != Element::integer) {
199  "value for an integer called with not an integer: "
200  << elem->str());
201  }
202  s_val.reset(new Val(static_cast<uint32_t>(elem->intValue()), type));
203  break;
204 
205  case SR_INT8_T:
206  if (elem->getType() != Element::integer) {
208  "value for an integer called with not an integer: "
209  << elem->str());
210  }
211  s_val.reset(new Val(static_cast<int8_t>(elem->intValue()), type));
212  break;
213 
214  case SR_INT16_T:
215  if (elem->getType() != Element::integer) {
217  "value for an integer called with not an integer: "
218  << elem->str());
219  }
220  s_val.reset(new Val(static_cast<int16_t>(elem->intValue()), type));
221  break;
222 
223  case SR_INT32_T:
224  if (elem->getType() != Element::integer) {
226  "value for an integer called with not an integer: "
227  << elem->str());
228  }
229  s_val.reset(new Val(static_cast<int32_t>(elem->intValue()), type));
230  break;
231 
232  case SR_BINARY_T:
233  if (elem->getType() != Element::string) {
235  "value for a base64 called with not a string: "
236  << elem->str());
237  }
238  s_val.reset(new Val(encode64(elem->stringValue()).c_str(), type));
239  break;
240 
241  default:
243  "value called with unupported type: " << type);
244  }
245 
246  return (s_val);
247 }
248 
249 void
250 TranslatorBasic::setItem(const string& xpath, ConstElementPtr elem,
251  sr_type_t type) {
252  S_Val s_val = value(elem, type);
253  if (!s_val && (type != SR_LIST_T)) {
254  return;
255  }
256  try {
257  session_->set_item(xpath.c_str(), s_val);
258  } catch (const sysrepo_exception& ex) {
260  "sysrepo error setting item '" << elem->str()
261  << "' at '" << xpath << "': " << ex.what());
262  }
263 }
264 
265 void
266 TranslatorBasic::delItem(const std::string& xpath) {
267  try {
268  session_->delete_item(xpath.c_str());
269  } catch (const sysrepo_exception& ex) {
271  "sysrepo error deleting item at '"
272  << xpath << "': " << ex.what());
273  }
274 }
275 
276 
277 S_Iter_Value
278 TranslatorBasic::getIter(const std::string& xpath) {
279  return (session_->get_items_iter(xpath.c_str()));
280 }
281 
282 string
283 TranslatorBasic::getNext(S_Iter_Value iter) {
284  if (!iter) {
285  isc_throw(BadValue, "getNext called with null");
286  }
287  S_Val s_val;
288  try {
289  s_val = session_->get_item_next(iter);
290  } catch (const sysrepo_exception&) {
291  // Should not happen according to the doc but still happen?
292  return ("");
293  }
294  if (!s_val) {
295  return ("");
296  }
297  return (s_val->xpath());
298 }
299 
300 }; // end of namespace isc::yang
301 }; // end of namespace isc
isc::yang::TranslatorBasic::getIter
sysrepo::S_Iter_Value getIter(const std::string &xpath)
List iterator methods keeping the session private.
Definition: translator.cc:278
isc::util::encode
Definition: base32hex.h:22
isc::yang::TranslatorBasic::~TranslatorBasic
virtual ~TranslatorBasic()
Destructor.
Definition: translator.cc:47
isc::yang::TranslatorBasic::getNext
std::string getNext(sysrepo::S_Iter_Value iter)
Get xpath of the next YANG list item.
Definition: translator.cc:283
isc::yang::TranslatorBasic::getItem
isc::data::ElementPtr getItem(const std::string &xpath)
Get and translate basic value from YANG to JSON.
Definition: translator.cc:104
isc::data
Definition: cfg_to_element.h:25
isc::yang::TranslatorBasic::getItems
isc::data::ElementPtr getItems(const std::string &xpath)
Get and translate a list of basic values from YANG to JSON.
Definition: translator.cc:119
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
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_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
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
base64.h
isc::yang::TranslatorBasic::delItem
void delItem(const std::string &xpath)
Delete basic value from YANG.
Definition: translator.cc:266
isc::yang::TranslatorBasic::setItem
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:250
isc::yang::TranslatorBasic::value
static isc::data::ElementPtr value(sysrepo::S_Val s_val)
Translate basic value from YANG to JSON.
isc::yang::TranslatorBasic::session_
sysrepo::S_Session session_
The sysrepo session.
Definition: translator.h:126
isc::util::encode::encodeBase64
std::string encodeBase64(const std::vector< uint8_t > &binary)
Encode binary data in the base64 format.
Definition: base_n.cc:441
isc::util::encode::decodeBase64
void decodeBase64(const std::string &input, std::vector< uint8_t > &result)
Decode a text encoded in the base64 format into the original data.
Definition: base_n.cc:446
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
isc::NotImplemented
A generic exception that is thrown when a function is not implemented.
Definition: exceptions/exceptions.h:165
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
translator.h
isc::yang::SysrepoError
Sysrepo error.
Definition: sysrepo_error.h:16