blob: 1f791132717c15af7eab9f4b2cad971b77f6144c [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;
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 SHIMIZU8fa670a2016-01-14 11:17:18 -080092 printResource(Resource.discrete(deviceId, portNumber), 0);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080093 } else if (deviceIdStr != null) {
94 DeviceId deviceId = deviceId(deviceIdStr);
95
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080096 printResource(Resource.discrete(deviceId), 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
104 Collection<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
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800116 if (resource instanceof ContinuousResource) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800117 print("%s%s: %f", Strings.repeat(" ", level),
118 resource.last(),
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800119 ((ContinuousResource) resource).value());
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800120 // Continuous resource is terminal node, stop here
121 return;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800122 } else {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800123
124 String toString = String.valueOf(resource.last());
125 if (toString.startsWith(resourceName)) {
126 print("%s%s", Strings.repeat(" ", level),
127 toString);
128 } else {
129 print("%s%s: %s", Strings.repeat(" ", level),
130 resourceName,
131 toString);
132 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800133 }
134 }
135
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800136
137 // Classify children into aggregatable terminal resources and everything else
138
139 Set<Class<?>> aggregatableTypes = ImmutableSet.<Class<?>>builder()
140 .add(VlanId.class)
141 .add(MplsLabel.class)
142 .build();
143 // (last() resource name) -> { Resource }
144 Multimap<String, Resource> aggregatables = ArrayListMultimap.create();
145 List<Resource> nonAggregatable = new ArrayList<>();
146
147 for (Resource r : children) {
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800148 if (r instanceof ContinuousResource) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800149 // non-aggregatable terminal node
150 nonAggregatable.add(r);
151 } else if (aggregatableTypes.contains(r.last().getClass())) {
152 // aggregatable & terminal node
Naoki Shiotad7627462016-01-25 15:02:06 -0800153 String className = r.last().getClass().getSimpleName();
154 if (typesToPrint.isEmpty() || typesToPrint.contains(className)) {
155 aggregatables.put(className, r);
156 }
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800157 } else {
158 nonAggregatable.add(r);
159 }
160 }
161
162 // print aggregated (terminal)
163 aggregatables.asMap().entrySet()
164 .forEach(e -> {
165 // for each type...
166 String resourceName = e.getKey();
167
168 RangeSet<Long> rangeSet = TreeRangeSet.create();
169
170 // aggregate into RangeSet
171 e.getValue().stream()
172 .map(Resource::last)
173 .map(res -> {
174 if (res instanceof VlanId) {
175 return (long) ((VlanId) res).toShort();
176 } else if (res instanceof MplsLabel) {
177 return (long) ((MplsLabel) res).toInt();
178 } else if (res instanceof TributarySlot) {
179 return ((TributarySlot) res).index();
180 }
181 // TODO support Lambda (OchSignal types)
182 return 0L;
183 })
184 .map(Range::singleton)
185 .map(range -> range.canonical(DiscreteDomain.longs()))
186 .forEach(rangeSet::add);
187
188 print("%s%s: %s", Strings.repeat(" ", level + 1),
189 resourceName,
190 rangeSet);
191 });
192
193
194 // print non-aggregatables (recurse)
Naoki Shiota3f342182016-01-13 11:15:10 -0800195 if (sort) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800196 nonAggregatable.stream()
Naoki Shiota3f342182016-01-13 11:15:10 -0800197 .sorted((o1, o2) -> String.valueOf(o1.id()).compareTo(String.valueOf(o2.id())))
198 .forEach(r -> printResource(r, level + 1));
199 } else {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800200 nonAggregatable.forEach(r -> printResource(r, level + 1));
Naoki Shiota3f342182016-01-13 11:15:10 -0800201 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800202 }
203}