lsa: simplify initializeFromContent and improve naming

refs: #4354

Change-Id: I5ddd5a9f482f59e6ca52061ac92514feab8c3070
diff --git a/src/lsa.cpp b/src/lsa.cpp
index 00aa797..16616c7 100644
--- a/src/lsa.cpp
+++ b/src/lsa.cpp
@@ -31,7 +31,6 @@
 #include <algorithm>
 #include <cmath>
 #include <limits>
-#include <boost/tokenizer.hpp>
 #include <boost/algorithm/string.hpp>
 
 namespace nlsr {
@@ -53,6 +52,19 @@
   return ndn::Name(m_origRouter).append(std::to_string(getType()));
 }
 
+bool
+Lsa::deserializeCommon(boost::tokenizer<boost::char_separator<char>>::iterator& iterator)
+{
+  m_origRouter = ndn::Name(*iterator++);
+  if (m_origRouter.size() <= 0)
+    return false;
+  if (*iterator++ != std::to_string(getType()))
+    return false;
+  m_lsSeqNo = boost::lexical_cast<uint32_t>(*iterator++);
+  m_expirationTimePoint = ndn::time::fromIsoString(*iterator++);
+  return true;
+}
+
 NameLsa::NameLsa(const ndn::Name& origR, uint32_t lsn,
                  const ndn::time::system_clock::TimePoint& lt,
                  NamePrefixList& npl)
@@ -78,34 +90,27 @@
 }
 
 bool
-NameLsa::initializeFromContent(const std::string& content)
+NameLsa::deserialize(const std::string& content)
 {
   uint32_t numName = 0;
   boost::char_separator<char> sep("|");
   boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
   boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
                                                tokens.begin();
-  m_origRouter = ndn::Name(*tok_iter++);
-  if (!(m_origRouter.size() > 0)) {
-    return false;
-  }
-  try {
-    if (*tok_iter++ != std::to_string(Lsa::Type::NAME)) {
-      return false;
-    }
 
-    m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
-    m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
+  try {
+    if (!deserializeCommon(tok_iter))
+      return false;
     numName = boost::lexical_cast<uint32_t>(*tok_iter++);
+    for (uint32_t i = 0; i < numName; i++) {
+      ndn::Name name(*tok_iter++);
+      addName(name);
+    }
   }
   catch (const std::exception& e) {
-    _LOG_ERROR(e.what());
+    _LOG_ERROR("Could not deserialize from content: " << e.what());
     return false;
   }
-  for (uint32_t i = 0; i < numName; i++) {
-    ndn::Name name(*tok_iter++);
-    addName(name);
-  }
   return true;
 }
 
@@ -174,33 +179,24 @@
 }
 
 bool
-CoordinateLsa::initializeFromContent(const std::string& content)
+CoordinateLsa::deserialize(const std::string& content)
 {
   boost::char_separator<char> sep("|");
   boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
   boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
                                                tokens.begin();
-  m_origRouter = ndn::Name(*tok_iter++);
-  if (!(m_origRouter.size() > 0)) {
-    return false;
-  }
 
   try {
-    if (*tok_iter++ != std::to_string(Lsa::Type::COORDINATE)) {
+    if (!deserializeCommon(tok_iter))
       return false;
-    }
-
-    m_lsSeqNo  = boost::lexical_cast<uint32_t>(*tok_iter++);
-    m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
-    m_corRad   = boost::lexical_cast<double>(*tok_iter++);
+    m_corRad = boost::lexical_cast<double>(*tok_iter++);
     int numAngles = boost::lexical_cast<uint32_t>(*tok_iter++);
-
-   for (int i = 0; i < numAngles; i++) {
-     m_angles.push_back(boost::lexical_cast<double>(*tok_iter++));
-   }
+    for (int i = 0; i < numAngles; i++) {
+      m_angles.push_back(boost::lexical_cast<double>(*tok_iter++));
+    }
   }
   catch (const std::exception& e) {
-    _LOG_ERROR(e.what());
+    _LOG_ERROR("Could not deserialize from content: " << e.what());
     return false;
   }
   return true;
@@ -257,32 +253,19 @@
 }
 
 bool
