nlsr: accept NFD-RIB-style register/unregister commands

refs: #3818

Change-Id: I7a5455bdec815bdcbe53ad535dab44249d1a18ce
diff --git a/src/update/nfd-rib-command-processor.cpp b/src/update/nfd-rib-command-processor.cpp
new file mode 100644
index 0000000..f4baeb5
--- /dev/null
+++ b/src/update/nfd-rib-command-processor.cpp
@@ -0,0 +1,118 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2017,  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 "nfd-rib-command-processor.hpp"
+#include "logger.hpp"
+
+namespace nlsr {
+namespace update {
+
+INIT_LOGGER("NfdRibProcessor");
+
+const ndn::PartialName REGISTER_VERB = ndn::PartialName("rib/register");
+const ndn::PartialName UNREGISTER_VERB = ndn::PartialName("rib/unregister");
+const ndn::Name COMMAND_PREFIX = ndn::Name("/localhost/nlsr");
+
+NfdRibCommandProcessor::NfdRibCommandProcessor(ndn::mgmt::Dispatcher& dispatcher,
+                                               NamePrefixList& namePrefixes,
+                                               Lsdb& lsdb,
+                                               SyncLogicHandler& sync)
+  : m_dispatcher(dispatcher)
+  , m_namePrefixList(namePrefixes)
+  , m_lsdb(lsdb)
+  , m_sync(sync)
+{
+}
+
+void
+NfdRibCommandProcessor::startListening()
+{
+  _LOG_DEBUG("Registering control command prefixes for: " << REGISTER_VERB <<
+             " and " << UNREGISTER_VERB);
+  m_dispatcher.addControlCommand<ndn::nfd::ControlParameters>(REGISTER_VERB,
+                                 ndn::mgmt::makeAcceptAllAuthorization(),
+                                 std::bind(&NfdRibCommandProcessor::validate<NfdRibRegisterCommand>,
+                                           this, _1, REGISTER_VERB),
+                                 [this] (const ndn::Name& prefix, const ndn::Interest& interest,
+                                         const ndn::mgmt::ControlParameters& parameters,
+                                         const ndn::mgmt::CommandContinuation& done) {
+                                   _LOG_DEBUG("Params verified, calling insertPrefix()");
+                                   this->insertPrefix(parameters);
+                                 });
+  m_dispatcher.addControlCommand<ndn::nfd::ControlParameters>(UNREGISTER_VERB,
+                                 ndn::mgmt::makeAcceptAllAuthorization(),
+                                 std::bind(&NfdRibCommandProcessor::validate<NfdRibUnregisterCommand>,
+                                           this, _1, UNREGISTER_VERB),
+                                 [this] (const ndn::Name& prefix, const ndn::Interest& interest,
+                                         const ndn::mgmt::ControlParameters& parameters,
+                                         const ndn::mgmt::CommandContinuation& done) {
+                                   _LOG_DEBUG("Params verified, calling removePrefix()");
+                                   this->removePrefix(parameters);
+                                 });
+}
+
+template<typename T>
+bool
+NfdRibCommandProcessor::validate(const ndn::mgmt::ControlParameters& parameters,
+                                 const ndn::PartialName& command)
+{
+  _LOG_DEBUG("Attempting to verify incoming params for " << command <<
+             " command...");
+  bool wasValidated = true;
+  try {
+      wasValidated = this->validateParameters<T>(parameters);
+  } catch (const ndn::nfd::ControlCommand::ArgumentError& ae) {
+    _LOG_DEBUG("Could not parse params. Message: " << ae.what());
+    wasValidated = false;
+  }
+  return wasValidated;
+}
+
+void
+NfdRibCommandProcessor::insertPrefix(const ndn::mgmt::ControlParameters& parameters)
+{
+  const ndn::nfd::ControlParameters& castParams =
+    static_cast<const ndn::nfd::ControlParameters&>(parameters);
+
+  _LOG_DEBUG("Inserting prefix into the FIB: " << castParams.getName() << "\n");
+
+  if (m_namePrefixList.insert(castParams.getName())) {
+    m_lsdb.buildAndInstallOwnNameLsa();
+    m_sync.publishRoutingUpdate();
+  }
+}
+
+void
+NfdRibCommandProcessor::removePrefix(const ndn::mgmt::ControlParameters& parameters)
+{
+  const ndn::nfd::ControlParameters& castParams =
+    static_cast<const ndn::nfd::ControlParameters&>(parameters);
+
+  _LOG_DEBUG("Removing prefix from the FIB: " << castParams.getName() << "\n");
+
+  if (m_namePrefixList.remove(castParams.getName())) {
+    m_lsdb.buildAndInstallOwnNameLsa();
+    m_sync.publishRoutingUpdate();
+  }
+}
+
+} // namespace update
+} // namespace nlsr