rib: refactor RibManager to use ManagementDispatcher

Change-Id: I3f689de7d043e85531f0b3f4accf65345cde5d9e
refs: #2857
diff --git a/rib/rib-manager.hpp b/rib/rib-manager.hpp
index d7c6819..0e5d68d 100644
--- a/rib/rib-manager.hpp
+++ b/rib/rib-manager.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014-2015,  Regents of the University of California,
+ * Copyright (c) 2014-2016,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -28,6 +28,7 @@
 
 #include "rib.hpp"
 #include "core/config-file.hpp"
+#include "core/manager-base.hpp"
 #include "rib-status-publisher.hpp"
 #include "auto-prefix-propagator.hpp"
 #include "fib-updater.hpp"
@@ -48,7 +49,7 @@
 
 using ndn::nfd::FaceEventNotification;
 
-class RibManager : noncopyable
+class RibManager : public nfd::ManagerBase
 {
 public:
   class Error : public std::runtime_error
@@ -61,7 +62,8 @@
     }
   };
 
-  RibManager(ndn::Face& face, ndn::KeyChain& keyChain);
+public:
+  RibManager(Dispatcher& dispatcher, ndn::Face& face, ndn::KeyChain& keyChain);
 
   ~RibManager();
 
@@ -80,107 +82,55 @@
   void
   onRibUpdateFailure(const RibUpdate& update, uint32_t code, const std::string& error);
 
-private:
+private: // initialization helpers
   void
-  onConfig(const ConfigSection& configSection,
-           bool isDryRun,
-           const std::string& filename);
+  onConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename);
 
   void
-  startListening(const Name& commandPrefix, const ndn::OnInterest& onRequest);
+  registerTopPrefix(const Name& topPrefix);
+
+private: // ControlCommand and StatusDataset
+  void
+  registerEntry(const Name& topPrefix, const Interest& interest,
+                ControlParameters parameters,
+                const ndn::mgmt::CommandContinuation& done);
 
   void
-  onLocalhopRequest(const Interest& request);
+  unregisterEntry(const Name& topPrefix, const Interest& interest,
+                  ControlParameters parameters,
+                  const ndn::mgmt::CommandContinuation& done);
 
   void
-  onLocalhostRequest(const Interest& request);
+  listEntries(const Name& topPrefix, const Interest& interest,
+              ndn::mgmt::StatusDatasetContext& context);
 
   void
-  sendResponse(const Name& name,
-               const ControlResponse& response);
+  setFaceForSelfRegistration(const Interest& request, ControlParameters& parameters);
 
-  void
-  sendResponse(const Name& name,
-               uint32_t code,
-               const std::string& text);
+private: // command validation
+  /**
+   * @brief validate a request for ControlCommand.
+   *
+   * This is called by the dispatcher.
+   *
+   * @pre params != null
+   * @pre typeid(*params) == typeid(ndn::nfd::ControlParameters)
+   *
+   * @param prefix the top prefix
+   * @param interest a request for ControlCommand
+   * @param params the parameters for ControlCommand
+   * @param accept callback of successful validation, take the requester string as a argument
+   * @param reject callback of failure in validation, take the action code as a argument
+   *
+   * use m_localhostValidator / m_localhopValidator to validate commands according to @p prefix.
+   */
+  virtual void
+  authorize(const Name& prefix, const Interest& interest,
+            const ndn::mgmt::ControlParameters* params,
+            ndn::mgmt::AcceptContinuation accept,
+            ndn::mgmt::RejectContinuation reject) override;
 
-  void
-  sendSuccessResponse(const shared_ptr<const Interest>& request,
-                      const ControlParameters& parameters);
-
-  void
-  sendErrorResponse(uint32_t code, const std::string& error,
-                    const shared_ptr<const Interest>& request);
-
-  void
-  registerEntry(const shared_ptr<const Interest>& request,
-                ControlParameters& parameters);
-
-  void
-  unregisterEntry(const shared_ptr<const Interest>& request,
-                  ControlParameters& parameters);
-
-private:
-  void
-  onCommandValidated(const shared_ptr<const Interest>& request);
-
-  void
-  onCommandValidationFailed(const shared_ptr<const Interest>& request,
-                            const std::string& failureInfo);
-
-
-  void
-  onCommandError(uint32_t code, const std::string& error,
-                 const shared_ptr<const Interest>& request,
-                 const Route& route);
-
-  void
-  onRegSuccess(const shared_ptr<const Interest>& request,
-               const ControlParameters& parameters,
-               const Route& route);
-
-  void
-  onUnRegSuccess(const shared_ptr<const Interest>& request,
-                 const ControlParameters& parameters,
-                 const Route& route);
-
-PUBLIC_WITH_TESTS_ELSE_PRIVATE:
-  void
-  onNrdCommandPrefixAddNextHopSuccess(const Name& prefix,
-                                      const ndn::nfd::ControlParameters& result);
-
-private:
-  void
-  onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg);
-
-  void
-  onControlHeaderSuccess();
-
-  void
-  onControlHeaderError(uint32_t code, const std::string& reason);
-
-  static bool
-  extractParameters(const Name::Component& parameterComponent,
-                    ControlParameters& extractedParameters);
-
-  bool
-  validateParameters(const ControlCommand& command,
-                     ControlParameters& parameters);
-
-  void
-  onNotification(const FaceEventNotification& notification);
-
-PUBLIC_WITH_TESTS_ELSE_PRIVATE:
-  void
-  onFaceDestroyedEvent(uint64_t faceId);
-
-private:
-  void
-  listEntries(const Interest& request);
-
-  void
-  scheduleActiveFaceFetch(const time::seconds& timeToWait);
-
+private: // Face monitor
   void
   fetchActiveFaces();
 
