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