**breaking** route: consolidate routing tlv into route

refs: #5116

Plus some misc improvements

Change-Id: Id0902fec65160b4368b1b5066f460433aced98ed
diff --git a/src/route/routing-table.cpp b/src/route/routing-table.cpp
index 3fcd654..a2ec12b 100644
--- a/src/route/routing-table.cpp
+++ b/src/route/routing-table.cpp
@@ -16,7 +16,7 @@
  *
  * 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/>.
- **/
+ */
 
 #include "routing-table.hpp"
 #include "nlsr.hpp"
@@ -26,6 +26,7 @@
 #include "routing-table-entry.hpp"
 #include "name-prefix-table.hpp"
 #include "logger.hpp"
+#include "tlv-nlsr.hpp"
 
 #include <list>
 #include <string>
@@ -94,24 +95,22 @@
         // Inform the NPT that updates have been made
         NLSR_LOG_DEBUG("Calling Update NPT With new Route");
         (*afterRoutingChange)(m_rTable);
-        writeLog();
+        NLSR_LOG_DEBUG(*this);
         m_namePrefixTable.writeLog();
         m_fib.writeLog();
       }
       else {
-        NLSR_LOG_DEBUG("Adjacency building is scheduled, so"
-                   " routing table can not be calculated :(");
+        NLSR_LOG_DEBUG("Adjacency building is scheduled, so routing table can not be calculated :(");
       }
     }
     else {
-      NLSR_LOG_DEBUG("No Adj LSA of router itself,"
-                 " so Routing table can not be calculated :(");
+      NLSR_LOG_DEBUG("No Adj LSA of router itself, so Routing table can not be calculated :(");
       clearRoutingTable();
       clearDryRoutingTable(); // for dry run options
       // need to update NPT here
       NLSR_LOG_DEBUG("Calling Update NPT With new Route");
       (*afterRoutingChange)(m_rTable);
-      writeLog();
+      NLSR_LOG_DEBUG(*this);
       m_namePrefixTable.writeLog();
       m_fib.writeLog();
       // debugging purpose end
@@ -199,26 +198,6 @@
 }
 
 void
-RoutingTable::writeLog()
-{
-  NLSR_LOG_DEBUG("---------------Routing Table------------------");
-  for (const auto& rte : m_rTable) {
-    NLSR_LOG_DEBUG("Destination: " << rte.getDestination());
-    NLSR_LOG_DEBUG("Nexthops: ");
-    rte.getNexthopList().writeLog();
-  }
-
-  if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
-    NLSR_LOG_DEBUG("--------Hyperbolic Routing Table(Dry)---------");
-    for (const auto& rte : m_dryTable) {
-      NLSR_LOG_DEBUG("Destination: " << rte.getDestination());
-      NLSR_LOG_DEBUG("Nexthops: ");
-      rte.getNexthopList().writeLog();
-    }
-  }
-}
-
-void
 RoutingTable::addNextHopToDryTable(const ndn::Name& destRouter, NextHop& nh)
 {
   NLSR_LOG_DEBUG("Adding " << nh << " to dry table for destination: " << destRouter);
@@ -251,4 +230,100 @@
   }
 }
 
+template<ndn::encoding::Tag TAG>
+size_t
+RoutingTableStatus::wireEncode(ndn::EncodingImpl<TAG>& block) const
+{
+  size_t totalLength = 0;
+
+  for (auto it = m_dryTable.rbegin(); it != m_dryTable.rend(); ++it) {
+    totalLength += it->wireEncode(block);
+  }
+
+  for (auto it = m_rTable.rbegin(); it != m_rTable.rend(); ++it) {
+    totalLength += it->wireEncode(block);
+  }
+
+  totalLength += block.prependVarNumber(totalLength);
+  totalLength += block.prependVarNumber(ndn::tlv::nlsr::RoutingTable);
+
+  return totalLength;
+}
+
+NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(RoutingTableStatus);
+
+const ndn::Block&
+RoutingTableStatus::wireEncode() const
+{
+  if (m_wire.hasWire()) {
+    return m_wire;
+  }
+
+  ndn::EncodingEstimator estimator;
+  size_t estimatedSize = wireEncode(estimator);
+
+  ndn::EncodingBuffer buffer(estimatedSize, 0);
+  wireEncode(buffer);
+
+  m_wire = buffer.block();
+
+  return m_wire;
+}
+
+void
+RoutingTableStatus::wireDecode(const ndn::Block& wire)
+{
+  m_rTable.clear();
+
+  m_wire = wire;
+
+  if (m_wire.type() != ndn::tlv::nlsr::RoutingTable) {
+    std::stringstream error;
+    error << "Expected RoutingTableStatus Block, but Block is of a different type: #"
+          << m_wire.type();
+    BOOST_THROW_EXCEPTION(Error(error.str()));
+  }
+
+  m_wire.parse();
+
+  auto val = m_wire.elements_begin();
+
+  std::set<ndn::Name> destinations;
+  for (; val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::RoutingTableEntry; ++val) {
+    auto entry = RoutingTableEntry(*val);
+
+    if (destinations.emplace(entry.getDestination()).second) {
+      m_rTable.push_back(entry);
+    }
+    else {
+      // If destination already exists then this is the start of dry HR table
+      m_dryTable.push_back(entry);
+    }
+  }
+
+  if (val != m_wire.elements_end()) {
+    std::stringstream error;
+    error << "Expected the end of elements, but Block is of a different type: #"
+          << val->type();
+    BOOST_THROW_EXCEPTION(Error(error.str()));
+  }
+}
+
+std::ostream&
+operator<<(std::ostream& os, const RoutingTableStatus& rts)
+{
+  os << "Routing Table:\n";
+  for (const auto& rte : rts.getRoutingTableEntry()) {
+    os << rte;
+  }
+
+  if (!rts.getDryRoutingTableEntry().empty()) {
+    os << "Dry-Run Hyperbolic Routing Table:\n";
+    for (const auto& rte : rts.getDryRoutingTableEntry()) {
+      os << rte;
+    }
+  }
+  return os;
+}
+
 } // namespace nlsr