Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
| 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 |
| 33 | * \sa http://redmine.named-data.net/projects/nfd/wiki/ControlCommand |
| 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 |
| 140 | * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Create-a-face |
| 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 | validateRequest(const ControlParameters& parameters) const override; |
| 152 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame^] | 153 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 154 | validateResponse(const ControlParameters& parameters) const override; |
| 155 | }; |
| 156 | |
| 157 | |
| 158 | /** |
| 159 | * \ingroup management |
| 160 | * \brief represents a faces/update command |
| 161 | * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Update-a-face |
| 162 | */ |
| 163 | class FaceUpdateCommand : public ControlCommand |
| 164 | { |
| 165 | public: |
| 166 | FaceUpdateCommand(); |
| 167 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame^] | 168 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 169 | applyDefaultsToRequest(ControlParameters& parameters) const override; |
| 170 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame^] | 171 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 172 | validateRequest(const ControlParameters& parameters) const override; |
| 173 | |
| 174 | /** |
| 175 | * \note This can only validate ControlParameters in a success response. |
| 176 | * Failure responses should be validated with validateRequest. |
| 177 | */ |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame^] | 178 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 179 | validateResponse(const ControlParameters& parameters) const override; |
| 180 | }; |
| 181 | |
| 182 | |
| 183 | /** |
| 184 | * \ingroup management |
| 185 | * \brief represents a faces/destroy command |
| 186 | * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Destroy-a-face |
| 187 | */ |
| 188 | class FaceDestroyCommand : public ControlCommand |
| 189 | { |
| 190 | public: |
| 191 | FaceDestroyCommand(); |
| 192 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame^] | 193 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 194 | validateRequest(const ControlParameters& parameters) const override; |
| 195 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame^] | 196 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 197 | validateResponse(const ControlParameters& parameters) const override; |
| 198 | }; |
| 199 | |
| 200 | |
| 201 | /** |
| 202 | * \ingroup management |
| 203 | * \brief Base class for faces/[*]-local-control commands |
| 204 | */ |
| 205 | class FaceLocalControlCommand : public ControlCommand |
| 206 | { |
| 207 | public: |
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 | validateRequest(const ControlParameters& parameters) const override; |
| 210 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame^] | 211 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 212 | validateResponse(const ControlParameters& parameters) const override; |
| 213 | |
| 214 | protected: |
| 215 | explicit |
| 216 | FaceLocalControlCommand(const std::string& verb); |
| 217 | }; |
| 218 | |
| 219 | |
| 220 | /** |
| 221 | * \ingroup management |
| 222 | * \brief represents a faces/enable-local-control command |
| 223 | * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Enable-a-LocalControlHeader-feature |
| 224 | */ |
| 225 | class FaceEnableLocalControlCommand : public FaceLocalControlCommand |
| 226 | { |
| 227 | public: |
| 228 | FaceEnableLocalControlCommand(); |
| 229 | }; |
| 230 | |
| 231 | |
| 232 | /** |
| 233 | * \ingroup management |
| 234 | * \brief represents a faces/disable-local-control command |
| 235 | * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Disable-a-LocalControlHeader-feature |
| 236 | */ |
| 237 | class FaceDisableLocalControlCommand : public FaceLocalControlCommand |
| 238 | { |
| 239 | public: |
| 240 | FaceDisableLocalControlCommand(); |
| 241 | }; |
| 242 | |
| 243 | |
| 244 | /** |
| 245 | * \ingroup management |
| 246 | * \brief represents a fib/add-nexthop command |
| 247 | * \sa http://redmine.named-data.net/projects/nfd/wiki/FibMgmt#Add-a-nexthop |
| 248 | */ |
| 249 | class FibAddNextHopCommand : public ControlCommand |
| 250 | { |
| 251 | public: |
| 252 | FibAddNextHopCommand(); |
| 253 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame^] | 254 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 255 | applyDefaultsToRequest(ControlParameters& parameters) const override; |
| 256 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame^] | 257 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 258 | validateResponse(const ControlParameters& parameters) const override; |
| 259 | }; |
| 260 | |
| 261 | |
| 262 | /** |
| 263 | * \ingroup management |
| 264 | * \brief represents a fib/remove-nexthop command |
| 265 | * \sa http://redmine.named-data.net/projects/nfd/wiki/FibMgmt#Remove-a-nexthop |
| 266 | */ |
| 267 | class FibRemoveNextHopCommand : public ControlCommand |
| 268 | { |
| 269 | public: |
| 270 | FibRemoveNextHopCommand(); |
| 271 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame^] | 272 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 273 | applyDefaultsToRequest(ControlParameters& parameters) const override; |
| 274 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame^] | 275 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 276 | validateResponse(const ControlParameters& parameters) const override; |
| 277 | }; |
| 278 | |
| 279 | |
| 280 | /** |
| 281 | * \ingroup management |
| 282 | * \brief represents a strategy-choice/set command |
| 283 | * \sa http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice#Set-the-strategy-for-a-namespace |
| 284 | */ |
| 285 | class StrategyChoiceSetCommand : public ControlCommand |
| 286 | { |
| 287 | public: |
| 288 | StrategyChoiceSetCommand(); |
| 289 | }; |
| 290 | |
| 291 | |
| 292 | /** |
| 293 | * \ingroup management |
| 294 | * \brief represents a strategy-choice/set command |
| 295 | * \sa http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice#Unset-the-strategy-for-a-namespace |
| 296 | */ |
| 297 | class StrategyChoiceUnsetCommand : public ControlCommand |
| 298 | { |
| 299 | public: |
| 300 | StrategyChoiceUnsetCommand(); |
| 301 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame^] | 302 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 303 | validateRequest(const ControlParameters& parameters) const override; |
| 304 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame^] | 305 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 306 | validateResponse(const ControlParameters& parameters) const override; |
| 307 | }; |
| 308 | |
| 309 | |
| 310 | /** |
| 311 | * \ingroup management |
| 312 | * \brief represents a rib/register command |
| 313 | * \sa http://redmine.named-data.net/projects/nfd/wiki/RibMgmt#Register-a-route |
| 314 | */ |
| 315 | class RibRegisterCommand : public ControlCommand |
| 316 | { |
| 317 | public: |
| 318 | RibRegisterCommand(); |
| 319 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame^] | 320 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 321 | applyDefaultsToRequest(ControlParameters& parameters) const override; |
| 322 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame^] | 323 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 324 | validateResponse(const ControlParameters& parameters) const override; |
| 325 | }; |
| 326 | |
| 327 | |
| 328 | /** |
| 329 | * \ingroup management |
| 330 | * \brief represents a rib/unregister command |
| 331 | * \sa http://redmine.named-data.net/projects/nfd/wiki/RibMgmt#Unregister-a-route |
| 332 | */ |
| 333 | class RibUnregisterCommand : public ControlCommand |
| 334 | { |
| 335 | public: |
| 336 | RibUnregisterCommand(); |
| 337 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame^] | 338 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 339 | applyDefaultsToRequest(ControlParameters& parameters) const override; |
| 340 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame^] | 341 | void |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 342 | validateResponse(const ControlParameters& parameters) const override; |
| 343 | }; |
| 344 | |
| 345 | } // namespace nfd |
| 346 | } // namespace ndn |
| 347 | |
| 348 | #endif // NDN_MGMT_NFD_CONTROL_COMMAND_HPP |