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