mgmt: add EndpointId field to NextHopRecord class

Refs: #4816
Change-Id: I1e0fb691ca520679f491502a62d827672ec53d8b
diff --git a/ndn-cxx/encoding/tlv-nfd.hpp b/ndn-cxx/encoding/tlv-nfd.hpp
index a891818..3048b73 100644
--- a/ndn-cxx/encoding/tlv-nfd.hpp
+++ b/ndn-cxx/encoding/tlv-nfd.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2013-2018 Regents of the University of California.
+ * Copyright (c) 2013-2019 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -94,6 +94,7 @@
   // FIB Management
   FibEntry      = 128,
   NextHopRecord = 129,
+  EndpointId    = 113,
 
   // Strategy Choice Management
   StrategyChoice = 128,
diff --git a/ndn-cxx/mgmt/nfd/fib-entry.cpp b/ndn-cxx/mgmt/nfd/fib-entry.cpp
index 94154e3..6fcb8e5 100644
--- a/ndn-cxx/mgmt/nfd/fib-entry.cpp
+++ b/ndn-cxx/mgmt/nfd/fib-entry.cpp
@@ -53,6 +53,22 @@
 }
 
 NextHopRecord&
+NextHopRecord::setEndpointId(uint64_t endpointId)
+{
+  m_endpointId = endpointId;
+  m_wire.reset();
+  return *this;
+}
+
+NextHopRecord&
+NextHopRecord::unsetEndpointId()
+{
+  m_endpointId = nullopt;
+  m_wire.reset();
+  return *this;
+}
+
+NextHopRecord&
 NextHopRecord::setCost(uint64_t cost)
 {
   m_cost = cost;
@@ -66,6 +82,9 @@
 {
   size_t totalLength = 0;
 
+  if (m_endpointId) {
+    totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::EndpointId, *m_endpointId);
+  }
   totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::Cost, m_cost);
   totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::FaceId, m_faceId);
 
@@ -120,22 +139,38 @@
   }
   m_cost = readNonNegativeInteger(*val);
   ++val;
+
+  if (val != m_wire.elements_end() && val->type() == tlv::nfd::EndpointId) {
+    m_endpointId = readNonNegativeInteger(*val);
+    ++val;
+  }
+  else {
+    m_endpointId = nullopt;
+  }
 }
 
 bool
 operator==(const NextHopRecord& a, const NextHopRecord& b)
 {
   return a.getFaceId() == b.getFaceId() &&
+      a.hasEndpointId() == b.hasEndpointId() &&
+      (!a.hasEndpointId() || a.getEndpointId() == b.getEndpointId()) &&
       a.getCost() == b.getCost();
 }
 
 std::ostream&
 operator<<(std::ostream& os, const NextHopRecord& nh)
 {
-  return os << "NextHopRecord("
-            << "FaceId: " << nh.getFaceId() << ", "
-            << "Cost: " << nh.getCost()
-            << ")";
+  os << "NextHopRecord("
+     << "FaceId: " << nh.getFaceId() << ", ";
+
+  if (nh.hasEndpointId()) {
+    os << "EndpointId: " << nh.getEndpointId() << ", ";
+  }
+
+  os << "Cost: " << nh.getCost();
+
+  return os << ")";
 }
 
 ////////////////////
diff --git a/ndn-cxx/mgmt/nfd/fib-entry.hpp b/ndn-cxx/mgmt/nfd/fib-entry.hpp
index 74dd611..72a79dc 100644
--- a/ndn-cxx/mgmt/nfd/fib-entry.hpp
+++ b/ndn-cxx/mgmt/nfd/fib-entry.hpp
@@ -54,6 +54,25 @@
   NextHopRecord&
   setFaceId(uint64_t faceId);
 
