src: use InMemoryStoragePersistent instead of custom LsaSegmentStorage

refs: #4788

Change-Id: Ie5d8721915864ead2f30e7f5e6181f56acefc559
diff --git a/src/lsa-segment-storage.cpp b/src/lsa-segment-storage.cpp
deleted file mode 100644
index e7d6f2d..0000000
--- a/src/lsa-segment-storage.cpp
+++ /dev/null
@@ -1,147 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/*
- * Copyright (c) 2014-2019,  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-segment-storage.hpp"
-#include "logger.hpp"
-#include "lsa.hpp"
-#include "utility/name-helper.hpp"
-#include "conf-parameter.hpp"
-
-namespace nlsr {
-
-INIT_LOGGER(LsaSegmentStorage);
-
-LsaSegmentStorage::LsaSegmentStorage(ndn::Scheduler& scheduler)
-  : m_scheduler(scheduler)
-{
-}
-
-void
-LsaSegmentStorage::connectToFetcher(ndn::util::SegmentFetcher& fetcher)
-{
-  fetcher.afterSegmentValidated.connect(std::bind(&LsaSegmentStorage::afterFetcherSignalEmitted,
-                                                  this, _1));
-}
-
-const ndn::Data*
-LsaSegmentStorage::getLsaSegment(const ndn::Interest& interest)
-{
-  ndn::Name lsaSegmentsKey = interest.getName();
-
-  // If this is the first interest then it does not contain the segment number,
-  // so need to append zero segment component at the end to match with the data
-  if (lsaSegmentsKey.size() > 0) {
-    if (!lsaSegmentsKey.get(-1).isSegment()) {
-      lsaSegmentsKey.appendSegment(0);
-    }
-
-    auto it = m_lsaSegments.find(lsaSegmentsKey);
-    if (it == m_lsaSegments.end()) {
-      NLSR_LOG_TRACE("Data for interest: " << interest.getName() << " cannot be found in the lsa storage");
-
-      return nullptr;
-    }
-    else {
-      NLSR_LOG_TRACE("Data for interest: " << interest.getName() << " is in the storage.");
-      return &(it->second);
-    }
-  }
-  else {
-    NLSR_LOG_ERROR("Received interest has empty name.");
-    return nullptr;
-  }
-}
-
-void
-LsaSegmentStorage::afterFetcherSignalEmitted(const ndn::Data& lsaSegment)
-{
-  NLSR_LOG_TRACE("Received a LSA segment: " << lsaSegment.getName());
-
-  // lsaSegmentName is /<router-prefix>/<LS type>/<sequence no.>/<version no.>/<segment no.>
-  auto lsaSegmentName = lsaSegment.getName();
-
-  if (lsaSegmentName.size() > 0) {
-    // lsaSegmentsKey is /<router-prefix>/<LS type>/<sequence no.>/<segment no.>
-    ndn::Name lsaSegmentsKey(lsaSegmentName.getPrefix(lsaSegmentName.size() - 2));
-    lsaSegmentsKey.append(lsaSegmentName.get(-1));
-
-    // No need to store same LSA multiple time
-    if (m_lsaSegments.find(lsaSegmentsKey) == m_lsaSegments.end()) {
-      NLSR_LOG_TRACE("Received LSA segment is new. Storing it in the storage.");
-      NLSR_LOG_TRACE("LSA data segment's name: " << lsaSegmentName);
-
-      // Delete the same LSA with lower sequence number
-      deleteOldLsas(lsaSegmentName);
-
-      m_lsaSegments[lsaSegmentsKey] = lsaSegment;
-    }
-    else {
-      NLSR_LOG_TRACE("The received segment is already in the storage.");
-    }
-
-    ndn::time::seconds expirationTime(LSA_REFRESH_TIME_DEFAULT);
-
-    // schedule the segment deletion
-    scheduleLsaSegmentDeletion(lsaSegmentsKey, expirationTime);
-  }
-  else {
-    NLSR_LOG_ERROR("The received LSA segment has empty name.");
-  }
-}
-
-void
-LsaSegmentStorage::deleteOldLsas(const ndn::Name& newLsaName)
-{
-  auto newLsaKey = newLsaName.getPrefix(newLsaName.size() - 3);
-  auto newSeqNo = newLsaName.get(-3).toNumber();
-
-  std::vector<decltype(m_lsaSegments)::key_type> lsaToDelete;
-
-  for (const auto& segment : m_lsaSegments) {
-    ndn::Name segmentKey = segment.first;
-    auto oldSeqNo = segmentKey.get(-2).toNumber();
-    auto existingLsaKey = segmentKey.getPrefix(segmentKey.size() - 2);
-
-    if (newLsaKey == existingLsaKey) {
-      if (newSeqNo > oldSeqNo) { // in the key the second last component is the sequence number
-        NLSR_LOG_TRACE("Outdated LSA: " << segmentKey << " with seq no: " <<
-                       oldSeqNo << " is deleted.");
-        lsaToDelete.push_back(segmentKey);
-      }
-    }
-  }
-
-  for (const auto& segmentKey : lsaToDelete) {
-    m_lsaSegments.erase(segmentKey);
-  }
-}
-
-void
-LsaSegmentStorage::scheduleLsaSegmentDeletion(const ndn::Name& lsaSegmentsKey,
-                                              ndn::time::seconds expirationTime)
-{
-  NLSR_LOG_TRACE("Scheduling LSA segment deletion for "
-                 << lsaSegmentsKey << " in: " << expirationTime);
-  m_scheduler.schedule(expirationTime,
-                       [lsaSegmentsKey, this] { m_lsaSegments.erase(lsaSegmentsKey); });
-}
-
-} // namespace nlsr
diff --git a/src/lsa-segment-storage.hpp b/src/lsa-segment-storage.hpp
deleted file mode 100644
index 474f422..0000000
--- a/src/lsa-segment-storage.hpp
+++ /dev/null
@@ -1,85 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2018,  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_LSA_SEGMENT_STORAGE_HPP
-#define NLSR_LSA_SEGMENT_STORAGE_HPP
-
-#include "test-access-control.hpp"
-
-#include <ndn-cxx/util/segment-fetcher.hpp>
-#include <ndn-cxx/util/signal.hpp>
-#include <ndn-cxx/util/time.hpp>
-
-#include <vector>
-#include <tuple>
-
-namespace nlsr {
-
-class LsaSegmentStorage
-{
-public:
-  LsaSegmentStorage(ndn::Scheduler& scheduler);
-
-  /*! \brief Get connected to the signal emitted by SegmentFetcher
-   * \param fetcher The SegmentFetcher to whose signal LsaSegmentStorage will subscribe to.
-   */
-  void
-  connectToFetcher(ndn::util::SegmentFetcher& fetcher);
-
-  /*! \brief Returns an LSA segment for an interest from LsaSegmentStorage
-   * \param interest Interest corresponding to the returned LSA segment.
-   */
-  const ndn::Data*
-  getLsaSegment(const ndn::Interest& interest);
-
-PUBLIC_WITH_TESTS_ELSE_PRIVATE:
-  /*! \brief Callback when SegmentFetcher retrieves a segment.
-   */
-   void
-   afterFetcherSignalEmitted(const ndn::Data& lsaSegment);
-
-private:
-  /*! \brief Given an LSA name check whether Data for the same name exists in the
-   * LsaSegmentStorage. If the matched LSA data are of a lower sequence number,
-   * then remove them from LsaSegmentStorage.
-   * \param newLsaName Name of the LSA that will be matched against
-   */
-  void
-  deleteOldLsas(const ndn::Name& newLsaName);
-
-  /*! \brief Schedules the deletion of a LSA data given the segmentKey
-   */
-  void
-  scheduleLsaSegmentDeletion(const ndn::Name& segmentKey, ndn::time::seconds expirationTime);
-
-
-private:
-  ndn::Scheduler& m_scheduler;
-
-  // Key: /<router-prefix>/<LS type>/<sequence no.>/<segment no.>
-  // Value: corresponding LSA data packet
-  //        Data name: /<router-prefix>/<LS type>/<sequence no.>/<version no.>/<segment no.>
-  std::unordered_map<ndn::Name, ndn::Data> m_lsaSegments;
-};
-
-} // namespace nlsr
-
-#endif // NLSR_LSA_SEGMENT_STORAGE_HPP
diff --git a/src/lsdb.cpp b/src/lsdb.cpp
index 58963f4..d917fa6 100644
--- a/src/lsdb.cpp
+++ b/src/lsdb.cpp
@@ -22,7 +22,6 @@
 #include "lsdb.hpp"
 
 #include "logger.hpp"
