Add CLI command to view groups on switches

Change-Id: I0ce3cca85e8b38d2e713bf1f0abd4303629e15e4
diff --git a/cli/src/main/java/org/onosproject/cli/net/GroupsListCommand.java b/cli/src/main/java/org/onosproject/cli/net/GroupsListCommand.java
new file mode 100644
index 0000000..cb5ba42
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/GroupsListCommand.java
@@ -0,0 +1,52 @@
+/*
+ * 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.net.DeviceId;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.group.Group;
+import org.onosproject.net.group.GroupService;
+
+/**
+ * Lists all groups in the system.
+ */
+@Command(scope = "onos", name = "groups",
+        description = "Lists all groups in the system")
+public class GroupsListCommand extends AbstractShellCommand {
+
+    private static final String FORMAT =
+            "   key=%s, id=%s, state=%s, bytes=%s, packets=%s, appId=%s, buckets=%s";
+
+    @Override
+    protected void execute() {
+        DeviceService deviceService = get(DeviceService.class);
+        GroupService groupService = get(GroupService.class);
+
+        deviceService.getDevices().forEach(d ->
+                printGroups(d.id(), groupService.getGroups(d.id()))
+        );
+    }
+
+    private void printGroups(DeviceId deviceId, Iterable<Group> groups) {
+        print("deviceId=%s", deviceId);
+        for (Group group : groups) {
+            print(FORMAT, group.appCookie(), group.id(), group.state(),
+                  group.bytes(), group.packets(), group.appId(), group.buckets());
+        }
+    }
+}
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 c772785..adf831e 100644
--- a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -276,6 +276,10 @@
         </command>
 
         <command>
+            <action class="org.onosproject.cli.net.GroupsListCommand"/>
+        </command>
+
+        <command>
             <action class="org.onosproject.cli.net.FlowsListCommand"/>
             <completers>
                 <ref component-id="flowRuleStatusCompleter"/>
diff --git a/core/api/src/main/java/org/onosproject/net/group/GroupService.java b/core/api/src/main/java/org/onosproject/net/group/GroupService.java
index 8502aea..cdd56c7 100644
--- a/core/api/src/main/java/org/onosproject/net/group/GroupService.java
+++ b/core/api/src/main/java/org/onosproject/net/group/GroupService.java
@@ -127,6 +127,14 @@
     Iterable<Group> getGroups(DeviceId deviceId, ApplicationId appId);
 
     /**
+     * Returns all groups associated with the given device.
+     *
+     * @param deviceId device ID to get groups for
+     * @return iterable of device's groups
+     */
+    Iterable<Group> getGroups(DeviceId deviceId);
+
+    /**
      * Adds the specified group listener.
      *
      * @param listener group listener
diff --git a/core/net/src/main/java/org/onosproject/net/group/impl/GroupManager.java b/core/net/src/main/java/org/onosproject/net/group/impl/GroupManager.java
index 1490b0b..2d8f81c 100644
--- a/core/net/src/main/java/org/onosproject/net/group/impl/GroupManager.java
+++ b/core/net/src/main/java/org/onosproject/net/group/impl/GroupManager.java
@@ -15,13 +15,7 @@
  */
 package org.onosproject.net.group.impl;
 
-import static org.slf4j.LoggerFactory.getLogger;
-
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.Set;
-
+import com.google.common.collect.Sets;
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
@@ -54,7 +48,12 @@
 import org.onosproject.net.provider.AbstractProviderService;
 import org.slf4j.Logger;
 
-import com.google.common.collect.Sets;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Set;
+
+import static org.slf4j.LoggerFactory.getLogger;
 
 /**
  * Provides implementation of the group service APIs.
@@ -206,6 +205,11 @@
         return store.getGroups(deviceId);
     }
 
+    @Override
+    public Iterable<Group> getGroups(DeviceId deviceId) {
+        return store.getGroups(deviceId);
+    }
+
     /**
      * Adds the specified group listener.
      *