blob: 5e7b4fe194d0bc1c796ff2cce7af71de6e337bd3 [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 Shiota784af312016-02-01 15:47:21 -080020import java.util.Arrays;
21import java.util.Collections;
22import java.util.HashSet;
Yuta HIGUCHIb2ba8ab2016-09-21 19:45:41 -070023import java.util.List;
Naoki Shiota784af312016-02-01 15:47:21 -080024import java.util.Set;
Yuta HIGUCHIb2ba8ab2016-09-21 19:45:41 -070025import java.util.stream.Collectors;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080026import java.util.stream.StreamSupport;
27
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080028import com.google.common.base.Strings;
Naoki Shiota784af312016-02-01 15:47:21 -080029import com.google.common.collect.ImmutableSet;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070030import org.apache.karaf.shell.api.action.Argument;
31import org.apache.karaf.shell.api.action.Command;
Ray Milkey0068fd02018-10-11 15:45:39 -070032import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070033import org.apache.karaf.shell.api.action.lifecycle.Service;
34import org.apache.karaf.shell.api.action.Option;
Naoki Shiota784af312016-02-01 15:47:21 -080035import org.onlab.packet.MplsLabel;
36import org.onlab.packet.VlanId;
Naoki Shiotad7654d42016-03-31 18:46:49 -070037import org.onlab.util.Bandwidth;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080038import org.onosproject.cli.AbstractShellCommand;
39import org.onosproject.net.Device;
40import org.onosproject.net.DeviceId;
41import org.onosproject.net.OchSignal;
42import org.onosproject.net.Port;
43import org.onosproject.net.PortNumber;
Rimon Ashkenazyfbac3782016-05-02 14:01:41 +030044import org.onosproject.net.TributarySlot;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080045import org.onosproject.net.device.DeviceService;
Naoki Shiota784af312016-02-01 15:47:21 -080046import org.onosproject.net.intent.IntentId;
Naoki Shiotabd1974c2016-04-29 18:44:17 -070047import org.onosproject.net.resource.ResourceConsumerId;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080048import org.onosproject.net.resource.Resources;
49import org.onosproject.net.resource.DiscreteResourceId;
50import org.onosproject.net.resource.ResourceAllocation;
51import org.onosproject.net.resource.ResourceService;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080052
53/**
54 * Lists allocated resources.
55 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070056@Service
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080057@Command(scope = "onos", name = "allocations",
58 description = "Lists allocated resources")
59public class AllocationsCommand extends AbstractShellCommand {
60
Yuta HIGUCHIb2ba8ab2016-09-21 19:45:41 -070061 @Option(name = "-t", aliases = "--type",
62 description = "resource types to include in the list",
Naoki Shiota784af312016-02-01 15:47:21 -080063 required = false, multiValued = true)
64 String[] typeStrings = null;
65
66 Set<String> typesToPrint;
67
Yuta HIGUCHIb2ba8ab2016-09-21 19:45:41 -070068 @Option(name = "-i", aliases = "--intentId",
69 description = "Intent ID to include in the list",
Naoki Shiota784af312016-02-01 15:47:21 -080070 required = false, multiValued = true)
71 String[] intentStrings;
72
73 Set<String> intentsToPrint;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080074
75 @Argument(index = 0, name = "deviceIdString", description = "Device ID",
76 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070077 @Completion(DeviceIdCompleter.class)
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080078 String deviceIdStr = null;
79
80 @Argument(index = 1, name = "portNumberString", description = "PortNumber",
81 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070082 @Completion(PortNumberCompleter.class)
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080083 String portNumberStr = null;
84
85
86
87 private DeviceService deviceService;
88 private ResourceService resourceService;
89
90 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070091 protected void doExecute() {
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080092 deviceService = get(DeviceService.class);
93 resourceService = get(ResourceService.class);
94
Naoki Shiota784af312016-02-01 15:47:21 -080095 if (typeStrings != null) {
96 typesToPrint = new HashSet<>(Arrays.asList(typeStrings));
97 } else {
98 typesToPrint = Collections.emptySet();
99 }
100
101 if (intentStrings != null) {
102 intentsToPrint = new HashSet<>(Arrays.asList(intentStrings));
103 } else {
104 intentsToPrint = Collections.emptySet();
105 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800106
107 if (deviceIdStr != null && portNumberStr != null) {
108 DeviceId deviceId = deviceId(deviceIdStr);
109 PortNumber portNumber = PortNumber.fromString(portNumberStr);
110
111 printAllocation(deviceId, portNumber, 0);
112 } else if (deviceIdStr != null) {
113 DeviceId deviceId = deviceId(deviceIdStr);
114
115 printAllocation(deviceId, 0);
116 } else {
117 printAllocation();
118 }
119
120 }
121
122 private void printAllocation() {
123 print("ROOT");
124 StreamSupport.stream(deviceService.getAvailableDevices().spliterator(), false)
125 .map(Device::id)
126 .forEach(did -> printAllocation(did, 1));
127 }
128
129 private void printAllocation(DeviceId did, int level) {
130 print("%s%s", Strings.repeat(" ", level), did);
131 StreamSupport.stream(deviceService.getPorts(did).spliterator(), false)
132 .map(Port::number)
133 .forEach(num -> printAllocation(did, num, level + 1));
134 }
135
136 private void printAllocation(DeviceId did, PortNumber num, int level) {
137 if (level == 0) {
138 // print DeviceId when Port was directly specified.
139 print("%s", did);
140 }
Yuta HIGUCHIb2ba8ab2016-09-21 19:45:41 -0700141
142 DiscreteResourceId resourceId = Resources.discrete(did, num).id();
143
144 List<String> portConsumers = resourceService.getResourceAllocations(resourceId)
145 .stream()
146 .filter(this::isSubjectToPrint)
147 .map(ResourceAllocation::consumerId)
148 .map(AllocationsCommand::asVerboseString)
149 .collect(Collectors.toList());
150 if (portConsumers.isEmpty()) {
151 print("%s%s", Strings.repeat(" ", level), asVerboseString(num));
152 } else {
153 print("%s%s allocated by %s", Strings.repeat(" ", level), asVerboseString(num),
154 portConsumers);
155 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800156
Naoki Shiota784af312016-02-01 15:47:21 -0800157 // FIXME: This workaround induces a lot of distributed store access.
158 // ResourceService should have an API to get all allocations under a parent resource.
159 Set<Class<?>> subResourceTypes = ImmutableSet.<Class<?>>builder()
160 .add(OchSignal.class)
161 .add(VlanId.class)
162 .add(MplsLabel.class)
Naoki Shiotad7654d42016-03-31 18:46:49 -0700163 .add(Bandwidth.class)
Rimon Ashkenazyfbac3782016-05-02 14:01:41 +0300164 .add(TributarySlot.class)
Naoki Shiota784af312016-02-01 15:47:21 -0800165 .build();
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800166
Naoki Shiota784af312016-02-01 15:47:21 -0800167 for (Class<?> t : subResourceTypes) {
168 resourceService.getResourceAllocations(resourceId, t).stream()
169 .filter(a -> isSubjectToPrint(a))
170 .forEach(a -> print("%s%s allocated by %s", Strings.repeat(" ", level + 1),
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700171 a.resource().valueAs(Object.class).orElse(""), asVerboseString(a.consumerId())));
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800172
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800173 }
174 }
175
Naoki Shiota784af312016-02-01 15:47:21 -0800176 private boolean isSubjectToPrint(ResourceAllocation allocation) {
177 if (!intentsToPrint.isEmpty()
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700178 && allocation.consumerId().isClassOf(IntentId.class)
179 && !intentsToPrint.contains(allocation.consumerId().toString())) {
Naoki Shiota784af312016-02-01 15:47:21 -0800180 return false;
181 }
182
183 if (!typesToPrint.isEmpty()
184 && !typesToPrint.contains(allocation.resource().simpleTypeName())) {
185 return false;
186 }
187
188 return true;
189 }
190
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800191 /**
192 * Add type name if the toString does not start with them.
193 *
194 * e.g., IntentId#toString result in "42"
195 * asVerboseString(id) will result in "IntentId:42"
196 *
197 * @param obj non-null Object to print.
198 * @return verbose String representation
199 */
200 private static String asVerboseString(Object obj) {
201 String name = obj.getClass().getSimpleName();
202 String toString = String.valueOf(obj);
203 if (toString.startsWith(name)) {
204 return toString;
205 } else {
206 return String.format("%s:%s", name, toString);
207 }
208 }
209
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700210 private static String asVerboseString(ResourceConsumerId consumerId) {
211 return String.format("%s:%s", consumerId.consumerClass(), consumerId.value());
212 }
213
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800214}