blob: d6e637f0e46eb1e2eebc972d464103173ad4ee77 [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 SHIMIZUdd3750c2016-02-01 11:37:04 -080037import org.onosproject.net.newresource.DiscreteResource;
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080038import org.onosproject.net.newresource.Resource;
Naoki Shiota88745922016-02-10 16:28:25 -080039import org.onosproject.net.newresource.Resources;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080040import org.onosproject.net.newresource.ResourceService;
41
42import com.google.common.base.Strings;
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -080043import com.google.common.collect.ArrayListMultimap;
44import com.google.common.collect.DiscreteDomain;
45import com.google.common.collect.ImmutableSet;
46import com.google.common.collect.Multimap;
47import com.google.common.collect.Range;
48import com.google.common.collect.RangeSet;
49import com.google.common.collect.TreeRangeSet;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080050
51/**
52 * Lists available resources.
53 */
54@Command(scope = "onos", name = "resources",
55 description = "Lists available resources")
56public class ResourcesCommand extends AbstractShellCommand {
57
Naoki Shiota3f342182016-01-13 11:15:10 -080058 @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 Yuta848324f2015-12-04 21:11:26 -080068 @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 Shiota3f342182016-01-13 11:15:10 -080083 if (typeStrings != null) {
84 typesToPrint = new HashSet<>(Arrays.asList(typeStrings));
85 } else {
86 typesToPrint = Collections.emptySet();
87 }
88
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080089 if (deviceIdStr != null && portNumberStr != null) {
90 DeviceId deviceId = deviceId(deviceIdStr);
91 PortNumber portNumber = PortNumber.fromString(portNumberStr);
92
Sho SHIMIZU460b9722016-01-28 10:48:26 -080093 printResource(Resources.discrete(deviceId, portNumber).resource(), 0);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080094 } else if (deviceIdStr != null) {
95 DeviceId deviceId = deviceId(deviceIdStr);
96
Sho SHIMIZU460b9722016-01-28 10:48:26 -080097 printResource(Resources.discrete(deviceId).resource(), 0);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080098 } else {
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080099 printResource(Resource.ROOT, 0);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800100 }
101 }
102
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800103 private void printResource(Resource resource, int level) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800104 // TODO add an option to show only available resource
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800105 // 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 Shiota3f342182016-01-13 11:15:10 -0800112
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800113 if (resource.equals(Resource.ROOT)) {
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800114 print("ROOT");
115 } else {
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800116 if (resource instanceof ContinuousResource) {
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800117 String s = ((String) resource.last());
118 String simpleName = s.substring(s.lastIndexOf('.') + 1);
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800119 print("%s%s: %f", Strings.repeat(" ", level),
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800120 simpleName,
121 // 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 {
Naoki Shiota88745922016-02-10 16:28:25 -0800128 String resourceName = resource.last().getClass().getSimpleName();
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) {
Naoki Shiota88745922016-02-10 16:28:25 -0800154 if (!isPrintTarget(r)) {
155 continue;
156 }
157
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800158 if (r instanceof ContinuousResource) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800159 // non-aggregatable terminal node
160 nonAggregatable.add(r);
161 } else if (aggregatableTypes.contains(r.last().getClass())) {
162 // aggregatable & terminal node
Naoki Shiotad7627462016-01-25 15:02:06 -0800163 String className = r.last().getClass().getSimpleName();
Naoki Shiota88745922016-02-10 16:28:25 -0800164 aggregatables.put(className, r);
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800165 } else {
166 nonAggregatable.add(r);
167 }
168 }
169
170 // print aggregated (terminal)
171 aggregatables.asMap().entrySet()
172 .forEach(e -> {
173 // for each type...
174 String resourceName = e.getKey();
175
176 RangeSet<Long> rangeSet = TreeRangeSet.create();
177
178 // aggregate into RangeSet
179 e.getValue().stream()
180 .map(Resource::last)
181 .map(res -> {
182 if (res instanceof VlanId) {
183 return (long) ((VlanId) res).toShort();
184 } else if (res instanceof MplsLabel) {
185 return (long) ((MplsLabel) res).toInt();
186 } else if (res instanceof TributarySlot) {
187 return ((TributarySlot) res).index();
188 }
189 // TODO support Lambda (OchSignal types)
190 return 0L;
191 })
192 .map(Range::singleton)
193 .map(range -> range.canonical(DiscreteDomain.longs()))
194 .forEach(rangeSet::add);
195
196 print("%s%s: %s", Strings.repeat(" ", level + 1),
197 resourceName,
198 rangeSet);
199 });
200
201
202 // print non-aggregatables (recurse)
Naoki Shiota3f342182016-01-13 11:15:10 -0800203 if (sort) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800204 nonAggregatable.stream()
Naoki Shiota3f342182016-01-13 11:15:10 -0800205 .sorted((o1, o2) -> String.valueOf(o1.id()).compareTo(String.valueOf(o2.id())))
206 .forEach(r -> printResource(r, level + 1));
207 } else {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800208 nonAggregatable.forEach(r -> printResource(r, level + 1));
Naoki Shiota3f342182016-01-13 11:15:10 -0800209 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800210 }
Naoki Shiota88745922016-02-10 16:28:25 -0800211
212 private boolean isPrintTarget(Resource resource) {
213 if (typesToPrint.isEmpty()) {
214 return true;
215 }
216
217 String resourceName;
218 if (resource instanceof ContinuousResource) {
219 String s = (String) resource.last();
220 resourceName = s.substring(s.lastIndexOf('.') + 1);
221 } else if (resource instanceof DiscreteResource) {
222 // TODO This distributed store access incurs overhead.
223 // This should be merged with the one in printResource()
224 if (!resourceService.getRegisteredResources(((DiscreteResource) resource).id()).isEmpty()) {
225 // resource which has children should be printed
226 return true;
227 }
228 resourceName = resource.last().getClass().getSimpleName();
229 } else {
230 log.warn("Unexpected resource class: {}", resource.getClass().getSimpleName());
231 return false;
232 }
233
234 return typesToPrint.contains(resourceName);
235 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800236}