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