HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 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 | 784af31 | 2016-02-01 15:47:21 -0800 | [diff] [blame] | 20 | import java.util.Arrays; |
| 21 | import java.util.Collections; |
| 22 | import java.util.HashSet; |
Yuta HIGUCHI | b2ba8ab | 2016-09-21 19:45:41 -0700 | [diff] [blame] | 23 | import java.util.List; |
Naoki Shiota | 784af31 | 2016-02-01 15:47:21 -0800 | [diff] [blame] | 24 | import java.util.Set; |
Yuta HIGUCHI | b2ba8ab | 2016-09-21 19:45:41 -0700 | [diff] [blame] | 25 | import java.util.stream.Collectors; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 26 | import java.util.stream.StreamSupport; |
| 27 | |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 28 | import com.google.common.base.Strings; |
Naoki Shiota | 784af31 | 2016-02-01 15:47:21 -0800 | [diff] [blame] | 29 | import com.google.common.collect.ImmutableSet; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 30 | import org.apache.karaf.shell.commands.Argument; |
| 31 | import org.apache.karaf.shell.commands.Command; |
| 32 | import org.apache.karaf.shell.commands.Option; |
Naoki Shiota | 784af31 | 2016-02-01 15:47:21 -0800 | [diff] [blame] | 33 | import org.onlab.packet.MplsLabel; |
| 34 | import org.onlab.packet.VlanId; |
Naoki Shiota | d7654d4 | 2016-03-31 18:46:49 -0700 | [diff] [blame] | 35 | import org.onlab.util.Bandwidth; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 36 | import org.onosproject.cli.AbstractShellCommand; |
| 37 | import org.onosproject.net.Device; |
| 38 | import org.onosproject.net.DeviceId; |
| 39 | import org.onosproject.net.OchSignal; |
| 40 | import org.onosproject.net.Port; |
| 41 | import org.onosproject.net.PortNumber; |
Rimon Ashkenazy | fbac378 | 2016-05-02 14:01:41 +0300 | [diff] [blame] | 42 | import org.onosproject.net.TributarySlot; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 43 | import org.onosproject.net.device.DeviceService; |
Naoki Shiota | 784af31 | 2016-02-01 15:47:21 -0800 | [diff] [blame] | 44 | import org.onosproject.net.intent.IntentId; |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 45 | import org.onosproject.net.resource.ResourceConsumerId; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 46 | import org.onosproject.net.resource.Resources; |
| 47 | import org.onosproject.net.resource.DiscreteResourceId; |
| 48 | import org.onosproject.net.resource.ResourceAllocation; |
| 49 | import org.onosproject.net.resource.ResourceService; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Lists allocated resources. |
| 53 | */ |
| 54 | @Command(scope = "onos", name = "allocations", |
| 55 | description = "Lists allocated resources") |
| 56 | public class AllocationsCommand extends AbstractShellCommand { |
| 57 | |
Yuta HIGUCHI | b2ba8ab | 2016-09-21 19:45:41 -0700 | [diff] [blame] | 58 | @Option(name = "-t", aliases = "--type", |
| 59 | description = "resource types to include in the list", |
Naoki Shiota | 784af31 | 2016-02-01 15:47:21 -0800 | [diff] [blame] | 60 | required = false, multiValued = true) |
| 61 | String[] typeStrings = null; |
| 62 | |
| 63 | Set<String> typesToPrint; |
| 64 | |
Yuta HIGUCHI | b2ba8ab | 2016-09-21 19:45:41 -0700 | [diff] [blame] | 65 | @Option(name = "-i", aliases = "--intentId", |
| 66 | description = "Intent ID to include in the list", |
Naoki Shiota | 784af31 | 2016-02-01 15:47:21 -0800 | [diff] [blame] | 67 | required = false, multiValued = true) |
| 68 | String[] intentStrings; |
| 69 | |
| 70 | Set<String> intentsToPrint; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 71 | |
| 72 | @Argument(index = 0, name = "deviceIdString", description = "Device ID", |
| 73 | required = false, multiValued = false) |
| 74 | String deviceIdStr = null; |
| 75 | |
| 76 | @Argument(index = 1, name = "portNumberString", description = "PortNumber", |
| 77 | required = false, multiValued = false) |
| 78 | String portNumberStr = null; |
| 79 | |
| 80 | |
| 81 | |
| 82 | private DeviceService deviceService; |
| 83 | private ResourceService resourceService; |
| 84 | |
| 85 | @Override |
| 86 | protected void execute() { |
| 87 | deviceService = get(DeviceService.class); |
| 88 | resourceService = get(ResourceService.class); |
| 89 | |
Naoki Shiota | 784af31 | 2016-02-01 15:47:21 -0800 | [diff] [blame] | 90 | if (typeStrings != null) { |
| 91 | typesToPrint = new HashSet<>(Arrays.asList(typeStrings)); |
| 92 | } else { |
| 93 | typesToPrint = Collections.emptySet(); |
| 94 | } |
| 95 | |
| 96 | if (intentStrings != null) { |
| 97 | intentsToPrint = new HashSet<>(Arrays.asList(intentStrings)); |
| 98 | } else { |
| 99 | intentsToPrint = Collections.emptySet(); |
| 100 | } |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 101 | |
| 102 | if (deviceIdStr != null && portNumberStr != null) { |
| 103 | DeviceId deviceId = deviceId(deviceIdStr); |
| 104 | PortNumber portNumber = PortNumber.fromString(portNumberStr); |
| 105 | |
| 106 | printAllocation(deviceId, portNumber, 0); |
| 107 | } else if (deviceIdStr != null) { |
| 108 | DeviceId deviceId = deviceId(deviceIdStr); |
| 109 | |
| 110 | printAllocation(deviceId, 0); |
| 111 | } else { |
| 112 | printAllocation(); |
| 113 | } |
| 114 | |
| 115 | } |
| 116 | |
| 117 | private void printAllocation() { |
| 118 | print("ROOT"); |
| 119 | StreamSupport.stream(deviceService.getAvailableDevices().spliterator(), false) |
| 120 | .map(Device::id) |
| 121 | .forEach(did -> printAllocation(did, 1)); |
| 122 | } |
| 123 | |
| 124 | private void printAllocation(DeviceId did, int level) { |
| 125 | print("%s%s", Strings.repeat(" ", level), did); |
| 126 | StreamSupport.stream(deviceService.getPorts(did).spliterator(), false) |
| 127 | .map(Port::number) |
| 128 | .forEach(num -> printAllocation(did, num, level + 1)); |
| 129 | } |
| 130 | |
| 131 | private void printAllocation(DeviceId did, PortNumber num, int level) { |
| 132 | if (level == 0) { |
| 133 | // print DeviceId when Port was directly specified. |
| 134 | print("%s", did); |
| 135 | } |
Yuta HIGUCHI | b2ba8ab | 2016-09-21 19:45:41 -0700 | [diff] [blame] | 136 | |
| 137 | DiscreteResourceId resourceId = Resources.discrete(did, num).id(); |
| 138 | |
| 139 | List<String> portConsumers = resourceService.getResourceAllocations(resourceId) |
| 140 | .stream() |
| 141 | .filter(this::isSubjectToPrint) |
| 142 | .map(ResourceAllocation::consumerId) |
| 143 | .map(AllocationsCommand::asVerboseString) |
| 144 | .collect(Collectors.toList()); |
| 145 | if (portConsumers.isEmpty()) { |
| 146 | print("%s%s", Strings.repeat(" ", level), asVerboseString(num)); |
| 147 | } else { |
| 148 | print("%s%s allocated by %s", Strings.repeat(" ", level), asVerboseString(num), |
| 149 | portConsumers); |
| 150 | } |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 151 | |
Naoki Shiota | 784af31 | 2016-02-01 15:47:21 -0800 | [diff] [blame] | 152 | // FIXME: This workaround induces a lot of distributed store access. |
| 153 | // ResourceService should have an API to get all allocations under a parent resource. |
| 154 | Set<Class<?>> subResourceTypes = ImmutableSet.<Class<?>>builder() |
| 155 | .add(OchSignal.class) |
| 156 | .add(VlanId.class) |
| 157 | .add(MplsLabel.class) |
Naoki Shiota | d7654d4 | 2016-03-31 18:46:49 -0700 | [diff] [blame] | 158 | .add(Bandwidth.class) |
Rimon Ashkenazy | fbac378 | 2016-05-02 14:01:41 +0300 | [diff] [blame] | 159 | .add(TributarySlot.class) |
Naoki Shiota | 784af31 | 2016-02-01 15:47:21 -0800 | [diff] [blame] | 160 | .build(); |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 161 | |
Naoki Shiota | 784af31 | 2016-02-01 15:47:21 -0800 | [diff] [blame] | 162 | for (Class<?> t : subResourceTypes) { |
| 163 | resourceService.getResourceAllocations(resourceId, t).stream() |
| 164 | .filter(a -> isSubjectToPrint(a)) |
| 165 | .forEach(a -> print("%s%s allocated by %s", Strings.repeat(" ", level + 1), |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 166 | a.resource().valueAs(Object.class).orElse(""), asVerboseString(a.consumerId()))); |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 167 | |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Naoki Shiota | 784af31 | 2016-02-01 15:47:21 -0800 | [diff] [blame] | 171 | private boolean isSubjectToPrint(ResourceAllocation allocation) { |
| 172 | if (!intentsToPrint.isEmpty() |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 173 | && allocation.consumerId().isClassOf(IntentId.class) |
| 174 | && !intentsToPrint.contains(allocation.consumerId().toString())) { |
Naoki Shiota | 784af31 | 2016-02-01 15:47:21 -0800 | [diff] [blame] | 175 | return false; |
| 176 | } |
| 177 | |
| 178 | if (!typesToPrint.isEmpty() |
| 179 | && !typesToPrint.contains(allocation.resource().simpleTypeName())) { |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | return true; |
| 184 | } |
| 185 | |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 186 | /** |
| 187 | * Add type name if the toString does not start with them. |
| 188 | * |
| 189 | * e.g., IntentId#toString result in "42" |
| 190 | * asVerboseString(id) will result in "IntentId:42" |
| 191 | * |
| 192 | * @param obj non-null Object to print. |
| 193 | * @return verbose String representation |
| 194 | */ |
| 195 | private static String asVerboseString(Object obj) { |
| 196 | String name = obj.getClass().getSimpleName(); |
| 197 | String toString = String.valueOf(obj); |
| 198 | if (toString.startsWith(name)) { |
| 199 | return toString; |
| 200 | } else { |
| 201 | return String.format("%s:%s", name, toString); |
| 202 | } |
| 203 | } |
| 204 | |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 205 | private static String asVerboseString(ResourceConsumerId consumerId) { |
| 206 | return String.format("%s:%s", consumerId.consumerClass(), consumerId.value()); |
| 207 | } |
| 208 | |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 209 | } |