Kea  2.3.5-git
d2_controller.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 
9 #include <cfgrpt/config_report.h>
10 #include <config/command_mgr.h>
11 #include <d2/d2_controller.h>
12 #include <d2/d2_process.h>
13 #include <d2/parser_context.h>
14 #include <stats/stats_mgr.h>
15 
16 #include <stdlib.h>
17 
18 using namespace isc::config;
19 using namespace isc::process;
20 using namespace isc::stats;
21 namespace ph = std::placeholders;
22 
23 namespace isc {
24 namespace d2 {
25 
28 const char* D2Controller::d2_app_name_ = "DhcpDdns";
29 
31 const char* D2Controller::d2_bin_name_ = "kea-dhcp-ddns";
32 
34 D2Controller::instance() {
35  // If the instance hasn't been created yet, create it. Note this method
36  // must use the base class singleton instance methods.
37  if (!getController()) {
38  DControllerBasePtr controller_ptr(new D2Controller());
39  setController(controller_ptr);
40  }
41 
42  return (getController());
43 }
44 
45 DProcessBase* D2Controller::createProcess() {
46  // Instantiate and return an instance of the D2 application process. Note
47  // that the process is passed the controller's io_service.
48  return (new D2Process(getAppName().c_str(), getIOService()));
49 }
50 
51 D2Controller::D2Controller()
52  : DControllerBase(d2_app_name_, d2_bin_name_) {
53 }
54 
55 void
56 D2Controller::registerCommands() {
57  // These are the commands always supported by the D2 server.
58  // Please keep the list in alphabetic order.
59  CommandMgr::instance().registerCommand(BUILD_REPORT_COMMAND,
60  std::bind(&D2Controller::buildReportHandler, this, ph::_1, ph::_2));
61 
62  CommandMgr::instance().registerCommand(CONFIG_GET_COMMAND,
63  std::bind(&D2Controller::configGetHandler, this, ph::_1, ph::_2));
64 
65  CommandMgr::instance().registerCommand(CONFIG_RELOAD_COMMAND,
66  std::bind(&D2Controller::configReloadHandler, this, ph::_1, ph::_2));
67 
68  CommandMgr::instance().registerCommand(CONFIG_SET_COMMAND,
69  std::bind(&D2Controller::configSetHandler, this, ph::_1, ph::_2));
70 
71  CommandMgr::instance().registerCommand(CONFIG_TEST_COMMAND,
72  std::bind(&D2Controller::configTestHandler, this, ph::_1, ph::_2));
73 
74  CommandMgr::instance().registerCommand(CONFIG_WRITE_COMMAND,
75  std::bind(&D2Controller::configWriteHandler, this, ph::_1, ph::_2));
76 
77  CommandMgr::instance().registerCommand(SHUT_DOWN_COMMAND,
78  std::bind(&D2Controller::shutdownHandler, this, ph::_1, ph::_2));
79 
80  CommandMgr::instance().registerCommand(STATUS_GET_COMMAND,
81  std::bind(&DControllerBase::statusGetHandler, this, ph::_1, ph::_2));
82 
83  CommandMgr::instance().registerCommand(VERSION_GET_COMMAND,
84  std::bind(&D2Controller::versionGetHandler, this, ph::_1, ph::_2));
85 
86  // Register statistic related commands.
87  CommandMgr::instance().registerCommand("statistic-get",
88  std::bind(&StatsMgr::statisticGetHandler, ph::_1, ph::_2));
89 
90  CommandMgr::instance().registerCommand("statistic-get-all",
91  std::bind(&StatsMgr::statisticGetAllHandler, ph::_1, ph::_2));
92 
93  CommandMgr::instance().registerCommand("statistic-reset",
94  std::bind(&StatsMgr::statisticResetHandler, ph::_1, ph::_2));
95 
96  CommandMgr::instance().registerCommand("statistic-reset-all",
97  std::bind(&StatsMgr::statisticResetAllHandler, ph::_1, ph::_2));
98 }
99 
100 void
101 D2Controller::deregisterCommands() {
102  try {
103  // Close the command socket (if it exists).
105 
106  // Deregister any registered commands (please keep in alphabetic order)
107  CommandMgr::instance().deregisterCommand(BUILD_REPORT_COMMAND);
108  CommandMgr::instance().deregisterCommand(CONFIG_GET_COMMAND);
109  CommandMgr::instance().deregisterCommand(CONFIG_RELOAD_COMMAND);
110  CommandMgr::instance().deregisterCommand(CONFIG_SET_COMMAND);
111  CommandMgr::instance().deregisterCommand(CONFIG_TEST_COMMAND);
112  CommandMgr::instance().deregisterCommand(CONFIG_WRITE_COMMAND);
113  CommandMgr::instance().deregisterCommand(SHUT_DOWN_COMMAND);
114  CommandMgr::instance().deregisterCommand("statistic-get");
115  CommandMgr::instance().deregisterCommand("statistic-get-all");
116  CommandMgr::instance().deregisterCommand("statistic-reset");
117  CommandMgr::instance().deregisterCommand("statistic-reset-all");
118  CommandMgr::instance().deregisterCommand(STATUS_GET_COMMAND);
119  CommandMgr::instance().deregisterCommand(VERSION_GET_COMMAND);
120 
121  } catch (...) {
122  // What to do? Simply ignore...
123  }
124 }
125 
127 D2Controller::parseFile(const std::string& file_name) {
129 
130  // Read contents of the file and parse it as JSON
131  D2ParserContext parser;
132  elements = parser.parseFile(file_name, D2ParserContext::PARSER_DHCPDDNS);
133  if (!elements) {
134  isc_throw(isc::BadValue, "no configuration found in file");
135  }
136 
137  return (elements);
138 }
139 
140 D2Controller::~D2Controller() {
141 }
142 
143 // Refer to config_report so it will be embedded in the binary.
145 
146 std::string
147 D2Controller::getVersionAddendum() {
148  std::stringstream stream;
149  // Currently the only dependency D2 adds to base is cryptolink
150  stream << isc::cryptolink::CryptoLink::getVersion() << std::endl;
151  return (stream.str());
152 
153 }
154 
155 } // end namespace isc::d2
156 } // end namespace isc
boost::shared_ptr< DControllerBase > DControllerBasePtr
Definition: d_controller.h:78
Evaluation context, an interface to the expression evaluation.
void registerCommand(const std::string &cmd, CommandHandler handler)
Registers specified command handler for a given command.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
void closeCommandSocket()
Shuts down any open control sockets.
Definition: command_mgr.cc:627
void deregisterCommand(const std::string &cmd)
Deregisters specified command handler.
DHCP-DDNS Application Process.
Definition: d2_process.h:24
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
Defines the logger used by the top-level component of kea-lfc.
const char *const config_report[]
isc::data::ElementPtr parseFile(const std::string &filename, ParserType parser_type)
Run the parser on the file specified.
Application Process Interface.
Definition: d_process.h:81
Application Controller.
Definition: d_controller.h:104
const char *const * d2_config_report
static CommandMgr & instance()
CommandMgr is a singleton class.
Definition: command_mgr.cc:650
Process Controller for D2 Process This class is the DHCP-DDNS specific derivation of DControllerBase...
Definition: d2_controller.h:29