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