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