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