Use separate name prefix and sequence number for each LSA type

refs: #1523

Change-Id: I9db6b3a3ea9ce5e17e132d2a4e2ae9f30dd4f591
diff --git a/src/sequencing-manager.cpp b/src/sequencing-manager.cpp
index 1c91700..6eaab5d 100644
--- a/src/sequencing-manager.cpp
+++ b/src/sequencing-manager.cpp
@@ -25,6 +25,7 @@
 #include <pwd.h>
 #include <cstdlib>
 #include <unistd.h>
+#include <boost/algorithm/string.hpp>
 
 #include "sequencing-manager.hpp"
 #include "logger.hpp"
@@ -36,28 +37,15 @@
 using namespace std;
 
 void
-SequencingManager::splitSequenceNo(uint64_t seqNo)
-{
-  m_combinedSeqNo = seqNo;
-  m_adjLsaSeq = (m_combinedSeqNo & 0xFFFFF);
-  m_corLsaSeq = ((m_combinedSeqNo >> 20) & 0xFFFFF);
-  m_nameLsaSeq = ((m_combinedSeqNo >> 40) & 0xFFFFFF);
-}
-
-void
-SequencingManager::combineSequenceNo()
-{
-  m_combinedSeqNo = 0;
-  m_combinedSeqNo = m_combinedSeqNo | m_adjLsaSeq;
-  m_combinedSeqNo = m_combinedSeqNo | (m_corLsaSeq << 20);
-  m_combinedSeqNo = m_combinedSeqNo | (m_nameLsaSeq << 40);
-}
-
-void
 SequencingManager::writeSeqNoToFile() const
 {
-  std::ofstream outputFile(m_seqFileNameWithPath.c_str(), ios::binary);
-  outputFile << m_combinedSeqNo;
+  writeLog();
+  std::ofstream outputFile(m_seqFileNameWithPath.c_str());
+  std::ostringstream os;
+  os << "NameLsaSeq " << std::to_string(m_nameLsaSeq) << "\n"
+     << "AdjLsaSeq "  << std::to_string(m_adjLsaSeq)  << "\n"
+     << "CorLsaSeq "  << std::to_string(m_corLsaSeq);
+  outputFile << os.str();
   outputFile.close();
 }
 
@@ -65,10 +53,40 @@
 SequencingManager::initiateSeqNoFromFile(int hypState)
 {
   _LOG_DEBUG("Seq File Name: " << m_seqFileNameWithPath);
-  std::ifstream inputFile(m_seqFileNameWithPath.c_str(), ios::binary);
+  std::ifstream inputFile(m_seqFileNameWithPath.c_str());
+
+  // Good checks that file is not (bad or eof or fail)
   if (inputFile.good()) {
-    inputFile >> m_combinedSeqNo;
-    splitSequenceNo(m_combinedSeqNo);
+    std::string lsaOrCombinedSeqNo;
+    uint64_t seqNo = 0;
+
+    // If file has a combined seq number, lsaOrCombinedSeqNo would hold it
+    // and seqNo will be zero everytime
+    inputFile >> lsaOrCombinedSeqNo >> seqNo;
+    m_nameLsaSeq = seqNo;
+
+    inputFile >> lsaOrCombinedSeqNo >> seqNo;
+    m_adjLsaSeq = seqNo;
+
+    inputFile >> lsaOrCombinedSeqNo >> seqNo;;
+    m_corLsaSeq = seqNo;
+
+    // File was in old format and had a combined sequence number
+    // if all of the seqNo should are still zero and
+    // lsaOrCombinedSeqNo != CorLsaSeq
+    if (m_nameLsaSeq == 0 && m_adjLsaSeq == 0 && m_corLsaSeq == 0 &&
+        lsaOrCombinedSeqNo != "CorLsaSeq") {
+      _LOG_DEBUG("Old file had combined sequence number: " << lsaOrCombinedSeqNo);
+      std::istringstream iss(lsaOrCombinedSeqNo);
+      iss >> seqNo;
+      m_adjLsaSeq = (seqNo & 0xFFFFF);
+      m_corLsaSeq = ((seqNo >> 20) & 0xFFFFF);
+      m_nameLsaSeq = ((seqNo >> 40) & 0xFFFFFF);
+    }
+
+    inputFile.close();
+
+    m_nameLsaSeq += 10;
 
     // Increment the adjacency LSA seq. no. if link-state or dry HR is enabled
     if (hypState != HYPERBOLIC_STATE_ON) {
@@ -82,43 +100,32 @@
 
     // 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) {
+      if (m_adjLsaSeq != 0) {
         _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();
   }
-  else {
-    splitSequenceNo(0);
-  }
+  writeLog();
 }
 
 void
-SequencingManager::setSeqFileName(string filePath)
+SequencingManager::setSeqFileDirectory(string filePath)
 {
   m_seqFileNameWithPath = filePath;
+
   if (m_seqFileNameWithPath.empty()) {
-    m_seqFileNameWithPath = getUserHomeDirectory();
+    string homeDirPath(getpwuid(getuid())->pw_dir);
+    if (homeDirPath.empty()) {
+      homeDirPath = getenv("HOME");
+    }
+    m_seqFileNameWithPath = homeDirPath;
   }
   m_seqFileNameWithPath = m_seqFileNameWithPath + "/nlsrSeqNo.txt";
 }
 
-string
-SequencingManager::getUserHomeDirectory()
-{
-  string homeDirPath(getpwuid(getuid())->pw_dir);
-  if (homeDirPath.empty()) {
-    homeDirPath = getenv("HOME");
-  }
-  return homeDirPath;
-}
-
 void
 SequencingManager::writeLog() const
 {
@@ -126,7 +133,6 @@
   _LOG_DEBUG("Adj LSA seq no: " << m_adjLsaSeq);
   _LOG_DEBUG("Cor LSA Seq no: " << m_corLsaSeq);
   _LOG_DEBUG("Name LSA Seq no: " << m_nameLsaSeq);
-  _LOG_DEBUG("Combined LSDB Seq no: " << m_combinedSeqNo);
 }
 
 } // namespace nlsr