blob: 7863ad4b51a6acccfd15d3dbec6d4489c45ecd8d [file] [log] [blame]
Junxiao Shi70911652014-08-12 10:14:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev73e30042015-09-17 17:09:51 -07003 * Copyright (c) 2013-2015 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
124FaceDestroyCommand::FaceDestroyCommand()
125 : ControlCommand("faces", "destroy")
126{
127 m_requestValidator
128 .required(CONTROL_PARAMETER_FACE_ID);
129 m_responseValidator = m_requestValidator;
130}
131
132void
133FaceDestroyCommand::validateRequest(const ControlParameters& parameters) const
134{
135 this->ControlCommand::validateRequest(parameters);
136
137 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700138 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700139 }
140}
141
142void
143FaceDestroyCommand::validateResponse(const ControlParameters& parameters) const
144{
145 this->validateRequest(parameters);
146}
147
148FaceLocalControlCommand::FaceLocalControlCommand(const std::string& verb)
149 : ControlCommand("faces", verb)
150{
151 m_requestValidator
152 .required(CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE);
153 m_responseValidator = m_requestValidator;
154}
155
156void
157FaceLocalControlCommand::validateRequest(const ControlParameters& parameters) const
158{
159 this->ControlCommand::validateRequest(parameters);
160
161 switch (parameters.getLocalControlFeature()) {
162 case LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID:
163 case LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID:
164 break;
165 default:
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700166 BOOST_THROW_EXCEPTION(ArgumentError("LocalControlFeature is invalid"));
Junxiao Shi70911652014-08-12 10:14:24 -0700167 }
168}
169
170void
171FaceLocalControlCommand::validateResponse(const ControlParameters& parameters) const
172{
173 this->validateRequest(parameters);
174}
175
176FaceEnableLocalControlCommand::FaceEnableLocalControlCommand()
177 : FaceLocalControlCommand("enable-local-control")
178{
179}
180
181FaceDisableLocalControlCommand::FaceDisableLocalControlCommand()
182 : FaceLocalControlCommand("disable-local-control")
183{
184}
185
186FibAddNextHopCommand::FibAddNextHopCommand()
187 : ControlCommand("fib", "add-nexthop")
188{
189 m_requestValidator
190 .required(CONTROL_PARAMETER_NAME)
191 .optional(CONTROL_PARAMETER_FACE_ID)
192 .optional(CONTROL_PARAMETER_COST);
193 m_responseValidator
194 .required(CONTROL_PARAMETER_NAME)
195 .required(CONTROL_PARAMETER_FACE_ID)
196 .required(CONTROL_PARAMETER_COST);
197}
198
199void
200FibAddNextHopCommand::applyDefaultsToRequest(ControlParameters& parameters) const
201{
202 if (!parameters.hasFaceId()) {
203 parameters.setFaceId(0);
204 }
205 if (!parameters.hasCost()) {
206 parameters.setCost(0);
207 }
208}
209
210void
211FibAddNextHopCommand::validateResponse(const ControlParameters& parameters) const
212{
213 this->ControlCommand::validateResponse(parameters);
214
215 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700216 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700217 }
218}
219
220FibRemoveNextHopCommand::FibRemoveNextHopCommand()
221 : ControlCommand("fib", "remove-nexthop")
222{
223 m_requestValidator
224 .required(CONTROL_PARAMETER_NAME)
225 .optional(CONTROL_PARAMETER_FACE_ID);
226 m_responseValidator
227 .required(CONTROL_PARAMETER_NAME)
228 .required(CONTROL_PARAMETER_FACE_ID);
229}
230
231void
232FibRemoveNextHopCommand::applyDefaultsToRequest(ControlParameters& parameters) const
233{
234 if (!parameters.hasFaceId()) {
235 parameters.setFaceId(0);
236 }
237}
238
239void
240FibRemoveNextHopCommand::validateResponse(const ControlParameters& parameters) const
241{
242 this->ControlCommand::validateResponse(parameters);
243
244 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700245 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700246 }
247}
248
249StrategyChoiceSetCommand::StrategyChoiceSetCommand()
250 : ControlCommand("strategy-choice", "set")
251{
252 m_requestValidator
253 .required(CONTROL_PARAMETER_NAME)
254 .required(CONTROL_PARAMETER_STRATEGY);
255 m_responseValidator = m_requestValidator;
256}
257
258StrategyChoiceUnsetCommand::StrategyChoiceUnsetCommand()
259 : ControlCommand("strategy-choice", "unset")
260{
261 m_requestValidator
262 .required(CONTROL_PARAMETER_NAME);
263 m_responseValidator = m_requestValidator;
264}
265
266void
267StrategyChoiceUnsetCommand::validateRequest(const ControlParameters& parameters) const
268{
269 this->ControlCommand::validateRequest(parameters);
270
271 if (parameters.getName().size() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700272 BOOST_THROW_EXCEPTION(ArgumentError("Name must not be ndn:/"));
Junxiao Shi70911652014-08-12 10:14:24 -0700273 }
274}
275
276void
277StrategyChoiceUnsetCommand::validateResponse(const ControlParameters& parameters) const
278{
279 this->validateRequest(parameters);
280}
281
282RibRegisterCommand::RibRegisterCommand()
283 : ControlCommand("rib", "register")
284{
285 m_requestValidator
286 .required(CONTROL_PARAMETER_NAME)
287 .optional(CONTROL_PARAMETER_FACE_ID)
288 .optional(CONTROL_PARAMETER_ORIGIN)
289 .optional(CONTROL_PARAMETER_COST)
290 .optional(CONTROL_PARAMETER_FLAGS)
291 .optional(CONTROL_PARAMETER_EXPIRATION_PERIOD);
292 m_responseValidator
293 .required(CONTROL_PARAMETER_NAME)
294 .required(CONTROL_PARAMETER_FACE_ID)
295 .required(CONTROL_PARAMETER_ORIGIN)
296 .required(CONTROL_PARAMETER_COST)
297 .required(CONTROL_PARAMETER_FLAGS)
298 .optional(CONTROL_PARAMETER_EXPIRATION_PERIOD);
299}
300
301void
302RibRegisterCommand::applyDefaultsToRequest(ControlParameters& parameters) const
303{
304 if (!parameters.hasFaceId()) {
305 parameters.setFaceId(0);
306 }
307 if (!parameters.hasOrigin()) {
308 parameters.setOrigin(ROUTE_ORIGIN_APP);
309 }
310 if (!parameters.hasCost()) {
311 parameters.setCost(0);
312 }
313 if (!parameters.hasFlags()) {
314 parameters.setFlags(ROUTE_FLAG_CHILD_INHERIT);
315 }
316}
317
318void
319RibRegisterCommand::validateResponse(const ControlParameters& parameters) const
320{
321 this->ControlCommand::validateResponse(parameters);
322
323 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700324 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700325 }
326}
327
328RibUnregisterCommand::RibUnregisterCommand()
329 : ControlCommand("rib", "unregister")
330{
331 m_requestValidator
332 .required(CONTROL_PARAMETER_NAME)
333 .optional(CONTROL_PARAMETER_FACE_ID)
334 .optional(CONTROL_PARAMETER_ORIGIN);
335 m_responseValidator
336 .required(CONTROL_PARAMETER_NAME)
337 .required(CONTROL_PARAMETER_FACE_ID)
338 .required(CONTROL_PARAMETER_ORIGIN);
339}
340
341void
342RibUnregisterCommand::applyDefaultsToRequest(ControlParameters& parameters) const
343{
344 if (!parameters.hasFaceId()) {
345 parameters.setFaceId(0);
346 }
347 if (!parameters.hasOrigin()) {
348 parameters.setOrigin(ROUTE_ORIGIN_APP);
349 }
350}
351
352void
353RibUnregisterCommand::validateResponse(const ControlParameters& parameters) const
354{
355 this->ControlCommand::validateResponse(parameters);
356
357 if (parameters.getFaceId() == 0) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700358 BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
Junxiao Shi70911652014-08-12 10:14:24 -0700359 }
360}
361
362} // namespace nfd
363} // namespace ndn