table: Strategy Choice table

refs #1309

Change-Id: I6fd8efdfc98175a1cc39fdc3aa7175671596f470
diff --git a/daemon/table/name-tree-entry.cpp b/daemon/table/name-tree-entry.cpp
index 40e19c8..673be47 100644
--- a/daemon/table/name-tree-entry.cpp
+++ b/daemon/table/name-tree-entry.cpp
@@ -99,5 +99,11 @@
   return true;
 }
 
+void
+Entry::setStrategyChoiceEntry(shared_ptr<strategy_choice::Entry> strategyChoiceEntry)
+{
+  m_strategyChoiceEntry = strategyChoiceEntry;
+}
+
 } // namespace name_tree
 } // namespace nfd
diff --git a/daemon/table/name-tree-entry.hpp b/daemon/table/name-tree-entry.hpp
index cef42ff..1708dbf 100644
--- a/daemon/table/name-tree-entry.hpp
+++ b/daemon/table/name-tree-entry.hpp
@@ -13,6 +13,7 @@
 #include "table/fib-entry.hpp"
 #include "table/pit-entry.hpp"
 #include "table/measurements-entry.hpp"
+#include "table/strategy-choice-entry.hpp"
 
 namespace nfd {
 
@@ -106,6 +107,12 @@
   bool
   eraseMeasurementsEntry(shared_ptr<measurements::Entry> measurements);
 
+  void
+  setStrategyChoiceEntry(shared_ptr<strategy_choice::Entry> strategyChoiceEntry);
+
+  shared_ptr<strategy_choice::Entry>
+  getStrategyChoiceEntry() const;
+
 private:
   uint32_t m_hash;
   Name m_prefix;
@@ -114,6 +121,7 @@
   shared_ptr<fib::Entry> m_fibEntry;
   std::vector<shared_ptr<pit::Entry> > m_pitEntries;
   shared_ptr<measurements::Entry> m_measurementsEntry;
+  shared_ptr<strategy_choice::Entry> m_strategyChoiceEntry;
 
   // get the Name Tree Node that is associated with this Name Tree Entry
   Node* m_node;
@@ -170,6 +178,12 @@
   return m_measurementsEntry;
 }
 
+inline shared_ptr<strategy_choice::Entry>
+Entry::getStrategyChoiceEntry() const
+{
+  return m_strategyChoiceEntry;
+}
+
 } // namespace name_tree
 } // namespace nfd
 
