blob: f1d6df91f9797507dc7e5177b475005c163475b5 [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 Shi034c1882016-06-24 18:06:51 +000026#include "nfd-status-dataset.hpp"
27#include "nfd-command-options.hpp"
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070028#include "../face.hpp"
Junxiao Shi70911652014-08-12 10:14:24 -070029#include "../security/key-chain.hpp"
Junxiao Shi034c1882016-06-24 18:06:51 +000030#include "../security/validator-null.hpp"
Alexander Afanasyeve289b532014-02-09 22:14:44 -080031
32namespace ndn {
Alexander Afanasyeve289b532014-02-09 22:14:44 -080033namespace nfd {
34
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040035/**
Alexander Afanasyev197e5652014-06-13 16:56:31 -070036 * \defgroup management Management
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040037 * \brief Classes and data structures to manage NDN forwarder
38 */
Davide Pesavento18cf81b2015-09-12 23:36:43 +020039
Alexander Afanasyev4671bf72014-05-19 09:01:37 -040040/**
41 * \ingroup management
42 * \brief NFD Management protocol - ControlCommand client
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070043 */
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -070044class Controller : noncopyable
Alexander Afanasyeve289b532014-02-09 22:14:44 -080045{
46public:
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070047 /** \brief a callback on command success
Alexander Afanasyeve289b532014-02-09 22:14:44 -080048 */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070049 typedef function<void(const ControlParameters&)> CommandSucceedCallback;
50
51 /** \brief a callback on command failure
52 */
Junxiao Shi034c1882016-06-24 18:06:51 +000053 typedef function<void(uint32_t code, const std::string& reason)> CommandFailCallback;
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070054
Junxiao Shi70911652014-08-12 10:14:24 -070055 /** \brief construct a Controller that uses face for transport,
56 * and uses the passed KeyChain to sign commands
57 */
Junxiao Shi0f3f0b42016-07-14 13:26:37 +000058 Controller(Face& face, KeyChain& keyChain, Validator& validator = s_validatorNull);
Junxiao Shi70911652014-08-12 10:14:24 -070059
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070060 /** \brief start command execution
61 */
62 template<typename Command>
63 void
64 start(const ControlParameters& parameters,
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070065 const CommandSucceedCallback& onSuccess,
Junxiao Shi5c785d62014-04-20 18:10:20 -070066 const CommandFailCallback& onFailure,
Junxiao Shi5de006b2014-10-26 20:20:52 -070067 const CommandOptions& options = CommandOptions())
Yingdi Yue66bf2a2014-04-28 17:07:36 -070068 {
Junxiao Shi5de006b2014-10-26 20:20:52 -070069 shared_ptr<ControlCommand> command = make_shared<Command>();
70 this->startCommand(command, parameters, onSuccess, onFailure, options);
71 }
72
Junxiao Shi034c1882016-06-24 18:06:51 +000073 /** \brief start dataset fetching
74 */
75 template<typename Dataset>
76 typename std::enable_if<std::is_default_constructible<Dataset>::value>::type
77 fetch(const std::function<void(typename Dataset::ResultType)>& onSuccess,
78 const CommandFailCallback& onFailure,
79 const CommandOptions& options = CommandOptions())
80 {
81 this->fetchDataset(make_shared<Dataset>(), onSuccess, onFailure, options);
82 }
83
84 /** \brief start dataset fetching
85 */
86 template<typename Dataset, typename ParamType = typename Dataset::ParamType>
87 void
88 fetch(const ParamType& param,
89 const std::function<void(typename Dataset::ResultType)>& onSuccess,
90 const CommandFailCallback& onFailure,
91 const CommandOptions& options = CommandOptions())
92 {
93 this->fetchDataset(make_shared<Dataset>(param), onSuccess, onFailure, options);
94 }
95
Alexander Afanasyeve289b532014-02-09 22:14:44 -080096private:
97 void
Junxiao Shi70911652014-08-12 10:14:24 -070098 startCommand(const shared_ptr<ControlCommand>& command,
99 const ControlParameters& parameters,
100 const CommandSucceedCallback& onSuccess,
101 const CommandFailCallback& onFailure,
Junxiao Shi5de006b2014-10-26 20:20:52 -0700102 const CommandOptions& options);
103
Junxiao Shi70911652014-08-12 10:14:24 -0700104 void
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700105 processCommandResponse(const Data& data,
106 const shared_ptr<ControlCommand>& command,
107 const CommandSucceedCallback& onSuccess,
108 const CommandFailCallback& onFailure);
hilata7d160f22014-03-13 19:51:42 -0500109
Junxiao Shi54f727d2016-08-08 20:29:11 +0000110 void
111 processValidatedCommandResponse(const Data& data,
112 const shared_ptr<ControlCommand>& command,
113 const CommandSucceedCallback& onSuccess,
114 const CommandFailCallback& onFailure);
115
Junxiao Shi034c1882016-06-24 18:06:51 +0000116 template<typename Dataset>
117 void
118 fetchDataset(shared_ptr<Dataset> dataset,
119 const std::function<void(typename Dataset::ResultType)>& onSuccess,
120 const CommandFailCallback& onFailure,
121 const CommandOptions& options);
122
123 void
124 fetchDataset(const Name& prefix,
125 const std::function<void(const ConstBufferPtr&)>& processResponse,
126 const CommandFailCallback& onFailure,
127 const CommandOptions& options);
128
129 template<typename Dataset>
130 void
131 processDatasetResponse(shared_ptr<Dataset> dataset,
132 const std::function<void(typename Dataset::ResultType)>& onSuccess,
133 const CommandFailCallback& onFailure,
134 ConstBufferPtr payload);
135
136 void
137 processDatasetFetchError(const CommandFailCallback& onFailure, uint32_t code, std::string msg);
138
Junxiao Shi5c785d62014-04-20 18:10:20 -0700139public:
Junxiao Shi5de006b2014-10-26 20:20:52 -0700140 /** \brief error code for timeout
Junxiao Shi5de006b2014-10-26 20:20:52 -0700141 */
142 static const uint32_t ERROR_TIMEOUT;
143
Junxiao Shib1990df2015-11-05 00:14:44 +0000144 /** \brief error code for network Nack
145 */
146 static const uint32_t ERROR_NACK;
147
Junxiao Shi0f3f0b42016-07-14 13:26:37 +0000148 /** \brief error code for response validation failure
149 */
150 static const uint32_t ERROR_VALIDATION;
151
Junxiao Shi5de006b2014-10-26 20:20:52 -0700152 /** \brief error code for server error
153 */
154 static const uint32_t ERROR_SERVER;
155
156 /** \brief inclusive lower bound of error codes
157 */
158 static const uint32_t ERROR_LBOUND;
159
hilataa99e37e2014-02-15 23:52:46 -0600160protected:
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800161 Face& m_face;
Junxiao Shi70911652014-08-12 10:14:24 -0700162 KeyChain& m_keyChain;
Junxiao Shi034c1882016-06-24 18:06:51 +0000163 Validator& m_validator;
164
165private:
166 static ValidatorNull s_validatorNull;
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800167};
168
Junxiao Shi034c1882016-06-24 18:06:51 +0000169template<typename Dataset>
170inline void
171Controller::fetchDataset(shared_ptr<Dataset> dataset,
Junxiao Shi600f7112016-07-16 11:57:18 +0000172 const std::function<void(typename Dataset::ResultType)>& onSuccess1,
173 const CommandFailCallback& onFailure1,
Junxiao Shi034c1882016-06-24 18:06:51 +0000174 const CommandOptions& options)
175{
Junxiao Shi600f7112016-07-16 11:57:18 +0000176 const std::function<void(typename Dataset::ResultType)>& onSuccess = onSuccess1 ?
177 onSuccess1 : [] (const typename Dataset::ResultType&) {};
178 const CommandFailCallback& onFailure = onFailure1 ?
179 onFailure1 : [] (uint32_t, const std::string&) {};
180
Junxiao Shi034c1882016-06-24 18:06:51 +0000181 Name prefix = dataset->getDatasetPrefix(options.getPrefix());
182 this->fetchDataset(prefix,
183 bind(&Controller::processDatasetResponse<Dataset>, this, dataset, onSuccess, onFailure, _1),
184 onFailure,
185 options);
186}
187
188template<typename Dataset>
189inline void
190Controller::processDatasetResponse(shared_ptr<Dataset> dataset,
191 const std::function<void(typename Dataset::ResultType)>& onSuccess,
192 const CommandFailCallback& onFailure,
193 ConstBufferPtr payload)
194{
195 typename Dataset::ResultType result;
196 try {
197 result = dataset->parseResult(payload);
198 }
199 catch (const tlv::Error& ex) {
200 onFailure(ERROR_SERVER, ex.what());
201 return;
202 }
Junxiao Shi600f7112016-07-16 11:57:18 +0000203
Junxiao Shi034c1882016-06-24 18:06:51 +0000204 onSuccess(result);
205}
206
Alexander Afanasyeve289b532014-02-09 22:14:44 -0800207} // namespace nfd
208} // namespace ndn
209
Alexander Afanasyev26c24d22014-03-20 09:31:21 -0700210#endif // NDN_MANAGEMENT_NFD_CONTROLLER_HPP