mgmt: fib manager implicit fib entry insertion and deletion

refs: #1340

Change-Id: If552bbff71b54c4616ff4478a470c9cdc2a5c4e4
diff --git a/daemon/mgmt/fib-manager.cpp b/daemon/mgmt/fib-manager.cpp
index dee765b..4e1dbe7 100644
--- a/daemon/mgmt/fib-manager.cpp
+++ b/daemon/mgmt/fib-manager.cpp
@@ -30,15 +30,6 @@
 
 const FibManager::VerbAndProcessor FibManager::COMMAND_VERBS[] =
   {
-    VerbAndProcessor(
-                     Name::Component("insert"),
-                     &FibManager::insertEntry
-                     ),
-
-    VerbAndProcessor(
-                     Name::Component("delete"),
-                     &FibManager::deleteEntry
-                     ),
 
     VerbAndProcessor(
                      Name::Component("add-nexthop"),
@@ -66,6 +57,11 @@
                           bind(&FibManager::onFibRequest, this, _2));
 }
 
+FibManager::~FibManager()
+{
+
+}
+
 void
 FibManager::onFibRequest(const Interest& request)
 {
@@ -152,35 +148,6 @@
 }
 
 void
-FibManager::insertEntry(const FibManagementOptions& options,
-                        ControlResponse& response)
-{
-  NFD_LOG_DEBUG("insert prefix: " << options.getName());
-  NFD_LOG_INFO("insert result: OK"
-               << " prefix: " << options.getName());
-  std::pair<shared_ptr<fib::Entry>, bool> insertResult = m_managedFib.insert(options.getName());
-  setResponse(response, 200, "Success", options.wireEncode());
-}
-
-void
-FibManager::deleteEntry(const FibManagementOptions& options,
-                        ControlResponse& response)
-{
-  NFD_LOG_DEBUG("delete prefix: " << options.getName());
-  NFD_LOG_INFO("delete result: OK"
-               << " prefix: " << options.getName());
-
-  m_managedFib.erase(options.getName());
-  setResponse(response, 200, "Success", options.wireEncode());
-}
-
-static inline bool
-nextHopEqPredicate(const fib::NextHop& target, const fib::NextHop& hop)
-{
-  return target.getFace()->getId() == hop.getFace()->getId();
-}
-
-void
 FibManager::addNextHop(const FibManagementOptions& options,
                        ControlResponse& response)
 {
@@ -191,22 +158,15 @@
   shared_ptr<Face> nextHopFace = m_getFace(options.getFaceId());
   if (static_cast<bool>(nextHopFace))
     {
-      shared_ptr<fib::Entry> entry = m_managedFib.findExactMatch(options.getName());
-      if (static_cast<bool>(entry))
-        {
-          entry->addNextHop(nextHopFace, options.getCost());
+      shared_ptr<fib::Entry> entry = m_managedFib.insert(options.getName()).first;
 
-          NFD_LOG_INFO("add-nexthop result: OK"
-                       << " prefix:" << options.getName()
-                       << " faceid: " << options.getFaceId()
-                       << " cost: " << options.getCost());
-          setResponse(response, 200, "Success", options.wireEncode());
-        }
-      else
-        {
-          NFD_LOG_INFO("add-nexthop result: FAIL reason: unknown-prefix: " << options.getName());
-          setResponse(response, 404, "Prefix not found");
-        }
+      entry->addNextHop(nextHopFace, options.getCost());
+
+      NFD_LOG_INFO("add-nexthop result: OK"
+                   << " prefix:" << options.getName()
+                   << " faceid: " << options.getFaceId()
+                   << " cost: " << options.getCost());
+      setResponse(response, 200, "Success", options.wireEncode());
     }
   else
     {
@@ -232,6 +192,11 @@
           NFD_LOG_INFO("remove-nexthop result: OK prefix: " << options.getName()
                        << " faceid: " << options.getFaceId());
 
+          if (!entry->hasNextHops())
+            {
+              m_managedFib.erase(*entry);
+            }
+
           setResponse(response, 200, "Success", options.wireEncode());
         }
       else
diff --git a/daemon/mgmt/fib-manager.hpp b/daemon/mgmt/fib-manager.hpp
index 43b410e..1cbc49c 100644
--- a/daemon/mgmt/fib-manager.hpp
+++ b/daemon/mgmt/fib-manager.hpp
@@ -32,6 +32,9 @@
              function<shared_ptr<Face>(FaceId)> getFace,
              shared_ptr<InternalFace> face);
 
+  virtual
+  ~FibManager();
+
   void
   onFibRequest(const Interest& request);
 
diff --git a/daemon/table/fib.hpp b/daemon/table/fib.hpp
index 0fbacea..e1e5c5a 100644
--- a/daemon/table/fib.hpp
+++ b/daemon/table/fib.hpp
@@ -55,6 +55,9 @@
   void
   erase(const Name& prefix);
 
+  void
+  erase(const fib::Entry& entry);
+
   /** \brief removes the NextHop record for face in all entrites
    *  This is usually invoked when face goes away.
    *  Removing all NextHops in a FIB entry will not remove the FIB entry.
@@ -66,9 +69,6 @@
   size() const;
 
 private:
-  void
-  erase(const fib::Entry& entry);
-
   shared_ptr<fib::Entry>
   findLongestPrefixMatch(shared_ptr<name_tree::Entry> nameTreeEntry) const;