blob: 37faa0b47fd57d7f3ea768874b5965c5221b548d [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
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080027import org.apache.karaf.shell.commands.Argument;
28import org.apache.karaf.shell.commands.Command;
Naoki Shiota3f342182016-01-13 11:15:10 -080029import org.apache.karaf.shell.commands.Option;
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -080030import org.onlab.packet.MplsLabel;
31import org.onlab.packet.VlanId;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080032import org.onosproject.cli.AbstractShellCommand;
33import org.onosproject.net.DeviceId;
34import org.onosproject.net.PortNumber;
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -080035import org.onosproject.net.TributarySlot;
Sho SHIMIZUf33b8932016-01-25 18:43:32 -080036import org.onosproject.net.newresource.ContinuousResource;
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080037import org.onosproject.net.newresource.Resource;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080038import org.onosproject.net.newresource.ResourceService;
39
40import com.google.common.base.Strings;
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -080041import com.google.common.collect.ArrayListMultimap;
42import com.google.common.collect.DiscreteDomain;
43import com.google.common.collect.ImmutableSet;
44import com.google.common.collect.Multimap;
45import com.google.common.collect.Range;
46import com.google.common.collect.RangeSet;
47import com.google.common.collect.TreeRangeSet;
Sho SHIMIZU460b9722016-01-28 10:48:26 -080048import org.onosproject.net.newresource.Resources;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080049
50/**
51 * Lists available resources.
52 */
53@Command(scope = "onos", name = "resources",
54 description = "Lists available resources")
55public class ResourcesCommand extends AbstractShellCommand {
56
Naoki Shiota3f342182016-01-13 11:15:10 -080057 @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 Yuta848324f2015-12-04 21:11:26 -080067 @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 Shiota3f342182016-01-13 11:15:10 -080082 if (typeStrings != null) {
83 typesToPrint = new HashSet<>(Arrays.asList(typeStrings));
84 } else {
85 typesToPrint = Collections.emptySet();
86 }
87
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080088 if (deviceIdStr != null && portNumberStr != null) {
89 DeviceId deviceId = deviceId(deviceIdStr);
90 PortNumber portNumber = PortNumber.fromString(portNumberStr);
91
Sho SHIMIZU460b9722016-01-28 10:48:26 -080092 printResource(Resources.discrete(deviceId, portNumber).resource(), 0);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080093 } else if (deviceIdStr != null) {
94 DeviceId deviceId = deviceId(deviceIdStr);
95
Sho SHIMIZU460b9722016-01-28 10:48:26 -080096 printResource(Resources.discrete(deviceId).resource(), 0);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080097 } else {
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080098 printResource(Resource.ROOT, 0);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080099 }
100 }
101
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800102 private void printResource(Resource resource, int level) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800103 // TODO add an option to show only available resource
Sho SHIMIZU83258ae2016-01-29 17:39:07 -0800104 Set<Resource> children = resourceService.getRegisteredResources(resource);
Naoki Shiota3f342182016-01-13 11:15:10 -0800105
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800106 if (resource.equals(Resource.ROOT)) {
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800107 print("ROOT");
108 } else {
Naoki Shiota3f342182016-01-13 11:15:10 -0800109 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 Yuta1d7c9cb2016-01-20 18:22:36 -0800116
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800117 if (resource instanceof ContinuousResource) {
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800118 String s = ((String) resource.last());
119 String simpleName = s.substring(s.lastIndexOf('.') + 1);
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800120 print("%s%s: %f", Strings.repeat(" ", level),
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800121 simpleName,
122 // Note: last() does not return, what we've registered
123 // following does not work
124 //((Class<?>) resource.last()).getSimpleName(),
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800125 ((ContinuousResource) resource).value());
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800126 // Continuous resource is terminal node, stop here
127 return;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800128 } else {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800129
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 Yuta848324f2015-12-04 21:11:26 -0800139 }
140 }
141
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800142
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 SHIMIZUf33b8932016-01-25 18:43:32 -0800154 if (r instanceof ContinuousResource) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800155 // non-aggregatable terminal node
156 nonAggregatable.add(r);
157 } else if (aggregatableTypes.contains(r.last().getClass())) {
158 // aggregatable & terminal node
Naoki Shiotad7627462016-01-25 15:02:06 -0800159 String className = r.last().getClass().getSimpleName();
160 if (typesToPrint.isEmpty() || typesToPrint.contains(className)) {
161 aggregatables.put(className, r);
162 }
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()
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 Shiota3f342182016-01-13 11:15:10 -0800201 if (sort) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800202 nonAggregatable.stream()
Naoki Shiota3f342182016-01-13 11:15:10 -0800203 .sorted((o1, o2) -> String.valueOf(o1.id()).compareTo(String.valueOf(o2.id())))
204 .forEach(r -> printResource(r, level + 1));
205 } else {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800206 nonAggregatable.forEach(r -> printResource(r, level + 1));
Naoki Shiota3f342182016-01-13 11:15:10 -0800207 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800208 }
209}