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