fib: add EndpointId field in NextHop record

refs: #4284

Change-Id: If0cfc21cfa81d6fa3c1590ecdce8fbbc1ea95e13
diff --git a/daemon/fw/access-strategy.cpp b/daemon/fw/access-strategy.cpp
index 8460a00..31e2b91 100644
--- a/daemon/fw/access-strategy.cpp
+++ b/daemon/fw/access-strategy.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2018,  Regents of the University of California,
+ * Copyright (c) 2014-2019,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -137,7 +137,7 @@
   }
 
   Face* outFace = this->getFace(mi.lastNexthop);
-  if (outFace == nullptr || !fibEntry.hasNextHop(*outFace)) {
+  if (outFace == nullptr || !fibEntry.hasNextHop(*outFace, 0)) {
     NFD_LOG_DEBUG(pitEntry->getInterest() << " last-nexthop-gone");
     return false;
   }
diff --git a/daemon/fw/ncc-strategy.cpp b/daemon/fw/ncc-strategy.cpp
index 0f7f4e1..bb96009 100644
--- a/daemon/fw/ncc-strategy.cpp
+++ b/daemon/fw/ncc-strategy.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2018,  Regents of the University of California,
+ * Copyright (c) 2014-2019,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -81,7 +81,7 @@
   size_t nUpstreams = nexthops.size();
 
   shared_ptr<Face> bestFace = meInfo.getBestFace();
-  if (bestFace != nullptr && fibEntry.hasNextHop(*bestFace) &&
+  if (bestFace != nullptr && fibEntry.hasNextHop(*bestFace, 0) &&
       !wouldViolateScope(inFace, interest, *bestFace) &&
       canForwardToLegacy(*pitEntry, *bestFace)) {
     // TODO Should we use `randlow = 100 + nrand48(h->seed) % 4096U;` ?
@@ -111,7 +111,7 @@
   }
 
   shared_ptr<Face> previousFace = meInfo.previousFace.lock();
-  if (previousFace != nullptr && fibEntry.hasNextHop(*previousFace) &&
+  if (previousFace != nullptr && fibEntry.hasNextHop(*previousFace, 0) &&
       !wouldViolateScope(inFace, interest, *previousFace) &&
       canForwardToLegacy(*pitEntry, *previousFace)) {
     --nUpstreams;
@@ -157,7 +157,7 @@
   MeasurementsEntryInfo& meInfo = this->getMeasurementsEntryInfo(pitEntry);
 
   shared_ptr<Face> previousFace = meInfo.previousFace.lock();
-  if (previousFace != nullptr && fibEntry.hasNextHop(*previousFace) &&
+  if (previousFace != nullptr && fibEntry.hasNextHop(*previousFace, 0) &&
       !wouldViolateScope(*inFace, interest, *previousFace) &&
       canForwardToLegacy(*pitEntry, *previousFace)) {
     this->sendInterest(pitEntry, *previousFace, interest);
diff --git a/daemon/mgmt/fib-manager.cpp b/daemon/mgmt/fib-manager.cpp
index 5c04977..b73698d 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-2018,  Regents of the University of California,
+ * Copyright (c) 2014-2019,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -78,7 +78,7 @@
   }
 
   fib::Entry* entry = m_fib.insert(prefix).first;
-  entry->addNextHop(*face, cost);
+  entry->addOrUpdateNextHop(*face, 0, cost);
 
   NFD_LOG_TRACE("fib/add-nexthop(" << prefix << ',' << faceId << ',' << cost << "): OK");
   return done(ControlResponse(200, "Success").setBody(parameters.wireEncode()));
@@ -107,7 +107,7 @@
     return;
   }
 
-  entry->removeNextHop(*face);
+  entry->removeNextHop(*face, 0);
   if (!entry->hasNextHops()) {
     m_fib.erase(*entry);
     NFD_LOG_TRACE("fib/remove-nexthop(" << prefix << ',' << faceId << "): OK entry-erased");
diff --git a/daemon/nfd.cpp b/daemon/nfd.cpp
index 28f6989..f4d136a 100644
--- a/daemon/nfd.cpp
+++ b/daemon/nfd.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2018,  Regents of the University of California,
+ * Copyright (c) 2014-2019,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -169,7 +169,7 @@
 
   // add FIB entry for NFD Management Protocol
   Name topPrefix("/localhost/nfd");
-  m_forwarder->getFib().insert(topPrefix).first->addNextHop(*m_internalFace, 0);
+  m_forwarder->getFib().insert(topPrefix).first->addOrUpdateNextHop(*m_internalFace, 0, 0);
   m_dispatcher->addTopPrefix(topPrefix, false);
 }
 
diff --git a/daemon/table/cleanup.cpp b/daemon/table/cleanup.cpp
index 104bdf1..5a45bf7 100644
--- a/daemon/table/cleanup.cpp
+++ b/daemon/table/cleanup.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-2019,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -36,7 +36,7 @@
   for (const name_tree::Entry& nte : nt) {
     fib::Entry* fibEntry = nte.getFibEntry();
     if (fibEntry != nullptr) {
-      fib.removeNextHop(*fibEntry, face);
+      fib.removeNextHopByFace(*fibEntry, face);
     }
 
     for (const auto& pitEntry : nte.getPitEntries()) {
diff --git a/daemon/table/fib-entry.cpp b/daemon/table/fib-entry.cpp
index c2e299f..cf14936 100644
--- a/daemon/table/fib-entry.cpp
+++ b/daemon/table/fib-entry.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-2019,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -35,43 +35,52 @@
 }
 
 NextHopList::iterator
-Entry::findNextHop(const Face& face)
+Entry::findNextHop(const Face& face, uint64_t endpointId)
 {
   return std::find_if(m_nextHops.begin(), m_nextHops.end(),
-                      [&face] (const NextHop& nexthop) {
-                        return &nexthop.getFace() == &face;
+                      [&face, endpointId] (const NextHop& nexthop) {
+                        return &nexthop.getFace() == &face && nexthop.getEndpointId() == endpointId;
                       });
 }
 
 bool
-Entry::hasNextHop(const Face& face) const
+Entry::hasNextHop(const Face& face, uint64_t endpointId) const
 {
-  return const_cast<Entry*>(this)->findNextHop(face) != m_nextHops.end();
+  return const_cast<Entry*>(this)->findNextHop(face, endpointId) != m_nextHops.end();
 }
 
 void
-Entry::addNextHop(Face& face, uint64_t cost)
+Entry::addOrUpdateNextHop(Face& face, uint64_t endpointId, uint64_t cost)
 {
-  auto it = this->findNextHop(face);
+  auto it = this->findNextHop(face, endpointId);
   if (it == m_nextHops.end()) {
-    m_nextHops.emplace_back(face);
+    m_nextHops.emplace_back(face, endpointId);
     it = std::prev(m_nextHops.end());
   }
-
   it->setCost(cost);
   this->sortNextHops();
 }
 
 void
-Entry::removeNextHop(const Face& face)
+Entry::removeNextHop(const Face& face, uint64_t endpointId)
 {
-  auto it = this->findNextHop(face);
+  auto it = this->findNextHop(face, endpointId);
   if (it != m_nextHops.end()) {
     m_nextHops.erase(it);
   }
 }
 
 void
+Entry::removeNextHopByFace(const Face& face)
+{
+  auto it = std::remove_if(m_nextHops.begin(), m_nextHops.end(),
+                           [&face] (const NextHop& nexthop) {
+                             return &nexthop.getFace() == &face;
+                           });
+  m_nextHops.erase(it, m_nextHops.end());
+}
+
+void
 Entry::sortNextHops()
 {
   std::sort(m_nextHops.begin(), m_nextHops.end(),
diff --git a/daemon/table/fib-entry.hpp b/daemon/table/fib-entry.hpp
index 24489b9..2b86f66 100644
--- a/daemon/table/fib-entry.hpp
+++ b/daemon/table/fib-entry.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2018,  Regents of the University of California,
+ * Copyright (c) 2014-2019,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -74,32 +74,36 @@
     return !m_nextHops.empty();
   }
 
-  /** \return whether there is a NextHop record for \p face
+  /** \return whether there is a NextHop record for \p face with the given \p endpointId
    */
   bool
-  hasNextHop(const Face& face) const;
+  hasNextHop(const Face& face, uint64_t endpointId) const;
 
   /** \brief adds a NextHop record
    *
-   *  If a NextHop record for \p face already exists, its cost is updated.
+   *  If a NextHop record for \p face and \p endpointId already exists,
+   *  its cost is updated.
    */
   void
-  addNextHop(Face& face, uint64_t cost);
+  addOrUpdateNextHop(Face& face, uint64_t endpointId, uint64_t cost);
 
-  /** \brief removes a NextHop record
-   *
-   *  If no NextHop record for face exists, do nothing.
+  /** \brief removes the NextHop record for \p face with the given \p endpointId
    */
   void
-  removeNextHop(const Face& face);
+  removeNextHop(const Face& face, uint64_t endpointId);
+
+  /** \brief removes all NextHop records on \p face for any \p endpointId
+   */
+  void
+  removeNextHopByFace(const Face& face);
 
 private:
   /** \note This method is non-const because mutable iterators are needed by callers.
    */
   NextHopList::iterator
-  findNextHop(const Face& face);
+  findNextHop(const Face& face, uint64_t endpointId);
 
-  /** \brief sorts the nexthop list
+  /** \brief sorts the nexthop list by cost
    */
   void
   sortNextHops();
diff --git a/daemon/table/fib-nexthop.cpp b/daemon/table/fib-nexthop.cpp
index e554362..e456c15 100644
--- a/daemon/table/fib-nexthop.cpp
+++ b/daemon/table/fib-nexthop.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-2019,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -28,8 +28,9 @@
 namespace nfd {
 namespace fib {
 
-NextHop::NextHop(Face& face)
+NextHop::NextHop(Face& face, uint64_t endpointId)
   : m_face(&face)
+  , m_endpointId(endpointId)
   , m_cost(0)
 {
 }
diff --git a/daemon/table/fib-nexthop.hpp b/daemon/table/fib-nexthop.hpp
index 83b774f..313508d 100644
--- a/daemon/table/fib-nexthop.hpp
+++ b/daemon/table/fib-nexthop.hpp
@@ -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-2019,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -39,7 +39,7 @@
 {
 public:
   explicit
-  NextHop(Face& face);
+  NextHop(Face& face, uint64_t endpointId);
 
   Face&
   getFace() const
@@ -48,6 +48,12 @@
   }
 
   uint64_t
+  getEndpointId() const
+  {
+    return m_endpointId;
+  }
+
+  uint64_t
   getCost() const
   {
     return m_cost;
@@ -61,6 +67,7 @@
 
 private:
   Face* m_face;
+  uint64_t m_endpointId;
   uint64_t m_cost;
 };
 
diff --git a/daemon/table/fib.cpp b/daemon/table/fib.cpp
index 7b6d21a..ccde921 100644
--- a/daemon/table/fib.cpp
+++ b/daemon/table/fib.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2018,  Regents of the University of California,
+ * Copyright (c) 2014-2019,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -134,16 +134,28 @@
 }
 
 void
-Fib::removeNextHop(Entry& entry, const Face& face)
+Fib::eraseIfEmpty(Entry& entry)
 {
-  entry.removeNextHop(face);
-
   if (!entry.hasNextHops()) {
     name_tree::Entry* nte = m_nameTree.getEntry(entry);
     this->erase(nte, false);
   }
 }
 
+void
+Fib::removeNextHop(Entry& entry, const Face& face, uint64_t endpointId)
+{
+  entry.removeNextHop(face, endpointId);
+  this->eraseIfEmpty(entry);
+}
+
+void
+Fib::removeNextHopByFace(Entry& entry, const Face& face)
+{
+  entry.removeNextHopByFace(face);
+  this->eraseIfEmpty(entry);
+}
+
 Fib::Range
 Fib::getRange() const
 {
diff --git a/daemon/table/fib.hpp b/daemon/table/fib.hpp
index 4b94c21..cfdc4f1 100644
--- a/daemon/table/fib.hpp
+++ b/daemon/table/fib.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2014-2018,  Regents of the University of California,
+ * Copyright (c) 2014-2019,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -105,10 +105,15 @@
   void
   erase(const Entry& entry);
 
-  /** \brief removes the NextHop record for face
+  /** \brief removes the NextHop record for \p face with a given \p endpointId
    */
   void
-  removeNextHop(Entry& entry, const Face& face);
+  removeNextHop(Entry& entry, const Face& face, uint64_t endpointId);
+
+  /** \brief removes the NextHop record for \p face for any \p endpointId
+   */
+  void
+  removeNextHopByFace(Entry& entry, const Face& face);
 
 public: // enumeration
   typedef boost::transformed_range<name_tree::GetTableEntry<Entry>, const name_tree::Range> Range;
@@ -144,6 +149,11 @@
   void
   erase(name_tree::Entry* nte, bool canDeleteNte = true);
 
+  /** \brief erase \p entry if it contains no nexthop record
+   */
+  void
+  eraseIfEmpty(Entry& entry);
+
   Range
   getRange() const;