mgmt: add Flags field to FaceStatus

refs #3732

Change-Id: I933c1471a185fd96777e6935c6810210868b0765
diff --git a/src/mgmt/nfd/face-status.cpp b/src/mgmt/nfd/face-status.cpp
index bdc7404..584208c 100644
--- a/src/mgmt/nfd/face-status.cpp
+++ b/src/mgmt/nfd/face-status.cpp
@@ -43,6 +43,7 @@
   , m_nOutNacks(0)
   , m_nInBytes(0)
   , m_nOutBytes(0)
+  , m_flags(0)
 {
 }
 
@@ -58,6 +59,8 @@
   size_t totalLength = 0;
 
   totalLength += prependNonNegativeIntegerBlock(encoder,
+                 tlv::nfd::Flags, m_flags);
+  totalLength += prependNonNegativeIntegerBlock(encoder,
                  tlv::nfd::NOutBytes, m_nOutBytes);
   totalLength += prependNonNegativeIntegerBlock(encoder,
                  tlv::nfd::NInBytes, m_nInBytes);
@@ -248,6 +251,14 @@
   else {
     BOOST_THROW_EXCEPTION(Error("missing required NOutBytes field"));
   }
+
+  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) {
+    m_flags = readNonNegativeInteger(*val);
+    ++val;
+  }
+  else {
+    BOOST_THROW_EXCEPTION(Error("missing required Flags field"));
+  }
 }
 
 FaceStatus&
@@ -323,6 +334,43 @@
   return *this;
 }
 
+FaceStatus&
+FaceStatus::setFlags(uint64_t flags)
+{
+  m_wire.reset();
+  m_flags = flags;
+  return *this;
+}
+
+bool
+FaceStatus::getFlagBit(size_t bit) const
+{
+  if (bit >= 64) {
+    BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
+  }
+
+  return m_flags & (1 << bit);
+}
+
+FaceStatus&
+FaceStatus::setFlagBit(size_t bit, bool value)
+{
+  if (bit >= 64) {
+    BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
+  }
+
+  m_wire.reset();
+
+  if (value) {
+    m_flags |= (1 << bit);
+  }
+  else {
+    m_flags &= ~(1 << bit);
+  }
+
+  return *this;
+}
+
 void
 FaceStatus::wireReset() const
 {
@@ -346,8 +394,13 @@
 
   os << "FaceScope: " << status.getFaceScope() << ",\n"
      << "FacePersistency: " << status.getFacePersistency() << ",\n"
-     << "LinkType: " << status.getLinkType() << ",\n"
-     << "Counters: { Interests: {in: " << status.getNInInterests() << ", "
+     << "LinkType: " << status.getLinkType() << ",\n";
+
+  auto osFlags = os.flags();
+  os << "Flags: " << std::showbase << std::hex << status.getFlags() << ",\n";
+  os.flags(osFlags);
+
+  os << "Counters: { Interests: {in: " << status.getNInInterests() << ", "
      << "out: " << status.getNOutInterests() << "},\n"
      << "            Data: {in: " << status.getNInDatas() << ", "
      << "out: " << status.getNOutDatas() << "},\n"
diff --git a/src/mgmt/nfd/face-status.hpp b/src/mgmt/nfd/face-status.hpp
index 4f20e53..a2ea259 100644
--- a/src/mgmt/nfd/face-status.hpp
+++ b/src/mgmt/nfd/face-status.hpp
@@ -147,6 +147,21 @@
   FaceStatus&
   setNOutBytes(uint64_t nOutBytes);
 
+  uint64_t
+  getFlags() const
+  {
+    return m_flags;
+  }
+
+  FaceStatus&
+  setFlags(uint64_t flags);
+
+  bool
+  getFlagBit(size_t bit) const;
+
+  FaceStatus&
+  setFlagBit(size_t bit, bool value);
+
 protected:
   void
   wireReset() const;
@@ -162,6 +177,7 @@
   uint64_t m_nOutNacks;
   uint64_t m_nInBytes;
   uint64_t m_nOutBytes;
+  uint64_t m_flags;
 
   mutable Block m_wire;
 };
