Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2014 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 | #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 | |
| 124 | FaceDestroyCommand::FaceDestroyCommand() |
| 125 | : ControlCommand("faces", "destroy") |
| 126 | { |
| 127 | m_requestValidator |
| 128 | .required(CONTROL_PARAMETER_FACE_ID); |
| 129 | m_responseValidator = m_requestValidator; |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | FaceDestroyCommand::validateRequest(const ControlParameters& parameters) const |
| 134 | { |
| 135 | this->ControlCommand::validateRequest(parameters); |
| 136 | |
| 137 | if (parameters.getFaceId() == 0) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 138 | BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | void |
| 143 | FaceDestroyCommand::validateResponse(const ControlParameters& parameters) const |
| 144 | { |
| 145 | this->validateRequest(parameters); |
| 146 | } |
| 147 | |
| 148 | FaceLocalControlCommand::FaceLocalControlCommand(const std::string& verb) |
| 149 | : ControlCommand("faces", verb) |
| 150 | { |
| 151 | m_requestValidator |
| 152 | .required(CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE); |
| 153 | m_responseValidator = m_requestValidator; |
| 154 | } |
| 155 | |
| 156 | void |
| 157 | FaceLocalControlCommand::validateRequest(const ControlParameters& parameters) const |
| 158 | { |
| 159 | this->ControlCommand::validateRequest(parameters); |
| 160 | |
| 161 | switch (parameters.getLocalControlFeature()) { |
| 162 | case LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID: |
| 163 | case LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID: |
| 164 | break; |
| 165 | default: |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 166 | BOOST_THROW_EXCEPTION(ArgumentError("LocalControlFeature is invalid")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | void |
| 171 | FaceLocalControlCommand::validateResponse(const ControlParameters& parameters) const |
| 172 | { |
| 173 | this->validateRequest(parameters); |
| 174 | } |
| 175 | |
| 176 | FaceEnableLocalControlCommand::FaceEnableLocalControlCommand() |
| 177 | : FaceLocalControlCommand("enable-local-control") |
| 178 | { |
| 179 | } |
| 180 | |
| 181 | FaceDisableLocalControlCommand::FaceDisableLocalControlCommand() |
| 182 | : FaceLocalControlCommand("disable-local-control") |
| 183 | { |
| 184 | } |
| 185 | |
| 186 | FibAddNextHopCommand::FibAddNextHopCommand() |
| 187 | : ControlCommand("fib", "add-nexthop") |
| 188 | { |
| 189 | m_requestValidator |
| 190 | .required(CONTROL_PARAMETER_NAME) |
| 191 | .optional(CONTROL_PARAMETER_FACE_ID) |
| 192 | .optional(CONTROL_PARAMETER_COST); |
| 193 | m_responseValidator |
| 194 | .required(CONTROL_PARAMETER_NAME) |
| 195 | .required(CONTROL_PARAMETER_FACE_ID) |
| 196 | .required(CONTROL_PARAMETER_COST); |
| 197 | } |
| 198 | |
| 199 | void |
| 200 | FibAddNextHopCommand::applyDefaultsToRequest(ControlParameters& parameters) const |
| 201 | { |
| 202 | if (!parameters.hasFaceId()) { |
| 203 | parameters.setFaceId(0); |
| 204 | } |
| 205 | if (!parameters.hasCost()) { |
| 206 | parameters.setCost(0); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | void |
| 211 | FibAddNextHopCommand::validateResponse(const ControlParameters& parameters) const |
| 212 | { |
| 213 | this->ControlCommand::validateResponse(parameters); |
| 214 | |
| 215 | if (parameters.getFaceId() == 0) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 216 | BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | FibRemoveNextHopCommand::FibRemoveNextHopCommand() |
| 221 | : ControlCommand("fib", "remove-nexthop") |
| 222 | { |
| 223 | m_requestValidator |
| 224 | .required(CONTROL_PARAMETER_NAME) |
| 225 | .optional(CONTROL_PARAMETER_FACE_ID); |
| 226 | m_responseValidator |
| 227 | .required(CONTROL_PARAMETER_NAME) |
| 228 | .required(CONTROL_PARAMETER_FACE_ID); |
| 229 | } |
| 230 | |
| 231 | void |
| 232 | FibRemoveNextHopCommand::applyDefaultsToRequest(ControlParameters& parameters) const |
| 233 | { |
| 234 | if (!parameters.hasFaceId()) { |
| 235 | parameters.setFaceId(0); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | void |
| 240 | FibRemoveNextHopCommand::validateResponse(const ControlParameters& parameters) const |
| 241 | { |
| 242 | this->ControlCommand::validateResponse(parameters); |
| 243 | |
| 244 | if (parameters.getFaceId() == 0) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 245 | BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
| 249 | StrategyChoiceSetCommand::StrategyChoiceSetCommand() |
| 250 | : ControlCommand("strategy-choice", "set") |
| 251 | { |
| 252 | m_requestValidator |
| 253 | .required(CONTROL_PARAMETER_NAME) |
| 254 | .required(CONTROL_PARAMETER_STRATEGY); |
| 255 | m_responseValidator = m_requestValidator; |
| 256 | } |
| 257 | |
| 258 | StrategyChoiceUnsetCommand::StrategyChoiceUnsetCommand() |
| 259 | : ControlCommand("strategy-choice", "unset") |
| 260 | { |
| 261 | m_requestValidator |
| 262 | .required(CONTROL_PARAMETER_NAME); |
| 263 | m_responseValidator = m_requestValidator; |
| 264 | } |
| 265 | |
| 266 | void |
| 267 | StrategyChoiceUnsetCommand::validateRequest(const ControlParameters& parameters) const |
| 268 | { |
| 269 | this->ControlCommand::validateRequest(parameters); |
| 270 | |
| 271 | if (parameters.getName().size() == 0) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 272 | BOOST_THROW_EXCEPTION(ArgumentError("Name must not be ndn:/")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
| 276 | void |
| 277 | StrategyChoiceUnsetCommand::validateResponse(const ControlParameters& parameters) const |
| 278 | { |
| 279 | this->validateRequest(parameters); |
| 280 | } |
| 281 | |
| 282 | RibRegisterCommand::RibRegisterCommand() |
| 283 | : ControlCommand("rib", "register") |
| 284 | { |
| 285 | m_requestValidator |
| 286 | .required(CONTROL_PARAMETER_NAME) |
| 287 | .optional(CONTROL_PARAMETER_FACE_ID) |
| 288 | .optional(CONTROL_PARAMETER_ORIGIN) |
| 289 | .optional(CONTROL_PARAMETER_COST) |
| 290 | .optional(CONTROL_PARAMETER_FLAGS) |
| 291 | .optional(CONTROL_PARAMETER_EXPIRATION_PERIOD); |
| 292 | m_responseValidator |
| 293 | .required(CONTROL_PARAMETER_NAME) |
| 294 | .required(CONTROL_PARAMETER_FACE_ID) |
| 295 | .required(CONTROL_PARAMETER_ORIGIN) |
| 296 | .required(CONTROL_PARAMETER_COST) |
| 297 | .required(CONTROL_PARAMETER_FLAGS) |
| 298 | .optional(CONTROL_PARAMETER_EXPIRATION_PERIOD); |
| 299 | } |
| 300 | |
| 301 | void |
| 302 | RibRegisterCommand::applyDefaultsToRequest(ControlParameters& parameters) const |
| 303 | { |
| 304 | if (!parameters.hasFaceId()) { |
| 305 | parameters.setFaceId(0); |
| 306 | } |
| 307 | if (!parameters.hasOrigin()) { |
| 308 | parameters.setOrigin(ROUTE_ORIGIN_APP); |
| 309 | } |
| 310 | if (!parameters.hasCost()) { |
| 311 | parameters.setCost(0); |
| 312 | } |
| 313 | if (!parameters.hasFlags()) { |
| 314 | parameters.setFlags(ROUTE_FLAG_CHILD_INHERIT); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | void |
| 319 | RibRegisterCommand::validateResponse(const ControlParameters& parameters) const |
| 320 | { |
| 321 | this->ControlCommand::validateResponse(parameters); |
| 322 | |
| 323 | if (parameters.getFaceId() == 0) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 324 | BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
| 328 | RibUnregisterCommand::RibUnregisterCommand() |
| 329 | : ControlCommand("rib", "unregister") |
| 330 | { |
| 331 | m_requestValidator |
| 332 | .required(CONTROL_PARAMETER_NAME) |
| 333 | .optional(CONTROL_PARAMETER_FACE_ID) |
| 334 | .optional(CONTROL_PARAMETER_ORIGIN); |
| 335 | m_responseValidator |
| 336 | .required(CONTROL_PARAMETER_NAME) |
| 337 | .required(CONTROL_PARAMETER_FACE_ID) |
| 338 | .required(CONTROL_PARAMETER_ORIGIN); |
| 339 | } |
| 340 | |
| 341 | void |
| 342 | RibUnregisterCommand::applyDefaultsToRequest(ControlParameters& parameters) const |
| 343 | { |
| 344 | if (!parameters.hasFaceId()) { |
| 345 | parameters.setFaceId(0); |
| 346 | } |
| 347 | if (!parameters.hasOrigin()) { |
| 348 | parameters.setOrigin(ROUTE_ORIGIN_APP); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | void |
| 353 | RibUnregisterCommand::validateResponse(const ControlParameters& parameters) const |
| 354 | { |
| 355 | this->ControlCommand::validateResponse(parameters); |
| 356 | |
| 357 | if (parameters.getFaceId() == 0) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 358 | BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero")); |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
| 362 | } // namespace nfd |
| 363 | } // namespace ndn |