-#include "lsa-segment-storage.hpp"
 #include "nlsr.hpp"
 #include "utility/name-helper.hpp"
 
@@ -51,7 +50,6 @@
                    const uint64_t& sequenceNumber) {
              return isLsaNew(routerName, lsaType, sequenceNumber);
            }, m_confParam)
-  , m_lsaStorage(m_scheduler)
   , m_lsaRefreshTime(ndn::time::seconds(m_confParam.getLsaRefreshTime()))
   , m_thisRouterPrefix(m_confParam.getRouterPrefix().toUri())
   , m_adjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval())
@@ -747,7 +745,6 @@
 {
   auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
                          std::bind(adjLsaCompareByKey, _1, key));
-
   if (it != m_adjLsdb.end()) {
     NLSR_LOG_DEBUG("Deleting Adj Lsa");
     it->writeLog();
@@ -956,22 +953,29 @@
 
   auto it = m_fetchers.insert(fetcher).first;
 
-  fetcher->onComplete.connect([this, interestName, it] (ndn::ConstBufferPtr bufferPtr) {
-                                afterFetchLsa(bufferPtr, interestName);
-                                m_fetchers.erase(it);
-                              });
-
-  fetcher->onError.connect([this, interestName, timeoutCount, deadline, lsaName, seqNo, it]
-                           (uint32_t errorCode, const std::string& msg) {
-                             onFetchLsaError(errorCode, msg, interestName,
-                                             timeoutCount, deadline, lsaName, seqNo);
-                             m_fetchers.erase(it);
-                           });
-
-  m_lsaStorage.connectToFetcher(*fetcher);
   fetcher->afterSegmentValidated.connect([this] (const ndn::Data& data) {
-                                          afterSegmentValidatedSignal(data);
-                                         });
+    // Nlsr class subscribes to this to fetch certificates
+    afterSegmentValidatedSignal(data);
+
+    // If we don't do this IMS throws: std::bad_weak_ptr: bad_weak_ptr
+    auto lsaSegment = std::make_shared<const ndn::Data>(data);
+    m_lsaStorage.insert(*lsaSegment);
+    const ndn::Name& segmentName = lsaSegment->getName();
+    // Schedule deletion of the segment
+    m_scheduler.schedule(ndn::time::seconds(LSA_REFRESH_TIME_DEFAULT),
+                         [this, segmentName] { m_lsaStorage.erase(segmentName); });
+  });
+
+  fetcher->onComplete.connect([=] (const ndn::ConstBufferPtr& bufferPtr) {
+    m_lsaStorage.erase(ndn::Name(lsaName).appendNumber(seqNo - 1));
+    afterFetchLsa(bufferPtr, interestName);
+    m_fetchers.erase(it);
+  });
+
+  fetcher->onError.connect([=] (uint32_t errorCode, const std::string& msg) {
+    onFetchLsaError(errorCode, msg, interestName, timeoutCount, deadline, lsaName, seqNo);
+    m_fetchers.erase(it);
+  });
 
   // increment a specific SENT_LSA_INTEREST
   Lsa::Type lsaType;
