blob: de4a4dda6c7244be1fd8f1d61c303ffa95834534 [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;
32import org.apache.karaf.shell.api.action.lifecycle.Service;
33import org.apache.karaf.shell.api.action.Option;
Naoki Shiota784af312016-02-01 15:47:21 -080034import org.onlab.packet.MplsLabel;
35import org.onlab.packet.VlanId;
Naoki Shiotad7654d42016-03-31 18:46:49 -070036import org.onlab.util.Bandwidth;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080037import org.onosproject.cli.AbstractShellCommand;
38import org.onosproject.net.Device;
39import org.onosproject.net.DeviceId;
40import org.onosproject.net.OchSignal;
41import org.onosproject.net.Port;
42import org.onosproject.net.PortNumber;
Rimon Ashkenazyfbac3782016-05-02 14:01:41 +030043import org.onosproject.net.TributarySlot;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080044import org.onosproject.net.device.DeviceService;
Naoki Shiota784af312016-02-01 15:47:21 -080045import org.onosproject.net.intent.IntentId;
Naoki Shiotabd1974c2016-04-29 18:44:17 -070046import org.onosproject.net.resource.ResourceConsumerId;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080047import org.onosproject.net.resource.Resources;
48import org.onosproject.net.resource.DiscreteResourceId;
49import org.onosproject.net.resource.ResourceAllocation;
50import org.onosproject.net.resource.ResourceService;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080051
52/**
53 * Lists allocated resources.
54 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070055@Service
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080056@Command(scope = "onos", name = "allocations",
57 description = "Lists allocated resources")
58public class AllocationsCommand extends AbstractShellCommand {
59
Yuta HIGUCHIb2ba8ab2016-09-21 19:45:41 -070060 @Option(name = "-t", aliases = "--type",
61 description = "resource types to include in the list",
Naoki Shiota784af312016-02-01 15:47:21 -080062 required = false, multiValued = true)
63 String[] typeStrings = null;
64
65 Set<String> typesToPrint;
66
Yuta HIGUCHIb2ba8ab2016-09-21 19:45:41 -070067 @Option(name = "-i", aliases = "--intentId",
68 description = "Intent ID to include in the list",
Naoki Shiota784af312016-02-01 15:47:21 -080069 required = false, multiValued = true)
70 String[] intentStrings;
71
72 Set<String> intentsToPrint;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080073
74 @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
83
84 private DeviceService deviceService;
85 private ResourceService resourceService;
86
87 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070088 protected void doExecute() {
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080089 deviceService = get(DeviceService.class);
90 resourceService = get(ResourceService.class);
91
Naoki Shiota784af312016-02-01 15:47:21 -080092 if (typeStrings != null) {
93 typesToPrint = new HashSet<>(Arrays.asList(typeStrings));
94 } else {
95 typesToPrint = Collections.emptySet();
96 }
97
98 if (intentStrings != null) {
99 intentsToPrint = new HashSet<>(Arrays.asList(intentStrings));
100 } else {
101 intentsToPrint = Collections.emptySet();
102 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800103
104 if (deviceIdStr != null && portNumberStr != null) {
105 DeviceId deviceId = deviceId(deviceIdStr);
106 PortNumber portNumber = PortNumber.fromString(portNumberStr);
107
108 printAllocation(deviceId, portNumber, 0);
109 } else if (deviceIdStr != null) {
110 DeviceId deviceId = deviceId(deviceIdStr);
111
112 printAllocation(deviceId, 0);
113 } else {
114 printAllocation();
115 }
116
117 }
118
119 private void printAllocation() {
120 print("ROOT");
121 StreamSupport.stream(deviceService.getAvailableDevices().spliterator(), false)
122 .map(Device::id)
123 .forEach(did -> printAllocation(did, 1));
124 }
125
126 private void printAllocation(DeviceId did, int level) {
127 print("%s%s", Strings.repeat(" ", level), did);
128 StreamSupport.stream(deviceService.getPorts(did).spliterator(), false)
129 .map(Port::number)
130 .forEach(num -> printAllocation(did, num, level + 1));
131 }
132
133 private void printAllocation(DeviceId did, PortNumber num, int level) {
134 if (level == 0) {
135 // print DeviceId when Port was directly specified.
136 print("%s", did);
137 }
Yuta HIGUCHIb2ba8ab2016-09-21 19:45:41 -0700138
139 DiscreteResourceId resourceId = Resources.discrete(did, num).id();
140
141 List<String> portConsumers = resourceService.getResourceAllocations(resourceId)
142 .stream()
143 .filter(this::isSubjectToPrint)
144 .map(ResourceAllocation::consumerId)
145 .map(AllocationsCommand::asVerboseString)
146 .collect(Collectors.toList());
147 if (portConsumers.isEmpty()) {
148 print("%s%s", Strings.repeat(" ", level), asVerboseString(num));
149 } else {
150 print("%s%s allocated by %s", Strings.repeat(" ", level), asVerboseString(num),
151 portConsumers);
152 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800153
Naoki Shiota784af312016-02-01 15:47:21 -0800154 // FIXME: This workaround induces a lot of distributed store access.
155 // ResourceService should have an API to get all allocations under a parent resource.
156 Set<Class<?>> subResourceTypes = ImmutableSet.<Class<?>>builder()
157 .add(OchSignal.class)
158 .add(VlanId.class)
159 .add(MplsLabel.class)
Naoki Shiotad7654d42016-03-31 18:46:49 -0700160 .add(Bandwidth.class)
Rimon Ashkenazyfbac3782016-05-02 14:01:41 +0300161 .add(TributarySlot.class)
Naoki Shiota784af312016-02-01 15:47:21 -0800162 .build();
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800163
Naoki Shiota784af312016-02-01 15:47:21 -0800164 for (Class<?> t : subResourceTypes) {
165 resourceService.getResourceAllocations(resourceId, t).stream()
166 .filter(a -> isSubjectToPrint(a))
167 .forEach(a -> print("%s%s allocated by %s", Strings.repeat(" ", level + 1),
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700168 a.resource().valueAs(Object.class).orElse(""), asVerboseString(a.consumerId())));
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800169
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800170 }
171 }
172
Naoki Shiota784af312016-02-01 15:47:21 -0800173 private boolean isSubjectToPrint(ResourceAllocation allocation) {
174 if (!intentsToPrint.isEmpty()
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700175 && allocation.consumerId().isClassOf(IntentId.class)
176 && !intentsToPrint.contains(allocation.consumerId().toString())) {
Naoki Shiota784af312016-02-01 15:47:21 -0800177 return false;
178 }
179
180 if (!typesToPrint.isEmpty()
181 && !typesToPrint.contains(allocation.resource().simpleTypeName())) {
182 return false;
183 }
184
185 return true;
186 }
187
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800188 /**
189 * Add type name if the toString does not start with them.
190 *
191 * e.g., IntentId#toString result in "42"
192 * asVerboseString(id) will result in "IntentId:42"
193 *
194 * @param obj non-null Object to print.
195 * @return verbose String representation
196 */
197 private static String asVerboseString(Object obj) {
198 String name = obj.getClass().getSimpleName();
199 String toString = String.valueOf(obj);
200 if (toString.startsWith(name)) {
201 return toString;
202 } else {
203 return String.format("%s:%s", name, toString);
204 }
205 }
206
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700207 private static String asVerboseString(ResourceConsumerId consumerId) {
208 return String.format("%s:%s", consumerId.consumerClass(), consumerId.value());
209 }
210
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800211}