Allow HTTP metadata URLs to return 404 until available
- Use exponential backoff to wait for HTTP URLs

Change-Id: Iefae1bdbaa4179974d356c27a223c0ab09803c4e
diff --git a/core/net/src/main/java/org/onosproject/cluster/impl/ConfigFileBasedClusterMetadataProvider.java b/core/net/src/main/java/org/onosproject/cluster/impl/ConfigFileBasedClusterMetadataProvider.java
index 7bfab0f..b097e2a 100644
--- a/core/net/src/main/java/org/onosproject/cluster/impl/ConfigFileBasedClusterMetadataProvider.java
+++ b/core/net/src/main/java/org/onosproject/cluster/impl/ConfigFileBasedClusterMetadataProvider.java
@@ -21,7 +21,6 @@
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
-import java.io.InputStream;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.util.Set;
@@ -169,13 +168,9 @@
             if ("file".equals(url.getProtocol())) {
                 File file = new File(metadataUrl.replaceFirst("file://", ""));
                 return file.exists();
-            } else if ("http".equals(url.getProtocol())) {
-                try (InputStream file = url.openStream()) {
-                    return true;
-                }
             } else {
-                // Unsupported protocol
-                return false;
+                // Return true for HTTP URLs since we allow blocking until HTTP servers come up
+                return "http".equals(url.getProtocol());
             }
         } catch (Exception e) {
             log.warn("Exception accessing metadata file at {}:", metadataUrl, e);
@@ -184,6 +179,7 @@
     }
 
     private Versioned<ClusterMetadata> blockForMetadata(String metadataUrl) {
+        int iterations = 0;
         for (;;) {
             Versioned<ClusterMetadata> metadata = fetchMetadata(metadataUrl);
             if (metadata != null) {
@@ -191,7 +187,7 @@
             }
 
             try {
-                Thread.sleep(10);
+                Thread.sleep(Math.min((int) Math.pow(2, ++iterations) * 10, 1000));
             } catch (InterruptedException e) {
                 throw Throwables.propagate(e);
             }
@@ -209,6 +205,10 @@
                 metadata = mapper.readValue(new FileInputStream(file), ClusterMetadata.class);
             } else if ("http".equals(url.getProtocol())) {
                 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+                if (conn.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
+                    log.warn("Could not reach metadata URL {}. Retrying...", url);
+                    return null;
+                }
                 if (conn.getResponseCode() == HttpURLConnection.HTTP_NO_CONTENT) {
                     return null;
                 }