Skip to content

Commit

Permalink
mgmt: adapt to API changes in ndn-cxx ControlCommand and Dispatcher
Browse files Browse the repository at this point in the history
Change-Id: I7515353444366d0a36fe962deeae70ef567334ac
  • Loading branch information
Pesa committed Jan 3, 2025
1 parent 401d1a4 commit 45c1f6a
Show file tree
Hide file tree
Showing 22 changed files with 142 additions and 193 deletions.
4 changes: 2 additions & 2 deletions daemon/mgmt/command-authenticator.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2024, Regents of the University of California,
* Copyright (c) 2014-2025, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
Expand Down Expand Up @@ -213,7 +213,7 @@ CommandAuthenticator::makeAuthorization(const std::string& module, const std::st
m_validators[module]; // declares module, so that privilege is recognized

return [module, self = shared_from_this()] (const Name&, const Interest& interest,
const ndn::mgmt::ControlParameters*,
const ndn::mgmt::ControlParametersBase*,
const ndn::mgmt::AcceptContinuation& accept,
const ndn::mgmt::RejectContinuation& reject) {
auto validator = self->m_validators.at(module);
Expand Down
10 changes: 5 additions & 5 deletions daemon/mgmt/cs-manager.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2024, Regents of the University of California,
* Copyright (c) 2014-2025, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
Expand Down Expand Up @@ -40,16 +40,16 @@ CsManager::CsManager(Cs& cs, const ForwarderCounters& fwCounters,
, m_fwCounters(fwCounters)
{
registerCommandHandler<ndn::nfd::CsConfigCommand>("config",
[this] (auto&&, auto&&, auto&&, auto&&... args) { changeConfig(std::forward<decltype(args)>(args)...); });
[this] (auto&&, auto&&, auto&&... args) { changeConfig(std::forward<decltype(args)>(args)...); });
registerCommandHandler<ndn::nfd::CsEraseCommand>("erase",
[this] (auto&&, auto&&, auto&&, auto&&... args) { erase(std::forward<decltype(args)>(args)...); });
[this] (auto&&, auto&&, auto&&... args) { erase(std::forward<decltype(args)>(args)...); });
registerStatusDatasetHandler("info",
[this] (auto&&, auto&&, auto&&... args) { serveInfo(std::forward<decltype(args)>(args)...); });
}

void
CsManager::changeConfig(const ControlParameters& parameters,
const ndn::mgmt::CommandContinuation& done)
const CommandContinuation& done)
{
using ndn::nfd::CsFlagBit;

Expand All @@ -74,7 +74,7 @@ CsManager::changeConfig(const ControlParameters& parameters,

void
CsManager::erase(const ControlParameters& parameters,
const ndn::mgmt::CommandContinuation& done)
const CommandContinuation& done)
{
size_t count = parameters.hasCount() ?
parameters.getCount() :
Expand Down
17 changes: 9 additions & 8 deletions daemon/mgmt/cs-manager.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2023, Regents of the University of California,
* Copyright (c) 2014-2025, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
Expand Down Expand Up @@ -47,19 +47,20 @@ class CsManager final : public ManagerBase
Dispatcher& dispatcher, CommandAuthenticator& authenticator);

private:
/** \brief Process cs/config command.
/**
* \brief Process `cs/config` command.
*/
void
changeConfig(const ControlParameters& parameters,
const ndn::mgmt::CommandContinuation& done);
changeConfig(const ControlParameters& parameters, const CommandContinuation& done);

/** \brief Process cs/erase command.
/**
* \brief Process `cs/erase` command.
*/
void
erase(const ControlParameters& parameters,
const ndn::mgmt::CommandContinuation& done);
erase(const ControlParameters& parameters, const CommandContinuation& done);

/** \brief Serve CS information dataset.
/**
* \brief Serve CS information dataset.
*/
void
serveInfo(ndn::mgmt::StatusDatasetContext& context) const;
Expand Down
16 changes: 8 additions & 8 deletions daemon/mgmt/face-manager.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2024, Regents of the University of California,
* Copyright (c) 2014-2025, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
Expand Down Expand Up @@ -50,11 +50,11 @@ FaceManager::FaceManager(FaceSystem& faceSystem,
{
// register handlers for ControlCommand
registerCommandHandler<ndn::nfd::FaceCreateCommand>("create",
[this] (auto&&, auto&&, auto&&, auto&&... args) { createFace(std::forward<decltype(args)>(args)...); });
[this] (auto&&, auto&&, auto&&... args) { createFace(std::forward<decltype(args)>(args)...); });
registerCommandHandler<ndn::nfd::FaceUpdateCommand>("update",
[this] (auto&&, auto&&, auto&&... args) { updateFace(std::forward<decltype(args)>(args)...); });
[this] (auto&&, auto&&... args) { updateFace(std::forward<decltype(args)>(args)...); });
registerCommandHandler<ndn::nfd::FaceDestroyCommand>("destroy",
[this] (auto&&, auto&&, auto&&, auto&&... args) { destroyFace(std::forward<decltype(args)>(args)...); });
[this] (auto&&, auto&&, auto&&... args) { destroyFace(std::forward<decltype(args)>(args)...); });