diff --git a/daemon/table/strategy-choice-entry.cpp b/daemon/table/strategy-choice-entry.cpp
new file mode 100644
index 0000000..9d3f218
--- /dev/null
+++ b/daemon/table/strategy-choice-entry.cpp
@@ -0,0 +1,25 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "strategy-choice-entry.hpp"
+
+namespace nfd {
+namespace strategy_choice {
+
+Entry::Entry(const Name& prefix)
+  : m_prefix(prefix)
+{
+}
+
+void
+Entry::setStrategy(shared_ptr<fw::Strategy> strategy)
+{
+  BOOST_ASSERT(static_cast<bool>(strategy));
+  m_strategy = strategy;
+}
+
+} // namespace strategy_choice
+} // namespace nfd
diff --git a/daemon/table/strategy-choice-entry.hpp b/daemon/table/strategy-choice-entry.hpp
new file mode 100644
index 0000000..f1b8c7a
--- /dev/null
+++ b/daemon/table/strategy-choice-entry.hpp
@@ -0,0 +1,66 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NFD_TABLE_STRATEGY_CHOICE_ENTRY_HPP
+#define NFD_TABLE_STRATEGY_CHOICE_ENTRY_HPP
+
+#include "common.hpp"
+
+namespace nfd {
+
+class NameTree;
+namespace name_tree {
+class Entry;
+}
+namespace fw {
+class Strategy;
+}
+
+namespace strategy_choice {
+
+/** \brief represents a Strategy Choice entry
+ */
+class Entry : noncopyable
+{
+public:
+  Entry(const Name& prefix);
+
+  const Name&
+  getPrefix() const;
+
+  fw::Strategy&
+  getStrategy() const;
+
+  void
+  setStrategy(shared_ptr<fw::Strategy> strategy);
+
+private:
+  Name m_prefix;
+  shared_ptr<fw::Strategy> m_strategy;
+
+  shared_ptr<name_tree::Entry> m_nameTreeEntry;
+  friend class nfd::NameTree;
+  friend class nfd::name_tree::Entry;
+};
+
+
+inline const Name&
+Entry::getPrefix() const
+{
+  return m_prefix;
+}
+
+inline fw::Strategy&
+Entry::getStrategy() const
+{
+  BOOST_ASSERT(static_cast<bool>(m_strategy));
+  return *m_strategy;
+}
+
+} // namespace strategy_choice
+} // namespace nfd
+
+#endif // NFD_TABLE_STRATEGY_CHOICE_ENTRY_HPP
diff --git a/daemon/table/strategy-choice.cpp b/daemon/table/strategy-choice.cpp
new file mode 100644
index 0000000..9e5cb37
--- /dev/null
+++ b/daemon/table/strategy-choice.cpp
@@ -0,0 +1,178 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "strategy-choice.hpp"
+#include "fw/strategy.hpp"
+#include "pit-entry.hpp"
+
+namespace nfd {
+
+using strategy_choice::Entry;
+using fw::Strategy;
+
+NFD_LOG_INIT("StrategyChoice");
+
+StrategyChoice::StrategyChoice(NameTree& nameTree, shared_ptr<Strategy> defaultStrategy)
+  : m_nameTree(nameTree)
+{
+  this->setDefaultStrategy(defaultStrategy);
+}
+
+bool
+StrategyChoice::hasStrategy(const Name& strategyName) const
+{
+  return m_strategyInstances.count(strategyName) > 0;
+}
+
+bool
+StrategyChoice::install(shared_ptr<Strategy> strategy)
+{
+  BOOST_ASSERT(static_cast<bool>(strategy));
+  const Name& strategyName = strategy->getName();
+
+  if (this->hasStrategy(strategyName)) {
+    NFD_LOG_ERROR("install(" << strategyName << ") duplicate strategyName");
+    return false;
+  }
+
+  m_strategyInstances[strategyName] = strategy;
+  return true;
+}
+
+bool
+StrategyChoice::insert(const Name& prefix, const Name& strategyName)
+{
+  shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(prefix);
+  shared_ptr<Entry> entry = nameTreeEntry->getStrategyChoiceEntry();
+  shared_ptr<Strategy> oldStrategy;
+
+  if (static_cast<bool>(entry)) {
+    if (entry->getStrategy().getName() == strategyName) {
+      NFD_LOG_INFO("insert(" << prefix << "," << strategyName << ") not changing");
+      return true;
+    }
+    oldStrategy = entry->getStrategy().shared_from_this();
+    NFD_LOG_INFO("insert(" << prefix << "," << strategyName << ") "
+                 "changing from " << oldStrategy->getName());
+  }
+
+  shared_ptr<Strategy> strategy = this->getStrategy(strategyName);
+  if (!static_cast<bool>(strategy)) {
+    NFD_LOG_ERROR("insert(" << prefix << "," << strategyName << ") strategy not installed");
+    return false;
+  }
+
+  if (!static_cast<bool>(entry)) {
+    oldStrategy = this->findEffectiveStrategy(prefix).shared_from_this();
+    entry = make_shared<Entry>(prefix);
+    nameTreeEntry->setStrategyChoiceEntry(entry);
+    ++m_nItems;
+    NFD_LOG_INFO("insert(" << prefix << "," << strategyName << ") new entry");
+  }
+
+  this->changeStrategy(entry, oldStrategy, strategy);
+  return true;
+}
+
+void
+StrategyChoice::erase(const Name& prefix)
+{
+  BOOST_ASSERT(prefix.size() > 0);
+
+  shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix);
+  if (!static_cast<bool>(nameTreeEntry)) {
+    return;
+  }
+
+  shared_ptr<Entry> entry = nameTreeEntry->getStrategyChoiceEntry();
+  if (!static_cast<bool>(entry)) {
+    return;
+  }
+
+  Strategy& oldStrategy = entry->getStrategy();
+  nameTreeEntry->setStrategyChoiceEntry(shared_ptr<Entry>());
+  --m_nItems;
+
+  Strategy& parentStrategy = this->findEffectiveStrategy(prefix);
+  this->changeStrategy(entry, oldStrategy.shared_from_this(), parentStrategy.shared_from_this());
+}
+
+shared_ptr<const Name>
+StrategyChoice::get(const Name& prefix) const
+{
+  shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.findExactMatch(prefix);
+  if (!static_cast<bool>(nameTreeEntry)) {
+    return shared_ptr<const Name>();
+  }
+
+  shared_ptr<Entry> entry = nameTreeEntry->getStrategyChoiceEntry();
+  if (!static_cast<bool>(entry)) {
+    return shared_ptr<const Name>();
+  }
+
+  return entry->getStrategy().getName().shared_from_this();
+}
+
+static inline bool
+predicate_NameTreeEntry_hasStrategyChoiceEntry(const name_tree::Entry& entry)
+{
+  return static_cast<bool>(entry.getStrategyChoiceEntry());
+}
+
+Strategy&
+StrategyChoice::findEffectiveStrategy(const Name& prefix) const
+{
+  shared_ptr<name_tree::Entry> nameTreeEntry =
+    m_nameTree.findLongestPrefixMatch(prefix, &predicate_NameTreeEntry_hasStrategyChoiceEntry);
+  BOOST_ASSERT(static_cast<bool>(nameTreeEntry));
+  return nameTreeEntry->getStrategyChoiceEntry()->getStrategy();
+}
+
+Strategy&
+StrategyChoice::findEffectiveStrategy(const pit::Entry& pitEntry) const
+{
+  return this->findEffectiveStrategy(pitEntry.getName());
+}
+
+shared_ptr<fw::Strategy>
+StrategyChoice::getStrategy(const Name& strategyName)
+{
+  StrategyInstanceTable::iterator it = m_strategyInstances.find(strategyName);
+  return it != m_strategyInstances.end() ? it->second : shared_ptr<fw::Strategy>();
+}
+
+void
+StrategyChoice::setDefaultStrategy(shared_ptr<Strategy> strategy)
+{
+  this->install(strategy);
+
+  // don't use .insert here, because it will invoke findEffectiveStrategy
+  // which expects an existing root entry
+  shared_ptr<name_tree::Entry> nameTreeEntry = m_nameTree.lookup(Name());
+  shared_ptr<Entry> entry = make_shared<Entry>(Name());
+  nameTreeEntry->setStrategyChoiceEntry(entry);
+  ++m_nItems;
+  NFD_LOG_INFO("setDefaultStrategy(" << strategy->getName() << ") new entry");
+
+  entry->setStrategy(strategy);
+}
+
+void
+StrategyChoice::changeStrategy(shared_ptr<strategy_choice::Entry> entry,
+                               shared_ptr<fw::Strategy> oldStrategy,
+                               shared_ptr<fw::Strategy> newStrategy)
+{
+  entry->setStrategy(newStrategy);
+  if (oldStrategy == newStrategy) {
+    return;
+  }
+
+  // TODO delete incompatible StrategyInfo
+  NFD_LOG_WARN("changeStrategy(" << entry->getPrefix() << ") " <<
+               "runtime strategy change not implemented");
+}
+
+} // namespace nfd
diff --git a/daemon/table/strategy-choice.hpp b/daemon/table/strategy-choice.hpp
new file mode 100644
index 0000000..7624dd4
--- /dev/null
+++ b/daemon/table/strategy-choice.hpp
@@ -0,0 +1,94 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NFD_TABLE_STRATEGY_CHOICE_HPP
+#define NFD_TABLE_STRATEGY_CHOICE_HPP
+
+#include "strategy-choice-entry.hpp"
+#include "name-tree.hpp"
+
+namespace nfd {
+
+class StrategyChoice : noncopyable
+{
+public:
+  StrategyChoice(NameTree& nameTree, shared_ptr<fw::Strategy> defaultStrategy);
+
+public: // available Strategy types
+  /** \return true if strategy is installed
+   */
+  bool
+  hasStrategy(const Name& strategyName) const;
+
+  /** \brief install a strategy
+   *  \return true if installed; false if not installed due to duplicate strategyName
+   */
+  bool
+  install(shared_ptr<fw::Strategy> strategy);
+
+public: // Strategy Choice table
+  /** \brief set strategy of prefix to be strategyName
+   *  \param strategyName the strategy to be used, must be installed
+   *  \return true on success
+   */
+  bool
+  insert(const Name& prefix, const Name& strategyName);
+
+  /** \brief make prefix to inherit strategy from its parent
+   *
+   *  not allowed for root prefix (ndn:/)
+   */
+  void
+  erase(const Name& prefix);
+
+  /** \brief get strategy Name of prefix
+   *  \return strategyName at exact match, or nullptr
+   */
+  shared_ptr<const Name>
+  get(const Name& prefix) const;
+
+public: // effect strategy
+  /// get effective strategy for prefix
+  fw::Strategy&
+  findEffectiveStrategy(const Name& prefix) const;
+
+  /// get effective strategy for pitEntry
+  fw::Strategy&
+  findEffectiveStrategy(const pit::Entry& pitEntry) const;
+
+  /// number of entries stored
+  size_t
+  size() const;
+
+private:
+  shared_ptr<fw::Strategy>
+  getStrategy(const Name& strategyName);
+
+  void
+  setDefaultStrategy(shared_ptr<fw::Strategy> strategy);
+
+  void
+  changeStrategy(shared_ptr<strategy_choice::Entry> entry,
+                 shared_ptr<fw::Strategy> oldStrategy,
+                 shared_ptr<fw::Strategy> newStrategy);
+
+private:
+  NameTree& m_nameTree;
+  size_t m_nItems;
+
+  typedef std::map<Name, shared_ptr<fw::Strategy> > StrategyInstanceTable;
+  StrategyInstanceTable m_strategyInstances;
+};
+
+inline size_t
+StrategyChoice::size() const
+{
+  return m_nItems;
+}
+
+} // namespace nfd
+
+#endif // NFD_TABLE_STRATEGY_CHOICE_HPP