mgmt+tests: no need to wrap ndn::nfd::FibEntry anymore

ndn-cxx provides the required operators now.

Change-Id: I7d7f371b89f8231853cb75682066ac09a858916d
Refs: #3903
diff --git a/daemon/mgmt/fib-manager.cpp b/daemon/mgmt/fib-manager.cpp
index 14675b8..7c27649 100644
--- a/daemon/mgmt/fib-manager.cpp
+++ b/daemon/mgmt/fib-manager.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014-2016,  Regents of the University of California,
+ * Copyright (c) 2014-2017,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -25,9 +25,12 @@
 
 #include "fib-manager.hpp"
 #include "fw/face-table.hpp"
+
 #include <ndn-cxx/lp/tags.hpp>
 #include <ndn-cxx/mgmt/nfd/fib-entry.hpp>
 
+#include <boost/range/adaptor/transformed.hpp>
+
 namespace nfd {
 
 NFD_LOG_INIT("FibManager");
@@ -118,23 +121,18 @@
 FibManager::listEntries(const Name& topPrefix, const Interest& interest,
                         ndn::mgmt::StatusDatasetContext& context)
 {
-  for (auto&& entry : m_fib) {
-    auto prefix = entry.getPrefix();
-    ndn::nfd::FibEntry record;
-    const auto& nextHops = entry.getNextHops();
-
-    for (auto&& next : nextHops) {
-      ndn::nfd::NextHopRecord nextHopRecord;
-      nextHopRecord.setFaceId(next.getFace().getId());
-      nextHopRecord.setCost(next.getCost());
-
-      record.addNextHopRecord(nextHopRecord);
-    }
-
-    record.setPrefix(prefix);
-    context.append(record.wireEncode());
+  for (const auto& entry : m_fib) {
+    const auto& nexthops = entry.getNextHops() |
+                           boost::adaptors::transformed([] (const fib::NextHop& nh) {
+                             return ndn::nfd::NextHopRecord()
+                                 .setFaceId(nh.getFace().getId())
+                                 .setCost(nh.getCost());
+                           });
+    context.append(ndn::nfd::FibEntry()
+                   .setPrefix(entry.getPrefix())
+                   .setNextHopRecords(std::begin(nexthops), std::end(nexthops))
+                   .wireEncode());
   }
-
   context.end();
 }
 
diff --git a/tests/daemon/mgmt/fib-manager.t.cpp b/tests/daemon/mgmt/fib-manager.t.cpp
index 161e2f7..6fc59d1 100644
--- a/tests/daemon/mgmt/fib-manager.t.cpp
+++ b/tests/daemon/mgmt/fib-manager.t.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014-2016,  Regents of the University of California,
+ * Copyright (c) 2014-2017,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -35,6 +35,8 @@
 namespace nfd {
 namespace tests {
 
+using ndn::nullopt;
+
 class FibManagerFixture : public NfdManagerCommonFixture
 {
 public:
@@ -48,13 +50,13 @@
   }
 
 public: // for test
-  ControlParameters
+  static ControlParameters
   makeParameters(const Name& name, const FaceId& id)
   {
     return ControlParameters().setName(name).setFaceId(id);
   }
 
-  ControlParameters
+  static ControlParameters
   makeParameters(const Name& name, const FaceId& id, const uint32_t& cost)
   {
     return ControlParameters().setName(name).setFaceId(id).setCost(cost);
@@ -78,14 +80,14 @@
     WRONG_N_NEXTHOPS,
     NO_NEXTHOP,
     WRONG_COST
-   };
+  };
 
   /**
    * @brief check whether the nexthop record is added / removed properly
    *
-   * @param expectedNNextHops use -1 to skip this check
-   * @param faceId use face::FACEID_NULL to skip NextHopRecord checks
-   * @param expectedCost use -1 to skip this check
+   * @param expectedNNextHops use nullopt to skip this check
+   * @param faceId use nullopt to skip NextHopRecord checks
+   * @param expectedCost use nullopt to skip this check
    *
    * @retval OK FIB entry is found by exact match and has the expected number of nexthops;
    *            NextHopRe record for faceId is found and has the expected cost
@@ -95,23 +97,25 @@
    * @retval WRONG_COST NextHopRecord for faceId has wrong cost
    */
   CheckNextHopResult
-  checkNextHop(const Name& prefix, ssize_t expectedNNextHops = -1,
-               FaceId faceId = face::FACEID_NULL, int32_t expectedCost = -1)
+  checkNextHop(const Name& prefix,
+               ndn::optional<size_t> expectedNNextHops = nullopt,
+               ndn::optional<FaceId> faceId = nullopt,
+               ndn::optional<uint64_t> expectedCost = nullopt) const
   {
     const fib::Entry* entry = m_fib.findExactMatch(prefix);
     if (entry == nullptr) {
       return CheckNextHopResult::NO_FIB_ENTRY;
     }
 
-    const fib::NextHopList& nextHops = entry->getNextHops();
-    if (expectedNNextHops != -1 && nextHops.size() != static_cast<size_t>(expectedNNextHops)) {
+    const auto& nextHops = entry->getNextHops();
+    if (expectedNNextHops && nextHops.size() != *expectedNNextHops) {
       return CheckNextHopResult::WRONG_N_NEXTHOPS;
     }
 
-    if (faceId != face::FACEID_NULL) {
+    if (faceId) {
       for (const auto& record : nextHops) {
-        if (record.getFace().getId() == faceId) {
-          if (expectedCost != -1 && record.getCost() != static_cast<uint32_t>(expectedCost))
+        if (record.getFace().getId() == *faceId) {
+          if (expectedCost && record.getCost() != *expectedCost)
             return CheckNextHopResult::WRONG_COST;
           else
             return CheckNextHopResult::OK;
@@ -129,29 +133,22 @@
 };
 
 std::ostream&
-operator<<(std::ostream &os, const FibManagerFixture::CheckNextHopResult& result)
+operator<<(std::ostream& os, FibManagerFixture::CheckNextHopResult result)
 {
   switch (result) {
   case FibManagerFixture::CheckNextHopResult::OK:
-    os << "OK";
-    break;
+    return os << "OK";
   case FibManagerFixture::CheckNextHopResult::NO_FIB_ENTRY:
-    os << "NO_FIB_ENTRY";
-    break;
+    return os << "NO_FIB_ENTRY";
   case FibManagerFixture::CheckNextHopResult::WRONG_N_NEXTHOPS:
-    os << "WRONG_N_NEXTHOPS";
-    break;
+    return os << "WRONG_N_NEXTHOPS";
   case FibManagerFixture::CheckNextHopResult::NO_NEXTHOP:
-    os << "NO_NEXTHOP";
-    break;
+    return os << "NO_NEXTHOP";
   case FibManagerFixture::CheckNextHopResult::WRONG_COST:
-    os << "WRONG_COST";
-    break;
-  default:
-    break;
+    return os << "WRONG_COST";
   };
 
-  return os;
+  return os << static_cast<int>(result);
 }
 
 BOOST_AUTO_TEST_SUITE(Mgmt)
@@ -171,7 +168,7 @@
                     CheckResponseResult::OK);
 
   // double check that the next hop was not added
-  BOOST_CHECK_EQUAL(checkNextHop("/hello", -1, face::FACEID_NULL, 101), CheckNextHopResult::NO_FIB_ENTRY);
+  BOOST_CHECK_EQUAL(checkNextHop("/hello", nullopt, nullopt, 101), CheckNextHopResult::NO_FIB_ENTRY);
 }
 
 BOOST_AUTO_TEST_CASE(ImplicitFaceId)
@@ -381,70 +378,18 @@
   testRemoveNextHop(makeParameters("/hello", face2 + 100));
   BOOST_REQUIRE_EQUAL(m_responses.size(), 1); // face does not exist
   BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
-  BOOST_CHECK_EQUAL(checkNextHop("/hello", -1, face2 + 100), CheckNextHopResult::NO_NEXTHOP);
+  BOOST_CHECK_EQUAL(checkNextHop("/hello", nullopt, face2 + 100), CheckNextHopResult::NO_NEXTHOP);
 
   testRemoveNextHop(makeParameters("/hello", face2));
   BOOST_REQUIRE_EQUAL(m_responses.size(), 1); // record does not exist
   BOOST_CHECK_EQUAL(checkResponse(0, expectedName, expectedResponse), CheckResponseResult::OK);
-  BOOST_CHECK_EQUAL(checkNextHop("/hello", -1, face2), CheckNextHopResult::NO_NEXTHOP);
+  BOOST_CHECK_EQUAL(checkNextHop("/hello", nullopt, face2), CheckNextHopResult::NO_NEXTHOP);
 }
 
 BOOST_AUTO_TEST_SUITE_END() // RemoveNextHop
 
 BOOST_AUTO_TEST_SUITE(List)
 
-// @todo Remove when ndn::nfd::FibEntry implements operator!= and operator<<
-class FibEntry : public ndn::nfd::FibEntry
-{
-public:
-  FibEntry() = default;
-
-  FibEntry(const ndn::nfd::FibEntry& entry)
-    : ndn::nfd::FibEntry(entry)
-  {
-  }
-};
-
-bool
-operator!=(const FibEntry& left, const FibEntry& right)
-{
-  if (left.getPrefix() != right.getPrefix()) {
-    return true;
-  }
-
-  const auto& leftNextHops = left.getNextHopRecords();
-  const auto& rightNextHops = right.getNextHopRecords();
-  if (leftNextHops.size() != rightNextHops.size()) {
-    return true;
-  }
-
-  for (const auto& nexthop : leftNextHops) {
-    auto hitEntry = std::find_if(rightNextHops.begin(), rightNextHops.end(),
-      [&] (const ndn::nfd::NextHopRecord& record) {
-        return nexthop.getCost() == record.getCost() && nexthop.getFaceId() == record.getFaceId();
-      });
-
-    if (hitEntry == rightNextHops.end()) {
-      return true;
-    }
-  }
-
-  return false;
-}
-
-std::ostream&
-operator<<(std::ostream &os, const FibEntry& entry)
-{
-  const auto& nexthops = entry.getNextHopRecords();
-  os << "[" << entry.getPrefix() << ", " << nexthops.size() << ": ";
-  for (auto record : nexthops) {
-    os << "{" << record.getFaceId() << ", " << record.getCost() << "} ";
-  }
-  os << "]";
-
-  return os;
-}
-
 BOOST_AUTO_TEST_CASE(FibDataset)
 {
   const size_t nEntries = 108;
@@ -459,34 +404,26 @@
 
   receiveInterest(makeInterest("/localhost/nfd/fib/list"));
 
-  Block content;
-  BOOST_CHECK_NO_THROW(content = concatenateResponses());
-  BOOST_CHECK_NO_THROW(content.parse());
+  Block content = concatenateResponses();
+  content.parse();
   BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
 
-  std::vector<FibEntry> receivedRecords, expectedRecords;
+  std::vector<ndn::nfd::FibEntry> receivedRecords, expectedRecords;
   for (size_t idx = 0; idx < nEntries; ++idx) {
-    BOOST_TEST_MESSAGE("processing element: " << idx);
-
-    FibEntry decodedEntry;
-    BOOST_REQUIRE_NO_THROW(decodedEntry.wireDecode(content.elements()[idx]));
+    ndn::nfd::FibEntry decodedEntry(content.elements()[idx]);
     receivedRecords.push_back(decodedEntry);
-
     actualPrefixes.erase(decodedEntry.getPrefix());
 
     auto matchedEntry = m_fib.findExactMatch(decodedEntry.getPrefix());
     BOOST_REQUIRE(matchedEntry != nullptr);
 
-    FibEntry record;
-    record.setPrefix(matchedEntry->getPrefix());
-    const auto& nextHops = matchedEntry->getNextHops();
-    for (const auto& next : nextHops) {
-      ndn::nfd::NextHopRecord nextHopRecord;
-      nextHopRecord.setFaceId(next.getFace().getId());
-      nextHopRecord.setCost(next.getCost());
-      record.addNextHopRecord(nextHopRecord);
+    expectedRecords.emplace_back();
+    expectedRecords.back().setPrefix(matchedEntry->getPrefix());
+    for (const auto& nh : matchedEntry->getNextHops()) {
+      expectedRecords.back().addNextHopRecord(ndn::nfd::NextHopRecord()
+                                              .setFaceId(nh.getFace().getId())
+                                              .setCost(nh.getCost()));
     }
-    expectedRecords.push_back(record);
   }
 
   BOOST_CHECK_EQUAL(actualPrefixes.size(), 0);