rib: change object ownership

rib::Service instead of RibManager now owns Controller,
Rib, FibUpdater, AutoPrefixPropagator, Readvertise objects.
RibManager is only responsible for serving RIB management
commands and datasets.

refs #4650

Change-Id: Idb99cdc6cb97b3e3c1bc56685b3ca121b062c7f5
diff --git a/rib/rib-manager.cpp b/rib/rib-manager.cpp
index 5b0e0c6..c45aede 100644
--- a/rib/rib-manager.cpp
+++ b/rib/rib-manager.cpp
@@ -24,9 +24,6 @@
  */
 
 #include "rib-manager.hpp"
-#include "readvertise/readvertise.hpp"
-#include "readvertise/client-to-nlsr-readvertise-policy.hpp"
-#include "readvertise/nfd-rib-readvertise-destination.hpp"
 
 #include "core/fib-max-depth.hpp"
 #include "core/logger.hpp"
@@ -49,21 +46,19 @@
 const std::string RibManager::MGMT_MODULE_NAME = "rib";
 const Name RibManager::FACES_LIST_DATASET_PREFIX = "/localhost/nfd/faces/list";
 const time::seconds RibManager::ACTIVE_FACE_FETCH_INTERVAL = time::seconds(300);
-const Name RibManager::READVERTISE_NLSR_PREFIX = "/localhost/nlsr";
 
-RibManager::RibManager(Dispatcher& dispatcher,
+RibManager::RibManager(Rib& rib,
+                       Dispatcher& dispatcher,
                        ndn::Face& face,
-                       ndn::KeyChain& keyChain)
+                       ndn::nfd::Controller& controller,
+                       AutoPrefixPropagator& propagator)
   : ManagerBase(dispatcher, MGMT_MODULE_NAME)
-  , m_face(face)
-  , m_keyChain(keyChain)
-  , m_nfdController(m_face, m_keyChain)
-  , m_faceMonitor(m_face)
-  , m_localhostValidator(m_face)
-  , m_localhopValidator(m_face)
-  , m_isLocalhopEnabled(false)
-  , m_prefixPropagator(m_nfdController, m_keyChain, m_rib)
-  , m_fibUpdater(m_rib, m_nfdController)
+  , m_rib(rib)
+  , m_nfdController(controller)
+  , m_faceMonitor(face)
+  , m_localhostValidator(face)
+  , m_localhopValidator(face)
+  , m_prefixPropagator(propagator)
   , m_addTopPrefix([&dispatcher] (const Name& topPrefix) {
       dispatcher.addTopPrefix(topPrefix, false);
     })
@@ -128,12 +123,10 @@
 }
 
 void
-RibManager::onConfig(const ConfigSection& configSection,
-                     bool isDryRun,
-                     const std::string& filename)
+RibManager::onConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename)
 {
-  bool isAutoPrefixPropagatorEnabled = false;
-  bool wantReadvertiseToNlsr = false;
+  wantAutoPrefixPropagator = false;
+  wantReadvertiseToNlsr = false;
 
   for (const auto& item : configSection) {
     if (item.first == "localhost_security") {
@@ -145,14 +138,7 @@
     }
     else if (item.first == "auto_prefix_propagate") {
       m_prefixPropagator.loadConfig(item.second);
-      isAutoPrefixPropagatorEnabled = true;
-
-      // Avoid other actions when isDryRun == true
-      if (isDryRun) {
-        continue;
-      }
-
-      m_prefixPropagator.enable();
+      wantAutoPrefixPropagator = true;
     }
     else if (item.first == "readvertise_nlsr") {
       wantReadvertiseToNlsr = ConfigFile::parseYesNo(item, "rib.readvertise_nlsr");
@@ -161,22 +147,6 @@
       BOOST_THROW_EXCEPTION(Error("Unrecognized rib property: " + item.first));
     }
   }
-
-  if (!isAutoPrefixPropagatorEnabled) {
-    m_prefixPropagator.disable();
-  }
-
-  if (wantReadvertiseToNlsr && m_readvertiseNlsr == nullptr) {
-    NFD_LOG_DEBUG("Enabling readvertise-to-nlsr");
-    m_readvertiseNlsr = make_unique<Readvertise>(
-      m_rib,
-      make_unique<ClientToNlsrReadvertisePolicy>(),
-      make_unique<NfdRibReadvertiseDestination>(m_nfdController, READVERTISE_NLSR_PREFIX, m_rib));
-  }
-  else if (!wantReadvertiseToNlsr && m_readvertiseNlsr != nullptr) {
-    NFD_LOG_DEBUG("Disabling readvertise-to-nlsr");
-    m_readvertiseNlsr.reset();
-  }
 }
 
 void
