table: Strategy Choice table

refs #1309

Change-Id: I6fd8efdfc98175a1cc39fdc3aa7175671596f470
diff --git a/daemon/fw/best-route-strategy.cpp b/daemon/fw/best-route-strategy.cpp
index d60411f..19458e9 100644
--- a/daemon/fw/best-route-strategy.cpp
+++ b/daemon/fw/best-route-strategy.cpp
@@ -10,7 +10,7 @@
 namespace fw {
 
 BestRouteStrategy::BestRouteStrategy(Forwarder& forwarder)
-  : Strategy(forwarder)
+  : Strategy(forwarder, "ndn:/localhost/nfd/strategy/best-route")
 {
 }
 
diff --git a/daemon/fw/broadcast-strategy.cpp b/daemon/fw/broadcast-strategy.cpp
index 49d0ef6..4cc4e5f 100644
--- a/daemon/fw/broadcast-strategy.cpp
+++ b/daemon/fw/broadcast-strategy.cpp
@@ -10,7 +10,7 @@
 namespace fw {
 
 BroadcastStrategy::BroadcastStrategy(Forwarder& forwarder)
-  : Strategy(forwarder)
+  : Strategy(forwarder, "ndn:/localhost/nfd/strategy/broadcast")
 {
 }
 
diff --git a/daemon/fw/forwarder.cpp b/daemon/fw/forwarder.cpp
index b32041e..6c9f3ce 100644
--- a/daemon/fw/forwarder.cpp
+++ b/daemon/fw/forwarder.cpp
@@ -20,6 +20,7 @@
   , m_fib(m_nameTree)
   , m_pit(m_nameTree)
   , m_measurements(m_nameTree)
+  , m_strategyChoice(m_nameTree, make_shared<fw::BestRouteStrategy>(boost::ref(*this)))
 {
   m_strategy = make_shared<fw::BestRouteStrategy>(boost::ref(*this));
 }
diff --git a/daemon/fw/forwarder.hpp b/daemon/fw/forwarder.hpp
index 726d8a0..7ab0849 100644
--- a/daemon/fw/forwarder.hpp
+++ b/daemon/fw/forwarder.hpp
@@ -14,6 +14,7 @@
 #include "table/pit.hpp"
 #include "table/cs.hpp"
 #include "table/measurements.hpp"
+#include "table/strategy-choice.hpp"
 #include "strategy.hpp"
 
 namespace nfd {
@@ -72,6 +73,9 @@
   Measurements&
   getMeasurements();
 
+  StrategyChoice&
+  getStrategyChoice();
+
 PUBLIC_WITH_TESTS_ELSE_PRIVATE: // pipelines
   /** \brief incoming Interest pipeline
    */
@@ -133,11 +137,14 @@
 private:
   FaceTable m_faceTable;
 
-  NameTree     m_nameTree;
-  Fib          m_fib;
-  Pit          m_pit;
-  Cs           m_cs;
-  Measurements m_measurements;
+  // tables
+  NameTree       m_nameTree;
+  Fib            m_fib;
+  Pit            m_pit;
+  Cs             m_cs;
+  Measurements   m_measurements;
+  StrategyChoice m_strategyChoice;
+
   /// the active strategy (only one strategy in mock)
   shared_ptr<fw::Strategy> m_strategy;
 
@@ -207,6 +214,11 @@
   return m_measurements;
 }
 
+inline StrategyChoice&
+Forwarder::getStrategyChoice()
+{
+  return m_strategyChoice;
+}
 
 } // namespace nfd
 
diff --git a/daemon/fw/ncc-strategy.cpp b/daemon/fw/ncc-strategy.cpp
index d08260c..101e847 100644
--- a/daemon/fw/ncc-strategy.cpp
+++ b/daemon/fw/ncc-strategy.cpp
@@ -10,7 +10,7 @@
 namespace fw {
 
 NccStrategy::NccStrategy(Forwarder& forwarder)
-  : Strategy(forwarder)
+  : Strategy(forwarder, "ndn:/localhost/nfd/strategy/ncc")
 {
 }
 
diff --git a/daemon/fw/strategy.cpp b/daemon/fw/strategy.cpp
index 09268a8..fbb2b4a 100644
--- a/daemon/fw/strategy.cpp
+++ b/daemon/fw/strategy.cpp
@@ -13,8 +13,9 @@
 
 NFD_LOG_INIT("Strategy");
 
-Strategy::Strategy(Forwarder& forwarder)
-  : m_forwarder(forwarder)
+Strategy::Strategy(Forwarder& forwarder, const Name& name)
+  : m_name(name)
+  , m_forwarder(forwarder)
   , m_measurements(m_forwarder.getMeasurements(), m_forwarder.getFib(), this)
 {
 }
diff --git a/daemon/fw/strategy.hpp b/daemon/fw/strategy.hpp
index edffdb3..df4c946 100644
--- a/daemon/fw/strategy.hpp
+++ b/daemon/fw/strategy.hpp
@@ -18,18 +18,20 @@
 
 namespace fw {
 
-/** \class Strategy
- *  \brief represents a forwarding strategy
+/** \brief represents a forwarding strategy
  */
-class Strategy
+class Strategy : public enable_shared_from_this<Strategy>, noncopyable
 {
 public:
-  explicit
-  Strategy(Forwarder& forwarder);
+  Strategy(Forwarder& forwarder, const Name& name);
 
   virtual
   ~Strategy();
 
+  /// a Name that represent the Strategy program
+  const Name&
+  getName() const;
+
 public: // triggers
   /** \brief trigger after Interest is received
    *
@@ -113,6 +115,8 @@
   getMeasurements();
 
 private:
+  Name m_name;
+
   /** \brief reference to the forwarder
    *
    *  Triggers can access forwarder indirectly via actions.
@@ -122,6 +126,12 @@
   MeasurementsAccessor m_measurements;
 };
 
+inline const Name&
+Strategy::getName() const
+{
+  return m_name;
+}
+
 inline MeasurementsAccessor&
 Strategy::getMeasurements()
 {
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