lp: implement CachePolicy and CachePolicyType

refs #2963

Change-Id: I343ff95eb4ae61a4bc29603e63647b219661542c
diff --git a/src/lp/cache-policy.cpp b/src/lp/cache-policy.cpp
new file mode 100644
index 0000000..6bd1be9
--- /dev/null
+++ b/src/lp/cache-policy.cpp
@@ -0,0 +1,139 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2015 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.
+ *
+ * @author Eric Newberry <enewberry@email.arizona.edu>
+ */
+
+#include "cache-policy.hpp"
+
+namespace ndn {
+namespace lp {
+
+std::ostream&
+operator<<(std::ostream& os, CachePolicyType policy)
+{
+  switch (policy) {
+  case CachePolicyType::NO_CACHE:
+    os << "NoCache";
+    break;
+  default:
+    os << "None";
+    break;
+  }
+
+  return os;
+}
+
+CachePolicy::CachePolicy()
+  : m_policy(CachePolicyType::NONE)
+{
+}
+
+CachePolicy::CachePolicy(const Block& block)
+{
+  wireDecode(block);
+}
+
+template<encoding::Tag TAG>
+size_t
+CachePolicy::wireEncode(EncodingImpl<TAG>& encoder) const
+{
+  if (m_policy == CachePolicyType::NONE) {
+    throw Error("CachePolicyType must be set");
+  }
+  size_t length = 0;
+  length += prependNonNegativeIntegerBlock(encoder, tlv::CachePolicyType,
+                                           static_cast<uint32_t>(m_policy));
+  length += encoder.prependVarNumber(length);
+  length += encoder.prependVarNumber(tlv::CachePolicy);
+  return length;
+}
+
+template size_t
+CachePolicy::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
+
+template size_t
+CachePolicy::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
+
+const Block&
+CachePolicy::wireEncode() const
+{
+  if (m_policy == CachePolicyType::NONE) {
+    throw Error("CachePolicyType must be set");
+  }
+
+  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
+CachePolicy::wireDecode(const Block& wire)
+{
+  if (wire.type() != tlv::CachePolicy) {
+    throw Error("expecting CachePolicy block");
+  }
+
+  m_wire = wire;
+  m_wire.parse();
+
+  Block::element_const_iterator it = m_wire.elements_begin();
+  if (it != m_wire.elements_end() && it->type() == tlv::CachePolicyType) {
+    m_policy = static_cast<CachePolicyType>(readNonNegativeInteger(*it));
+    if (this->getPolicy() == CachePolicyType::NONE) {
+      throw Error("unknown CachePolicyType");
+    }
+  }
+  else {
+    throw Error("expecting CachePolicyType block");
+  }
+}
+
+CachePolicyType
+CachePolicy::getPolicy() const
+{
+  switch (m_policy) {
+  case CachePolicyType::NO_CACHE:
+    return m_policy;
+  default:
+    return CachePolicyType::NONE;
+  }
+}
+
+CachePolicy&
+CachePolicy::setPolicy(CachePolicyType policy)
+{
+  m_policy = policy;
+  m_wire.reset();
+  return *this;
+}
+
+} // namespace lp
+} // namespace ndn
diff --git a/src/lp/cache-policy.hpp b/src/lp/cache-policy.hpp
new file mode 100644
index 0000000..a67a9c3
--- /dev/null
+++ b/src/lp/cache-policy.hpp
@@ -0,0 +1,112 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2015 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.
+ *
+ * @author Eric Newberry <enewberry@email.arizona.edu>
+ */
+
+#ifndef NDN_CXX_LP_CACHE_POLICY_HPP
+#define NDN_CXX_LP_CACHE_POLICY_HPP
+
+#include "../common.hpp"
+#include "../encoding/encoding-buffer.hpp"
+#include "../encoding/block-helpers.hpp"
+
+#include "tlv.hpp"
+
+namespace ndn {
+namespace lp {
+
+/**
+ * \brief indicates the cache policy applied to a Data packet
+ */
+enum class CachePolicyType {
+  NONE = 0,
+  NO_CACHE = 1
+};
+
+std::ostream&
+operator<<(std::ostream& os, CachePolicyType policy);
+
+/**
+ * \brief represents a CachePolicy header field
+ */
+class CachePolicy
+{
+public:
+  class Error : public ndn::tlv::Error
+  {
+  public:
+    explicit
+    Error(const std::string& what)
+      : ndn::tlv::Error(what)
+    {
+    }
+  };
+
+  CachePolicy();
+
+  explicit
+  CachePolicy(const Block& block);
+
+  /**
+   * \brief prepend CachePolicy to encoder
+   * \pre getPolicy() != CachePolicyType::NONE
+   * \throw Error policy type is unset
+   */
+  template<encoding::Tag TAG>
+  size_t
+  wireEncode(EncodingImpl<TAG>& encoder) const;
+
+  /**
+   * \brief encode CachePolicy into wire format
+   */
+  const Block&
+  wireEncode() const;
+
+  /**
+   * \brief get CachePolicyType from wire format
+   */
+  void
+  wireDecode(const Block& wire);
+
+public: // policy type
+  /**
+   * \return policy type code
+   * \retval CachePolicyType::NONE if policy type is unset or has an unknown code
+   */
+  CachePolicyType
+  getPolicy() const;
+
+  /**
+   * \brief set policy type code
+   * \param policy a policy type code; CachePolicyType::NONE clears the policy
+   */
+  CachePolicy&
+  setPolicy(CachePolicyType policy);
+
+private:
+  CachePolicyType m_policy;
+  mutable Block m_wire;
+};
+
+} // namespace lp
+} // namespace ndn
+
+#endif // NDN_CXX_LP_CACHE_POLICY_HPP
\ No newline at end of file
diff --git a/tests/unit-tests/lp/cache-policy.t.cpp b/tests/unit-tests/lp/cache-policy.t.cpp
new file mode 100644
index 0000000..8d249dc
--- /dev/null
+++ b/tests/unit-tests/lp/cache-policy.t.cpp
@@ -0,0 +1,104 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2015 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.
+ *
+ * @author Eric Newberry <enewberry@email.arizona.edu>
+ */
+
+#include "lp/cache-policy.hpp"
+
+#include "boost-test.hpp"
+
+namespace ndn {
+namespace lp {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(LpCachePoicy)
+
+BOOST_AUTO_TEST_CASE(Encode)
+{
+  CachePolicy policy;
+  policy.setPolicy(CachePolicyType::NO_CACHE);
+
+  Block wire;
+  BOOST_REQUIRE_NO_THROW(wire = policy.wireEncode());
+
+  // Sample encoded value obtained with:
+  // for (Buffer::const_iterator it = wire.begin(); it != wire.end(); ++it) {
+  //   printf("0x%02x, ", *it);
+  // }
+
+  // Contains CachePolicyType::NO_CACHE
+  static const uint8_t expectedBlock[] = {
+    0xfd, 0x03, 0x34, 0x05, 0xfd, 0x03, 0x35, 0x01, 0x01
+  };
+
+  BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
+                                wire.begin(), wire.end());
+
+  BOOST_REQUIRE_NO_THROW(policy.wireDecode(wire));
+}
+
+BOOST_AUTO_TEST_CASE(DecodeUnknownPolicyError)
+{
+  static const uint8_t expectedBlock[] = {
+    0xfd, 0x03, 0x34, 0x08, 0xfd, 0x03, 0x35, 0x04, 0xff, 0xff, 0xff, 0xff
+  };
+
+  CachePolicy policy;
+  Block wire(expectedBlock, sizeof(expectedBlock));
+  BOOST_REQUIRE_THROW(policy.wireDecode(wire), CachePolicy::Error);
+}
+
+BOOST_AUTO_TEST_CASE(DecodeMissingPolicyError)
+{
+  static const uint8_t inputBlock[] = {
+    0xfd, 0x03, 0x34, 0x00
+  };
+
+  CachePolicy policy;
+  Block wire(inputBlock, sizeof(inputBlock));
+  BOOST_REQUIRE_THROW(policy.wireDecode(wire), CachePolicy::Error);
+}
+
+BOOST_AUTO_TEST_CASE(DecodeInvalidPolicyError)
+{
+  static const uint8_t inputBlock[] = {
+    0xfd, 0x03, 0x34, 0x05, 0xfd, 0x03, 0x35, 0x01, 0x00
+  };
+
+  CachePolicy policy;
+  Block wire(inputBlock, sizeof(inputBlock));
+  BOOST_REQUIRE_THROW(policy.wireDecode(wire), CachePolicy::Error);
+}
+
+BOOST_AUTO_TEST_CASE(Policy)
+{
+  CachePolicy policy;
+  BOOST_CHECK_EQUAL(policy.getPolicy(), CachePolicyType::NONE);
+
+  policy.setPolicy(CachePolicyType::NO_CACHE);
+  BOOST_CHECK_EQUAL(policy.getPolicy(), CachePolicyType::NO_CACHE);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace lp
+} // namespace ndn
\ No newline at end of file