diff --git a/rib/rib-manager.hpp b/rib/rib-manager.hpp
index 6da3ef4..be13c44 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-2017,  Regents of the University of California,
+/*
+ * Copyright (c) 2014-2018,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -59,7 +59,8 @@
   };
 
 public:
-  RibManager(Dispatcher& dispatcher, ndn::Face& face, ndn::KeyChain& keyChain);
+  RibManager(Rib& rib, Dispatcher& dispatcher, ndn::Face& face,
+             ndn::nfd::Controller& controller, AutoPrefixPropagator& propagator);
 
   ~RibManager() override;
 
@@ -149,20 +150,18 @@
   void
   onEnableLocalFieldsError(const ControlResponse& response);
 
+public:
+  bool wantAutoPrefixPropagator = false;
+  bool wantReadvertiseToNlsr = false;
+
 private:
-  ndn::Face& m_face;
-  ndn::KeyChain& m_keyChain;
-  ndn::nfd::Controller m_nfdController;
+  Rib& m_rib;
+  ndn::nfd::Controller& m_nfdController;
   ndn::nfd::FaceMonitor m_faceMonitor;
   ndn::ValidatorConfig m_localhostValidator;
   ndn::ValidatorConfig m_localhopValidator;
-  bool m_isLocalhopEnabled;
-  AutoPrefixPropagator m_prefixPropagator;
-  unique_ptr<Readvertise> m_readvertiseNlsr;
-
-PUBLIC_WITH_TESTS_ELSE_PRIVATE:
-  Rib m_rib;
-  FibUpdater m_fibUpdater;
+  bool m_isLocalhopEnabled = false;
+  AutoPrefixPropagator& m_prefixPropagator;
 
 private:
   static const Name LOCAL_HOST_TOP_PREFIX;
@@ -171,7 +170,6 @@
   static const Name FACES_LIST_DATASET_PREFIX;
   static const time::seconds ACTIVE_FACE_FETCH_INTERVAL;
   scheduler::ScopedEventId m_activeFaceFetchEvent;
-  static const Name READVERTISE_NLSR_PREFIX;
 
   typedef std::set<uint64_t> FaceIdSet;
   /** \brief contains FaceIds with one or more Routes in the RIB
diff --git a/rib/service.cpp b/rib/service.cpp
index de103b4..739883c 100644
--- a/rib/service.cpp
+++ b/rib/service.cpp
@@ -24,18 +24,26 @@
  */
 
 #include "service.hpp"
+
+#include "auto-prefix-propagator.hpp"
+#include "fib-updater.hpp"
 #include "rib-manager.hpp"
+#include "readvertise/client-to-nlsr-readvertise-policy.hpp"
+#include "readvertise/nfd-rib-readvertise-destination.hpp"
+#include "readvertise/readvertise.hpp"
+
 #include "core/global-io.hpp"
 
 #include <boost/property_tree/info_parser.hpp>
 
-#include <ndn-cxx/transport/unix-transport.hpp>
 #include <ndn-cxx/transport/tcp-transport.hpp>
