src: Fix non-incrementing Adjacency number in AdjLsa::writeLog

refs #2594

Change-Id: I64ae57060ffb95c92e86da356eb6ba80f1d77f06
diff --git a/src/adjacency-list.hpp b/src/adjacency-list.hpp
index 76c9e5f..2325f49 100644
--- a/src/adjacency-list.hpp
+++ b/src/adjacency-list.hpp
@@ -1,7 +1,8 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  University of Memphis,
- *                     Regents of the University of California
+ * Copyright (c) 2014-2015,  The University of Memphis,
+ *                           Regents of the University of California,
+ *                           Arizona Board of Regents.
  *
  * This file is part of NLSR (Named-data Link State Routing).
  * See AUTHORS.md for complete list of NLSR authors and contributors.
@@ -16,10 +17,8 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NLSR, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- *
- * \author A K M Mahmudul Hoque <ahoque1@memphis.edu>
- *
  **/
+
 #ifndef NLSR_ADJACENCY_LIST_HPP
 #define NLSR_ADJACENCY_LIST_HPP
 
@@ -34,8 +33,10 @@
 
 class AdjacencyList
 {
-
 public:
+  typedef std::list<Adjacent>::const_iterator const_iterator;
+  typedef std::list<Adjacent>::iterator iterator;
+
   AdjacencyList();
   ~AdjacencyList();
 
@@ -110,8 +111,21 @@
   void
   writeLog();
 
+public:
+  const_iterator
+  begin() const
+  {
+    return m_adjList.begin();
+  }
+
+  const_iterator
+  end() const
+  {
+    return m_adjList.end();
+  }
+
 private:
-  std::list<Adjacent>::iterator
+  iterator
   find(const ndn::Name& adjName);
 
 private:
diff --git a/src/lsa.cpp b/src/lsa.cpp
index 7b96db2..8830951 100644
--- a/src/lsa.cpp
+++ b/src/lsa.cpp
@@ -320,22 +320,30 @@
 void
 AdjLsa::writeLog()
 {
-  _LOG_DEBUG("Adj Lsa: ");
-  _LOG_DEBUG("  Origination Router: " << m_origRouter);
-  _LOG_DEBUG("  Ls Type: " << m_lsType);
-  _LOG_DEBUG("  Ls Seq No: " << m_lsSeqNo);
-  _LOG_DEBUG("  Ls Lifetime: " << m_expirationTimePoint);
-  _LOG_DEBUG("  Adjacents: ");
-  int i = 1;
-  std::list<Adjacent> al = m_adl.getAdjList();
-  for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++)
-  {
-    _LOG_DEBUG("    Adjacent " << i << ": ");;
-    _LOG_DEBUG("      Adjacent Name: " << (*it).getName());
-    _LOG_DEBUG("      Connecting FaceUri: " << (*it).getConnectingFaceUri());
-    _LOG_DEBUG("      Link Cost: " << (*it).getLinkCost());
-  }
-  _LOG_DEBUG("adj_lsa_end");
+  _LOG_DEBUG(*this);
 }
 
