Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yanbiao Li | cbdacb2 | 2016-08-02 16:02:35 +0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [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 | #include "nfd-control-command.hpp" |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 23 | #include "nfd-command-options.hpp" // only used in deprecated functions |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 24 | |
| 25 | namespace ndn { |
| 26 | namespace nfd { |
| 27 | |
| 28 | ControlCommand::ControlCommand(const std::string& module, const std::string& verb) |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 29 | : m_module(module) |
| 30 | , m_verb(verb) |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 31 | { |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Davide Pesavento | aeeb3fc | 2016-08-14 03:40:02 +0200 | [diff] [blame] | 34 | ControlCommand::~ControlCommand() = default; |
| 35 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 36 | void |
| 37 | ControlCommand::validateRequest(const ControlParameters& parameters) const |
| 38 | { |
| 39 | m_requestValidator.validate(parameters); |
| 40 | } |
| 41 | |
| 42 | void |
| 43 | ControlCommand::applyDefaultsToRequest(ControlParameters& parameters) const |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | ControlCommand::validateResponse(const ControlParameters& parameters) const |
| 49 | { |
| 50 | m_responseValidator.validate(parameters); |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | ControlCommand::applyDefaultsToResponse(ControlParameters& parameters) const |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | Name |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 59 | ControlCommand::getRequestName(const Name& commandPrefix, |
| 60 | const ControlParameters& parameters) const |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 61 | { |
| 62 | this->validateRequest(parameters); |
| 63 | |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 64 | Name name = commandPrefix; |
| 65 | name.append(m_module).append(m_verb); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 66 | name.append(parameters.wireEncode()); |
| 67 | return name; |
| 68 | } |
| 69 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 70 | ControlCommand::FieldValidator::FieldValidator() |
| 71 | : m_required(CONTROL_PARAMETER_UBOUND) |
| 72 | , m_optional(CONTROL_PARAMETER_UBOUND) |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | ControlCommand::FieldValidator::validate(const ControlParameters& parameters) const |
| 78 | { |
| 79 | const std::vector<bool>& presentFields = parameters.getPresentFields(); |
| 80 | |
| 81 | for (size_t i = 0; i < CONTROL_PARAMETER_UBOUND; ++i) { |
| 82 | bool isPresent = presentFields[i]; |
| 83 | if (m_required[i]) { |
| 84 | if (!isPresent) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 85 | BOOST_THROW_EXCEPTION(ArgumentError(CONTROL_PARAMETER_FIELD[i] + " is required but " |
| 86 | "missing")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | else if (isPresent && !m_optional[i]) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 90 | BOOST_THROW_EXCEPTION(ArgumentError(CONTROL_PARAMETER_FIELD[i] + " is forbidden but " |
| 91 | "present")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | FaceCreateCommand::FaceCreateCommand() |
| 97 | : ControlCommand("faces", "create") |
| 98 | { |
| 99 | m_requestValidator |
Yukai Tu | d93c5fc | 2015-08-25 11:37:16 +0800 | [diff] [blame] | 100 | .required(CONTROL_PARAMETER_URI) |
Eric Newberry | da916d6 | 2016-08-11 23:04:34 -0700 | [diff] [blame] | 101 | .optional(CONTROL_PARAMETER_FACE_PERSISTENCY) |
| 102 | .optional(CONTROL_PARAMETER_FLAGS) |
| 103 | .optional(CONTROL_PARAMETER_MASK); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 104 | m_responseValidator |
Yukai Tu | d93c5fc | 2015-08-25 11:37:16 +0800 | [diff] [blame] | 105 | .required(CONTROL_PARAMETER_FACE_ID) |
Eric Newberry | da916d6 | 2016-08-11 23:04:34 -0700 | [diff] [blame] | 106 | .required(CONTROL_PARAMETER_FACE_PERSISTENCY) |
| 107 | .optional(CONTROL_PARAMETER_FLAGS) |
| 108 | .optional(CONTROL_PARAMETER_URI); |
Yukai Tu | d93c5fc | 2015-08-25 11:37:16 +0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void |
| 112 | FaceCreateCommand::applyDefaultsToRequest(ControlParameters& parameters) const |
| 113 | { |
Eric Newberry | da916d6 | 2016-08-11 23:04:34 -0700 | [diff] [blame] | 114 | parameters.setFaceId(0); |
| 115 | |
Yukai Tu | d93c5fc | 2015-08-25 11:37:16 +0800 | [diff] [blame] | 116 | if (!parameters.hasFacePersistency()) { |
| 117 | parameters.setFacePersistency(FacePersistency::FACE_PERSISTENCY_PERSISTENT); |
| 118 | } |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void |
Eric Newberry | da916d6 | 2016-08-11 23:04:34 -0700 | [diff] [blame] | 122 | FaceCreateCommand::validateRequest(const ControlParameters& parameters) const |
| 123 | { |
| 124 | this->ControlCommand::validateRequest(parameters); |
| 125 | |
| 126 | if (parameters.hasFlags() != parameters.hasMask()) { |
| 127 | BOOST_THROW_EXCEPTION(ArgumentError("Flags must be accompanied by Mask")); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 132 | FaceCreateCommand::validateResponse(const ControlParameters& parameters) const |
| 133 | { |
| 134 | this->ControlCommand::validateResponse(parameters); |
| 135 | |
| 136 | if (parameters.getFaceId() == 0) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 137 | BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Yanbiao Li | cbdacb2 | 2016-08-02 16:02:35 +0800 | [diff] [blame] | 141 | FaceUpdateCommand::FaceUpdateCommand() |
| 142 | : ControlCommand("faces", "update") |
| 143 | { |
| 144 | m_requestValidator |
Eric Newberry | da916d6 | 2016-08-11 23:04:34 -0700 | [diff] [blame] | 145 | .optional(CONTROL_PARAMETER_FACE_ID) |
| 146 | .optional(CONTROL_PARAMETER_FACE_PERSISTENCY) |
| 147 | .optional(CONTROL_PARAMETER_FLAGS) |
| 148 | .optional(CONTROL_PARAMETER_MASK); |
Yanbiao Li | cbdacb2 | 2016-08-02 16:02:35 +0800 | [diff] [blame] | 149 | m_responseValidator |
Eric Newberry | da916d6 | 2016-08-11 23:04:34 -0700 | [diff] [blame] | 150 | .optional(CONTROL_PARAMETER_FACE_ID) |
| 151 | .optional(CONTROL_PARAMETER_FACE_PERSISTENCY) |
| 152 | .optional(CONTROL_PARAMETER_FLAGS) |
| 153 | .optional(CONTROL_PARAMETER_MASK); |
| 154 | } |
| 155 | |
| 156 | void |
| 157 | FaceUpdateCommand::applyDefaultsToRequest(ControlParameters& parameters) const |
| 158 | { |
| 159 | parameters.setFaceId(0); |
Yanbiao Li | cbdacb2 | 2016-08-02 16:02:35 +0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void |
| 163 | FaceUpdateCommand::validateRequest(const ControlParameters& parameters) const |
| 164 | { |
| 165 | this->ControlCommand::validateRequest(parameters); |
| 166 | |
Eric Newberry | da916d6 | 2016-08-11 23:04:34 -0700 | [diff] [blame] | 167 | if (parameters.hasFlags() != parameters.hasMask()) { |
| 168 | BOOST_THROW_EXCEPTION(ArgumentError("Flags must be accompanied by Mask")); |
Yanbiao Li | cbdacb2 | 2016-08-02 16:02:35 +0800 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | FaceUpdateCommand::validateResponse(const ControlParameters& parameters) const |
| 174 | { |
| 175 | this->validateRequest(parameters); |
| 176 | } |
| 177 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 178 | FaceDestroyCommand::FaceDestroyCommand() |
| 179 | : ControlCommand("faces", "destroy") |
| 180 | { |
| 181 | m_requestValidator |
| 182 | .required(CONTROL_PARAMETER_FACE_ID); |
| 183 | m_responseValidator = m_requestValidator; |
| 184 | } |
| 185 | |
| 186 | void |
| 187 | FaceDestroyCommand::validateRequest(const ControlParameters& parameters) const |
| 188 | { |
| 189 | this->ControlCommand::validateRequest(parameters); |
| 190 | |
| 191 | if (parameters.getFaceId() == 0) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 192 | BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
| 196 | void |
| 197 | FaceDestroyCommand::validateResponse(const ControlParameters& parameters) const |
| 198 | { |
| 199 | this->validateRequest(parameters); |
| 200 | } |
| 201 | |
| 202 | FaceLocalControlCommand::FaceLocalControlCommand(const std::string& verb) |
| 203 | : ControlCommand("faces", verb) |
| 204 | { |
| 205 | m_requestValidator |
| 206 | .required(CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE); |
| 207 | m_responseValidator = m_requestValidator; |
| 208 | } |
| 209 | |
| 210 | void |
| 211 | FaceLocalControlCommand::validateRequest(const ControlParameters& parameters) const |
| 212 | { |
| 213 | this->ControlCommand::validateRequest(parameters); |
| 214 | |
| 215 | switch (parameters.getLocalControlFeature()) { |
| 216 | case LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID: |
| 217 | case LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID: |
| 218 | break; |
| 219 | default: |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 220 | BOOST_THROW_EXCEPTION(ArgumentError("LocalControlFeature is invalid")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
| 224 | void |
| 225 | FaceLocalControlCommand::validateResponse(const ControlParameters& parameters) const |
| 226 | { |
| 227 | this->validateRequest(parameters); |
| 228 | } |
| 229 | |
| 230 | FaceEnableLocalControlCommand::FaceEnableLocalControlCommand() |
| 231 | : FaceLocalControlCommand("enable-local-control") |
| 232 | { |
| 233 | } |
| 234 | |
| 235 | FaceDisableLocalControlCommand::FaceDisableLocalControlCommand() |
| 236 | : FaceLocalControlCommand("disable-local-control") |
| 237 | { |
| 238 | } |
| 239 | |
| 240 | FibAddNextHopCommand::FibAddNextHopCommand() |
| 241 | : ControlCommand("fib", "add-nexthop") |
| 242 | { |
| 243 | m_requestValidator |
| 244 | .required(CONTROL_PARAMETER_NAME) |
| 245 | .optional(CONTROL_PARAMETER_FACE_ID) |
| 246 | .optional(CONTROL_PARAMETER_COST); |
| 247 | m_responseValidator |
| 248 | .required(CONTROL_PARAMETER_NAME) |
| 249 | .required(CONTROL_PARAMETER_FACE_ID) |
| 250 | .required(CONTROL_PARAMETER_COST); |
| 251 | } |
| 252 | |
| 253 | void |
| 254 | FibAddNextHopCommand::applyDefaultsToRequest(ControlParameters& parameters) const |
| 255 | { |
| 256 | if (!parameters.hasFaceId()) { |
| 257 | parameters.setFaceId(0); |
| 258 | } |
| 259 | if (!parameters.hasCost()) { |
| 260 | parameters.setCost(0); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | void |
| 265 | FibAddNextHopCommand::validateResponse(const ControlParameters& parameters) const |
| 266 | { |
| 267 | this->ControlCommand::validateResponse(parameters); |
| 268 | |
| 269 | if (parameters.getFaceId() == 0) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 270 | BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
| 274 | FibRemoveNextHopCommand::FibRemoveNextHopCommand() |
| 275 | : ControlCommand("fib", "remove-nexthop") |
| 276 | { |
| 277 | m_requestValidator |
| 278 | .required(CONTROL_PARAMETER_NAME) |
| 279 | .optional(CONTROL_PARAMETER_FACE_ID); |
| 280 | m_responseValidator |
| 281 | .required(CONTROL_PARAMETER_NAME) |
| 282 | .required(CONTROL_PARAMETER_FACE_ID); |
| 283 | } |
| 284 | |
| 285 | void |
| 286 | FibRemoveNextHopCommand::applyDefaultsToRequest(ControlParameters& parameters) const |
| 287 | { |
| 288 | if (!parameters.hasFaceId()) { |
| 289 | parameters.setFaceId(0); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | void |
| 294 | FibRemoveNextHopCommand::validateResponse(const ControlParameters& parameters) const |
| 295 | { |
| 296 | this->ControlCommand::validateResponse(parameters); |
| 297 | |
| 298 | if (parameters.getFaceId() == 0) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 299 | BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
| 303 | StrategyChoiceSetCommand::StrategyChoiceSetCommand() |
| 304 | : ControlCommand("strategy-choice", "set") |
| 305 | { |
| 306 | m_requestValidator |
| 307 | .required(CONTROL_PARAMETER_NAME) |
| 308 | .required(CONTROL_PARAMETER_STRATEGY); |
| 309 | m_responseValidator = m_requestValidator; |
| 310 | } |
| 311 | |
| 312 | StrategyChoiceUnsetCommand::StrategyChoiceUnsetCommand() |
| 313 | : ControlCommand("strategy-choice", "unset") |
| 314 | { |
| 315 | m_requestValidator |
| 316 | .required(CONTROL_PARAMETER_NAME); |
| 317 | m_responseValidator = m_requestValidator; |
| 318 | } |
| 319 | |
| 320 | void |
| 321 | StrategyChoiceUnsetCommand::validateRequest(const ControlParameters& parameters) const |
| 322 | { |
| 323 | this->ControlCommand::validateRequest(parameters); |
| 324 | |
| 325 | if (parameters.getName().size() == 0) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 326 | BOOST_THROW_EXCEPTION(ArgumentError("Name must not be ndn:/")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
| 330 | void |
| 331 | StrategyChoiceUnsetCommand::validateResponse(const ControlParameters& parameters) const |
| 332 | { |
| 333 | this->validateRequest(parameters); |
| 334 | } |
| 335 | |
| 336 | RibRegisterCommand::RibRegisterCommand() |
| 337 | : ControlCommand("rib", "register") |
| 338 | { |
| 339 | m_requestValidator |
| 340 | .required(CONTROL_PARAMETER_NAME) |
| 341 | .optional(CONTROL_PARAMETER_FACE_ID) |
| 342 | .optional(CONTROL_PARAMETER_ORIGIN) |
| 343 | .optional(CONTROL_PARAMETER_COST) |
| 344 | .optional(CONTROL_PARAMETER_FLAGS) |
| 345 | .optional(CONTROL_PARAMETER_EXPIRATION_PERIOD); |
| 346 | m_responseValidator |
| 347 | .required(CONTROL_PARAMETER_NAME) |
| 348 | .required(CONTROL_PARAMETER_FACE_ID) |
| 349 | .required(CONTROL_PARAMETER_ORIGIN) |
| 350 | .required(CONTROL_PARAMETER_COST) |
| 351 | .required(CONTROL_PARAMETER_FLAGS) |
| 352 | .optional(CONTROL_PARAMETER_EXPIRATION_PERIOD); |
| 353 | } |
| 354 | |
| 355 | void |
| 356 | RibRegisterCommand::applyDefaultsToRequest(ControlParameters& parameters) const |
| 357 | { |
| 358 | if (!parameters.hasFaceId()) { |
| 359 | parameters.setFaceId(0); |
| 360 | } |
| 361 | if (!parameters.hasOrigin()) { |
| 362 | parameters.setOrigin(ROUTE_ORIGIN_APP); |
| 363 | } |
| 364 | if (!parameters.hasCost()) { |
| 365 | parameters.setCost(0); |
| 366 | } |
| 367 | if (!parameters.hasFlags()) { |
| 368 | parameters.setFlags(ROUTE_FLAG_CHILD_INHERIT); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | void |
| 373 | RibRegisterCommand::validateResponse(const ControlParameters& parameters) const |
| 374 | { |
| 375 | this->ControlCommand::validateResponse(parameters); |
| 376 | |
| 377 | if (parameters.getFaceId() == 0) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 378 | BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
| 382 | RibUnregisterCommand::RibUnregisterCommand() |
| 383 | : ControlCommand("rib", "unregister") |
| 384 | { |
| 385 | m_requestValidator |
| 386 | .required(CONTROL_PARAMETER_NAME) |
| 387 | .optional(CONTROL_PARAMETER_FACE_ID) |
| 388 | .optional(CONTROL_PARAMETER_ORIGIN); |
| 389 | m_responseValidator |
| 390 | .required(CONTROL_PARAMETER_NAME) |
| 391 | .required(CONTROL_PARAMETER_FACE_ID) |
| 392 | .required(CONTROL_PARAMETER_ORIGIN); |
| 393 | } |
| 394 | |
| 395 | void |
| 396 | RibUnregisterCommand::applyDefaultsToRequest(ControlParameters& parameters) const |
| 397 | { |
| 398 | if (!parameters.hasFaceId()) { |
| 399 | parameters.setFaceId(0); |
| 400 | } |
| 401 | if (!parameters.hasOrigin()) { |
| 402 | parameters.setOrigin(ROUTE_ORIGIN_APP); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | void |
| 407 | RibUnregisterCommand::validateResponse(const ControlParameters& parameters) const |
| 408 | { |
| 409 | this->ControlCommand::validateResponse(parameters); |
| 410 | |
| 411 | if (parameters.getFaceId() == 0) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 412 | BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
| 416 | } // namespace nfd |
| 417 | } // namespace ndn |