blob: 1cc0463f8f86af994a536e3a9996dad43e451990 [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 SHIMIZU8fa670a2016-01-14 11:17:18 -080037import org.onosproject.net.newresource.Resource;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080038import org.onosproject.net.newresource.ResourceService;
39
40import com.google.common.base.Strings;
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -080041import com.google.common.collect.ArrayListMultimap;
42import com.google.common.collect.DiscreteDomain;
43import com.google.common.collect.ImmutableSet;
44import com.google.common.collect.Multimap;
45import com.google.common.collect.Range;
46import com.google.common.collect.RangeSet;
47import com.google.common.collect.TreeRangeSet;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080048
49/**
50 * Lists available resources.
51 */
52@Command(scope = "onos", name = "resources",
53 description = "Lists available resources")
54public class ResourcesCommand extends AbstractShellCommand {
55
Naoki Shiota3f342182016-01-13 11:15:10 -080056 @Option(name = "-s", aliases = "--sort", description = "Sort output",
57 required = false, multiValued = false)
58 boolean sort = false;
59
60 @Option(name = "-t", aliases = "--typeStrings", description = "List of resource types to be printed",
61 required = false, multiValued = true)
62 String[] typeStrings = null;
63
64 Set<String> typesToPrint;
65
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080066 @Argument(index = 0, name = "deviceIdString", description = "Device ID",
67 required = false, multiValued = false)
68 String deviceIdStr = null;
69
70 @Argument(index = 1, name = "portNumberString", description = "PortNumber",
71 required = false, multiValued = false)
72 String portNumberStr = null;
73
74
75 private ResourceService resourceService;
76
77 @Override
78 protected void execute() {
79 resourceService = get(ResourceService.class);
80
Naoki Shiota3f342182016-01-13 11:15:10 -080081 if (typeStrings != null) {
82 typesToPrint = new HashSet<>(Arrays.asList(typeStrings));
83 } else {
84 typesToPrint = Collections.emptySet();
85 }
86
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080087 if (deviceIdStr != null && portNumberStr != null) {
88 DeviceId deviceId = deviceId(deviceIdStr);
89 PortNumber portNumber = PortNumber.fromString(portNumberStr);
90
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080091 printResource(Resource.discrete(deviceId, portNumber), 0);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080092 } else if (deviceIdStr != null) {
93 DeviceId deviceId = deviceId(deviceIdStr);
94
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080095 printResource(Resource.discrete(deviceId), 0);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080096 } else {
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080097 printResource(Resource.ROOT, 0);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080098 }
99 }
100
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800101 private void printResource(Resource resource, int level) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800102 // TODO add an option to show only available resource
103 Collection<Resource> children = resourceService.getRegisteredResources(resource);
Naoki Shiota3f342182016-01-13 11:15:10 -0800104
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800105 if (resource.equals(Resource.ROOT)) {
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800106 print("ROOT");
107 } else {
Naoki Shiota3f342182016-01-13 11:15:10 -0800108 String resourceName = resource.last().getClass().getSimpleName();
109
110 if (children.isEmpty() && !typesToPrint.isEmpty() && !typesToPrint.contains(resourceName)) {
111 // This resource is target of filtering
112 return;
113 }
114
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800115 if (resource instanceof Resource.Continuous) {
116 print("%s%s: %f", Strings.repeat(" ", level),
117 resource.last(),
118 ((Resource.Continuous) resource).value());
119 // Continuous resource is terminal node, stop here
120 return;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800121 } else {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800122
123 String toString = String.valueOf(resource.last());
124 if (toString.startsWith(resourceName)) {
125 print("%s%s", Strings.repeat(" ", level),
126 toString);
127 } else {
128 print("%s%s: %s", Strings.repeat(" ", level),
129 resourceName,
130 toString);
131 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800132 }
133 }
134
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800135
136 // Classify children into aggregatable terminal resources and everything else
137
138 Set<Class<?>> aggregatableTypes = ImmutableSet.<Class<?>>builder()
139 .add(VlanId.class)
140 .add(MplsLabel.class)
141 .build();
142 // (last() resource name) -> { Resource }
143 Multimap<String, Resource> aggregatables = ArrayListMultimap.create();
144 List<Resource> nonAggregatable = new ArrayList<>();
145
146 for (Resource r : children) {
147 if (r instanceof Resource.Continuous) {
148 // non-aggregatable terminal node
149 nonAggregatable.add(r);
150 } else if (aggregatableTypes.contains(r.last().getClass())) {
151 // aggregatable & terminal node
152 aggregatables.put(r.last().getClass().getSimpleName(), r);
153 } else {
154 nonAggregatable.add(r);
155 }
156 }
157
158 // print aggregated (terminal)
159 aggregatables.asMap().entrySet()
160 .forEach(e -> {
161 // for each type...
162 String resourceName = e.getKey();
163
164 RangeSet<Long> rangeSet = TreeRangeSet.create();
165
166 // aggregate into RangeSet
167 e.getValue().stream()
168 .map(Resource::last)
169 .map(res -> {
170 if (res instanceof VlanId) {
171 return (long) ((VlanId) res).toShort();
172 } else if (res instanceof MplsLabel) {
173 return (long) ((MplsLabel) res).toInt();
174 } else if (res instanceof TributarySlot) {
175 return ((TributarySlot) res).index();
176 }
177 // TODO support Lambda (OchSignal types)
178 return 0L;
179 })
180 .map(Range::singleton)
181 .map(range -> range.canonical(DiscreteDomain.longs()))
182 .forEach(rangeSet::add);
183
184 print("%s%s: %s", Strings.repeat(" ", level + 1),
185 resourceName,
186 rangeSet);
187 });
188
189
190 // print non-aggregatables (recurse)
Naoki Shiota3f342182016-01-13 11:15:10 -0800191 if (sort) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800192 nonAggregatable.stream()
Naoki Shiota3f342182016-01-13 11:15:10 -0800193 .sorted((o1, o2) -> String.valueOf(o1.id()).compareTo(String.valueOf(o2.id())))
194 .forEach(r -> printResource(r, level + 1));
195 } else {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800196 nonAggregatable.forEach(r -> printResource(r, level + 1));
Naoki Shiota3f342182016-01-13 11:15:10 -0800197 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800198 }
199}