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 | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 37 | import org.onosproject.net.newresource.Resource; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 38 | import org.onosproject.net.newresource.ResourceService; |
| 39 | |
| 40 | import com.google.common.base.Strings; |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame^] | 41 | import com.google.common.collect.ArrayListMultimap; |
| 42 | import com.google.common.collect.DiscreteDomain; |
| 43 | import com.google.common.collect.ImmutableSet; |
| 44 | import com.google.common.collect.Multimap; |
| 45 | import com.google.common.collect.Range; |
| 46 | import com.google.common.collect.RangeSet; |
| 47 | import com.google.common.collect.TreeRangeSet; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Lists available resources. |
| 51 | */ |
| 52 | @Command(scope = "onos", name = "resources", |
| 53 | description = "Lists available resources") |
| 54 | public class ResourcesCommand extends AbstractShellCommand { |
| 55 | |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 56 | @Option(name = "-s", aliases = "--sort", description = "Sort output", |
| 57 | required = false, multiValued = false) |
| 58 | boolean sort = false; |
| 59 | |
| 60 | @Option(name = "-t", aliases = "--typeStrings", description = "List of resource types to be printed", |
| 61 | required = false, multiValued = true) |
| 62 | String[] typeStrings = null; |
| 63 | |
| 64 | Set<String> typesToPrint; |
| 65 | |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 66 | @Argument(index = 0, name = "deviceIdString", description = "Device ID", |
| 67 | required = false, multiValued = false) |
| 68 | String deviceIdStr = null; |
| 69 | |
| 70 | @Argument(index = 1, name = "portNumberString", description = "PortNumber", |
| 71 | required = false, multiValued = false) |
| 72 | String portNumberStr = null; |
| 73 | |
| 74 | |
| 75 | private ResourceService resourceService; |
| 76 | |
| 77 | @Override |
| 78 | protected void execute() { |
| 79 | resourceService = get(ResourceService.class); |
| 80 | |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 81 | if (typeStrings != null) { |
| 82 | typesToPrint = new HashSet<>(Arrays.asList(typeStrings)); |
| 83 | } else { |
| 84 | typesToPrint = Collections.emptySet(); |
| 85 | } |
| 86 | |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 87 | if (deviceIdStr != null && portNumberStr != null) { |
| 88 | DeviceId deviceId = deviceId(deviceIdStr); |
| 89 | PortNumber portNumber = PortNumber.fromString(portNumberStr); |
| 90 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 91 | printResource(Resource.discrete(deviceId, portNumber), 0); |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 92 | } else if (deviceIdStr != null) { |
| 93 | DeviceId deviceId = deviceId(deviceIdStr); |
| 94 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 95 | printResource(Resource.discrete(deviceId), 0); |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 96 | } else { |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 97 | printResource(Resource.ROOT, 0); |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 101 | private void printResource(Resource resource, int level) { |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame^] | 102 | // TODO add an option to show only available resource |
| 103 | Collection<Resource> children = resourceService.getRegisteredResources(resource); |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 104 | |
Sho SHIMIZU | 8fa670a | 2016-01-14 11:17:18 -0800 | [diff] [blame] | 105 | if (resource.equals(Resource.ROOT)) { |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 106 | print("ROOT"); |
| 107 | } else { |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 108 | String resourceName = resource.last().getClass().getSimpleName(); |
| 109 | |
| 110 | if (children.isEmpty() && !typesToPrint.isEmpty() && !typesToPrint.contains(resourceName)) { |
| 111 | // This resource is target of filtering |
| 112 | return; |
| 113 | } |
| 114 | |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame^] | 115 | if (resource instanceof Resource.Continuous) { |
| 116 | print("%s%s: %f", Strings.repeat(" ", level), |
| 117 | resource.last(), |
| 118 | ((Resource.Continuous) resource).value()); |
| 119 | // Continuous resource is terminal node, stop here |
| 120 | return; |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 121 | } else { |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame^] | 122 | |
| 123 | String toString = String.valueOf(resource.last()); |
| 124 | if (toString.startsWith(resourceName)) { |
| 125 | print("%s%s", Strings.repeat(" ", level), |
| 126 | toString); |
| 127 | } else { |
| 128 | print("%s%s: %s", Strings.repeat(" ", level), |
| 129 | resourceName, |
| 130 | toString); |
| 131 | } |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame^] | 135 | |
| 136 | // Classify children into aggregatable terminal resources and everything else |
| 137 | |
| 138 | Set<Class<?>> aggregatableTypes = ImmutableSet.<Class<?>>builder() |
| 139 | .add(VlanId.class) |
| 140 | .add(MplsLabel.class) |
| 141 | .build(); |
| 142 | // (last() resource name) -> { Resource } |
| 143 | Multimap<String, Resource> aggregatables = ArrayListMultimap.create(); |
| 144 | List<Resource> nonAggregatable = new ArrayList<>(); |
| 145 | |
| 146 | for (Resource r : children) { |
| 147 | if (r instanceof Resource.Continuous) { |
| 148 | // non-aggregatable terminal node |
| 149 | nonAggregatable.add(r); |
| 150 | } else if (aggregatableTypes.contains(r.last().getClass())) { |
| 151 | // aggregatable & terminal node |
| 152 | aggregatables.put(r.last().getClass().getSimpleName(), r); |
| 153 | } else { |
| 154 | nonAggregatable.add(r); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // print aggregated (terminal) |
| 159 | aggregatables.asMap().entrySet() |
| 160 | .forEach(e -> { |
| 161 | // for each type... |
| 162 | String resourceName = e.getKey(); |
| 163 | |
| 164 | RangeSet<Long> rangeSet = TreeRangeSet.create(); |
| 165 | |
| 166 | // aggregate into RangeSet |
| 167 | e.getValue().stream() |
| 168 | .map(Resource::last) |
| 169 | .map(res -> { |
| 170 | if (res instanceof VlanId) { |
| 171 | return (long) ((VlanId) res).toShort(); |
| 172 | } else if (res instanceof MplsLabel) { |
| 173 | return (long) ((MplsLabel) res).toInt(); |
| 174 | } else if (res instanceof TributarySlot) { |
| 175 | return ((TributarySlot) res).index(); |
| 176 | } |
| 177 | // TODO support Lambda (OchSignal types) |
| 178 | return 0L; |
| 179 | }) |
| 180 | .map(Range::singleton) |
| 181 | .map(range -> range.canonical(DiscreteDomain.longs())) |
| 182 | .forEach(rangeSet::add); |
| 183 | |
| 184 | print("%s%s: %s", Strings.repeat(" ", level + 1), |
| 185 | resourceName, |
| 186 | rangeSet); |
| 187 | }); |
| 188 | |
| 189 | |
| 190 | // print non-aggregatables (recurse) |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 191 | if (sort) { |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame^] | 192 | nonAggregatable.stream() |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 193 | .sorted((o1, o2) -> String.valueOf(o1.id()).compareTo(String.valueOf(o2.id()))) |
| 194 | .forEach(r -> printResource(r, level + 1)); |
| 195 | } else { |
HIGUCHI Yuta | 634df8f | 2016-01-20 18:18:01 -0800 | [diff] [blame^] | 196 | nonAggregatable.forEach(r -> printResource(r, level + 1)); |
Naoki Shiota | 3f34218 | 2016-01-13 11:15:10 -0800 | [diff] [blame] | 197 | } |
HIGUCHI Yuta | 848324f | 2015-12-04 21:11:26 -0800 | [diff] [blame] | 198 | } |
| 199 | } |