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