rib: reimplement auto prefix propagation based on readvertise

Change-Id: I6c4d81b829cf78084d26121c81d977d0a86c9a0c
Refs: #3819
diff --git a/rib/readvertise/host-to-gateway-readvertise-policy.cpp b/rib/readvertise/host-to-gateway-readvertise-policy.cpp
new file mode 100644
index 0000000..2d508f8
--- /dev/null
+++ b/rib/readvertise/host-to-gateway-readvertise-policy.cpp
@@ -0,0 +1,91 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2018,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "host-to-gateway-readvertise-policy.hpp"
+#include "core/scope-prefix.hpp"
+#include "rib/rib-manager.hpp"
+
+#include <ndn-cxx/security/pib/identity.hpp>
+#include <ndn-cxx/security/signing-helpers.hpp>
+
+namespace nfd {
+namespace rib {
+
+static const name::Component IGNORE_COMPONENT("nrd");
+static const time::seconds DEFAULT_REFRESH_INTERVAL = 25_s;
+
+HostToGatewayReadvertisePolicy::HostToGatewayReadvertisePolicy(const ndn::KeyChain& keyChain,
+                                                               const ConfigSection& section)
+  : m_keyChain(keyChain)
+{
+  auto interval = section.get_optional<uint64_t>("refresh_interval");
+  m_refreshInterval = interval ? time::seconds(*interval) : DEFAULT_REFRESH_INTERVAL;
+}
+
+optional<ReadvertiseAction>
+HostToGatewayReadvertisePolicy::handleNewRoute(const RibRouteRef& ribRoute) const
+{
+  auto ribEntryName = ribRoute.entry->getName();
+  if (scope_prefix::LOCALHOST.isPrefixOf(ribEntryName) ||
+      ribEntryName == RibManager::LOCALHOP_TOP_PREFIX) {
+    return nullopt;
+  }
+
+  // find out the shortest identity whose name is a prefix of the RIB entry name
+  auto prefixToAdvertise = ribEntryName;
+  ndn::security::pib::Identity signingIdentity;
+  bool isFound = false;
+
+  for (const auto& identity : m_keyChain.getPib().getIdentities()) {
+    auto prefix = identity.getName();
+
+    // ignore the identity name's last component if it is "nrd"
+    if (!prefix.empty() && IGNORE_COMPONENT == prefix.at(-1)) {
+      prefix = prefix.getPrefix(-1);
+    }
+
+    if (prefix.isPrefixOf(prefixToAdvertise)) {
+      isFound = true;
+      prefixToAdvertise = prefix;
+      signingIdentity = identity;
+    }
+  }
+
+  if (isFound) {
+    return ReadvertiseAction{prefixToAdvertise, ndn::security::signingByIdentity(signingIdentity)};
+  }
+  else {
+    return nullopt;
+  }
+}
+
+time::milliseconds
+HostToGatewayReadvertisePolicy::getRefreshInterval() const
+{
+  return m_refreshInterval;
+}
+
+} // namespace rib
+} // namespace nfd
diff --git a/rib/readvertise/host-to-gateway-readvertise-policy.hpp b/rib/readvertise/host-to-gateway-readvertise-policy.hpp
new file mode 100644
index 0000000..a2961b3
--- /dev/null
+++ b/rib/readvertise/host-to-gateway-readvertise-policy.hpp
@@ -0,0 +1,60 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2018,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NFD_RIB_READVERTISE_HOST_TO_GATEWAY_READVERTISE_POLICY_HPP
+#define NFD_RIB_READVERTISE_HOST_TO_GATEWAY_READVERTISE_POLICY_HPP
+
+#include "readvertise-policy.hpp"
+#include "core/config-file.hpp"
+
+#include <ndn-cxx/security/key-chain.hpp>
+
+namespace nfd {
+namespace rib {
+
+/** \brief a policy to readvertise routes registered by local applications into remote gateway
+ */
+class HostToGatewayReadvertisePolicy : public ReadvertisePolicy
+{
+public:
+  HostToGatewayReadvertisePolicy(const ndn::KeyChain& keyChain,
+                                 const ConfigSection& section);
+
+public:
+  optional<ReadvertiseAction>
+  handleNewRoute(const RibRouteRef& ribRoute) const override;
+
+  time::milliseconds
+  getRefreshInterval() const override;
+
+private:
+  const ndn::KeyChain& m_keyChain;
+  time::seconds m_refreshInterval;
+};
+
+} // namespace rib
+} // namespace nfd
+
+#endif // NFD_RIB_READVERTISE_HOST_TO_GATEWAY_READVERTISE_POLICY_HPP
diff --git a/rib/readvertise/nfd-rib-readvertise-destination.cpp b/rib/readvertise/nfd-rib-readvertise-destination.cpp
index 530a9d4..f0a0482 100644
--- a/rib/readvertise/nfd-rib-readvertise-destination.cpp
+++ b/rib/readvertise/nfd-rib-readvertise-destination.cpp
@@ -26,9 +26,7 @@
 #include "nfd-rib-readvertise-destination.hpp"
 #include "core/logger.hpp"
 
-#include <ndn-cxx/mgmt/nfd/command-options.hpp>
 #include <ndn-cxx/mgmt/nfd/control-command.hpp>
