Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 6d6f207 | 2022-09-12 23:08:34 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_MGMT_MANAGER_BASE_HPP |
| 27 | #define NFD_DAEMON_MGMT_MANAGER_BASE_HPP |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 29 | #include "command-authenticator.hpp" |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 30 | |
| 31 | #include <ndn-cxx/mgmt/dispatcher.hpp> |
Junxiao Shi | 25c6ce4 | 2016-09-09 13:49:59 +0000 | [diff] [blame] | 32 | #include <ndn-cxx/mgmt/nfd/control-command.hpp> |
Junxiao Shi | 25c6ce4 | 2016-09-09 13:49:59 +0000 | [diff] [blame] | 33 | #include <ndn-cxx/mgmt/nfd/control-parameters.hpp> |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 34 | #include <ndn-cxx/mgmt/nfd/control-response.hpp> |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 35 | |
| 36 | namespace nfd { |
| 37 | |
| 38 | using ndn::mgmt::Dispatcher; |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 39 | using ndn::nfd::ControlCommand; |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 40 | using ndn::nfd::ControlParameters; |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 41 | using ndn::nfd::ControlResponse; |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 44 | * @brief A collection of common functions shared by all NFD managers, |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 45 | * such as communicating with the dispatcher and command validator. |
| 46 | */ |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 47 | class ManagerBase : noncopyable |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 48 | { |
| 49 | public: |
| 50 | class Error : public std::runtime_error |
| 51 | { |
| 52 | public: |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 53 | using std::runtime_error::runtime_error; |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
Davide Pesavento | d396b61 | 2017-02-20 22:11:50 -0500 | [diff] [blame] | 56 | virtual |
| 57 | ~ManagerBase(); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 58 | |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 59 | const std::string& |
| 60 | getModule() const |
| 61 | { |
| 62 | return m_module; |
| 63 | } |
| 64 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 65 | protected: |
| 66 | /** |
| 67 | * @warning if you use this constructor, you MUST override makeAuthorization() |
| 68 | */ |
| 69 | ManagerBase(const std::string& module, Dispatcher& dispatcher); |
| 70 | |
| 71 | ManagerBase(const std::string& module, Dispatcher& dispatcher, |
| 72 | CommandAuthenticator& authenticator); |
| 73 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 74 | NFD_PUBLIC_WITH_TESTS_ELSE_PROTECTED: // registrations to the dispatcher |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 75 | // difference from mgmt::ControlCommand: accepts nfd::ControlParameters |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 76 | using ControlCommandHandler = std::function<void(const ControlCommand& command, |
| 77 | const Name& prefix, const Interest& interest, |
| 78 | const ControlParameters& parameters, |
| 79 | const ndn::mgmt::CommandContinuation done)>; |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 80 | |
| 81 | template<typename Command> |
| 82 | void |
| 83 | registerCommandHandler(const std::string& verb, |
| 84 | const ControlCommandHandler& handler); |
| 85 | |
| 86 | void |
| 87 | registerStatusDatasetHandler(const std::string& verb, |
| 88 | const ndn::mgmt::StatusDatasetHandler& handler); |
| 89 | |
| 90 | ndn::mgmt::PostNotification |
| 91 | registerNotificationStream(const std::string& verb); |
| 92 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 93 | NFD_PUBLIC_WITH_TESTS_ELSE_PROTECTED: // helpers |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 94 | /** |
Davide Pesavento | 6d6f207 | 2022-09-12 23:08:34 -0400 | [diff] [blame] | 95 | * @brief Extracts the name from the %KeyLocator of a ControlCommand request. |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 96 | * |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 97 | * This is called after the signature has been validated. |
Davide Pesavento | 6d6f207 | 2022-09-12 23:08:34 -0400 | [diff] [blame] | 98 | * Returns an empty string if %SignatureInfo or %KeyLocator are missing or malformed. |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 99 | */ |
Davide Pesavento | 6d6f207 | 2022-09-12 23:08:34 -0400 | [diff] [blame] | 100 | static std::string |
| 101 | extractSigner(const Interest& interest); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 102 | |
Davide Pesavento | 264af77 | 2021-02-09 21:48:24 -0500 | [diff] [blame] | 103 | NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Junxiao Shi | 2173840 | 2016-08-19 19:48:00 +0000 | [diff] [blame] | 104 | /** |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 105 | * @brief Returns an authorization function for a specific management module and verb. |
Junxiao Shi | 2173840 | 2016-08-19 19:48:00 +0000 | [diff] [blame] | 106 | */ |
| 107 | virtual ndn::mgmt::Authorization |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 108 | makeAuthorization(const std::string& verb); |
Junxiao Shi | 2173840 | 2016-08-19 19:48:00 +0000 | [diff] [blame] | 109 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 110 | /** |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 111 | * @brief Validates the @p parameters for a given @p command. |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 112 | * |
| 113 | * @param parameters the original ControlParameters |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 114 | * @return whether the original ControlParameters can be validated |
| 115 | */ |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame^] | 116 | [[nodiscard]] static bool |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 117 | validateParameters(const ControlCommand& command, |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 118 | const ndn::mgmt::ControlParameters& parameters); |
| 119 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 120 | /** |
| 121 | * @brief Handles a control command. |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 122 | */ |
| 123 | static void |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 124 | handleCommand(shared_ptr<ControlCommand> command, |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 125 | const ControlCommandHandler& handler, |
| 126 | const Name& prefix, const Interest& interest, |
| 127 | const ndn::mgmt::ControlParameters& params, |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 128 | const ndn::mgmt::CommandContinuation& done); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 129 | |
| 130 | /** |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 131 | * @brief Generates the relative prefix for a handler by appending the verb name to the module name. |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 132 | * |
| 133 | * @param verb the verb name |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 134 | * @return the generated relative prefix |
| 135 | */ |
| 136 | PartialName |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 137 | makeRelPrefix(const std::string& verb) |
| 138 | { |
| 139 | return PartialName(m_module).append(verb); |
| 140 | } |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 141 | |
| 142 | private: |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 143 | std::string m_module; |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 144 | Dispatcher& m_dispatcher; |
| 145 | CommandAuthenticator* m_authenticator = nullptr; |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 148 | template<typename Command> |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 149 | void |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 150 | ManagerBase::registerCommandHandler(const std::string& verb, |
| 151 | const ControlCommandHandler& handler) |
| 152 | { |
| 153 | auto command = make_shared<Command>(); |
| 154 | |
| 155 | m_dispatcher.addControlCommand<ControlParameters>( |
| 156 | makeRelPrefix(verb), |
Junxiao Shi | 2173840 | 2016-08-19 19:48:00 +0000 | [diff] [blame] | 157 | makeAuthorization(verb), |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 158 | [=] (const auto& params) { return validateParameters(*command, params); }, |
| 159 | [=] (auto&&... args) { handleCommand(command, handler, std::forward<decltype(args)>(args)...); }); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | } // namespace nfd |
| 163 | |
Davide Pesavento | 78ddcab | 2019-02-28 22:00:03 -0500 | [diff] [blame] | 164 | #endif // NFD_DAEMON_MGMT_MANAGER_BASE_HPP |