blob: c9a2ad652a770e24bb9de8997f2d5f5065b04501 [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;
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
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 {
Naoki Shiota3f342182016-01-13 11:15:10 -0800116 String resourceName = resource.last().getClass().getSimpleName();
117
118 if (children.isEmpty() && !typesToPrint.isEmpty() && !typesToPrint.contains(resourceName)) {
119 // This resource is target of filtering
120 return;
121 }
122
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800123
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800124 if (resource instanceof ContinuousResource) {
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800125 String s = ((String) resource.last());
126 String simpleName = s.substring(s.lastIndexOf('.') + 1);
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800127 print("%s%s: %f", Strings.repeat(" ", level),
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800128 simpleName,
129 // Note: last() does not return, what we've registered
130 // following does not work
131 //((Class<?>) resource.last()).getSimpleName(),
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800132 ((ContinuousResource) resource).value());
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800133 // Continuous resource is terminal node, stop here
134 return;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800135 } else {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800136
137 String toString = String.valueOf(resource.last());
138 if (toString.startsWith(resourceName)) {
139 print("%s%s", Strings.repeat(" ", level),
140 toString);
141 } else {
142 print("%s%s: %s", Strings.repeat(" ", level),
143 resourceName,
144 toString);
145 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800146 }
147 }
148
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800149
150 // Classify children into aggregatable terminal resources and everything else
151
152 Set<Class<?>> aggregatableTypes = ImmutableSet.<Class<?>>builder()
153 .add(VlanId.class)
154 .add(MplsLabel.class)
155 .build();
156 // (last() resource name) -> { Resource }
157 Multimap<String, Resource> aggregatables = ArrayListMultimap.create();
158 List<Resource> nonAggregatable = new ArrayList<>();
159
160 for (Resource r : children) {
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800161 if (r instanceof ContinuousResource) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800162 // non-aggregatable terminal node
163 nonAggregatable.add(r);
164 } else if (aggregatableTypes.contains(r.last().getClass())) {
165 // aggregatable & terminal node
Naoki Shiotad7627462016-01-25 15:02:06 -0800166 String className = r.last().getClass().getSimpleName();
167 if (typesToPrint.isEmpty() || typesToPrint.contains(className)) {
168 aggregatables.put(className, r);
169 }
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800170 } else {
171 nonAggregatable.add(r);
172 }
173 }
174
175 // print aggregated (terminal)
176 aggregatables.asMap().entrySet()
177 .forEach(e -> {
178 // for each type...
179 String resourceName = e.getKey();
180
181 RangeSet<Long> rangeSet = TreeRangeSet.create();
182
183 // aggregate into RangeSet
184 e.getValue().stream()
185 .map(Resource::last)
186 .map(res -> {
187 if (res instanceof VlanId) {
188 return (long) ((VlanId) res).toShort();
189 } else if (res instanceof MplsLabel) {
190 return (long) ((MplsLabel) res).toInt();
191 } else if (res instanceof TributarySlot) {
192 return ((TributarySlot) res).index();
193 }
194 // TODO support Lambda (OchSignal types)
195 return 0L;
196 })
197 .map(Range::singleton)
198 .map(range -> range.canonical(DiscreteDomain.longs()))
199 .forEach(rangeSet::add);
200
201 print("%s%s: %s", Strings.repeat(" ", level + 1),
202 resourceName,
203 rangeSet);
204 });
205
206
207 // print non-aggregatables (recurse)
Naoki Shiota3f342182016-01-13 11:15:10 -0800208 if (sort) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800209 nonAggregatable.stream()
Naoki Shiota3f342182016-01-13 11:15:10 -0800210 .sorted((o1, o2) -> String.valueOf(o1.id()).compareTo(String.valueOf(o2.id())))
211 .forEach(r -> printResource(r, level + 1));
212 } else {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800213 nonAggregatable.forEach(r -> printResource(r, level + 1));
Naoki Shiota3f342182016-01-13 11:15:10 -0800214 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800215 }
216}