blob: 9853f9d9a4c85bafd2d5afe71d262215e2f29e07 [file] [log] [blame]
Junxiao Shi70911652014-08-12 10:14:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yanbiao Licbdacb22016-08-02 16:02:35 +08003 * Copyright (c) 2013-2016 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
22#include "nfd-control-command.hpp"
Junxiao Shi5de006b2014-10-26 20:20:52 -070023#include "nfd-command-options.hpp" // only used in deprecated functions
Junxiao Shi70911652014-08-12 10:14:24 -070024
25namespace ndn {
26namespace nfd {
27
28ControlCommand::ControlCommand(const std::string& module, const std::string& verb)
Junxiao Shi5de006b2014-10-26 20:20:52 -070029 : m_module(module)
30 , m_verb(verb)
Junxiao Shi70911652014-08-12 10:14:24 -070031{
Junxiao Shi70911652014-08-12 10:14:24 -070032}
33
Davide Pesaventoaeeb3fc2016-08-14 03:40:02 +020034ControlCommand::~ControlCommand() = default;
35
Junxiao Shi70911652014-08-12 10:14:24 -070036void
37ControlCommand::validateRequest(const ControlParameters& parameters) const
38{
39 m_requestValidator.validate(parameters);
40}
41
42void
43ControlCommand::applyDefaultsToRequest(ControlParameters& parameters) const
44{
45}
46
47void
48ControlCommand::validateResponse(const ControlParameters& parameters) const
49{
50 m_responseValidator.validate(parameters);
51}
52
53void
54ControlCommand::applyDefaultsToResponse(ControlParameters& parameters) const
55{
56}
57
58Name
Junxiao Shi5de006b2014-10-26 20:20:52 -070059ControlCommand::getRequestName(const Name& commandPrefix,
60 const ControlParameters& parameters) const
Junxiao Shi70911652014-08-12 10:14:24 -070061{
62 this->validateRequest(parameters);
63
Junxiao Shi5de006b2014-10-26 20:20:52 -070064 Name name = commandPrefix;
65 name.append(m_module).append(m_verb);
Junxiao Shi70911652014-08-12 10:14:24 -070066 name.append(parameters.wireEncode());
67 return name;
68}
69
Junxiao Shi70911652014-08-12 10:14:24 -070070ControlCommand::FieldValidator::FieldValidator()
71 : m_required(CONTROL_PARAMETER_UBOUND)
72 , m_optional(CONTROL_PARAMETER_UBOUND)
73{
74}
75
76void
77ControlCommand::FieldValidator::validate(const ControlParameters& parameters) const
78{
79 const std::vector<bool>& presentFields = parameters.getPresentFields();
80
81 for (size_t i = 0; i < CONTROL_PARAMETER_UBOUND; ++i) {
82 bool isPresent = presentFields[i];
83 if (m_required[i]) {
84 if (!isPresent) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070085 BOOST_THROW_EXCEPTION(ArgumentError(CONTROL_PARAMETER_FIELD[i] + " is required but "
86 "missing"));
Junxiao Shi70911652014-08-12 10:14:24 -070087 }
88 }
89 else if (isPresent && !m_optional[i]) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070090 BOOST_THROW_EXCEPTION(ArgumentError(CONTROL_PARAMETER_FIELD[i] + " is forbidden but "
91 "present"));
Junxiao Shi70911652014-08-12 10:14:24 -070092 }
93 }
94}
95
96FaceCreateCommand::FaceCreateCommand()
97 : ControlCommand("faces", "create")
98{
99 m_requestValidator
Yukai Tud93c5fc2015-08-25 11:37:16 +0800100 .required(CONTROL_PARAMETER_URI)
Eric Newberryda916d62016-08-11 23:04:34 -0700101 .optional(CONTROL_PARAMETER_FACE_PERSISTENCY)
102 .optional(CONTROL_PARAMETER_FLAGS)
103 .optional(CONTROL_PARAMETER_MASK);
Junxiao Shi70911652014-08-12 10:14:24 -0700104 m_responseValidator
Yukai Tud93c5fc2015-08-25 11:37:16 +0800105 .required(CONTROL_PARAMETER_FACE_ID)
Eric Newberryda916d62016-08-11 23:04:34 -0700106 .required(CONTROL_PARAMETER_FACE_PERSISTENCY)
107 .optional(CONTROL_PARAMETER_FLAGS)
108 .optional(CONTROL_PARAMETER_URI);
Yukai Tud93c5fc2015-08-25 11:37:16 +0800109}
110
111void
112FaceCreateCommand::applyDefaultsToRequest(ControlParameters& parameters) const
113{
Eric Newberryda916d62016-08-11 23:04:34 -0700114 parameters.setFaceId(0);
115
Yukai Tud93c5fc2015-08-25 11:37:16 +0800116 if (!parameters.hasFacePersistency()) {
117 parameters.setFacePersistency(FacePersistency::FACE_PERSISTENCY_PERSISTENT);
118 }
Junxiao Shi70911652014-08-12 10:14:24 -0700119}
120
121void
Eric Newberryda916d62016-08-11 23:04:34 -0700122FaceCreateCommand::validateRequest(const ControlParameters& parameters) const
123{
124 this->ControlCommand::validateRequest(parameters);
125
126 if (parameters.hasFlags() != parameters.hasMask()) {
127 BOOST_THROW_EXCEPTION(ArgumentError("Flags must be accompanied by Mask"));
128 }
129}
130
131void
Junxiao Shi70911652014-08-12 10:14:24 -0700132FaceCreateCommand::validateResponse(const ControlParameters& parameters) const
133{
134 this->ControlCommand::validateResponse(parameters);
135
136 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700137 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700138 }
139}
140
Yanbiao Licbdacb22016-08-02 16:02:35 +0800141FaceUpdateCommand::FaceUpdateCommand()
142 : ControlCommand("faces", "update")
143{
144 m_requestValidator
Eric Newberryda916d62016-08-11 23:04:34 -0700145 .optional(CONTROL_PARAMETER_FACE_ID)
146 .optional(CONTROL_PARAMETER_FACE_PERSISTENCY)
147 .optional(CONTROL_PARAMETER_FLAGS)
148 .optional(CONTROL_PARAMETER_MASK);
Yanbiao Licbdacb22016-08-02 16:02:35 +0800149 m_responseValidator
Eric Newberryda916d62016-08-11 23:04:34 -0700150 .optional(CONTROL_PARAMETER_FACE_ID)
151 .optional(CONTROL_PARAMETER_FACE_PERSISTENCY)
152 .optional(CONTROL_PARAMETER_FLAGS)
153 .optional(CONTROL_PARAMETER_MASK);
154}
155
156void
157FaceUpdateCommand::applyDefaultsToRequest(ControlParameters& parameters) const
158{
159 parameters.setFaceId(0);
Yanbiao Licbdacb22016-08-02 16:02:35 +0800160}
161
162void
163FaceUpdateCommand::validateRequest(const ControlParameters& parameters) const
164{
165 this->ControlCommand::validateRequest(parameters);
166
Eric Newberryda916d62016-08-11 23:04:34 -0700167 if (parameters.hasFlags() != parameters.hasMask()) {
168 BOOST_THROW_EXCEPTION(ArgumentError("Flags must be accompanied by Mask"));
Yanbiao Licbdacb22016-08-02 16:02:35 +0800169 }
170}
171
172void
173FaceUpdateCommand::validateResponse(const ControlParameters& parameters) const
174{
175 this->validateRequest(parameters);
176}
177
Junxiao Shi70911652014-08-12 10:14:24 -0700178FaceDestroyCommand::FaceDestroyCommand()
179 : ControlCommand("faces", "destroy")
180{
181 m_requestValidator
182 .required(CONTROL_PARAMETER_FACE_ID);
183 m_responseValidator = m_requestValidator;
184}
185
186void
187FaceDestroyCommand::validateRequest(const ControlParameters& parameters) const
188{
189 this->ControlCommand::validateRequest(parameters);
190
191 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700192 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700193 }
194}
195
196void
197FaceDestroyCommand::validateResponse(const ControlParameters& parameters) const
198{
199 this->validateRequest(parameters);
200}
201
202FaceLocalControlCommand::FaceLocalControlCommand(const std::string& verb)
203 : ControlCommand("faces", verb)
204{
205 m_requestValidator
206 .required(CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE);
207 m_responseValidator = m_requestValidator;
208}
209
210void
211FaceLocalControlCommand::validateRequest(const ControlParameters& parameters) const
212{
213 this->ControlCommand::validateRequest(parameters);
214
215 switch (parameters.getLocalControlFeature()) {
216 case LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID:
217 case LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID:
218 break;
219 default:
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700220 BOOST_THROW_EXCEPTION(ArgumentError("LocalControlFeature is invalid"));
Junxiao Shi70911652014-08-12 10:14:24 -0700221 }
222}
223
224void
225FaceLocalControlCommand::validateResponse(const ControlParameters& parameters) const
226{
227 this->validateRequest(parameters);
228}
229
230FaceEnableLocalControlCommand::FaceEnableLocalControlCommand()
231 : FaceLocalControlCommand("enable-local-control")
232{
233}
234
235FaceDisableLocalControlCommand::FaceDisableLocalControlCommand()
236 : FaceLocalControlCommand("disable-local-control")
237{
238}
239
240FibAddNextHopCommand::FibAddNextHopCommand()
241 : ControlCommand("fib", "add-nexthop")
242{
243 m_requestValidator
244 .required(CONTROL_PARAMETER_NAME)
245 .optional(CONTROL_PARAMETER_FACE_ID)
246 .optional(CONTROL_PARAMETER_COST);
247 m_responseValidator
248 .required(CONTROL_PARAMETER_NAME)
249 .required(CONTROL_PARAMETER_FACE_ID)
250 .required(CONTROL_PARAMETER_COST);
251}
252
253void
254FibAddNextHopCommand::applyDefaultsToRequest(ControlParameters& parameters) const
255{
256 if (!parameters.hasFaceId()) {
257 parameters.setFaceId(0);
258 }
259 if (!parameters.hasCost()) {
260 parameters.setCost(0);
261 }
262}
263
264void
265FibAddNextHopCommand::validateResponse(const ControlParameters& parameters) const
266{
267 this->ControlCommand::validateResponse(parameters);
268
269 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700270 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700271 }
272}
273
274FibRemoveNextHopCommand::FibRemoveNextHopCommand()
275 : ControlCommand("fib", "remove-nexthop")
276{
277 m_requestValidator
278 .required(CONTROL_PARAMETER_NAME)
279 .optional(CONTROL_PARAMETER_FACE_ID);
280 m_responseValidator
281 .required(CONTROL_PARAMETER_NAME)
282 .required(CONTROL_PARAMETER_FACE_ID);
283}
284
285void
286FibRemoveNextHopCommand::applyDefaultsToRequest(ControlParameters& parameters) const
287{
288 if (!parameters.hasFaceId()) {
289 parameters.setFaceId(0);
290 }
291}
292
293void
294FibRemoveNextHopCommand::validateResponse(const ControlParameters& parameters) const
295{
296 this->ControlCommand::validateResponse(parameters);
297
298 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700299 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700300 }
301}
302
303StrategyChoiceSetCommand::StrategyChoiceSetCommand()
304 : ControlCommand("strategy-choice", "set")
305{
306 m_requestValidator
307 .required(CONTROL_PARAMETER_NAME)
308 .required(CONTROL_PARAMETER_STRATEGY);
309 m_responseValidator = m_requestValidator;
310}
311
312StrategyChoiceUnsetCommand::StrategyChoiceUnsetCommand()
313 : ControlCommand("strategy-choice", "unset")
314{
315 m_requestValidator
316 .required(CONTROL_PARAMETER_NAME);
317 m_responseValidator = m_requestValidator;
318}
319
320void
321StrategyChoiceUnsetCommand::validateRequest(const ControlParameters& parameters) const
322{
323 this->ControlCommand::validateRequest(parameters);
324
325 if (parameters.getName().size() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700326 BOOST_THROW_EXCEPTION(ArgumentError("Name must not be ndn:/"));
Junxiao Shi70911652014-08-12 10:14:24 -0700327 }
328}
329
330void
331StrategyChoiceUnsetCommand::validateResponse(const ControlParameters& parameters) const
332{
333 this->validateRequest(parameters);
334}
335
336RibRegisterCommand::RibRegisterCommand()
337 : ControlCommand("rib", "register")
338{
339 m_requestValidator
340 .required(CONTROL_PARAMETER_NAME)
341 .optional(CONTROL_PARAMETER_FACE_ID)
342 .optional(CONTROL_PARAMETER_ORIGIN)
343 .optional(CONTROL_PARAMETER_COST)
344 .optional(CONTROL_PARAMETER_FLAGS)
345 .optional(CONTROL_PARAMETER_EXPIRATION_PERIOD);
346 m_responseValidator
347 .required(CONTROL_PARAMETER_NAME)
348 .required(CONTROL_PARAMETER_FACE_ID)
349 .required(CONTROL_PARAMETER_ORIGIN)
350 .required(CONTROL_PARAMETER_COST)
351 .required(CONTROL_PARAMETER_FLAGS)
352 .optional(CONTROL_PARAMETER_EXPIRATION_PERIOD);
353}
354
355void
356RibRegisterCommand::applyDefaultsToRequest(ControlParameters& parameters) const
357{
358 if (!parameters.hasFaceId()) {
359 parameters.setFaceId(0);
360 }
361 if (!parameters.hasOrigin()) {
362 parameters.setOrigin(ROUTE_ORIGIN_APP);
363 }
364 if (!parameters.hasCost()) {
365 parameters.setCost(0);
366 }
367 if (!parameters.hasFlags()) {
368 parameters.setFlags(ROUTE_FLAG_CHILD_INHERIT);
369 }
370}
371
372void
373RibRegisterCommand::validateResponse(const ControlParameters& parameters) const
374{
375 this->ControlCommand::validateResponse(parameters);
376
377 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700378 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700379 }
380}
381
382RibUnregisterCommand::RibUnregisterCommand()
383 : ControlCommand("rib", "unregister")
384{
385 m_requestValidator
386 .required(CONTROL_PARAMETER_NAME)
387 .optional(CONTROL_PARAMETER_FACE_ID)
388 .optional(CONTROL_PARAMETER_ORIGIN);
389 m_responseValidator
390 .required(CONTROL_PARAMETER_NAME)
391 .required(CONTROL_PARAMETER_FACE_ID)
392 .required(CONTROL_PARAMETER_ORIGIN);
393}
394
395void
396RibUnregisterCommand::applyDefaultsToRequest(ControlParameters& parameters) const
397{
398 if (!parameters.hasFaceId()) {
399 parameters.setFaceId(0);
400 }
401 if (!parameters.hasOrigin()) {
402 parameters.setOrigin(ROUTE_ORIGIN_APP);
403 }
404}
405
406void
407RibUnregisterCommand::validateResponse(const ControlParameters& parameters) const
408{
409 this->ControlCommand::validateResponse(parameters);
410
411 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700412 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700413 }
414}
415
416} // namespace nfd
417} // namespace ndn