mgmt: publish Strategy Choice dataset

refs: #1632

Change-Id: I4ed09d191433fd80071367e7b2ffcd2ac81c5c17
diff --git a/daemon/mgmt/strategy-choice-manager.cpp b/daemon/mgmt/strategy-choice-manager.cpp
index 261358a..b51badc 100644
--- a/daemon/mgmt/strategy-choice-manager.cpp
+++ b/daemon/mgmt/strategy-choice-manager.cpp
@@ -5,7 +5,8 @@
  *                     Colorado State University,
  *                     University Pierre & Marie Curie, Sorbonne University,
  *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ *                     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.
@@ -42,10 +43,13 @@
   StrategyChoiceManager::COMMAND_UNSIGNED_NCOMPS +
   4; // (timestamp, nonce, signed info tlv, signature tlv)
 
+const Name StrategyChoiceManager::LIST_DATASET_PREFIX("/localhost/nfd/strategy-choice/list");
+
 StrategyChoiceManager::StrategyChoiceManager(StrategyChoice& strategyChoice,
                                              shared_ptr<InternalFace> face)
   : ManagerBase(face, STRATEGY_CHOICE_PRIVILEGE)
   , m_strategyChoice(strategyChoice)
+  , m_listPublisher(strategyChoice, m_face, LIST_DATASET_PREFIX)
 {
   face->setInterestFilter("/localhost/nfd/strategy-choice",
                           bind(&StrategyChoiceManager::onStrategyChoiceRequest, this, _2));
@@ -62,6 +66,12 @@
   const Name& command = request.getName();
   const size_t commandNComps = command.size();
 
+  if (command == LIST_DATASET_PREFIX)
+    {
+      listStrategies(request);
+      return;
+    }
+
   if (COMMAND_UNSIGNED_NCOMPS <= commandNComps &&
       commandNComps < COMMAND_SIGNED_NCOMPS)
     {
@@ -84,6 +94,12 @@
 }
 
 void
+StrategyChoiceManager::listStrategies(const Interest& request)
+{
+  m_listPublisher.publish();
+}
+
+void
 StrategyChoiceManager::onValidatedStrategyChoiceRequest(const shared_ptr<const Interest>& request)
 {
   static const Name::Component VERB_SET("set");
diff --git a/daemon/mgmt/strategy-choice-manager.hpp b/daemon/mgmt/strategy-choice-manager.hpp
index cf46e96..f3a0ed5 100644
--- a/daemon/mgmt/strategy-choice-manager.hpp
+++ b/daemon/mgmt/strategy-choice-manager.hpp
@@ -5,7 +5,8 @@
  *                     Colorado State University,
  *                     University Pierre & Marie Curie, Sorbonne University,
  *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ *                     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.
@@ -26,6 +27,7 @@
 #define NFD_DAEMON_MGMT_STRATEGY_CHOICE_MANAGER_HPP
 
 #include "mgmt/manager-base.hpp"
+#include "mgmt/strategy-choice-publisher.hpp"
 
 #include <ndn-cxx/management/nfd-control-parameters.hpp>
 
@@ -48,6 +50,10 @@
   onStrategyChoiceRequest(const Interest& request);
 
 PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+
+  void
+  listStrategies(const Interest& request);
+
   void
   onValidatedStrategyChoiceRequest(const shared_ptr<const Interest>& request);
 
@@ -63,6 +69,8 @@
 
   StrategyChoice& m_strategyChoice;
 
+  StrategyChoicePublisher m_listPublisher;
+
   static const Name COMMAND_PREFIX; // /localhost/nfd/strategy-choice
 
   // number of components in an invalid, but not malformed, unsigned command.
@@ -73,6 +81,8 @@
   // (see UNSIGNED_NCOMPS), 9 with signed Interest support.
   static const size_t COMMAND_SIGNED_NCOMPS;
 
+  static const Name LIST_DATASET_PREFIX; // /localhost/nfd/strategy-choice/list
+
 };
 
 } // namespace nfd
