blob: d9b351af5678755fddc19289d49aabe34781c073 [file] [log] [blame]
Yanbiao Li698f4fe2015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento87fc0f82018-04-11 23:43:51 -04002/*
Davide Pesaventoae430302023-05-11 01:42:46 -04003 * Copyright (c) 2014-2023, Regents of the University of California,
Yanbiao Li698f4fe2015-08-19 16:30:16 -07004 * 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 Pesavento78ddcab2019-02-28 22:00:03 -050026#ifndef NFD_DAEMON_MGMT_MANAGER_BASE_HPP
27#define NFD_DAEMON_MGMT_MANAGER_BASE_HPP
Yanbiao Li698f4fe2015-08-19 16:30:16 -070028
Davide Pesavento78ddcab2019-02-28 22:00:03 -050029#include "command-authenticator.hpp"
Yanbiao Li698f4fe2015-08-19 16:30:16 -070030
31#include <ndn-cxx/mgmt/dispatcher.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000032#include <ndn-cxx/mgmt/nfd/control-command.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000033#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
Davide Pesavento78ddcab2019-02-28 22:00:03 -050034#include <ndn-cxx/mgmt/nfd/control-response.hpp>
Yanbiao Li698f4fe2015-08-19 16:30:16 -070035
36namespace nfd {
37
38using ndn::mgmt::Dispatcher;
Yanbiao Li698f4fe2015-08-19 16:30:16 -070039using ndn::nfd::ControlCommand;
Yanbiao Li698f4fe2015-08-19 16:30:16 -070040using ndn::nfd::ControlParameters;
Davide Pesavento78ddcab2019-02-28 22:00:03 -050041using ndn::nfd::ControlResponse;
Yanbiao Li698f4fe2015-08-19 16:30:16 -070042
43/**
Davide Pesavento78ddcab2019-02-28 22:00:03 -050044 * @brief A collection of common functions shared by all NFD managers,
Yanbiao Li698f4fe2015-08-19 16:30:16 -070045 * such as communicating with the dispatcher and command validator.
46 */
Yanbiao Lidf846e52016-01-30 21:53:47 -080047class ManagerBase : noncopyable
Yanbiao Li698f4fe2015-08-19 16:30:16 -070048{
49public:
50 class Error : public std::runtime_error
51 {
52 public:
Davide Pesavento78ddcab2019-02-28 22:00:03 -050053 using std::runtime_error::runtime_error;
Yanbiao Li698f4fe2015-08-19 16:30:16 -070054 };
55
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000056 const std::string&
57 getModule() const
58 {
59 return m_module;
60 }
61
Davide Pesavento78ddcab2019-02-28 22:00:03 -050062protected:
63 /**
Davide Pesaventoae430302023-05-11 01:42:46 -040064 * @warning If you use this constructor, you MUST override makeAuthorization().
Davide Pesavento78ddcab2019-02-28 22:00:03 -050065 */
Davide Pesaventoae430302023-05-11 01:42:46 -040066 ManagerBase(std::string_view module, Dispatcher& dispatcher);
Davide Pesavento78ddcab2019-02-28 22:00:03 -050067
Davide Pesaventoae430302023-05-11 01:42:46 -040068 ManagerBase(std::string_view module, Dispatcher& dispatcher,
Davide Pesavento78ddcab2019-02-28 22:00:03 -050069 CommandAuthenticator& authenticator);
70
Davide Pesavento0a05f7a2023-10-16 20:28:06 -040071 // ManagerBase is not supposed to be used polymorphically, so we make the destructor
72 // protected to prevent deletion of derived objects through a pointer to the base class,
73 // which would be UB when the destructor is non-virtual.
74 ~ManagerBase();
75
Davide Pesavento264af772021-02-09 21:48:24 -050076NFD_PUBLIC_WITH_TESTS_ELSE_PROTECTED: // registrations to the dispatcher
Yanbiao Li698f4fe2015-08-19 16:30:16 -070077 // difference from mgmt::ControlCommand: accepts nfd::ControlParameters
Davide Pesavento87fc0f82018-04-11 23:43:51 -040078 using ControlCommandHandler = std::function<void(const ControlCommand& command,
79 const Name& prefix, const Interest& interest,
80 const ControlParameters& parameters,
81 const ndn::mgmt::CommandContinuation done)>;
Yanbiao Li698f4fe2015-08-19 16:30:16 -070082
83 template<typename Command>
84 void
85 registerCommandHandler(const std::string& verb,
86 const ControlCommandHandler& handler);
87
88 void
89 registerStatusDatasetHandler(const std::string& verb,
90 const ndn::mgmt::StatusDatasetHandler& handler);
91
92 ndn::mgmt::PostNotification
93 registerNotificationStream(const std::string& verb);
94
Davide Pesavento264af772021-02-09 21:48:24 -050095NFD_PUBLIC_WITH_TESTS_ELSE_PROTECTED: // helpers
Yanbiao Li698f4fe2015-08-19 16:30:16 -070096 /**
Davide Pesavento6d6f2072022-09-12 23:08:34 -040097 * @brief Extracts the name from the %KeyLocator of a ControlCommand request.
Yanbiao Li698f4fe2015-08-19 16:30:16 -070098 *
Davide Pesavento78ddcab2019-02-28 22:00:03 -050099 * This is called after the signature has been validated.
Davide Pesavento6d6f2072022-09-12 23:08:34 -0400100 * Returns an empty string if %SignatureInfo or %KeyLocator are missing or malformed.
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700101 */
Davide Pesavento6d6f2072022-09-12 23:08:34 -0400102 static std::string
103 extractSigner(const Interest& interest);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700104
Davide Pesavento264af772021-02-09 21:48:24 -0500105NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Junxiao Shi21738402016-08-19 19:48:00 +0000106 /**
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500107 * @brief Returns an authorization function for a specific management module and verb.
Junxiao Shi21738402016-08-19 19:48:00 +0000108 */
109 virtual ndn::mgmt::Authorization
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500110 makeAuthorization(const std::string& verb);
Junxiao Shi21738402016-08-19 19:48:00 +0000111
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700112 /**
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500113 * @brief Validates the @p parameters for a given @p command.
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700114 *
115 * @param parameters the original ControlParameters
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700116 * @return whether the original ControlParameters can be validated
117 */
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400118 [[nodiscard]] static bool
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500119 validateParameters(const ControlCommand& command,
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700120 const ndn::mgmt::ControlParameters& parameters);
121
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500122 /**
123 * @brief Handles a control command.
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700124 */
125 static void
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500126 handleCommand(shared_ptr<ControlCommand> command,
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700127 const ControlCommandHandler& handler,
128 const Name& prefix, const Interest& interest,
129 const ndn::mgmt::ControlParameters& params,
Davide Pesavento412c9822021-07-02 00:21:05 -0400130 const ndn::mgmt::CommandContinuation& done);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700131
132 /**
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500133 * @brief Generates the relative prefix for a handler by appending the verb name to the module name.
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700134 *
135 * @param verb the verb name
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700136 * @return the generated relative prefix
137 */
138 PartialName
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500139 makeRelPrefix(const std::string& verb)
140 {
141 return PartialName(m_module).append(verb);
142 }
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700143
144private:
Junxiao Shi9ddf1b52016-08-22 03:58:55 +0000145 std::string m_module;
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500146 Dispatcher& m_dispatcher;
147 CommandAuthenticator* m_authenticator = nullptr;
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700148};
149
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700150template<typename Command>
Davide Pesavento412c9822021-07-02 00:21:05 -0400151void
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700152ManagerBase::registerCommandHandler(const std::string& verb,
153 const ControlCommandHandler& handler)
154{
155 auto command = make_shared<Command>();
156
157 m_dispatcher.addControlCommand<ControlParameters>(
158 makeRelPrefix(verb),
Junxiao Shi21738402016-08-19 19:48:00 +0000159 makeAuthorization(verb),
Davide Pesavento412c9822021-07-02 00:21:05 -0400160 [=] (const auto& params) { return validateParameters(*command, params); },
161 [=] (auto&&... args) { handleCommand(command, handler, std::forward<decltype(args)>(args)...); });
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700162}
163
164} // namespace nfd
165
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500166#endif // NFD_DAEMON_MGMT_MANAGER_BASE_HPP