update: Process Name prefix update commands
refs #1834
Change-Id: I18c86d0743b4a10ce3a8681f202b59a86602e43f
diff --git a/src/update/prefix-update-processor.cpp b/src/update/prefix-update-processor.cpp
new file mode 100644
index 0000000..75e6bae
--- /dev/null
+++ b/src/update/prefix-update-processor.cpp
@@ -0,0 +1,207 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2015, The University of Memphis,
+ * Regents of the University of California,
+ * Arizona Board of Regents.
+ *
+ * This file is part of NLSR (Named-data Link State Routing).
+ * See AUTHORS.md for complete list of NLSR authors and contributors.
+ *
+ * NLSR 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.
+ *
+ * NLSR 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
+ * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+#include "prefix-update-processor.hpp"
+
+#include "lsdb.hpp"
+#include "nlsr.hpp"
+#include "prefix-update-commands.hpp"
+#include "communication/sync-logic-handler.hpp"
+
+#include <ndn-cxx/management/nfd-control-response.hpp>
+
+namespace nlsr {
+namespace update {
+
+INIT_LOGGER("PrefixUpdateProcessor");
+
+const ndn::Name::Component PrefixUpdateProcessor::MODULE_COMPONENT = ndn::Name::Component("prefix-update");
+const ndn::Name::Component PrefixUpdateProcessor::ADVERTISE_VERB = ndn::Name::Component("advertise");
+const ndn::Name::Component PrefixUpdateProcessor::WITHDRAW_VERB = ndn::Name::Component("withdraw");
+
+PrefixUpdateProcessor::PrefixUpdateProcessor(ndn::Face& face,
+ NamePrefixList& namePrefixList,
+ Lsdb& lsdb,
+ SyncLogicHandler& sync,
+ const ndn::Name broadcastPrefix,
+ ndn::KeyChain& keyChain,
+ ndn::shared_ptr<ndn::CertificateCacheTtl> certificateCache)
+ : m_face(face)
+ , m_namePrefixList(namePrefixList)
+ , m_lsdb(lsdb)
+ , m_sync(sync)
+ , m_keyChain(keyChain)
+ , m_validator(m_face, broadcastPrefix, certificateCache)
+ , COMMAND_PREFIX(ndn::Name(Nlsr::LOCALHOST_PREFIX).append(MODULE_COMPONENT))
+{
+}
+
+void
+PrefixUpdateProcessor::startListening()
+{
+ _LOG_DEBUG("Setting Interest filter for: " << COMMAND_PREFIX);
+
+ m_face.setInterestFilter(COMMAND_PREFIX, bind(&PrefixUpdateProcessor::onInterest, this, _2));
+}
+
+void
+PrefixUpdateProcessor::onInterest(const ndn::Interest& request)
+{
+ _LOG_TRACE("Received Interest: " << request);
+
+ m_validator.validate(request,
+ bind(&PrefixUpdateProcessor::onCommandValidated, this, _1),
+ bind(&PrefixUpdateProcessor::onCommandValidationFailed, this, _1, _2));
+}
+
+void
+PrefixUpdateProcessor::loadValidator(boost::property_tree::ptree section,
+ const std::string& filename)
+{
+ m_validator.load(section, filename);
+}
+
+void
+PrefixUpdateProcessor::onCommandValidated(const std::shared_ptr<const ndn::Interest>& request)
+{
+ const ndn::Name& command = request->getName();
+ const ndn::Name::Component& verb = command[COMMAND_PREFIX.size()];
+ const ndn::Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
+
+ if (verb == ADVERTISE_VERB || verb == WITHDRAW_VERB) {
+ ndn::nfd::ControlParameters parameters;
+
+ if (!extractParameters(parameterComponent, parameters)) {
+ sendResponse(request, 400, "Malformed command");
+ return;
+ }
+
+ if (verb == ADVERTISE_VERB) {
+ advertise(request, parameters);
+ }
+ else if (verb == WITHDRAW_VERB) {
+ withdraw(request, parameters);
+ }
+
+ sendResponse(request, 200, "Success");
+ }
+ else {
+ sendResponse(request, 501, "Unsupported command");
+ }
+}
+
+void
+PrefixUpdateProcessor::onCommandValidationFailed(const std::shared_ptr<const ndn::Interest>& request,
+ const std::string& failureInfo)
+{
+ sendResponse(request, 403, failureInfo);
+}
+
+bool
+PrefixUpdateProcessor::extractParameters(const ndn::Name::Component& parameterComponent,
+ ndn::nfd::ControlParameters& extractedParameters)
+{
+ try {
+ ndn::Block rawParameters = parameterComponent.blockFromValue();
+ extractedParameters.wireDecode(rawParameters);
+ }
+ catch (const ndn::tlv::Error&) {
+ return false;
+ }
+
+ return true;
+}
+
+void
+PrefixUpdateProcessor::advertise(const std::shared_ptr<const ndn::Interest>& request,
+ const ndn::nfd::ControlParameters& parameters)
+{
+ AdvertisePrefixCommand command;
+
+ if (!validateParameters(command, parameters)) {
+ sendResponse(request, 400, "Malformed command");
+ return;
+ }
+
+ _LOG_INFO("Advertising name: " << parameters.getName());
+
+ if (m_namePrefixList.insert(parameters.getName())) {
+ // Only build a Name LSA if the added name is new
+ m_lsdb.buildAndInstallOwnNameLsa();
+ m_sync.publishRoutingUpdate();
+ }
+}
+
+void
+PrefixUpdateProcessor::withdraw(const std::shared_ptr<const ndn::Interest>& request,
+ const ndn::nfd::ControlParameters& parameters)
+{
+ WithdrawPrefixCommand command;
+
+ if (!validateParameters(command, parameters)) {
+ sendResponse(request, 400, "Malformed command");
+ return;
+ }
+
+ _LOG_INFO("Withdrawing name: " << parameters.getName());
+
+ if (m_namePrefixList.remove(parameters.getName())) {
+ // Only build a Name LSA if a name was actually removed
+ m_lsdb.buildAndInstallOwnNameLsa();
+ m_sync.publishRoutingUpdate();
+ }
+}
+
+bool
+PrefixUpdateProcessor::validateParameters(const ndn::nfd::ControlCommand& command,
+ const ndn::nfd::ControlParameters& parameters)
+{
+ try {
+ command.validateRequest(parameters);
+ }
+ catch (const ndn::nfd::ControlCommand::ArgumentError&) {
+ return false;
+ }
+
+ return true;
+}
+
+void
+PrefixUpdateProcessor::sendResponse(const std::shared_ptr<const ndn::Interest>& request,
+ uint32_t code,
+ const std::string& text)
+{
+ if (request == nullptr) {
+ return;
+ }
+
+ ndn::nfd::ControlResponse response(code, text);
+ const ndn::Block& encodedControl = response.wireEncode();
+
+ std::shared_ptr<ndn::Data> responseData = ndn::make_shared<ndn::Data>(request->getName());
+ responseData->setContent(encodedControl);
+
+ m_keyChain.sign(*responseData);
+ m_face.put(*responseData);
+}
+
+} // namespace update
+} // namespace nlsr