lsa: leverage class hierarchy when serializing.

refs: #4354

Change-Id: Ia3e79694c785b208184137d8ef0813629d61cc83
diff --git a/src/adjacency-list.cpp b/src/adjacency-list.cpp
index 4ceaa82..5666ed0 100644
--- a/src/adjacency-list.cpp
+++ b/src/adjacency-list.cpp
@@ -109,7 +109,6 @@
 
   for (size_t i = 0; i < ourList.size(); i++) {
     if (*(ourList[i]) != *(theirList[i])) {
-      std::cout << *ourList[i] << ":" << *theirList[i];
       return false;
     }
   }
diff --git a/src/lsa.cpp b/src/lsa.cpp
index 9c05710..afb76a1 100644
--- a/src/lsa.cpp
+++ b/src/lsa.cpp
@@ -38,6 +38,15 @@
 
 INIT_LOGGER("Lsa");
 
+std::string
+Lsa::getData() const
+{
+  std::ostringstream os;
+  os << m_origRouter << "|" << getType() << "|" << m_lsSeqNo << "|"
+     << ndn::time::toIsoString(m_expirationTimePoint) << "|";
+  return os.str();
+}
+
 const ndn::Name
 NameLsa::getKey() const
 {
@@ -59,11 +68,10 @@
 }
 
 std::string
-NameLsa::getData() const
+NameLsa::serialize() const
 {
   std::ostringstream os;
-  os << m_origRouter << "|" << Lsa::Type::NAME << "|" << m_lsSeqNo << "|"
-     << ndn::time::toIsoString(m_expirationTimePoint) << "|" << m_npl.size();
+  os << getData() << m_npl.size();
   for (const auto& name : m_npl.getNames()) {
     os << "|" << name;
   }
@@ -165,17 +173,13 @@
 }
 
 std::string
-CoordinateLsa::getData() const
+CoordinateLsa::serialize() const
 {
   std::ostringstream os;
-  os << m_origRouter << "|" << Lsa::Type::COORDINATE << "|" << m_lsSeqNo << "|"
-     << ndn::time::toIsoString(m_expirationTimePoint) << "|" << m_corRad << "|"
-     << m_angles.size() << "|";
-
+  os << getData() << m_corRad << "|" << m_angles.size() << "|";
   for (const auto& angle: m_angles) {
     os << angle << "|";
   }
-
   return os.str();
 }
 
@@ -258,11 +262,10 @@
 }
 
 std::string
-AdjLsa::getData() const
+AdjLsa::serialize() const
 {
   std::ostringstream os;
-  os << m_origRouter << "|" << Lsa::Type::ADJACENCY << "|" << m_lsSeqNo << "|"
-     << ndn::time::toIsoString(m_expirationTimePoint) << "|" << m_adl.size();
+  os << getData() << m_adl.size();
   for (const auto& adjacent : m_adl.getAdjList()) {
     os << "|" << adjacent.getName() << "|" << adjacent.getFaceUri()
        << "|" << adjacent.getLinkCost();
diff --git a/src/lsa.hpp b/src/lsa.hpp
index afd97e7..72ca145 100644
--- a/src/lsa.hpp
+++ b/src/lsa.hpp
@@ -107,13 +107,24 @@
     return m_expiringEventId;
   }
 
+  /*! \brief Return the data that this LSA represents.
+   */
   virtual std::string
-  getData() const = 0;
+  serialize() const = 0;
 
   virtual bool
   initializeFromContent(const std::string& content) = 0;
 
 protected:
+  /*! Get data common to all LSA types.
+
+    This method should be called by all LSA classes in their
+    serialize() method.
+   */
+  std::string
+  getData() const;
+
+protected:
   ndn::Name m_origRouter;
   uint32_t m_lsSeqNo;
   ndn::time::system_clock::TimePoint m_expirationTimePoint;
@@ -168,15 +179,6 @@
   const ndn::Name
   getKey() const;
 
-  /*! \brief Returns the data that this name LSA has.
-
-    Format is: \<original router
-    prefix\>|name|\<seq. no.\>|\<exp. time\>|\<prefix 1\>|\<prefix
-    2\>|...|\<prefix n\>|
-   */
-  std::string
-  getData() const override;
-
   /*! \brief Initializes this LSA object with content's data.
 
     \param content The data (e.g. name prefixes) to initialize this LSA with.
@@ -194,6 +196,15 @@
   void
   writeLog();
 
+  /*! \brief Returns the data that this name LSA has.
+
+    Format is: \<original router
+    prefix\>|name|\<seq. no.\>|\<exp. time\>|\<prefix 1\>|\<prefix
+    2\>|...|\<prefix n\>|
+   */
+  std::string
+  serialize() const override;
+
 private:
   NamePrefixList m_npl;
 };
