lsdb: emit signals when modified

RoutingTable and NamePrefixTable consume the signal
and behave accordingly.

refs: #4127

Change-Id: I6540f30f0222f804b01dc7d9640831c84e5264cc
diff --git a/tests/route/test-name-prefix-table.cpp b/tests/route/test-name-prefix-table.cpp
index 9585106..fbe6c5a 100644
--- a/tests/route/test-name-prefix-table.cpp
+++ b/tests/route/test-name-prefix-table.cpp
@@ -20,7 +20,9 @@
  */
 
 #include "route/name-prefix-table.hpp"
-#include "nlsr.hpp"
+#include "route/fib.hpp"
+#include "route/routing-table.hpp"
+#include "lsdb.hpp"
 #include "../test-common.hpp"
 
 #include <ndn-cxx/util/dummy-client-face.hpp>
@@ -34,32 +36,38 @@
   NamePrefixTableFixture()
     : face(m_ioService, m_keyChain)
     , conf(face, m_keyChain)
-    , nlsr(face, m_keyChain, conf)
-    , lsdb(nlsr.m_lsdb)
-    , npt(nlsr.m_namePrefixTable)
+    , confProcessor(conf)
+    , lsdb(face, m_keyChain, conf)
+    , fib(face, m_scheduler, conf.getAdjacencyList(), conf, m_keyChain)
+    , rt(m_scheduler, lsdb, conf)
+    , npt(conf.getRouterPrefix(), fib, rt, rt.afterRoutingChange, lsdb.onLsdbModified)
   {
   }
 
+  bool
+  isNameInNpt(const ndn::Name& name)
+  {
+    auto it = std::find_if(npt.begin(), npt.end(),
+                           [&] (const auto& entry) { return name == entry->getNamePrefix(); });
+    return it != npt.end();
+  }
+
 public:
   ndn::util::DummyClientFace face;
   ConfParameter conf;
-  Nlsr nlsr;
+  DummyConfFileProcessor confProcessor;
 
