control-command.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2018 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 "control-command.hpp"
23 
24 namespace ndn {
25 namespace nfd {
26 
27 ControlCommand::ControlCommand(const std::string& module, const std::string& verb)
28  : m_module(module)
29  , m_verb(verb)
30 {
31 }
32 
34 
35 void
37 {
38  m_requestValidator.validate(parameters);
39 }
40 
41 void
43 {
44 }
45 
46 void
48 {
49  m_responseValidator.validate(parameters);
50 }
51 
52 void
54 {
55 }
56 
57 Name
58 ControlCommand::getRequestName(const Name& commandPrefix,
59  const ControlParameters& parameters) const
60 {
61  this->validateRequest(parameters);
62 
63  Name name = commandPrefix;
64  name.append(m_module).append(m_verb);
65  name.append(parameters.wireEncode());
66  return name;
67 }
68 
70  : m_required(CONTROL_PARAMETER_UBOUND)
71  , m_optional(CONTROL_PARAMETER_UBOUND)
72 {
73 }
74 
75 void
77 {
78  const std::vector<bool>& presentFields = parameters.getPresentFields();
79 
80  for (size_t i = 0; i < CONTROL_PARAMETER_UBOUND; ++i) {
81  bool isPresent = presentFields[i];
82  if (m_required[i]) {
83  if (!isPresent) {
84  BOOST_THROW_EXCEPTION(ArgumentError(CONTROL_PARAMETER_FIELD[i] + " is required but missing"));
85  }
86  }
87  else if (isPresent && !m_optional[i]) {
88  BOOST_THROW_EXCEPTION(ArgumentError(CONTROL_PARAMETER_FIELD[i] + " is forbidden but present"));
89  }
90  }
91 
92  if (m_optional[CONTROL_PARAMETER_FLAGS] && m_optional[CONTROL_PARAMETER_MASK]) {
93  if (parameters.hasFlags() != parameters.hasMask()) {
94  BOOST_THROW_EXCEPTION(ArgumentError("Flags must be accompanied by Mask"));
95  }
96  }
97 }
98 
100  : ControlCommand("faces", "create")
101 {
118 }
119 
120 void
122 {
123  if (!parameters.hasFacePersistency()) {
125  }
126 }
127 
128 void
130 {
131  this->ControlCommand::validateResponse(parameters);
132 
133  if (parameters.getFaceId() == INVALID_FACE_ID) {
134  BOOST_THROW_EXCEPTION(ArgumentError("FaceId must be valid"));
135  }
136 }
137 
139  : ControlCommand("faces", "update")
140 {
154 }
155 
156 void
158 {
159  if (!parameters.hasFaceId()) {
160  parameters.setFaceId(0);
161  }
162 }
163 
164 void
166 {
167  this->ControlCommand::validateResponse(parameters);
168 
169  if (parameters.getFaceId() == INVALID_FACE_ID) {
170  BOOST_THROW_EXCEPTION(ArgumentError("FaceId must be valid"));
171  }
172 }
173 
175  : ControlCommand("faces", "destroy")
176 {
180 }
181 
182 void
184 {
185  this->ControlCommand::validateRequest(parameters);
186 
187  if (parameters.getFaceId() == INVALID_FACE_ID) {
188  BOOST_THROW_EXCEPTION(ArgumentError("FaceId must be valid"));
189  }
190 }
191 
192 void
194 {
195  this->validateRequest(parameters);
196 }
197 
199  : ControlCommand("fib", "add-nexthop")
200 {
209 }
210 
211 void
213 {
214  if (!parameters.hasFaceId()) {
215  parameters.setFaceId(0);
216  }
217  if (!parameters.hasCost()) {
218  parameters.setCost(0);
219  }
220 }
221 
222 void
224 {
225  this->ControlCommand::validateResponse(parameters);
226 
227  if (parameters.getFaceId() == INVALID_FACE_ID) {
228  BOOST_THROW_EXCEPTION(ArgumentError("FaceId must be valid"));
229  }
230 }
231 
233  : ControlCommand("fib", "remove-nexthop")
234 {
241 }
242 
243 void
245 {
246  if (!parameters.hasFaceId()) {
247  parameters.setFaceId(0);
248  }
249 }
250 
251 void
253 {
254  this->ControlCommand::validateResponse(parameters);
255 
256  if (parameters.getFaceId() == INVALID_FACE_ID) {
257  BOOST_THROW_EXCEPTION(ArgumentError("FaceId must be valid"));
258  }
259 }
260 
262  : ControlCommand("cs", "config")
263 {
271 }
272 
274  : ControlCommand("strategy-choice", "set")
275 {
280 }
281 
283  : ControlCommand("strategy-choice", "unset")
284 {
288 }
289 
290 void
292 {
293  this->ControlCommand::validateRequest(parameters);
294 
295  if (parameters.getName().size() == 0) {
296  BOOST_THROW_EXCEPTION(ArgumentError("Name must not be ndn:/"));
297  }
298 }
299 
300 void
302 {
303  this->validateRequest(parameters);
304 }
305 
307  : ControlCommand("rib", "register")
308 {
323 }
324 
325 void
327 {
328  if (!parameters.hasFaceId()) {
329  parameters.setFaceId(0);
330  }
331  if (!parameters.hasOrigin()) {
332  parameters.setOrigin(ROUTE_ORIGIN_APP);
333  }
334  if (!parameters.hasCost()) {
335  parameters.setCost(0);
336  }
337  if (!parameters.hasFlags()) {
339  }
340 }
341 
342 void
344 {
345  this->ControlCommand::validateResponse(parameters);
346 
347  if (parameters.getFaceId() == INVALID_FACE_ID) {
348  BOOST_THROW_EXCEPTION(ArgumentError("FaceId must be valid"));
349  }
350 }
351 
353  : ControlCommand("rib", "unregister")
354 {
363 }
364 
365 void
367 {
368  if (!parameters.hasFaceId()) {
369  parameters.setFaceId(0);
370  }
371  if (!parameters.hasOrigin()) {
372  parameters.setOrigin(ROUTE_ORIGIN_APP);
373  }
374 }
375 
376 void
378 {
379  this->ControlCommand::validateResponse(parameters);
380 
381  if (parameters.getFaceId() == INVALID_FACE_ID) {
382  BOOST_THROW_EXCEPTION(ArgumentError("FaceId must be valid"));
383  }
384 }
385 
386 } // namespace nfd
387 } // namespace ndn
void validateResponse(const ControlParameters &parameters) const override
validate response parameters
virtual void applyDefaultsToResponse(ControlParameters &parameters) const
apply default values to missing fields in response
ControlParameters & setFaceId(uint64_t faceId)
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
void validate(const ControlParameters &parameters) const
verify that all required fields are present, and all present fields are either required or optional ...
void applyDefaultsToRequest(ControlParameters &parameters) const override
apply default values to missing fields in request
void applyDefaultsToRequest(ControlParameters &parameters) const override
apply default values to missing fields in request
represents parameters in a ControlCommand request or response
void applyDefaultsToRequest(ControlParameters &parameters) const override
apply default values to missing fields in request
FieldValidator m_requestValidator
FieldValidator for request ControlParameters.
void applyDefaultsToRequest(ControlParameters &parameters) const override
apply default values to missing fields in request
base class of NFD ControlCommand
virtual void validateResponse(const ControlParameters &parameters) const
validate response parameters
void applyDefaultsToRequest(ControlParameters &parameters) const override
apply default values to missing fields in request
virtual void validateRequest(const ControlParameters &parameters) const
validate request parameters
ControlParameters & setFlags(uint64_t flags)
FieldValidator m_responseValidator
FieldValidator for response ControlParameters.
Name & append(const Component &component)
Append a component.
Definition: name.hpp:256
ControlCommand(const std::string &module, const std::string &verb)
void validateResponse(const ControlParameters &parameters) const override
validate response parameters
void validateRequest(const ControlParameters &parameters) const override
validate request parameters
void validateResponse(const ControlParameters &parameters) const override
validate response parameters
void validateResponse(const ControlParameters &parameters) const override
validate response parameters
const std::vector< bool > & getPresentFields() const
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Name getRequestName(const Name &commandPrefix, const ControlParameters &parameters) const
construct the Name for a request Interest
void validateResponse(const ControlParameters &parameters) const override
validate response parameters
FieldValidator & required(ControlParameterField field)
declare a required field
size_t size() const
Get number of components.
Definition: name.hpp:154
void validateResponse(const ControlParameters &parameters) const override
validate response parameters
void validateResponse(const ControlParameters &parameters) const override
validate response parameters
Represents an absolute name.
Definition: name.hpp:42
const std::string CONTROL_PARAMETER_FIELD[CONTROL_PARAMETER_UBOUND]
void validateResponse(const ControlParameters &parameters) const override
void validateRequest(const ControlParameters &parameters) const override
validate request parameters
ControlParameters & setCost(uint64_t cost)
void applyDefaultsToRequest(ControlParameters &parameters) const override
apply default values to missing fields in request
ControlParameters & setFacePersistency(FacePersistency persistency)
ControlParameters & setOrigin(RouteOrigin origin)
virtual void applyDefaultsToRequest(ControlParameters &parameters) const
apply default values to missing fields in request
represents an error in ControlParameters
const uint64_t INVALID_FACE_ID
FieldValidator & optional(ControlParameterField field)
declare an optional field