blob: f851bf15c03513406ad45ab8cc99c7ecc6f73863 [file] [log] [blame]
Junxiao Shi70911652014-08-12 10:14:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi22f85682018-01-22 19:23:22 +00002/*
3 * Copyright (c) 2013-2018 Regents of the University of California.
Junxiao Shi70911652014-08-12 10:14:24 -07004 *
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 Shi7357ef22016-09-07 02:39:37 +000022#include "control-command.hpp"
Junxiao Shi70911652014-08-12 10:14:24 -070023
24namespace ndn {
25namespace nfd {
26
27ControlCommand::ControlCommand(const std::string& module, const std::string& verb)
Junxiao Shi5de006b2014-10-26 20:20:52 -070028 : m_module(module)
29 , m_verb(verb)
Junxiao Shi70911652014-08-12 10:14:24 -070030{
Junxiao Shi70911652014-08-12 10:14:24 -070031}
32
Davide Pesaventoaeeb3fc2016-08-14 03:40:02 +020033ControlCommand::~ControlCommand() = default;
34
Junxiao Shi70911652014-08-12 10:14:24 -070035void
36ControlCommand::validateRequest(const ControlParameters& parameters) const
37{
38 m_requestValidator.validate(parameters);
39}
40
41void
42ControlCommand::applyDefaultsToRequest(ControlParameters& parameters) const
43{
44}
45
46void
47ControlCommand::validateResponse(const ControlParameters& parameters) const
48{
49 m_responseValidator.validate(parameters);
50}
51
52void
53ControlCommand::applyDefaultsToResponse(ControlParameters& parameters) const
54{
55}
56
57Name
Junxiao Shi5de006b2014-10-26 20:20:52 -070058ControlCommand::getRequestName(const Name& commandPrefix,
59 const ControlParameters& parameters) const
Junxiao Shi70911652014-08-12 10:14:24 -070060{
61 this->validateRequest(parameters);
62
Junxiao Shi5de006b2014-10-26 20:20:52 -070063 Name name = commandPrefix;
64 name.append(m_module).append(m_verb);
Junxiao Shi70911652014-08-12 10:14:24 -070065 name.append(parameters.wireEncode());
66 return name;
67}
68
Junxiao Shi70911652014-08-12 10:14:24 -070069ControlCommand::FieldValidator::FieldValidator()
70 : m_required(CONTROL_PARAMETER_UBOUND)
71 , m_optional(CONTROL_PARAMETER_UBOUND)
72{
73}
74
75void
76ControlCommand::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 Pesaventof8503d22017-02-17 01:19:10 -050084 BOOST_THROW_EXCEPTION(ArgumentError(CONTROL_PARAMETER_FIELD[i] + " is required but missing"));
Junxiao Shi70911652014-08-12 10:14:24 -070085 }
86 }
87 else if (isPresent && !m_optional[i]) {
Davide Pesaventof8503d22017-02-17 01:19:10 -050088 BOOST_THROW_EXCEPTION(ArgumentError(CONTROL_PARAMETER_FIELD[i] + " is forbidden but present"));
Junxiao Shi70911652014-08-12 10:14:24 -070089 }
90 }
Junxiao Shi22f85682018-01-22 19:23:22 +000091
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 Shi70911652014-08-12 10:14:24 -070097}
98
99FaceCreateCommand::FaceCreateCommand()
100 : ControlCommand("faces", "create")
101{
102 m_requestValidator
Yukai Tud93c5fc2015-08-25 11:37:16 +0800103 .required(CONTROL_PARAMETER_URI)
Teng Liangf4908b12017-03-29 15:24:03 -0700104 .optional(CONTROL_PARAMETER_LOCAL_URI)
Eric Newberryda916d62016-08-11 23:04:34 -0700105 .optional(CONTROL_PARAMETER_FACE_PERSISTENCY)
Eric Newberry07d05c92018-01-22 16:08:01 -0700106 .optional(CONTROL_PARAMETER_BASE_CONGESTION_MARKING_INTERVAL)
107 .optional(CONTROL_PARAMETER_DEFAULT_CONGESTION_THRESHOLD)
Eric Newberry3c9bc042018-06-02 17:58:10 -0700108 .optional(CONTROL_PARAMETER_MTU)
Eric Newberryda916d62016-08-11 23:04:34 -0700109 .optional(CONTROL_PARAMETER_FLAGS)
110 .optional(CONTROL_PARAMETER_MASK);
Junxiao Shi70911652014-08-12 10:14:24 -0700111 m_responseValidator
Yukai Tud93c5fc2015-08-25 11:37:16 +0800112 .required(CONTROL_PARAMETER_FACE_ID)
Junxiao Shi8c2ab2e2017-05-05 20:26:34 +0000113 .required(CONTROL_PARAMETER_URI)
114 .required(CONTROL_PARAMETER_LOCAL_URI)
Eric Newberryda916d62016-08-11 23:04:34 -0700115 .required(CONTROL_PARAMETER_FACE_PERSISTENCY)
Eric Newberryd567aab2018-01-27 16:38:24 -0700116 .optional(CONTROL_PARAMETER_BASE_CONGESTION_MARKING_INTERVAL)
117 .optional(CONTROL_PARAMETER_DEFAULT_CONGESTION_THRESHOLD)
Eric Newberry3c9bc042018-06-02 17:58:10 -0700118 .optional(CONTROL_PARAMETER_MTU)
Junxiao Shi8c2ab2e2017-05-05 20:26:34 +0000119 .required(CONTROL_PARAMETER_FLAGS);
Yukai Tud93c5fc2015-08-25 11:37:16 +0800120}
121
122void
123FaceCreateCommand::applyDefaultsToRequest(ControlParameters& parameters) const
124{
125 if (!parameters.hasFacePersistency()) {
126 parameters.setFacePersistency(FacePersistency::FACE_PERSISTENCY_PERSISTENT);
127 }
Junxiao Shi70911652014-08-12 10:14:24 -0700128}
129
130void
131FaceCreateCommand::validateResponse(const ControlParameters& parameters) const
132{
133 this->ControlCommand::validateResponse(parameters);
134
Davide Pesaventof8503d22017-02-17 01:19:10 -0500135 if (parameters.getFaceId() == INVALID_FACE_ID) {
136 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must be valid"));
Junxiao Shi70911652014-08-12 10:14:24 -0700137 }
138}
139
Yanbiao Licbdacb22016-08-02 16:02:35 +0800140FaceUpdateCommand::FaceUpdateCommand()
141 : ControlCommand("faces", "update")
142{
143 m_requestValidator
Eric Newberryda916d62016-08-11 23:04:34 -0700144 .optional(CONTROL_PARAMETER_FACE_ID)
145 .optional(CONTROL_PARAMETER_FACE_PERSISTENCY)
Eric Newberry07d05c92018-01-22 16:08:01 -0700146 .optional(CONTROL_PARAMETER_BASE_CONGESTION_MARKING_INTERVAL)
147 .optional(CONTROL_PARAMETER_DEFAULT_CONGESTION_THRESHOLD)
Eric Newberryda916d62016-08-11 23:04:34 -0700148 .optional(CONTROL_PARAMETER_FLAGS)
149 .optional(CONTROL_PARAMETER_MASK);
Yanbiao Licbdacb22016-08-02 16:02:35 +0800150 m_responseValidator
Eric Newberry138ef2c2016-08-15 20:29:03 -0700151 .required(CONTROL_PARAMETER_FACE_ID)
152 .required(CONTROL_PARAMETER_FACE_PERSISTENCY)
Eric Newberryd567aab2018-01-27 16:38:24 -0700153 .optional(CONTROL_PARAMETER_BASE_CONGESTION_MARKING_INTERVAL)
154 .optional(CONTROL_PARAMETER_DEFAULT_CONGESTION_THRESHOLD)
Eric Newberry138ef2c2016-08-15 20:29:03 -0700155 .required(CONTROL_PARAMETER_FLAGS);
Eric Newberryda916d62016-08-11 23:04:34 -0700156}
157
158void
159FaceUpdateCommand::applyDefaultsToRequest(ControlParameters& parameters) const
160{
Eric Newberry138ef2c2016-08-15 20:29:03 -0700161 if (!parameters.hasFaceId()) {
162 parameters.setFaceId(0);
163 }
Yanbiao Licbdacb22016-08-02 16:02:35 +0800164}
165
166void
Yanbiao Licbdacb22016-08-02 16:02:35 +0800167FaceUpdateCommand::validateResponse(const ControlParameters& parameters) const
168{
Eric Newberry138ef2c2016-08-15 20:29:03 -0700169 this->ControlCommand::validateResponse(parameters);
170
Davide Pesaventof8503d22017-02-17 01:19:10 -0500171 if (parameters.getFaceId() == INVALID_FACE_ID) {
172 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must be valid"));
Eric Newberry138ef2c2016-08-15 20:29:03 -0700173 }
Yanbiao Licbdacb22016-08-02 16:02:35 +0800174}
175
Junxiao Shi70911652014-08-12 10:14:24 -0700176FaceDestroyCommand::FaceDestroyCommand()
177 : ControlCommand("faces", "destroy")
178{
179 m_requestValidator
180 .required(CONTROL_PARAMETER_FACE_ID);
181 m_responseValidator = m_requestValidator;
182}
183
184void
185FaceDestroyCommand::validateRequest(const ControlParameters& parameters) const
186{
187 this->ControlCommand::validateRequest(parameters);
188
Davide Pesaventof8503d22017-02-17 01:19:10 -0500189 if (parameters.getFaceId() == INVALID_FACE_ID) {
190 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must be valid"));
Junxiao Shi70911652014-08-12 10:14:24 -0700191 }
192}
193
194void
195FaceDestroyCommand::validateResponse(const ControlParameters& parameters) const
196{
197 this->validateRequest(parameters);
198}
199
Junxiao Shi70911652014-08-12 10:14:24 -0700200FibAddNextHopCommand::FibAddNextHopCommand()
201 : ControlCommand("fib", "add-nexthop")
202{
203 m_requestValidator
204 .required(CONTROL_PARAMETER_NAME)
205 .optional(CONTROL_PARAMETER_FACE_ID)
206 .optional(CONTROL_PARAMETER_COST);
207 m_responseValidator
208 .required(CONTROL_PARAMETER_NAME)
209 .required(CONTROL_PARAMETER_FACE_ID)
210 .required(CONTROL_PARAMETER_COST);
211}
212
213void
214FibAddNextHopCommand::applyDefaultsToRequest(ControlParameters& parameters) const
215{
216 if (!parameters.hasFaceId()) {
217 parameters.setFaceId(0);
218 }
219 if (!parameters.hasCost()) {
220 parameters.setCost(0);
221 }
222}
223
224void
225FibAddNextHopCommand::validateResponse(const ControlParameters& parameters) const
226{
227 this->ControlCommand::validateResponse(parameters);
228
Davide Pesaventof8503d22017-02-17 01:19:10 -0500229 if (parameters.getFaceId() == INVALID_FACE_ID) {
230 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must be valid"));
Junxiao Shi70911652014-08-12 10:14:24 -0700231 }
232}
233
234FibRemoveNextHopCommand::FibRemoveNextHopCommand()
235 : ControlCommand("fib", "remove-nexthop")
236{
237 m_requestValidator
238 .required(CONTROL_PARAMETER_NAME)
239 .optional(CONTROL_PARAMETER_FACE_ID);
240 m_responseValidator
241 .required(CONTROL_PARAMETER_NAME)
242 .required(CONTROL_PARAMETER_FACE_ID);
243}
244
245void
246FibRemoveNextHopCommand::applyDefaultsToRequest(ControlParameters& parameters) const
247{
248 if (!parameters.hasFaceId()) {
249 parameters.setFaceId(0);
250 }
251}
252
253void
254FibRemoveNextHopCommand::validateResponse(const ControlParameters& parameters) const
255{
256 this->ControlCommand::validateResponse(parameters);
257
Davide Pesaventof8503d22017-02-17 01:19:10 -0500258 if (parameters.getFaceId() == INVALID_FACE_ID) {
259 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must be valid"));
Junxiao Shi70911652014-08-12 10:14:24 -0700260 }
261}
262
Junxiao Shi22f85682018-01-22 19:23:22 +0000263CsConfigCommand::CsConfigCommand()
264 : ControlCommand("cs", "config")
265{
266 m_requestValidator
267 .optional(CONTROL_PARAMETER_CAPACITY)
268 .optional(CONTROL_PARAMETER_FLAGS)
269 .optional(CONTROL_PARAMETER_MASK);
270 m_responseValidator
271 .required(CONTROL_PARAMETER_CAPACITY)
272 .required(CONTROL_PARAMETER_FLAGS);
273}
274
Junxiao Shidf505382018-03-04 13:40:44 +0000275CsEraseCommand::CsEraseCommand()
276 : ControlCommand("cs", "erase")
277{
278 m_requestValidator
279 .required(CONTROL_PARAMETER_NAME)
Davide Pesavento5e2ccca2018-03-06 19:00:15 -0500280 .optional(CONTROL_PARAMETER_COUNT);
Junxiao Shidf505382018-03-04 13:40:44 +0000281 m_responseValidator
282 .required(CONTROL_PARAMETER_NAME)
283 .optional(CONTROL_PARAMETER_CAPACITY)
Davide Pesavento5e2ccca2018-03-06 19:00:15 -0500284 .required(CONTROL_PARAMETER_COUNT);
Junxiao Shidf505382018-03-04 13:40:44 +0000285}
286
287void
288CsEraseCommand::validateRequest(const ControlParameters& parameters) const
289{
290 this->ControlCommand::validateRequest(parameters);
291
Davide Pesavento5e2ccca2018-03-06 19:00:15 -0500292 if (parameters.hasCount() && parameters.getCount() == 0) {
293 BOOST_THROW_EXCEPTION(ArgumentError("Count must be positive"));
Junxiao Shidf505382018-03-04 13:40:44 +0000294 }
295}
296
297void
298CsEraseCommand::validateResponse(const ControlParameters& parameters) const
299{
300 this->ControlCommand::validateResponse(parameters);
301
302 if (parameters.hasCapacity() && parameters.getCapacity() == 0) {
303 BOOST_THROW_EXCEPTION(ArgumentError("Capacity must be positive"));
304 }
305}
306
Junxiao Shi70911652014-08-12 10:14:24 -0700307StrategyChoiceSetCommand::StrategyChoiceSetCommand()
308 : ControlCommand("strategy-choice", "set")
309{
310 m_requestValidator
311 .required(CONTROL_PARAMETER_NAME)
312 .required(CONTROL_PARAMETER_STRATEGY);
313 m_responseValidator = m_requestValidator;
314}
315
316StrategyChoiceUnsetCommand::StrategyChoiceUnsetCommand()
317 : ControlCommand("strategy-choice", "unset")
318{
319 m_requestValidator
320 .required(CONTROL_PARAMETER_NAME);
321 m_responseValidator = m_requestValidator;
322}
323
324void
325StrategyChoiceUnsetCommand::validateRequest(const ControlParameters& parameters) const
326{
327 this->ControlCommand::validateRequest(parameters);
328
329 if (parameters.getName().size() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700330 BOOST_THROW_EXCEPTION(ArgumentError("Name must not be ndn:/"));
Junxiao Shi70911652014-08-12 10:14:24 -0700331 }
332}
333
334void
335StrategyChoiceUnsetCommand::validateResponse(const ControlParameters& parameters) const
336{
337 this->validateRequest(parameters);
338}
339
340RibRegisterCommand::RibRegisterCommand()
341 : ControlCommand("rib", "register")
342{
343 m_requestValidator
344 .required(CONTROL_PARAMETER_NAME)
345 .optional(CONTROL_PARAMETER_FACE_ID)
346 .optional(CONTROL_PARAMETER_ORIGIN)
347 .optional(CONTROL_PARAMETER_COST)
348 .optional(CONTROL_PARAMETER_FLAGS)
349 .optional(CONTROL_PARAMETER_EXPIRATION_PERIOD);
350 m_responseValidator
351 .required(CONTROL_PARAMETER_NAME)
352 .required(CONTROL_PARAMETER_FACE_ID)
353 .required(CONTROL_PARAMETER_ORIGIN)
354 .required(CONTROL_PARAMETER_COST)
355 .required(CONTROL_PARAMETER_FLAGS)
356 .optional(CONTROL_PARAMETER_EXPIRATION_PERIOD);
357}
358
359void
360RibRegisterCommand::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 if (!parameters.hasCost()) {
369 parameters.setCost(0);
370 }
371 if (!parameters.hasFlags()) {
372 parameters.setFlags(ROUTE_FLAG_CHILD_INHERIT);
373 }
374}
375
376void
377RibRegisterCommand::validateResponse(const ControlParameters& parameters) const
378{
379 this->ControlCommand::validateResponse(parameters);
380
Davide Pesaventof8503d22017-02-17 01:19:10 -0500381 if (parameters.getFaceId() == INVALID_FACE_ID) {
382 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must be valid"));
Junxiao Shi70911652014-08-12 10:14:24 -0700383 }
384}
385
386RibUnregisterCommand::RibUnregisterCommand()
387 : ControlCommand("rib", "unregister")
388{
389 m_requestValidator
390 .required(CONTROL_PARAMETER_NAME)
391 .optional(CONTROL_PARAMETER_FACE_ID)
392 .optional(CONTROL_PARAMETER_ORIGIN);
393 m_responseValidator
394 .required(CONTROL_PARAMETER_NAME)
395 .required(CONTROL_PARAMETER_FACE_ID)
396 .required(CONTROL_PARAMETER_ORIGIN);
397}
398
399void
400RibUnregisterCommand::applyDefaultsToRequest(ControlParameters& parameters) const
401{
402 if (!parameters.hasFaceId()) {
403 parameters.setFaceId(0);
404 }
405 if (!parameters.hasOrigin()) {
406 parameters.setOrigin(ROUTE_ORIGIN_APP);
407 }
408}
409
410void
411RibUnregisterCommand::validateResponse(const ControlParameters& parameters) const
412{
413 this->ControlCommand::validateResponse(parameters);
414
Davide Pesaventof8503d22017-02-17 01:19:10 -0500415 if (parameters.getFaceId() == INVALID_FACE_ID) {
416 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must be valid"));
Junxiao Shi70911652014-08-12 10:14:24 -0700417 }
418}
419
420} // namespace nfd
421} // namespace ndn