Kea 1.5.0
d_cfg_mgr.cc
Go to the documentation of this file.
1// Copyright (C) 2013-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>
8
10#include <dhcp/libdhcp++.h>
11#include <process/d_log.h>
12#include <process/d_cfg_mgr.h>
13#include <util/encode/hex.h>
14#include <util/strutil.h>
15
16#include <boost/foreach.hpp>
17#include <boost/lexical_cast.hpp>
18#include <boost/algorithm/string.hpp>
19
20#include <limits>
21#include <iostream>
22#include <vector>
23#include <map>
24
25using namespace std;
26using namespace isc;
27using namespace isc::dhcp;
28using namespace isc::data;
29using namespace isc::asiolink;
30
31namespace isc {
32namespace process {
33
34// *********************** DCfgMgrBase *************************
35
37 setContext(context);
38}
39
41}
42
43void
45 ConfigPtr context = createNewContext();
46 setContext(context);
47}
48
49void
51 if (!context) {
52 isc_throw(DCfgMgrBaseError, "DCfgMgrBase: context cannot be NULL");
53 }
54
55 context_ = context;
56}
57
60 bool check_only,
61 const std::function<void()>& post_config_cb) {
62 if (!config_set) {
64 std::string("Can't parse NULL config")));
65 }
67 DCTL_CONFIG_START).arg(config_set->str());
68
69 // The parsers implement data inheritance by directly accessing
70 // configuration context. For this reason the data parsers must store
71 // the parsed data into context immediately. This may cause data
72 // inconsistency if the parsing operation fails after the context has been
73 // modified. We need to preserve the original context here
74 // so as we can rollback changes when an error occurs.
75 ConfigPtr original_context = context_;
77
78 // Answer will hold the result returned to the caller.
79 ConstElementPtr answer;
80
81 try {
82 // Let's call the actual implementation
83 answer = parse(config_set, check_only);
84
85 // and check the response returned.
86 int code = 0;
87 isc::config::parseAnswer(code, answer);
88
89 // Everything was fine. Configuration set processed successfully.
90 if (!check_only) {
91 if (post_config_cb) {
92 post_config_cb();
93 }
94
95 if (code == 0) {
96 LOG_INFO(dctl_logger, DCTL_CONFIG_COMPLETE).arg(getConfigSummary(0));
97 }
98
99 // Use the answer provided.
100 //answer = isc::config::createAnswer(0, "Configuration committed.");
101 } else {
102 LOG_INFO(dctl_logger, DCTL_CONFIG_CHECK_COMPLETE)
103 .arg(getConfigSummary(0))
104 .arg(config::answerToText(answer));
105 }
106
107 } catch (const std::exception& ex) {
108 LOG_ERROR(dctl_logger, DCTL_PARSER_FAIL).arg(ex.what());
109 answer = isc::config::createAnswer(1, ex.what());
110
111 // An error occurred, so make sure that we restore original context.
112 context_ = original_context;
113 return (answer);
114 }
115
116 if (check_only) {
117 // If this is a configuration check only, then don't actually apply
118 // the configuration and reverse to the previous one.
119 context_ = original_context;
120 }
121
122 return (answer);
123}
124
125
126void
128}
129
132 isc_throw(DCfgMgrBaseError, "This class does not implement simple parser paradigm yet");
133}
134
135}; // end of isc::dhcp namespace
136}; // end of isc namespace
Exception thrown if the configuration manager encounters an error.
Definition: d_cfg_mgr.h:27
virtual void setCfgDefaults(isc::data::ElementPtr mutable_config)
Adds default values to the given config.
Definition: d_cfg_mgr.cc:127
DCfgMgrBase(ConfigPtr context)
Constructor.
Definition: d_cfg_mgr.cc:36
virtual isc::data::ConstElementPtr parse(isc::data::ConstElementPtr config, bool check_only)
Parses actual configuration.
Definition: d_cfg_mgr.cc:131
virtual ~DCfgMgrBase()
Destructor.
Definition: d_cfg_mgr.cc:40
isc::data::ConstElementPtr simpleParseConfig(isc::data::ConstElementPtr config, bool check_only=false, const std::function< void()> &post_config_cb=nullptr)
Acts as the receiver of new configurations.
Definition: d_cfg_mgr.cc:59
void resetContext()
Replaces existing context with a new, empty context.
Definition: d_cfg_mgr.cc:44
void setContext(ConfigPtr &context)
Update the current context.
Definition: d_cfg_mgr.cc:50
virtual ConfigPtr createNewContext()=0
Abstract factory which creates a context instance.
virtual std::string getConfigSummary(const uint32_t selection)=0
Returns configuration summary in the textual format.
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
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition: macros.h:20
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition: macros.h:14
ConstElementPtr createAnswer()
Creates a standard config/command level success answer message (i.e.
ConstElementPtr parseAnswer(int &rcode, const ConstElementPtr &msg)
std::string answerToText(const ConstElementPtr &msg)
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
boost::shared_ptr< Element > ElementPtr
Definition: data.h:22
const int DBGLVL_COMMAND
This debug level is reserved for logging the exchange of messages/commands between processes,...
Definition: log_dbglevels.h:54
isc::log::Logger dctl_logger("dctl")
Defines the logger used within libkea-process library.
Definition: d_log.h:18
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.