// register handlers for StatusDataset
registerStatusDatasetHandler("list",
Expand All @@ -77,7 +77,7 @@ FaceManager::FaceManager(FaceSystem& faceSystem,

void
FaceManager::createFace(const ControlParameters& parameters,
const ndn::mgmt::CommandContinuation& done)
const CommandContinuation& done)
{
FaceUri remoteUri;
if (!remoteUri.parse(parameters.getUri())) {
Expand Down Expand Up @@ -203,7 +203,7 @@ makeCreateFaceResponse(const Face& face)
void
FaceManager::afterCreateFaceSuccess(const shared_ptr<Face>& face,
const ControlParameters& parameters,
const ndn::mgmt::CommandContinuation& done)
const CommandContinuation& done)
{
if (face->getId() != face::INVALID_FACEID) { // Face already exists
NFD_LOG_TRACE("Attempted to create duplicate face of " << face->getId());
Expand Down Expand Up @@ -262,7 +262,7 @@ updateLinkServiceOptions(Face& face, const ControlParameters& parameters)
void
FaceManager::updateFace(const Interest& interest,
const ControlParameters& parameters,
const ndn::mgmt::CommandContinuation& done)
const CommandContinuation& done)
{
FaceId faceId = parameters.getFaceId();
if (faceId == face::INVALID_FACEID) { // Self-update
Expand Down Expand Up @@ -336,7 +336,7 @@ FaceManager::updateFace(const Interest& interest,

void
FaceManager::destroyFace(const ControlParameters& parameters,
const ndn::mgmt::CommandContinuation& done)
const CommandContinuation& done)
{
Face* face = m_faceTable.get(parameters.getFaceId());
if (face != nullptr) {
Expand Down
10 changes: 5 additions & 5 deletions daemon/mgmt/face-manager.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2024, Regents of the University of California,
* Copyright (c) 2014-2025, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
Expand Down Expand Up @@ -47,22 +47,22 @@ class FaceManager final : public ManagerBase
private: // ControlCommand
void
createFace(const ControlParameters& parameters,
const ndn::mgmt::CommandContinuation& done);
const CommandContinuation& done);

void
updateFace(const Interest& interest,
const ControlParameters& parameters,
const ndn::mgmt::CommandContinuation& done);
const CommandContinuation& done);

void
destroyFace(const ControlParameters& parameters,
const ndn::mgmt::CommandContinuation& done);
const CommandContinuation& done);

private: // helpers for ControlCommand
void
afterCreateFaceSuccess(const shared_ptr<Face>& face,
const ControlParameters& parameters,
const ndn::mgmt::CommandContinuation& done);
const CommandContinuation& done);

private: // StatusDataset
void
Expand Down
10 changes: 5 additions & 5 deletions daemon/mgmt/fib-manager.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2024, Regents of the University of California,
* Copyright (c) 2014-2025, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
Expand Down Expand Up @@ -45,16 +45,16 @@ FibManager::FibManager(Fib& fib, const FaceTable& faceTable,
, m_faceTable(faceTable)
{
registerCommandHandler<ndn::nfd::FibAddNextHopCommand>("add-nexthop",
[this] (auto&&, auto&&, auto&&... args) { addNextHop(std::forward<decltype(args)>(args)...); });
[this] (auto&&, auto&&... args) { addNextHop(std::forward<decltype(args)>(args)...); });
registerCommandHandler<ndn::nfd::FibRemoveNextHopCommand>("remove-nexthop",
[this] (auto&&, auto&&, auto&&... args) { removeNextHop(std::forward<decltype(args)>(args)...); });
[this] (auto&&, auto&&... args) { removeNextHop(std::forward<decltype(args)>(args)...); });
registerStatusDatasetHandler("list",
[this] (auto&&, auto&&, auto&&... args) { listEntries(std::forward<decltype(args)>(args)...); });
}

void
FibManager::addNextHop(const Interest& interest, ControlParameters parameters,
const ndn::mgmt::CommandContinuation& done)
const CommandContinuation& done)
{
setFaceForSelfRegistration(interest, parameters);
const Name& prefix = parameters.getName();
Expand Down Expand Up @@ -84,7 +84,7 @@ FibManager::addNextHop(const Interest& interest, ControlParameters parameters,

void
FibManager::removeNextHop(const Interest& interest, ControlParameters parameters,
const ndn::mgmt::CommandContinuation& done)
const CommandContinuation& done)
{
setFaceForSelfRegistration(interest, parameters);
const Name& prefix = parameters.getName();
Expand Down
6 changes: 3 additions & 3 deletions daemon/mgmt/fib-manager.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2023, Regents of the University of California,
* Copyright (c) 2014-2025, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
Expand Down Expand Up @@ -49,11 +49,11 @@ class FibManager final : public ManagerBase
private:
void
addNextHop(const Interest& interest, ControlParameters parameters,
const ndn::mgmt::CommandContinuation& done);
const CommandContinuation& done);

void
removeNextHop(const Interest& interest, ControlParameters parameters,
const ndn::mgmt::CommandContinuation& done);
const CommandContinuation& done);

void
listEntries(ndn::mgmt::StatusDatasetContext& context);
Expand Down
31 changes: 1 addition & 30 deletions daemon/mgmt/manager-base.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2023, Regents of the University of California,
* Copyright (c) 2014-2025, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
Expand Down Expand Up @@ -82,33 +82,4 @@ ManagerBase::makeAuthorization(const std::string& verb)
return m_authenticator->makeAuthorization(m_module, verb);
}

