Kea 2.7.5
|
Commands Manager, responsible for processing external commands. More...
#include <base_command_mgr.h>
Classes | |
struct | HandlersPair |
Public Types | |
typedef std::function< isc::data::ConstElementPtr(const std::string &name, const isc::data::ConstElementPtr ¶ms) | CommandHandler) |
Defines command handler type. | |
typedef std::function< isc::data::ConstElementPtr(const std::string &name, const isc::data::ConstElementPtr ¶ms, const isc::data::ConstElementPtr &original) | ExtendedCommandHandler) |
Defines extended command handler type. | |
Public Member Functions | |
BaseCommandMgr () | |
Constructor. | |
virtual | ~BaseCommandMgr () |
Destructor. | |
void | deregisterAll () |
Auxiliary method that removes all installed commands. | |
void | deregisterCommand (const std::string &cmd) |
Deregisters specified command handler. | |
virtual isc::data::ConstElementPtr | processCommand (const isc::data::ConstElementPtr &cmd) |
Triggers command processing. | |
void | registerCommand (const std::string &cmd, CommandHandler handler) |
Registers specified command handler for a given command. | |
void | registerExtendedCommand (const std::string &cmd, ExtendedCommandHandler handler) |
Registers specified command handler for a given command. | |
Static Public Member Functions | |
static std::string | getHash (const isc::data::ConstElementPtr &config) |
returns a hash of a given Element structure | |
Protected Types | |
typedef std::map< std::string, HandlersPair > | HandlerContainer |
Type of the container for command handlers. | |
Protected Member Functions | |
virtual isc::data::ConstElementPtr | handleCommand (const std::string &cmd_name, const isc::data::ConstElementPtr ¶ms, const isc::data::ConstElementPtr &original_cmd) |
Handles the command having a given name and arguments. | |
Protected Attributes | |
HandlerContainer | handlers_ |
Container for command handlers. | |
Commands Manager, responsible for processing external commands.
Commands Manager is a generic interface for handling external commands. Commands are received over control sockets. Derivations of this class provide implementations of the control socket layers, e.g. unix domain sockets, TCP sockets etc. This base class merely provides methods to manage command handling functions, i.e. register commands, deregister commands. It also includes a BaseCommandMgr::processCommand method which uses the command as an input and invokes appropriate handlers.
The commands and responses are formatted using JSON. See https://gitlab.isc.org/isc-projects/kea/wikis/designs/Stats-design for details.
Below is an example of the command using JSON format:
And the response is:
BaseCommandsMgr does not implement the commands (except one, "list-commands") itself, but rather provides an interface (see registerCommand, deregisterCommand, processCommand) for other components to use it.
Definition at line 72 of file base_command_mgr.h.
typedef std::function<isc::data::ConstElementPtr (const std::string& name, const isc::data::ConstElementPtr& params) isc::config::BaseCommandMgr::CommandHandler) |
Defines command handler type.
Command handlers are expected to use this format.
name | name of the commands |
params | parameters specific to the command |
Definition at line 83 of file base_command_mgr.h.
typedef std::function<isc::data::ConstElementPtr (const std::string& name, const isc::data::ConstElementPtr& params, const isc::data::ConstElementPtr& original) isc::config::BaseCommandMgr::ExtendedCommandHandler) |
Defines extended command handler type.
This command handler includes third parameter which holds the entire command control message. The handler can retrieve additional information from this parameter, e.g. 'service'.
name | name of the commands |
params | parameters specific to the command |
original | original control command. |
Definition at line 97 of file base_command_mgr.h.
|
protected |
Type of the container for command handlers.
Definition at line 193 of file base_command_mgr.h.
isc::config::BaseCommandMgr::BaseCommandMgr | ( | ) |
Constructor.
Registers hookpoint "command-processed" Registers "list-commands" command.
Definition at line 47 of file base_command_mgr.cc.
References registerCommand().
|
inlinevirtual |
Destructor.
Definition at line 106 of file base_command_mgr.h.
void isc::config::BaseCommandMgr::deregisterAll | ( | ) |
Auxiliary method that removes all installed commands.
The only unwipeable method is list-commands, which is internally handled at all times.
Definition at line 109 of file base_command_mgr.cc.
References handlers_, and registerCommand().
void isc::config::BaseCommandMgr::deregisterCommand | ( | const std::string & | cmd | ) |
Deregisters specified command handler.
cmd | Name of the command that's no longer handled. |
Definition at line 92 of file base_command_mgr.cc.
References isc::config::COMMAND_DEREGISTERED, isc::config::command_logger, isc::config::DBG_COMMAND, handlers_, isc_throw, and LOG_DEBUG.
|
static |
returns a hash of a given Element structure
The hash is currently implemented as SHA256 on the string
config | typically full config, but hash can be calculated on any structure |
Definition at line 193 of file base_command_mgr.cc.
References isc::cryptolink::digest(), isc::util::encode::encodeHex(), isc_throw, and isc::cryptolink::SHA256.
Referenced by isc::process::DControllerBase::configGetHandler(), isc::process::DControllerBase::configHashGetHandler(), isc::dhcp::configureDhcp4Server(), isc::dhcp::configureDhcp6Server(), and isc::d2::D2CfgMgr::parse().
|
protectedvirtual |
Handles the command having a given name and arguments.
This method can be overridden in the derived classes to provide custom logic for processing commands. For example, the HookedCommandMgr extends this method to delegate commands processing to a hook library.
cmd_name | Command name. |
params | Command arguments. |
original_cmd | Pointer to the entire command received. It may be sometimes useful to retrieve additional parameters from this command. |
Reimplemented in isc::agent::CtrlAgentCommandMgr, and isc::config::HookedCommandMgr.
Definition at line 164 of file base_command_mgr.cc.
References isc::config::CONTROL_RESULT_COMMAND_UNSUPPORTED, isc::config::createAnswer(), and handlers_.
Referenced by isc::config::HookedCommandMgr::handleCommand(), and processCommand().
|
virtual |
Triggers command processing.
This method processes specified command. The command is specified using a single Element. See BaseCommandMgr for description of its syntax. After the command has been handled, callouts for the hook point, "command-processed" will be invoked.
This method is virtual so it can be overridden in derived classes to pre-process command and post-process response if necessary.
This method is an entry point for dealing with a command. Internally it calls BaseCommandMgr::handleCommand
.
cmd | Pointer to the data element representing command in JSON format. |
Reimplemented in isc::agent::CtrlAgentCommandMgr.
Definition at line 119 of file base_command_mgr.cc.
References isc::hooks::HooksManager::callCallouts(), isc::hooks::HooksManager::calloutsPresent(), isc::config::command_logger, isc::config::COMMAND_PROCESS_ERROR2, isc::config::COMMAND_RECEIVED, isc::config::CONTROL_RESULT_ERROR, isc::config::createAnswer(), isc::hooks::HooksManager::createCalloutHandle(), handleCommand(), Hooks, LOG_INFO, LOG_WARN, and isc::config::parseCommand().
void isc::config::BaseCommandMgr::registerCommand | ( | const std::string & | cmd, |
CommandHandler | handler ) |
Registers specified command handler for a given command.
cmd | Name of the command to be handled. |
handler | Pointer to the method that will handle the command. |
Definition at line 53 of file base_command_mgr.cc.
References isc::config::command_logger, isc::config::COMMAND_REGISTERED, isc::config::DBG_COMMAND, isc::config::BaseCommandMgr::HandlersPair::handler, handlers_, isc_throw, and LOG_DEBUG.
Referenced by BaseCommandMgr(), and deregisterAll().
void isc::config::BaseCommandMgr::registerExtendedCommand | ( | const std::string & | cmd, |
ExtendedCommandHandler | handler ) |
Registers specified command handler for a given command.
This variant of the method uses extended command handler which, besides command name and arguments, also has a third parameter 'original_cmd' in its signature. Such handlers can retrieve additional parameters from the command, e.g. 'service' indicating where the command should be routed.
cmd | Name of the command to be handled. |
handler | Pointer to the method that will handle the command. |
Definition at line 72 of file base_command_mgr.cc.
References isc::config::COMMAND_EXTENDED_REGISTERED, isc::config::command_logger, isc::config::DBG_COMMAND, isc::config::BaseCommandMgr::HandlersPair::extended_handler, handlers_, isc_throw, and LOG_DEBUG.
|
protected |
Container for command handlers.
Definition at line 196 of file base_command_mgr.h.
Referenced by deregisterAll(), deregisterCommand(), handleCommand(), registerCommand(), and registerExtendedCommand().