+  bool
+  hasEndpointId() const
+  {
+    return m_endpointId.has_value();
+  }
+
+  uint64_t
+  getEndpointId() const
+  {
+    BOOST_ASSERT(hasEndpointId());
+    return *m_endpointId;
+  }
+
+  NextHopRecord&
+  setEndpointId(uint64_t endpointId);
+
+  NextHopRecord&
+  unsetEndpointId();
+
   uint64_t
   getCost() const
   {
@@ -75,6 +94,7 @@
 
 private:
   uint64_t m_faceId;
+  optional<uint64_t> m_endpointId;
   uint64_t m_cost;
 
   mutable Block m_wire;
diff --git a/tests/unit/mgmt/nfd/fib-entry.t.cpp b/tests/unit/mgmt/nfd/fib-entry.t.cpp
index 2a84f67..b199ba9 100644
--- a/tests/unit/mgmt/nfd/fib-entry.t.cpp
+++ b/tests/unit/mgmt/nfd/fib-entry.t.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2013-2018 Regents of the University of California.
+ * Copyright (c) 2013-2019 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -39,8 +39,10 @@
   for (size_t i = 1; i < 4; i++) {
     nexthops.push_back(NextHopRecord()
                        .setFaceId(i * 10)
+                       .setEndpointId(i * 10 + 1)
                        .setCost(i * 100 + 100));
   }
+  nexthops.front().unsetEndpointId();
 
   return FibEntry()
       .setPrefix("/this/is/a/test")
@@ -51,11 +53,15 @@
 {
   NextHopRecord record1;
   record1.setFaceId(10)
+      .setEndpointId(7)
       .setCost(200);
   const Block& wire = record1.wireEncode();
 
   static const uint8_t expected[] = {
-    0x81, 0x06, 0x69, 0x01, 0x0a, 0x6a, 0x01, 0xc8
+    0x81, 0x09, // NextHopRecord
+          0x69, 0x01, 0x0a, // FaceId
+          0x6a, 0x01, 0xc8, // Cost
+          0x71, 0x01, 0x07 // EndpointId
   };
   BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
                                 wire.begin(), wire.end());
@@ -73,12 +79,20 @@
   record2 = record1;
   BOOST_CHECK_EQUAL(record1, record2);
 
+  record1.setEndpointId(7);
+  record2.setEndpointId(7);
+  BOOST_CHECK_EQUAL(record1, record2);
+
   record2.setFaceId(42);
   BOOST_CHECK_NE(record1, record2);
 
   record2 = record1;
   record2.setCost(42);
   BOOST_CHECK_NE(record1, record2);
+
+  record2 = record1;
+  record2.setEndpointId(42);
+  BOOST_CHECK_NE(record1, record2);
 }
 
 BOOST_AUTO_TEST_CASE(FibEntryNoNextHopsEncode)
@@ -105,16 +119,34 @@
   FibEntry entry1 = makeFibEntry();
   NextHopRecord oneMore;
   oneMore.setFaceId(40);
+  oneMore.setEndpointId(8);
   oneMore.setCost(500);
   entry1.addNextHopRecord(oneMore);
   const Block& wire = entry1.wireEncode();
 
-  static const uint8_t expected[] = {
-    0x80, 0x38, 0x07, 0x13, 0x08, 0x04, 0x74, 0x68, 0x69, 0x73, 0x08, 0x02, 0x69, 0x73, 0x08, 0x01,
-    0x61, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x81, 0x06, 0x69, 0x01, 0x0a, 0x6a, 0x01, 0xc8, 0x81,
-    0x07, 0x69, 0x01, 0x14, 0x6a, 0x02, 0x01, 0x2c, 0x81, 0x07, 0x69, 0x01, 0x1e, 0x6a, 0x02, 0x01,
-    0x90, 0x81, 0x07, 0x69, 0x01, 0x28, 0x6a, 0x02, 0x01, 0xf4
-  };
+static const uint8_t expected[] = {
+     0x80, 0x41, // FibEntry
+           0x07, 0x13, // Name
+                 0x08, 0x04, 0x74, 0x68, 0x69, 0x73, // GenericNameComponent
+                 0x08, 0x02, 0x69, 0x73, // GenericNameComponent
+                 0x08, 0x01, 0x61, // GenericNameComponent
+                 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, // GenericNameComponent
+           0x81, 0x06, // NextHopRecord
+                 0x69, 0x01, 0x0a, // FaceId
+                 0x6a, 0x01, 0xc8, // Cost
+           0x81, 0x0a, // NextHopRecord
+                 0x69, 0x01, 0x14, // FaceId
+                 0x6a, 0x02, 0x01, 0x2c, // Cost
+                 0x71, 0x01, 0x15, // EndpointId
+           0x81, 0x0a, // NextHopRecord
+                 0x69, 0x01, 0x1e, // FaceId
+                 0x6a, 0x02, 0x01, 0x90, // Cost
+                 0x71, 0x01, 0x1f, // EndpointId
+           0x81, 0x0a, // NextHopRecord
+                 0x69, 0x01, 0x28, // FaceId
+                 0x6a, 0x02, 0x01, 0xf4, // Cost
+                 0x71, 0x01, 0x08 // EndpointId
+   };
   BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
                                 wire.begin(), wire.end());
 
@@ -143,6 +175,7 @@
   entry2 = entry1;
   auto nh1 = NextHopRecord()
              .setFaceId(1)
+             .setEndpointId(7)
              .setCost(1000);
   entry1.addNextHopRecord(nh1);
   BOOST_CHECK_NE(entry1, entry2);
@@ -150,6 +183,7 @@
 
   auto nh42 = NextHopRecord()
               .setFaceId(42)
+              .setEndpointId(22)
               .setCost(42);
   entry1.addNextHopRecord(nh42);
   entry2.addNextHopRecord(nh42)
@@ -182,8 +216,8 @@
   BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(entry),
                     "FibEntry(Prefix: /this/is/a/test,\n"
                     "         NextHops: [NextHopRecord(FaceId: 10, Cost: 200),\n"
-                    "                    NextHopRecord(FaceId: 20, Cost: 300),\n"
-                    "                    NextHopRecord(FaceId: 30, Cost: 400)]\n"
+                    "                    NextHopRecord(FaceId: 20, EndpointId: 21, Cost: 300),\n"
+                    "                    NextHopRecord(FaceId: 30, EndpointId: 31, Cost: 400)]\n"
                     "         )");
 }