management: Adding nfd::FaceStatus data structure

Change-Id: I45585a17a4163931c38ece5621d12489aa6a3b73
Refs: #1328
diff --git a/src/management/nfd-face-status.hpp b/src/management/nfd-face-status.hpp
new file mode 100644
index 0000000..f3abdb5
--- /dev/null
+++ b/src/management/nfd-face-status.hpp
@@ -0,0 +1,229 @@
+/* -*- 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 NDN_MANAGEMENT_NFD_FACE_STATUS_HPP
+#define NDN_MANAGEMENT_NFD_FACE_STATUS_HPP
+
+#include "../encoding/encoding-buffer.hpp"
+#include "../encoding/tlv-nfd.hpp"
+
+namespace ndn {
+namespace nfd {
+
+class FaceStatus
+{
+public:
+  class Error : public Tlv::Error
+  {
+  public:
+    Error(const std::string& what) : Tlv::Error(what) { }
+  };
+
+  FaceStatus(const uint64_t faceId,
+             const std::string& uri,
+             uint64_t inInterest, uint64_t inData, uint64_t outInterest, uint64_t outData);
+
+  explicit
+  FaceStatus(const Block& block);
+
+  uint64_t
+  getFaceId() const
+  {
+    return m_faceId;
+  }
+
+  const std::string&
+  getUri() const
+  {
+    return m_uri;
+  }
+
+  const uint64_t
+  getInInterest() const
+  {
+    return m_inInterest;
+  }
+
+  const uint64_t
+  getInData() const
+  {
+    return m_inData;
+  }
+
+  const uint64_t
+  getOutInterest() const
+  {
+    return m_outInterest;
+  }
+
+  const uint64_t
+  getOutData() const
+  {
+    return m_outData;
+  }
+
+  template<bool T>
+  size_t
+  wireEncode(EncodingImpl<T>& buffer) const;
+
+  const Block&
+  wireEncode() const;
+
+  void
+  wireDecode(const Block& wire);
+
+private:
+  uint64_t m_faceId;
+  std::string m_uri;
+  uint64_t m_inInterest;
+  uint64_t m_inData;
+  uint64_t m_outInterest;
+  uint64_t m_outData;
+
+  mutable Block m_wire;
+};
+
+inline
+FaceStatus::FaceStatus(const uint64_t faceId,
+             const std::string& uri,
+             uint64_t inInterest, uint64_t inData, uint64_t outInterest, uint64_t outData)
+  : m_faceId(faceId)
+  , m_uri(uri)
+  , m_inInterest(inInterest)
+  , m_inData(inData)
+  , m_outInterest(outInterest)
+  , m_outData(outData)
+{
+}
+
+inline
+FaceStatus::FaceStatus(const Block& block)
+{
+  wireDecode(block);
+}
+
+template<bool T>
+size_t
+FaceStatus::wireEncode(EncodingImpl<T>& buffer) const
+{
+  size_t totalLength = 0;
+
+  totalLength += prependNonNegativeIntegerBlock(buffer,
+                                                tlv::nfd::TotalOutgoingDataCounter,
+                                                m_outData);
+
+  totalLength += prependNonNegativeIntegerBlock(buffer,
+                                                tlv::nfd::TotalOutgoingInterestCounter,
+                                                m_outInterest);
+
+  totalLength += prependNonNegativeIntegerBlock(buffer,
+                                                tlv::nfd::TotalIncomingDataCounter,
+                                                m_inData);
+
+  totalLength += prependNonNegativeIntegerBlock(buffer,
+                                                tlv::nfd::TotalIncomingInterestCounter,
+                                                m_inInterest);
+
+  totalLength += prependByteArrayBlock(buffer,
+                                       tlv::nfd::Uri,
+                                       reinterpret_cast<const uint8_t*>(m_uri.c_str()),
+                                       m_uri.size());
+
+  totalLength += prependNonNegativeIntegerBlock(buffer,
+                                                tlv::nfd::FaceId,
+                                                m_faceId);
+
+  totalLength += buffer.prependVarNumber(totalLength);
+  totalLength += buffer.prependVarNumber(tlv::nfd::FaceStatus);
+
+  return totalLength;
+}
+
+const Block&
+FaceStatus::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;
+}
+
+inline void
+FaceStatus::wireDecode(const Block &wire)
+{
+  m_wire = wire;
+
+  if (m_wire.type() != tlv::nfd::FaceStatus)
+    throw Error("Requested decoding of FaceStatus, but Block is of different type");
+
+  m_wire.parse();
+
+  // FaceKind
+  Block::element_const_iterator val = m_wire.elements_begin();
+  if (val == m_wire.elements_end() || val->type() != tlv::nfd::FaceId)
+    throw Error("Missing required FaceId block");
+  m_faceId = readNonNegativeInteger(*val);
+
+  // URI
+  ++val;
+  if (val == m_wire.elements_end() || val->type() != tlv::nfd::Uri)
+    throw Error("Missing required Uri block");
+  m_uri = std::string(reinterpret_cast<const char*>(val->value()), val->value_size());
+
+  // TotalIncomingInterestCounter
+  ++val;
+  if (val == m_wire.elements_end() || val->type() != tlv::nfd::TotalIncomingInterestCounter)
+    throw Error("Missing required FaceId block");
+  m_inInterest = readNonNegativeInteger(*val);
+
+  // TotalIncomingDataCounter
+  ++val;
+  if (val == m_wire.elements_end() || val->type() != tlv::nfd::TotalIncomingDataCounter)
+    throw Error("Missing required FaceId block");
+  m_inData = readNonNegativeInteger(*val);
+
+  // TotalOutgoingInterestCounter
+  ++val;
+  if (val == m_wire.elements_end() || val->type() != tlv::nfd::TotalOutgoingInterestCounter)
+    throw Error("Missing required FaceId block");
+  m_outInterest = readNonNegativeInteger(*val);
+
+  // TotalOutgoingDataCounter
+  ++val;
+  if (val == m_wire.elements_end() || val->type() != tlv::nfd::TotalOutgoingDataCounter)
+    throw Error("Missing required FaceId block");
+  m_outData = readNonNegativeInteger(*val);
+}
+
+inline std::ostream&
+operator << (std::ostream& os, const FaceStatus& status)
+{
+  os << "FaceStatus(";
+
+  // FaceID
+  os << "FaceID: " << status.getFaceId() << ", ";
+
+  // URI
+  os << "Uri: " << status.getUri() << ", ";
+
+  os << "Counters: " << status.getInInterest() << "|" << status.getInData()
+     << "|" << status.getOutInterest() << "|" << status.getOutData();
+
+  os << ")";
+  return os;
+}
+
+} // namespace nfd
+} // namespace ndn
+
+#endif // NDN_MANAGEMENT_NFD_FACE_EVENT_STATUS_HPP
diff --git a/tests/management/test-nfd-control.cpp b/tests/management/test-nfd-control.cpp
index 299620e..68df397 100644
--- a/tests/management/test-nfd-control.cpp
+++ b/tests/management/test-nfd-control.cpp
@@ -8,6 +8,7 @@
 #include "management/nfd-fib-management-options.hpp"
 #include "management/nfd-face-management-options.hpp"
 #include "management/nfd-face-event-notification.hpp"
+#include "management/nfd-face-status.hpp"
 
 #include <boost/test/unit_test.hpp>
 #include <boost/test/output_test_stream.hpp>
@@ -46,6 +47,13 @@
   0x2e, 0x31, 0x3a, 0x36, 0x33, 0x36, 0x33
 };
 
+const uint8_t TestFaceStatus[] = {
+  0x80, 0x27, 0x69, 0x01, 0x64, 0x72, 0x15, 0x74, 0x63, 0x70, 0x34, 0x3a,
+  0x2f, 0x2f, 0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x3a,
+  0x36, 0x33, 0x36, 0x33, 0x91, 0x01, 0x0a, 0x90, 0x01, 0x14, 0x92, 0x01,
+  0x1e, 0x93, 0x02, 0x01, 0x90
+};
+
 // ControlResponse
 
 BOOST_AUTO_TEST_CASE(ControlResponseEncode)
@@ -182,6 +190,38 @@
   }
 }
 
+BOOST_AUTO_TEST_CASE(FaceStatusEncodingDecoding)
+{
+  {
+    FaceStatus faceStatus(100, "tcp4://127.0.0.1:6363", 10, 20, 30, 400);
+    BOOST_REQUIRE_NO_THROW(faceStatus.wireEncode());
+
+    BOOST_CHECK_EQUAL_COLLECTIONS(TestFaceStatus,
+                                  TestFaceStatus + sizeof(TestFaceStatus),
+                                  faceStatus.wireEncode().begin(), faceStatus.wireEncode().end());
+
+    std::ostringstream os;
+    os << faceStatus;
+    BOOST_CHECK_EQUAL(os.str(), "FaceStatus(FaceID: 100, Uri: tcp4://127.0.0.1:6363, "
+                      "Counters: 10|20|30|400)");
+  }
+
+  {
+    Block block(TestFaceStatus, sizeof(TestFaceStatus));
+    BOOST_REQUIRE_NO_THROW((FaceStatus(block)));
+
+    FaceStatus faceStatus(block);
+
+    BOOST_CHECK_EQUAL(faceStatus.getFaceId(), 100);
+    BOOST_CHECK_EQUAL(faceStatus.getUri(), "tcp4://127.0.0.1:6363");
+
+    BOOST_CHECK_EQUAL(faceStatus.getInInterest(),  10);
+    BOOST_CHECK_EQUAL(faceStatus.getInData(),      20);
+    BOOST_CHECK_EQUAL(faceStatus.getOutInterest(), 30);
+    BOOST_CHECK_EQUAL(faceStatus.getOutData(),     400);
+  }
+}
+
 BOOST_AUTO_TEST_SUITE_END()
 
 } // namespace nfd