Kea 2.2.0
netconf_config.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
12#include <asiolink/io_error.h>
13
14#include <boost/foreach.hpp>
15#include <boost/scoped_ptr.hpp>
16#include <boost/algorithm/string/predicate.hpp>
17
18#include <sstream>
19#include <string>
20
21using namespace std;
22using namespace isc::process;
23using namespace isc::data;
24using namespace isc::http;
25
26namespace isc {
27namespace netconf {
28
29// *********************** CfgControlSocket *************************
30
32 const Url& url)
33 : type_(type), name_(name), url_(url) {
34}
35
37CfgControlSocket::stringToType(const string& type) {
38 if (type == "unix") {
39 return (CfgControlSocket::Type::UNIX);
40 } else if (type == "http") {
41 return (CfgControlSocket::Type::HTTP);
42 } else if (type == "stdout") {
43 return (CfgControlSocket::Type::STDOUT);
44 }
45
46 isc_throw(BadValue, "Unknown control socket type: " << type);
47}
48
49const string
51 switch (type) {
52 case CfgControlSocket::Type::UNIX:
53 return ("unix");
54 case CfgControlSocket::Type::HTTP:
55 return ("http");
56 case CfgControlSocket::Type::STDOUT:
57 return ("stdout");
58 default:
59 isc_throw(BadValue, "Unknown control socket type: " << type);
60 }
61}
62
65 ElementPtr result = Element::createMap();
66 // Set user-context
67 contextToElement(result);
68 // Set type
69 result->set("socket-type", Element::create(typeToString(type_)));
70 // Set name
71 result->set("socket-name", Element::create(name_));
72 // Set url
73 result->set("socket-url", Element::create(url_.toText()));
74 return (result);
75}
76
77// *********************** CfgServer *************************
78CfgServer::CfgServer(const string& model, CfgControlSocketPtr ctrl_sock)
79 : model_(model), boot_update_(true), subscribe_changes_(true),
80 subscribe_notifications_(true), validate_changes_(true),
81 control_socket_(ctrl_sock) {
82}
83
84string
86 ostringstream s;
87 s << "model: " << model_ << ", control socker: ";
88 if (!control_socket_) {
89 s << "none";
90 } else {
91 switch (control_socket_->getType()) {
92 case CfgControlSocket::Type::UNIX:
93 s << "UNIX:'" << control_socket_->getName() << "'";
94 break;
95 case CfgControlSocket::Type::HTTP:
96 s << "HTTP:'" << control_socket_->getUrl().toText() << "'";
97 break;
98 case CfgControlSocket::Type::STDOUT:
99 s << "STDOUT";
100 break;
101 }
102 }
103 return (s.str());
104}
105
108 ElementPtr result = Element::createMap();
109 // Set user-context
110 contextToElement(result);
111 // Set model
112 result->set("model", Element::create(model_));
113 // Set boot-update
114 result->set("boot-update", Element::create(boot_update_));
115 // Set subscribe-changes
116 result->set("subscribe-changes", Element::create(subscribe_changes_));
117 // Set validate-changes
118 result->set("validate-changes", Element::create(validate_changes_));
119 // Set control-socket
120 if (control_socket_) {
121 result->set("control-socket", control_socket_->toElement());
122 }
123 return (result);
124}
125
126ostream&
127operator<<(ostream& os, const CfgServer& server) {
128 os << server.toText();
129 return (os);
130}
131
132// *************************** PARSERS ***********************************
133
134// *********************** ControlSocketConfigParser *************************
135
138 CfgControlSocketPtr result;
139 string type_str = getString(ctrl_sock_config, "socket-type");
140 string name = getString(ctrl_sock_config, "socket-name");
141 string url_str = getString(ctrl_sock_config, "socket-url");
142 ConstElementPtr user_context = ctrl_sock_config->get("user-context");
143
144 // Type must be valid.
146 try {
147 type = CfgControlSocket::stringToType(type_str);
148 } catch (const std::exception& ex) {
149 isc_throw(ConfigError, ex.what() << " '" << type_str << "' ("
150 << getPosition("socket-type", ctrl_sock_config) << ")");
151 }
152
153 // Url must be valid.
154 Url url(url_str);
155 if (!url.isValid()) {
156 isc_throw(ConfigError, "invalid control socket url: "
157 << url.getErrorMessage() << " '" << url_str << "' ("
158 << getPosition("socket-url", ctrl_sock_config) << ")");
159 }
160
161 // Create the control socket.
162 try {
163 result.reset(new CfgControlSocket(type, name, url));
164 } catch (const std::exception& ex) {
165 isc_throw(ConfigError, ex.what() << " ("
166 << ctrl_sock_config->getPosition() << ")");
167 }
168
169 // Add user-context.
170 if (user_context) {
171 result->setContext(user_context);
172 }
173
174 return (result);
175}
176
177// *********************** ServerConfigParser *************************
178
181 CfgServerPtr result;
182 string model = getString(server_config, "model");
183 ConstElementPtr user_context = server_config->get("user-context");
184 ConstElementPtr ctrl_sock_config = server_config->get("control-socket");
185 CfgControlSocketPtr ctrl_sock;
186 if (ctrl_sock_config) {
188 ctrl_sock = parser.parse(ctrl_sock_config);
189 }
190 try {
191 result.reset(new CfgServer(model, ctrl_sock));
192 } catch (const std::exception& ex) {
193 isc_throw(ConfigError, ex.what() << " ("
194 << server_config->getPosition() << ")");
195 }
196
197 // Add flags.
198 result->setBootUpdate(getBoolean(server_config, "boot-update"));
199 result->setSubscribeChanges(getBoolean(server_config, "subscribe-changes"));
200 result->setValidateChanges(getBoolean(server_config, "validate-changes"));
201
202 // Add user-context.
203 if (user_context) {
204 result->setContext(user_context);
205 }
206
207 return (result);
208}
209
210} // namespace netconf
211} // namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
An exception that is thrown if an error occurs while configuring any server.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
static const data::Element::Position & getPosition(const std::string &name, const data::ConstElementPtr parent)
Utility method that returns position of an element.
static std::string getString(isc::data::ConstElementPtr scope, const std::string &name)
Returns a string parameter from a scope.
static bool getBoolean(isc::data::ConstElementPtr scope, const std::string &name)
Returns a boolean parameter from a scope.
Represents an URL.
Definition: url.h:20
std::string toText() const
Returns textual representation of the URL.
Definition: url.cc:65
std::string getErrorMessage() const
Returns parsing error message.
Definition: url.h:52
bool isValid() const
Checks if the URL is valid.
Definition: url.h:47
Represents a Control Socket.
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
Type
Defines the list of possible control socket types.
CfgControlSocket(Type type, const std::string &name, const isc::http::Url &url)
Constructor.
static const std::string typeToString(CfgControlSocket::Type type)
Converts CfgControlSocket::Type to string.
static Type stringToType(const std::string &type)
Converts socket type name to CfgControlSocket::Type.
Represents a Managed CfgServer.
CfgServer(const std::string &model, CfgControlSocketPtr ctrl_sock)
Constructor.
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
std::string toText() const
Returns a text representation for the server.
Parser for CfgControlSocket.
CfgControlSocketPtr parse(data::ConstElementPtr ctrl_sock_config)
Performs the actual parsing of the given "control-socket" element.
CfgServerPtr parse(data::ConstElementPtr server_config)
Performs the actual parsing of the given value from the "managed-servers" map.
const Name & name_
Definition: dns/message.cc:693
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
ostream & operator<<(ostream &os, const CfgServer &server)
Dumps the contents of a CfgServer as text to a output stream.
boost::shared_ptr< CfgControlSocket > CfgControlSocketPtr
Defines a pointer for CfgControlSocket instances.
boost::shared_ptr< CfgServer > CfgServerPtr
Defines a pointer for CfgServer instances.
Defines the logger used by the top-level component of kea-lfc.
Contains declarations for loggers used by the Kea netconf agent.
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
Definition: user_context.cc:15