ca_command_mgr.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 Exception thrown when command forwarding has been skipped.
  23. class CommandForwardingSkip : public Exception {
  24. public:
  25. CommandForwardingSkip(const char* file, size_t line, const char* what) :
  26. isc::Exception(file, line, what) { };
  27. };
  28. /// @brief Command Manager for Control Agent.
  29. ///
  30. /// This is an implementation of the Command Manager within Control Agent.
  31. /// In addition to the standard capabilities of the @ref HookedCommandMgr
  32. /// it is also intended to forward commands to the respective Kea servers
  33. /// when the command is not supported directly by the Control Agent.
  34. ///
  35. /// @todo This Command Manager doesn't yet support forwarding commands.
  36. ///
  37. /// The @ref CtrlAgentCommandMgr is implemented as a singleton. The commands
  38. /// are registered using @c CtrlAgentCommandMgr::instance().registerCommand().
  39. /// The @ref CtrlAgentResponseCreator uses the sole instance of the Command
  40. /// Manager to handle incoming commands.
  41. class CtrlAgentCommandMgr : public config::HookedCommandMgr,
  42. public boost::noncopyable {
  43. public:
  44. /// @brief Returns sole instance of the Command Manager.
  45. static CtrlAgentCommandMgr& instance();
  46. /// @brief Handles the command having a given name and arguments.
  47. ///
  48. /// This method extends the base implementation with the ability to forward
  49. /// commands to Kea servers if the Control Agent failed to handle it itself.
  50. ///
  51. /// @todo Currently this method only wraps an answer within a list Element.
  52. /// This will be later used to include multiple answers within this list.
  53. /// For now it is just a single answer from the Control Agent.
  54. ///
  55. /// @param cmd_name Command name.
  56. /// @param params Command arguments.
  57. /// @param original_cmd Original command being processed.
  58. ///
  59. /// @return Pointer to the const data element representing response
  60. /// to a command.
  61. virtual isc::data::ConstElementPtr
  62. handleCommand(const std::string& cmd_name,
  63. const isc::data::ConstElementPtr& params,
  64. const isc::data::ConstElementPtr& orginal_cmd);
  65. private:
  66. /// @brief Tries to forward received control command to Kea servers.
  67. ///
  68. /// When the Control Agent was unable to process the control command
  69. /// because it doesn't recognize it, the command should be forwarded to
  70. /// the specific Kea services listed within a 'service' parameter.
  71. ///
  72. /// @todo Currently only one service per control command is supported.
  73. /// Forwarding to multiple services should be allowed in the future.
  74. ///
  75. /// This method makes an attempt to forward the control command. If
  76. /// the 'service' parameter is not specified or it is empty, the
  77. /// command is not forwarded and the @ref CommandForwardingSkip exception
  78. /// is thrown. The caller catching this exception should not treat
  79. /// this situation as an error but this is normal situation when the
  80. /// message is not intended to be forwarded.
  81. ///
  82. /// All other exceptions should be treated as an error.
  83. ///
  84. /// @param cmd_name Command name.
  85. /// @param command Pointer to the object representing the forwarded command.
  86. ///
  87. /// @return Response to forwarded command.
  88. /// @throw CommandForwardingError when an error occurred during forwarding.
  89. /// @throw CommandForwardingSkip when 'service' parameter hasn't been
  90. /// specified which means that the command should not be forwarded.
  91. isc::data::ConstElementPtr
  92. tryForwardCommand(const std::string& cmd_name,
  93. const isc::data::ConstElementPtr& command);
  94. /// @brief Private constructor.
  95. ///
  96. /// The instance should be created using @ref CtrlAgentCommandMgr::instance,
  97. /// thus the constructor is private.
  98. CtrlAgentCommandMgr();
  99. /// @brief Buffer into which responses to forwarded commands are stored.
  100. std::array<char, 8192> receive_buf_;
  101. };
  102. } // end of namespace isc::agent
  103. } // end of namespace isc
  104. #endif