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