@@ -238,16 +249,6 @@
   const ndn::Name
   getKey() const;
 
-  /*! \brief Returns the data this adjacency LSA has.
-
-    The format is: \<original
-    router\>|adjacency|\<seq. no.\>|\<exp. time\>|\<size\>|\<adjacency prefix
-    1\>|\<face uri 1\>|\<cost 1\>|...|\<adjacency prefix n\>|\<face uri
-    n\>|\<cost n\>|
-   */
-  std::string
-  getData() const override;
-
   /*! \brief Initializes this adj. LSA from the supplied content.
 
     \param content The content that this LSA is to have, formatted
@@ -279,7 +280,6 @@
   void
   writeLog();
 
-public:
   const_iterator
   begin() const
   {
@@ -292,6 +292,16 @@
     return m_adl.end();
   }
 
+  /*! \brief Returns the data this adjacency LSA has.
+
+    The format is: \<original
+    router\>|adjacency|\<seq. no.\>|\<exp. time\>|\<size\>|\<adjacency prefix
+    1\>|\<face uri 1\>|\<cost 1\>|...|\<adjacency prefix n\>|\<face uri
+    n\>|\<cost n\>|
+   */
+  std::string
+  serialize() const override;
+
 private:
   uint32_t m_noLink;
   AdjacencyList m_adl;
@@ -318,14 +328,6 @@
   const ndn::Name
   getKey() const;
 
-  /*! \brief Returns the data that this coordinate LSA represents.
-
-    The format is: \<original
-    router\>|coordinate|\<seq. no.\>|\<exp. time\>|\<radians\>|\<theta\>|
-  */
-  std::string
-  getData() const override;
-
   /*! \brief Initializes this coordinate LSA with the data in content.
 
     \param content The string content that is used to build the LSA.
@@ -367,6 +369,14 @@
   void
   writeLog();
 
+  /*! \brief Returns the data that this coordinate LSA represents.
+
+    The format is: \<original
+    router\>|coordinate|\<seq. no.\>|\<exp. time\>|\<radians\>|\<theta\>|
+  */
+  std::string
+  serialize() const override;
+
 private:
   double m_corRad;
   std::vector<double> m_angles;
