look for local cache first when running onos-lib-gen

Change-Id: I7dc6cef377154a87d43a18307398cad61d8e2e21
diff --git a/tools/build/libgen/src/main/java/org/onosproject/libgen/BuckLibGenerator.java b/tools/build/libgen/src/main/java/org/onosproject/libgen/BuckLibGenerator.java
index 05cd40a..0fddf1b 100644
--- a/tools/build/libgen/src/main/java/org/onosproject/libgen/BuckLibGenerator.java
+++ b/tools/build/libgen/src/main/java/org/onosproject/libgen/BuckLibGenerator.java
@@ -28,6 +28,8 @@
 import java.io.PrintWriter;
 import java.net.URL;
 import java.net.URLConnection;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.time.Instant;
@@ -36,6 +38,8 @@
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Stream;
 
 /**
  * Generates a BUCK file from a JSON file containing third-party library
@@ -103,7 +107,7 @@
         System.out.flush();
         BuckArtifact buckArtifact;
         if (uri.startsWith("http")) {
-            String sha = getHttpSha(uri);
+            String sha = getHttpSha(name, uri);
             buckArtifact = BuckArtifact.getArtifact(name, uri, sha);
         } else if (uri.startsWith("mvn")) {
             uri = uri.replaceFirst("mvn:", "");
@@ -198,16 +202,49 @@
         }
     }
 
-    String getHttpSha(String url) {
-        //TODO look in buck-out/gen first
+    String getHttpSha(String name, String urlStr) {
         //FIXME need http download cache
         try {
-            URLConnection connection = new URL(url).openConnection();
+            MessageDigest md = MessageDigest.getInstance("SHA-1");
+            byte[] buffer = new byte[8192];
+
+            URL url = new URL(urlStr);
+            String fname = new File(url.getPath()).getName();
+
+            // naively look for cache in buck-out/gen/
+            // only works for `buck fetch`-ed artifacts
+            // (=doesn't work for bndexe unless manually fetched)
+            Optional<File> cache = Optional.ofNullable(System.getenv("ONOS_ROOT"))
+                .map(Paths::get)
+                .map(Stream::of)
+                .orElseGet(Stream::empty)
+                // look for remote_file, remote_jar path
+                .flatMap(root -> Stream.of(root.resolve("buck-out/gen/lib/" + name + "/" + fname),
+                                           root.resolve("buck-out/gen/lib/" + fname + "/" + fname)))
+                .map(Path::toFile)
+                .filter(File::canRead)
+                .findAny();
+
+            if (cache.isPresent()) {
+                try (FileInputStream stream = new FileInputStream(cache.get())) {
+                    int read;
+                    while ((read = stream.read(buffer)) >= 0) {
+                        md.update(buffer, 0, read);
+                    }
+                    StringBuilder result = new StringBuilder();
+                    byte[] digest = md.digest();
+                    for (byte b : digest) {
+                        result.append(String.format("%02x", b));
+                    }
+                    return result.toString();
+                } catch (IOException e) {
+                    // fall back to regular download
+                }
+            }
+            URLConnection connection = url.openConnection();
             connection.connect();
             InputStream stream = connection.getInputStream();
-            MessageDigest md = MessageDigest.getInstance("SHA-1");
 
-            byte[] buffer = new byte[8192];
             int read;
             while ((read = stream.read(buffer)) >= 0) {
                 md.update(buffer, 0, read);