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 
25 using namespace std;
26 using namespace isc;
27 using namespace isc::dhcp;
28 using namespace isc::data;
29 using namespace isc::asiolink;
30 
31 namespace isc {
32 namespace process {
33 
34 // *********************** DCfgMgrBase *************************
35 
36 DCfgMgrBase::DCfgMgrBase(ConfigPtr context) {
37  setContext(context);
38 }
39 
40 DCfgMgrBase::~DCfgMgrBase() {
41 }
42 
43 void
44 DCfgMgrBase::resetContext() {
45  ConfigPtr context = createNewContext();
46  setContext(context);
47 }
48 
49 void
50 DCfgMgrBase::setContext(ConfigPtr& context) {
51  if (!context) {
52  isc_throw(DCfgMgrBaseError, "DCfgMgrBase: context cannot be NULL");
53  }
54 
55  context_ = context;
56 }
57 
59 DCfgMgrBase::simpleParseConfig(isc::data::ConstElementPtr config_set,
60  bool check_only,
61  const std::function<void()>& post_config_cb) {
62  if (!config_set) {
63  return (isc::config::createAnswer(1,
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_;
76  resetContext();
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 
126 void
127 DCfgMgrBase::setCfgDefaults(isc::data::ElementPtr) {
128 }
129 
131 DCfgMgrBase::parse(isc::data::ConstElementPtr, bool) {
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
#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:20
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
std::string answerToText(const ConstElementPtr &msg)
Exception thrown if the configuration manager encounters an error.
Definition: d_cfg_mgr.h:27
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
ConstElementPtr parseAnswer(int &rcode, const ConstElementPtr &msg)
Defines the logger used by the top-level component of kea-dhcp-ddns.
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
boost::shared_ptr< ConfigBase > ConfigPtr
Non-const pointer to the SrvConfig.
Definition: config_base.h:119