management: add Flags and Mask fields to faces/create and faces/update

refs #3731

Change-Id: I35fb84a875767649da0cb354be5e5c07de179198
diff --git a/src/encoding/nfd-constants.hpp b/src/encoding/nfd-constants.hpp
index 671bd2c..4c38bd9 100644
--- a/src/encoding/nfd-constants.hpp
+++ b/src/encoding/nfd-constants.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 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).
  *
@@ -64,6 +64,14 @@
 
 /** \ingroup management
  */
+enum FaceFlagBit {
+  /** \brief bit that controls whether local fields are enabled on a face
+   */
+  BIT_LOCAL_FIELDS_ENABLED = 0
+};
+
+/** \ingroup management
+ */
 enum LinkType : uint8_t {
   LINK_TYPE_NONE = std::numeric_limits<uint8_t>::max(),
   /** \brief link is point-to-point
diff --git a/src/encoding/tlv-nfd.hpp b/src/encoding/tlv-nfd.hpp
index 51cfc4f..0033f22 100644
--- a/src/encoding/tlv-nfd.hpp
+++ b/src/encoding/tlv-nfd.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).
  *
@@ -39,6 +39,7 @@
   Origin              = 111,
   Cost                = 106,
   Flags               = 108,
+  Mask                = 112,
   Strategy            = 107,
   ExpirationPeriod    = 109,
 
diff --git a/src/management/nfd-control-command.cpp b/src/management/nfd-control-command.cpp
index 88db4f9..9853f9d 100644
--- a/src/management/nfd-control-command.cpp
+++ b/src/management/nfd-control-command.cpp
@@ -98,22 +98,37 @@
 {
   m_requestValidator
     .required(CONTROL_PARAMETER_URI)
-    .optional(CONTROL_PARAMETER_FACE_PERSISTENCY);
+    .optional(CONTROL_PARAMETER_FACE_PERSISTENCY)
+    .optional(CONTROL_PARAMETER_FLAGS)
+    .optional(CONTROL_PARAMETER_MASK);
   m_responseValidator
-    .required(CONTROL_PARAMETER_URI)
     .required(CONTROL_PARAMETER_FACE_ID)
-    .required(CONTROL_PARAMETER_FACE_PERSISTENCY);
+    .required(CONTROL_PARAMETER_FACE_PERSISTENCY)
+    .optional(CONTROL_PARAMETER_FLAGS)
+    .optional(CONTROL_PARAMETER_URI);
 }
 
 void
 FaceCreateCommand::applyDefaultsToRequest(ControlParameters& parameters) const
 {
+  parameters.setFaceId(0);
+
   if (!parameters.hasFacePersistency()) {
     parameters.setFacePersistency(FacePersistency::FACE_PERSISTENCY_PERSISTENT);
   }
 }
 
 void
+FaceCreateCommand::validateRequest(const ControlParameters& parameters) const
+{
+  this->ControlCommand::validateRequest(parameters);
+
+  if (parameters.hasFlags() != parameters.hasMask()) {
+    BOOST_THROW_EXCEPTION(ArgumentError("Flags must be accompanied by Mask"));
+  }
+}
+
+void
 FaceCreateCommand::validateResponse(const ControlParameters& parameters) const
 {
   this->ControlCommand::validateResponse(parameters);
@@ -127,12 +142,21 @@
   : ControlCommand("faces", "update")
 {
   m_requestValidator
-    .required(CONTROL_PARAMETER_FACE_ID)
-    .optional(CONTROL_PARAMETER_FACE_PERSISTENCY);
+    .optional(CONTROL_PARAMETER_FACE_ID)
+    .optional(CONTROL_PARAMETER_FACE_PERSISTENCY)
+    .optional(CONTROL_PARAMETER_FLAGS)
+    .optional(CONTROL_PARAMETER_MASK);
   m_responseValidator
-    .required(CONTROL_PARAMETER_FACE_ID)
-    .optional(CONTROL_PARAMETER_FACE_PERSISTENCY);
-  m_responseValidator = m_requestValidator;
+    .optional(CONTROL_PARAMETER_FACE_ID)
+    .optional(CONTROL_PARAMETER_FACE_PERSISTENCY)
+    .optional(CONTROL_PARAMETER_FLAGS)
+    .optional(CONTROL_PARAMETER_MASK);
+}
+
+void
+FaceUpdateCommand::applyDefaultsToRequest(ControlParameters& parameters) const
+{
+  parameters.setFaceId(0);
 }
 
 void
@@ -140,8 +164,8 @@
 {
   this->ControlCommand::validateRequest(parameters);
 
-  if (parameters.getFaceId() == 0) {
-    BOOST_THROW_EXCEPTION(ArgumentError("FaceId must not be zero"));
+  if (parameters.hasFlags() != parameters.hasMask()) {
+    BOOST_THROW_EXCEPTION(ArgumentError("Flags must be accompanied by Mask"));
   }
 }
 
diff --git a/src/management/nfd-control-command.hpp b/src/management/nfd-control-command.hpp
index dfe876b..2f1c4e0 100644
--- a/src/management/nfd-control-command.hpp
+++ b/src/management/nfd-control-command.hpp
@@ -148,6 +148,9 @@
   applyDefaultsToRequest(ControlParameters& parameters) const override;
 
   virtual void
+  validateRequest(const ControlParameters& parameters) const override;
+
+  virtual void
   validateResponse(const ControlParameters& parameters) const override;
 };
 
@@ -163,6 +166,9 @@
   FaceUpdateCommand();
 
   virtual void
+  applyDefaultsToRequest(ControlParameters& parameters) const override;
+
+  virtual void
   validateRequest(const ControlParameters& parameters) const override;
 
   virtual void
diff --git a/src/management/nfd-control-parameters.cpp b/src/management/nfd-control-parameters.cpp
index f94148b..f68ca26 100644
--- a/src/management/nfd-control-parameters.cpp
+++ b/src/management/nfd-control-parameters.cpp
@@ -61,6 +61,9 @@
   if (this->hasStrategy()) {
     totalLength += prependNestedBlock(encoder, tlv::nfd::Strategy, m_strategy);
   }
+  if (this->hasMask()) {
+    totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Mask, m_mask);
+  }
   if (this->hasFlags()) {
     totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Flags, m_flags);
   }
@@ -167,6 +170,12 @@
     m_flags = static_cast<uint64_t>(readNonNegativeInteger(*val));
   }
 
+  val = m_wire.find(tlv::nfd::Mask);
+  m_hasFields[CONTROL_PARAMETER_MASK] = val != m_wire.elements_end();
+  if (this->hasMask()) {
+    m_mask = static_cast<uint64_t>(readNonNegativeInteger(*val));
+  }
+
   val = m_wire.find(tlv::nfd::Strategy);
   m_hasFields[CONTROL_PARAMETER_STRATEGY] = val != m_wire.elements_end();
   if (this->hasStrategy()) {
@@ -192,6 +201,79 @@
   }
 }
 
+bool
+ControlParameters::hasFlagBit(size_t bit) const
+{
+  if (bit >= 64) {
+    BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
+  }
+
+  if (!hasMask()) {
+    return false;
+  }
+
+  return getMask() & (1 << bit);
+}
+
+bool
+ControlParameters::getFlagBit(size_t bit) const
+{
+  if (bit >= 64) {
+    BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
+  }
+
+  if (!hasFlags()) {
+    return false;
+  }
+
+  return getFlags() & (1 << bit);
+}
+
+ControlParameters&
+ControlParameters::setFlagBit(size_t bit, bool value, bool wantMask/* = true*/)
+{
+  if (bit >= 64) {
+    BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
+  }
+
+  uint64_t flags = hasFlags() ? getFlags() : 0;
+  if (value) {
+    flags |= (1 << bit);
+  }
+  else {
+    flags &= ~(1 << bit);
+  }
+  setFlags(flags);
+
+  if (wantMask) {
+    uint64_t mask = hasMask() ? getMask() : 0;
+    mask |= (1 << bit);
+    setMask(mask);
+  }
+
+  return *this;
+}
+
+ControlParameters&
+ControlParameters::unsetFlagBit(size_t bit)
+{
+  if (bit >= 64) {
+    BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
+  }
+
+  uint64_t mask = hasMask() ? getMask() : 0;
+  mask &= ~(1 << bit);
+  if (mask == 0) {
+    unsetMask();
+    unsetFlags();
+  }
+  else {
+    setMask(mask);
+  }
+
+  return *this;
+}
+
 std::ostream&
 operator<<(std::ostream& os, const ControlParameters& parameters)
 {
@@ -222,7 +304,15 @@
   }
 
   if (parameters.hasFlags()) {
-    os << "Flags: " << parameters.getFlags() << ", ";
+    std::ios_base::fmtflags osFlags = os.flags();
+    os << "Flags: " << std::showbase << std::hex << parameters.getFlags() << ", ";
+    os.flags(osFlags);
+  }
+
+  if (parameters.hasMask()) {
+    std::ios_base::fmtflags osFlags = os.flags();
+    os << "Mask: " << std::showbase << std::hex << parameters.getMask() << ", ";
+    os.flags(osFlags);
   }
 
   if (parameters.hasStrategy()) {
diff --git a/src/management/nfd-control-parameters.hpp b/src/management/nfd-control-parameters.hpp
index ed077a7..92f40b8 100644
--- a/src/management/nfd-control-parameters.hpp
+++ b/src/management/nfd-control-parameters.hpp
@@ -41,6 +41,7 @@
   CONTROL_PARAMETER_ORIGIN,
   CONTROL_PARAMETER_COST,
   CONTROL_PARAMETER_FLAGS,
+  CONTROL_PARAMETER_MASK,
   CONTROL_PARAMETER_STRATEGY,
   CONTROL_PARAMETER_EXPIRATION_PERIOD,
   CONTROL_PARAMETER_FACE_PERSISTENCY,
@@ -55,6 +56,7 @@
   "Origin",
   "Cost",
   "Flags",
+  "Mask",
   "Strategy",
   "ExpirationPeriod",
   "FacePersistency"
@@ -62,6 +64,7 @@
 
 /**
  * \ingroup management
+ * \deprecated use Flags+Mask fields instead
  */
 enum LocalControlFeature {
   LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID = 1,
@@ -69,10 +72,10 @@
 };
 
 /**
- * @ingroup management
- * @brief represents parameters in a ControlCommand request or response
- * @sa http://redmine.named-data.net/projects/nfd/wiki/ControlCommand#ControlParameters
- * @details This type is copyable because it's an abstraction of a TLV type.
+ * \ingroup management
+ * \brief represents parameters in a ControlCommand request or response
+ * \sa http://redmine.named-data.net/projects/nfd/wiki/ControlCommand#ControlParameters
+ * \details This type is copyable because it's an abstraction of a TLV type.
  */
 class ControlParameters : public ndn::mgmt::ControlParameters
 {
@@ -103,7 +106,6 @@
   wireDecode(const Block& wire) final;
 
 public: // getters & setters
-
   bool
   hasName() const
   {
@@ -194,12 +196,18 @@
     return *this;
   }
 
+  /**
+   * \deprecated use Flags+Mask fields instead
+   */
   bool
   hasLocalControlFeature() const
   {
     return m_hasFields[CONTROL_PARAMETER_LOCAL_CONTROL_FEATURE];
   }
 
+  /**
+   * \deprecated use Flags+Mask fields instead
+   */
   LocalControlFeature
   getLocalControlFeature() const
   {
@@ -207,6 +215,9 @@
     return m_localControlFeature;
   }
 
+  /**
+   * \deprecated use Flags+Mask fields instead
+   */
   ControlParameters&
   setLocalControlFeature(LocalControlFeature localControlFeature)
   {
@@ -216,6 +227,9 @@
     return *this;
   }
 
+  /**
+   * \deprecated use Flags+Mask fields instead
+   */
   ControlParameters&
   unsetLocalControlFeature()
   {
@@ -315,6 +329,36 @@
   }
 
   bool
+  hasMask() const
+  {
+    return m_hasFields[CONTROL_PARAMETER_MASK];
+  }
+
+  uint64_t
+  getMask() const
+  {
+    BOOST_ASSERT(this->hasMask());
+    return m_mask;
+  }
+
+  ControlParameters&
+  setMask(uint64_t mask)
+  {
+    m_wire.reset();
+    m_mask = mask;
+    m_hasFields[CONTROL_PARAMETER_MASK] = true;
+    return *this;
+  }
+
+  ControlParameters&
+  unsetMask()
+  {
+    m_wire.reset();
+    m_hasFields[CONTROL_PARAMETER_MASK] = false;
+    return *this;
+  }
+
+  bool
   hasStrategy() const
   {
     return m_hasFields[CONTROL_PARAMETER_STRATEGY];
@@ -410,6 +454,38 @@
     return m_hasFields;
   }
 
+public: // Flags and Mask helpers
+  /**
+   * \return whether bit is enabled in Mask
+   * \param bit bit position within range [0, 64) (least significant bit is 0)
+   */
+  bool
+  hasFlagBit(size_t bit) const;
+
+  /**
+   * \return bit at a position in Flags
+   * \param bit bit position within range [0, 64) (least significant bit is 0)
+   */
+  bool
+  getFlagBit(size_t bit) const;
+
+  /**
+   * \brief set a bit in Flags
+   * \param bit bit position within range [0, 64) (least significant bit is 0)
+   * \param value new value in Flags
+   * \param wantMask if true, enable the bit in Mask
+   */
+  ControlParameters&
+  setFlagBit(size_t bit, bool value, bool wantMask = true);
+
+  /**
+   * \brief disable a bit in Mask
+   * \param bit bit position within range [0, 64) (least significant bit is 0)
+   * \post If all bits are disabled, Flags and Mask fields are deleted.
+   */
+  ControlParameters&
+  unsetFlagBit(size_t bit);
+
 private: // fields
   std::vector<bool>   m_hasFields;
 
@@ -420,6 +496,7 @@
   uint64_t            m_origin;
   uint64_t            m_cost;
   uint64_t            m_flags;
+  uint64_t            m_mask;
   Name                m_strategy;
   time::milliseconds  m_expirationPeriod;
   FacePersistency     m_facePersistency;