blob: 9d57f6626fbc98402763ed6277d709eda6585719 [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;
30import org.apache.karaf.shell.api.action.lifecycle.Service;
31import org.apache.karaf.shell.api.action.Option;
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -080032import org.onlab.packet.MplsLabel;
33import org.onlab.packet.VlanId;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080034import org.onosproject.cli.AbstractShellCommand;
35import org.onosproject.net.DeviceId;
36import org.onosproject.net.PortNumber;
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -080037import org.onosproject.net.TributarySlot;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080038import org.onosproject.net.resource.ContinuousResource;
39import org.onosproject.net.resource.DiscreteResource;
40import org.onosproject.net.resource.Resource;
41import org.onosproject.net.resource.Resources;
Sho SHIMIZUa6b4dc72016-03-11 19:00:20 -080042import org.onosproject.net.resource.ResourceQueryService;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080043
44import com.google.common.base.Strings;
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -080045import com.google.common.collect.ArrayListMultimap;
46import com.google.common.collect.DiscreteDomain;
47import com.google.common.collect.ImmutableSet;
48import com.google.common.collect.Multimap;
49import com.google.common.collect.Range;
50import com.google.common.collect.RangeSet;
51import com.google.common.collect.TreeRangeSet;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080052
53/**
Yuta HIGUCHIdd193712016-09-21 11:36:09 -070054 * Lists registered resources.
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080055 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070056@Service
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080057@Command(scope = "onos", name = "resources",
Yuta HIGUCHIdd193712016-09-21 11:36:09 -070058 description = "Lists registered resources")
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080059public class ResourcesCommand extends AbstractShellCommand {
60
Yuta HIGUCHIf5908c92016-09-22 16:35:44 -070061 @Option(name = "-a", aliases = "--available",
62 description = "Output available resources only",
63 required = false, multiValued = false)
64 boolean availablesOnly = false;
65
Naoki Shiota3f342182016-01-13 11:15:10 -080066 @Option(name = "-s", aliases = "--sort", description = "Sort output",
67 required = false, multiValued = false)
68 boolean sort = false;
69
70 @Option(name = "-t", aliases = "--typeStrings", description = "List of resource types to be printed",
71 required = false, multiValued = true)
72 String[] typeStrings = null;
73
74 Set<String> typesToPrint;
75
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080076 @Argument(index = 0, name = "deviceIdString", description = "Device ID",
77 required = false, multiValued = false)
78 String deviceIdStr = null;
79
80 @Argument(index = 1, name = "portNumberString", description = "PortNumber",
81 required = false, multiValued = false)
82 String portNumberStr = null;
83
84
Sho SHIMIZUa6b4dc72016-03-11 19:00:20 -080085 private ResourceQueryService resourceService;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080086
87 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070088 protected void doExecute() {
Sho SHIMIZUa6b4dc72016-03-11 19:00:20 -080089 resourceService = get(ResourceQueryService.class);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080090
Naoki Shiota3f342182016-01-13 11:15:10 -080091 if (typeStrings != null) {
92 typesToPrint = new HashSet<>(Arrays.asList(typeStrings));
93 } else {
94 typesToPrint = Collections.emptySet();
95 }
96
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080097 if (deviceIdStr != null && portNumberStr != null) {
98 DeviceId deviceId = deviceId(deviceIdStr);
99 PortNumber portNumber = PortNumber.fromString(portNumberStr);
100
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800101 printResource(Resources.discrete(deviceId, portNumber).resource(), 0);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800102 } else if (deviceIdStr != null) {
103 DeviceId deviceId = deviceId(deviceIdStr);
104
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800105 printResource(Resources.discrete(deviceId).resource(), 0);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800106 } else {
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800107 printResource(Resource.ROOT, 0);
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800108 }
109 }
110
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800111 private void printResource(Resource resource, int level) {
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800112 // workaround to preserve the original behavior of ResourceService#getRegisteredResources
113 Set<Resource> children;
114 if (resource instanceof DiscreteResource) {
115 children = resourceService.getRegisteredResources(((DiscreteResource) resource).id());
116 } else {
117 children = Collections.emptySet();
118 }
Naoki Shiota3f342182016-01-13 11:15:10 -0800119
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800120 if (resource.equals(Resource.ROOT)) {
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800121 print("ROOT");
122 } else {
Sho SHIMIZU5a8e8f92016-02-22 11:33:36 -0800123 String resourceName = resource.simpleTypeName();
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800124 if (resource instanceof ContinuousResource) {
Luca Pretede10c782017-01-05 17:23:08 -0800125 if (availablesOnly) {
126 // Get the total resource
127 double total = ((ContinuousResource) resource).value();
128 // Get allocated resource
129 double allocated = resourceService.getResourceAllocations(resource.id()).stream()
130 .mapToDouble(rA -> ((ContinuousResource) rA.resource()).value())
131 .sum();
132 // Difference
133 double difference = total - allocated;
134 print("%s%s: %f", Strings.repeat(" ", level),
135 resourceName, difference);
136 } else {
137 print("%s%s: %f", Strings.repeat(" ", level),
138 resourceName,
139 ((ContinuousResource) resource).value());
140 }
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800141 // Continuous resource is terminal node, stop here
142 return;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800143 } else {
Yuta HIGUCHIf5908c92016-09-22 16:35:44 -0700144 String availability = "";
145 if (availablesOnly && !children.isEmpty()) {
146 // intermediate nodes cannot be omitted, print availability
147 if (resourceService.isAvailable(resource)) {
148 availability = " ✔";
149 } else {
150 availability = " ✘";
151 }
152 }
Sho SHIMIZUf08cb4c2016-02-11 18:35:59 -0800153 String toString = String.valueOf(resource.valueAs(Object.class).orElse(""));
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800154 if (toString.startsWith(resourceName)) {
Yuta HIGUCHIf5908c92016-09-22 16:35:44 -0700155 print("%s%s%s", Strings.repeat(" ", level),
156 toString, availability);
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800157 } else {
Yuta HIGUCHIf5908c92016-09-22 16:35:44 -0700158 print("%s%s: %s%s", Strings.repeat(" ", level),
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800159 resourceName,
Yuta HIGUCHIf5908c92016-09-22 16:35:44 -0700160 toString, availability);
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800161 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800162 }
163 }
164
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800165
166 // Classify children into aggregatable terminal resources and everything else
167
168 Set<Class<?>> aggregatableTypes = ImmutableSet.<Class<?>>builder()
169 .add(VlanId.class)
170 .add(MplsLabel.class)
171 .build();
172 // (last() resource name) -> { Resource }
173 Multimap<String, Resource> aggregatables = ArrayListMultimap.create();
174 List<Resource> nonAggregatable = new ArrayList<>();
175
176 for (Resource r : children) {
Naoki Shiota88745922016-02-10 16:28:25 -0800177 if (!isPrintTarget(r)) {
178 continue;
179 }
180
Sho SHIMIZUf33b8932016-01-25 18:43:32 -0800181 if (r instanceof ContinuousResource) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800182 // non-aggregatable terminal node
183 nonAggregatable.add(r);
Sho SHIMIZU003ed322016-02-11 12:58:42 -0800184 } else if (Iterables.any(aggregatableTypes, r::isTypeOf)) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800185 // aggregatable & terminal node
Sho SHIMIZU5a8e8f92016-02-22 11:33:36 -0800186 String simpleName = r.simpleTypeName();
187 aggregatables.put(simpleName, r);
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800188 } else {
189 nonAggregatable.add(r);
190 }
191 }
192
193 // print aggregated (terminal)
194 aggregatables.asMap().entrySet()
195 .forEach(e -> {
196 // for each type...
197 String resourceName = e.getKey();
198
199 RangeSet<Long> rangeSet = TreeRangeSet.create();
200
201 // aggregate into RangeSet
202 e.getValue().stream()
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800203 .map(res -> {
Sho SHIMIZUf08cb4c2016-02-11 18:35:59 -0800204 if (res.isTypeOf(VlanId.class)) {
205 return (long) res.valueAs(VlanId.class).get().toShort();
206 } else if (res.isTypeOf(MplsLabel.class)) {
207 return (long) res.valueAs(MplsLabel.class).get().toInt();
208 } else if (res.isTypeOf(TributarySlot.class)) {
209 return res.valueAs(TributarySlot.class).get().index();
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800210 }
211 // TODO support Lambda (OchSignal types)
212 return 0L;
213 })
214 .map(Range::singleton)
215 .map(range -> range.canonical(DiscreteDomain.longs()))
216 .forEach(rangeSet::add);
217
218 print("%s%s: %s", Strings.repeat(" ", level + 1),
219 resourceName,
220 rangeSet);
221 });
222
223
224 // print non-aggregatables (recurse)
Naoki Shiota3f342182016-01-13 11:15:10 -0800225 if (sort) {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800226 nonAggregatable.stream()
Naoki Shiota3f342182016-01-13 11:15:10 -0800227 .sorted((o1, o2) -> String.valueOf(o1.id()).compareTo(String.valueOf(o2.id())))
228 .forEach(r -> printResource(r, level + 1));
229 } else {
HIGUCHI Yuta634df8f2016-01-20 18:18:01 -0800230 nonAggregatable.forEach(r -> printResource(r, level + 1));
Naoki Shiota3f342182016-01-13 11:15:10 -0800231 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800232 }
Naoki Shiota88745922016-02-10 16:28:25 -0800233
234 private boolean isPrintTarget(Resource resource) {
235 if (typesToPrint.isEmpty()) {
236 return true;
237 }
238
Sho SHIMIZU5a8e8f92016-02-22 11:33:36 -0800239 String resourceName = resource.simpleTypeName();
240 if (resource instanceof DiscreteResource) {
Naoki Shiota88745922016-02-10 16:28:25 -0800241 // TODO This distributed store access incurs overhead.
242 // This should be merged with the one in printResource()
243 if (!resourceService.getRegisteredResources(((DiscreteResource) resource).id()).isEmpty()) {
244 // resource which has children should be printed
245 return true;
246 }
Yuta HIGUCHIf5908c92016-09-22 16:35:44 -0700247 if (availablesOnly && !resourceService.isAvailable(resource)) {
248 // don't print unavailable discrete resource
249 return false;
250 }
Sho SHIMIZU5a8e8f92016-02-22 11:33:36 -0800251 } else if (!(resource instanceof ContinuousResource)) {
Naoki Shiota88745922016-02-10 16:28:25 -0800252 log.warn("Unexpected resource class: {}", resource.getClass().getSimpleName());
253 return false;
254 }
255
256 return typesToPrint.contains(resourceName);
257 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800258}