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