-AdjLsa::initializeFromContent(const std::string& content)
+AdjLsa::deserialize(const std::string& content)
 {
   uint32_t numLink = 0;
   boost::char_separator<char> sep("|");
   boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
   boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
                                                tokens.begin();
-  m_origRouter = ndn::Name(*tok_iter++);
-  if (!(m_origRouter.size() > 0)) {
-    return false;
-  }
-  try {
-    if (*tok_iter++ != std::to_string(Lsa::Type::ADJACENCY)) {
-      return false;
-    }
 
-    m_lsSeqNo  = boost::lexical_cast<uint32_t>(*tok_iter++);
-    m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
-    numLink    = boost::lexical_cast<uint32_t>(*tok_iter++);
-  }
-  catch (const std::exception& e) {
-    _LOG_ERROR(e.what());
-    return false;
-  }
-  for (uint32_t i = 0; i < numLink; i++) {
-    try {
+  try {
+    if (!deserializeCommon(tok_iter))
+      return false;
+    numLink = boost::lexical_cast<uint32_t>(*tok_iter++);
+    for (uint32_t i = 0; i < numLink; i++) {
       ndn::Name adjName(*tok_iter++);
       std::string connectingFaceUri(*tok_iter++);
       double linkCost = boost::lexical_cast<double>(*tok_iter++);
@@ -290,10 +273,10 @@
                         Adjacent::STATUS_INACTIVE, 0, 0);
       addAdjacent(adjacent);
     }
-    catch (const std::exception& e) {
-      _LOG_ERROR(e.what());
-      return false;
-    }
+  }
+  catch (const std::exception& e) {
+    _LOG_ERROR("Could not deserialize from content: " << e.what());
+    return false;
   }
   return true;
 }
diff --git a/src/lsa.hpp b/src/lsa.hpp
index 8bc9e30..b2e0900 100644
--- a/src/lsa.hpp
+++ b/src/lsa.hpp
@@ -29,6 +29,7 @@
 #include <boost/cstdint.hpp>
 #include <ndn-cxx/util/scheduler.hpp>
 #include <ndn-cxx/util/time.hpp>
+#include <boost/tokenizer.hpp>
 
 namespace nlsr {
 
@@ -120,7 +121,7 @@
   getKey() const;
 
   virtual bool
-  initializeFromContent(const std::string& content) = 0;
+  deserialize(const std::string& content) = 0;
 
 protected:
   /*! Get data common to all LSA types.
@@ -131,6 +132,9 @@
   std::string
   getData() const;
 
+  bool
+  deserializeCommon(boost::tokenizer<boost::char_separator<char>>::iterator& iterator);
+
 protected:
   ndn::Name m_origRouter;
   uint32_t m_lsSeqNo;
@@ -188,7 +192,7 @@
     getData(); getData() returns data of this format, in other words.
    */
   bool
-  initializeFromContent(const std::string& content) override;
+  deserialize(const std::string& content) override;
 
   bool
   isEqualContent(const NameLsa& other) const;
@@ -252,7 +256,7 @@
     according to getData().
    */
   bool
-  initializeFromContent(const std::string& content) override;
+  deserialize(const std::string& content) override;
 
   uint32_t
   getNoLink()
@@ -331,7 +335,7 @@
     same as for getData();
   */
   bool
-  initializeFromContent(const std::string& content) override;
+  deserialize(const std::string& content) override;
 
   double
   getCorRadius() const
diff --git a/src/lsdb.cpp b/src/lsdb.cpp
index 327c3f1..2d78610 100644
--- a/src/lsdb.cpp
+++ b/src/lsdb.cpp
@@ -1230,7 +1230,7 @@
   lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_DATA);
   if (isNameLsaNew(lsaKey, lsSeqNo)) {
     NameLsa nameLsa;
-    if (nameLsa.initializeFromContent(dataContent)) {
+    if (nameLsa.deserialize(dataContent)) {
       installNameLsa(nameLsa);
     }
     else {
@@ -1247,7 +1247,7 @@
   lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_DATA);
   if (isAdjLsaNew(lsaKey, lsSeqNo)) {
     AdjLsa adjLsa;
-    if (adjLsa.initializeFromContent(dataContent)) {
+    if (adjLsa.deserialize(dataContent)) {
       installAdjLsa(adjLsa);
     }
     else {
@@ -1264,7 +1264,7 @@
   lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_DATA);
   if (isCoordinateLsaNew(lsaKey, lsSeqNo)) {
     CoordinateLsa corLsa;
-    if (corLsa.initializeFromContent(dataContent)) {
+    if (corLsa.deserialize(dataContent)) {
       installCoordinateLsa(corLsa);
     }
     else {
diff --git a/tests/mocks/lsa.cpp b/tests/mocks/lsa.cpp
index f4f6348..3ab6689 100644
--- a/tests/mocks/lsa.cpp
+++ b/tests/mocks/lsa.cpp
@@ -31,22 +31,15 @@
 }
 
 bool
-MockLsa::initializeFromContent(const std::string& content)
+MockLsa::deserialize(const std::string& content)
 {
   boost::char_separator<char> sep("|");
   boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
   boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
     tokens.begin();
-  m_origRouter = ndn::Name(*tok_iter++);
-  if (!(m_origRouter.size() > 0)) {
-    return false;
-  }
+
   try {
-    if (*tok_iter++ != std::to_string(Lsa::Type::MOCK)) {
-      return false;
-    }
-    m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
-    m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
+    deserializeCommon(tok_iter);
   }
   catch (const std::exception& e) {
     return false;
diff --git a/tests/mocks/lsa.hpp b/tests/mocks/lsa.hpp
index 738ad8f..0ce6e3a 100644
--- a/tests/mocks/lsa.hpp
+++ b/tests/mocks/lsa.hpp
@@ -42,7 +42,7 @@
   serialize() const override;
 
   bool
-  initializeFromContent(const std::string& content) override;
+  deserialize(const std::string& content) override;
 };
 
 } // namespace test
diff --git a/tests/test-lsa.cpp b/tests/test-lsa.cpp
index f3eff5c..413e393 100644
--- a/tests/test-lsa.cpp
+++ b/tests/test-lsa.cpp
@@ -163,7 +163,7 @@
 
   //If we don't do this the test will fail
   //Adjacent has default cost of 10 but no default
-  //connecting face URI, so initializeFromContent fails
+  //connecting face URI, so deserialize fails
   adj1.setFaceUri(ndn::util::FaceUri("udp://10.0.0.1"));
   adj2.setFaceUri(ndn::util::FaceUri("udp://10.0.0.2"));
 
@@ -176,7 +176,7 @@
   AdjLsa adjlsa1("router1", 1, testTimePoint, adjList.size(), adjList);
   AdjLsa adjlsa2;
 
-  BOOST_CHECK(adjlsa2.initializeFromContent(adjlsa1.serialize()));
+  BOOST_CHECK(adjlsa2.deserialize(adjlsa1.serialize()));
 
   BOOST_CHECK(adjlsa1.isEqualContent(adjlsa2));
 
@@ -188,7 +188,7 @@
   NameLsa nlsa1("router1", 1, testTimePoint, npl1);
   NameLsa nlsa2;
 
-  BOOST_CHECK(nlsa2.initializeFromContent(nlsa1.serialize()));
+  BOOST_CHECK(nlsa2.deserialize(nlsa1.serialize()));
 
   BOOST_CHECK_EQUAL(nlsa1.serialize(), nlsa2.serialize());
 
@@ -197,7 +197,7 @@
   CoordinateLsa clsa1("router1", 12, testTimePoint, 2.5, angles);
   CoordinateLsa clsa2;
 
-  BOOST_CHECK(clsa2.initializeFromContent(clsa1.serialize()));
+  BOOST_CHECK(clsa2.deserialize(clsa1.serialize()));
 
   BOOST_CHECK_EQUAL(clsa1.serialize(), clsa2.serialize());
 }