Browse Source

[master] lease_cmds hook lib handlers are now implmeneted as hook callouts

    Merges branch 'trac5332'
Thomas Markwalder 7 years ago
parent
commit
c902bda9f0

+ 1 - 0
doc/Doxyfile

@@ -779,6 +779,7 @@ INPUT                  = ../src/bin/agent \
                          ../src/bin/sockcreator \
                          ../src/bin/lfc \
                          ../src/hooks/dhcp/user_chk \
+                         ../src/hooks/dhcp/lease_cmds \
                          ../src/lib/asiodns \
                          ../src/lib/asiolink \
                          ../src/lib/cc \

+ 1 - 1
src/hooks/dhcp/lease_cmds/Makefile.am

@@ -25,9 +25,9 @@ CLEANFILES = *.gcno *.gcda lease_cmds_messages.h lease_cmds_messages.cc s-messag
 noinst_LTLIBRARIES = liblease_cmds.la
 
 liblease_cmds_la_SOURCES  = lease_cmds.cc lease_cmds.h
+liblease_cmds_la_SOURCES += lease_cmds_callouts.cc
 liblease_cmds_la_SOURCES += lease_parser.h lease_parser.cc
 liblease_cmds_la_SOURCES += lease_cmds_log.cc lease_cmds_log.h
-liblease_cmds_la_SOURCES += load_unload.cc
 liblease_cmds_la_SOURCES += version.cc
 
 nodist_liblease_cmds_la_SOURCES = lease_cmds_messages.cc lease_cmds_messages.h

File diff suppressed because it is too large
+ 292 - 476
src/hooks/dhcp/lease_cmds/lease_cmds.cc


+ 2 - 2
src/hooks/dhcp/lease_cmds/lease_cmds.dox

@@ -53,8 +53,8 @@ is the same. First, it starts with parameters sanitization and then some
 interaction with isc::dhcp::LeaseMgr is conducted.
 
 For commands that do something with a specific lease (lease4-get, lease6-get,
-lease4-del, lease6-del), there is a @ref isc::lease_cmds::Parameters class that
-contains parsed elements.
+lease4-del, lease6-del), there is a @ref isc::lease_cmds::LeaseCmdsImpl::Parameters
+class that contains parsed elements.
 
 For details see documentation and code of the following handlers:
 - @ref isc::lease_cmds::LeaseCmdsImpl::leaseAddHandler (lease4-add, lease6-add)

+ 289 - 36
src/hooks/dhcp/lease_cmds/lease_cmds.h

@@ -7,6 +7,9 @@
 #ifndef LEASE_CMDS_H
 #define LEASE_CMDS_H
 
