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) |
| 101 | .optional(CONTROL_PARAMETER_FACE_PERSISTENCY); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 102 | m_responseValidator |
| 103 | .required(CONTROL_PARAMETER_URI) |
Yukai Tu | d93c5fc | 2015-08-25 11:37:16 +0800 | [diff] [blame] | 104 | .required(CONTROL_PARAMETER_FACE_ID) |
| 105 | .required(CONTROL_PARAMETER_FACE_PERSISTENCY); |
| 106 | } |
| 107 | |
| 108 | void |
| 109 | FaceCreateCommand::applyDefaultsToRequest(ControlParameters& parameters) const |
| 110 | { |
| 111 | if (!parameters.hasFacePersistency()) { |
| 112 | parameters.setFacePersistency(FacePersistency::FACE_PERSISTENCY_PERSISTENT); |
| 113 | } |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void |
| 117 | FaceCreateCommand::validateResponse(const ControlParameters& parameters) const |
| 118 | { |
| 119 | this->ControlCommand::validateResponse(parameters); |
| 120 | |
| 121 | if (parameters.getFaceId() == 0) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 122 | BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Yanbiao Li | cbdacb2 | 2016-08-02 16:02:35 +0800 | [diff] [blame] | 126 | FaceUpdateCommand::FaceUpdateCommand() |
| 127 | : ControlCommand("faces", "update") |
| 128 | { |
| 129 | m_requestValidator |
| 130 | .required(CONTROL_PARAMETER_FACE_ID) |
| 131 | .optional(CONTROL_PARAMETER_FACE_PERSISTENCY); |
| 132 | m_responseValidator |
| 133 | .required(CONTROL_PARAMETER_FACE_ID) |
| 134 | .optional(CONTROL_PARAMETER_FACE_PERSISTENCY); |
| 135 | m_responseValidator = m_requestValidator; |
| 136 | } |
| 137 | |
| 138 | void |
| 139 | FaceUpdateCommand::validateRequest(const ControlParameters& parameters) const |
| 140 | { |
| 141 | this->ControlCommand::validateRequest(parameters); |
| 142 | |
| 143 | if (parameters.getFaceId() == 0) { |
| 144 | BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero")); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void |
| 149 | FaceUpdateCommand::validateResponse(const ControlParameters& parameters) const |
| 150 | { |
| 151 | this->validateRequest(parameters); |
| 152 | } |
| 153 | |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 154 | FaceDestroyCommand::FaceDestroyCommand() |
| 155 | : ControlCommand("faces", "destroy") |
| 156 | { |
| 157 | m_requestValidator |
| 158 | .required(CONTROL_PARAMETER_FACE_ID); |
| 159 | m_responseValidator = m_requestValidator; |
| 160 | } |
| 161 | |
| 162 | void |
| 163 | FaceDestroyCommand::validateRequest(const ControlParameters& parameters) const |
| 164 | { |
| 165 | this->ControlCommand::validateRequest(parameters); |
| 166 | |
| 167 | if (parameters.getFaceId() == 0) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 168 | BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | FaceDestroyCommand::validateResponse(const ControlParameters& parameters) const |
| 174 | { |
| 175 | this->validateRequest(parameters); |
| 176 | } |
| 177 | |
| 178 | FaceLocalControlCommand::FaceLocalControlCommand(const std::string& verb) |
| 179 | : ControlCommand("faces", verb) |
| 180 | { |
| 181 | m_requestValidator |
| 182 | .required(CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE); |
| 183 | m_responseValidator = m_requestValidator; |
| 184 | } |
| 185 | |
| 186 | void |
| 187 | FaceLocalControlCommand::validateRequest(const ControlParameters& parameters) const |
| 188 | { |
| 189 | this->ControlCommand::validateRequest(parameters); |
| 190 | |
| 191 | switch (parameters.getLocalControlFeature()) { |
| 192 | case LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID: |
| 193 | case LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID: |
| 194 | break; |
| 195 | default: |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 196 | BOOST_THROW_EXCEPTION(ArgumentError("LocalControlFeature is invalid")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | void |
| 201 | FaceLocalControlCommand::validateResponse(const ControlParameters& parameters) const |
| 202 | { |
| 203 | this->validateRequest(parameters); |
| 204 | } |
| 205 | |
| 206 | FaceEnableLocalControlCommand::FaceEnableLocalControlCommand() |
| 207 | : FaceLocalControlCommand("enable-local-control") |
| 208 | { |
| 209 | } |
| 210 | |
| 211 | FaceDisableLocalControlCommand::FaceDisableLocalControlCommand() |
| 212 | : FaceLocalControlCommand("disable-local-control") |
| 213 | { |
| 214 | } |
| 215 | |
| 216 | FibAddNextHopCommand::FibAddNextHopCommand() |
| 217 | : ControlCommand("fib", "add-nexthop") |
| 218 | { |
| 219 | m_requestValidator |
| 220 | .required(CONTROL_PARAMETER_NAME) |
| 221 | .optional(CONTROL_PARAMETER_FACE_ID) |
| 222 | .optional(CONTROL_PARAMETER_COST); |
| 223 | m_responseValidator |
| 224 | .required(CONTROL_PARAMETER_NAME) |
| 225 | .required(CONTROL_PARAMETER_FACE_ID) |
| 226 | .required(CONTROL_PARAMETER_COST); |
| 227 | } |
| 228 | |
| 229 | void |
| 230 | FibAddNextHopCommand::applyDefaultsToRequest(ControlParameters& parameters) const |
| 231 | { |
| 232 | if (!parameters.hasFaceId()) { |
| 233 | parameters.setFaceId(0); |
| 234 | } |
| 235 | if (!parameters.hasCost()) { |
| 236 | parameters.setCost(0); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | void |
| 241 | FibAddNextHopCommand::validateResponse(const ControlParameters& parameters) const |
| 242 | { |
| 243 | this->ControlCommand::validateResponse(parameters); |
| 244 | |
| 245 | if (parameters.getFaceId() == 0) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 246 | BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
| 250 | FibRemoveNextHopCommand::FibRemoveNextHopCommand() |
| 251 | : ControlCommand("fib", "remove-nexthop") |
| 252 | { |
| 253 | m_requestValidator |
| 254 | .required(CONTROL_PARAMETER_NAME) |
| 255 | .optional(CONTROL_PARAMETER_FACE_ID); |
| 256 | m_responseValidator |
| 257 | .required(CONTROL_PARAMETER_NAME) |
| 258 | .required(CONTROL_PARAMETER_FACE_ID); |
| 259 | } |
| 260 | |
| 261 | void |
| 262 | FibRemoveNextHopCommand::applyDefaultsToRequest(ControlParameters& parameters) const |
| 263 | { |
| 264 | if (!parameters.hasFaceId()) { |
| 265 | parameters.setFaceId(0); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | void |
| 270 | FibRemoveNextHopCommand::validateResponse(const ControlParameters& parameters) const |
| 271 | { |
| 272 | this->ControlCommand::validateResponse(parameters); |
| 273 | |
| 274 | if (parameters.getFaceId() == 0) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 275 | BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
| 279 | StrategyChoiceSetCommand::StrategyChoiceSetCommand() |
| 280 | : ControlCommand("strategy-choice", "set") |
| 281 | { |
| 282 | m_requestValidator |
| 283 | .required(CONTROL_PARAMETER_NAME) |
| 284 | .required(CONTROL_PARAMETER_STRATEGY); |
| 285 | m_responseValidator = m_requestValidator; |
| 286 | } |
| 287 | |
| 288 | StrategyChoiceUnsetCommand::StrategyChoiceUnsetCommand() |
| 289 | : ControlCommand("strategy-choice", "unset") |
| 290 | { |
| 291 | m_requestValidator |
| 292 | .required(CONTROL_PARAMETER_NAME); |
| 293 | m_responseValidator = m_requestValidator; |
| 294 | } |
| 295 | |
| 296 | void |
| 297 | StrategyChoiceUnsetCommand::validateRequest(const ControlParameters& parameters) const |
| 298 | { |
| 299 | this->ControlCommand::validateRequest(parameters); |
| 300 | |
| 301 | if (parameters.getName().size() == 0) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 302 | BOOST_THROW_EXCEPTION(ArgumentError("Name must not be ndn:/")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
| 306 | void |
| 307 | StrategyChoiceUnsetCommand::validateResponse(const ControlParameters& parameters) const |
| 308 | { |
| 309 | this->validateRequest(parameters); |
| 310 | } |
| 311 | |
| 312 | RibRegisterCommand::RibRegisterCommand() |
| 313 | : ControlCommand("rib", "register") |
| 314 | { |
| 315 | m_requestValidator |
| 316 | .required(CONTROL_PARAMETER_NAME) |
| 317 | .optional(CONTROL_PARAMETER_FACE_ID) |
| 318 | .optional(CONTROL_PARAMETER_ORIGIN) |
| 319 | .optional(CONTROL_PARAMETER_COST) |
| 320 | .optional(CONTROL_PARAMETER_FLAGS) |
| 321 | .optional(CONTROL_PARAMETER_EXPIRATION_PERIOD); |
| 322 | m_responseValidator |
| 323 | .required(CONTROL_PARAMETER_NAME) |
| 324 | .required(CONTROL_PARAMETER_FACE_ID) |
| 325 | .required(CONTROL_PARAMETER_ORIGIN) |
| 326 | .required(CONTROL_PARAMETER_COST) |
| 327 | .required(CONTROL_PARAMETER_FLAGS) |
| 328 | .optional(CONTROL_PARAMETER_EXPIRATION_PERIOD); |
| 329 | } |
| 330 | |
| 331 | void |
| 332 | RibRegisterCommand::applyDefaultsToRequest(ControlParameters& parameters) const |
| 333 | { |
| 334 | if (!parameters.hasFaceId()) { |
| 335 | parameters.setFaceId(0); |
| 336 | } |
| 337 | if (!parameters.hasOrigin()) { |
| 338 | parameters.setOrigin(ROUTE_ORIGIN_APP); |
| 339 | } |
| 340 | if (!parameters.hasCost()) { |
| 341 | parameters.setCost(0); |
| 342 | } |
| 343 | if (!parameters.hasFlags()) { |
| 344 | parameters.setFlags(ROUTE_FLAG_CHILD_INHERIT); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | void |
| 349 | RibRegisterCommand::validateResponse(const ControlParameters& parameters) const |
| 350 | { |
| 351 | this->ControlCommand::validateResponse(parameters); |
| 352 | |
| 353 | if (parameters.getFaceId() == 0) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 354 | BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
| 358 | RibUnregisterCommand::RibUnregisterCommand() |
| 359 | : ControlCommand("rib", "unregister") |
| 360 | { |
| 361 | m_requestValidator |
| 362 | .required(CONTROL_PARAMETER_NAME) |
| 363 | .optional(CONTROL_PARAMETER_FACE_ID) |
| 364 | .optional(CONTROL_PARAMETER_ORIGIN); |
| 365 | m_responseValidator |
| 366 | .required(CONTROL_PARAMETER_NAME) |
| 367 | .required(CONTROL_PARAMETER_FACE_ID) |
| 368 | .required(CONTROL_PARAMETER_ORIGIN); |
| 369 | } |
| 370 | |
| 371 | void |
| 372 | RibUnregisterCommand::applyDefaultsToRequest(ControlParameters& parameters) const |
| 373 | { |
| 374 | if (!parameters.hasFaceId()) { |
| 375 | parameters.setFaceId(0); |
| 376 | } |
| 377 | if (!parameters.hasOrigin()) { |
| 378 | parameters.setOrigin(ROUTE_ORIGIN_APP); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | void |
| 383 | RibUnregisterCommand::validateResponse(const ControlParameters& parameters) const |
| 384 | { |
| 385 | this->ControlCommand::validateResponse(parameters); |
| 386 | |
| 387 | if (parameters.getFaceId() == 0) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 388 | BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
| 392 | } // namespace nfd |
| 393 | } // namespace ndn |