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 | |
| 20 | import java.util.Collection; |
| 21 | import org.apache.karaf.shell.commands.Argument; |
| 22 | import org.apache.karaf.shell.commands.Command; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.PortNumber; |
| 26 | import org.onosproject.net.newresource.ResourcePath; |
| 27 | import org.onosproject.net.newresource.ResourceService; |
| 28 | |
| 29 | import com.google.common.base.Strings; |
| 30 | |
| 31 | /** |
| 32 | * Lists available resources. |
| 33 | */ |
| 34 | @Command(scope = "onos", name = "resources", |
| 35 | description = "Lists available resources") |
| 36 | public class ResourcesCommand extends AbstractShellCommand { |
| 37 | |
| 38 | @Argument(index = 0, name = "deviceIdString", description = "Device ID", |
| 39 | required = false, multiValued = false) |
| 40 | String deviceIdStr = null; |
| 41 | |
| 42 | @Argument(index = 1, name = "portNumberString", description = "PortNumber", |
| 43 | required = false, multiValued = false) |
| 44 | String portNumberStr = null; |
| 45 | |
| 46 | |
| 47 | private ResourceService resourceService; |
| 48 | |
| 49 | @Override |
| 50 | protected void execute() { |
| 51 | resourceService = get(ResourceService.class); |
| 52 | |
| 53 | if (deviceIdStr != null && portNumberStr != null) { |
| 54 | DeviceId deviceId = deviceId(deviceIdStr); |
| 55 | PortNumber portNumber = PortNumber.fromString(portNumberStr); |
| 56 | |
| 57 | printResource(ResourcePath.discrete(deviceId, portNumber), 0); |
| 58 | } else if (deviceIdStr != null) { |
| 59 | DeviceId deviceId = deviceId(deviceIdStr); |
| 60 | |
| 61 | printResource(ResourcePath.discrete(deviceId), 0); |
| 62 | } else { |
| 63 | printResource(ResourcePath.ROOT, 0); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | private void printResource(ResourcePath resource, int level) { |
| 68 | if (resource.equals(ResourcePath.ROOT)) { |
| 69 | print("ROOT"); |
| 70 | } else { |
| 71 | String name = resource.last().getClass().getSimpleName(); |
| 72 | String toString = String.valueOf(resource.last()); |
| 73 | if (toString.startsWith(name)) { |
| 74 | print("%s%s", Strings.repeat(" ", level), |
| 75 | toString); |
| 76 | |
| 77 | } else { |
| 78 | print("%s%s:%s", Strings.repeat(" ", level), |
| 79 | name, |
| 80 | toString); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | Collection<ResourcePath> resources = resourceService.getAvailableResources(resource); |
| 85 | // TODO: Should consider better output for leaf nodes |
| 86 | resources.forEach(r -> printResource(r, level + 1)); |
| 87 | } |
| 88 | } |