Implement command to view Raft partitions

Change-Id: I9d3cea49877d69c2d7935dadbbad2770349e793a
diff --git a/cli/src/main/java/org/onosproject/cli/net/PartitionsListCommand.java b/cli/src/main/java/org/onosproject/cli/net/PartitionsListCommand.java
new file mode 100644
index 0000000..955bbc2
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/PartitionsListCommand.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.onosproject.cli.net;
+
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.store.service.PartitionInfo;
+import org.onosproject.store.service.StorageAdminService;
+
+import java.util.List;
+
+/**
+ * Command to list the database partitions in the system.
+ */
+@Command(scope = "onos", name = "partitions",
+        description = "Lists information about partitions in the system")
+public class PartitionsListCommand extends AbstractShellCommand {
+
+    private static final String FMT = "%-20s %8s %25s %s";
+
+    @Override
+    protected void execute() {
+        StorageAdminService storageAdminService = get(StorageAdminService.class);
+        List<PartitionInfo> partitionInfo = storageAdminService.getPartitionInfo();
+
+        print(FMT, "Name", "Term", "Members", "");
+
+        for (PartitionInfo info : partitionInfo) {
+            boolean first = true;
+            for (String member : info.members()) {
+                if (first) {
+                    print(FMT, info.name(), info.term(), member,
+                          member.equals(info.leader()) ? "*" : "");
+                    first = false;
+                } else {
+                    print(FMT, "", "", member,
+                          member.equals(info.leader()) ? "*" : "");
+                }
+            }
+        }
+    }
+}
diff --git a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index 6102aa0..c772785 100644
--- a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -244,6 +244,9 @@
             <action class="org.onosproject.cli.net.ClustersListCommand"/>
         </command>
         <command>
+            <action class="org.onosproject.cli.net.PartitionsListCommand"/>
+        </command>
+        <command>
             <action class="org.onosproject.cli.net.ClusterDevicesCommand"/>
             <completers>
                 <ref component-id="clusterIdCompleter"/>
diff --git a/core/api/src/main/java/org/onosproject/store/service/PartitionInfo.java b/core/api/src/main/java/org/onosproject/store/service/PartitionInfo.java
new file mode 100644
index 0000000..a0f0648
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/store/service/PartitionInfo.java
@@ -0,0 +1,81 @@
+/*
+ * 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.onosproject.store.service;
+
+import com.google.common.collect.ImmutableList;
+
+import java.util.List;
+
+/**
+ * Contains information about a database partition.
+ */
+public class PartitionInfo {
+    private final String name;
+    private final long term;
+    private final List<String> members;
+    private final String leader;
+
+    /**
+     * Class constructor.
+     *
+     * @param name partition name
+     * @param term term number
+     * @param members partition members
+     * @param leader leader name
+     */
+    public PartitionInfo(String name, long term, List<String> members, String leader) {
+        this.name = name;
+        this.term = term;
+        this.members = ImmutableList.copyOf(members);
+        this.leader = leader;
+    }
+
+    /**
+     * Returns the name of the partition.
+     *
+     * @return partition name
+     */
+    public String name() {
+        return name;
+    }
+
+    /**
+     * Returns the term number.
+     *
+     * @return term number
+     */
+    public long term() {
+        return term;
+    }
+
+    /**
+     * Returns the list of partition members.
+     *
+     * @return partition members
+     */
+    public List<String> members() {
+        return members;
+    }
+
+    /**
+     * Returns the partition leader.
+     *
+     * @return partition leader
+     */
+    public String leader() {
+        return leader;
+    }
+}
diff --git a/core/api/src/main/java/org/onosproject/store/service/StorageAdminService.java b/core/api/src/main/java/org/onosproject/store/service/StorageAdminService.java
new file mode 100644
index 0000000..427b953
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/store/service/StorageAdminService.java
@@ -0,0 +1,31 @@
+/*
+ * 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.onosproject.store.service;
+
+import java.util.List;
+
+/**
+ * Service for administering storage instances.
+ */
+public interface StorageAdminService {
+
+    /**
+     * Returns information about all partitions in the system.
+     *
+     * @return list of partition information
+     */
+    List<PartitionInfo> getPartitionInfo();
+}
diff --git a/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DatabaseManager.java b/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DatabaseManager.java
index 1a2758b..354068b 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DatabaseManager.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DatabaseManager.java
@@ -16,19 +16,12 @@
 
 package org.onosproject.store.consistent.impl;
 
-import static org.slf4j.LoggerFactory.getLogger;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.Map;
-import java.util.Set;
-import java.util.stream.Collectors;
-
+import com.google.common.collect.Sets;
 import net.kuujo.copycat.cluster.ClusterConfig;
+import net.kuujo.copycat.cluster.Member;
 import net.kuujo.copycat.log.FileLog;
 import net.kuujo.copycat.netty.NettyTcpProtocol;
 import net.kuujo.copycat.protocol.Consistency;
-
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
@@ -39,19 +32,28 @@
 import org.onosproject.cluster.ControllerNode;
 import org.onosproject.cluster.DefaultControllerNode;
 import org.onosproject.store.service.ConsistentMap;
+import org.onosproject.store.service.PartitionInfo;
 import org.onosproject.store.service.Serializer;
+import org.onosproject.store.service.StorageAdminService;
 import org.onosproject.store.service.StorageService;
 import org.onosproject.store.service.TransactionContext;
 import org.slf4j.Logger;
 
-import com.google.common.collect.Sets;
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import static org.slf4j.LoggerFactory.getLogger;
 
 /**
  * Database manager.
  */
 @Component(immediate = true, enabled = true)
 @Service
-public class DatabaseManager implements StorageService {
+public class DatabaseManager implements StorageService, StorageAdminService {
 
     private final Logger log = getLogger(getClass());
     private PartitionedDatabase partitionedDatabase;
@@ -160,4 +162,29 @@
     public TransactionContext createTransactionContext() {
         return new DefaultTransactionContext(partitionedDatabase);
     }
-}
\ No newline at end of file
+
+    @Override
+    public List<PartitionInfo> getPartitionInfo() {
+        return partitionedDatabase.getRegisteredPartitions()
+                .values()
+                .stream()
+                .map(DatabaseManager::toPartitionInfo)
+                .collect(Collectors.toList());
+    }
+
+    /**
+     * Maps a Raft Database object to a PartitionInfo object.
+     *
+     * @param database database containing input data
+     * @return PartitionInfo object
+     */
+    private static PartitionInfo toPartitionInfo(Database database) {
+        return new PartitionInfo(database.name(),
+                          database.cluster().term(),
+                          database.cluster().members().stream()
+                                  .map(Member::uri)
+                                  .collect(Collectors.toList()),
+                          database.cluster().leader() != null ?
+                                  database.cluster().leader().uri() : null);
+    }
+}