blob: 88db4f95a04339e92acac700e9235fca0ff8fe1b [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)
101 .optional(CONTROL_PARAMETER_FACE_PERSISTENCY);
Junxiao Shi70911652014-08-12 10:14:24 -0700102 m_responseValidator
103 .required(CONTROL_PARAMETER_URI)
Yukai Tud93c5fc2015-08-25 11:37:16 +0800104 .required(CONTROL_PARAMETER_FACE_ID)
105 .required(CONTROL_PARAMETER_FACE_PERSISTENCY);
106}
107
108void
109FaceCreateCommand::applyDefaultsToRequest(ControlParameters& parameters) const
110{
111 if (!parameters.hasFacePersistency()) {
112 parameters.setFacePersistency(FacePersistency::FACE_PERSISTENCY_PERSISTENT);
113 }
Junxiao Shi70911652014-08-12 10:14:24 -0700114}
115
116void
117FaceCreateCommand::validateResponse(const ControlParameters& parameters) const
118{
119 this->ControlCommand::validateResponse(parameters);
120
121 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700122 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700123 }
124}
125
Yanbiao Licbdacb22016-08-02 16:02:35 +0800126FaceUpdateCommand::FaceUpdateCommand()
127 : ControlCommand("faces", "update")
128{
129 m_requestValidator
130 .required(CONTROL_PARAMETER_FACE_ID)
131 .optional(CONTROL_PARAMETER_FACE_PERSISTENCY);
132 m_responseValidator
133 .required(CONTROL_PARAMETER_FACE_ID)
134 .optional(CONTROL_PARAMETER_FACE_PERSISTENCY);
135 m_responseValidator = m_requestValidator;
136}
137
138void
139FaceUpdateCommand::validateRequest(const ControlParameters& parameters) const
140{
141 this->ControlCommand::validateRequest(parameters);
142
143 if (parameters.getFaceId() == 0) {
144 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
145 }
146}
147
148void
149FaceUpdateCommand::validateResponse(const ControlParameters& parameters) const
150{
151 this->validateRequest(parameters);
152}
153
Junxiao Shi70911652014-08-12 10:14:24 -0700154FaceDestroyCommand::FaceDestroyCommand()
155 : ControlCommand("faces", "destroy")
156{
157 m_requestValidator
158 .required(CONTROL_PARAMETER_FACE_ID);
159 m_responseValidator = m_requestValidator;
160}
161
162void
163FaceDestroyCommand::validateRequest(const ControlParameters& parameters) const
164{
165 this->ControlCommand::validateRequest(parameters);
166
167 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700168 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700169 }
170}
171
172void
173FaceDestroyCommand::validateResponse(const ControlParameters& parameters) const
174{
175 this->validateRequest(parameters);
176}
177
178FaceLocalControlCommand::FaceLocalControlCommand(const std::string& verb)
179 : ControlCommand("faces", verb)
180{
181 m_requestValidator
182 .required(CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE);
183 m_responseValidator = m_requestValidator;
184}
185
186void
187FaceLocalControlCommand::validateRequest(const ControlParameters& parameters) const
188{
189 this->ControlCommand::validateRequest(parameters);
190
191 switch (parameters.getLocalControlFeature()) {
192 case LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID:
193 case LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID:
194 break;
195 default:
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700196 BOOST_THROW_EXCEPTION(ArgumentError("LocalControlFeature is invalid"));
Junxiao Shi70911652014-08-12 10:14:24 -0700197 }
198}
199
200void
201FaceLocalControlCommand::validateResponse(const ControlParameters& parameters) const
202{
203 this->validateRequest(parameters);
204}
205
206FaceEnableLocalControlCommand::FaceEnableLocalControlCommand()
207 : FaceLocalControlCommand("enable-local-control")
208{
209}
210
211FaceDisableLocalControlCommand::FaceDisableLocalControlCommand()
212 : FaceLocalControlCommand("disable-local-control")
213{
214}
215
216FibAddNextHopCommand::FibAddNextHopCommand()
217 : ControlCommand("fib", "add-nexthop")
218{
219 m_requestValidator
220 .required(CONTROL_PARAMETER_NAME)
221 .optional(CONTROL_PARAMETER_FACE_ID)
222 .optional(CONTROL_PARAMETER_COST);
223 m_responseValidator
224 .required(CONTROL_PARAMETER_NAME)
225 .required(CONTROL_PARAMETER_FACE_ID)
226 .required(CONTROL_PARAMETER_COST);
227}
228
229void
230FibAddNextHopCommand::applyDefaultsToRequest(ControlParameters& parameters) const
231{
232 if (!parameters.hasFaceId()) {
233 parameters.setFaceId(0);
234 }
235 if (!parameters.hasCost()) {
236 parameters.setCost(0);
237 }
238}
239
240void
241FibAddNextHopCommand::validateResponse(const ControlParameters& parameters) const
242{
243 this->ControlCommand::validateResponse(parameters);
244
245 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700246 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700247 }
248}
249
250FibRemoveNextHopCommand::FibRemoveNextHopCommand()
251 : ControlCommand("fib", "remove-nexthop")
252{
253 m_requestValidator
254 .required(CONTROL_PARAMETER_NAME)
255 .optional(CONTROL_PARAMETER_FACE_ID);
256 m_responseValidator
257 .required(CONTROL_PARAMETER_NAME)
258 .required(CONTROL_PARAMETER_FACE_ID);
259}
260
261void
262FibRemoveNextHopCommand::applyDefaultsToRequest(ControlParameters& parameters) const
263{
264 if (!parameters.hasFaceId()) {
265 parameters.setFaceId(0);
266 }
267}
268
269void
270FibRemoveNextHopCommand::validateResponse(const ControlParameters& parameters) const
271{
272 this->ControlCommand::validateResponse(parameters);
273
274 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700275 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700276 }
277}
278
279StrategyChoiceSetCommand::StrategyChoiceSetCommand()
280 : ControlCommand("strategy-choice", "set")
281{
282 m_requestValidator
283 .required(CONTROL_PARAMETER_NAME)
284 .required(CONTROL_PARAMETER_STRATEGY);
285 m_responseValidator = m_requestValidator;
286}
287
288StrategyChoiceUnsetCommand::StrategyChoiceUnsetCommand()
289 : ControlCommand("strategy-choice", "unset")
290{
291 m_requestValidator
292 .required(CONTROL_PARAMETER_NAME);
293 m_responseValidator = m_requestValidator;
294}
295
296void
297StrategyChoiceUnsetCommand::validateRequest(const ControlParameters& parameters) const
298{
299 this->ControlCommand::validateRequest(parameters);
300
301 if (parameters.getName().size() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700302 BOOST_THROW_EXCEPTION(ArgumentError("Name must not be ndn:/"));
Junxiao Shi70911652014-08-12 10:14:24 -0700303 }
304}
305
306void
307StrategyChoiceUnsetCommand::validateResponse(const ControlParameters& parameters) const
308{
309 this->validateRequest(parameters);
310}
311
312RibRegisterCommand::RibRegisterCommand()
313 : ControlCommand("rib", "register")
314{
315 m_requestValidator
316 .required(CONTROL_PARAMETER_NAME)
317 .optional(CONTROL_PARAMETER_FACE_ID)
318 .optional(CONTROL_PARAMETER_ORIGIN)
319 .optional(CONTROL_PARAMETER_COST)
320 .optional(CONTROL_PARAMETER_FLAGS)
321 .optional(CONTROL_PARAMETER_EXPIRATION_PERIOD);
322 m_responseValidator
323 .required(CONTROL_PARAMETER_NAME)
324 .required(CONTROL_PARAMETER_FACE_ID)
325 .required(CONTROL_PARAMETER_ORIGIN)
326 .required(CONTROL_PARAMETER_COST)
327 .required(CONTROL_PARAMETER_FLAGS)
328 .optional(CONTROL_PARAMETER_EXPIRATION_PERIOD);
329}
330
331void
332RibRegisterCommand::applyDefaultsToRequest(ControlParameters& parameters) const
333{
334 if (!parameters.hasFaceId()) {
335 parameters.setFaceId(0);
336 }
337 if (!parameters.hasOrigin()) {
338 parameters.setOrigin(ROUTE_ORIGIN_APP);
339 }
340 if (!parameters.hasCost()) {
341 parameters.setCost(0);
342 }
343 if (!parameters.hasFlags()) {
344 parameters.setFlags(ROUTE_FLAG_CHILD_INHERIT);
345 }
346}
347
348void
349RibRegisterCommand::validateResponse(const ControlParameters& parameters) const
350{
351 this->ControlCommand::validateResponse(parameters);
352
353 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700354 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700355 }
356}
357
358RibUnregisterCommand::RibUnregisterCommand()
359 : ControlCommand("rib", "unregister")
360{
361 m_requestValidator
362 .required(CONTROL_PARAMETER_NAME)
363 .optional(CONTROL_PARAMETER_FACE_ID)
364 .optional(CONTROL_PARAMETER_ORIGIN);
365 m_responseValidator
366 .required(CONTROL_PARAMETER_NAME)
367 .required(CONTROL_PARAMETER_FACE_ID)
368 .required(CONTROL_PARAMETER_ORIGIN);
369}
370
371void
372RibUnregisterCommand::applyDefaultsToRequest(ControlParameters& parameters) const
373{
374 if (!parameters.hasFaceId()) {
375 parameters.setFaceId(0);
376 }
377 if (!parameters.hasOrigin()) {
378 parameters.setOrigin(ROUTE_ORIGIN_APP);
379 }
380}
381
382void
383RibUnregisterCommand::validateResponse(const ControlParameters& parameters) const
384{
385 this->ControlCommand::validateResponse(parameters);
386
387 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700388 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700389 }
390}
391
392} // namespace nfd
393} // namespace ndn