ca_command_mgr.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright (C) 2017 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. #ifndef CTRL_AGENT_COMMAND_MGR_H
  7. #define CTRL_AGENT_COMMAND_MGR_H
  8. #include <config/hooked_command_mgr.h>
  9. #include <exceptions/exceptions.h>
  10. #include <boost/noncopyable.hpp>
  11. #include <boost/shared_ptr.hpp>
  12. #include <array>
  13. namespace isc {
  14. namespace agent {
  15. /// @brief Exception thrown when an error occurred during control command
  16. /// forwarding.
  17. class CommandForwardingError : public Exception {
  18. public:
  19. CommandForwardingError(const char* file, size_t line, const char* what) :
  20. isc::Exception(file, line, what) { };
  21. };
  22. /// @brief Command Manager for Control Agent.
  23. ///
  24. /// This is an implementation of the Command Manager within Control Agent.
  25. /// In addition to the standard capabilities of the @ref HookedCommandMgr
  26. /// it is also intended to forward commands to the respective Kea servers
  27. /// when the command is not supported directly by the Control Agent.
  28. ///
  29. /// @todo This Command Manager doesn't yet support forwarding commands.
  30. ///
  31. /// The @ref CtrlAgentCommandMgr is implemented as a singleton. The commands
  32. /// are registered using @c CtrlAgentCommandMgr::instance().registerCommand().
  33. /// The @ref CtrlAgentResponseCreator uses the sole instance of the Command
  34. /// Manager to handle incoming commands.
  35. class CtrlAgentCommandMgr : public config::HookedCommandMgr,
  36. public boost::noncopyable {
  37. public:
  38. /// @brief Returns sole instance of the Command Manager.
  39. static CtrlAgentCommandMgr& instance();
  40. /// @brief Handles the command having a given name and arguments.
  41. ///
  42. /// This method extends the base implementation with the ability to forward
  43. /// commands to Kea servers.
  44. ///
  45. /// If the received command doesn't include 'service' parameter or this
  46. /// parameter is blank, the command is first handled by the attached hooks
  47. /// libraries, and if still unhandled, the Control Agent itself.
  48. ///
  49. /// If the non-blank 'service' parameter has been specified the hooks
  50. /// are executed. If the hooks process the command the result is returned
  51. /// to the controlling client. Otherwise, the command is forwarded to each
  52. /// Kea server listed in the 'service' parameter.
  53. ///
  54. /// @param cmd_name Command name.
  55. /// @param params Command arguments.
  56. /// @param original_cmd Original command being processed.
  57. ///
  58. /// @return Pointer to the const data element representing a list of
  59. /// responses to the command. If the command has been handled by the CA,
  60. /// this list includes one response.
  61. virtual isc::data::ConstElementPtr
  62. handleCommand(const std::string& cmd_name,
  63. const isc::data::ConstElementPtr& params,
  64. const isc::data::ConstElementPtr& original_cmd);
  65. private:
  66. /// @brief Implements the logic for @ref CtrlAgentCommandMgr::handleCommand.
  67. ///
  68. /// All parameters are passed by value because they may be modified within
  69. /// the method.
  70. ///
  71. /// @param cmd_name Command name.
  72. /// @param params Command arguments.
  73. /// @param original_cmd Original command being processed.
  74. ///
  75. /// @return Pointer to the const data element representing a list of responses
  76. /// to the command or a single response (not wrapped in a list). The
  77. /// @ref CtrlAgentCommandMgr::handleCommand will wrap non-list value returned
  78. /// in a single element list.
  79. isc::data::ConstElementPtr
  80. handleCommandInternal(std::string cmd_name,
  81. isc::data::ConstElementPtr params,
  82. isc::data::ConstElementPtr original_cmd);
  83. /// @brief Tries to forward received control command to a specified server.
  84. ///
  85. /// @param service Contains name of the service where the command should be
  86. /// forwarded.
  87. /// @param cmd_name Command name.
  88. /// @param command Pointer to the object representing the forwarded command.
  89. ///
  90. /// @return Response to forwarded command.
  91. /// @throw CommandForwardingError when an error occurred during forwarding.
  92. isc::data::ConstElementPtr
  93. forwardCommand(const std::string& service, const std::string& cmd_name,
  94. const isc::data::ConstElementPtr& command);
  95. /// @brief Private constructor.
  96. ///
  97. /// The instance should be created using @ref CtrlAgentCommandMgr::instance,
  98. /// thus the constructor is private.
  99. CtrlAgentCommandMgr();
  100. /// @brief Buffer into which responses to forwarded commands are stored.
  101. std::array<char, 8192> receive_buf_;
  102. };
  103. } // end of namespace isc::agent
  104. } // end of namespace isc
  105. #endif