helper+model: Create helper to select the replacement policy of NFD's CS

Change-Id: I10933b5c96fd95bc395a1d08642a1e07e826dc45
Refs: #3837
diff --git a/helper/ndn-stack-helper.cpp b/helper/ndn-stack-helper.cpp
index 3b5571a..4b9cd90 100644
--- a/helper/ndn-stack-helper.cpp
+++ b/helper/ndn-stack-helper.cpp
@@ -36,6 +36,9 @@
 #include <map>
 #include <boost/lexical_cast.hpp>
 
+#include "ns3/ndnSIM/NFD/daemon/table/cs-policy-priority-fifo.hpp"
+#include "ns3/ndnSIM/NFD/daemon/table/cs-policy-lru.hpp"
+
 NS_LOG_COMPONENT_DEFINE("ndn.StackHelper");
 
 namespace ns3 {
@@ -51,6 +54,11 @@
 {
   setCustomNdnCxxClocks();
 
+  m_csPolicies.insert({"nfd::cs::lru", [] { return make_unique<nfd::cs::LruPolicy>(); }});
+  m_csPolicies.insert({"nfd::cs::priority_fifo", [] () { return make_unique<nfd::cs::PriorityFifoPolicy>(); }});
+
+  m_csPolicyCreationFunc = m_csPolicies["nfd::cs::lru"];
+
   m_ndnFactory.SetTypeId("ns3::ndn::L3Protocol");
   m_contentStoreFactory.SetTypeId("ns3::ndn::cs::Lru");
 
@@ -127,6 +135,22 @@
   m_maxCsSize = maxSize;
 }
 
+void
+StackHelper::setPolicy(const std::string& policy)
+{
+  auto found = m_csPolicies.find(policy);
+  if (found != m_csPolicies.end()) {
+    m_csPolicyCreationFunc = found->second;
+  }
+  else {
+    NS_FATAL_ERROR("Cache replacement policy " << policy << " not found");
+    NS_LOG_DEBUG("Available cache replacement policies: ");
+    for (auto it = m_csPolicies.begin(); it != m_csPolicies.end(); it++) {
+      NS_LOG_DEBUG("    " << it->first);
+    }
+  }
+}
+
 Ptr<FaceContainer>
 StackHelper::Install(const NodeContainer& c) const
 {
@@ -178,6 +202,10 @@
   if (m_maxCsSize == 0) {
     ndn->AggregateObject(m_contentStoreFactory.Create<ContentStore>());
   }
+  // if NFD's CS is enabled, check if a replacement policy has been specified
+  else {
+    ndn->setCsReplacementPolicy(m_csPolicyCreationFunc);
+  }
 
   // Aggregate L3Protocol on node (must be after setting ndnSIM CS)
   node->AggregateObject(ndn);
diff --git a/helper/ndn-stack-helper.hpp b/helper/ndn-stack-helper.hpp
index 0cb3413..bae814f 100644
--- a/helper/ndn-stack-helper.hpp
+++ b/helper/ndn-stack-helper.hpp
@@ -31,6 +31,12 @@
 #include "ndn-fib-helper.hpp"
 #include "ndn-strategy-choice-helper.hpp"
 
+namespace nfd {
+namespace cs {
+class Policy;
+} // namespace cs
+} // namespace nfd
+
 namespace ns3 {
 
 class Node;
@@ -75,6 +81,12 @@
   setCsSize(size_t maxSize);
 
   /**
+   * @brief Set the cache replacement policy for NFD's Content Store
+   */
+  void
+  setPolicy(const std::string& policy);
+
+  /**
    * @brief Set ndnSIM 1.0 content store implementation and its attributes
    * @param contentStoreClass string, representing class of the content store
    * @note ndnSIM 1.0 content store implementation have limited support for Interest selectors
@@ -262,6 +274,11 @@
   bool m_needSetDefaultRoutes;
   size_t m_maxCsSize;
 
+  typedef std::function<std::unique_ptr<nfd::cs::Policy>()> PolicyCreationCallback;
+  PolicyCreationCallback m_csPolicyCreationFunc;
+
+  std::map<std::string, PolicyCreationCallback> m_csPolicies;
+
   typedef std::list<std::pair<TypeId, FaceCreateCallback>> NetDeviceCallbackList;
   NetDeviceCallbackList m_netDeviceCallbacks;
 };