-  Lsdb& lsdb;
-  NamePrefixTable& npt;
+  Lsdb lsdb;
+  Fib fib;
+  RoutingTable rt;
+  NamePrefixTable npt;
 };
 
 BOOST_AUTO_TEST_SUITE(TestNamePrefixTable)
 
 BOOST_FIXTURE_TEST_CASE(Bupt, NamePrefixTableFixture)
 {
-  conf.setNetwork("/ndn");
-  conf.setSiteName("/router");
-  conf.setRouterName("/a");
-  conf.buildRouterAndSyncUserPrefix();
-
-  RoutingTable& routingTable = nlsr.m_routingTable;
-  routingTable.setRoutingCalcInterval(0);
+  rt.m_routingCalcInterval = 0_s;
 
   Adjacent thisRouter(conf.getRouterPrefix(), ndn::FaceUri("udp4://10.0.0.1"), 0, Adjacent::STATUS_ACTIVE, 0, 0);
 
@@ -265,7 +273,6 @@
 
 BOOST_FIXTURE_TEST_CASE(RoutingTableUpdate, NamePrefixTableFixture)
 {
-  RoutingTable& routingTable = nlsr.m_routingTable;
   const ndn::Name destination = ndn::Name{"/ndn/destination1"};
   NextHop hop1{"upd4://10.0.0.1", 0};
   NextHop hop2{"udp4://10.0.0.2", 1};
@@ -273,10 +280,10 @@
   const NamePrefixTableEntry entry1{"/ndn/router1"};
   npt.addEntry(entry1.getNamePrefix(), destination);
 
-  routingTable.addNextHop(destination, hop1);
-  routingTable.addNextHop(destination, hop2);
+  rt.addNextHop(destination, hop1);
+  rt.addNextHop(destination, hop2);
 
-  npt.updateWithNewRoute(routingTable.m_rTable);
+  npt.updateWithNewRoute(rt.m_rTable);
 
   // At this point the NamePrefixTableEntry should have two NextHops.
   auto nameIterator = std::find_if(npt.begin(), npt.end(),
@@ -291,8 +298,8 @@
   BOOST_CHECK_EQUAL(nextHops.size(), 2);
 
   // Add the other NextHop
-  routingTable.addNextHop(destination, hop3);
-  npt.updateWithNewRoute(routingTable.m_rTable);
+  rt.addNextHop(destination, hop3);
+  npt.updateWithNewRoute(rt.m_rTable);
 
   // At this point the NamePrefixTableEntry should have three NextHops.
   nameIterator = std::find_if(npt.begin(), npt.end(),
@@ -306,6 +313,65 @@
   BOOST_CHECK_EQUAL(nextHops.size(), 3);
 }
 
+BOOST_FIXTURE_TEST_CASE(UpdateFromLsdb, NamePrefixTableFixture)
+{
+  ndn::time::system_clock::TimePoint testTimePoint =  ndn::time::system_clock::now();
+  NamePrefixList npl1;
+  ndn::Name n1("name1");
+  ndn::Name n2("name2");
+  ndn::Name router1("/router1/1");
+
+  npl1.insert(n1);
+  npl1.insert(n2);
+
+  NameLsa nlsa1(router1, 12, testTimePoint, npl1);
+  std::shared_ptr<Lsa> lsaPtr = std::make_shared<NameLsa>(nlsa1);
+
+  BOOST_CHECK(npt.begin() == npt.end());
+  npt.updateFromLsdb(lsaPtr, LsdbUpdate::INSTALLED, {}, {});
+  BOOST_CHECK_EQUAL(npt.m_table.size(), 3); // Router + 2 names
+
+  BOOST_CHECK(isNameInNpt(n1));
+  BOOST_CHECK(isNameInNpt(n2));
+
+  ndn::Name n3("name3");
+  auto nlsa = std::static_pointer_cast<NameLsa>(lsaPtr);
+  nlsa->removeName(n2);
+  nlsa->addName(n3);
+  npt.updateFromLsdb(lsaPtr, LsdbUpdate::UPDATED, {n3}, {n2});
+  BOOST_CHECK(isNameInNpt(n1));
+  BOOST_CHECK(!isNameInNpt(n2)); // Removed
+  BOOST_CHECK(isNameInNpt(n3));
+
+  BOOST_CHECK_EQUAL(npt.m_table.size(), 3); // Still router + 2 names
+
+  npt.updateFromLsdb(lsaPtr, LsdbUpdate::REMOVED, {}, {});
+  BOOST_CHECK_EQUAL(npt.m_table.size(), 0);
+
+  // Adj and Coordinate LSAs router
+  ndn::Name router2("/router2/2");
+  AdjLsa adjLsa(router2, 12, testTimePoint, 2, conf.getAdjacencyList());
+  lsaPtr = std::make_shared<AdjLsa>(adjLsa);
+  BOOST_CHECK(npt.begin() == npt.end());
+  npt.updateFromLsdb(lsaPtr, LsdbUpdate::INSTALLED, {}, {});
+  BOOST_CHECK_EQUAL(npt.m_table.size(), 1);
+  BOOST_CHECK(isNameInNpt(router2));
+
+  npt.updateFromLsdb(lsaPtr, LsdbUpdate::REMOVED, {}, {});
+  BOOST_CHECK_EQUAL(npt.m_table.size(), 0);
+
+  ndn::Name router3("/router3/3");
+  CoordinateLsa corLsa(router3, 12, testTimePoint, 2, {3});
+  lsaPtr = std::make_shared<CoordinateLsa>(corLsa);
+  BOOST_CHECK(npt.begin() == npt.end());
+  npt.updateFromLsdb(lsaPtr, LsdbUpdate::INSTALLED, {}, {});
+  BOOST_CHECK_EQUAL(npt.m_table.size(), 1);
+  BOOST_CHECK(isNameInNpt(router3));
+
+  npt.updateFromLsdb(lsaPtr, LsdbUpdate::REMOVED, {}, {});
+  BOOST_CHECK_EQUAL(npt.m_table.size(), 0);
+}
+
 BOOST_AUTO_TEST_SUITE_END()
 
 } // namespace test
diff --git a/tests/route/test-routing-table.cpp b/tests/route/test-routing-table.cpp
index 0bce153..6a2971e 100644
--- a/tests/route/test-routing-table.cpp
+++ b/tests/route/test-routing-table.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2020,  The University of Memphis,
+ * Copyright (c) 2014-2021,  The University of Memphis,
  *                           Regents of the University of California
  *
  * This file is part of NLSR (Named-data Link State Routing).
@@ -28,23 +28,25 @@
 namespace nlsr {
 namespace test {
 
-class RoutingTableFixture
+class RoutingTableFixture : public UnitTestTimeFixture
 {
 public:
   RoutingTableFixture()
-    : conf(face, keyChain)
-    , nlsr(face, keyChain, conf)
-    , rt(nlsr.m_routingTable)
+    : face(m_ioService, m_keyChain, {true, true})
+    , conf(face, m_keyChain)
+    , confProcessor(conf)
+    , lsdb(face, m_keyChain, conf)
+    , rt(m_scheduler, lsdb, conf)
   {
   }
 
 public:
   ndn::util::DummyClientFace face;
-  ndn::KeyChain keyChain;
   ConfParameter conf;
-  Nlsr nlsr;
+  DummyConfFileProcessor confProcessor;
 
-  RoutingTable& rt;
+  Lsdb lsdb;
+  RoutingTable rt;
 };
 
 BOOST_AUTO_TEST_SUITE(TestRoutingTable)
@@ -130,6 +132,83 @@
                     "    NextHop(Uri: nexthop, Cost: 99)\n");
 }
 
+BOOST_FIXTURE_TEST_CASE(UpdateFromLsdb, RoutingTableFixture)
+{
+  ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now() + 3600_s;
+  ndn::Name router2("/router2");
+  AdjLsa adjLsa(router2, 12, testTimePoint, 2, conf.getAdjacencyList());
+  std::shared_ptr<Lsa> lsaPtr = std::make_shared<AdjLsa>(adjLsa);
+  BOOST_CHECK(!rt.m_isRouteCalculationScheduled);
+  lsdb.installLsa(lsaPtr);
+  BOOST_CHECK(rt.m_isRouteCalculationScheduled);
+
+  // After 15_s (by default) routing table calculation is done
+  advanceClocks(15_s);
+  BOOST_CHECK(!rt.m_isRouteCalculationScheduled);
+
+  // Update to installed LSA
+  std::shared_ptr<Lsa> lsaPtr2 = std::make_shared<AdjLsa>(adjLsa);
+  auto adjPtr = std::static_pointer_cast<AdjLsa>(lsaPtr2);
+  adjPtr->addAdjacent(Adjacent("router3"));
+  adjPtr->setSeqNo(13);
+  lsdb.installLsa(lsaPtr2);
+  BOOST_CHECK(rt.m_isRouteCalculationScheduled);
+
+  // Insert a neighbor so that AdjLsa can be installed
+  AdjacencyList adjl;
+  Adjacent ownAdj(conf.getRouterPrefix());
+  ownAdj.setStatus(Adjacent::STATUS_ACTIVE);
+  adjl.insert(ownAdj);
+  AdjLsa adjLsa4("/router4", 12, testTimePoint, 2, adjl);
+  lsaPtr = std::make_shared<AdjLsa>(adjLsa4);
+  lsdb.installLsa(lsaPtr);
+
+  Adjacent adj("/router4");
+  adj.setStatus(Adjacent::STATUS_ACTIVE);
+  conf.getAdjacencyList().insert(adj);
+  lsdb.scheduleAdjLsaBuild();
+  BOOST_CHECK_EQUAL(rt.m_rTable.size(), 0);
+  advanceClocks(15_s);
+  BOOST_CHECK_EQUAL(rt.m_rTable.size(), 1);
+
+  rt.wireEncode();
+  BOOST_CHECK(rt.m_wire.isValid());
+  BOOST_CHECK_GT(rt.m_wire.size(), 0);
+
+  // Remove own Adj Lsa - Make sure routing table is wiped out
+  conf.getAdjacencyList().setStatusOfNeighbor("/router4", Adjacent::STATUS_INACTIVE);
+  conf.getAdjacencyList().setTimedOutInterestCount("/router4", HELLO_RETRIES_MAX);
+  lsdb.scheduleAdjLsaBuild();
+  advanceClocks(15_s);
+  BOOST_CHECK_EQUAL(rt.m_rTable.size(), 0);
+  BOOST_CHECK(!rt.m_wire.isValid());
+
+  // Check that HR routing is scheduled, once Coordinate LSA is added
+  BOOST_CHECK(!rt.m_isRouteCalculationScheduled);
+  rt.m_hyperbolicState = HYPERBOLIC_STATE_ON;
+  CoordinateLsa clsa("router5", 12, testTimePoint, 2.5, {30.0});
+  auto clsaPtr = std::make_shared<CoordinateLsa>(clsa);
+  lsdb.installLsa(clsaPtr);
+  BOOST_CHECK(rt.m_isRouteCalculationScheduled);
+
+  Adjacent router5("/router5");
+  router5.setStatus(Adjacent::STATUS_ACTIVE);
+  conf.getAdjacencyList().insert(router5);
+  conf.getAdjacencyList().setStatusOfNeighbor("/router5", Adjacent::STATUS_ACTIVE);
+  advanceClocks(15_s);
+  rt.wireEncode();
+  BOOST_CHECK(rt.m_wire.isValid());
+  BOOST_CHECK_GT(rt.m_wire.size(), 0);
+  BOOST_CHECK(!rt.m_isRouteCalculationScheduled);
+
+  // Emulate HelloProtocol neighbor down
+  conf.getAdjacencyList().setStatusOfNeighbor("/router5", Adjacent::STATUS_INACTIVE);
+  rt.scheduleRoutingTableCalculation();
+  advanceClocks(15_s);
+  BOOST_CHECK_EQUAL(rt.m_rTable.size(), 0);
+  BOOST_CHECK(!rt.m_wire.isValid());
+}
+
 BOOST_AUTO_TEST_SUITE_END()
 
 } // namespace test