diff --git a/tests/unit-tests/mgmt/nfd/face-status.t.cpp b/tests/unit-tests/mgmt/nfd/face-status.t.cpp
index c8a97f3..9f7118d 100644
--- a/tests/unit-tests/mgmt/nfd/face-status.t.cpp
+++ b/tests/unit-tests/mgmt/nfd/face-status.t.cpp
@@ -48,7 +48,8 @@
          .setNOutDatas(4)
          .setNOutNacks(2)
          .setNInBytes(1329719163)
-         .setNOutBytes(999110448);
+         .setNOutBytes(999110448)
+         .setFlags(0x7);
 
   Block wire;
   BOOST_REQUIRE_NO_THROW(wire = status1.wireEncode());
@@ -59,7 +60,7 @@
   //  printf("0x%02x, ", *it);
   // }
   static const uint8_t expected[] = {
-    0x80, 0x5e, 0x69, 0x01, 0x64, 0x72, 0x15, 0x74, 0x63, 0x70,
+    0x80, 0x61, 0x69, 0x01, 0x64, 0x72, 0x15, 0x74, 0x63, 0x70,
     0x34, 0x3a, 0x2f, 0x2f, 0x31, 0x39, 0x32, 0x2e, 0x30, 0x2e,
     0x32, 0x2e, 0x31, 0x3a, 0x36, 0x33, 0x36, 0x33, 0x81, 0x16,
     0x74, 0x63, 0x70, 0x34, 0x3a, 0x2f, 0x2f, 0x31, 0x39, 0x32,
@@ -68,7 +69,7 @@
     0x01, 0x01, 0x86, 0x01, 0x01, 0x90, 0x01, 0x0a, 0x91, 0x01,
     0xc8, 0x97, 0x01, 0x01, 0x92, 0x02, 0x0b, 0xb8, 0x93, 0x01,
     0x04, 0x98, 0x01, 0x02, 0x94, 0x04, 0x4f, 0x41, 0xe7, 0x7b,
-    0x95, 0x04, 0x3b, 0x8d, 0x37, 0x30,
+    0x95, 0x04, 0x3b, 0x8d, 0x37, 0x30, 0x6c, 0x01, 0x07,
   };
   BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
                                 wire.begin(), wire.end());
@@ -89,6 +90,7 @@
   BOOST_CHECK_EQUAL(status1.getNOutNacks(), status2.getNOutNacks());
   BOOST_CHECK_EQUAL(status1.getNInBytes(), status2.getNInBytes());
   BOOST_CHECK_EQUAL(status1.getNOutBytes(), status2.getNOutBytes());
+  BOOST_CHECK_EQUAL(status1.getFlags(), status2.getFlags());
 
   std::ostringstream os;
   os << status2;
@@ -99,6 +101,7 @@
                               "FaceScope: local,\n"
                               "FacePersistency: on-demand,\n"
                               "LinkType: multi-access,\n"
+                              "Flags: 0x7,\n"
                               "Counters: { Interests: {in: 10, out: 3000},\n"
                               "            Data: {in: 200, out: 4},\n"
                               "            Nack: {in: 1, out: 2},\n"
@@ -106,6 +109,26 @@
                               ")");
 }
 
+BOOST_AUTO_TEST_CASE(FlagBit)
+{
+  FaceStatus status;
+  status.setFlags(0x7);
+  BOOST_CHECK_EQUAL(status.getFlags(), 0x7);
+
+  BOOST_CHECK(status.getFlagBit(0));
+  BOOST_CHECK(status.getFlagBit(1));
+  BOOST_CHECK(status.getFlagBit(2));
+  BOOST_CHECK(!status.getFlagBit(3));
+
+  status.setFlagBit(3, true);
+  BOOST_CHECK_EQUAL(status.getFlags(), 0xf);
+  BOOST_CHECK(status.getFlagBit(3));
+
+  status.setFlagBit(1, false);
+  BOOST_CHECK_EQUAL(status.getFlags(), 0xd);
+  BOOST_CHECK(!status.getFlagBit(1));
+}
+
 BOOST_AUTO_TEST_SUITE_END() // TestFaceStatus
 BOOST_AUTO_TEST_SUITE_END() // Nfd
 BOOST_AUTO_TEST_SUITE_END() // Mgmt