Adding ability to easily create name thread factories with threads belonging to hierarchical thread groups.

Change-Id: Iaab3251c13e14b73c54a8edc945f5aa476a7ca54
diff --git a/utils/misc/src/main/java/org/onlab/util/GroupedThreadFactory.java b/utils/misc/src/main/java/org/onlab/util/GroupedThreadFactory.java
new file mode 100644
index 0000000..9001cf5
--- /dev/null
+++ b/utils/misc/src/main/java/org/onlab/util/GroupedThreadFactory.java
@@ -0,0 +1,88 @@
+/*
+ * 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.util;
+
+import org.apache.commons.lang3.concurrent.ConcurrentUtils;
+
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ThreadFactory;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+
+/**
+ * Thread factory for creating threads that belong to the specified thread group.
+ */
+public final class GroupedThreadFactory implements ThreadFactory {
+
+    public static final String DELIMITER = "/";
+
+    private final ThreadGroup group;
+
+    // Cache of created thread factories.
+    private static final ConcurrentHashMap<String, GroupedThreadFactory> FACTORIES =
+            new ConcurrentHashMap<>();
+
+    /**
+     * Returns thread factory for producing threads associated with the specified
+     * group name. The group name-space is hierarchical, based on slash-delimited
+     * name segments, e.g. {@code onos/intent}.
+     *
+     * @param groupName group name
+     * @return thread factory
+     */
+    public static GroupedThreadFactory groupedThreadFactory(String groupName) {
+        GroupedThreadFactory factory = FACTORIES.get(groupName);
+        if (factory != null) {
+            return factory;
+        }
+
+        // Find the parent group or root the group hierarchy under default group.
+        int i = groupName.lastIndexOf(DELIMITER);
+        if (i > 0) {
+            String name = groupName.substring(0, i);
+            ThreadGroup parentGroup = groupedThreadFactory(name).threadGroup();
+            factory = new GroupedThreadFactory(new ThreadGroup(parentGroup, groupName));
+        } else {
+            factory = new GroupedThreadFactory(new ThreadGroup(groupName));
+        }
+
+        return ConcurrentUtils.putIfAbsent(FACTORIES, groupName, factory);
+    }
+
+    // Creates a new thread group
+    private GroupedThreadFactory(ThreadGroup group) {
+        this.group = group;
+    }
+
+    /**
+     * Returns the thread group associated with the factory.
+     *
+     * @return thread group
+     */
+    public ThreadGroup threadGroup() {
+        return group;
+    }
+
+    @Override
+    public Thread newThread(Runnable r) {
+        return new Thread(group, r);
+    }
+
+    @Override
+    public String toString() {
+        return toStringHelper(this).add("group", group).toString();
+    }
+}
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 5e28fcd..9e690c0 100644
--- a/utils/misc/src/main/java/org/onlab/util/Tools.java
+++ b/utils/misc/src/main/java/org/onlab/util/Tools.java
@@ -15,16 +15,16 @@
  */
 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 com.google.common.base.Strings;
+import com.google.common.primitives.UnsignedLongs;
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+import org.slf4j.Logger;
 
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
-import java.lang.Thread.UncaughtExceptionHandler;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.FileVisitResult;
 import java.nio.file.Files;
@@ -38,11 +38,10 @@
 import java.util.List;
 import java.util.concurrent.ThreadFactory;
 
-import org.slf4j.Logger;
-
-import com.google.common.base.Strings;
-import com.google.common.primitives.UnsignedLongs;
-import com.google.common.util.concurrent.ThreadFactoryBuilder;
+import static java.nio.file.Files.delete;
+import static java.nio.file.Files.walkFileTree;
+import static org.onlab.util.GroupedThreadFactory.groupedThreadFactory;
+import static org.slf4j.LoggerFactory.getLogger;
 
 public abstract class Tools {
 
@@ -62,13 +61,25 @@
         return new ThreadFactoryBuilder()
                 .setNameFormat(pattern)
                         // FIXME remove UncaughtExceptionHandler before release
-                .setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
+                .setUncaughtExceptionHandler((t, e) -> log.error("Uncaught exception on {}", t.getName(), e)).build();
+    }
 
