management: Updating nfd::Controller for latest changes in protocol

Also this commit introduces two generic methods that can be used to send
fib/(add|remove)-nexthop.  In addition, face/* and fib/* commands now
generate proper CommandInterests (using default identity/certificate).

Change-Id: Ib683ad37023365046e31a54ae03ed084feadc662
Refs: #1343
diff --git a/src/management/nfd-controller.cpp b/src/management/nfd-controller.cpp
index 735c927..723c984 100644
--- a/src/management/nfd-controller.cpp
+++ b/src/management/nfd-controller.cpp
@@ -17,7 +17,6 @@
 
 Controller::Controller(Face& face)
   : m_face(face)
-  , m_faceId(0)
 {
 }
 
@@ -26,49 +25,39 @@
                                const SuccessCallback& onSuccess,
                                const FailCallback&    onFail)
 {
-  // two stage process:
-  // 1. insert FIB entry
-  // 2. add-nexthop <self> to the FIB entry
-
-  // Step 1.
-  startFibCommand("insert",
-                  FibManagementOptions()
-                    .setName(prefixToRegister),
-                  bind(&Controller::selfRegisterPrefixAddNextop, this, _1, onSuccess, onFail),
-                  onFail);
+  fibAddNextHop(prefixToRegister, 0, 0, onSuccess, onFail);
 }
 
 void
-Controller::selfRegisterPrefixAddNextop(const FibManagementOptions& entry,
-                                        const SuccessCallback& onSuccess,
-                                        const FailCallback&    onFail)
-{
-  // Step 2.
-  startFibCommand("add-nexthop",
-                  FibManagementOptions(entry) // prefixToRegister should be inside the entry
-                    .setFaceId(0) // self-registration
-                    .setCost(0),
-                  bind(&Controller::recordSelfRegisteredFaceId, this, _1, onSuccess),
-                  onFail);
-}
-
-
-void
-Controller::selfDeregisterPrefix(const Name& prefixToRegister,
+Controller::selfDeregisterPrefix(const Name& prefixToDeRegister,
                                  const SuccessCallback& onSuccess,
                                  const FailCallback&    onFail)
 {
-  if (m_faceId == 0)
-    {
-      if (static_cast<bool>(onFail))
-        onFail("Face ID is not set, should have been set after a successful prefix registration command");
-      return;
-    }
+  fibRemoveNextHop(prefixToDeRegister, 0, onSuccess, onFail);
+}
 
+void
+Controller::fibAddNextHop(const Name& prefix, uint64_t faceId, int cost,
+                          const SuccessCallback& onSuccess,
+                          const FailCallback&    onFail)
+{
+  startFibCommand("add-nexthop",
+                  FibManagementOptions()
+                    .setName(prefix)
+                    .setFaceId(faceId)
+                    .setCost(cost),
+                  bind(onSuccess), onFail);
+}
+
+void
+Controller::fibRemoveNextHop(const Name& prefix, uint64_t faceId,
+                             const SuccessCallback& onSuccess,
+                             const FailCallback&    onFail)
+{
   startFibCommand("remove-nexthop",
                   FibManagementOptions()
-                    .setName(prefixToRegister)
-                    .setFaceId(m_faceId),
+                    .setName(prefix)
+                    .setFaceId(faceId),
                   bind(onSuccess), onFail);
 }
 
@@ -84,7 +73,7 @@
     .append(options.wireEncode());
 
   Interest fibCommandInterest(fibCommandInterestName);
-  // m_keyChain.sign(fibCommandInterest);
+  m_commandInterestGenerator.generate(fibCommandInterest);
 
   m_face.expressInterest(fibCommandInterest,
                          bind(&Controller::processFibCommandResponse, this, _2,
@@ -93,14 +82,6 @@
 }
 
 void
-Controller::recordSelfRegisteredFaceId(const FibManagementOptions& entry,
-                                       const SuccessCallback& onSuccess)
-{
-  m_faceId = entry.getFaceId();
-  onSuccess();
-}
-
-void
 Controller::processFibCommandResponse(Data& data,
                                       const FibCommandSucceedCallback& onSuccess,
                                       const FailCallback& onFail)
@@ -133,7 +114,7 @@
     .append(options.wireEncode());
 
   Interest faceCommandInterest(faceCommandInterestName);
-  // m_keyChain.sign(fibCommandInterest);
+  m_commandInterestGenerator.generate(faceCommandInterest);
 
   m_face.expressInterest(faceCommandInterest,
                          bind(&Controller::processFaceCommandResponse, this, _2,
@@ -161,5 +142,6 @@
         return onFail(e.what());
     }
 }
+
 } // namespace nfd
 } // namespace ndn
diff --git a/src/management/nfd-controller.hpp b/src/management/nfd-controller.hpp
index 968f232..aa85656 100644
--- a/src/management/nfd-controller.hpp
+++ b/src/management/nfd-controller.hpp
@@ -8,7 +8,7 @@
 #define NDN_MANAGEMENT_NFD_CONTROL_HPP
 
 #include "controller.hpp"
-#include "../security/key-chain.hpp"
+#include "../util/command-interest-generator.hpp"
 
 namespace ndn {
 
@@ -34,10 +34,47 @@
                      const FailCallback&    onFail);
 
   virtual void
-  selfDeregisterPrefix(const Name& prefixToRegister,
+  selfDeregisterPrefix(const Name& prefixToDeRegister,
                        const SuccessCallback& onSuccess,
                        const FailCallback&    onFail);
 
+  /**
+   * \brief Adds a nexthop to an existing or new FIB entry
+   *
+   * If FIB entry for the specified prefix does not exist, it will be automatically created.
+   *
+   * \param prefix    Prefix of the FIB entry
+   * \param faceId    ID of the face which should be added as a next hop for prefix FIB entry.
+   *                  If a nexthop of same FaceId exists on the FIB entry, its cost is updated.
+   *                  If FaceId is set to zero, it is implied as the face of the entity sending
+   *                  this command.
+   * \param cost      Cost that should be associated with the next hop
+   * \param onSuccess Callback that will be called when operation succeeds
+   * \param onFail    Callback that will be called when operation fails
+   */
+  void
+  fibAddNextHop(const Name& prefix, uint64_t faceId, int cost,
+                const SuccessCallback& onSuccess,
+                const FailCallback&    onFail);
+
+  /**
+   * \brief Remove a nexthop from FIB entry
+   *
+   * If after removal of the nexthop FIB entry has zero next hops, this FIB entry will
+   * be automatically deleted.
+   *
+   * \param prefix    Prefix of the FIB entry
+   * \param faceId    ID of the face which should be removed FIB entry.
+   *                  If FaceId is set to zero, it is implied as the face of the entity sending
+   *                  this command.
+   * \param onSuccess Callback that will be called when operation succeeds
+   * \param onFail    Callback that will be called when operation fails
+   */
+  void
+  fibRemoveNextHop(const Name& prefix, uint64_t faceId,
+                   const SuccessCallback& onSuccess,
+                   const FailCallback&    onFail);
+
 protected:
   void
   startFibCommand(const std::string& command,
@@ -53,15 +90,6 @@
 
 private:
   void
-  selfRegisterPrefixAddNextop(const FibManagementOptions& entry,
-                              const SuccessCallback& onSuccess,
-                              const FailCallback&    onFail);
-
-  void
-  recordSelfRegisteredFaceId(const FibManagementOptions& entry,
-                             const SuccessCallback& onSuccess);
-
-  void
   processFibCommandResponse(Data& data,
                             const FibCommandSucceedCallback& onSuccess,
                             const FailCallback& onFail);
@@ -73,8 +101,7 @@
 
 protected:
   Face& m_face;
-  KeyChain m_keyChain;
-  uint64_t m_faceId; // internal face ID (needed for prefix de-registration)
+  CommandInterestGenerator m_commandInterestGenerator;
 };
 
 } // namespace nfd