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; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 39 | import org.onosproject.net.newresource.ResourceService; |
| 40 | |
| 41 | import com.google.common.base.Strings; |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 42 | import com.google.common.collect.ArrayListMultimap; |
| 43 | import com.google.common.collect.DiscreteDomain; |
| 44 | import com.google.common.collect.ImmutableSet; |
| 45 | import com.google.common.collect.Multimap; |
| 46 | import com.google.common.collect.Range; |
| 47 | import com.google.common.collect.RangeSet; |
| 48 | import com.google.common.collect.TreeRangeSet; |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 49 | import org.onosproject.net.newresource.Resources; |
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 { |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 116 | String resourceName = resource.last().getClass().getSimpleName(); |
| 117 | |
| 118 | if (children.isEmpty() && !typesToPrint.isEmpty() && !typesToPrint.contains(resourceName)) { |
| 119 | // This resource is target of filtering |
| 120 | return; |
| 121 | } |
| 122 | |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 123 | |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 124 | if (resource instanceof ContinuousResource) { |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 125 | String s = ((String) resource.last()); |
| 126 | String simpleName = s.substring(s.lastIndexOf('.') + 1); |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 127 | print("%s%s: %f", Strings.repeat(" ", level), |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 128 | simpleName, |
| 129 | // Note: last() does not return, what we've registered |
| 130 | // following does not work |
| 131 | //((Class<?>) resource.last()).getSimpleName(), |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 132 | ((ContinuousResource) resource).value()); |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 133 | // Continuous resource is terminal node, stop here |
| 134 | return; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 135 | } else { |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 136 | |
| 137 | String toString = String.valueOf(resource.last()); |
| 138 | if (toString.startsWith(resourceName)) { |
| 139 | print("%s%s", Strings.repeat(" ", level), |
| 140 | toString); |
| 141 | } else { |
| 142 | print("%s%s: %s", Strings.repeat(" ", level), |
| 143 | resourceName, |
| 144 | toString); |
| 145 | } |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 149 | |
| 150 | // Classify children into aggregatable terminal resources and everything else |
| 151 | |
| 152 | Set<Class<?>> aggregatableTypes = ImmutableSet.<Class<?>>builder() |
| 153 | .add(VlanId.class) |
| 154 | .add(MplsLabel.class) |
| 155 | .build(); |
| 156 | // (last() resource name) -> { Resource } |
| 157 | Multimap<String, Resource> aggregatables = ArrayListMultimap.create(); |
| 158 | List<Resource> nonAggregatable = new ArrayList<>(); |
| 159 | |
| 160 | for (Resource r : children) { |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 161 | if (r instanceof ContinuousResource) { |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 162 | // non-aggregatable terminal node |
| 163 | nonAggregatable.add(r); |
| 164 | } else if (aggregatableTypes.contains(r.last().getClass())) { |
| 165 | // aggregatable & terminal node |
Naoki Shiota | d762746 | 2016-01-25 15:02:06 -0800 | [diff] [blame] | 166 | String className = r.last().getClass().getSimpleName(); |
| 167 | if (typesToPrint.isEmpty() || typesToPrint.contains(className)) { |
| 168 | aggregatables.put(className, r); |
| 169 | } |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 170 | } else { |
| 171 | nonAggregatable.add(r); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // print aggregated (terminal) |
| 176 | aggregatables.asMap().entrySet() |
| 177 | .forEach(e -> { |
| 178 | // for each type... |
| 179 | String resourceName = e.getKey(); |
| 180 | |
| 181 | RangeSet<Long> rangeSet = TreeRangeSet.create(); |
| 182 | |
| 183 | // aggregate into RangeSet |
| 184 | e.getValue().stream() |
| 185 | .map(Resource::last) |
| 186 | .map(res -> { |
| 187 | if (res instanceof VlanId) { |
| 188 | return (long) ((VlanId) res).toShort(); |
| 189 | } else if (res instanceof MplsLabel) { |
| 190 | return (long) ((MplsLabel) res).toInt(); |
| 191 | } else if (res instanceof TributarySlot) { |
| 192 | return ((TributarySlot) res).index(); |
| 193 | } |
| 194 | // TODO support Lambda (OchSignal types) |
| 195 | return 0L; |
| 196 | }) |
| 197 | .map(Range::singleton) |
| 198 | .map(range -> range.canonical(DiscreteDomain.longs())) |
| 199 | .forEach(rangeSet::add); |
| 200 | |
| 201 | print("%s%s: %s", Strings.repeat(" ", level + 1), |
| 202 | resourceName, |
| 203 | rangeSet); |
| 204 | }); |
| 205 | |
| 206 | |
| 207 | // print non-aggregatables (recurse) |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 208 | if (sort) { |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 209 | nonAggregatable.stream() |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 210 | .sorted((o1, o2) -> String.valueOf(o1.id()).compareTo(String.valueOf(o2.id()))) |
| 211 | .forEach(r -> printResource(r, level + 1)); |
| 212 | } else { |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 213 | nonAggregatable.forEach(r -> printResource(r, level + 1)); |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 214 | } |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 215 | } |
| 216 | } |