Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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 | |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 26 | #ifndef NFD_CORE_MANAGER_BASE_HPP |
| 27 | #define NFD_CORE_MANAGER_BASE_HPP |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 28 | |
| 29 | #include "common.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> |
| 33 | #include <ndn-cxx/mgmt/nfd/control-response.hpp> |
| 34 | #include <ndn-cxx/mgmt/nfd/control-parameters.hpp> |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 35 | |
| 36 | namespace nfd { |
| 37 | |
| 38 | using ndn::mgmt::Dispatcher; |
| 39 | |
| 40 | using ndn::nfd::ControlCommand; |
| 41 | using ndn::nfd::ControlResponse; |
| 42 | using ndn::nfd::ControlParameters; |
| 43 | |
| 44 | /** |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 45 | * @brief a collection of common functions shared by all NFD managers and RIB manager, |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 46 | * such as communicating with the dispatcher and command validator. |
| 47 | */ |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 48 | class ManagerBase : noncopyable |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 49 | { |
| 50 | public: |
| 51 | class Error : public std::runtime_error |
| 52 | { |
| 53 | public: |
| 54 | explicit |
| 55 | Error(const std::string& what) |
| 56 | : std::runtime_error(what) |
| 57 | { |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | public: |
| 62 | ManagerBase(Dispatcher& dispatcher, |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 63 | const std::string& module); |
| 64 | |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 65 | const std::string& |
| 66 | getModule() const |
| 67 | { |
| 68 | return m_module; |
| 69 | } |
| 70 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 71 | PUBLIC_WITH_TESTS_ELSE_PROTECTED: // registrations to the dispatcher |
| 72 | |
| 73 | // difference from mgmt::ControlCommand: accepts nfd::ControlParameters |
| 74 | typedef function<void(const ControlCommand& command, |
| 75 | const Name& prefix, const Interest& interest, |
| 76 | const ControlParameters& parameters, |
| 77 | const ndn::mgmt::CommandContinuation done)> ControlCommandHandler; |
| 78 | |
| 79 | template<typename Command> |
| 80 | void |
| 81 | registerCommandHandler(const std::string& verb, |
| 82 | const ControlCommandHandler& handler); |
| 83 | |
| 84 | void |
| 85 | registerStatusDatasetHandler(const std::string& verb, |
| 86 | const ndn::mgmt::StatusDatasetHandler& handler); |
| 87 | |
| 88 | ndn::mgmt::PostNotification |
| 89 | registerNotificationStream(const std::string& verb); |
| 90 | |
Junxiao Shi | 2173840 | 2016-08-19 19:48:00 +0000 | [diff] [blame] | 91 | PUBLIC_WITH_TESTS_ELSE_PROTECTED: |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 92 | /** |
| 93 | * @brief extract a requester from a ControlCommand request |
| 94 | * |
| 95 | * This is called after the signature is validated. |
| 96 | * |
| 97 | * @param interest a request for ControlCommand |
| 98 | * @param accept callback of successful validation, take the requester string as a argument |
| 99 | */ |
| 100 | void |
| 101 | extractRequester(const Interest& interest, |
| 102 | ndn::mgmt::AcceptContinuation accept); |
| 103 | |
Junxiao Shi | 2173840 | 2016-08-19 19:48:00 +0000 | [diff] [blame] | 104 | PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 105 | /** |
| 106 | * @return an authorization function for specified management module and verb |
| 107 | */ |
| 108 | virtual ndn::mgmt::Authorization |
| 109 | makeAuthorization(const std::string& verb) = 0; |
| 110 | |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 111 | /** |
| 112 | * @brief validate the @p parameters for a given @p command |
| 113 | * |
| 114 | * @param parameters the original ControlParameters |
| 115 | * |
| 116 | * @return whether the original ControlParameters can be validated |
| 117 | */ |
| 118 | static bool |
| 119 | validateParameters(const nfd::ControlCommand& command, |
| 120 | const ndn::mgmt::ControlParameters& parameters); |
| 121 | |
| 122 | /** @brief Handle control command |
| 123 | */ |
| 124 | static void |
| 125 | handleCommand(shared_ptr<nfd::ControlCommand> command, |
| 126 | const ControlCommandHandler& handler, |
| 127 | const Name& prefix, const Interest& interest, |
| 128 | const ndn::mgmt::ControlParameters& params, |
| 129 | ndn::mgmt::CommandContinuation done); |
| 130 | |
| 131 | /** |
| 132 | * @brief generate the relative prefix for a handler, |
| 133 | * by appending the verb name to the module name. |
| 134 | * |
| 135 | * @param verb the verb name |
| 136 | * |
| 137 | * @return the generated relative prefix |
| 138 | */ |
| 139 | PartialName |
| 140 | makeRelPrefix(const std::string& verb); |
| 141 | |
| 142 | private: |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 143 | Dispatcher& m_dispatcher; |
| 144 | std::string m_module; |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | inline PartialName |
| 148 | ManagerBase::makeRelPrefix(const std::string& verb) |
| 149 | { |
Junxiao Shi | 9ddf1b5 | 2016-08-22 03:58:55 +0000 | [diff] [blame] | 150 | return PartialName(m_module).append(verb); |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | template<typename Command> |
| 154 | inline void |
| 155 | ManagerBase::registerCommandHandler(const std::string& verb, |
| 156 | const ControlCommandHandler& handler) |
| 157 | { |
| 158 | auto command = make_shared<Command>(); |
| 159 | |
| 160 | m_dispatcher.addControlCommand<ControlParameters>( |
| 161 | makeRelPrefix(verb), |
Junxiao Shi | 2173840 | 2016-08-19 19:48:00 +0000 | [diff] [blame] | 162 | makeAuthorization(verb), |
Yanbiao Li | 698f4fe | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 163 | bind(&ManagerBase::validateParameters, cref(*command), _1), |
| 164 | bind(&ManagerBase::handleCommand, command, handler, _1, _2, _3, _4)); |
| 165 | } |
| 166 | |
| 167 | } // namespace nfd |
| 168 | |
Yanbiao Li | df846e5 | 2016-01-30 21:53:47 -0800 | [diff] [blame] | 169 | #endif // NFD_CORE_MANAGER_BASE_HPP |