@@ -1043,8 +1047,8 @@
     lsaIncrementSignal(Statistics::PacketType::SENT_LSA_DATA);
   }
   else { // else the interest is for other router's lsa, serve from LsaSegmentStorage
-    const ndn::Data* lsaSegment = m_lsaStorage.getLsaSegment(interest);
-    if (lsaSegment != nullptr) {
+    std::shared_ptr<const ndn::Data> lsaSegment = m_lsaStorage.find(interest);
+    if (lsaSegment) {
       NLSR_LOG_TRACE("Found data in lsa storage. Sending the data for " << interest.getName());
       m_face.put(*lsaSegment);
     }
diff --git a/src/lsdb.hpp b/src/lsdb.hpp
index 2c68906..9d6575f 100644
--- a/src/lsdb.hpp
+++ b/src/lsdb.hpp
@@ -24,7 +24,6 @@
 
 #include "conf-parameter.hpp"
 #include "lsa.hpp"
-#include "lsa-segment-storage.hpp"
 #include "sequencing-manager.hpp"
 #include "test-access-control.hpp"
 #include "communication/sync-logic-handler.hpp"
@@ -35,6 +34,7 @@
 #include <ndn-cxx/util/signal.hpp>
 #include <ndn-cxx/util/time.hpp>
 #include <ndn-cxx/util/segment-fetcher.hpp>
+#include <ndn-cxx/ims/in-memory-storage-persistent.hpp>
 
 #include <PSync/segment-publisher.hpp>
 
@@ -52,12 +52,6 @@
 
   ~Lsdb();
 
-  SyncLogicHandler&
-  getSyncLogicHandler()
-  {
-    return m_sync;
-  }
-
   bool
   isLsaNew(const ndn::Name& routerName, const Lsa::Type& lsaType, const uint64_t& sequenceNumber);
 
@@ -191,18 +185,6 @@
     m_adjLsaBuildInterval = ndn::time::seconds(interval);
   }
 
-  const ndn::time::seconds&
-  getAdjLsaBuildInterval() const
-  {
-    return m_adjLsaBuildInterval;
-  }
-
-  SequencingManager&
-  getSequencingManager()
-  {
-    return m_sequencingManager;
-  }
-
   void
   writeAdjLsdbLog();
 
@@ -326,6 +308,7 @@
   processInterestForCoordinateLsa(const ndn::Interest& interest,
                                   const ndn::Name& lsaKey,
                                   uint64_t seqNo);
+
   void
   onContentValidated(const std::shared_ptr<const ndn::Data>& data);
 
@@ -392,10 +375,9 @@
   ConfParameter& m_confParam;
   NamePrefixTable& m_namePrefixTable;
   RoutingTable& m_routingTable;
-  SyncLogicHandler m_sync;
 
 PUBLIC_WITH_TESTS_ELSE_PRIVATE:
-  LsaSegmentStorage m_lsaStorage;
+  SyncLogicHandler m_sync;
 
 private:
   std::list<NameLsa> m_nameLsdb;
@@ -414,10 +396,12 @@
   static const ndn::time::seconds GRACE_PERIOD;
   static const ndn::time::steady_clock::TimePoint DEFAULT_LSA_RETRIEVAL_DEADLINE;
 
+PUBLIC_WITH_TESTS_ELSE_PRIVATE:
   ndn::time::seconds m_adjLsaBuildInterval;
 
   SequencingManager m_sequencingManager;
 
+private:
   ndn::util::signal::ScopedConnection m_onNewLsaConnection;
 
   std::set<std::shared_ptr<ndn::util::SegmentFetcher>> m_fetchers;
@@ -425,6 +409,9 @@
 
   bool m_isBuildAdjLsaSheduled;
   int64_t m_adjBuildCount;
+
+PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+  ndn::InMemoryStoragePersistent m_lsaStorage;
 };
 
 } // namespace nlsr