bool
ManagerBase::validateParameters(const ControlCommand& command,
const ndn::mgmt::ControlParameters& parameters)
{
BOOST_ASSERT(dynamic_cast<const ControlParameters*>(&parameters) != nullptr);

try {
command.validateRequest(static_cast<const ControlParameters&>(parameters));
}
catch (const ControlCommand::ArgumentError&) {
return false;
}
return true;
}

void
ManagerBase::handleCommand(shared_ptr<ControlCommand> command,
const ControlCommandHandler& handler,
const Name& prefix, const Interest& interest,
const ndn::mgmt::ControlParameters& params,
const ndn::mgmt::CommandContinuation& done)
{
BOOST_ASSERT(dynamic_cast<const ControlParameters*>(&params) != nullptr);

ControlParameters parameters = static_cast<const ControlParameters&>(params);
command->applyDefaultsToRequest(parameters);
handler(*command, prefix, interest, parameters, done);
}

} // namespace nfd
65 changes: 29 additions & 36 deletions daemon/mgmt/manager-base.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2024, Regents of the University of California,
* Copyright (c) 2014-2025, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
Expand Down Expand Up @@ -38,7 +38,7 @@
namespace nfd {

using ndn::mgmt::Dispatcher;
using ndn::nfd::ControlCommand;
using ndn::mgmt::CommandContinuation;
using ndn::nfd::ControlParameters;
using ndn::nfd::ControlResponse;

Expand Down Expand Up @@ -76,16 +76,15 @@ class ManagerBase : noncopyable
~ManagerBase();

NFD_PUBLIC_WITH_TESTS_ELSE_PROTECTED: // registrations to the dispatcher
// difference from mgmt::ControlCommand: accepts nfd::ControlParameters
using ControlCommandHandler = std::function<void(const ControlCommand& command,
const Name& prefix, const Interest& interest,
// difference from ndn::mgmt::ControlCommandHandler: accepts nfd::ControlParameters
using ControlCommandHandler = std::function<void(const Name& prefix, const Interest& interest,
const ControlParameters& parameters,
const ndn::mgmt::CommandContinuation done)>;
const CommandContinuation& done)>;

template<typename Command>
void
registerCommandHandler(const std::string& verb,
const ControlCommandHandler& handler);
ControlCommandHandler handler);

