blob: 307810dccb49aef8d9073f8f9bf40c91d5af8567 [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
34void
35ControlCommand::validateRequest(const ControlParameters& parameters) const
36{
37 m_requestValidator.validate(parameters);
38}
39
40void
41ControlCommand::applyDefaultsToRequest(ControlParameters& parameters) const
42{
43}
44
45void
46ControlCommand::validateResponse(const ControlParameters& parameters) const
47{
48 m_responseValidator.validate(parameters);
49}
50
51void
52ControlCommand::applyDefaultsToResponse(ControlParameters& parameters) const
53{
54}
55
56Name
Junxiao Shi5de006b2014-10-26 20:20:52 -070057ControlCommand::getRequestName(const Name& commandPrefix,
58 const ControlParameters& parameters) const
Junxiao Shi70911652014-08-12 10:14:24 -070059{
60 this->validateRequest(parameters);
61
Junxiao Shi5de006b2014-10-26 20:20:52 -070062 Name name = commandPrefix;
63 name.append(m_module).append(m_verb);
Junxiao Shi70911652014-08-12 10:14:24 -070064 name.append(parameters.wireEncode());
65 return name;
66}
67
Junxiao Shi70911652014-08-12 10:14:24 -070068ControlCommand::FieldValidator::FieldValidator()
69 : m_required(CONTROL_PARAMETER_UBOUND)
70 , m_optional(CONTROL_PARAMETER_UBOUND)
71{
72}
73
74void
75ControlCommand::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) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070083 BOOST_THROW_EXCEPTION(ArgumentError(CONTROL_PARAMETER_FIELD[i] + " is required but "
84 "missing"));
Junxiao Shi70911652014-08-12 10:14:24 -070085 }
86 }
87 else if (isPresent && !m_optional[i]) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070088 BOOST_THROW_EXCEPTION(ArgumentError(CONTROL_PARAMETER_FIELD[i] + " is forbidden but "
89 "present"));
Junxiao Shi70911652014-08-12 10:14:24 -070090 }
91 }
92}
93
94FaceCreateCommand::FaceCreateCommand()
95 : ControlCommand("faces", "create")
96{
97 m_requestValidator
Yukai Tud93c5fc2015-08-25 11:37:16 +080098 .required(CONTROL_PARAMETER_URI)
99 .optional(CONTROL_PARAMETER_FACE_PERSISTENCY);
Junxiao Shi70911652014-08-12 10:14:24 -0700100 m_responseValidator
101 .required(CONTROL_PARAMETER_URI)
Yukai Tud93c5fc2015-08-25 11:37:16 +0800102 .required(CONTROL_PARAMETER_FACE_ID)
103 .required(CONTROL_PARAMETER_FACE_PERSISTENCY);
104}
105
106void
107FaceCreateCommand::applyDefaultsToRequest(ControlParameters& parameters) const
108{
109 if (!parameters.hasFacePersistency()) {
110 parameters.setFacePersistency(FacePersistency::FACE_PERSISTENCY_PERSISTENT);
111 }
Junxiao Shi70911652014-08-12 10:14:24 -0700112}
113
114void
115FaceCreateCommand::validateResponse(const ControlParameters& parameters) const
116{
117 this->ControlCommand::validateResponse(parameters);
118
119 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700120 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700121 }
122}
123
Yanbiao Licbdacb22016-08-02 16:02:35 +0800124FaceUpdateCommand::FaceUpdateCommand()
125 : ControlCommand("faces", "update")
126{
127 m_requestValidator
128 .required(CONTROL_PARAMETER_FACE_ID)
129 .optional(CONTROL_PARAMETER_FACE_PERSISTENCY);
130 m_responseValidator
131 .required(CONTROL_PARAMETER_FACE_ID)
132 .optional(CONTROL_PARAMETER_FACE_PERSISTENCY);
133 m_responseValidator = m_requestValidator;
134}
135
136void
137FaceUpdateCommand::validateRequest(const ControlParameters& parameters) const
138{
139 this->ControlCommand::validateRequest(parameters);
140
141 if (parameters.getFaceId() == 0) {
142 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
143 }
144}
145
146void
147FaceUpdateCommand::validateResponse(const ControlParameters& parameters) const
148{
149 this->validateRequest(parameters);
150}
151
Junxiao Shi70911652014-08-12 10:14:24 -0700152FaceDestroyCommand::FaceDestroyCommand()
153 : ControlCommand("faces", "destroy")
154{
155 m_requestValidator
156 .required(CONTROL_PARAMETER_FACE_ID);
157 m_responseValidator = m_requestValidator;
158}
159
160void
161FaceDestroyCommand::validateRequest(const ControlParameters& parameters) const
162{
163 this->ControlCommand::validateRequest(parameters);
164
165 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700166 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700167 }
168}
169
170void
171FaceDestroyCommand::validateResponse(const ControlParameters& parameters) const
172{
173 this->validateRequest(parameters);
174}
175
176FaceLocalControlCommand::FaceLocalControlCommand(const std::string& verb)
177 : ControlCommand("faces", verb)
178{
179 m_requestValidator
180 .required(CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE);
181 m_responseValidator = m_requestValidator;
182}
183
184void
185FaceLocalControlCommand::validateRequest(const ControlParameters& parameters) const
186{
187 this->ControlCommand::validateRequest(parameters);
188
189 switch (parameters.getLocalControlFeature()) {
190 case LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID:
191 case LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID:
192 break;
193 default:
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700194 BOOST_THROW_EXCEPTION(ArgumentError("LocalControlFeature is invalid"));
Junxiao Shi70911652014-08-12 10:14:24 -0700195 }
196}
197
198void
199FaceLocalControlCommand::validateResponse(const ControlParameters& parameters) const
200{
201 this->validateRequest(parameters);
202}
203
204FaceEnableLocalControlCommand::FaceEnableLocalControlCommand()
205 : FaceLocalControlCommand("enable-local-control")
206{
207}
208
209FaceDisableLocalControlCommand::FaceDisableLocalControlCommand()
210 : FaceLocalControlCommand("disable-local-control")
211{
212}
213
214FibAddNextHopCommand::FibAddNextHopCommand()
215 : ControlCommand("fib", "add-nexthop")
216{
217 m_requestValidator
218 .required(CONTROL_PARAMETER_NAME)
219 .optional(CONTROL_PARAMETER_FACE_ID)
220 .optional(CONTROL_PARAMETER_COST);
221 m_responseValidator
222 .required(CONTROL_PARAMETER_NAME)
223 .required(CONTROL_PARAMETER_FACE_ID)
224 .required(CONTROL_PARAMETER_COST);
225}
226
227void
228FibAddNextHopCommand::applyDefaultsToRequest(ControlParameters& parameters) const
229{
230 if (!parameters.hasFaceId()) {
231 parameters.setFaceId(0);
232 }
233 if (!parameters.hasCost()) {
234 parameters.setCost(0);
235 }
236}
237
238void
239FibAddNextHopCommand::validateResponse(const ControlParameters& parameters) const
240{
241 this->ControlCommand::validateResponse(parameters);
242
243 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700244 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700245 }
246}
247
248FibRemoveNextHopCommand::FibRemoveNextHopCommand()
249 : ControlCommand("fib", "remove-nexthop")
250{
251 m_requestValidator
252 .required(CONTROL_PARAMETER_NAME)
253 .optional(CONTROL_PARAMETER_FACE_ID);
254 m_responseValidator
255 .required(CONTROL_PARAMETER_NAME)
256 .required(CONTROL_PARAMETER_FACE_ID);
257}
258
259void
260FibRemoveNextHopCommand::applyDefaultsToRequest(ControlParameters& parameters) const
261{
262 if (!parameters.hasFaceId()) {
263 parameters.setFaceId(0);
264 }
265}
266
267void
268FibRemoveNextHopCommand::validateResponse(const ControlParameters& parameters) const
269{
270 this->ControlCommand::validateResponse(parameters);
271
272 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700273 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700274 }
275}
276
277StrategyChoiceSetCommand::StrategyChoiceSetCommand()
278 : ControlCommand("strategy-choice", "set")
279{
280 m_requestValidator
281 .required(CONTROL_PARAMETER_NAME)
282 .required(CONTROL_PARAMETER_STRATEGY);
283 m_responseValidator = m_requestValidator;
284}
285
286StrategyChoiceUnsetCommand::StrategyChoiceUnsetCommand()
287 : ControlCommand("strategy-choice", "unset")
288{
289 m_requestValidator
290 .required(CONTROL_PARAMETER_NAME);
291 m_responseValidator = m_requestValidator;
292}
293
294void
295StrategyChoiceUnsetCommand::validateRequest(const ControlParameters& parameters) const
296{
297 this->ControlCommand::validateRequest(parameters);
298
299 if (parameters.getName().size() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700300 BOOST_THROW_EXCEPTION(ArgumentError("Name must not be ndn:/"));
Junxiao Shi70911652014-08-12 10:14:24 -0700301 }
302}
303
304void
305StrategyChoiceUnsetCommand::validateResponse(const ControlParameters& parameters) const
306{
307 this->validateRequest(parameters);
308}
309
310RibRegisterCommand::RibRegisterCommand()
311 : ControlCommand("rib", "register")
312{
313 m_requestValidator
314 .required(CONTROL_PARAMETER_NAME)
315 .optional(CONTROL_PARAMETER_FACE_ID)
316 .optional(CONTROL_PARAMETER_ORIGIN)
317 .optional(CONTROL_PARAMETER_COST)
318 .optional(CONTROL_PARAMETER_FLAGS)
319 .optional(CONTROL_PARAMETER_EXPIRATION_PERIOD);
320 m_responseValidator
321 .required(CONTROL_PARAMETER_NAME)
322 .required(CONTROL_PARAMETER_FACE_ID)
323 .required(CONTROL_PARAMETER_ORIGIN)
324 .required(CONTROL_PARAMETER_COST)
325 .required(CONTROL_PARAMETER_FLAGS)
326 .optional(CONTROL_PARAMETER_EXPIRATION_PERIOD);
327}
328
329void
330RibRegisterCommand::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 if (!parameters.hasCost()) {
339 parameters.setCost(0);
340 }
341 if (!parameters.hasFlags()) {
342 parameters.setFlags(ROUTE_FLAG_CHILD_INHERIT);
343 }
344}
345
346void
347RibRegisterCommand::validateResponse(const ControlParameters& parameters) const
348{
349 this->ControlCommand::validateResponse(parameters);
350
351 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700352 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700353 }
354}
355
356RibUnregisterCommand::RibUnregisterCommand()
357 : ControlCommand("rib", "unregister")
358{
359 m_requestValidator
360 .required(CONTROL_PARAMETER_NAME)
361 .optional(CONTROL_PARAMETER_FACE_ID)
362 .optional(CONTROL_PARAMETER_ORIGIN);
363 m_responseValidator
364 .required(CONTROL_PARAMETER_NAME)
365 .required(CONTROL_PARAMETER_FACE_ID)
366 .required(CONTROL_PARAMETER_ORIGIN);
367}
368
369void
370RibUnregisterCommand::applyDefaultsToRequest(ControlParameters& parameters) const
371{
372 if (!parameters.hasFaceId()) {
373 parameters.setFaceId(0);
374 }
375 if (!parameters.hasOrigin()) {
376 parameters.setOrigin(ROUTE_ORIGIN_APP);
377 }
378}
379
380void
381RibUnregisterCommand::validateResponse(const ControlParameters& parameters) const
382{
383 this->ControlCommand::validateResponse(parameters);
384
385 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700386 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700387 }
388}
389
390} // namespace nfd
391} // namespace ndn