rib: put PrefixAnnouncement in Route and RibEntry

refs #4650

Change-Id: I03a846cd6e013d52f080bd7b092cd392b0284f88
diff --git a/rib/rib-entry.cpp b/rib/rib-entry.cpp
index ff646fa..d59338d 100644
--- a/rib/rib-entry.cpp
+++ b/rib/rib-entry.cpp
@@ -238,6 +238,39 @@
   return candidate;
 }
 
+ndn::PrefixAnnouncement
+RibEntry::getPrefixAnnouncement(time::milliseconds minExpiration,
+                                time::milliseconds maxExpiration) const
+{
+  const Route* bestAnnRoute = nullptr;
+  auto entryExpiry = time::steady_clock::TimePoint::min();
+
+  for (const Route& route : *this) {
+    if (route.expires) {
+      entryExpiry = std::max(entryExpiry, *route.expires);
+      if (route.announcement) {
+        if (bestAnnRoute == nullptr || *route.expires > *bestAnnRoute->expires) {
+          bestAnnRoute = &route;
+        }
+      }
+    }
+    else {
+      entryExpiry = time::steady_clock::TimePoint::max();
+    }
+  }
+
+  if (bestAnnRoute != nullptr) {
+    return *bestAnnRoute->announcement;
+  }
+
+  ndn::PrefixAnnouncement ann;
+  ann.setAnnouncedName(m_name);
+  ann.setExpiration(ndn::clamp(
+    time::duration_cast<time::milliseconds>(entryExpiry - time::steady_clock::now()),
+    minExpiration, maxExpiration));
+  return ann;
+}
+
 std::ostream&
 operator<<(std::ostream& os, const RibEntry& entry)
 {
diff --git a/rib/rib-entry.hpp b/rib/rib-entry.hpp
index 161ebd4..60da09b 100644
--- a/rib/rib-entry.hpp
+++ b/rib/rib-entry.hpp
@@ -165,6 +165,22 @@
   const Route*
   getRouteWithLowestCostAndChildInheritByFaceId(uint64_t faceId) const;
 
+  /** \brief Retrieve a prefix announcement suitable for readvertising this route.
+   *
+   *  If one or more routes in this RIB entry contains a prefix announcement, this method returns
+   *  the announcement from the route that expires last.
+   *
+   *  If this RIB entry does not have a route containing a prefix announcement, this method creates
+   *  a new announcement. Its expiration period reflects the remaining lifetime of this RIB entry,
+   *  confined within [\p minExpiration, \p maxExpiration] range. The caller is expected to sign
+   *  this announcement.
+   *
+   *  \warning (minExpiration > maxExpiration) triggers undefined behavior.
+   */
+  ndn::PrefixAnnouncement
+  getPrefixAnnouncement(time::milliseconds minExpiration = 15_s,
+                        time::milliseconds maxExpiration = 1_h) const;
+
   const_iterator
   begin() const;
 
diff --git a/rib/rib.cpp b/rib/rib.cpp
index 542a582..5385cf1 100644
--- a/rib/rib.cpp
+++ b/rib/rib.cpp
@@ -113,12 +113,10 @@
         scheduler::cancel(entryIt->getExpirationEvent());
       }
 
+      *entryIt = route;
+
       // No checks are required here as the iterator needs to be updated in all cases.
       entryIt->setExpirationEvent(route.getExpirationEvent());