diff --git a/tests/communication/test-sync-logic-handler.cpp b/tests/communication/test-sync-logic-handler.cpp
index eba8064..b94bcb5 100644
--- a/tests/communication/test-sync-logic-handler.cpp
+++ b/tests/communication/test-sync-logic-handler.cpp
@@ -268,8 +268,7 @@
   BOOST_REQUIRE(lsa == nullptr);
 
   // Publish a routing update before an Adjacency LSA is built
-  BOOST_CHECK_NO_THROW(nlsr.m_lsdb.getSyncLogicHandler()
-                       .publishRoutingUpdate(Lsa::Type::ADJACENCY, 0));
+  BOOST_CHECK_NO_THROW(nlsr.m_lsdb.m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, 0));
 }
 
 BOOST_AUTO_TEST_SUITE_END()
diff --git a/tests/test-lsa-rule.cpp b/tests/test-lsa-rule.cpp
index 26c8e25..56b1f5c 100644
--- a/tests/test-lsa-rule.cpp
+++ b/tests/test-lsa-rule.cpp
@@ -120,7 +120,7 @@
   lsaDataName.append(std::to_string(Lsa::Type::NAME));
 
   // This would be the sequence number of its own NameLsa
-  lsaDataName.appendNumber(nlsr.m_lsdb.getSequencingManager().getNameLsaSeq());
+  lsaDataName.appendNumber(nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
 
   // Append version, segmentNo
   lsaDataName.appendNumber(1).appendNumber(1);
@@ -150,7 +150,7 @@
   lsaDataName.append(std::to_string(Lsa::Type::NAME));
 
   // This would be the sequence number of its own NameLsa
-  lsaDataName.appendNumber(nlsr.m_lsdb.getSequencingManager().getNameLsaSeq());
+  lsaDataName.appendNumber(nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
 
   // Append version, segmentNo
   lsaDataName.appendNumber(1).appendNumber(1);
diff --git a/tests/test-lsa-segment-storage.cpp b/tests/test-lsa-segment-storage.cpp
index 7ebd900..ac2671f 100644
--- a/tests/test-lsa-segment-storage.cpp
+++ b/tests/test-lsa-segment-storage.cpp
@@ -23,7 +23,6 @@
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "lsa-segment-storage.hpp"
 #include "test-common.hpp"
 #include "nlsr.hpp"
 #include "name-prefix-list.hpp"
@@ -38,38 +37,43 @@
 {
 public:
   LsaSegmentStorageFixture()
-    : face(m_ioService, m_keyChain)
+    : face(m_ioService, m_keyChain, {true, true})
     , conf(face)
+    , confProcessor(conf)
     , nlsr(face, m_keyChain, conf)
     , lsdb(nlsr.m_lsdb)
-    , lsaStorage(lsdb.m_lsaStorage)
+    , segmentPublisher(face, m_keyChain)
+    , numValidationSignal(0)
+    , afterSegmentValidatedConnection(lsdb.afterSegmentValidatedSignal.connect(
+                                      [this] (const ndn::Data& data) { ++numValidationSignal; }))
   {
+    std::string config = R"CONF(
+                            trust-anchor
+                              {
+                                type any
+                              }
+                          )CONF";
+    conf.m_validator.load(config, "config-file-from-string");
+
+    nlsr.initialize();
+
+    this->advanceClocks(ndn::time::milliseconds(10));
+
+    face.sentInterests.clear();
   }
 
-  std::string
-  makeLsaContent()
+  void
+  makeLsaContent(const ndn::Name& interestName, int numNames = 1000)
   {
-    ndn::Name s1{"name1"};
-    ndn::Name s2{"name2"};
-    NamePrefixList npl1{s1, s2};
+    NamePrefixList npl1;
+    for (int i = 0; i < numNames; ++i) {
+      npl1.insert("name1-" + std::to_string(i));
+    }
     NameLsa nameLsa("/ndn/other-site/%C1.Router/other-router", 12,
                     ndn::time::system_clock::now() + ndn::time::seconds(LSA_REFRESH_TIME_DEFAULT), npl1);
-    return nameLsa.serialize();
-  }
-
-  shared_ptr<ndn::Data>
-  makeLsaSegment(const ndn::Name& baseName, uint64_t segmentNo, bool isFinal)
-  {
-    ndn::Name lsaDataName(baseName);
-    lsaDataName.appendSegment(segmentNo);
-    auto lsaSegment = make_shared<ndn::Data>(ndn::Name(lsaDataName));
-    std::string content = makeLsaContent();
-    lsaSegment->setContent(reinterpret_cast<const uint8_t*>(content.c_str()), content.length());
-    if (isFinal) {
-      lsaSegment->setFinalBlock(lsaSegment->getName()[-1]);
-    }
-
-    return signData(lsaSegment);
+    segmentPublisher.publish(interestName, interestName,
+                             ndn::encoding::makeStringBlock(ndn::tlv::Content, nameLsa.serialize()),
+                             ndn::time::seconds(LSA_REFRESH_TIME_DEFAULT));
   }
 
   void
@@ -90,9 +94,13 @@
 public:
   ndn::util::DummyClientFace face;
   ConfParameter conf;
+  DummyConfFileProcessor confProcessor;
   Nlsr nlsr;
   Lsdb& lsdb;
-  LsaSegmentStorage& lsaStorage;
+  psync::SegmentPublisher segmentPublisher;
+
+  int numValidationSignal;
+  ndn::util::signal::ScopedConnection afterSegmentValidatedConnection;
 };
 
 BOOST_FIXTURE_TEST_SUITE(TestLsaSegmentStorage, LsaSegmentStorageFixture)
@@ -102,85 +110,39 @@
   ndn::Name lsaInterestName("/ndn/NLSR/LSA/other-site/%C1.Router/other-router/NAME");
   lsaInterestName.appendNumber(12);
 
-  ndn::Name lsaDataName(lsaInterestName);
-  lsaDataName.appendVersion();
+  lsdb.expressInterest(lsaInterestName, 0);
+  advanceClocks(ndn::time::milliseconds(10));
 
-  for (uint64_t segmentNo = 0; segmentNo < 4; ++segmentNo) {
-    auto lsaData = makeLsaSegment(lsaDataName, segmentNo, segmentNo == 4);
-    lsaStorage.afterFetcherSignalEmitted(*lsaData);
+  makeLsaContent(lsaInterestName);
+  advanceClocks(ndn::time::milliseconds(10));
+
+  for (const auto& interest : face.sentInterests) {
+    segmentPublisher.replyFromStore(interest.getName());
+    advanceClocks(ndn::time::milliseconds(10));
   }
 
-  // receive interest for other-router's LSA that is stored in this router's storage
-  for (uint64_t segmentNo = 0; segmentNo < 4; ++segmentNo) {
-    receiveLsaInterest(lsaInterestName, segmentNo, segmentNo == 0);
-  }
+  // 3 data segments should be in the storage
+  BOOST_CHECK_EQUAL(lsdb.m_lsaStorage.size(), 3);
+  BOOST_CHECK_EQUAL(numValidationSignal, 3);
+  numValidationSignal = 0;
+  face.sentInterests.clear();
 
-  // 4 data segments should be sent in response to 4 interests
-  BOOST_CHECK_EQUAL(face.sentData.size(), 4);
-}
+  // Remove older LSA from storage upon receiving that of higher sequence
+  ndn::Name lsaInterestName2("/ndn/NLSR/LSA/other-site/%C1.Router/other-router/NAME");
+  lsaInterestName2.appendNumber(13);
+  lsdb.expressInterest(lsaInterestName2, 0);
+  advanceClocks(ndn::time::milliseconds(10));
 
-BOOST_AUTO_TEST_CASE(DeleteOldLsa)
-{
-  ndn::Name lsaDataName("/ndn/NLSR/LSA/other-site/%C1.Router/other-router/NAME");
-  uint64_t segmentNo = 0;
+  makeLsaContent(lsaInterestName2, 1);
+  advanceClocks(ndn::time::milliseconds(10));
+  // Should have cleared all the three segments for the previous interest w/ seq 12
+  // And add one segment for this sequence 13
+  BOOST_CHECK_EQUAL(lsdb.m_lsaStorage.size(), 1);
+  BOOST_CHECK_EQUAL(numValidationSignal, 1);
 
-  uint64_t oldSeqNo = 12;
-  ndn::Name oldLsaDataName(lsaDataName);
-  oldLsaDataName.appendNumber(oldSeqNo);
-  oldLsaDataName.appendVersion();
-
-  auto oldLsaData = makeLsaSegment(oldLsaDataName, segmentNo, true);
-  lsaStorage.afterFetcherSignalEmitted(*oldLsaData);
-  advanceClocks(ndn::time::milliseconds(1), 10);
-
-  uint64_t newSeqNo = 13;
-  ndn::Name newLsaDataName(lsaDataName);
-  newLsaDataName.appendNumber(newSeqNo);
-  newLsaDataName.appendVersion();
-
-  auto newLsaData = makeLsaSegment(newLsaDataName, segmentNo, true);
-  lsaStorage.afterFetcherSignalEmitted(*newLsaData);
-  advanceClocks(ndn::time::milliseconds(1), 10);
-
-  ndn::Name lsaInterestName(lsaDataName);
-
-  ndn::Name oldLsaInterestName(lsaInterestName);
-  oldLsaInterestName.appendNumber(oldSeqNo);
-  receiveLsaInterest(oldLsaInterestName, segmentNo, true);
-
-  advanceClocks(ndn::time::milliseconds(1), 10);
-
-  BOOST_CHECK_EQUAL(face.sentData.size(), 0);
-
-  ndn::Name newLsaInterestName(lsaInterestName);
-  newLsaInterestName.appendNumber(newSeqNo);
-  receiveLsaInterest(newLsaInterestName, segmentNo, true);
-
-  advanceClocks(ndn::time::milliseconds(1), 10);
-
-  BOOST_CHECK_EQUAL(face.sentData.size(), 1);
-}
-
-BOOST_AUTO_TEST_CASE(ScheduledDeletion)
-{
-  ndn::Name lsaInterestName("/ndn/NLSR/LSA/other-site/%C1.Router/other-router/NAME");
-  lsaInterestName.appendNumber(12);
-
-  ndn::Name lsaDataName(lsaInterestName);
-  lsaDataName.appendVersion();
-
-  auto lsaData = makeLsaSegment(lsaDataName, 0, true);
-  lsaStorage.afterFetcherSignalEmitted(*lsaData);
-
-  BOOST_CHECK(lsaStorage.getLsaSegment(ndn::Interest(lsaInterestName)) != nullptr);
-
-  // Make sure it was not deleted earlier somehow
-  advanceClocks(ndn::time::seconds(100), 10);
-  BOOST_CHECK(lsaStorage.getLsaSegment(ndn::Interest(lsaInterestName)) != nullptr);
-
-  advanceClocks(ndn::time::seconds(LSA_REFRESH_TIME_DEFAULT), 10);
-
-  BOOST_CHECK(lsaStorage.getLsaSegment(ndn::Interest(lsaInterestName)) == nullptr);
+  // Scheduled removal of LSA
+  advanceClocks(ndn::time::seconds(LSA_REFRESH_TIME_DEFAULT));
+  BOOST_CHECK_EQUAL(lsdb.m_lsaStorage.size(), 0);
 }
 
 BOOST_AUTO_TEST_SUITE_END() // TestLsaSegmentStorage
diff --git a/tests/test-nlsr.cpp b/tests/test-nlsr.cpp
index 17801d6..229c004 100644
--- a/tests/test-nlsr.cpp
+++ b/tests/test-nlsr.cpp
@@ -129,7 +129,7 @@
   const Lsdb& lsdb = nlsr2.m_lsdb;
   const RoutingTable& rt = nlsr2.m_routingTable;
 
-  BOOST_CHECK_EQUAL(lsdb.getAdjLsaBuildInterval(), ndn::time::seconds(3));
+  BOOST_CHECK_EQUAL(lsdb.m_adjLsaBuildInterval, ndn::time::seconds(3));
   BOOST_CHECK_EQUAL(conf.getFirstHelloInterval(), 6);
   BOOST_CHECK_EQUAL(rt.getRoutingCalcInterval(), ndn::time::seconds(9));
 }
