Browse Source

[5332] Created config::CmdsImpl base class

    Extracted command helper functions into new base class, so
    other hook libs may use it.
Thomas Markwalder 7 years ago
parent
commit
969956ddc1
3 changed files with 83 additions and 1 deletions
  1. 4 1
      src/hooks/dhcp/lease_cmds/lease_cmds.cc
  2. 1 0
      src/lib/config/Makefile.am
  3. 78 0
      src/lib/config/cmds_impl.h

+ 4 - 1
src/hooks/dhcp/lease_cmds/lease_cmds.cc

@@ -6,6 +6,7 @@
 
 #include <config.h>
 #include <config/command_mgr.h>
+#include <config/cmds_impl.h>
 #include <cc/command_interpreter.h>
 #include <cc/data.h>
 #include <asiolink/io_address.h>
@@ -36,7 +37,7 @@ namespace isc {
 namespace lease_cmds {
 
 /// @brief Wrapper class around reservation command handlers.
-class LeaseCmdsImpl {
+class LeaseCmdsImpl : private CmdsImpl {
 public:
     /// @brief Parameters specified for reservation-get and reservation-del
     ///
@@ -201,6 +202,7 @@ public:
     /// @throw BadValue if input arguments don't make sense.
     Parameters getParameters(bool v6, const ConstElementPtr& args);
 
+#if 0
 private:
     /// @brief Extracts the command name and arguments from a Callout handle
     ///
@@ -252,6 +254,7 @@ private:
 
     /// @brief Stores the command arguments extracted by a call to extractCommand
     ConstElementPtr cmd_args_;
+#endif
 };
 
 int

+ 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