-
-      entryIt->flags = route.flags;
-      entryIt->cost = route.cost;
-      entryIt->expires = route.expires;
     }
   }
   else {
diff --git a/rib/route.cpp b/rib/route.cpp
index 08e469b..04999bb 100644
--- a/rib/route.cpp
+++ b/rib/route.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2017,  Regents of the University of California,
+/*
+ * Copyright (c) 2014-2018,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -29,11 +29,33 @@
 namespace nfd {
 namespace rib {
 
-Route::Route()
-  : faceId(0)
-  , origin(ndn::nfd::ROUTE_ORIGIN_APP)
-  , cost(0)
-  , flags(ndn::nfd::ROUTE_FLAGS_NONE)
+const uint64_t PA_ROUTE_COST = 2048; ///< cost of route created by prefix announcement
+
+static time::steady_clock::TimePoint
+computeExpiration(const ndn::PrefixAnnouncement& ann)
+{
+  time::steady_clock::Duration validityEnd = time::steady_clock::Duration::max();
+  if (ann.getValidityPeriod()) {
+    auto now = time::system_clock::now();
+    if (!ann.getValidityPeriod()->isValid(now)) {
+      validityEnd = time::steady_clock::Duration::zero();
+    }
+    else {
+      validityEnd = ann.getValidityPeriod()->getPeriod().second - now;
+    }
+  }
+  return time::steady_clock::now() +
+    std::min(validityEnd, time::duration_cast<time::steady_clock::Duration>(ann.getExpiration()));
+}
+
+Route::Route(const ndn::PrefixAnnouncement& ann, uint64_t faceId)
+  : faceId(faceId)
+  , origin(ndn::nfd::ROUTE_ORIGIN_PREFIXANN)
+  , cost(PA_ROUTE_COST)
+  , flags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT)
+  , expires(computeExpiration(ann))
+  , announcement(ann)
+  , annExpires(*expires)
 {
 }
 
@@ -44,7 +66,8 @@
          lhs.origin == rhs.origin &&
          lhs.flags == rhs.flags &&
          lhs.cost == rhs.cost &&
-         lhs.expires == rhs.expires;
+         lhs.expires == rhs.expires &&
+         lhs.announcement == rhs.announcement;
 }
 
 std::ostream&
@@ -61,7 +84,10 @@
   else {
     os << ", never expires";
   }
-  os << ")";
+  if (route.announcement) {
+    os << ", announcement: (" << *route.announcement << ')';
+  }
+  os << ')';
 
   return os;
 }
diff --git a/rib/route.hpp b/rib/route.hpp
index 5ad4d79..8106406 100644
--- a/rib/route.hpp
+++ b/rib/route.hpp
@@ -30,6 +30,7 @@
 
 #include <ndn-cxx/encoding/nfd-constants.hpp>
 #include <ndn-cxx/mgmt/nfd/route-flags-traits.hpp>
+#include <ndn-cxx/prefix-announcement.hpp>
 
 #include <type_traits>
 
@@ -41,7 +42,15 @@
 class Route : public ndn::nfd::RouteFlagsTraits<Route>
 {
 public:
-  Route();
+  /** \brief default constructor
+   */
+  Route() = default;
+
+  /** \brief construct from a prefix announcement
+   *  \param ann a prefix announcement that has passed verification
+   *  \param faceId the face on which \p ann arrived
+   */
+  Route(const ndn::PrefixAnnouncement& ann, uint64_t faceId);
 
   void
   setExpirationEvent(const scheduler::EventId eid)
@@ -62,12 +71,28 @@
   }
 
 public:
-  uint64_t faceId;
-  ndn::nfd::RouteOrigin origin;
-  uint64_t cost;
-  std::underlying_type<ndn::nfd::RouteFlags>::type flags;
+  uint64_t faceId = 0;
+  ndn::nfd::RouteOrigin origin = ndn::nfd::ROUTE_ORIGIN_APP;
+  uint64_t cost = 0;
+  std::underlying_type<ndn::nfd::RouteFlags>::type flags = ndn::nfd::ROUTE_FLAGS_NONE;
   optional<time::steady_clock::TimePoint> expires;
 
+  /** \brief The prefix announcement that caused the creation of this route.
+   *
+   *  This is nullopt if this route is not created by a prefix announcement.
+   */
+  optional<ndn::PrefixAnnouncement> announcement;
+
+  /** \brief Expiration time of the prefix announcement.
+   *
+   *  Valid only if announcement is not nullopt.
+   *
+   *  If this field is before or equal the current time, it indicates the prefix announcement is
+   *  not yet valid or has expired. In this case, the exact value of this field does not matter.
+   *  If this field is after the current time, it indicates when the prefix announcement expires.
+   */
+  time::steady_clock::TimePoint annExpires;
+
 private:
   scheduler::EventId m_expirationEvent;
 };
