management: StatusDataset client

refs #3329

Change-Id: Icf755ba7ed1fd36f6ea09af97cb77f70757f84c7
diff --git a/src/management/nfd-controller.cpp b/src/management/nfd-controller.cpp
index 2ebdb4a..acd2435 100644
--- a/src/management/nfd-controller.cpp
+++ b/src/management/nfd-controller.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2015 Regents of the University of California.
+ * Copyright (c) 2013-2016 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -21,18 +21,23 @@
 
 #include "nfd-controller.hpp"
 #include "nfd-control-response.hpp"
+#include "../util/segment-fetcher.hpp"
 
 namespace ndn {
 namespace nfd {
 
+using ndn::util::SegmentFetcher;
+
 const uint32_t Controller::ERROR_TIMEOUT = 10060; // WinSock ESAETIMEDOUT
 const uint32_t Controller::ERROR_NACK = 10800; // 10000 + TLV-TYPE of Nack header
 const uint32_t Controller::ERROR_SERVER = 500;
 const uint32_t Controller::ERROR_LBOUND = 400;
+ValidatorNull Controller::s_validatorNull;
 
 Controller::Controller(Face& face, KeyChain& keyChain)
   : m_face(face)
   , m_keyChain(keyChain)
+  , m_validator(s_validatorNull) /// \todo #3653 accept validator as constructor parameter
 {
 }
 
@@ -103,5 +108,41 @@
     onSuccess(parameters);
 }
 
+void
+Controller::fetchDataset(const Name& prefix,
+                         const std::function<void(const ConstBufferPtr&)>& processResponse,
+                         const CommandFailCallback& onFailure,
+                         const CommandOptions& options)
+{
+  Interest baseInterest(prefix);
+  baseInterest.setInterestLifetime(options.getTimeout());
+
+  SegmentFetcher::fetch(m_face, baseInterest, m_validator, processResponse,
+                        bind(&Controller::processDatasetFetchError, this, onFailure, _1, _2));
+}
+
+void
+Controller::processDatasetFetchError(const CommandFailCallback& onFailure,
+                                     uint32_t code, std::string msg)
+{
+  switch (static_cast<SegmentFetcher::ErrorCode>(code)) {
+    // It's intentional to cast as SegmentFetcher::ErrorCode, and to not have a 'default' clause.
+    // This forces the switch statement to handle every defined SegmentFetcher::ErrorCode,
+    // and breaks compilation if it does not.
+    case SegmentFetcher::ErrorCode::INTEREST_TIMEOUT:
+      onFailure(ERROR_TIMEOUT, msg);
+      break;
+    case SegmentFetcher::ErrorCode::DATA_HAS_NO_SEGMENT:
+      onFailure(ERROR_SERVER, msg);
+      break;
+    case SegmentFetcher::ErrorCode::SEGMENT_VALIDATION_FAIL:
+      BOOST_ASSERT(false); /// \todo #3653 introduce ERROR_VALIDATION
+      break;
+    case SegmentFetcher::ErrorCode::NACK_ERROR:
+      onFailure(ERROR_NACK, msg);
+      break;
+  }
+}
+
 } // namespace nfd
 } // namespace ndn
diff --git a/src/management/nfd-controller.hpp b/src/management/nfd-controller.hpp
index 4fbfa3e..7ebc5eb 100644
--- a/src/management/nfd-controller.hpp
+++ b/src/management/nfd-controller.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2015 Regents of the University of California.
+ * Copyright (c) 2013-2016 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -23,9 +23,11 @@
 #define NDN_MANAGEMENT_NFD_CONTROLLER_HPP
 
 #include "nfd-control-command.hpp"
+#include "nfd-status-dataset.hpp"
+#include "nfd-command-options.hpp"
 #include "../face.hpp"
 #include "../security/key-chain.hpp"
