Kea  1.9.9-git
netconf_cfg_mgr.cc
Go to the documentation of this file.
1 // Copyright (C) 2018-2021 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) {
128  } else {
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 = ConfigBase::toElement();
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  return (result);
165 }
166 
167 std::list<std::list<std::string>>
169  static std::list<std::list<std::string>> const list({
170  {"hooks-libraries", "[]", "parameters", "*"},
171  });
172  return list;
173 }
174 
175 } // namespace isc::netconf
176 } // namespace isc
static size_t setAllDefaults(const isc::data::ElementPtr &global)
Sets all defaults for Netconf configuration.
const isc::log::MessageID NETCONF_CONFIG_CHECK_FAIL
Netconf Configuration Context.
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
ConstElementPtr createAnswer(const int status_code, const std::string &text, const ConstElementPtr &arg)
const int CONTROL_RESULT_SUCCESS
Status code indicating a successful operation.
Base class for all configurations.
Definition: config_base.h:33
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition: macros.h:32
const int CONTROL_RESULT_ERROR
Status code indicating a general failure.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
Configuration Manager.
Definition: d_cfg_mgr.h:108
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
Definition: user_context.cc:15
std::vector< HookLibInfo > HookLibsCollection
A storage for information about hook libraries.
Definition: libinfo.h:31
void parse(const NetconfConfigPtr &ctx, const isc::data::ConstElementPtr &config, bool check_only)
Parses the netconf configuration.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
virtual std::string getConfigSummary(const uint32_t selection)
Returns configuration summary in the textual format.
std::map< std::string, CfgServerPtr > CfgServersMap
Defines a map of CfgServers, keyed by the name.
To be removed. Please use ConfigError instead.
isc::data::ElementPtr toElement() const
Unparse a configuration object.
std::list< std::list< std::string > > jsonPathsToRedact() const
Return a list of all paths that contain passwords or secrets for kea-netconf.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
virtual ~NetconfCfgMgr()
Destructor.
NetconfConfig()
Default constructor.
isc::log::Logger netconf_logger(NETCONF_LOGGER_NAME)
Base logger for the netconf agent.
Definition: netconf_log.h:49
virtual isc::data::ConstElementPtr parse(isc::data::ConstElementPtr config, bool check_only)
Parses configuration of Netconf.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-dhcp-ddns.
This file contains several functions and constants that are used for handling commands and responses ...
virtual process::ConfigPtr createNewContext()
Creates a new, blank NetconfConfig context.
const isc::log::MessageID NETCONF_CONFIG_FAIL
The Element class represents a piece of data, used by the command channel and configuration parts...
Definition: data.h:66
Contains declarations for loggers used by the Kea netconf agent.
NetconfConfigPtr getNetconfConfig()
Convenience method that returns the Netconf configuration context.
static size_t deriveParameters(isc::data::ConstElementPtr global)
Derives (inherits) all parameters from global to more specific scopes.
void addConfiguredGlobal(const std::string &name, isc::data::ConstElementPtr value)
Adds a parameter to the collection configured globals.
boost::shared_ptr< ConfigBase > ConfigPtr
Non-const pointer to the ConfigBase.
Definition: config_base.h:176
void extractConfiguredGlobals(isc::data::ConstElementPtr config)
Saves scalar elements from the global scope of a configuration.
boost::shared_ptr< NetconfConfig > NetconfConfigPtr
Pointer to a configuration context.