@@ -288,7 +288,7 @@
   BOOST_REQUIRE(lsa != nullptr);
 
   uint32_t lastAdjLsaSeqNo = lsa->getLsSeqNo();
-  nlsr.m_lsdb.getSequencingManager().setAdjLsaSeq(lastAdjLsaSeqNo);
+  nlsr.m_lsdb.m_sequencingManager.setAdjLsaSeq(lastAdjLsaSeqNo);
 
   this->advanceClocks(1500_ms, 10);
 
diff --git a/tests/update/test-nfd-rib-command-processor.cpp b/tests/update/test-nfd-rib-command-processor.cpp
index 8aa8cdf..17485da 100644
--- a/tests/update/test-nfd-rib-command-processor.cpp
+++ b/tests/update/test-nfd-rib-command-processor.cpp
@@ -49,7 +49,7 @@
     this->advanceClocks(ndn::time::milliseconds(10), 10);
     face.sentInterests.clear();
 
-    nameLsaSeqNoBeforeInterest = nlsr.m_lsdb.getSequencingManager().getNameLsaSeq();
+    nameLsaSeqNoBeforeInterest = nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq();
   }
 
   void
@@ -67,7 +67,7 @@
     lsaInterestName.append(conf.getSiteName());
     lsaInterestName.append(conf.getRouterName());
     lsaInterestName.append(std::to_string(Lsa::Type::NAME));
