Kea  2.1.7-git
translator_control_socket.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 TranslatorControlSocket::TranslatorControlSocket(S_Session session,
22  const string& model)
23  : TranslatorBasic(session, model) {
24 }
25 
27 }
28 
31  try {
32  if ((model_ == KEA_DHCP4_SERVER) ||
33  (model_ == KEA_DHCP6_SERVER) ||
34  (model_ == KEA_DHCP_DDNS) ||
35  (model_ == KEA_CTRL_AGENT)) {
36  return (getControlSocketKea(xpath));
37  }
38  } catch (const sysrepo_exception& ex) {
40  "sysrepo error getting control socket at '" << xpath
41  << "': " << ex.what());
42  }
44  "getControlSocket not implemented for the model: " << model_);
45 }
46 
49  ConstElementPtr name = getItem(xpath + "/socket-name");
50  ConstElementPtr type = getItem(xpath + "/socket-type");
51  if (name && type) {
52  ElementPtr result = Element::createMap();
53  result->set("socket-name", name);
54  result->set("socket-type", type);
55  ConstElementPtr context = getItem(xpath + "/user-context");
56  if (context) {
57  result->set("user-context",
58  Element::fromJSON(context->stringValue()));
59  }
60  return (result);
61  }
62  return (ElementPtr());
63 }
64 
65 void
67  ConstElementPtr elem) {
68  try {
69  if ((model_ == KEA_DHCP4_SERVER) ||
70  (model_ == KEA_DHCP6_SERVER) ||
71  (model_ == KEA_DHCP_DDNS) ||
72  (model_ == KEA_CTRL_AGENT)) {
73  setControlSocketKea(xpath, elem);
74  } else {
76  "setControlSocket not implemented for the model: "
77  << model_);
78  }
79  } catch (const sysrepo_exception& ex) {
81  "sysrepo error setting control socket '" << elem->str()
82  << "' at '" << xpath << "': " << ex.what());
83  }
84 }
85 
86 void
88  ConstElementPtr elem) {
89  if (!elem) {
90  delItem(xpath);
91  return;
92  }
93  ConstElementPtr name = elem->get("socket-name");
94  if (!name) {
95  isc_throw(BadValue, "setControlSocket missing socket name");
96  }
97  ConstElementPtr type = elem->get("socket-type");
98  if (!type) {
99  isc_throw(BadValue, "setControlSocket missing socket type");
100  }
101  setItem(xpath + "/socket-name", name, SR_STRING_T);
102  setItem(xpath + "/socket-type", type, SR_ENUM_T);
103  ConstElementPtr context = Adaptor::getContext(elem);
104  if (context) {
105  setItem(xpath + "/user-context", Element::create(context->str()),
106  SR_STRING_T);
107  }
108 }
109 
110 } // namespace yang
111 } // 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.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
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:260
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
void delItem(const std::string &xpath)
Delete basic value from YANG.
Definition: translator.cc:294
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::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.