diff --git a/daemon/mgmt/strategy-choice-publisher.cpp b/daemon/mgmt/strategy-choice-publisher.cpp
new file mode 100644
index 0000000..933cc29
--- /dev/null
+++ b/daemon/mgmt/strategy-choice-publisher.cpp
@@ -0,0 +1,72 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014  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 "strategy-choice-publisher.hpp"
+#include "face-flags.hpp"
+#include "core/logger.hpp"
+#include "table/strategy-choice.hpp"
+
+#include <ndn-cxx/management/nfd-strategy-choice.hpp>
+
+namespace nfd {
+
+NFD_LOG_INIT("StrategyChoicePublisher");
+
+
+StrategyChoicePublisher::StrategyChoicePublisher(const StrategyChoice& strategyChoice,
+                                                 shared_ptr<AppFace> face,
+                                                 const Name& prefix)
+  : SegmentPublisher(face, prefix)
+  , m_strategyChoice(strategyChoice)
+{
+
+}
+
+StrategyChoicePublisher::~StrategyChoicePublisher()
+{
+
+}
+
+size_t
+StrategyChoicePublisher::generate(ndn::EncodingBuffer& outBuffer)
+{
+  size_t totalLength = 0;
+
+  for (StrategyChoice::const_iterator i = m_strategyChoice.begin();
+       i != m_strategyChoice.end();
+       ++i)
+    {
+      ndn::nfd::StrategyChoice entry;
+
+      entry.setName(i->getPrefix())
+           .setStrategy(i->getStrategyName());
+
+      totalLength += entry.wireEncode(outBuffer);
+    }
+
+  return totalLength;
+}
+
+} // namespace nfd
diff --git a/daemon/mgmt/strategy-choice-publisher.hpp b/daemon/mgmt/strategy-choice-publisher.hpp
new file mode 100644
index 0000000..d19a92e
--- /dev/null
+++ b/daemon/mgmt/strategy-choice-publisher.hpp
@@ -0,0 +1,58 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014  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_MGMT_STRATEGY_CHOICE_PUBLISHER_HPP
+#define NFD_DAEMON_MGMT_STRATEGY_CHOICE_PUBLISHER_HPP
+
+#include "mgmt/segment-publisher.hpp"
+
+namespace nfd {
+
+class StrategyChoice;
+
+class StrategyChoicePublisher : public SegmentPublisher
+{
+public:
+  StrategyChoicePublisher(const StrategyChoice& strategyChoice,
+                          shared_ptr<AppFace> face,
+                          const Name& prefix);
+
+  virtual
+  ~StrategyChoicePublisher();
+
+protected:
+
+  virtual size_t
+  generate(ndn::EncodingBuffer& outBuffer);
+
+private:
+
+  const StrategyChoice& m_strategyChoice;
+
+};
+
+} // namespace nfd
+
+#endif // NFD_DAEMON_MGMT_STRATEGY_CHOICE_PUBLISHER_HPP
diff --git a/tests/daemon/mgmt/strategy-choice-manager.cpp b/tests/daemon/mgmt/strategy-choice-manager.cpp
index 9b5f2c5..9f6818e 100644
--- a/tests/daemon/mgmt/strategy-choice-manager.cpp
+++ b/tests/daemon/mgmt/strategy-choice-manager.cpp
@@ -32,6 +32,8 @@
 #include "tests/daemon/face/dummy-face.hpp"
 #include "tests/daemon/fw/dummy-strategy.hpp"
 
+#include <ndn-cxx/management/nfd-strategy-choice.hpp>
+
 #include "tests/test-common.hpp"
 #include "validation-common.hpp"
 
@@ -118,6 +120,15 @@
 
   }
 
+  void
+  validateList(const Data& data, const ndn::nfd::StrategyChoice& expectedChoice)
+  {
+    m_callbackFired = true;
+    ndn::nfd::StrategyChoice choice(data.getContent().blockFromValue());
+    BOOST_CHECK_EQUAL(choice.getStrategy(), expectedChoice.getStrategy());
+    BOOST_CHECK_EQUAL(choice.getName(), expectedChoice.getName());
+  }
+
   bool
   didCallbackFire()
   {
@@ -544,6 +555,22 @@
                     "/localhost/nfd/strategy/test-strategy-b");
 }
 
+BOOST_AUTO_TEST_CASE(Publish)
+{
+  Name commandName("/localhost/nfd/strategy-choice/list");
+  shared_ptr<Interest> command(make_shared<Interest>(commandName));
+
+  ndn::nfd::StrategyChoice expectedChoice;
+  expectedChoice.setStrategy("/localhost/nfd/strategy/test-strategy-a");
+  expectedChoice.setName("/");
+
+  getFace()->onReceiveData +=
+    bind(&StrategyChoiceManagerFixture::validateList, this, _1, expectedChoice);
+
+  m_manager.onStrategyChoiceRequest(*command);
+  BOOST_REQUIRE(didCallbackFire());
+}
+
 BOOST_AUTO_TEST_SUITE_END()
 
 } // namespace tests