void
registerStatusDatasetHandler(const std::string& verb,
Expand All @@ -111,34 +110,14 @@ class ManagerBase : noncopyable
virtual ndn::mgmt::Authorization
makeAuthorization(const std::string& verb);

/**
* @brief Validates the @p parameters for a given @p command.
*
* @param parameters the original ControlParameters
* @return whether the original ControlParameters can be validated
*/
[[nodiscard]] static bool
validateParameters(const ControlCommand& command,
const ndn::mgmt::ControlParameters& parameters);

/**
* @brief Handles a control command.
*/
static void
handleCommand(shared_ptr<ControlCommand> command,
const ControlCommandHandler& handler,
const Name& prefix, const Interest& interest,
const ndn::mgmt::ControlParameters& params,
const ndn::mgmt::CommandContinuation& done);

/**
* @brief Generates the relative prefix for a handler by appending the verb name to the module name.
*
* @param verb the verb name
* @return the generated relative prefix
*/
PartialName
makeRelPrefix(const std::string& verb)
makeRelPrefix(const std::string& verb) const
{
return PartialName(m_module).append(verb);
}
Expand All @@ -151,16 +130,30 @@ class ManagerBase : noncopyable

template<typename Command>
void
ManagerBase::registerCommandHandler(const std::string& verb,
const ControlCommandHandler& handler)
ManagerBase::registerCommandHandler(const std::string& verb, ControlCommandHandler handler)
{
auto command = make_shared<Command>();
auto validate = [] (const ndn::mgmt::ControlParametersBase& params) {
BOOST_ASSERT(dynamic_cast<const ControlParameters*>(&params) != nullptr);
try {
Command::validateRequest(static_cast<const ControlParameters&>(params));
return true;
}
catch (const std::invalid_argument&) {
return false;
}
};

auto handle = [handler = std::move(handler)] (const Name& prefix, const Interest& interest,
const ndn::mgmt::ControlParametersBase& params,
const CommandContinuation& done) {
BOOST_ASSERT(dynamic_cast<const ControlParameters*>(&params) != nullptr);
ControlParameters parameters = static_cast<const ControlParameters&>(params);
Command::applyDefaultsToRequest(parameters);
handler(prefix, interest, parameters, done);
};

m_dispatcher.addControlCommand<ControlParameters>(
makeRelPrefix(verb),
makeAuthorization(verb),
[=] (const auto& params) { return validateParameters(*command, params); },
[=] (auto&&... args) { handleCommand(command, handler, std::forward<decltype(args)>(args)...); });
m_dispatcher.addControlCommand<ControlParameters>(makeRelPrefix(verb), makeAuthorization(verb),
std::move(validate), std::move(handle));
}

} // namespace nfd
Expand Down
Loading

0 comments on commit 45c1f6a

Please sign in to comment.