+#include <cc/data.h>
+#include <hooks/hooks.h>
+
 #include <boost/shared_ptr.hpp>
 
 namespace isc {
@@ -15,48 +18,298 @@ namespace lease_cmds {
 /// @brief Forward declaration of implementation class.
 class LeaseCmdsImpl;
 
-/// @brief A wrapper class that provides convenient initialization to the library.
-///
-/// This is a wrapper class that simply registers extra commands when
-/// instantiated and deregisters them when the instance is destroyed.
+/// @brief Implements the logic for processing commands pertaining to
+/// lease manipulation.
 ///
-/// For an actual implementation, see @ref LeaseCmdsImpl class in lease_cmds.cc file.
+/// This class is used by the callouts implementing command handlers for
+/// lease manipulations.
 class LeaseCmds {
 public:
-
-    /// @brief Initializes additional lease commands.
-    ///
-    /// It registers the following commands:
-    /// - lease4-add
-    /// - lease6-add
-    /// - lease4-get
-    /// - lease6-get
-    /// - lease4-del
-    /// - lease6-del
-    /// - lease4-update
-    /// - lease6-update
-    /// - lease4-del-all
-    /// - lease6-del-all
-    ///
-    /// @throw Unexpected If any of the above fails.
+    /// @brief Constructor.
+    ///
+    /// It creates an instance of the @c LeaseCmdsImpl.
     LeaseCmds();
 
-    /// @brief Destructor
-    ///
-    /// Unregisters commands:
-    /// - lease4-add
-    /// - lease6-add
-    /// - lease4-get
-    /// - lease6-get
-    /// - lease4-del
-    /// - lease6-del
-    /// - lease4-update
-    /// - lease6-update
-    /// - lease4-del-all
-    /// - lease6-del-all
-    ~LeaseCmds();
-private:
+    /// @brief lease4-add, lease6-add command handler
+    ///
+    /// This command attempts to add a lease.
+    /// It extracts the command name and arguments from the given Callouthandle,
+    /// attempts to process them, and then set's the handle's "response"
+    /// argument accordingly.
+    ///
+    /// This function covers both v4 and v6 leases.
+    ///
+    /// Example command for v4:
+    /// {
+    ///     "command": "lease4-add",
+    ///     "parameters": {
+    ///         "address": "192.0.2.1",
+    ///         "hwaddr": "00:01:02:03:04:05",
+    ///         "client-id": "this-is-a-client",
+    ///         "valid-lft": 3600,
+    ///         "expire": 12345678,
+    ///         "subnet-id": 1,
+    ///         "fqdn-fwd": true,
+    ///         "fqdn-rev": true,
+    ///         "hostname": "myhost.example.org",
+    ///         "state": 0
+    ///     }
+    /// }
+    /// Example command for v6:
+    /// {
+    ///     "command": "lease6-add",
+    ///     "arguments": {
+    ///         "subnet-id": 66,
+    ///         "ip-address": "2001:db8:abcd::",
+    ///         "type": "IA_PD",
+    ///         "prefix-len": 48,
+    ///         "duid": "01:02:03:04:05:06:07:08",
+    ///         "iaid": 1234,
+    ///         "preferred-lft": 500,
+    ///         "valid-lft": 1000,
+    ///         "expire": 12345678,
+    ///         "fqdn-fwd": true,
+    ///         "fqdn-rev": true,
+    ///         "hostname": "urania.example.org""
+    ///     }
+    /// }
+    ///
+    ///
+    /// @param handle Callout context - which is expected to contain the
+    /// add command JSON text in the "command" argument
+    /// @return result of the operation
+    int
+    leaseAddHandler(hooks::CalloutHandle& handle);
+
+    /// @brief lease4-get, lease6-get command handler
+    ///
+    /// This command attempts to retrieve a lease that match selected criteria.
+    /// It extracts the command name and arguments from the given Callouthandle,
+    /// attempts to process them, and then set's the handle's "response"
+    /// argument accordingly.
+    ///
+    /// The following types of parameters are supported:
+    /// - (subnet-id, address) for both v4 and v6
+    /// - (subnet-id, identifier-type, identifier) for v4
+    /// - (subnet-id, type, iana, identifier-type, identifier) for v6
+    ///
+    /// Example command for query by (subnet-id, address):
+    /// {
+    ///     "command": "lease4-get",
+    ///     "arguments": {
+    ///         "subnet-id": 1,
+    ///         "ip-address": "192.0.2.202"
+    ///     }
+    /// }
+    ///
+    /// Example command for query by (subnet-id, identifier-type, identifier)
+    /// {
+    ///     "command": "lease4-get",
+    ///     "arguments": {
+    ///         "subnet-id": 1,
+    ///         "identifier-type": "hw-address",
+    ///         "identifier": "00:01:02:03:04:05"
+    ///     }
+    /// }
+    ///
+    /// Example command for query by (subnet-id, type, iana, identifier-type,
+    ///                               identifier):
+    /// {
+    ///     "command": "lease6-get",
+    ///     "arguments": {
+    ///     "subnet-id": 66,
+    ///     "iaid": 42,
+    ///     "type": "IA_NA",
+    ///     "identifier-type": "duid",
+    ///     "identifier": "77:77:77:77:77:77:77:77"
+    ///     }
+    /// }
+    ///
+    /// @param handle Callout context - which is expected to contain the
+    /// get command JSON text in the "command" argument
+    /// @return result of the operation (includes lease details, if found)
+    int
+    leaseGetHandler(hooks::CalloutHandle& handle);
+
+    /// @brief lease4-del command handler
+    ///
+    /// This command attempts to delete an IPv4 lease that match selected
+    /// criteria.
+    /// It extracts the command name and arguments from the given Callouthandle,
+    /// attempts to process them, and then set's the handle's "response"
+    /// argument accordingly.
+    ///
+    /// Two types of parameters are supported: (subnet-id, address) or
+    /// (subnet-id, identifier-type, identifier).
+    ///
+    ///
+    /// Example command for deletion by (subnet-id, address):
+    /// {
+    ///     "command": "lease4-del",
+    ///     "arguments": {
+    ///         "subnet-id": 1,
+    ///         "ip-address": "192.0.2.202"
+    ///     }
+    /// }
+    ///
+    /// Example command for deletion by (subnet-id, identifier-type, identifier)
+    /// {
+    ///     "command": "lease4-del",
+    ///     "arguments": {
+    ///         "subnet-id": 1,
+    ///         "identifier-type": "hw-address",
+    ///         "identifier": "00:01:02:03:04:05"
+    ///     }
+    /// }";
+    ///
+    /// @param handle Callout context - which is expected to contain the
+    /// delete command JSON text in the "command" argument
+    /// @return result of the operation
+    int
+    lease4DelHandler(hooks::CalloutHandle& handle);
 
+    /// @brief lease6-del command handler
+    ///
+    /// This command attempts to delete a lease that match selected criteria.
+    /// It extracts the command name and arguments from the given Callouthandle,
+    /// attempts to process them, and then set's the handle's "response"
+    /// argument accordingly.
+    ///
+    /// Two types of parameters are supported: (subnet-id, address) or
+    /// (subnet-id, type, iaid, identifier-type, identifier).
+    ///
+    /// Example command for deletion by (subnet-id, address):
+    /// {
+    ///     "command": "lease6-del",
+    ///     "arguments": {
+    ///         "subnet-id": 1,
+    ///         "ip-address": "192.0.2.202"
+    ///     }
+    /// }
+    ///
+    /// Example command for deletion by (subnet-id, type, iaid, identifier-type,
+    /// identifier):
+    /// {
+    ///     "command": "lease6-del",
+    ///     "arguments": {
+    ///         "subnet-id": 1,
+    ///         "type": "IA_NA",
+    ///         "iaid": 123456,
+    ///         "identifier-type": "hw-address",
+    ///         "identifier": "00:01:02:03:04:05"
+    ///     }
+    /// }";
+    ///
+    /// @param handle Callout context - which is expected to contain the
+    /// delete command JSON text in the "command" argument
+    /// @return result of the operation
+    int
+    lease6DelHandler(hooks::CalloutHandle& handle);
+
+    /// @brief lease4-update handler
+    ///
+    /// This command attempts to update existing IPv4 lease. The parameters
+    /// specified will replace existing lease. The only condition is that
+    /// the IP address must not change. If you want to change the IP address,
+    /// please use lease4-del and lease4-add instead.
+    /// It extracts the command name and arguments from the given Callouthandle,
+    /// attempts to process them, and then set's the handle's "response"
+    /// argument accordingly.
+    ///
+    /// Example command:
+    /// {
+    ///     "command": "lease4-update",
+    ///     "arguments": {
+    ///         "subnet-id": 44,
+    ///         "ip-address": "192.0.2.1",
+    ///         "hw-address": "1a:1b:1c:1d:1e:1f",
+    ///         "hostname": "newhostname.example.org"
+    ///     }
+    /// };
+    ///
+    /// @param handle Callout context - which is expected to contain the
+    /// update command JSON text in the "command" argument
+    /// @return result of the operation
+    int
+    lease4UpdateHandler(hooks::CalloutHandle& handle);
+
+    /// @brief lease6-update handler
+    ///
+    /// This command attempts to update existing IPv6 lease. The parameters
+    /// specified will replace existing lease. The only condition is that
+    /// the IP address must not change. If you want to change the IP address,
+    /// please use lease6-del and lease6-add instead.
+    /// It extracts the command name and arguments from the given Callouthandle,
+    /// attempts to process them, and then set's the handle's "response"
+    /// argument accordingly.
+    ///
+    /// Example command:
+    /// {
+    ///     "command": "lease6-update",
+    ///     "arguments": {
+    ///         "subnet-id": 66,
+    ///         "ip-address": "2001:db8::1",
+    ///         "iaid": 7654321,
+    ///         "duid": "88:88:88:88:88:88:88:88",
+    ///         "hostname": "newhostname.example.org"
+    ///     }
+    /// }";
+    ///
+    /// @param handle Callout context - which is expected to contain the
+    /// update command JSON text in the "command" argument
+    /// @return result of the operation
+    int
+    lease6UpdateHandler(hooks::CalloutHandle& handle);
+
+    /// @brief lease4-wipe handler
+    ///
+    /// This commands attempts to remove all IPv4 leases from a specific
+    /// subnet. Currently the leases are removed from the database,
+    /// without any processing (like calling hooks or doing DDNS
+    /// cleanups).
+    /// It extracts the command name and arguments from the given Callouthandle,
+    /// attempts to process them, and then set's the handle's "response"
+    /// argument accordingly.
+    ///
+    /// Example command:
+    /// {\n"
+    ///     "command": "lease4-wipe",\n"
+    ///     "arguments": {"
+    ///         "subnet-id": 44
+    ///     }\n"
+    /// }";
+    ///
+    /// @param handle Callout context - which is expected to contain the
+    /// wipe command JSON text in the "command" argument
+    /// @return result of the operation
+    int
+    lease4WipeHandler(hooks::CalloutHandle& handle);
+
+    /// @brief lease6-wipe handler
+    ///
+    /// This commands attempts to remove all IPv4 leases from a specific
+    /// subnet. Currently the leases are removed from the database,
+    /// without any processing (like calling hooks or doing DDNS
+    /// cleanups).
+    /// It extracts the command name and arguments from the given Callouthandle,
+    /// attempts to process them, and then set's the handle's "response"
+    /// argument accordingly.
+    ///
+    /// Example command:
+    /// {\n"
+    ///     "command": "lease4-wipe",\n"
+    ///     "arguments": {"
+    ///         "subnet-id": 44
+    ///     }\n"
+    /// }";
+    ///
+    /// @param handle Callout context - which is expected to contain the
+    /// wipe command JSON text in the "command" argument
+    /// @return result of the operation
+    int
+    lease6WipeHandler(hooks::CalloutHandle& handle);
+
+private:
     /// Pointer to the actual implementation
     boost::shared_ptr<LeaseCmdsImpl> impl_;
 };

+ 161 - 0
src/hooks/dhcp/lease_cmds/lease_cmds_callouts.cc

@@ -0,0 +1,161 @@
+// Copyright (C) 2017 Internet Systems Consortium, Inc. ("ISC")
+//
+// This Source Code Form is subject to the terms of the End User License
+// Agreement. See COPYING file in the premium/ directory.
+
+
+// Functions accessed by the hooks framework use C linkage to avoid the name
+// mangling that accompanies use of the C++ compiler as well as to avoid
+// issues related to namespaces.
+
+#include <lease_cmds.h>
+#include <lease_cmds_log.h>
+#include <cc/command_interpreter.h>
+#include <hooks/hooks.h>
+
+using namespace isc::config;
+using namespace isc::data;
+using namespace isc::hooks;
+using namespace isc::lease_cmds;
+
+extern "C" {
+
+/// @brief This is a command callout for 'lease4-add' command.
+///
+/// @param handle Callout handle used to retrieve a command and
+/// provide a response.
+/// @return 0 if this callout has been invoked successfully,
+/// 1 otherwise.
+int lease4_add(CalloutHandle& handle) {
+    LeaseCmds lease_cmds;
+    return(lease_cmds.leaseAddHandler(handle));
+}
+
+/// @brief This is a command callout for 'lease6-add' command.
+///
+/// @param handle Callout handle used to retrieve a command and
+/// provide a response.
+/// @return 0 if this callout has been invoked successfully,
+/// 1 otherwise.
+int lease6_add(CalloutHandle& handle) {
+    LeaseCmds lease_cmds;
+    return(lease_cmds.leaseAddHandler(handle));
+}
+
+/// @brief This is a command callout for 'lease4-get' command.
+///
+/// @param handle Callout handle used to retrieve a command and
+/// provide a response.
+/// @return 0 if this callout has been invoked successfully,
+/// 1 otherwise.
+int lease4_get(CalloutHandle& handle) {
+    LeaseCmds lease_cmds;
+    return(lease_cmds.leaseGetHandler(handle));
+}
+
+/// @brief This is a command callout for 'lease6-get' command.
+///
+/// @param handle Callout handle used to retrieve a command and
+/// provide a response.
+/// @return 0 if this callout has been invoked successfully,
+/// 1 otherwise.
+int lease6_get(CalloutHandle& handle) {
+    LeaseCmds lease_cmds;
+    return(lease_cmds.leaseGetHandler(handle));
+}
+
+/// @brief This is a command callout for 'lease4-del' command.
+///
+/// @param handle Callout handle used to retrieve a command and
+/// provide a response.
+/// @return 0 if this callout has been invoked successfully,
+/// 1 otherwise.
+int lease4_del(CalloutHandle& handle) {
+    LeaseCmds lease_cmds;
+    return(lease_cmds.lease4DelHandler(handle));
+}
+
+/// @brief This is a command callout for 'lease6-del' command.
+///
+/// @param handle Callout handle used to retrieve a command and
+/// provide a response.
+/// @return 0 if this callout has been invoked successfully,
+/// 1 otherwise.
+int lease6_del(CalloutHandle& handle) {
+    LeaseCmds lease_cmds;
+    return(lease_cmds.lease6DelHandler(handle));
+}
+
+/// @brief This is a command callout for 'lease4-update' command.
+///
+/// @param handle Callout handle used to retrieve a command and
+/// provide a response.
+/// @return 0 if this callout has been invoked successfully,
+/// 1 otherwise.
+int lease4_update(CalloutHandle& handle) {
+    LeaseCmds lease_cmds;
+    return(lease_cmds.lease4UpdateHandler(handle));
+}
+
+/// @brief This is a command callout for 'lease6-update' command.
+///
+/// @param handle Callout handle used to retrieve a command and
+/// provide a response.
+/// @return 0 if this callout has been invoked successfully,
+/// 1 otherwise.
+int lease6_update(CalloutHandle& handle) {
+    LeaseCmds lease_cmds;
+    return(lease_cmds.lease6UpdateHandler(handle));
+}
+
+/// @brief This is a command callout for 'lease4-wipe' command.
+///
+/// @param handle Callout handle used to retrieve a command and
+/// provide a response.
+/// @return 0 if this callout has been invoked successfully,
+/// 1 otherwise.
+int lease4_wipe(CalloutHandle& handle) {
+    LeaseCmds lease_cmds;
+    return(lease_cmds.lease4WipeHandler(handle));
+}
+
+/// @brief This is a command callout for 'lease6-wipe' command.
+///
+/// @param handle Callout handle used to retrieve a command and
+/// provide a response.
+/// @return 0 if this callout has been invoked successfully,
+/// 1 otherwise.
+int lease6_wipe(CalloutHandle& handle) {
+    LeaseCmds lease_cmds;
+    return(lease_cmds.lease6WipeHandler(handle));
+}
+
+/// @brief This function is called when the library is loaded.
+///
+/// @param handle library handle
+/// @return 0 when initialization is successful, 1 otherwise
+int load(LibraryHandle& handle) {
+    handle.registerCommandCallout("lease4-add", lease4_add);
+    handle.registerCommandCallout("lease6-add", lease6_add);
+    handle.registerCommandCallout("lease4-get", lease4_get);
+    handle.registerCommandCallout("lease6-get", lease6_get);
+    handle.registerCommandCallout("lease4-del", lease4_del);
+    handle.registerCommandCallout("lease6-del", lease6_del);
+    handle.registerCommandCallout("lease4-update", lease4_update);
+    handle.registerCommandCallout("lease6-update", lease6_update);
+    handle.registerCommandCallout("lease4-wipe", lease4_wipe);
+    handle.registerCommandCallout("lease6-wipe", lease6_wipe);
+
+    LOG_INFO(lease_cmds_logger, LEASE_CMDS_INIT_OK);
+    return (0);
+}
+
+/// @brief This function is called when the library is unloaded.
+///
+/// @return 0 if deregistration was successful, 1 otherwise
+int unload() {
+    LOG_INFO(lease_cmds_logger, LEASE_CMDS_DEINIT_OK);
+    return (0);
+}
+
+} // end extern "C"

+ 28 - 28
src/hooks/dhcp/lease_cmds/lease_cmds_messages.mes

@@ -1,13 +1,20 @@
 # Copyright (C) 2017 Internet Systems Consortium, Inc. ("ISC")
 
-% LEASE_CMDS_INIT_FAILED loading Lease Commands hooks library failed: %1
-This error message indicates an error during loading the Lease Commands
-hooks library. The details of the error are provided as argument of
-the log message.
+% LEASE_CMDS_ADD4 lease4-add command successful (parameters: %1)
+The lease4-add command has been successful. Parameters of the host
+added are logged.
 
-% LEASE_CMDS_INIT_OK loading Lease Commands hooks library successful
-This info message indicates that the Lease Commands hooks library has been
-loaded successfully. Enjoy!
+% LEASE_CMDS_ADD4_FAILED lease4-add command failed (parameters: %1, reason: %2)
+The lease4-add command has failed. Both the reason as well as the
+parameters passed are logged.
+
+% LEASE_CMDS_ADD6 lease6-add command successful (parameters: %1)
+The lease6-add command has been successful. Parameters of the host
+added are logged.
+
+% LEASE_CMDS_ADD6_FAILED Lease6-add command failed (parameters: %1, reason: %2)
+The lease6-add command has failed. Both the reason as well as the
+parameters passed are logged.
 
 % LEASE_CMDS_DEINIT_FAILED unloading Lease Commands hooks library failed: %1
 This error message indicates an error during unloading the Lease Commands
@@ -18,34 +25,27 @@ the log message.
 This info message indicates that the Lease Commands hooks library has been
 removed successfully.
 
-% LEASE_CMDS_ADD4_FAILED lease4-add command failed (parameters: %1, reason: %2)
-The lease4-add command has failed. Both the reason as well as the
-parameters passed are logged.
-
-% LEASE_CMDS_ADD6_FAILED Lease6-add command failed (parameters: %1, reason: %2)
-The lease6-add command has failed. Both the reason as well as the
-parameters passed are logged.
-
-% LEASE_CMDS_ADD4 lease4-add command successful (parameters: %1)
-The lease4-add command has been successful. Parameters of the host
-added are logged.
-
-% LEASE_CMDS_ADD6 lease6-add command successful (parameters: %1)
-The lease6-add command has been successful. Parameters of the host
-added are logged.
+% LEASE_CMDS_DEL4 lease4-del command successful (parameters: %1)
+The attempt to delete an IPv4 lease (lease4-del command) has been successful.
+Parameters of the host removed are logged.
 
 % LEASE_CMDS_DEL4_FAILED lease4-del command failed (parameters: %1, reason: %2)
 The attempt to delete an IPv4 lease (lease4-del command) has failed. Both the
 reason as well as the parameters passed are logged.
 
+% LEASE_CMDS_DEL6 lease4-del command successful (parameters: %1)
+The attempt to delete an IPv4 lease (lease4-del command) has been successful.
+Parameters of the host removed are logged.
+
 % LEASE_CMDS_DEL6_FAILED lease6-del command failed (parameters: %1, reason: %2)
 The attempt to delete an IPv6 lease (lease4-del command) has failed. Both the
 reason as well as the parameters passed are logged.
 
-% LEASE_CMDS_DEL4 lease4-del command successful (parameters: %1)
-The attempt to delete an IPv4 lease (lease4-del command) has been successful.
-Parameters of the host removed are logged.
+% LEASE_CMDS_INIT_FAILED loading Lease Commands hooks library failed: %1
+This error message indicates an error during loading the Lease Commands
+hooks library. The details of the error are provided as argument of
+the log message.
 
-% LEASE_CMDS_DEL6 lease4-del command successful (parameters: %1)
-The attempt to delete an IPv4 lease (lease4-del command) has been successful.
-Parameters of the host removed are logged.
+% LEASE_CMDS_INIT_OK loading Lease Commands hooks library successful
+This info message indicates that the Lease Commands hooks library has been
+loaded successfully. Enjoy!

+ 0 - 63
src/hooks/dhcp/lease_cmds/load_unload.cc

@@ -1,63 +0,0 @@
-// Copyright (C) 2017 Internet Systems Consortium, Inc. ("ISC")
-//
-// This Source Code Form is subject to the terms of the Mozilla Public
-// License, v. 2.0. If a copy of the MPL was not distributed with this
-// file, You can obtain one at http://mozilla.org/MPL/2.0/.
-
-/// @file load_unload.cc Defines the load and unload hooks library functions.
-
-#include <config.h>
-#include <lease_cmds.h>
-#include <lease_cmds_log.h>
-#include <string>
-#include <hooks/hooks.h>
-#include <exceptions/exceptions.h>
-
-using namespace isc::hooks;
-using namespace isc::lease_cmds;
-
-boost::shared_ptr<LeaseCmds> instance;
-
-extern "C" {
-
-/// @brief This function is called when the library is loaded.
-///
-/// This function creates LeaseCmds object that registers
-/// additional commands.
-///
-/// @param handle library handle (ignored)
-/// @return 0 when initialization is successful, 1 otherwise
-int load(LibraryHandle& /*handle*/) {
-
-    try {
-        instance.reset(new LeaseCmds());
-    } catch (const isc::Unexpected& ex) {
-        LOG_ERROR(lease_cmds_logger, LEASE_CMDS_INIT_FAILED)
-            .arg(ex.what());
-        return (1);
-    }
-
-    LOG_INFO(lease_cmds_logger, LEASE_CMDS_INIT_OK);
-    return (0);
-}
-
-/// @brief This function is called when the library is unloaded.
-///
-/// This function creates LeaseCmds object that deregisters
-/// additional commands.
-///
-/// @return 0 if deregistration was successful, 1 otherwise
-int unload() {
-    try {
-        instance.reset();
-    } catch (const isc::Unexpected& ex) {
-        LOG_ERROR(lease_cmds_logger, LEASE_CMDS_DEINIT_FAILED)
-            .arg(ex.what());
-        return (1);
-    }
-
-    LOG_INFO(lease_cmds_logger, LEASE_CMDS_DEINIT_OK);
-    return (0);
-}
-
-}

+ 1 - 0
src/lib/config/Makefile.am

@@ -14,6 +14,7 @@ BUILT_SOURCES = config_messages.h config_messages.cc
 
 lib_LTLIBRARIES = libkea-cfgclient.la
 libkea_cfgclient_la_SOURCES = config_data.h config_data.cc
+libkea_cfgclient_la_SOURCES += cmds_impl.h
 libkea_cfgclient_la_SOURCES += module_spec.h module_spec.cc
 libkea_cfgclient_la_SOURCES += base_command_mgr.cc base_command_mgr.h
 libkea_cfgclient_la_SOURCES += client_connection.cc client_connection.h

+ 78 - 0
src/lib/config/cmds_impl.h

@@ -0,0 +1,78 @@
+// Copyright (C) 2017 Internet Systems Consortium, Inc. ("ISC")
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+#ifndef CMDS_IMPL_H
+#define CMDS_IMPL_H
+
+#include <config.h>
+#include <cc/command_interpreter.h>
+#include <cc/data.h>
+#include <hooks/hooks.h>
+#include <exceptions/exceptions.h>
+
+#include <string>
+
+namespace isc {
+namespace config {
+
+/// @brief Base class that command handler implementers may use for common tasks.
+class CmdsImpl {
+protected:
+    /// @brief Extracts the command name and arguments from a Callout handle
+    ///
+    /// @param handle Callout context handle expected to contain the JSON command
+    /// text
+    ///
+    /// @throw isc::BadValue if the text does not contain a properly formed command
+    void extractCommand(hooks::CalloutHandle& handle) {
+        try {
+            data::ConstElementPtr command;
+            handle.getArgument("command", command);
+            cmd_name_ = parseCommand(cmd_args_, command);
+        } catch (std::exception& ex) {
+            isc_throw(isc::BadValue, "JSON command text is invalid: " << ex.what());
+        }
+    }
+
+    /// @brief Set the callout argument "response" to indicate success
+    ///
+    /// @param handle Callout context handle in which to set the "response" argument
+    /// @param text string text to be used as the response description
+    void setSuccessResponse(hooks::CalloutHandle& handle, const std::string& text) {
+        data::ConstElementPtr response = createAnswer(CONTROL_RESULT_SUCCESS, text);
+        setResponse (handle, response);
+    }
+
+    /// @brief Set the callout argument "response" to indicate an error
+    ///
+    /// @param handle Callout context handle in which to set the "response" argument
+    /// @param text string text to be used as the response description
+    /// @param status numeric value to use as the response result, defaults to
+    /// CONTROL_RESULT_ERROR
+    void setErrorResponse(hooks::CalloutHandle& handle, const std::string& text,
+        int status=CONTROL_RESULT_ERROR) {
+        data::ConstElementPtr response = createAnswer(status, text);
+        setResponse (handle, response);
+    }
+
+    /// @brief Set the callout argument "response" to the given response
+    ///
+    /// @param handle Callout context handle in which to set the "response" argument
+    /// @param response ElementPtr to a the result to use as the reponse
+    void setResponse(hooks::CalloutHandle& handle, data::ConstElementPtr& response) {
+        handle.setArgument ("response", response);
+    }
+
+    /// @brief Stores the command name extracted by a call to extractCommand
+    std::string cmd_name_;
+
+    /// @brief Stores the command arguments extracted by a call to extractCommand
+    data::ConstElementPtr cmd_args_;
+};
+
+}
+}
+
+#endif // CMDS_IMPL_H