blob: 76aaa5c3cc38fe3071db8087a8e2e52503e34348 [file] [log] [blame]
HIGUCHI Yuta848324f2015-12-04 21:11:26 -08001/*
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 */
16package org.onosproject.cli.net;
17
18import static org.onosproject.net.DeviceId.deviceId;
19
Naoki Shiota3f342182016-01-13 11:15:10 -080020import java.util.Set;
21import java.util.HashSet;
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -080022import java.util.List;
23import java.util.ArrayList;
Naoki Shiota3f342182016-01-13 11:15:10 -080024import java.util.Arrays;
Naoki Shiota3f342182016-01-13 11:15:10 -080025import java.util.Collections;
26
Sho SHIMIZU003ed322016-02-11 12:58:42 -080027import com.google.common.collect.Iterables;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080028import org.apache.karaf.shell.commands.Argument;
29import org.apache.karaf.shell.commands.Command;
Naoki Shiota3f342182016-01-13 11:15:10 -080030import org.apache.karaf.shell.commands.Option;
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -080031import org.onlab.packet.MplsLabel;
32import org.onlab.packet.VlanId;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080033import org.onosproject.cli.AbstractShellCommand;
34import org.onosproject.net.DeviceId;
35import org.onosproject.net.PortNumber;
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -080036import org.onosproject.net.TributarySlot;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080037import org.onosproject.net.resource.ContinuousResource;
38import org.onosproject.net.resource.DiscreteResource;
39import org.onosproject.net.resource.Resource;
40import org.onosproject.net.resource.Resources;
41import org.onosproject.net.resource.ResourceService;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080042
43import com.google.common.base.Strings;
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -080044import com.google.common.collect.ArrayListMultimap;
45import com.google.common.collect.DiscreteDomain;
46import com.google.common.collect.ImmutableSet;
47import com.google.common.collect.Multimap;
48import com.google.common.collect.Range;
49import com.google.common.collect.RangeSet;
50import com.google.common.collect.TreeRangeSet;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080051
52/**
53 * Lists available resources.
54 */
55@Command(scope = "onos", name = "resources",
56 description = "Lists available resources")
57public class ResourcesCommand extends AbstractShellCommand {
58
Naoki Shiota3f342182016-01-13 11:15:10 -080059 @Option(name = "-s", aliases = "--sort", description = "Sort output",
60 required = false, multiValued = false)
61 boolean sort = false;
62
63 @Option(name = "-t", aliases = "--typeStrings", description = "List of resource types to be printed",
64 required = false, multiValued = true)
65 String[] typeStrings = null;
66
67 Set<String> typesToPrint;
68
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080069 @Argument(index = 0, name = "deviceIdString", description = "Device ID",
70 required = false, multiValued = false)
71 String deviceIdStr = null;
72
73 @Argument(index = 1, name = "portNumberString", description = "PortNumber",
74 required = false, multiValued = false)
75 String portNumberStr = null;
76
77
78 private ResourceService resourceService;
79
80 @Override
81 protected void execute() {
82 resourceService = get(ResourceService.class);
83
Naoki Shiota3f342182016-01-13 11:15:10 -080084 if (typeStrings != null) {
85 typesToPrint = new HashSet<>(Arrays.asList(typeStrings));
86 } else {
87 typesToPrint = Collections.emptySet();
88 }
89
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080090 if (deviceIdStr != null && portNumberStr != null) {
91 DeviceId deviceId = deviceId(deviceIdStr);
92 PortNumber portNumber = PortNumber.fromString(portNumberStr);
93
Sho SHIMIZU460b9722016-01-28 10:48:26 -080094 printResource(Resources.discrete(deviceId, portNumber).resource(), 0);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080095 } else if (deviceIdStr != null) {
96 DeviceId deviceId = deviceId(deviceIdStr);
97
Sho SHIMIZU460b9722016-01-28 10:48:26 -080098 printResource(Resources.discrete(deviceId).resource(), 0);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080099 } else {
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800100 printResource(Resource.ROOT, 0);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800101 }
102 }
103
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800104 private void printResource(Resource resource, int level) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800105 // TODO add an option to show only available resource
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800106 // workaround to preserve the original behavior of ResourceService#getRegisteredResources
107 Set<Resource> children;
108 if (resource instanceof DiscreteResource) {
109 children = resourceService.getRegisteredResources(((DiscreteResource) resource).id());
110 } else {
111 children = Collections.emptySet();
112 }
Naoki Shiota3f342182016-01-13 11:15:10 -0800113
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800114 if (resource.equals(Resource.ROOT)) {
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800115 print("ROOT");
116 } else {
Sho SHIMIZU5a8e8f92016-02-22 11:33:36 -0800117 String resourceName = resource.simpleTypeName();
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800118 if (resource instanceof ContinuousResource) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800119 print("%s%s: %f", Strings.repeat(" ", level),
Sho SHIMIZU5a8e8f92016-02-22 11:33:36 -0800120 resourceName,
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800121 // Note: last() does not return, what we've registered
122 // following does not work
123 //((Class<?>) resource.last()).getSimpleName(),
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800124 ((ContinuousResource) resource).value());
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800125 // Continuous resource is terminal node, stop here
126 return;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800127 } else {
Sho SHIMIZUf08cb4c2016-02-11 18:35:59 -0800128 String toString = String.valueOf(resource.valueAs(Object.class).orElse(""));
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800129 if (toString.startsWith(resourceName)) {
130 print("%s%s", Strings.repeat(" ", level),
131 toString);
132 } else {
133 print("%s%s: %s", Strings.repeat(" ", level),
134 resourceName,
135 toString);
136 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800137 }
138 }
139
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800140
141 // Classify children into aggregatable terminal resources and everything else
142
143 Set<Class<?>> aggregatableTypes = ImmutableSet.<Class<?>>builder()
144 .add(VlanId.class)
145 .add(MplsLabel.class)
146 .build();
147 // (last() resource name) -> { Resource }
148 Multimap<String, Resource> aggregatables = ArrayListMultimap.create();
149 List<Resource> nonAggregatable = new ArrayList<>();
150
151 for (Resource r : children) {
Naoki Shiota88745922016-02-10 16:28:25 -0800152 if (!isPrintTarget(r)) {
153 continue;
154 }
155
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800156 if (r instanceof ContinuousResource) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800157 // non-aggregatable terminal node
158 nonAggregatable.add(r);
Sho SHIMIZU003ed322016-02-11 12:58:42 -0800159 } else if (Iterables.any(aggregatableTypes, r::isTypeOf)) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800160 // aggregatable & terminal node
Sho SHIMIZU5a8e8f92016-02-22 11:33:36 -0800161 String simpleName = r.simpleTypeName();
162 aggregatables.put(simpleName, r);
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800163 } 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()
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800178 .map(res -> {
Sho SHIMIZUf08cb4c2016-02-11 18:35:59 -0800179 if (res.isTypeOf(VlanId.class)) {
180 return (long) res.valueAs(VlanId.class).get().toShort();
181 } else if (res.isTypeOf(MplsLabel.class)) {
182 return (long) res.valueAs(MplsLabel.class).get().toInt();
183 } else if (res.isTypeOf(TributarySlot.class)) {
184 return res.valueAs(TributarySlot.class).get().index();
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800185 }
186 // TODO support Lambda (OchSignal types)
187 return 0L;
188 })
189 .map(Range::singleton)
190 .map(range -> range.canonical(DiscreteDomain.longs()))
191 .forEach(rangeSet::add);
192
193 print("%s%s: %s", Strings.repeat(" ", level + 1),
194 resourceName,
195 rangeSet);
196 });
197
198
199 // print non-aggregatables (recurse)
Naoki Shiota3f342182016-01-13 11:15:10 -0800200 if (sort) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800201 nonAggregatable.stream()
Naoki Shiota3f342182016-01-13 11:15:10 -0800202 .sorted((o1, o2) -> String.valueOf(o1.id()).compareTo(String.valueOf(o2.id())))
203 .forEach(r -> printResource(r, level + 1));
204 } else {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800205 nonAggregatable.forEach(r -> printResource(r, level + 1));
Naoki Shiota3f342182016-01-13 11:15:10 -0800206 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800207 }
Naoki Shiota88745922016-02-10 16:28:25 -0800208
209 private boolean isPrintTarget(Resource resource) {
210 if (typesToPrint.isEmpty()) {
211 return true;
212 }
213
Sho SHIMIZU5a8e8f92016-02-22 11:33:36 -0800214 String resourceName = resource.simpleTypeName();
215 if (resource instanceof DiscreteResource) {
Naoki Shiota88745922016-02-10 16:28:25 -0800216 // TODO This distributed store access incurs overhead.
217 // This should be merged with the one in printResource()
218 if (!resourceService.getRegisteredResources(((DiscreteResource) resource).id()).isEmpty()) {
219 // resource which has children should be printed
220 return true;
221 }
Sho SHIMIZU5a8e8f92016-02-22 11:33:36 -0800222 } else if (!(resource instanceof ContinuousResource)) {
Naoki Shiota88745922016-02-10 16:28:25 -0800223 log.warn("Unexpected resource class: {}", resource.getClass().getSimpleName());
224 return false;
225 }
226
227 return typesToPrint.contains(resourceName);
228 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800229}