publisher: simplified LSDB dataset interest handler.

refs: #4235

Change-Id: Ia3f606947884304e8b01211b5887ad62488f7167
diff --git a/src/lsdb.cpp b/src/lsdb.cpp
index 789edf6..480248e 100644
--- a/src/lsdb.cpp
+++ b/src/lsdb.cpp
@@ -366,7 +366,7 @@
 }
 
 const std::list<NameLsa>&
-Lsdb::getNameLsdb()
+Lsdb::getNameLsdb() const
 {
   return m_nameLsdb;
 }
@@ -567,7 +567,7 @@
 }
 
 const std::list<CoordinateLsa>&
-Lsdb::getCoordinateLsdb()
+Lsdb::getCoordinateLsdb() const
 {
   return m_corLsdb;
 }
@@ -805,7 +805,7 @@
 }
 
 const std::list<AdjLsa>&
-Lsdb::getAdjLsdb()
+Lsdb::getAdjLsdb() const
 {
   return m_adjLsdb;
 }
diff --git a/src/lsdb.hpp b/src/lsdb.hpp
index 4574242..3a63e68 100644
--- a/src/lsdb.hpp
+++ b/src/lsdb.hpp
@@ -95,7 +95,7 @@
   writeNameLsdbLog();
 
   const std::list<NameLsa>&
-  getNameLsdb();
+  getNameLsdb() const;
 
   /*! \brief Builds a cor. LSA for this router and installs it into the LSDB. */
   bool
@@ -134,7 +134,7 @@
   writeCorLsdbLog();
 
   const std::list<CoordinateLsa>&
-  getCoordinateLsdb();
+  getCoordinateLsdb() const;
 
   //function related to Adj LSDB
 
@@ -175,7 +175,7 @@
   findAdjLsa(const ndn::Name& key);
 
   const std::list<AdjLsa>&
-  getAdjLsdb();
+  getAdjLsdb() const;
 
   void
   setAdjLsaBuildInterval(uint32_t interval)