diff --git a/tests/rib/rib-entry.t.cpp b/tests/rib/rib-entry.t.cpp
index 65f73dc..a5c9f3c 100644
--- a/tests/rib/rib-entry.t.cpp
+++ b/tests/rib/rib-entry.t.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2017,  Regents of the University of California,
+/*
+ * Copyright (c) 2014-2018,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -30,7 +30,9 @@
 namespace rib {
 namespace tests {
 
-BOOST_FIXTURE_TEST_SUITE(TestRibEntry, nfd::tests::BaseFixture)
+using namespace nfd::tests;
+
+BOOST_FIXTURE_TEST_SUITE(TestRibEntry, BaseFixture)
 
 BOOST_AUTO_TEST_CASE(Basic)
 {
@@ -72,6 +74,100 @@
   BOOST_CHECK(entry.findRoute(route2) != entry.getRoutes().end());
 }
 
+BOOST_FIXTURE_TEST_SUITE(GetAnnouncement, UnitTestTimeFixture)
+
+static Route
+makeSimpleRoute(uint64_t faceId)
+{
+  Route route;
+  route.faceId = faceId;
+  return route;
+}
+
+static Route
+makeSimpleRoute(uint64_t faceId, time::steady_clock::Duration expiration)
+{
+  Route route = makeSimpleRoute(faceId);
+  route.expires = time::steady_clock::now() + expiration;
+  return route;
+}
+
+BOOST_AUTO_TEST_CASE(Empty)
+{
+  RibEntry entry;
+  // entry has no Route
+
+  advanceClocks(1_s);
+  auto pa = entry.getPrefixAnnouncement(15_s, 30_s);
+  BOOST_CHECK_GE(pa.getExpiration(), 15_s);
+  BOOST_CHECK_LE(pa.getExpiration(), 30_s);
+  // A RibEntry without Route does not have a defined expiration time,
+  // so this test only checks that there's no exception and the expiration period is clamped.
+}
+
+BOOST_AUTO_TEST_CASE(ReuseAnnouncement1)
+{
+  RibEntry entry;
+  entry.insertRoute(Route(makePrefixAnn("/A", 212_s), 5858));
+  entry.insertRoute(Route(makePrefixAnn("/A", 555_s), 2454));
+  entry.insertRoute(makeSimpleRoute(8282, 799_s));
+  entry.insertRoute(makeSimpleRoute(1141));
+
+  advanceClocks(1_s);
+  auto pa = entry.getPrefixAnnouncement(15_s, 30_s);
+  BOOST_CHECK_EQUAL(pa.getExpiration(), 555_s); // not modified and not clamped
+}
+
+BOOST_AUTO_TEST_CASE(ReuseAnnouncement2)
+{
+  RibEntry entry;
+  entry.insertRoute(Route(makePrefixAnn("/A", 212_s), 7557)); // expires at +212s
+
+  advanceClocks(100_s);
+  entry.insertRoute(Route(makePrefixAnn("/A", 136_s), 1010)); // expires at +236s
+
+  advanceClocks(1_s);
+  auto pa = entry.getPrefixAnnouncement(15_s, 30_s);
+  BOOST_CHECK_EQUAL(pa.getExpiration(), 136_s);
+  // Although face 7557's announcement has a longer expiration period,
+  // the route for face 1010 expires last and thus its announcement is chosen.
+}
+
+BOOST_AUTO_TEST_CASE(MakeAnnouncement)
+{
+  RibEntry entry;
+  entry.insertRoute(makeSimpleRoute(6398, 765_s));
+  entry.insertRoute(makeSimpleRoute(2954, 411_s));
+
+  advanceClocks(1_s);
+  auto pa = entry.getPrefixAnnouncement(15_s, 999_s);
+  BOOST_CHECK_EQUAL(pa.getExpiration(), 764_s);
+}
+
+BOOST_AUTO_TEST_CASE(MakeAnnouncementInfinite)
+{
+  RibEntry entry;
+  entry.insertRoute(makeSimpleRoute(4877, 240_s));
+  entry.insertRoute(makeSimpleRoute(5053));
+
+  advanceClocks(1_s);
+  auto pa = entry.getPrefixAnnouncement(15_s, 877_s);
+  BOOST_CHECK_EQUAL(pa.getExpiration(), 877_s); // clamped at maxExpiration
+}
+
+BOOST_AUTO_TEST_CASE(MakeAnnouncementShortExpiration)
+{
+  RibEntry entry;
+  entry.insertRoute(makeSimpleRoute(4877, 8_s));
+  entry.insertRoute(makeSimpleRoute(5053, 9_s));
+
+  advanceClocks(1_s);
+  auto pa = entry.getPrefixAnnouncement(15_s, 666_s);
+  BOOST_CHECK_EQUAL(pa.getExpiration(), 15_s); // clamped at minExpiration
+}
+
+BOOST_AUTO_TEST_SUITE_END() // GetAnnouncement
+
 BOOST_AUTO_TEST_SUITE_END() // TestRibEntry
 
 } // namespace tests
diff --git a/tests/rib/route.t.cpp b/tests/rib/route.t.cpp
index 4d66d0d..b1390b8 100644
--- a/tests/rib/route.t.cpp
+++ b/tests/rib/route.t.cpp
@@ -35,6 +35,59 @@
 
 BOOST_FIXTURE_TEST_SUITE(TestRoute, BaseFixture)
 
+BOOST_FIXTURE_TEST_SUITE(CreateFromAnnouncement, UnitTestTimeFixture)
+
+BOOST_AUTO_TEST_CASE(NoValidity)
+{
+  auto pa = makePrefixAnn("/A", 212_s);
+  Route route(pa, 1815);
+  BOOST_CHECK_EQUAL(route.faceId, 1815);
+  BOOST_CHECK_EQUAL(route.origin, ndn::nfd::ROUTE_ORIGIN_PREFIXANN);
+  BOOST_CHECK_EQUAL(route.cost, 2048);
+  BOOST_CHECK_EQUAL(route.flags, ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
+  BOOST_CHECK_EQUAL(route.annExpires, time::steady_clock::now() + 212_s);
+  BOOST_REQUIRE(route.expires);
+  BOOST_CHECK_EQUAL(*route.expires, route.annExpires);
+}
+
+BOOST_AUTO_TEST_CASE(BeforeNotBefore)
+{
+  auto pa = makePrefixAnn("/A", 212_s, {10_s, 80_s});
+  Route route(pa, 1053);
+  BOOST_CHECK_LE(route.annExpires, time::steady_clock::now());
+  BOOST_REQUIRE(route.expires);
+  BOOST_CHECK_LE(*route.expires, time::steady_clock::now());
+}
+
+BOOST_AUTO_TEST_CASE(AfterNotAfter)
+{
+  auto pa = makePrefixAnn("/A", 212_s, {-80_s, -10_s});
+  Route route(pa, 2972);
+  BOOST_CHECK_LE(route.annExpires, time::steady_clock::now());
+  BOOST_REQUIRE(route.expires);
+  BOOST_CHECK_LE(*route.expires, time::steady_clock::now());
+}
+
+BOOST_AUTO_TEST_CASE(ExpirationLtValidity)
+{
+  auto pa = makePrefixAnn("/A", 212_s, {-100_s, 300_s});
+  Route route(pa, 7804);
+  BOOST_CHECK_EQUAL(route.annExpires, time::steady_clock::now() + 212_s);
+  BOOST_REQUIRE(route.expires);
+  BOOST_CHECK_EQUAL(*route.expires, route.annExpires);
+}
+
+BOOST_AUTO_TEST_CASE(ValidityLtExpiration)
+{
+  auto pa = makePrefixAnn("/A", 212_s, {-100_s, 200_s});
+  Route route(pa, 7804);
+  BOOST_CHECK_EQUAL(route.annExpires, time::steady_clock::now() + 200_s);
+  BOOST_REQUIRE(route.expires);
+  BOOST_CHECK_EQUAL(*route.expires, route.annExpires);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // CreateFromAnnouncement
+
 BOOST_AUTO_TEST_CASE(Equality)
 {
   Route a;
@@ -45,7 +98,7 @@
   a.origin = b.origin = ndn::nfd::ROUTE_ORIGIN_NLSR;
   a.flags = b.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
   a.cost = b.cost = 28826;
-  a.expires = b.expires = time::steady_clock::now() + time::milliseconds(26782232);
+  a.expires = b.expires = time::steady_clock::now() + 26782232_ms;
   BOOST_CHECK_EQUAL(a, b);
 
   b.faceId = 18559;
@@ -71,6 +124,15 @@
   BOOST_CHECK_EQUAL(a, b);
 }
 
+BOOST_FIXTURE_TEST_CASE(EqualityAnn, UnitTestTimeFixture)
+{
+  auto ann1 = makePrefixAnn("/ann", 1_h);
+  auto ann2 = makePrefixAnn("/ann", 2_h);
+  BOOST_CHECK_EQUAL(Route(ann1, 7001), Route(ann1, 7001));
+  BOOST_CHECK_NE(Route(ann1, 7001), Route(ann1, 7002));
+  BOOST_CHECK_NE(Route(ann1, 7001), Route(ann2, 7001));
+}
+
 BOOST_FIXTURE_TEST_CASE(Output, UnitTestTimeFixture)
 {
   Route r;
@@ -81,13 +143,19 @@
   r.origin = ndn::nfd::ROUTE_ORIGIN_STATIC;
   r.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
   r.cost = 2312;
-  r.expires = time::steady_clock::now() + time::milliseconds(791214234);
+  r.expires = time::steady_clock::now() + 791214234_ms;
   BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(r),
-                    "Route(faceid: 4980, origin: static, cost: 2312, flags: 0x1, expires in: 791214234 milliseconds)");
+                    "Route(faceid: 4980, origin: static, cost: 2312, flags: 0x1, expires in: "
+                    "791214234 milliseconds)");
 
   r.expires = nullopt;
   BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(r),
                     "Route(faceid: 4980, origin: static, cost: 2312, flags: 0x1, never expires)");
+
+  r = Route(makePrefixAnn("/ann", 1_h), 3247);
+  BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(r),
+                    "Route(faceid: 3247, origin: prefixann, cost: 2048, flags: 0x1, expires in: "
+                    "3600000 milliseconds, announcement: (/ann expires=3600000 milliseconds))");
 }
 
 BOOST_AUTO_TEST_SUITE_END() // TestRoute
diff --git a/tests/test-common.cpp b/tests/test-common.cpp
index 21cb91a..62e728a 100644
--- a/tests/test-common.cpp
+++ b/tests/test-common.cpp
@@ -126,5 +126,33 @@
   return makeNack(std::move(interest), reason);
 }
 
+ndn::PrefixAnnouncement
+makePrefixAnn(const Name& announcedName, time::milliseconds expiration,
+              optional<ndn::security::ValidityPeriod> validity)
+{
+  ndn::PrefixAnnouncement pa;
+  pa.setAnnouncedName(announcedName);
+  pa.setExpiration(expiration);
+  pa.setValidityPeriod(validity);
+  return pa;
+}
+
+ndn::PrefixAnnouncement
+makePrefixAnn(const Name& announcedName, time::milliseconds expiration,
+              std::pair<time::seconds, time::seconds> validityFromNow)
+{
+  auto now = time::system_clock::now();
+  return makePrefixAnn(announcedName, expiration,
+    ndn::security::ValidityPeriod(now + validityFromNow.first, now + validityFromNow.second));
+}
+
+ndn::PrefixAnnouncement
+signPrefixAnn(ndn::PrefixAnnouncement&& pa, ndn::KeyChain& keyChain,
+              const ndn::security::SigningInfo& si, optional<uint64_t> version)
+{
+  pa.toData(keyChain, si, version);
+  return std::move(pa);
+}
+
 } // namespace tests
 } // namespace nfd
diff --git a/tests/test-common.hpp b/tests/test-common.hpp
index 1a4a0d5..c01484c 100644
--- a/tests/test-common.hpp
+++ b/tests/test-common.hpp
@@ -30,6 +30,7 @@
 
 #include "core/global-io.hpp"
 
+#include <ndn-cxx/prefix-announcement.hpp>
 #include <ndn-cxx/util/time-unit-test-clock.hpp>
 
 #ifdef HAVE_PRIVILEGE_DROP_AND_ELEVATE
@@ -175,6 +176,28 @@
   packet.setName(name);
 }
 
+/** \brief create a prefix announcement without signing
+ */
+ndn::PrefixAnnouncement
+makePrefixAnn(const Name& announcedName, time::milliseconds expiration,
+              optional<ndn::security::ValidityPeriod> validity = nullopt);
+
+/** \brief create a prefix announcement without signing
+ *  \param announcedName announced name
+ *  \param expiration expiration period
+ *  \param validityFromNow validity period, relative from now
+ */
+ndn::PrefixAnnouncement
+makePrefixAnn(const Name& announcedName, time::milliseconds expiration,
+              std::pair<time::seconds, time::seconds> validityFromNow);
+
+/** \brief sign a prefix announcement
+ */
+ndn::PrefixAnnouncement
+signPrefixAnn(ndn::PrefixAnnouncement&& pa, ndn::KeyChain& keyChain,
+              const ndn::security::SigningInfo& si = ndn::KeyChain::getDefaultSigningInfo(),
+              optional<uint64_t> version = nullopt);
+
 } // namespace tests
 } // namespace nfd