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 | 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; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 28 | import org.apache.karaf.shell.api.action.Argument; |
| 29 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 30 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 31 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 32 | import org.apache.karaf.shell.api.action.Option; |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 33 | import org.onlab.packet.MplsLabel; |
| 34 | import org.onlab.packet.VlanId; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 35 | import org.onosproject.cli.AbstractShellCommand; |
| 36 | import org.onosproject.net.DeviceId; |
| 37 | import org.onosproject.net.PortNumber; |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 38 | import org.onosproject.net.TributarySlot; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 39 | import org.onosproject.net.resource.ContinuousResource; |
| 40 | import org.onosproject.net.resource.DiscreteResource; |
| 41 | import org.onosproject.net.resource.Resource; |
| 42 | import org.onosproject.net.resource.Resources; |
Sho SHIMIZU | a6b4dc7 | 2016-03-11 19:00:20 -0800 | [diff] [blame] | 43 | import org.onosproject.net.resource.ResourceQueryService; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 44 | |
| 45 | import com.google.common.base.Strings; |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 46 | import com.google.common.collect.ArrayListMultimap; |
| 47 | import com.google.common.collect.DiscreteDomain; |
| 48 | import com.google.common.collect.ImmutableSet; |
| 49 | import com.google.common.collect.Multimap; |
| 50 | import com.google.common.collect.Range; |
| 51 | import com.google.common.collect.RangeSet; |
| 52 | import com.google.common.collect.TreeRangeSet; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 53 | |
| 54 | /** |
Yuta HIGUCHI | dd19371 | 2016-09-21 11:36:09 -0700 | [diff] [blame] | 55 | * Lists registered resources. |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 56 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 57 | @Service |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 58 | @Command(scope = "onos", name = "resources", |
Yuta HIGUCHI | dd19371 | 2016-09-21 11:36:09 -0700 | [diff] [blame] | 59 | description = "Lists registered resources") |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 60 | public class ResourcesCommand extends AbstractShellCommand { |
| 61 | |
Yuta HIGUCHI | f5908c9 | 2016-09-22 16:35:44 -0700 | [diff] [blame] | 62 | @Option(name = "-a", aliases = "--available", |
| 63 | description = "Output available resources only", |
| 64 | required = false, multiValued = false) |
| 65 | boolean availablesOnly = false; |
| 66 | |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 67 | @Option(name = "-s", aliases = "--sort", description = "Sort output", |
| 68 | required = false, multiValued = false) |
| 69 | boolean sort = false; |
| 70 | |
| 71 | @Option(name = "-t", aliases = "--typeStrings", description = "List of resource types to be printed", |
| 72 | required = false, multiValued = true) |
| 73 | String[] typeStrings = null; |
| 74 | |
| 75 | Set<String> typesToPrint; |
| 76 | |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 77 | @Argument(index = 0, name = "deviceIdString", description = "Device ID", |
| 78 | required = false, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 79 | @Completion(DeviceIdCompleter.class) |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 80 | String deviceIdStr = null; |
| 81 | |
| 82 | @Argument(index = 1, name = "portNumberString", description = "PortNumber", |
| 83 | required = false, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 84 | @Completion(PortNumberCompleter.class) |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 85 | String portNumberStr = null; |
| 86 | |
| 87 | |
Sho SHIMIZU | a6b4dc7 | 2016-03-11 19:00:20 -0800 | [diff] [blame] | 88 | private ResourceQueryService resourceService; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 89 | |
| 90 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 91 | protected void doExecute() { |
Sho SHIMIZU | a6b4dc7 | 2016-03-11 19:00:20 -0800 | [diff] [blame] | 92 | resourceService = get(ResourceQueryService.class); |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 93 | |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 94 | if (typeStrings != null) { |
| 95 | typesToPrint = new HashSet<>(Arrays.asList(typeStrings)); |
| 96 | } else { |
| 97 | typesToPrint = Collections.emptySet(); |
| 98 | } |
| 99 | |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 100 | if (deviceIdStr != null && portNumberStr != null) { |
| 101 | DeviceId deviceId = deviceId(deviceIdStr); |
| 102 | PortNumber portNumber = PortNumber.fromString(portNumberStr); |
| 103 | |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 104 | printResource(Resources.discrete(deviceId, portNumber).resource(), 0); |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 105 | } else if (deviceIdStr != null) { |
| 106 | DeviceId deviceId = deviceId(deviceIdStr); |
| 107 | |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 108 | printResource(Resources.discrete(deviceId).resource(), 0); |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 109 | } else { |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 110 | printResource(Resource.ROOT, 0); |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 114 | private void printResource(Resource resource, int level) { |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 115 | // workaround to preserve the original behavior of ResourceService#getRegisteredResources |
| 116 | Set<Resource> children; |
| 117 | if (resource instanceof DiscreteResource) { |
| 118 | children = resourceService.getRegisteredResources(((DiscreteResource) resource).id()); |
| 119 | } else { |
| 120 | children = Collections.emptySet(); |
| 121 | } |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 122 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 123 | if (resource.equals(Resource.ROOT)) { |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 124 | print("ROOT"); |
| 125 | } else { |
Sho SHIMIZU | 5a8e8f9 | 2016-02-22 11:33:36 -0800 | [diff] [blame] | 126 | String resourceName = resource.simpleTypeName(); |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 127 | if (resource instanceof ContinuousResource) { |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 128 | if (availablesOnly) { |
| 129 | // Get the total resource |
| 130 | double total = ((ContinuousResource) resource).value(); |
| 131 | // Get allocated resource |
| 132 | double allocated = resourceService.getResourceAllocations(resource.id()).stream() |
| 133 | .mapToDouble(rA -> ((ContinuousResource) rA.resource()).value()) |
| 134 | .sum(); |
| 135 | // Difference |
| 136 | double difference = total - allocated; |
| 137 | print("%s%s: %f", Strings.repeat(" ", level), |
| 138 | resourceName, difference); |
| 139 | } else { |
| 140 | print("%s%s: %f", Strings.repeat(" ", level), |
| 141 | resourceName, |
| 142 | ((ContinuousResource) resource).value()); |
| 143 | } |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 144 | // Continuous resource is terminal node, stop here |
| 145 | return; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 146 | } else { |
Yuta HIGUCHI | f5908c9 | 2016-09-22 16:35:44 -0700 | [diff] [blame] | 147 | String availability = ""; |
| 148 | if (availablesOnly && !children.isEmpty()) { |
| 149 | // intermediate nodes cannot be omitted, print availability |
| 150 | if (resourceService.isAvailable(resource)) { |
| 151 | availability = " ✔"; |
| 152 | } else { |
| 153 | availability = " ✘"; |
| 154 | } |
| 155 | } |
Sho SHIMIZU | f08cb4c | 2016-02-11 18:35:59 -0800 | [diff] [blame] | 156 | String toString = String.valueOf(resource.valueAs(Object.class).orElse("")); |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 157 | if (toString.startsWith(resourceName)) { |
Yuta HIGUCHI | f5908c9 | 2016-09-22 16:35:44 -0700 | [diff] [blame] | 158 | print("%s%s%s", Strings.repeat(" ", level), |
| 159 | toString, availability); |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 160 | } else { |
Yuta HIGUCHI | f5908c9 | 2016-09-22 16:35:44 -0700 | [diff] [blame] | 161 | print("%s%s: %s%s", Strings.repeat(" ", level), |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 162 | resourceName, |
Yuta HIGUCHI | f5908c9 | 2016-09-22 16:35:44 -0700 | [diff] [blame] | 163 | toString, availability); |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 164 | } |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 168 | |
| 169 | // Classify children into aggregatable terminal resources and everything else |
| 170 | |
| 171 | Set<Class<?>> aggregatableTypes = ImmutableSet.<Class<?>>builder() |
| 172 | .add(VlanId.class) |
| 173 | .add(MplsLabel.class) |
| 174 | .build(); |
| 175 | // (last() resource name) -> { Resource } |
| 176 | Multimap<String, Resource> aggregatables = ArrayListMultimap.create(); |
| 177 | List<Resource> nonAggregatable = new ArrayList<>(); |
| 178 | |
| 179 | for (Resource r : children) { |
goeonpark | 84bbd85 | 2018-09-15 02:56:06 +0900 | [diff] [blame] | 180 | if (!isPrintTarget(r)) { // A |
Naoki Shiota | 8874592 | 2016-02-10 16:28:25 -0800 | [diff] [blame] | 181 | continue; |
| 182 | } |
| 183 | |
goeonpark | 84bbd85 | 2018-09-15 02:56:06 +0900 | [diff] [blame] | 184 | if (r instanceof DiscreteResource) { |
| 185 | |
| 186 | if (resourceService.getRegisteredResources(((DiscreteResource) r).id()).isEmpty()) { |
| 187 | // resource which has children should be printed |
| 188 | continue; |
| 189 | } |
| 190 | } |
| 191 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 192 | if (r instanceof ContinuousResource) { |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 193 | // non-aggregatable terminal node |
| 194 | nonAggregatable.add(r); |
Sho SHIMIZU | 003ed32 | 2016-02-11 12:58:42 -0800 | [diff] [blame] | 195 | } else if (Iterables.any(aggregatableTypes, r::isTypeOf)) { |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 196 | // aggregatable & terminal node |
Sho SHIMIZU | 5a8e8f9 | 2016-02-22 11:33:36 -0800 | [diff] [blame] | 197 | String simpleName = r.simpleTypeName(); |
| 198 | aggregatables.put(simpleName, r); |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 199 | } else { |
| 200 | nonAggregatable.add(r); |
| 201 | } |
| 202 | } |
| 203 | |
goeonpark | 84bbd85 | 2018-09-15 02:56:06 +0900 | [diff] [blame] | 204 | |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 205 | // print aggregated (terminal) |
| 206 | aggregatables.asMap().entrySet() |
| 207 | .forEach(e -> { |
| 208 | // for each type... |
| 209 | String resourceName = e.getKey(); |
| 210 | |
| 211 | RangeSet<Long> rangeSet = TreeRangeSet.create(); |
| 212 | |
| 213 | // aggregate into RangeSet |
| 214 | e.getValue().stream() |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 215 | .map(res -> { |
Sho SHIMIZU | f08cb4c | 2016-02-11 18:35:59 -0800 | [diff] [blame] | 216 | if (res.isTypeOf(VlanId.class)) { |
| 217 | return (long) res.valueAs(VlanId.class).get().toShort(); |
| 218 | } else if (res.isTypeOf(MplsLabel.class)) { |
| 219 | return (long) res.valueAs(MplsLabel.class).get().toInt(); |
| 220 | } else if (res.isTypeOf(TributarySlot.class)) { |
| 221 | return res.valueAs(TributarySlot.class).get().index(); |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 222 | } |
| 223 | // TODO support Lambda (OchSignal types) |
| 224 | return 0L; |
| 225 | }) |
| 226 | .map(Range::singleton) |
| 227 | .map(range -> range.canonical(DiscreteDomain.longs())) |
| 228 | .forEach(rangeSet::add); |
| 229 | |
| 230 | print("%s%s: %s", Strings.repeat(" ", level + 1), |
| 231 | resourceName, |
| 232 | rangeSet); |
| 233 | }); |
| 234 | |
| 235 | |
| 236 | // print non-aggregatables (recurse) |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 237 | if (sort) { |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 238 | nonAggregatable.stream() |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 239 | .sorted((o1, o2) -> String.valueOf(o1.id()).compareTo(String.valueOf(o2.id()))) |
| 240 | .forEach(r -> printResource(r, level + 1)); |
| 241 | } else { |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 242 | nonAggregatable.forEach(r -> printResource(r, level + 1)); |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 243 | } |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 244 | } |
Naoki Shiota | 8874592 | 2016-02-10 16:28:25 -0800 | [diff] [blame] | 245 | |
| 246 | private boolean isPrintTarget(Resource resource) { |
| 247 | if (typesToPrint.isEmpty()) { |
| 248 | return true; |
| 249 | } |
| 250 | |
Sho SHIMIZU | 5a8e8f9 | 2016-02-22 11:33:36 -0800 | [diff] [blame] | 251 | String resourceName = resource.simpleTypeName(); |
| 252 | if (resource instanceof DiscreteResource) { |
goeonpark | 84bbd85 | 2018-09-15 02:56:06 +0900 | [diff] [blame] | 253 | |
Yuta HIGUCHI | f5908c9 | 2016-09-22 16:35:44 -0700 | [diff] [blame] | 254 | if (availablesOnly && !resourceService.isAvailable(resource)) { |
| 255 | // don't print unavailable discrete resource |
| 256 | return false; |
| 257 | } |
Sho SHIMIZU | 5a8e8f9 | 2016-02-22 11:33:36 -0800 | [diff] [blame] | 258 | } else if (!(resource instanceof ContinuousResource)) { |
Naoki Shiota | 8874592 | 2016-02-10 16:28:25 -0800 | [diff] [blame] | 259 | log.warn("Unexpected resource class: {}", resource.getClass().getSimpleName()); |
| 260 | return false; |
| 261 | } |
Naoki Shiota | 8874592 | 2016-02-10 16:28:25 -0800 | [diff] [blame] | 262 | return typesToPrint.contains(resourceName); |
| 263 | } |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 264 | } |