blob: ac7a40d40bc202960b39f517b576788874a417af [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"
23
24namespace ndn {
25namespace nfd {
26
27ControlCommand::ControlCommand(const std::string& module, const std::string& verb)
28 : m_prefix("ndn:/localhost/nfd")
29{
30 m_prefix.append(module).append(verb);
31}
32
33void
34ControlCommand::validateRequest(const ControlParameters& parameters) const
35{
36 m_requestValidator.validate(parameters);
37}
38
39void
40ControlCommand::applyDefaultsToRequest(ControlParameters& parameters) const
41{
42}
43
44void
45ControlCommand::validateResponse(const ControlParameters& parameters) const
46{
47 m_responseValidator.validate(parameters);
48}
49
50void
51ControlCommand::applyDefaultsToResponse(ControlParameters& parameters) const
52{
53}
54
55Name
56ControlCommand::getRequestName(const ControlParameters& parameters) const
57{
58 this->validateRequest(parameters);
59
60 Name name = m_prefix;
61 name.append(parameters.wireEncode());
62 return name;
63}
64
65Interest
66ControlCommand::makeCommandInterest(const ControlParameters& parameters,
67 const Sign& sign) const
68{
69 Interest commandInterest(this->getRequestName(parameters));
70 sign(commandInterest);
71 return commandInterest;
72}
73
74ControlCommand::FieldValidator::FieldValidator()
75 : m_required(CONTROL_PARAMETER_UBOUND)
76 , m_optional(CONTROL_PARAMETER_UBOUND)
77{
78}
79
80void
81ControlCommand::FieldValidator::validate(const ControlParameters& parameters) const
82{
83 const std::vector<bool>& presentFields = parameters.getPresentFields();
84
85 for (size_t i = 0; i < CONTROL_PARAMETER_UBOUND; ++i) {
86 bool isPresent = presentFields[i];
87 if (m_required[i]) {
88 if (!isPresent) {
89 throw ArgumentError(CONTROL_PARAMETER_FIELD[i] + " is required but missing");
90 }
91 }
92 else if (isPresent && !m_optional[i]) {
93 throw ArgumentError(CONTROL_PARAMETER_FIELD[i] + " is forbidden but present");
94 }
95 }
96}
97
98FaceCreateCommand::FaceCreateCommand()
99 : ControlCommand("faces", "create")
100{
101 m_requestValidator
102 .required(CONTROL_PARAMETER_URI);
103 m_responseValidator
104 .required(CONTROL_PARAMETER_URI)
105 .required(CONTROL_PARAMETER_FACE_ID);
106}
107
108void
109FaceCreateCommand::validateResponse(const ControlParameters& parameters) const
110{
111 this->ControlCommand::validateResponse(parameters);
112
113 if (parameters.getFaceId() == 0) {
114 throw ArgumentError("FaceId must not be zero");
115 }
116}
117
118FaceDestroyCommand::FaceDestroyCommand()
119 : ControlCommand("faces", "destroy")
120{
121 m_requestValidator
122 .required(CONTROL_PARAMETER_FACE_ID);
123 m_responseValidator = m_requestValidator;
124}
125
126void
127FaceDestroyCommand::validateRequest(const ControlParameters& parameters) const
128{
129 this->ControlCommand::validateRequest(parameters);
130
131 if (parameters.getFaceId() == 0) {
132 throw ArgumentError("FaceId must not be zero");
133 }
134}
135
136void
137FaceDestroyCommand::validateResponse(const ControlParameters& parameters) const
138{
139 this->validateRequest(parameters);
140}
141
142FaceLocalControlCommand::FaceLocalControlCommand(const std::string& verb)
143 : ControlCommand("faces", verb)
144{
145 m_requestValidator
146 .required(CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE);
147 m_responseValidator = m_requestValidator;
148}
149
150void
151FaceLocalControlCommand::validateRequest(const ControlParameters& parameters) const
152{
153 this->ControlCommand::validateRequest(parameters);
154
155 switch (parameters.getLocalControlFeature()) {
156 case LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID:
157 case LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID:
158 break;
159 default:
160 throw ArgumentError("LocalControlFeature is invalid");
161 }
162}
163
164void
165FaceLocalControlCommand::validateResponse(const ControlParameters& parameters) const
166{
167 this->validateRequest(parameters);
168}
169
170FaceEnableLocalControlCommand::FaceEnableLocalControlCommand()
171 : FaceLocalControlCommand("enable-local-control")
172{
173}
174
175FaceDisableLocalControlCommand::FaceDisableLocalControlCommand()
176 : FaceLocalControlCommand("disable-local-control")
177{
178}
179
180FibAddNextHopCommand::FibAddNextHopCommand()
181 : ControlCommand("fib", "add-nexthop")
182{
183 m_requestValidator
184 .required(CONTROL_PARAMETER_NAME)
185 .optional(CONTROL_PARAMETER_FACE_ID)
186 .optional(CONTROL_PARAMETER_COST);
187 m_responseValidator
188 .required(CONTROL_PARAMETER_NAME)
189 .required(CONTROL_PARAMETER_FACE_ID)
190 .required(CONTROL_PARAMETER_COST);
191}
192
193void
194FibAddNextHopCommand::applyDefaultsToRequest(ControlParameters& parameters) const
195{
196 if (!parameters.hasFaceId()) {
197 parameters.setFaceId(0);
198 }
199 if (!parameters.hasCost()) {
200 parameters.setCost(0);
201 }
202}
203
204void
205FibAddNextHopCommand::validateResponse(const ControlParameters& parameters) const
206{
207 this->ControlCommand::validateResponse(parameters);
208
209 if (parameters.getFaceId() == 0) {
210 throw ArgumentError("FaceId must not be zero");
211 }
212}
213
214FibRemoveNextHopCommand::FibRemoveNextHopCommand()
215 : ControlCommand("fib", "remove-nexthop")
216{
217 m_requestValidator
218 .required(CONTROL_PARAMETER_NAME)
219 .optional(CONTROL_PARAMETER_FACE_ID);
220 m_responseValidator
221 .required(CONTROL_PARAMETER_NAME)
222 .required(CONTROL_PARAMETER_FACE_ID);
223}
224
225void
226FibRemoveNextHopCommand::applyDefaultsToRequest(ControlParameters& parameters) const
227{
228 if (!parameters.hasFaceId()) {
229 parameters.setFaceId(0);
230 }
231}
232
233void
234FibRemoveNextHopCommand::validateResponse(const ControlParameters& parameters) const
235{
236 this->ControlCommand::validateResponse(parameters);
237
238 if (parameters.getFaceId() == 0) {
239 throw ArgumentError("FaceId must not be zero");
240 }
241}
242
243StrategyChoiceSetCommand::StrategyChoiceSetCommand()
244 : ControlCommand("strategy-choice", "set")
245{
246 m_requestValidator
247 .required(CONTROL_PARAMETER_NAME)
248 .required(CONTROL_PARAMETER_STRATEGY);
249 m_responseValidator = m_requestValidator;
250}
251
252StrategyChoiceUnsetCommand::StrategyChoiceUnsetCommand()
253 : ControlCommand("strategy-choice", "unset")
254{
255 m_requestValidator
256 .required(CONTROL_PARAMETER_NAME);
257 m_responseValidator = m_requestValidator;
258}
259
260void
261StrategyChoiceUnsetCommand::validateRequest(const ControlParameters& parameters) const
262{
263 this->ControlCommand::validateRequest(parameters);
264
265 if (parameters.getName().size() == 0) {
266 throw ArgumentError("Name must not be ndn:/");
267 }
268}
269
270void
271StrategyChoiceUnsetCommand::validateResponse(const ControlParameters& parameters) const
272{
273 this->validateRequest(parameters);
274}
275
276RibRegisterCommand::RibRegisterCommand()
277 : ControlCommand("rib", "register")
278{
279 m_requestValidator
280 .required(CONTROL_PARAMETER_NAME)
281 .optional(CONTROL_PARAMETER_FACE_ID)
282 .optional(CONTROL_PARAMETER_ORIGIN)
283 .optional(CONTROL_PARAMETER_COST)
284 .optional(CONTROL_PARAMETER_FLAGS)
285 .optional(CONTROL_PARAMETER_EXPIRATION_PERIOD);
286 m_responseValidator
287 .required(CONTROL_PARAMETER_NAME)
288 .required(CONTROL_PARAMETER_FACE_ID)
289 .required(CONTROL_PARAMETER_ORIGIN)
290 .required(CONTROL_PARAMETER_COST)
291 .required(CONTROL_PARAMETER_FLAGS)
292 .optional(CONTROL_PARAMETER_EXPIRATION_PERIOD);
293}
294
295void
296RibRegisterCommand::applyDefaultsToRequest(ControlParameters& parameters) const
297{
298 if (!parameters.hasFaceId()) {
299 parameters.setFaceId(0);
300 }
301 if (!parameters.hasOrigin()) {
302 parameters.setOrigin(ROUTE_ORIGIN_APP);
303 }
304 if (!parameters.hasCost()) {
305 parameters.setCost(0);
306 }
307 if (!parameters.hasFlags()) {
308 parameters.setFlags(ROUTE_FLAG_CHILD_INHERIT);
309 }
310}
311
312void
313RibRegisterCommand::validateResponse(const ControlParameters& parameters) const
314{
315 this->ControlCommand::validateResponse(parameters);
316
317 if (parameters.getFaceId() == 0) {
318 throw ArgumentError("FaceId must not be zero");
319 }
320}
321
322RibUnregisterCommand::RibUnregisterCommand()
323 : ControlCommand("rib", "unregister")
324{
325 m_requestValidator
326 .required(CONTROL_PARAMETER_NAME)
327 .optional(CONTROL_PARAMETER_FACE_ID)
328 .optional(CONTROL_PARAMETER_ORIGIN);
329 m_responseValidator
330 .required(CONTROL_PARAMETER_NAME)
331 .required(CONTROL_PARAMETER_FACE_ID)
332 .required(CONTROL_PARAMETER_ORIGIN);
333}
334
335void
336RibUnregisterCommand::applyDefaultsToRequest(ControlParameters& parameters) const
337{
338 if (!parameters.hasFaceId()) {
339 parameters.setFaceId(0);
340 }
341 if (!parameters.hasOrigin()) {
342 parameters.setOrigin(ROUTE_ORIGIN_APP);
343 }
344}
345
346void
347RibUnregisterCommand::validateResponse(const ControlParameters& parameters) const
348{
349 this->ControlCommand::validateResponse(parameters);
350
351 if (parameters.getFaceId() == 0) {
352 throw ArgumentError("FaceId must not be zero");
353 }
354}
355
356} // namespace nfd
357} // namespace ndn