HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.cli.net; |
| 17 | |
| 18 | import static org.onosproject.net.DeviceId.deviceId; |
| 19 | |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 20 | import java.util.Set; |
| 21 | import java.util.HashSet; |
| 22 | import java.util.Arrays; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 23 | import java.util.Collection; |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 24 | import java.util.Collections; |
| 25 | |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 26 | import org.apache.karaf.shell.commands.Argument; |
| 27 | import org.apache.karaf.shell.commands.Command; |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 28 | import org.apache.karaf.shell.commands.Option; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 29 | import org.onosproject.cli.AbstractShellCommand; |
| 30 | import org.onosproject.net.DeviceId; |
| 31 | import org.onosproject.net.PortNumber; |
| 32 | import org.onosproject.net.newresource.ResourcePath; |
| 33 | import org.onosproject.net.newresource.ResourceService; |
| 34 | |
| 35 | import com.google.common.base.Strings; |
| 36 | |
| 37 | /** |
| 38 | * Lists available resources. |
| 39 | */ |
| 40 | @Command(scope = "onos", name = "resources", |
| 41 | description = "Lists available resources") |
| 42 | public class ResourcesCommand extends AbstractShellCommand { |
| 43 | |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 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 | |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 54 | @Argument(index = 0, name = "deviceIdString", description = "Device ID", |
| 55 | required = false, multiValued = false) |
| 56 | String deviceIdStr = null; |
| 57 | |
| 58 | @Argument(index = 1, name = "portNumberString", description = "PortNumber", |
| 59 | required = false, multiValued = false) |
| 60 | String portNumberStr = null; |
| 61 | |
| 62 | |
| 63 | private ResourceService resourceService; |
| 64 | |
| 65 | @Override |
| 66 | protected void execute() { |
| 67 | resourceService = get(ResourceService.class); |
| 68 | |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 69 | if (typeStrings != null) { |
| 70 | typesToPrint = new HashSet<>(Arrays.asList(typeStrings)); |
| 71 | } else { |
| 72 | typesToPrint = Collections.emptySet(); |
| 73 | } |
| 74 | |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 75 | if (deviceIdStr != null && portNumberStr != null) { |
| 76 | DeviceId deviceId = deviceId(deviceIdStr); |
| 77 | PortNumber portNumber = PortNumber.fromString(portNumberStr); |
| 78 | |
| 79 | printResource(ResourcePath.discrete(deviceId, portNumber), 0); |
| 80 | } else if (deviceIdStr != null) { |
| 81 | DeviceId deviceId = deviceId(deviceIdStr); |
| 82 | |
| 83 | printResource(ResourcePath.discrete(deviceId), 0); |
| 84 | } else { |
| 85 | printResource(ResourcePath.ROOT, 0); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | private void printResource(ResourcePath resource, int level) { |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 90 | Collection<ResourcePath> children = resourceService.getAvailableResources(resource); |
| 91 | |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 92 | if (resource.equals(ResourcePath.ROOT)) { |
| 93 | print("ROOT"); |
| 94 | } else { |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 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 | |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 102 | String toString = String.valueOf(resource.last()); |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 103 | if (toString.startsWith(resourceName)) { |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 104 | print("%s%s", Strings.repeat(" ", level), |
| 105 | toString); |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 106 | } else { |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 107 | print("%s%s: %s", Strings.repeat(" ", level), |
| 108 | resourceName, |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 109 | toString); |
| 110 | } |
| 111 | } |
| 112 | |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 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 { |
| 118 | // TODO: Should consider better output for leaf nodes |
| 119 | children.forEach(r -> printResource(r, level + 1)); |
| 120 | } |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 121 | } |
| 122 | } |