fw: instantiate strategy of next higher version

When an exact strategy version is unavailable, strategy registry now
instantiates the next higher version which can offer backwards compatibility.

All existing strategies do not support other versions.

refs #3868

Change-Id: Id2cf9ae3a1fa078ce8be815eea9396f5aa81ecbb
diff --git a/daemon/fw/access-strategy.cpp b/daemon/fw/access-strategy.cpp
index 3705037..b391ba9 100644
--- a/daemon/fw/access-strategy.cpp
+++ b/daemon/fw/access-strategy.cpp
@@ -42,6 +42,10 @@
   if (!parsed.parameters.empty()) {
     BOOST_THROW_EXCEPTION(std::invalid_argument("AccessStrategy does not accept parameters"));
   }
+  if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
+    BOOST_THROW_EXCEPTION(std::invalid_argument(
+      "AccessStrategy does not support version " + std::to_string(*parsed.version)));
+  }
   this->setInstanceName(makeInstanceName(name, getStrategyName()));
 }
 
diff --git a/daemon/fw/asf-strategy.cpp b/daemon/fw/asf-strategy.cpp
index 076a3bb..45b0d72 100644
--- a/daemon/fw/asf-strategy.cpp
+++ b/daemon/fw/asf-strategy.cpp
@@ -50,6 +50,10 @@
   if (!parsed.parameters.empty()) {
     BOOST_THROW_EXCEPTION(std::invalid_argument("AsfStrategy does not accept parameters"));
   }
+  if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
+    BOOST_THROW_EXCEPTION(std::invalid_argument(
+      "AsfStrategy does not support version " + std::to_string(*parsed.version)));
+  }
   this->setInstanceName(makeInstanceName(name, getStrategyName()));
 }
 
diff --git a/daemon/fw/best-route-strategy.cpp b/daemon/fw/best-route-strategy.cpp
index ad70ab6..b5f62a8 100644
--- a/daemon/fw/best-route-strategy.cpp
+++ b/daemon/fw/best-route-strategy.cpp
@@ -67,6 +67,10 @@
   if (!parsed.parameters.empty()) {
     BOOST_THROW_EXCEPTION(std::invalid_argument("BestRouteStrategy does not accept parameters"));
   }
+  if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
+    BOOST_THROW_EXCEPTION(std::invalid_argument(
+      "BestRouteStrategy does not support version " + std::to_string(*parsed.version)));
+  }
   this->setInstanceName(makeInstanceName(name, getStrategyName()));
 }
 
diff --git a/daemon/fw/best-route-strategy2.cpp b/daemon/fw/best-route-strategy2.cpp
index 0ba94e8..974567a 100644
--- a/daemon/fw/best-route-strategy2.cpp
+++ b/daemon/fw/best-route-strategy2.cpp
@@ -46,6 +46,10 @@
   if (!parsed.parameters.empty()) {
     BOOST_THROW_EXCEPTION(std::invalid_argument("BestRouteStrategy2 does not accept parameters"));
   }
+  if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
+    BOOST_THROW_EXCEPTION(std::invalid_argument(
+      "BestRouteStrategy2 does not support version " + std::to_string(*parsed.version)));
+  }
   this->setInstanceName(makeInstanceName(name, getStrategyName()));
 }
 
diff --git a/daemon/fw/client-control-strategy.cpp b/daemon/fw/client-control-strategy.cpp
index 8c99db2..b4bb601 100644
--- a/daemon/fw/client-control-strategy.cpp
+++ b/daemon/fw/client-control-strategy.cpp
@@ -39,6 +39,10 @@
   if (!parsed.parameters.empty()) {
     BOOST_THROW_EXCEPTION(std::invalid_argument("ClientControlStrategy does not accept parameters"));
   }
