mgmt: encode/decode CS Information Dataset

refs #4219

Change-Id: Ia51b455fc9b16f8f3c468de2847ced6d73a87bdc
diff --git a/src/encoding/tlv-nfd.hpp b/src/encoding/tlv-nfd.hpp
index 2799223..f7af459 100644
--- a/src/encoding/tlv-nfd.hpp
+++ b/src/encoding/tlv-nfd.hpp
@@ -80,6 +80,11 @@
   NInBytes      = 148,
   NOutBytes     = 149,
 
+  // Content Store Management
+  CsInfo  = 128,
+  NHits   = 129,
+  NMisses = 130,
+
   // FIB Management
   FibEntry      = 128,
   NextHopRecord = 129,
diff --git a/src/mgmt/nfd/cs-info.cpp b/src/mgmt/nfd/cs-info.cpp
new file mode 100644
index 0000000..b776c53
--- /dev/null
+++ b/src/mgmt/nfd/cs-info.cpp
@@ -0,0 +1,137 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2017 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 "cs-info.hpp"
+#include "encoding/block-helpers.hpp"
+#include "encoding/encoding-buffer.hpp"
+#include "encoding/tlv-nfd.hpp"
+#include "util/concepts.hpp"
+
+namespace ndn {
+namespace nfd {
+
+BOOST_CONCEPT_ASSERT((StatusDatasetItem<CsInfo>));
+
+CsInfo::CsInfo()
+  : m_nHits(0)
+  , m_nMisses(0)
+{
+}
+
+CsInfo::CsInfo(const Block& block)
+{
+  this->wireDecode(block);
+}
+
+template<encoding::Tag TAG>
+size_t
+CsInfo::wireEncode(EncodingImpl<TAG>& encoder) const
+{
+  size_t totalLength = 0;
+
+  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NMisses, m_nMisses);
+  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NHits, m_nHits);
+
+  totalLength += encoder.prependVarNumber(totalLength);
+  totalLength += encoder.prependVarNumber(tlv::nfd::CsInfo);
+  return totalLength;
+}
+
+NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(CsInfo);
+
+const Block&
+CsInfo::wireEncode() const
+{
+  if (m_wire.hasWire())
+    return m_wire;
+
+  EncodingEstimator estimator;
+  size_t estimatedSize = wireEncode(estimator);
+
+  EncodingBuffer buffer(estimatedSize, 0);
+  wireEncode(buffer);
+
+  m_wire = buffer.block();
+  return m_wire;
+}
+
+void
+CsInfo::wireDecode(const Block& block)
+{
+  if (block.type() != tlv::nfd::CsInfo) {
+    BOOST_THROW_EXCEPTION(Error("expecting CsInfo block, got " + to_string(block.type())));
+  }
+  m_wire = block;
+  m_wire.parse();
+  auto val = m_wire.elements_begin();
+
+  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NHits) {
+    m_nHits = readNonNegativeInteger(*val);
+    ++val;
+  }
+  else {
+    BOOST_THROW_EXCEPTION(Error("missing required NHits field"));
+  }
+
+  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NMisses) {
+    m_nMisses = readNonNegativeInteger(*val);
+    ++val;
+  }
+  else {
+    BOOST_THROW_EXCEPTION(Error("missing required NMisses field"));
+  }
+}
+
+CsInfo&
+CsInfo::setNHits(uint64_t nHits)
+{
+  m_wire.reset();
+  m_nHits = nHits;
+  return *this;
+}
+
+CsInfo&
+CsInfo::setNMisses(uint64_t nMisses)
+{
+  m_wire.reset();
+  m_nMisses = nMisses;
+  return *this;
+}
+
+bool
+operator==(const CsInfo& a, const CsInfo& b)
+{
+  return a.getNHits() == b.getNHits() &&
+         a.getNMisses() == b.getNMisses();
+}
+
+std::ostream&
+operator<<(std::ostream& os, const CsInfo& status)
+{
+  os << "CS: "
+     << status.getNHits() << (status.getNHits() == 1 ? " hit" : " hits")
+     << ", "
+     << status.getNMisses() << (status.getNMisses() == 1 ? " miss" : " misses");
+  return os;
+}
+
+} // namespace nfd
+} // namespace ndn
diff --git a/src/mgmt/nfd/cs-info.hpp b/src/mgmt/nfd/cs-info.hpp
new file mode 100644
index 0000000..0b9ad0a
--- /dev/null
+++ b/src/mgmt/nfd/cs-info.hpp
@@ -0,0 +1,104 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2017 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_MGMT_NFD_CS_INFO_HPP
+#define NDN_MGMT_NFD_CS_INFO_HPP
+
+#include "../../encoding/block.hpp"
+
+namespace ndn {
+namespace nfd {
+
+/**
+ * \ingroup management
+ * \brief represents the CS Information dataset
+ * \sa https://redmine.named-data.net/projects/nfd/wiki/CsMgmt#CS-Information-Dataset
+ */
+class CsInfo
+{
+public:
+  class Error : public tlv::Error
+  {
+  public:
+    explicit
+    Error(const std::string& what)
+      : tlv::Error(what)
+    {
+    }
+  };
+
+  CsInfo();
+
+  explicit
+  CsInfo(const Block& block);
+
+  template<encoding::Tag TAG>
+  size_t
+  wireEncode(EncodingImpl<TAG>& encoder) const;
+
+  const Block&
+  wireEncode() const;
+
+  void
+  wireDecode(const Block& wire);
+
+  uint64_t
+  getNHits() const
+  {
+    return m_nHits;
+  }
+
+  CsInfo&
+  setNHits(uint64_t nHits);
+
+  uint64_t
+  getNMisses() const
+  {
+    return m_nMisses;
+  }
+
+  CsInfo&
+  setNMisses(uint64_t nMisses);
+
+private:
+  uint64_t m_nHits;
+  uint64_t m_nMisses;
+  mutable Block m_wire;
+};
+
+NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(CsInfo);
+
+bool
+operator==(const CsInfo& a, const CsInfo& b);
+
+inline bool
+operator!=(const CsInfo& a, const CsInfo& b)
+{
+  return !(a == b);
+}
+
+std::ostream&
+operator<<(std::ostream& os, const CsInfo& status);
+
+} // namespace nfd
+} // namespace ndn
+
+#endif // NDN_MGMT_NFD_CS_INFO_HPP
diff --git a/src/mgmt/nfd/status-dataset.cpp b/src/mgmt/nfd/status-dataset.cpp
index cc0d6ea..5b6d66d 100644
--- a/src/mgmt/nfd/status-dataset.cpp
+++ b/src/mgmt/nfd/status-dataset.cpp
@@ -1,5 +1,5 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
  * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -138,6 +138,17 @@
   return parseDatasetVector<FibEntry>(payload);
 }
 