-#include "nfd-command-options.hpp"
+#include "../security/validator-null.hpp"
 
 namespace ndn {
 namespace nfd {
@@ -48,7 +50,7 @@
 
   /** \brief a callback on command failure
    */
-  typedef function<void(uint32_t/*code*/,const std::string&/*reason*/)> CommandFailCallback;
+  typedef function<void(uint32_t code, const std::string& reason)> CommandFailCallback;
 
   /** \brief construct a Controller that uses face for transport,
    *         and uses the passed KeyChain to sign commands
@@ -68,6 +70,29 @@
     this->startCommand(command, parameters, onSuccess, onFailure, options);
   }
 
+  /** \brief start dataset fetching
+   */
+  template<typename Dataset>
+  typename std::enable_if<std::is_default_constructible<Dataset>::value>::type
+  fetch(const std::function<void(typename Dataset::ResultType)>& onSuccess,
+        const CommandFailCallback& onFailure,
+        const CommandOptions& options = CommandOptions())
+  {
+    this->fetchDataset(make_shared<Dataset>(), onSuccess, onFailure, options);
+  }
+
+  /** \brief start dataset fetching
+   */
+  template<typename Dataset, typename ParamType = typename Dataset::ParamType>
+  void
+  fetch(const ParamType& param,
+        const std::function<void(typename Dataset::ResultType)>& onSuccess,
+        const CommandFailCallback& onFailure,
+        const CommandOptions& options = CommandOptions())
+  {
+    this->fetchDataset(make_shared<Dataset>(param), onSuccess, onFailure, options);
+  }
+
 private:
   void
   startCommand(const shared_ptr<ControlCommand>& command,
@@ -82,6 +107,30 @@
                          const CommandSucceedCallback& onSuccess,
                          const CommandFailCallback& onFailure);
 
+  template<typename Dataset>
+  void
+  fetchDataset(shared_ptr<Dataset> dataset,
+               const std::function<void(typename Dataset::ResultType)>& onSuccess,
+               const CommandFailCallback& onFailure,
+               const CommandOptions& options);
+
+  void
+  fetchDataset(const Name& prefix,
+               const std::function<void(const ConstBufferPtr&)>& processResponse,
+               const CommandFailCallback& onFailure,
+               const CommandOptions& options);
+
+  template<typename Dataset>
+  void
+  processDatasetResponse(shared_ptr<Dataset> dataset,
+                         const std::function<void(typename Dataset::ResultType)>& onSuccess,
+                         const CommandFailCallback& onFailure,
+                         ConstBufferPtr payload);
+
+  void
+  processDatasetFetchError(const CommandFailCallback& onFailure, uint32_t code, std::string msg);
+
+
 public:
   /** \brief error code for timeout
    */
@@ -102,8 +151,44 @@
 protected:
   Face& m_face;
   KeyChain& m_keyChain;
+  Validator& m_validator;
+
+private:
+  static ValidatorNull s_validatorNull;
 };
 
+template<typename Dataset>
+inline void
+Controller::fetchDataset(shared_ptr<Dataset> dataset,
+                         const std::function<void(typename Dataset::ResultType)>& onSuccess,
+                         const CommandFailCallback& onFailure,
+                         const CommandOptions& options)
+{
+  Name prefix = dataset->getDatasetPrefix(options.getPrefix());
+  this->fetchDataset(prefix,
+                     bind(&Controller::processDatasetResponse<Dataset>, this, dataset, onSuccess, onFailure, _1),
+                     onFailure,
+                     options);
+}
+
+template<typename Dataset>
+inline void
+Controller::processDatasetResponse(shared_ptr<Dataset> dataset,
+                                   const std::function<void(typename Dataset::ResultType)>& onSuccess,
+                                   const CommandFailCallback& onFailure,
+                                   ConstBufferPtr payload)
+{
+  typename Dataset::ResultType result;
+  try {
+    result = dataset->parseResult(payload);
+  }
+  catch (const tlv::Error& ex) {
+    onFailure(ERROR_SERVER, ex.what());
+    return;
+  }
+  onSuccess(result);
+}
+
 } // namespace nfd
 } // namespace ndn
 
