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-2023 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::nfd {
25 
26 ControlCommand::ControlCommand(const std::string& module, const std::string& verb)
27  : m_module(module)
28  , m_verb(verb)
29 {
30 }
31 
33 
34 void
36 {
37  m_requestValidator.validate(parameters);
38 }
39 
40 void
42 {
43 }
44 
45 void
47 {
48  m_responseValidator.validate(parameters);
49 }
50 
51 void
53 {
54 }
55 
56 Name
57 ControlCommand::getRequestName(const Name& commandPrefix,
58  const ControlParameters& parameters) const
59 {
60  this->validateRequest(parameters);
61 
62  return Name(commandPrefix)
63  .append(m_module)
64  .append(m_verb)
65  .append(parameters.wireEncode());
66 }
67 
69  : m_required(CONTROL_PARAMETER_UBOUND)
70  , m_optional(CONTROL_PARAMETER_UBOUND)
71 {
72 }
73 
74 void
76 {
77  const auto& 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  NDN_THROW(ArgumentError(std::string(CONTROL_PARAMETER_FIELD[i]) + " is required but missing"));
84  }
85  }
86  else if (isPresent && !m_optional[i]) {
87  NDN_THROW(ArgumentError(std::string(CONTROL_PARAMETER_FIELD[i]) + " is forbidden but present"));
88  }
89  }
90 
91  if (m_optional[CONTROL_PARAMETER_FLAGS] && m_optional[CONTROL_PARAMETER_MASK]) {
92  if (parameters.hasFlags() != parameters.hasMask()) {
93  NDN_THROW(ArgumentError("Flags must be accompanied by Mask"));
94  }
95  }
96 }
97 
99  : ControlCommand("faces", "create")
100 {
119 }
120 
121 void
123 {
124  if (!parameters.hasFacePersistency()) {
126  }
127 }
128 
129 void
131 {
132  this->ControlCommand::validateResponse(parameters);
133 
134  if (parameters.getFaceId() == INVALID_FACE_ID) {
135  NDN_THROW(ArgumentError("FaceId must be valid"));
136  }
137 }
138 
140  : ControlCommand("faces", "update")
141 {
157 }
158 
159 void
161 {
162  if (!parameters.hasFaceId()) {
163  parameters.setFaceId(0);
164  }
165 }
166 
167 void
169 {
170  this->ControlCommand::validateResponse(parameters);
171 
172  if (parameters.getFaceId() == INVALID_FACE_ID) {
173  NDN_THROW(ArgumentError("FaceId must be valid"));
174  }
175 }
176 
178  : ControlCommand("faces", "destroy")
179 {
183 }
184 
185 void
187 {
188  this->ControlCommand::validateRequest(parameters);
189 
190  if (parameters.getFaceId() == INVALID_FACE_ID) {
191  NDN_THROW(ArgumentError("FaceId must be valid"));
192  }
193 }
194 
195 void
197 {
198  this->validateRequest(parameters);
199 }
200 
202  : ControlCommand("fib", "add-nexthop")
203 {
212 }
213 
214 void
216 {
217  if (!parameters.hasFaceId()) {
218  parameters.setFaceId(0);
219  }
220  if (!parameters.hasCost()) {
221  parameters.setCost(0);
222  }
223 }
224 
225 void
227 {
228  this->ControlCommand::validateResponse(parameters);
229 
230  if (parameters.getFaceId() == INVALID_FACE_ID) {
231  NDN_THROW(ArgumentError("FaceId must be valid"));
232  }
233 }
234 
236  : ControlCommand("fib", "remove-nexthop")
237 {
244 }
245 
246 void
248 {
249  if (!parameters.hasFaceId()) {
250  parameters.setFaceId(0);
251  }
252 }
253 
254 void
256 {
257  this->ControlCommand::validateResponse(parameters);
258 
259  if (parameters.getFaceId() == INVALID_FACE_ID) {
260  NDN_THROW(ArgumentError("FaceId must be valid"));
261  }
262 }
263 
265  : ControlCommand("cs", "config")
266 {
274 }
275 
277  : ControlCommand("cs", "erase")
278 {
286 }
287 
288 void
290 {
291  this->ControlCommand::validateRequest(parameters);
292 
293  if (parameters.hasCount() && parameters.getCount() == 0) {
294  NDN_THROW(ArgumentError("Count must be positive"));
295  }
296 }
297 
298 void
300 {
301  this->ControlCommand::validateResponse(parameters);
302 
303  if (parameters.hasCapacity() && parameters.getCapacity() == 0) {
304  NDN_THROW(ArgumentError("Capacity must be positive"));
305  }
306 }
307 
309  : ControlCommand("strategy-choice", "set")
310 {
315 }
316 
318  : ControlCommand("strategy-choice", "unset")
319 {
323 }
324 
325 void
327 {
328  this->ControlCommand::validateRequest(parameters);
329 
330  if (parameters.getName().empty()) {
331  NDN_THROW(ArgumentError("Name must not be ndn:/"));
332  }
333 }
334 
335 void
337 {
338  this->validateRequest(parameters);
339 }
340 
342  : ControlCommand("rib", "register")
343 {
358 }
359 
360 void
362 {
363  if (!parameters.hasFaceId()) {
364  parameters.setFaceId(0);
365  }
366  if (!parameters.hasOrigin()) {
367  parameters.setOrigin(ROUTE_ORIGIN_APP);
368  }
369  if (!parameters.hasCost()) {
370  parameters.setCost(0);
371  }
372  if (!parameters.hasFlags()) {
374  }
375 }
376 
377 void
379 {
380  this->ControlCommand::validateResponse(parameters);
381 
382  if (parameters.getFaceId() == INVALID_FACE_ID) {
383  NDN_THROW(ArgumentError("FaceId must be valid"));
384  }
385 }
386 
388  : ControlCommand("rib", "unregister")
389 {
398 }
399 
400 void
402 {
403  if (!parameters.hasFaceId()) {
404  parameters.setFaceId(0);
405  }
406  if (!parameters.hasOrigin()) {
407  parameters.setOrigin(ROUTE_ORIGIN_APP);
408  }
409 }
410 
411 void
413 {
414  this->ControlCommand::validateResponse(parameters);
415 
416  if (parameters.getFaceId() == INVALID_FACE_ID) {
417  NDN_THROW(ArgumentError("FaceId must be valid"));
418  }
419 }
420 
421 } // namespace ndn::nfd
Represents an absolute name.
Definition: name.hpp:45
bool empty() const noexcept
Checks if the name is empty, i.e., has no components.
Definition: name.hpp:171
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:56
@ 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
Contains classes and functions related to the NFD Management protocol.
constexpr uint64_t INVALID_FACE_ID
constexpr std::string_view CONTROL_PARAMETER_FIELD[CONTROL_PARAMETER_UBOUND]
@ Name
Definition: tlv.hpp:71