Kea  2.1.7-git
d_cfg_mgr.cc
Go to the documentation of this file.
1 // Copyright (C) 2013-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>
8 
10 #include <dhcp/libdhcp++.h>
11 #include <process/d_log.h>
12 #include <process/d_cfg_mgr.h>
13 #include <process/daemon.h>
14 #include <process/redact_config.h>
15 #include <util/encode/hex.h>
16 #include <util/strutil.h>
17 
18 #include <boost/foreach.hpp>
19 #include <boost/lexical_cast.hpp>
20 #include <boost/algorithm/string.hpp>
21 
22 #include <limits>
23 #include <iostream>
24 #include <vector>
25 #include <map>
26 
27 using namespace std;
28 using namespace isc;
29 using namespace isc::dhcp;
30 using namespace isc::data;
31 using namespace isc::asiolink;
32 
33 namespace isc {
34 namespace process {
35 
36 // *********************** DCfgMgrBase *************************
37 
38 DCfgMgrBase::DCfgMgrBase(ConfigPtr context) {
39  setContext(context);
40 }
41 
42 DCfgMgrBase::~DCfgMgrBase() {
43 }
44 
45 void
46 DCfgMgrBase::resetContext() {
47  ConfigPtr context = createNewContext();
48  setContext(context);
49 }
50 
51 void
52 DCfgMgrBase::setContext(ConfigPtr& context) {
53  if (!context) {
54  isc_throw(DCfgMgrBaseError, "DCfgMgrBase: context cannot be NULL");
55  }
56 
57  context_ = context;
58 }
59 
62  ConstElementPtr result(config);
63  for (std::list<std::string>& json_path : jsonPathsToRedact()) {
64  result = isc::process::redactConfig(result, json_path);
65  }
66  return result;
67 }
68 
69 list<list<string>> DCfgMgrBase::jsonPathsToRedact() const {
70  static list<list<string>> const list;
71  return list;
72 }
73 
75 DCfgMgrBase::simpleParseConfig(isc::data::ConstElementPtr config_set,
76  bool check_only,
77  const std::function<void()>& post_config_cb) {
78  if (!config_set) {
79  return (isc::config::createAnswer(1,
80  std::string("Can't parse NULL config")));
81  }
83  .arg(redactConfig(config_set)->str());
84 
85  // The parsers implement data inheritance by directly accessing
86  // configuration context. For this reason the data parsers must store
87  // the parsed data into context immediately. This may cause data
88  // inconsistency if the parsing operation fails after the context has been
89  // modified. We need to preserve the original context here
90  // so as we can rollback changes when an error occurs.
91  ConfigPtr original_context = context_;
92  resetContext();
93  bool rollback = false;
94 
95  // Answer will hold the result returned to the caller.
96  ConstElementPtr answer;
97 
98  try {
99  // Logging is common so factor it.
100  Daemon::configureLogger(config_set, context_);
101 
102  // Let's call the actual implementation
103  answer = parse(config_set, check_only);
104 
105  // and check the response returned.
106  int code = 0;
107  isc::config::parseAnswer(code, answer);
108 
109  // Everything was fine. Configuration set processed successfully.
110  if (!check_only) {
111  if (code == 0) {
112  // Call the callback only when parsing was successful.
113  if (post_config_cb) {
114  post_config_cb();
115  }
116  LOG_INFO(dctl_logger, DCTL_CONFIG_COMPLETE).arg(getConfigSummary(0));
117  // Set the last commit timestamp.
118  auto now = boost::posix_time::second_clock::universal_time();
119  context_->setLastCommitTime(now);
120  } else {
121  rollback = true;
122  }
123 
124  // Use the answer provided.
125  //answer = isc::config::createAnswer(0, "Configuration committed.");
126  } else {
128  .arg(getConfigSummary(0))
129  .arg(config::answerToText(answer));
130  }
131 
132  } catch (const std::exception& ex) {
133  LOG_ERROR(dctl_logger, DCTL_PARSER_FAIL).arg(ex.what());
134  answer = isc::config::createAnswer(1, ex.what());
135  rollback = true;
136  }
137 
138  if (check_only) {
139  // If this is a configuration check only, then don't actually apply
140  // the configuration and reverse to the previous one.
141  context_ = original_context;
142  }
143 
144  if (rollback) {
145  // An error occurred, so make sure that we restore original context.
146  context_ = original_context;
147  }
148 
149  return (answer);
150 }
151 
152 
153 void
154 DCfgMgrBase::setCfgDefaults(isc::data::ElementPtr) {
155 }
156 
158 DCfgMgrBase::parse(isc::data::ConstElementPtr, bool) {
159  isc_throw(DCfgMgrBaseError, "This class does not implement simple parser paradigm yet");
160 }
161 
162 } // end of isc::dhcp namespace
163 } // end of isc namespace
const isc::log::MessageID DCTL_PARSER_FAIL
#define LOG_INFO(LOGGER, MESSAGE)
Macro to conveniently test info output and log it.
Definition: macros.h:20
ConstElementPtr createAnswer(const int status_code, const std::string &text, const ConstElementPtr &arg)
#define LOG_ERROR(LOGGER, MESSAGE)
Macro to conveniently test error output and log it.
Definition: macros.h:32
isc::log::Logger dctl_logger("dctl")
Defines the logger used within libkea-process library.
Definition: d_log.h:18
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
STL namespace.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
std::string answerToText(const ConstElementPtr &msg)
const isc::log::MessageID DCTL_CONFIG_CHECK_COMPLETE
Exception thrown if the configuration manager encounters an error.
Definition: d_cfg_mgr.h:29
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
ConstElementPtr redactConfig(ConstElementPtr const &element, list< string > const &json_path)
Redact a configuration.
ConstElementPtr parseAnswer(int &rcode, const ConstElementPtr &msg)
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 ...
const int DBGLVL_COMMAND
This debug level is reserved for logging the exchange of messages/commands between processes...
Definition: log_dbglevels.h:54
#define LOG_DEBUG(LOGGER, LEVEL, MESSAGE)
Macro to conveniently test debug output and log it.
Definition: macros.h:14
const isc::log::MessageID DCTL_CONFIG_COMPLETE
const isc::log::MessageID DCTL_CONFIG_START
boost::shared_ptr< ConfigBase > ConfigPtr
Non-const pointer to the ConfigBase.
Definition: config_base.h:176