Introduce Delegation and DelegationList

DelegationList will become the payload structure of ForwardingHint
and LinkContent in a later commit.

refs #4055

Change-Id: If29a39c47b779aafdd4aebb104092403f28e6653
diff --git a/src/delegation-list.cpp b/src/delegation-list.cpp
new file mode 100644
index 0000000..a0dd7c8
--- /dev/null
+++ b/src/delegation-list.cpp
@@ -0,0 +1,240 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2017 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 "delegation-list.hpp"
+
+namespace ndn {
+
+BOOST_CONCEPT_ASSERT((boost::EqualityComparable<DelegationList>));
+BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<DelegationList>));
+BOOST_CONCEPT_ASSERT((WireDecodable<DelegationList>));
+
+DelegationList::Error::Error(const std::string& what)
+  : tlv::Error(what)
+{
+}
+
+DelegationList::Error::Error(const std::string& what, const std::exception& innerException)
+  : Error(what + std::string(": ") + innerException.what())
+{
+}
+
+DelegationList::DelegationList()
+  : m_isSorted(true)
+{
+}
+
+DelegationList::DelegationList(const Block& block, bool wantSort)
+{
+  this->wireDecode(block, wantSort);
+}
+
+bool
+DelegationList::isValidTlvType(uint32_t type)
+{
+  switch (type) {
+    case tlv::Content:
+    case tlv::ForwardingHint:
+      return true;
+    default:
+      return false;
+  }
+}
+
+template<encoding::Tag TAG>
+size_t
+DelegationList::wireEncode(EncodingImpl<TAG>& encoder, uint32_t type) const
+{
+  if (!isValidTlvType(type)) {
+    BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid TLV-TYPE " + to_string(type) +
+                                                " when encoding DelegationList"));
+  }
+
+  if (this->size() == 0) {
+    BOOST_THROW_EXCEPTION(Error("Empty DelegationList"));
+  }
+
+  // LinkContent ::= (type) TLV-LENGTH
+  //                    Delegation+
+
+  // Delegation ::= LINK-DELEGATION-TYPE TLV-LENGTH
+  //                  Preference
+  //                  Name
+
+  // Preference ::= LINK-PREFERENCE-TYPE TLV-LENGTH
+  //       nonNegativeInteger
+
+  size_t totalLen = 0;
+  for (auto i = m_dels.rbegin(); i != m_dels.rend(); ++i) {
+    size_t delLen = 0;
+    delLen += i->name.wireEncode(encoder);
+    delLen += prependNonNegativeIntegerBlock(encoder, tlv::LinkPreference, i->preference);
+    delLen += encoder.prependVarNumber(delLen);
+    delLen += encoder.prependVarNumber(tlv::LinkDelegation);
+    totalLen += delLen;
+  }
+  totalLen += encoder.prependVarNumber(totalLen);
+  totalLen += encoder.prependVarNumber(type);
+  return totalLen;
+}
+
+template size_t
+DelegationList::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&, uint32_t) const;
+
+template size_t
+DelegationList::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&, uint32_t) const;
+
+void
+DelegationList::wireDecode(const Block& block, bool wantSort)
+{
+  if (!isValidTlvType(block.type())) {
+    BOOST_THROW_EXCEPTION(Error("Unexpected TLV-TYPE " + to_string(block.type()) +
+                                " when decoding DelegationList"));
+  }
+
+  m_isSorted = wantSort;
+  m_dels.clear();
+
+  block.parse();
+  for (const auto& del : block.elements()) {
+    if (del.type() != tlv::LinkDelegation) {
+      BOOST_THROW_EXCEPTION(Error("Unexpected TLV-TYPE " + to_string(del.type()) +
+                                  " when decoding Delegation"));
+    }
+    del.parse();
+
+    auto val = del.elements_begin();
+    if (val == del.elements_end() || val->type() != tlv::LinkPreference) {
+      BOOST_THROW_EXCEPTION(Error("Missing Preference field in Delegation"));
+    }
+    uint64_t preference = 0;
+    try {
+      preference = readNonNegativeInteger(*val);
+    }
+    catch (const tlv::Error& inner) {
+      BOOST_THROW_EXCEPTION(Error("Invalid Preference field in Delegation", inner));
+    }
+
+    ++val;
+    if (val == del.elements_end() || val->type() != tlv::Name) {
+      BOOST_THROW_EXCEPTION(Error("Missing Name field in Delegation"));
+    }
+    Name name;
+    try {
+      name.wireDecode(*val);
+    }
+    catch (const tlv::Error& inner) {
+      BOOST_THROW_EXCEPTION(Error("Invalid Name field in Delegation", inner));
+    }
+
+    this->insertImpl(preference, name);
+  }
+
+  if (this->size() == 0) {
+    BOOST_THROW_EXCEPTION(Error("Empty DelegationList"));
+  }
+}
+
+void
+DelegationList::sort()
+{
+  if (m_isSorted) {
+    return;
+  }
+
+  std::vector<Delegation> dels;
+  dels.swap(m_dels);
+
+  m_isSorted = true;
+  for (const Delegation& del : dels) {
+    this->insertImpl(del.preference, del.name);
+  }
+}
+
+bool
+DelegationList::insert(uint64_t preference, const Name& name,
+                       InsertConflictResolution onConflict)
+{
+  switch (onConflict) {
+    case INS_REPLACE:
+      this->eraseImpl(nullopt, name);
+      this->insertImpl(preference, name);
+      return true;
+    case INS_APPEND:
+      this->insertImpl(preference, name);
+      return true;
+    case INS_SKIP:
+      if (!std::any_of(m_dels.begin(), m_dels.end(),
+                       [name] (const Delegation& del) { return del.name == name; })) {
+        this->insertImpl(preference, name);
+        return true;
+      }
+      return false;
+  }
+  BOOST_ASSERT_MSG(false, "Unknown onConflict");
+  return false;
+}
+
+void
+DelegationList::insertImpl(uint64_t preference, const Name& name)
+{
+  if (!m_isSorted) {
+    m_dels.push_back({preference, name});
+    return;
+  }
+
+  Delegation del{preference, name};
+  auto pos = std::upper_bound(m_dels.begin(), m_dels.end(), del);
+  m_dels.insert(pos, del);
+}
+
+size_t
+DelegationList::eraseImpl(optional<uint64_t> preference, const Name& name)
+{
+  size_t nErased = 0;
+  for (auto i = m_dels.begin(); i != m_dels.end();) {
+    if ((!preference || i->preference == *preference) &&
+        i->name == name) {
+      ++nErased;
+      i = m_dels.erase(i);
+    }
+    else {
+      ++i;
+    }
+  }
+  return nErased;
+}
+
+bool
+operator==(const DelegationList& lhs, const DelegationList& rhs)
+{
+  return lhs.m_dels == rhs.m_dels;
+}
+
+std::ostream&
+operator<<(std::ostream& os, const DelegationList& dl)
+{
+  os << '[';
+  std::copy(dl.begin(), dl.end(), make_ostream_joiner(os, ','));
+  return os << ']';
+}
+
+} // namespace ndn