diff --git a/src/lsdb.cpp b/src/lsdb.cpp
index 2661813..327c3f1 100644
--- a/src/lsdb.cpp
+++ b/src/lsdb.cpp
@@ -1097,7 +1097,7 @@
   NameLsa*  nameLsa = m_nlsr.getLsdb().findNameLsa(lsaKey);
   if (nameLsa != 0) {
     if (nameLsa->getLsSeqNo() == seqNo) {
-      std::string content = nameLsa->getData();
+      std::string content = nameLsa->serialize();
       putLsaData(interest,content);
       // increment SENT_NAME_LSA_DATA
       lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_DATA);
@@ -1131,7 +1131,7 @@
   AdjLsa* adjLsa = m_nlsr.getLsdb().findAdjLsa(lsaKey);
   if (adjLsa != 0) {
     if (adjLsa->getLsSeqNo() == seqNo) {
-      std::string content = adjLsa->getData();
+      std::string content = adjLsa->serialize();
       putLsaData(interest,content);
       // increment SENT_ADJ_LSA_DATA
       lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_DATA);
@@ -1165,7 +1165,7 @@
   CoordinateLsa* corLsa = m_nlsr.getLsdb().findCoordinateLsa(lsaKey);
   if (corLsa != 0) {
     if (corLsa->getLsSeqNo() == seqNo) {
-      std::string content = corLsa->getData();
+      std::string content = corLsa->serialize();
       putLsaData(interest,content);
       // increment SENT_COORD_LSA_DATA
       lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_DATA);
diff --git a/tests/mocks/lsa.cpp b/tests/mocks/lsa.cpp
index 5d495a1..f4f6348 100644
--- a/tests/mocks/lsa.cpp
+++ b/tests/mocks/lsa.cpp
@@ -25,7 +25,7 @@
 namespace test {
 
 std::string
-MockLsa::getData() const
+MockLsa::serialize() const
 {
   return "";
 }
diff --git a/tests/mocks/lsa.hpp b/tests/mocks/lsa.hpp
index c447384..738ad8f 100644
--- a/tests/mocks/lsa.hpp
+++ b/tests/mocks/lsa.hpp
@@ -39,7 +39,7 @@
   }
 
   std::string
-  getData() const override;
+  serialize() const override;
 
   bool
   initializeFromContent(const std::string& content) override;
diff --git a/tests/test-lsa.cpp b/tests/test-lsa.cpp
index c7851f8..f15507d 100644
--- a/tests/test-lsa.cpp
+++ b/tests/test-lsa.cpp
@@ -109,7 +109,7 @@
 
   BOOST_CHECK(clsa1.isEqualContent(clsa2));
 
-  BOOST_CHECK_EQUAL(clsa1.getData(), clsa2.getData());
+  BOOST_CHECK_EQUAL(clsa1.serialize(), clsa2.serialize());
 }
 
 BOOST_AUTO_TEST_CASE(IncrementAdjacentNumber)
@@ -179,7 +179,7 @@
   AdjLsa adjlsa1("router1", 1, testTimePoint, adjList.size(), adjList);
   AdjLsa adjlsa2;
 
-  BOOST_CHECK(adjlsa2.initializeFromContent(adjlsa1.getData()));
+  BOOST_CHECK(adjlsa2.initializeFromContent(adjlsa1.serialize()));
 
   BOOST_CHECK(adjlsa1.isEqualContent(adjlsa2));
 
@@ -195,18 +195,18 @@
   NameLsa nlsa1("router1", 1, testTimePoint, npl1);
   NameLsa nlsa2;
 
-  BOOST_CHECK(nlsa2.initializeFromContent(nlsa1.getData()));
+  BOOST_CHECK(nlsa2.initializeFromContent(nlsa1.serialize()));
 
-  BOOST_CHECK_EQUAL(nlsa1.getData(), nlsa2.getData());
+  BOOST_CHECK_EQUAL(nlsa1.serialize(), nlsa2.serialize());
 
   //Coordinate LSA
   std::vector<double> angles = {30, 40.0};
   CoordinateLsa clsa1("router1", 12, testTimePoint, 2.5, angles);
   CoordinateLsa clsa2;
 
-  BOOST_CHECK(clsa2.initializeFromContent(clsa1.getData()));
+  BOOST_CHECK(clsa2.initializeFromContent(clsa1.serialize()));
 
-  BOOST_CHECK_EQUAL(clsa1.getData(), clsa2.getData());
+  BOOST_CHECK_EQUAL(clsa1.serialize(), clsa2.serialize());
 }
 
 BOOST_AUTO_TEST_SUITE(TestNameLsa)
diff --git a/tests/test-lsdb.cpp b/tests/test-lsdb.cpp
index e04c74b..2a4d11c 100644
--- a/tests/test-lsdb.cpp
+++ b/tests/test-lsdb.cpp
@@ -180,11 +180,11 @@
   ndn::Name prefix("/ndn/edu/memphis/netlab/research/nlsr/test/prefix/");
 
   int nPrefixes = 0;
-  while (lsa.getData().size() < ndn::MAX_NDN_PACKET_SIZE) {
+  while (lsa.serialize().size() < ndn::MAX_NDN_PACKET_SIZE) {
     lsa.addName(ndn::Name(prefix).appendNumber(++nPrefixes));
   }
 
-  std::string expectedDataContent = lsa.getData();
+  std::string expectedDataContent = lsa.serialize();
   lsdb.installNameLsa(lsa);
 
   ndn::Name interestName("/ndn/NLSR/LSA/cs/%C1.Router/router1/NAME/");
@@ -226,14 +226,14 @@
   ndn::Name interestName("/ndn/NLSR/LSA/cs/%C1.Router/router1/NAME/");
   interestName.appendNumber(seqNo);
 
-  const ndn::ConstBufferPtr bufferPtr = std::make_shared<ndn::Buffer>(lsa.getData().c_str(),
-                                                                 lsa.getData().size());
+  const ndn::ConstBufferPtr bufferPtr = std::make_shared<ndn::Buffer>(lsa.serialize().c_str(),
+                                                                 lsa.serialize().size());
   lsdb.afterFetchLsa(bufferPtr, interestName);
 
   NameLsa* foundLsa = lsdb.findNameLsa(lsa.getKey());
   BOOST_REQUIRE(foundLsa != nullptr);
 
-  BOOST_CHECK_EQUAL(foundLsa->getData(), lsa.getData());
+  BOOST_CHECK_EQUAL(foundLsa->serialize(), lsa.serialize());
 }
 
 BOOST_AUTO_TEST_CASE(LsdbRemoveAndExists)
diff --git a/tests/test-statistics.cpp b/tests/test-statistics.cpp
index ad2f0d9..bc029e1 100644
--- a/tests/test-statistics.cpp
+++ b/tests/test-statistics.cpp
@@ -296,8 +296,8 @@
   AdjLsa aLsa(routerName, seqNo, MAX_TIME, 1, nlsr.getAdjacencyList());
   lsdb.installAdjLsa(aLsa);
 
-  const ndn::ConstBufferPtr aBuffer = std::make_shared<ndn::Buffer>(aLsa.getData().c_str(),
-                                                                    aLsa.getData().size());
+  const ndn::ConstBufferPtr aBuffer = std::make_shared<ndn::Buffer>(aLsa.serialize().c_str(),
+                                                                    aLsa.serialize().size());
   lsdb.afterFetchLsa(aBuffer, adjInterest);
   BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_ADJ_LSA_DATA), 1);
 
