blob: ec03463c1e12cab1cbfa6810779b1dc875c7a1ef [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 Pesavento45c1f6a2025-01-01 19:30:30 -05003 * Copyright (c) 2014-2025, 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
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050036#include <functional>
37
Yanbiao Li698f4fe2015-08-19 16:30:16 -070038namespace nfd {
39
40using ndn::mgmt::Dispatcher;
Davide Pesavento45c1f6a2025-01-01 19:30:30 -050041using ndn::mgmt::CommandContinuation;
Yanbiao Li698f4fe2015-08-19 16:30:16 -070042using ndn::nfd::ControlParameters;
Davide Pesavento78ddcab2019-02-28 22:00:03 -050043using ndn::nfd::ControlResponse;
Yanbiao Li698f4fe2015-08-19 16:30:16 -070044
45/**
Davide Pesavento78ddcab2019-02-28 22:00:03 -050046 * @brief A collection of common functions shared by all NFD managers,
Yanbiao Li698f4fe2015-08-19 16:30:16 -070047 * such as communicating with the dispatcher and command validator.
48 */
Yanbiao Lidf846e52016-01-30 21:53:47 -080049class ManagerBase : noncopyable
Yanbiao Li698f4fe2015-08-19 16:30:16 -070050{
51public:
52 class Error : public std::runtime_error
53 {
54 public:
Davide Pesavento78ddcab2019-02-28 22:00:03 -050055 using std::runtime_error::runtime_error;
Yanbiao Li698f4fe2015-08-19 16:30:16 -070056 };
57
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000058 const std::string&
59 getModule() const
60 {
61 return m_module;
62 }
63
Davide Pesavento78ddcab2019-02-28 22:00:03 -050064protected:
65 /**
Davide Pesaventoae430302023-05-11 01:42:46 -040066 * @warning If you use this constructor, you MUST override makeAuthorization().
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
Davide Pesaventoae430302023-05-11 01:42:46 -040070 ManagerBase(std::string_view module, Dispatcher& dispatcher,
Davide Pesavento78ddcab2019-02-28 22:00:03 -050071 CommandAuthenticator& authenticator);
72
Davide Pesavento0a05f7a2023-10-16 20:28:06 -040073 // ManagerBase is not supposed to be used polymorphically, so we make the destructor
74 // protected to prevent deletion of derived objects through a pointer to the base class,
75 // which would be UB when the destructor is non-virtual.
76 ~ManagerBase();
77
Davide Pesavento264af772021-02-09 21:48:24 -050078NFD_PUBLIC_WITH_TESTS_ELSE_PROTECTED: // registrations to the dispatcher
Davide Pesavento1db1bb62025-01-06 01:23:41 -050079 template<typename Command>
Davide Pesavento45c1f6a2025-01-01 19:30:30 -050080 using ControlCommandHandler = std::function<void(const Name& prefix, const Interest& interest,
Davide Pesavento1db1bb62025-01-06 01:23:41 -050081 const typename Command::RequestParameters& parameters,
Davide Pesavento45c1f6a2025-01-01 19:30:30 -050082 const CommandContinuation& done)>;
Yanbiao Li698f4fe2015-08-19 16:30:16 -070083
84 template<typename Command>
85 void
Davide Pesavento1db1bb62025-01-06 01:23:41 -050086 registerCommandHandler(ControlCommandHandler<Command> handler)
87 {
88 auto handle = [h = std::move(handler)] (const auto& prefix, const auto& interest,
89 const auto& params, const auto& done) {
90 const auto& reqParams = static_cast<const typename Command::RequestParameters&>(params);
91 h(prefix, interest, reqParams, done);
92 };
93 m_dispatcher.addControlCommand<Command>(makeAuthorization(Command::verb.toUri()),
94 std::move(handle));
95 }
Yanbiao Li698f4fe2015-08-19 16:30:16 -070096
97 void
98 registerStatusDatasetHandler(const std::string& verb,
99 const ndn::mgmt::StatusDatasetHandler& handler);
100
101 ndn::mgmt::PostNotification
102 registerNotificationStream(const std::string& verb);
103
Davide Pesavento264af772021-02-09 21:48:24 -0500104NFD_PUBLIC_WITH_TESTS_ELSE_PROTECTED: // helpers
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700105 /**
Davide Pesavento6d6f2072022-09-12 23:08:34 -0400106 * @brief Extracts the name from the %KeyLocator of a ControlCommand request.
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700107 *
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500108 * This is called after the signature has been validated.
Davide Pesavento6d6f2072022-09-12 23:08:34 -0400109 * Returns an empty string if %SignatureInfo or %KeyLocator are missing or malformed.
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700110 */
Davide Pesavento6d6f2072022-09-12 23:08:34 -0400111 static std::string
112 extractSigner(const Interest& interest);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700113
Davide Pesavento264af772021-02-09 21:48:24 -0500114NFD_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Junxiao Shi21738402016-08-19 19:48:00 +0000115 /**
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500116 * @brief Returns an authorization function for a specific management module and verb.
Junxiao Shi21738402016-08-19 19:48:00 +0000117 */
118 virtual ndn::mgmt::Authorization
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500119 makeAuthorization(const std::string& verb);
Junxiao Shi21738402016-08-19 19:48:00 +0000120
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700121 /**
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500122 * @brief Generates the relative prefix for a handler by appending the verb name to the module name.
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700123 *
124 * @param verb the verb name
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700125 * @return the generated relative prefix
126 */
127 PartialName
Davide Pesavento45c1f6a2025-01-01 19:30:30 -0500128 makeRelPrefix(const std::string& verb) const
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500129 {
130 return PartialName(m_module).append(verb);
131 }
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700132
133private:
Junxiao Shi9ddf1b52016-08-22 03:58:55 +0000134 std::string m_module;
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500135 Dispatcher& m_dispatcher;
136 CommandAuthenticator* m_authenticator = nullptr;
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700137};
138
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700139} // namespace nfd
140
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500141#endif // NFD_DAEMON_MGMT_MANAGER_BASE_HPP