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