+CsInfoDataset::CsInfoDataset()
+  : StatusDataset("cs/info")
+{
+}
+
+CsInfoDataset::ResultType
+CsInfoDataset::parseResult(ConstBufferPtr payload) const
+{
+  return CsInfo(Block(payload));
+}
+
 StrategyChoiceDataset::StrategyChoiceDataset()
   : StatusDataset("strategy-choice/list")
 {
diff --git a/src/mgmt/nfd/status-dataset.hpp b/src/mgmt/nfd/status-dataset.hpp
index f3d2c5e..f7f7917 100644
--- a/src/mgmt/nfd/status-dataset.hpp
+++ b/src/mgmt/nfd/status-dataset.hpp
@@ -1,5 +1,5 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
  * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -28,6 +28,7 @@
 #include "face-query-filter.hpp"
 #include "channel-status.hpp"
 #include "fib-entry.hpp"
+#include "cs-info.hpp"
 #include "strategy-choice.hpp"
 #include "rib-entry.hpp"
 
@@ -50,7 +51,7 @@
    * \brief if defined, specifies constructor argument type;
    *        otherwise, constructor has no argument
    */
-  typedef int ParamType;
+  using ParamType = int;
 #endif
 
   /**
@@ -65,7 +66,7 @@
   /**
    * \brief provides the result type, usually a vector
    */
-  typedef std::vector<int> ResultType;
+  using ResultType = std::vector<int>;
 #endif
 
   /**
@@ -111,7 +112,6 @@
   PartialName m_datasetName;
 };
 
-
 /**
  * \ingroup management
  * \brief represents a status/general dataset
@@ -122,13 +122,12 @@
 public:
   ForwarderGeneralStatusDataset();
 
-  typedef ForwarderStatus ResultType;
+  using ResultType = ForwarderStatus;
 
   ResultType
   parseResult(ConstBufferPtr payload) const;
 };
 
-
 /**
  * \ingroup management
  * \brief provides common functionality among FaceDataset and FaceQueryDataset
@@ -136,7 +135,7 @@
 class FaceDatasetBase : public StatusDataset
 {
 public:
-  typedef std::vector<FaceStatus> ResultType;
+  using ResultType = std::vector<FaceStatus>;
 
   ResultType
   parseResult(ConstBufferPtr payload) const;
@@ -146,7 +145,6 @@
   FaceDatasetBase(const PartialName& datasetName);
 };
 
-
 /**
  * \ingroup management
  * \brief represents a faces/list dataset
@@ -158,7 +156,6 @@
   FaceDataset();
 };
 
-
 /**
  * \ingroup management
  * \brief represents a faces/query dataset
@@ -167,7 +164,7 @@
 class FaceQueryDataset : public FaceDatasetBase
 {
 public:
-  typedef FaceQueryFilter ParamType;
+  using ParamType = FaceQueryFilter;
 
   explicit
   FaceQueryDataset(const FaceQueryFilter& filter);
@@ -180,7 +177,6 @@
   FaceQueryFilter m_filter;
 };
 
-
 /**
  * \ingroup management
  * \brief represents a faces/channels dataset
@@ -191,13 +187,12 @@
 public:
   ChannelDataset();
 
-  typedef std::vector<ChannelStatus> ResultType;
+  using ResultType = std::vector<ChannelStatus>;
 
   ResultType
   parseResult(ConstBufferPtr payload) const;
 };
 
-
 /**
  * \ingroup management
  * \brief represents a fib/list dataset
@@ -208,12 +203,27 @@
 public:
   FibDataset();
 
-  typedef std::vector<FibEntry> ResultType;
+  using ResultType = std::vector<FibEntry>;
 
   ResultType
   parseResult(ConstBufferPtr payload) const;
 };
 
+/**
+ * \ingroup management
+ * \brief represents a cs/info dataset
+ * \sa https://redmine.named-data.net/projects/nfd/wiki/CsMgmt#CS-Information-Dataset
+ */
+class CsInfoDataset : public StatusDataset
+{
+public:
+  CsInfoDataset();
+
+  using ResultType = CsInfo;
+
+  ResultType
+  parseResult(ConstBufferPtr payload) const;
+};
 
 /**
  * \ingroup management
@@ -225,13 +235,12 @@
 public:
   StrategyChoiceDataset();
 
-  typedef std::vector<StrategyChoice> ResultType;
+  using ResultType = std::vector<StrategyChoice>;
 
   ResultType
   parseResult(ConstBufferPtr payload) const;
 };
 
-
 /**
  * \ingroup management
  * \brief represents a rib/list dataset
@@ -242,13 +251,12 @@
 public:
   RibDataset();
 
-  typedef std::vector<RibEntry> ResultType;
+  using ResultType = std::vector<RibEntry>;
 
   ResultType
   parseResult(ConstBufferPtr payload) const;
 };
 
-
 } // namespace nfd
 } // namespace ndn
 
diff --git a/tests/unit-tests/mgmt/nfd/cs-info.t.cpp b/tests/unit-tests/mgmt/nfd/cs-info.t.cpp
new file mode 100644
index 0000000..6e55b85
--- /dev/null
+++ b/tests/unit-tests/mgmt/nfd/cs-info.t.cpp
@@ -0,0 +1,91 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2017 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 "mgmt/nfd/cs-info.hpp"
+
+#include "boost-test.hpp"
+#include <boost/lexical_cast.hpp>
+
+namespace ndn {
+namespace nfd {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(Mgmt)
+BOOST_AUTO_TEST_SUITE(Nfd)
+BOOST_AUTO_TEST_SUITE(TestCsInfo)
+
+static CsInfo
+makeCsInfo()
+{
+  return CsInfo()
+    .setNHits(12951)
+    .setNMisses(28179);
+}
+
+BOOST_AUTO_TEST_CASE(Encode)
+{
+  CsInfo csi1 = makeCsInfo();
+  Block wire = csi1.wireEncode();
+
+  static const uint8_t EXPECTED[] = {
+    0x80, 0x08, // CsInfo
+          0x81, 0x02, 0x32, 0x97, // NHits
+          0x82, 0x02, 0x6E, 0x13, // NMisses
+  };
+  BOOST_CHECK_EQUAL_COLLECTIONS(wire.begin(), wire.end(), EXPECTED, EXPECTED + sizeof(EXPECTED));
+
+  CsInfo csi2(wire);
+  BOOST_CHECK_EQUAL(csi1, csi2);
+}
+
+BOOST_AUTO_TEST_CASE(Equality)
+{
+  CsInfo csi1, csi2;
+  BOOST_CHECK_EQUAL(csi1, csi2);
+
+  csi1 = makeCsInfo();
+  BOOST_CHECK_NE(csi1, csi2);
+  csi2 = csi1;
+  BOOST_CHECK_EQUAL(csi1, csi2);
+
+  csi2.setNHits(8267);
+  BOOST_CHECK_NE(csi1, csi2);
+}
+
+BOOST_AUTO_TEST_CASE(Print)
+{
+  CsInfo csi;
+  BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(csi), "CS: 0 hits, 0 misses");
+
+  csi = makeCsInfo();
+  BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(csi), "CS: 12951 hits, 28179 misses");
+
+  csi.setNHits(1).setNMisses(1);
+  BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(csi), "CS: 1 hit, 1 miss");
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestCsInfo
+BOOST_AUTO_TEST_SUITE_END() // Nfd
+BOOST_AUTO_TEST_SUITE_END() // Mgmt
+
+} // namespace tests
+} // namespace nfd
+} // namespace ndn
diff --git a/tests/unit-tests/mgmt/nfd/status-dataset.t.cpp b/tests/unit-tests/mgmt/nfd/status-dataset.t.cpp
index 13cd203..990f7f1 100644
--- a/tests/unit-tests/mgmt/nfd/status-dataset.t.cpp
+++ b/tests/unit-tests/mgmt/nfd/status-dataset.t.cpp
@@ -378,6 +378,28 @@
   BOOST_CHECK_EQUAL(failCodes.size(), 0);
 }
 
+BOOST_AUTO_TEST_CASE(CsInfo)
+{
+  using ndn::nfd::CsInfo;
+
+  bool hasResult = false;
+  controller.fetch<CsInfoDataset>(
+    [&hasResult] (const CsInfo& result) {
+      hasResult = true;
+      BOOST_CHECK_EQUAL(result.getNHits(), 4539);
+    },
+    datasetFailCallback);
+  this->advanceClocks(time::milliseconds(500));
+
+  CsInfo payload;
+  payload.setNHits(4539);
+  this->sendDataset("/localhost/nfd/cs/info", payload);
+  this->advanceClocks(time::milliseconds(500));
+
+  BOOST_CHECK(hasResult);
+  BOOST_CHECK_EQUAL(failCodes.size(), 0);
+}
+
 BOOST_AUTO_TEST_CASE(StrategyChoiceList)
 {
   bool hasResult = false;