-#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
 #include <ndn-cxx/mgmt/nfd/control-response.hpp>
 
 namespace nfd {
@@ -36,15 +34,15 @@
 
 NFD_LOG_INIT(NfdRibReadvertiseDestination);
 
-using ndn::nfd::CommandOptions;
-using ndn::nfd::ControlParameters;
 using ndn::nfd::ControlResponse;
 
 NfdRibReadvertiseDestination::NfdRibReadvertiseDestination(ndn::nfd::Controller& controller,
-                                                           const Name& commandPrefix,
-                                                           Rib& rib)
+                                                           Rib& rib,
+                                                           const ndn::nfd::CommandOptions& options,
+                                                           const ndn::nfd::ControlParameters& parameters)
   : m_controller(controller)
-  , m_commandPrefix(commandPrefix)
+  , m_commandOptions(options)
+  , m_controlParameters(parameters)
 {
   m_ribInsertConn = rib.afterInsertEntry.connect(
     std::bind(&NfdRibReadvertiseDestination::handleRibInsert, this, _1));
@@ -57,13 +55,13 @@
                                         std::function<void()> successCb,
                                         std::function<void(const std::string&)> failureCb)
 {
-  NFD_LOG_DEBUG("advertise " << rr.prefix << " on " << m_commandPrefix);
+  NFD_LOG_DEBUG("advertise " << rr.prefix << " on " << m_commandOptions.getPrefix());
 
   m_controller.start<ndn::nfd::RibRegisterCommand>(
-    ControlParameters().setName(rr.prefix).setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT),
+    getControlParameters().setName(rr.prefix),
     [=] (const ControlParameters& cp) { successCb(); },
     [=] (const ControlResponse& cr) { failureCb(cr.getText()); },
-    CommandOptions().setPrefix(m_commandPrefix).setSigningInfo(rr.signer));
+    getCommandOptions().setSigningInfo(rr.signer));
 }
 
 void
@@ -71,19 +69,31 @@
                                        std::function<void()> successCb,
                                        std::function<void(const std::string&)> failureCb)
 {
-  NFD_LOG_DEBUG("withdraw " << rr.prefix << " on " << m_commandPrefix);
+  NFD_LOG_DEBUG("withdraw " << rr.prefix << " on " << m_commandOptions.getPrefix());
 
   m_controller.start<ndn::nfd::RibUnregisterCommand>(
-    ControlParameters().setName(rr.prefix).setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT),
+    getControlParameters().setName(rr.prefix),
     [=] (const ControlParameters& cp) { successCb(); },
     [=] (const ControlResponse& cr) { failureCb(cr.getText()); },
-    CommandOptions().setPrefix(m_commandPrefix).setSigningInfo(rr.signer));
+    getCommandOptions().setSigningInfo(rr.signer));
+}
+
+ndn::nfd::ControlParameters
+NfdRibReadvertiseDestination::getControlParameters()
+{
+  return m_controlParameters;
+}
+
+ndn::nfd::CommandOptions
+NfdRibReadvertiseDestination::getCommandOptions()
+{
+  return m_commandOptions;
 }
 
 void
 NfdRibReadvertiseDestination::handleRibInsert(const ndn::Name& name)
 {
-  if (name.isPrefixOf(m_commandPrefix)) {
+  if (name.isPrefixOf(m_commandOptions.getPrefix())) {
     setAvailability(true);
   }
 }
@@ -91,7 +101,7 @@
 void
 NfdRibReadvertiseDestination::handleRibErase(const ndn::Name& name)
 {
-  if (name.isPrefixOf(m_commandPrefix)) {
+  if (name.isPrefixOf(m_commandOptions.getPrefix())) {
     setAvailability(false);
   }
 }
diff --git a/rib/readvertise/nfd-rib-readvertise-destination.hpp b/rib/readvertise/nfd-rib-readvertise-destination.hpp
index ec71d98..d31e81b 100644
--- a/rib/readvertise/nfd-rib-readvertise-destination.hpp
+++ b/rib/readvertise/nfd-rib-readvertise-destination.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,
@@ -29,7 +29,9 @@
 #include "readvertise-destination.hpp"
 #include "../rib.hpp"
 
+#include <ndn-cxx/mgmt/nfd/command-options.hpp>
 #include <ndn-cxx/mgmt/nfd/controller.hpp>
+#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
 
 namespace nfd {
 namespace rib {
@@ -40,8 +42,11 @@
 {
 public:
   NfdRibReadvertiseDestination(ndn::nfd::Controller& controller,
-                               const Name& commandPrefix,
-                               Rib& rib);
+                               Rib& rib,
+                               const ndn::nfd::CommandOptions& options = ndn::nfd::CommandOptions(),
+                               const ndn::nfd::ControlParameters& parameters =
+                                 ndn::nfd::ControlParameters()
+                                   .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT));
 
   /** \brief add a name prefix into NFD RIB
    */
@@ -57,6 +62,13 @@
            std::function<void()> successCb,
            std::function<void(const std::string&)> failureCb) override;
 
+protected:
+  ndn::nfd::ControlParameters
+  getControlParameters();
+
+  ndn::nfd::CommandOptions
+  getCommandOptions();
+
 private:
   void
   handleRibInsert(const Name& name);
@@ -66,10 +78,12 @@
 
 private:
   ndn::nfd::Controller& m_controller;
-  Name m_commandPrefix;
 
   signal::ScopedConnection m_ribInsertConn;
   signal::ScopedConnection m_ribEraseConn;
+
+  ndn::nfd::CommandOptions m_commandOptions;
+  ndn::nfd::ControlParameters m_controlParameters;
 };
 
 } // namespace rib