Kea  1.5.0
ca_cfg_mgr.cc
Go to the documentation of this file.
1 // Copyright (C) 2016-2018 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 #include <agent/ca_cfg_mgr.h>
9 #include <agent/ca_log.h>
10 #include <agent/simple_parser.h>
11 #include <cc/simple_parser.h>
12 #include <cc/command_interpreter.h>
13 #include <exceptions/exceptions.h>
14 
15 using namespace isc::config;
16 using namespace isc::dhcp;
17 using namespace isc::process;
18 using namespace isc::data;
19 
20 namespace isc {
21 namespace agent {
22 
23 CtrlAgentCfgContext::CtrlAgentCfgContext()
24  :http_host_(""), http_port_(0) {
25 }
26 
28  : ConfigBase(), ctrl_sockets_(orig.ctrl_sockets_),
29  http_host_(orig.http_host_), http_port_(orig.http_port_),
30  hooks_config_(orig.hooks_config_) {
31 }
32 
35 }
36 
38 }
39 
40 std::string
41 CtrlAgentCfgMgr::getConfigSummary(const uint32_t /*selection*/) {
42 
44 
45  // First print the http stuff.
46  std::ostringstream s;
47  s << "listening on " << ctx->getHttpHost() << ", port "
48  << ctx->getHttpPort() << ", control sockets: ";
49 
50  // Then print the control-sockets
51  s << ctx->getControlSocketInfoSummary();
52 
53  // Finally, print the hook libraries names
54  const isc::hooks::HookLibsCollection libs = ctx->getHooksConfig().get();
55  s << ", " << libs.size() << " lib(s):";
56  for (auto lib = libs.begin(); lib != libs.end(); ++lib) {
57  s << lib->first << " ";
58  }
59 
60  return (s.str());
61 }
62 
65  return (ConfigPtr(new CtrlAgentCfgContext()));
66 }
67 
69 CtrlAgentCfgMgr::parse(isc::data::ConstElementPtr config_set, bool check_only) {
70  // Do a sanity check first.
71  if (!config_set) {
72  isc_throw(DhcpConfigError, "Mandatory config parameter not provided");
73  }
74 
76 
77  // Set the defaults
78  ElementPtr cfg = boost::const_pointer_cast<Element>(config_set);
80 
81  // And parse the configuration.
82  ConstElementPtr answer;
83  std::string excuse;
84  try {
85  // Do the actual parsing
86  AgentSimpleParser parser;
87  parser.parse(ctx, cfg, check_only);
88  } catch (const isc::Exception& ex) {
89  excuse = ex.what();
90  answer = createAnswer(CONTROL_RESULT_ERROR, excuse);
91  } catch (...) {
92  excuse = "undefined configuration parsing error";
93  answer = createAnswer(CONTROL_RESULT_ERROR, excuse);
94  }
95 
96  // At this stage the answer was created only in case of exception.
97  if (answer) {
98  if (check_only) {
99  LOG_ERROR(agent_logger, CTRL_AGENT_CONFIG_CHECK_FAIL).arg(excuse);
100  } else {
101  LOG_ERROR(agent_logger, CTRL_AGENT_CONFIG_FAIL).arg(excuse);
102  }
103  return (answer);
104  }
105 
106  if (check_only) {
108  "Configuration check successful");
109  } else {
111  "Configuration applied successfully.");
112  }
113 
114  return (answer);
115 }
116 
118 CtrlAgentCfgContext::getControlSocketInfo(const std::string& service) const {
119  auto si = ctrl_sockets_.find(service);
120  return ((si != ctrl_sockets_.end()) ? si->second : ConstElementPtr());
121 }
122 
123 void
125  const std::string& service) {
126  ctrl_sockets_[service] = control_socket;
127 }
128 
129 std::string
131  std::ostringstream s;
132  for (auto si = ctrl_sockets_.cbegin(); si != ctrl_sockets_.end(); ++si) {
133  if (s.tellp() != 0) {
134  s << " ";
135  }
136  s << si->first;
137  }
138 
139  if (s.tellp() == 0) {
140  s << "none";
141  }
142 
143  return (s.str());
144 }
145 
148  ElementPtr ca = Element::createMap();
149  // Set user-context
150  contextToElement(ca);
151  // Set http-host
152  ca->set("http-host", Element::create(http_host_));
153  // Set http-port
154  ca->set("http-port", Element::create(static_cast<int64_t>(http_port_)));
155  // Set hooks-libraries
156  ca->set("hooks-libraries", hooks_config_.toElement());
157  // Set control-sockets
158  ElementPtr control_sockets = Element::createMap();
159  for (auto si = ctrl_sockets_.cbegin(); si != ctrl_sockets_.cend(); ++si) {
160  ConstElementPtr socket = UserContext::toElement(si->second);
161  control_sockets->set(si->first, socket);
162  }
163  ca->set("control-sockets", control_sockets);
164  // Set Control-agent
165  ElementPtr result = Element::createMap();
166  result->set("Control-agent", ca);
167 
168  // Set Logging (not yet)
169 
170  return (result);
171 }
172 
173 } // namespace isc::agent
174 } // namespace isc
isc::process::ConfigBase
Base class for all configurations.
Definition: config_base.h:31
isc::agent::CtrlAgentCfgMgr::~CtrlAgentCfgMgr
virtual ~CtrlAgentCfgMgr()
Destructor.
Definition: ca_cfg_mgr.cc:37
isc::agent::CtrlAgentCfgContext::getControlSocketInfoSummary
std::string getControlSocketInfoSummary() const
Returns socket configuration summary in a textual format.
Definition: ca_cfg_mgr.cc:130
isc::dhcp::DhcpConfigError
To be removed. Please use ConfigError instead.
Definition: dhcp_config_error.h:58
isc::process::DCfgMgrBase
Configuration Manager.
Definition: d_cfg_mgr.h:106
LOG_ERROR
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition: macros.h:32
isc::agent::CtrlAgentCfgMgr::createNewContext
virtual process::ConfigPtr createNewContext()
Creates a new, blank CtrlAgentCfgContext context.
Definition: ca_cfg_mgr.cc:64
isc::config::CONTROL_RESULT_SUCCESS
const int CONTROL_RESULT_SUCCESS
Status code indicating a successful operation.
Definition: command_interpreter.h:39
isc::agent::CtrlAgentCfgMgr::getConfigSummary
virtual std::string getConfigSummary(const uint32_t selection)
Returns configuration summary in the textual format.
Definition: ca_cfg_mgr.cc:41
ca_log.h
simple_parser.h
isc::config::createAnswer
ConstElementPtr createAnswer(const int status_code, const std::string &text, const ConstElementPtr &arg)
Definition: command_interpreter.cc:33
isc::agent::agent_logger
isc::log::Logger agent_logger("ctrl-agent")
Control Agent logger.
Definition: ca_log.h:18
isc::config
Definition: command_interpreter.cc:23
isc::agent::CtrlAgentCfgMgr::CtrlAgentCfgMgr
CtrlAgentCfgMgr()
Constructor.
Definition: ca_cfg_mgr.cc:33
isc::agent::AgentSimpleParser::parse
void parse(const CtrlAgentCfgContextPtr &ctx, const isc::data::ConstElementPtr &config, bool check_only)
Parses the control agent configuration.
Definition: bin/agent/simple_parser.cc:83
isc::agent::CtrlAgentCfgContext::toElement
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
Definition: ca_cfg_mgr.cc:147
ca_cfg_mgr.h
isc::agent::CtrlAgentCfgMgr::getCtrlAgentCfgContext
CtrlAgentCfgContextPtr getCtrlAgentCfgContext()
Convenience method that returns the Control Agent configuration context.
Definition: ca_cfg_mgr.h:171
isc::config::CONTROL_RESULT_ERROR
const int CONTROL_RESULT_ERROR
Status code indicating a general failure.
Definition: command_interpreter.h:42
isc::data
Definition: cfg_to_element.h:25
isc::agent::CtrlAgentCfgContext::getControlSocketInfo
isc::data::ConstElementPtr getControlSocketInfo(const std::string &service) const
Returns information about control socket.
Definition: ca_cfg_mgr.cc:118
isc::Exception
This is a base class for exceptions thrown from the DNS library module.
Definition: exceptions/exceptions.h:23
isc::process::ConfigPtr
boost::shared_ptr< ConfigBase > ConfigPtr
Non-const pointer to the SrvConfig.
Definition: config_base.h:119
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition: exceptions/exceptions.cc:32
isc::agent::CtrlAgentCfgContext
Control Agent Configuration Context.
Definition: ca_cfg_mgr.h:31
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
command_interpreter.h
isc::dhcp
Definition: ctrl_dhcp4_srv.cc:75
isc::agent::CtrlAgentCfgContext::CtrlAgentCfgContext
CtrlAgentCfgContext()
Default constructor.
Definition: ca_cfg_mgr.cc:23
isc::process
Definition: config_base.cc:16
isc::data::UserContext::contextToElement
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
Definition: user_context.cc:15
isc::agent::CtrlAgentCfgContext::setControlSocketInfo
void setControlSocketInfo(const isc::data::ConstElementPtr &control_socket, const std::string &service)
Sets information about the control socket.
Definition: ca_cfg_mgr.cc:124
isc::agent::CtrlAgentCfgContextPtr
boost::shared_ptr< CtrlAgentCfgContext > CtrlAgentCfgContextPtr
Pointer to a configuration context.
Definition: ca_cfg_mgr.h:20
isc::agent::CtrlAgentCfgMgr::parse
virtual isc::data::ConstElementPtr parse(isc::data::ConstElementPtr config, bool check_only)
Parses configuration of the Control Agent.
Definition: ca_cfg_mgr.cc:69
isc::agent::AgentSimpleParser
Definition: bin/agent/simple_parser.h:23
isc::hooks::HooksConfig::toElement
isc::data::ElementPtr toElement() const
Unparse a configuration object.
Definition: hooks_config.cc:106
exceptions.h
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
isc::hooks::HookLibsCollection
std::vector< HookLibInfo > HookLibsCollection
A storage for information about hook libraries.
Definition: libinfo.h:31
isc::agent::AgentSimpleParser::setAllDefaults
static size_t setAllDefaults(const isc::data::ElementPtr &global)
Sets all defaults for Control Agent configuration.
Definition: bin/agent/simple_parser.cc:54
simple_parser.h