management: adding all high-level methods in nrd::Controller

Change-Id: Ib8d50dae5168f6f3ed3d30b22787cb3574f87e87
diff --git a/src/management/nfd-controller.cpp b/src/management/nfd-controller.cpp
index 723c984..7a99693 100644
--- a/src/management/nfd-controller.cpp
+++ b/src/management/nfd-controller.cpp
@@ -25,7 +25,7 @@
                                const SuccessCallback& onSuccess,
                                const FailCallback&    onFail)
 {
-  fibAddNextHop(prefixToRegister, 0, 0, onSuccess, onFail);
+  fibAddNextHop(prefixToRegister, 0, 0, bind(onSuccess), onFail);
 }
 
 void
@@ -33,32 +33,32 @@
                                  const SuccessCallback& onSuccess,
                                  const FailCallback&    onFail)
 {
-  fibRemoveNextHop(prefixToDeRegister, 0, onSuccess, onFail);
+  fibRemoveNextHop(prefixToDeRegister, 0, bind(onSuccess), onFail);
 }
 
 void
 Controller::fibAddNextHop(const Name& prefix, uint64_t faceId, int cost,
-                          const SuccessCallback& onSuccess,
-                          const FailCallback&    onFail)
+                          const FibCommandSucceedCallback& onSuccess,
+                          const FailCallback& onFail)
 {
   startFibCommand("add-nexthop",
                   FibManagementOptions()
                     .setName(prefix)
                     .setFaceId(faceId)
                     .setCost(cost),
-                  bind(onSuccess), onFail);
+                  onSuccess, onFail);
 }
 
 void
 Controller::fibRemoveNextHop(const Name& prefix, uint64_t faceId,
-                             const SuccessCallback& onSuccess,
-                             const FailCallback&    onFail)
+                             const FibCommandSucceedCallback& onSuccess,
+                             const FailCallback& onFail)
 {
   startFibCommand("remove-nexthop",
                   FibManagementOptions()
                     .setName(prefix)
                     .setFaceId(faceId),
-                  bind(onSuccess), onFail);
+                  onSuccess, onFail);
 }
 
 void
@@ -86,6 +86,8 @@
                                       const FibCommandSucceedCallback& onSuccess,
                                       const FailCallback& onFail)
 {
+  /// \todo Add validation of incoming Data
+
   try
     {
       ControlResponse response(data.getContent().blockFromValue());
@@ -127,6 +129,8 @@
                                        const FaceCommandSucceedCallback& onSuccess,
                                        const FailCallback& onFail)
 {
+  /// \todo Add validation of incoming Data
+
   try
     {
       ControlResponse response(data.getContent().blockFromValue());
diff --git a/src/management/nfd-controller.hpp b/src/management/nfd-controller.hpp
index aa85656..a864db3 100644
--- a/src/management/nfd-controller.hpp
+++ b/src/management/nfd-controller.hpp
@@ -54,8 +54,8 @@
    */
   void
   fibAddNextHop(const Name& prefix, uint64_t faceId, int cost,
-                const SuccessCallback& onSuccess,
-                const FailCallback&    onFail);
+                const FibCommandSucceedCallback& onSuccess,
+                const FailCallback& onFail);
 
   /**
    * \brief Remove a nexthop from FIB entry
@@ -72,8 +72,8 @@
    */
   void
   fibRemoveNextHop(const Name& prefix, uint64_t faceId,
-                   const SuccessCallback& onSuccess,
-                   const FailCallback&    onFail);
+                   const FibCommandSucceedCallback& onSuccess,
+                   const FailCallback& onFail);
 
 protected:
   void
diff --git a/src/management/nrd-controller.cpp b/src/management/nrd-controller.cpp
index 2d5557f..1d0e7a1 100644
--- a/src/management/nrd-controller.cpp
+++ b/src/management/nrd-controller.cpp
@@ -19,7 +19,6 @@
 
 Controller::Controller(Face& face)
   : m_face(face)
-  , m_faceId(0)
 {
 }
 
@@ -33,8 +32,7 @@
                  .setName(prefixToRegister)
                  .setFaceId(0) // self-registration
                  .setCost(0),
-               bind(&Controller::recordSelfRegisteredFaceId, this, _1, onSuccess),
-               onFail);
+               bind(onSuccess), onFail);
 }
 
 void
