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 | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 20 | import java.util.Set; |
| 21 | import java.util.HashSet; |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 22 | import java.util.List; |
| 23 | import java.util.ArrayList; |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 24 | import java.util.Arrays; |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 25 | import java.util.Collections; |
| 26 | |
Sho SHIMIZU | 003ed32 | 2016-02-11 12:58:42 -0800 | [diff] [blame^] | 27 | import com.google.common.collect.Iterables; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 28 | import org.apache.karaf.shell.commands.Argument; |
| 29 | import org.apache.karaf.shell.commands.Command; |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 30 | import org.apache.karaf.shell.commands.Option; |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 31 | import org.onlab.packet.MplsLabel; |
| 32 | import org.onlab.packet.VlanId; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 33 | import org.onosproject.cli.AbstractShellCommand; |
| 34 | import org.onosproject.net.DeviceId; |
| 35 | import org.onosproject.net.PortNumber; |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 36 | import org.onosproject.net.TributarySlot; |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 37 | import org.onosproject.net.newresource.ContinuousResource; |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 38 | import org.onosproject.net.newresource.DiscreteResource; |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 39 | import org.onosproject.net.newresource.Resource; |
Naoki Shiota | 8874592 | 2016-02-10 16:28:25 -0800 | [diff] [blame] | 40 | import org.onosproject.net.newresource.Resources; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 41 | import org.onosproject.net.newresource.ResourceService; |
| 42 | |
| 43 | import com.google.common.base.Strings; |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 44 | import com.google.common.collect.ArrayListMultimap; |
| 45 | import com.google.common.collect.DiscreteDomain; |
| 46 | import com.google.common.collect.ImmutableSet; |
| 47 | import com.google.common.collect.Multimap; |
| 48 | import com.google.common.collect.Range; |
| 49 | import com.google.common.collect.RangeSet; |
| 50 | import com.google.common.collect.TreeRangeSet; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Lists available resources. |
| 54 | */ |
| 55 | @Command(scope = "onos", name = "resources", |
| 56 | description = "Lists available resources") |
| 57 | public class ResourcesCommand extends AbstractShellCommand { |
| 58 | |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 59 | @Option(name = "-s", aliases = "--sort", description = "Sort output", |
| 60 | required = false, multiValued = false) |
| 61 | boolean sort = false; |
| 62 | |
| 63 | @Option(name = "-t", aliases = "--typeStrings", description = "List of resource types to be printed", |
| 64 | required = false, multiValued = true) |
| 65 | String[] typeStrings = null; |
| 66 | |
| 67 | Set<String> typesToPrint; |
| 68 | |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 69 | @Argument(index = 0, name = "deviceIdString", description = "Device ID", |
| 70 | required = false, multiValued = false) |
| 71 | String deviceIdStr = null; |
| 72 | |
| 73 | @Argument(index = 1, name = "portNumberString", description = "PortNumber", |
| 74 | required = false, multiValued = false) |
| 75 | String portNumberStr = null; |
| 76 | |
| 77 | |
| 78 | private ResourceService resourceService; |
| 79 | |
| 80 | @Override |
| 81 | protected void execute() { |
| 82 | resourceService = get(ResourceService.class); |
| 83 | |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 84 | if (typeStrings != null) { |
| 85 | typesToPrint = new HashSet<>(Arrays.asList(typeStrings)); |
| 86 | } else { |
| 87 | typesToPrint = Collections.emptySet(); |
| 88 | } |
| 89 | |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 90 | if (deviceIdStr != null && portNumberStr != null) { |
| 91 | DeviceId deviceId = deviceId(deviceIdStr); |
| 92 | PortNumber portNumber = PortNumber.fromString(portNumberStr); |
| 93 | |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 94 | printResource(Resources.discrete(deviceId, portNumber).resource(), 0); |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 95 | } else if (deviceIdStr != null) { |
| 96 | DeviceId deviceId = deviceId(deviceIdStr); |
| 97 | |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 98 | printResource(Resources.discrete(deviceId).resource(), 0); |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 99 | } else { |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 100 | printResource(Resource.ROOT, 0); |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 104 | private void printResource(Resource resource, int level) { |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 105 | // TODO add an option to show only available resource |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 106 | // workaround to preserve the original behavior of ResourceService#getRegisteredResources |
| 107 | Set<Resource> children; |
| 108 | if (resource instanceof DiscreteResource) { |
| 109 | children = resourceService.getRegisteredResources(((DiscreteResource) resource).id()); |
| 110 | } else { |
| 111 | children = Collections.emptySet(); |
| 112 | } |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 113 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 114 | if (resource.equals(Resource.ROOT)) { |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 115 | print("ROOT"); |
| 116 | } else { |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 117 | if (resource instanceof ContinuousResource) { |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 118 | String s = ((String) resource.last()); |
| 119 | String simpleName = s.substring(s.lastIndexOf('.') + 1); |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 120 | print("%s%s: %f", Strings.repeat(" ", level), |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 121 | simpleName, |
| 122 | // Note: last() does not return, what we've registered |
| 123 | // following does not work |
| 124 | //((Class<?>) resource.last()).getSimpleName(), |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 125 | ((ContinuousResource) resource).value()); |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 126 | // Continuous resource is terminal node, stop here |
| 127 | return; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 128 | } else { |
Naoki Shiota | 8874592 | 2016-02-10 16:28:25 -0800 | [diff] [blame] | 129 | String resourceName = resource.last().getClass().getSimpleName(); |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 130 | |
| 131 | String toString = String.valueOf(resource.last()); |
| 132 | if (toString.startsWith(resourceName)) { |
| 133 | print("%s%s", Strings.repeat(" ", level), |
| 134 | toString); |
| 135 | } else { |
| 136 | print("%s%s: %s", Strings.repeat(" ", level), |
| 137 | resourceName, |
| 138 | toString); |
| 139 | } |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 143 | |
| 144 | // Classify children into aggregatable terminal resources and everything else |
| 145 | |
| 146 | Set<Class<?>> aggregatableTypes = ImmutableSet.<Class<?>>builder() |
| 147 | .add(VlanId.class) |
| 148 | .add(MplsLabel.class) |
| 149 | .build(); |
| 150 | // (last() resource name) -> { Resource } |
| 151 | Multimap<String, Resource> aggregatables = ArrayListMultimap.create(); |
| 152 | List<Resource> nonAggregatable = new ArrayList<>(); |
| 153 | |
| 154 | for (Resource r : children) { |
Naoki Shiota | 8874592 | 2016-02-10 16:28:25 -0800 | [diff] [blame] | 155 | if (!isPrintTarget(r)) { |
| 156 | continue; |
| 157 | } |
| 158 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 159 | if (r instanceof ContinuousResource) { |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 160 | // non-aggregatable terminal node |
| 161 | nonAggregatable.add(r); |
Sho SHIMIZU | 003ed32 | 2016-02-11 12:58:42 -0800 | [diff] [blame^] | 162 | } else if (Iterables.any(aggregatableTypes, r::isTypeOf)) { |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 163 | // aggregatable & terminal node |
Naoki Shiota | d762746 | 2016-01-25 15:02:06 -0800 | [diff] [blame] | 164 | String className = r.last().getClass().getSimpleName(); |
Naoki Shiota | 8874592 | 2016-02-10 16:28:25 -0800 | [diff] [blame] | 165 | aggregatables.put(className, r); |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 166 | } else { |
| 167 | nonAggregatable.add(r); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // print aggregated (terminal) |
| 172 | aggregatables.asMap().entrySet() |
| 173 | .forEach(e -> { |
| 174 | // for each type... |
| 175 | String resourceName = e.getKey(); |
| 176 | |
| 177 | RangeSet<Long> rangeSet = TreeRangeSet.create(); |
| 178 | |
| 179 | // aggregate into RangeSet |
| 180 | e.getValue().stream() |
| 181 | .map(Resource::last) |
| 182 | .map(res -> { |
| 183 | if (res instanceof VlanId) { |
| 184 | return (long) ((VlanId) res).toShort(); |
| 185 | } else if (res instanceof MplsLabel) { |
| 186 | return (long) ((MplsLabel) res).toInt(); |
| 187 | } else if (res instanceof TributarySlot) { |
| 188 | return ((TributarySlot) res).index(); |
| 189 | } |
| 190 | // TODO support Lambda (OchSignal types) |
| 191 | return 0L; |
| 192 | }) |
| 193 | .map(Range::singleton) |
| 194 | .map(range -> range.canonical(DiscreteDomain.longs())) |
| 195 | .forEach(rangeSet::add); |
| 196 | |
| 197 | print("%s%s: %s", Strings.repeat(" ", level + 1), |
| 198 | resourceName, |
| 199 | rangeSet); |
| 200 | }); |
| 201 | |
| 202 | |
| 203 | // print non-aggregatables (recurse) |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 204 | if (sort) { |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 205 | nonAggregatable.stream() |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 206 | .sorted((o1, o2) -> String.valueOf(o1.id()).compareTo(String.valueOf(o2.id()))) |
| 207 | .forEach(r -> printResource(r, level + 1)); |
| 208 | } else { |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 209 | nonAggregatable.forEach(r -> printResource(r, level + 1)); |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 210 | } |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 211 | } |
Naoki Shiota | 8874592 | 2016-02-10 16:28:25 -0800 | [diff] [blame] | 212 | |
| 213 | private boolean isPrintTarget(Resource resource) { |
| 214 | if (typesToPrint.isEmpty()) { |
| 215 | return true; |
| 216 | } |
| 217 | |
| 218 | String resourceName; |
| 219 | if (resource instanceof ContinuousResource) { |
| 220 | String s = (String) resource.last(); |
| 221 | resourceName = s.substring(s.lastIndexOf('.') + 1); |
| 222 | } else if (resource instanceof DiscreteResource) { |
| 223 | // TODO This distributed store access incurs overhead. |
| 224 | // This should be merged with the one in printResource() |
| 225 | if (!resourceService.getRegisteredResources(((DiscreteResource) resource).id()).isEmpty()) { |
| 226 | // resource which has children should be printed |
| 227 | return true; |
| 228 | } |
| 229 | resourceName = resource.last().getClass().getSimpleName(); |
| 230 | } else { |
| 231 | log.warn("Unexpected resource class: {}", resource.getClass().getSimpleName()); |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | return typesToPrint.contains(resourceName); |
| 236 | } |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 237 | } |