@@ -308,8 +308,8 @@
   CoordinateLsa cLsa(routerName, seqNo, MAX_TIME, 2.5, angles);
   lsdb.installCoordinateLsa(cLsa);
 
-  const ndn::ConstBufferPtr cBuffer = std::make_shared<ndn::Buffer>(cLsa.getData().c_str(),
-                                                                   cLsa.getData().size());
+  const ndn::ConstBufferPtr cBuffer = std::make_shared<ndn::Buffer>(cLsa.serialize().c_str(),
+                                                                   cLsa.serialize().size());
   lsdb.afterFetchLsa(cBuffer, coordInterest);
   BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_COORD_LSA_DATA), 1);
 
@@ -319,8 +319,8 @@
   NameLsa nLsa(routerName, seqNo, MAX_TIME, nlsr.getNamePrefixList());
   lsdb.installNameLsa(nLsa);
 
-  const ndn::ConstBufferPtr nBuffer = std::make_shared<ndn::Buffer>(nLsa.getData().c_str(),
-                                                                   nLsa.getData().size());
+  const ndn::ConstBufferPtr nBuffer = std::make_shared<ndn::Buffer>(nLsa.serialize().c_str(),
+                                                                   nLsa.serialize().size());
   lsdb.afterFetchLsa(nBuffer, interestName);
   BOOST_CHECK_EQUAL(collector.getStatistics().get(Statistics::PacketType::RCV_NAME_LSA_DATA), 1);