Do not distribute coordinate LSAs when using link-state routing.

refs: #3661

Change-Id: I6a1467225def5ba04e2179480503cfe01968cede
diff --git a/.jenkins.d/01-dependencies.sh b/.jenkins.d/01-dependencies.sh
index 5c6c0d5..b1ba81f 100755
--- a/.jenkins.d/01-dependencies.sh
+++ b/.jenkins.d/01-dependencies.sh
@@ -10,7 +10,7 @@
     brew update
     brew upgrade
     brew install boost pkg-config sqlite cryptopp log4cxx protobuf openssl
-    brew link --force openssl
+    brew link --force openssl log4cxx protobuf
     brew cleanup
 fi
 
diff --git a/src/communication/sync-logic-handler.cpp b/src/communication/sync-logic-handler.cpp
index 1434aa4..d99b3e1 100644
--- a/src/communication/sync-logic-handler.cpp
+++ b/src/communication/sync-logic-handler.cpp
@@ -201,8 +201,7 @@
 
     update.getSequencingManager().writeLog();
 
-    try {
-      if (isLsaNew(originRouter, NameLsa::TYPE_STRING, update.getNameLsaSeqNo())) {
+    if (isLsaNew(originRouter, NameLsa::TYPE_STRING, update.getNameLsaSeqNo())) {
         _LOG_DEBUG("Received sync update with higher Name LSA sequence number than entry in LSDB");
 
         expressInterestForLsa(update, NameLsa::TYPE_STRING, update.getNameLsaSeqNo());
@@ -210,20 +209,29 @@
 
       if (isLsaNew(originRouter, AdjLsa::TYPE_STRING, update.getAdjLsaSeqNo())) {
         _LOG_DEBUG("Received sync update with higher Adj LSA sequence number than entry in LSDB");
-
-        expressInterestForLsa(update, AdjLsa::TYPE_STRING, update.getAdjLsaSeqNo());
+        if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
+          if (update.getAdjLsaSeqNo() != 0) {
+            _LOG_ERROR("Tried to fetch an adjacency LSA when hyperbolic routing"
+                       << " is enabled.");
+          }
+        }
+        else {
+          expressInterestForLsa(update, AdjLsa::TYPE_STRING, update.getAdjLsaSeqNo());
+        }
       }
 
       if (isLsaNew(originRouter, CoordinateLsa::TYPE_STRING, update.getCorLsaSeqNo())) {
         _LOG_DEBUG("Received sync update with higher Cor LSA sequence number than entry in LSDB");
-
-        expressInterestForLsa(update, CoordinateLsa::TYPE_STRING, update.getCorLsaSeqNo());
+        if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
+          if (update.getCorLsaSeqNo() != 0) {
+            _LOG_ERROR("Tried to fetch a coordinate LSA when link-state"
+                       << " is enabled.");
+          }
+        }
+        else {
+          expressInterestForLsa(update, CoordinateLsa::TYPE_STRING, update.getCorLsaSeqNo());
+        }
       }
-    }
-    catch (std::exception& e) {
-      std::cerr << e.what() << std::endl;
-      return;
-    }
   }
 }
 
diff --git a/src/conf-parameter.hpp b/src/conf-parameter.hpp
index 72949c7..d6833ed 100644
--- a/src/conf-parameter.hpp
+++ b/src/conf-parameter.hpp
@@ -86,7 +86,7 @@
   MAX_FACES_PER_PREFIX_MAX = 60
 };
 
-enum {
+enum HyperbolicState {
   HYPERBOLIC_STATE_OFF = 0,
   HYPERBOLIC_STATE_ON = 1,
   HYPERBOLIC_STATE_DRY_RUN = 2,
diff --git a/src/lsdb.cpp b/src/lsdb.cpp
index 8d7f1ec..5b1fda6 100644
--- a/src/lsdb.cpp
+++ b/src/lsdb.cpp
@@ -359,8 +359,15 @@
                        getLsaExpirationTimePoint(),
                        m_nlsr.getConfParameter().getCorR(),
                        m_nlsr.getConfParameter().getCorTheta());
-  m_nlsr.getSequencingManager().increaseCorLsaSeq();
+
+  // Sync coordinate LSAs if using HR or HR dry run.
+  if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
+    m_nlsr.getSequencingManager().increaseCorLsaSeq();
+    m_sync.publishRoutingUpdate();
+  }
+
   installCoordinateLsa(corLsa);
+
   return true;
 }
 
@@ -411,11 +418,12 @@
     clsa.writeLog();
     addCoordinateLsa(clsa);
 
+    // Register the LSA's origin router prefix
     if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
       m_nlsr.getNamePrefixTable().addEntry(clsa.getOrigRouter(),
                                            clsa.getOrigRouter());
     }
-    if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
+    if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
       m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
     }
     if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