diff --git a/src/management/nfd-status-dataset.cpp b/src/management/nfd-status-dataset.cpp
new file mode 100644
index 0000000..2409254
--- /dev/null
+++ b/src/management/nfd-status-dataset.cpp
@@ -0,0 +1,151 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2016 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library 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 Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "nfd-status-dataset.hpp"
+#include "../util/concepts.hpp"
+
+namespace ndn {
+namespace nfd {
+
+StatusDataset::StatusDataset(const PartialName& datasetName)
+  : m_datasetName(datasetName)
+{
+}
+
+Name
+StatusDataset::getDatasetPrefix(const Name& prefix) const
+{
+  Name name;
+  name.append(prefix).append(m_datasetName);
+  this->addParameters(name);
+  return name;
+}
+
+void
+StatusDataset::addParameters(Name& name) const
+{
+}
+
+/**
+ * \brief parses elements into a vector of T
+ * \tparam T element type
+ * \param payload pointer to a buffer of zero or more blocks of decodable by T
+ * \return a vector of T
+ * \throw tlv::Error cannot parse payload
+ */
+template<typename T>
+static std::vector<T>
+parseDatasetVector(ConstBufferPtr payload)
+{
+  BOOST_CONCEPT_ASSERT((WireDecodable<T>));
+
+  std::vector<T> result;
+
+  size_t offset = 0;
+  while (offset < payload->size()) {
+    bool isOk = false;
+    Block block;
+    std::tie(isOk, block) = Block::fromBuffer(payload, offset);
+    if (!isOk) {
+      BOOST_THROW_EXCEPTION(StatusDataset::ParseResultError("cannot decode Block"));
+    }
+
+    offset += block.size();
+    result.emplace_back(block);
+  }
+
+  return result;
+}
+
+ForwarderGeneralStatusDataset::ForwarderGeneralStatusDataset()
+  : StatusDataset("status/general")
+{
+}
+
+ForwarderGeneralStatusDataset::ResultType
+ForwarderGeneralStatusDataset::parseResult(ConstBufferPtr payload) const
+{
+  return ForwarderStatus(Block(tlv::Content, payload));
+}
+
+FaceDatasetBase::FaceDatasetBase(const PartialName& datasetName)
+  : StatusDataset(datasetName)
+{
+}
+
+FaceDatasetBase::ResultType
+FaceDatasetBase::parseResult(ConstBufferPtr payload) const
+{
+  return parseDatasetVector<FaceStatus>(payload);
+}
+
+FaceDataset::FaceDataset()
+  : FaceDatasetBase("faces/list")
+{
+}
+
+FaceQueryDataset::FaceQueryDataset(const FaceQueryFilter& filter)
+  : FaceDatasetBase("faces/query")
+  , m_filter(filter)
+{
+}
+
+void
+FaceQueryDataset::addParameters(Name& name) const
+{
+  name.append(m_filter.wireEncode());
+}
+
+FibDataset::FibDataset()
+  : StatusDataset("fib/list")
+{
+}
+
+FibDataset::ResultType
+FibDataset::parseResult(ConstBufferPtr payload) const
+{
+  return parseDatasetVector<FibEntry>(payload);
+}
+
+StrategyChoiceDataset::StrategyChoiceDataset()
+  : StatusDataset("strategy-choice/list")
+{
+}
+
+StrategyChoiceDataset::ResultType
+StrategyChoiceDataset::parseResult(ConstBufferPtr payload) const
+{
+  return parseDatasetVector<StrategyChoice>(payload);
+}
+
+RibDataset::RibDataset()
+  : StatusDataset("rib/list")
+{
+}
+
+RibDataset::ResultType
+RibDataset::parseResult(ConstBufferPtr payload) const
+{
+  return parseDatasetVector<RibEntry>(payload);
+}
+
+} // namespace nfd
+} // namespace ndn
diff --git a/src/management/nfd-status-dataset.hpp b/src/management/nfd-status-dataset.hpp
new file mode 100644
index 0000000..39537bc
--- /dev/null
+++ b/src/management/nfd-status-dataset.hpp
@@ -0,0 +1,234 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2016 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library 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 Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_MANAGEMENT_NFD_STATUS_DATASET_HPP
+#define NDN_MANAGEMENT_NFD_STATUS_DATASET_HPP
+
+#include "../name.hpp"
+#include "nfd-forwarder-status.hpp"
+#include "nfd-face-status.hpp"
+#include "nfd-face-query-filter.hpp"
+#include "nfd-fib-entry.hpp"
+#include "nfd-strategy-choice.hpp"
+#include "nfd-rib-entry.hpp"
+
+namespace ndn {
+namespace nfd {
+
+/**
+ * \ingroup management
+ * \brief base class of NFD StatusDataset
+ * \sa http://redmine.named-data.net/projects/nfd/wiki/StatusDataset
+ */
+class StatusDataset : noncopyable
+{
+public:
+#ifdef DOXYGEN
+  /**
+   * \brief if defined, specifies constructor argument type;
+   *        otherwise, constructor has no argument
+   */
+  typedef int ParamType;
+#endif
+
+  /**
+   * \brief constructs a name prefix for the dataset
+   * \param prefix top-level prefix, such as ndn:/localhost/nfd
+   * \return name prefix without version and segment components
+   */
+  Name
+  getDatasetPrefix(const Name& prefix) const;
+
+#ifdef DOXYGEN
+  /**
+   * \brief provides the result type, usually a vector
+   */
+  typedef std::vector<int> ResultType;
+#endif
+
+  /**
+   * \brief indicates reassembled payload cannot be parsed as ResultType
+   */
+  class ParseResultError : public tlv::Error
+  {
+  public:
+    explicit
+    ParseResultError(const std::string& what)
+      : tlv::Error(what)
+    {
+    }
+  };
+
+#ifdef DOXYGEN
+  /**
+   * \brief parses a result from reassembled payload
+   * \param payload reassembled payload
+   * \throw tlv::Error cannot parse payload
+   */
+  ResultType
+  parseResult(ConstBufferPtr payload) const;
+#endif
+
+protected:
+  /**
+   * \brief constructs a StatusDataset instance with given sub-prefix
+   * \param datasetName dataset name after top-level prefix, such as faces/list
+   */
+  explicit
+  StatusDataset(const PartialName& datasetName);
+
+private:
+  /**
+   * \brief appends parameters to the dataset name prefix
+   * \param[in,out] the dataset name prefix onto which parameter components can be appended
+   */
+  virtual void
+  addParameters(Name& name) const;
+
+private:
+  PartialName m_datasetName;
+};
+
+
+/**
+ * \ingroup management
+ * \brief represents a status/general dataset
+ * \sa http://redmine.named-data.net/projects/nfd/wiki/ForwarderStatus#General-Status-Dataset
+ */
+class ForwarderGeneralStatusDataset : public StatusDataset
+{
+public:
+  ForwarderGeneralStatusDataset();
+
+  typedef ForwarderStatus ResultType;
+
+  ResultType
+  parseResult(ConstBufferPtr payload) const;
+};
+
+
+/**
+ * \ingroup management
+ * \brief provides common functionality among FaceDataset and FaceQueryDataset
+ */
+class FaceDatasetBase : public StatusDataset
+{
+public:
+  typedef std::vector<FaceStatus> ResultType;
+
+  ResultType
+  parseResult(ConstBufferPtr payload) const;
+
+protected:
+  explicit
+  FaceDatasetBase(const PartialName& datasetName);
+};
+
+
+/**
+ * \ingroup management
+ * \brief represents a faces/list dataset
+ * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Face-Dataset
+ */
+class FaceDataset : public FaceDatasetBase
+{
+public:
+  FaceDataset();
+};
+
+
+/**
+ * \ingroup management
+ * \brief represents a faces/query dataset
+ * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Query-Operation
+ */
+class FaceQueryDataset : public FaceDatasetBase
+{
+public:
+  typedef FaceQueryFilter ParamType;
+
+  explicit
+  FaceQueryDataset(const FaceQueryFilter& filter);
+
+private:
+  virtual void
+  addParameters(Name& name) const override;
+
+private:
+  FaceQueryFilter m_filter;
+};
+
+
+/**
+ * \ingroup management
+ * \brief represents a fib/list dataset
+ * \sa http://redmine.named-data.net/projects/nfd/wiki/FibMgmt#FIB-Dataset
+ */
+class FibDataset : public StatusDataset
+{
+public:
+  FibDataset();
+
+  typedef std::vector<FibEntry> ResultType;
+
+  ResultType
+  parseResult(ConstBufferPtr payload) const;
+};
+
+
+/**
+ * \ingroup management
+ * \brief represents a strategy-choice/list dataset
+ * \sa http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice#Strategy-Choice-Dataset
+ */
+class StrategyChoiceDataset : public StatusDataset
+{
+public:
+  StrategyChoiceDataset();
+
+  typedef std::vector<StrategyChoice> ResultType;
+
+  ResultType
+  parseResult(ConstBufferPtr payload) const;
+};
+
+
+/**
+ * \ingroup management
+ * \brief represents a rib/list dataset
+ * \sa http://redmine.named-data.net/projects/nfd/wiki/RibMgmt#RIB-Dataset
+ */
+class RibDataset : public StatusDataset
+{
+public:
+  RibDataset();
+
+  typedef std::vector<RibEntry> ResultType;
+
+  ResultType
+  parseResult(ConstBufferPtr payload) const;
+};
+
+
+} // namespace nfd
+} // namespace ndn
+
+#endif // NDN_MANAGEMENT_NFD_STATUS_DATASET_HPP