publisher: Serve LSDB dataset on localhost prefix

refs: #2737

Change-Id: I4cc9bd1d07dea583937676dd491ab5c68fd821ea
diff --git a/src/publisher/lsa-publisher.cpp b/src/publisher/lsa-publisher.cpp
index 31192fd..a880fba 100644
--- a/src/publisher/lsa-publisher.cpp
+++ b/src/publisher/lsa-publisher.cpp
@@ -32,9 +32,8 @@
 
 AdjacencyLsaPublisher::AdjacencyLsaPublisher(Lsdb& lsdb,
                                              ndn::Face& face,
-                                             const ndn::Name& prefix,
                                              ndn::KeyChain& keyChain)
-  : LsaPublisher(face, prefix, keyChain, DATASET_COMPONENT)
+  : LsaPublisher(face, keyChain)
   , m_adjacencyLsas(lsdb.getAdjLsdb())
 {
 }
@@ -73,9 +72,8 @@
 
 CoordinateLsaPublisher::CoordinateLsaPublisher(Lsdb& lsdb,
                                                ndn::Face& face,
-                                               const ndn::Name& prefix,
                                                ndn::KeyChain& keyChain)
-  : LsaPublisher(face, prefix, keyChain, DATASET_COMPONENT)
+  : LsaPublisher(face, keyChain)
   , m_coordinateLsas(lsdb.getCoordinateLsdb())
 {
 }
@@ -109,9 +107,8 @@
 
 NameLsaPublisher::NameLsaPublisher(Lsdb& lsdb,
                                    ndn::Face& face,
-                                   const ndn::Name& prefix,
                                    ndn::KeyChain& keyChain)
-  : LsaPublisher(face, prefix, keyChain, DATASET_COMPONENT)
+  : LsaPublisher(face, keyChain)
   , m_nameLsas(lsdb.getNameLsdb())
 {
 }
