Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 2 | /* |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * 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. |
| 20 | */ |
| 21 | |
| 22 | #ifndef NDN_MGMT_NFD_CONTROLLER_HPP |
| 23 | #define NDN_MGMT_NFD_CONTROLLER_HPP |
| 24 | |
| 25 | #include "control-command.hpp" |
| 26 | #include "control-response.hpp" |
| 27 | #include "status-dataset.hpp" |
| 28 | #include "command-options.hpp" |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 29 | #include "../../security/command-interest-signer.hpp" |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 30 | #include "../../security/validator-null.hpp" |
| 31 | #include "../../security/v2/key-chain.hpp" |
| 32 | #include "../../security/v2/validator.hpp" |
Ashlesh Gawande | 3e3a989 | 2018-11-16 20:26:15 -0600 | [diff] [blame^] | 33 | #include "../../util/segment-fetcher.hpp" |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 34 | |
| 35 | namespace ndn { |
| 36 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 37 | class Face; |
| 38 | |
| 39 | namespace nfd { |
| 40 | |
| 41 | /** |
| 42 | * \defgroup management Management |
| 43 | * \brief Classes and data structures to manage NDN forwarder |
| 44 | */ |
| 45 | |
| 46 | /** |
| 47 | * \ingroup management |
| 48 | * \brief NFD Management protocol client |
| 49 | * \sa https://redmine.named-data.net/projects/nfd/wiki/Management |
| 50 | */ |
| 51 | class Controller : noncopyable |
| 52 | { |
| 53 | public: |
| 54 | /** \brief a callback on command success |
| 55 | */ |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 56 | using CommandSucceedCallback = function<void(const ControlParameters&)>; |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 57 | |
| 58 | /** \brief a callback on command failure |
| 59 | */ |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 60 | using CommandFailCallback = function<void(const ControlResponse&)>; |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 61 | |
| 62 | /** \brief a callback on dataset retrieval failure |
| 63 | */ |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 64 | using DatasetFailCallback = function<void(uint32_t code, const std::string& reason)>; |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 65 | |
| 66 | /** \brief construct a Controller that uses face for transport, |
| 67 | * and uses the passed KeyChain to sign commands |
| 68 | */ |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 69 | Controller(Face& face, KeyChain& keyChain, |
| 70 | security::v2::Validator& validator = security::getAcceptAllValidator()); |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 71 | |
Ashlesh Gawande | 3e3a989 | 2018-11-16 20:26:15 -0600 | [diff] [blame^] | 72 | ~Controller(); |
| 73 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 74 | /** \brief start command execution |
| 75 | */ |
| 76 | template<typename Command> |
| 77 | void |
| 78 | start(const ControlParameters& parameters, |
| 79 | const CommandSucceedCallback& onSuccess, |
| 80 | const CommandFailCallback& onFailure, |
Eric Newberry | 71a2f03 | 2018-06-18 23:49:43 -0700 | [diff] [blame] | 81 | const CommandOptions& options = CommandOptions()) |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 82 | { |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 83 | startCommand(make_shared<Command>(), parameters, onSuccess, onFailure, options); |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** \brief start dataset fetching |
| 87 | */ |
| 88 | template<typename Dataset> |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 89 | std::enable_if_t<std::is_default_constructible<Dataset>::value> |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 90 | fetch(const std::function<void(typename Dataset::ResultType)>& onSuccess, |
| 91 | const DatasetFailCallback& onFailure, |
Eric Newberry | 71a2f03 | 2018-06-18 23:49:43 -0700 | [diff] [blame] | 92 | const CommandOptions& options = CommandOptions()) |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 93 | { |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 94 | fetchDataset(make_shared<Dataset>(), onSuccess, onFailure, options); |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** \brief start dataset fetching |
| 98 | */ |
| 99 | template<typename Dataset, typename ParamType = typename Dataset::ParamType> |
| 100 | void |
| 101 | fetch(const ParamType& param, |
| 102 | const std::function<void(typename Dataset::ResultType)>& onSuccess, |
| 103 | const DatasetFailCallback& onFailure, |
Eric Newberry | 71a2f03 | 2018-06-18 23:49:43 -0700 | [diff] [blame] | 104 | const CommandOptions& options = CommandOptions()) |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 105 | { |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 106 | fetchDataset(make_shared<Dataset>(param), onSuccess, onFailure, options); |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | private: |
| 110 | void |
| 111 | startCommand(const shared_ptr<ControlCommand>& command, |
| 112 | const ControlParameters& parameters, |
| 113 | const CommandSucceedCallback& onSuccess, |
| 114 | const CommandFailCallback& onFailure, |
| 115 | const CommandOptions& options); |
| 116 | |
| 117 | void |
| 118 | processCommandResponse(const Data& data, |
| 119 | const shared_ptr<ControlCommand>& command, |
| 120 | const CommandSucceedCallback& onSuccess, |
| 121 | const CommandFailCallback& onFailure); |
| 122 | |
| 123 | void |
| 124 | processValidatedCommandResponse(const Data& data, |
| 125 | const shared_ptr<ControlCommand>& command, |
| 126 | const CommandSucceedCallback& onSuccess, |
| 127 | const CommandFailCallback& onFailure); |
| 128 | |
| 129 | template<typename Dataset> |
| 130 | void |
| 131 | fetchDataset(shared_ptr<Dataset> dataset, |
| 132 | const std::function<void(typename Dataset::ResultType)>& onSuccess, |
| 133 | const DatasetFailCallback& onFailure, |
| 134 | const CommandOptions& options); |
| 135 | |
| 136 | void |
| 137 | fetchDataset(const Name& prefix, |
Eric Newberry | e345baa | 2018-05-23 18:17:07 -0700 | [diff] [blame] | 138 | const std::function<void(ConstBufferPtr)>& processResponse, |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 139 | const DatasetFailCallback& onFailure, |
| 140 | const CommandOptions& options); |
| 141 | |
| 142 | template<typename Dataset> |
| 143 | void |
| 144 | processDatasetResponse(shared_ptr<Dataset> dataset, |
| 145 | const std::function<void(typename Dataset::ResultType)>& onSuccess, |
| 146 | const DatasetFailCallback& onFailure, |
| 147 | ConstBufferPtr payload); |
| 148 | |
| 149 | void |
| 150 | processDatasetFetchError(const DatasetFailCallback& onFailure, uint32_t code, std::string msg); |
| 151 | |
| 152 | public: |
| 153 | /** \brief error code for timeout |
| 154 | */ |
| 155 | static const uint32_t ERROR_TIMEOUT; |
| 156 | |
| 157 | /** \brief error code for network Nack |
| 158 | */ |
| 159 | static const uint32_t ERROR_NACK; |
| 160 | |
| 161 | /** \brief error code for response validation failure |
| 162 | */ |
| 163 | static const uint32_t ERROR_VALIDATION; |
| 164 | |
| 165 | /** \brief error code for server error |
| 166 | */ |
| 167 | static const uint32_t ERROR_SERVER; |
| 168 | |
| 169 | /** \brief inclusive lower bound of error codes |
| 170 | */ |
| 171 | static const uint32_t ERROR_LBOUND; |
| 172 | |
| 173 | protected: |
| 174 | Face& m_face; |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 175 | KeyChain& m_keyChain; |
Alexander Afanasyev | 6dfeffe | 2017-01-30 22:40:32 -0800 | [diff] [blame] | 176 | security::v2::Validator& m_validator; |
Alexander Afanasyev | 80782e0 | 2017-01-04 13:16:54 -0800 | [diff] [blame] | 177 | security::CommandInterestSigner m_signer; |
Ashlesh Gawande | 3e3a989 | 2018-11-16 20:26:15 -0600 | [diff] [blame^] | 178 | |
| 179 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PROTECTED: |
| 180 | std::set<shared_ptr<util::SegmentFetcher>> m_fetchers; |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | template<typename Dataset> |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 184 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 185 | Controller::fetchDataset(shared_ptr<Dataset> dataset, |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 186 | const std::function<void(typename Dataset::ResultType)>& onSuccess, |
| 187 | const DatasetFailCallback& onFailure, |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 188 | const CommandOptions& options) |
| 189 | { |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 190 | Name prefix = dataset->getDatasetPrefix(options.getPrefix()); |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 191 | fetchDataset(prefix, |
| 192 | [=, d = std::move(dataset)] (ConstBufferPtr p) { |
| 193 | processDatasetResponse(std::move(d), onSuccess, onFailure, std::move(p)); |
| 194 | }, |
| 195 | onFailure, options); |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | template<typename Dataset> |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 199 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 200 | Controller::processDatasetResponse(shared_ptr<Dataset> dataset, |
| 201 | const std::function<void(typename Dataset::ResultType)>& onSuccess, |
| 202 | const DatasetFailCallback& onFailure, |
| 203 | ConstBufferPtr payload) |
| 204 | { |
| 205 | typename Dataset::ResultType result; |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 206 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 207 | try { |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 208 | result = dataset->parseResult(std::move(payload)); |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 209 | } |
| 210 | catch (const tlv::Error& e) { |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 211 | if (onFailure) |
| 212 | onFailure(ERROR_SERVER, e.what()); |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 213 | return; |
| 214 | } |
| 215 | |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 216 | if (onSuccess) |
| 217 | onSuccess(result); |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | } // namespace nfd |
| 221 | } // namespace ndn |
| 222 | |
| 223 | #endif // NDN_MGMT_NFD_CONTROLLER_HPP |