@@ -479,12 +487,12 @@
                                                                  _1, key));
   if (it != m_corLsdb.end()) {
     _LOG_DEBUG("Deleting Coordinate Lsa");
-    (*it).writeLog();
-    if ((*it).getOrigRouter() !=
-        m_nlsr.getConfParameter().getRouterPrefix()) {
-      m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
-                                              (*it).getOrigRouter());
+    it->writeLog();
+
+    if (it->getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
+      m_nlsr.getNamePrefixTable().removeEntry(it->getOrigRouter(), it->getOrigRouter());
     }
+
     m_corLsdb.erase(it);
     return true;
   }
@@ -533,6 +541,11 @@
 {
   m_nlsr.incrementAdjBuildCount();
 
+  if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
+    // Don't build adjacency LSAs in hyperbolic routing
+    return;
+  }
+
   if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
     _LOG_DEBUG("Scheduling Adjacency LSA build in " << m_adjLsaBuildInterval);
 
@@ -681,9 +694,11 @@
                 m_nlsr.getAdjacencyList().getNumOfActiveNeighbor(),
                 m_nlsr.getAdjacencyList());
 
-  m_nlsr.getSequencingManager().increaseAdjLsaSeq();
-
-  m_sync.publishRoutingUpdate();
+  //Sync adjacency LSAs if link-state or dry-run HR is enabled.
+  if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_ON) {
+    m_nlsr.getSequencingManager().increaseAdjLsaSeq();
+    m_sync.publishRoutingUpdate();
+  }
 
   return installAdjLsa(adjLsa);
 }
@@ -815,7 +830,10 @@
         _LOG_DEBUG("Deleting Coordinate Lsa");
         chkCorLsa->writeLog();
         chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1);
-        m_nlsr.getSequencingManager().setCorLsaSeq(chkCorLsa->getLsSeqNo());
+        if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
+          m_nlsr.getSequencingManager().setCorLsaSeq(chkCorLsa->getLsSeqNo());
+        }
+
         chkCorLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
         _LOG_DEBUG("Adding Coordinate Lsa");
         chkCorLsa->writeLog();
@@ -824,13 +842,16 @@
                                         chkCorLsa->getKey(),
                                         chkCorLsa->getLsSeqNo(),
                                         m_lsaRefreshTime));
-        m_sync.publishRoutingUpdate();
+        // Only sync coordinate LSAs if link-state routing is disabled
+        if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
+          m_sync.publishRoutingUpdate();
+        }
       }
       else {
         _LOG_DEBUG("Other's Cor LSA, so removing form LSDB");
         removeCoordinateLsa(lsaKey);
       }
-      if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
+      if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
         m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
       }
     }
