management: add objects for encoding/decoding NFD FIB enumeration protocol units
Change-Id: Icf64385c9da1139ad785d4904f31f1aae66b5590
diff --git a/src/management/nfd-fib-entry.hpp b/src/management/nfd-fib-entry.hpp
new file mode 100644
index 0000000..e38dbb7
--- /dev/null
+++ b/src/management/nfd-fib-entry.hpp
@@ -0,0 +1,317 @@
+/* -*- 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_FIB_ENTRY_HPP
+#define NDN_MANAGEMENT_NFD_FIB_ENTRY_HPP
+
+#include "../encoding/block.hpp"
+#include "../encoding/encoding-buffer.hpp"
+#include "../encoding/tlv-nfd.hpp"
+#include "../name.hpp"
+
+namespace ndn {
+namespace nfd {
+
+// NextHopRecord := NEXT-HOP-RECORD TLV-LENGTH
+// FaceId
+// Cost
+
+class NextHopRecord
+{
+public:
+ class Error : public Tlv::Error
+ {
+ public:
+ Error(const std::string& what) : Tlv::Error(what)
+ {
+ }
+ };
+
+ NextHopRecord()
+ : m_faceId(std::numeric_limits<uint64_t>::max())
+ , m_cost(0)
+ {
+ }
+
+ NextHopRecord(const Block& block)
+ {
+ wireDecode(block);
+ }
+
+ uint64_t
+ getFaceId() const
+ {
+ return m_faceId;
+ }
+
+ NextHopRecord&
+ setFaceId(uint64_t faceId)
+ {
+ m_faceId = faceId;
+ m_wire.reset();
+ return *this;
+ }
+
+ uint64_t
+ getCost() const
+ {
+ return m_cost;
+ }
+
+ NextHopRecord&
+ setCost(uint64_t cost)
+ {
+ m_cost = cost;
+ m_wire.reset();
+ return *this;
+ }
+
+ template<bool T>
+ size_t
+ 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;
+ }
+
+ const Block&
+ 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
+ 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: #"
+ << error.str();
+ throw Error(error.str());
+ }
+ m_cost = readNonNegativeInteger(*val);
+ }
+
+
+private:
+ uint64_t m_faceId;
+ uint64_t m_cost;
+
+ mutable Block m_wire;
+};
+
+
+// FibEntry := FIB-ENTRY-TYPE TLV-LENGTH
+// Name
+// NextHopRecord*
+
+class FibEntry
+{
+public:
+ class Error : public Tlv::Error
+ {
+ public:
+ Error(const std::string& what) : Tlv::Error(what)
+ {
+ }
+ };
+
+ FibEntry()
+ {
+
+ }
+
+ const Name&
+ getPrefix() const
+ {
+ return m_prefix;
+ }
+
+ FibEntry&
+ setPrefix(const Name& prefix)
+ {
+ m_prefix = prefix;
+ m_wire.reset();
+ return *this;
+ }
+
+ const std::list<NextHopRecord>&
+ getNextHopRecords() const
+ {
+ return m_nextHopRecords;
+ }
+
+ FibEntry&
+ addNextHopRecord(const NextHopRecord& nextHopRecord)
+ {
+ m_nextHopRecords.push_back(nextHopRecord);
+ m_wire.reset();
+ return *this;
+ }
+
+ template<typename T>
+ FibEntry&
+ setNextHopRecords(const T& begin, const T& end)
+ {
+ m_nextHopRecords.clear();
+ m_nextHopRecords.assign(begin, end);
+ m_wire.reset();
+ return *this;
+ }
+
+ template<bool T>
+ size_t
+ wireEncode(EncodingImpl<T>& block) const
+ {
+ size_t totalLength = 0;
+
+ for (std::list<NextHopRecord>::const_iterator i = m_nextHopRecords.begin();
+ i != m_nextHopRecords.end();
+ ++i)
+ {
+ totalLength += i->wireEncode(block);
+ }
+
+ totalLength += m_prefix.wireEncode(block);
+ totalLength += block.prependVarNumber(totalLength);
+ totalLength += block.prependVarNumber(tlv::nfd::FibEntry);
+
+ return totalLength;
+ }
+
+ const Block&
+ 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
+ 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(*val);
+ }
+ }
+
+private:
+ Name m_prefix;
+ std::list<NextHopRecord> m_nextHopRecords;
+
+ mutable Block m_wire;
+};
+
+} // namespace nfd
+} // namespace ndn
+
+#endif // NDN_MANAGEMENT_NFD_FIB_ENTRY_HPP
+
diff --git a/tests/management/test-nfd-fib-entry.cpp b/tests/management/test-nfd-fib-entry.cpp
new file mode 100644
index 0000000..fb3383c
--- /dev/null
+++ b/tests/management/test-nfd-fib-entry.cpp
@@ -0,0 +1,145 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "management/nfd-fib-entry.hpp"
+
+#include <boost/test/unit_test.hpp>
+#include <boost/test/output_test_stream.hpp>
+
+namespace ndn {
+namespace nfd {
+
+BOOST_AUTO_TEST_SUITE(TestNfdFibEntry)
+
+const uint8_t TestNextHopRecord[] =
+{
+ 0x81, 0x06, 0x69, 0x01, 0x0a, 0x6a, 0x01, 0xc8
+};
+
+const uint8_t TestFibEntryNoNextHops[] =
+{
+ 0x80, 0x15, 0x07, 0x13, 0x08, 0x04, 0x74, 0x68, 0x69, 0x73,
+ 0x08, 0x02, 0x69, 0x73, 0x08, 0x01, 0x61, 0x08, 0x04, 0x74,
+ 0x65, 0x73, 0x74
+};
+
+const uint8_t TestFibEntry[] =
+{
+ 0x80, 0x38, 0x07, 0x13, 0x08, 0x04, 0x74, 0x68,
+ 0x69, 0x73, 0x08, 0x02, 0x69, 0x73, 0x08, 0x01,
+ 0x61, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x81,
+ 0x07, 0x69, 0x01, 0x28, 0x6a, 0x02, 0x01, 0xf4,
+ 0x81, 0x07, 0x69, 0x01, 0x1e, 0x6a, 0x02, 0x01,
+ 0x90, 0x81, 0x07, 0x69, 0x01, 0x14, 0x6a, 0x02,
+ 0x01, 0x2c, 0x81, 0x06, 0x69, 0x01, 0x0a, 0x6a,
+ 0x01, 0xc8
+};
+
+BOOST_AUTO_TEST_CASE(TestNextHopRecordEncode)
+{
+ NextHopRecord record;
+ record.setFaceId(10);
+ record.setCost(200);
+
+ const Block& wire = record.wireEncode();
+ BOOST_REQUIRE_EQUAL_COLLECTIONS(TestNextHopRecord,
+ TestNextHopRecord + sizeof(TestNextHopRecord),
+ wire.begin(), wire.end());
+
+}
+
+BOOST_AUTO_TEST_CASE(TestNextHopRecordDecode)
+{
+ NextHopRecord record;
+
+ BOOST_REQUIRE_NO_THROW(record.wireDecode(Block(TestNextHopRecord,
+ sizeof(TestNextHopRecord))));
+ BOOST_REQUIRE_EQUAL(record.getFaceId(), 10);
+ BOOST_REQUIRE_EQUAL(record.getCost(), 200);
+}
+
+BOOST_AUTO_TEST_CASE(TestFibEntryNoNextHopEncode)
+{
+ FibEntry entry;
+ entry.setPrefix("/this/is/a/test");
+
+ const Block& wire = entry.wireEncode();
+ BOOST_REQUIRE_EQUAL_COLLECTIONS(TestFibEntryNoNextHops,
+ TestFibEntryNoNextHops + sizeof(TestFibEntryNoNextHops),
+ wire.begin(), wire.end());
+}
+
+BOOST_AUTO_TEST_CASE(TestFibEntryNoNextHopsDecode)
+{
+ FibEntry entry;
+ BOOST_REQUIRE_NO_THROW(entry.wireDecode(Block(TestFibEntryNoNextHops,
+ sizeof(TestFibEntryNoNextHops))));
+
+ BOOST_REQUIRE_EQUAL(entry.getPrefix(), "/this/is/a/test");
+ BOOST_REQUIRE(entry.getNextHopRecords().empty());
+}
+
+BOOST_AUTO_TEST_CASE(TestFibEntryEncode)
+{
+ FibEntry entry;
+ entry.setPrefix("/this/is/a/test");
+
+ std::list<NextHopRecord> records;
+
+ for (int i = 1; i < 4; i++)
+ {
+ NextHopRecord record;
+ record.setFaceId(i * 10);
+ record.setCost((i * 100) + 100);
+ records.push_back(record);
+ }
+
+ entry.setNextHopRecords(records.begin(), records.end());
+
+ NextHopRecord oneMore;
+ oneMore.setFaceId(40);
+ oneMore.setCost(500);
+
+ entry.addNextHopRecord(oneMore);
+
+ const Block& wire = entry.wireEncode();
+ BOOST_REQUIRE_EQUAL_COLLECTIONS(TestFibEntry,
+ TestFibEntry + sizeof(TestFibEntry),
+ wire.begin(), wire.end());
+
+ // std::ofstream of("out.tmp");
+ // of.write((const char*)entry.wireEncode().wire(),
+ // entry.wireEncode().size());
+
+}
+
+BOOST_AUTO_TEST_CASE(TestFibEntryDecode)
+{
+ FibEntry entry;
+ BOOST_REQUIRE_NO_THROW(entry.wireDecode(Block(TestFibEntry,
+ sizeof(TestFibEntry))));
+
+ std::list<NextHopRecord> records = entry.getNextHopRecords();
+
+ BOOST_REQUIRE_EQUAL(entry.getPrefix(), "/this/is/a/test");
+ BOOST_REQUIRE_EQUAL(entry.getNextHopRecords().size(), 4);
+
+ size_t value = 4;
+
+ for (std::list<NextHopRecord>::const_iterator i = records.begin();
+ i != records.end();
+ ++i)
+ {
+ BOOST_CHECK_EQUAL(i->getFaceId(), value * 10);
+ BOOST_CHECK_EQUAL(i->getCost(), (value * 100) + 100);
+ --value;
+ }
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace nfd
+} // namespace ndn