blob: 297f113d70fe130bc9c831712e55c9578b3a26ae [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyeve289b532014-02-09 22:14:44 -08002/**
Junxiao Shi034c1882016-06-24 18:06:51 +00003 * Copyright (c) 2013-2016 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyeve289b532014-02-09 22:14:44 -080020 */
21
Alexander Afanasyev26c24d22014-03-20 09:31:21 -070022#ifndef NDN_MANAGEMENT_NFD_CONTROLLER_HPP
23#define NDN_MANAGEMENT_NFD_CONTROLLER_HPP
Alexander Afanasyeve289b532014-02-09 22:14:44 -080024
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070025#include "nfd-control-command.hpp"
Junxiao Shie7c7f152016-08-20 22:36:22 +000026#include "nfd-control-response.hpp"
Junxiao Shi034c1882016-06-24 18:06:51 +000027#include "nfd-status-dataset.hpp"
28#include "nfd-command-options.hpp"
Junxiao Shi034c1882016-06-24 18:06:51 +000029#include "../security/validator-null.hpp"
Alexander Afanasyeve289b532014-02-09 22:14:44 -080030
31namespace ndn {
Junxiao Shie7c7f152016-08-20 22:36:22 +000032
33namespace security {
34class KeyChain;
35} // namespace security
36class Face;
37class Validator;
38
Alexander Afanasyeve289b532014-02-09 22:14:44 -080039namespace nfd {
40
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040041/**
Alexander Afanasyev197e5652014-06-13 16:56:31 -070042 * \defgroup management Management
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040043 * \brief Classes and data structures to manage NDN forwarder
44 */
Davide Pesavento18cf81b2015-09-12 23:36:43 +020045
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040046/**
47 * \ingroup management
Junxiao Shie7c7f152016-08-20 22:36:22 +000048 * \brief NFD Management protocol client
49 * \sa https://redmine.named-data.net/projects/nfd/wiki/Management
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070050 */
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070051class Controller : noncopyable
Alexander Afanasyeve289b532014-02-09 22:14:44 -080052{
53public:
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070054 /** \brief a callback on command success
Alexander Afanasyeve289b532014-02-09 22:14:44 -080055 */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070056 typedef function<void(const ControlParameters&)> CommandSucceedCallback;
57
58 /** \brief a callback on command failure
59 */
Junxiao Shie7c7f152016-08-20 22:36:22 +000060 typedef function<void(const ControlResponse&)> CommandFailCallback;
61
62 /** \brief a callback on dataset retrieval failure
63 */
64 typedef function<void(uint32_t code, const std::string& reason)> DatasetFailCallback;
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070065
Junxiao Shi70911652014-08-12 10:14:24 -070066 /** \brief construct a Controller that uses face for transport,
67 * and uses the passed KeyChain to sign commands
68 */
Junxiao Shie7c7f152016-08-20 22:36:22 +000069 Controller(Face& face, security::KeyChain& keyChain, Validator& validator = s_validatorNull);
Junxiao Shi70911652014-08-12 10:14:24 -070070
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070071 /** \brief start command execution
72 */
73 template<typename Command>
74 void
75 start(const ControlParameters& parameters,
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070076 const CommandSucceedCallback& onSuccess,
Junxiao Shi5c785d62014-04-20 18:10:20 -070077 const CommandFailCallback& onFailure,
Junxiao Shi5de006b2014-10-26 20:20:52 -070078 const CommandOptions& options = CommandOptions())
Yingdi Yue66bf2a2014-04-28 17:07:36 -070079 {
Junxiao Shi5de006b2014-10-26 20:20:52 -070080 shared_ptr<ControlCommand> command = make_shared<Command>();
81 this->startCommand(command, parameters, onSuccess, onFailure, options);
82 }
83
Junxiao Shi034c1882016-06-24 18:06:51 +000084 /** \brief start dataset fetching
85 */
86 template<typename Dataset>
87 typename std::enable_if<std::is_default_constructible<Dataset>::value>::type
88 fetch(const std::function<void(typename Dataset::ResultType)>& onSuccess,
Junxiao Shie7c7f152016-08-20 22:36:22 +000089 const DatasetFailCallback& onFailure,
Junxiao Shi034c1882016-06-24 18:06:51 +000090 const CommandOptions& options = CommandOptions())
91 {
92 this->fetchDataset(make_shared<Dataset>(), onSuccess, onFailure, options);
93 }
94
95 /** \brief start dataset fetching
96 */
97 template<typename Dataset, typename ParamType = typename Dataset::ParamType>
98 void
99 fetch(const ParamType& param,
100 const std::function<void(typename Dataset::ResultType)>& onSuccess,
Junxiao Shie7c7f152016-08-20 22:36:22 +0000101 const DatasetFailCallback& onFailure,
Junxiao Shi034c1882016-06-24 18:06:51 +0000102 const CommandOptions& options = CommandOptions())
103 {
104 this->fetchDataset(make_shared<Dataset>(param), onSuccess, onFailure, options);
105 }
106
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800107private:
108 void
Junxiao Shi70911652014-08-12 10:14:24 -0700109 startCommand(const shared_ptr<ControlCommand>& command,
110 const ControlParameters& parameters,
111 const CommandSucceedCallback& onSuccess,
112 const CommandFailCallback& onFailure,
Junxiao Shi5de006b2014-10-26 20:20:52 -0700113 const CommandOptions& options);
114
Junxiao Shi70911652014-08-12 10:14:24 -0700115 void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700116 processCommandResponse(const Data& data,
117 const shared_ptr<ControlCommand>& command,
118 const CommandSucceedCallback& onSuccess,
119 const CommandFailCallback& onFailure);
hilata7d160f22014-03-13 19:51:42 -0500120
Junxiao Shi54f727d2016-08-08 20:29:11 +0000121 void
122 processValidatedCommandResponse(const Data& data,
123 const shared_ptr<ControlCommand>& command,
124 const CommandSucceedCallback& onSuccess,
125 const CommandFailCallback& onFailure);
126
Junxiao Shi034c1882016-06-24 18:06:51 +0000127 template<typename Dataset>
128 void
129 fetchDataset(shared_ptr<Dataset> dataset,
130 const std::function<void(typename Dataset::ResultType)>& onSuccess,
Junxiao Shie7c7f152016-08-20 22:36:22 +0000131 const DatasetFailCallback& onFailure,
Junxiao Shi034c1882016-06-24 18:06:51 +0000132 const CommandOptions& options);
133
134 void
135 fetchDataset(const Name& prefix,
136 const std::function<void(const ConstBufferPtr&)>& processResponse,
Junxiao Shie7c7f152016-08-20 22:36:22 +0000137 const DatasetFailCallback& onFailure,
Junxiao Shi034c1882016-06-24 18:06:51 +0000138 const CommandOptions& options);
139
140 template<typename Dataset>
141 void
142 processDatasetResponse(shared_ptr<Dataset> dataset,
143 const std::function<void(typename Dataset::ResultType)>& onSuccess,
Junxiao Shie7c7f152016-08-20 22:36:22 +0000144 const DatasetFailCallback& onFailure,
Junxiao Shi034c1882016-06-24 18:06:51 +0000145 ConstBufferPtr payload);
146
147 void
Junxiao Shie7c7f152016-08-20 22:36:22 +0000148 processDatasetFetchError(const DatasetFailCallback& onFailure, uint32_t code, std::string msg);
Junxiao Shi034c1882016-06-24 18:06:51 +0000149
Junxiao Shi5c785d62014-04-20 18:10:20 -0700150public:
Junxiao Shi5de006b2014-10-26 20:20:52 -0700151 /** \brief error code for timeout
Junxiao Shi5de006b2014-10-26 20:20:52 -0700152 */
153 static const uint32_t ERROR_TIMEOUT;
154
Junxiao Shib1990df2015-11-05 00:14:44 +0000155 /** \brief error code for network Nack
156 */
157 static const uint32_t ERROR_NACK;
158
Junxiao Shi0f3f0b42016-07-14 13:26:37 +0000159 /** \brief error code for response validation failure
160 */
161 static const uint32_t ERROR_VALIDATION;
162
Junxiao Shi5de006b2014-10-26 20:20:52 -0700163 /** \brief error code for server error
164 */
165 static const uint32_t ERROR_SERVER;
166
167 /** \brief inclusive lower bound of error codes
168 */
169 static const uint32_t ERROR_LBOUND;
170
hilataa99e37e2014-02-15 23:52:46 -0600171protected:
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800172 Face& m_face;
Junxiao Shie7c7f152016-08-20 22:36:22 +0000173 security::KeyChain& m_keyChain;
Junxiao Shi034c1882016-06-24 18:06:51 +0000174 Validator& m_validator;
175
176private:
177 static ValidatorNull s_validatorNull;
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800178};
179
Junxiao Shi034c1882016-06-24 18:06:51 +0000180template<typename Dataset>
181inline void
182Controller::fetchDataset(shared_ptr<Dataset> dataset,
Junxiao Shi600f7112016-07-16 11:57:18 +0000183 const std::function<void(typename Dataset::ResultType)>& onSuccess1,
Junxiao Shie7c7f152016-08-20 22:36:22 +0000184 const DatasetFailCallback& onFailure1,
Junxiao Shi034c1882016-06-24 18:06:51 +0000185 const CommandOptions& options)
186{
Junxiao Shi600f7112016-07-16 11:57:18 +0000187 const std::function<void(typename Dataset::ResultType)>& onSuccess = onSuccess1 ?
188 onSuccess1 : [] (const typename Dataset::ResultType&) {};
Junxiao Shie7c7f152016-08-20 22:36:22 +0000189 const DatasetFailCallback& onFailure = onFailure1 ?
Junxiao Shi600f7112016-07-16 11:57:18 +0000190 onFailure1 : [] (uint32_t, const std::string&) {};
191
Junxiao Shi034c1882016-06-24 18:06:51 +0000192 Name prefix = dataset->getDatasetPrefix(options.getPrefix());
193 this->fetchDataset(prefix,
194 bind(&Controller::processDatasetResponse<Dataset>, this, dataset, onSuccess, onFailure, _1),
195 onFailure,
196 options);
197}
198
199template<typename Dataset>
200inline void
201Controller::processDatasetResponse(shared_ptr<Dataset> dataset,
202 const std::function<void(typename Dataset::ResultType)>& onSuccess,
Junxiao Shie7c7f152016-08-20 22:36:22 +0000203 const DatasetFailCallback& onFailure,
Junxiao Shi034c1882016-06-24 18:06:51 +0000204 ConstBufferPtr payload)
205{
206 typename Dataset::ResultType result;
207 try {
208 result = dataset->parseResult(payload);
209 }
Junxiao Shie7c7f152016-08-20 22:36:22 +0000210 catch (const tlv::Error& e) {
211 onFailure(ERROR_SERVER, e.what());
Junxiao Shi034c1882016-06-24 18:06:51 +0000212 return;
213 }
Junxiao Shi600f7112016-07-16 11:57:18 +0000214
Junxiao Shi034c1882016-06-24 18:06:51 +0000215 onSuccess(result);
216}
217
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800218} // namespace nfd
219} // namespace ndn
220
Alexander Afanasyev26c24d22014-03-20 09:31:21 -0700221#endif // NDN_MANAGEMENT_NFD_CONTROLLER_HPP