@@ -934,6 +955,10 @@
                                      const ndn::Name& lsaKey,
                                      uint64_t seqNo)
 {
+  if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
+    _LOG_ERROR("Received interest for an adjacency LSA when hyperbolic routing is enabled.");
+  }
+
   AdjLsa* adjLsa = m_nlsr.getLsdb().findAdjLsa(lsaKey);
   if (adjLsa != 0) {
     if (adjLsa->getLsSeqNo() == seqNo) {
@@ -948,6 +973,10 @@
                                       const ndn::Name& lsaKey,
                                       uint64_t seqNo)
 {
+  if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
+    _LOG_ERROR("Received Interest for a coordinate LSA when link-state routing is enabled.");
+  }
+
   CoordinateLsa* corLsa = m_nlsr.getLsdb().findCoordinateLsa(lsaKey);
   if (corLsa != 0) {
     if (corLsa->getLsSeqNo() == seqNo) {
diff --git a/src/nlsr.cpp b/src/nlsr.cpp
index a6039e4..6753a13 100644
--- a/src/nlsr.cpp
+++ b/src/nlsr.cpp
@@ -183,7 +183,7 @@
   m_nlsrLsdb.setThisRouterPrefix(m_confParam.getRouterPrefix().toUri());
   m_fib.setEntryRefreshTime(2 * m_confParam.getLsaRefreshTime());
   m_sequencingManager.setSeqFileName(m_confParam.getSeqFileDir());
-  m_sequencingManager.initiateSeqNoFromFile();
+  m_sequencingManager.initiateSeqNoFromFile(m_confParam.getHyperbolicState());
 
   m_syncLogicHandler.createSyncSocket(m_confParam.getChronosyncPrefix());
 
@@ -204,7 +204,11 @@
   m_routingTable.setRoutingCalcInterval(m_confParam.getRoutingCalcInterval());
 
   m_nlsrLsdb.buildAndInstallOwnNameLsa();
-  m_nlsrLsdb.buildAndInstallOwnCoordinateLsa();
+
+  // Install coordinate LSAs if using HR or dry-run HR.
+  if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
+    m_nlsrLsdb.buildAndInstallOwnCoordinateLsa();
+  }
 
   registerKeyPrefix();
   registerLocalhostPrefix();
diff --git a/src/sequencing-manager.cpp b/src/sequencing-manager.cpp
index 7749ae0..2bffc28 100644
--- a/src/sequencing-manager.cpp
+++ b/src/sequencing-manager.cpp
@@ -62,15 +62,34 @@
 }
 
 void
-SequencingManager::initiateSeqNoFromFile()
+SequencingManager::initiateSeqNoFromFile(int hypState)
 {
   _LOG_DEBUG("Seq File Name: " << m_seqFileNameWithPath);
   std::ifstream inputFile(m_seqFileNameWithPath.c_str(), ios::binary);
   if (inputFile.good()) {
     inputFile >> m_combinedSeqNo;
     splitSequenceNo(m_combinedSeqNo);
-    m_adjLsaSeq += 10;
-    m_corLsaSeq += 10;
+
+    // Increment the adjacency LSA seq. no. if link-state or dry HR is enabled
+    if (hypState != HYPERBOLIC_STATE_ON) {
+      if (m_corLsaSeq != 0) {
+        _LOG_WARN("This router was previously configured for hyperbolic"
+                   << " routing without clearing the seq. no. file.");
+        m_corLsaSeq = 0;
+      }
+      m_adjLsaSeq += 10;
+    }
+
+    // Similarly, increment the coordinate LSA seq. no only if link-state is disabled.
+    if (hypState != HYPERBOLIC_STATE_OFF) {
+      if (m_adjLsaSeq != 0 &&
+          hypState == HYPERBOLIC_STATE_ON) {
+        _LOG_WARN("This router was previously configured for link-state"
+                  << " routing without clearing the seq. no. file.");
+        m_adjLsaSeq = 0;
+      }
+      m_corLsaSeq += 10;
+    }
     m_nameLsaSeq += 10;
     combineSequenceNo();
     inputFile.close();
@@ -111,5 +130,3 @@
 }
 
 }//namespace nlsr
-
-
diff --git a/src/sequencing-manager.hpp b/src/sequencing-manager.hpp
index c023b38..32793d7 100644
--- a/src/sequencing-manager.hpp
+++ b/src/sequencing-manager.hpp
@@ -28,7 +28,10 @@
 
 #include <ndn-cxx/face.hpp>
 
+#include "conf-parameter.hpp"
+
 namespace nlsr {
+
 class SequencingManager
 {
 public:
@@ -124,7 +127,7 @@
   writeSeqNoToFile() const;
 
   void
-  initiateSeqNoFromFile();
+  initiateSeqNoFromFile(int hypState);
 
   void
   setSeqFileName(std::string filePath);
diff --git a/tests/test-sync-logic-handler.cpp b/tests/test-sync-logic-handler.cpp
index c2e092d..0547c6c 100644
--- a/tests/test-sync-logic-handler.cpp
+++ b/tests/test-sync-logic-handler.cpp
@@ -49,7 +49,7 @@
   }
 
   void
-  receiveUpdate(std::string prefix, uint64_t seqNo)
+  receiveUpdate(std::string prefix, uint64_t seqNo, SyncLogicHandler& p_sync)
   {
     Sync::MissingDataInfo info = {prefix, 0, seqNo};
 
@@ -59,7 +59,7 @@
     face->processEvents(ndn::time::milliseconds(1));
     face->sentInterests.clear();
 
-    sync.onNsyncUpdate(updates, NULL);
+    p_sync.onNsyncUpdate(updates, NULL);
 
     face->processEvents(ndn::time::milliseconds(1));
   }
@@ -76,17 +76,86 @@
 
 BOOST_FIXTURE_TEST_SUITE(TestSyncLogicHandler, SyncLogicFixture)
 
-BOOST_AUTO_TEST_CASE(UpdateForOther)
+BOOST_AUTO_TEST_CASE(UpdateForOtherLS)
 {
   std::string updateName = nlsr.getConfParameter().getLsaPrefix().toUri() +
                            CONFIG_SITE + "/%C1.Router/other-router/";
 
-  receiveUpdate(updateName, 1);
+  uint64_t syncSeqNo = 1;
+  receiveUpdate(updateName, syncSeqNo, sync);
 
   std::vector<ndn::Interest>& interests = face->sentInterests;
+  std::vector<ndn::Interest>::iterator it = interests.begin();
+
+  BOOST_REQUIRE_EQUAL(interests.size(), 2);
+
+  BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + NameLsa::TYPE_STRING + "/");
+
+  ++it;
+  BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + AdjLsa::TYPE_STRING + "/");
+}
+
+BOOST_AUTO_TEST_CASE(UpdateForOtherHR)
+{
+  Nlsr nlsr_hr(g_ioService, g_scheduler, ndn::ref(*face));
+  SyncLogicHandler& sync_hr(nlsr_hr.getSyncLogicHandler());
+
+  nlsr_hr.getConfParameter().setNetwork(CONFIG_NETWORK);
+  nlsr_hr.getConfParameter().setSiteName(CONFIG_SITE);
+  nlsr_hr.getConfParameter().setRouterName(CONFIG_ROUTER_NAME);
+  nlsr_hr.getConfParameter().buildRouterPrefix();
+
+  nlsr_hr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_ON);
+
+  nlsr_hr.initialize();
+
+  std::string updateName = nlsr_hr.getConfParameter().getLsaPrefix().toUri() +
+                           CONFIG_SITE + "/%C1.Router/other-router/";
+
+  uint64_t syncSeqNo = 0;
+  syncSeqNo = syncSeqNo | (static_cast<uint64_t>(1) << 20);
+
+  receiveUpdate(updateName, syncSeqNo, sync_hr);
+
+  std::vector<ndn::Interest>& interests = face->sentInterests;
+  std::vector<ndn::Interest>::iterator it = interests.begin();
+
+  BOOST_REQUIRE_EQUAL(interests.size(), 2);
+
+  BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + NameLsa::TYPE_STRING + "/");
+
+  ++it;
+  BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + CoordinateLsa::TYPE_STRING + "/");
+}
+
+BOOST_AUTO_TEST_CASE(UpdateForOtherHRDry)
+{
+
+  Nlsr nlsr_hrdry(g_ioService, g_scheduler, ndn::ref(*face));
+  SyncLogicHandler& sync_hrdry(nlsr_hrdry.getSyncLogicHandler());
+
+  nlsr_hrdry.getConfParameter().setNetwork(CONFIG_NETWORK);
+  nlsr_hrdry.getConfParameter().setSiteName(CONFIG_SITE);
+  nlsr_hrdry.getConfParameter().setRouterName(CONFIG_ROUTER_NAME);
+  nlsr_hrdry.getConfParameter().buildRouterPrefix();
+
+  nlsr_hrdry.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
+
+  nlsr_hrdry.initialize();
+
+  std::string updateName = nlsr.getConfParameter().getLsaPrefix().toUri() +
+                           CONFIG_SITE + "/%C1.Router/other-router/";
+
+
+  uint64_t syncSeqNo = 1;
+  syncSeqNo = syncSeqNo | (static_cast<uint64_t>(1) << 20);
+  receiveUpdate(updateName, syncSeqNo, sync_hrdry);
+
+  std::vector<ndn::Interest>& interests = face->sentInterests;
+  std::vector<ndn::Interest>::iterator it = interests.begin();
+
   BOOST_REQUIRE_EQUAL(interests.size(), 3);
 
-  std::vector<ndn::Interest>::iterator it = interests.begin();
   BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + NameLsa::TYPE_STRING + "/");
 
   ++it;
