management: ensure Element::Error inherits from tlv::Error

Add static asserts to ensure ndn::nfd::*::Error are subclasses of tlv::Error.

Add WireEncodable and WireDecodable concept checks to
ndn::nfd::* classes that represent TLV abstraction.

Move definition of ChannelStatus, FibEntry, ForwarderStatus,
StrategyChoice methods into .cpp.

Move declaration of FaceScope, FacePersistency, LinkType
into encoding/nfd-constants.hpp.

Eliminate duplicate RibFlags declaration.

refs #1983

Change-Id: I207bae479aa6da54a581a7cca5b2a13743827ff0
diff --git a/src/management/nfd-fib-entry.cpp b/src/management/nfd-fib-entry.cpp
new file mode 100644
index 0000000..73b5f42
--- /dev/null
+++ b/src/management/nfd-fib-entry.cpp
@@ -0,0 +1,268 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 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 "nfd-fib-entry.hpp"
+#include <sstream>
+#include "encoding/tlv-nfd.hpp"
+#include "encoding/block-helpers.hpp"
+#include "util/concepts.hpp"
+
+namespace ndn {
+namespace nfd {
+
+//BOOST_CONCEPT_ASSERT((boost::EqualityComparable<NextHopRecord>));
+BOOST_CONCEPT_ASSERT((WireEncodable<NextHopRecord>));
+BOOST_CONCEPT_ASSERT((WireDecodable<NextHopRecord>));
+static_assert(std::is_base_of<tlv::Error, NextHopRecord::Error>::value,
+              "NextHopRecord::Error must inherit from tlv::Error");
+
+//BOOST_CONCEPT_ASSERT((boost::EqualityComparable<FibEntry>));
+BOOST_CONCEPT_ASSERT((WireEncodable<FibEntry>));
+BOOST_CONCEPT_ASSERT((WireDecodable<FibEntry>));
+static_assert(std::is_base_of<tlv::Error, FibEntry::Error>::value,
+              "FibEntry::Error must inherit from tlv::Error");
+
+// NextHopRecord := NEXT-HOP-RECORD TLV-LENGTH
+//                    FaceId
+//                    Cost
+
+NextHopRecord::NextHopRecord()
+  : m_faceId(std::numeric_limits<uint64_t>::max())
+  , m_cost(0)
+{
+}
+
+NextHopRecord::NextHopRecord(const Block& block)
+{
+  this->wireDecode(block);
+}
+
+NextHopRecord&
+NextHopRecord::setFaceId(uint64_t faceId)
+{
+  m_faceId = faceId;
+  m_wire.reset();
+  return *this;
+}
+
+NextHopRecord&
+NextHopRecord::setCost(uint64_t cost)
+{
+  m_cost = cost;
+  m_wire.reset();
+  return *this;
+}
+
+template<bool T>
+size_t
+NextHopRecord::wireEncode(EncodingImpl<T>& block) const
+{
+  size_t totalLength = 0;
+  totalLength += prependNonNegativeIntegerBlock(block,
+                                                ndn::tlv::nfd::Cost,
+                                                m_cost);
+
+  totalLength += prependNonNegativeIntegerBlock(block,
+                                                ndn::tlv::nfd::FaceId,
+                                                m_faceId);
+
+  totalLength += block.prependVarNumber(totalLength);
+  totalLength += block.prependVarNumber(ndn::tlv::nfd::NextHopRecord);
+  return totalLength;
+}
+
+template size_t
+NextHopRecord::wireEncode<true>(EncodingImpl<true>& block) const;
+
+template size_t
+NextHopRecord::wireEncode<false>(EncodingImpl<false>& block) const;
+
+const Block&
+NextHopRecord::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
+NextHopRecord::wireDecode(const Block& wire)
+{
+  m_faceId = std::numeric_limits<uint64_t>::max();
+  m_cost = 0;
+
+  m_wire = wire;
+
+  if (m_wire.type() != tlv::nfd::NextHopRecord) {
+    std::stringstream error;
+    error << "Requested decoding of NextHopRecord, but Block is of a different type: #"
+          << m_wire.type();
+    throw Error(error.str());
+  }
+  m_wire.parse();
+
+  Block::element_const_iterator val = m_wire.elements_begin();
+  if (val == m_wire.elements_end()) {
+    throw Error("Unexpected end of NextHopRecord");
+  }
+  else if (val->type() != tlv::nfd::FaceId) {
+    std::stringstream error;
+    error << "Expected FaceId, but Block is of a different type: #"
+          << val->type();
+    throw Error(error.str());
+  }
+  m_faceId = readNonNegativeInteger(*val);
+  ++val;
+
+  if (val == m_wire.elements_end()) {
+    throw Error("Unexpected end of NextHopRecord");
+  }
+  else if (val->type() != tlv::nfd::Cost) {
+    std::stringstream error;
+    error << "Expected Cost, but Block is of a different type: #"
+          << m_wire.type();
+    throw Error(error.str());
+  }
+  m_cost = readNonNegativeInteger(*val);
+}
+
+// FibEntry      := FIB-ENTRY-TYPE TLV-LENGTH
+//                    Name
+//                    NextHopRecord*
+
+FibEntry::FibEntry()
+{
+}
+
+FibEntry::FibEntry(const Block& block)
+{
+  this->wireDecode(block);
+}
+
+FibEntry&
+FibEntry::setPrefix(const Name& prefix)
+{
+  m_prefix = prefix;
+  m_wire.reset();
+  return *this;
+}
+
+FibEntry&
+FibEntry::addNextHopRecord(const NextHopRecord& nextHopRecord)
+{
+  m_nextHopRecords.push_back(nextHopRecord);
+  m_wire.reset();
+  return *this;
+}
+
+template<bool T>
+size_t
+FibEntry::wireEncode(EncodingImpl<T>& block) const
+{
+  size_t totalLength = 0;
+
+  for (auto i = m_nextHopRecords.rbegin(); i != m_nextHopRecords.rend(); ++i) {
+    totalLength += i->wireEncode(block);
+  }
+
+  totalLength += m_prefix.wireEncode(block);
+  totalLength += block.prependVarNumber(totalLength);
+  totalLength += block.prependVarNumber(tlv::nfd::FibEntry);
+
+  return totalLength;
+}
+
+template size_t
+FibEntry::wireEncode<true>(EncodingImpl<true>& block) const;
+
+template size_t
+FibEntry::wireEncode<false>(EncodingImpl<false>& block) const;
+
+const Block&
+FibEntry::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
+FibEntry::wireDecode(const Block& wire)
+{
+  m_prefix.clear();
+  m_nextHopRecords.clear();
+
+  m_wire = wire;
+
+  if (m_wire.type() != tlv::nfd::FibEntry) {
+    std::stringstream error;
+    error << "Requested decoding of FibEntry, but Block is of a different type: #"
+          << m_wire.type();
+    throw Error(error.str());
+  }
+
+  m_wire.parse();
+
+  Block::element_const_iterator val = m_wire.elements_begin();
+  if (val == m_wire.elements_end()) {
+    throw Error("Unexpected end of FibEntry");
+  }
+  else if (val->type() != tlv::Name) {
+    std::stringstream error;
+    error << "Expected Name, but Block is of a different type: #"
+          << val->type();
+    throw Error(error.str());
+  }
+  m_prefix.wireDecode(*val);
+  ++val;
+
+  for (; val != m_wire.elements_end(); ++val) {
+    if (val->type() != tlv::nfd::NextHopRecord) {
+      std::stringstream error;
+      error << "Expected NextHopRecords, but Block is of a different type: #"
+            << val->type();
+      throw Error(error.str());
+    }
+    m_nextHopRecords.push_back(NextHopRecord(*val));
+  }
+}
+
+} // namespace nfd
+} // namespace ndn