rib: fetch face dataset with StatusDataset client

refs #3620

Change-Id: Ibf1a0e064e105389d6b733b2d1f0791dff356208
diff --git a/rib/rib-manager.cpp b/rib/rib-manager.cpp
index c0c6ce8..4408b03 100644
--- a/rib/rib-manager.cpp
+++ b/rib/rib-manager.cpp
@@ -318,41 +318,16 @@
 {
   NFD_LOG_DEBUG("Fetching active faces");
 
-  Interest interest(FACES_LIST_DATASET_PREFIX);
-  interest.setChildSelector(1);
-  interest.setMustBeFresh(true);
-
-  shared_ptr<ndn::OBufferStream> buffer = make_shared<ndn::OBufferStream>();
-
-  m_face.expressInterest(interest,
-                         bind(&RibManager::fetchSegments, this, _2, buffer),
-                         bind(&RibManager::onFetchFaceStatusTimeout, this));
+  m_nfdController.fetch<ndn::nfd::FaceDataset>(
+    bind(&RibManager::removeInvalidFaces, this, _1),
+    bind(&RibManager::onFetchActiveFacesFailure, this, _1, _2),
+    ndn::nfd::CommandOptions());
 }
 
 void
-RibManager::fetchSegments(const Data& data, shared_ptr<ndn::OBufferStream> buffer)
+RibManager::onFetchActiveFacesFailure(uint32_t code, const std::string& reason)
 {
-  buffer->write(reinterpret_cast<const char*>(data.getContent().value()),
-                data.getContent().value_size());
-
-  uint64_t currentSegment = data.getName().get(-1).toSegment();
-
-  const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId();
-
-  if (finalBlockId.empty() || finalBlockId.toSegment() > currentSegment) {
-    m_face.expressInterest(data.getName().getPrefix(-1).appendSegment(currentSegment+1),
-                           bind(&RibManager::fetchSegments, this, _2, buffer),
-                           bind(&RibManager::onFetchFaceStatusTimeout, this));
-  }
-  else {
-    removeInvalidFaces(buffer);
-  }
-}
-
-void
-RibManager::onFetchFaceStatusTimeout()
-{
-  std::cerr << "Face Status Dataset request timed out" << std::endl;
+  NFD_LOG_DEBUG("Face Status Dataset request failure " << code << " " << reason);
   scheduleActiveFaceFetch(ACTIVE_FACE_FETCH_INTERVAL);
 }
 
@@ -373,34 +348,19 @@
 }
 
 void
-RibManager::removeInvalidFaces(shared_ptr<ndn::OBufferStream> buffer)
+RibManager::removeInvalidFaces(const std::vector<ndn::nfd::FaceStatus>& activeFaces)
 {
   NFD_LOG_DEBUG("Checking for invalid face registrations");
 
-  ndn::ConstBufferPtr buf = buffer->buf();
-
-  Block block;
-  size_t offset = 0;
-  FaceIdSet activeFaces;
-
-  while (offset < buf->size()) {
-    bool isOk = false;
-    std::tie(isOk, block) = Block::fromBuffer(buf, offset);
-    if (!isOk) {
-      std::cerr << "ERROR: cannot decode FaceStatus TLV" << std::endl;
-      break;
-    }
-
-    offset += block.size();
-
-    ndn::nfd::FaceStatus status(block);
-    activeFaces.insert(status.getFaceId());
+  FaceIdSet activeFaceIds;
+  for (const ndn::nfd::FaceStatus& item : activeFaces) {
+    activeFaceIds.insert(item.getFaceId());
   }
 
   // Look for face IDs that were registered but not active to find missed
   // face destroyed events
   for (auto&& faceId : m_registeredFaces) {
-    if (activeFaces.find(faceId) == activeFaces.end()) {
+    if (activeFaceIds.count(faceId) == 0) {
       NFD_LOG_DEBUG("Removing invalid face ID: " << faceId);
 
       scheduler::schedule(time::seconds(0),
diff --git a/rib/rib-manager.hpp b/rib/rib-manager.hpp
index 686ecd7..99f5ccd 100644
--- a/rib/rib-manager.hpp
+++ b/rib/rib-manager.hpp
@@ -115,10 +115,7 @@
   fetchActiveFaces();
 
   void
-  fetchSegments(const Data& data, shared_ptr<ndn::OBufferStream> buffer);
-
-  void
-  onFetchFaceStatusTimeout();
+  onFetchActiveFacesFailure(uint32_t code, const std::string& reason);
 
   void
   onFaceDestroyedEvent(uint64_t faceId);
@@ -130,10 +127,10 @@
   /**
    * @brief remove invalid faces
    *
-   * @param buffer Face dataset contents
+   * @param status Face dataset
   */
   void
-  removeInvalidFaces(shared_ptr<ndn::OBufferStream> buffer);
+  removeInvalidFaces(const std::vector<ndn::nfd::FaceStatus>& activeFaces);
 
   /**
    * @brief response to face events
diff --git a/tests/rib/rib-manager.t.cpp b/tests/rib/rib-manager.t.cpp
index f3df261..450016d 100644
--- a/tests/rib/rib-manager.t.cpp
+++ b/tests/rib/rib-manager.t.cpp
@@ -570,15 +570,10 @@
 
   ndn::nfd::FaceStatus status;
   status.setFaceId(1);
+  std::vector<ndn::nfd::FaceStatus> activeFaces;
+  activeFaces.push_back(status);
 
-  auto data = makeData("/localhost/nfd/faces/list");
-  data->setContent(status.wireEncode());
-
-  auto buffer = make_shared<ndn::OBufferStream>();
-  buffer->write(reinterpret_cast<const char*>(data->getContent().value()),
-                data->getContent().value_size());
-
-  m_manager.removeInvalidFaces(buffer);
+  m_manager.removeInvalidFaces(activeFaces);
   advanceClocks(time::milliseconds(100));
   BOOST_REQUIRE_EQUAL(m_rib.size(), 1);