blob: cddb7c1cc367b7970863fe1b0519cad60d7be142 [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/*
3 * Copyright (c) 2014-2018, 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
Yanbiao Lidf846e52016-01-30 21:53:47 -080026#ifndef NFD_CORE_MANAGER_BASE_HPP
27#define NFD_CORE_MANAGER_BASE_HPP
Yanbiao Li698f4fe2015-08-19 16:30:16 -070028
29#include "common.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>
33#include <ndn-cxx/mgmt/nfd/control-response.hpp>
34#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
Yanbiao Li698f4fe2015-08-19 16:30:16 -070035
36namespace nfd {
37
38using ndn::mgmt::Dispatcher;
39
40using ndn::nfd::ControlCommand;
41using ndn::nfd::ControlResponse;
42using ndn::nfd::ControlParameters;
43
44/**
Yanbiao Lidf846e52016-01-30 21:53:47 -080045 * @brief a collection of common functions shared by all NFD managers and RIB manager,
Yanbiao Li698f4fe2015-08-19 16:30:16 -070046 * such as communicating with the dispatcher and command validator.
47 */
Yanbiao Lidf846e52016-01-30 21:53:47 -080048class ManagerBase : noncopyable
Yanbiao Li698f4fe2015-08-19 16:30:16 -070049{
50public:
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
61public:
Davide Pesaventod396b612017-02-20 22:11:50 -050062 ManagerBase(Dispatcher& dispatcher, const std::string& module);
63
64 virtual
65 ~ManagerBase();
Yanbiao Li698f4fe2015-08-19 16:30:16 -070066
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000067 const std::string&
68 getModule() const
69 {
70 return m_module;
71 }
72
Yanbiao Li698f4fe2015-08-19 16:30:16 -070073PUBLIC_WITH_TESTS_ELSE_PROTECTED: // registrations to the dispatcher
Yanbiao Li698f4fe2015-08-19 16:30:16 -070074 // difference from mgmt::ControlCommand: accepts nfd::ControlParameters
Davide Pesavento87fc0f82018-04-11 23:43:51 -040075 using ControlCommandHandler = std::function<void(const ControlCommand& command,
76 const Name& prefix, const Interest& interest,
77 const ControlParameters& parameters,
78 const ndn::mgmt::CommandContinuation done)>;
Yanbiao Li698f4fe2015-08-19 16:30:16 -070079
80 template<typename Command>
81 void
82 registerCommandHandler(const std::string& verb,
83 const ControlCommandHandler& handler);
84
85 void
86 registerStatusDatasetHandler(const std::string& verb,
87 const ndn::mgmt::StatusDatasetHandler& handler);
88
89 ndn::mgmt::PostNotification
90 registerNotificationStream(const std::string& verb);
91
Junxiao Shi21738402016-08-19 19:48:00 +000092PUBLIC_WITH_TESTS_ELSE_PROTECTED:
Yanbiao Li698f4fe2015-08-19 16:30:16 -070093 /**
94 * @brief extract a requester from a ControlCommand request
95 *
96 * This is called after the signature is validated.
97 *
98 * @param interest a request for ControlCommand
99 * @param accept callback of successful validation, take the requester string as a argument
100 */
101 void
102 extractRequester(const Interest& interest,
103 ndn::mgmt::AcceptContinuation accept);
104
Junxiao Shi21738402016-08-19 19:48:00 +0000105PUBLIC_WITH_TESTS_ELSE_PRIVATE:
106 /**
107 * @return an authorization function for specified management module and verb
108 */
109 virtual ndn::mgmt::Authorization
110 makeAuthorization(const std::string& verb) = 0;
111
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700112 /**
113 * @brief validate the @p parameters for a given @p command
114 *
115 * @param parameters the original ControlParameters
116 *
117 * @return whether the original ControlParameters can be validated
118 */
119 static bool
120 validateParameters(const nfd::ControlCommand& command,
121 const ndn::mgmt::ControlParameters& parameters);
122
123 /** @brief Handle control command
124 */
125 static void
126 handleCommand(shared_ptr<nfd::ControlCommand> command,
127 const ControlCommandHandler& handler,
128 const Name& prefix, const Interest& interest,
129 const ndn::mgmt::ControlParameters& params,
130 ndn::mgmt::CommandContinuation done);
131
132 /**
133 * @brief generate the relative prefix for a handler,
134 * by appending the verb name to the module name.
135 *
136 * @param verb the verb name
137 *
138 * @return the generated relative prefix
139 */
140 PartialName
141 makeRelPrefix(const std::string& verb);
142
143private:
Junxiao Shi9ddf1b52016-08-22 03:58:55 +0000144 Dispatcher& m_dispatcher;
145 std::string m_module;
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700146};
147
148inline PartialName
149ManagerBase::makeRelPrefix(const std::string& verb)
150{
Junxiao Shi9ddf1b52016-08-22 03:58:55 +0000151 return PartialName(m_module).append(verb);
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700152}
153
154template<typename Command>
155inline void
156ManagerBase::registerCommandHandler(const std::string& verb,
157 const ControlCommandHandler& handler)
158{
159 auto command = make_shared<Command>();
160
161 m_dispatcher.addControlCommand<ControlParameters>(
162 makeRelPrefix(verb),
Junxiao Shi21738402016-08-19 19:48:00 +0000163 makeAuthorization(verb),
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700164 bind(&ManagerBase::validateParameters, cref(*command), _1),
165 bind(&ManagerBase::handleCommand, command, handler, _1, _2, _3, _4));
166}
167
168} // namespace nfd
169
Yanbiao Lidf846e52016-01-30 21:53:47 -0800170#endif // NFD_CORE_MANAGER_BASE_HPP