ONOS-542 Defining application subsystem interfaces & public constructs.

Change-Id: Iba0d2cb69dace5beee8a68def9918059ce755b5c
diff --git a/utils/junit/src/main/java/org/onlab/junit/ExceptionTest.java b/utils/junit/src/main/java/org/onlab/junit/ExceptionTest.java
new file mode 100644
index 0000000..09b3fe3
--- /dev/null
+++ b/utils/junit/src/main/java/org/onlab/junit/ExceptionTest.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.junit;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+
+/**
+ * Base for exception tests.
+ */
+public abstract class ExceptionTest {
+
+    protected static final Throwable CAUSE = new RuntimeException("boom");
+    protected static final String MESSAGE = "Uh oh.... boom";
+
+    protected abstract Exception getDefault();
+    protected abstract Exception getWithMessage();
+    protected abstract Exception getWithMessageAndCause();
+
+    @Test
+    public void noMessageNoCause() {
+        Exception e = getDefault();
+        assertEquals("incorrect message", null, e.getMessage());
+        assertEquals("incorrect cause", null, e.getCause());
+    }
+
+    @Test
+    public void withMessage() {
+        Exception e = getWithMessage();
+        assertEquals("incorrect message", MESSAGE, e.getMessage());
+        assertEquals("incorrect cause", null, e.getCause());
+    }
+
+    @Test
+    public void withCause() {
+        Exception e = getWithMessageAndCause();
+        assertEquals("incorrect message", MESSAGE, e.getMessage());
+        assertSame("incorrect cause", CAUSE, e.getCause());
+    }
+}
diff --git a/utils/junit/src/main/java/org/onlab/junit/TestTools.java b/utils/junit/src/main/java/org/onlab/junit/TestTools.java
index 550a028..400f710 100644
--- a/utils/junit/src/main/java/org/onlab/junit/TestTools.java
+++ b/utils/junit/src/main/java/org/onlab/junit/TestTools.java
@@ -15,6 +15,14 @@
  */
 package org.onlab.junit;
 
+import com.google.common.collect.ImmutableList;
+import com.google.common.io.Files;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Random;
+
 import static com.google.common.base.Preconditions.checkArgument;
 import static org.junit.Assert.fail;
 
@@ -23,6 +31,8 @@
  */
 public final class TestTools {
 
+    private static final Random RANDOM = new Random();
+
     // Prohibit construction
     private TestTools() {
     }
@@ -109,4 +119,92 @@
         assertAfter(0, duration, assertions);
     }
 
+
+    /**
+     * Creates a directory tree of test files. To signify creating a directory
+     * file path should end with '/'.
+     *
+     * @param paths list of file paths
+     * @return list of created files
+     * @throws java.io.IOException if there is an issue
+     */
+    public static List<File> createTestFiles(List<String> paths) throws IOException {
+        return createTestFiles(paths, 32, 1024);
+    }
+
+    /**
+     * Creates a directory tree of test files. To signify creating a directory
+     * file path should end with '/'.
+     *
+     * @param paths   list of file paths
+     * @param minSize minimum file size in bytes
+     * @param maxSize maximum file size in bytes
+     * @return list of created files
+     * @throws java.io.IOException if there is an issue
+     */
+    public static List<File> createTestFiles(List<String> paths,
+                                             int minSize, int maxSize) throws IOException {
+        ImmutableList.Builder<File> files = ImmutableList.builder();
+        for (String p : paths) {
+            File f = new File(p);
+            if (p.endsWith("/")) {
+                if (f.mkdirs()) {
+                    files.add(f);
+                }
+            } else {
+                Files.createParentDirs(f);
+                if (f.createNewFile()) {
+                    writeRandomFile(f, minSize, maxSize);
+                    files.add(f);
+                }
+            }
+        }
+        return files.build();
+    }
+
+    /**
+     * Writes random binary content into the specified file. The number of
+     * bytes will be random between the given minimum and maximum.
+     *
+     * @param file    file to write data to
+     * @param minSize minimum number of bytes to write
+     * @param maxSize maximum number of bytes to write
+     * @throws IOException if there is an issue
+     */
+    public static void writeRandomFile(File file, int minSize, int maxSize) throws IOException {
+        int size = minSize + (minSize == maxSize ? 0 : RANDOM.nextInt(maxSize - minSize));
+        byte[] data = new byte[size];
+        tweakBytes(RANDOM, data, size / 4);
+        Files.write(data, file);
+    }
+
+
+    /**
+     * Tweaks the given number of bytes in a byte array.
+     *
+     * @param random random number generator
+     * @param data   byte array to be tweaked
+     * @param count  number of bytes to tweak
+     */
+    public static void tweakBytes(Random random, byte[] data, int count) {
+        tweakBytes(random, data, count, 0, data.length);
+    }
+
+    /**
+     * Tweaks the given number of bytes in the specified range of a byte array.
+     *
+     * @param random random number generator
+     * @param data   byte array to be tweaked
+     * @param count  number of bytes to tweak
+     * @param start  index at beginning of range (inclusive)
+     * @param end    index at end of range (exclusive)
+     */
+    public static void tweakBytes(Random random, byte[] data, int count,
+                                  int start, int end) {
+        int len = end - start;
+        for (int i = 0; i < count; i++) {
+            data[start + random.nextInt(len)] = (byte) random.nextInt();
+        }
+    }
+
 }