@@ -42,21 +40,46 @@
                                  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;
-    }
-
   startCommand("unregister",
                PrefixRegOptions()
                  .setName(prefixToRegister)
-                 .setFaceId(m_faceId),
+                 .setFaceId(0), // self-registration
                bind(onSuccess), onFail);
 }
 
 void
+Controller::registerPrefix(const PrefixRegOptions& options,
+                           const CommandSucceedCallback& onSuccess,
+                           const FailCallback& onFail)
+{
+  startCommand("register", options, onSuccess, onFail);
+}
+
+void
+Controller::unregisterPrefix(const PrefixRegOptions& options,
+                             const CommandSucceedCallback& onSuccess,
+                             const FailCallback& onFail)
+{
+  startCommand("unregister", options, onSuccess, onFail);
+}
+
+void
+Controller::advertisePrefix(const PrefixRegOptions& options,
+                            const CommandSucceedCallback& onSuccess,
+                            const FailCallback& onFail)
+{
+  startCommand("advertise", options, onSuccess, onFail);
+}
+
+void
+Controller::withdrawPrefix(const PrefixRegOptions& options,
+                            const CommandSucceedCallback& onSuccess,
+                            const FailCallback& onFail)
+{
+  startCommand("withdraw", options, onSuccess, onFail);
+}
+
+void
 Controller::startCommand(const std::string& command,
                          const PrefixRegOptions& options,
                          const CommandSucceedCallback& onSuccess,
@@ -68,7 +91,7 @@
     .append(options.wireEncode());
 
   Interest commandInterest(commandInterestName);
-  // m_keyChain.sign(commandInterest);
+  m_commandInterestGenerator.generate(commandInterest);
 
   m_face.expressInterest(commandInterest,
                          bind(&Controller::processCommandResponse, this, _2,
@@ -77,18 +100,12 @@
 }
 
 void
-Controller::recordSelfRegisteredFaceId(const PrefixRegOptions& entry,
-                                       const SuccessCallback& onSuccess)
-{
-  m_faceId = entry.getFaceId();
-  onSuccess();
-}
-
-void
 Controller::processCommandResponse(Data& data,
                                    const CommandSucceedCallback& onSuccess,
                                    const FailCallback& onFail)
 {
+  /// \todo Add validation of incoming Data
+
   try
     {
       nfd::ControlResponse response(data.getContent().blockFromValue());
diff --git a/src/management/nrd-controller.hpp b/src/management/nrd-controller.hpp
index c5bd7bf..6de9f5b 100644
--- a/src/management/nrd-controller.hpp
+++ b/src/management/nrd-controller.hpp
@@ -8,7 +8,7 @@
 #define NDN_MANAGEMENT_NFD_CONTROLLER_HPP
 
 #include "controller.hpp"
-#include "../security/key-chain.hpp"
+#include "../util/command-interest-generator.hpp"
 
 namespace ndn {
 namespace nrd {
@@ -35,6 +35,26 @@
                        const SuccessCallback& onSuccess,
                        const FailCallback&    onFail);
 
+  void
+  registerPrefix(const PrefixRegOptions& options,
+                 const CommandSucceedCallback& onSuccess,
+                 const FailCallback& onFail);
+
+  void
+  unregisterPrefix(const PrefixRegOptions& options,
+                 const CommandSucceedCallback& onSuccess,
+                 const FailCallback&    onFail);
+
+  void
+  advertisePrefix(const PrefixRegOptions& options,
+                  const CommandSucceedCallback& onSuccess,
+                  const FailCallback& onFail);
+
+  void
+  withdrawPrefix(const PrefixRegOptions& options,
+                 const CommandSucceedCallback& onSuccess,
+                 const FailCallback& onFail);
+
 protected:
   void
   startCommand(const std::string& command,
@@ -44,18 +64,13 @@
 
 private:
   void
-  recordSelfRegisteredFaceId(const PrefixRegOptions& entry,
-                             const SuccessCallback& onSuccess);
-
-  void
   processCommandResponse(Data& data,
                          const CommandSucceedCallback& onSuccess,
                          const FailCallback& onFail);
 
 protected:
   Face& m_face;
-  KeyChain m_keyChain;
-  uint64_t m_faceId; // internal face ID (needed for prefix de-registration)
+  CommandInterestGenerator m_commandInterestGenerator;
 };
 
 } // namespace nrd
diff --git a/src/management/nrd-prefix-reg-options.hpp b/src/management/nrd-prefix-reg-options.hpp
index 12136e8..412194b 100644
--- a/src/management/nrd-prefix-reg-options.hpp
+++ b/src/management/nrd-prefix-reg-options.hpp
@@ -44,21 +44,21 @@
   template<bool T>
   size_t
   wireEncode(EncodingImpl<T>& block) const;
-  
+
   const Block&
   wireEncode() const;
-  
-  void 
+
+  void
   wireDecode(const Block& wire);
-  
+
   ////////////////////////////////////////////////////////
-  
-  const Name& 
+
+  const Name&
   getName() const
   {
     return m_name;
   }
-  
+
   PrefixRegOptions&
   setName(const Name& name)
   {
@@ -68,7 +68,7 @@
   }
 
   //
-  
+
   uint64_t
   getFaceId() const
   {
@@ -84,8 +84,8 @@
   }
 
   //
-  
-  uint64_t 
+
+  uint64_t
   getFlags() const
   {
     return m_flags;
@@ -100,8 +100,8 @@
   }
 
   //
-  
-  uint64_t 
+
+  uint64_t
   getCost() const
   {
     return m_cost;
@@ -116,7 +116,7 @@
   }
 
   //
-  
+
   Milliseconds
   getExpirationPeriod() const
   {
@@ -132,24 +132,8 @@
   }
 
   //
-  
-  const Name& 
-  getStrategy() const
-  {
-    return m_strategy;
-  }
 
-  PrefixRegOptions&
-  setStrategy(const Name& strategy)
-  {
-    m_strategy = strategy;
-    m_wire.reset();
-    return *this;
-  }
-
-  //
-
-  const std::string& 
+  const std::string&
   getProtocol() const
   {
     return m_protocol;
@@ -162,16 +146,15 @@
     m_wire.reset();
     return *this;
   }
-  
+
 private:
   Name m_name;
   uint64_t m_faceId;
   uint64_t m_flags;
   uint64_t m_cost;
   Milliseconds m_expirationPeriod;
-  Name m_strategy;
   std::string m_protocol;
-  
+
   mutable Block m_wire;
 };
 
@@ -185,9 +168,8 @@
   //                        Name
   //                        FaceId?
   //                        Flags?
-  //                        Cost?    
+  //                        Cost?
   //                        ExpirationPeriod?
-  //                        StrategyName?
   //                        Protocol?
 
   // (reverse encoding)
@@ -201,12 +183,6 @@
                                          m_protocol.size());
     }
 
-  // StrategyName
-  if (!m_strategy.empty())
-    {
-      total_len += prependNestedBlock(block, tlv::nrd::StrategyName, m_strategy);
-    }
-
   // ExpirationPeriod
   if (m_expirationPeriod > 0)
     {
@@ -248,24 +224,23 @@
 
   EncodingEstimator estimator;
   size_t estimatedSize = wireEncode(estimator);
-  
+
   EncodingBuffer buffer(estimatedSize, 0);
   wireEncode(buffer);
 
   m_wire = buffer.block();
   return m_wire;
 }
-  
-inline void 
+
+inline void
 PrefixRegOptions::wireDecode(const Block& wire)
 {
   // PrefixRegOptions ::= PREFIX-REG-OPTIONS-TYPE TLV-LENGTH
   //                        Name
   //                        FaceId?
   //                        Flags?
-  //                        Cost?    
+  //                        Cost?
   //                        ExpirationPeriod?
-  //                        StrategyName?
   //                        Protocol?
 
   m_name.clear();
@@ -273,14 +248,13 @@
   m_flags = DEFAULT_FLAGS;
   m_cost = DEFAULT_COST;
   m_expirationPeriod = -1;
-  m_strategy.clear();
   m_protocol.clear();
 
   m_wire = wire;
 
   if (m_wire.type() != tlv::nrd::PrefixRegOptions)
     throw Error("Requested decoding of PrefixRegOptions, but Block is of different type");
-  
+
   m_wire.parse ();
 
   // Name
@@ -296,7 +270,7 @@
     {
       m_faceId = readNonNegativeInteger(*val);
     }
-  
+
   // Flags
   val = m_wire.find(tlv::nrd::Flags);
   if (val != m_wire.elements_end())
@@ -318,15 +292,6 @@
       m_expirationPeriod = readNonNegativeInteger(*val);
     }
 
-  // StrategyName
-  val = m_wire.find(tlv::nrd::StrategyName);
-  if (val != m_wire.elements_end())
-    {
-      val->parse();
-      if (!val->elements().empty())
-        m_strategy.wireDecode(*val->elements_begin());
-    }
-
   // Protocol
   val = m_wire.find(tlv::nrd::Protocol);
   if (val != m_wire.elements_end())
@@ -336,26 +301,32 @@
     }
 }
 