@@ -190,70 +140,61 @@
   void
   onFetchFaceStatusTimeout();
 
+  void
+  onFaceDestroyedEvent(uint64_t faceId);
+
 PUBLIC_WITH_TESTS_ELSE_PRIVATE:
-  /** \param buffer Face dataset contents
+  void
+  scheduleActiveFaceFetch(const time::seconds& timeToWait);
+
+  /**
+   * @brief remove invalid faces
+   *
+   * @param buffer Face dataset contents
   */
   void
   removeInvalidFaces(shared_ptr<ndn::OBufferStream> buffer);
 
-PUBLIC_WITH_TESTS_ELSE_PRIVATE:
-  Rib m_managedRib;
+  /**
+   * @brief response to face events
+   *
+   * @param notification
+   */
+  void
+  onNotification(const FaceEventNotification& notification);
+
+private:
+  void
+  onNrdCommandPrefixAddNextHopSuccess(const Name& prefix,
+                                      const ndn::nfd::ControlParameters& result);
+
+  void
+  onNrdCommandPrefixAddNextHopError(const Name& name, const std::string& msg);
+
+  void
+  onControlHeaderSuccess();
+
+  void
+  onControlHeaderError(uint32_t code, const std::string& reason);
 
 private:
   ndn::Face& m_face;
   ndn::KeyChain& m_keyChain;
   ndn::nfd::Controller m_nfdController;
+  ndn::nfd::FaceMonitor m_faceMonitor;
   ndn::ValidatorConfig m_localhostValidator;
   ndn::ValidatorConfig m_localhopValidator;
-  ndn::nfd::FaceMonitor m_faceMonitor;
   bool m_isLocalhopEnabled;
   AutoPrefixPropagator m_prefixPropagator;
 
-  RibStatusPublisher m_ribStatusPublisher;
-
 PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+  Rib m_rib;
   FibUpdater m_fibUpdater;
 
 private:
-  typedef function<void(RibManager*,
-                        const shared_ptr<const Interest>& request,
-                        ControlParameters& parameters)> SignedVerbProcessor;
-
-  typedef std::map<name::Component, SignedVerbProcessor> SignedVerbDispatchTable;
-
-  typedef std::pair<name::Component, SignedVerbProcessor> SignedVerbAndProcessor;
-
-
-  const SignedVerbDispatchTable m_signedVerbDispatch;
-
-  static const Name COMMAND_PREFIX; // /localhost/nrd
-
-PUBLIC_WITH_TESTS_ELSE_PRIVATE:
-  static const Name REMOTE_COMMAND_PREFIX; // /localhop/nrd
-
-private:
-  // number of components in an invalid, but not malformed, unsigned command.
-  // (/localhost/nrd + verb + options) = 4
-  static const size_t COMMAND_UNSIGNED_NCOMPS;
-
-  // number of components in a valid signed Interest.
-  // 8 with signed Interest support.
-  static const size_t COMMAND_SIGNED_NCOMPS;
-
-  static const SignedVerbAndProcessor SIGNED_COMMAND_VERBS[];
-
-  typedef function<void(RibManager*, const Interest&)> UnsignedVerbProcessor;
-  typedef std::map<Name::Component, UnsignedVerbProcessor> UnsignedVerbDispatchTable;
-  typedef std::pair<Name::Component, UnsignedVerbProcessor> UnsignedVerbAndProcessor;
-
-  const UnsignedVerbDispatchTable m_unsignedVerbDispatch;
-  static const UnsignedVerbAndProcessor UNSIGNED_COMMAND_VERBS[];
-
-  static const Name LIST_COMMAND_PREFIX;
-  static const size_t LIST_COMMAND_NCOMPS;
-
+  static const Name LOCAL_HOST_TOP_PREFIX;
+  static const Name LOCAL_HOP_TOP_PREFIX;
   static const Name FACES_LIST_DATASET_PREFIX;
-
   static const time::seconds ACTIVE_FACE_FETCH_INTERVAL;
   scheduler::EventId m_activeFaceFetchEvent;
 
@@ -261,6 +202,8 @@
   /** \brief contains FaceIds with one or more Routes in the RIB
   */
   FaceIdSet m_registeredFaces;
+
+  std::function<void(const Name& topPrefix)> m_addTopPrefix;
 };
 
 } // namespace rib