Kea  1.5.0
netconf_cfg_mgr.cc
Go to the documentation of this file.
1 // Copyright (C) 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>
9 #include <netconf/netconf_log.h>
10 #include <netconf/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 netconf {
22 
23 NetconfConfig::NetconfConfig()
24  : configured_globals_(Element::createMap()),
25  servers_map_(new CfgServersMap()) {
26 }
27 
29  : ConfigBase(), configured_globals_(orig.configured_globals_),
30  servers_map_(orig.servers_map_), hooks_config_(orig.hooks_config_) {
31 }
32 
33 void
35  if (config->getType() != Element::map) {
37  "extractConfiguredGlobals must be given a map element");
38  }
39 
40  const std::map<std::string, ConstElementPtr>& values = config->mapValue();
41  for (auto value = values.begin(); value != values.end(); ++value) {
42  if (value->second->getType() != Element::list &&
43  value->second->getType() != Element::map) {
44  addConfiguredGlobal(value->first, value->second);
45  }
46  }
47 }
48 
51 }
52 
54 }
55 
56 std::string
57 NetconfCfgMgr::getConfigSummary(const uint32_t /*selection*/) {
58 
60 
61  // No globals to print.
62  std::ostringstream s;
63 
64  // Then print managed servers.
65  for (auto serv : *ctx->getCfgServersMap()) {
66  if (s.tellp() != 0) {
67  s << " ";
68  }
69  s << serv.first;
70  }
71 
72  if (s.tellp() == 0) {
73  s << "none";
74  }
75 
76  // Finally, print the hook libraries names
77  const isc::hooks::HookLibsCollection libs = ctx->getHooksConfig().get();
78  s << ", " << libs.size() << " lib(s):";
79  for (auto lib = libs.begin(); lib != libs.end(); ++lib) {
80  s << lib->first << " ";
81  }
82 
83  return (s.str());
84 }
85 
88  return (ConfigPtr(new NetconfConfig()));
89 }
90 
93  bool check_only) {
94  // Do a sanity check first.
95  if (!config_set) {
96  isc_throw(DhcpConfigError, "Mandatory config parameter not provided");
97  }
98 
100 
101  // Preserve all scalar global parameters.
102  ctx->extractConfiguredGlobals(config_set);
103 
104  // Set the defaults and derive parameters.
105  ElementPtr cfg = boost::const_pointer_cast<Element>(config_set);
108 
109  // And parse the configuration.
110  ConstElementPtr answer;
111  std::string excuse;
112  try {
113  // Do the actual parsing
114  NetconfSimpleParser parser;
115  parser.parse(ctx, cfg, check_only);
116  } catch (const isc::Exception& ex) {
117  excuse = ex.what();
118  answer = createAnswer(CONTROL_RESULT_ERROR, excuse);
119  } catch (...) {
120  excuse = "undefined configuration parsing error";
121  answer = createAnswer(CONTROL_RESULT_ERROR, excuse);
122  }
123 
124  // At this stage the answer was created only in case of exception.
125  if (answer) {
126  if (check_only) {
127  LOG_ERROR(netconf_logger, NETCONF_CONFIG_CHECK_FAIL).arg(excuse);
128  } else {
129  LOG_ERROR(netconf_logger, NETCONF_CONFIG_FAIL).arg(excuse);
130  }
131  return (answer);
132  }
133 
134  if (check_only) {
136  "Configuration check successful");
137  } else {
139  "Configuration applied successfully.");
140  }
141 
142  return (answer);
143 }
144 
147  ElementPtr netconf = Element::createMap();
148  // Set user-context
149  contextToElement(netconf);
150  // Add in explicitly configured globals.
151  netconf->setValue(configured_globals_->mapValue());
152  // Set hooks-libraries
153  netconf->set("hooks-libraries", hooks_config_.toElement());
154  // Set managed-servers
155  ElementPtr servers = Element::createMap();
156  for (auto serv : *servers_map_) {
157  ConstElementPtr server = serv.second->toElement();
158  servers->set(serv.first, server);
159  }
160  netconf->set("managed-servers", servers);
161  // Set Netconf
162  ElementPtr result = Element::createMap();
163  result->set("Netconf", netconf);
164 
165  // Set Logging (not yet)
166 
167  return (result);
168 }
169 
170 } // namespace isc::netconf
171 } // namespace isc
isc::process::ConfigBase
Base class for all configurations.
Definition: config_base.h:31
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
isc::netconf::NetconfConfig
Netconf Configuration Context.
Definition: netconf_cfg_mgr.h:32
LOG_ERROR
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition: macros.h:32
isc::netconf::NetconfConfig::NetconfConfig
NetconfConfig()
Default constructor.
Definition: netconf_cfg_mgr.cc:23
isc::config::CONTROL_RESULT_SUCCESS
const int CONTROL_RESULT_SUCCESS
Status code indicating a successful operation.
Definition: command_interpreter.h:39
isc::netconf::NetconfCfgMgr::getNetconfConfig
NetconfConfigPtr getNetconfConfig()
Convenience method that returns the Netconf configuration context.
Definition: netconf_cfg_mgr.h:137
simple_parser.h
isc::netconf::NetconfConfig::extractConfiguredGlobals
void extractConfiguredGlobals(isc::data::ConstElementPtr config)
Saves scalar elements from the global scope of a configuration.
Definition: netconf_cfg_mgr.cc:34
isc::config::createAnswer
ConstElementPtr createAnswer(const int status_code, const std::string &text, const ConstElementPtr &arg)
Definition: command_interpreter.cc:33
isc::config
Definition: command_interpreter.cc:23
isc::netconf::NetconfConfig::addConfiguredGlobal
void addConfiguredGlobal(const std::string &name, isc::data::ConstElementPtr value)
Adds a parameter to the collection configured globals.
Definition: netconf_cfg_mgr.h:52
isc::netconf::NetconfCfgMgr::NetconfCfgMgr
NetconfCfgMgr()
Constructor.
Definition: netconf_cfg_mgr.cc:49
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::Exception
This is a base class for exceptions thrown from the DNS library module.
Definition: exceptions/exceptions.h:23
isc::netconf::netconf_logger
isc::log::Logger netconf_logger(NETCONF_LOGGER_NAME)
Base logger for the netconf agent.
Definition: netconf_log.h:49
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_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
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
netconf_cfg_mgr.h
command_interpreter.h
isc::netconf::NetconfConfigPtr
boost::shared_ptr< NetconfConfig > NetconfConfigPtr
Pointer to a configuration context.
Definition: netconf_cfg_mgr.h:21
isc::netconf::NetconfSimpleParser::deriveParameters
static size_t deriveParameters(isc::data::ConstElementPtr global)
Derives (inherits) all parameters from global to more specific scopes.
Definition: bin/netconf/simple_parser.cc:105
isc::dhcp
Definition: ctrl_dhcp4_srv.cc:75
isc::netconf::NetconfCfgMgr::~NetconfCfgMgr
virtual ~NetconfCfgMgr()
Destructor.
Definition: netconf_cfg_mgr.cc:53
isc::netconf::NetconfSimpleParser::parse
void parse(const NetconfConfigPtr &ctx, const isc::data::ConstElementPtr &config, bool check_only)
Parses the netconf configuration.
Definition: bin/netconf/simple_parser.cc:152
isc::netconf::NetconfSimpleParser::setAllDefaults
static size_t setAllDefaults(const isc::data::ElementPtr &global)
Sets all defaults for Netconf configuration.
Definition: bin/netconf/simple_parser.cc:89
isc::process
Definition: config_base.cc:16
isc::data::Element
The Element class represents a piece of data, used by the command channel and configuration parts.
Definition: data.h:66
isc::data::UserContext::contextToElement
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
Definition: user_context.cc:15
isc::netconf::NetconfCfgMgr::getConfigSummary
virtual std::string getConfigSummary(const uint32_t selection)
Returns configuration summary in the textual format.
Definition: netconf_cfg_mgr.cc:57
isc::netconf::CfgServersMap
std::map< std::string, CfgServerPtr > CfgServersMap
Defines a map of CfgServers, keyed by the name.
Definition: netconf_config.h:255
isc::netconf::NetconfCfgMgr::createNewContext
virtual process::ConfigPtr createNewContext()
Creates a new, blank NetconfConfig context.
Definition: netconf_cfg_mgr.cc:87
netconf_log.h
isc::hooks::HooksConfig::toElement
isc::data::ElementPtr toElement() const
Unparse a configuration object.
Definition: hooks_config.cc:106
isc::netconf::NetconfConfig::toElement
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
Definition: netconf_cfg_mgr.cc:146
exceptions.h
isc::netconf::NetconfSimpleParser
Definition: bin/netconf/simple_parser.h:23
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::netconf::NetconfCfgMgr::parse
virtual isc::data::ConstElementPtr parse(isc::data::ConstElementPtr config, bool check_only)
Parses configuration of Netconf.
Definition: netconf_cfg_mgr.cc:92
simple_parser.h