diff --git a/src/publisher/lsa-publisher.cpp b/src/publisher/lsa-publisher.cpp
deleted file mode 100644
index 6bfc1dc..0000000
--- a/src/publisher/lsa-publisher.cpp
+++ /dev/null
@@ -1,137 +0,0 @@
-/* -*- 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 "lsa-publisher.hpp"
-
-#include "lsa.hpp"
-
-#include <ndn-cxx/face.hpp>
-
-namespace nlsr {
-
-const ndn::Name::Component AdjacencyLsaPublisher::DATASET_COMPONENT =
-  ndn::Name::Component("adjacencies");
-
-AdjacencyLsaPublisher::AdjacencyLsaPublisher(Lsdb& lsdb,
-                                             ndn::Face& face,
-                                             ndn::KeyChain& keyChain)
-  : m_adjacencyLsas(lsdb.getAdjLsdb())
-{
-}
-
-std::list<tlv::AdjacencyLsa>
-AdjacencyLsaPublisher::getTlvLsas()
-{
-  std::list<tlv::AdjacencyLsa> lsas;
-
-  for (AdjLsa lsa : m_adjacencyLsas) {
-    tlv::AdjacencyLsa tlvLsa;
-
-    std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(lsa);
-    tlvLsa.setLsaInfo(*tlvLsaInfo);
-
-    for (const Adjacent& adj : lsa.getAdl().getAdjList()) {
-      tlv::Adjacency tlvAdj;
-      tlvAdj.setName(adj.getName());
-      tlvAdj.setUri(adj.getFaceUri().toString());
-      tlvAdj.setCost(adj.getLinkCost());
-      tlvLsa.addAdjacency(tlvAdj);
-    }
-
-    lsas.push_back(tlvLsa);
-  }
-
-  return lsas;
-}
-
-//////////////////////////////////////////////////////////////////////
-//////////////////////////////////////////////////////////////////////
-//////////////////////////////////////////////////////////////////////
-
-const ndn::Name::Component CoordinateLsaPublisher::DATASET_COMPONENT =
-  ndn::Name::Component("coordinates");
-
-CoordinateLsaPublisher::CoordinateLsaPublisher(Lsdb& lsdb,
-                                               ndn::Face& face,
-                                               ndn::KeyChain& keyChain)
-  : m_coordinateLsas(lsdb.getCoordinateLsdb())
-{
-}
-
-  // Returns the list of coordinate LSAs represented by this object.
-std::list<tlv::CoordinateLsa>
-CoordinateLsaPublisher::getTlvLsas()
-{
-  std::list<tlv::CoordinateLsa> lsas;
-
-  for (const CoordinateLsa lsa : m_coordinateLsas) {
-    tlv::CoordinateLsa tlvLsa;
-
-    std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(lsa);
-    tlvLsa.setLsaInfo(*tlvLsaInfo);
-
-    tlvLsa.setHyperbolicRadius(lsa.getCorRadius());
-    tlvLsa.setHyperbolicAngle(lsa.getCorTheta());
-
-    lsas.push_back(tlvLsa);
-  }
-
-  return lsas;
-}
-
-//////////////////////////////////////////////////////////////////////
-//////////////////////////////////////////////////////////////////////
-//////////////////////////////////////////////////////////////////////
-
-const ndn::Name::Component NameLsaPublisher::DATASET_COMPONENT =
-  ndn::Name::Component("names");
-
-NameLsaPublisher::NameLsaPublisher(Lsdb& lsdb,
-                                   ndn::Face& face,
-                                   ndn::KeyChain& keyChain)
-  : m_nameLsas(lsdb.getNameLsdb())
-{
-}
-
-  // Returns the list of name LSAs represented by this object.
-  // Note: each name LSA has a list of prefixes as well.
-std::list<tlv::NameLsa>
-NameLsaPublisher::getTlvLsas()
-{
-  std::list<tlv::NameLsa> lsas;
-
-  for (NameLsa lsa : m_nameLsas) {
-    tlv::NameLsa tlvLsa;
-
-    std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(lsa);
-    tlvLsa.setLsaInfo(*tlvLsaInfo);
-
-    for (const ndn::Name& name : lsa.getNpl().getNames()) {
-      tlvLsa.addName(name);
-    }
-
-    lsas.push_back(tlvLsa);
-  }
-
-  return lsas;
-}
-
-} // namespace nlsr
diff --git a/src/publisher/lsa-publisher.hpp b/src/publisher/lsa-publisher.hpp
deleted file mode 100644
index 6726b5a..0000000
--- a/src/publisher/lsa-publisher.hpp
+++ /dev/null
@@ -1,108 +0,0 @@
-/* -*- 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/>.
- **/
-
-#ifndef NLSR_PUBLISHER_LSA_PUBLISHER_HPP
-#define NLSR_PUBLISHER_LSA_PUBLISHER_HPP
-
-#include "lsdb.hpp"
-#include "segment-publisher.hpp"
-#include "tlv/adjacency-lsa.hpp"
-#include "tlv/coordinate-lsa.hpp"
-#include "tlv/name-lsa.hpp"
-
-#include <ndn-cxx/face.hpp>
-
-namespace nlsr {
-
-  /*! \brief Class to publish adjacency lsa dataset
-
-    \sa https://redmine.named-data.net/projects/nlsr/wiki/LSDB_DataSet
-
-  */
-class AdjacencyLsaPublisher
-{
-public:
-  AdjacencyLsaPublisher(Lsdb& lsdb,
-                        ndn::Face& face,
-                        ndn::KeyChain& keyChain);
-
-  /*! \brief Generates an TLV-format AdjacencyLsa from AdjacencyLsas
-   * and their Adjacents.
-   */
-  std::list<tlv::AdjacencyLsa>
-  getTlvLsas();
-
-public:
-  static const ndn::Name::Component DATASET_COMPONENT;
-
-private:
-  const std::list<AdjLsa>& m_adjacencyLsas;
-};
-
-  /*! \brief Class to publish coordinate lsa dataset
-    \sa https://redmine.named-data.net/projects/nlsr/wiki/LSDB_DataSet
-  */
-class CoordinateLsaPublisher
-{
-public:
-  CoordinateLsaPublisher(Lsdb& lsdb,
-                        ndn::Face& face,
-                        ndn::KeyChain& keyChain);
-
-  /*! \brief Generates a TLV-format CoordinateLsa from CoordinateLsas
-   * and their hyperbolic coordinates.
-   */
-  std::list<tlv::CoordinateLsa>
-  getTlvLsas();
-
-public:
-  static const ndn::Name::Component DATASET_COMPONENT;
-
-private:
-  const std::list<CoordinateLsa>& m_coordinateLsas;
-};
-
-  /*! \brief Class to publish name lsa dataset
-    \sa https://redmine.named-data.net/projects/nlsr/wiki/LSDB_DataSet
-  */
-class NameLsaPublisher
-{
-public:
-  NameLsaPublisher(Lsdb& lsdb,
-                   ndn::Face& face,
-                   ndn::KeyChain& keyChain);
-
-  /*! \brief Generates a TLV-format NameLsa from NameLsas and their
-   * list of name prefixes.
-   */
-  std::list<tlv::NameLsa>
-  getTlvLsas();
-
-public:
-  static const ndn::Name::Component DATASET_COMPONENT;
-
-private:
-  const std::list<NameLsa>& m_nameLsas;
-};
-
-} // namespace nlsr
-
-#endif // NLSR_PUBLISHER_LSA_PUBLISHER_HPP
diff --git a/src/publisher/lsdb-dataset-interest-handler.cpp b/src/publisher/lsdb-dataset-interest-handler.cpp
index 30634ac..7a4ba1c 100644
--- a/src/publisher/lsdb-dataset-interest-handler.cpp
+++ b/src/publisher/lsdb-dataset-interest-handler.cpp
@@ -27,9 +27,10 @@
  */
 
 #include "lsdb-dataset-interest-handler.hpp"
