ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
control-command.hpp
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
22#ifndef NDN_CXX_MGMT_NFD_CONTROL_COMMAND_HPP
23#define NDN_CXX_MGMT_NFD_CONTROL_COMMAND_HPP
24
25#include "ndn-cxx/interest.hpp"
28
29#include <bitset>
30
31namespace ndn::nfd {
32
37class ArgumentError : public std::invalid_argument
38{
39public:
40 using std::invalid_argument::invalid_argument;
41};
42
43
54{
55public:
57
63 {
64 m_required.set(field);
65 return *this;
66 }
67
73 {
74 m_optional.set(field);
75 return *this;
76 }
77
83 void
84 validate(const ControlParameters& params) const;
85
89 static shared_ptr<ControlParameters>
90 decode(const Interest& interest, size_t prefixLen);
91
96 static void
97 encode(Interest& interest, const ControlParameters& params);
98
99private:
100 std::bitset<CONTROL_PARAMETER_UBOUND> m_required;
101 std::bitset<CONTROL_PARAMETER_UBOUND> m_optional;
102};
103
104
111template<typename PT>
113{
114public:
115 using ParametersType = PT;
116
120 void
122 {
123 }
124
128 static shared_ptr<ParametersType>
129 decode(const Interest& interest, size_t prefixLen = 0)
130 {
131 auto params = make_shared<ParametersType>();
132 params->wireDecode(interest.getApplicationParameters());
133 return params;
134 }
135
139 static void
140 encode(Interest& interest, const ParametersType& params)
141 {
142 interest.setApplicationParameters(params.wireEncode());
143 }
144};
145
146
157template<typename Derived,
158 typename RequestFormatType = ControlParametersCommandFormat,
159 typename ResponseFormatType = ControlParametersCommandFormat>
160class ControlCommand : noncopyable
161{
162protected:
164
165public:
166 using RequestFormat = RequestFormatType;
167 using ResponseFormat = ResponseFormatType;
168 using RequestParameters = typename RequestFormat::ParametersType;
169 using ResponseParameters = typename ResponseFormat::ParametersType;
170
171 ControlCommand() = delete;
172
176 static PartialName
178 {
179 return PartialName().append(Derived::module).append(Derived::verb);
180 }
181
186 static Interest
187 createRequest(Name commandPrefix, const RequestParameters& params)
188 {
189 validateRequest(params);
190
191 Interest request(commandPrefix.append(Derived::module).append(Derived::verb));
192 Derived::s_requestFormat.encode(request, params);
193 return request;
194 }
195
199 static shared_ptr<mgmt::ControlParametersBase>
200 parseRequest(const Interest& interest, size_t prefixLen)
201 {
202 // +2 to account for module and verb components
203 return Derived::s_requestFormat.decode(interest, prefixLen + 2);
204 }
205
210 static void
212 {
213 Derived::s_requestFormat.validate(params);
214 Derived::validateRequestImpl(params);
215 }
216
220 static void
222 {
223 Derived::applyDefaultsToRequestImpl(params);
224 }
225
230 static void
232 {
233 Derived::s_responseFormat.validate(params);
234 Derived::validateResponseImpl(params);
235 }
236
240 static void
242 {
243 Derived::applyDefaultsToResponseImpl(params);
244 }
245
246private:
247 static void
248 validateRequestImpl(const RequestParameters&)
249 {
250 }
251
252 static void
253 applyDefaultsToRequestImpl(RequestParameters&)
254 {
255 }
256
257 static void
258 validateResponseImpl(const ResponseParameters&)
259 {
260 }
261
262 static void
263 applyDefaultsToResponseImpl(ResponseParameters&)
264 {
265 }
266};
267
268#define NDN_CXX_CONTROL_COMMAND(module_, verb_) \
269 public: \
270 static inline const ::ndn::name::Component module{std::string_view(module_)}; \
271 static inline const ::ndn::name::Component verb{std::string_view(verb_)}; \
272 private: \
273 static const RequestFormat s_requestFormat; \
274 static const ResponseFormat s_responseFormat; \
275 friend Base
276
277
283class FaceCreateCommand : public ControlCommand<FaceCreateCommand>
284{
285 NDN_CXX_CONTROL_COMMAND("faces", "create");
286
287 static void
288 applyDefaultsToRequestImpl(ControlParameters& parameters);
289
290 static void
291 validateResponseImpl(const ControlParameters& parameters);
292};
293
294
300class FaceUpdateCommand : public ControlCommand<FaceUpdateCommand>
301{
302 NDN_CXX_CONTROL_COMMAND("faces", "update");
303
304 static void
305 applyDefaultsToRequestImpl(ControlParameters& parameters);
306
311 static void
312 validateResponseImpl(const ControlParameters& parameters);
313};
314
315
321class FaceDestroyCommand : public ControlCommand<FaceDestroyCommand>
322{
323 NDN_CXX_CONTROL_COMMAND("faces", "destroy");
324
325 static void
326 validateRequestImpl(const ControlParameters& parameters);
327
328 static void
329 validateResponseImpl(const ControlParameters& parameters);
330};
331
332
338class FibAddNextHopCommand : public ControlCommand<FibAddNextHopCommand>
339{
340 NDN_CXX_CONTROL_COMMAND("fib", "add-nexthop");
341
342 static void
343 applyDefaultsToRequestImpl(ControlParameters& parameters);
344
345 static void
346 validateResponseImpl(const ControlParameters& parameters);
347};
348
349
355class FibRemoveNextHopCommand : public ControlCommand<FibRemoveNextHopCommand>
356{
357 NDN_CXX_CONTROL_COMMAND("fib", "remove-nexthop");
358
359 static void
360 applyDefaultsToRequestImpl(ControlParameters& parameters);
361
362 static void
363 validateResponseImpl(const ControlParameters& parameters);
364};
365
366
372class CsConfigCommand : public ControlCommand<CsConfigCommand>
373{
374 NDN_CXX_CONTROL_COMMAND("cs", "config");
375};
376
377
383class CsEraseCommand : public ControlCommand<CsEraseCommand>
384{
386
387 static void
388 validateRequestImpl(const ControlParameters& parameters);
389
390 static void
391 validateResponseImpl(const ControlParameters& parameters);
392};
393
394
400class StrategyChoiceSetCommand : public ControlCommand<StrategyChoiceSetCommand>
401{
402 NDN_CXX_CONTROL_COMMAND("strategy-choice", "set");
403};
404
405
411class StrategyChoiceUnsetCommand : public ControlCommand<StrategyChoiceUnsetCommand>
412{
413 NDN_CXX_CONTROL_COMMAND("strategy-choice", "unset");
414
415 static void
416 validateRequestImpl(const ControlParameters& parameters);
417
418 static void
419 validateResponseImpl(const ControlParameters& parameters);
420};
421
422
428class RibRegisterCommand : public ControlCommand<RibRegisterCommand>
429{
430 NDN_CXX_CONTROL_COMMAND("rib", "register");
431 friend class RibAnnounceCommand;
432
433 static void
434 applyDefaultsToRequestImpl(ControlParameters& parameters);
435
436 static void
437 validateResponseImpl(const ControlParameters& parameters);
438};
439
440
446class RibUnregisterCommand : public ControlCommand<RibUnregisterCommand>
447{
448 NDN_CXX_CONTROL_COMMAND("rib", "unregister");
449
450 static void
451 applyDefaultsToRequestImpl(ControlParameters& parameters);
452
453 static void
454 validateResponseImpl(const ControlParameters& parameters);
455};
456
457
463{
464public:
465 class Error : public tlv::Error
466 {
467 public:
468 using tlv::Error::Error;
469 };
470
471 const PrefixAnnouncement&
473 {
474 return m_prefixAnn;
475 }
476
479 {
480 m_prefixAnn = pa;
481 return *this;
482 }
483
484 void
485 wireDecode(const Block& wire) final;
486
487 Block
488 wireEncode() const final;
489
490private:
491 PrefixAnnouncement m_prefixAnn;
492};
493
494
502{
503 NDN_CXX_CONTROL_COMMAND("rib", "announce");
504
505 static void
506 validateRequestImpl(const RibAnnounceParameters& parameters);
507
508 static void
509 validateResponseImpl(const ControlParameters& parameters);
510};
511
512} // namespace ndn::nfd
513
514#endif // NDN_CXX_MGMT_NFD_CONTROL_COMMAND_HPP
Represents a TLV element of the NDN packet format.
Definition block.hpp:45
Represents an Interest packet.
Definition interest.hpp:50
Block getApplicationParameters() const
Get the ApplicationParameters element.
Definition interest.hpp:325
Interest & setApplicationParameters(const Block &block)
Set ApplicationParameters from a Block.
Definition interest.cpp:493
Represents an absolute name.
Definition name.hpp:45
Name & append(const Component &component)
Append a name component.
Definition name.hpp:308
A prefix announcement object that represents an application's intent of registering a prefix toward i...
Base class for a struct that contains the parameters for a ControlCommand.
Implements decoding, encoding, and validation of control command parameters carried in the Applicatio...
static shared_ptr< ParametersType > decode(const Interest &interest, size_t prefixLen=0)
Extract the parameters from the request interest.
static void encode(Interest &interest, const ParametersType &params)
Serialize the parameters into the request interest.
void validate(const ParametersType &) const
Does nothing.
Represents an error in the parameters of the control command request or response.
Base class for all NFD control commands.
static shared_ptr< mgmt::ControlParametersBase > parseRequest(const Interest &interest, size_t prefixLen)
Extract parameters from request Interest.
static Interest createRequest(Name commandPrefix, const RequestParameters &params)
Construct request Interest.
static void validateRequest(const RequestParameters &params)
Validate request parameters.
static void applyDefaultsToRequest(RequestParameters &params)
Apply default values to missing fields in request.
static void applyDefaultsToResponse(ResponseParameters &params)
Apply default values to missing fields in response.
typename ResponseFormat::ParametersType ResponseParameters
ResponseFormatType ResponseFormat
static PartialName getName()
Return the command name (module + verb).
typename RequestFormat::ParametersType RequestParameters
static void validateResponse(const ResponseParameters &params)
Validate response parameters.
RequestFormatType RequestFormat
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.
Represents a cs/config command.
Represents a cs/erase command.
Represents a faces/create command.
Represents a faces/destroy command.
Represents a faces/update command.
Represents a fib/add-nexthop command.
Represents a fib/remove-nexthop command.
Represents a rib/announce command.
Request parameters for rib/announce command.
const PrefixAnnouncement & getPrefixAnnouncement() const
void wireDecode(const Block &wire) final
RibAnnounceParameters & setPrefixAnnouncement(const PrefixAnnouncement &pa)
Represents a rib/register command.
Represents a rib/unregister command.
Represents a strategy-choice/set command.
Represents a strategy-choice/unset command.
Represents an error in TLV encoding or decoding.
Definition tlv.hpp:54
Error(const char *expectedType, uint32_t actualType)
Definition tlv.cpp:28
#define NDN_CXX_CONTROL_COMMAND(module_, verb_)
Contains classes and functions related to the NFD Management protocol.
Name PartialName
Represents an arbitrary sequence of name components.
Definition name.hpp:38