@@ -94,6 +163,7 @@
 
   ++it;
   BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + CoordinateLsa::TYPE_STRING + "/");
+
 }
 
 BOOST_AUTO_TEST_CASE(NoUpdateForSelf)
@@ -101,7 +171,7 @@
   std::string updateName = nlsr.getConfParameter().getLsaPrefix().toUri() +
                            CONFIG_SITE + CONFIG_ROUTER_NAME;
 
-  receiveUpdate(updateName, 1);
+  receiveUpdate(updateName, 1, sync);
 
   std::vector<ndn::Interest>& interests = face->sentInterests;
   BOOST_CHECK_EQUAL(interests.size(), 0);
@@ -143,21 +213,21 @@
 
   // Lower NameLSA sequence number
   uint64_t lowerSeqNo = static_cast<uint64_t>(998) << 40;
-  receiveUpdate(updateName, lowerSeqNo);
+  receiveUpdate(updateName, lowerSeqNo, sync);
 
   std::vector<ndn::Interest>& interests = face->sentInterests;
   BOOST_REQUIRE_EQUAL(interests.size(), 0);
 
   // Same NameLSA sequence number
   uint64_t sameSeqNo = static_cast<uint64_t>(999) << 40;
-  receiveUpdate(updateName, sameSeqNo);
+  receiveUpdate(updateName, sameSeqNo, sync);
 
   interests = face->sentInterests;
   BOOST_REQUIRE_EQUAL(interests.size(), 0);
 
   // Higher NameLSA sequence number
   uint64_t higherSeqNo = static_cast<uint64_t>(1000) << 40;
-  receiveUpdate(updateName, higherSeqNo);
+  receiveUpdate(updateName, higherSeqNo, sync);
 
   interests = face->sentInterests;
   BOOST_REQUIRE_EQUAL(interests.size(), 1);