blob: 3b8ca89a8dca161ef8dca3f0a40e720f729a32b6 [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 SHIMIZUf33b8932016-01-25 18:43:32 -080037import org.onosproject.net.newresource.ContinuousResource;
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -080038import org.onosproject.net.newresource.DiscreteResource;
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080039import org.onosproject.net.newresource.Resource;
Naoki Shiota88745922016-02-10 16:28:25 -080040import org.onosproject.net.newresource.Resources;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080041import org.onosproject.net.newresource.ResourceService;
42
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 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 {
Naoki Shiota88745922016-02-10 16:28:25 -0800129 String resourceName = resource.last().getClass().getSimpleName();
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800130
131 String toString = String.valueOf(resource.last());
132 if (toString.startsWith(resourceName)) {
133 print("%s%s", Strings.repeat(" ", level),
134 toString);
135 } else {
136 print("%s%s: %s", Strings.repeat(" ", level),
137 resourceName,
138 toString);
139 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800140 }
141 }
142
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800143
144 // Classify children into aggregatable terminal resources and everything else
145
146 Set<Class<?>> aggregatableTypes = ImmutableSet.<Class<?>>builder()
147 .add(VlanId.class)
148 .add(MplsLabel.class)
149 .build();
150 // (last() resource name) -> { Resource }
151 Multimap<String, Resource> aggregatables = ArrayListMultimap.create();
152 List<Resource> nonAggregatable = new ArrayList<>();
153
154 for (Resource r : children) {
Naoki Shiota88745922016-02-10 16:28:25 -0800155 if (!isPrintTarget(r)) {
156 continue;
157 }
158
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800159 if (r instanceof ContinuousResource) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800160 // non-aggregatable terminal node
161 nonAggregatable.add(r);
Sho SHIMIZU003ed322016-02-11 12:58:42 -0800162 } else if (Iterables.any(aggregatableTypes, r::isTypeOf)) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800163 // aggregatable & terminal node
Naoki Shiotad7627462016-01-25 15:02:06 -0800164 String className = r.last().getClass().getSimpleName();
Naoki Shiota88745922016-02-10 16:28:25 -0800165 aggregatables.put(className, r);
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800166 } else {
167 nonAggregatable.add(r);
168 }
169 }
170
171 // print aggregated (terminal)
172 aggregatables.asMap().entrySet()
173 .forEach(e -> {
174 // for each type...
175 String resourceName = e.getKey();
176
177 RangeSet<Long> rangeSet = TreeRangeSet.create();
178
179 // aggregate into RangeSet
180 e.getValue().stream()
181 .map(Resource::last)
182 .map(res -> {
183 if (res instanceof VlanId) {
184 return (long) ((VlanId) res).toShort();
185 } else if (res instanceof MplsLabel) {
186 return (long) ((MplsLabel) res).toInt();
187 } else if (res instanceof TributarySlot) {
188 return ((TributarySlot) res).index();
189 }
190 // TODO support Lambda (OchSignal types)
191 return 0L;
192 })
193 .map(Range::singleton)
194 .map(range -> range.canonical(DiscreteDomain.longs()))
195 .forEach(rangeSet::add);
196
197 print("%s%s: %s", Strings.repeat(" ", level + 1),
198 resourceName,
199 rangeSet);
200 });
201
202
203 // print non-aggregatables (recurse)
Naoki Shiota3f342182016-01-13 11:15:10 -0800204 if (sort) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800205 nonAggregatable.stream()
Naoki Shiota3f342182016-01-13 11:15:10 -0800206 .sorted((o1, o2) -> String.valueOf(o1.id()).compareTo(String.valueOf(o2.id())))
207 .forEach(r -> printResource(r, level + 1));
208 } else {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800209 nonAggregatable.forEach(r -> printResource(r, level + 1));
Naoki Shiota3f342182016-01-13 11:15:10 -0800210 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800211 }
Naoki Shiota88745922016-02-10 16:28:25 -0800212
213 private boolean isPrintTarget(Resource resource) {
214 if (typesToPrint.isEmpty()) {
215 return true;
216 }
217
218 String resourceName;
219 if (resource instanceof ContinuousResource) {
220 String s = (String) resource.last();
221 resourceName = s.substring(s.lastIndexOf('.') + 1);
222 } else if (resource instanceof DiscreteResource) {
223 // TODO This distributed store access incurs overhead.
224 // This should be merged with the one in printResource()
225 if (!resourceService.getRegisteredResources(((DiscreteResource) resource).id()).isEmpty()) {
226 // resource which has children should be printed
227 return true;
228 }
229 resourceName = resource.last().getClass().getSimpleName();
230 } else {
231 log.warn("Unexpected resource class: {}", resource.getClass().getSimpleName());
232 return false;
233 }
234
235 return typesToPrint.contains(resourceName);
236 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800237}