+  if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
+    BOOST_THROW_EXCEPTION(std::invalid_argument(
+      "ClientControlStrategy does not support version " + std::to_string(*parsed.version)));
+  }
   this->setInstanceName(makeInstanceName(name, getStrategyName()));
 
   NFD_LOG_WARN("NextHopFaceId field is honored universally and "
diff --git a/daemon/fw/multicast-strategy.cpp b/daemon/fw/multicast-strategy.cpp
index 7a416e3..cbf409c 100644
--- a/daemon/fw/multicast-strategy.cpp
+++ b/daemon/fw/multicast-strategy.cpp
@@ -38,6 +38,10 @@
   if (!parsed.parameters.empty()) {
     BOOST_THROW_EXCEPTION(std::invalid_argument("MulticastStrategy does not accept parameters"));
   }
+  if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
+    BOOST_THROW_EXCEPTION(std::invalid_argument(
+      "MulticastStrategy does not support version " + std::to_string(*parsed.version)));
+  }
   this->setInstanceName(makeInstanceName(name, getStrategyName()));
 }
 
diff --git a/daemon/fw/ncc-strategy.cpp b/daemon/fw/ncc-strategy.cpp
index cfc22cb..6369c2f 100644
--- a/daemon/fw/ncc-strategy.cpp
+++ b/daemon/fw/ncc-strategy.cpp
@@ -43,6 +43,10 @@
   if (!parsed.parameters.empty()) {
     BOOST_THROW_EXCEPTION(std::invalid_argument("NccStrategy does not accept parameters"));
   }
+  if (parsed.version && *parsed.version != getStrategyName()[-1].toVersion()) {
+    BOOST_THROW_EXCEPTION(std::invalid_argument(
+      "NccStrategy does not support version " + std::to_string(*parsed.version)));
+  }
   this->setInstanceName(makeInstanceName(name, getStrategyName()));
 }
 
diff --git a/daemon/fw/strategy.cpp b/daemon/fw/strategy.cpp
index 3009123..9f2dd19 100644
--- a/daemon/fw/strategy.cpp
+++ b/daemon/fw/strategy.cpp
@@ -48,24 +48,36 @@
   const Registry& registry = getRegistry();
   ParsedInstanceName parsed = parseInstanceName(instanceName);
 
-  ///\todo #3868 accommodate parameters
   if (parsed.version) {
-    NFD_LOG_TRACE("find " << instanceName <<
-                  " strategyName=" << parsed.strategyName << " versioned");
-    ///\todo #3868 if exact version unavailable, choose closest higher version
-    return registry.find(parsed.strategyName);
+    // specified version: find exact or next higher version
+
+    auto found = registry.lower_bound(parsed.strategyName);
+    if (found != registry.end()) {
+      if (parsed.strategyName.getPrefix(-1).isPrefixOf(found->first)) {
+        NFD_LOG_TRACE("find " << instanceName << " versioned found=" << found->first);
+        return found;
+      }
+    }
+
+    NFD_LOG_TRACE("find " << instanceName << " versioned not-found");
+    return registry.end();
   }
 
-  NFD_LOG_TRACE("find " << instanceName <<
-                " strategyName=" << parsed.strategyName << " unversioned");
-  Registry::const_iterator candidate = registry.end();
-  for (auto it = registry.lower_bound(parsed.strategyName);
-       it != registry.end() && parsed.strategyName.isPrefixOf(it->first); ++it) {
-    if (it->first.size() == parsed.strategyName.size() + 1) { // only one extra component: version
-      candidate = it;
+  // no version specified: find highest version
+
+  if (!parsed.strategyName.empty()) { // Name().getSuccessor() would be invalid
+    auto found = registry.lower_bound(parsed.strategyName.getSuccessor());
+    if (found != registry.begin()) {
+      --found;
+      if (parsed.strategyName.isPrefixOf(found->first)) {
+        NFD_LOG_TRACE("find " << instanceName << " unversioned found=" << found->first);
+        return found;
+      }
     }
   }
-  return candidate;
+
+  NFD_LOG_TRACE("find " << instanceName << " unversioned not-found");
+  return registry.end();
 }
 
 bool
diff --git a/daemon/fw/strategy.hpp b/daemon/fw/strategy.hpp
index 1f34ce2..b7b7adb 100644
--- a/daemon/fw/strategy.hpp
+++ b/daemon/fw/strategy.hpp
@@ -47,6 +47,7 @@
   static void
   registerType(const Name& strategyName = S::getStrategyName())
   {
+    BOOST_ASSERT(strategyName.size() > 1);
     BOOST_ASSERT(strategyName.at(-1).isVersion());
     Registry& registry = getRegistry();
     BOOST_ASSERT(registry.count(strategyName) == 0);