Modified onos:resources CLI command to be able to filter resource types (ONOS-3617).

Change-Id: I3465cca0ba44fce14d3607be5f4fccf29cfabbed
diff --git a/cli/src/main/java/org/onosproject/cli/net/ResourcesCommand.java b/cli/src/main/java/org/onosproject/cli/net/ResourcesCommand.java
index 4a885cd..b70723e 100644
--- a/cli/src/main/java/org/onosproject/cli/net/ResourcesCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/ResourcesCommand.java
@@ -17,9 +17,15 @@
 
 import static org.onosproject.net.DeviceId.deviceId;
 
+import java.util.Set;
+import java.util.HashSet;
+import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
+
 import org.apache.karaf.shell.commands.Argument;
 import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
 import org.onosproject.cli.AbstractShellCommand;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.PortNumber;
@@ -35,6 +41,16 @@
          description = "Lists available resources")
 public class ResourcesCommand extends AbstractShellCommand {
 
+    @Option(name = "-s", aliases = "--sort", description = "Sort output",
+            required = false, multiValued = false)
+    boolean sort = false;
+
+    @Option(name = "-t", aliases = "--typeStrings", description = "List of resource types to be printed",
+            required = false, multiValued = true)
+    String[] typeStrings = null;
+
+    Set<String> typesToPrint;
+
     @Argument(index = 0, name = "deviceIdString", description = "Device ID",
               required = false, multiValued = false)
     String deviceIdStr = null;
@@ -50,6 +66,12 @@
     protected void execute() {
         resourceService = get(ResourceService.class);
 
+        if (typeStrings != null) {
+            typesToPrint = new HashSet<>(Arrays.asList(typeStrings));
+        } else {
+            typesToPrint = Collections.emptySet();
+        }
+
         if (deviceIdStr != null && portNumberStr != null) {
             DeviceId deviceId = deviceId(deviceIdStr);
             PortNumber portNumber = PortNumber.fromString(portNumberStr);
@@ -65,24 +87,36 @@
     }
 
     private void printResource(ResourcePath resource, int level) {
+        Collection<ResourcePath> children = resourceService.getAvailableResources(resource);
+
         if (resource.equals(ResourcePath.ROOT)) {
             print("ROOT");
         } else {
-            String name = resource.last().getClass().getSimpleName();
+            String resourceName = resource.last().getClass().getSimpleName();
+
+            if (children.isEmpty() && !typesToPrint.isEmpty() && !typesToPrint.contains(resourceName)) {
+                // This resource is target of filtering
+                return;
+            }
+
             String toString = String.valueOf(resource.last());
-            if (toString.startsWith(name)) {
+            if (toString.startsWith(resourceName)) {
                 print("%s%s", Strings.repeat(" ", level),
                               toString);
-
             } else {
-                print("%s%s:%s", Strings.repeat(" ", level),
-                                 name,
+                print("%s%s: %s", Strings.repeat(" ", level),
+                                 resourceName,
                                  toString);
             }
         }
 
-        Collection<ResourcePath> resources = resourceService.getAvailableResources(resource);
-        // TODO: Should consider better output for leaf nodes
-        resources.forEach(r -> printResource(r, level + 1));
+        if (sort) {
+            children.stream()
+                    .sorted((o1, o2) -> String.valueOf(o1.id()).compareTo(String.valueOf(o2.id())))
+                    .forEach(r -> printResource(r, level + 1));
+        } else {
+            // TODO: Should consider better output for leaf nodes
+            children.forEach(r -> printResource(r, level + 1));
+        }
     }
 }