ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
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-2025 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
24namespace ndn::nfd {
25
26void
28{
29 auto presentFields = parameters.getPresentFields();
30 BOOST_ASSERT(presentFields.size() == m_required.size() &&
31 presentFields.size() == m_optional.size());
32
33 for (size_t i = 0; i < presentFields.size(); ++i) {
34 bool isPresent = presentFields[i];
35 if (m_required[i]) {
36 if (!isPresent) {
37 NDN_THROW(ArgumentError(std::string(CONTROL_PARAMETER_FIELD[i]) + " is required but missing"));
38 }
39 }
40 else if (isPresent && !m_optional[i]) {
41 NDN_THROW(ArgumentError(std::string(CONTROL_PARAMETER_FIELD[i]) + " is forbidden but present"));
42 }
43 }
44
45 if (m_optional[CONTROL_PARAMETER_FLAGS] && m_optional[CONTROL_PARAMETER_MASK]) {
46 if (parameters.hasFlags() != parameters.hasMask()) {
47 NDN_THROW(ArgumentError("Flags must be accompanied by Mask"));
48 }
49 }
50}
51
52shared_ptr<ControlParameters>
53ControlParametersCommandFormat::decode(const Interest& interest, size_t prefixLen)
54{
55 auto block = interest.getName().at(prefixLen).blockFromValue();
56 return make_shared<ControlParameters>(block);
57}
58
59void
61{
62 auto name = interest.getName();
63 name.append(params.wireEncode());
64 interest.setName(name);
65}
66
67const FaceCreateCommand::RequestFormat FaceCreateCommand::s_requestFormat =
68 RequestFormat()
77const FaceCreateCommand::ResponseFormat FaceCreateCommand::s_responseFormat =
78 ResponseFormat()
87
88void
89FaceCreateCommand::applyDefaultsToRequestImpl(ControlParameters& parameters)
90{
91 if (!parameters.hasFacePersistency()) {
93 }
94}
95
96void
97FaceCreateCommand::validateResponseImpl(const ControlParameters& parameters)
98{
99 if (parameters.getFaceId() == INVALID_FACE_ID) {
100 NDN_THROW(ArgumentError("FaceId must be valid"));
101 }
102}
103
104const FaceUpdateCommand::RequestFormat FaceUpdateCommand::s_requestFormat =
105 RequestFormat()
113const FaceUpdateCommand::ResponseFormat FaceUpdateCommand::s_responseFormat =
114 ResponseFormat()
121
122void
123FaceUpdateCommand::applyDefaultsToRequestImpl(ControlParameters& parameters)
124{
125 if (!parameters.hasFaceId()) {
126 parameters.setFaceId(0);
127 }
128}
129
130void
131FaceUpdateCommand::validateResponseImpl(const ControlParameters& parameters)
132{
133 if (parameters.getFaceId() == INVALID_FACE_ID) {
134 NDN_THROW(ArgumentError("FaceId must be valid"));
135 }
136}
137
138const FaceDestroyCommand::RequestFormat FaceDestroyCommand::s_requestFormat =
139 RequestFormat()
141const FaceDestroyCommand::ResponseFormat FaceDestroyCommand::s_responseFormat =
142 ResponseFormat()
144
145void
146FaceDestroyCommand::validateRequestImpl(const ControlParameters& parameters)
147{
148 if (parameters.getFaceId() == INVALID_FACE_ID) {
149 NDN_THROW(ArgumentError("FaceId must be valid"));
150 }
151}
152
153void
154FaceDestroyCommand::validateResponseImpl(const ControlParameters& parameters)
155{
156 validateRequestImpl(parameters);
157}
158
159const FibAddNextHopCommand::RequestFormat FibAddNextHopCommand::s_requestFormat =
160 RequestFormat()
164const FibAddNextHopCommand::ResponseFormat FibAddNextHopCommand::s_responseFormat =
165 ResponseFormat()
169
170void
171FibAddNextHopCommand::applyDefaultsToRequestImpl(ControlParameters& parameters)
172{
173 if (!parameters.hasFaceId()) {
174 parameters.setFaceId(0);
175 }
176 if (!parameters.hasCost()) {
177 parameters.setCost(0);
178 }
179}
180
181void
182FibAddNextHopCommand::validateResponseImpl(const ControlParameters& parameters)
183{
184 if (parameters.getFaceId() == INVALID_FACE_ID) {
185 NDN_THROW(ArgumentError("FaceId must be valid"));
186 }
187}
188
189const FibRemoveNextHopCommand::RequestFormat FibRemoveNextHopCommand::s_requestFormat =
190 RequestFormat()
193const FibRemoveNextHopCommand::ResponseFormat FibRemoveNextHopCommand::s_responseFormat =
194 ResponseFormat()
197
198void
199FibRemoveNextHopCommand::applyDefaultsToRequestImpl(ControlParameters& parameters)
200{
201 if (!parameters.hasFaceId()) {
202 parameters.setFaceId(0);
203 }
204}
205
206void
207FibRemoveNextHopCommand::validateResponseImpl(const ControlParameters& parameters)
208{
209 if (parameters.getFaceId() == INVALID_FACE_ID) {
210 NDN_THROW(ArgumentError("FaceId must be valid"));
211 }
212}
213
214const CsConfigCommand::RequestFormat CsConfigCommand::s_requestFormat =
215 RequestFormat()
219const CsConfigCommand::ResponseFormat CsConfigCommand::s_responseFormat =
220 ResponseFormat()
223
224const CsEraseCommand::RequestFormat CsEraseCommand::s_requestFormat =
225 RequestFormat()
228const CsEraseCommand::ResponseFormat CsEraseCommand::s_responseFormat =
229 ResponseFormat()
233
234void
235CsEraseCommand::validateRequestImpl(const ControlParameters& parameters)
236{
237 if (parameters.hasCount() && parameters.getCount() == 0) {
238 NDN_THROW(ArgumentError("Count must be positive"));
239 }
240}
241
242void
243CsEraseCommand::validateResponseImpl(const ControlParameters& parameters)
244{
245 if (parameters.hasCapacity() && parameters.getCapacity() == 0) {
246 NDN_THROW(ArgumentError("Capacity must be positive"));
247 }
248}
249
250const StrategyChoiceSetCommand::RequestFormat StrategyChoiceSetCommand::s_requestFormat =
251 RequestFormat()
254const StrategyChoiceSetCommand::ResponseFormat StrategyChoiceSetCommand::s_responseFormat =
255 ResponseFormat()
258
259const StrategyChoiceUnsetCommand::RequestFormat StrategyChoiceUnsetCommand::s_requestFormat =
260 RequestFormat()
262const StrategyChoiceUnsetCommand::ResponseFormat StrategyChoiceUnsetCommand::s_responseFormat =
263 ResponseFormat()
265
266void
267StrategyChoiceUnsetCommand::validateRequestImpl(const ControlParameters& parameters)
268{
269 if (parameters.getName().empty()) {
270 NDN_THROW(ArgumentError("Name must not be ndn:/"));
271 }
272}
273
274void
275StrategyChoiceUnsetCommand::validateResponseImpl(const ControlParameters& parameters)
276{
277 validateRequestImpl(parameters);
278}
279
280const RibRegisterCommand::RequestFormat RibRegisterCommand::s_requestFormat =
281 RequestFormat()
288const RibRegisterCommand::ResponseFormat RibRegisterCommand::s_responseFormat =
289 ResponseFormat()
296
297void
298RibRegisterCommand::applyDefaultsToRequestImpl(ControlParameters& parameters)
299{
300 if (!parameters.hasFaceId()) {
301 parameters.setFaceId(0);
302 }
303 if (!parameters.hasOrigin()) {
304 parameters.setOrigin(ROUTE_ORIGIN_APP);
305 }
306 if (!parameters.hasCost()) {
307 parameters.setCost(0);
308 }
309 if (!parameters.hasFlags()) {
310 parameters.setFlags(ROUTE_FLAG_CHILD_INHERIT);
311 }
312}
313
314void
315RibRegisterCommand::validateResponseImpl(const ControlParameters& parameters)
316{
317 if (parameters.getFaceId() == INVALID_FACE_ID) {
318 NDN_THROW(ArgumentError("FaceId must be valid"));
319 }
320}
321
322const RibUnregisterCommand::RequestFormat RibUnregisterCommand::s_requestFormat =
323 RequestFormat()
327const RibUnregisterCommand::ResponseFormat RibUnregisterCommand::s_responseFormat =
328 ResponseFormat()
332
333void
334RibUnregisterCommand::applyDefaultsToRequestImpl(ControlParameters& parameters)
335{
336 if (!parameters.hasFaceId()) {
337 parameters.setFaceId(0);
338 }
339 if (!parameters.hasOrigin()) {
340 parameters.setOrigin(ROUTE_ORIGIN_APP);
341 }
342}
343
344void
345RibUnregisterCommand::validateResponseImpl(const ControlParameters& parameters)
346{
347 if (parameters.getFaceId() == INVALID_FACE_ID) {
348 NDN_THROW(ArgumentError("FaceId must be valid"));
349 }
350}
351
352void
354{
355 wire.parse();
356 auto it = wire.find(tlv::Data);
357 if (it == wire.elements_end()) {
358 NDN_THROW(Error("Missing prefix announcement parameter"));
359 }
360 m_prefixAnn = PrefixAnnouncement(Data(*it));
361}
362
363Block
365{
366 if (!m_prefixAnn.getData()) {
367 NDN_THROW(Error("Prefix announcement must be signed"));
368 }
369 return m_prefixAnn.getData()->wireEncode();
370}
371
372const RibAnnounceCommand::RequestFormat RibAnnounceCommand::s_requestFormat;
373const RibAnnounceCommand::ResponseFormat RibAnnounceCommand::s_responseFormat =
374 RibRegisterCommand::s_responseFormat;
375
376void
377RibAnnounceCommand::validateRequestImpl(const RibAnnounceParameters& parameters)
378{
379 if (!parameters.getPrefixAnnouncement().getData()) {
380 NDN_THROW(ArgumentError("Prefix announcement must be signed"));
381 }
382}
383
384void
385RibAnnounceCommand::validateResponseImpl(const ControlParameters& parameters)
386{
387 RibRegisterCommand::validateResponseImpl(parameters);
388}
389
390} // namespace ndn::nfd
Represents a TLV element of the NDN packet format.
Definition block.hpp:45
element_const_iterator find(uint32_t type) const
Find the first sub-element of the specified TLV-TYPE.
Definition block.cpp:425
Block blockFromValue() const
Return a new Block constructed from the TLV-VALUE of this Block.
Definition block.cpp:314
element_const_iterator elements_end() const noexcept
Equivalent to elements().end().
Definition block.hpp:442
void parse() const
Parse TLV-VALUE into sub-elements.
Definition block.cpp:326
Represents a Data packet.
Definition data.hpp:39
Represents an Interest packet.
Definition interest.hpp:50
const Name & getName() const noexcept
Get the Interest name.
Definition interest.hpp:179
Interest & setName(const Name &name)
Set the Interest name.
Definition interest.cpp:354
Name & append(const Component &component)
Append a name component.
Definition name.hpp:308
const Component & at(ssize_t i) const
Returns an immutable reference to the component at the specified index, with bounds checking.
Definition name.cpp:146
A prefix announcement object that represents an application's intent of registering a prefix toward i...
const std::optional< Data > & getData() const
Get the Data representing the prefix announcement, if available.
Implements decoding, encoding, and validation of control command parameters carried in the Applicatio...
Represents an error in the parameters of the control command request or response.
ControlParametersCommandFormat ResponseFormat
Implements decoding, encoding, and validation of ControlParameters in control commands.
ControlParametersCommandFormat & required(ControlParameterField field)
Declare a required field.
static shared_ptr< ControlParameters > decode(const Interest &interest, size_t prefixLen)
Extract the parameters from the request interest.
void validate(const ControlParameters &params) const
Verify that all required fields are present, and all present fields are either required or optional.
static void encode(Interest &interest, const ControlParameters &params)
Serialize the parameters into the request interest.
ControlParametersCommandFormat & optional(ControlParameterField field)
Declare an optional field.
Represents parameters in a ControlCommand request or response.
ControlParameters & setFacePersistency(FacePersistency persistency)
auto getPresentFields() const noexcept
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Request parameters for rib/announce command.
const PrefixAnnouncement & getPrefixAnnouncement() const
void wireDecode(const Block &wire) final
#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::array CONTROL_PARAMETER_FIELD
@ Data
Definition tlv.hpp:69