-// inline std::ostream&
-// operator << (std::ostream &os, const PrefixRegOptions &option)
-// {
-//   os << "ForwardingEntry(";
-  
-//   // Name
-//   os << "Prefix: " << option.getName() << ", ";
+inline std::ostream&
+operator << (std::ostream &os, const PrefixRegOptions &option)
+{
+  os << "PrefixRegOptions(";
 
-//   // FaceID
-//   os << "FaceID: " << option.getFaceId() << ", ";
+  // Name
+  os << "Prefix: " << option.getName() << ", ";
 
-//   // Cost
-//   os << "Cost: " << option.getCost() << ", ";
+  // FaceID
+  os << "FaceID: " << option.getFaceId() << ", ";
 
-//   // Strategy
-//   os << "Strategy: " << option.getStrategy() << ", ";
-  
-//   os << ")";
-//   return os;
-// }
+  // Flags
+  os << "Flags: " << option.getFlags() << ", ";
+
+  // Cost
+  os << "Cost: " << option.getCost() << ", ";
+
+  // ExpirationPeriod
+  os << "ExpirationPeriod: " << option.getExpirationPeriod() << ", ";
+
+  // Protocol
+  os << "Protocol: " << option.getProtocol();
+
+  os << ")";
+  return os;
+}
 
 } // namespace nrd
 } // namespace ndn
