Kea 2.7.1
netconf_cfg_mgr.cc
Go to the documentation of this file.
1// Copyright (C) 2018-2024 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>
13#include <netconf/netconf_log.h>
15
16#include <map>
17#include <sstream>
18
19using namespace isc::config;
20using namespace isc::dhcp;
21using namespace isc::process;
22using namespace isc::data;
23using namespace isc::hooks;
24
25using namespace std;
26
27namespace isc {
28namespace netconf {
29
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
40void
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
58
59string
60NetconfCfgMgr::getConfigSummary(const uint32_t /*selection*/) {
61
63
64 // No globals to print.
65 ostringstream s;
66
67 // Then print managed servers.
68 for (auto const& 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
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) {
138 answer = createAnswer(CONTROL_RESULT_SUCCESS, "Configuration check successful");
139 } else {
140 answer = createAnswer(CONTROL_RESULT_SUCCESS, "Configuration applied successfully.");
141 }
142
143 return (answer);
144}
145
149 // Set user-context
150 contextToElement(netconf);
151 // Add in explicitly configured globals.
152 netconf->setValue(configured_globals_->mapValue());
153 // Set hooks-libraries
154 netconf->set("hooks-libraries", hooks_config_.toElement());
155 // Set managed-servers
156 ElementPtr servers = Element::createMap();
157 for (auto const& serv : *servers_map_) {
158 ElementPtr server = serv.second->toElement();
159 servers->set(serv.first, server);
160 }
161 netconf->set("managed-servers", servers);
162 // Set Netconf
164 result->set("Netconf", netconf);
165 return (result);
166}
167
168list<list<string>>
170 static list<list<string>> const list({
171 {"hooks-libraries", "[]", "parameters", "*"},
172 });
173 return list;
174}
175
176} // namespace netconf
177} // 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.
The Element class represents a piece of data, used by the command channel and configuration parts.
Definition data.h:72
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:304
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
virtual isc::data::ElementPtr toElement() const
Converts to Element representation.
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()
Creates a standard config/command level success answer message (i.e.
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:1420
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
boost::shared_ptr< Element > ElementPtr
Definition data.h:28
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.
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.