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-2022 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 
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  return Name(commandPrefix)
64  .append(m_module)
65  .append(m_verb)
66  .append(parameters.wireEncode());
67 }
68 
70  : m_required(CONTROL_PARAMETER_UBOUND)
71  , m_optional(CONTROL_PARAMETER_UBOUND)
72 {
73 }
74 
75 void
77 {
78  const auto& 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  NDN_THROW(ArgumentError(CONTROL_PARAMETER_FIELD[i] + " is required but missing"));
85  }
86  }
87  else if (isPresent && !m_optional[i]) {
88  NDN_THROW(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  NDN_THROW(ArgumentError("Flags must be accompanied by Mask"));
95  }
96  }
97 }
98 
100  : ControlCommand("faces", "create")
101 {
120 }
121 
122 void
124 {
125  if (!parameters.hasFacePersistency()) {
127  }
128 }
129 
130 void
132 {
133  this->ControlCommand::validateResponse(parameters);
134 
135  if (parameters.getFaceId() == INVALID_FACE_ID) {
136  NDN_THROW(ArgumentError("FaceId must be valid"));
137  }
138 }
139 
141  : ControlCommand("faces", "update")
142 {
158 }
159 
160 void
162 {
163  if (!parameters.hasFaceId()) {
164  parameters.setFaceId(0);
165  }
166 }
167 
168 void
170 {
171  this->ControlCommand::validateResponse(parameters);
172 
173  if (parameters.getFaceId() == INVALID_FACE_ID) {
174  NDN_THROW(ArgumentError("FaceId must be valid"));
175  }
176 }
177 
179  : ControlCommand("faces", "destroy")
180 {
184 }
185 
186 void
188 {
189  this->ControlCommand::validateRequest(parameters);
190 
191  if (parameters.getFaceId() == INVALID_FACE_ID) {
192  NDN_THROW(ArgumentError("FaceId must be valid"));
193  }
194 }
195 
196 void
198 {
199  this->validateRequest(parameters);
200 }
201 
203  : ControlCommand("fib", "add-nexthop")
204 {
213 }
214 
215 void
217 {
218  if (!parameters.hasFaceId()) {
219  parameters.setFaceId(0);
220  }
221  if (!parameters.hasCost()) {
222  parameters.setCost(0);
223  }
224 }
225 
226 void
228 {
229  this->ControlCommand::validateResponse(parameters);
230 
231  if (parameters.getFaceId() == INVALID_FACE_ID) {
232  NDN_THROW(ArgumentError("FaceId must be valid"));
233  }
234 }
235 
237  : ControlCommand("fib", "remove-nexthop")
238 {
245 }
246 
247 void
249 {
250  if (!parameters.hasFaceId()) {
251  parameters.setFaceId(0);
252  }
253 }
254 
255 void
257 {
258  this->ControlCommand::validateResponse(parameters);
259 
260  if (parameters.getFaceId() == INVALID_FACE_ID) {
261  NDN_THROW(ArgumentError("FaceId must be valid"));
262  }
263 }
264 
266  : ControlCommand("cs", "config")
267 {
275 }
276 
278  : ControlCommand("cs", "erase")
279 {
287 }
288 
289 void
291 {
292  this->ControlCommand::validateRequest(parameters);
293 
294  if (parameters.hasCount() && parameters.getCount() == 0) {
295  NDN_THROW(ArgumentError("Count must be positive"));
296  }
297 }
298 
299 void
301 {
302  this->ControlCommand::validateResponse(parameters);
303 
304  if (parameters.hasCapacity() && parameters.getCapacity() == 0) {
305  NDN_THROW(ArgumentError("Capacity must be positive"));
306  }
307 }
308 
310  : ControlCommand("strategy-choice", "set")
311 {
316 }
317 
319  : ControlCommand("strategy-choice", "unset")
320 {
324 }
325 
326 void
328 {
329  this->ControlCommand::validateRequest(parameters);
330 
331  if (parameters.getName().empty()) {
332  NDN_THROW(ArgumentError("Name must not be ndn:/"));
333  }
334 }
335 
336 void
338 {
339  this->validateRequest(parameters);
340 }
341 
343  : ControlCommand("rib", "register")
344 {
359 }
360 
361 void
363 {
364  if (!parameters.hasFaceId()) {
365  parameters.setFaceId(0);
366  }
367  if (!parameters.hasOrigin()) {
368  parameters.setOrigin(ROUTE_ORIGIN_APP);
369  }
370  if (!parameters.hasCost()) {
371  parameters.setCost(0);
372  }
373  if (!parameters.hasFlags()) {
375  }
376 }
377 
378 void
380 {
381  this->ControlCommand::validateResponse(parameters);
382 
383  if (parameters.getFaceId() == INVALID_FACE_ID) {
384  NDN_THROW(ArgumentError("FaceId must be valid"));
385  }
386 }
387 
389  : ControlCommand("rib", "unregister")
390 {
399 }
400 
401 void
403 {
404  if (!parameters.hasFaceId()) {
405  parameters.setFaceId(0);
406  }
407  if (!parameters.hasOrigin()) {
408  parameters.setOrigin(ROUTE_ORIGIN_APP);
409  }
410 }
411 
412 void
414 {
415  this->ControlCommand::validateResponse(parameters);
416 
417  if (parameters.getFaceId() == INVALID_FACE_ID) {
418  NDN_THROW(ArgumentError("FaceId must be valid"));
419  }
420 }
421 
422 } // namespace nfd
423 } // namespace ndn
Represents an absolute name.
Definition: name.hpp:44
bool empty() const noexcept
Checks if the name is empty, i.e., has no components.
Definition: name.hpp:146
Represents an error in ControlParameters.
FieldValidator & required(ControlParameterField field)
Declare a required field.
FieldValidator & optional(ControlParameterField field)
Declare an optional field.
void validate(const ControlParameters &parameters) const
Verify that all required fields are present, and all present fields are either required or optional.
Base class of NFD ControlCommand.
ControlCommand(const std::string &module, const std::string &verb)
Name getRequestName(const Name &commandPrefix, const ControlParameters &parameters) const
Construct the Name for a request Interest.
virtual void validateResponse(const ControlParameters &parameters) const
Validate response parameters.
virtual void applyDefaultsToRequest(ControlParameters &parameters) const
Apply default values to missing fields in request.
virtual void validateRequest(const ControlParameters &parameters) const
Validate request parameters.
virtual void applyDefaultsToResponse(ControlParameters &parameters) const
Apply default values to missing fields in response.
FieldValidator m_responseValidator
FieldValidator for response ControlParameters.
FieldValidator m_requestValidator
FieldValidator for request ControlParameters.
Represents parameters in a ControlCommand request or response.
ControlParameters & setOrigin(RouteOrigin origin)
ControlParameters & setCost(uint64_t cost)
const std::vector< bool > & getPresentFields() const
ControlParameters & setFacePersistency(FacePersistency persistency)
ControlParameters & setFlags(uint64_t flags)
size_t wireEncode(EncodingImpl< TAG > &encoder) const
ControlParameters & setFaceId(uint64_t faceId)
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 applyDefaultsToRequest(ControlParameters &parameters) const override
Apply default values to missing fields in request.
void validateRequest(const ControlParameters &parameters) const override
Validate request parameters.
void validateResponse(const ControlParameters &parameters) const override
Validate response parameters.
void applyDefaultsToRequest(ControlParameters &parameters) const override
Apply default values to missing fields in request.
void validateResponse(const ControlParameters &parameters) const override
void applyDefaultsToRequest(ControlParameters &parameters) const override
Apply default values to missing fields in request.
void validateResponse(const ControlParameters &parameters) const override
Validate response parameters.
void applyDefaultsToRequest(ControlParameters &parameters) const override
Apply default values to missing fields in request.
void validateResponse(const ControlParameters &parameters) const override
Validate response parameters.
void applyDefaultsToRequest(ControlParameters &parameters) const override
Apply default values to missing fields in request.
void validateResponse(const ControlParameters &parameters) const override
Validate response parameters.
void validateResponse(const ControlParameters &parameters) const override
Validate response parameters.
void applyDefaultsToRequest(ControlParameters &parameters) const override
Apply default values to missing fields in request.
void validateRequest(const ControlParameters &parameters) const override
Validate request parameters.
void validateResponse(const ControlParameters &parameters) const override
Validate response parameters.
#define NDN_THROW(e)
Definition: exception.hpp:61
@ FACE_PERSISTENCY_PERSISTENT
face is persistent
@ ROUTE_FLAG_CHILD_INHERIT
@ CONTROL_PARAMETER_FACE_PERSISTENCY
@ CONTROL_PARAMETER_DEFAULT_CONGESTION_THRESHOLD
@ CONTROL_PARAMETER_EXPIRATION_PERIOD
@ CONTROL_PARAMETER_BASE_CONGESTION_MARKING_INTERVAL
const uint64_t INVALID_FACE_ID
const std::string CONTROL_PARAMETER_FIELD[CONTROL_PARAMETER_UBOUND]
@ Name
Definition: tlv.hpp:71
Definition: data.cpp:25