Kea  2.1.7-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 
53 std::string
54 NetconfCfgMgr::getConfigSummary(const uint32_t /*selection*/) {
55 
57 
58  // No globals to print.
59  std::ostringstream s;
60 
61  // Then print managed servers.
62  for (auto serv : *ctx->getCfgServersMap()) {
63  if (s.tellp() != 0) {
64  s << " ";
65  }
66  s << serv.first;
67  }
68 
69  if (s.tellp() == 0) {
70  s << "none";
71  }
72 
73  // Finally, print the hook libraries names
74  const isc::hooks::HookLibsCollection libs = ctx->getHooksConfig().get();
75  s << ", " << libs.size() << " lib(s):";
76  for (auto lib = libs.begin(); lib != libs.end(); ++lib) {
77  s << lib->first << " ";
78  }
79 
80  return (s.str());
81 }
82 
85  return (ConfigPtr(new NetconfConfig()));
86 }
87 
90  bool check_only) {
91  // Do a sanity check first.
92  if (!config_set) {
93  isc_throw(ConfigError, "Mandatory config parameter not provided");
94  }
95 
97 
98  // Preserve all scalar global parameters.
99  ctx->extractConfiguredGlobals(config_set);
100 
101  // Set the defaults and derive parameters.
102  ElementPtr cfg = boost::const_pointer_cast<Element>(config_set);
105 
106  // And parse the configuration.
107  ConstElementPtr answer;
108  std::string excuse;
109  try {
110  // Do the actual parsing
111  NetconfSimpleParser parser;
112  parser.parse(ctx, cfg, check_only);
113  } catch (const isc::Exception& ex) {
114  excuse = ex.what();
115  answer = createAnswer(CONTROL_RESULT_ERROR, excuse);
116  } catch (...) {
117  excuse = "undefined configuration parsing error";
118  answer = createAnswer(CONTROL_RESULT_ERROR, excuse);
119  }
120 
121  // At this stage the answer was created only in case of exception.
122  if (answer) {
123  if (check_only) {
125  } else {
127  }
128  return (answer);
129  }
130 
131  if (check_only) {
133  "Configuration check successful");
134  } else {
136  "Configuration applied successfully.");
137  }
138 
139  return (answer);
140 }
141 
144  ElementPtr netconf = ConfigBase::toElement();
145  // Set user-context
146  contextToElement(netconf);
147  // Add in explicitly configured globals.
148  netconf->setValue(configured_globals_->mapValue());
149  // Set hooks-libraries
150  netconf->set("hooks-libraries", hooks_config_.toElement());
151  // Set managed-servers
152  ElementPtr servers = Element::createMap();
153  for (auto serv : *servers_map_) {
154  ConstElementPtr server = serv.second->toElement();
155  servers->set(serv.first, server);
156  }
157  netconf->set("managed-servers", servers);
158  // Set Netconf
159  ElementPtr result = Element::createMap();
160  result->set("Netconf", netconf);
161  return (result);
162 }
163 
164 std::list<std::list<std::string>>
166  static std::list<std::list<std::string>> const list({
167  {"hooks-libraries", "[]", "parameters", "*"},
168  });
169  return list;
170 }
171 
172 } // namespace isc::netconf
173 } // 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.
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:24
Configuration Manager.
Definition: d_cfg_mgr.h:108
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
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.
An exception that is thrown if an error occurs while configuring any server.
#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.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
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.
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-lfc.
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:70
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
Contains declarations for loggers used by the Kea netconf agent.
std::list< std::list< std::string > > jsonPathsToRedact() const
Return a list of all paths that contain passwords or secrets for kea-netconf.
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.