-    lsaInterestName.appendNumber(nlsr.m_lsdb.getSequencingManager().getNameLsaSeq());
+    lsaInterestName.appendNumber(nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
 
     face.receive(ndn::Interest(lsaInterestName).setCanBePrefix(true));
     this->advanceClocks(ndn::time::milliseconds(10), 10);
@@ -144,7 +144,7 @@
   }
   BOOST_CHECK_EQUAL((*itr), prefixName);
   BOOST_CHECK(wasRoutingUpdatePublished());
-  BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.getSequencingManager().getNameLsaSeq());
+  BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
 }
 
 BOOST_AUTO_TEST_CASE(onReceiveInterestUnregisterCommand)
@@ -160,7 +160,7 @@
 
   BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 0);
   BOOST_CHECK(wasRoutingUpdatePublished());
-  BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.getSequencingManager().getNameLsaSeq());
+  BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
 }
 
 BOOST_AUTO_TEST_CASE(onReceiveInterestInvalidPrefix)
@@ -176,7 +176,7 @@
 
   // Cannot use routingUpdatePublish test now since in
   // initialize nlsr calls buildOwnNameLsa which publishes the routing update
-  BOOST_CHECK(nameLsaSeqNoBeforeInterest == nlsr.m_lsdb.getSequencingManager().getNameLsaSeq());
+  BOOST_CHECK(nameLsaSeqNoBeforeInterest == nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
 }
 
 BOOST_AUTO_TEST_SUITE_END()