diff --git a/src/publisher/lsa-publisher.hpp b/src/publisher/lsa-publisher.hpp
index 09aec8c..0f38037 100644
--- a/src/publisher/lsa-publisher.hpp
+++ b/src/publisher/lsa-publisher.hpp
@@ -36,11 +36,8 @@
 class LsaPublisher : public SegmentPublisher<ndn::Face>
 {
 public:
-  LsaPublisher(ndn::Face& face,
-                const ndn::Name& prefix,
-                ndn::KeyChain& keyChain,
-                const ndn::Name::Component& datasetComponent)
-  : SegmentPublisher<ndn::Face>(face, ndn::Name(prefix).append(datasetComponent), keyChain)
+  LsaPublisher(ndn::Face& face, ndn::KeyChain& keyChain)
+  : SegmentPublisher<ndn::Face>(face, keyChain)
   {
   }
 
@@ -75,7 +72,6 @@
 public:
   AdjacencyLsaPublisher(Lsdb& lsdb,
                         ndn::Face& face,
-                        const ndn::Name& prefix,
                         ndn::KeyChain& keyChain);
 
   std::list<tlv::AdjacencyLsa>
@@ -97,7 +93,6 @@
 public:
   CoordinateLsaPublisher(Lsdb& lsdb,
                         ndn::Face& face,
-                        const ndn::Name& prefix,
                         ndn::KeyChain& keyChain);
 
   std::list<tlv::CoordinateLsa>
@@ -119,7 +114,6 @@
 public:
   NameLsaPublisher(Lsdb& lsdb,
                    ndn::Face& face,
-                   const ndn::Name& prefix,
                    ndn::KeyChain& keyChain);
 
   std::list<tlv::NameLsa>
diff --git a/src/publisher/lsdb-dataset-interest-handler.cpp b/src/publisher/lsdb-dataset-interest-handler.cpp
index 5285d28..11ddc69 100644
--- a/src/publisher/lsdb-dataset-interest-handler.cpp
+++ b/src/publisher/lsdb-dataset-interest-handler.cpp
@@ -22,6 +22,7 @@
 #include "lsdb-dataset-interest-handler.hpp"
 
 #include "logger.hpp"
+#include "nlsr.hpp"
 
 #include <ndn-cxx/face.hpp>
 #include <ndn-cxx/management/nfd-control-response.hpp>
@@ -31,58 +32,93 @@
 
 INIT_LOGGER("LsdbDatasetInterestHandler");
 
+const uint32_t LsdbDatasetInterestHandler::ERROR_CODE_MALFORMED_COMMAND = 400;
+const uint32_t LsdbDatasetInterestHandler::ERROR_CODE_UNSUPPORTED_COMMAND = 501;
+
 LsdbDatasetInterestHandler::LsdbDatasetInterestHandler(Lsdb& lsdb,
                                                        ndn::Face& face,
                                                        const ndn::Name& routerName,
                                                        ndn::KeyChain& keyChain)
-  : COMMAND_PREFIX(ndn::Name(routerName).append(Lsdb::NAME_COMPONENT))
+  : LOCALHOST_COMMAND_PREFIX(ndn::Name(Nlsr::LOCALHOST_PREFIX).append(Lsdb::NAME_COMPONENT))
+  , ROUTER_NAME_COMMAND_PREFIX(ndn::Name(routerName).append(Lsdb::NAME_COMPONENT))
   , m_face(face)
   , m_keyChain(keyChain)
-  , m_adjacencyLsaPublisher(lsdb, face, COMMAND_PREFIX, keyChain)
-  , m_coordinateLsaPublisher(lsdb, face, COMMAND_PREFIX, keyChain)
-  , m_nameLsaPublisher(lsdb, face, COMMAND_PREFIX, keyChain)
-  , m_lsdbStatusPublisher(lsdb, face, COMMAND_PREFIX, keyChain,
+  , m_adjacencyLsaPublisher(lsdb, face, keyChain)
+  , m_coordinateLsaPublisher(lsdb, face, keyChain)
+  , m_nameLsaPublisher(lsdb, face, keyChain)
+  , m_lsdbStatusPublisher(lsdb, face, keyChain,
                           m_adjacencyLsaPublisher,
                           m_coordinateLsaPublisher,
                           m_nameLsaPublisher)
 
 {
-  _LOG_DEBUG("Setting interest filter for: " << COMMAND_PREFIX);
-  m_face.setInterestFilter(COMMAND_PREFIX,
-                           std::bind(&LsdbDatasetInterestHandler::onInterest, this, _2));
 }
 
 void
-LsdbDatasetInterestHandler::onInterest(const ndn::Interest& interest)
+LsdbDatasetInterestHandler::startListeningOnLocalhost()
 {
-  // Does interest match command prefix with one additional component?
-  if (interest.getName().size() != COMMAND_PREFIX.size() + 1 ||
-      !COMMAND_PREFIX.isPrefixOf(interest.getName()))
+  _LOG_DEBUG("Setting interest filter for: " << LOCALHOST_COMMAND_PREFIX);
+  m_face.setInterestFilter(LOCALHOST_COMMAND_PREFIX,
+                           std::bind(&LsdbDatasetInterestHandler::onInterest, this, _2,
+                                     std::cref(LOCALHOST_COMMAND_PREFIX)));
+}
+
+void
+LsdbDatasetInterestHandler::startListeningOnRouterPrefix()
+{
+  _LOG_DEBUG("Setting interest filter for: " << ROUTER_NAME_COMMAND_PREFIX);
+  m_face.setInterestFilter(ROUTER_NAME_COMMAND_PREFIX,
+                           std::bind(&LsdbDatasetInterestHandler::onInterest, this, _2,
+                                     std::cref(ROUTER_NAME_COMMAND_PREFIX)));
+}
+
+void
+LsdbDatasetInterestHandler::onInterest(const ndn::Interest& interest,
+                                       const ndn::Name& commandPrefix)
+{
+  if (!isValidCommandPrefix(interest, commandPrefix))
   {
     _LOG_DEBUG("Received malformed interest: " << interest.getName());
 
-    sendErrorResponse(interest.getName(), 400, "Malformed command");
+    sendErrorResponse(interest.getName(), ERROR_CODE_MALFORMED_COMMAND, "Malformed command");
     return;
   }
 
-  ndn::Name::Component command = interest.getName().get(COMMAND_PREFIX.size());
+  ndn::Name::Component command = interest.getName().get(commandPrefix.size());
+  processCommand(interest, command);
+}
+
+bool
+LsdbDatasetInterestHandler::isValidCommandPrefix(const ndn::Interest& interest,
+                                                 const ndn::Name& commandPrefix)
+{
+  size_t commandSize = interest.getName().size();
+
+  // Does the Interest match the command prefix with one additional component?
+  return (commandSize == commandPrefix.size() + 1 && commandPrefix.isPrefixOf(interest.getName()));
+}
+
+void
+LsdbDatasetInterestHandler::processCommand(const ndn::Interest& interest,
+                                           const ndn::Name::Component& command)
+{
   _LOG_TRACE("Received interest with command: " << command);
 
   if (command.equals(AdjacencyLsaPublisher::DATASET_COMPONENT)) {
-    m_adjacencyLsaPublisher.publish();
+    m_adjacencyLsaPublisher.publish(interest.getName());
   }
   else if (command.equals(CoordinateLsaPublisher::DATASET_COMPONENT)) {
-    m_coordinateLsaPublisher.publish();
+    m_coordinateLsaPublisher.publish(interest.getName());
   }
   else if (command.equals(NameLsaPublisher::DATASET_COMPONENT)) {
-    m_nameLsaPublisher.publish();
+    m_nameLsaPublisher.publish(interest.getName());
   }
   else if (command.equals(LsdbStatusPublisher::DATASET_COMPONENT)) {
-    m_lsdbStatusPublisher.publish();
+    m_lsdbStatusPublisher.publish(interest.getName());
   }
   else {
     _LOG_DEBUG("Unsupported command: " << command);
-    sendErrorResponse(interest.getName(), 501, "Unsupported command");
+    sendErrorResponse(interest.getName(), ERROR_CODE_UNSUPPORTED_COMMAND, "Unsupported command");
   }
 }
 
diff --git a/src/publisher/lsdb-dataset-interest-handler.hpp b/src/publisher/lsdb-dataset-interest-handler.hpp
index 8f43c87..c56c638 100644
--- a/src/publisher/lsdb-dataset-interest-handler.hpp
+++ b/src/publisher/lsdb-dataset-interest-handler.hpp
@@ -52,13 +52,39 @@
                              ndn::KeyChain& keyChain);
 
   void
-  onInterest(const ndn::Interest& interest);
+  startListeningOnLocalhost();
+
+  void
+  startListeningOnRouterPrefix();
+
+  const ndn::Name&
+  getLocalhostCommandPrefix()
+  {
+    return LOCALHOST_COMMAND_PREFIX;
+  }
+
+  const ndn::Name&
+  getRouterNameCommandPrefix()
+  {
+    return ROUTER_NAME_COMMAND_PREFIX;
+  }
+
+private:
+  void
+  onInterest(const ndn::Interest& interest, const ndn::Name& commandPrefix);
+
+  bool
+  isValidCommandPrefix(const ndn::Interest& interest, const ndn::Name& commandPrefix);
+
+  void
+  processCommand(const ndn::Interest& interest, const ndn::Name::Component& command);
 
   void
   sendErrorResponse(const ndn::Name& name, uint32_t code, const std::string& error);
 
 private:
-  const ndn::Name COMMAND_PREFIX;
+  const ndn::Name LOCALHOST_COMMAND_PREFIX;
+  const ndn::Name ROUTER_NAME_COMMAND_PREFIX;
 
   ndn::Face& m_face;
   ndn::KeyChain& m_keyChain;
@@ -67,6 +93,10 @@
   CoordinateLsaPublisher m_coordinateLsaPublisher;
   NameLsaPublisher m_nameLsaPublisher;
   LsdbStatusPublisher m_lsdbStatusPublisher;
+
+PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+  static const uint32_t ERROR_CODE_MALFORMED_COMMAND;
+  static const uint32_t ERROR_CODE_UNSUPPORTED_COMMAND;
 };
 
 } // namespace nlsr
