blob: ab8b6deb85bab624417f08f66853d3fd220f0235 [file] [log] [blame]
HIGUCHI Yuta848324f2015-12-04 21:11:26 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080030import org.apache.karaf.shell.commands.Argument;
31import org.apache.karaf.shell.commands.Command;
32import org.apache.karaf.shell.commands.Option;
Naoki Shiota784af312016-02-01 15:47:21 -080033import org.onlab.packet.MplsLabel;
34import org.onlab.packet.VlanId;
Naoki Shiotad7654d42016-03-31 18:46:49 -070035import org.onlab.util.Bandwidth;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080036import org.onosproject.cli.AbstractShellCommand;
37import org.onosproject.net.Device;
38import org.onosproject.net.DeviceId;
39import org.onosproject.net.OchSignal;
40import org.onosproject.net.Port;
41import org.onosproject.net.PortNumber;
Rimon Ashkenazyfbac3782016-05-02 14:01:41 +030042import org.onosproject.net.TributarySlot;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080043import org.onosproject.net.device.DeviceService;
Naoki Shiota784af312016-02-01 15:47:21 -080044import org.onosproject.net.intent.IntentId;
Naoki Shiotabd1974c2016-04-29 18:44:17 -070045import org.onosproject.net.resource.ResourceConsumerId;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080046import org.onosproject.net.resource.Resources;
47import org.onosproject.net.resource.DiscreteResourceId;
48import org.onosproject.net.resource.ResourceAllocation;
49import org.onosproject.net.resource.ResourceService;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080050
51/**
52 * Lists allocated resources.
53 */
54@Command(scope = "onos", name = "allocations",
55 description = "Lists allocated resources")
56public class AllocationsCommand extends AbstractShellCommand {
57
Yuta HIGUCHIb2ba8ab2016-09-21 19:45:41 -070058 @Option(name = "-t", aliases = "--type",
59 description = "resource types to include in the list",
Naoki Shiota784af312016-02-01 15:47:21 -080060 required = false, multiValued = true)
61 String[] typeStrings = null;
62
63 Set<String> typesToPrint;
64
Yuta HIGUCHIb2ba8ab2016-09-21 19:45:41 -070065 @Option(name = "-i", aliases = "--intentId",
66 description = "Intent ID to include in the list",
Naoki Shiota784af312016-02-01 15:47:21 -080067 required = false, multiValued = true)
68 String[] intentStrings;
69
70 Set<String> intentsToPrint;
HIGUCHI Yuta848324f2015-12-04 21:11:26 -080071
72 @Argument(index = 0, name = "deviceIdString", description = "Device ID",
73 required = false, multiValued = false)
74 String deviceIdStr = null;
75
76 @Argument(index = 1, name = "portNumberString", description = "PortNumber",
77 required = false, multiValued = false)
78 String portNumberStr = null;
79
80
81
82 private DeviceService deviceService;
83 private ResourceService resourceService;
84
85 @Override
86 protected void execute() {
87 deviceService = get(DeviceService.class);
88 resourceService = get(ResourceService.class);
89
Naoki Shiota784af312016-02-01 15:47:21 -080090 if (typeStrings != null) {
91 typesToPrint = new HashSet<>(Arrays.asList(typeStrings));
92 } else {
93 typesToPrint = Collections.emptySet();
94 }
95
96 if (intentStrings != null) {
97 intentsToPrint = new HashSet<>(Arrays.asList(intentStrings));
98 } else {
99 intentsToPrint = Collections.emptySet();
100 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800101
102 if (deviceIdStr != null && portNumberStr != null) {
103 DeviceId deviceId = deviceId(deviceIdStr);
104 PortNumber portNumber = PortNumber.fromString(portNumberStr);
105
106 printAllocation(deviceId, portNumber, 0);
107 } else if (deviceIdStr != null) {
108 DeviceId deviceId = deviceId(deviceIdStr);
109
110 printAllocation(deviceId, 0);
111 } else {
112 printAllocation();
113 }
114
115 }
116
117 private void printAllocation() {
118 print("ROOT");
119 StreamSupport.stream(deviceService.getAvailableDevices().spliterator(), false)
120 .map(Device::id)
121 .forEach(did -> printAllocation(did, 1));
122 }
123
124 private void printAllocation(DeviceId did, int level) {
125 print("%s%s", Strings.repeat(" ", level), did);
126 StreamSupport.stream(deviceService.getPorts(did).spliterator(), false)
127 .map(Port::number)
128 .forEach(num -> printAllocation(did, num, level + 1));
129 }
130
131 private void printAllocation(DeviceId did, PortNumber num, int level) {
132 if (level == 0) {
133 // print DeviceId when Port was directly specified.
134 print("%s", did);
135 }
Yuta HIGUCHIb2ba8ab2016-09-21 19:45:41 -0700136
137 DiscreteResourceId resourceId = Resources.discrete(did, num).id();
138
139 List<String> portConsumers = resourceService.getResourceAllocations(resourceId)
140 .stream()
141 .filter(this::isSubjectToPrint)
142 .map(ResourceAllocation::consumerId)
143 .map(AllocationsCommand::asVerboseString)
144 .collect(Collectors.toList());
145 if (portConsumers.isEmpty()) {
146 print("%s%s", Strings.repeat(" ", level), asVerboseString(num));
147 } else {
148 print("%s%s allocated by %s", Strings.repeat(" ", level), asVerboseString(num),
149 portConsumers);
150 }
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800151
Naoki Shiota784af312016-02-01 15:47:21 -0800152 // FIXME: This workaround induces a lot of distributed store access.
153 // ResourceService should have an API to get all allocations under a parent resource.
154 Set<Class<?>> subResourceTypes = ImmutableSet.<Class<?>>builder()
155 .add(OchSignal.class)
156 .add(VlanId.class)
157 .add(MplsLabel.class)
Naoki Shiotad7654d42016-03-31 18:46:49 -0700158 .add(Bandwidth.class)
Rimon Ashkenazyfbac3782016-05-02 14:01:41 +0300159 .add(TributarySlot.class)
Naoki Shiota784af312016-02-01 15:47:21 -0800160 .build();
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800161
Naoki Shiota784af312016-02-01 15:47:21 -0800162 for (Class<?> t : subResourceTypes) {
163 resourceService.getResourceAllocations(resourceId, t).stream()
164 .filter(a -> isSubjectToPrint(a))
165 .forEach(a -> print("%s%s allocated by %s", Strings.repeat(" ", level + 1),
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700166 a.resource().valueAs(Object.class).orElse(""), asVerboseString(a.consumerId())));
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800167
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800168 }
169 }
170
Naoki Shiota784af312016-02-01 15:47:21 -0800171 private boolean isSubjectToPrint(ResourceAllocation allocation) {
172 if (!intentsToPrint.isEmpty()
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700173 && allocation.consumerId().isClassOf(IntentId.class)
174 && !intentsToPrint.contains(allocation.consumerId().toString())) {
Naoki Shiota784af312016-02-01 15:47:21 -0800175 return false;
176 }
177
178 if (!typesToPrint.isEmpty()
179 && !typesToPrint.contains(allocation.resource().simpleTypeName())) {
180 return false;
181 }
182
183 return true;
184 }
185
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800186 /**
187 * Add type name if the toString does not start with them.
188 *
189 * e.g., IntentId#toString result in "42"
190 * asVerboseString(id) will result in "IntentId:42"
191 *
192 * @param obj non-null Object to print.
193 * @return verbose String representation
194 */
195 private static String asVerboseString(Object obj) {
196 String name = obj.getClass().getSimpleName();
197 String toString = String.valueOf(obj);
198 if (toString.startsWith(name)) {
199 return toString;
200 } else {
201 return String.format("%s:%s", name, toString);
202 }
203 }
204
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700205 private static String asVerboseString(ResourceConsumerId consumerId) {
206 return String.format("%s:%s", consumerId.consumerClass(), consumerId.value());
207 }
208
HIGUCHI Yuta848324f2015-12-04 21:11:26 -0800209}