Committed by
Gerrit Code Review
Modified onos:resources CLI command to be able to filter resource types (ONOS-3617).
Change-Id: I3465cca0ba44fce14d3607be5f4fccf29cfabbed
Showing
1 changed file
with
41 additions
and
7 deletions
| ... | @@ -17,9 +17,15 @@ package org.onosproject.cli.net; | ... | @@ -17,9 +17,15 @@ package org.onosproject.cli.net; |
| 17 | 17 | ||
| 18 | import static org.onosproject.net.DeviceId.deviceId; | 18 | import static org.onosproject.net.DeviceId.deviceId; |
| 19 | 19 | ||
| 20 | +import java.util.Set; | ||
| 21 | +import java.util.HashSet; | ||
| 22 | +import java.util.Arrays; | ||
| 20 | import java.util.Collection; | 23 | import java.util.Collection; |
| 24 | +import java.util.Collections; | ||
| 25 | + | ||
| 21 | import org.apache.karaf.shell.commands.Argument; | 26 | import org.apache.karaf.shell.commands.Argument; |
| 22 | import org.apache.karaf.shell.commands.Command; | 27 | import org.apache.karaf.shell.commands.Command; |
| 28 | +import org.apache.karaf.shell.commands.Option; | ||
| 23 | import org.onosproject.cli.AbstractShellCommand; | 29 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.net.DeviceId; | 30 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.PortNumber; | 31 | import org.onosproject.net.PortNumber; |
| ... | @@ -35,6 +41,16 @@ import com.google.common.base.Strings; | ... | @@ -35,6 +41,16 @@ import com.google.common.base.Strings; |
| 35 | description = "Lists available resources") | 41 | description = "Lists available resources") |
| 36 | public class ResourcesCommand extends AbstractShellCommand { | 42 | public class ResourcesCommand extends AbstractShellCommand { |
| 37 | 43 | ||
| 44 | + @Option(name = "-s", aliases = "--sort", description = "Sort output", | ||
| 45 | + required = false, multiValued = false) | ||
| 46 | + boolean sort = false; | ||
| 47 | + | ||
| 48 | + @Option(name = "-t", aliases = "--typeStrings", description = "List of resource types to be printed", | ||
| 49 | + required = false, multiValued = true) | ||
| 50 | + String[] typeStrings = null; | ||
| 51 | + | ||
| 52 | + Set<String> typesToPrint; | ||
| 53 | + | ||
| 38 | @Argument(index = 0, name = "deviceIdString", description = "Device ID", | 54 | @Argument(index = 0, name = "deviceIdString", description = "Device ID", |
| 39 | required = false, multiValued = false) | 55 | required = false, multiValued = false) |
| 40 | String deviceIdStr = null; | 56 | String deviceIdStr = null; |
| ... | @@ -50,6 +66,12 @@ public class ResourcesCommand extends AbstractShellCommand { | ... | @@ -50,6 +66,12 @@ public class ResourcesCommand extends AbstractShellCommand { |
| 50 | protected void execute() { | 66 | protected void execute() { |
| 51 | resourceService = get(ResourceService.class); | 67 | resourceService = get(ResourceService.class); |
| 52 | 68 | ||
| 69 | + if (typeStrings != null) { | ||
| 70 | + typesToPrint = new HashSet<>(Arrays.asList(typeStrings)); | ||
| 71 | + } else { | ||
| 72 | + typesToPrint = Collections.emptySet(); | ||
| 73 | + } | ||
| 74 | + | ||
| 53 | if (deviceIdStr != null && portNumberStr != null) { | 75 | if (deviceIdStr != null && portNumberStr != null) { |
| 54 | DeviceId deviceId = deviceId(deviceIdStr); | 76 | DeviceId deviceId = deviceId(deviceIdStr); |
| 55 | PortNumber portNumber = PortNumber.fromString(portNumberStr); | 77 | PortNumber portNumber = PortNumber.fromString(portNumberStr); |
| ... | @@ -65,24 +87,36 @@ public class ResourcesCommand extends AbstractShellCommand { | ... | @@ -65,24 +87,36 @@ public class ResourcesCommand extends AbstractShellCommand { |
| 65 | } | 87 | } |
| 66 | 88 | ||
| 67 | private void printResource(ResourcePath resource, int level) { | 89 | private void printResource(ResourcePath resource, int level) { |
| 90 | + Collection<ResourcePath> children = resourceService.getAvailableResources(resource); | ||
| 91 | + | ||
| 68 | if (resource.equals(ResourcePath.ROOT)) { | 92 | if (resource.equals(ResourcePath.ROOT)) { |
| 69 | print("ROOT"); | 93 | print("ROOT"); |
| 70 | } else { | 94 | } else { |
| 71 | - String name = resource.last().getClass().getSimpleName(); | 95 | + String resourceName = resource.last().getClass().getSimpleName(); |
| 96 | + | ||
| 97 | + if (children.isEmpty() && !typesToPrint.isEmpty() && !typesToPrint.contains(resourceName)) { | ||
| 98 | + // This resource is target of filtering | ||
| 99 | + return; | ||
| 100 | + } | ||
| 101 | + | ||
| 72 | String toString = String.valueOf(resource.last()); | 102 | String toString = String.valueOf(resource.last()); |
| 73 | - if (toString.startsWith(name)) { | 103 | + if (toString.startsWith(resourceName)) { |
| 74 | print("%s%s", Strings.repeat(" ", level), | 104 | print("%s%s", Strings.repeat(" ", level), |
| 75 | toString); | 105 | toString); |
| 76 | - | ||
| 77 | } else { | 106 | } else { |
| 78 | - print("%s%s:%s", Strings.repeat(" ", level), | 107 | + print("%s%s: %s", Strings.repeat(" ", level), |
| 79 | - name, | 108 | + resourceName, |
| 80 | toString); | 109 | toString); |
| 81 | } | 110 | } |
| 82 | } | 111 | } |
| 83 | 112 | ||
| 84 | - Collection<ResourcePath> resources = resourceService.getAvailableResources(resource); | 113 | + if (sort) { |
| 114 | + children.stream() | ||
| 115 | + .sorted((o1, o2) -> String.valueOf(o1.id()).compareTo(String.valueOf(o2.id()))) | ||
| 116 | + .forEach(r -> printResource(r, level + 1)); | ||
| 117 | + } else { | ||
| 85 | // TODO: Should consider better output for leaf nodes | 118 | // TODO: Should consider better output for leaf nodes |
| 86 | - resources.forEach(r -> printResource(r, level + 1)); | 119 | + children.forEach(r -> printResource(r, level + 1)); |
| 120 | + } | ||
| 87 | } | 121 | } |
| 88 | } | 122 | } | ... | ... |
-
Please register or login to post a comment