-#include "logger.hpp"
+
 #include "nlsr.hpp"
 #include "tlv/lsdb-status.hpp"
+#include "logger.hpp"
 
 #include <ndn-cxx/face.hpp>
 #include <ndn-cxx/mgmt/nfd/control-response.hpp>
@@ -49,14 +50,9 @@
                                                        ndn::mgmt::Dispatcher& routerNameDispatcher,
                                                        ndn::Face& face,
                                                        ndn::KeyChain& keyChain)
-  : m_localhostDispatcher(localHostDispatcher)
+  : m_lsdb(lsdb)
+  , m_localhostDispatcher(localHostDispatcher)
   , m_routerNameDispatcher(routerNameDispatcher)
-  , m_adjacencyLsaPublisher(lsdb, face, keyChain)
-  , m_coordinateLsaPublisher(lsdb, face, keyChain)
-  , m_nameLsaPublisher(lsdb, face, keyChain)
-  , m_adjacencyLsas(lsdb.getAdjLsdb())
-  , m_coordinateLsas(lsdb.getCoordinateLsdb())
-  , m_nameLsas(lsdb.getNameLsdb())
 {
   _LOG_DEBUG("Setting dispatcher for lsdb status dataset:");
   setDispatcher(m_localhostDispatcher);
@@ -85,12 +81,16 @@
                                              ndn::mgmt::StatusDatasetContext& context)
 {
   _LOG_DEBUG("Received interest:  " << interest);
-  for (AdjLsa lsa : m_adjacencyLsas) {
+
+  auto lsaRange = std::make_pair<std::list<AdjLsa>::const_iterator,
+                                 std::list<AdjLsa>::const_iterator>(
+    m_lsdb.getAdjLsdb().cbegin(), m_lsdb.getAdjLsdb().cend());
+  for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
     tlv::AdjacencyLsa tlvLsa;
-    std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(lsa);
+    std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
     tlvLsa.setLsaInfo(*tlvLsaInfo);
 
-    for (const Adjacent& adj : lsa.getAdl().getAdjList()) {
+    for (const Adjacent& adj : lsa->getAdl().getAdjList()) {
       tlv::Adjacency tlvAdj;
       tlvAdj.setName(adj.getName());
       tlvAdj.setUri(adj.getFaceUri().toString());
@@ -107,14 +107,18 @@
 LsdbDatasetInterestHandler::publishCoordinateStatus(const ndn::Name& topPrefix, const ndn::Interest& interest,
                                                     ndn::mgmt::StatusDatasetContext& context)
 {
+  auto lsaRange = std::make_pair<std::list<CoordinateLsa>::const_iterator,
+                                 std::list<CoordinateLsa>::const_iterator>(
+    m_lsdb.getCoordinateLsdb().cbegin(), m_lsdb.getCoordinateLsdb().cend());
+
   _LOG_DEBUG("Received interest:  " << interest);
-  for (const CoordinateLsa lsa : m_coordinateLsas) {
+  for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
     tlv::CoordinateLsa tlvLsa;
-    std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(lsa);
+    std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
     tlvLsa.setLsaInfo(*tlvLsaInfo);
 
-    tlvLsa.setHyperbolicRadius(lsa.getCorRadius());
-    tlvLsa.setHyperbolicAngle(lsa.getCorTheta());
+    tlvLsa.setHyperbolicRadius(lsa->getCorRadius());
+    tlvLsa.setHyperbolicAngle(lsa->getCorTheta());
 
     const ndn::Block& wire = tlvLsa.wireEncode();
     context.append(wire);
@@ -126,14 +130,16 @@
 LsdbDatasetInterestHandler::publishNameStatus(const ndn::Name& topPrefix, const ndn::Interest& interest,
                                               ndn::mgmt::StatusDatasetContext& context)
 {
+  auto lsaRange = std::make_pair<std::list<NameLsa>::const_iterator, std::list<NameLsa>::const_iterator>(
+    m_lsdb.getNameLsdb().cbegin(), m_lsdb.getNameLsdb().cend());
   _LOG_DEBUG("Received interest:  " << interest);
-  for (NameLsa lsa : m_nameLsas) {
+  for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
     tlv::NameLsa tlvLsa;
 
-    std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(lsa);
+    std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
     tlvLsa.setLsaInfo(*tlvLsaInfo);
 
-    for (const ndn::Name& name : lsa.getNpl().getNames()) {
+    for (const ndn::Name& name : lsa->getNpl().getNames()) {
       tlvLsa.addName(name);
     }
 
@@ -149,15 +155,15 @@
 {
   _LOG_DEBUG("Received interest:  " << interest);
   tlv::LsdbStatus lsdbStatus;
-  for (const tlv::AdjacencyLsa& tlvLsa : m_adjacencyLsaPublisher.getTlvLsas()) {
+  for (const tlv::AdjacencyLsa& tlvLsa : getTlvLsas<tlv::AdjacencyLsa>(m_lsdb)) {
     lsdbStatus.addAdjacencyLsa(tlvLsa);
   }
 
-  for (const tlv::CoordinateLsa& tlvLsa : m_coordinateLsaPublisher.getTlvLsas()) {
+  for (const tlv::CoordinateLsa& tlvLsa : getTlvLsas<tlv::CoordinateLsa>(m_lsdb)) {
     lsdbStatus.addCoordinateLsa(tlvLsa);
   }
 
-  for (const tlv::NameLsa& tlvLsa : m_nameLsaPublisher.getTlvLsas()) {
+  for (const tlv::NameLsa& tlvLsa : getTlvLsas<tlv::NameLsa>(m_lsdb)) {
     lsdbStatus.addNameLsa(tlvLsa);
   }
   const ndn::Block& wire = lsdbStatus.wireEncode();
@@ -165,4 +171,84 @@
   context.end();
 }
 
+template<> std::list<tlv::AdjacencyLsa>
+getTlvLsas<tlv::AdjacencyLsa>(const Lsdb& lsdb)
+{
+  std::list<tlv::AdjacencyLsa> lsas;
+
+  auto lsaRange = std::make_pair<std::list<AdjLsa>::const_iterator,
+                                 std::list<AdjLsa>::const_iterator>(
+    lsdb.getAdjLsdb().cbegin(), lsdb.getAdjLsdb().cend());
+  for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
+    tlv::AdjacencyLsa tlvLsa;
+
+    std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
+    tlvLsa.setLsaInfo(*tlvLsaInfo);
+
+    for (const Adjacent& adj : lsa->getAdl().getAdjList()) {
+      tlv::Adjacency tlvAdj;
+      tlvAdj.setName(adj.getName());
+      tlvAdj.setUri(adj.getFaceUri().toString());
+      tlvAdj.setCost(adj.getLinkCost());
+      tlvLsa.addAdjacency(tlvAdj);
+    }
+
+    lsas.push_back(tlvLsa);
+  }
+
+  return lsas;
+
+}
+
+template<> std::list<tlv::CoordinateLsa>
+getTlvLsas<tlv::CoordinateLsa>(const Lsdb& lsdb)
+{
+  std::list<tlv::CoordinateLsa> lsas;
+
+  auto lsaRange = std::make_pair<std::list<CoordinateLsa>::const_iterator,
+                                 std::list<CoordinateLsa>::const_iterator>(
+    lsdb.getCoordinateLsdb().cbegin(), lsdb.getCoordinateLsdb().cend());
+
+  for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
+    tlv::CoordinateLsa tlvLsa;
+
+    std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
+    tlvLsa.setLsaInfo(*tlvLsaInfo);
+
+    tlvLsa.setHyperbolicRadius(lsa->getCorRadius());
+    tlvLsa.setHyperbolicAngle(lsa->getCorTheta());
+
+    lsas.push_back(tlvLsa);
+  }
+
+  return lsas;
+
+}
+
+template<> std::list<tlv::NameLsa>
+getTlvLsas<tlv::NameLsa>(const Lsdb& lsdb)
+{
+  std::list<tlv::NameLsa> lsas;
+
+  auto lsaRange = std::make_pair<std::list<NameLsa>::const_iterator,
+                                 std::list<NameLsa>::const_iterator>(
+    lsdb.getNameLsdb().cbegin(), lsdb.getNameLsdb().cend());
+  for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
+    tlv::NameLsa tlvLsa;
+
+    std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
+    tlvLsa.setLsaInfo(*tlvLsaInfo);
+
+    for (const ndn::Name& name : lsa->getNpl().getNames()) {
+      tlvLsa.addName(name);
+    }
+
+    lsas.push_back(tlvLsa);
+  }
+
+  return lsas;
+
+}
+
+
 } // namespace nlsr
diff --git a/src/publisher/lsdb-dataset-interest-handler.hpp b/src/publisher/lsdb-dataset-interest-handler.hpp
index b92a72f..e01a5da 100644
--- a/src/publisher/lsdb-dataset-interest-handler.hpp
+++ b/src/publisher/lsdb-dataset-interest-handler.hpp
@@ -22,10 +22,10 @@
 #ifndef NLSR_PUBLISHER_LSDB_DATASET_INTEREST_HANDLER_HPP
 #define NLSR_PUBLISHER_LSDB_DATASET_INTEREST_HANDLER_HPP
 
-#include "lsa-publisher.hpp"
 #include "tlv/adjacency-lsa.hpp"
 #include "tlv/coordinate-lsa.hpp"
 #include "tlv/name-lsa.hpp"
+#include "lsdb.hpp"
 
 #include <ndn-cxx/mgmt/dispatcher.hpp>
 #include <ndn-cxx/face.hpp>
@@ -33,6 +33,12 @@
 
 namespace nlsr {
 
+namespace dataset {
+  const ndn::Name::Component ADJACENCY_COMPONENT = ndn::Name::Component{"adjacencies"};
+  const ndn::Name::Component NAME_COMPONENT = ndn::Name::Component{"names"};
+  const ndn::Name::Component COORDINATE_COMPONENT = ndn::Name::Component{"coordinates"};
+} // namespace dataset
+
 /*!
    \brief Class to publish all lsa dataset
    \sa https://redmine.named-data.net/projects/nlsr/wiki/LSDB_DataSet
@@ -100,20 +106,25 @@
                    ndn::mgmt::StatusDatasetContext& context);
 
 private:
+  const Lsdb& m_lsdb;
   ndn::Name m_routerNamePrefix;
 
   ndn::mgmt::Dispatcher& m_localhostDispatcher;
   ndn::mgmt::Dispatcher& m_routerNameDispatcher;
-
-  AdjacencyLsaPublisher m_adjacencyLsaPublisher;
-  CoordinateLsaPublisher m_coordinateLsaPublisher;
-  NameLsaPublisher m_nameLsaPublisher;
-
-  const std::list<AdjLsa>& m_adjacencyLsas;
-  const std::list<CoordinateLsa>& m_coordinateLsas;
-  const std::list<NameLsa>& m_nameLsas;
 };
 
+template<typename T> std::list<T>
+getTlvLsas(const Lsdb& lsdb);
+
+template<> std::list<tlv::AdjacencyLsa>
+getTlvLsas<tlv::AdjacencyLsa>(const Lsdb& lsdb);
+
+template<> std::list<tlv::CoordinateLsa>
+getTlvLsas<tlv::CoordinateLsa>(const Lsdb& lsdb);
+
+template<> std::list<tlv::NameLsa>
+getTlvLsas<tlv::NameLsa>(const Lsdb& lsdb);
+
 } // namespace nlsr
 
 #endif // NLSR_PUBLISHER_LSDB_DATASET_INTEREST_HANDLER_HPP