diff --git a/tests/daemon/mgmt/strategy-choice-publisher.cpp b/tests/daemon/mgmt/strategy-choice-publisher.cpp
new file mode 100644
index 0000000..ac86cd1
--- /dev/null
+++ b/tests/daemon/mgmt/strategy-choice-publisher.cpp
@@ -0,0 +1,168 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014  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_TESTS_NFD_MGMT_STRATEGY_CHOICE_PUBLISHER_HPP
+#define NFD_TESTS_NFD_MGMT_STRATEGY_CHOICE_PUBLISHER_HPP
+
+#include "mgmt/strategy-choice-publisher.hpp"
+#include "mgmt/app-face.hpp"
+#include "mgmt/internal-face.hpp"
+#include "mgmt/face-flags.hpp"
+#include "fw/forwarder.hpp"
+#include "../fw/dummy-strategy.hpp"
+
+#include "tests/test-common.hpp"
+
+#include <ndn-cxx/management/nfd-strategy-choice.hpp>
+
+namespace nfd {
+namespace tests {
+
+class StrategyChoicePublisherFixture : BaseFixture
+{
+public:
+
+  StrategyChoicePublisherFixture()
+    : m_strategyChoice(m_forwarder.getStrategyChoice())
+    , m_face(make_shared<InternalFace>())
+    , m_publisher(m_strategyChoice, m_face, "/localhost/nfd/strategy-choice/list")
+    , STRATEGY_A(make_shared<DummyStrategy>(boost::ref(m_forwarder),
+                                            "/localhost/nfd/strategy/dummy-strategy-a"))
+    , STRATEGY_B(make_shared<DummyStrategy>(boost::ref(m_forwarder),
+                                            "/localhost/nfd/strategy/dummy-strategy-b"))
+    , m_finished(false)
+  {
+    m_strategyChoice.install(STRATEGY_A);
+    m_strategyChoice.install(STRATEGY_B);
+
+    ndn::nfd::StrategyChoice expectedRootEntry;
+    expectedRootEntry.setStrategy(STRATEGY_A->getName());
+    expectedRootEntry.setName("/");
+
+    m_strategyChoice.insert("/", STRATEGY_A->getName());
+    m_expectedEntries["/"] = expectedRootEntry;
+  }
+
+  void
+  validatePublish(const Data& data)
+  {
+    Block payload = data.getContent();
+
+    m_buffer.appendByteArray(payload.value(), payload.value_size());
+
+    BOOST_CHECK_NO_THROW(data.getName()[-1].toSegment());
+    if (data.getFinalBlockId() != data.getName()[-1])
+      {
+        return;
+      }
+
+    // wrap the Strategy Choice entries in a single Content TLV for easy parsing
+    m_buffer.prependVarNumber(m_buffer.size());
+    m_buffer.prependVarNumber(ndn::Tlv::Content);
+
+    ndn::Block parser(m_buffer.buf(), m_buffer.size());
+    parser.parse();
+
+    BOOST_REQUIRE_EQUAL(parser.elements_size(), m_expectedEntries.size());
+
+    for (Block::element_const_iterator i = parser.elements_begin();
+         i != parser.elements_end();
+         ++i)
+      {
+        if (i->type() != ndn::tlv::nfd::StrategyChoice)
+          {
+            BOOST_FAIL("expected StrategyChoice, got type #" << i->type());
+          }
+
+        ndn::nfd::StrategyChoice entry(*i);
+
+        std::map<std::string, ndn::nfd::StrategyChoice>::const_iterator expectedEntryPos =
+          m_expectedEntries.find(entry.getName().toUri());
+
+        BOOST_REQUIRE(expectedEntryPos != m_expectedEntries.end());
+        const ndn::nfd::StrategyChoice& expectedEntry = expectedEntryPos->second;
+
+        BOOST_CHECK_EQUAL(entry.getStrategy(), expectedEntry.getStrategy());
+        BOOST_CHECK_EQUAL(entry.getName(), expectedEntry.getName());
+
+        m_matchedEntries.insert(entry.getName().toUri());
+      }
+
+    BOOST_CHECK_EQUAL(m_matchedEntries.size(), m_expectedEntries.size());
+
+    m_finished = true;
+  }
+
+protected:
+  Forwarder m_forwarder;
+  StrategyChoice& m_strategyChoice;
+  shared_ptr<InternalFace> m_face;
+  StrategyChoicePublisher m_publisher;
+
+  shared_ptr<DummyStrategy> STRATEGY_A;
+  shared_ptr<DummyStrategy> STRATEGY_B;
+
+  ndn::EncodingBuffer m_buffer;
+
+  std::map<std::string, ndn::nfd::StrategyChoice> m_expectedEntries;
+  std::set<std::string> m_matchedEntries;
+
+  bool m_finished;
+};
+
+
+
+BOOST_FIXTURE_TEST_SUITE(MgmtStrategyChoicePublisher, StrategyChoicePublisherFixture)
+
+BOOST_AUTO_TEST_CASE(Publish)
+{
+  m_strategyChoice.insert("/test/a", STRATEGY_A->getName());
+  m_strategyChoice.insert("/test/b", STRATEGY_B->getName());
+
+  ndn::nfd::StrategyChoice expectedEntryA;
+  expectedEntryA.setStrategy(STRATEGY_A->getName());
+  expectedEntryA.setName("/test/a");
+
+  ndn::nfd::StrategyChoice expectedEntryB;
+  expectedEntryB.setStrategy(STRATEGY_B->getName());
+  expectedEntryB.setName("/test/b");
+
+  m_expectedEntries["/test/a"] = expectedEntryA;
+  m_expectedEntries["/test/b"] = expectedEntryB;
+
+  m_face->onReceiveData +=
+    bind(&StrategyChoicePublisherFixture::validatePublish, this, _1);
+
+  m_publisher.publish();
+  BOOST_REQUIRE(m_finished);
+}
+
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace nfd
+
+#endif // NFD_TESTS_NFD_MGMT_STRATEGY_CHOICE_PUBLISHER_HPP