blob: 472f32913735e6f3df426ccc08bc6decdbe0c4d7 [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 Newberryda916d62016-08-11 23:04:34 -0700108 .optional(CONTROL_PARAMETER_FLAGS)
109 .optional(CONTROL_PARAMETER_MASK);
Junxiao Shi70911652014-08-12 10:14:24 -0700110 m_responseValidator
Yukai Tud93c5fc2015-08-25 11:37:16 +0800111 .required(CONTROL_PARAMETER_FACE_ID)
Junxiao Shi8c2ab2e2017-05-05 20:26:34 +0000112 .required(CONTROL_PARAMETER_URI)
113 .required(CONTROL_PARAMETER_LOCAL_URI)
Eric Newberryda916d62016-08-11 23:04:34 -0700114 .required(CONTROL_PARAMETER_FACE_PERSISTENCY)
Eric Newberry07d05c92018-01-22 16:08:01 -0700115 .required(CONTROL_PARAMETER_BASE_CONGESTION_MARKING_INTERVAL)
116 .required(CONTROL_PARAMETER_DEFAULT_CONGESTION_THRESHOLD)
Junxiao Shi8c2ab2e2017-05-05 20:26:34 +0000117 .required(CONTROL_PARAMETER_FLAGS);
Yukai Tud93c5fc2015-08-25 11:37:16 +0800118}
119
120void
121FaceCreateCommand::applyDefaultsToRequest(ControlParameters& parameters) const
122{
123 if (!parameters.hasFacePersistency()) {
124 parameters.setFacePersistency(FacePersistency::FACE_PERSISTENCY_PERSISTENT);
125 }
Junxiao Shi70911652014-08-12 10:14:24 -0700126}
127
128void
129FaceCreateCommand::validateResponse(const ControlParameters& parameters) const
130{
131 this->ControlCommand::validateResponse(parameters);
132
Davide Pesaventof8503d22017-02-17 01:19:10 -0500133 if (parameters.getFaceId() == INVALID_FACE_ID) {
134 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must be valid"));
Junxiao Shi70911652014-08-12 10:14:24 -0700135 }
136}
137
Yanbiao Licbdacb22016-08-02 16:02:35 +0800138FaceUpdateCommand::FaceUpdateCommand()
139 : ControlCommand("faces", "update")
140{
141 m_requestValidator
Eric Newberryda916d62016-08-11 23:04:34 -0700142 .optional(CONTROL_PARAMETER_FACE_ID)
143 .optional(CONTROL_PARAMETER_FACE_PERSISTENCY)
Eric Newberry07d05c92018-01-22 16:08:01 -0700144 .optional(CONTROL_PARAMETER_BASE_CONGESTION_MARKING_INTERVAL)
145 .optional(CONTROL_PARAMETER_DEFAULT_CONGESTION_THRESHOLD)
Eric Newberryda916d62016-08-11 23:04:34 -0700146 .optional(CONTROL_PARAMETER_FLAGS)
147 .optional(CONTROL_PARAMETER_MASK);
Yanbiao Licbdacb22016-08-02 16:02:35 +0800148 m_responseValidator
Eric Newberry138ef2c2016-08-15 20:29:03 -0700149 .required(CONTROL_PARAMETER_FACE_ID)
150 .required(CONTROL_PARAMETER_FACE_PERSISTENCY)
Eric Newberry07d05c92018-01-22 16:08:01 -0700151 .required(CONTROL_PARAMETER_BASE_CONGESTION_MARKING_INTERVAL)
152 .required(CONTROL_PARAMETER_DEFAULT_CONGESTION_THRESHOLD)
Eric Newberry138ef2c2016-08-15 20:29:03 -0700153 .required(CONTROL_PARAMETER_FLAGS);
Eric Newberryda916d62016-08-11 23:04:34 -0700154}
155
156void
157FaceUpdateCommand::applyDefaultsToRequest(ControlParameters& parameters) const
158{
Eric Newberry138ef2c2016-08-15 20:29:03 -0700159 if (!parameters.hasFaceId()) {
160 parameters.setFaceId(0);
161 }
Yanbiao Licbdacb22016-08-02 16:02:35 +0800162}
163
164void
Yanbiao Licbdacb22016-08-02 16:02:35 +0800165FaceUpdateCommand::validateResponse(const ControlParameters& parameters) const
166{
Eric Newberry138ef2c2016-08-15 20:29:03 -0700167 this->ControlCommand::validateResponse(parameters);
168
Davide Pesaventof8503d22017-02-17 01:19:10 -0500169 if (parameters.getFaceId() == INVALID_FACE_ID) {
170 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must be valid"));
Eric Newberry138ef2c2016-08-15 20:29:03 -0700171 }
Yanbiao Licbdacb22016-08-02 16:02:35 +0800172}
173
Junxiao Shi70911652014-08-12 10:14:24 -0700174FaceDestroyCommand::FaceDestroyCommand()
175 : ControlCommand("faces", "destroy")
176{
177 m_requestValidator
178 .required(CONTROL_PARAMETER_FACE_ID);
179 m_responseValidator = m_requestValidator;
180}
181
182void
183FaceDestroyCommand::validateRequest(const ControlParameters& parameters) const
184{
185 this->ControlCommand::validateRequest(parameters);
186
Davide Pesaventof8503d22017-02-17 01:19:10 -0500187 if (parameters.getFaceId() == INVALID_FACE_ID) {
188 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must be valid"));
Junxiao Shi70911652014-08-12 10:14:24 -0700189 }
190}
191
192void
193FaceDestroyCommand::validateResponse(const ControlParameters& parameters) const
194{
195 this->validateRequest(parameters);
196}
197
Junxiao Shi70911652014-08-12 10:14:24 -0700198FibAddNextHopCommand::FibAddNextHopCommand()
199 : ControlCommand("fib", "add-nexthop")
200{
201 m_requestValidator
202 .required(CONTROL_PARAMETER_NAME)
203 .optional(CONTROL_PARAMETER_FACE_ID)
204 .optional(CONTROL_PARAMETER_COST);
205 m_responseValidator
206 .required(CONTROL_PARAMETER_NAME)
207 .required(CONTROL_PARAMETER_FACE_ID)
208 .required(CONTROL_PARAMETER_COST);
209}
210
211void
212FibAddNextHopCommand::applyDefaultsToRequest(ControlParameters& parameters) const
213{
214 if (!parameters.hasFaceId()) {
215 parameters.setFaceId(0);
216 }
217 if (!parameters.hasCost()) {
218 parameters.setCost(0);
219 }
220}
221
222void
223FibAddNextHopCommand::validateResponse(const ControlParameters& parameters) const
224{
225 this->ControlCommand::validateResponse(parameters);
226
Davide Pesaventof8503d22017-02-17 01:19:10 -0500227 if (parameters.getFaceId() == INVALID_FACE_ID) {
228 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must be valid"));
Junxiao Shi70911652014-08-12 10:14:24 -0700229 }
230}
231
232FibRemoveNextHopCommand::FibRemoveNextHopCommand()
233 : ControlCommand("fib", "remove-nexthop")
234{
235 m_requestValidator
236 .required(CONTROL_PARAMETER_NAME)
237 .optional(CONTROL_PARAMETER_FACE_ID);
238 m_responseValidator
239 .required(CONTROL_PARAMETER_NAME)
240 .required(CONTROL_PARAMETER_FACE_ID);
241}
242
243void
244FibRemoveNextHopCommand::applyDefaultsToRequest(ControlParameters& parameters) const
245{
246 if (!parameters.hasFaceId()) {
247 parameters.setFaceId(0);
248 }
249}
250
251void
252FibRemoveNextHopCommand::validateResponse(const ControlParameters& parameters) const
253{
254 this->ControlCommand::validateResponse(parameters);
255
Davide Pesaventof8503d22017-02-17 01:19:10 -0500256 if (parameters.getFaceId() == INVALID_FACE_ID) {
257 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must be valid"));
Junxiao Shi70911652014-08-12 10:14:24 -0700258 }
259}
260
Junxiao Shi22f85682018-01-22 19:23:22 +0000261CsConfigCommand::CsConfigCommand()
262 : ControlCommand("cs", "config")
263{
264 m_requestValidator
265 .optional(CONTROL_PARAMETER_CAPACITY)
266 .optional(CONTROL_PARAMETER_FLAGS)
267 .optional(CONTROL_PARAMETER_MASK);
268 m_responseValidator
269 .required(CONTROL_PARAMETER_CAPACITY)
270 .required(CONTROL_PARAMETER_FLAGS);
271}
272
Junxiao Shi70911652014-08-12 10:14:24 -0700273StrategyChoiceSetCommand::StrategyChoiceSetCommand()
274 : ControlCommand("strategy-choice", "set")
275{
276 m_requestValidator
277 .required(CONTROL_PARAMETER_NAME)
278 .required(CONTROL_PARAMETER_STRATEGY);
279 m_responseValidator = m_requestValidator;
280}
281
282StrategyChoiceUnsetCommand::StrategyChoiceUnsetCommand()
283 : ControlCommand("strategy-choice", "unset")
284{
285 m_requestValidator
286 .required(CONTROL_PARAMETER_NAME);
287 m_responseValidator = m_requestValidator;
288}
289
290void
291StrategyChoiceUnsetCommand::validateRequest(const ControlParameters& parameters) const
292{
293 this->ControlCommand::validateRequest(parameters);
294
295 if (parameters.getName().size() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700296 BOOST_THROW_EXCEPTION(ArgumentError("Name must not be ndn:/"));
Junxiao Shi70911652014-08-12 10:14:24 -0700297 }
298}
299
300void
301StrategyChoiceUnsetCommand::validateResponse(const ControlParameters& parameters) const
302{
303 this->validateRequest(parameters);
304}
305
306RibRegisterCommand::RibRegisterCommand()
307 : ControlCommand("rib", "register")
308{
309 m_requestValidator
310 .required(CONTROL_PARAMETER_NAME)
311 .optional(CONTROL_PARAMETER_FACE_ID)
312 .optional(CONTROL_PARAMETER_ORIGIN)
313 .optional(CONTROL_PARAMETER_COST)
314 .optional(CONTROL_PARAMETER_FLAGS)
315 .optional(CONTROL_PARAMETER_EXPIRATION_PERIOD);
316 m_responseValidator
317 .required(CONTROL_PARAMETER_NAME)
318 .required(CONTROL_PARAMETER_FACE_ID)
319 .required(CONTROL_PARAMETER_ORIGIN)
320 .required(CONTROL_PARAMETER_COST)
321 .required(CONTROL_PARAMETER_FLAGS)
322 .optional(CONTROL_PARAMETER_EXPIRATION_PERIOD);
323}
324
325void
326RibRegisterCommand::applyDefaultsToRequest(ControlParameters& parameters) const
327{
328 if (!parameters.hasFaceId()) {
329 parameters.setFaceId(0);
330 }
331 if (!parameters.hasOrigin()) {
332 parameters.setOrigin(ROUTE_ORIGIN_APP);
333 }
334 if (!parameters.hasCost()) {
335 parameters.setCost(0);
336 }
337 if (!parameters.hasFlags()) {
338 parameters.setFlags(ROUTE_FLAG_CHILD_INHERIT);
339 }
340}
341
342void
343RibRegisterCommand::validateResponse(const ControlParameters& parameters) const
344{
345 this->ControlCommand::validateResponse(parameters);
346
Davide Pesaventof8503d22017-02-17 01:19:10 -0500347 if (parameters.getFaceId() == INVALID_FACE_ID) {
348 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must be valid"));
Junxiao Shi70911652014-08-12 10:14:24 -0700349 }
350}
351
352RibUnregisterCommand::RibUnregisterCommand()
353 : ControlCommand("rib", "unregister")
354{
355 m_requestValidator
356 .required(CONTROL_PARAMETER_NAME)
357 .optional(CONTROL_PARAMETER_FACE_ID)
358 .optional(CONTROL_PARAMETER_ORIGIN);
359 m_responseValidator
360 .required(CONTROL_PARAMETER_NAME)
361 .required(CONTROL_PARAMETER_FACE_ID)
362 .required(CONTROL_PARAMETER_ORIGIN);
363}
364
365void
366RibUnregisterCommand::applyDefaultsToRequest(ControlParameters& parameters) const
367{
368 if (!parameters.hasFaceId()) {
369 parameters.setFaceId(0);
370 }
371 if (!parameters.hasOrigin()) {
372 parameters.setOrigin(ROUTE_ORIGIN_APP);
373 }
374}
375
376void
377RibUnregisterCommand::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
386} // namespace nfd
387} // namespace ndn