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