Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 2 | /* |
| 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_CONTROL_COMMAND_HPP |
| 23 | #define NDN_MGMT_NFD_CONTROL_COMMAND_HPP |
| 24 | |
| 25 | #include "control-parameters.hpp" |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace nfd { |
| 29 | |
| 30 | /** |
| 31 | * \ingroup management |
| 32 | * \brief base class of NFD ControlCommand |
Alexander Afanasyev | e6835fe | 2017-01-19 20:05:01 -0800 | [diff] [blame] | 33 | * \sa https://redmine.named-data.net/projects/nfd/wiki/ControlCommand |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 34 | */ |
| 35 | class ControlCommand : noncopyable |
| 36 | { |
| 37 | public: |
| 38 | /** \brief represents an error in ControlParameters |
| 39 | */ |
| 40 | class ArgumentError : public std::invalid_argument |
| 41 | { |
| 42 | public: |
| 43 | explicit |
| 44 | ArgumentError(const std::string& what) |
| 45 | : std::invalid_argument(what) |
| 46 | { |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | virtual |
| 51 | ~ControlCommand(); |
| 52 | |
| 53 | /** \brief validate request parameters |
| 54 | * \throw ArgumentError if parameters are invalid |
| 55 | */ |
| 56 | virtual void |
| 57 | validateRequest(const ControlParameters& parameters) const; |
| 58 | |
| 59 | /** \brief apply default values to missing fields in request |
| 60 | */ |
| 61 | virtual void |
| 62 | applyDefaultsToRequest(ControlParameters& parameters) const; |
| 63 | |
| 64 | /** \brief validate response parameters |
| 65 | * \throw ArgumentError if parameters are invalid |
| 66 | */ |
| 67 | virtual void |
| 68 | validateResponse(const ControlParameters& parameters) const; |
| 69 | |
| 70 | /** \brief apply default values to missing fields in response |
| 71 | */ |
| 72 | virtual void |
| 73 | applyDefaultsToResponse(ControlParameters& parameters) const; |
| 74 | |
| 75 | /** \brief construct the Name for a request Interest |
| 76 | * \throw ArgumentError if parameters are invalid |
| 77 | */ |
| 78 | Name |
| 79 | getRequestName(const Name& commandPrefix, const ControlParameters& parameters) const; |
| 80 | |
| 81 | protected: |
| 82 | ControlCommand(const std::string& module, const std::string& verb); |
| 83 | |
| 84 | class FieldValidator |
| 85 | { |
| 86 | public: |
| 87 | FieldValidator(); |
| 88 | |
| 89 | /** \brief declare a required field |
| 90 | */ |
| 91 | FieldValidator& |
| 92 | required(ControlParameterField field) |
| 93 | { |
| 94 | m_required[field] = true; |
| 95 | return *this; |
| 96 | } |
| 97 | |
| 98 | /** \brief declare an optional field |
| 99 | */ |
| 100 | FieldValidator& |
| 101 | optional(ControlParameterField field) |
| 102 | { |
| 103 | m_optional[field] = true; |
| 104 | return *this; |
| 105 | } |
| 106 | |
| 107 | /** \brief verify that all required fields are present, |
| 108 | * and all present fields are either required or optional |
| 109 | * \throw ArgumentError |
| 110 | */ |
| 111 | void |
| 112 | validate(const ControlParameters& parameters) const; |
| 113 | |
| 114 | private: |
| 115 | std::vector<bool> m_required; |
| 116 | std::vector<bool> m_optional; |
| 117 | }; |
| 118 | |
| 119 | protected: |
| 120 | /** \brief FieldValidator for request ControlParameters |
| 121 | * |
| 122 | * Constructor of subclass should populate this validator. |
| 123 | */ |
| 124 | FieldValidator m_requestValidator; |
| 125 | /** \brief FieldValidator for response ControlParameters |
| 126 | * |
| 127 | * Constructor of subclass should populate this validator. |
| 128 | */ |
| 129 | FieldValidator m_responseValidator; |
| 130 | |
| 131 | private: |
| 132 | name::Component m_module; |
| 133 | name::Component m_verb; |
| 134 | }; |
| 135 | |
| 136 | |
| 137 | /** |
| 138 | * \ingroup management |
| 139 | * \brief represents a faces/create command |
Alexander Afanasyev | e6835fe | 2017-01-19 20:05:01 -0800 | [diff] [blame] | 140 | * \sa https://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Create-a-face |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 141 | */ |
| 142 | class FaceCreateCommand : public ControlCommand |
| 143 | { |
| 144 | public: |
| 145 | FaceCreateCommand(); |
| 146 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 147 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 148 | applyDefaultsToRequest(ControlParameters& parameters) const override; |
| 149 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 150 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 151 | validateResponse(const ControlParameters& parameters) const override; |
| 152 | }; |
| 153 | |
| 154 | |
| 155 | /** |
| 156 | * \ingroup management |
| 157 | * \brief represents a faces/update command |
Junxiao Shi | df50538 | 2018-03-04 13:40:44 +0000 | [diff] [blame] | 158 | * \sa https://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Update-the-static-properties-of-a-face |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 159 | */ |
| 160 | class FaceUpdateCommand : public ControlCommand |
| 161 | { |
| 162 | public: |
| 163 | FaceUpdateCommand(); |
| 164 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 165 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 166 | applyDefaultsToRequest(ControlParameters& parameters) const override; |
| 167 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 168 | /** |
| 169 | * \note This can only validate ControlParameters in a success response. |
| 170 | * Failure responses should be validated with validateRequest. |
| 171 | */ |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 172 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 173 | validateResponse(const ControlParameters& parameters) const override; |
| 174 | }; |
| 175 | |
| 176 | |
| 177 | /** |
| 178 | * \ingroup management |
| 179 | * \brief represents a faces/destroy command |
Alexander Afanasyev | e6835fe | 2017-01-19 20:05:01 -0800 | [diff] [blame] | 180 | * \sa https://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Destroy-a-face |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 181 | */ |
| 182 | class FaceDestroyCommand : public ControlCommand |
| 183 | { |
| 184 | public: |
| 185 | FaceDestroyCommand(); |
| 186 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 187 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 188 | validateRequest(const ControlParameters& parameters) const override; |
| 189 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 190 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 191 | validateResponse(const ControlParameters& parameters) const override; |
| 192 | }; |
| 193 | |
| 194 | |
| 195 | /** |
| 196 | * \ingroup management |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 197 | * \brief represents a fib/add-nexthop command |
Alexander Afanasyev | e6835fe | 2017-01-19 20:05:01 -0800 | [diff] [blame] | 198 | * \sa https://redmine.named-data.net/projects/nfd/wiki/FibMgmt#Add-a-nexthop |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 199 | */ |
| 200 | class FibAddNextHopCommand : public ControlCommand |
| 201 | { |
| 202 | public: |
| 203 | FibAddNextHopCommand(); |
| 204 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 205 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 206 | applyDefaultsToRequest(ControlParameters& parameters) const override; |
| 207 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 208 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 209 | validateResponse(const ControlParameters& parameters) const override; |
| 210 | }; |
| 211 | |
| 212 | |
| 213 | /** |
| 214 | * \ingroup management |
| 215 | * \brief represents a fib/remove-nexthop command |
Alexander Afanasyev | e6835fe | 2017-01-19 20:05:01 -0800 | [diff] [blame] | 216 | * \sa https://redmine.named-data.net/projects/nfd/wiki/FibMgmt#Remove-a-nexthop |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 217 | */ |
| 218 | class FibRemoveNextHopCommand : public ControlCommand |
| 219 | { |
| 220 | public: |
| 221 | FibRemoveNextHopCommand(); |
| 222 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 223 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 224 | applyDefaultsToRequest(ControlParameters& parameters) const override; |
| 225 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 226 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 227 | validateResponse(const ControlParameters& parameters) const override; |
| 228 | }; |
| 229 | |
| 230 | |
| 231 | /** |
| 232 | * \ingroup management |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 233 | * \brief represents a cs/config command |
Junxiao Shi | df50538 | 2018-03-04 13:40:44 +0000 | [diff] [blame] | 234 | * \sa https://redmine.named-data.net/projects/nfd/wiki/CsMgmt#Update-configuration |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 235 | */ |
| 236 | class CsConfigCommand : public ControlCommand |
| 237 | { |
| 238 | public: |
| 239 | CsConfigCommand(); |
| 240 | }; |
| 241 | |
| 242 | |
| 243 | /** |
| 244 | * \ingroup management |
Junxiao Shi | df50538 | 2018-03-04 13:40:44 +0000 | [diff] [blame] | 245 | * \brief represents a cs/erase command |
| 246 | * \sa https://redmine.named-data.net/projects/nfd/wiki/CsMgmt#Erase-entries |
| 247 | */ |
| 248 | class CsEraseCommand : public ControlCommand |
| 249 | { |
| 250 | public: |
| 251 | CsEraseCommand(); |
| 252 | |
| 253 | void |
| 254 | validateRequest(const ControlParameters& parameters) const override; |
| 255 | |
| 256 | void |
| 257 | validateResponse(const ControlParameters& parameters) const override; |
| 258 | }; |
| 259 | |
| 260 | |
| 261 | /** |
| 262 | * \ingroup management |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 263 | * \brief represents a strategy-choice/set command |
Alexander Afanasyev | e6835fe | 2017-01-19 20:05:01 -0800 | [diff] [blame] | 264 | * \sa https://redmine.named-data.net/projects/nfd/wiki/StrategyChoice#Set-the-strategy-for-a-namespace |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 265 | */ |
| 266 | class StrategyChoiceSetCommand : public ControlCommand |
| 267 | { |
| 268 | public: |
| 269 | StrategyChoiceSetCommand(); |
| 270 | }; |
| 271 | |
| 272 | |
| 273 | /** |
| 274 | * \ingroup management |
| 275 | * \brief represents a strategy-choice/set command |
Alexander Afanasyev | e6835fe | 2017-01-19 20:05:01 -0800 | [diff] [blame] | 276 | * \sa https://redmine.named-data.net/projects/nfd/wiki/StrategyChoice#Unset-the-strategy-for-a-namespace |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 277 | */ |
| 278 | class StrategyChoiceUnsetCommand : public ControlCommand |
| 279 | { |
| 280 | public: |
| 281 | StrategyChoiceUnsetCommand(); |
| 282 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 283 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 284 | validateRequest(const ControlParameters& parameters) const override; |
| 285 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 286 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 287 | validateResponse(const ControlParameters& parameters) const override; |
| 288 | }; |
| 289 | |
| 290 | |
| 291 | /** |
| 292 | * \ingroup management |
| 293 | * \brief represents a rib/register command |
Alexander Afanasyev | e6835fe | 2017-01-19 20:05:01 -0800 | [diff] [blame] | 294 | * \sa https://redmine.named-data.net/projects/nfd/wiki/RibMgmt#Register-a-route |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 295 | */ |
| 296 | class RibRegisterCommand : public ControlCommand |
| 297 | { |
| 298 | public: |
| 299 | RibRegisterCommand(); |
| 300 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 301 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 302 | applyDefaultsToRequest(ControlParameters& parameters) const override; |
| 303 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 304 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 305 | validateResponse(const ControlParameters& parameters) const override; |
| 306 | }; |
| 307 | |
| 308 | |
| 309 | /** |
| 310 | * \ingroup management |
| 311 | * \brief represents a rib/unregister command |
Alexander Afanasyev | e6835fe | 2017-01-19 20:05:01 -0800 | [diff] [blame] | 312 | * \sa https://redmine.named-data.net/projects/nfd/wiki/RibMgmt#Unregister-a-route |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 313 | */ |
| 314 | class RibUnregisterCommand : public ControlCommand |
| 315 | { |
| 316 | public: |
| 317 | RibUnregisterCommand(); |
| 318 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 319 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 320 | applyDefaultsToRequest(ControlParameters& parameters) const override; |
| 321 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 322 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 323 | validateResponse(const ControlParameters& parameters) const override; |
| 324 | }; |
| 325 | |
| 326 | } // namespace nfd |
| 327 | } // namespace ndn |
| 328 | |
| 329 | #endif // NDN_MGMT_NFD_CONTROL_COMMAND_HPP |