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; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 25 | import java.util.Collection; |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 26 | import java.util.Collections; |
| 27 | |
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 | 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; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Lists available resources. |
| 52 | */ |
| 53 | @Command(scope = "onos", name = "resources", |
| 54 | description = "Lists available resources") |
| 55 | public class ResourcesCommand extends AbstractShellCommand { |
| 56 | |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 57 | @Option(name = "-s", aliases = "--sort", description = "Sort output", |
| 58 | required = false, multiValued = false) |
| 59 | boolean sort = false; |
| 60 | |
| 61 | @Option(name = "-t", aliases = "--typeStrings", description = "List of resource types to be printed", |
| 62 | required = false, multiValued = true) |
| 63 | String[] typeStrings = null; |
| 64 | |
| 65 | Set<String> typesToPrint; |
| 66 | |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 67 | @Argument(index = 0, name = "deviceIdString", description = "Device ID", |
| 68 | required = false, multiValued = false) |
| 69 | String deviceIdStr = null; |
| 70 | |
| 71 | @Argument(index = 1, name = "portNumberString", description = "PortNumber", |
| 72 | required = false, multiValued = false) |
| 73 | String portNumberStr = null; |
| 74 | |
| 75 | |
| 76 | private ResourceService resourceService; |
| 77 | |
| 78 | @Override |
| 79 | protected void execute() { |
| 80 | resourceService = get(ResourceService.class); |
| 81 | |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 82 | if (typeStrings != null) { |
| 83 | typesToPrint = new HashSet<>(Arrays.asList(typeStrings)); |
| 84 | } else { |
| 85 | typesToPrint = Collections.emptySet(); |
| 86 | } |
| 87 | |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 88 | if (deviceIdStr != null && portNumberStr != null) { |
| 89 | DeviceId deviceId = deviceId(deviceIdStr); |
| 90 | PortNumber portNumber = PortNumber.fromString(portNumberStr); |
| 91 | |
Sho SHIMIZU | f95b96e | 2016-01-25 19:35:15 -0800 | [diff] [blame^] | 92 | printResource(Resource.discrete(deviceId, portNumber).resource(), 0); |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 93 | } else if (deviceIdStr != null) { |
| 94 | DeviceId deviceId = deviceId(deviceIdStr); |
| 95 | |
Sho SHIMIZU | f95b96e | 2016-01-25 19:35:15 -0800 | [diff] [blame^] | 96 | printResource(Resource.discrete(deviceId).resource(), 0); |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 97 | } else { |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 98 | printResource(Resource.ROOT, 0); |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 102 | private void printResource(Resource resource, int level) { |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 103 | // TODO add an option to show only available resource |
| 104 | Collection<Resource> children = resourceService.getRegisteredResources(resource); |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 105 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 106 | if (resource.equals(Resource.ROOT)) { |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 107 | print("ROOT"); |
| 108 | } else { |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 109 | String resourceName = resource.last().getClass().getSimpleName(); |
| 110 | |
| 111 | if (children.isEmpty() && !typesToPrint.isEmpty() && !typesToPrint.contains(resourceName)) { |
| 112 | // This resource is target of filtering |
| 113 | return; |
| 114 | } |
| 115 | |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 116 | |
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 { |
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) { |
Sho SHIMIZU | f33b893 | 2016-01-25 18:43:32 -0800 | [diff] [blame] | 154 | if (r instanceof ContinuousResource) { |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 155 | // non-aggregatable terminal node |
| 156 | nonAggregatable.add(r); |
| 157 | } else if (aggregatableTypes.contains(r.last().getClass())) { |
| 158 | // aggregatable & terminal node |
Naoki Shiota | d762746 | 2016-01-25 15:02:06 -0800 | [diff] [blame] | 159 | String className = r.last().getClass().getSimpleName(); |
| 160 | if (typesToPrint.isEmpty() || typesToPrint.contains(className)) { |
| 161 | aggregatables.put(className, r); |
| 162 | } |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 163 | } else { |
| 164 | nonAggregatable.add(r); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // print aggregated (terminal) |
| 169 | aggregatables.asMap().entrySet() |
| 170 | .forEach(e -> { |
| 171 | // for each type... |
| 172 | String resourceName = e.getKey(); |
| 173 | |
| 174 | RangeSet<Long> rangeSet = TreeRangeSet.create(); |
| 175 | |
| 176 | // aggregate into RangeSet |
| 177 | e.getValue().stream() |
| 178 | .map(Resource::last) |
| 179 | .map(res -> { |
| 180 | if (res instanceof VlanId) { |
| 181 | return (long) ((VlanId) res).toShort(); |
| 182 | } else if (res instanceof MplsLabel) { |
| 183 | return (long) ((MplsLabel) res).toInt(); |
| 184 | } else if (res instanceof TributarySlot) { |
| 185 | return ((TributarySlot) res).index(); |
| 186 | } |
| 187 | // TODO support Lambda (OchSignal types) |
| 188 | return 0L; |
| 189 | }) |
| 190 | .map(Range::singleton) |
| 191 | .map(range -> range.canonical(DiscreteDomain.longs())) |
| 192 | .forEach(rangeSet::add); |
| 193 | |
| 194 | print("%s%s: %s", Strings.repeat(" ", level + 1), |
| 195 | resourceName, |
| 196 | rangeSet); |
| 197 | }); |
| 198 | |
| 199 | |
| 200 | // print non-aggregatables (recurse) |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 201 | if (sort) { |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 202 | nonAggregatable.stream() |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 203 | .sorted((o1, o2) -> String.valueOf(o1.id()).compareTo(String.valueOf(o2.id()))) |
| 204 | .forEach(r -> printResource(r, level + 1)); |
| 205 | } else { |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame] | 206 | nonAggregatable.forEach(r -> printResource(r, level + 1)); |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 207 | } |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 208 | } |
| 209 | } |