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>
11#include <cc/simple_parser.h>
14
15using namespace isc::config;
16using namespace isc::dhcp;
17using namespace isc::process;
18using namespace isc::data;
19
20namespace isc {
21namespace netconf {
22
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
33void
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
56std::string
57NetconfCfgMgr::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
163 result->set("Netconf", netconf);
164
165 // Set Logging (not yet)
166
167 return (result);
168}
169
170} // namespace isc::netconf
171} // namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
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:66
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition: data.cc:268
To be removed. Please use ConfigError instead.
isc::data::ElementPtr toElement() const
Unparse a configuration object.
virtual process::ConfigPtr createNewContext()
Creates a new, blank NetconfConfig context.
virtual ~NetconfCfgMgr()
Destructor.
virtual std::string getConfigSummary(const uint32_t selection)
Returns configuration summary in the textual format.
NetconfConfigPtr getNetconfConfig()
Convenience method that returns the Netconf configuration context.
virtual isc::data::ConstElementPtr parse(isc::data::ConstElementPtr config, bool check_only)
Parses configuration of Netconf.
Netconf Configuration Context.
NetconfConfig()
Default constructor.
virtual isc::data::ElementPtr toElement() const
Unparse a configuration object.
void extractConfiguredGlobals(isc::data::ConstElementPtr config)
Saves scalar elements from the global scope of a configuration.
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.
void parse(const NetconfConfigPtr &ctx, const isc::data::ConstElementPtr &config, bool check_only)
Parses the netconf configuration.
static size_t deriveParameters(isc::data::ConstElementPtr global)
Derives (inherits) all parameters from global to more specific scopes.
Base class for all configurations.
Definition: config_base.h:31
Configuration Manager.
Definition: d_cfg_mgr.h:106
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.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
boost::shared_ptr< Element > ElementPtr
Definition: data.h:22
std::vector< HookLibInfo > HookLibsCollection
A storage for information about hook libraries.
Definition: libinfo.h:31
isc::log::Logger netconf_logger(NETCONF_LOGGER_NAME)
Base logger for the netconf agent.
Definition: netconf_log.h:49
boost::shared_ptr< NetconfConfig > NetconfConfigPtr
Pointer to a configuration context.
std::map< std::string, CfgServerPtr > CfgServersMap
Defines a map of CfgServers, keyed by the name.
boost::shared_ptr< ConfigBase > ConfigPtr
Non-const pointer to the SrvConfig.
Definition: config_base.h:119
Defines the logger used by the top-level component of kea-dhcp-ddns.
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