diff --git a/src/publisher/lsdb-status-publisher.cpp b/src/publisher/lsdb-status-publisher.cpp
index 6905df6..65f570c 100644
--- a/src/publisher/lsdb-status-publisher.cpp
+++ b/src/publisher/lsdb-status-publisher.cpp
@@ -32,12 +32,11 @@
 
 LsdbStatusPublisher::LsdbStatusPublisher(Lsdb& lsdb,
                                          ndn::Face& face,
-                                         const ndn::Name& prefix,
                                          ndn::KeyChain& keyChain,
                                          AdjacencyLsaPublisher& adjacencyLsaPublisher,
                                          CoordinateLsaPublisher& coordinateLsaPublisher,
                                          NameLsaPublisher& nameLsaPublisher)
-  : SegmentPublisher<ndn::Face>(face, ndn::Name(prefix).append(DATASET_COMPONENT), keyChain)
+  : SegmentPublisher<ndn::Face>(face, keyChain)
   , m_adjacencyLsaPublisher(adjacencyLsaPublisher)
   , m_coordinateLsaPublisher(coordinateLsaPublisher)
   , m_nameLsaPublisher(nameLsaPublisher)
diff --git a/src/publisher/lsdb-status-publisher.hpp b/src/publisher/lsdb-status-publisher.hpp
index b8145a8..f9d5841 100644
--- a/src/publisher/lsdb-status-publisher.hpp
+++ b/src/publisher/lsdb-status-publisher.hpp
@@ -39,7 +39,6 @@
 public:
   LsdbStatusPublisher(Lsdb& lsdb,
                       ndn::Face& face,
-                      const ndn::Name& prefix,
                       ndn::KeyChain& keyChain,
                       AdjacencyLsaPublisher& adjacencyLsaPublisher,
                       CoordinateLsaPublisher& coordinateLsaPublisher,
diff --git a/src/publisher/segment-publisher.hpp b/src/publisher/segment-publisher.hpp
index cb311e9..5aefa52 100644
--- a/src/publisher/segment-publisher.hpp
+++ b/src/publisher/segment-publisher.hpp
@@ -39,11 +39,9 @@
 {
 public:
   SegmentPublisher(FaceBase& face,
-                   const ndn::Name& prefix,
                    ndn::KeyChain& keyChain,
                    const ndn::time::milliseconds& freshnessPeriod = getDefaultFreshness())
     : m_face(face)
-    , m_prefix(prefix)
     , m_keyChain(keyChain)
     , m_freshnessPeriod(freshnessPeriod)
   {
@@ -67,8 +65,10 @@
     return ndn::time::milliseconds(1000);
   }
 
+  /** \brief Publish data under provided prefix
+   */
   void
-  publish()
+  publish(const ndn::Name& prefix)
   {
     ndn::EncodingBuffer buffer;
     generate(buffer);
@@ -77,7 +77,7 @@
     const uint8_t* segmentBegin = rawBuffer;
     const uint8_t* end = rawBuffer + buffer.size();
 
-    ndn::Name segmentPrefix(m_prefix);
+    ndn::Name segmentPrefix(prefix);
     segmentPrefix.appendVersion();
 
     uint64_t segmentNo = 0;
@@ -120,7 +120,6 @@
 
 private:
   FaceBase& m_face;
-  const ndn::Name m_prefix;
   ndn::KeyChain& m_keyChain;
   const ndn::time::milliseconds m_freshnessPeriod;
 };