-                    @Override
-                    public void uncaughtException(Thread t, Throwable e) {
-                        log.error("Uncaught exception on {}", t.getName(), e);
-                    }
-                }).build();
+    /**
+     * Returns a thread factory that produces threads named according to the
+     * supplied name pattern and from the specified thread-group. The thread
+     * group name is expected to be specified in slash-delimited format, e.g.
+     * {@code onos/intent}.
+     *
+     * @param groupName group name in slash-delimited format to indicate hierarchy
+     * @param pattern   name pattern
+     * @return thread factory
+     */
+    public static ThreadFactory groupedThreads(String groupName, String pattern) {
+        return new ThreadFactoryBuilder()
+                .setThreadFactory(groupedThreadFactory(groupName))
+                .setNameFormat(pattern)
+                        // FIXME remove UncaughtExceptionHandler before release
+                .setUncaughtExceptionHandler((t, e) -> log.error("Uncaught exception on {}", t.getName(), e)).build();
     }
 
     /**
diff --git a/utils/misc/src/test/java/org/onlab/util/GroupedThreadFactoryTest.java b/utils/misc/src/test/java/org/onlab/util/GroupedThreadFactoryTest.java
new file mode 100644
index 0000000..5be1cda
--- /dev/null
+++ b/utils/misc/src/test/java/org/onlab/util/GroupedThreadFactoryTest.java
@@ -0,0 +1,53 @@
+/*
+ * 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.util;
+
+import org.junit.Test;
+import org.onlab.junit.TestTools;
+
+import static org.junit.Assert.*;
+
+/**
+ * Tests of the group thread factory.
+ */
+public class GroupedThreadFactoryTest {
+
+    @Test
+    public void basics() {
+        GroupedThreadFactory a = GroupedThreadFactory.groupedThreadFactory("foo");
+        GroupedThreadFactory b = GroupedThreadFactory.groupedThreadFactory("foo");
+        assertSame("factories should be same", a, b);
+
+        assertTrue("wrong toString", a.toString().contains("foo"));
+        Thread t = a.newThread(() -> TestTools.print("yo"));
+        assertSame("wrong group", a.threadGroup(), t.getThreadGroup());
+    }
+
+    @Test
+    public void hierarchical() {
+        GroupedThreadFactory a = GroupedThreadFactory.groupedThreadFactory("foo/bar");
+        GroupedThreadFactory b = GroupedThreadFactory.groupedThreadFactory("foo/goo");
+        GroupedThreadFactory p = GroupedThreadFactory.groupedThreadFactory("foo");
+
+        assertSame("groups should be same", p.threadGroup(), a.threadGroup().getParent());
+        assertSame("groups should be same", p.threadGroup(), b.threadGroup().getParent());
+
+        assertEquals("wrong name", "foo/bar", a.threadGroup().getName());
+        assertEquals("wrong name", "foo/goo", b.threadGroup().getName());
+        assertEquals("wrong name", "foo", p.threadGroup().getName());
+    }
+
+}
\ No newline at end of file
diff --git a/utils/misc/src/test/java/org/onlab/util/ToolsTest.java b/utils/misc/src/test/java/org/onlab/util/ToolsTest.java
index bb5f7b4..5c361bd 100644
--- a/utils/misc/src/test/java/org/onlab/util/ToolsTest.java
+++ b/utils/misc/src/test/java/org/onlab/util/ToolsTest.java
@@ -16,6 +16,9 @@
 package org.onlab.util;
 
 import org.junit.Test;
+import org.onlab.junit.TestTools;
+
+import java.util.concurrent.ThreadFactory;
 
 import static org.junit.Assert.*;
 
@@ -42,4 +45,20 @@
         assertEquals("ffffffffffffffff", Tools.toHex(0xffffffffffffffffL));
 
     }
+
+    @Test
+    public  void namedThreads() {
+        ThreadFactory f = Tools.namedThreads("foo-%d");
+        Thread t = f.newThread(() -> TestTools.print("yo"));
+        assertTrue("wrong pattern", t.getName().startsWith("foo-"));
+    }
+
+    @Test
+    public  void groupedThreads() {
+        ThreadFactory f = Tools.groupedThreads("foo/bar", "foo-%d");
+        Thread t = f.newThread(() -> TestTools.print("yo"));
+        assertTrue("wrong pattern", t.getName().startsWith("foo-"));
+        assertTrue("wrong group", t.getThreadGroup().getName().equals("foo/bar"));
+    }
+
 }