blob: c64518c306034f5779cad79cb80e59b2ed7445a3 [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;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080025import java.util.Collection;
Naoki Shiota3f342182016-01-13 11:15:10 -080026import java.util.Collections;
27
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 SHIMIZU8fa670a2016-01-14 11:17:18 -080038import org.onosproject.net.newresource.Resource;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080039import org.onosproject.net.newresource.ResourceService;
40
41import com.google.common.base.Strings;
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -080042import com.google.common.collect.ArrayListMultimap;
43import com.google.common.collect.DiscreteDomain;
44import com.google.common.collect.ImmutableSet;
45import com.google.common.collect.Multimap;
46import com.google.common.collect.Range;
47import com.google.common.collect.RangeSet;
48import com.google.common.collect.TreeRangeSet;
Sho SHIMIZU460b9722016-01-28 10:48:26 -080049import org.onosproject.net.newresource.Resources;
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
105 Collection<Resource> children = resourceService.getRegisteredResources(resource);
Naoki Shiota3f342182016-01-13 11:15:10 -0800106
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800107 if (resource.equals(Resource.ROOT)) {
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800108 print("ROOT");
109 } else {
Naoki Shiota3f342182016-01-13 11:15:10 -0800110 String resourceName = resource.last().getClass().getSimpleName();
111
112 if (children.isEmpty() && !typesToPrint.isEmpty() && !typesToPrint.contains(resourceName)) {
113 // This resource is target of filtering
114 return;
115 }
116
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800117
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800118 if (resource instanceof ContinuousResource) {
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800119 String s = ((String) resource.last());
120 String simpleName = s.substring(s.lastIndexOf('.') + 1);
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800121 print("%s%s: %f", Strings.repeat(" ", level),
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800122 simpleName,
123 // Note: last() does not return, what we've registered
124 // following does not work
125 //((Class<?>) resource.last()).getSimpleName(),
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800126 ((ContinuousResource) resource).value());
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800127 // Continuous resource is terminal node, stop here
128 return;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800129 } else {
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) {
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800155 if (r instanceof ContinuousResource) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800156 // non-aggregatable terminal node
157 nonAggregatable.add(r);
158 } else if (aggregatableTypes.contains(r.last().getClass())) {
159 // aggregatable & terminal node
Naoki Shiotad7627462016-01-25 15:02:06 -0800160 String className = r.last().getClass().getSimpleName();
161 if (typesToPrint.isEmpty() || typesToPrint.contains(className)) {
162 aggregatables.put(className, r);
163 }
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800164 } else {
165 nonAggregatable.add(r);
166 }
167 }
168
169 // print aggregated (terminal)
170 aggregatables.asMap().entrySet()
171 .forEach(e -> {
172 // for each type...
173 String resourceName = e.getKey();
174
175 RangeSet<Long> rangeSet = TreeRangeSet.create();
176
177 // aggregate into RangeSet
178 e.getValue().stream()
179 .map(Resource::last)
180 .map(res -> {
181 if (res instanceof VlanId) {
182 return (long) ((VlanId) res).toShort();
183 } else if (res instanceof MplsLabel) {
184 return (long) ((MplsLabel) res).toInt();
185 } else if (res instanceof TributarySlot) {
186 return ((TributarySlot) res).index();
187 }
188 // TODO support Lambda (OchSignal types)
189 return 0L;
190 })
191 .map(Range::singleton)
192 .map(range -> range.canonical(DiscreteDomain.longs()))
193 .forEach(rangeSet::add);
194
195 print("%s%s: %s", Strings.repeat(" ", level + 1),
196 resourceName,
197 rangeSet);
198 });
199
200
201 // print non-aggregatables (recurse)
Naoki Shiota3f342182016-01-13 11:15:10 -0800202 if (sort) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800203 nonAggregatable.stream()
Naoki Shiota3f342182016-01-13 11:15:10 -0800204 .sorted((o1, o2) -> String.valueOf(o1.id()).compareTo(String.valueOf(o2.id())))
205 .forEach(r -> printResource(r, level + 1));
206 } else {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800207 nonAggregatable.forEach(r -> printResource(r, level + 1));
Naoki Shiota3f342182016-01-13 11:15:10 -0800208 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800209 }
210}