blob: 1e24cb90bd202e1739e46ec64a3c8a2039e37963 [file] [log] [blame]
HIGUCHI Yuta848324f2015-12-04 21:11:26 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
HIGUCHI Yuta848324f2015-12-04 21:11:26 -08003 *
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 SHIMIZUe18cb122016-02-22 21:04:56 -080037import org.onosproject.net.resource.ContinuousResource;
38import org.onosproject.net.resource.DiscreteResource;
39import org.onosproject.net.resource.Resource;
40import org.onosproject.net.resource.Resources;
Sho SHIMIZUa6b4dc72016-03-11 19:00:20 -080041import org.onosproject.net.resource.ResourceQueryService;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080042
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/**
Yuta HIGUCHIdd193712016-09-21 11:36:09 -070053 * Lists registered resources.
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080054 */
55@Command(scope = "onos", name = "resources",
Yuta HIGUCHIdd193712016-09-21 11:36:09 -070056 description = "Lists registered resources")
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080057public class ResourcesCommand extends AbstractShellCommand {
58
Yuta HIGUCHIf5908c92016-09-22 16:35:44 -070059 @Option(name = "-a", aliases = "--available",
60 description = "Output available resources only",
61 required = false, multiValued = false)
62 boolean availablesOnly = false;
63
Naoki Shiota3f342182016-01-13 11:15:10 -080064 @Option(name = "-s", aliases = "--sort", description = "Sort output",
65 required = false, multiValued = false)
66 boolean sort = false;
67
68 @Option(name = "-t", aliases = "--typeStrings", description = "List of resource types to be printed",
69 required = false, multiValued = true)
70 String[] typeStrings = null;
71
72 Set<String> typesToPrint;
73
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080074 @Argument(index = 0, name = "deviceIdString", description = "Device ID",
75 required = false, multiValued = false)
76 String deviceIdStr = null;
77
78 @Argument(index = 1, name = "portNumberString", description = "PortNumber",
79 required = false, multiValued = false)
80 String portNumberStr = null;
81
82
Sho SHIMIZUa6b4dc72016-03-11 19:00:20 -080083 private ResourceQueryService resourceService;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080084
85 @Override
86 protected void execute() {
Sho SHIMIZUa6b4dc72016-03-11 19:00:20 -080087 resourceService = get(ResourceQueryService.class);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080088
Naoki Shiota3f342182016-01-13 11:15:10 -080089 if (typeStrings != null) {
90 typesToPrint = new HashSet<>(Arrays.asList(typeStrings));
91 } else {
92 typesToPrint = Collections.emptySet();
93 }
94
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080095 if (deviceIdStr != null && portNumberStr != null) {
96 DeviceId deviceId = deviceId(deviceIdStr);
97 PortNumber portNumber = PortNumber.fromString(portNumberStr);
98
Sho SHIMIZU460b9722016-01-28 10:48:26 -080099 printResource(Resources.discrete(deviceId, portNumber).resource(), 0);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800100 } else if (deviceIdStr != null) {
101 DeviceId deviceId = deviceId(deviceIdStr);
102
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800103 printResource(Resources.discrete(deviceId).resource(), 0);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800104 } else {
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800105 printResource(Resource.ROOT, 0);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800106 }
107 }
108
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800109 private void printResource(Resource resource, int level) {
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800110 // workaround to preserve the original behavior of ResourceService#getRegisteredResources
111 Set<Resource> children;
112 if (resource instanceof DiscreteResource) {
113 children = resourceService.getRegisteredResources(((DiscreteResource) resource).id());
114 } else {
115 children = Collections.emptySet();
116 }
Naoki Shiota3f342182016-01-13 11:15:10 -0800117
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800118 if (resource.equals(Resource.ROOT)) {
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800119 print("ROOT");
120 } else {
Sho SHIMIZU5a8e8f92016-02-22 11:33:36 -0800121 String resourceName = resource.simpleTypeName();
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800122 if (resource instanceof ContinuousResource) {
Luca Pretede10c782017-01-05 17:23:08 -0800123 if (availablesOnly) {
124 // Get the total resource
125 double total = ((ContinuousResource) resource).value();
126 // Get allocated resource
127 double allocated = resourceService.getResourceAllocations(resource.id()).stream()
128 .mapToDouble(rA -> ((ContinuousResource) rA.resource()).value())
129 .sum();
130 // Difference
131 double difference = total - allocated;
132 print("%s%s: %f", Strings.repeat(" ", level),
133 resourceName, difference);
134 } else {
135 print("%s%s: %f", Strings.repeat(" ", level),
136 resourceName,
137 ((ContinuousResource) resource).value());
138 }
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800139 // Continuous resource is terminal node, stop here
140 return;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800141 } else {
Yuta HIGUCHIf5908c92016-09-22 16:35:44 -0700142 String availability = "";
143 if (availablesOnly && !children.isEmpty()) {
144 // intermediate nodes cannot be omitted, print availability
145 if (resourceService.isAvailable(resource)) {
146 availability = " ✔";
147 } else {
148 availability = " ✘";
149 }
150 }
Sho SHIMIZUf08cb4c2016-02-11 18:35:59 -0800151 String toString = String.valueOf(resource.valueAs(Object.class).orElse(""));
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800152 if (toString.startsWith(resourceName)) {
Yuta HIGUCHIf5908c92016-09-22 16:35:44 -0700153 print("%s%s%s", Strings.repeat(" ", level),
154 toString, availability);
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800155 } else {
Yuta HIGUCHIf5908c92016-09-22 16:35:44 -0700156 print("%s%s: %s%s", Strings.repeat(" ", level),
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800157 resourceName,
Yuta HIGUCHIf5908c92016-09-22 16:35:44 -0700158 toString, availability);
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800159 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800160 }
161 }
162
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800163
164 // Classify children into aggregatable terminal resources and everything else
165
166 Set<Class<?>> aggregatableTypes = ImmutableSet.<Class<?>>builder()
167 .add(VlanId.class)
168 .add(MplsLabel.class)
169 .build();
170 // (last() resource name) -> { Resource }
171 Multimap<String, Resource> aggregatables = ArrayListMultimap.create();
172 List<Resource> nonAggregatable = new ArrayList<>();
173
174 for (Resource r : children) {
Naoki Shiota88745922016-02-10 16:28:25 -0800175 if (!isPrintTarget(r)) {
176 continue;
177 }
178
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800179 if (r instanceof ContinuousResource) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800180 // non-aggregatable terminal node
181 nonAggregatable.add(r);
Sho SHIMIZU003ed322016-02-11 12:58:42 -0800182 } else if (Iterables.any(aggregatableTypes, r::isTypeOf)) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800183 // aggregatable & terminal node
Sho SHIMIZU5a8e8f92016-02-22 11:33:36 -0800184 String simpleName = r.simpleTypeName();
185 aggregatables.put(simpleName, r);
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800186 } else {
187 nonAggregatable.add(r);
188 }
189 }
190
191 // print aggregated (terminal)
192 aggregatables.asMap().entrySet()
193 .forEach(e -> {
194 // for each type...
195 String resourceName = e.getKey();
196
197 RangeSet<Long> rangeSet = TreeRangeSet.create();
198
199 // aggregate into RangeSet
200 e.getValue().stream()
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800201 .map(res -> {
Sho SHIMIZUf08cb4c2016-02-11 18:35:59 -0800202 if (res.isTypeOf(VlanId.class)) {
203 return (long) res.valueAs(VlanId.class).get().toShort();
204 } else if (res.isTypeOf(MplsLabel.class)) {
205 return (long) res.valueAs(MplsLabel.class).get().toInt();
206 } else if (res.isTypeOf(TributarySlot.class)) {
207 return res.valueAs(TributarySlot.class).get().index();
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800208 }
209 // TODO support Lambda (OchSignal types)
210 return 0L;
211 })
212 .map(Range::singleton)
213 .map(range -> range.canonical(DiscreteDomain.longs()))
214 .forEach(rangeSet::add);
215
216 print("%s%s: %s", Strings.repeat(" ", level + 1),
217 resourceName,
218 rangeSet);
219 });
220
221
222 // print non-aggregatables (recurse)
Naoki Shiota3f342182016-01-13 11:15:10 -0800223 if (sort) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800224 nonAggregatable.stream()
Naoki Shiota3f342182016-01-13 11:15:10 -0800225 .sorted((o1, o2) -> String.valueOf(o1.id()).compareTo(String.valueOf(o2.id())))
226 .forEach(r -> printResource(r, level + 1));
227 } else {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800228 nonAggregatable.forEach(r -> printResource(r, level + 1));
Naoki Shiota3f342182016-01-13 11:15:10 -0800229 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800230 }
Naoki Shiota88745922016-02-10 16:28:25 -0800231
232 private boolean isPrintTarget(Resource resource) {
233 if (typesToPrint.isEmpty()) {
234 return true;
235 }
236
Sho SHIMIZU5a8e8f92016-02-22 11:33:36 -0800237 String resourceName = resource.simpleTypeName();
238 if (resource instanceof DiscreteResource) {
Naoki Shiota88745922016-02-10 16:28:25 -0800239 // TODO This distributed store access incurs overhead.
240 // This should be merged with the one in printResource()
241 if (!resourceService.getRegisteredResources(((DiscreteResource) resource).id()).isEmpty()) {
242 // resource which has children should be printed
243 return true;
244 }
Yuta HIGUCHIf5908c92016-09-22 16:35:44 -0700245 if (availablesOnly && !resourceService.isAvailable(resource)) {
246 // don't print unavailable discrete resource
247 return false;
248 }
Sho SHIMIZU5a8e8f92016-02-22 11:33:36 -0800249 } else if (!(resource instanceof ContinuousResource)) {
Naoki Shiota88745922016-02-10 16:28:25 -0800250 log.warn("Unexpected resource class: {}", resource.getClass().getSimpleName());
251 return false;
252 }
253
254 return typesToPrint.contains(resourceName);
255 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800256}