Kea 2.5.7
netconf_config.cc
Go to the documentation of this file.
1// Copyright (C) 2018-2022 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 <asiolink/io_error.h>
11#include <netconf/netconf_log.h>
13
14#include <sstream>
15#include <string>
16
17using namespace std;
18using namespace isc::process;
19using namespace isc::data;
20using namespace isc::http;
21
22namespace isc {
23namespace netconf {
24
25// *********************** CfgControlSocket *************************
26
28 const Url& url)
29 : type_(type), name_(name), url_(url) {
30}
31
33CfgControlSocket::stringToType(const string& type) {
34 if (type == "unix") {
36 } else if (type == "http") {
38 } else if (type == "stdout") {
40 }
41
42 isc_throw(BadValue, "Unknown control socket type: " << type);
43}
44
45const string
47 switch (type) {
49 return ("unix");
51 return ("http");
53 return ("stdout");
54 default:
55 isc_throw(BadValue, "Unknown control socket type: " << type);
56 }
57}
58
62 // Set user-context
63 contextToElement(result);
64 // Set type
65 result->set("socket-type", Element::create(typeToString(type_)));
66 // Set name
67 result->set("socket-name", Element::create(name_));
68 // Set url
69 result->set("socket-url", Element::create(url_.toText()));
70 return (result);
71}
72
73// *********************** CfgServer *************************
74CfgServer::CfgServer(const string& model, CfgControlSocketPtr ctrl_sock)
75 : model_(model), boot_update_(true), subscribe_changes_(true),
76 subscribe_notifications_(true), validate_changes_(true),
77 control_socket_(ctrl_sock) {
78}
79
80string
82 ostringstream s;
83 s << "model: " << model_ << ", control socker: ";
84 if (!control_socket_) {
85 s << "none";
86 } else {
87 switch (control_socket_->getType()) {
89 s << "UNIX:'" << control_socket_->getName() << "'";
90 break;
92 s << "HTTP:'" << control_socket_->getUrl().toText() << "'";
93 break;
95 s << "STDOUT";
96 break;
97 }
98 }
99 return (s.str());
100}
101
105 // Set user-context
106 contextToElement(result);
107 // Set model
108 result->set("model", Element::create(model_));
109 // Set boot-update
110 result->set("boot-update", Element::create(boot_update_));
111 // Set subscribe-changes
112 result->set("subscribe-changes", Element::create(subscribe_changes_));
113 // Set validate-changes
114 result->set("validate-changes", Element::create(validate_changes_));
115 // Set control-socket
116 if (control_socket_) {
117 result->set("control-socket", control_socket_->toElement());
118 }
119 return (result);
120}
121
122ostream&
123operator<<(ostream& os, const CfgServer& server) {
124 os << server.toText();
125 return (os);
126}
127
128// *************************** PARSERS ***********************************
129
130// *********************** ControlSocketConfigParser *************************
131
134 CfgControlSocketPtr result;
135 string type_str = getString(ctrl_sock_config, "socket-type");
136 string name = getString(ctrl_sock_config, "socket-name");
137 string url_str = getString(ctrl_sock_config, "socket-url");
138 ConstElementPtr user_context = ctrl_sock_config->get("user-context");
139
140 // Type must be valid.
142 try {
143 type = CfgControlSocket::stringToType(type_str);
144 } catch (exception const& ex) {
145 isc_throw(ConfigError, ex.what() << " '" << type_str << "' ("
146 << getPosition("socket-type", ctrl_sock_config) << ")");
147 }
148
149 // Url must be valid.
150 Url url(url_str);
151 if (!url.isValid()) {
152 isc_throw(ConfigError, "invalid control socket url: "
153 << url.getErrorMessage() << " '" << url_str << "' ("
154 << getPosition("socket-url", ctrl_sock_config) << ")");
155 }
156
157 // Create the control socket.
158 try {
159 result.reset(new CfgControlSocket(type, name, url));
160 } catch (exception const& ex) {
161 isc_throw(ConfigError, ex.what() << " ("
162 << ctrl_sock_config->getPosition() << ")");
163 }
164
165 // Add user-context.
166 if (user_context) {
167 result->setContext(user_context);
168 }
169
170 return (result);
171}
172
173// *********************** ServerConfigParser *************************
174
177 CfgServerPtr result;
178 string model = getString(server_config, "model");
179 ConstElementPtr user_context = server_config->get("user-context");
180 ConstElementPtr ctrl_sock_config = server_config->get("control-socket");
181 CfgControlSocketPtr ctrl_sock;
182 if (ctrl_sock_config) {
184 ctrl_sock = parser.parse(ctrl_sock_config);
185 }
186 try {
187 result.reset(new CfgServer(model, ctrl_sock));
188 } catch (exception const& ex) {
189 isc_throw(ConfigError, ex.what() << " ("
190 << server_config->getPosition() << ")");
191 }
192
193 // Add flags.
194 result->setBootUpdate(getBoolean(server_config, "boot-update"));
195 result->setSubscribeChanges(getBoolean(server_config, "subscribe-changes"));
196 result->setValidateChanges(getBoolean(server_config, "validate-changes"));
197
198 // Add user-context.
199 if (user_context) {
200 result->setContext(user_context);
201 }
202
203 return (result);
204}
205
206} // namespace netconf
207} // 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 ElementPtr create(const Position &pos=ZERO_POSITION())
Definition: data.cc:249
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition: data.cc:304
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.
isc::data::ElementPtr toElement() const override final
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.
isc::data::ElementPtr toElement() const override final
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:699
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:29
boost::shared_ptr< Element > ElementPtr
Definition: data.h:28
ostream & operator<<(ostream &os, const CfgServer &server)
Dumps the contents of a CfgServer as text to a output stream.
std::shared_ptr< CfgServer > CfgServerPtr
Defines a pointer for CfgServer instances.
std::shared_ptr< CfgControlSocket > CfgControlSocketPtr
Defines a pointer for CfgControlSocket 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