blob: d4c1a405c08d654422ccfd6cf9dc970cf67fb520 [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;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070028import org.apache.karaf.shell.api.action.Argument;
29import org.apache.karaf.shell.api.action.Command;
Ray Milkey0068fd02018-10-11 15:45:39 -070030import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070031import org.apache.karaf.shell.api.action.lifecycle.Service;
32import org.apache.karaf.shell.api.action.Option;
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -080033import org.onlab.packet.MplsLabel;
34import org.onlab.packet.VlanId;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080035import org.onosproject.cli.AbstractShellCommand;
36import org.onosproject.net.DeviceId;
37import org.onosproject.net.PortNumber;
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -080038import org.onosproject.net.TributarySlot;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080039import org.onosproject.net.resource.ContinuousResource;
40import org.onosproject.net.resource.DiscreteResource;
41import org.onosproject.net.resource.Resource;
42import org.onosproject.net.resource.Resources;
Sho SHIMIZUa6b4dc72016-03-11 19:00:20 -080043import org.onosproject.net.resource.ResourceQueryService;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080044
45import com.google.common.base.Strings;
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -080046import com.google.common.collect.ArrayListMultimap;
47import com.google.common.collect.DiscreteDomain;
48import com.google.common.collect.ImmutableSet;
49import com.google.common.collect.Multimap;
50import com.google.common.collect.Range;
51import com.google.common.collect.RangeSet;
52import com.google.common.collect.TreeRangeSet;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080053
54/**
Yuta HIGUCHIdd193712016-09-21 11:36:09 -070055 * Lists registered resources.
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080056 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070057@Service
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080058@Command(scope = "onos", name = "resources",
Yuta HIGUCHIdd193712016-09-21 11:36:09 -070059 description = "Lists registered resources")
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080060public class ResourcesCommand extends AbstractShellCommand {
61
Yuta HIGUCHIf5908c92016-09-22 16:35:44 -070062 @Option(name = "-a", aliases = "--available",
63 description = "Output available resources only",
64 required = false, multiValued = false)
65 boolean availablesOnly = false;
66
Naoki Shiota3f342182016-01-13 11:15:10 -080067 @Option(name = "-s", aliases = "--sort", description = "Sort output",
68 required = false, multiValued = false)
69 boolean sort = false;
70
71 @Option(name = "-t", aliases = "--typeStrings", description = "List of resource types to be printed",
72 required = false, multiValued = true)
73 String[] typeStrings = null;
74
75 Set<String> typesToPrint;
76
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080077 @Argument(index = 0, name = "deviceIdString", description = "Device ID",
78 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070079 @Completion(DeviceIdCompleter.class)
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080080 String deviceIdStr = null;
81
82 @Argument(index = 1, name = "portNumberString", description = "PortNumber",
83 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070084 @Completion(PortNumberCompleter.class)
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080085 String portNumberStr = null;
86
87
Sho SHIMIZUa6b4dc72016-03-11 19:00:20 -080088 private ResourceQueryService resourceService;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080089
90 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070091 protected void doExecute() {
Sho SHIMIZUa6b4dc72016-03-11 19:00:20 -080092 resourceService = get(ResourceQueryService.class);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080093
Naoki Shiota3f342182016-01-13 11:15:10 -080094 if (typeStrings != null) {
95 typesToPrint = new HashSet<>(Arrays.asList(typeStrings));
96 } else {
97 typesToPrint = Collections.emptySet();
98 }
99
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800100 if (deviceIdStr != null && portNumberStr != null) {
101 DeviceId deviceId = deviceId(deviceIdStr);
102 PortNumber portNumber = PortNumber.fromString(portNumberStr);
103
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800104 printResource(Resources.discrete(deviceId, portNumber).resource(), 0);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800105 } else if (deviceIdStr != null) {
106 DeviceId deviceId = deviceId(deviceIdStr);
107
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800108 printResource(Resources.discrete(deviceId).resource(), 0);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800109 } else {
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800110 printResource(Resource.ROOT, 0);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800111 }
112 }
113
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800114 private void printResource(Resource resource, int level) {
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800115 // workaround to preserve the original behavior of ResourceService#getRegisteredResources
116 Set<Resource> children;
117 if (resource instanceof DiscreteResource) {
118 children = resourceService.getRegisteredResources(((DiscreteResource) resource).id());
119 } else {
120 children = Collections.emptySet();
121 }
Naoki Shiota3f342182016-01-13 11:15:10 -0800122
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800123 if (resource.equals(Resource.ROOT)) {
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800124 print("ROOT");
125 } else {
Sho SHIMIZU5a8e8f92016-02-22 11:33:36 -0800126 String resourceName = resource.simpleTypeName();
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800127 if (resource instanceof ContinuousResource) {
Luca Pretede10c782017-01-05 17:23:08 -0800128 if (availablesOnly) {
129 // Get the total resource
130 double total = ((ContinuousResource) resource).value();
131 // Get allocated resource
132 double allocated = resourceService.getResourceAllocations(resource.id()).stream()
133 .mapToDouble(rA -> ((ContinuousResource) rA.resource()).value())
134 .sum();
135 // Difference
136 double difference = total - allocated;
137 print("%s%s: %f", Strings.repeat(" ", level),
138 resourceName, difference);
139 } else {
140 print("%s%s: %f", Strings.repeat(" ", level),
141 resourceName,
142 ((ContinuousResource) resource).value());
143 }
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800144 // Continuous resource is terminal node, stop here
145 return;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800146 } else {
Yuta HIGUCHIf5908c92016-09-22 16:35:44 -0700147 String availability = "";
148 if (availablesOnly && !children.isEmpty()) {
149 // intermediate nodes cannot be omitted, print availability
150 if (resourceService.isAvailable(resource)) {
151 availability = " ✔";
152 } else {
153 availability = " ✘";
154 }
155 }
Sho SHIMIZUf08cb4c2016-02-11 18:35:59 -0800156 String toString = String.valueOf(resource.valueAs(Object.class).orElse(""));
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800157 if (toString.startsWith(resourceName)) {
Yuta HIGUCHIf5908c92016-09-22 16:35:44 -0700158 print("%s%s%s", Strings.repeat(" ", level),
159 toString, availability);
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800160 } else {
Yuta HIGUCHIf5908c92016-09-22 16:35:44 -0700161 print("%s%s: %s%s", Strings.repeat(" ", level),
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800162 resourceName,
Yuta HIGUCHIf5908c92016-09-22 16:35:44 -0700163 toString, availability);
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800164 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800165 }
166 }
167
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800168
169 // Classify children into aggregatable terminal resources and everything else
170
171 Set<Class<?>> aggregatableTypes = ImmutableSet.<Class<?>>builder()
172 .add(VlanId.class)
173 .add(MplsLabel.class)
174 .build();
175 // (last() resource name) -> { Resource }
176 Multimap<String, Resource> aggregatables = ArrayListMultimap.create();
177 List<Resource> nonAggregatable = new ArrayList<>();
178
179 for (Resource r : children) {
Naoki Shiota88745922016-02-10 16:28:25 -0800180 if (!isPrintTarget(r)) {
181 continue;
182 }
183
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800184 if (r instanceof ContinuousResource) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800185 // non-aggregatable terminal node
186 nonAggregatable.add(r);
Sho SHIMIZU003ed322016-02-11 12:58:42 -0800187 } else if (Iterables.any(aggregatableTypes, r::isTypeOf)) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800188 // aggregatable & terminal node
Sho SHIMIZU5a8e8f92016-02-22 11:33:36 -0800189 String simpleName = r.simpleTypeName();
190 aggregatables.put(simpleName, r);
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800191 } else {
192 nonAggregatable.add(r);
193 }
194 }
195
196 // print aggregated (terminal)
197 aggregatables.asMap().entrySet()
198 .forEach(e -> {
199 // for each type...
200 String resourceName = e.getKey();
201
202 RangeSet<Long> rangeSet = TreeRangeSet.create();
203
204 // aggregate into RangeSet
205 e.getValue().stream()
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800206 .map(res -> {
Sho SHIMIZUf08cb4c2016-02-11 18:35:59 -0800207 if (res.isTypeOf(VlanId.class)) {
208 return (long) res.valueAs(VlanId.class).get().toShort();
209 } else if (res.isTypeOf(MplsLabel.class)) {
210 return (long) res.valueAs(MplsLabel.class).get().toInt();
211 } else if (res.isTypeOf(TributarySlot.class)) {
212 return res.valueAs(TributarySlot.class).get().index();
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800213 }
214 // TODO support Lambda (OchSignal types)
215 return 0L;
216 })
217 .map(Range::singleton)
218 .map(range -> range.canonical(DiscreteDomain.longs()))
219 .forEach(rangeSet::add);
220
221 print("%s%s: %s", Strings.repeat(" ", level + 1),
222 resourceName,
223 rangeSet);
224 });
225
226
227 // print non-aggregatables (recurse)
Naoki Shiota3f342182016-01-13 11:15:10 -0800228 if (sort) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800229 nonAggregatable.stream()
Naoki Shiota3f342182016-01-13 11:15:10 -0800230 .sorted((o1, o2) -> String.valueOf(o1.id()).compareTo(String.valueOf(o2.id())))
231 .forEach(r -> printResource(r, level + 1));
232 } else {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800233 nonAggregatable.forEach(r -> printResource(r, level + 1));
Naoki Shiota3f342182016-01-13 11:15:10 -0800234 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800235 }
Naoki Shiota88745922016-02-10 16:28:25 -0800236
237 private boolean isPrintTarget(Resource resource) {
238 if (typesToPrint.isEmpty()) {
239 return true;
240 }
241
Sho SHIMIZU5a8e8f92016-02-22 11:33:36 -0800242 String resourceName = resource.simpleTypeName();
243 if (resource instanceof DiscreteResource) {
Naoki Shiota88745922016-02-10 16:28:25 -0800244 // TODO This distributed store access incurs overhead.
245 // This should be merged with the one in printResource()
246 if (!resourceService.getRegisteredResources(((DiscreteResource) resource).id()).isEmpty()) {
247 // resource which has children should be printed
248 return true;
249 }
Yuta HIGUCHIf5908c92016-09-22 16:35:44 -0700250 if (availablesOnly && !resourceService.isAvailable(resource)) {
251 // don't print unavailable discrete resource
252 return false;
253 }
Sho SHIMIZU5a8e8f92016-02-22 11:33:36 -0800254 } else if (!(resource instanceof ContinuousResource)) {
Naoki Shiota88745922016-02-10 16:28:25 -0800255 log.warn("Unexpected resource class: {}", resource.getClass().getSimpleName());
256 return false;
257 }
258
259 return typesToPrint.contains(resourceName);
260 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800261}