| Kea 3.0.2-git

netconf_config.cc
Go to the documentation of this file.
1// Copyright (C) 2018-2024 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>
10#include <exceptions/exceptions.h>
11#include <netconf/netconf_cfg_mgr.h>
12#include <netconf/netconf_log.h>
13
14#include <sstream>
15#include <string>
16
17 using namespace std;
18 using namespace isc::process;
19 using namespace isc::data;
20 using namespace isc::http;
21
22 namespace isc {
23 namespace netconf {
24
25// *********************** CfgControlSocket *************************
26
27 CfgControlSocket::CfgControlSocket(Type type, const string& name,
28 const Url& url)
29 : type_(type), name_(name), url_(url) {
30}
31
32 CfgControlSocket::Type
33 CfgControlSocket::stringToType(const string& type) {
34 if (type == "unix") {
35 return (CfgControlSocket::Type::UNIX);
36 } else if (type == "http") {
37 return (CfgControlSocket::Type::HTTP);
38 } else if (type == "stdout") {
39 return (CfgControlSocket::Type::STDOUT);
40 }
41
42 isc_throw(BadValue, "Unknown control socket type: " << type);
43}
44
45 const string
46 CfgControlSocket::typeToString(CfgControlSocket::Type type) {
47 switch (type) {
48 case CfgControlSocket::Type::UNIX:
49 return ("unix");
50 case CfgControlSocket::Type::HTTP:
51 return ("http");
52 case CfgControlSocket::Type::STDOUT:
53 return ("stdout");
54 default:
55 isc_throw(BadValue, "Unknown control socket type: " << type);
56 }
57}
58
59 ElementPtr
60 CfgControlSocket::toElement() const {
61 ElementPtr result = Element::createMap();
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 *************************
74 CfgServer::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
80 string
81 CfgServer::toText() const {
82 ostringstream s;
83 s << "model: " << model_ << ", control socker: ";
84 if (!control_socket_) {
85 s << "none";
86 } else {
87 switch (control_socket_->getType()) {
88 case CfgControlSocket::Type::UNIX:
89 s << "UNIX:'" << control_socket_->getName() << "'";
90 break;
91 case CfgControlSocket::Type::HTTP:
92 s << "HTTP:'" << control_socket_->getUrl().toText() << "'";
93 break;
94 case CfgControlSocket::Type::STDOUT:
95 s << "STDOUT";
96 break;
97 }
98 }
99 return (s.str());
100}
101
102 ElementPtr
103 CfgServer::toElement() const {
104 ElementPtr result = Element::createMap();
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&
123 operator<<(ostream& os, const CfgServer& server) {
124 os << server.toText();
125 return (os);
126}
127
128// *************************** PARSERS ***********************************
129
130// *********************** ControlSocketConfigParser *************************
131
132 CfgControlSocketPtr
133 ControlSocketConfigParser::parse(ConstElementPtr ctrl_sock_config) {
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.
141 CfgControlSocket::Type type;
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
175 CfgServerPtr
176 ServerConfigParser::parse(ConstElementPtr server_config) {
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) {
183 ControlSocketConfigParser parser;
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
Element::create
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition data.cc:249
Element::createMap
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition exceptions/exceptions.h:132
isc::ConfigError
An exception that is thrown if an error occurs while configuring any server.
Definition dhcp_config_error.h:43
isc::data::SimpleParser::getPosition
static const data::Element::Position & getPosition(const std::string &name, const data::ConstElementPtr parent)
Utility method that returns position of an element.
Definition lib/cc/simple_parser.cc:158
isc::data::SimpleParser::getString
static std::string getString(isc::data::ConstElementPtr scope, const std::string &name)
Returns a string parameter from a scope.
Definition lib/cc/simple_parser.cc:60
isc::data::SimpleParser::getBoolean
static bool getBoolean(isc::data::ConstElementPtr scope, const std::string &name)
Returns a boolean parameter from a scope.
Definition lib/cc/simple_parser.cc:108
isc::http::Url
Represents an URL.
Definition url.h:20
isc::http::Url::getErrorMessage
std::string getErrorMessage() const
Returns parsing error message.
Definition url.h:52
isc::http::Url::isValid
bool isValid() const
Checks if the URL is valid.
Definition url.h:47
isc::netconf::CfgControlSocket
Represents a Control Socket.
Definition netconf_config.h:70
isc::netconf::CfgControlSocket::toElement
isc::data::ElementPtr toElement() const override final
Unparse a configuration object.
Definition netconf_config.cc:60
isc::netconf::CfgControlSocket::Type
Type
Defines the list of possible control socket types.
Definition netconf_config.h:73
isc::netconf::CfgControlSocket::STDOUT
@ STDOUT
Definition netconf_config.h:76
isc::netconf::CfgControlSocket::HTTP
@ HTTP
Definition netconf_config.h:75
isc::netconf::CfgControlSocket::UNIX
@ UNIX
Definition netconf_config.h:74
isc::netconf::CfgControlSocket::CfgControlSocket
CfgControlSocket(Type type, const std::string &name, const isc::http::Url &url)
Constructor.
Definition netconf_config.cc:27
isc::netconf::CfgControlSocket::typeToString
static const std::string typeToString(CfgControlSocket::Type type)
Converts CfgControlSocket::Type to string.
Definition netconf_config.cc:46
isc::netconf::CfgControlSocket::stringToType
static Type stringToType(const std::string &type)
Converts socket type name to CfgControlSocket::Type.
Definition netconf_config.cc:33
isc::netconf::CfgServer
Represents a Managed CfgServer.
Definition netconf_config.h:149
isc::netconf::CfgServer::CfgServer
CfgServer(const std::string &model, CfgControlSocketPtr ctrl_sock)
Constructor.
Definition netconf_config.cc:74
isc::netconf::CfgServer::toElement
isc::data::ElementPtr toElement() const override final
Unparse a configuration object.
Definition netconf_config.cc:103
isc::netconf::CfgServer::toText
std::string toText() const
Returns a text representation for the server.
Definition netconf_config.cc:81
isc::netconf::ControlSocketConfigParser
Parser for CfgControlSocket.
Definition netconf_config.h:293
isc::netconf::ControlSocketConfigParser::parse
CfgControlSocketPtr parse(data::ConstElementPtr ctrl_sock_config)
Performs the actual parsing of the given "control-socket" element.
Definition netconf_config.cc:133
isc::netconf::ServerConfigParser::parse
CfgServerPtr parse(data::ConstElementPtr server_config)
Performs the actual parsing of the given value from the "managed-servers" map.
Definition netconf_config.cc:176
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
io_error.h
isc::data
Definition base_stamped_element.cc:12
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
isc::http
Definition auth_config.h:19
isc::netconf
Definition control_socket.cc:20
isc::netconf::operator<<
ostream & operator<<(ostream &os, const CfgServer &server)
Dumps the contents of a CfgServer as text to a output stream.
Definition netconf_config.cc:123
isc::netconf::CfgServerPtr
std::shared_ptr< CfgServer > CfgServerPtr
Defines a pointer for CfgServer instances.
Definition netconf_config.h:272
isc::netconf::CfgControlSocketPtr
std::shared_ptr< CfgControlSocket > CfgControlSocketPtr
Defines a pointer for CfgControlSocket instances.
Definition netconf_config.h:143
isc::process
Definition cb_ctl_base.h:22
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
netconf_cfg_mgr.h
netconf_log.h
Contains declarations for loggers used by the Kea netconf agent.
isc::data::UserContext::contextToElement
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
Definition user_context.cc:15