Kea  2.5.3
netconf_cfg_mgr.cc
Go to the documentation of this file.
1 // Copyright (C) 2018-2022 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 
10 #include <cc/simple_parser.h>
11 #include <exceptions/exceptions.h>
13 #include <netconf/netconf_log.h>
14 #include <netconf/simple_parser.h>
15 
16 #include <map>
17 #include <sstream>
18 
19 using namespace isc::config;
20 using namespace isc::dhcp;
21 using namespace isc::process;
22 using namespace isc::data;
23 using namespace isc::hooks;
24 
25 using namespace std;
26 
27 namespace isc {
28 namespace netconf {
29 
30 NetconfConfig::NetconfConfig()
31  : configured_globals_(Element::createMap()),
32  servers_map_(new CfgServersMap()) {
33 }
34 
36  : ConfigBase(), configured_globals_(orig.configured_globals_),
37  servers_map_(orig.servers_map_), hooks_config_(orig.hooks_config_) {
38 }
39 
40 void
42  if (config->getType() != Element::map) {
44  "extractConfiguredGlobals must be given a map element");
45  }
46 
47  for (auto const& value : config->mapValue()) {
48  if (value.second->getType() != Element::list &&
49  value.second->getType() != Element::map) {
50  addConfiguredGlobal(value.first, value.second);
51  }
52  }
53 }
54 
57 }
58 
59 string
60 NetconfCfgMgr::getConfigSummary(const uint32_t /*selection*/) {
61 
63 
64  // No globals to print.
65  ostringstream s;
66 
67  // Then print managed servers.
68  for (auto serv : *ctx->getCfgServersMap()) {
69  if (s.tellp() != 0) {
70  s << " ";
71  }
72  s << serv.first;
73  }
74 
75  if (s.tellp() == 0) {
76  s << "none";
77  }
78 
79  // Finally, print the hook libraries names
80  const HookLibsCollection libs = ctx->getHooksConfig().get();
81  s << ", " << libs.size() << " lib(s):";
82  for (HookLibInfo const& lib : libs) {
83  s << lib.first << " ";
84  }
85 
86  return (s.str());
87 }
88 
91  return (ConfigPtr(new NetconfConfig()));
92 }
93 
96  bool check_only) {
97  // Do a sanity check first.
98  if (!config_set) {
99  isc_throw(ConfigError, "Mandatory config parameter not provided");
100  }
101 
103 
104  // Preserve all scalar global parameters.
105  ctx->extractConfiguredGlobals(config_set);
106 
107  // Set the defaults and derive parameters.
108  ElementPtr cfg = copy(config_set, 0);
111 
112  // And parse the configuration.
113  ConstElementPtr answer;
114  string excuse;
115  try {
116  // Do the actual parsing
117  NetconfSimpleParser parser;
118  parser.parse(ctx, cfg, check_only);
119  } catch (const isc::Exception& ex) {
120  excuse = ex.what();
121  answer = createAnswer(CONTROL_RESULT_ERROR, excuse);
122  } catch (...) {
123  excuse = "undefined configuration parsing error";
124  answer = createAnswer(CONTROL_RESULT_ERROR, excuse);
125  }
126 
127  // At this stage the answer was created only in case of exception.
128  if (answer) {
129  if (check_only) {
131  } else {
133  }
134  return (answer);
135  }
136 
137  if (check_only) {
139  "Configuration check successful");
140  } else {
142  "Configuration applied successfully.");
143  }
144 
145  return (answer);
146 }
147 
150  ElementPtr netconf = ConfigBase::toElement();
151  // Set user-context
152  contextToElement(netconf);
153  // Add in explicitly configured globals.
154  netconf->setValue(configured_globals_->mapValue());
155  // Set hooks-libraries
156  netconf->set("hooks-libraries", hooks_config_.toElement());
157  // Set managed-servers
158  ElementPtr servers = Element::createMap();
159  for (auto serv : *servers_map_) {
160  ElementPtr server = serv.second->toElement();
161  servers->set(serv.first, server);
162  }
163  netconf->set("managed-servers", servers);
164  // Set Netconf
165  ElementPtr result = Element::createMap();
166  result->set("Netconf", netconf);
167  return (result);
168 }
169 
170 list<list<string>>
172  static list<list<string>> const list({
173  {"hooks-libraries", "[]", "parameters", "*"},
174  });
175  return list;
176 }
177 
178 } // namespace netconf
179 } // namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
An exception that is thrown if an error occurs while configuring any server.
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
The Element class represents a piece of data, used by the command channel and configuration parts.
Definition: data.h:72
isc::data::ElementPtr toElement() const
Unparse a configuration object.
isc::data::ConstElementPtr parse(isc::data::ConstElementPtr config, bool check_only) override final
Parses configuration of Netconf.
std::list< std::list< std::string > > jsonPathsToRedact() const override final
Return a list of all paths that contain passwords or secrets for kea-netconf.
NetconfConfigPtr getNetconfConfig()
Convenience method that returns the Netconf configuration context.
std::string getConfigSummary(const uint32_t selection) override final
Returns configuration summary in the textual format.
process::ConfigPtr createNewContext() override final
Creates a new, blank NetconfConfig context.
Netconf Configuration Context.
NetconfConfig()
Default constructor.
void extractConfiguredGlobals(isc::data::ConstElementPtr config)
Saves scalar elements from the global scope of a configuration.
isc::data::ElementPtr toElement() const override final
Unparse a configuration object.
void addConfiguredGlobal(const std::string &name, isc::data::ConstElementPtr value)
Adds a parameter to the collection configured globals.
static size_t setAllDefaults(const isc::data::ElementPtr &global)
Sets all defaults for Netconf configuration.
static size_t deriveParameters(isc::data::ElementPtr global)
Derives (inherits) all parameters from global to more specific scopes.
void parse(const NetconfConfigPtr &ctx, const isc::data::ElementPtr &config, bool check_only)
Parses the netconf configuration.
Base class for all configurations.
Definition: config_base.h:33
Configuration Manager.
Definition: d_cfg_mgr.h:108
This file contains several functions and constants that are used for handling commands and responses ...
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#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.
ConstElementPtr createAnswer(const int status_code, const std::string &text, const ConstElementPtr &arg)
const int CONTROL_RESULT_SUCCESS
Status code indicating a successful operation.
ElementPtr copy(ConstElementPtr from, int level)
Copy the data up to a nesting level.
Definition: data.cc:1395
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:29
boost::shared_ptr< Element > ElementPtr
Definition: data.h:26
std::pair< std::string, data::ConstElementPtr > HookLibInfo
Entity that holds information about hook libraries and their parameters.
Definition: libinfo.h:28
std::vector< HookLibInfo > HookLibsCollection
A storage for information about hook libraries.
Definition: libinfo.h:31
boost::shared_ptr< NetconfConfig > NetconfConfigPtr
Pointer to a configuration context.
isc::log::Logger netconf_logger(NETCONF_LOGGER_NAME)
Base logger for the netconf agent.
Definition: netconf_log.h:49
const isc::log::MessageID NETCONF_CONFIG_CHECK_FAIL
std::unordered_map< std::string, CfgServerPtr > CfgServersMap
Defines a map of CfgServers, keyed by the name.
const isc::log::MessageID NETCONF_CONFIG_FAIL
boost::shared_ptr< ConfigBase > ConfigPtr
Non-const pointer to the ConfigBase.
Definition: config_base.h:176
Defines the logger used by the top-level component of kea-lfc.
Contains declarations for loggers used by the Kea netconf agent.
void contextToElement(data::ElementPtr map) const
Merge unparse a user_context object.
Definition: user_context.cc:15