blob: d8ab2c6e131a40ccd524acd23732eaf43ecf31ce [file] [log] [blame]
Junxiao Shi70911652014-08-12 10:14:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014 Regents of the University of California.
4 *
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
98 .required(CONTROL_PARAMETER_URI);
99 m_responseValidator
100 .required(CONTROL_PARAMETER_URI)
101 .required(CONTROL_PARAMETER_FACE_ID);
102}
103
104void
105FaceCreateCommand::validateResponse(const ControlParameters& parameters) const
106{
107 this->ControlCommand::validateResponse(parameters);
108
109 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700110 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700111 }
112}
113
114FaceDestroyCommand::FaceDestroyCommand()
115 : ControlCommand("faces", "destroy")
116{
117 m_requestValidator
118 .required(CONTROL_PARAMETER_FACE_ID);
119 m_responseValidator = m_requestValidator;
120}
121
122void
123FaceDestroyCommand::validateRequest(const ControlParameters& parameters) const
124{
125 this->ControlCommand::validateRequest(parameters);
126
127 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700128 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700129 }
130}
131
132void
133FaceDestroyCommand::validateResponse(const ControlParameters& parameters) const
134{
135 this->validateRequest(parameters);
136}
137
138FaceLocalControlCommand::FaceLocalControlCommand(const std::string& verb)
139 : ControlCommand("faces", verb)
140{
141 m_requestValidator
142 .required(CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE);
143 m_responseValidator = m_requestValidator;
144}
145
146void
147FaceLocalControlCommand::validateRequest(const ControlParameters& parameters) const
148{
149 this->ControlCommand::validateRequest(parameters);
150
151 switch (parameters.getLocalControlFeature()) {
152 case LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID:
153 case LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID:
154 break;
155 default:
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700156 BOOST_THROW_EXCEPTION(ArgumentError("LocalControlFeature is invalid"));
Junxiao Shi70911652014-08-12 10:14:24 -0700157 }
158}
159
160void
161FaceLocalControlCommand::validateResponse(const ControlParameters& parameters) const
162{
163 this->validateRequest(parameters);
164}
165
166FaceEnableLocalControlCommand::FaceEnableLocalControlCommand()
167 : FaceLocalControlCommand("enable-local-control")
168{
169}
170
171FaceDisableLocalControlCommand::FaceDisableLocalControlCommand()
172 : FaceLocalControlCommand("disable-local-control")
173{
174}
175
176FibAddNextHopCommand::FibAddNextHopCommand()
177 : ControlCommand("fib", "add-nexthop")
178{
179 m_requestValidator
180 .required(CONTROL_PARAMETER_NAME)
181 .optional(CONTROL_PARAMETER_FACE_ID)
182 .optional(CONTROL_PARAMETER_COST);
183 m_responseValidator
184 .required(CONTROL_PARAMETER_NAME)
185 .required(CONTROL_PARAMETER_FACE_ID)
186 .required(CONTROL_PARAMETER_COST);
187}
188
189void
190FibAddNextHopCommand::applyDefaultsToRequest(ControlParameters& parameters) const
191{
192 if (!parameters.hasFaceId()) {
193 parameters.setFaceId(0);
194 }
195 if (!parameters.hasCost()) {
196 parameters.setCost(0);
197 }
198}
199
200void
201FibAddNextHopCommand::validateResponse(const ControlParameters& parameters) const
202{
203 this->ControlCommand::validateResponse(parameters);
204
205 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700206 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700207 }
208}
209
210FibRemoveNextHopCommand::FibRemoveNextHopCommand()
211 : ControlCommand("fib", "remove-nexthop")
212{
213 m_requestValidator
214 .required(CONTROL_PARAMETER_NAME)
215 .optional(CONTROL_PARAMETER_FACE_ID);
216 m_responseValidator
217 .required(CONTROL_PARAMETER_NAME)
218 .required(CONTROL_PARAMETER_FACE_ID);
219}
220
221void
222FibRemoveNextHopCommand::applyDefaultsToRequest(ControlParameters& parameters) const
223{
224 if (!parameters.hasFaceId()) {
225 parameters.setFaceId(0);
226 }
227}
228
229void
230FibRemoveNextHopCommand::validateResponse(const ControlParameters& parameters) const
231{
232 this->ControlCommand::validateResponse(parameters);
233
234 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700235 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700236 }
237}
238
239StrategyChoiceSetCommand::StrategyChoiceSetCommand()
240 : ControlCommand("strategy-choice", "set")
241{
242 m_requestValidator
243 .required(CONTROL_PARAMETER_NAME)
244 .required(CONTROL_PARAMETER_STRATEGY);
245 m_responseValidator = m_requestValidator;
246}
247
248StrategyChoiceUnsetCommand::StrategyChoiceUnsetCommand()
249 : ControlCommand("strategy-choice", "unset")
250{
251 m_requestValidator
252 .required(CONTROL_PARAMETER_NAME);
253 m_responseValidator = m_requestValidator;
254}
255
256void
257StrategyChoiceUnsetCommand::validateRequest(const ControlParameters& parameters) const
258{
259 this->ControlCommand::validateRequest(parameters);
260
261 if (parameters.getName().size() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700262 BOOST_THROW_EXCEPTION(ArgumentError("Name must not be ndn:/"));
Junxiao Shi70911652014-08-12 10:14:24 -0700263 }
264}
265
266void
267StrategyChoiceUnsetCommand::validateResponse(const ControlParameters& parameters) const
268{
269 this->validateRequest(parameters);
270}
271
272RibRegisterCommand::RibRegisterCommand()
273 : ControlCommand("rib", "register")
274{
275 m_requestValidator
276 .required(CONTROL_PARAMETER_NAME)
277 .optional(CONTROL_PARAMETER_FACE_ID)
278 .optional(CONTROL_PARAMETER_ORIGIN)
279 .optional(CONTROL_PARAMETER_COST)
280 .optional(CONTROL_PARAMETER_FLAGS)
281 .optional(CONTROL_PARAMETER_EXPIRATION_PERIOD);
282 m_responseValidator
283 .required(CONTROL_PARAMETER_NAME)
284 .required(CONTROL_PARAMETER_FACE_ID)
285 .required(CONTROL_PARAMETER_ORIGIN)
286 .required(CONTROL_PARAMETER_COST)
287 .required(CONTROL_PARAMETER_FLAGS)
288 .optional(CONTROL_PARAMETER_EXPIRATION_PERIOD);
289}
290
291void
292RibRegisterCommand::applyDefaultsToRequest(ControlParameters& parameters) const
293{
294 if (!parameters.hasFaceId()) {
295 parameters.setFaceId(0);
296 }
297 if (!parameters.hasOrigin()) {
298 parameters.setOrigin(ROUTE_ORIGIN_APP);
299 }
300 if (!parameters.hasCost()) {
301 parameters.setCost(0);
302 }
303 if (!parameters.hasFlags()) {
304 parameters.setFlags(ROUTE_FLAG_CHILD_INHERIT);
305 }
306}
307
308void
309RibRegisterCommand::validateResponse(const ControlParameters& parameters) const
310{
311 this->ControlCommand::validateResponse(parameters);
312
313 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700314 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700315 }
316}
317
318RibUnregisterCommand::RibUnregisterCommand()
319 : ControlCommand("rib", "unregister")
320{
321 m_requestValidator
322 .required(CONTROL_PARAMETER_NAME)
323 .optional(CONTROL_PARAMETER_FACE_ID)
324 .optional(CONTROL_PARAMETER_ORIGIN);
325 m_responseValidator
326 .required(CONTROL_PARAMETER_NAME)
327 .required(CONTROL_PARAMETER_FACE_ID)
328 .required(CONTROL_PARAMETER_ORIGIN);
329}
330
331void
332RibUnregisterCommand::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}
341
342void
343RibUnregisterCommand::validateResponse(const ControlParameters& parameters) const
344{
345 this->ControlCommand::validateResponse(parameters);
346
347 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700348 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700349 }
350}
351
352} // namespace nfd
353} // namespace ndn