diff --git a/tests/management/test-nrd.cpp b/tests/management/test-nrd.cpp
index e45eb73..de95ee2 100644
--- a/tests/management/test-nrd.cpp
+++ b/tests/management/test-nrd.cpp
@@ -32,9 +32,14 @@
 
   const Block& blk = opt.wireEncode ();
 
-  BOOST_CHECK_EQUAL_COLLECTIONS (RealPrefixRegOptions,
-                                   RealPrefixRegOptions + sizeof (RealPrefixRegOptions),
-                                   blk.begin (), blk.end ());
+  BOOST_CHECK_EQUAL_COLLECTIONS(RealPrefixRegOptions,
+                                RealPrefixRegOptions + sizeof (RealPrefixRegOptions),
+                                blk.begin (), blk.end ());
+
+  std::ostringstream os;
+  os << opt;
+  BOOST_CHECK_EQUAL(os.str(), "PrefixRegOptions(Prefix: /localhost/reg/test, "
+                    "FaceID: 0, Flags: 1, Cost: 0, ExpirationPeriod: -1, Protocol: )");
 }
 
 BOOST_AUTO_TEST_CASE (PrefixRegOptionsDecoding)
diff --git a/tests/test-faces.cpp b/tests/test-faces.cpp
index 1fab695..0205cb1 100644
--- a/tests/test-faces.cpp
+++ b/tests/test-faces.cpp
@@ -160,7 +160,7 @@
   Face face;
   Face face2(face.ioService());
   Scheduler scheduler(*face.ioService());
-  scheduler.scheduleEvent(time::seconds(0.3),
+  scheduler.scheduleEvent(time::seconds(1),
                           bind(&FacesFixture::terminate, this, func_lib::ref(face)));
   
   regPrefixId = face.setInterestFilter("/Hello/World",