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