rib: remote prefix registration

Change-Id: I0ee01317c213380481eed5c3a13cc19fb0b897ee
Refs: #2056
This commit is contained in:
Yanbiao Li committed 2014-11-26 21:24:58 -08:00
1 parent 10783f221d
commit c17de8379b
10 files changed
+1265 -8

No files matched your search

+11
View File
@@ -287,4 +287,15 @@ rib
; ; file-name keys/ndn-testbed.ndncert
; ; }
; }
remote_register
{
cost 15 ; forwarding cost of prefix registered on remote router
timeout 10000 ; timeout (in milliseconds) of remote prefix registration command
retry 0 ; maximum number of retries for each remote prefix registration command
refresh_interval 300 ; interval (in seconds) before refreshing the registration
; This setting should be less than face_system.udp.idle_time,
; so that the face is kept alive on the remote router.
}
}
+423
View File
@@ -0,0 +1,423 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2014, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
* Washington University in St. Louis,
* Beijing Institute of Technology,
* The University of Memphis
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
*
* NFD is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/
#include "remote-registrator.hpp"
#include "core/logger.hpp"
#include "core/scheduler.hpp"
namespace nfd {
namespace rib {
NFD_LOG_INIT("RemoteRegistrator");
using ndn::nfd::ControlParameters;
using ndn::nfd::CommandOptions;
const Name RemoteRegistrator::RM_LOCAL_PREFIX = "/localhost";
const Name RemoteRegistrator::RM_HUB_PREFIX = "/localhop/nfd";
const name::Component RemoteRegistrator::RM_IGNORE_COMMPONENT("rib");
RemoteRegistrator::RemoteRegistrator(ndn::nfd::Controller& controller,
ndn::KeyChain& keyChain,
Rib& rib)
: m_nfdController(controller)
, m_keyChain(keyChain)
, m_rib(rib)
, m_refreshInterval(time::seconds(25))
, m_hasConnectedHub(false)
, m_nRetries(0)
{
}
RemoteRegistrator::~RemoteRegistrator()
{
// cancel all periodically refresh events.
for (auto&& entry : m_regEntries)
{
scheduler::cancel(entry.second);
}
}
void
RemoteRegistrator::loadConfig(const ConfigSection& configSection)
{
size_t cost = 15, timeout = 10000;
size_t retry = 0;
size_t interval = 0;
const size_t intervalDef = 25, intervalMax = 600;
NFD_LOG_INFO("Load remote_register section in rib section");
for (auto&& i : configSection)
{
if (i.first == "cost")
{
cost = i.second.get_value<size_t>();
}
else if (i.first == "timeout")
{
timeout = i.second.get_value<size_t>();
}
else if (i.first == "retry")
{
retry = i.second.get_value<size_t>();
}
else if (i.first == "refresh_interval")
{
interval = i.second.get_value<size_t>();
}
else
{
throw ConfigFile::Error("Unrecognized option \"" + i.first +
"\" in \"remote-registrator\" section");
}
}
m_controlParameters
.setCost(cost)
.setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT)// set origin to client.
.setFaceId(0);// the remote hub will take the input face as the faceId.
m_commandOptions
.setPrefix(RM_HUB_PREFIX)
.setTimeout(time::milliseconds(timeout));
m_nRetries = retry;
if (interval == 0)
{
interval = intervalDef;
}
interval = std::min(interval, intervalMax);
m_refreshInterval = time::seconds(interval);
}
void
RemoteRegistrator::registerPrefix(const Name& prefix)
{
if (RM_LOCAL_PREFIX.isPrefixOf(prefix))
{
NFD_LOG_INFO("local registration only for " << prefix);
return;
}
bool isHubPrefix = prefix == RM_HUB_PREFIX;
if (isHubPrefix)
{
NFD_LOG_INFO("this is a prefix registered by some hub: " << prefix);
m_hasConnectedHub = true;
redoRegistration();
return;
}
if (!m_hasConnectedHub)
{
NFD_LOG_INFO("no hub connected when registering " << prefix);
return;
}
std::pair<Name, size_t> identity = findIdentityForRegistration(prefix);
if (0 == identity.second)
{
NFD_LOG_INFO("no proper identity found for registering " << prefix);
return;
}
Name prefixForRegistration;
if (identity.first.size() == identity.second)
{
prefixForRegistration = identity.first;
}
else
{
prefixForRegistration = identity.first.getPrefix(-1);
}
if (m_regEntries.find(prefixForRegistration) != m_regEntries.end())
{
NFD_LOG_INFO("registration already in process for " << prefix);
return;
}
// make copies of m_controlParameters and m_commandOptions to
// avoid unreasonable overwriting during concurrent registration
// and unregistration.
ControlParameters parameters = m_controlParameters;
CommandOptions options = m_commandOptions;
startRegistration(parameters.setName(prefixForRegistration),
options.setSigningIdentity(identity.first),
m_nRetries);
}
void
RemoteRegistrator::unregisterPrefix(const Name& prefix)
{
if (prefix == RM_HUB_PREFIX)
{
NFD_LOG_INFO("disconnected to hub with prefix: " << prefix);
// for phase 1: suppose there is at most one hub connected.
// if the hub prefix has been unregistered locally, there may
// be no connected hub.
m_hasConnectedHub = false;
clearRefreshEvents();
return;
}
if (!m_hasConnectedHub)
{
NFD_LOG_INFO("no hub connected when unregistering " << prefix);
return;
}
std::pair<Name, size_t> identity = findIdentityForRegistration(prefix);
if (0 == identity.second)
{
NFD_LOG_INFO("no proper identity found for unregistering " << prefix);
return;
}
Name prefixForRegistration;
if (identity.first.size() == identity.second)
{
prefixForRegistration = identity.first;
}
else
{
prefixForRegistration = identity.first.getPrefix(-1);
}
RegisteredEntryIt iRegEntry = m_regEntries.find(prefixForRegistration);
if (m_regEntries.end() == iRegEntry)
{
NFD_LOG_INFO("no existing entry found when unregistering " << prefix);
return;
}
for (auto&& entry : m_rib)
{
if (prefixForRegistration.isPrefixOf(entry.first) &&
findIdentityForRegistration(entry.first) == identity)
{
NFD_LOG_INFO("this identity should be kept for other rib entry: "
<< entry.first);
return;
}
}
scheduler::cancel(iRegEntry->second);
m_regEntries.erase(iRegEntry);
// make copies of m_controlParameters and m_commandOptions to
// avoid unreasonable overwriting during concurrent registration
// and unregistration.
ControlParameters parameters = m_controlParameters;
CommandOptions options = m_commandOptions;
startUnregistration(parameters.setName(prefixForRegistration).unsetCost(),
options.setSigningIdentity(identity.first),
m_nRetries);
}
std::pair<Name, size_t>
RemoteRegistrator::findIdentityForRegistration(const Name& prefix)
{
std::pair<Name, size_t> candidateIdentity;
std::vector<Name> identities;
bool isPrefix = false;
size_t maxLength = 0, curLength = 0;
// get all identies from the key-cahin except the default one.
m_keyChain.getAllIdentities(identities, false);
// get the default identity.
identities.push_back(m_keyChain.getDefaultIdentity());
// longest prefix matching to all indenties.
for (auto&& i : identities)
{
if (!i.empty() && RM_IGNORE_COMMPONENT == i.at(-1))
{
isPrefix = i.getPrefix(-1).isPrefixOf(prefix);
curLength = i.size() - 1;
}
else
{
isPrefix = i.isPrefixOf(prefix);
curLength = i.size();
}
if (isPrefix && curLength > maxLength)
{
candidateIdentity.first = i;
maxLength = curLength;
}
}
candidateIdentity.second = maxLength;
return candidateIdentity;
}
void
RemoteRegistrator::startRegistration(const ControlParameters& parameters,
const CommandOptions& options,
int nRetries)
{
NFD_LOG_INFO("start register " << parameters.getName());
m_nfdController.start<ndn::nfd::RibRegisterCommand>(
parameters,
bind(&RemoteRegistrator::onRegSuccess,
this, parameters, options),
bind(&RemoteRegistrator::onRegFailure,
this, _1, _2, parameters, options, nRetries),
options);
}
void
RemoteRegistrator::startUnregistration(const ControlParameters& parameters,
const CommandOptions& options,
int nRetries)
{
NFD_LOG_INFO("start unregister " << parameters.getName());
m_nfdController.start<ndn::nfd::RibUnregisterCommand>(
parameters,
bind(&RemoteRegistrator::onUnregSuccess,
this, parameters, options),
bind(&RemoteRegistrator::onUnregFailure,
this, _1, _2, parameters, options, nRetries),
options);
}
void
RemoteRegistrator::onRegSuccess(const ControlParameters& parameters,
const CommandOptions& options)
{
NFD_LOG_INFO("success to register " << parameters.getName());
RegisteredEntryIt iRegEntry = m_regEntries.find(parameters.getName());
if (m_regEntries.end() != iRegEntry)
{
NFD_LOG_DEBUG("Existing Entry: (" << iRegEntry->first
<< ", " << iRegEntry->second
<< ")");
scheduler::cancel(iRegEntry->second);
iRegEntry->second = scheduler::schedule(
m_refreshInterval,
bind(&RemoteRegistrator::startRegistration,
this, parameters, options, m_nRetries));
}
else
{
NFD_LOG_DEBUG("New Entry");
m_regEntries.insert(RegisteredEntry(
parameters.getName(),
scheduler::schedule(
m_refreshInterval,
bind(&RemoteRegistrator::startRegistration,
this, parameters, options, m_nRetries))));
}
}
void
RemoteRegistrator::onRegFailure(uint32_t code, const std::string& reason,
const ControlParameters& parameters,
const CommandOptions& options,
int nRetries)
{
NFD_LOG_INFO("fail to unregister " << parameters.getName()
<< "\n\t reason:" << reason
<< "\n\t remain retries:" << nRetries);
if (nRetries > 0)
{
startRegistration(parameters, options, nRetries - 1);
}
}
void
RemoteRegistrator::onUnregSuccess(const ControlParameters& parameters,
const CommandOptions& options)
{
NFD_LOG_INFO("success to unregister " << parameters.getName());
}
void
RemoteRegistrator::onUnregFailure(uint32_t code, const std::string& reason,
const ControlParameters& parameters,
const CommandOptions& options,
int nRetries)
{
NFD_LOG_INFO("fail to unregister " << parameters.getName()
<< "\n\t reason:" << reason
<< "\n\t remain retries:" << nRetries);
if (nRetries > 0)
{
startUnregistration(parameters, options, nRetries - 1);
}
}
void
RemoteRegistrator::redoRegistration()
{
NFD_LOG_INFO("redo " << m_regEntries.size()
<< " registration when new Hub connection is built.");
for (auto&& entry : m_regEntries)
{
// make copies to avoid unreasonable overwrite.
ControlParameters parameters = m_controlParameters;
CommandOptions options = m_commandOptions;
startRegistration(parameters.setName(entry.first),
options.setSigningIdentity(entry.first),
m_nRetries);
}
}
void
RemoteRegistrator::clearRefreshEvents()
{
for (auto&& entry : m_regEntries)
{
scheduler::cancel(entry.second);
}
}
} // namespace rib
} // namespace nfd
+235
View File
@@ -0,0 +1,235 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2014, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
* Washington University in St. Louis,
* Beijing Institute of Technology,
* The University of Memphis
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
*
* NFD is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef NFD_RIB_REMOTE_REGISTRATOR_HPP
#define NFD_RIB_REMOTE_REGISTRATOR_HPP
#include "rib.hpp"
#include "core/config-file.hpp"
#include "rib-status-publisher.hpp"
#include <unordered_map>
#include <ndn-cxx/security/key-chain.hpp>
#include <ndn-cxx/management/nfd-controller.hpp>
#include <ndn-cxx/management/nfd-control-command.hpp>
#include <ndn-cxx/management/nfd-control-parameters.hpp>
#include <ndn-cxx/management/nfd-command-options.hpp>
namespace nfd {
namespace rib {
/**
* @brief define the RemoteRegistrator class, which handles
* the registration/unregistration to remote hub(s).
*/
class RemoteRegistrator : noncopyable
{
public:
class Error : public std::runtime_error
{
public:
explicit
Error(const std::string& what)
: std::runtime_error(what)
{
}
};
RemoteRegistrator(ndn::nfd::Controller& controller,
ndn::KeyChain& keyChain,
Rib& rib);
~RemoteRegistrator();
/**
* @brief load the "remote_register" section from config file
*
* @param configSection the sub section in "rib" section.
*/
void
loadConfig(const ConfigSection& configSection);
/**
* @brief register a prefix to remote hub(s).
*
* For the input prefix, we find the longest identity
* in the key-chain that can sign it, and then
* register this identity to remote hub(s).
*
* @param prefix the prefix being registered in local RIB.
*/
void
registerPrefix(const Name& prefix);
/**
* @brief unregister a prefix from remote hub(s).
*
* For the input prefix, if the longest identity can sign it
* is already registered remotely, that identity should be
* unregistered from remote hub(s).
*
* @param prefix the prefix being unregistered in local RIB.
*/
void
unregisterPrefix(const Name& prefix);
private:
/**
* @brief find the most proper identity that can sign the
* registration/unregistration command for the input prefix.
*
* @return the identity and the length of the longest match to the
* input prefix.
*
* @retval { ignored, 0 } no matching identity
*/
std::pair<Name, size_t>
findIdentityForRegistration(const Name& prefix);
/**
* @brief make and send the remote registration command.
*
* @param nRetries remaining number of retries.
*/
void
startRegistration(const ndn::nfd::ControlParameters& parameters,
const ndn::nfd::CommandOptions& options,
int nRetries);
/**
* @brief make and send the remote unregistration command.
*
* @param nRetries remaining number of retries.
*/
void
startUnregistration(const ndn::nfd::ControlParameters& parameters,
const ndn::nfd::CommandOptions& options,
int nRetries);
/**
* @brief refresh the remotely registered entry if registration
* successes by re-sending the registration command.
*
* The interval of sending refresh command is defined in the
* "remote_register" section of the config file.
*
* @param parameters the same paremeters from startRegistration.
* @param options the same options as in startRegistration.
*/
void
onRegSuccess(const ndn::nfd::ControlParameters& parameters,
const ndn::nfd::CommandOptions& options);
/**
* @brief retry to send registration command if registration fails.
*
* the number of retries is defined in the "remote_register"
* section of the config file.
*
* @param code error code.
* @param reason error reason in string.
* @param parameters the same paremeters from startRegistration.
* @param options the same options from startRegistration.
* @param nRetries remaining number of retries.
*/
void
onRegFailure(uint32_t code, const std::string& reason,
const ndn::nfd::ControlParameters& parameters,
const ndn::nfd::CommandOptions& options,
int nRetries);
void
onUnregSuccess(const ndn::nfd::ControlParameters& parameters,
const ndn::nfd::CommandOptions& options);
/**
* @brief retry to send unregistration command if registration fails.
*
* the number of retries is defined in the "remote_register"
* section of the config file.
*
* @param code error code.
* @param reason error reason in string.
* @param parameters the same paremeters as in startRegistration.
* @param options the same options as in startRegistration.
* @param nRetries remaining number of retries.
*/
void
onUnregFailure(uint32_t code, const std::string& reason,
const ndn::nfd::ControlParameters& parameters,
const ndn::nfd::CommandOptions& options,
int nRetries);
/**
* @brief re-register all prefixes
*
* This is called when a HUB connection is established.
*/
void
redoRegistration();
/**
* @brief clear all refresh events
*
* This is called when all HUB connections are lost.
*/
void
clearRefreshEvents();
PUBLIC_WITH_TESTS_ELSE_PRIVATE:
/**
* When a locally registered prefix triggles remote
* registration, we actually register the longest
* identity that can sign this prefix to remote hub(s).
*
* Thus, the remotely reigstered prefix does not equal
* to Route Name. So it needs seperate sotrage instead
* of storing within the RIB.
*/
typedef std::unordered_map<Name, EventId> RegisteredList;
typedef RegisteredList::iterator RegisteredEntryIt;
typedef RegisteredList::value_type RegisteredEntry;
RegisteredList m_regEntries;
private:
ndn::nfd::Controller& m_nfdController;
ndn::KeyChain& m_keyChain;
Rib& m_rib;
ndn::nfd::ControlParameters m_controlParameters;
ndn::nfd::CommandOptions m_commandOptions;
time::seconds m_refreshInterval;
bool m_hasConnectedHub;
int m_nRetries;
static const Name RM_LOCAL_PREFIX; // /localhost
static const Name RM_HUB_PREFIX; // /localhop/nfd
static const name::Component RM_IGNORE_COMMPONENT; // rib
};
} // namespace rib
} // namespace nfd
#endif // NFD_RIB_REMOTE_REGISTRATOR_HPP
+16
View File
@@ -80,6 +80,7 @@ RibManager::RibManager(ndn::Face& face)
, m_localhopValidator(m_face)
, m_faceMonitor(m_face)
, m_isLocalhopEnabled(false)
, m_remoteRegistrator(m_nfdController, m_keyChain, m_managedRib)
, m_ribStatusPublisher(m_managedRib, face, LIST_COMMAND_PREFIX, m_keyChain)
, m_lastTransactionId(0)
, m_signedVerbDispatch(SIGNED_COMMAND_VERBS,
@@ -153,6 +154,21 @@ RibManager::onConfig(const ConfigSection& configSection,
m_localhopValidator.load(i->second, filename);
m_isLocalhopEnabled = true;
}
else if (i->first == "remote_register")
{
m_remoteRegistrator.loadConfig(i->second);
// register callback to the RIB.
// do remote registration after an entry is inserted into the RIB.
// do remote unregistration after an entry is erased from the RIB.
m_managedRib.afterInsertEntry += [this] (const Name& prefix) {
m_remoteRegistrator.registerPrefix(prefix);
};
m_managedRib.afterEraseEntry += [this] (const Name& prefix) {
m_remoteRegistrator.unregisterPrefix(prefix);
};
}
else
throw Error("Unrecognized rib property: " + i->first);
}
+2
View File
@@ -29,6 +29,7 @@
#include "rib.hpp"
#include "core/config-file.hpp"
#include "rib-status-publisher.hpp"
#include "remote-registrator.hpp"
#include <ndn-cxx/security/validator-config.hpp>
#include <ndn-cxx/management/nfd-face-monitor.hpp>
@@ -239,6 +240,7 @@ private:
ndn::ValidatorConfig m_localhopValidator;
ndn::nfd::FaceMonitor m_faceMonitor;
bool m_isLocalhopEnabled;
RemoteRegistrator m_remoteRegistrator;
RibStatusPublisher m_ribStatusPublisher;
+6 -5
View File
@@ -67,11 +67,6 @@ Rib::Rib()
{
}
Rib::~Rib()
{
}
Rib::const_iterator
Rib::find(const Name& prefix) const
{
@@ -193,6 +188,9 @@ Rib::insert(const Name& prefix, const FaceEntry& face)
m_faceMap[face.faceId].push_back(entry);
createFibUpdatesForNewRibEntry(*entry, face);
// do something after inserting an entry
afterInsertEntry(prefix);
}
}
@@ -385,6 +383,9 @@ Rib::eraseEntry(RibTable::iterator it)
m_rib.erase(it);
// do something after erasing an entry.
afterEraseEntry(entry->getName());
return nextIt;
}
+4 -3
View File
@@ -29,7 +29,6 @@
#include "rib-entry.hpp"
#include "fib-update.hpp"
#include "common.hpp"
#include "rib-entry.hpp"
#include <ndn-cxx/management/nfd-control-command.hpp>
namespace nfd {
@@ -50,8 +49,6 @@ public:
Rib();
~Rib();
const_iterator
find(const Name& prefix) const;
@@ -139,6 +136,10 @@ private:
void
removeInheritedFacesFromEntry(RibEntry& entry, const Rib::FaceSet& facesToRemove);
public:
ndn::util::EventEmitter<Name> afterInsertEntry;
ndn::util::EventEmitter<Name> afterEraseEntry;
private:
RibTable m_rib;
FaceLookupTable m_faceMap;
+53
View File
@@ -0,0 +1,53 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2013-2014 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
* ndn-cxx library is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received copies of the GNU General Public License and GNU Lesser
* General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
* <http://www.gnu.org/licenses/>.
*
* See AUTHORS.md for complete list of ndn-cxx authors and contributors.
*/
#include "identity-management-fixture.hpp"
namespace nfd {
namespace tests {
IdentityManagementFixture::IdentityManagementFixture()
: m_keyChain("sqlite3", "file")
{
}
IdentityManagementFixture::~IdentityManagementFixture()
{
for (auto&& id : m_identities) {
m_keyChain.deleteIdentity(id);
}
}
bool
IdentityManagementFixture::addIdentity(const ndn::Name& identity, const ndn::KeyParams& params)
{
try {
m_keyChain.createIdentity(identity, params);
m_identities.push_back(identity);
return true;
}
catch (std::runtime_error&) {
return false;
}
}
} // namespace tests
} // namespace nfd
+56
View File
@@ -0,0 +1,56 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2013-2014 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
* ndn-cxx library is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received copies of the GNU General Public License and GNU Lesser
* General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
* <http://www.gnu.org/licenses/>.
*
* See AUTHORS.md for complete list of ndn-cxx authors and contributors.
*/
#include "tests/test-common.hpp"
#include <ndn-cxx/security/key-chain.hpp>
#include <vector>
#include "boost-test.hpp"
namespace nfd {
namespace tests {
/**
* @brief IdentityManagementFixture is a test suite level fixture.
*
* Test cases in the suite can use this fixture to create identities.
* Identities added via addIdentity method are automatically deleted
* during test teardown.
*/
class IdentityManagementFixture : public nfd::tests::BaseFixture
{
public:
IdentityManagementFixture();
~IdentityManagementFixture();
// @brief add identity, return true if succeed.
bool
addIdentity(const ndn::Name& identity,
const ndn::KeyParams& params = ndn::KeyChain::DEFAULT_KEY_PARAMS);
protected:
ndn::KeyChain m_keyChain;
std::vector<ndn::Name> m_identities;
};
} // namespace tests
} // namespace nfd
+459
View File
@@ -0,0 +1,459 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2014, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
* Washington University in St. Louis,
* Beijing Institute of Technology,
* The University of Memphis
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
*
* NFD is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/
#include "rib/remote-registrator.hpp"
#include "tests/limited-io.hpp"
#include "tests/identity-management-fixture.hpp"
#include <ndn-cxx/util/dummy-client-face.hpp>
namespace nfd {
namespace rib {
namespace tests {
class RemoteRegistratorFixture : public nfd::tests::IdentityManagementFixture
{
public:
RemoteRegistratorFixture()
: face(ndn::util::makeDummyClientFace())
, controller(make_shared<ndn::nfd::Controller>(std::ref(*face), m_keyChain))
, remoteRegistrator(make_shared<RemoteRegistrator>(std::ref(*controller),
m_keyChain,
rib))
, COMMAND_PREFIX("/localhop/nfd/rib")
, REGISTER_VERB("register")
, UNREGISTER_VERB("unregister")
{
readConfig();
registerCallback();
face->processEvents(time::milliseconds(1));
face->sentInterests.clear();
}
void
readConfig()
{
ConfigFile config;
config.addSectionHandler("remote_register",
bind(&RemoteRegistrator::loadConfig, remoteRegistrator, _1));
const std::string CONFIG_STRING =
"remote_register\n"
"{\n"
" cost 15\n"
" timeout 10000\n"
" retry 2\n"
" refresh_interval 5\n"
"}";
config.parse(CONFIG_STRING, true, "test-remote-register");
}
void
registerCallback()
{
rib.afterInsertEntry += [this] (const Name& prefix) {
remoteRegistrator->registerPrefix(prefix);
};
rib.afterEraseEntry += [this] (const Name& prefix) {
remoteRegistrator->unregisterPrefix(prefix);
};
}
void
insertEntryWithIdentity(Name identity,
name::Component appName = DEFAULT_APP_NAME,
uint64_t faceId = 0)
{
BOOST_CHECK_EQUAL(addIdentity(identity), true);
FaceEntry faceEntry;
faceEntry.faceId = faceId;
rib.insert(identity.append(appName), faceEntry);
face->processEvents(time::milliseconds(1));
}
void
insertEntryWithoutIdentity(Name identity,
name::Component appName = DEFAULT_APP_NAME,
uint64_t faceId = 0)
{
FaceEntry faceEntry;
faceEntry.faceId = faceId;
rib.insert(identity.append(appName), faceEntry);
face->processEvents(time::milliseconds(1));
}
void
eraseEntryWithIdentity(Name identity,
name::Component appName = DEFAULT_APP_NAME,
uint64_t faceId = 0)
{
BOOST_CHECK_EQUAL(addIdentity(identity), true);
FaceEntry faceEntry;
faceEntry.faceId = faceId;
rib.erase(identity.append(appName), faceEntry);
face->processEvents(time::milliseconds(1));
}
void
eraseEntryWithoutIdentity(Name identity,
name::Component appName = DEFAULT_APP_NAME,
uint64_t faceId = 0)
{
FaceEntry faceEntry;
faceEntry.faceId = faceId;
rib.erase(identity.append(appName), faceEntry);
face->processEvents(time::milliseconds(1));
}
void
eraseFace(uint64_t faceId)
{
rib.erase(faceId);
face->processEvents(time::milliseconds(1));
}
void
connectToHub()
{
rib.insert(Name("/localhop/nfd"), FaceEntry());
face->processEvents(time::milliseconds(1));
}
void
disconnectToHub()
{
rib.erase(Name("/localhop/nfd"), FaceEntry());
face->processEvents(time::milliseconds(1));
}
void
extractParameters(Interest& interest, Name::Component& verb,
ndn::nfd::ControlParameters& extractedParameters)
{
const Name& name = interest.getName();
verb = name[COMMAND_PREFIX.size()];
const Name::Component& parameterComponent = name[COMMAND_PREFIX.size() + 1];
Block rawParameters = parameterComponent.blockFromValue();
extractedParameters.wireDecode(rawParameters);
}
public:
Rib rib;
shared_ptr<ndn::util::DummyClientFace> face;
shared_ptr<ndn::nfd::Controller> controller;
shared_ptr<RemoteRegistrator> remoteRegistrator;
const Name COMMAND_PREFIX;
const name::Component REGISTER_VERB;
const name::Component UNREGISTER_VERB;
static const name::Component DEFAULT_APP_NAME;
};
const name::Component RemoteRegistratorFixture::DEFAULT_APP_NAME("app");
BOOST_FIXTURE_TEST_SUITE(RemoteRegistrator, RemoteRegistratorFixture)
BOOST_FIXTURE_TEST_CASE(AutoTest, RemoteRegistratorFixture)
{
BOOST_REQUIRE_EQUAL(1, 1);
}
BOOST_FIXTURE_TEST_CASE(RegisterWithoutConnection, RemoteRegistratorFixture)
{
insertEntryWithIdentity("/remote/register");
BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 0);
}
BOOST_FIXTURE_TEST_CASE(RegisterWithoutIdentity, RemoteRegistratorFixture)
{
connectToHub();
insertEntryWithoutIdentity("/remote/register");
BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 0);
}
BOOST_FIXTURE_TEST_CASE(RegisterWithHubPrefix, RemoteRegistratorFixture)
{
connectToHub();
BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 0);
}
BOOST_FIXTURE_TEST_CASE(RegisterWithLocalPrefix, RemoteRegistratorFixture)
{
connectToHub();
insertEntryWithIdentity("/localhost/prefix");
BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 0);
}
BOOST_FIXTURE_TEST_CASE(RegisterBasic, RemoteRegistratorFixture)
{
connectToHub();
Name identity("/remote/register");
insertEntryWithIdentity(identity);
BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1);
Interest& request = face->sentInterests[0];
ndn::nfd::ControlParameters extractedParameters;
Name::Component verb;
extractParameters(request, verb, extractedParameters);
BOOST_CHECK_EQUAL(verb, REGISTER_VERB);
BOOST_CHECK_EQUAL(extractedParameters.getName(), identity);
}
BOOST_FIXTURE_TEST_CASE(RegisterAdvanced, RemoteRegistratorFixture)
{
connectToHub();
Name identity("/remote/register");
Name identityAddRib("/remote/register/rib");
insertEntryWithIdentity(identityAddRib);
BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1);
Interest& request = face->sentInterests[0];
ndn::nfd::ControlParameters extractedParameters;
Name::Component verb;
extractParameters(request, verb, extractedParameters);
BOOST_CHECK_EQUAL(verb, REGISTER_VERB);
BOOST_CHECK_EQUAL(extractedParameters.getName(), identity);
}
BOOST_FIXTURE_TEST_CASE(UnregisterWithoutInsert, RemoteRegistratorFixture)
{
connectToHub();
eraseEntryWithIdentity("/remote/register");
BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 0);
}
BOOST_FIXTURE_TEST_CASE(UnregisterWithoutConnection, RemoteRegistratorFixture)
{
connectToHub();
disconnectToHub();
Name indentity("/remote/register");
remoteRegistrator->m_regEntries.insert(
nfd::rib::RemoteRegistrator::RegisteredEntry(indentity, EventId()));
eraseEntryWithIdentity(indentity);
BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 0);
}
BOOST_FIXTURE_TEST_CASE(UnregisterWithoutSuccessfullRegistration,
RemoteRegistratorFixture)
{
connectToHub();
Name identity("/remote/register");
insertEntryWithIdentity(identity);
eraseEntryWithIdentity(identity);
BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1);
Interest& request = face->sentInterests[0];
ndn::nfd::ControlParameters extractedParameters;
Name::Component verb;
extractParameters(request, verb, extractedParameters);
BOOST_CHECK_EQUAL(verb, REGISTER_VERB);
BOOST_CHECK_EQUAL(extractedParameters.getName(), identity);
}
BOOST_FIXTURE_TEST_CASE(UnregisterBasic, RemoteRegistratorFixture)
{
connectToHub();
Name identity("/remote/register");
insertEntryWithIdentity(identity);
EventId event;
remoteRegistrator->m_regEntries.insert(
nfd::rib::RemoteRegistrator::RegisteredEntry(identity, event));
eraseEntryWithIdentity(identity);
BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 2);
Interest& request = face->sentInterests[1];
ndn::nfd::ControlParameters extractedParameters;
Name::Component verb;
extractParameters(request, verb, extractedParameters);
BOOST_CHECK_EQUAL(verb, UNREGISTER_VERB);
BOOST_CHECK_EQUAL(extractedParameters.getName(), identity);
}
BOOST_FIXTURE_TEST_CASE(UnregisterAdvanced, RemoteRegistratorFixture)
{
connectToHub();
Name identityShort("/remote/register");
Name identityLong("/remote/register/long");
EventId eventShort;
EventId eventLong;
insertEntryWithIdentity(identityShort, name::Component("appA"));
remoteRegistrator->m_regEntries.insert(
nfd::rib::RemoteRegistrator::RegisteredEntry(identityShort,
eventShort));
insertEntryWithIdentity(identityShort, name::Component("appB"));
insertEntryWithIdentity(identityLong);
remoteRegistrator->m_regEntries.insert(
nfd::rib::RemoteRegistrator::RegisteredEntry(identityLong,
eventLong));
// two registration commands are generated for identityShort and identityLong
BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 2);
eraseEntryWithIdentity(identityShort, name::Component("appA"));
// no unregistration command is generated as appB also exists
BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 2);
eraseEntryWithIdentity(identityShort, name::Component("appB"));
// one unregistration command is generated for identityShort
BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 3);
Interest& request = face->sentInterests[2];
ndn::nfd::ControlParameters extractedParameters;
Name::Component verb;
extractParameters(request, verb, extractedParameters);
BOOST_CHECK_EQUAL(verb, UNREGISTER_VERB);
BOOST_CHECK_EQUAL(extractedParameters.getName(), identityShort);
}
BOOST_FIXTURE_TEST_CASE(EraseFace, RemoteRegistratorFixture)
{
connectToHub();
Name identity("/remote/register");
uint64_t faceId = 517;
insertEntryWithIdentity(identity, DEFAULT_APP_NAME, faceId);
EventId event;
remoteRegistrator->m_regEntries.insert(
nfd::rib::RemoteRegistrator::RegisteredEntry(identity, event));
eraseFace(faceId);
BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 2);
Interest& request = face->sentInterests[1];
ndn::nfd::ControlParameters extractedParameters;
Name::Component verb;
extractParameters(request, verb, extractedParameters);
BOOST_CHECK_EQUAL(verb, UNREGISTER_VERB);
BOOST_CHECK_EQUAL(extractedParameters.getName(), identity);
}
BOOST_FIXTURE_TEST_CASE(RebuildConnection, RemoteRegistratorFixture)
{
connectToHub();
Name identity("/remote/register");
insertEntryWithIdentity(identity);
EventId event;
remoteRegistrator->m_regEntries.insert(
nfd::rib::RemoteRegistrator::RegisteredEntry(identity, event));
disconnectToHub();
connectToHub();
BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 2);
Interest& request1 = face->sentInterests[0];
Interest& request2 = face->sentInterests[1];
ndn::nfd::ControlParameters extractedParameters1, extractedParameters2;
Name::Component verb1, verb2;
extractParameters(request1, verb1, extractedParameters1);
extractParameters(request2, verb2, extractedParameters2);
BOOST_CHECK_EQUAL(verb1, REGISTER_VERB);
BOOST_CHECK_EQUAL(verb2, REGISTER_VERB);
BOOST_CHECK_EQUAL(extractedParameters1.getName(),
extractedParameters2.getName());
}
BOOST_AUTO_TEST_SUITE_END()
} // namespace tests
} // namespace rib
} // namespace nfd