Kea  1.9.9-git
translator_control_socket.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 TranslatorControlSocket::TranslatorControlSocket(S_Session session,
24  const string& model)
25  : TranslatorBasic(session, model) {
26 }
27 
29 }
30 
33  try {
34  if ((model_ == KEA_DHCP4_SERVER) ||
35  (model_ == KEA_DHCP6_SERVER) ||
36  (model_ == KEA_DHCP_DDNS) ||
37  (model_ == KEA_CTRL_AGENT)) {
38  return (getControlSocketKea(xpath));
39  }
40  } catch (const sysrepo_exception& ex) {
42  "sysrepo error getting control socket at '" << xpath
43  << "': " << ex.what());
44  }
46  "getControlSocket not implemented for the model: " << model_);
47 }
48 
51  ConstElementPtr name = getItem(xpath + "/socket-name");
52  ConstElementPtr type = getItem(xpath + "/socket-type");
53  if (name && type) {
54  ElementPtr result = Element::createMap();
55  result->set("socket-name", name);
56  result->set("socket-type", type);
57  ConstElementPtr context = getItem(xpath + "/user-context");
58  if (context) {
59  result->set("user-context",
60  Element::fromJSON(context->stringValue()));
61  }
62  return (result);
63  }
64  return (ElementPtr());
65 }
66 
67 void
69  ConstElementPtr elem) {
70  try {
71  if ((model_ == KEA_DHCP4_SERVER) ||
72  (model_ == KEA_DHCP6_SERVER) ||
73  (model_ == KEA_DHCP_DDNS) ||
74  (model_ == KEA_CTRL_AGENT)) {
75  setControlSocketKea(xpath, elem);
76  } else {
78  "setControlSocket not implemented for the model: "
79  << model_);
80  }
81  } catch (const sysrepo_exception& ex) {
83  "sysrepo error setting control socket '" << elem->str()
84  << "' at '" << xpath << "': " << ex.what());
85  }
86 }
87 
88 void
90  ConstElementPtr elem) {
91  if (!elem) {
92  delItem(xpath + "/socket-name");
93  delItem(xpath + "/socket-type");
94  delItem(xpath + "/user-context");
95  delItem(xpath);
96  return;
97  }
98  ConstElementPtr name = elem->get("socket-name");
99  if (!name) {
100  isc_throw(BadValue, "setControlSocket missing socket name");
101  }
102  ConstElementPtr type = elem->get("socket-type");
103  if (!type) {
104  isc_throw(BadValue, "setControlSocket missing socket type");
105  }
106  setItem(xpath + "/socket-name", name, SR_STRING_T);
107  setItem(xpath + "/socket-type", type, SR_ENUM_T);
108  ConstElementPtr context = Adaptor::getContext(elem);
109  if (context) {
110  setItem(xpath + "/user-context", Element::create(context->str()),
111  SR_STRING_T);
112  }
113 }
114 
115 }; // end of namespace isc::yang
116 }; // 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.
void setControlSocket(const std::string &xpath, isc::data::ConstElementPtr elem)
Translate and set control socket from JSON to YANG.
#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 setControlSocketKea(const std::string &xpath, isc::data::ConstElementPtr elem)
setControlSocket for kea models.
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
std::string model_
The model.
Definition: translator.h:132
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
static isc::data::ConstElementPtr getContext(isc::data::ConstElementPtr parent)
Get user context.
Definition: adaptor.cc:27
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
void delItem(const std::string &xpath)
Delete basic value from YANG.
Definition: translator.cc:304
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::ElementPtr getControlSocketKea(const std::string &xpath)
getControlSocket JSON for kea models.
isc::data::ConstElementPtr getControlSocket(const std::string &xpath)
Get and translate a control socket from YANG to JSON.