-}//namespace nlsr
+std::ostream&
+operator<<(std::ostream& os, const AdjLsa& adjLsa)
+{
+  os << "Adj Lsa:\n"
+     << "  Origination Router: " << adjLsa.getOrigRouter() << "\n"
+     << "  Ls Type: " << adjLsa.getLsType() << "\n"
+     << "  Ls Seq No: " << adjLsa.getLsSeqNo() << "\n"
+     << "  Ls Lifetime: " << adjLsa.getExpirationTimePoint() << "\n"
+     << "  Adjacents: \n";
+
+  int adjacencyIndex = 1;
+
+  for (const Adjacent& adjacency : adjLsa) {
+  os << "    Adjacent " << adjacencyIndex++ << ":\n"
+     << "      Adjacent Name: " << adjacency.getName() << "\n"
+     << "      Connecting FaceUri: " << adjacency.getConnectingFaceUri() << "\n"
+     << "      Link Cost: " << adjacency.getLinkCost() << "\n";
+  }
+  os << "adj_lsa_end";
+
+  return os;
+}
+
+} // namespace nlsr
diff --git a/src/lsa.hpp b/src/lsa.hpp
index dcf5f01..544f01d 100644
--- a/src/lsa.hpp
+++ b/src/lsa.hpp
@@ -165,6 +165,8 @@
 class AdjLsa: public Lsa
 {
 public:
+  typedef AdjacencyList::const_iterator const_iterator;
+
   AdjLsa()
     : Lsa()
     , m_adl()
@@ -215,6 +217,19 @@
   void
   writeLog();
 
+public:
+  const_iterator
+  begin() const
+  {
+    return m_adl.begin();
+  }
+
+  const_iterator
+  end() const
+  {
+    return m_adl.end();
+  }
+
 private:
   uint32_t m_noLink;
   AdjacencyList m_adl;
@@ -285,6 +300,9 @@
   static const std::string TYPE_STRING;
 };
 
+std::ostream&
+operator<<(std::ostream& os, const AdjLsa& adjLsa);
+
 }//namespace nlsr
 
 #endif //NLSR_LSA_HPP
diff --git a/tests/test-lsa.cpp b/tests/test-lsa.cpp
index ca6d05b..57fa403 100644
--- a/tests/test-lsa.cpp
+++ b/tests/test-lsa.cpp
@@ -19,14 +19,19 @@
  * NLSR, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  **/
 
+#include "test-common.hpp"
+
+#include "adjacent.hpp"
 #include "lsa.hpp"
 #include "name-prefix-list.hpp"
-#include "adjacent.hpp"
-#include <boost/test/unit_test.hpp>
+
 #include <ndn-cxx/util/time.hpp>
 
+#include <sstream>
+
 namespace nlsr {
 namespace test {
+
 BOOST_AUTO_TEST_SUITE(TestLsa)
 
 BOOST_AUTO_TEST_CASE(NameLsaBasic)
@@ -91,7 +96,51 @@
   BOOST_CHECK_EQUAL(clsa1.getData(), clsa2.getData());
 }
 
+BOOST_AUTO_TEST_CASE(IncrementAdjacentNumber)
+{
+  Adjacent adj1("adjacent1");
+  Adjacent adj2("adjacent2");
+
+  adj1.setStatus(Adjacent::STATUS_ACTIVE);
+  adj2.setStatus(Adjacent::STATUS_ACTIVE);
+
+  AdjacencyList adjList;
+  adjList.insert(adj1);
+  adjList.insert(adj2);
+
+  ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now();
+
+  std::ostringstream ss;
+  ss << testTimePoint;
+
+  const std::string TEST_TIME_POINT_STRING = ss.str();
+
+  AdjLsa lsa("router1", AdjLsa::TYPE_STRING, 12, testTimePoint, 1, adjList);
+
+  std::string EXPECTED_OUTPUT =
+    "Adj Lsa:\n"
+    "  Origination Router: /router1\n"
+    "  Ls Type: adjacency\n"
+    "  Ls Seq No: 12\n"
+    "  Ls Lifetime: " + TEST_TIME_POINT_STRING + "\n"
+    "  Adjacents: \n"
+    "    Adjacent 1:\n"
+    "      Adjacent Name: /adjacent1\n"
+    "      Connecting FaceUri: \n"
+    "      Link Cost: 10\n"
+    "    Adjacent 2:\n"
+    "      Adjacent Name: /adjacent2\n"
+    "      Connecting FaceUri: \n"
+    "      Link Cost: 10\n"
+    "adj_lsa_end";
+
+  std::ostringstream os;
+  os << lsa;
+
+  BOOST_CHECK_EQUAL(os.str(), EXPECTED_OUTPUT);
+}
+
 BOOST_AUTO_TEST_SUITE_END()
 
-} //namespace test
-} //namespace nlsr
+} // namespace test
+} // namespace nlsr