fw: UnsolicitedDataPolicy

The decision of whether to cache or drop an unsolicited Data is moved from
Data unsolicited pipeline to UnsolicitedDataPolicy.

refs #2181

Change-Id: Iba3e72c2214e0ac5f6f305c2c1c0ed182b008e85
diff --git a/daemon/fw/forwarder.cpp b/daemon/fw/forwarder.cpp
index 4fc9c38..ae4369e 100644
--- a/daemon/fw/forwarder.cpp
+++ b/daemon/fw/forwarder.cpp
@@ -35,10 +35,9 @@
 
 NFD_LOG_INIT("Forwarder");
 
-using fw::Strategy;
-
 Forwarder::Forwarder()
-  : m_fib(m_nameTree)
+  : m_unsolicitedDataPolicy(new fw::AdmitLocalUnsolicitedDataPolicy())
+  , m_fib(m_nameTree)
   , m_pit(m_nameTree)
   , m_measurements(m_nameTree)
   , m_strategyChoice(m_nameTree, fw::makeDefaultStrategy(*this))
@@ -389,15 +388,15 @@
 Forwarder::onDataUnsolicited(Face& inFace, const Data& data)
 {
   // accept to cache?
-  bool acceptToCache = inFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL;
-  if (acceptToCache) {
+  fw::UnsolicitedDataDecision decision = m_unsolicitedDataPolicy->decide(inFace, data);
+  if (decision == fw::UnsolicitedDataDecision::CACHE) {
     // CS insert
     m_cs.insert(data, true);
   }
 
   NFD_LOG_DEBUG("onDataUnsolicited face=" << inFace.getId() <<
                 " data=" << data.getName() <<
-                (acceptToCache ? " cached" : " not cached"));
+                " decision=" << decision);
 }
 
 void
diff --git a/daemon/fw/forwarder.hpp b/daemon/fw/forwarder.hpp
index 855c24e..00645ba 100644
--- a/daemon/fw/forwarder.hpp
+++ b/daemon/fw/forwarder.hpp
@@ -30,6 +30,7 @@
 #include "core/scheduler.hpp"
 #include "forwarder-counters.hpp"
 #include "face-table.hpp"
+#include "unsolicited-data-policy.hpp"
 #include "table/fib.hpp"
 #include "table/pit.hpp"
 #include "table/cs.hpp"
@@ -62,7 +63,7 @@
     return m_counters;
   }
 
-public: // faces
+public: // faces and policies
   FaceTable&
   getFaceTable()
   {
@@ -89,6 +90,19 @@
     m_faceTable.add(face);
   }
 
+  fw::UnsolicitedDataPolicy&
+  getUnsolicitedDataPolicy() const
+  {
+    return *m_unsolicitedDataPolicy;
+  }
+
+  void
+  setUnsolicitedDataPolicy(unique_ptr<fw::UnsolicitedDataPolicy> policy)
+  {
+    BOOST_ASSERT(policy != nullptr);
+    m_unsolicitedDataPolicy = std::move(policy);
+  }
+
 public: // forwarding entrypoints and tables
   /** \brief start incoming Interest processing
    *  \param face face on which Interest is received
@@ -273,8 +287,8 @@
   ForwarderCounters m_counters;
 
   FaceTable m_faceTable;
+  unique_ptr<fw::UnsolicitedDataPolicy> m_unsolicitedDataPolicy;
 
-  // tables
   NameTree           m_nameTree;
   Fib                m_fib;
   Pit                m_pit;
diff --git a/daemon/fw/unsolicited-data-policy.cpp b/daemon/fw/unsolicited-data-policy.cpp
new file mode 100644
index 0000000..894cc86
--- /dev/null
+++ b/daemon/fw/unsolicited-data-policy.cpp
@@ -0,0 +1,53 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2016,  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 "unsolicited-data-policy.hpp"
+
+namespace nfd {
+namespace fw {
+
+std::ostream&
+operator<<(std::ostream& os, UnsolicitedDataDecision d)
+{
+  switch (d) {
+    case UnsolicitedDataDecision::DROP:
+      return os << "drop";
+    case UnsolicitedDataDecision::CACHE:
+      return os << "cache";
+  }
+  return os << static_cast<int>(d);
+}
+
+UnsolicitedDataDecision
+AdmitLocalUnsolicitedDataPolicy::decide(const Face& inFace, const Data& data) const
+{
+  if (inFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) {
+    return UnsolicitedDataDecision::CACHE;
+  }
+  return UnsolicitedDataDecision::DROP;
+}
+
+} // namespace fw
+} // namespace nfd
diff --git a/daemon/fw/unsolicited-data-policy.hpp b/daemon/fw/unsolicited-data-policy.hpp
new file mode 100644
index 0000000..54112ec
--- /dev/null
+++ b/daemon/fw/unsolicited-data-policy.hpp
@@ -0,0 +1,71 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2016,  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_DAEMON_FW_UNSOLICITED_DATA_POLICY_HPP
+#define NFD_DAEMON_FW_UNSOLICITED_DATA_POLICY_HPP
+
+#include "face/face.hpp"
+
+namespace nfd {
+namespace fw {
+
+/** \brief a decision made by UnsolicitedDataPolicy
+ */
+enum class UnsolicitedDataDecision {
+  DROP, ///< the Data should be dropped
+  CACHE ///< the Data should be cached in the ContentStore
+};
+
+std::ostream&
+operator<<(std::ostream& os, UnsolicitedDataDecision d);
+
+/** \brief determines how to process an unsolicited Data
+ *
+ *  An incoming Data is unsolicited if it does not match any PIT entry.
+ *  This class assists forwarding pipelines to decide whether to drop an unsolicited Data
+ *  or admit it into the ContentStore.
+ */
+class UnsolicitedDataPolicy : noncopyable
+{
+public:
+  virtual ~UnsolicitedDataPolicy() = default;
+
+  virtual UnsolicitedDataDecision
+  decide(const Face& inFace, const Data& data) const = 0;
+};
+
+/** \brief admits unsolicited Data from local faces
+ */
+class AdmitLocalUnsolicitedDataPolicy : public UnsolicitedDataPolicy
+{
+public:
+  virtual UnsolicitedDataDecision
+  decide(const Face& inFace, const Data& data) const final;
+};
+
+} // namespace fw
+} // namespace nfd
+
+#endif // NFD_DAEMON_FW_UNSOLICITED_DATA_POLICY_HPP