+#include <ndn-cxx/transport/unix-transport.hpp>
 
 namespace nfd {
 namespace rib {
 
 static const std::string INTERNAL_CONFIG = "internal://nfd.conf";
+static const Name READVERTISE_NLSR_PREFIX = "/localhost/nlsr";
 
 Service* Service::s_instance = nullptr;
 
@@ -52,8 +60,8 @@
   s_instance = this;
 }
 
-Service::Service(const ConfigSection& config, ndn::KeyChain& keyChain)
-  : m_configSection(config)
+Service::Service(const ConfigSection& configSection, ndn::KeyChain& keyChain)
+  : m_configSection(configSection)
   , m_keyChain(keyChain)
 {
   if (s_instance != nullptr) {
@@ -86,8 +94,11 @@
 Service::initialize()
 {
   m_face = make_unique<ndn::Face>(getLocalNfdTransport(), getGlobalIoService(), m_keyChain);
+  m_nfdController = make_unique<ndn::nfd::Controller>(*m_face, m_keyChain);
+  m_fibUpdater = make_unique<FibUpdater>(m_rib, *m_nfdController);
+  m_prefixPropagator = make_unique<AutoPrefixPropagator>(*m_nfdController, m_keyChain, m_rib);
   m_dispatcher = make_unique<ndn::mgmt::Dispatcher>(*m_face, m_keyChain);
-  m_ribManager = make_unique<RibManager>(*m_dispatcher, *m_face, m_keyChain);
+  m_ribManager = make_unique<RibManager>(m_rib, *m_dispatcher, *m_face, *m_nfdController, *m_prefixPropagator);
 
   ConfigFile config([] (const std::string& filename, const std::string& sectionName,
                         const ConfigSection& section, bool isDryRun) {
@@ -109,6 +120,23 @@
     config.parse(m_configSection, false, INTERNAL_CONFIG);
   }
 
+  if (m_ribManager->wantAutoPrefixPropagator) {
+    m_prefixPropagator->enable();
+  }
+  else {
+    m_prefixPropagator->disable();
+  }
+
+  if (m_ribManager->wantReadvertiseToNlsr && m_readvertiseNlsr == nullptr) {
+    m_readvertiseNlsr = make_unique<Readvertise>(
+      m_rib,
+      make_unique<ClientToNlsrReadvertisePolicy>(),
+      make_unique<NfdRibReadvertiseDestination>(*m_nfdController, READVERTISE_NLSR_PREFIX, m_rib));
+  }
+  else if (!m_ribManager->wantReadvertiseToNlsr && m_readvertiseNlsr != nullptr) {
+    m_readvertiseNlsr.reset();
+  }
+
   m_ribManager->registerWithNfd();
   m_ribManager->enableLocalFields();
 }
@@ -131,7 +159,7 @@
     // unix socket enabled
 
     auto socketPath = config.get<std::string>("face_system.unix.path", "/var/run/nfd.sock");
-    // default socketPath should be the same as in FaceManager::processSectionUnix
+    // default socketPath should be the same as in UnixStreamFactory::processConfig
 
     return make_shared<ndn::UnixTransport>(socketPath);
   }
@@ -140,7 +168,7 @@
     // tcp is enabled
 
     auto port = config.get<std::string>("face_system.tcp.port", "6363");
-    // default port should be the same as in FaceManager::processSectionTcp
+    // default port should be the same as in TcpFactory::processConfig
 
     return make_shared<ndn::TcpTransport>("localhost", port);
   }
diff --git a/rib/service.hpp b/rib/service.hpp
index 6c01f3e..a32d996 100644
--- a/rib/service.hpp
+++ b/rib/service.hpp
@@ -26,16 +26,21 @@
 #ifndef NFD_RIB_SERVICE_HPP
 #define NFD_RIB_SERVICE_HPP
 
+#include "rib.hpp"
 #include "core/config-file.hpp"
 
 #include <ndn-cxx/face.hpp>
 #include <ndn-cxx/mgmt/dispatcher.hpp>
+#include <ndn-cxx/mgmt/nfd/controller.hpp>
 #include <ndn-cxx/security/key-chain.hpp>
 #include <ndn-cxx/transport/transport.hpp>
 
 namespace nfd {
 namespace rib {
 
+class AutoPrefixPropagator;
+class FibUpdater;
+class Readvertise;
 class RibManager;
 
 /**
@@ -67,7 +72,7 @@
 
   /**
    * \brief create NFD-RIB service
-   * \param config parsed configuration section
+   * \param configSection parsed configuration section
    * \param keyChain the KeyChain
    * \note This constructor overload is more appropriate for integrated environments,
    *       such as NS-3 or android. Error messages related to configuration file
@@ -75,7 +80,7 @@
    * \throw std::logic_error Instance of rib::Service has been already constructed
    * \throw std::logic_error Instance of rib::Service is not constructed on RIB thread
    */
-  Service(const ConfigSection& config, ndn::KeyChain& keyChain);
+  Service(const ConfigSection& configSection, ndn::KeyChain& keyChain);
 
   /**
    * \brief Destructor
@@ -113,7 +118,13 @@
   ConfigSection m_configSection;
 
   ndn::KeyChain& m_keyChain;
+  Rib m_rib;
+
   unique_ptr<ndn::Face> m_face;
+  unique_ptr<ndn::nfd::Controller> m_nfdController;
+  unique_ptr<FibUpdater> m_fibUpdater;
+  unique_ptr<AutoPrefixPropagator> m_prefixPropagator;
+  unique_ptr<Readvertise> m_readvertiseNlsr;
   unique_ptr<ndn::mgmt::Dispatcher> m_dispatcher;
   unique_ptr<RibManager> m_ribManager;
 };
diff --git a/tests/rib/rib-manager.t.cpp b/tests/rib/rib-manager.t.cpp
index cdf1581..77b1ed3 100644
--- a/tests/rib/rib-manager.t.cpp
+++ b/tests/rib/rib-manager.t.cpp
@@ -53,8 +53,10 @@
                     bool shouldClearRib)
     : m_commands(m_face.sentInterests)
     , m_status(status)
-    , m_manager(m_dispatcher, m_face, m_keyChain)
-    , m_rib(m_manager.m_rib)
+    , m_nfdController(m_face, m_keyChain)
+    , m_fibUpdater(m_rib, m_nfdController)
+    , m_prefixPropagator(m_nfdController, m_keyChain, m_rib)
+    , m_manager(m_rib, m_dispatcher, m_face, m_nfdController, m_prefixPropagator)
   {
     m_rib.m_onSendBatchFromQueue = bind(&RibManagerFixture::onSendBatchFromQueue, this, _1);
 
@@ -168,9 +170,8 @@
     RibUpdate update = *(batch.begin());
 
     // Simulate a successful response from NFD
-    FibUpdater& updater = m_manager.m_fibUpdater;
-    m_manager.m_rib.onFibUpdateSuccess(batch, updater.m_inheritedRoutes,
-                                              bind(&RibManager::onRibUpdateSuccess, &m_manager, update));
+    m_rib.onFibUpdateSuccess(batch, m_fibUpdater.m_inheritedRoutes,
+                             bind(&RibManager::onRibUpdateSuccess, &m_manager, update));
   }
 
 public:
@@ -222,14 +223,16 @@
     return CheckCommandResult::OK;
   }
 
-public:
+protected:
   static const Name FIB_COMMAND_PREFIX;
   std::vector<Interest>& m_commands;
   ConfigurationStatus m_status;
 
-protected:
+  ndn::nfd::Controller m_nfdController;
+  Rib m_rib;
+  FibUpdater m_fibUpdater;
+  AutoPrefixPropagator m_prefixPropagator;
   RibManager m_manager;
-  Rib& m_rib;
 };
 
 const Name RibManagerFixture::FIB_COMMAND_PREFIX("/localhost/nfd/fib");