diff --git a/utils/misc/src/main/java/org/onlab/util/Tools.java b/utils/misc/src/main/java/org/onlab/util/Tools.java
index e908300..498f3b1 100644
--- a/utils/misc/src/main/java/org/onlab/util/Tools.java
+++ b/utils/misc/src/main/java/org/onlab/util/Tools.java
@@ -15,6 +15,8 @@
  */
 package org.onlab.util;
 
+import static java.nio.file.Files.delete;
+import static java.nio.file.Files.walkFileTree;
 import static org.slf4j.LoggerFactory.getLogger;
 
 import java.io.BufferedReader;
@@ -24,6 +26,11 @@
 import java.io.InputStreamReader;
 import java.lang.Thread.UncaughtExceptionHandler;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.ThreadFactory;
@@ -39,7 +46,7 @@
     private Tools() {
     }
 
-    private static final Logger TOOLS_LOG = getLogger(Tools.class);
+    private static final Logger log = getLogger(Tools.class);
 
     /**
      * Returns a thread factory that produces threads named according to the
@@ -51,12 +58,12 @@
     public static ThreadFactory namedThreads(String pattern) {
         return new ThreadFactoryBuilder()
                 .setNameFormat(pattern)
-                // FIXME remove UncaughtExceptionHandler before release
+                        // FIXME remove UncaughtExceptionHandler before release
                 .setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
 
                     @Override
                     public void uncaughtException(Thread t, Throwable e) {
-                        TOOLS_LOG.error("Uncaught exception on {}", t.getName(), e);
+                        log.error("Uncaught exception on {}", t.getName(), e);
                     }
                 }).build();
     }
@@ -69,9 +76,9 @@
      */
     public static ThreadFactory minPriority(ThreadFactory factory) {
         return new ThreadFactoryBuilder()
-                    .setThreadFactory(factory)
-                    .setPriority(Thread.MIN_PRIORITY)
-                    .build();
+                .setThreadFactory(factory)
+                .setPriority(Thread.MIN_PRIORITY)
+                .build();
     }
 
     /**
@@ -127,7 +134,7 @@
     public static List<String> slurp(File path) {
         try {
             BufferedReader br = new BufferedReader(
-                new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8));
+                    new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8));
 
             List<String> lines = new ArrayList<>();
             String line;
@@ -141,4 +148,56 @@
         }
     }
 
+
+    /**
+     * Purges the specified directory path.&nbsp;Use with great caution since
+     * no attempt is made to check for symbolic links, which could result in
+     * deletion of unintended files.
+     *
+     * @param path directory to be removed
+     * @throws java.io.IOException if unable to remove contents
+     */
+    public static void removeDirectory(String path) throws IOException {
+        walkFileTree(Paths.get(path), new DirectoryDeleter());
+    }
+
+    /**
+     * Purges the specified directory path.&nbsp;Use with great caution since
+     * no attempt is made to check for symbolic links, which could result in
+     * deletion of unintended files.
+     *
+     * @param dir directory to be removed
+     * @throws java.io.IOException if unable to remove contents
+     */
+    public static void removeDirectory(File dir) throws IOException {
+        walkFileTree(Paths.get(dir.getAbsolutePath()), new DirectoryDeleter());
+    }
+
+
+    private static class DirectoryDeleter extends SimpleFileVisitor<Path> {
+        @Override
+        public FileVisitResult visitFile(Path file, BasicFileAttributes attributes)
+                throws IOException {
+            if (attributes.isRegularFile()) {
+                delete(file);
+            }
+            return FileVisitResult.CONTINUE;
+        }
+
+        @Override
+        public FileVisitResult postVisitDirectory(Path directory, IOException ioe)
+                throws IOException {
+            delete(directory);
+            return FileVisitResult.CONTINUE;
+        }
+
+        @Override
+        public FileVisitResult visitFileFailed(Path file, IOException ioe)
+                throws IOException {
+            log.warn("Unable to delete file {}", file);
+            log.warn("Boom", ioe);
+            return FileVisitResult.CONTINUE;
+        }
+    }
+
 }