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 java.util.stream.StreamSupport; |
| 22 | |
| 23 | import org.apache.karaf.shell.commands.Argument; |
| 24 | import org.apache.karaf.shell.commands.Command; |
| 25 | import org.apache.karaf.shell.commands.Option; |
| 26 | import org.onosproject.cli.AbstractShellCommand; |
| 27 | import org.onosproject.net.Device; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.OchSignal; |
| 30 | import org.onosproject.net.Port; |
| 31 | import org.onosproject.net.PortNumber; |
| 32 | import org.onosproject.net.device.DeviceService; |
| 33 | import org.onosproject.net.newresource.ResourceAllocation; |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 34 | import org.onosproject.net.newresource.Resource; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 35 | import org.onosproject.net.newresource.ResourceService; |
| 36 | |
| 37 | import com.google.common.base.Strings; |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 38 | import org.onosproject.net.newresource.Resources; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Lists allocated resources. |
| 42 | */ |
| 43 | @Command(scope = "onos", name = "allocations", |
| 44 | description = "Lists allocated resources") |
| 45 | public class AllocationsCommand extends AbstractShellCommand { |
| 46 | |
| 47 | // TODO add other resource types |
| 48 | @Option(name = "-l", aliases = "--lambda", description = "Lambda Resource", |
| 49 | required = false, multiValued = false) |
| 50 | private boolean lambda = true; |
| 51 | |
| 52 | @Argument(index = 0, name = "deviceIdString", description = "Device ID", |
| 53 | required = false, multiValued = false) |
| 54 | String deviceIdStr = null; |
| 55 | |
| 56 | @Argument(index = 1, name = "portNumberString", description = "PortNumber", |
| 57 | required = false, multiValued = false) |
| 58 | String portNumberStr = null; |
| 59 | |
| 60 | |
| 61 | |
| 62 | private DeviceService deviceService; |
| 63 | private ResourceService resourceService; |
| 64 | |
| 65 | @Override |
| 66 | protected void execute() { |
| 67 | deviceService = get(DeviceService.class); |
| 68 | resourceService = get(ResourceService.class); |
| 69 | |
| 70 | |
| 71 | if (deviceIdStr != null && portNumberStr != null) { |
| 72 | DeviceId deviceId = deviceId(deviceIdStr); |
| 73 | PortNumber portNumber = PortNumber.fromString(portNumberStr); |
| 74 | |
| 75 | printAllocation(deviceId, portNumber, 0); |
| 76 | } else if (deviceIdStr != null) { |
| 77 | DeviceId deviceId = deviceId(deviceIdStr); |
| 78 | |
| 79 | printAllocation(deviceId, 0); |
| 80 | } else { |
| 81 | printAllocation(); |
| 82 | } |
| 83 | |
| 84 | } |
| 85 | |
| 86 | private void printAllocation() { |
| 87 | print("ROOT"); |
| 88 | StreamSupport.stream(deviceService.getAvailableDevices().spliterator(), false) |
| 89 | .map(Device::id) |
| 90 | .forEach(did -> printAllocation(did, 1)); |
| 91 | } |
| 92 | |
| 93 | private void printAllocation(DeviceId did, int level) { |
| 94 | print("%s%s", Strings.repeat(" ", level), did); |
| 95 | StreamSupport.stream(deviceService.getPorts(did).spliterator(), false) |
| 96 | .map(Port::number) |
| 97 | .forEach(num -> printAllocation(did, num, level + 1)); |
| 98 | } |
| 99 | |
| 100 | private void printAllocation(DeviceId did, PortNumber num, int level) { |
| 101 | if (level == 0) { |
| 102 | // print DeviceId when Port was directly specified. |
| 103 | print("%s", did); |
| 104 | } |
| 105 | print("%s%s", Strings.repeat(" ", level), asVerboseString(num)); |
| 106 | |
| 107 | // TODO: Current design cannot deal with sub-resources |
| 108 | // (e.g., TX/RX under Port) |
| 109 | |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 110 | Resource resource = Resources.discrete(did, num).resource(); |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 111 | if (lambda) { |
| 112 | //print("Lambda resources:"); |
| 113 | Collection<ResourceAllocation> allocations |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 114 | = resourceService.getResourceAllocations(resource, OchSignal.class); |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 115 | |
| 116 | for (ResourceAllocation a : allocations) { |
| 117 | print("%s%s allocated by %s", Strings.repeat(" ", level + 1), |
| 118 | a.resource().last(), asVerboseString(a.consumer())); |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Add type name if the toString does not start with them. |
| 125 | * |
| 126 | * e.g., IntentId#toString result in "42" |
| 127 | * asVerboseString(id) will result in "IntentId:42" |
| 128 | * |
| 129 | * @param obj non-null Object to print. |
| 130 | * @return verbose String representation |
| 131 | */ |
| 132 | private static String asVerboseString(Object obj) { |
| 133 | String name = obj.getClass().getSimpleName(); |
| 134 | String toString = String.valueOf(obj); |
| 135 | if (toString.startsWith(name)) { |
| 136 | return toString; |
| 137 | } else { |
| 138 | return String.format("%s:%s", name, toString); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | } |