blob: 31de566a03db8eeacd114253d5c51a297c5a580b [file] [log] [blame]
Yanbiao Li698f4fe2015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yanbiao Lidf846e52016-01-30 21:53:47 -08003 * Copyright (c) 2014-2016, 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>
32#include <ndn-cxx/management/nfd-control-command.hpp>
33#include <ndn-cxx/management/nfd-control-response.hpp>
34#include <ndn-cxx/management/nfd-control-parameters.hpp>
35
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:
62 ManagerBase(Dispatcher& dispatcher,
Yanbiao Li698f4fe2015-08-19 16:30:16 -070063 const std::string& module);
64
65PUBLIC_WITH_TESTS_ELSE_PROTECTED: // registrations to the dispatcher
66
67 // difference from mgmt::ControlCommand: accepts nfd::ControlParameters
68 typedef function<void(const ControlCommand& command,
69 const Name& prefix, const Interest& interest,
70 const ControlParameters& parameters,
71 const ndn::mgmt::CommandContinuation done)> ControlCommandHandler;
72
73 template<typename Command>
74 void
75 registerCommandHandler(const std::string& verb,
76 const ControlCommandHandler& handler);
77
78 void
79 registerStatusDatasetHandler(const std::string& verb,
80 const ndn::mgmt::StatusDatasetHandler& handler);
81
82 ndn::mgmt::PostNotification
83 registerNotificationStream(const std::string& verb);
84
Yanbiao Lidf846e52016-01-30 21:53:47 -080085PUBLIC_WITH_TESTS_ELSE_PROTECTED: // command validation
Yanbiao Li698f4fe2015-08-19 16:30:16 -070086 /**
87 * @brief validate a request for ControlCommand.
88 *
89 * This is called by the dispatcher.
90 *
91 * @pre params != null
92 * @pre typeid(*params) == typeid(ndn::nfd::ControlParameters)
93 *
94 * @param prefix the top prefix
95 * @param interest a request for ControlCommand
96 * @param params the parameters for ControlCommand
97 * @param accept callback of successful validation, take the requester string as a argument
98 * @param reject callback of failure in validation, take the action code as a argument
99 */
Yanbiao Lidf846e52016-01-30 21:53:47 -0800100 virtual void
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700101 authorize(const Name& prefix, const Interest& interest,
102 const ndn::mgmt::ControlParameters* params,
103 ndn::mgmt::AcceptContinuation accept,
Yanbiao Lidf846e52016-01-30 21:53:47 -0800104 ndn::mgmt::RejectContinuation reject) = 0;
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700105
106 /**
107 * @brief extract a requester from a ControlCommand request
108 *
109 * This is called after the signature is validated.
110 *
111 * @param interest a request for ControlCommand
112 * @param accept callback of successful validation, take the requester string as a argument
113 */
114 void
115 extractRequester(const Interest& interest,
116 ndn::mgmt::AcceptContinuation accept);
117
118PUBLIC_WITH_TESTS_ELSE_PRIVATE: // helpers
119 /**
120 * @brief validate the @p parameters for a given @p command
121 *
122 * @param parameters the original ControlParameters
123 *
124 * @return whether the original ControlParameters can be validated
125 */
126 static bool
127 validateParameters(const nfd::ControlCommand& command,
128 const ndn::mgmt::ControlParameters& parameters);
129
130 /** @brief Handle control command
131 */
132 static void
133 handleCommand(shared_ptr<nfd::ControlCommand> command,
134 const ControlCommandHandler& handler,
135 const Name& prefix, const Interest& interest,
136 const ndn::mgmt::ControlParameters& params,
137 ndn::mgmt::CommandContinuation done);
138
139 /**
140 * @brief generate the relative prefix for a handler,
141 * by appending the verb name to the module name.
142 *
143 * @param verb the verb name
144 *
145 * @return the generated relative prefix
146 */
147 PartialName
148 makeRelPrefix(const std::string& verb);
149
150private:
151 Dispatcher& m_dispatcher;
Yanbiao Li698f4fe2015-08-19 16:30:16 -0700152 std::string m_mgmtModuleName;
153};
154
155inline PartialName
156ManagerBase::makeRelPrefix(const std::string& verb)
157{
158 return PartialName(m_mgmtModuleName).append(verb);
159}
160
161template<typename Command>
162inline void
163ManagerBase::registerCommandHandler(const std::string& verb,
164 const ControlCommandHandler& handler)
165{
166 auto command = make_shared<Command>();
167
168 m_dispatcher.addControlCommand<ControlParameters>(
169 makeRelPrefix(verb),
170 bind(&ManagerBase::authorize, this, _1, _2, _3, _4, _5),
171 bind(&ManagerBase::validateParameters, cref(*command), _1),
172 bind(&ManagerBase::handleCommand, command, handler, _1, _2, _3, _4));
173}
174
175} // namespace nfd
176
Yanbiao Lidf846e52016-01-30 21:53:47 -0800177#endif // NFD_CORE_MANAGER_BASE_HPP