diff --git a/tests/update/test-prefix-update-processor.cpp b/tests/update/test-prefix-update-processor.cpp
index 3cdb8c4..5a6cbde 100644
--- a/tests/update/test-prefix-update-processor.cpp
+++ b/tests/update/test-prefix-update-processor.cpp
@@ -100,7 +100,7 @@
     lsaInterestName.append(conf.getRouterName());
     lsaInterestName.append(std::to_string(Lsa::Type::NAME));
 
-    lsaInterestName.appendNumber(nlsr.m_lsdb.getSequencingManager().getNameLsaSeq());
+    lsaInterestName.appendNumber(nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
 
     auto lsaInterest = std::make_shared<Interest>(lsaInterestName);
     lsaInterest->setCanBePrefix(true);
@@ -145,7 +145,7 @@
 
 BOOST_AUTO_TEST_CASE(Basic)
 {
-  uint64_t nameLsaSeqNoBeforeInterest = nlsr.m_lsdb.getSequencingManager().getNameLsaSeq();
+  uint64_t nameLsaSeqNoBeforeInterest = nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq();
 
   ndn::nfd::ControlParameters parameters;
   parameters.setName("/prefix/to/advertise/");
@@ -177,10 +177,10 @@
   BOOST_CHECK_EQUAL(namePrefixList.getNames().front(), parameters.getName());
 
   BOOST_CHECK(wasRoutingUpdatePublished());
-  BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.getSequencingManager().getNameLsaSeq());
+  BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
 
   face.sentData.clear();
-  nameLsaSeqNoBeforeInterest = nlsr.m_lsdb.getSequencingManager().getNameLsaSeq();
+  nameLsaSeqNoBeforeInterest = nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq();
 
   //Withdraw
   ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw");
@@ -196,7 +196,7 @@
   BOOST_CHECK_EQUAL(namePrefixList.size(), 0);
 
   BOOST_CHECK(wasRoutingUpdatePublished());
-  BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.getSequencingManager().getNameLsaSeq());
+  BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.m_lsdb.m_sequencingManager.getNameLsaSeq());
 }
 
 BOOST_AUTO_TEST_SUITE_END()